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
18 from sfa.planetlab.pldriver import PlDriver
19 self.driver = PlDriver(config)
20 elif flavour == "nitos":
21 from sfa.nitos.nitosdriver import NitosDriver
22 self.driver = NitosDriver(config)
24 from sfa.federica.fddriver import FdDriver
25 self.driver = FdDriver(config)
26 elif flavour == "dummy":
27 from sfa.dummy.dummydriver import DummyDriver
28 self.driver = DummyDriver(config)
29 elif flavour == "slab":
30 from sfa.senslab.slabdriver import SlabDriver
31 self.driver = SlabDriver(config)
33 logger.info("DriverV2Adapter unknown flavour !!!\n supported flavours: pl, nitos, fd, dummy, slab")
36 if config.SFA_AGGREGATE_CACHING:
38 self.cache = self.driver.cache
43 def __getattr__(self, name):
44 def func(*args, **kwds):
45 if name == "list_resources":
46 (version, options) = args
47 slice_urn = slice_hrn = None
49 rspec = getattr(self.driver, "list_resources")(slice_urn, slice_hrn, [], options)
52 elif name == "describe":
53 (urns, version, options) = args
55 slice_hrn, type = urn_to_hrn(slice_urn)
57 rspec = getattr(self.driver, "list_resources")(slice_urn, slice_hrn, creds, options)
60 if len(urns) == 1 and Xrn(xrn=urns[0]).type == 'slice':
61 constraint = SliverAllocation.slice_urn.in_(urns)
63 constraint = SliverAllocation.sliver_id.in_(urns)
65 sliver_allocations = dbsession.query (SliverAllocation).filter (constraint)
66 sliver_status = getattr(self.driver, "sliver_status")(slice_urn, slice_hrn)
67 if 'geni_expires' in sliver_status.keys():
68 geni_expires = sliver_status['geni_expires']
73 for sliver_allocation in sliver_allocations:
75 geni_sliver['geni_expires'] = geni_expires
76 geni_sliver['geni_allocation'] = sliver_allocation.allocation_state
77 geni_sliver['geni_sliver_urn'] = sliver_allocation.sliver_id
78 geni_sliver['geni_error'] = ''
79 if geni_sliver['geni_allocation'] == 'geni_allocated':
80 geni_sliver['geni_operational_status'] = 'geni_pending_allocation'
82 geni_sliver['geni_operational_status'] = 'geni_ready'
83 geni_slivers.append(geni_sliver)
86 result = {'geni_urn': slice_urn,
88 'geni_slivers': geni_slivers}
90 elif name == "allocate":
91 (slice_urn, rspec_string, expiration, options) = args
92 slice_hrn, type = urn_to_hrn(slice_urn)
94 users = options.get('geni_users', [])
95 manifest_string = getattr(self.driver, "create_sliver")(slice_urn, slice_hrn, creds, rspec_string, users, options)
98 rspec = RSpec(manifest_string)
99 slivers = rspec.version.get_nodes_with_slivers()
102 for sliver in slivers:
103 client_id = sliver['client_id']
104 component_id = sliver['component_id']
105 component_name = sliver['component_name']
106 slice_name = slice_hrn.replace('.','-')
107 component_short_name = component_name.split('.')[0]
109 sliver_hrn = '%s.%s-%s' % (self.driver.hrn, slice_name, component_short_name)
110 sliver_id = Xrn(sliver_hrn, type='sliver').urn
111 record = SliverAllocation(sliver_id=sliver_id,
113 component_id=component_id,
114 slice_urn = slice_urn,
115 allocation_state='geni_allocated')
121 rspec_version = RSpec(rspec_string).version
122 rspec_version_str = "%s"%rspec_version
123 options['geni_rspec_version'] = {'version': rspec_version_str.split(' ')[1], 'type': rspec_version_str.lower().split(' ')[0]}
124 result = self.describe([slice_urn], rspec_version, options)
126 elif name == "provision":
127 (urns, options) = args
128 if len(urns) == 1 and Xrn(xrn=urns[0]).type == 'slice':
129 constraint = SliverAllocation.slice_urn.in_(urns)
131 constraint = SliverAllocation.sliver_id.in_(urns)
133 sliver_allocations = dbsession.query (SliverAllocation).filter(constraint)
134 for sliver_allocation in sliver_allocations:
135 sliver_allocation.allocation_state = 'geni_provisioned'
138 result = self.describe(urns, '', options)
140 elif name == "status":
143 options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'}
144 descr = self.describe(urns[0], '', options)
145 result = {'geni_urn': descr['geni_urn'],
146 'geni_slivers': descr['geni_slivers']}
148 elif name == "delete":
149 (urns, options) = args
151 slice_hrn, type = urn_to_hrn(slice_urn)
153 options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'}
154 descr = self.describe(urns, '', options)
156 for sliver_allocation in descr['geni_slivers']:
157 geni_sliver = {'geni_sliver_urn': sliver_allocation['geni_sliver_urn'],
158 'geni_allocation_status': 'geni_unallocated',
159 'geni_expires': sliver_allocation['geni_expires'],
160 'geni_error': sliver_allocation['geni_error']}
162 result.append(geni_sliver)
164 getattr(self.driver, "delete_sliver")(slice_urn, slice_hrn, creds, options)
167 constraints = SliverAllocation.slice_urn.in_(urns)
168 sliver_allocations = dbsession.query(SliverAllocation).filter(constraints)
169 sliver_ids = [sliver_allocation.sliver_id for sliver_allocation in sliver_allocations]
170 SliverAllocation.delete_allocations(sliver_ids)
173 elif name == "renew":
174 (urns, expiration_time, options) = args
176 slice_hrn, type = urn_to_hrn(slice_urn)
179 getattr(self.driver, "renew_sliver")(slice_urn, slice_hrn, creds, expiration_time, options)
181 options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'}
182 descr = self.describe(urns, '', options)
183 result = descr['geni_slivers']
186 elif name == "perform_operational_action":
187 (urns, action, options) = args
188 options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'}
189 result = self.describe(urns, '', options)['geni_slivers']
193 # same as v2 ( registry methods)
194 result=getattr(self.driver, name)(*args, **kwds)