blob: bb53d7c2db1c6750854fc492afe024c81de73c8e [file] [log] [blame]
tierno59d22d22018-09-25 18:10:19 +02001# -*- coding: utf-8 -*-
2
tierno2e215512018-11-28 09:37:52 +00003##
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##
tierno59d22d22018-09-25 18:10:19 +020018
kuused124bfe2019-06-18 12:09:24 +020019import asyncio
tierno59d22d22018-09-25 18:10:19 +020020from collections import OrderedDict
tiernobaa51102018-12-14 13:16:18 +000021# from osm_common.dbbase import DbException
tierno59d22d22018-09-25 18:10:19 +020022
23__author__ = "Alfonso Tierno"
24
25
26class LcmException(Exception):
27 pass
28
29
tiernof578e552018-11-08 19:07:20 +010030class LcmExceptionNoMgmtIP(LcmException):
31 pass
32
33
gcalvinoed7f6d42018-12-14 14:44:56 +010034class LcmExceptionExit(LcmException):
35 pass
36
37
tierno59d22d22018-09-25 18:10:19 +020038def versiontuple(v):
tierno27246d82018-09-27 15:59:09 +020039 """utility for compare dot separate versions. Fills with zeros to proper number comparison
40 package version will be something like 4.0.1.post11+gb3f024d.dirty-1. Where 4.0.1 is the git tag, postXX is the
41 number of commits from this tag, and +XXXXXXX is the git commit short id. Total length is 16 with until 999 commits
42 """
tierno59d22d22018-09-25 18:10:19 +020043 filled = []
44 for point in v.split("."):
tierno27246d82018-09-27 15:59:09 +020045 filled.append(point.zfill(16))
tierno59d22d22018-09-25 18:10:19 +020046 return tuple(filled)
47
48
kuused124bfe2019-06-18 12:09:24 +020049# LcmBase must be listed before TaskRegistry, as it is a dependency.
50class LcmBase:
51
52 def __init__(self, db, msg, fs, logger):
53 """
54
55 :param db: database connection
56 """
57 self.db = db
58 self.msg = msg
59 self.fs = fs
60 self.logger = logger
61
62 def update_db_2(self, item, _id, _desc):
63 """
64 Updates database with _desc information. If success _desc is cleared
65 :param item:
66 :param _id:
67 :param _desc: dictionary with the content to update. Keys are dot separated keys for
68 :return: None. Exception is raised on error
69 """
70 if not _desc:
71 return
72 self.db.set_one(item, {"_id": _id}, _desc)
73 _desc.clear()
74 # except DbException as e:
75 # self.logger.error("Updating {} _id={} with '{}'. Error: {}".format(item, _id, _desc, e))
76
77
78class TaskRegistry(LcmBase):
tierno59d22d22018-09-25 18:10:19 +020079 """
80 Implements a registry of task needed for later cancelation, look for related tasks that must be completed before
81 etc. It stores a four level dict
82 First level is the topic, ns, vim_account, sdn
83 Second level is the _id
84 Third level is the operation id
85 Fourth level is a descriptive name, the value is the task class
kuused124bfe2019-06-18 12:09:24 +020086
87 The HA (High-Availability) methods are used when more than one LCM instance is running.
88 To register the current task in the external DB, use LcmBase as base class, to be able
89 to reuse LcmBase.update_db_2()
90 The DB registry uses the following fields to distinguish a task:
91 - op_type: operation type ("nslcmops" or "nsilcmops")
92 - op_id: operation ID
93 - worker: the worker ID for this process
tierno59d22d22018-09-25 18:10:19 +020094 """
95
kuuse6a470c62019-07-10 13:52:45 +020096 # NS/NSI: "services" VIM/WIM/SDN: "accounts"
97 topic_service_list = ['ns', 'nsi']
98 topic_account_list = ['vim', 'wim', 'sdn']
99
100 # Map topic to InstanceID
101 topic2instid_dict = {
102 'ns': 'nsInstanceId',
103 'nsi': 'netsliceInstanceId'}
104
105 # Map topic to DB table name
106 topic2dbtable_dict = {
107 'ns': 'nslcmops',
108 'nsi': 'nsilcmops',
109 'vim': 'vim_accounts',
110 'wim': 'wim_accounts',
111 'sdn': 'sdns'}
kuused124bfe2019-06-18 12:09:24 +0200112
113 def __init__(self, worker_id=None, db=None, logger=None):
tierno59d22d22018-09-25 18:10:19 +0200114 self.task_registry = {
115 "ns": {},
Felipe Vicensc2033f22018-11-15 15:09:58 +0100116 "nsi": {},
tierno59d22d22018-09-25 18:10:19 +0200117 "vim_account": {},
tiernoe37b57d2018-12-11 17:22:51 +0000118 "wim_account": {},
tierno59d22d22018-09-25 18:10:19 +0200119 "sdn": {},
120 }
kuused124bfe2019-06-18 12:09:24 +0200121 self.worker_id = worker_id
122 self.db = db
123 self.logger = logger
tierno59d22d22018-09-25 18:10:19 +0200124
125 def register(self, topic, _id, op_id, task_name, task):
126 """
127 Register a new task
Felipe Vicensc2033f22018-11-15 15:09:58 +0100128 :param topic: Can be "ns", "nsi", "vim_account", "sdn"
tierno59d22d22018-09-25 18:10:19 +0200129 :param _id: _id of the related item
130 :param op_id: id of the operation of the related item
131 :param task_name: Task descriptive name, as create, instantiate, terminate. Must be unique in this op_id
132 :param task: Task class
133 :return: none
134 """
135 if _id not in self.task_registry[topic]:
136 self.task_registry[topic][_id] = OrderedDict()
137 if op_id not in self.task_registry[topic][_id]:
138 self.task_registry[topic][_id][op_id] = {task_name: task}
139 else:
140 self.task_registry[topic][_id][op_id][task_name] = task
141 # print("registering task", topic, _id, op_id, task_name, task)
142
143 def remove(self, topic, _id, op_id, task_name=None):
144 """
tiernobaa51102018-12-14 13:16:18 +0000145 When task is ended, it should be removed. It ignores missing tasks. It also removes tasks done with this _id
Felipe Vicensc2033f22018-11-15 15:09:58 +0100146 :param topic: Can be "ns", "nsi", "vim_account", "sdn"
tierno59d22d22018-09-25 18:10:19 +0200147 :param _id: _id of the related item
148 :param op_id: id of the operation of the related item
tiernobaa51102018-12-14 13:16:18 +0000149 :param task_name: Task descriptive name. If none it deletes all tasks with same _id and op_id
150 :return: None
tierno59d22d22018-09-25 18:10:19 +0200151 """
tiernobaa51102018-12-14 13:16:18 +0000152 if not self.task_registry[topic].get(_id):
tierno59d22d22018-09-25 18:10:19 +0200153 return
154 if not task_name:
tiernobaa51102018-12-14 13:16:18 +0000155 self.task_registry[topic][_id].pop(op_id, None)
156 elif self.task_registry[topic][_id].get(op_id):
157 self.task_registry[topic][_id][op_id].pop(task_name, None)
158
159 # delete done tasks
160 for op_id_ in list(self.task_registry[topic][_id]):
161 for name, task in self.task_registry[topic][_id][op_id_].items():
162 if not task.done():
163 break
164 else:
165 del self.task_registry[topic][_id][op_id_]
tierno59d22d22018-09-25 18:10:19 +0200166 if not self.task_registry[topic][_id]:
167 del self.task_registry[topic][_id]
168
169 def lookfor_related(self, topic, _id, my_op_id=None):
170 task_list = []
171 task_name_list = []
172 if _id not in self.task_registry[topic]:
173 return "", task_name_list
174 for op_id in reversed(self.task_registry[topic][_id]):
175 if my_op_id:
176 if my_op_id == op_id:
177 my_op_id = None # so that the next task is taken
178 continue
179
180 for task_name, task in self.task_registry[topic][_id][op_id].items():
tiernobaa51102018-12-14 13:16:18 +0000181 if not task.done():
182 task_list.append(task)
183 task_name_list.append(task_name)
tierno59d22d22018-09-25 18:10:19 +0200184 break
185 return ", ".join(task_name_list), task_list
186
187 def cancel(self, topic, _id, target_op_id=None, target_task_name=None):
188 """
kuused124bfe2019-06-18 12:09:24 +0200189 Cancel all active tasks of a concrete ns, nsi, vim_account, sdn identified for _id. If op_id is supplied only
Felipe Vicensc2033f22018-11-15 15:09:58 +0100190 this is cancelled, and the same with task_name
tierno59d22d22018-09-25 18:10:19 +0200191 """
192 if not self.task_registry[topic].get(_id):
193 return
194 for op_id in reversed(self.task_registry[topic][_id]):
195 if target_op_id and target_op_id != op_id:
196 continue
197 for task_name, task in self.task_registry[topic][_id][op_id].items():
198 if target_task_name and target_task_name != task_name:
199 continue
200 # result =
201 task.cancel()
202 # if result:
203 # self.logger.debug("{} _id={} order_id={} task={} cancelled".format(topic, _id, op_id, task_name))
204
kuuse6a470c62019-07-10 13:52:45 +0200205 # Is topic NS/NSI?
206 def _is_service_type_HA(self, topic):
207 return topic in self.topic_service_list
208
209 # Is topic VIM/WIM/SDN?
210 def _is_account_type_HA(self, topic):
211 return topic in self.topic_account_list
212
213 # Input: op_id, example: 'abc123def:3' Output: account_id='abc123def', op_index=3
214 def _get_account_and_op_HA(self, op_id):
215 if not op_id:
216 return (None, None)
217 account_id, _, op_index = op_id.rpartition(':')
218 if not account_id:
219 return (None, None)
220 if not op_index.isdigit():
221 return (None, None)
222 return account_id, op_index
223
224 # Get '_id' for any topic and operation
225 def _get_instance_id_HA(self, topic, op_type, op_id):
226 _id = None
227 # Special operation 'ANY', for SDN account associated to a VIM account: op_id as '_id'
228 if op_type == 'ANY':
229 _id = op_id
230 # NS/NSI: Use op_id as '_id'
231 elif self._is_service_type_HA(topic):
232 _id = op_id
233 # VIM/SDN/WIM: Split op_id to get Account ID and Operation Index, use Account ID as '_id'
234 elif self._is_account_type_HA(topic):
235 _id, _ = self._get_account_and_op_HA(op_id)
236 return _id
237
238 # Set DB _filter for querying any related process state
239 def _get_waitfor_filter_HA(self, db_lcmop, topic, op_type, op_id):
240 _filter = {}
241 # Special operation 'ANY', for SDN account associated to a VIM account: op_id as '_id'
242 # In this special case, the timestamp is ignored
243 if op_type == 'ANY':
244 _filter = {'operationState': 'PROCESSING'}
245 # Otherwise, get 'startTime' timestamp for this operation
246 else:
247 # NS/NSI
248 if self._is_service_type_HA(topic):
249 starttime_this_op = db_lcmop.get("startTime")
250 instance_id_label = self.topic2instid_dict.get(topic)
251 instance_id = db_lcmop.get(instance_id_label)
252 _filter = {instance_id_label: instance_id,
253 'operationState': 'PROCESSING',
254 'startTime.lt': starttime_this_op}
255 # VIM/WIM/SDN
256 elif self._is_account_type_HA(topic):
257 _, op_index = self._get_account_and_op_HA(op_id)
258 _ops = db_lcmop['_admin']['operations']
259 _this_op = _ops[int(op_index)]
260 starttime_this_op = _this_op.get('startTime', None)
261 _filter = {'operationState': 'PROCESSING',
262 'startTime.lt': starttime_this_op}
263 return _filter
264
265 # Get DB params for any topic and operation
266 def _get_dbparams_for_lock_HA(self, topic, op_type, op_id):
267 q_filter = {}
268 update_dict = {}
269 # NS/NSI
270 if self._is_service_type_HA(topic):
271 q_filter = {'_id': op_id, '_admin.worker': None}
272 update_dict = {'_admin.worker': self.worker_id}
273 # VIM/WIM/SDN
274 elif self._is_account_type_HA(topic):
275 account_id, op_index = self._get_account_and_op_HA(op_id)
276 if not account_id:
277 return None, None
278 if op_type == 'create':
279 # Creating a VIM/WIM/SDN account implies setting '_admin.current_operation' = 0
280 op_index = 0
281 q_filter = {'_id': account_id, "_admin.operations.{}.worker".format(op_index): None}
282 update_dict = {'_admin.operations.{}.worker'.format(op_index): self.worker_id,
283 '_admin.current_operation': op_index}
284 return q_filter, update_dict
285
kuused124bfe2019-06-18 12:09:24 +0200286 def lock_HA(self, topic, op_type, op_id):
287 """
kuuse6a470c62019-07-10 13:52:45 +0200288 Lock a task, if possible, to indicate to the HA system that
kuused124bfe2019-06-18 12:09:24 +0200289 the task will be executed in this LCM instance.
kuuse6a470c62019-07-10 13:52:45 +0200290 :param topic: Can be "ns", "nsi", "vim", "wim", or "sdn"
291 :param op_type: Operation type, can be "nslcmops", "nsilcmops", "create", "edit", "delete"
292 :param op_id: NS, NSI: Operation ID VIM,WIM,SDN: Account ID + ':' + Operation Index
kuused124bfe2019-06-18 12:09:24 +0200293 :return:
kuuse6a470c62019-07-10 13:52:45 +0200294 True=lock was successful => execute the task (not registered by any other LCM instance)
kuused124bfe2019-06-18 12:09:24 +0200295 False=lock failed => do NOT execute the task (already registered by another LCM instance)
kuuse6a470c62019-07-10 13:52:45 +0200296
297 HA tasks and backward compatibility:
298 If topic is "account type" (VIM/WIM/SDN) and op_id is None, 'op_id' was not provided by NBI.
299 This means that the running NBI instance does not support HA.
300 In such a case this method should always return True, to always execute
301 the task in this instance of LCM, without querying the DB.
tierno59d22d22018-09-25 18:10:19 +0200302 """
303
kuuse6a470c62019-07-10 13:52:45 +0200304 # Backward compatibility for VIM/WIM/SDN without op_id
305 if self._is_account_type_HA(topic) and op_id is None:
306 return True
tierno59d22d22018-09-25 18:10:19 +0200307
kuuse6a470c62019-07-10 13:52:45 +0200308 # Try to lock this task
309 db_table_name = self.topic2dbtable_dict.get(topic)
310 q_filter, update_dict = self._get_dbparams_for_lock_HA(topic, op_type, op_id)
311 db_lock_task = self.db.set_one(db_table_name,
312 q_filter=q_filter,
313 update_dict=update_dict,
314 fail_on_empty=False)
kuused124bfe2019-06-18 12:09:24 +0200315 if db_lock_task is None:
316 self.logger.debug("Task {} operation={} already locked by another worker".format(topic, op_id))
317 return False
318 else:
kuuse6a470c62019-07-10 13:52:45 +0200319 # Set 'detailed-status' to 'In progress' for VIM/WIM/SDN operations
320 if self._is_account_type_HA(topic):
321 detailed_status = 'In progress'
322 account_id, op_index = self._get_account_and_op_HA(op_id)
323 q_filter = {'_id': account_id}
324 update_dict = {'_admin.operations.{}.detailed-status'.format(op_index): detailed_status}
325 self.db.set_one(db_table_name,
326 q_filter=q_filter,
327 update_dict=update_dict,
328 fail_on_empty=False)
kuused124bfe2019-06-18 12:09:24 +0200329 return True
330
kuuse6a470c62019-07-10 13:52:45 +0200331 def register_HA(self, topic, op_type, op_id, operationState, detailed_status):
332 """
333 Register a task, done when finished a VIM/WIM/SDN 'create' operation.
334 :param topic: Can be "vim", "wim", or "sdn"
335 :param op_type: Operation type, can be "create", "edit", "delete"
336 :param op_id: Account ID + ':' + Operation Index
337 :return: nothing
338 """
339
340 # Backward compatibility
341 if not self._is_account_type_HA(topic) or (self._is_account_type_HA(topic) and op_id is None):
342 return
343
344 # Get Account ID and Operation Index
345 account_id, op_index = self._get_account_and_op_HA(op_id)
346 db_table_name = self.topic2dbtable_dict.get(topic)
347
348 # If this is a 'delete' operation, the account may have been deleted (SUCCESS) or may still exist (FAILED)
349 # If the account exist, register the HA task.
350 # Update DB for HA tasks
351 q_filter = {'_id': account_id}
352 update_dict = {'_admin.operations.{}.operationState'.format(op_index): operationState,
353 '_admin.operations.{}.detailed-status'.format(op_index): detailed_status}
354 self.db.set_one(db_table_name,
355 q_filter=q_filter,
356 update_dict=update_dict,
357 fail_on_empty=False)
358 return
359
kuused124bfe2019-06-18 12:09:24 +0200360 async def waitfor_related_HA(self, topic, op_type, op_id=None):
tierno59d22d22018-09-25 18:10:19 +0200361 """
kuused124bfe2019-06-18 12:09:24 +0200362 Wait for any pending related HA tasks
tierno59d22d22018-09-25 18:10:19 +0200363 """
kuused124bfe2019-06-18 12:09:24 +0200364
kuuse6a470c62019-07-10 13:52:45 +0200365 # Backward compatibility
366 if not (self._is_service_type_HA(topic) or self._is_account_type_HA(topic)) and (op_id is None):
367 return
kuused124bfe2019-06-18 12:09:24 +0200368
kuuse6a470c62019-07-10 13:52:45 +0200369 # Get DB table name
370 db_table_name = self.topic2dbtable_dict.get(topic)
371
372 # Get instance ID
373 _id = self._get_instance_id_HA(topic, op_type, op_id)
374 _filter = {"_id": _id}
375 db_lcmop = self.db.get_one(db_table_name,
376 _filter,
kuused124bfe2019-06-18 12:09:24 +0200377 fail_on_empty=False)
378 if not db_lcmop:
tierno59d22d22018-09-25 18:10:19 +0200379 return
kuuse6a470c62019-07-10 13:52:45 +0200380
381 # Set DB _filter for querying any related process state
382 _filter = self._get_waitfor_filter_HA(db_lcmop, topic, op_type, op_id)
kuused124bfe2019-06-18 12:09:24 +0200383
384 # For HA, get list of tasks from DB instead of from dictionary (in-memory) variable.
385 timeout_wait_for_task = 3600 # Max time (seconds) to wait for a related task to finish
386 # interval_wait_for_task = 30 # A too long polling interval slows things down considerably
387 interval_wait_for_task = 10 # Interval in seconds for polling related tasks
388 time_left = timeout_wait_for_task
389 old_num_related_tasks = 0
390 while True:
kuuse6a470c62019-07-10 13:52:45 +0200391 # Get related tasks (operations within the same instance as this) which are
kuused124bfe2019-06-18 12:09:24 +0200392 # still running (operationState='PROCESSING') and which were started before this task.
kuuse6a470c62019-07-10 13:52:45 +0200393 # In the case of op_type='ANY', get any related tasks with operationState='PROCESSING', ignore timestamps.
394 db_waitfor_related_task = self.db.get_list(db_table_name,
kuused124bfe2019-06-18 12:09:24 +0200395 q_filter=_filter)
396 new_num_related_tasks = len(db_waitfor_related_task)
kuuse6a470c62019-07-10 13:52:45 +0200397 # If there are no related tasks, there is nothing to wait for, so return.
kuused124bfe2019-06-18 12:09:24 +0200398 if not new_num_related_tasks:
kuused124bfe2019-06-18 12:09:24 +0200399 return
400 # If number of pending related tasks have changed,
401 # update the 'detailed-status' field and log the change.
kuuse6a470c62019-07-10 13:52:45 +0200402 # Do NOT update the 'detailed-status' for SDNC-associated-to-VIM operations ('ANY').
403 if (op_type != 'ANY') and (new_num_related_tasks != old_num_related_tasks):
404 step = "Waiting for {} related tasks to be completed.".format(new_num_related_tasks)
405 update_dict = {}
406 q_filter = {'_id': _id}
407 # NS/NSI
408 if self._is_service_type_HA(topic):
409 update_dict = {'detailed-status': step}
410 # VIM/WIM/SDN
411 elif self._is_account_type_HA(topic):
412 _, op_index = self._get_account_and_op_HA(op_id)
413 update_dict = {'_admin.operations.{}.detailed-status'.format(op_index): step}
414 self.logger.debug("Task {} operation={} {}".format(topic, _id, step))
415 self.db.set_one(db_table_name,
416 q_filter=q_filter,
417 update_dict=update_dict,
418 fail_on_empty=False)
kuused124bfe2019-06-18 12:09:24 +0200419 old_num_related_tasks = new_num_related_tasks
420 time_left -= interval_wait_for_task
421 if time_left < 0:
422 raise LcmException(
423 "Timeout ({}) when waiting for related tasks to be completed".format(
424 timeout_wait_for_task))
425 await asyncio.sleep(interval_wait_for_task)
426
427 return