move version package to __init__
[osm/NBI.git] / osm_nbi / engine.py
index 0211bd6..ca7a837 100644 (file)
@@ -15,7 +15,7 @@
 
 import logging
 import yaml
-from osm_common import dbmongo, dbmemory, fslocal, msglocal, msgkafka, version as common_version
+from osm_common import dbmongo, dbmemory, fslocal, fsmongo, msglocal, msgkafka, version as common_version
 from osm_common.dbbase import DbException
 from osm_common.fsbase import FsException
 from osm_common.msgbase import MsgException
@@ -68,16 +68,18 @@ class Engine(object):
         # Add new versions here
     }
 
-    def __init__(self):
+    def __init__(self, authenticator):
         self.db = None
         self.fs = None
         self.msg = None
-        self.auth = None
+        self.authconn = None
         self.config = None
         self.operations = None
         self.logger = logging.getLogger("nbi.engine")
         self.map_topic = {}
         self.write_lock = None
+        # self.token_cache = token_cache
+        self.authenticator = authenticator
 
     def start(self, config):
         """
@@ -106,6 +108,9 @@ class Engine(object):
                 if config["storage"]["driver"] == "local":
                     self.fs = fslocal.FsLocal()
                     self.fs.fs_connect(config["storage"])
+                elif config["storage"]["driver"] == "mongo":
+                    self.fs = fsmongo.FsMongo()
+                    self.fs.fs_connect(config["storage"])
                 else:
                     raise EngineException("Invalid configuration param '{}' at '[storage]':'driver'".format(
                         config["storage"]["driver"]))
@@ -119,11 +124,11 @@ class Engine(object):
                 else:
                     raise EngineException("Invalid configuration param '{}' at '[message]':'driver'".format(
                         config["message"]["driver"]))
-            if not self.auth:
+            if not self.authconn:
                 if config["authentication"]["backend"] == "keystone":
-                    self.auth = AuthconnKeystone(config["authentication"], self.db, None)
+                    self.authconn = AuthconnKeystone(config["authentication"], self.db)
                 else:
-                    self.auth = AuthconnInternal(config["authentication"], self.db, dict())
+                    self.authconn = AuthconnInternal(config["authentication"], self.db)
             if not self.operations:
                 if "resources_to_operations" in config["rbac"]:
                     resources_to_operations_file = config["rbac"]["resources_to_operations"]
@@ -153,13 +158,14 @@ class Engine(object):
             for topic, topic_class in self.map_from_topic_to_class.items():
                 # if self.auth and topic_class in (UserTopicAuth, ProjectTopicAuth):
                 #     self.map_topic[topic] = topic_class(self.db, self.fs, self.msg, self.auth)
-                if self.auth and topic_class == RoleTopicAuth:
-                    self.map_topic[topic] = topic_class(self.db, self.fs, self.msg, self.auth,
+                if self.authconn and topic_class == RoleTopicAuth:
+                    self.map_topic[topic] = topic_class(self.db, self.fs, self.msg, self.authconn,
                                                         self.operations)
                 else:
-                    self.map_topic[topic] = topic_class(self.db, self.fs, self.msg, self.auth)
+                    self.map_topic[topic] = topic_class(self.db, self.fs, self.msg, self.authconn)
             
-            self.map_topic["pm_jobs"] = PmJobsTopic(config["prometheus"].get("host"), config["prometheus"].get("port"))
+            self.map_topic["pm_jobs"] = PmJobsTopic(self.db, config["prometheus"].get("host"),
+                                                    config["prometheus"].get("port"))
         except (DbException, FsException, MsgException) as e:
             raise EngineException(str(e), http_code=e.http_code)
 
@@ -259,18 +265,21 @@ class Engine(object):
         with self.write_lock:
             return self.map_topic[topic].delete_list(session, _filter)
 
-    def del_item(self, session, topic, _id):
+    def del_item(self, session, topic, _id, not_send_msg=None):
         """
         Delete item by its internal id
         :param session: contains the used login username and working project
         :param topic: it can be: users, projects, vnfds, nsds, ...
         :param _id: server id of the item
+        :param not_send_msg: If False, message will not be sent to kafka.
+            If a list, message is not sent, but content is stored in this variable so that the caller can send this
+            message using its own loop. If None, message is sent
         :return: dictionary with deleted item _id. It raises exception if not found.
         """
         if topic not in self.map_topic:
             raise EngineException("Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR)
         with self.write_lock:
-            return self.map_topic[topic].delete(session, _id)
+            return self.map_topic[topic].delete(session, _id, not_send_msg=not_send_msg)
 
     def edit_item(self, session, topic, _id, indata=None, kwargs=None):
         """