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
14 def __init__(self, api):
16 flavour = config.SFA_GENERIC_FLAVOUR
18 if flavour == "nitos":
19 from sfa.nitos.nitosdriver import NitosDriver
20 self.driver = NitosDriver(api)
23 "V2ToV3Adapter: Unknown Flavour !!!\n Supported Flavours: nitos")
26 if config.SFA_AGGREGATE_CACHING:
28 self.cache = self.driver.cache
32 def __getattr__(self, name):
33 def func(*args, **kwds):
34 if name == "list_resources":
35 (version, options) = args
36 slice_urn = slice_hrn = None
38 rspec = getattr(self.driver, "list_resources")(
39 slice_urn, slice_hrn, [], options)
42 elif name == "describe":
43 (urns, version, options) = args
45 slice_hrn, type = urn_to_hrn(slice_urn)
47 rspec = getattr(self.driver, "list_resources")(
48 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(
57 SliverAllocation).filter(constraint)
58 sliver_status = getattr(self.driver, "sliver_status")(
60 if 'geni_expires' in sliver_status.keys():
61 geni_expires = sliver_status['geni_expires']
66 for sliver_allocation in sliver_allocations:
68 geni_sliver['geni_expires'] = geni_expires
70 'geni_allocation'] = sliver_allocation.allocation_state
72 'geni_sliver_urn'] = sliver_allocation.sliver_id
73 geni_sliver['geni_error'] = ''
74 if geni_sliver['geni_allocation'] == 'geni_allocated':
76 'geni_operational_status'] = 'geni_pending_allocation'
78 geni_sliver['geni_operational_status'] = 'geni_ready'
79 geni_slivers.append(geni_sliver)
81 result = {'geni_urn': slice_urn,
83 'geni_slivers': geni_slivers}
85 elif name == "allocate":
86 (slice_urn, rspec_string, expiration, options) = args
87 slice_hrn, type = urn_to_hrn(slice_urn)
89 users = options.get('sfa_users', [])
90 manifest_string = getattr(self.driver, "create_sliver")(
91 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,
106 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,
112 allocation_state='geni_allocated')
114 record.sync(self.driver.api.dbsession())
117 rspec_version = RSpec(rspec_string).version
118 rspec_version_str = "%s" % rspec_version
119 options['geni_rspec_version'] = {'version': rspec_version_str.split(
120 ' ')[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 dbsession = self.driver.api.dbsession()
131 sliver_allocations = dbsession.query(
132 SliverAllocation).filter(constraint)
133 for sliver_allocation in sliver_allocations:
134 sliver_allocation.allocation_state = 'geni_provisioned'
137 result = self.describe(urns, '', options)
139 elif name == "status":
142 options['geni_rspec_version'] = {
143 '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'] = {
154 'version': '3', 'type': 'GENI'}
155 descr = self.describe(urns, '', options)
157 for sliver_allocation in descr['geni_slivers']:
158 geni_sliver = {'geni_sliver_urn': sliver_allocation['geni_sliver_urn'],
159 'geni_allocation_status': 'geni_unallocated',
160 'geni_expires': sliver_allocation['geni_expires'],
161 'geni_error': sliver_allocation['geni_error']}
163 result.append(geni_sliver)
165 getattr(self.driver, "delete_sliver")(
166 slice_urn, slice_hrn, creds, options)
169 constraints = SliverAllocation.slice_urn.in_(urns)
170 dbsession = self.driver.api.dbsession()
171 sliver_allocations = dbsession.query(
172 SliverAllocation).filter(constraints)
174 sliver_allocation.sliver_id for sliver_allocation in sliver_allocations]
175 SliverAllocation.delete_allocations(sliver_ids, dbsession)
177 elif name == "renew":
178 (urns, expiration_time, options) = args
180 slice_hrn, type = urn_to_hrn(slice_urn)
183 getattr(self.driver, "renew_sliver")(
184 slice_urn, slice_hrn, creds, expiration_time, options)
186 options['geni_rspec_version'] = {
187 'version': '3', 'type': 'GENI'}
188 descr = self.describe(urns, '', options)
189 result = descr['geni_slivers']
191 elif name == "perform_operational_action":
192 (urns, action, options) = args
193 options['geni_rspec_version'] = {
194 'version': '3', 'type': 'GENI'}
195 result = self.describe(urns, '', options)['geni_slivers']
198 # same as v2 ( registry methods)
199 result = getattr(self.driver, name)(*args, **kwds)