2 # an adapter on top of driver implementing AM API v2 to be AM API v3 compliant
5 from sfa.util.sfalogging import logger
6 from sfa.util.xrn import Xrn, urn_to_hrn, hrn_to_urn, get_leaf, get_authority
7 from sfa.util.cache import Cache
8 from sfa.rspecs.rspec import RSpec
9 from sfa.storage.model import SliverAllocation
10 # xxx 1-dbsession-per-request
11 from sfa.storage.alchemy import dbsession
15 def __init__ (self, config):
16 flavour = config.SFA_GENERIC_FLAVOUR
18 if flavour == "nitos":
19 from sfa.nitos.nitosdriver import NitosDriver
20 self.driver = NitosDriver(config)
22 from sfa.federica.fddriver import FdDriver
23 self.driver = FdDriver(config)
24 elif flavour == "dummy":
25 from sfa.dummy.dummydriver import DummyDriver
26 self.driver = DummyDriver(config)
27 elif flavour == "slab":
28 from sfa.senslab.slabdriver import SlabDriver
29 self.driver = SlabDriver(config)
31 logger.info("DriverV2Adapter unknown flavour !!!\n supported flavours: pl, nitos, fd, dummy, slab")
34 if config.SFA_AGGREGATE_CACHING:
36 self.cache = self.driver.cache
41 def __getattr__(self, name):
42 def func(*args, **kwds):
43 if name == "list_resources":
44 (version, options) = args
45 slice_urn = slice_hrn = None
47 rspec = getattr(self.driver, "list_resources")(slice_urn, slice_hrn, [], options)
50 elif name == "describe":
51 (urns, version, options) = args
53 slice_hrn, type = urn_to_hrn(slice_urn)
55 rspec = getattr(self.driver, "list_resources")(slice_urn, slice_hrn, creds, options)
58 if len(urns) == 1 and Xrn(xrn=urns[0]).type == 'slice':
59 constraint = SliverAllocation.slice_urn.in_(urns)
61 constraint = SliverAllocation.sliver_id.in_(urns)
63 sliver_allocations = dbsession.query (SliverAllocation).filter (constraint)
64 sliver_status = getattr(self.driver, "sliver_status")(slice_urn, slice_hrn)
65 if 'geni_expires' in sliver_status.keys():
66 geni_expires = sliver_status['geni_expires']
71 for sliver_allocation in sliver_allocations:
73 geni_sliver['geni_expires'] = geni_expires
74 geni_sliver['geni_allocation'] = sliver_allocation.allocation_state
75 geni_sliver['geni_sliver_urn'] = sliver_allocation.sliver_id
76 geni_sliver['geni_error'] = ''
77 if geni_sliver['geni_allocation'] == 'geni_allocated':
78 geni_sliver['geni_operational_status'] = 'geni_pending_allocation'
80 geni_sliver['geni_operational_status'] = 'geni_ready'
81 geni_slivers.append(geni_sliver)
84 result = {'geni_urn': slice_urn,
86 'geni_slivers': geni_slivers}
88 elif name == "allocate":
89 (slice_urn, rspec_string, expiration, options) = args
90 slice_hrn, type = urn_to_hrn(slice_urn)
92 users = options.get('sfa_users', [])
93 manifest_string = getattr(self.driver, "create_sliver")(slice_urn, slice_hrn, creds, rspec_string, users, options)
96 rspec = RSpec(manifest_string)
97 slivers = rspec.version.get_nodes_with_slivers()
100 for sliver in slivers:
101 client_id = sliver['client_id']
102 component_id = sliver['component_id']
103 component_name = sliver['component_name']
104 slice_name = slice_hrn.replace('.','-')
105 component_short_name = component_name.split('.')[0]
107 sliver_hrn = '%s.%s-%s' % (self.driver.hrn, slice_name, component_short_name)
108 sliver_id = Xrn(sliver_hrn, type='sliver').urn
109 record = SliverAllocation(sliver_id=sliver_id,
111 component_id=component_id,
112 slice_urn = slice_urn,
113 allocation_state='geni_allocated')
119 rspec_version = RSpec(rspec_string).version
120 rspec_version_str = "%s"%rspec_version
121 options['geni_rspec_version'] = {'version': rspec_version_str.split(' ')[1], 'type': rspec_version_str.lower().split(' ')[0]}
122 result = self.describe([slice_urn], rspec_version, options)
124 elif name == "provision":
125 (urns, options) = args
126 if len(urns) == 1 and Xrn(xrn=urns[0]).type == 'slice':
127 constraint = SliverAllocation.slice_urn.in_(urns)
129 constraint = SliverAllocation.sliver_id.in_(urns)
131 sliver_allocations = dbsession.query (SliverAllocation).filter(constraint)
132 for sliver_allocation in sliver_allocations:
133 sliver_allocation.allocation_state = 'geni_provisioned'
136 result = self.describe(urns, '', options)
138 elif name == "status":
141 options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'}
142 descr = self.describe(urns[0], '', options)
143 result = {'geni_urn': descr['geni_urn'],
144 'geni_slivers': descr['geni_slivers']}
146 elif name == "delete":
147 (urns, options) = args
149 slice_hrn, type = urn_to_hrn(slice_urn)
151 options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'}
152 descr = self.describe(urns, '', options)
154 for sliver_allocation in descr['geni_slivers']:
155 geni_sliver = {'geni_sliver_urn': sliver_allocation['geni_sliver_urn'],
156 'geni_allocation_status': 'geni_unallocated',
157 'geni_expires': sliver_allocation['geni_expires'],
158 'geni_error': sliver_allocation['geni_error']}
160 result.append(geni_sliver)
162 getattr(self.driver, "delete_sliver")(slice_urn, slice_hrn, creds, options)
165 constraints = SliverAllocation.slice_urn.in_(urns)
166 sliver_allocations = dbsession.query(SliverAllocation).filter(constraints)
167 sliver_ids = [sliver_allocation.sliver_id for sliver_allocation in sliver_allocations]
168 SliverAllocation.delete_allocations(sliver_ids)
171 elif name == "renew":
172 (urns, expiration_time, options) = args
174 slice_hrn, type = urn_to_hrn(slice_urn)
177 getattr(self.driver, "renew_sliver")(slice_urn, slice_hrn, creds, expiration_time, options)
179 options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'}
180 descr = self.describe(urns, '', options)
181 result = descr['geni_slivers']
184 elif name == "perform_operational_action":
185 (urns, action, options) = args
186 options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'}
187 result = self.describe(urns, '', options)['geni_slivers']
191 # same as v2 ( registry methods)
192 result=getattr(self.driver, name)(*args, **kwds)