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 from sfa.storage.alchemy import dbsession
14 def __init__ (self, config):
15 flavour = config.SFA_GENERIC_FLAVOUR
17 if flavour == "nitos":
18 from sfa.nitos.nitosdriver import NitosDriver
19 self.driver = NitosDriver(config)
21 from sfa.federica.fddriver import FdDriver
22 self.driver = FdDriver(config)
23 elif flavour == "dummy":
24 from sfa.dummy.dummydriver import DummyDriver
25 self.driver = DummyDriver(config)
26 elif flavour == "slab":
27 from sfa.senslab.slabdriver import SlabDriver
28 self.driver = SlabDriver(config)
30 logger.info("DriverV2Adapter unknown flavour !!!\n supported flavours: pl, nitos, fd, dummy, slab")
33 if config.SFA_AGGREGATE_CACHING:
35 self.cache = self.driver.cache
40 def __getattr__(self, name):
41 def func(*args, **kwds):
42 if name == "list_resources":
43 (version, options) = args
44 slice_urn = slice_hrn = None
46 rspec = getattr(self.driver, "list_resources")(slice_urn, slice_hrn, [], options)
49 elif name == "describe":
50 (urns, version, options) = args
52 slice_hrn, type = urn_to_hrn(slice_urn)
54 rspec = getattr(self.driver, "list_resources")(slice_urn, slice_hrn, creds, options)
57 if len(urns) == 1 and Xrn(xrn=urns[0]).type == 'slice':
58 constraint = SliverAllocation.slice_urn.in_(urns)
60 constraint = SliverAllocation.sliver_id.in_(urns)
62 sliver_allocations = dbsession.query (SliverAllocation).filter (constraint)
63 sliver_status = getattr(self.driver, "sliver_status")(slice_urn, slice_hrn)
64 if 'geni_expires' in sliver_status.keys():
65 geni_expires = sliver_status['geni_expires']
70 for sliver_allocation in sliver_allocations:
72 geni_sliver['geni_expires'] = geni_expires
73 geni_sliver['geni_allocation'] = sliver_allocation.allocation_state
74 geni_sliver['geni_sliver_urn'] = sliver_allocation.sliver_id
75 geni_sliver['geni_error'] = ''
76 if geni_sliver['geni_allocation'] == 'geni_allocated':
77 geni_sliver['geni_operational_status'] = 'geni_pending_allocation'
79 geni_sliver['geni_operational_status'] = 'geni_ready'
80 geni_slivers.append(geni_sliver)
83 result = {'geni_urn': slice_urn,
85 'geni_slivers': geni_slivers}
87 elif name == "allocate":
88 (slice_urn, rspec_string, expiration, options) = args
89 slice_hrn, type = urn_to_hrn(slice_urn)
91 users = options.get('sfa_users', [])
92 manifest_string = getattr(self.driver, "create_sliver")(slice_urn, slice_hrn, creds, rspec_string, users, options)
95 rspec = RSpec(manifest_string)
96 slivers = rspec.version.get_nodes_with_slivers()
99 for sliver in slivers:
100 client_id = sliver['client_id']
101 component_id = sliver['component_id']
102 component_name = sliver['component_name']
103 slice_name = slice_hrn.replace('.','-')
104 component_short_name = component_name.split('.')[0]
106 sliver_hrn = '%s.%s-%s' % (self.driver.hrn, slice_name, component_short_name)
107 sliver_id = Xrn(sliver_hrn, type='sliver').urn
108 record = SliverAllocation(sliver_id=sliver_id,
110 component_id=component_id,
111 slice_urn = slice_urn,
112 allocation_state='geni_allocated')
118 rspec_version = RSpec(rspec_string).version
119 rspec_version_str = "%s"%rspec_version
120 options['geni_rspec_version'] = {'version': rspec_version_str.split(' ')[1], 'type': rspec_version_str.lower().split(' ')[0]}
121 result = self.describe([slice_urn], rspec_version, options)
123 elif name == "provision":
124 (urns, options) = args
125 if len(urns) == 1 and Xrn(xrn=urns[0]).type == 'slice':
126 constraint = SliverAllocation.slice_urn.in_(urns)
128 constraint = SliverAllocation.sliver_id.in_(urns)
130 sliver_allocations = dbsession.query (SliverAllocation).filter(constraint)
131 for sliver_allocation in sliver_allocations:
132 sliver_allocation.allocation_state = 'geni_provisioned'
135 result = self.describe(urns, '', options)
137 elif name == "status":
140 options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'}
141 descr = self.describe(urns[0], '', options)
142 result = {'geni_urn': descr['geni_urn'],
143 'geni_slivers': descr['geni_slivers']}
145 elif name == "delete":
146 (urns, options) = args
148 slice_hrn, type = urn_to_hrn(slice_urn)
150 options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'}
151 descr = self.describe(urns, '', options)
153 for sliver_allocation in descr['geni_slivers']:
154 geni_sliver = {'geni_sliver_urn': sliver_allocation['geni_sliver_urn'],
155 'geni_allocation_status': 'geni_unallocated',
156 'geni_expires': sliver_allocation['geni_expires'],
157 'geni_error': sliver_allocation['geni_error']}
159 result.append(geni_sliver)
161 getattr(self.driver, "delete_sliver")(slice_urn, slice_hrn, creds, options)
164 constraints = SliverAllocation.slice_urn.in_(urns)
165 sliver_allocations = dbsession.query(SliverAllocation).filter(constraints)
166 sliver_ids = [sliver_allocation.sliver_id for sliver_allocation in sliver_allocations]
167 SliverAllocation.delete_allocations(sliver_ids)
170 elif name == "renew":
171 (urns, expiration_time, options) = args
173 slice_hrn, type = urn_to_hrn(slice_urn)
176 getattr(self.driver, "renew_sliver")(slice_urn, slice_hrn, creds, expiration_time, options)
178 options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'}
179 descr = self.describe(urns, '', options)
180 result = descr['geni_slivers']
183 elif name == "perform_operational_action":
184 (urns, action, options) = args
185 options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'}
186 result = self.describe(urns, '', options)['geni_slivers']
190 # same as v2 ( registry methods)
191 result=getattr(self.driver, name)(*args, **kwds)