1 from sfa.util.sfalogging import logger
2 from sfa.util.config import Config
4 from sfa.managers.managerwrapper import ManagerWrapper
6 # a bundle is the combination of
7 # (*) an api that reacts on the incoming requests to trigger the API methods
8 # (*) a manager that implements the function of the service,
9 # either aggregate, registry, or slicemgr
10 # (*) a driver that controls the underlying testbed
13 # The Generic class is a utility that uses the configuration to figure out
14 # which combination of these pieces need to be put together
16 # this extra indirection is needed to adapt to the current naming scheme
17 # where we have 'pl' and 'plc' and components and the like, that does not
18 # yet follow a sensible scheme
20 # needs refinements to cache more efficiently, esp. wrt the config
24 def __init__ (self, flavour, config):
29 # example flavour='pl' -> sfa.generic.pl.pl()
31 def the_flavour (flavour=None, config=None):
32 if config is None: config=Config()
33 if flavour is None: flavour=config.SFA_GENERIC_FLAVOUR
34 flavour = flavour.lower()
35 #mixed = flavour.capitalize()
36 module_path="sfa.generic.%s"%flavour
37 classname="%s"%flavour
38 logger.debug("Generic.the_flavour with flavour=%s"%flavour)
40 module = __import__ (module_path, globals(), locals(), [classname])
41 return getattr(module, classname)(flavour,config)
43 logger.log_exc("Cannot locate generic instance with flavour=%s"%flavour)
45 # in the simplest case these can be redefined to the class/module objects to be used
46 # see pl.py for an example
47 # some descendant of SfaApi
48 def api_class (self) : pass
49 # the python classes to use to build up the context
50 def registry_class (self) : pass
51 def slicemgr_class (self) : pass
52 def aggregate_class (self) : pass
53 def component_class (self) : pass
57 # insert a manager instance
58 def make_api (self, *args, **kwargs):
59 # interface is a required arg
60 if not 'interface' in kwargs:
61 logger.critical("Generic.make_api: no interface found")
62 api = self.api_class()(*args, **kwargs)
63 # xxx can probably drop support for managers implemented as modules
64 # which makes it a bit awkward
65 manager_class_or_module = self.make_manager(api.interface)
66 driver = self.make_driver (api.config, api.interface)
67 ### arrange stuff together
68 # add a manager wrapper
69 manager_wrap = ManagerWrapper(manager_class_or_module,api.interface,api.config)
70 api.manager=manager_wrap
71 # insert driver in manager
72 logger.debug("Setting manager.driver, manager=%s"%manager_class_or_module)
73 # xxx this should go into the object and not the class !?!
74 manager_class_or_module.driver=driver
75 # add it in api as well for convenience
79 def make_manager (self, interface):
81 interface expected in ['registry', 'aggregate', 'slicemgr', 'component']
82 flavour is e.g. 'pl' or 'max' or whatever
84 flavour = self.flavour
85 message="Generic.make_manager for interface=%s and flavour=%s"%(interface,flavour)
87 classname = "%s_manager_class"%interface
89 module_or_class = getattr(self,classname)()
90 logger.debug("%s : %s"%(message,module_or_class))
91 # this gets passed to ManagerWrapper that will call the class constructor
92 # if it's a class, or use the module as is if it's a module
93 # so bottom line is, don't try the constructor here
94 return module_or_class
96 logger.log_exc_critical(message)
98 # need interface to select the right driver
99 def make_driver (self, config, interface):
100 flavour = self.flavour
101 message="Generic.make_driver for flavour=%s and interface=%s"%(flavour,interface)
103 if interface == "component":
104 classname = "component_driver_class"
106 classname = "driver_class"
108 class_obj = getattr(self,classname)()
109 logger.debug("%s : %s"%(message,class_obj))
110 return class_obj(config)
112 logger.log_exc_critical(message)