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
13 def __init__ (self, api):
15 flavour = config.SFA_GENERIC_FLAVOUR
17 if flavour == "nitos":
18 from sfa.nitos.nitosdriver import NitosDriver
19 self.driver = NitosDriver(api)
21 from sfa.federica.fddriver import FdDriver
22 self.driver = FdDriver(api)
23 elif flavour == "iotlab":
24 from sfa.iotlab.iotlabdriver import IotlabDriver
25 self.driver = IotlabDriver(api)
27 logger.info("DriverV2Adapter unknown flavour !!!\n supported flavours: pl, nitos, fd, iotlab")
30 if config.SFA_AGGREGATE_CACHING:
32 self.cache = self.driver.cache
37 def __getattr__(self, name):
38 def func(*args, **kwds):
39 if name == "list_resources":
40 (version, options) = args
41 slice_urn = slice_hrn = None
43 rspec = getattr(self.driver, "list_resources")(slice_urn, slice_hrn, [], options)
46 elif name == "describe":
47 (urns, version, options) = args
49 slice_hrn, type = urn_to_hrn(slice_urn)
51 rspec = getattr(self.driver, "list_resources")(slice_urn, slice_hrn, creds, options)
54 if len(urns) == 1 and Xrn(xrn=urns[0]).type == 'slice':
55 constraint = SliverAllocation.slice_urn.in_(urns)
57 constraint = SliverAllocation.sliver_id.in_(urns)
59 sliver_allocations = self.driver.api.dbsession().query (SliverAllocation).filter (constraint)
60 sliver_status = getattr(self.driver, "sliver_status")(slice_urn, slice_hrn)
61 if 'geni_expires' in sliver_status.keys():
62 geni_expires = sliver_status['geni_expires']
67 for sliver_allocation in sliver_allocations:
69 geni_sliver['geni_expires'] = geni_expires
70 geni_sliver['geni_allocation'] = sliver_allocation.allocation_state
71 geni_sliver['geni_sliver_urn'] = sliver_allocation.sliver_id
72 geni_sliver['geni_error'] = ''
73 if geni_sliver['geni_allocation'] == 'geni_allocated':
74 geni_sliver['geni_operational_status'] = 'geni_pending_allocation'
76 geni_sliver['geni_operational_status'] = 'geni_ready'
77 geni_slivers.append(geni_sliver)
80 result = {'geni_urn': slice_urn,
82 'geni_slivers': geni_slivers}
84 elif name == "allocate":
85 (slice_urn, rspec_string, expiration, options) = args
86 slice_hrn, type = urn_to_hrn(slice_urn)
88 users = options.get('sfa_users', [])
89 manifest_string = getattr(self.driver, "create_sliver")(slice_urn, slice_hrn, creds, rspec_string, users, options)
92 rspec = RSpec(manifest_string)
93 slivers = rspec.version.get_nodes_with_slivers()
96 for sliver in slivers:
97 client_id = sliver['client_id']
98 component_id = sliver['component_id']
99 component_name = sliver['component_name']
100 slice_name = slice_hrn.replace('.','-')
101 component_short_name = component_name.split('.')[0]
103 sliver_hrn = '%s.%s-%s' % (self.driver.hrn, slice_name, component_short_name)
104 sliver_id = Xrn(sliver_hrn, type='sliver').urn
105 record = SliverAllocation(sliver_id=sliver_id,
107 component_id=component_id,
108 slice_urn = slice_urn,
109 allocation_state='geni_allocated')
115 rspec_version = RSpec(rspec_string).version
116 rspec_version_str = "%s"%rspec_version
117 options['geni_rspec_version'] = {'version': rspec_version_str.split(' ')[1], 'type': rspec_version_str.lower().split(' ')[0]}
118 result = self.describe([slice_urn], rspec_version, options)
120 elif name == "provision":
121 (urns, options) = args
122 if len(urns) == 1 and Xrn(xrn=urns[0]).type == 'slice':
123 constraint = SliverAllocation.slice_urn.in_(urns)
125 constraint = SliverAllocation.sliver_id.in_(urns)
127 dbsession = self.driver.api.dbsession()
128 sliver_allocations = dbsession.query (SliverAllocation).filter(constraint)
129 for sliver_allocation in sliver_allocations:
130 sliver_allocation.allocation_state = 'geni_provisioned'
133 result = self.describe(urns, '', options)
135 elif name == "status":
138 options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'}
139 descr = self.describe(urns[0], '', options)
140 result = {'geni_urn': descr['geni_urn'],
141 'geni_slivers': descr['geni_slivers']}
143 elif name == "delete":
144 (urns, options) = args
146 slice_hrn, type = urn_to_hrn(slice_urn)
148 options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'}
149 descr = self.describe(urns, '', options)
151 for sliver_allocation in descr['geni_slivers']:
152 geni_sliver = {'geni_sliver_urn': sliver_allocation['geni_sliver_urn'],
153 'geni_allocation_status': 'geni_unallocated',
154 'geni_expires': sliver_allocation['geni_expires'],
155 'geni_error': sliver_allocation['geni_error']}
157 result.append(geni_sliver)
159 getattr(self.driver, "delete_sliver")(slice_urn, slice_hrn, creds, options)
162 constraints = SliverAllocation.slice_urn.in_(urns)
163 dbsession = self.driver.api.dbsession()
164 sliver_allocations = dbsession.query(SliverAllocation).filter(constraints)
165 sliver_ids = [sliver_allocation.sliver_id for sliver_allocation in sliver_allocations]
166 SliverAllocation.delete_allocations(sliver_ids, dbsession)
169 elif name == "renew":
170 (urns, expiration_time, options) = args
172 slice_hrn, type = urn_to_hrn(slice_urn)
175 getattr(self.driver, "renew_sliver")(slice_urn, slice_hrn, creds, expiration_time, options)
177 options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'}
178 descr = self.describe(urns, '', options)
179 result = descr['geni_slivers']
182 elif name == "perform_operational_action":
183 (urns, action, options) = args
184 options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'}
185 result = self.describe(urns, '', options)['geni_slivers']
189 # same as v2 ( registry methods)
190 result=getattr(self.driver, name)(*args, **kwds)