1 # -*- coding: utf-8 -*-
4 # Copyright 2018 Telefonica S.A.
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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
20 from collections
import OrderedDict
22 # from osm_common.dbbase import DbException
24 __author__
= "Alfonso Tierno"
27 class LcmException(Exception):
31 class LcmExceptionNoMgmtIP(LcmException
):
35 class LcmExceptionExit(LcmException
):
40 """utility for compare dot separate versions. Fills with zeros to proper number comparison
41 package version will be something like 4.0.1.post11+gb3f024d.dirty-1. Where 4.0.1 is the git tag, postXX is the
42 number of commits from this tag, and +XXXXXXX is the git commit short id. Total length is 16 with until 999 commits
45 for point
in v
.split("."):
46 point
, _
, _
= point
.partition("+")
47 point
, _
, _
= point
.partition("-")
48 filled
.append(point
.zfill(20))
52 # LcmBase must be listed before TaskRegistry, as it is a dependency.
55 def __init__(self
, db
, msg
, fs
, logger
):
58 :param db: database connection
65 def update_db_2(self
, item
, _id
, _desc
):
67 Updates database with _desc information. If success _desc is cleared
70 :param _desc: dictionary with the content to update. Keys are dot separated keys for
71 :return: None. Exception is raised on error
76 _desc
["_admin.modified"] = now
77 self
.db
.set_one(item
, {"_id": _id
}, _desc
)
79 # except DbException as e:
80 # self.logger.error("Updating {} _id={} with '{}'. Error: {}".format(item, _id, _desc, e))
83 class TaskRegistry(LcmBase
):
85 Implements a registry of task needed for later cancelation, look for related tasks that must be completed before
86 etc. It stores a four level dict
87 First level is the topic, ns, vim_account, sdn
88 Second level is the _id
89 Third level is the operation id
90 Fourth level is a descriptive name, the value is the task class
92 The HA (High-Availability) methods are used when more than one LCM instance is running.
93 To register the current task in the external DB, use LcmBase as base class, to be able
94 to reuse LcmBase.update_db_2()
95 The DB registry uses the following fields to distinguish a task:
96 - op_type: operation type ("nslcmops" or "nsilcmops")
98 - worker: the worker ID for this process
101 # NS/NSI: "services" VIM/WIM/SDN: "accounts"
102 topic_service_list
= ['ns', 'nsi']
103 topic_account_list
= ['vim', 'wim', 'sdn']
105 # Map topic to InstanceID
106 topic2instid_dict
= {
107 'ns': 'nsInstanceId',
108 'nsi': 'netsliceInstanceId'}
110 # Map topic to DB table name
111 topic2dbtable_dict
= {
114 'vim': 'vim_accounts',
115 'wim': 'wim_accounts',
118 def __init__(self
, worker_id
=None, db
=None, logger
=None):
119 self
.task_registry
= {
126 self
.worker_id
= worker_id
130 def register(self
, topic
, _id
, op_id
, task_name
, task
):
133 :param topic: Can be "ns", "nsi", "vim_account", "sdn"
134 :param _id: _id of the related item
135 :param op_id: id of the operation of the related item
136 :param task_name: Task descriptive name, as create, instantiate, terminate. Must be unique in this op_id
137 :param task: Task class
140 if _id
not in self
.task_registry
[topic
]:
141 self
.task_registry
[topic
][_id
] = OrderedDict()
142 if op_id
not in self
.task_registry
[topic
][_id
]:
143 self
.task_registry
[topic
][_id
][op_id
] = {task_name
: task
}
145 self
.task_registry
[topic
][_id
][op_id
][task_name
] = task
146 # print("registering task", topic, _id, op_id, task_name, task)
148 def remove(self
, topic
, _id
, op_id
, task_name
=None):
150 When task is ended, it should be removed. It ignores missing tasks. It also removes tasks done with this _id
151 :param topic: Can be "ns", "nsi", "vim_account", "sdn"
152 :param _id: _id of the related item
153 :param op_id: id of the operation of the related item
154 :param task_name: Task descriptive name. If none it deletes all tasks with same _id and op_id
157 if not self
.task_registry
[topic
].get(_id
):
160 self
.task_registry
[topic
][_id
].pop(op_id
, None)
161 elif self
.task_registry
[topic
][_id
].get(op_id
):
162 self
.task_registry
[topic
][_id
][op_id
].pop(task_name
, None)
165 for op_id_
in list(self
.task_registry
[topic
][_id
]):
166 for name
, task
in self
.task_registry
[topic
][_id
][op_id_
].items():
170 del self
.task_registry
[topic
][_id
][op_id_
]
171 if not self
.task_registry
[topic
][_id
]:
172 del self
.task_registry
[topic
][_id
]
174 def lookfor_related(self
, topic
, _id
, my_op_id
=None):
177 if _id
not in self
.task_registry
[topic
]:
178 return "", task_name_list
179 for op_id
in reversed(self
.task_registry
[topic
][_id
]):
181 if my_op_id
== op_id
:
182 my_op_id
= None # so that the next task is taken
185 for task_name
, task
in self
.task_registry
[topic
][_id
][op_id
].items():
187 task_list
.append(task
)
188 task_name_list
.append(task_name
)
190 return ", ".join(task_name_list
), task_list
192 def cancel(self
, topic
, _id
, target_op_id
=None, target_task_name
=None):
194 Cancel all active tasks of a concrete ns, nsi, vim_account, sdn identified for _id. If op_id is supplied only
195 this is cancelled, and the same with task_name
197 if not self
.task_registry
[topic
].get(_id
):
199 for op_id
in reversed(self
.task_registry
[topic
][_id
]):
200 if target_op_id
and target_op_id
!= op_id
:
202 for task_name
, task
in self
.task_registry
[topic
][_id
][op_id
].items():
203 if target_task_name
and target_task_name
!= task_name
:
208 # self.logger.debug("{} _id={} order_id={} task={} cancelled".format(topic, _id, op_id, task_name))
211 def _is_service_type_HA(self
, topic
):
212 return topic
in self
.topic_service_list
214 # Is topic VIM/WIM/SDN?
215 def _is_account_type_HA(self
, topic
):
216 return topic
in self
.topic_account_list
218 # Input: op_id, example: 'abc123def:3' Output: account_id='abc123def', op_index=3
219 def _get_account_and_op_HA(self
, op_id
):
222 account_id
, _
, op_index
= op_id
.rpartition(':')
225 if not op_index
.isdigit():
227 return account_id
, op_index
229 # Get '_id' for any topic and operation
230 def _get_instance_id_HA(self
, topic
, op_type
, op_id
):
232 # Special operation 'ANY', for SDN account associated to a VIM account: op_id as '_id'
235 # NS/NSI: Use op_id as '_id'
236 elif self
._is
_service
_type
_HA
(topic
):
238 # VIM/SDN/WIM: Split op_id to get Account ID and Operation Index, use Account ID as '_id'
239 elif self
._is
_account
_type
_HA
(topic
):
240 _id
, _
= self
._get
_account
_and
_op
_HA
(op_id
)
243 # Set DB _filter for querying any related process state
244 def _get_waitfor_filter_HA(self
, db_lcmop
, topic
, op_type
, op_id
):
246 # Special operation 'ANY', for SDN account associated to a VIM account: op_id as '_id'
247 # In this special case, the timestamp is ignored
249 _filter
= {'operationState': 'PROCESSING'}
250 # Otherwise, get 'startTime' timestamp for this operation
253 if self
._is
_service
_type
_HA
(topic
):
255 starttime_this_op
= db_lcmop
.get("startTime")
256 instance_id_label
= self
.topic2instid_dict
.get(topic
)
257 instance_id
= db_lcmop
.get(instance_id_label
)
258 _filter
= {instance_id_label
: instance_id
,
259 'operationState': 'PROCESSING',
260 'startTime.lt': starttime_this_op
,
261 "_admin.modified.gt": now
- 2*3600, # ignore if tow hours of inactivity
264 elif self
._is
_account
_type
_HA
(topic
):
265 _
, op_index
= self
._get
_account
_and
_op
_HA
(op_id
)
266 _ops
= db_lcmop
['_admin']['operations']
267 _this_op
= _ops
[int(op_index
)]
268 starttime_this_op
= _this_op
.get('startTime', None)
269 _filter
= {'operationState': 'PROCESSING',
270 'startTime.lt': starttime_this_op
}
273 # Get DB params for any topic and operation
274 def _get_dbparams_for_lock_HA(self
, topic
, op_type
, op_id
):
278 if self
._is
_service
_type
_HA
(topic
):
279 q_filter
= {'_id': op_id
, '_admin.worker': None}
280 update_dict
= {'_admin.worker': self
.worker_id
}
282 elif self
._is
_account
_type
_HA
(topic
):
283 account_id
, op_index
= self
._get
_account
_and
_op
_HA
(op_id
)
286 if op_type
== 'create':
287 # Creating a VIM/WIM/SDN account implies setting '_admin.current_operation' = 0
289 q_filter
= {'_id': account_id
, "_admin.operations.{}.worker".format(op_index
): None}
290 update_dict
= {'_admin.operations.{}.worker'.format(op_index
): self
.worker_id
,
291 '_admin.current_operation': op_index
}
292 return q_filter
, update_dict
294 def lock_HA(self
, topic
, op_type
, op_id
):
296 Lock a task, if possible, to indicate to the HA system that
297 the task will be executed in this LCM instance.
298 :param topic: Can be "ns", "nsi", "vim", "wim", or "sdn"
299 :param op_type: Operation type, can be "nslcmops", "nsilcmops", "create", "edit", "delete"
300 :param op_id: NS, NSI: Operation ID VIM,WIM,SDN: Account ID + ':' + Operation Index
302 True=lock was successful => execute the task (not registered by any other LCM instance)
303 False=lock failed => do NOT execute the task (already registered by another LCM instance)
305 HA tasks and backward compatibility:
306 If topic is "account type" (VIM/WIM/SDN) and op_id is None, 'op_id' was not provided by NBI.
307 This means that the running NBI instance does not support HA.
308 In such a case this method should always return True, to always execute
309 the task in this instance of LCM, without querying the DB.
312 # Backward compatibility for VIM/WIM/SDN without op_id
313 if self
._is
_account
_type
_HA
(topic
) and op_id
is None:
316 # Try to lock this task
317 db_table_name
= self
.topic2dbtable_dict
.get(topic
)
318 q_filter
, update_dict
= self
._get
_dbparams
_for
_lock
_HA
(topic
, op_type
, op_id
)
319 db_lock_task
= self
.db
.set_one(db_table_name
,
321 update_dict
=update_dict
,
323 if db_lock_task
is None:
324 self
.logger
.debug("Task {} operation={} already locked by another worker".format(topic
, op_id
))
327 # Set 'detailed-status' to 'In progress' for VIM/WIM/SDN operations
328 if self
._is
_account
_type
_HA
(topic
):
329 detailed_status
= 'In progress'
330 account_id
, op_index
= self
._get
_account
_and
_op
_HA
(op_id
)
331 q_filter
= {'_id': account_id
}
332 update_dict
= {'_admin.operations.{}.detailed-status'.format(op_index
): detailed_status
}
333 self
.db
.set_one(db_table_name
,
335 update_dict
=update_dict
,
339 def register_HA(self
, topic
, op_type
, op_id
, operationState
, detailed_status
):
341 Register a task, done when finished a VIM/WIM/SDN 'create' operation.
342 :param topic: Can be "vim", "wim", or "sdn"
343 :param op_type: Operation type, can be "create", "edit", "delete"
344 :param op_id: Account ID + ':' + Operation Index
348 # Backward compatibility
349 if not self
._is
_account
_type
_HA
(topic
) or (self
._is
_account
_type
_HA
(topic
) and op_id
is None):
352 # Get Account ID and Operation Index
353 account_id
, op_index
= self
._get
_account
_and
_op
_HA
(op_id
)
354 db_table_name
= self
.topic2dbtable_dict
.get(topic
)
356 # If this is a 'delete' operation, the account may have been deleted (SUCCESS) or may still exist (FAILED)
357 # If the account exist, register the HA task.
358 # Update DB for HA tasks
359 q_filter
= {'_id': account_id
}
360 update_dict
= {'_admin.operations.{}.operationState'.format(op_index
): operationState
,
361 '_admin.operations.{}.detailed-status'.format(op_index
): detailed_status
}
362 self
.db
.set_one(db_table_name
,
364 update_dict
=update_dict
,
368 async def waitfor_related_HA(self
, topic
, op_type
, op_id
=None):
370 Wait for any pending related HA tasks
373 # Backward compatibility
374 if not (self
._is
_service
_type
_HA
(topic
) or self
._is
_account
_type
_HA
(topic
)) and (op_id
is None):
378 db_table_name
= self
.topic2dbtable_dict
.get(topic
)
381 _id
= self
._get
_instance
_id
_HA
(topic
, op_type
, op_id
)
382 _filter
= {"_id": _id
}
383 db_lcmop
= self
.db
.get_one(db_table_name
,
389 # Set DB _filter for querying any related process state
390 _filter
= self
._get
_waitfor
_filter
_HA
(db_lcmop
, topic
, op_type
, op_id
)
392 # For HA, get list of tasks from DB instead of from dictionary (in-memory) variable.
393 timeout_wait_for_task
= 3600 # Max time (seconds) to wait for a related task to finish
394 # interval_wait_for_task = 30 # A too long polling interval slows things down considerably
395 interval_wait_for_task
= 10 # Interval in seconds for polling related tasks
396 time_left
= timeout_wait_for_task
397 old_num_related_tasks
= 0
399 # Get related tasks (operations within the same instance as this) which are
400 # still running (operationState='PROCESSING') and which were started before this task.
401 # In the case of op_type='ANY', get any related tasks with operationState='PROCESSING', ignore timestamps.
402 db_waitfor_related_task
= self
.db
.get_list(db_table_name
,
404 new_num_related_tasks
= len(db_waitfor_related_task
)
405 # If there are no related tasks, there is nothing to wait for, so return.
406 if not new_num_related_tasks
:
408 # If number of pending related tasks have changed,
409 # update the 'detailed-status' field and log the change.
410 # Do NOT update the 'detailed-status' for SDNC-associated-to-VIM operations ('ANY').
411 if (op_type
!= 'ANY') and (new_num_related_tasks
!= old_num_related_tasks
):
412 step
= "Waiting for {} related tasks to be completed.".format(new_num_related_tasks
)
414 q_filter
= {'_id': _id
}
416 if self
._is
_service
_type
_HA
(topic
):
417 update_dict
= {'detailed-status': step
}
419 elif self
._is
_account
_type
_HA
(topic
):
420 _
, op_index
= self
._get
_account
_and
_op
_HA
(op_id
)
421 update_dict
= {'_admin.operations.{}.detailed-status'.format(op_index
): step
}
422 self
.logger
.debug("Task {} operation={} {}".format(topic
, _id
, step
))
423 self
.db
.set_one(db_table_name
,
425 update_dict
=update_dict
,
427 old_num_related_tasks
= new_num_related_tasks
428 time_left
-= interval_wait_for_task
431 "Timeout ({}) when waiting for related tasks to be completed".format(
432 timeout_wait_for_task
))
433 await asyncio
.sleep(interval_wait_for_task
)