X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_lcm%2Flcm_utils.py;h=dcea3c0b7420e752e33488f547d834c18d7d65e1;hb=7ecbc3423bc9e08a662780b2eec10ee7cfe78b74;hp=bb53d7c2db1c6750854fc492afe024c81de73c8e;hpb=6a470c6529293da9a67fb366713f078a9fa70541;p=osm%2FLCM.git diff --git a/osm_lcm/lcm_utils.py b/osm_lcm/lcm_utils.py index bb53d7c..dcea3c0 100644 --- a/osm_lcm/lcm_utils.py +++ b/osm_lcm/lcm_utils.py @@ -18,6 +18,7 @@ import asyncio from collections import OrderedDict +from time import time # from osm_common.dbbase import DbException __author__ = "Alfonso Tierno" @@ -42,11 +43,56 @@ def versiontuple(v): """ filled = [] for point in v.split("."): - filled.append(point.zfill(16)) + point, _, _ = point.partition("+") + point, _, _ = point.partition("-") + filled.append(point.zfill(20)) 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 .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): @@ -69,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: @@ -95,7 +143,7 @@ class TaskRegistry(LcmBase): # NS/NSI: "services" VIM/WIM/SDN: "accounts" topic_service_list = ['ns', 'nsi'] - topic_account_list = ['vim', 'wim', 'sdn'] + topic_account_list = ['vim', 'wim', 'sdn', 'k8scluster', 'k8srepo'] # Map topic to InstanceID topic2instid_dict = { @@ -108,7 +156,9 @@ class TaskRegistry(LcmBase): 'nsi': 'nsilcmops', 'vim': 'vim_accounts', 'wim': 'wim_accounts', - 'sdn': 'sdns'} + 'sdn': 'sdns', + 'k8scluster': 'k8sclusters', + 'k8srepo': 'k8srepos'} def __init__(self, worker_id=None, db=None, logger=None): self.task_registry = { @@ -117,6 +167,8 @@ class TaskRegistry(LcmBase): "vim_account": {}, "wim_account": {}, "sdn": {}, + "k8scluster": {}, + "k8srepo": {}, } self.worker_id = worker_id self.db = db @@ -213,12 +265,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 @@ -230,7 +280,7 @@ class TaskRegistry(LcmBase): # NS/NSI: Use op_id as '_id' elif self._is_service_type_HA(topic): _id = op_id - # VIM/SDN/WIM: Split op_id to get Account ID and Operation Index, use Account ID as '_id' + # VIM/SDN/WIM/K8SCLUSTER: Split op_id to get Account ID and Operation Index, use Account ID as '_id' elif self._is_account_type_HA(topic): _id, _ = self._get_account_and_op_HA(op_id) return _id @@ -246,13 +296,16 @@ 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} - # VIM/WIM/SDN + '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) _ops = db_lcmop['_admin']['operations'] @@ -301,12 +354,12 @@ class TaskRegistry(LcmBase): the task in this instance of LCM, without querying the DB. """ - # Backward compatibility for VIM/WIM/SDN without op_id + # Backward compatibility for VIM/WIM/SDN/k8scluster without op_id if self._is_account_type_HA(topic) and op_id is None: 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, @@ -328,7 +381,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" @@ -338,19 +391,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, @@ -406,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)