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, api):
17 flavour = config.SFA_GENERIC_FLAVOUR
19 if flavour == "nitos":
20 from sfa.nitos.nitosdriver import NitosDriver
21 self.driver = NitosDriver(api)
23 from sfa.federica.fddriver import FdDriver
24 self.driver = FdDriver(api)
25 elif flavour == "iotlab":
26 from sfa.iotlab.iotlabdriver import IotlabDriver
27 self.driver = IotlabDriver(api)
29 logger.info("DriverV2Adapter unknown flavour !!!\n supported flavours: pl, nitos, fd, iotlab")
32 if config.SFA_AGGREGATE_CACHING:
34 self.cache = self.driver.cache
39 def __getattr__(self, name):
40 def func(*args, **kwds):
41 if name == "list_resources":
42 (version, options) = args
43 slice_urn = slice_hrn = None
45 rspec = getattr(self.driver, "list_resources")(slice_urn, slice_hrn, [], options)
48 elif name == "describe":
49 (urns, version, options) = args
51 slice_hrn, type = urn_to_hrn(slice_urn)
53 rspec = getattr(self.driver, "list_resources")(slice_urn, slice_hrn, creds, options)
56 if len(urns) == 1 and Xrn(xrn=urns[0]).type == 'slice':
57 constraint = SliverAllocation.slice_urn.in_(urns)
59 constraint = SliverAllocation.sliver_id.in_(urns)
61 sliver_allocations = dbsession.query (SliverAllocation).filter (constraint)
62 sliver_status = getattr(self.driver, "sliver_status")(slice_urn, slice_hrn)
63 if 'geni_expires' in sliver_status.keys():
64 geni_expires = sliver_status['geni_expires']
69 for sliver_allocation in sliver_allocations:
71 geni_sliver['geni_expires'] = geni_expires
72 geni_sliver['geni_allocation'] = sliver_allocation.allocation_state
73 geni_sliver['geni_sliver_urn'] = sliver_allocation.sliver_id
74 geni_sliver['geni_error'] = ''
75 if geni_sliver['geni_allocation'] == 'geni_allocated':
76 geni_sliver['geni_operational_status'] = 'geni_pending_allocation'
78 geni_sliver['geni_operational_status'] = 'geni_ready'
79 geni_slivers.append(geni_sliver)
82 result = {'geni_urn': slice_urn,
84 'geni_slivers': geni_slivers}
86 elif name == "allocate":
87 (slice_urn, rspec_string, expiration, options) = args
88 slice_hrn, type = urn_to_hrn(slice_urn)
90 users = options.get('sfa_users', [])
91 manifest_string = getattr(self.driver, "create_sliver")(slice_urn, slice_hrn, creds, rspec_string, users, options)
94 rspec = RSpec(manifest_string)
95 slivers = rspec.version.get_nodes_with_slivers()
98 for sliver in slivers:
99 client_id = sliver['client_id']
100 component_id = sliver['component_id']
101 component_name = sliver['component_name']
102 slice_name = slice_hrn.replace('.','-')
103 component_short_name = component_name.split('.')[0]
105 sliver_hrn = '%s.%s-%s' % (self.driver.hrn, slice_name, component_short_name)
106 sliver_id = Xrn(sliver_hrn, type='sliver').urn
107 record = SliverAllocation(sliver_id=sliver_id,
109 component_id=component_id,
110 slice_urn = slice_urn,
111 allocation_state='geni_allocated')
117 rspec_version = RSpec(rspec_string).version
118 rspec_version_str = "%s"%rspec_version
119 options['geni_rspec_version'] = {'version': rspec_version_str.split(' ')[1], 'type': rspec_version_str.lower().split(' ')[0]}
120 result = self.describe([slice_urn], rspec_version, options)
122 elif name == "provision":
123 (urns, options) = args
124 if len(urns) == 1 and Xrn(xrn=urns[0]).type == 'slice':
125 constraint = SliverAllocation.slice_urn.in_(urns)
127 constraint = SliverAllocation.sliver_id.in_(urns)
129 sliver_allocations = dbsession.query (SliverAllocation).filter(constraint)
130 for sliver_allocation in sliver_allocations:
131 sliver_allocation.allocation_state = 'geni_provisioned'
134 result = self.describe(urns, '', options)
136 elif name == "status":
139 options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'}
140 descr = self.describe(urns[0], '', options)
141 result = {'geni_urn': descr['geni_urn'],
142 'geni_slivers': descr['geni_slivers']}
144 elif name == "delete":
145 (urns, options) = args
147 slice_hrn, type = urn_to_hrn(slice_urn)
149 options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'}
150 descr = self.describe(urns, '', options)
152 for sliver_allocation in descr['geni_slivers']:
153 geni_sliver = {'geni_sliver_urn': sliver_allocation['geni_sliver_urn'],
154 'geni_allocation_status': 'geni_unallocated',
155 'geni_expires': sliver_allocation['geni_expires'],
156 'geni_error': sliver_allocation['geni_error']}
158 result.append(geni_sliver)
160 getattr(self.driver, "delete_sliver")(slice_urn, slice_hrn, creds, options)
163 constraints = SliverAllocation.slice_urn.in_(urns)
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)
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)