| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| tierno | 2e21551 | 2018-11-28 09:37:52 +0000 | [diff] [blame] | 3 | ## |
| 4 | # Copyright 2018 Telefonica S.A. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | ## |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 18 | |
| kuuse | d124bfe | 2019-06-18 12:09:24 +0200 | [diff] [blame] | 19 | import asyncio |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 20 | from collections import OrderedDict |
| tierno | 79cd8ad | 2019-10-18 13:03:10 +0000 | [diff] [blame] | 21 | from time import time |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 22 | from osm_lcm.data_utils.database.database import Database |
| 23 | from osm_lcm.data_utils.filesystem.filesystem import Filesystem |
| 24 | |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 25 | # from osm_common.dbbase import DbException |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 26 | |
| 27 | __author__ = "Alfonso Tierno" |
| 28 | |
| 29 | |
| 30 | class LcmException(Exception): |
| 31 | pass |
| 32 | |
| 33 | |
| tierno | f578e55 | 2018-11-08 19:07:20 +0100 | [diff] [blame] | 34 | class LcmExceptionNoMgmtIP(LcmException): |
| 35 | pass |
| 36 | |
| 37 | |
| gcalvino | ed7f6d4 | 2018-12-14 14:44:56 +0100 | [diff] [blame] | 38 | class LcmExceptionExit(LcmException): |
| 39 | pass |
| 40 | |
| 41 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 42 | def versiontuple(v): |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 43 | """utility for compare dot separate versions. Fills with zeros to proper number comparison |
| 44 | package version will be something like 4.0.1.post11+gb3f024d.dirty-1. Where 4.0.1 is the git tag, postXX is the |
| 45 | number of commits from this tag, and +XXXXXXX is the git commit short id. Total length is 16 with until 999 commits |
| 46 | """ |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 47 | filled = [] |
| 48 | for point in v.split("."): |
| tierno | e64f7fb | 2019-09-11 08:55:52 +0000 | [diff] [blame] | 49 | point, _, _ = point.partition("+") |
| 50 | point, _, _ = point.partition("-") |
| 51 | filled.append(point.zfill(20)) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 52 | return tuple(filled) |
| 53 | |
| 54 | |
| tierno | 744303e | 2020-01-13 16:46:31 +0000 | [diff] [blame] | 55 | def deep_get(target_dict, key_list, default_value=None): |
| tierno | 626e015 | 2019-11-29 14:16:16 +0000 | [diff] [blame] | 56 | """ |
| 57 | Get a value from target_dict entering in the nested keys. If keys does not exist, it returns None |
| 58 | 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 |
| 59 | :param target_dict: dictionary to be read |
| 60 | :param key_list: list of keys to read from target_dict |
| tierno | 744303e | 2020-01-13 16:46:31 +0000 | [diff] [blame] | 61 | :param default_value: value to return if key is not present in the nested dictionary |
| tierno | 626e015 | 2019-11-29 14:16:16 +0000 | [diff] [blame] | 62 | :return: The wanted value if exist, None otherwise |
| 63 | """ |
| 64 | for key in key_list: |
| 65 | if not isinstance(target_dict, dict) or key not in target_dict: |
| tierno | 744303e | 2020-01-13 16:46:31 +0000 | [diff] [blame] | 66 | return default_value |
| tierno | 626e015 | 2019-11-29 14:16:16 +0000 | [diff] [blame] | 67 | target_dict = target_dict[key] |
| 68 | return target_dict |
| 69 | |
| 70 | |
| tierno | 744303e | 2020-01-13 16:46:31 +0000 | [diff] [blame] | 71 | def get_iterable(in_dict, in_key): |
| 72 | """ |
| 73 | Similar to <dict>.get(), but if value is None, False, ..., An empty tuple is returned instead |
| 74 | :param in_dict: a dictionary |
| 75 | :param in_key: the key to look for at in_dict |
| 76 | :return: in_dict[in_var] or () if it is None or not present |
| 77 | """ |
| 78 | if not in_dict.get(in_key): |
| 79 | return () |
| 80 | return in_dict[in_key] |
| 81 | |
| 82 | |
| 83 | def populate_dict(target_dict, key_list, value): |
| 84 | """ |
| 85 | Update target_dict creating nested dictionaries with the key_list. Last key_list item is asigned the value. |
| 86 | Example target_dict={K: J}; key_list=[a,b,c]; target_dict will be {K: J, a: {b: {c: value}}} |
| 87 | :param target_dict: dictionary to be changed |
| 88 | :param key_list: list of keys to insert at target_dict |
| 89 | :param value: |
| 90 | :return: None |
| 91 | """ |
| 92 | for key in key_list[0:-1]: |
| 93 | if key not in target_dict: |
| 94 | target_dict[key] = {} |
| 95 | target_dict = target_dict[key] |
| 96 | target_dict[key_list[-1]] = value |
| 97 | |
| 98 | |
| kuuse | d124bfe | 2019-06-18 12:09:24 +0200 | [diff] [blame] | 99 | class LcmBase: |
| 100 | |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 101 | def __init__(self, msg, logger): |
| kuuse | d124bfe | 2019-06-18 12:09:24 +0200 | [diff] [blame] | 102 | """ |
| 103 | |
| 104 | :param db: database connection |
| 105 | """ |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 106 | self.db = Database().instance.db |
| kuuse | d124bfe | 2019-06-18 12:09:24 +0200 | [diff] [blame] | 107 | self.msg = msg |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 108 | self.fs = Filesystem().instance.fs |
| kuuse | d124bfe | 2019-06-18 12:09:24 +0200 | [diff] [blame] | 109 | self.logger = logger |
| 110 | |
| 111 | def update_db_2(self, item, _id, _desc): |
| 112 | """ |
| 113 | Updates database with _desc information. If success _desc is cleared |
| 114 | :param item: |
| 115 | :param _id: |
| 116 | :param _desc: dictionary with the content to update. Keys are dot separated keys for |
| 117 | :return: None. Exception is raised on error |
| 118 | """ |
| 119 | if not _desc: |
| 120 | return |
| tierno | 79cd8ad | 2019-10-18 13:03:10 +0000 | [diff] [blame] | 121 | now = time() |
| 122 | _desc["_admin.modified"] = now |
| kuuse | d124bfe | 2019-06-18 12:09:24 +0200 | [diff] [blame] | 123 | self.db.set_one(item, {"_id": _id}, _desc) |
| 124 | _desc.clear() |
| 125 | # except DbException as e: |
| 126 | # self.logger.error("Updating {} _id={} with '{}'. Error: {}".format(item, _id, _desc, e)) |
| 127 | |
| 128 | |
| 129 | class TaskRegistry(LcmBase): |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 130 | """ |
| 131 | Implements a registry of task needed for later cancelation, look for related tasks that must be completed before |
| 132 | etc. It stores a four level dict |
| 133 | First level is the topic, ns, vim_account, sdn |
| 134 | Second level is the _id |
| 135 | Third level is the operation id |
| 136 | Fourth level is a descriptive name, the value is the task class |
| kuuse | d124bfe | 2019-06-18 12:09:24 +0200 | [diff] [blame] | 137 | |
| 138 | The HA (High-Availability) methods are used when more than one LCM instance is running. |
| 139 | To register the current task in the external DB, use LcmBase as base class, to be able |
| 140 | to reuse LcmBase.update_db_2() |
| 141 | The DB registry uses the following fields to distinguish a task: |
| 142 | - op_type: operation type ("nslcmops" or "nsilcmops") |
| 143 | - op_id: operation ID |
| 144 | - worker: the worker ID for this process |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 145 | """ |
| 146 | |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 147 | # NS/NSI: "services" VIM/WIM/SDN: "accounts" |
| 148 | topic_service_list = ['ns', 'nsi'] |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 149 | topic_account_list = ['vim', 'wim', 'sdn', 'k8scluster', 'k8srepo'] |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 150 | |
| 151 | # Map topic to InstanceID |
| 152 | topic2instid_dict = { |
| 153 | 'ns': 'nsInstanceId', |
| 154 | 'nsi': 'netsliceInstanceId'} |
| 155 | |
| 156 | # Map topic to DB table name |
| 157 | topic2dbtable_dict = { |
| 158 | 'ns': 'nslcmops', |
| 159 | 'nsi': 'nsilcmops', |
| 160 | 'vim': 'vim_accounts', |
| 161 | 'wim': 'wim_accounts', |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 162 | 'sdn': 'sdns', |
| 163 | 'k8scluster': 'k8sclusters', |
| 164 | 'k8srepo': 'k8srepos'} |
| kuuse | d124bfe | 2019-06-18 12:09:24 +0200 | [diff] [blame] | 165 | |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 166 | def __init__(self, worker_id=None, logger=None): |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 167 | self.task_registry = { |
| 168 | "ns": {}, |
| Felipe Vicens | c2033f2 | 2018-11-15 15:09:58 +0100 | [diff] [blame] | 169 | "nsi": {}, |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 170 | "vim_account": {}, |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 171 | "wim_account": {}, |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 172 | "sdn": {}, |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 173 | "k8scluster": {}, |
| 174 | "k8srepo": {}, |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 175 | } |
| kuuse | d124bfe | 2019-06-18 12:09:24 +0200 | [diff] [blame] | 176 | self.worker_id = worker_id |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 177 | self.db = Database().instance.db |
| kuuse | d124bfe | 2019-06-18 12:09:24 +0200 | [diff] [blame] | 178 | self.logger = logger |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 179 | |
| 180 | def register(self, topic, _id, op_id, task_name, task): |
| 181 | """ |
| 182 | Register a new task |
| Felipe Vicens | c2033f2 | 2018-11-15 15:09:58 +0100 | [diff] [blame] | 183 | :param topic: Can be "ns", "nsi", "vim_account", "sdn" |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 184 | :param _id: _id of the related item |
| 185 | :param op_id: id of the operation of the related item |
| 186 | :param task_name: Task descriptive name, as create, instantiate, terminate. Must be unique in this op_id |
| 187 | :param task: Task class |
| 188 | :return: none |
| 189 | """ |
| 190 | if _id not in self.task_registry[topic]: |
| 191 | self.task_registry[topic][_id] = OrderedDict() |
| 192 | if op_id not in self.task_registry[topic][_id]: |
| 193 | self.task_registry[topic][_id][op_id] = {task_name: task} |
| 194 | else: |
| 195 | self.task_registry[topic][_id][op_id][task_name] = task |
| 196 | # print("registering task", topic, _id, op_id, task_name, task) |
| 197 | |
| 198 | def remove(self, topic, _id, op_id, task_name=None): |
| 199 | """ |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 200 | When task is ended, it should be removed. It ignores missing tasks. It also removes tasks done with this _id |
| Felipe Vicens | c2033f2 | 2018-11-15 15:09:58 +0100 | [diff] [blame] | 201 | :param topic: Can be "ns", "nsi", "vim_account", "sdn" |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 202 | :param _id: _id of the related item |
| 203 | :param op_id: id of the operation of the related item |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 204 | :param task_name: Task descriptive name. If none it deletes all tasks with same _id and op_id |
| 205 | :return: None |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 206 | """ |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 207 | if not self.task_registry[topic].get(_id): |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 208 | return |
| 209 | if not task_name: |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 210 | self.task_registry[topic][_id].pop(op_id, None) |
| 211 | elif self.task_registry[topic][_id].get(op_id): |
| 212 | self.task_registry[topic][_id][op_id].pop(task_name, None) |
| 213 | |
| 214 | # delete done tasks |
| 215 | for op_id_ in list(self.task_registry[topic][_id]): |
| 216 | for name, task in self.task_registry[topic][_id][op_id_].items(): |
| 217 | if not task.done(): |
| 218 | break |
| 219 | else: |
| 220 | del self.task_registry[topic][_id][op_id_] |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 221 | if not self.task_registry[topic][_id]: |
| 222 | del self.task_registry[topic][_id] |
| 223 | |
| 224 | def lookfor_related(self, topic, _id, my_op_id=None): |
| 225 | task_list = [] |
| 226 | task_name_list = [] |
| 227 | if _id not in self.task_registry[topic]: |
| 228 | return "", task_name_list |
| 229 | for op_id in reversed(self.task_registry[topic][_id]): |
| 230 | if my_op_id: |
| 231 | if my_op_id == op_id: |
| 232 | my_op_id = None # so that the next task is taken |
| 233 | continue |
| 234 | |
| 235 | for task_name, task in self.task_registry[topic][_id][op_id].items(): |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 236 | if not task.done(): |
| 237 | task_list.append(task) |
| 238 | task_name_list.append(task_name) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 239 | break |
| 240 | return ", ".join(task_name_list), task_list |
| 241 | |
| 242 | def cancel(self, topic, _id, target_op_id=None, target_task_name=None): |
| 243 | """ |
| kuuse | d124bfe | 2019-06-18 12:09:24 +0200 | [diff] [blame] | 244 | Cancel all active tasks of a concrete ns, nsi, vim_account, sdn identified for _id. If op_id is supplied only |
| Felipe Vicens | c2033f2 | 2018-11-15 15:09:58 +0100 | [diff] [blame] | 245 | this is cancelled, and the same with task_name |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 246 | """ |
| 247 | if not self.task_registry[topic].get(_id): |
| 248 | return |
| 249 | for op_id in reversed(self.task_registry[topic][_id]): |
| 250 | if target_op_id and target_op_id != op_id: |
| 251 | continue |
| 252 | for task_name, task in self.task_registry[topic][_id][op_id].items(): |
| 253 | if target_task_name and target_task_name != task_name: |
| 254 | continue |
| 255 | # result = |
| 256 | task.cancel() |
| 257 | # if result: |
| 258 | # self.logger.debug("{} _id={} order_id={} task={} cancelled".format(topic, _id, op_id, task_name)) |
| 259 | |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 260 | # Is topic NS/NSI? |
| 261 | def _is_service_type_HA(self, topic): |
| 262 | return topic in self.topic_service_list |
| 263 | |
| 264 | # Is topic VIM/WIM/SDN? |
| 265 | def _is_account_type_HA(self, topic): |
| 266 | return topic in self.topic_account_list |
| 267 | |
| 268 | # Input: op_id, example: 'abc123def:3' Output: account_id='abc123def', op_index=3 |
| 269 | def _get_account_and_op_HA(self, op_id): |
| 270 | if not op_id: |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 271 | return None, None |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 272 | account_id, _, op_index = op_id.rpartition(':') |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 273 | if not account_id or not op_index.isdigit(): |
| 274 | return None, None |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 275 | return account_id, op_index |
| 276 | |
| 277 | # Get '_id' for any topic and operation |
| 278 | def _get_instance_id_HA(self, topic, op_type, op_id): |
| 279 | _id = None |
| 280 | # Special operation 'ANY', for SDN account associated to a VIM account: op_id as '_id' |
| 281 | if op_type == 'ANY': |
| 282 | _id = op_id |
| 283 | # NS/NSI: Use op_id as '_id' |
| 284 | elif self._is_service_type_HA(topic): |
| 285 | _id = op_id |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 286 | # VIM/SDN/WIM/K8SCLUSTER: Split op_id to get Account ID and Operation Index, use Account ID as '_id' |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 287 | elif self._is_account_type_HA(topic): |
| 288 | _id, _ = self._get_account_and_op_HA(op_id) |
| 289 | return _id |
| 290 | |
| 291 | # Set DB _filter for querying any related process state |
| 292 | def _get_waitfor_filter_HA(self, db_lcmop, topic, op_type, op_id): |
| 293 | _filter = {} |
| 294 | # Special operation 'ANY', for SDN account associated to a VIM account: op_id as '_id' |
| 295 | # In this special case, the timestamp is ignored |
| 296 | if op_type == 'ANY': |
| 297 | _filter = {'operationState': 'PROCESSING'} |
| 298 | # Otherwise, get 'startTime' timestamp for this operation |
| 299 | else: |
| 300 | # NS/NSI |
| 301 | if self._is_service_type_HA(topic): |
| tierno | 79cd8ad | 2019-10-18 13:03:10 +0000 | [diff] [blame] | 302 | now = time() |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 303 | starttime_this_op = db_lcmop.get("startTime") |
| 304 | instance_id_label = self.topic2instid_dict.get(topic) |
| 305 | instance_id = db_lcmop.get(instance_id_label) |
| 306 | _filter = {instance_id_label: instance_id, |
| 307 | 'operationState': 'PROCESSING', |
| tierno | 79cd8ad | 2019-10-18 13:03:10 +0000 | [diff] [blame] | 308 | 'startTime.lt': starttime_this_op, |
| 309 | "_admin.modified.gt": now - 2*3600, # ignore if tow hours of inactivity |
| 310 | } |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 311 | # VIM/WIM/SDN/K8scluster |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 312 | elif self._is_account_type_HA(topic): |
| 313 | _, op_index = self._get_account_and_op_HA(op_id) |
| 314 | _ops = db_lcmop['_admin']['operations'] |
| 315 | _this_op = _ops[int(op_index)] |
| 316 | starttime_this_op = _this_op.get('startTime', None) |
| 317 | _filter = {'operationState': 'PROCESSING', |
| 318 | 'startTime.lt': starttime_this_op} |
| 319 | return _filter |
| 320 | |
| 321 | # Get DB params for any topic and operation |
| 322 | def _get_dbparams_for_lock_HA(self, topic, op_type, op_id): |
| 323 | q_filter = {} |
| 324 | update_dict = {} |
| 325 | # NS/NSI |
| 326 | if self._is_service_type_HA(topic): |
| 327 | q_filter = {'_id': op_id, '_admin.worker': None} |
| 328 | update_dict = {'_admin.worker': self.worker_id} |
| 329 | # VIM/WIM/SDN |
| 330 | elif self._is_account_type_HA(topic): |
| 331 | account_id, op_index = self._get_account_and_op_HA(op_id) |
| 332 | if not account_id: |
| 333 | return None, None |
| 334 | if op_type == 'create': |
| 335 | # Creating a VIM/WIM/SDN account implies setting '_admin.current_operation' = 0 |
| 336 | op_index = 0 |
| 337 | q_filter = {'_id': account_id, "_admin.operations.{}.worker".format(op_index): None} |
| 338 | update_dict = {'_admin.operations.{}.worker'.format(op_index): self.worker_id, |
| 339 | '_admin.current_operation': op_index} |
| 340 | return q_filter, update_dict |
| 341 | |
| kuuse | d124bfe | 2019-06-18 12:09:24 +0200 | [diff] [blame] | 342 | def lock_HA(self, topic, op_type, op_id): |
| 343 | """ |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 344 | Lock a task, if possible, to indicate to the HA system that |
| kuuse | d124bfe | 2019-06-18 12:09:24 +0200 | [diff] [blame] | 345 | the task will be executed in this LCM instance. |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 346 | :param topic: Can be "ns", "nsi", "vim", "wim", or "sdn" |
| 347 | :param op_type: Operation type, can be "nslcmops", "nsilcmops", "create", "edit", "delete" |
| 348 | :param op_id: NS, NSI: Operation ID VIM,WIM,SDN: Account ID + ':' + Operation Index |
| kuuse | d124bfe | 2019-06-18 12:09:24 +0200 | [diff] [blame] | 349 | :return: |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 350 | True=lock was successful => execute the task (not registered by any other LCM instance) |
| kuuse | d124bfe | 2019-06-18 12:09:24 +0200 | [diff] [blame] | 351 | False=lock failed => do NOT execute the task (already registered by another LCM instance) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 352 | |
| 353 | HA tasks and backward compatibility: |
| 354 | If topic is "account type" (VIM/WIM/SDN) and op_id is None, 'op_id' was not provided by NBI. |
| 355 | This means that the running NBI instance does not support HA. |
| 356 | In such a case this method should always return True, to always execute |
| 357 | the task in this instance of LCM, without querying the DB. |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 358 | """ |
| 359 | |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 360 | # Backward compatibility for VIM/WIM/SDN/k8scluster without op_id |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 361 | if self._is_account_type_HA(topic) and op_id is None: |
| 362 | return True |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 363 | |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 364 | # Try to lock this task |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 365 | db_table_name = self.topic2dbtable_dict[topic] |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 366 | q_filter, update_dict = self._get_dbparams_for_lock_HA(topic, op_type, op_id) |
| 367 | db_lock_task = self.db.set_one(db_table_name, |
| 368 | q_filter=q_filter, |
| 369 | update_dict=update_dict, |
| 370 | fail_on_empty=False) |
| kuuse | d124bfe | 2019-06-18 12:09:24 +0200 | [diff] [blame] | 371 | if db_lock_task is None: |
| 372 | self.logger.debug("Task {} operation={} already locked by another worker".format(topic, op_id)) |
| 373 | return False |
| 374 | else: |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 375 | # Set 'detailed-status' to 'In progress' for VIM/WIM/SDN operations |
| 376 | if self._is_account_type_HA(topic): |
| 377 | detailed_status = 'In progress' |
| 378 | account_id, op_index = self._get_account_and_op_HA(op_id) |
| 379 | q_filter = {'_id': account_id} |
| 380 | update_dict = {'_admin.operations.{}.detailed-status'.format(op_index): detailed_status} |
| 381 | self.db.set_one(db_table_name, |
| 382 | q_filter=q_filter, |
| 383 | update_dict=update_dict, |
| 384 | fail_on_empty=False) |
| kuuse | d124bfe | 2019-06-18 12:09:24 +0200 | [diff] [blame] | 385 | return True |
| 386 | |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 387 | def unlock_HA(self, topic, op_type, op_id, operationState, detailed_status): |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 388 | """ |
| 389 | Register a task, done when finished a VIM/WIM/SDN 'create' operation. |
| 390 | :param topic: Can be "vim", "wim", or "sdn" |
| 391 | :param op_type: Operation type, can be "create", "edit", "delete" |
| 392 | :param op_id: Account ID + ':' + Operation Index |
| 393 | :return: nothing |
| 394 | """ |
| 395 | |
| 396 | # Backward compatibility |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 397 | if not self._is_account_type_HA(topic) or not op_id: |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 398 | return |
| 399 | |
| 400 | # Get Account ID and Operation Index |
| 401 | account_id, op_index = self._get_account_and_op_HA(op_id) |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 402 | db_table_name = self.topic2dbtable_dict[topic] |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 403 | |
| 404 | # If this is a 'delete' operation, the account may have been deleted (SUCCESS) or may still exist (FAILED) |
| 405 | # If the account exist, register the HA task. |
| 406 | # Update DB for HA tasks |
| 407 | q_filter = {'_id': account_id} |
| 408 | update_dict = {'_admin.operations.{}.operationState'.format(op_index): operationState, |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 409 | '_admin.operations.{}.detailed-status'.format(op_index): detailed_status, |
| 410 | '_admin.operations.{}.worker'.format(op_index): None, |
| 411 | '_admin.current_operation': None} |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 412 | self.db.set_one(db_table_name, |
| 413 | q_filter=q_filter, |
| 414 | update_dict=update_dict, |
| 415 | fail_on_empty=False) |
| 416 | return |
| 417 | |
| kuuse | d124bfe | 2019-06-18 12:09:24 +0200 | [diff] [blame] | 418 | async def waitfor_related_HA(self, topic, op_type, op_id=None): |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 419 | """ |
| kuuse | d124bfe | 2019-06-18 12:09:24 +0200 | [diff] [blame] | 420 | Wait for any pending related HA tasks |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 421 | """ |
| kuuse | d124bfe | 2019-06-18 12:09:24 +0200 | [diff] [blame] | 422 | |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 423 | # Backward compatibility |
| 424 | if not (self._is_service_type_HA(topic) or self._is_account_type_HA(topic)) and (op_id is None): |
| 425 | return |
| kuuse | d124bfe | 2019-06-18 12:09:24 +0200 | [diff] [blame] | 426 | |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 427 | # Get DB table name |
| 428 | db_table_name = self.topic2dbtable_dict.get(topic) |
| 429 | |
| 430 | # Get instance ID |
| 431 | _id = self._get_instance_id_HA(topic, op_type, op_id) |
| 432 | _filter = {"_id": _id} |
| 433 | db_lcmop = self.db.get_one(db_table_name, |
| 434 | _filter, |
| kuuse | d124bfe | 2019-06-18 12:09:24 +0200 | [diff] [blame] | 435 | fail_on_empty=False) |
| 436 | if not db_lcmop: |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 437 | return |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 438 | |
| 439 | # Set DB _filter for querying any related process state |
| 440 | _filter = self._get_waitfor_filter_HA(db_lcmop, topic, op_type, op_id) |
| kuuse | d124bfe | 2019-06-18 12:09:24 +0200 | [diff] [blame] | 441 | |
| 442 | # For HA, get list of tasks from DB instead of from dictionary (in-memory) variable. |
| 443 | timeout_wait_for_task = 3600 # Max time (seconds) to wait for a related task to finish |
| 444 | # interval_wait_for_task = 30 # A too long polling interval slows things down considerably |
| 445 | interval_wait_for_task = 10 # Interval in seconds for polling related tasks |
| 446 | time_left = timeout_wait_for_task |
| 447 | old_num_related_tasks = 0 |
| 448 | while True: |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 449 | # Get related tasks (operations within the same instance as this) which are |
| kuuse | d124bfe | 2019-06-18 12:09:24 +0200 | [diff] [blame] | 450 | # still running (operationState='PROCESSING') and which were started before this task. |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 451 | # In the case of op_type='ANY', get any related tasks with operationState='PROCESSING', ignore timestamps. |
| 452 | db_waitfor_related_task = self.db.get_list(db_table_name, |
| kuuse | d124bfe | 2019-06-18 12:09:24 +0200 | [diff] [blame] | 453 | q_filter=_filter) |
| 454 | new_num_related_tasks = len(db_waitfor_related_task) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 455 | # If there are no related tasks, there is nothing to wait for, so return. |
| kuuse | d124bfe | 2019-06-18 12:09:24 +0200 | [diff] [blame] | 456 | if not new_num_related_tasks: |
| kuuse | d124bfe | 2019-06-18 12:09:24 +0200 | [diff] [blame] | 457 | return |
| 458 | # If number of pending related tasks have changed, |
| 459 | # update the 'detailed-status' field and log the change. |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 460 | # Do NOT update the 'detailed-status' for SDNC-associated-to-VIM operations ('ANY'). |
| 461 | if (op_type != 'ANY') and (new_num_related_tasks != old_num_related_tasks): |
| 462 | step = "Waiting for {} related tasks to be completed.".format(new_num_related_tasks) |
| 463 | update_dict = {} |
| 464 | q_filter = {'_id': _id} |
| 465 | # NS/NSI |
| 466 | if self._is_service_type_HA(topic): |
| quilesj | 3655ae0 | 2019-12-12 16:08:35 +0000 | [diff] [blame] | 467 | update_dict = {'detailed-status': step, 'queuePosition': new_num_related_tasks} |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 468 | # VIM/WIM/SDN |
| 469 | elif self._is_account_type_HA(topic): |
| 470 | _, op_index = self._get_account_and_op_HA(op_id) |
| 471 | update_dict = {'_admin.operations.{}.detailed-status'.format(op_index): step} |
| 472 | self.logger.debug("Task {} operation={} {}".format(topic, _id, step)) |
| 473 | self.db.set_one(db_table_name, |
| 474 | q_filter=q_filter, |
| 475 | update_dict=update_dict, |
| 476 | fail_on_empty=False) |
| kuuse | d124bfe | 2019-06-18 12:09:24 +0200 | [diff] [blame] | 477 | old_num_related_tasks = new_num_related_tasks |
| 478 | time_left -= interval_wait_for_task |
| 479 | if time_left < 0: |
| 480 | raise LcmException( |
| 481 | "Timeout ({}) when waiting for related tasks to be completed".format( |
| 482 | timeout_wait_for_task)) |
| 483 | await asyncio.sleep(interval_wait_for_task) |
| 484 | |
| 485 | return |