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)
22 from sfa.federica.fddriver import FdDriver
23 self.driver = FdDriver(api)
26 "V2ToV3Adapter: Unknown Flavour !!!\n Supported Flavours: nitos, fd")
29 if config.SFA_AGGREGATE_CACHING:
31 self.cache = self.driver.cache
35 def __getattr__(self, name):
36 def func(*args, **kwds):
37 if name == "list_resources":
38 (version, options) = args
39 slice_urn = slice_hrn = None
41 rspec = getattr(self.driver, "list_resources")(
42 slice_urn, slice_hrn, [], options)
45 elif name == "describe":
46 (urns, version, options) = args
48 slice_hrn, type = urn_to_hrn(slice_urn)
50 rspec = getattr(self.driver, "list_resources")(
51 slice_urn, slice_hrn, creds, options)
54 if len(urns) == 1 and Xrn(xrn=urns[0]).type == 'slice':
55 constraint = SliverAllocation.slice_urn.in_(urns)
57 constraint = SliverAllocation.sliver_id.in_(urns)
59 sliver_allocations = self.driver.api.dbsession().query(
60 SliverAllocation).filter(constraint)
61 sliver_status = getattr(self.driver, "sliver_status")(
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
73 'geni_allocation'] = sliver_allocation.allocation_state
75 'geni_sliver_urn'] = sliver_allocation.sliver_id
76 geni_sliver['geni_error'] = ''
77 if geni_sliver['geni_allocation'] == 'geni_allocated':
79 'geni_operational_status'] = 'geni_pending_allocation'
81 geni_sliver['geni_operational_status'] = 'geni_ready'
82 geni_slivers.append(geni_sliver)
84 result = {'geni_urn': slice_urn,
86 'geni_slivers': geni_slivers}
88 elif name == "allocate":
89 (slice_urn, rspec_string, expiration, options) = args
90 slice_hrn, type = urn_to_hrn(slice_urn)
92 users = options.get('sfa_users', [])
93 manifest_string = getattr(self.driver, "create_sliver")(
94 slice_urn, slice_hrn, creds, rspec_string, users, options)
97 rspec = RSpec(manifest_string)
98 slivers = rspec.version.get_nodes_with_slivers()
101 for sliver in slivers:
102 client_id = sliver['client_id']
103 component_id = sliver['component_id']
104 component_name = sliver['component_name']
105 slice_name = slice_hrn.replace('.', '-')
106 component_short_name = component_name.split('.')[0]
108 sliver_hrn = '%s.%s-%s' % (self.driver.hrn,
109 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,
115 allocation_state='geni_allocated')
117 record.sync(self.driver.api.dbsession())
120 rspec_version = RSpec(rspec_string).version
121 rspec_version_str = "%s" % rspec_version
122 options['geni_rspec_version'] = {'version': rspec_version_str.split(
123 ' ')[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 dbsession = self.driver.api.dbsession()
134 sliver_allocations = dbsession.query(
135 SliverAllocation).filter(constraint)
136 for sliver_allocation in sliver_allocations:
137 sliver_allocation.allocation_state = 'geni_provisioned'
140 result = self.describe(urns, '', options)
142 elif name == "status":
145 options['geni_rspec_version'] = {
146 'version': '3', 'type': 'GENI'}
147 descr = self.describe(urns[0], '', options)
148 result = {'geni_urn': descr['geni_urn'],
149 'geni_slivers': descr['geni_slivers']}
151 elif name == "delete":
152 (urns, options) = args
154 slice_hrn, type = urn_to_hrn(slice_urn)
156 options['geni_rspec_version'] = {
157 'version': '3', 'type': 'GENI'}
158 descr = self.describe(urns, '', options)
160 for sliver_allocation in descr['geni_slivers']:
161 geni_sliver = {'geni_sliver_urn': sliver_allocation['geni_sliver_urn'],
162 'geni_allocation_status': 'geni_unallocated',
163 'geni_expires': sliver_allocation['geni_expires'],
164 'geni_error': sliver_allocation['geni_error']}
166 result.append(geni_sliver)
168 getattr(self.driver, "delete_sliver")(
169 slice_urn, slice_hrn, creds, options)
172 constraints = SliverAllocation.slice_urn.in_(urns)
173 dbsession = self.driver.api.dbsession()
174 sliver_allocations = dbsession.query(
175 SliverAllocation).filter(constraints)
177 sliver_allocation.sliver_id for sliver_allocation in sliver_allocations]
178 SliverAllocation.delete_allocations(sliver_ids, dbsession)
180 elif name == "renew":
181 (urns, expiration_time, options) = args
183 slice_hrn, type = urn_to_hrn(slice_urn)
186 getattr(self.driver, "renew_sliver")(
187 slice_urn, slice_hrn, creds, expiration_time, options)
189 options['geni_rspec_version'] = {
190 'version': '3', 'type': 'GENI'}
191 descr = self.describe(urns, '', options)
192 result = descr['geni_slivers']
194 elif name == "perform_operational_action":
195 (urns, action, options) = args
196 options['geni_rspec_version'] = {
197 'version': '3', 'type': 'GENI'}
198 result = self.describe(urns, '', options)['geni_slivers']
201 # same as v2 ( registry methods)
202 result = getattr(self.driver, name)(*args, **kwds)