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)
24 logger.error("V2ToV3Adapter: Unknown Flavour !!!\n Supported Flavours: nitos, fd")
27 if config.SFA_AGGREGATE_CACHING:
29 self.cache = self.driver.cache
34 def __getattr__(self, name):
35 def func(*args, **kwds):
36 if name == "list_resources":
37 (version, options) = args
38 slice_urn = slice_hrn = None
40 rspec = getattr(self.driver, "list_resources")(slice_urn, slice_hrn, [], options)
43 elif name == "describe":
44 (urns, version, options) = args
46 slice_hrn, type = urn_to_hrn(slice_urn)
48 rspec = getattr(self.driver, "list_resources")(slice_urn, slice_hrn, creds, options)
51 if len(urns) == 1 and Xrn(xrn=urns[0]).type == 'slice':
52 constraint = SliverAllocation.slice_urn.in_(urns)
54 constraint = SliverAllocation.sliver_id.in_(urns)
56 sliver_allocations = self.driver.api.dbsession().query (SliverAllocation).filter (constraint)
57 sliver_status = getattr(self.driver, "sliver_status")(slice_urn, slice_hrn)
58 if 'geni_expires' in sliver_status.keys():
59 geni_expires = sliver_status['geni_expires']
64 for sliver_allocation in sliver_allocations:
66 geni_sliver['geni_expires'] = geni_expires
67 geni_sliver['geni_allocation'] = sliver_allocation.allocation_state
68 geni_sliver['geni_sliver_urn'] = sliver_allocation.sliver_id
69 geni_sliver['geni_error'] = ''
70 if geni_sliver['geni_allocation'] == 'geni_allocated':
71 geni_sliver['geni_operational_status'] = 'geni_pending_allocation'
73 geni_sliver['geni_operational_status'] = 'geni_ready'
74 geni_slivers.append(geni_sliver)
77 result = {'geni_urn': slice_urn,
79 'geni_slivers': geni_slivers}
81 elif name == "allocate":
82 (slice_urn, rspec_string, expiration, options) = args
83 slice_hrn, type = urn_to_hrn(slice_urn)
85 users = options.get('sfa_users', [])
86 manifest_string = getattr(self.driver, "create_sliver")(slice_urn, slice_hrn, creds, rspec_string, users, options)
89 rspec = RSpec(manifest_string)
90 slivers = rspec.version.get_nodes_with_slivers()
93 for sliver in slivers:
94 client_id = sliver['client_id']
95 component_id = sliver['component_id']
96 component_name = sliver['component_name']
97 slice_name = slice_hrn.replace('.','-')
98 component_short_name = component_name.split('.')[0]
100 sliver_hrn = '%s.%s-%s' % (self.driver.hrn, slice_name, component_short_name)
101 sliver_id = Xrn(sliver_hrn, type='sliver').urn
102 record = SliverAllocation(sliver_id=sliver_id,
104 component_id=component_id,
105 slice_urn = slice_urn,
106 allocation_state='geni_allocated')
112 rspec_version = RSpec(rspec_string).version
113 rspec_version_str = "%s"%rspec_version
114 options['geni_rspec_version'] = {'version': rspec_version_str.split(' ')[1], 'type': rspec_version_str.lower().split(' ')[0]}
115 result = self.describe([slice_urn], rspec_version, options)
117 elif name == "provision":
118 (urns, options) = args
119 if len(urns) == 1 and Xrn(xrn=urns[0]).type == 'slice':
120 constraint = SliverAllocation.slice_urn.in_(urns)
122 constraint = SliverAllocation.sliver_id.in_(urns)
124 dbsession = self.driver.api.dbsession()
125 sliver_allocations = dbsession.query (SliverAllocation).filter(constraint)
126 for sliver_allocation in sliver_allocations:
127 sliver_allocation.allocation_state = 'geni_provisioned'
130 result = self.describe(urns, '', options)
132 elif name == "status":
135 options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'}
136 descr = self.describe(urns[0], '', options)
137 result = {'geni_urn': descr['geni_urn'],
138 'geni_slivers': descr['geni_slivers']}
140 elif name == "delete":
141 (urns, options) = args
143 slice_hrn, type = urn_to_hrn(slice_urn)
145 options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'}
146 descr = self.describe(urns, '', options)
148 for sliver_allocation in descr['geni_slivers']:
149 geni_sliver = {'geni_sliver_urn': sliver_allocation['geni_sliver_urn'],
150 'geni_allocation_status': 'geni_unallocated',
151 'geni_expires': sliver_allocation['geni_expires'],
152 'geni_error': sliver_allocation['geni_error']}
154 result.append(geni_sliver)
156 getattr(self.driver, "delete_sliver")(slice_urn, slice_hrn, creds, options)
159 constraints = SliverAllocation.slice_urn.in_(urns)
160 dbsession = self.driver.api.dbsession()
161 sliver_allocations = dbsession.query(SliverAllocation).filter(constraints)
162 sliver_ids = [sliver_allocation.sliver_id for sliver_allocation in sliver_allocations]
163 SliverAllocation.delete_allocations(sliver_ids, dbsession)
166 elif name == "renew":
167 (urns, expiration_time, options) = args
169 slice_hrn, type = urn_to_hrn(slice_urn)
172 getattr(self.driver, "renew_sliver")(slice_urn, slice_hrn, creds, expiration_time, options)
174 options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'}
175 descr = self.describe(urns, '', options)
176 result = descr['geni_slivers']
179 elif name == "perform_operational_action":
180 (urns, action, options) = args
181 options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'}
182 result = self.describe(urns, '', options)['geni_slivers']
186 # same as v2 ( registry methods)
187 result=getattr(self.driver, name)(*args, **kwds)