fix(sapd): sapd used to specify exposed connections at NS level. Fixes Bug 1321
[osm/LCM.git] / osm_lcm / lcm_utils.py
index 6895015..a05e5ac 100644 (file)
@@ -19,6 +19,9 @@
 import asyncio
 from collections import OrderedDict
 from time import time
+from osm_lcm.data_utils.database.database import Database
+from osm_lcm.data_utils.filesystem.filesystem import Filesystem
+
 # from osm_common.dbbase import DbException
 
 __author__ = "Alfonso Tierno"
@@ -95,14 +98,14 @@ def populate_dict(target_dict, key_list, value):
 
 class LcmBase:
 
-    def __init__(self, db, msg, fs, logger):
+    def __init__(self, msg, logger):
         """
 
         :param db: database connection
         """
-        self.db = db
+        self.db = Database().instance.db
         self.msg = msg
-        self.fs = fs
+        self.fs = Filesystem().instance.fs
         self.logger = logger
 
     def update_db_2(self, item, _id, _desc):
@@ -160,7 +163,7 @@ class TaskRegistry(LcmBase):
         'k8scluster': 'k8sclusters',
         'k8srepo': 'k8srepos'}
 
-    def __init__(self, worker_id=None, db=None, logger=None):
+    def __init__(self, worker_id=None, logger=None):
         self.task_registry = {
             "ns": {},
             "nsi": {},
@@ -171,7 +174,7 @@ class TaskRegistry(LcmBase):
             "k8srepo": {},
         }
         self.worker_id = worker_id
-        self.db = db
+        self.db = Database().instance.db
         self.logger = logger
 
     def register(self, topic, _id, op_id, task_name, task):
@@ -265,12 +268,10 @@ class TaskRegistry(LcmBase):
     # Input: op_id, example: 'abc123def:3' Output: account_id='abc123def', op_index=3
     def _get_account_and_op_HA(self, op_id):
         if not op_id:
-            return (None, None)
+            return None, None
         account_id, _, op_index = op_id.rpartition(':')
-        if not account_id:
-            return (None, None)
-        if not op_index.isdigit():
-            return (None, None)
+        if not account_id or not op_index.isdigit():
+            return None, None
         return account_id, op_index
 
     # Get '_id' for any topic and operation
@@ -361,7 +362,7 @@ class TaskRegistry(LcmBase):
             return True
 
         # Try to lock this task
-        db_table_name = self.topic2dbtable_dict.get(topic)
+        db_table_name = self.topic2dbtable_dict[topic]
         q_filter, update_dict = self._get_dbparams_for_lock_HA(topic, op_type, op_id)
         db_lock_task = self.db.set_one(db_table_name,
                                        q_filter=q_filter,
@@ -383,7 +384,7 @@ class TaskRegistry(LcmBase):
                                 fail_on_empty=False)
             return True
 
-    def register_HA(self, topic, op_type, op_id, operationState, detailed_status):
+    def unlock_HA(self, topic, op_type, op_id, operationState, detailed_status):
         """
         Register a task, done when finished a VIM/WIM/SDN 'create' operation.
         :param topic: Can be "vim", "wim", or "sdn"
@@ -393,19 +394,21 @@ class TaskRegistry(LcmBase):
         """
 
         # Backward compatibility
-        if not self._is_account_type_HA(topic) or (self._is_account_type_HA(topic) and op_id is None):
+        if not self._is_account_type_HA(topic) or not op_id:
             return
 
         # Get Account ID and Operation Index
         account_id, op_index = self._get_account_and_op_HA(op_id)
-        db_table_name = self.topic2dbtable_dict.get(topic)
+        db_table_name = self.topic2dbtable_dict[topic]
 
         # If this is a 'delete' operation, the account may have been deleted (SUCCESS) or may still exist (FAILED)
         # If the account exist, register the HA task.
         # Update DB for HA tasks
         q_filter = {'_id': account_id}
         update_dict = {'_admin.operations.{}.operationState'.format(op_index): operationState,
-                       '_admin.operations.{}.detailed-status'.format(op_index): detailed_status}
+                       '_admin.operations.{}.detailed-status'.format(op_index): detailed_status,
+                       '_admin.operations.{}.worker'.format(op_index): None,
+                       '_admin.current_operation': None}
         self.db.set_one(db_table_name,
                         q_filter=q_filter,
                         update_dict=update_dict,