From e6c8a0a5061bae7e646851b6f265a02cdfb57f22 Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Fri, 28 Oct 2011 01:31:00 +0200 Subject: [PATCH] generic to handle the manager instance in the api too get rid of import_manager --- sfa/generic/__init__.py | 71 ++++++++++++++++++++++++++++++---- sfa/generic/pl.py | 10 +++++ sfa/generic/plcm.py | 3 ++ sfa/managers/managerwrapper.py | 25 ------------ sfa/server/sfa-start.py | 27 +------------ sfa/server/sfaapi.py | 33 +++++----------- 6 files changed, 87 insertions(+), 82 deletions(-) diff --git a/sfa/generic/__init__.py b/sfa/generic/__init__.py index 362a4591..843cd7b0 100644 --- a/sfa/generic/__init__.py +++ b/sfa/generic/__init__.py @@ -1,6 +1,7 @@ from sfa.util.sfalogging import logger from sfa.util.config import Config -import traceback + +from sfa.managers.managerwrapper import ManagerWrapper # a bundle is the combination of # (*) an api that reacts on the incoming requests to trigger the API methods @@ -20,7 +21,8 @@ import traceback class Generic: - def __init__ (self, config): + def __init__ (self, flavour, config): + self.flavour=flavour self.config=config # proof of concept @@ -36,12 +38,67 @@ class Generic: logger.info("Generic.the_flavour with flavour=%s"%flavour) try: module = __import__ (module_path, globals(), locals(), [classname]) - return getattr(module, classname)(config) + return getattr(module, classname)(flavour,config) except: logger.log_exc("Cannot locate generic instance with flavour=%s"%flavour) + # in the simplest case these can be redefined to the class/module objects to be used + # see pl.py for an example + # some descendant of SfaApi + def api_class (self) : pass + # in practical terms these are modules for now + def registry_class (self) : pass + def slicemgr_class (self) : pass + def aggregate_class (self) : pass + def component_class (self) : pass + + + # build an API object + # insert a manager instance + def make_api (self, *args, **kwargs): + # interface is a required arg + if not 'interface' in kwargs: + logger.fatal("Generic.make_api: no interface found") + api = self.api_class()(*args, **kwargs) + interface=kwargs['interface'] + # or simpler, interface=api.interface + manager = self.make_manager(interface) + api.manager = ManagerWrapper(manager,interface) + return api - # how to build an API object - # default is to use api_class but can be redefined - def make_api (self, *args, **kwds): - return self.api_class()(*args, **kwds) + def make_manager (self, interface): + """ + interface expected in ['registry', 'aggregate', 'slice', 'component'] + flavour is e.g. 'pl' or 'max' or whatever + """ + flavour = self.flavour + message="Generic.make_manager for interface=%s and flavour=%s"%(interface,flavour) + + classname = "%s_class"%interface + try: + module = getattr(self,classname)() + logger.info("%s : %s"%(message,module)) + return module + except: + logger.log_exc(message) + logger.fatal("Aborting") + +# former logic was +# basepath = 'sfa.managers' +# qualified = "%s.%s_manager_%s"%(basepath,interface,flavour) +# generic = "%s.%s_manager"%(basepath,interface) +# +# try: +# manager = __import__(qualified, fromlist=[basepath]) +# logger.info ("%s: loaded %s"%(message,qualified)) +# except: +# try: +# manager = __import__ (generic, fromlist=[basepath]) +# if flavour != 'pl' : +# logger.warn ("%s: using generic with flavour!='pl'"%(message)) +# logger.info("%s: loaded %s"%(message,generic)) +# except: +# logger.log_exc("%s: unable to import either %s or %s"%(message,qualified,generic)) +# logger.fatal("Aborted") +# return manager + diff --git a/sfa/generic/pl.py b/sfa/generic/pl.py index 34753d67..853053d1 100644 --- a/sfa/generic/pl.py +++ b/sfa/generic/pl.py @@ -1,9 +1,19 @@ from sfa.generic import Generic import sfa.plc.plcsfaapi +import sfa.managers.registry_manager +import sfa.managers.slice_manager +import sfa.managers.aggregate_manager class pl (Generic): def api_class (self): return sfa.plc.plcsfaapi.PlcSfaApi + def registry_class (self) : + return sfa.managers.registry_manager + def slicemgr_class (self) : + return sfa.managers.slice_manager + def aggregate_class (self) : + return sfa.managers.aggregate_manager + diff --git a/sfa/generic/plcm.py b/sfa/generic/plcm.py index 2abdd127..dd24d3c2 100644 --- a/sfa/generic/plcm.py +++ b/sfa/generic/plcm.py @@ -1,8 +1,11 @@ from sfa.generic.pl import pl import sfa.plc.plccomponentapi +import sfa.managers.component_manager_pl class plcm (pl): def api_class (self): return sfa.plc.plccomponentapi.PlcComponentApi + def component_class (self): + return sfa.managers.component_manager_pl diff --git a/sfa/managers/managerwrapper.py b/sfa/managers/managerwrapper.py index 218cd4a4..5231c2aa 100644 --- a/sfa/managers/managerwrapper.py +++ b/sfa/managers/managerwrapper.py @@ -1,31 +1,6 @@ from sfa.util.faults import SfaNotImplemented from sfa.util.sfalogging import logger -## locate the right manager -def import_manager(kind, type): - """ - kind expected in ['registry', 'aggregate', 'slice', 'component'] - type is e.g. 'pl' or 'max' or whatever - """ - basepath = 'sfa.managers' - qualified = "%s.%s_manager_%s"%(basepath,kind,type) - generic = "%s.%s_manager"%(basepath,kind) - - message="import_manager for kind=%s and type=%s"%(kind,type) - try: - manager = __import__(qualified, fromlist=[basepath]) - logger.info ("%s: loaded %s"%(message,qualified)) - except: - try: - manager = __import__ (generic, fromlist=[basepath]) - if type != 'pl' : - logger.warn ("%s: using generic with type!='pl'"%(message)) - logger.info("%s: loaded %s"%(message,generic)) - except: - manager=None - logger.log_exc("%s: unable to import either %s or %s"%(message,qualified,generic)) - return manager - #################### class ManagerWrapper: """ diff --git a/sfa/server/sfa-start.py b/sfa/server/sfa-start.py index a39d9b71..966a13e0 100755 --- a/sfa/server/sfa-start.py +++ b/sfa/server/sfa-start.py @@ -48,8 +48,6 @@ from sfa.server.aggregate import Aggregates from sfa.util.xrn import get_authority, hrn_to_urn from sfa.util.sfalogging import logger -from sfa.managers.managerwrapper import import_manager - # after http://www.erlenstar.demon.co.uk/unix/faq_2.html def daemon(): """Daemonize the current process.""" @@ -136,28 +134,6 @@ def init_self_signed_cert(hrn, key, server_cert_file): cert.sign() cert.save_to_file(server_cert_file) -def init_server(options, config): - """ - Locate the manager based on config.*TYPE - Execute the init_server method (well in fact function, sigh) if defined in that module - In order to migrate to a more generic approach: - * search for <>_manager_.py - * if not found, try <>_manager.py (and issue a warning if !='pl') - """ - if options.registry: - manager=import_manager ("registry", config.SFA_REGISTRY_TYPE) - if manager and hasattr(manager, 'init_server'): manager.init_server() - if options.am: - manager=import_manager ("aggregate", config.SFA_AGGREGATE_TYPE) - if manager and hasattr(manager, 'init_server'): manager.init_server() - if options.sm: - manager=import_manager ("slice", config.SFA_SM_TYPE) - if manager and hasattr(manager, 'init_server'): manager.init_server() - if options.cm: - manager=import_manager ("component", config.SFA_CM_TYPE) - if manager and hasattr(manager, 'init_server'): manager.init_server() - - def install_peer_certs(server_key_file, server_cert_file): """ Attempt to install missing trusted gids and db records for @@ -280,8 +256,7 @@ def main(): server_cert_file = os.path.join(hierarchy.basedir, "server.cert") init_server_key(server_key_file, server_cert_file, config, hierarchy) - init_server(options, config) - + if (options.daemon): daemon() if options.trusted_certs: diff --git a/sfa/server/sfaapi.py b/sfa/server/sfaapi.py index 1c462575..242979cb 100644 --- a/sfa/server/sfaapi.py +++ b/sfa/server/sfaapi.py @@ -7,9 +7,9 @@ from sfa.util.cache import Cache from sfa.trust.auth import Auth from sfa.trust.certificate import Keypair, Certificate from sfa.trust.credential import Credential +from sfa.trust.rights import determine_rights # this is wrong all right, but temporary, will use generic -from sfa.managers.managerwrapper import ManagerWrapper, import_manager from sfa.server.xmlrpcapi import XmlrpcApi import os import datetime @@ -23,7 +23,9 @@ class SfaApi (XmlrpcApi): It also has the notion of neighbour sfa services as defined in /etc/sfa/{aggregates,registries}.xml Finally it contains a cache instance - It has no a priori knowledge of the underlying testbed + It gets augmented by the generic layer with + (*) an instance of manager (actually a manager module for now) + (*) which in turn holds an instance of a testbed driver """ def __init__ (self, encoding="utf-8", methods='sfa.methods', @@ -57,30 +59,13 @@ class SfaApi (XmlrpcApi): # load aggregates from sfa.server.aggregate import Aggregates self.aggregates = Aggregates() + + # filled later on by generic/Generic + self.manager=None - + # tmp def get_interface_manager(self, manager_base = 'sfa.managers'): - """ - Returns the appropriate manager module for this interface. - Modules are usually found in sfa/managers/ - """ - manager=None - if self.interface in ['registry']: - manager=import_manager ("registry", self.config.SFA_REGISTRY_TYPE) - elif self.interface in ['aggregate']: - manager=import_manager ("aggregate", self.config.SFA_AGGREGATE_TYPE) - elif self.interface in ['slicemgr', 'sm']: - manager=import_manager ("slice", self.config.SFA_SM_TYPE) - elif self.interface in ['component', 'cm']: - manager=import_manager ("component", self.config.SFA_CM_TYPE) - if not manager: - raise SfaAPIError("No manager for interface: %s" % self.interface) - - # this isnt necessary but will help to produce better error messages - # if someone tries to access an operation this manager doesn't implement - manager = ManagerWrapper(manager, self.interface) - - return manager + return self.manager def get_server(self, interface, cred, timeout=30): """ -- 2.43.0