fix devops-stages/stage-build.sh grpclib instalation
[osm/LCM.git] / osm_lcm / lcm_utils.py
index a6ce5bd..6895015 100644 (file)
@@ -18,6 +18,7 @@
 
 import asyncio
 from collections import OrderedDict
+from time import time
 # from osm_common.dbbase import DbException
 
 __author__ = "Alfonso Tierno"
@@ -48,7 +49,50 @@ def versiontuple(v):
     return tuple(filled)
 
 
-# LcmBase must be listed before TaskRegistry, as it is a dependency.
+def deep_get(target_dict, key_list, default_value=None):
+    """
+    Get a value from target_dict entering in the nested keys. If keys does not exist, it returns None
+    Example target_dict={a: {b: 5}}; key_list=[a,b] returns 5; both key_list=[a,b,c] and key_list=[f,h] return None
+    :param target_dict: dictionary to be read
+    :param key_list: list of keys to read from  target_dict
+    :param default_value: value to return if key is not present in the nested dictionary
+    :return: The wanted value if exist, None otherwise
+    """
+    for key in key_list:
+        if not isinstance(target_dict, dict) or key not in target_dict:
+            return default_value
+        target_dict = target_dict[key]
+    return target_dict
+
+
+def get_iterable(in_dict, in_key):
+    """
+    Similar to <dict>.get(), but if value is None, False, ..., An empty tuple is returned instead
+    :param in_dict: a dictionary
+    :param in_key: the key to look for at in_dict
+    :return: in_dict[in_var] or () if it is None or not present
+    """
+    if not in_dict.get(in_key):
+        return ()
+    return in_dict[in_key]
+
+
+def populate_dict(target_dict, key_list, value):
+    """
+    Update target_dict creating nested dictionaries with the key_list. Last key_list item is asigned the value.
+    Example target_dict={K: J}; key_list=[a,b,c];  target_dict will be {K: J, a: {b: {c: value}}}
+    :param target_dict: dictionary to be changed
+    :param key_list: list of keys to insert at target_dict
+    :param value:
+    :return: None
+    """
+    for key in key_list[0:-1]:
+        if key not in target_dict:
+            target_dict[key] = {}
+        target_dict = target_dict[key]
+    target_dict[key_list[-1]] = value
+
+
 class LcmBase:
 
     def __init__(self, db, msg, fs, logger):
@@ -71,6 +115,8 @@ class LcmBase:
         """
         if not _desc:
             return
+        now = time()
+        _desc["_admin.modified"] = now
         self.db.set_one(item, {"_id": _id}, _desc)
         _desc.clear()
         # except DbException as e:
@@ -252,12 +298,15 @@ class TaskRegistry(LcmBase):
         else:
             # NS/NSI
             if self._is_service_type_HA(topic):
+                now = time()
                 starttime_this_op = db_lcmop.get("startTime")
                 instance_id_label = self.topic2instid_dict.get(topic)
                 instance_id = db_lcmop.get(instance_id_label)
                 _filter = {instance_id_label: instance_id,
                            'operationState': 'PROCESSING',
-                           'startTime.lt': starttime_this_op}
+                           'startTime.lt': starttime_this_op,
+                           "_admin.modified.gt": now - 2*3600,  # ignore if tow hours of inactivity
+                           }
             # VIM/WIM/SDN/K8scluster
             elif self._is_account_type_HA(topic):
                 _, op_index = self._get_account_and_op_HA(op_id)
@@ -412,7 +461,7 @@ class TaskRegistry(LcmBase):
                 q_filter = {'_id': _id}
                 # NS/NSI
                 if self._is_service_type_HA(topic):
-                    update_dict = {'detailed-status': step}
+                    update_dict = {'detailed-status': step, 'queuePosition': new_num_related_tasks}
                 # VIM/WIM/SDN
                 elif self._is_account_type_HA(topic):
                     _, op_index = self._get_account_and_op_HA(op_id)