blob: 51d206ef1489db37bc620805deed0fcc69c4d59e [file] [log] [blame]
tierno1d213f42020-04-24 14:02:51 +00001# -*- coding: utf-8 -*-
2
3##
4# Copyright 2020 Telefonica Investigacion y Desarrollo, S.A.U.
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#
18##
19
20""""
21This is thread that interacts with a VIM. It processes TASKs sequentially against a single VIM.
22The tasks are stored at database in table ro_tasks
23A single ro_task refers to a VIM element (flavor, image, network, ...).
24A ro_task can contain several 'tasks', each one with a target, where to store the results
25"""
26
sousaedu80135b92021-02-17 15:05:18 +010027from copy import deepcopy
28from http import HTTPStatus
sousaedu049cbb12022-01-05 11:39:35 +000029import logging
sousaedu80135b92021-02-17 15:05:18 +010030from os import mkdir
sousaedu049cbb12022-01-05 11:39:35 +000031import queue
sousaedu80135b92021-02-17 15:05:18 +010032from shutil import rmtree
sousaedu049cbb12022-01-05 11:39:35 +000033import threading
34import time
sousaedu80135b92021-02-17 15:05:18 +010035from unittest.mock import Mock
36
sousaedu049cbb12022-01-05 11:39:35 +000037from importlib_metadata import entry_points
tierno1d213f42020-04-24 14:02:51 +000038from osm_common.dbbase import DbException
tiernof1b640f2020-12-09 15:06:01 +000039from osm_ng_ro.vim_admin import LockRenew
sousaedu049cbb12022-01-05 11:39:35 +000040from osm_ro_plugin import sdnconn, vimconn
41from osm_ro_plugin.sdn_dummy import SdnDummyConnector
42from osm_ro_plugin.vim_dummy import VimDummyConnector
43import yaml
tierno1d213f42020-04-24 14:02:51 +000044
45__author__ = "Alfonso Tierno"
46__date__ = "$28-Sep-2017 12:07:15$"
47
48
49def deep_get(target_dict, *args, **kwargs):
50 """
51 Get a value from target_dict entering in the nested keys. If keys does not exist, it returns None
52 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
53 :param target_dict: dictionary to be read
54 :param args: list of keys to read from target_dict
55 :param kwargs: only can contain default=value to return if key is not present in the nested dictionary
56 :return: The wanted value if exist, None or default otherwise
57 """
58 for key in args:
59 if not isinstance(target_dict, dict) or key not in target_dict:
60 return kwargs.get("default")
61 target_dict = target_dict[key]
62 return target_dict
63
64
65class NsWorkerException(Exception):
66 pass
67
68
69class FailingConnector:
70 def __init__(self, error_msg):
71 self.error_msg = error_msg
sousaedu80135b92021-02-17 15:05:18 +010072
tierno1d213f42020-04-24 14:02:51 +000073 for method in dir(vimconn.VimConnector):
74 if method[0] != "_":
sousaedu80135b92021-02-17 15:05:18 +010075 setattr(
76 self, method, Mock(side_effect=vimconn.VimConnException(error_msg))
77 )
78
tierno70eeb182020-10-19 16:38:00 +000079 for method in dir(sdnconn.SdnConnectorBase):
80 if method[0] != "_":
sousaedu80135b92021-02-17 15:05:18 +010081 setattr(
82 self, method, Mock(side_effect=sdnconn.SdnConnectorError(error_msg))
83 )
tierno1d213f42020-04-24 14:02:51 +000084
85
86class NsWorkerExceptionNotFound(NsWorkerException):
87 pass
88
89
tierno70eeb182020-10-19 16:38:00 +000090class VimInteractionBase:
sousaedu80135b92021-02-17 15:05:18 +010091 """Base class to call VIM/SDN for creating, deleting and refresh networks, VMs, flavors, ...
tierno70eeb182020-10-19 16:38:00 +000092 It implements methods that does nothing and return ok"""
sousaedu80135b92021-02-17 15:05:18 +010093
tierno70eeb182020-10-19 16:38:00 +000094 def __init__(self, db, my_vims, db_vims, logger):
tierno1d213f42020-04-24 14:02:51 +000095 self.db = db
tierno70eeb182020-10-19 16:38:00 +000096 self.logger = logger
97 self.my_vims = my_vims
98 self.db_vims = db_vims
tierno1d213f42020-04-24 14:02:51 +000099
tierno70eeb182020-10-19 16:38:00 +0000100 def new(self, ro_task, task_index, task_depends):
101 return "BUILD", {}
tierno1d213f42020-04-24 14:02:51 +0000102
tierno70eeb182020-10-19 16:38:00 +0000103 def refresh(self, ro_task):
104 """skip calling VIM to get image, flavor status. Assumes ok"""
tierno1d213f42020-04-24 14:02:51 +0000105 if ro_task["vim_info"]["vim_status"] == "VIM_ERROR":
106 return "FAILED", {}
sousaedu80135b92021-02-17 15:05:18 +0100107
tierno1d213f42020-04-24 14:02:51 +0000108 return "DONE", {}
109
tierno70eeb182020-10-19 16:38:00 +0000110 def delete(self, ro_task, task_index):
111 """skip calling VIM to delete image. Assumes ok"""
tierno1d213f42020-04-24 14:02:51 +0000112 return "DONE", {}
113
tierno70eeb182020-10-19 16:38:00 +0000114 def exec(self, ro_task, task_index, task_depends):
115 return "DONE", None, None
tierno1d213f42020-04-24 14:02:51 +0000116
tierno1d213f42020-04-24 14:02:51 +0000117
tierno70eeb182020-10-19 16:38:00 +0000118class VimInteractionNet(VimInteractionBase):
tierno70eeb182020-10-19 16:38:00 +0000119 def new(self, ro_task, task_index, task_depends):
tierno1d213f42020-04-24 14:02:51 +0000120 vim_net_id = None
121 task = ro_task["tasks"][task_index]
122 task_id = task["task_id"]
123 created = False
124 created_items = {}
125 target_vim = self.my_vims[ro_task["target_id"]]
aticige9a26f22021-12-10 12:59:20 +0300126 mgmtnet = False
127 mgmtnet_defined_in_vim = False
sousaedu80135b92021-02-17 15:05:18 +0100128
tierno1d213f42020-04-24 14:02:51 +0000129 try:
130 # FIND
131 if task.get("find_params"):
132 # if management, get configuration of VIM
133 if task["find_params"].get("filter_dict"):
134 vim_filter = task["find_params"]["filter_dict"]
aticige9a26f22021-12-10 12:59:20 +0300135 # management network
sousaedu80135b92021-02-17 15:05:18 +0100136 elif task["find_params"].get("mgmt"):
aticige9a26f22021-12-10 12:59:20 +0300137 mgmtnet = True
sousaedu80135b92021-02-17 15:05:18 +0100138 if deep_get(
139 self.db_vims[ro_task["target_id"]],
140 "config",
141 "management_network_id",
142 ):
aticige9a26f22021-12-10 12:59:20 +0300143 mgmtnet_defined_in_vim = True
sousaedu80135b92021-02-17 15:05:18 +0100144 vim_filter = {
145 "id": self.db_vims[ro_task["target_id"]]["config"][
146 "management_network_id"
147 ]
148 }
149 elif deep_get(
150 self.db_vims[ro_task["target_id"]],
151 "config",
152 "management_network_name",
153 ):
aticige9a26f22021-12-10 12:59:20 +0300154 mgmtnet_defined_in_vim = True
sousaedu80135b92021-02-17 15:05:18 +0100155 vim_filter = {
156 "name": self.db_vims[ro_task["target_id"]]["config"][
157 "management_network_name"
158 ]
159 }
tierno1d213f42020-04-24 14:02:51 +0000160 else:
161 vim_filter = {"name": task["find_params"]["name"]}
162 else:
sousaedu80135b92021-02-17 15:05:18 +0100163 raise NsWorkerExceptionNotFound(
164 "Invalid find_params for new_net {}".format(task["find_params"])
165 )
tierno1d213f42020-04-24 14:02:51 +0000166
167 vim_nets = target_vim.get_network_list(vim_filter)
168 if not vim_nets and not task.get("params"):
aticige9a26f22021-12-10 12:59:20 +0300169 # If there is mgmt-network in the descriptor,
170 # there is no mapping of that network to a VIM network in the descriptor,
171 # also there is no mapping in the "--config" parameter or at VIM creation;
172 # that mgmt-network will be created.
173 if mgmtnet and not mgmtnet_defined_in_vim:
174 net_name = (
175 vim_filter.get("name")
176 if vim_filter.get("name")
177 else vim_filter.get("id")[:16]
sousaedu80135b92021-02-17 15:05:18 +0100178 )
aticige9a26f22021-12-10 12:59:20 +0300179 vim_net_id, created_items = target_vim.new_network(
180 net_name, None
181 )
182 self.logger.debug(
183 "Created mgmt network vim_net_id: {}".format(vim_net_id)
184 )
185 created = True
186 else:
187 raise NsWorkerExceptionNotFound(
188 "Network not found with this criteria: '{}'".format(
189 task.get("find_params")
190 )
191 )
tierno1d213f42020-04-24 14:02:51 +0000192 elif len(vim_nets) > 1:
193 raise NsWorkerException(
sousaedu80135b92021-02-17 15:05:18 +0100194 "More than one network found with this criteria: '{}'".format(
195 task["find_params"]
196 )
197 )
198
tierno1d213f42020-04-24 14:02:51 +0000199 if vim_nets:
200 vim_net_id = vim_nets[0]["id"]
201 else:
202 # CREATE
203 params = task["params"]
204 vim_net_id, created_items = target_vim.new_network(**params)
205 created = True
206
sousaedu80135b92021-02-17 15:05:18 +0100207 ro_vim_item_update = {
208 "vim_id": vim_net_id,
209 "vim_status": "BUILD",
210 "created": created,
211 "created_items": created_items,
212 "vim_details": None,
213 }
tierno1d213f42020-04-24 14:02:51 +0000214 self.logger.debug(
sousaedu80135b92021-02-17 15:05:18 +0100215 "task={} {} new-net={} created={}".format(
216 task_id, ro_task["target_id"], vim_net_id, created
217 )
218 )
219
tierno1d213f42020-04-24 14:02:51 +0000220 return "BUILD", ro_vim_item_update
221 except (vimconn.VimConnException, NsWorkerException) as e:
sousaedu80135b92021-02-17 15:05:18 +0100222 self.logger.error(
223 "task={} vim={} new-net: {}".format(task_id, ro_task["target_id"], e)
224 )
225 ro_vim_item_update = {
226 "vim_status": "VIM_ERROR",
227 "created": created,
228 "vim_details": str(e),
229 }
230
tierno1d213f42020-04-24 14:02:51 +0000231 return "FAILED", ro_vim_item_update
232
tierno70eeb182020-10-19 16:38:00 +0000233 def refresh(self, ro_task):
tierno1d213f42020-04-24 14:02:51 +0000234 """Call VIM to get network status"""
235 ro_task_id = ro_task["_id"]
236 target_vim = self.my_vims[ro_task["target_id"]]
tierno1d213f42020-04-24 14:02:51 +0000237 vim_id = ro_task["vim_info"]["vim_id"]
238 net_to_refresh_list = [vim_id]
sousaedu80135b92021-02-17 15:05:18 +0100239
tierno1d213f42020-04-24 14:02:51 +0000240 try:
241 vim_dict = target_vim.refresh_nets_status(net_to_refresh_list)
242 vim_info = vim_dict[vim_id]
sousaedu80135b92021-02-17 15:05:18 +0100243
tierno1d213f42020-04-24 14:02:51 +0000244 if vim_info["status"] == "ACTIVE":
245 task_status = "DONE"
246 elif vim_info["status"] == "BUILD":
247 task_status = "BUILD"
248 else:
249 task_status = "FAILED"
250 except vimconn.VimConnException as e:
251 # Mark all tasks at VIM_ERROR status
sousaedu80135b92021-02-17 15:05:18 +0100252 self.logger.error(
253 "ro_task={} vim={} get-net={}: {}".format(
254 ro_task_id, ro_task["target_id"], vim_id, e
255 )
256 )
tierno1d213f42020-04-24 14:02:51 +0000257 vim_info = {"status": "VIM_ERROR", "error_msg": str(e)}
258 task_status = "FAILED"
259
260 ro_vim_item_update = {}
261 if ro_task["vim_info"]["vim_status"] != vim_info["status"]:
262 ro_vim_item_update["vim_status"] = vim_info["status"]
sousaedu80135b92021-02-17 15:05:18 +0100263
tierno1d213f42020-04-24 14:02:51 +0000264 if ro_task["vim_info"]["vim_name"] != vim_info.get("name"):
265 ro_vim_item_update["vim_name"] = vim_info.get("name")
sousaedu80135b92021-02-17 15:05:18 +0100266
tierno1d213f42020-04-24 14:02:51 +0000267 if vim_info["status"] in ("ERROR", "VIM_ERROR"):
tierno70eeb182020-10-19 16:38:00 +0000268 if ro_task["vim_info"]["vim_details"] != vim_info.get("error_msg"):
269 ro_vim_item_update["vim_details"] = vim_info.get("error_msg")
tierno1d213f42020-04-24 14:02:51 +0000270 elif vim_info["status"] == "DELETED":
271 ro_vim_item_update["vim_id"] = None
272 ro_vim_item_update["vim_details"] = "Deleted externally"
273 else:
274 if ro_task["vim_info"]["vim_details"] != vim_info["vim_info"]:
275 ro_vim_item_update["vim_details"] = vim_info["vim_info"]
sousaedu80135b92021-02-17 15:05:18 +0100276
tierno1d213f42020-04-24 14:02:51 +0000277 if ro_vim_item_update:
sousaedu80135b92021-02-17 15:05:18 +0100278 self.logger.debug(
279 "ro_task={} {} get-net={}: status={} {}".format(
280 ro_task_id,
281 ro_task["target_id"],
282 vim_id,
283 ro_vim_item_update.get("vim_status"),
284 ro_vim_item_update.get("vim_details")
285 if ro_vim_item_update.get("vim_status") != "ACTIVE"
286 else "",
287 )
288 )
289
tierno1d213f42020-04-24 14:02:51 +0000290 return task_status, ro_vim_item_update
291
tierno70eeb182020-10-19 16:38:00 +0000292 def delete(self, ro_task, task_index):
tierno1d213f42020-04-24 14:02:51 +0000293 task = ro_task["tasks"][task_index]
294 task_id = task["task_id"]
295 net_vim_id = ro_task["vim_info"]["vim_id"]
sousaedu80135b92021-02-17 15:05:18 +0100296 ro_vim_item_update_ok = {
297 "vim_status": "DELETED",
298 "created": False,
299 "vim_details": "DELETED",
300 "vim_id": None,
301 }
302
tierno1d213f42020-04-24 14:02:51 +0000303 try:
304 if net_vim_id or ro_task["vim_info"]["created_items"]:
305 target_vim = self.my_vims[ro_task["target_id"]]
sousaedu80135b92021-02-17 15:05:18 +0100306 target_vim.delete_network(
307 net_vim_id, ro_task["vim_info"]["created_items"]
308 )
tierno1d213f42020-04-24 14:02:51 +0000309 except vimconn.VimConnNotFoundException:
310 ro_vim_item_update_ok["vim_details"] = "already deleted"
tierno1d213f42020-04-24 14:02:51 +0000311 except vimconn.VimConnException as e:
sousaedu80135b92021-02-17 15:05:18 +0100312 self.logger.error(
313 "ro_task={} vim={} del-net={}: {}".format(
314 ro_task["_id"], ro_task["target_id"], net_vim_id, e
315 )
316 )
317 ro_vim_item_update = {
318 "vim_status": "VIM_ERROR",
319 "vim_details": "Error while deleting: {}".format(e),
320 }
321
tierno1d213f42020-04-24 14:02:51 +0000322 return "FAILED", ro_vim_item_update
323
sousaedu80135b92021-02-17 15:05:18 +0100324 self.logger.debug(
325 "task={} {} del-net={} {}".format(
326 task_id,
327 ro_task["target_id"],
328 net_vim_id,
329 ro_vim_item_update_ok.get("vim_details", ""),
330 )
331 )
332
tierno1d213f42020-04-24 14:02:51 +0000333 return "DONE", ro_vim_item_update_ok
334
tierno70eeb182020-10-19 16:38:00 +0000335
336class VimInteractionVdu(VimInteractionBase):
sousaedu80135b92021-02-17 15:05:18 +0100337 max_retries_inject_ssh_key = 20 # 20 times
338 time_retries_inject_ssh_key = 30 # wevery 30 seconds
tierno70eeb182020-10-19 16:38:00 +0000339
340 def new(self, ro_task, task_index, task_depends):
tierno1d213f42020-04-24 14:02:51 +0000341 task = ro_task["tasks"][task_index]
342 task_id = task["task_id"]
343 created = False
344 created_items = {}
345 target_vim = self.my_vims[ro_task["target_id"]]
sousaedu80135b92021-02-17 15:05:18 +0100346
tierno1d213f42020-04-24 14:02:51 +0000347 try:
348 created = True
349 params = task["params"]
350 params_copy = deepcopy(params)
351 net_list = params_copy["net_list"]
sousaedu80135b92021-02-17 15:05:18 +0100352
tierno1d213f42020-04-24 14:02:51 +0000353 for net in net_list:
sousaedu80135b92021-02-17 15:05:18 +0100354 # change task_id into network_id
355 if "net_id" in net and net["net_id"].startswith("TASK-"):
tierno1d213f42020-04-24 14:02:51 +0000356 network_id = task_depends[net["net_id"]]
sousaedu80135b92021-02-17 15:05:18 +0100357
tierno1d213f42020-04-24 14:02:51 +0000358 if not network_id:
sousaedu80135b92021-02-17 15:05:18 +0100359 raise NsWorkerException(
360 "Cannot create VM because depends on a network not created or found "
361 "for {}".format(net["net_id"])
362 )
363
tierno1d213f42020-04-24 14:02:51 +0000364 net["net_id"] = network_id
sousaedu80135b92021-02-17 15:05:18 +0100365
tierno1d213f42020-04-24 14:02:51 +0000366 if params_copy["image_id"].startswith("TASK-"):
367 params_copy["image_id"] = task_depends[params_copy["image_id"]]
sousaedu80135b92021-02-17 15:05:18 +0100368
tierno1d213f42020-04-24 14:02:51 +0000369 if params_copy["flavor_id"].startswith("TASK-"):
370 params_copy["flavor_id"] = task_depends[params_copy["flavor_id"]]
371
Alexis Romerob70f4ed2022-03-11 18:00:49 +0100372 affinity_group_list = params_copy["affinity_group_list"]
373 for affinity_group in affinity_group_list:
374 # change task_id into affinity_group_id
375 if "affinity_group_id" in affinity_group and affinity_group[
376 "affinity_group_id"
377 ].startswith("TASK-"):
378 affinity_group_id = task_depends[
379 affinity_group["affinity_group_id"]
380 ]
381
382 if not affinity_group_id:
383 raise NsWorkerException(
384 "found for {}".format(affinity_group["affinity_group_id"])
385 )
386
387 affinity_group["affinity_group_id"] = affinity_group_id
388
tierno1d213f42020-04-24 14:02:51 +0000389 vim_vm_id, created_items = target_vim.new_vminstance(**params_copy)
390 interfaces = [iface["vim_id"] for iface in params_copy["net_list"]]
391
sousaedu80135b92021-02-17 15:05:18 +0100392 ro_vim_item_update = {
393 "vim_id": vim_vm_id,
394 "vim_status": "BUILD",
395 "created": created,
396 "created_items": created_items,
397 "vim_details": None,
398 "interfaces_vim_ids": interfaces,
399 "interfaces": [],
400 }
tierno1d213f42020-04-24 14:02:51 +0000401 self.logger.debug(
sousaedu80135b92021-02-17 15:05:18 +0100402 "task={} {} new-vm={} created={}".format(
403 task_id, ro_task["target_id"], vim_vm_id, created
404 )
405 )
406
tierno1d213f42020-04-24 14:02:51 +0000407 return "BUILD", ro_vim_item_update
408 except (vimconn.VimConnException, NsWorkerException) as e:
sousaedu80135b92021-02-17 15:05:18 +0100409 self.logger.error(
410 "task={} {} new-vm: {}".format(task_id, ro_task["target_id"], e)
411 )
412 ro_vim_item_update = {
413 "vim_status": "VIM_ERROR",
414 "created": created,
415 "vim_details": str(e),
416 }
417
tierno1d213f42020-04-24 14:02:51 +0000418 return "FAILED", ro_vim_item_update
419
tierno70eeb182020-10-19 16:38:00 +0000420 def delete(self, ro_task, task_index):
tierno1d213f42020-04-24 14:02:51 +0000421 task = ro_task["tasks"][task_index]
422 task_id = task["task_id"]
423 vm_vim_id = ro_task["vim_info"]["vim_id"]
sousaedu80135b92021-02-17 15:05:18 +0100424 ro_vim_item_update_ok = {
425 "vim_status": "DELETED",
426 "created": False,
427 "vim_details": "DELETED",
428 "vim_id": None,
429 }
430
tierno1d213f42020-04-24 14:02:51 +0000431 try:
palaciosj8f2060b2022-02-24 12:05:59 +0000432 self.logger.debug(
433 "delete_vminstance: vm_vim_id={} created_items={}".format(
434 vm_vim_id, ro_task["vim_info"]["created_items"]
435 )
436 )
tierno1d213f42020-04-24 14:02:51 +0000437 if vm_vim_id or ro_task["vim_info"]["created_items"]:
438 target_vim = self.my_vims[ro_task["target_id"]]
sousaedu80135b92021-02-17 15:05:18 +0100439 target_vim.delete_vminstance(
palaciosj8f2060b2022-02-24 12:05:59 +0000440 vm_vim_id,
441 ro_task["vim_info"]["created_items"],
442 ro_task["vim_info"].get("volumes_to_hold", []),
sousaedu80135b92021-02-17 15:05:18 +0100443 )
tierno1d213f42020-04-24 14:02:51 +0000444 except vimconn.VimConnNotFoundException:
445 ro_vim_item_update_ok["vim_details"] = "already deleted"
tierno1d213f42020-04-24 14:02:51 +0000446 except vimconn.VimConnException as e:
sousaedu80135b92021-02-17 15:05:18 +0100447 self.logger.error(
448 "ro_task={} vim={} del-vm={}: {}".format(
449 ro_task["_id"], ro_task["target_id"], vm_vim_id, e
450 )
451 )
452 ro_vim_item_update = {
453 "vim_status": "VIM_ERROR",
454 "vim_details": "Error while deleting: {}".format(e),
455 }
456
tierno1d213f42020-04-24 14:02:51 +0000457 return "FAILED", ro_vim_item_update
458
sousaedu80135b92021-02-17 15:05:18 +0100459 self.logger.debug(
460 "task={} {} del-vm={} {}".format(
461 task_id,
462 ro_task["target_id"],
463 vm_vim_id,
464 ro_vim_item_update_ok.get("vim_details", ""),
465 )
466 )
467
tierno1d213f42020-04-24 14:02:51 +0000468 return "DONE", ro_vim_item_update_ok
469
tierno70eeb182020-10-19 16:38:00 +0000470 def refresh(self, ro_task):
tierno1d213f42020-04-24 14:02:51 +0000471 """Call VIM to get vm status"""
472 ro_task_id = ro_task["_id"]
473 target_vim = self.my_vims[ro_task["target_id"]]
tierno1d213f42020-04-24 14:02:51 +0000474 vim_id = ro_task["vim_info"]["vim_id"]
sousaedu80135b92021-02-17 15:05:18 +0100475
tierno1d213f42020-04-24 14:02:51 +0000476 if not vim_id:
477 return None, None
sousaedu80135b92021-02-17 15:05:18 +0100478
tierno1d213f42020-04-24 14:02:51 +0000479 vm_to_refresh_list = [vim_id]
480 try:
481 vim_dict = target_vim.refresh_vms_status(vm_to_refresh_list)
482 vim_info = vim_dict[vim_id]
sousaedu80135b92021-02-17 15:05:18 +0100483
tierno1d213f42020-04-24 14:02:51 +0000484 if vim_info["status"] == "ACTIVE":
485 task_status = "DONE"
486 elif vim_info["status"] == "BUILD":
487 task_status = "BUILD"
488 else:
489 task_status = "FAILED"
sousaedu80135b92021-02-17 15:05:18 +0100490
tierno70eeb182020-10-19 16:38:00 +0000491 # try to load and parse vim_information
492 try:
493 vim_info_info = yaml.safe_load(vim_info["vim_info"])
494 if vim_info_info.get("name"):
495 vim_info["name"] = vim_info_info["name"]
496 except Exception:
497 pass
tierno1d213f42020-04-24 14:02:51 +0000498 except vimconn.VimConnException as e:
499 # Mark all tasks at VIM_ERROR status
sousaedu80135b92021-02-17 15:05:18 +0100500 self.logger.error(
501 "ro_task={} vim={} get-vm={}: {}".format(
502 ro_task_id, ro_task["target_id"], vim_id, e
503 )
504 )
tierno1d213f42020-04-24 14:02:51 +0000505 vim_info = {"status": "VIM_ERROR", "error_msg": str(e)}
506 task_status = "FAILED"
507
508 ro_vim_item_update = {}
sousaedu80135b92021-02-17 15:05:18 +0100509
tierno70eeb182020-10-19 16:38:00 +0000510 # Interfaces cannot be present if e.g. VM is not present, that is status=DELETED
tierno1d213f42020-04-24 14:02:51 +0000511 vim_interfaces = []
tierno70eeb182020-10-19 16:38:00 +0000512 if vim_info.get("interfaces"):
513 for vim_iface_id in ro_task["vim_info"]["interfaces_vim_ids"]:
sousaedu80135b92021-02-17 15:05:18 +0100514 iface = next(
515 (
516 iface
517 for iface in vim_info["interfaces"]
518 if vim_iface_id == iface["vim_interface_id"]
519 ),
520 None,
521 )
tierno70eeb182020-10-19 16:38:00 +0000522 # if iface:
523 # iface.pop("vim_info", None)
524 vim_interfaces.append(iface)
tierno1d213f42020-04-24 14:02:51 +0000525
sousaedu80135b92021-02-17 15:05:18 +0100526 task_create = next(
527 t
528 for t in ro_task["tasks"]
529 if t and t["action"] == "CREATE" and t["status"] != "FINISHED"
530 )
tierno70eeb182020-10-19 16:38:00 +0000531 if vim_interfaces and task_create.get("mgmt_vnf_interface") is not None:
sousaedu80135b92021-02-17 15:05:18 +0100532 vim_interfaces[task_create["mgmt_vnf_interface"]][
533 "mgmt_vnf_interface"
534 ] = True
535
536 mgmt_vdu_iface = task_create.get(
537 "mgmt_vdu_interface", task_create.get("mgmt_vnf_interface", 0)
538 )
tierno70eeb182020-10-19 16:38:00 +0000539 if vim_interfaces:
540 vim_interfaces[mgmt_vdu_iface]["mgmt_vdu_interface"] = True
tierno1d213f42020-04-24 14:02:51 +0000541
542 if ro_task["vim_info"]["interfaces"] != vim_interfaces:
543 ro_vim_item_update["interfaces"] = vim_interfaces
sousaedu80135b92021-02-17 15:05:18 +0100544
tierno1d213f42020-04-24 14:02:51 +0000545 if ro_task["vim_info"]["vim_status"] != vim_info["status"]:
546 ro_vim_item_update["vim_status"] = vim_info["status"]
sousaedu80135b92021-02-17 15:05:18 +0100547
tierno1d213f42020-04-24 14:02:51 +0000548 if ro_task["vim_info"]["vim_name"] != vim_info.get("name"):
549 ro_vim_item_update["vim_name"] = vim_info.get("name")
sousaedu80135b92021-02-17 15:05:18 +0100550
tierno1d213f42020-04-24 14:02:51 +0000551 if vim_info["status"] in ("ERROR", "VIM_ERROR"):
tierno70eeb182020-10-19 16:38:00 +0000552 if ro_task["vim_info"]["vim_details"] != vim_info.get("error_msg"):
553 ro_vim_item_update["vim_details"] = vim_info.get("error_msg")
tierno1d213f42020-04-24 14:02:51 +0000554 elif vim_info["status"] == "DELETED":
555 ro_vim_item_update["vim_id"] = None
556 ro_vim_item_update["vim_details"] = "Deleted externally"
557 else:
558 if ro_task["vim_info"]["vim_details"] != vim_info["vim_info"]:
559 ro_vim_item_update["vim_details"] = vim_info["vim_info"]
sousaedu80135b92021-02-17 15:05:18 +0100560
tierno1d213f42020-04-24 14:02:51 +0000561 if ro_vim_item_update:
sousaedu80135b92021-02-17 15:05:18 +0100562 self.logger.debug(
563 "ro_task={} {} get-vm={}: status={} {}".format(
564 ro_task_id,
565 ro_task["target_id"],
566 vim_id,
567 ro_vim_item_update.get("vim_status"),
568 ro_vim_item_update.get("vim_details")
569 if ro_vim_item_update.get("vim_status") != "ACTIVE"
570 else "",
571 )
572 )
573
tierno1d213f42020-04-24 14:02:51 +0000574 return task_status, ro_vim_item_update
575
tierno70eeb182020-10-19 16:38:00 +0000576 def exec(self, ro_task, task_index, task_depends):
tierno1d213f42020-04-24 14:02:51 +0000577 task = ro_task["tasks"][task_index]
578 task_id = task["task_id"]
579 target_vim = self.my_vims[ro_task["target_id"]]
tierno70eeb182020-10-19 16:38:00 +0000580 db_task_update = {"retries": 0}
581 retries = task.get("retries", 0)
sousaedu80135b92021-02-17 15:05:18 +0100582
tierno1d213f42020-04-24 14:02:51 +0000583 try:
584 params = task["params"]
585 params_copy = deepcopy(params)
sousaedu80135b92021-02-17 15:05:18 +0100586 params_copy["ro_key"] = self.db.decrypt(
587 params_copy.pop("private_key"),
588 params_copy.pop("schema_version"),
589 params_copy.pop("salt"),
590 )
tierno70eeb182020-10-19 16:38:00 +0000591 params_copy["ip_addr"] = params_copy.pop("ip_address")
tierno1d213f42020-04-24 14:02:51 +0000592 target_vim.inject_user_key(**params_copy)
593 self.logger.debug(
sousaedu80135b92021-02-17 15:05:18 +0100594 "task={} {} action-vm=inject_key".format(task_id, ro_task["target_id"])
595 )
596
597 return (
598 "DONE",
599 None,
600 db_task_update,
601 ) # params_copy["key"]
tierno1d213f42020-04-24 14:02:51 +0000602 except (vimconn.VimConnException, NsWorkerException) as e:
tierno70eeb182020-10-19 16:38:00 +0000603 retries += 1
sousaedu80135b92021-02-17 15:05:18 +0100604
tierno70eeb182020-10-19 16:38:00 +0000605 if retries < self.max_retries_inject_ssh_key:
sousaedu80135b92021-02-17 15:05:18 +0100606 return (
607 "BUILD",
608 None,
609 {
610 "retries": retries,
611 "next_retry": self.time_retries_inject_ssh_key,
612 },
613 )
614
615 self.logger.error(
616 "task={} {} inject-ssh-key: {}".format(task_id, ro_task["target_id"], e)
617 )
tierno1d213f42020-04-24 14:02:51 +0000618 ro_vim_item_update = {"vim_details": str(e)}
sousaedu80135b92021-02-17 15:05:18 +0100619
tierno70eeb182020-10-19 16:38:00 +0000620 return "FAILED", ro_vim_item_update, db_task_update
621
622
623class VimInteractionImage(VimInteractionBase):
tierno70eeb182020-10-19 16:38:00 +0000624 def new(self, ro_task, task_index, task_depends):
625 task = ro_task["tasks"][task_index]
626 task_id = task["task_id"]
627 created = False
628 created_items = {}
629 target_vim = self.my_vims[ro_task["target_id"]]
sousaedu80135b92021-02-17 15:05:18 +0100630
tierno70eeb182020-10-19 16:38:00 +0000631 try:
632 # FIND
633 if task.get("find_params"):
634 vim_images = target_vim.get_image_list(**task["find_params"])
sousaedu80135b92021-02-17 15:05:18 +0100635
tierno70eeb182020-10-19 16:38:00 +0000636 if not vim_images:
sousaedu80135b92021-02-17 15:05:18 +0100637 raise NsWorkerExceptionNotFound(
638 "Image not found with this criteria: '{}'".format(
639 task["find_params"]
640 )
641 )
tierno70eeb182020-10-19 16:38:00 +0000642 elif len(vim_images) > 1:
643 raise NsWorkerException(
sousaeduee6a6202021-05-11 13:22:37 +0200644 "More than one image found with this criteria: '{}'".format(
sousaedu80135b92021-02-17 15:05:18 +0100645 task["find_params"]
646 )
647 )
tierno70eeb182020-10-19 16:38:00 +0000648 else:
649 vim_image_id = vim_images[0]["id"]
650
sousaedu80135b92021-02-17 15:05:18 +0100651 ro_vim_item_update = {
652 "vim_id": vim_image_id,
653 "vim_status": "DONE",
654 "created": created,
655 "created_items": created_items,
656 "vim_details": None,
657 }
tierno70eeb182020-10-19 16:38:00 +0000658 self.logger.debug(
sousaedu80135b92021-02-17 15:05:18 +0100659 "task={} {} new-image={} created={}".format(
660 task_id, ro_task["target_id"], vim_image_id, created
661 )
662 )
663
tierno70eeb182020-10-19 16:38:00 +0000664 return "DONE", ro_vim_item_update
665 except (NsWorkerException, vimconn.VimConnException) as e:
sousaedu80135b92021-02-17 15:05:18 +0100666 self.logger.error(
667 "task={} {} new-image: {}".format(task_id, ro_task["target_id"], e)
668 )
669 ro_vim_item_update = {
670 "vim_status": "VIM_ERROR",
671 "created": created,
672 "vim_details": str(e),
673 }
674
tierno1d213f42020-04-24 14:02:51 +0000675 return "FAILED", ro_vim_item_update
676
tierno70eeb182020-10-19 16:38:00 +0000677
678class VimInteractionFlavor(VimInteractionBase):
tierno70eeb182020-10-19 16:38:00 +0000679 def delete(self, ro_task, task_index):
680 task = ro_task["tasks"][task_index]
681 task_id = task["task_id"]
682 flavor_vim_id = ro_task["vim_info"]["vim_id"]
sousaedu80135b92021-02-17 15:05:18 +0100683 ro_vim_item_update_ok = {
684 "vim_status": "DELETED",
685 "created": False,
686 "vim_details": "DELETED",
687 "vim_id": None,
688 }
689
tierno70eeb182020-10-19 16:38:00 +0000690 try:
691 if flavor_vim_id:
692 target_vim = self.my_vims[ro_task["target_id"]]
693 target_vim.delete_flavor(flavor_vim_id)
tierno70eeb182020-10-19 16:38:00 +0000694 except vimconn.VimConnNotFoundException:
695 ro_vim_item_update_ok["vim_details"] = "already deleted"
tierno70eeb182020-10-19 16:38:00 +0000696 except vimconn.VimConnException as e:
sousaedu80135b92021-02-17 15:05:18 +0100697 self.logger.error(
698 "ro_task={} vim={} del-flavor={}: {}".format(
699 ro_task["_id"], ro_task["target_id"], flavor_vim_id, e
700 )
701 )
702 ro_vim_item_update = {
703 "vim_status": "VIM_ERROR",
704 "vim_details": "Error while deleting: {}".format(e),
705 }
706
tierno70eeb182020-10-19 16:38:00 +0000707 return "FAILED", ro_vim_item_update
708
sousaedu80135b92021-02-17 15:05:18 +0100709 self.logger.debug(
710 "task={} {} del-flavor={} {}".format(
711 task_id,
712 ro_task["target_id"],
713 flavor_vim_id,
714 ro_vim_item_update_ok.get("vim_details", ""),
715 )
716 )
717
tierno70eeb182020-10-19 16:38:00 +0000718 return "DONE", ro_vim_item_update_ok
719
720 def new(self, ro_task, task_index, task_depends):
721 task = ro_task["tasks"][task_index]
722 task_id = task["task_id"]
723 created = False
724 created_items = {}
725 target_vim = self.my_vims[ro_task["target_id"]]
sousaedu80135b92021-02-17 15:05:18 +0100726
tierno70eeb182020-10-19 16:38:00 +0000727 try:
728 # FIND
729 vim_flavor_id = None
sousaedu80135b92021-02-17 15:05:18 +0100730
tierno70eeb182020-10-19 16:38:00 +0000731 if task.get("find_params"):
732 try:
733 flavor_data = task["find_params"]["flavor_data"]
734 vim_flavor_id = target_vim.get_flavor_id_from_data(flavor_data)
735 except vimconn.VimConnNotFoundException:
736 pass
737
738 if not vim_flavor_id and task.get("params"):
739 # CREATE
740 flavor_data = task["params"]["flavor_data"]
741 vim_flavor_id = target_vim.new_flavor(flavor_data)
742 created = True
743
sousaedu80135b92021-02-17 15:05:18 +0100744 ro_vim_item_update = {
745 "vim_id": vim_flavor_id,
746 "vim_status": "DONE",
747 "created": created,
748 "created_items": created_items,
749 "vim_details": None,
750 }
tierno70eeb182020-10-19 16:38:00 +0000751 self.logger.debug(
sousaedu80135b92021-02-17 15:05:18 +0100752 "task={} {} new-flavor={} created={}".format(
753 task_id, ro_task["target_id"], vim_flavor_id, created
754 )
755 )
756
tierno70eeb182020-10-19 16:38:00 +0000757 return "DONE", ro_vim_item_update
758 except (vimconn.VimConnException, NsWorkerException) as e:
sousaedu80135b92021-02-17 15:05:18 +0100759 self.logger.error(
760 "task={} vim={} new-flavor: {}".format(task_id, ro_task["target_id"], e)
761 )
762 ro_vim_item_update = {
763 "vim_status": "VIM_ERROR",
764 "created": created,
765 "vim_details": str(e),
766 }
767
tierno70eeb182020-10-19 16:38:00 +0000768 return "FAILED", ro_vim_item_update
769
770
Alexis Romerob70f4ed2022-03-11 18:00:49 +0100771class VimInteractionAffinityGroup(VimInteractionBase):
772 def delete(self, ro_task, task_index):
773 task = ro_task["tasks"][task_index]
774 task_id = task["task_id"]
775 affinity_group_vim_id = ro_task["vim_info"]["vim_id"]
776 ro_vim_item_update_ok = {
777 "vim_status": "DELETED",
778 "created": False,
779 "vim_details": "DELETED",
780 "vim_id": None,
781 }
782
783 try:
784 if affinity_group_vim_id:
785 target_vim = self.my_vims[ro_task["target_id"]]
786 target_vim.delete_affinity_group(affinity_group_vim_id)
787 except vimconn.VimConnNotFoundException:
788 ro_vim_item_update_ok["vim_details"] = "already deleted"
789 except vimconn.VimConnException as e:
790 self.logger.error(
791 "ro_task={} vim={} del-affinity-or-anti-affinity-group={}: {}".format(
792 ro_task["_id"], ro_task["target_id"], affinity_group_vim_id, e
793 )
794 )
795 ro_vim_item_update = {
796 "vim_status": "VIM_ERROR",
797 "vim_details": "Error while deleting: {}".format(e),
798 }
799
800 return "FAILED", ro_vim_item_update
801
802 self.logger.debug(
803 "task={} {} del-affinity-or-anti-affinity-group={} {}".format(
804 task_id,
805 ro_task["target_id"],
806 affinity_group_vim_id,
807 ro_vim_item_update_ok.get("vim_details", ""),
808 )
809 )
810
811 return "DONE", ro_vim_item_update_ok
812
813 def new(self, ro_task, task_index, task_depends):
814 task = ro_task["tasks"][task_index]
815 task_id = task["task_id"]
816 created = False
817 created_items = {}
818 target_vim = self.my_vims[ro_task["target_id"]]
819
820 try:
821 affinity_group_vim_id = None
Alexis Romero123de182022-04-26 19:24:40 +0200822 affinity_group_data = None
Alexis Romerob70f4ed2022-03-11 18:00:49 +0100823
824 if task.get("params"):
Alexis Romero123de182022-04-26 19:24:40 +0200825 affinity_group_data = task["params"].get("affinity_group_data")
826
827 if affinity_group_data and affinity_group_data.get("vim-affinity-group-id"):
828 try:
829 param_affinity_group_id = task["params"]["affinity_group_data"].get(
830 "vim-affinity-group-id"
831 )
832 affinity_group_vim_id = target_vim.get_affinity_group(
833 param_affinity_group_id
834 ).get("id")
835 except vimconn.VimConnNotFoundException:
836 self.logger.error(
837 "task={} {} new-affinity-or-anti-affinity-group. Provided VIM Affinity Group ID {}"
838 "could not be found at VIM. Creating a new one.".format(
839 task_id, ro_task["target_id"], param_affinity_group_id
840 )
841 )
842
843 if not affinity_group_vim_id and affinity_group_data:
Alexis Romerob70f4ed2022-03-11 18:00:49 +0100844 affinity_group_vim_id = target_vim.new_affinity_group(
845 affinity_group_data
846 )
847 created = True
848
849 ro_vim_item_update = {
850 "vim_id": affinity_group_vim_id,
851 "vim_status": "DONE",
852 "created": created,
853 "created_items": created_items,
854 "vim_details": None,
855 }
856 self.logger.debug(
857 "task={} {} new-affinity-or-anti-affinity-group={} created={}".format(
858 task_id, ro_task["target_id"], affinity_group_vim_id, created
859 )
860 )
861
862 return "DONE", ro_vim_item_update
863 except (vimconn.VimConnException, NsWorkerException) as e:
864 self.logger.error(
865 "task={} vim={} new-affinity-or-anti-affinity-group:"
866 " {}".format(task_id, ro_task["target_id"], e)
867 )
868 ro_vim_item_update = {
869 "vim_status": "VIM_ERROR",
870 "created": created,
871 "vim_details": str(e),
872 }
873
874 return "FAILED", ro_vim_item_update
875
876
tierno70eeb182020-10-19 16:38:00 +0000877class VimInteractionSdnNet(VimInteractionBase):
tierno70eeb182020-10-19 16:38:00 +0000878 @staticmethod
879 def _match_pci(port_pci, mapping):
880 """
881 Check if port_pci matches with mapping
882 mapping can have brackets to indicate that several chars are accepted. e.g
883 pci '0000:af:10.1' matches with '0000:af:1[01].[1357]'
884 :param port_pci: text
885 :param mapping: text, can contain brackets to indicate several chars are available
886 :return: True if matches, False otherwise
887 """
888 if not port_pci or not mapping:
889 return False
890 if port_pci == mapping:
891 return True
892
893 mapping_index = 0
894 pci_index = 0
895 while True:
896 bracket_start = mapping.find("[", mapping_index)
sousaedu80135b92021-02-17 15:05:18 +0100897
tierno70eeb182020-10-19 16:38:00 +0000898 if bracket_start == -1:
899 break
sousaedu80135b92021-02-17 15:05:18 +0100900
tierno70eeb182020-10-19 16:38:00 +0000901 bracket_end = mapping.find("]", bracket_start)
902 if bracket_end == -1:
903 break
sousaedu80135b92021-02-17 15:05:18 +0100904
tierno70eeb182020-10-19 16:38:00 +0000905 length = bracket_start - mapping_index
sousaedu80135b92021-02-17 15:05:18 +0100906 if (
907 length
908 and port_pci[pci_index : pci_index + length]
909 != mapping[mapping_index:bracket_start]
910 ):
tierno70eeb182020-10-19 16:38:00 +0000911 return False
sousaedu80135b92021-02-17 15:05:18 +0100912
913 if (
914 port_pci[pci_index + length]
915 not in mapping[bracket_start + 1 : bracket_end]
916 ):
tierno70eeb182020-10-19 16:38:00 +0000917 return False
sousaedu80135b92021-02-17 15:05:18 +0100918
tierno70eeb182020-10-19 16:38:00 +0000919 pci_index += length + 1
920 mapping_index = bracket_end + 1
921
922 if port_pci[pci_index:] != mapping[mapping_index:]:
923 return False
sousaedu80135b92021-02-17 15:05:18 +0100924
tierno70eeb182020-10-19 16:38:00 +0000925 return True
926
927 def _get_interfaces(self, vlds_to_connect, vim_account_id):
928 """
929 :param vlds_to_connect: list with format vnfrs:<id>:vld.<vld_id> or nsrs:<id>:vld.<vld_id>
930 :param vim_account_id:
931 :return:
932 """
933 interfaces = []
sousaedu80135b92021-02-17 15:05:18 +0100934
tierno70eeb182020-10-19 16:38:00 +0000935 for vld in vlds_to_connect:
936 table, _, db_id = vld.partition(":")
937 db_id, _, vld = db_id.partition(":")
938 _, _, vld_id = vld.partition(".")
sousaedu80135b92021-02-17 15:05:18 +0100939
tierno70eeb182020-10-19 16:38:00 +0000940 if table == "vnfrs":
941 q_filter = {"vim-account-id": vim_account_id, "_id": db_id}
942 iface_key = "vnf-vld-id"
943 else: # table == "nsrs"
944 q_filter = {"vim-account-id": vim_account_id, "nsr-id-ref": db_id}
945 iface_key = "ns-vld-id"
sousaedu80135b92021-02-17 15:05:18 +0100946
tierno70eeb182020-10-19 16:38:00 +0000947 db_vnfrs = self.db.get_list("vnfrs", q_filter=q_filter)
sousaedu80135b92021-02-17 15:05:18 +0100948
tierno70eeb182020-10-19 16:38:00 +0000949 for db_vnfr in db_vnfrs:
950 for vdu_index, vdur in enumerate(db_vnfr.get("vdur", ())):
951 for iface_index, interface in enumerate(vdur["interfaces"]):
sousaedu80135b92021-02-17 15:05:18 +0100952 if interface.get(iface_key) == vld_id and interface.get(
953 "type"
954 ) in ("SR-IOV", "PCI-PASSTHROUGH"):
tierno70eeb182020-10-19 16:38:00 +0000955 # only SR-IOV o PT
956 interface_ = interface.copy()
sousaedu80135b92021-02-17 15:05:18 +0100957 interface_["id"] = "vnfrs:{}:vdu.{}.interfaces.{}".format(
958 db_vnfr["_id"], vdu_index, iface_index
959 )
960
tierno70eeb182020-10-19 16:38:00 +0000961 if vdur.get("status") == "ERROR":
962 interface_["status"] = "ERROR"
sousaedu80135b92021-02-17 15:05:18 +0100963
tierno70eeb182020-10-19 16:38:00 +0000964 interfaces.append(interface_)
sousaedu80135b92021-02-17 15:05:18 +0100965
tierno70eeb182020-10-19 16:38:00 +0000966 return interfaces
967
968 def refresh(self, ro_task):
969 # look for task create
sousaedu80135b92021-02-17 15:05:18 +0100970 task_create_index, _ = next(
971 i_t
972 for i_t in enumerate(ro_task["tasks"])
973 if i_t[1]
974 and i_t[1]["action"] == "CREATE"
975 and i_t[1]["status"] != "FINISHED"
976 )
tierno70eeb182020-10-19 16:38:00 +0000977
978 return self.new(ro_task, task_create_index, None)
979
980 def new(self, ro_task, task_index, task_depends):
981
982 task = ro_task["tasks"][task_index]
983 task_id = task["task_id"]
984 target_vim = self.my_vims[ro_task["target_id"]]
985
986 sdn_net_id = ro_task["vim_info"]["vim_id"]
987
988 created_items = ro_task["vim_info"].get("created_items")
989 connected_ports = ro_task["vim_info"].get("connected_ports", [])
990 new_connected_ports = []
991 last_update = ro_task["vim_info"].get("last_update", 0)
992 sdn_status = ro_task["vim_info"].get("vim_status", "BUILD") or "BUILD"
993 error_list = []
994 created = ro_task["vim_info"].get("created", False)
995
996 try:
tierno70eeb182020-10-19 16:38:00 +0000997 # CREATE
998 params = task["params"]
999 vlds_to_connect = params["vlds"]
1000 associated_vim = params["target_vim"]
sousaedu80135b92021-02-17 15:05:18 +01001001 # external additional ports
1002 additional_ports = params.get("sdn-ports") or ()
tierno70eeb182020-10-19 16:38:00 +00001003 _, _, vim_account_id = associated_vim.partition(":")
sousaedu80135b92021-02-17 15:05:18 +01001004
tierno70eeb182020-10-19 16:38:00 +00001005 if associated_vim:
1006 # get associated VIM
1007 if associated_vim not in self.db_vims:
sousaedu80135b92021-02-17 15:05:18 +01001008 self.db_vims[associated_vim] = self.db.get_one(
1009 "vim_accounts", {"_id": vim_account_id}
1010 )
1011
tierno70eeb182020-10-19 16:38:00 +00001012 db_vim = self.db_vims[associated_vim]
1013
1014 # look for ports to connect
1015 ports = self._get_interfaces(vlds_to_connect, vim_account_id)
1016 # print(ports)
1017
1018 sdn_ports = []
1019 pending_ports = error_ports = 0
1020 vlan_used = None
1021 sdn_need_update = False
sousaedu80135b92021-02-17 15:05:18 +01001022
tierno70eeb182020-10-19 16:38:00 +00001023 for port in ports:
1024 vlan_used = port.get("vlan") or vlan_used
sousaedu80135b92021-02-17 15:05:18 +01001025
tierno70eeb182020-10-19 16:38:00 +00001026 # TODO. Do not connect if already done
1027 if not port.get("compute_node") or not port.get("pci"):
1028 if port.get("status") == "ERROR":
1029 error_ports += 1
1030 else:
1031 pending_ports += 1
1032 continue
sousaedu80135b92021-02-17 15:05:18 +01001033
tierno70eeb182020-10-19 16:38:00 +00001034 pmap = None
sousaedu80135b92021-02-17 15:05:18 +01001035 compute_node_mappings = next(
1036 (
1037 c
1038 for c in db_vim["config"].get("sdn-port-mapping", ())
1039 if c and c["compute_node"] == port["compute_node"]
1040 ),
1041 None,
1042 )
1043
tierno70eeb182020-10-19 16:38:00 +00001044 if compute_node_mappings:
1045 # process port_mapping pci of type 0000:af:1[01].[1357]
sousaedu80135b92021-02-17 15:05:18 +01001046 pmap = next(
1047 (
1048 p
1049 for p in compute_node_mappings["ports"]
1050 if self._match_pci(port["pci"], p.get("pci"))
1051 ),
1052 None,
1053 )
1054
tierno70eeb182020-10-19 16:38:00 +00001055 if not pmap:
1056 if not db_vim["config"].get("mapping_not_needed"):
sousaedu80135b92021-02-17 15:05:18 +01001057 error_list.append(
1058 "Port mapping not found for compute_node={} pci={}".format(
1059 port["compute_node"], port["pci"]
1060 )
1061 )
tierno70eeb182020-10-19 16:38:00 +00001062 continue
sousaedu80135b92021-02-17 15:05:18 +01001063
tierno70eeb182020-10-19 16:38:00 +00001064 pmap = {}
1065
1066 service_endpoint_id = "{}:{}".format(port["compute_node"], port["pci"])
1067 new_port = {
sousaedu80135b92021-02-17 15:05:18 +01001068 "service_endpoint_id": pmap.get("service_endpoint_id")
1069 or service_endpoint_id,
1070 "service_endpoint_encapsulation_type": "dot1q"
1071 if port["type"] == "SR-IOV"
1072 else None,
tierno70eeb182020-10-19 16:38:00 +00001073 "service_endpoint_encapsulation_info": {
1074 "vlan": port.get("vlan"),
lloretgalleg160fcad2021-02-19 10:57:50 +00001075 "mac": port.get("mac-address"),
sousaedu80135b92021-02-17 15:05:18 +01001076 "device_id": pmap.get("device_id") or port["compute_node"],
1077 "device_interface_id": pmap.get("device_interface_id")
1078 or port["pci"],
tierno70eeb182020-10-19 16:38:00 +00001079 "switch_dpid": pmap.get("switch_id") or pmap.get("switch_dpid"),
1080 "switch_port": pmap.get("switch_port"),
1081 "service_mapping_info": pmap.get("service_mapping_info"),
sousaedu80135b92021-02-17 15:05:18 +01001082 },
tierno70eeb182020-10-19 16:38:00 +00001083 }
1084
1085 # TODO
1086 # if port["modified_at"] > last_update:
1087 # sdn_need_update = True
1088 new_connected_ports.append(port["id"]) # TODO
1089 sdn_ports.append(new_port)
1090
1091 if error_ports:
sousaedu80135b92021-02-17 15:05:18 +01001092 error_list.append(
1093 "{} interfaces have not been created as VDU is on ERROR status".format(
1094 error_ports
1095 )
1096 )
tierno70eeb182020-10-19 16:38:00 +00001097
1098 # connect external ports
1099 for index, additional_port in enumerate(additional_ports):
sousaedu80135b92021-02-17 15:05:18 +01001100 additional_port_id = additional_port.get(
1101 "service_endpoint_id"
1102 ) or "external-{}".format(index)
1103 sdn_ports.append(
1104 {
1105 "service_endpoint_id": additional_port_id,
1106 "service_endpoint_encapsulation_type": additional_port.get(
1107 "service_endpoint_encapsulation_type", "dot1q"
1108 ),
1109 "service_endpoint_encapsulation_info": {
1110 "vlan": additional_port.get("vlan") or vlan_used,
1111 "mac": additional_port.get("mac_address"),
1112 "device_id": additional_port.get("device_id"),
1113 "device_interface_id": additional_port.get(
1114 "device_interface_id"
1115 ),
1116 "switch_dpid": additional_port.get("switch_dpid")
1117 or additional_port.get("switch_id"),
1118 "switch_port": additional_port.get("switch_port"),
1119 "service_mapping_info": additional_port.get(
1120 "service_mapping_info"
1121 ),
1122 },
1123 }
1124 )
tierno70eeb182020-10-19 16:38:00 +00001125 new_connected_ports.append(additional_port_id)
1126 sdn_info = ""
sousaedu80135b92021-02-17 15:05:18 +01001127
tierno70eeb182020-10-19 16:38:00 +00001128 # if there are more ports to connect or they have been modified, call create/update
1129 if error_list:
1130 sdn_status = "ERROR"
1131 sdn_info = "; ".join(error_list)
1132 elif set(connected_ports) != set(new_connected_ports) or sdn_need_update:
1133 last_update = time.time()
sousaedu80135b92021-02-17 15:05:18 +01001134
tierno70eeb182020-10-19 16:38:00 +00001135 if not sdn_net_id:
1136 if len(sdn_ports) < 2:
1137 sdn_status = "ACTIVE"
sousaedu80135b92021-02-17 15:05:18 +01001138
tierno70eeb182020-10-19 16:38:00 +00001139 if not pending_ports:
sousaedu80135b92021-02-17 15:05:18 +01001140 self.logger.debug(
1141 "task={} {} new-sdn-net done, less than 2 ports".format(
1142 task_id, ro_task["target_id"]
1143 )
1144 )
tierno70eeb182020-10-19 16:38:00 +00001145 else:
1146 net_type = params.get("type") or "ELAN"
sousaedu80135b92021-02-17 15:05:18 +01001147 (
1148 sdn_net_id,
1149 created_items,
1150 ) = target_vim.create_connectivity_service(net_type, sdn_ports)
tierno70eeb182020-10-19 16:38:00 +00001151 created = True
sousaedu80135b92021-02-17 15:05:18 +01001152 self.logger.debug(
1153 "task={} {} new-sdn-net={} created={}".format(
1154 task_id, ro_task["target_id"], sdn_net_id, created
1155 )
1156 )
tierno70eeb182020-10-19 16:38:00 +00001157 else:
1158 created_items = target_vim.edit_connectivity_service(
sousaedu80135b92021-02-17 15:05:18 +01001159 sdn_net_id, conn_info=created_items, connection_points=sdn_ports
1160 )
tierno70eeb182020-10-19 16:38:00 +00001161 created = True
sousaedu80135b92021-02-17 15:05:18 +01001162 self.logger.debug(
1163 "task={} {} update-sdn-net={} created={}".format(
1164 task_id, ro_task["target_id"], sdn_net_id, created
1165 )
1166 )
1167
tierno70eeb182020-10-19 16:38:00 +00001168 connected_ports = new_connected_ports
1169 elif sdn_net_id:
sousaedu80135b92021-02-17 15:05:18 +01001170 wim_status_dict = target_vim.get_connectivity_service_status(
1171 sdn_net_id, conn_info=created_items
1172 )
tierno70eeb182020-10-19 16:38:00 +00001173 sdn_status = wim_status_dict["sdn_status"]
sousaedu80135b92021-02-17 15:05:18 +01001174
tierno70eeb182020-10-19 16:38:00 +00001175 if wim_status_dict.get("sdn_info"):
1176 sdn_info = str(wim_status_dict.get("sdn_info")) or ""
sousaedu80135b92021-02-17 15:05:18 +01001177
tierno70eeb182020-10-19 16:38:00 +00001178 if wim_status_dict.get("error_msg"):
1179 sdn_info = wim_status_dict.get("error_msg") or ""
1180
1181 if pending_ports:
1182 if sdn_status != "ERROR":
1183 sdn_info = "Waiting for getting interfaces location from VIM. Obtained '{}' of {}".format(
sousaedu80135b92021-02-17 15:05:18 +01001184 len(ports) - pending_ports, len(ports)
1185 )
1186
tierno70eeb182020-10-19 16:38:00 +00001187 if sdn_status == "ACTIVE":
1188 sdn_status = "BUILD"
1189
sousaedu80135b92021-02-17 15:05:18 +01001190 ro_vim_item_update = {
1191 "vim_id": sdn_net_id,
1192 "vim_status": sdn_status,
1193 "created": created,
1194 "created_items": created_items,
1195 "connected_ports": connected_ports,
1196 "vim_details": sdn_info,
1197 "last_update": last_update,
1198 }
1199
tierno70eeb182020-10-19 16:38:00 +00001200 return sdn_status, ro_vim_item_update
1201 except Exception as e:
sousaedu80135b92021-02-17 15:05:18 +01001202 self.logger.error(
1203 "task={} vim={} new-net: {}".format(task_id, ro_task["target_id"], e),
1204 exc_info=not isinstance(
1205 e, (sdnconn.SdnConnectorError, vimconn.VimConnException)
1206 ),
1207 )
1208 ro_vim_item_update = {
1209 "vim_status": "VIM_ERROR",
1210 "created": created,
1211 "vim_details": str(e),
1212 }
1213
tierno70eeb182020-10-19 16:38:00 +00001214 return "FAILED", ro_vim_item_update
1215
1216 def delete(self, ro_task, task_index):
1217 task = ro_task["tasks"][task_index]
1218 task_id = task["task_id"]
1219 sdn_vim_id = ro_task["vim_info"].get("vim_id")
sousaedu80135b92021-02-17 15:05:18 +01001220 ro_vim_item_update_ok = {
1221 "vim_status": "DELETED",
1222 "created": False,
1223 "vim_details": "DELETED",
1224 "vim_id": None,
1225 }
1226
tierno70eeb182020-10-19 16:38:00 +00001227 try:
1228 if sdn_vim_id:
1229 target_vim = self.my_vims[ro_task["target_id"]]
sousaedu80135b92021-02-17 15:05:18 +01001230 target_vim.delete_connectivity_service(
1231 sdn_vim_id, ro_task["vim_info"].get("created_items")
1232 )
tierno70eeb182020-10-19 16:38:00 +00001233
1234 except Exception as e:
sousaedu80135b92021-02-17 15:05:18 +01001235 if (
1236 isinstance(e, sdnconn.SdnConnectorError)
1237 and e.http_code == HTTPStatus.NOT_FOUND.value
1238 ):
tierno70eeb182020-10-19 16:38:00 +00001239 ro_vim_item_update_ok["vim_details"] = "already deleted"
1240 else:
sousaedu80135b92021-02-17 15:05:18 +01001241 self.logger.error(
1242 "ro_task={} vim={} del-sdn-net={}: {}".format(
1243 ro_task["_id"], ro_task["target_id"], sdn_vim_id, e
1244 ),
1245 exc_info=not isinstance(
1246 e, (sdnconn.SdnConnectorError, vimconn.VimConnException)
1247 ),
1248 )
1249 ro_vim_item_update = {
1250 "vim_status": "VIM_ERROR",
1251 "vim_details": "Error while deleting: {}".format(e),
1252 }
1253
tierno70eeb182020-10-19 16:38:00 +00001254 return "FAILED", ro_vim_item_update
1255
sousaedu80135b92021-02-17 15:05:18 +01001256 self.logger.debug(
1257 "task={} {} del-sdn-net={} {}".format(
1258 task_id,
1259 ro_task["target_id"],
1260 sdn_vim_id,
1261 ro_vim_item_update_ok.get("vim_details", ""),
1262 )
1263 )
1264
tierno70eeb182020-10-19 16:38:00 +00001265 return "DONE", ro_vim_item_update_ok
1266
1267
1268class NsWorker(threading.Thread):
1269 REFRESH_BUILD = 5 # 5 seconds
1270 REFRESH_ACTIVE = 60 # 1 minute
1271 REFRESH_ERROR = 600
1272 REFRESH_IMAGE = 3600 * 10
1273 REFRESH_DELETE = 3600 * 10
tiernof1b640f2020-12-09 15:06:01 +00001274 QUEUE_SIZE = 100
tierno70eeb182020-10-19 16:38:00 +00001275 terminate = False
tierno70eeb182020-10-19 16:38:00 +00001276
1277 def __init__(self, worker_index, config, plugins, db):
1278 """
1279
1280 :param worker_index: thread index
1281 :param config: general configuration of RO, among others the process_id with the docker id where it runs
1282 :param plugins: global shared dict with the loaded plugins
1283 :param db: database class instance to use
1284 """
1285 threading.Thread.__init__(self)
1286 self.config = config
1287 self.plugins = plugins
1288 self.plugin_name = "unknown"
sousaedu80135b92021-02-17 15:05:18 +01001289 self.logger = logging.getLogger("ro.worker{}".format(worker_index))
tierno70eeb182020-10-19 16:38:00 +00001290 self.worker_index = worker_index
1291 self.task_queue = queue.Queue(self.QUEUE_SIZE)
sousaedu80135b92021-02-17 15:05:18 +01001292 # targetvim: vimplugin class
1293 self.my_vims = {}
1294 # targetvim: vim information from database
1295 self.db_vims = {}
1296 # targetvim list
1297 self.vim_targets = []
tierno70eeb182020-10-19 16:38:00 +00001298 self.my_id = config["process_id"] + ":" + str(worker_index)
1299 self.db = db
1300 self.item2class = {
1301 "net": VimInteractionNet(self.db, self.my_vims, self.db_vims, self.logger),
1302 "vdu": VimInteractionVdu(self.db, self.my_vims, self.db_vims, self.logger),
sousaedu80135b92021-02-17 15:05:18 +01001303 "image": VimInteractionImage(
1304 self.db, self.my_vims, self.db_vims, self.logger
1305 ),
1306 "flavor": VimInteractionFlavor(
1307 self.db, self.my_vims, self.db_vims, self.logger
1308 ),
1309 "sdn_net": VimInteractionSdnNet(
1310 self.db, self.my_vims, self.db_vims, self.logger
1311 ),
Alexis Romerob70f4ed2022-03-11 18:00:49 +01001312 "affinity-or-anti-affinity-group": VimInteractionAffinityGroup(
1313 self.db, self.my_vims, self.db_vims, self.logger
1314 ),
tierno70eeb182020-10-19 16:38:00 +00001315 }
1316 self.time_last_task_processed = None
sousaedu80135b92021-02-17 15:05:18 +01001317 # lists of tasks to delete because nsrs or vnfrs has been deleted from db
1318 self.tasks_to_delete = []
1319 # it is idle when there are not vim_targets associated
1320 self.idle = True
tiernof1b640f2020-12-09 15:06:01 +00001321 self.task_locked_time = config["global"]["task_locked_time"]
tierno70eeb182020-10-19 16:38:00 +00001322
1323 def insert_task(self, task):
1324 try:
1325 self.task_queue.put(task, False)
1326 return None
1327 except queue.Full:
1328 raise NsWorkerException("timeout inserting a task")
1329
1330 def terminate(self):
1331 self.insert_task("exit")
1332
1333 def del_task(self, task):
1334 with self.task_lock:
1335 if task["status"] == "SCHEDULED":
1336 task["status"] = "SUPERSEDED"
1337 return True
1338 else: # task["status"] == "processing"
1339 self.task_lock.release()
1340 return False
1341
1342 def _process_vim_config(self, target_id, db_vim):
1343 """
1344 Process vim config, creating vim configuration files as ca_cert
1345 :param target_id: vim/sdn/wim + id
1346 :param db_vim: Vim dictionary obtained from database
1347 :return: None. Modifies vim. Creates a folder target_id:worker_index and several files
1348 """
1349 if not db_vim.get("config"):
1350 return
sousaedu80135b92021-02-17 15:05:18 +01001351
tierno70eeb182020-10-19 16:38:00 +00001352 file_name = ""
sousaedu80135b92021-02-17 15:05:18 +01001353
tierno70eeb182020-10-19 16:38:00 +00001354 try:
1355 if db_vim["config"].get("ca_cert_content"):
1356 file_name = "{}:{}".format(target_id, self.worker_index)
sousaedu80135b92021-02-17 15:05:18 +01001357
tierno70eeb182020-10-19 16:38:00 +00001358 try:
1359 mkdir(file_name)
1360 except FileExistsError:
1361 pass
sousaedu80135b92021-02-17 15:05:18 +01001362
tierno70eeb182020-10-19 16:38:00 +00001363 file_name = file_name + "/ca_cert"
sousaedu80135b92021-02-17 15:05:18 +01001364
tierno70eeb182020-10-19 16:38:00 +00001365 with open(file_name, "w") as f:
1366 f.write(db_vim["config"]["ca_cert_content"])
1367 del db_vim["config"]["ca_cert_content"]
1368 db_vim["config"]["ca_cert"] = file_name
1369 except Exception as e:
sousaedu80135b92021-02-17 15:05:18 +01001370 raise NsWorkerException(
1371 "Error writing to file '{}': {}".format(file_name, e)
1372 )
tierno70eeb182020-10-19 16:38:00 +00001373
1374 def _load_plugin(self, name, type="vim"):
1375 # type can be vim or sdn
1376 if "rovim_dummy" not in self.plugins:
1377 self.plugins["rovim_dummy"] = VimDummyConnector
sousaedu80135b92021-02-17 15:05:18 +01001378
tierno70eeb182020-10-19 16:38:00 +00001379 if "rosdn_dummy" not in self.plugins:
1380 self.plugins["rosdn_dummy"] = SdnDummyConnector
sousaedu80135b92021-02-17 15:05:18 +01001381
tierno70eeb182020-10-19 16:38:00 +00001382 if name in self.plugins:
1383 return self.plugins[name]
sousaedu80135b92021-02-17 15:05:18 +01001384
tierno70eeb182020-10-19 16:38:00 +00001385 try:
sousaedubecd0832021-04-08 00:16:18 +02001386 for ep in entry_points(group="osm_ro{}.plugins".format(type), name=name):
1387 self.plugins[name] = ep.load()
tierno70eeb182020-10-19 16:38:00 +00001388 except Exception as e:
1389 raise NsWorkerException("Cannot load plugin osm_{}: {}".format(name, e))
sousaedu80135b92021-02-17 15:05:18 +01001390
tierno70eeb182020-10-19 16:38:00 +00001391 if name and name not in self.plugins:
sousaedu80135b92021-02-17 15:05:18 +01001392 raise NsWorkerException(
1393 "Plugin 'osm_{n}' has not been installed".format(n=name)
1394 )
1395
tierno70eeb182020-10-19 16:38:00 +00001396 return self.plugins[name]
1397
1398 def _unload_vim(self, target_id):
1399 """
1400 Unload a vim_account. Removes it from self db_vims dictionary, my_vims dictionary and vim_targets list
1401 :param target_id: Contains type:_id; where type can be 'vim', ...
1402 :return: None.
1403 """
1404 try:
tierno70eeb182020-10-19 16:38:00 +00001405 self.db_vims.pop(target_id, None)
1406 self.my_vims.pop(target_id, None)
sousaedu80135b92021-02-17 15:05:18 +01001407
tierno86153522020-12-06 18:27:16 +00001408 if target_id in self.vim_targets:
1409 self.vim_targets.remove(target_id)
sousaedu80135b92021-02-17 15:05:18 +01001410
tierno86153522020-12-06 18:27:16 +00001411 self.logger.info("Unloaded {}".format(target_id))
tierno70eeb182020-10-19 16:38:00 +00001412 rmtree("{}:{}".format(target_id, self.worker_index))
1413 except FileNotFoundError:
1414 pass # this is raised by rmtree if folder does not exist
1415 except Exception as e:
1416 self.logger.error("Cannot unload {}: {}".format(target_id, e))
1417
1418 def _check_vim(self, target_id):
1419 """
1420 Load a VIM/SDN/WIM (if not loaded) and check connectivity, updating database with ENABLE or ERROR
1421 :param target_id: Contains type:_id; type can be 'vim', 'sdn' or 'wim'
1422 :return: None.
1423 """
1424 target, _, _id = target_id.partition(":")
1425 now = time.time()
1426 update_dict = {}
1427 unset_dict = {}
1428 op_text = ""
1429 step = ""
tierno86153522020-12-06 18:27:16 +00001430 loaded = target_id in self.vim_targets
sousaedu80135b92021-02-17 15:05:18 +01001431 target_database = (
1432 "vim_accounts"
1433 if target == "vim"
1434 else "wim_accounts"
1435 if target == "wim"
1436 else "sdns"
1437 )
1438
tierno70eeb182020-10-19 16:38:00 +00001439 try:
1440 step = "Getting {} from db".format(target_id)
1441 db_vim = self.db.get_one(target_database, {"_id": _id})
sousaedu80135b92021-02-17 15:05:18 +01001442
1443 for op_index, operation in enumerate(
1444 db_vim["_admin"].get("operations", ())
1445 ):
tierno70eeb182020-10-19 16:38:00 +00001446 if operation["operationState"] != "PROCESSING":
1447 continue
sousaedu80135b92021-02-17 15:05:18 +01001448
tierno70eeb182020-10-19 16:38:00 +00001449 locked_at = operation.get("locked_at")
sousaedu80135b92021-02-17 15:05:18 +01001450
tiernof1b640f2020-12-09 15:06:01 +00001451 if locked_at is not None and locked_at >= now - self.task_locked_time:
tierno70eeb182020-10-19 16:38:00 +00001452 # some other thread is doing this operation
1453 return
sousaedu80135b92021-02-17 15:05:18 +01001454
tierno70eeb182020-10-19 16:38:00 +00001455 # lock
1456 op_text = "_admin.operations.{}.".format(op_index)
sousaedu80135b92021-02-17 15:05:18 +01001457
1458 if not self.db.set_one(
1459 target_database,
1460 q_filter={
1461 "_id": _id,
1462 op_text + "operationState": "PROCESSING",
1463 op_text + "locked_at": locked_at,
1464 },
1465 update_dict={
1466 op_text + "locked_at": now,
1467 "admin.current_operation": op_index,
1468 },
1469 fail_on_empty=False,
1470 ):
tierno70eeb182020-10-19 16:38:00 +00001471 return
sousaedu80135b92021-02-17 15:05:18 +01001472
tierno70eeb182020-10-19 16:38:00 +00001473 unset_dict[op_text + "locked_at"] = None
1474 unset_dict["current_operation"] = None
1475 step = "Loading " + target_id
1476 error_text = self._load_vim(target_id)
sousaedu80135b92021-02-17 15:05:18 +01001477
tierno70eeb182020-10-19 16:38:00 +00001478 if not error_text:
1479 step = "Checking connectivity"
sousaedu80135b92021-02-17 15:05:18 +01001480
1481 if target == "vim":
tierno70eeb182020-10-19 16:38:00 +00001482 self.my_vims[target_id].check_vim_connectivity()
1483 else:
1484 self.my_vims[target_id].check_credentials()
sousaedu80135b92021-02-17 15:05:18 +01001485
tierno70eeb182020-10-19 16:38:00 +00001486 update_dict["_admin.operationalState"] = "ENABLED"
1487 update_dict["_admin.detailed-status"] = ""
1488 unset_dict[op_text + "detailed-status"] = None
1489 update_dict[op_text + "operationState"] = "COMPLETED"
sousaedu80135b92021-02-17 15:05:18 +01001490
tierno70eeb182020-10-19 16:38:00 +00001491 return
1492
1493 except Exception as e:
1494 error_text = "{}: {}".format(step, e)
1495 self.logger.error("{} for {}: {}".format(step, target_id, e))
1496
1497 finally:
1498 if update_dict or unset_dict:
1499 if error_text:
1500 update_dict[op_text + "operationState"] = "FAILED"
1501 update_dict[op_text + "detailed-status"] = error_text
1502 unset_dict.pop(op_text + "detailed-status", None)
1503 update_dict["_admin.operationalState"] = "ERROR"
1504 update_dict["_admin.detailed-status"] = error_text
sousaedu80135b92021-02-17 15:05:18 +01001505
tierno70eeb182020-10-19 16:38:00 +00001506 if op_text:
1507 update_dict[op_text + "statusEnteredTime"] = now
sousaedu80135b92021-02-17 15:05:18 +01001508
1509 self.db.set_one(
1510 target_database,
1511 q_filter={"_id": _id},
1512 update_dict=update_dict,
1513 unset=unset_dict,
1514 fail_on_empty=False,
1515 )
1516
tierno70eeb182020-10-19 16:38:00 +00001517 if not loaded:
1518 self._unload_vim(target_id)
1519
1520 def _reload_vim(self, target_id):
1521 if target_id in self.vim_targets:
1522 self._load_vim(target_id)
1523 else:
1524 # if the vim is not loaded, but database information of VIM is cached at self.db_vims,
1525 # just remove it to force load again next time it is needed
1526 self.db_vims.pop(target_id, None)
1527
1528 def _load_vim(self, target_id):
1529 """
1530 Load or reload a vim_account, sdn_controller or wim_account.
1531 Read content from database, load the plugin if not loaded.
1532 In case of error loading the plugin, it load a failing VIM_connector
1533 It fills self db_vims dictionary, my_vims dictionary and vim_targets list
1534 :param target_id: Contains type:_id; where type can be 'vim', ...
1535 :return: None if ok, descriptive text if error
1536 """
1537 target, _, _id = target_id.partition(":")
sousaedu80135b92021-02-17 15:05:18 +01001538 target_database = (
1539 "vim_accounts"
1540 if target == "vim"
1541 else "wim_accounts"
1542 if target == "wim"
1543 else "sdns"
1544 )
tierno70eeb182020-10-19 16:38:00 +00001545 plugin_name = ""
1546 vim = None
sousaedu80135b92021-02-17 15:05:18 +01001547
tierno70eeb182020-10-19 16:38:00 +00001548 try:
1549 step = "Getting {}={} from db".format(target, _id)
1550 # TODO process for wim, sdnc, ...
1551 vim = self.db.get_one(target_database, {"_id": _id})
1552
1553 # if deep_get(vim, "config", "sdn-controller"):
1554 # step = "Getting sdn-controller-id='{}' from db".format(vim["config"]["sdn-controller"])
1555 # db_sdn = self.db.get_one("sdns", {"_id": vim["config"]["sdn-controller"]})
1556
1557 step = "Decrypting password"
1558 schema_version = vim.get("schema_version")
sousaedu80135b92021-02-17 15:05:18 +01001559 self.db.encrypt_decrypt_fields(
1560 vim,
1561 "decrypt",
1562 fields=("password", "secret"),
1563 schema_version=schema_version,
1564 salt=_id,
1565 )
tierno70eeb182020-10-19 16:38:00 +00001566 self._process_vim_config(target_id, vim)
sousaedu80135b92021-02-17 15:05:18 +01001567
tierno70eeb182020-10-19 16:38:00 +00001568 if target == "vim":
1569 plugin_name = "rovim_" + vim["vim_type"]
1570 step = "Loading plugin '{}'".format(plugin_name)
1571 vim_module_conn = self._load_plugin(plugin_name)
1572 step = "Loading {}'".format(target_id)
1573 self.my_vims[target_id] = vim_module_conn(
sousaedu80135b92021-02-17 15:05:18 +01001574 uuid=vim["_id"],
1575 name=vim["name"],
1576 tenant_id=vim.get("vim_tenant_id"),
1577 tenant_name=vim.get("vim_tenant_name"),
1578 url=vim["vim_url"],
1579 url_admin=None,
1580 user=vim["vim_user"],
1581 passwd=vim["vim_password"],
1582 config=vim.get("config") or {},
1583 persistent_info={},
tierno70eeb182020-10-19 16:38:00 +00001584 )
1585 else: # sdn
1586 plugin_name = "rosdn_" + vim["type"]
1587 step = "Loading plugin '{}'".format(plugin_name)
1588 vim_module_conn = self._load_plugin(plugin_name, "sdn")
1589 step = "Loading {}'".format(target_id)
1590 wim = deepcopy(vim)
1591 wim_config = wim.pop("config", {}) or {}
1592 wim["uuid"] = wim["_id"]
1593 wim["wim_url"] = wim["url"]
sousaedu80135b92021-02-17 15:05:18 +01001594
tierno70eeb182020-10-19 16:38:00 +00001595 if wim.get("dpid"):
1596 wim_config["dpid"] = wim.pop("dpid")
sousaedu80135b92021-02-17 15:05:18 +01001597
tierno70eeb182020-10-19 16:38:00 +00001598 if wim.get("switch_id"):
1599 wim_config["switch_id"] = wim.pop("switch_id")
sousaedu80135b92021-02-17 15:05:18 +01001600
1601 # wim, wim_account, config
1602 self.my_vims[target_id] = vim_module_conn(wim, wim, wim_config)
tierno70eeb182020-10-19 16:38:00 +00001603 self.db_vims[target_id] = vim
1604 self.error_status = None
sousaedu80135b92021-02-17 15:05:18 +01001605
1606 self.logger.info(
1607 "Connector loaded for {}, plugin={}".format(target_id, plugin_name)
1608 )
tierno70eeb182020-10-19 16:38:00 +00001609 except Exception as e:
sousaedu80135b92021-02-17 15:05:18 +01001610 self.logger.error(
1611 "Cannot load {} plugin={}: {} {}".format(
1612 target_id, plugin_name, step, e
1613 )
1614 )
1615
tierno70eeb182020-10-19 16:38:00 +00001616 self.db_vims[target_id] = vim or {}
1617 self.db_vims[target_id] = FailingConnector(str(e))
1618 error_status = "{} Error: {}".format(step, e)
sousaedu80135b92021-02-17 15:05:18 +01001619
tierno70eeb182020-10-19 16:38:00 +00001620 return error_status
1621 finally:
1622 if target_id not in self.vim_targets:
1623 self.vim_targets.append(target_id)
1624
1625 def _get_db_task(self):
1626 """
1627 Read actions from database and reload them at memory. Fill self.refresh_list, pending_list, vim_actions
1628 :return: None
1629 """
1630 now = time.time()
sousaedu80135b92021-02-17 15:05:18 +01001631
tierno70eeb182020-10-19 16:38:00 +00001632 if not self.time_last_task_processed:
1633 self.time_last_task_processed = now
sousaedu80135b92021-02-17 15:05:18 +01001634
tierno70eeb182020-10-19 16:38:00 +00001635 try:
1636 while True:
gallardo2f4aaaa2022-01-31 16:50:48 +00001637 """
1638 # Log RO tasks only when loglevel is DEBUG
1639 if self.logger.getEffectiveLevel() == logging.DEBUG:
1640 self._log_ro_task(
1641 None,
1642 None,
1643 None,
1644 "TASK_WF",
1645 "task_locked_time="
1646 + str(self.task_locked_time)
1647 + " "
1648 + "time_last_task_processed="
1649 + str(self.time_last_task_processed)
1650 + " "
1651 + "now="
1652 + str(now),
1653 )
1654 """
tierno70eeb182020-10-19 16:38:00 +00001655 locked = self.db.set_one(
1656 "ro_tasks",
sousaedu80135b92021-02-17 15:05:18 +01001657 q_filter={
1658 "target_id": self.vim_targets,
1659 "tasks.status": ["SCHEDULED", "BUILD", "DONE", "FAILED"],
1660 "locked_at.lt": now - self.task_locked_time,
1661 "to_check_at.lt": self.time_last_task_processed,
1662 },
tierno70eeb182020-10-19 16:38:00 +00001663 update_dict={"locked_by": self.my_id, "locked_at": now},
sousaedu80135b92021-02-17 15:05:18 +01001664 fail_on_empty=False,
1665 )
1666
tierno70eeb182020-10-19 16:38:00 +00001667 if locked:
1668 # read and return
1669 ro_task = self.db.get_one(
1670 "ro_tasks",
sousaedu80135b92021-02-17 15:05:18 +01001671 q_filter={
1672 "target_id": self.vim_targets,
1673 "tasks.status": ["SCHEDULED", "BUILD", "DONE", "FAILED"],
1674 "locked_at": now,
1675 },
1676 )
tierno70eeb182020-10-19 16:38:00 +00001677 return ro_task
sousaedu80135b92021-02-17 15:05:18 +01001678
tierno70eeb182020-10-19 16:38:00 +00001679 if self.time_last_task_processed == now:
1680 self.time_last_task_processed = None
1681 return None
1682 else:
1683 self.time_last_task_processed = now
1684 # self.time_last_task_processed = min(self.time_last_task_processed + 1000, now)
1685
1686 except DbException as e:
1687 self.logger.error("Database exception at _get_db_task: {}".format(e))
1688 except Exception as e:
sousaedu80135b92021-02-17 15:05:18 +01001689 self.logger.critical(
1690 "Unexpected exception at _get_db_task: {}".format(e), exc_info=True
1691 )
1692
tierno70eeb182020-10-19 16:38:00 +00001693 return None
1694
gallardo2f4aaaa2022-01-31 16:50:48 +00001695 def _get_db_all_tasks(self):
1696 """
1697 Read all content of table ro_tasks to log it
1698 :return: None
1699 """
1700 try:
1701 # Checking the content of the BD:
1702
1703 # read and return
1704 ro_task = self.db.get_list("ro_tasks")
1705 for rt in ro_task:
1706 self._log_ro_task(rt, None, None, "TASK_WF", "GET_ALL_TASKS")
1707 return ro_task
1708
1709 except DbException as e:
1710 self.logger.error("Database exception at _get_db_all_tasks: {}".format(e))
1711 except Exception as e:
1712 self.logger.critical(
1713 "Unexpected exception at _get_db_all_tasks: {}".format(e), exc_info=True
1714 )
1715
1716 return None
1717
1718 def _log_ro_task(self, ro_task, db_ro_task_update, db_ro_task_delete, mark, event):
1719 """
1720 Generate a log with the following format:
1721
1722 Mark;Event;ro_task_id;locked_at;modified_at;created_at;to_check_at;locked_by;
1723 target_id;vim_info.refresh_at;vim_info;no_of_tasks;task_status;action_id;
1724 task_array_index;task_id;task_action;task_item;task_args
1725
1726 Example:
1727
1728 TASK_WF;GET_TASK;888f1864-749a-4fc2-bc1a-97c0fffd6a6f:2;1642158724.8210013;
1729 1642158640.7986135;1642158640.7986135;1642158640.7986135;b134c9494e75:0a
1730 ;vim:b7ff9e24-8868-4d68-8a57-a59dc11d0327;None;{'created': False,
1731 'created_items': None, 'vim_id': None, 'vim_name': None, 'vim_status': None,
1732 'vim_details': None, 'refresh_at': None};1;SCHEDULED;
1733 888f1864-749a-4fc2-bc1a-97c0fffd6a6f;0;888f1864-749a-4fc2-bc1a-97c0fffd6a6f:2;
1734 CREATE;image;{'filter_dict': {'name': 'ubuntu-os-cloud:image-family:ubuntu-1804-lts'}}
1735 """
1736 try:
1737 line = []
1738 i = 0
1739 if ro_task is not None and isinstance(ro_task, dict):
1740 for t in ro_task["tasks"]:
1741 line.clear()
1742 line.append(mark)
1743 line.append(event)
1744 line.append(ro_task.get("_id", ""))
1745 line.append(str(ro_task.get("locked_at", "")))
1746 line.append(str(ro_task.get("modified_at", "")))
1747 line.append(str(ro_task.get("created_at", "")))
1748 line.append(str(ro_task.get("to_check_at", "")))
1749 line.append(str(ro_task.get("locked_by", "")))
1750 line.append(str(ro_task.get("target_id", "")))
1751 line.append(str(ro_task.get("vim_info", {}).get("refresh_at", "")))
1752 line.append(str(ro_task.get("vim_info", "")))
1753 line.append(str(ro_task.get("tasks", "")))
1754 if isinstance(t, dict):
1755 line.append(str(t.get("status", "")))
1756 line.append(str(t.get("action_id", "")))
1757 line.append(str(i))
1758 line.append(str(t.get("task_id", "")))
1759 line.append(str(t.get("action", "")))
1760 line.append(str(t.get("item", "")))
1761 line.append(str(t.get("find_params", "")))
1762 line.append(str(t.get("params", "")))
1763 else:
1764 line.extend([""] * 2)
1765 line.append(str(i))
1766 line.extend([""] * 5)
1767
1768 i += 1
1769 self.logger.debug(";".join(line))
1770 elif db_ro_task_update is not None and isinstance(db_ro_task_update, dict):
1771 i = 0
1772 while True:
1773 st = "tasks.{}.status".format(i)
1774 if st not in db_ro_task_update:
1775 break
1776 line.clear()
1777 line.append(mark)
1778 line.append(event)
1779 line.append(db_ro_task_update.get("_id", ""))
1780 line.append(str(db_ro_task_update.get("locked_at", "")))
1781 line.append(str(db_ro_task_update.get("modified_at", "")))
1782 line.append("")
1783 line.append(str(db_ro_task_update.get("to_check_at", "")))
1784 line.append(str(db_ro_task_update.get("locked_by", "")))
1785 line.append("")
1786 line.append(str(db_ro_task_update.get("vim_info.refresh_at", "")))
1787 line.append("")
1788 line.append(str(db_ro_task_update.get("vim_info", "")))
1789 line.append(str(str(db_ro_task_update).count(".status")))
1790 line.append(db_ro_task_update.get(st, ""))
1791 line.append("")
1792 line.append(str(i))
1793 line.extend([""] * 3)
1794 i += 1
1795 self.logger.debug(";".join(line))
1796
1797 elif db_ro_task_delete is not None and isinstance(db_ro_task_delete, dict):
1798 line.clear()
1799 line.append(mark)
1800 line.append(event)
1801 line.append(db_ro_task_delete.get("_id", ""))
1802 line.append("")
1803 line.append(db_ro_task_delete.get("modified_at", ""))
1804 line.extend([""] * 13)
1805 self.logger.debug(";".join(line))
1806
1807 else:
1808 line.clear()
1809 line.append(mark)
1810 line.append(event)
1811 line.extend([""] * 16)
1812 self.logger.debug(";".join(line))
1813
1814 except Exception as e:
1815 self.logger.error("Error logging ro_task: {}".format(e))
1816
tierno70eeb182020-10-19 16:38:00 +00001817 def _delete_task(self, ro_task, task_index, task_depends, db_update):
1818 """
1819 Determine if this task need to be done or superseded
1820 :return: None
1821 """
1822 my_task = ro_task["tasks"][task_index]
1823 task_id = my_task["task_id"]
sousaedu80135b92021-02-17 15:05:18 +01001824 needed_delete = ro_task["vim_info"]["created"] or ro_task["vim_info"].get(
1825 "created_items", False
1826 )
1827
palaciosj8f2060b2022-02-24 12:05:59 +00001828 self.logger.warning("Needed delete: {}".format(needed_delete))
tierno70eeb182020-10-19 16:38:00 +00001829 if my_task["status"] == "FAILED":
1830 return None, None # TODO need to be retry??
sousaedu80135b92021-02-17 15:05:18 +01001831
tierno70eeb182020-10-19 16:38:00 +00001832 try:
1833 for index, task in enumerate(ro_task["tasks"]):
1834 if index == task_index or not task:
1835 continue # own task
sousaedu80135b92021-02-17 15:05:18 +01001836
1837 if (
1838 my_task["target_record"] == task["target_record"]
1839 and task["action"] == "CREATE"
1840 ):
tierno70eeb182020-10-19 16:38:00 +00001841 # set to finished
sousaedu80135b92021-02-17 15:05:18 +01001842 db_update["tasks.{}.status".format(index)] = task[
1843 "status"
1844 ] = "FINISHED"
1845 elif task["action"] == "CREATE" and task["status"] not in (
1846 "FINISHED",
1847 "SUPERSEDED",
1848 ):
tierno70eeb182020-10-19 16:38:00 +00001849 needed_delete = False
sousaedu80135b92021-02-17 15:05:18 +01001850
tierno70eeb182020-10-19 16:38:00 +00001851 if needed_delete:
palaciosj8f2060b2022-02-24 12:05:59 +00001852 self.logger.warning("Deleting ro_task={} task_index={}".format(ro_task, task_index))
tierno70eeb182020-10-19 16:38:00 +00001853 return self.item2class[my_task["item"]].delete(ro_task, task_index)
1854 else:
1855 return "SUPERSEDED", None
1856 except Exception as e:
1857 if not isinstance(e, NsWorkerException):
sousaedu80135b92021-02-17 15:05:18 +01001858 self.logger.critical(
1859 "Unexpected exception at _delete_task task={}: {}".format(
1860 task_id, e
1861 ),
1862 exc_info=True,
1863 )
1864
tierno70eeb182020-10-19 16:38:00 +00001865 return "FAILED", {"vim_status": "VIM_ERROR", "vim_details": str(e)}
1866
1867 def _create_task(self, ro_task, task_index, task_depends, db_update):
1868 """
1869 Determine if this task need to create something at VIM
1870 :return: None
1871 """
1872 my_task = ro_task["tasks"][task_index]
1873 task_id = my_task["task_id"]
1874 task_status = None
sousaedu80135b92021-02-17 15:05:18 +01001875
tierno70eeb182020-10-19 16:38:00 +00001876 if my_task["status"] == "FAILED":
1877 return None, None # TODO need to be retry??
1878 elif my_task["status"] == "SCHEDULED":
1879 # check if already created by another task
1880 for index, task in enumerate(ro_task["tasks"]):
1881 if index == task_index or not task:
1882 continue # own task
sousaedu80135b92021-02-17 15:05:18 +01001883
1884 if task["action"] == "CREATE" and task["status"] not in (
1885 "SCHEDULED",
1886 "FINISHED",
1887 "SUPERSEDED",
1888 ):
tierno70eeb182020-10-19 16:38:00 +00001889 return task["status"], "COPY_VIM_INFO"
1890
1891 try:
1892 task_status, ro_vim_item_update = self.item2class[my_task["item"]].new(
sousaedu80135b92021-02-17 15:05:18 +01001893 ro_task, task_index, task_depends
1894 )
tierno70eeb182020-10-19 16:38:00 +00001895 # TODO update other CREATE tasks
1896 except Exception as e:
1897 if not isinstance(e, NsWorkerException):
sousaedu80135b92021-02-17 15:05:18 +01001898 self.logger.error(
1899 "Error executing task={}: {}".format(task_id, e), exc_info=True
1900 )
1901
tierno70eeb182020-10-19 16:38:00 +00001902 task_status = "FAILED"
1903 ro_vim_item_update = {"vim_status": "VIM_ERROR", "vim_details": str(e)}
1904 # TODO update ro_vim_item_update
sousaedu80135b92021-02-17 15:05:18 +01001905
tierno70eeb182020-10-19 16:38:00 +00001906 return task_status, ro_vim_item_update
1907 else:
1908 return None, None
1909
1910 def _get_dependency(self, task_id, ro_task=None, target_id=None):
1911 """
1912 Look for dependency task
1913 :param task_id: Can be one of
1914 1. target_vim+blank+task.target_record_id: "(vim|sdn|wim):<id> (vnfrs|nsrs):(vld|vdu|flavor|image).<id>"
1915 2. task.target_record_id: "(vnfrs|nsrs):(vld|vdu|flavor|image).<id>"
1916 3. task.task_id: "<action_id>:number"
1917 :param ro_task:
1918 :param target_id:
1919 :return: database ro_task plus index of task
1920 """
sousaedu80135b92021-02-17 15:05:18 +01001921 if (
1922 task_id.startswith("vim:")
1923 or task_id.startswith("sdn:")
1924 or task_id.startswith("wim:")
1925 ):
tierno70eeb182020-10-19 16:38:00 +00001926 target_id, _, task_id = task_id.partition(" ")
1927
1928 if task_id.startswith("nsrs:") or task_id.startswith("vnfrs:"):
1929 ro_task_dependency = self.db.get_one(
1930 "ro_tasks",
sousaedu80135b92021-02-17 15:05:18 +01001931 q_filter={"target_id": target_id, "tasks.target_record_id": task_id},
1932 fail_on_empty=False,
1933 )
1934
tierno70eeb182020-10-19 16:38:00 +00001935 if ro_task_dependency:
1936 for task_index, task in enumerate(ro_task_dependency["tasks"]):
1937 if task["target_record_id"] == task_id:
1938 return ro_task_dependency, task_index
1939
1940 else:
1941 if ro_task:
1942 for task_index, task in enumerate(ro_task["tasks"]):
1943 if task and task["task_id"] == task_id:
1944 return ro_task, task_index
sousaedu80135b92021-02-17 15:05:18 +01001945
tierno70eeb182020-10-19 16:38:00 +00001946 ro_task_dependency = self.db.get_one(
1947 "ro_tasks",
sousaedu80135b92021-02-17 15:05:18 +01001948 q_filter={
1949 "tasks.ANYINDEX.task_id": task_id,
1950 "tasks.ANYINDEX.target_record.ne": None,
1951 },
1952 fail_on_empty=False,
1953 )
1954
palaciosj8f2060b2022-02-24 12:05:59 +00001955 self.logger.warning("ro_task_dependency={}".format(ro_task_dependency))
tierno70eeb182020-10-19 16:38:00 +00001956 if ro_task_dependency:
palaciosj8f2060b2022-02-24 12:05:59 +00001957 for task_index, task in enumerate(ro_task_dependency["tasks"]):
tierno70eeb182020-10-19 16:38:00 +00001958 if task["task_id"] == task_id:
1959 return ro_task_dependency, task_index
1960 raise NsWorkerException("Cannot get depending task {}".format(task_id))
1961
1962 def _process_pending_tasks(self, ro_task):
1963 ro_task_id = ro_task["_id"]
1964 now = time.time()
sousaedu80135b92021-02-17 15:05:18 +01001965 # one day
1966 next_check_at = now + (24 * 60 * 60)
tierno70eeb182020-10-19 16:38:00 +00001967 db_ro_task_update = {}
1968
1969 def _update_refresh(new_status):
1970 # compute next_refresh
1971 nonlocal task
1972 nonlocal next_check_at
1973 nonlocal db_ro_task_update
1974 nonlocal ro_task
1975
1976 next_refresh = time.time()
sousaedu80135b92021-02-17 15:05:18 +01001977
tierno70eeb182020-10-19 16:38:00 +00001978 if task["item"] in ("image", "flavor"):
1979 next_refresh += self.REFRESH_IMAGE
1980 elif new_status == "BUILD":
1981 next_refresh += self.REFRESH_BUILD
1982 elif new_status == "DONE":
1983 next_refresh += self.REFRESH_ACTIVE
1984 else:
1985 next_refresh += self.REFRESH_ERROR
sousaedu80135b92021-02-17 15:05:18 +01001986
tierno70eeb182020-10-19 16:38:00 +00001987 next_check_at = min(next_check_at, next_refresh)
1988 db_ro_task_update["vim_info.refresh_at"] = next_refresh
1989 ro_task["vim_info"]["refresh_at"] = next_refresh
1990
1991 try:
gallardo2f4aaaa2022-01-31 16:50:48 +00001992 """
1993 # Log RO tasks only when loglevel is DEBUG
1994 if self.logger.getEffectiveLevel() == logging.DEBUG:
1995 self._log_ro_task(ro_task, None, None, "TASK_WF", "GET_TASK")
1996 """
tiernof1b640f2020-12-09 15:06:01 +00001997 # 0: get task_status_create
1998 lock_object = None
tierno70eeb182020-10-19 16:38:00 +00001999 task_status_create = None
sousaedu80135b92021-02-17 15:05:18 +01002000 task_create = next(
2001 (
2002 t
2003 for t in ro_task["tasks"]
2004 if t
2005 and t["action"] == "CREATE"
2006 and t["status"] in ("BUILD", "DONE")
2007 ),
2008 None,
2009 )
2010
tierno70eeb182020-10-19 16:38:00 +00002011 if task_create:
2012 task_status_create = task_create["status"]
sousaedu80135b92021-02-17 15:05:18 +01002013
tiernof1b640f2020-12-09 15:06:01 +00002014 # 1: look for tasks in status SCHEDULED, or in status CREATE if action is DONE or BUILD
tierno70eeb182020-10-19 16:38:00 +00002015 for task_action in ("DELETE", "CREATE", "EXEC"):
2016 db_vim_update = None
2017 new_status = None
sousaedu80135b92021-02-17 15:05:18 +01002018
tierno70eeb182020-10-19 16:38:00 +00002019 for task_index, task in enumerate(ro_task["tasks"]):
2020 if not task:
2021 continue # task deleted
sousaedu80135b92021-02-17 15:05:18 +01002022
tierno55fa0bb2020-12-08 23:11:53 +00002023 task_depends = {}
tierno70eeb182020-10-19 16:38:00 +00002024 target_update = None
sousaedu80135b92021-02-17 15:05:18 +01002025
2026 if (
2027 (
2028 task_action in ("DELETE", "EXEC")
2029 and task["status"] not in ("SCHEDULED", "BUILD")
2030 )
2031 or task["action"] != task_action
2032 or (
2033 task_action == "CREATE"
2034 and task["status"] in ("FINISHED", "SUPERSEDED")
2035 )
2036 ):
tierno70eeb182020-10-19 16:38:00 +00002037 continue
sousaedu80135b92021-02-17 15:05:18 +01002038
tierno70eeb182020-10-19 16:38:00 +00002039 task_path = "tasks.{}.status".format(task_index)
2040 try:
2041 db_vim_info_update = None
sousaedu80135b92021-02-17 15:05:18 +01002042
tierno70eeb182020-10-19 16:38:00 +00002043 if task["status"] == "SCHEDULED":
tierno70eeb182020-10-19 16:38:00 +00002044 # check if tasks that this depends on have been completed
2045 dependency_not_completed = False
sousaedu80135b92021-02-17 15:05:18 +01002046
2047 for dependency_task_id in task.get("depends_on") or ():
2048 (
2049 dependency_ro_task,
2050 dependency_task_index,
2051 ) = self._get_dependency(
2052 dependency_task_id, target_id=ro_task["target_id"]
2053 )
2054 dependency_task = dependency_ro_task["tasks"][
2055 dependency_task_index
2056 ]
palaciosj8f2060b2022-02-24 12:05:59 +00002057 self.logger.warning("dependency_ro_task={} dependency_task_index={}".format(dependency_ro_task, dependency_task_index))
sousaedu80135b92021-02-17 15:05:18 +01002058
tierno70eeb182020-10-19 16:38:00 +00002059 if dependency_task["status"] == "SCHEDULED":
2060 dependency_not_completed = True
sousaedu80135b92021-02-17 15:05:18 +01002061 next_check_at = min(
2062 next_check_at, dependency_ro_task["to_check_at"]
2063 )
lloretgalleg88486222021-02-19 12:06:52 +00002064 # must allow dependent task to be processed first
2065 # to do this set time after last_task_processed
2066 next_check_at = max(
2067 self.time_last_task_processed, next_check_at
2068 )
tierno70eeb182020-10-19 16:38:00 +00002069 break
2070 elif dependency_task["status"] == "FAILED":
2071 error_text = "Cannot {} {} because depends on failed {} {} id={}): {}".format(
sousaedu80135b92021-02-17 15:05:18 +01002072 task["action"],
2073 task["item"],
2074 dependency_task["action"],
2075 dependency_task["item"],
2076 dependency_task_id,
2077 dependency_ro_task["vim_info"].get(
2078 "vim_details"
2079 ),
2080 )
2081 self.logger.error(
2082 "task={} {}".format(task["task_id"], error_text)
2083 )
tierno70eeb182020-10-19 16:38:00 +00002084 raise NsWorkerException(error_text)
2085
sousaedu80135b92021-02-17 15:05:18 +01002086 task_depends[dependency_task_id] = dependency_ro_task[
2087 "vim_info"
2088 ]["vim_id"]
2089 task_depends[
2090 "TASK-{}".format(dependency_task_id)
2091 ] = dependency_ro_task["vim_info"]["vim_id"]
2092
tierno70eeb182020-10-19 16:38:00 +00002093 if dependency_not_completed:
palaciosj8f2060b2022-02-24 12:05:59 +00002094 self.logger.warning("DEPENDENCY NOT COMPLETED {}".format(dependency_ro_task["vim_info"]["vim_id"]))
tierno70eeb182020-10-19 16:38:00 +00002095 # TODO set at vim_info.vim_details that it is waiting
2096 continue
sousaedu80135b92021-02-17 15:05:18 +01002097
tiernof1b640f2020-12-09 15:06:01 +00002098 # before calling VIM-plugin as it can take more than task_locked_time, insert to LockRenew
2099 # the task of renew this locking. It will update database locket_at periodically
2100 if not lock_object:
sousaedu80135b92021-02-17 15:05:18 +01002101 lock_object = LockRenew.add_lock_object(
2102 "ro_tasks", ro_task, self
2103 )
2104
tierno70eeb182020-10-19 16:38:00 +00002105 if task["action"] == "DELETE":
sousaedu80135b92021-02-17 15:05:18 +01002106 (new_status, db_vim_info_update,) = self._delete_task(
2107 ro_task, task_index, task_depends, db_ro_task_update
2108 )
2109 new_status = (
2110 "FINISHED" if new_status == "DONE" else new_status
2111 )
tierno70eeb182020-10-19 16:38:00 +00002112 # ^with FINISHED instead of DONE it will not be refreshing
sousaedu80135b92021-02-17 15:05:18 +01002113
tierno70eeb182020-10-19 16:38:00 +00002114 if new_status in ("FINISHED", "SUPERSEDED"):
2115 target_update = "DELETE"
2116 elif task["action"] == "EXEC":
sousaedu80135b92021-02-17 15:05:18 +01002117 (
2118 new_status,
2119 db_vim_info_update,
2120 db_task_update,
2121 ) = self.item2class[task["item"]].exec(
2122 ro_task, task_index, task_depends
2123 )
2124 new_status = (
2125 "FINISHED" if new_status == "DONE" else new_status
2126 )
tierno70eeb182020-10-19 16:38:00 +00002127 # ^with FINISHED instead of DONE it will not be refreshing
sousaedu80135b92021-02-17 15:05:18 +01002128
tierno70eeb182020-10-19 16:38:00 +00002129 if db_task_update:
2130 # load into database the modified db_task_update "retries" and "next_retry"
2131 if db_task_update.get("retries"):
sousaedu80135b92021-02-17 15:05:18 +01002132 db_ro_task_update[
2133 "tasks.{}.retries".format(task_index)
2134 ] = db_task_update["retries"]
2135
2136 next_check_at = time.time() + db_task_update.get(
2137 "next_retry", 60
2138 )
tierno70eeb182020-10-19 16:38:00 +00002139 target_update = None
2140 elif task["action"] == "CREATE":
2141 if task["status"] == "SCHEDULED":
2142 if task_status_create:
2143 new_status = task_status_create
2144 target_update = "COPY_VIM_INFO"
2145 else:
sousaedu80135b92021-02-17 15:05:18 +01002146 new_status, db_vim_info_update = self.item2class[
2147 task["item"]
2148 ].new(ro_task, task_index, task_depends)
tierno70eeb182020-10-19 16:38:00 +00002149 # self._create_task(ro_task, task_index, task_depends, db_ro_task_update)
2150 _update_refresh(new_status)
2151 else:
sousaedu80135b92021-02-17 15:05:18 +01002152 if (
2153 ro_task["vim_info"]["refresh_at"]
2154 and now > ro_task["vim_info"]["refresh_at"]
2155 ):
2156 new_status, db_vim_info_update = self.item2class[
2157 task["item"]
2158 ].refresh(ro_task)
tierno70eeb182020-10-19 16:38:00 +00002159 _update_refresh(new_status)
gallardo7788f692022-01-20 09:07:08 +00002160 else:
2161 # The refresh is updated to avoid set the value of "refresh_at" to
2162 # default value (next_check_at = now + (24 * 60 * 60)) when status is BUILD,
2163 # because it can happen that in this case the task is never processed
2164 _update_refresh(task["status"])
sousaedu80135b92021-02-17 15:05:18 +01002165
tierno70eeb182020-10-19 16:38:00 +00002166 except Exception as e:
2167 new_status = "FAILED"
sousaedu80135b92021-02-17 15:05:18 +01002168 db_vim_info_update = {
2169 "vim_status": "VIM_ERROR",
2170 "vim_details": str(e),
2171 }
2172
2173 if not isinstance(
2174 e, (NsWorkerException, vimconn.VimConnException)
2175 ):
2176 self.logger.error(
2177 "Unexpected exception at _delete_task task={}: {}".format(
2178 task["task_id"], e
2179 ),
2180 exc_info=True,
2181 )
tierno70eeb182020-10-19 16:38:00 +00002182
2183 try:
2184 if db_vim_info_update:
2185 db_vim_update = db_vim_info_update.copy()
sousaedu80135b92021-02-17 15:05:18 +01002186 db_ro_task_update.update(
2187 {
2188 "vim_info." + k: v
2189 for k, v in db_vim_info_update.items()
2190 }
2191 )
tierno70eeb182020-10-19 16:38:00 +00002192 ro_task["vim_info"].update(db_vim_info_update)
2193
2194 if new_status:
2195 if task_action == "CREATE":
2196 task_status_create = new_status
2197 db_ro_task_update[task_path] = new_status
tierno70eeb182020-10-19 16:38:00 +00002198
sousaedu80135b92021-02-17 15:05:18 +01002199 if target_update or db_vim_update:
tierno70eeb182020-10-19 16:38:00 +00002200 if target_update == "DELETE":
2201 self._update_target(task, None)
2202 elif target_update == "COPY_VIM_INFO":
2203 self._update_target(task, ro_task["vim_info"])
2204 else:
2205 self._update_target(task, db_vim_update)
2206
2207 except Exception as e:
sousaedu80135b92021-02-17 15:05:18 +01002208 if (
2209 isinstance(e, DbException)
2210 and e.http_code == HTTPStatus.NOT_FOUND
2211 ):
tierno70eeb182020-10-19 16:38:00 +00002212 # if the vnfrs or nsrs has been removed from database, this task must be removed
sousaedu80135b92021-02-17 15:05:18 +01002213 self.logger.debug(
2214 "marking to delete task={}".format(task["task_id"])
2215 )
tierno70eeb182020-10-19 16:38:00 +00002216 self.tasks_to_delete.append(task)
2217 else:
sousaedu80135b92021-02-17 15:05:18 +01002218 self.logger.error(
2219 "Unexpected exception at _update_target task={}: {}".format(
2220 task["task_id"], e
2221 ),
2222 exc_info=True,
2223 )
tierno70eeb182020-10-19 16:38:00 +00002224
tiernof1b640f2020-12-09 15:06:01 +00002225 locked_at = ro_task["locked_at"]
sousaedu80135b92021-02-17 15:05:18 +01002226
tiernof1b640f2020-12-09 15:06:01 +00002227 if lock_object:
sousaedu80135b92021-02-17 15:05:18 +01002228 locked_at = [
2229 lock_object["locked_at"],
2230 lock_object["locked_at"] + self.task_locked_time,
2231 ]
tiernof1b640f2020-12-09 15:06:01 +00002232 # locked_at contains two times to avoid race condition. In case the lock has been renew, it will
2233 # contain exactly locked_at + self.task_locked_time
2234 LockRenew.remove_lock_object(lock_object)
sousaedu80135b92021-02-17 15:05:18 +01002235
2236 q_filter = {
2237 "_id": ro_task["_id"],
2238 "to_check_at": ro_task["to_check_at"],
2239 "locked_at": locked_at,
2240 }
tierno70eeb182020-10-19 16:38:00 +00002241 # modify own task. Try filtering by to_next_check. For race condition if to_check_at has been modified,
2242 # outside this task (by ro_nbi) do not update it
2243 db_ro_task_update["locked_by"] = None
2244 # locked_at converted to int only for debugging. When has not decimals it means it has been unlocked
tiernof1b640f2020-12-09 15:06:01 +00002245 db_ro_task_update["locked_at"] = int(now - self.task_locked_time)
2246 db_ro_task_update["modified_at"] = now
tierno70eeb182020-10-19 16:38:00 +00002247 db_ro_task_update["to_check_at"] = next_check_at
sousaedu80135b92021-02-17 15:05:18 +01002248
gallardo2f4aaaa2022-01-31 16:50:48 +00002249 """
2250 # Log RO tasks only when loglevel is DEBUG
2251 if self.logger.getEffectiveLevel() == logging.DEBUG:
2252 db_ro_task_update_log = db_ro_task_update.copy()
2253 db_ro_task_update_log["_id"] = q_filter["_id"]
2254 self._log_ro_task(None, db_ro_task_update_log, None, "TASK_WF", "SET_TASK")
2255 """
2256
sousaedu80135b92021-02-17 15:05:18 +01002257 if not self.db.set_one(
2258 "ro_tasks",
2259 update_dict=db_ro_task_update,
2260 q_filter=q_filter,
2261 fail_on_empty=False,
2262 ):
tierno70eeb182020-10-19 16:38:00 +00002263 del db_ro_task_update["to_check_at"]
2264 del q_filter["to_check_at"]
gallardo2f4aaaa2022-01-31 16:50:48 +00002265 """
2266 # Log RO tasks only when loglevel is DEBUG
2267 if self.logger.getEffectiveLevel() == logging.DEBUG:
2268 self._log_ro_task(
2269 None,
2270 db_ro_task_update_log,
2271 None,
2272 "TASK_WF",
2273 "SET_TASK " + str(q_filter),
2274 )
2275 """
sousaedu80135b92021-02-17 15:05:18 +01002276 self.db.set_one(
2277 "ro_tasks",
2278 q_filter=q_filter,
2279 update_dict=db_ro_task_update,
2280 fail_on_empty=True,
2281 )
tierno70eeb182020-10-19 16:38:00 +00002282 except DbException as e:
sousaedu80135b92021-02-17 15:05:18 +01002283 self.logger.error(
2284 "ro_task={} Error updating database {}".format(ro_task_id, e)
2285 )
tierno70eeb182020-10-19 16:38:00 +00002286 except Exception as e:
sousaedu80135b92021-02-17 15:05:18 +01002287 self.logger.error(
2288 "Error executing ro_task={}: {}".format(ro_task_id, e), exc_info=True
2289 )
tierno70eeb182020-10-19 16:38:00 +00002290
2291 def _update_target(self, task, ro_vim_item_update):
2292 table, _, temp = task["target_record"].partition(":")
2293 _id, _, path_vim_status = temp.partition(":")
sousaedu80135b92021-02-17 15:05:18 +01002294 path_item = path_vim_status[: path_vim_status.rfind(".")]
2295 path_item = path_item[: path_item.rfind(".")]
tierno70eeb182020-10-19 16:38:00 +00002296 # path_vim_status: dot separated list targeting vim information, e.g. "vdur.10.vim_info.vim:id"
2297 # path_item: dot separated list targeting record information, e.g. "vdur.10"
sousaedu80135b92021-02-17 15:05:18 +01002298
tierno70eeb182020-10-19 16:38:00 +00002299 if ro_vim_item_update:
sousaedu80135b92021-02-17 15:05:18 +01002300 update_dict = {
2301 path_vim_status + "." + k: v
2302 for k, v in ro_vim_item_update.items()
2303 if k
2304 in ("vim_id", "vim_details", "vim_name", "vim_status", "interfaces")
2305 }
2306
tierno70eeb182020-10-19 16:38:00 +00002307 if path_vim_status.startswith("vdur."):
2308 # for backward compatibility, add vdur.name apart from vdur.vim_name
2309 if ro_vim_item_update.get("vim_name"):
2310 update_dict[path_item + ".name"] = ro_vim_item_update["vim_name"]
sousaedu80135b92021-02-17 15:05:18 +01002311
tierno70eeb182020-10-19 16:38:00 +00002312 # for backward compatibility, add vdur.vim-id apart from vdur.vim_id
2313 if ro_vim_item_update.get("vim_id"):
2314 update_dict[path_item + ".vim-id"] = ro_vim_item_update["vim_id"]
sousaedu80135b92021-02-17 15:05:18 +01002315
tierno70eeb182020-10-19 16:38:00 +00002316 # update general status
2317 if ro_vim_item_update.get("vim_status"):
sousaedu80135b92021-02-17 15:05:18 +01002318 update_dict[path_item + ".status"] = ro_vim_item_update[
2319 "vim_status"
2320 ]
2321
tierno70eeb182020-10-19 16:38:00 +00002322 if ro_vim_item_update.get("interfaces"):
2323 path_interfaces = path_item + ".interfaces"
sousaedu80135b92021-02-17 15:05:18 +01002324
tierno70eeb182020-10-19 16:38:00 +00002325 for i, iface in enumerate(ro_vim_item_update.get("interfaces")):
2326 if iface:
sousaedu80135b92021-02-17 15:05:18 +01002327 update_dict.update(
2328 {
2329 path_interfaces + ".{}.".format(i) + k: v
2330 for k, v in iface.items()
2331 if k in ("vlan", "compute_node", "pci")
2332 }
2333 )
2334
tierno70eeb182020-10-19 16:38:00 +00002335 # put ip_address and mac_address with ip-address and mac-address
sousaedu80135b92021-02-17 15:05:18 +01002336 if iface.get("ip_address"):
2337 update_dict[
2338 path_interfaces + ".{}.".format(i) + "ip-address"
2339 ] = iface["ip_address"]
2340
2341 if iface.get("mac_address"):
2342 update_dict[
2343 path_interfaces + ".{}.".format(i) + "mac-address"
2344 ] = iface["mac_address"]
2345
tierno70eeb182020-10-19 16:38:00 +00002346 if iface.get("mgmt_vnf_interface") and iface.get("ip_address"):
sousaedu80135b92021-02-17 15:05:18 +01002347 update_dict["ip-address"] = iface.get("ip_address").split(
2348 ";"
2349 )[0]
2350
tierno70eeb182020-10-19 16:38:00 +00002351 if iface.get("mgmt_vdu_interface") and iface.get("ip_address"):
sousaedu80135b92021-02-17 15:05:18 +01002352 update_dict[path_item + ".ip-address"] = iface.get(
2353 "ip_address"
2354 ).split(";")[0]
tierno70eeb182020-10-19 16:38:00 +00002355
2356 self.db.set_one(table, q_filter={"_id": _id}, update_dict=update_dict)
2357 else:
2358 update_dict = {path_item + ".status": "DELETED"}
sousaedu80135b92021-02-17 15:05:18 +01002359 self.db.set_one(
2360 table,
2361 q_filter={"_id": _id},
2362 update_dict=update_dict,
2363 unset={path_vim_status: None},
2364 )
tierno70eeb182020-10-19 16:38:00 +00002365
2366 def _process_delete_db_tasks(self):
2367 """
2368 Delete task from database because vnfrs or nsrs or both have been deleted
2369 :return: None. Uses and modify self.tasks_to_delete
2370 """
2371 while self.tasks_to_delete:
2372 task = self.tasks_to_delete[0]
2373 vnfrs_deleted = None
2374 nsr_id = task["nsr_id"]
sousaedu80135b92021-02-17 15:05:18 +01002375
tierno70eeb182020-10-19 16:38:00 +00002376 if task["target_record"].startswith("vnfrs:"):
2377 # check if nsrs is present
2378 if self.db.get_one("nsrs", {"_id": nsr_id}, fail_on_empty=False):
2379 vnfrs_deleted = task["target_record"].split(":")[1]
sousaedu80135b92021-02-17 15:05:18 +01002380
tierno70eeb182020-10-19 16:38:00 +00002381 try:
2382 self.delete_db_tasks(self.db, nsr_id, vnfrs_deleted)
2383 except Exception as e:
sousaedu80135b92021-02-17 15:05:18 +01002384 self.logger.error(
2385 "Error deleting task={}: {}".format(task["task_id"], e)
2386 )
tierno70eeb182020-10-19 16:38:00 +00002387 self.tasks_to_delete.pop(0)
2388
2389 @staticmethod
2390 def delete_db_tasks(db, nsr_id, vnfrs_deleted):
2391 """
2392 Static method because it is called from osm_ng_ro.ns
2393 :param db: instance of database to use
2394 :param nsr_id: affected nsrs id
2395 :param vnfrs_deleted: only tasks with this vnfr id. If None, all affected by nsr_id
2396 :return: None, exception is fails
2397 """
2398 retries = 5
2399 for retry in range(retries):
2400 ro_tasks = db.get_list("ro_tasks", {"tasks.nsr_id": nsr_id})
2401 now = time.time()
2402 conflict = False
sousaedu80135b92021-02-17 15:05:18 +01002403
tierno70eeb182020-10-19 16:38:00 +00002404 for ro_task in ro_tasks:
2405 db_update = {}
2406 to_delete_ro_task = True
sousaedu80135b92021-02-17 15:05:18 +01002407
tierno70eeb182020-10-19 16:38:00 +00002408 for index, task in enumerate(ro_task["tasks"]):
2409 if not task:
2410 pass
sousaedu80135b92021-02-17 15:05:18 +01002411 elif (not vnfrs_deleted and task["nsr_id"] == nsr_id) or (
2412 vnfrs_deleted
2413 and task["target_record"].startswith("vnfrs:" + vnfrs_deleted)
2414 ):
tierno70eeb182020-10-19 16:38:00 +00002415 db_update["tasks.{}".format(index)] = None
2416 else:
sousaedu80135b92021-02-17 15:05:18 +01002417 # used by other nsr, ro_task cannot be deleted
2418 to_delete_ro_task = False
2419
tierno70eeb182020-10-19 16:38:00 +00002420 # delete or update if nobody has changed ro_task meanwhile. Used modified_at for known if changed
2421 if to_delete_ro_task:
sousaedu80135b92021-02-17 15:05:18 +01002422 if not db.del_one(
2423 "ro_tasks",
2424 q_filter={
2425 "_id": ro_task["_id"],
2426 "modified_at": ro_task["modified_at"],
2427 },
2428 fail_on_empty=False,
2429 ):
tierno70eeb182020-10-19 16:38:00 +00002430 conflict = True
2431 elif db_update:
2432 db_update["modified_at"] = now
sousaedu80135b92021-02-17 15:05:18 +01002433 if not db.set_one(
2434 "ro_tasks",
2435 q_filter={
2436 "_id": ro_task["_id"],
2437 "modified_at": ro_task["modified_at"],
2438 },
2439 update_dict=db_update,
2440 fail_on_empty=False,
2441 ):
tierno70eeb182020-10-19 16:38:00 +00002442 conflict = True
2443 if not conflict:
2444 return
2445 else:
2446 raise NsWorkerException("Exceeded {} retries".format(retries))
2447
tierno1d213f42020-04-24 14:02:51 +00002448 def run(self):
2449 # load database
tierno86153522020-12-06 18:27:16 +00002450 self.logger.info("Starting")
tierno1d213f42020-04-24 14:02:51 +00002451 while True:
tierno70eeb182020-10-19 16:38:00 +00002452 # step 1: get commands from queue
tierno1d213f42020-04-24 14:02:51 +00002453 try:
tierno86153522020-12-06 18:27:16 +00002454 if self.vim_targets:
2455 task = self.task_queue.get(block=False)
2456 else:
2457 if not self.idle:
2458 self.logger.debug("enters in idle state")
2459 self.idle = True
2460 task = self.task_queue.get(block=True)
2461 self.idle = False
2462
tierno1d213f42020-04-24 14:02:51 +00002463 if task[0] == "terminate":
2464 break
tierno70eeb182020-10-19 16:38:00 +00002465 elif task[0] == "load_vim":
tierno86153522020-12-06 18:27:16 +00002466 self.logger.info("order to load vim {}".format(task[1]))
tierno1d213f42020-04-24 14:02:51 +00002467 self._load_vim(task[1])
tierno70eeb182020-10-19 16:38:00 +00002468 elif task[0] == "unload_vim":
tierno86153522020-12-06 18:27:16 +00002469 self.logger.info("order to unload vim {}".format(task[1]))
tierno70eeb182020-10-19 16:38:00 +00002470 self._unload_vim(task[1])
2471 elif task[0] == "reload_vim":
2472 self._reload_vim(task[1])
2473 elif task[0] == "check_vim":
tierno86153522020-12-06 18:27:16 +00002474 self.logger.info("order to check vim {}".format(task[1]))
tierno70eeb182020-10-19 16:38:00 +00002475 self._check_vim(task[1])
tierno1d213f42020-04-24 14:02:51 +00002476 continue
tierno70eeb182020-10-19 16:38:00 +00002477 except Exception as e:
2478 if isinstance(e, queue.Empty):
2479 pass
2480 else:
sousaedu80135b92021-02-17 15:05:18 +01002481 self.logger.critical(
2482 "Error processing task: {}".format(e), exc_info=True
2483 )
tierno1d213f42020-04-24 14:02:51 +00002484
tierno70eeb182020-10-19 16:38:00 +00002485 # step 2: process pending_tasks, delete not needed tasks
tierno1d213f42020-04-24 14:02:51 +00002486 try:
tierno70eeb182020-10-19 16:38:00 +00002487 if self.tasks_to_delete:
2488 self._process_delete_db_tasks()
tierno1d213f42020-04-24 14:02:51 +00002489 busy = False
gallardo2f4aaaa2022-01-31 16:50:48 +00002490 """
2491 # Log RO tasks only when loglevel is DEBUG
2492 if self.logger.getEffectiveLevel() == logging.DEBUG:
2493 _ = self._get_db_all_tasks()
2494 """
tierno1d213f42020-04-24 14:02:51 +00002495 ro_task = self._get_db_task()
2496 if ro_task:
palaciosj8f2060b2022-02-24 12:05:59 +00002497 self.logger.warning("Task to process: {}".format(ro_task))
2498 time.sleep(1)
tierno70eeb182020-10-19 16:38:00 +00002499 self._process_pending_tasks(ro_task)
tierno1d213f42020-04-24 14:02:51 +00002500 busy = True
2501 if not busy:
2502 time.sleep(5)
2503 except Exception as e:
sousaedu80135b92021-02-17 15:05:18 +01002504 self.logger.critical(
2505 "Unexpected exception at run: " + str(e), exc_info=True
2506 )
tierno1d213f42020-04-24 14:02:51 +00002507
tierno86153522020-12-06 18:27:16 +00002508 self.logger.info("Finishing")