generic to handle the manager instance in the api too
[sfa.git] / sfa / generic / __init__.py
index 362a459..843cd7b 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
@@ -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
+