last plc-dependent code moved to PlDriver
[sfa.git] / sfa / generic / __init__.py
index 362a459..3c3855d 100644 (file)
@@ -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
@@ -33,15 +35,78 @@ class Generic:
         #mixed = flavour.capitalize()
         module_path="sfa.generic.%s"%flavour
         classname="%s"%flavour
-        logger.info("Generic.the_flavour with flavour=%s"%flavour)
+        logger.debug("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
+    # the python classes to use to build up the context
+    def registry_class (self) : pass
+    def slicemgr_class (self) : pass
+    def aggregate_class (self) : pass
+    def component_class (self) : pass
+
 
-    # 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)
+    # 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.critical("Generic.make_api: no interface found")
+        api = self.api_class()(*args, **kwargs)
+        manager_class_or_module = self.make_manager(api.interface)
+        driver = self.make_driver (api.config, api.interface)
+        ### arrange stuff together
+        # add a manager wrapper
+        manager_wrap = ManagerWrapper(manager_class_or_module,api.interface,api.config)
+        api.manager=manager_wrap
+        # insert driver in manager
+        logger.debug("Setting manager.driver, manager=%s"%manager_class_or_module)
+        # xxx this should go into the object and not the class !?!
+        manager_class_or_module.driver=driver
+        # add it in api as well for convenience
+        api.driver=driver
+        return api
+
+    def make_manager (self, interface):
+        """
+        interface expected in ['registry', 'aggregate', 'slicemgr', '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_manager_class"%interface
+        try:
+            module_or_class = getattr(self,classname)()
+            logger.debug("%s : %s"%(message,module_or_class))
+            # this gets passed to ManagerWrapper that will call the class constructor 
+            # if it's a class, or use the module as is if it's a module
+            # so bottom line is, don't try the constructor here
+            return module_or_class
+        except:
+            logger.log_exc_critical(message)
+        
+    # need interface to select the right driver
+    def make_driver (self, config, interface):
+        flavour = self.flavour
+        message="Generic.make_driver for flavour=%s and interface=%s"%(flavour,interface)
+        
+        if interface == "component":
+            classname = "component_driver_class"
+        else:
+            classname = "driver_class"
+        try:
+            class_obj = getattr(self,classname)()
+            logger.debug("%s : %s"%(message,class_obj))
+            return class_obj(config)
+        except:
+            logger.log_exc_critical(message)
+        
+