X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfa%2Fmanagers%2Fv2_to_v3_adapter.py;h=15c89e08a9ebdb4ae0a273c7548e6ef691486024;hb=fd395e1944dcd49f10a4d5b27ce4983ad389fb96;hp=287144d0550822b3458d8c5046ce941dbc9b9599;hpb=e4f82236056f931f9b19f7d98559fea5ec80e2e9;p=sfa.git diff --git a/sfa/managers/v2_to_v3_adapter.py b/sfa/managers/v2_to_v3_adapter.py index 287144d0..15c89e08 100644 --- a/sfa/managers/v2_to_v3_adapter.py +++ b/sfa/managers/v2_to_v3_adapter.py @@ -7,46 +7,39 @@ from sfa.util.xrn import Xrn, urn_to_hrn, hrn_to_urn, get_leaf, get_authority from sfa.util.cache import Cache from sfa.rspecs.rspec import RSpec from sfa.storage.model import SliverAllocation -from sfa.storage.alchemy import dbsession + class V2ToV3Adapter: - def __init__ (self, config): + def __init__(self, api): + config = api.config flavour = config.SFA_GENERIC_FLAVOUR # to be cleaned - if flavour == "pl": - from sfa.planetlab.pldriver import PlDriver - self.driver = PlDriver(config) - elif flavour == "nitos": + if flavour == "nitos": from sfa.nitos.nitosdriver import NitosDriver - self.driver = NitosDriver(config) + self.driver = NitosDriver(api) elif flavour == "fd": from sfa.federica.fddriver import FdDriver - self.driver = FdDriver(config) - elif flavour == "dummy": - from sfa.dummy.dummydriver import DummyDriver - self.driver = DummyDriver(config) - elif flavour == "slab": - from sfa.senslab.slabdriver import SlabDriver - self.driver = SlabDriver(config) + self.driver = FdDriver(api) else: - logger.info("DriverV2Adapter unknown flavour !!!\n supported flavours: pl, nitos, fd, dummy, slab") - - # Caching + logger.error( + "V2ToV3Adapter: Unknown Flavour !!!\n Supported Flavours: nitos, fd") + + # Caching if config.SFA_AGGREGATE_CACHING: if self.driver.cache: self.cache = self.driver.cache else: self.cache = Cache() - def __getattr__(self, name): def func(*args, **kwds): if name == "list_resources": (version, options) = args slice_urn = slice_hrn = None creds = [] - rspec = getattr(self.driver, "list_resources")(slice_urn, slice_hrn, [], options) + rspec = getattr(self.driver, "list_resources")( + slice_urn, slice_hrn, [], options) result = rspec elif name == "describe": @@ -54,93 +47,103 @@ class V2ToV3Adapter: slice_urn = urns[0] slice_hrn, type = urn_to_hrn(slice_urn) creds = [] - rspec = getattr(self.driver, "list_resources")(slice_urn, slice_hrn, creds, options) - + rspec = getattr(self.driver, "list_resources")( + slice_urn, slice_hrn, creds, options) + # SliverAllocation if len(urns) == 1 and Xrn(xrn=urns[0]).type == 'slice': constraint = SliverAllocation.slice_urn.in_(urns) else: constraint = SliverAllocation.sliver_id.in_(urns) - - sliver_allocations = dbsession.query (SliverAllocation).filter (constraint) - sliver_status = getattr(self.driver, "sliver_status")(slice_urn, slice_hrn) + + sliver_allocations = self.driver.api.dbsession().query( + SliverAllocation).filter(constraint) + sliver_status = getattr(self.driver, "sliver_status")( + slice_urn, slice_hrn) if 'geni_expires' in sliver_status.keys(): geni_expires = sliver_status['geni_expires'] - else: + else: geni_expires = '' - + geni_slivers = [] for sliver_allocation in sliver_allocations: geni_sliver = {} geni_sliver['geni_expires'] = geni_expires - geni_sliver['geni_allocation'] = sliver_allocation.allocation_state - geni_sliver['geni_sliver_urn'] = sliver_allocation.sliver_id + geni_sliver[ + 'geni_allocation'] = sliver_allocation.allocation_state + geni_sliver[ + 'geni_sliver_urn'] = sliver_allocation.sliver_id geni_sliver['geni_error'] = '' if geni_sliver['geni_allocation'] == 'geni_allocated': - geni_sliver['geni_operational_status'] = 'geni_pending_allocation' - else: + geni_sliver[ + 'geni_operational_status'] = 'geni_pending_allocation' + else: geni_sliver['geni_operational_status'] = 'geni_ready' geni_slivers.append(geni_sliver) - result = {'geni_urn': slice_urn, - 'geni_rspec': rspec, - 'geni_slivers': geni_slivers} + 'geni_rspec': rspec, + 'geni_slivers': geni_slivers} elif name == "allocate": (slice_urn, rspec_string, expiration, options) = args slice_hrn, type = urn_to_hrn(slice_urn) creds = [] - users = options.get('geni_users', []) - manifest_string = getattr(self.driver, "create_sliver")(slice_urn, slice_hrn, creds, rspec_string, users, options) - + users = options.get('sfa_users', []) + manifest_string = getattr(self.driver, "create_sliver")( + slice_urn, slice_hrn, creds, rspec_string, users, options) + # slivers allocation rspec = RSpec(manifest_string) slivers = rspec.version.get_nodes_with_slivers() - - ##SliverAllocation + + # SliverAllocation for sliver in slivers: - client_id = sliver['client_id'] - component_id = sliver['component_id'] - component_name = sliver['component_name'] - slice_name = slice_hrn.replace('.','-') - component_short_name = component_name.split('.')[0] - # self.driver.hrn - sliver_hrn = '%s.%s-%s' % (self.driver.hrn, slice_name, component_short_name) - sliver_id = Xrn(sliver_hrn, type='sliver').urn - record = SliverAllocation(sliver_id=sliver_id, - client_id=client_id, - component_id=component_id, - slice_urn = slice_urn, - allocation_state='geni_allocated') - - record.sync() - - + client_id = sliver['client_id'] + component_id = sliver['component_id'] + component_name = sliver['component_name'] + slice_name = slice_hrn.replace('.', '-') + component_short_name = component_name.split('.')[0] + # self.driver.hrn + sliver_hrn = '%s.%s-%s' % (self.driver.hrn, + slice_name, component_short_name) + sliver_id = Xrn(sliver_hrn, type='sliver').urn + record = SliverAllocation(sliver_id=sliver_id, + client_id=client_id, + component_id=component_id, + slice_urn=slice_urn, + allocation_state='geni_allocated') + + record.sync(self.driver.api.dbsession()) + # return manifest rspec_version = RSpec(rspec_string).version - rspec_version_str = "%s"%rspec_version - options['geni_rspec_version'] = {'version': rspec_version_str.split(' ')[1], 'type': rspec_version_str.lower().split(' ')[0]} + rspec_version_str = "%s" % rspec_version + options['geni_rspec_version'] = {'version': rspec_version_str.split( + ' ')[1], 'type': rspec_version_str.lower().split(' ')[0]} result = self.describe([slice_urn], rspec_version, options) - - elif name == "provision": + + elif name == "provision": (urns, options) = args if len(urns) == 1 and Xrn(xrn=urns[0]).type == 'slice': - constraint = SliverAllocation.slice_urn.in_(urns) + constraint = SliverAllocation.slice_urn.in_(urns) else: - constraint = SliverAllocation.sliver_id.in_(urns) - - sliver_allocations = dbsession.query (SliverAllocation).filter(constraint) + constraint = SliverAllocation.sliver_id.in_(urns) + + dbsession = self.driver.api.dbsession() + sliver_allocations = dbsession.query( + SliverAllocation).filter(constraint) for sliver_allocation in sliver_allocations: - sliver_allocation.allocation_state = 'geni_provisioned' - + sliver_allocation.allocation_state = 'geni_provisioned' + dbsession.commit() result = self.describe(urns, '', options) elif name == "status": urns = args options = {} - options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'} + options['geni_rspec_version'] = { + 'version': '3', 'type': 'GENI'} descr = self.describe(urns[0], '', options) result = {'geni_urn': descr['geni_urn'], 'geni_slivers': descr['geni_slivers']} @@ -150,47 +153,52 @@ class V2ToV3Adapter: slice_urn = urns[0] slice_hrn, type = urn_to_hrn(slice_urn) creds = [] - options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'} + options['geni_rspec_version'] = { + 'version': '3', 'type': 'GENI'} descr = self.describe(urns, '', options) result = [] for sliver_allocation in descr['geni_slivers']: - geni_sliver = {'geni_sliver_urn': sliver_allocation['geni_sliver_urn'], - 'geni_allocation_status': 'geni_unallocated', - 'geni_expires': sliver_allocation['geni_expires'], - 'geni_error': sliver_allocation['geni_error']} - - result.append(geni_sliver) - - getattr(self.driver, "delete_sliver")(slice_urn, slice_hrn, creds, options) - - #SliverAllocation + geni_sliver = {'geni_sliver_urn': sliver_allocation['geni_sliver_urn'], + 'geni_allocation_status': 'geni_unallocated', + 'geni_expires': sliver_allocation['geni_expires'], + 'geni_error': sliver_allocation['geni_error']} + + result.append(geni_sliver) + + getattr(self.driver, "delete_sliver")( + slice_urn, slice_hrn, creds, options) + + # SliverAllocation constraints = SliverAllocation.slice_urn.in_(urns) - sliver_allocations = dbsession.query(SliverAllocation).filter(constraints) - sliver_ids = [sliver_allocation.sliver_id for sliver_allocation in sliver_allocations] - SliverAllocation.delete_allocations(sliver_ids) - + dbsession = self.driver.api.dbsession() + sliver_allocations = dbsession.query( + SliverAllocation).filter(constraints) + sliver_ids = [ + sliver_allocation.sliver_id for sliver_allocation in sliver_allocations] + SliverAllocation.delete_allocations(sliver_ids, dbsession) elif name == "renew": (urns, expiration_time, options) = args - slice_urn = urns[0] + slice_urn = urns[0] slice_hrn, type = urn_to_hrn(slice_urn) creds = [] - getattr(self.driver, "renew_sliver")(slice_urn, slice_hrn, creds, expiration_time, options) + getattr(self.driver, "renew_sliver")( + slice_urn, slice_hrn, creds, expiration_time, options) - options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'} + options['geni_rspec_version'] = { + 'version': '3', 'type': 'GENI'} descr = self.describe(urns, '', options) result = descr['geni_slivers'] - elif name == "perform_operational_action": (urns, action, options) = args - options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'} + options['geni_rspec_version'] = { + 'version': '3', 'type': 'GENI'} result = self.describe(urns, '', options)['geni_slivers'] - - else: - # same as v2 (list_resources and registry methods) - result=getattr(self.driver, name)(*args, **kwds) + else: + # same as v2 ( registry methods) + result = getattr(self.driver, name)(*args, **kwds) return result return func