blob: 106830de3b5902d120c463ea4b7f04e3dad58f0c [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
sousaedu0fe84cb2022-01-17 13:48:22 +000029import logging
sousaedu80135b92021-02-17 15:05:18 +010030from os import mkdir
sousaedu0fe84cb2022-01-17 13:48:22 +000031import queue
sousaedu80135b92021-02-17 15:05:18 +010032from shutil import rmtree
sousaedu0fe84cb2022-01-17 13:48:22 +000033import threading
34import time
sousaedu80135b92021-02-17 15:05:18 +010035from unittest.mock import Mock
36
sousaedu0fe84cb2022-01-17 13:48:22 +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
sousaedu0fe84cb2022-01-17 13:48:22 +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
sousaedu80135b92021-02-17 15:05:18 +010044
tierno1d213f42020-04-24 14:02:51 +000045
46__author__ = "Alfonso Tierno"
47__date__ = "$28-Sep-2017 12:07:15$"
48
49
50def deep_get(target_dict, *args, **kwargs):
51 """
52 Get a value from target_dict entering in the nested keys. If keys does not exist, it returns None
53 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
54 :param target_dict: dictionary to be read
55 :param args: list of keys to read from target_dict
56 :param kwargs: only can contain default=value to return if key is not present in the nested dictionary
57 :return: The wanted value if exist, None or default otherwise
58 """
59 for key in args:
60 if not isinstance(target_dict, dict) or key not in target_dict:
61 return kwargs.get("default")
62 target_dict = target_dict[key]
63 return target_dict
64
65
66class NsWorkerException(Exception):
67 pass
68
69
70class FailingConnector:
71 def __init__(self, error_msg):
72 self.error_msg = error_msg
sousaedu80135b92021-02-17 15:05:18 +010073
tierno1d213f42020-04-24 14:02:51 +000074 for method in dir(vimconn.VimConnector):
75 if method[0] != "_":
sousaedu80135b92021-02-17 15:05:18 +010076 setattr(
77 self, method, Mock(side_effect=vimconn.VimConnException(error_msg))
78 )
79
tierno70eeb182020-10-19 16:38:00 +000080 for method in dir(sdnconn.SdnConnectorBase):
81 if method[0] != "_":
sousaedu80135b92021-02-17 15:05:18 +010082 setattr(
83 self, method, Mock(side_effect=sdnconn.SdnConnectorError(error_msg))
84 )
tierno1d213f42020-04-24 14:02:51 +000085
86
87class NsWorkerExceptionNotFound(NsWorkerException):
88 pass
89
90
tierno70eeb182020-10-19 16:38:00 +000091class VimInteractionBase:
sousaedu80135b92021-02-17 15:05:18 +010092 """Base class to call VIM/SDN for creating, deleting and refresh networks, VMs, flavors, ...
tierno70eeb182020-10-19 16:38:00 +000093 It implements methods that does nothing and return ok"""
sousaedu80135b92021-02-17 15:05:18 +010094
tierno70eeb182020-10-19 16:38:00 +000095 def __init__(self, db, my_vims, db_vims, logger):
tierno1d213f42020-04-24 14:02:51 +000096 self.db = db
tierno70eeb182020-10-19 16:38:00 +000097 self.logger = logger
98 self.my_vims = my_vims
99 self.db_vims = db_vims
tierno1d213f42020-04-24 14:02:51 +0000100
tierno70eeb182020-10-19 16:38:00 +0000101 def new(self, ro_task, task_index, task_depends):
102 return "BUILD", {}
tierno1d213f42020-04-24 14:02:51 +0000103
tierno70eeb182020-10-19 16:38:00 +0000104 def refresh(self, ro_task):
105 """skip calling VIM to get image, flavor status. Assumes ok"""
tierno1d213f42020-04-24 14:02:51 +0000106 if ro_task["vim_info"]["vim_status"] == "VIM_ERROR":
107 return "FAILED", {}
sousaedu80135b92021-02-17 15:05:18 +0100108
tierno1d213f42020-04-24 14:02:51 +0000109 return "DONE", {}
110
tierno70eeb182020-10-19 16:38:00 +0000111 def delete(self, ro_task, task_index):
112 """skip calling VIM to delete image. Assumes ok"""
tierno1d213f42020-04-24 14:02:51 +0000113 return "DONE", {}
114
tierno70eeb182020-10-19 16:38:00 +0000115 def exec(self, ro_task, task_index, task_depends):
116 return "DONE", None, None
tierno1d213f42020-04-24 14:02:51 +0000117
tierno1d213f42020-04-24 14:02:51 +0000118
tierno70eeb182020-10-19 16:38:00 +0000119class VimInteractionNet(VimInteractionBase):
tierno70eeb182020-10-19 16:38:00 +0000120 def new(self, ro_task, task_index, task_depends):
tierno1d213f42020-04-24 14:02:51 +0000121 vim_net_id = None
122 task = ro_task["tasks"][task_index]
123 task_id = task["task_id"]
124 created = False
125 created_items = {}
126 target_vim = self.my_vims[ro_task["target_id"]]
aticig2e38b942021-12-10 12:59:20 +0300127 mgmtnet = False
128 mgmtnet_defined_in_vim = False
sousaedu80135b92021-02-17 15:05:18 +0100129
tierno1d213f42020-04-24 14:02:51 +0000130 try:
131 # FIND
132 if task.get("find_params"):
133 # if management, get configuration of VIM
134 if task["find_params"].get("filter_dict"):
135 vim_filter = task["find_params"]["filter_dict"]
aticig2e38b942021-12-10 12:59:20 +0300136 # management network
sousaedu80135b92021-02-17 15:05:18 +0100137 elif task["find_params"].get("mgmt"):
aticig2e38b942021-12-10 12:59:20 +0300138 mgmtnet = True
sousaedu80135b92021-02-17 15:05:18 +0100139 if deep_get(
140 self.db_vims[ro_task["target_id"]],
141 "config",
142 "management_network_id",
143 ):
aticig2e38b942021-12-10 12:59:20 +0300144 mgmtnet_defined_in_vim = True
sousaedu80135b92021-02-17 15:05:18 +0100145 vim_filter = {
146 "id": self.db_vims[ro_task["target_id"]]["config"][
147 "management_network_id"
148 ]
149 }
150 elif deep_get(
151 self.db_vims[ro_task["target_id"]],
152 "config",
153 "management_network_name",
154 ):
aticig2e38b942021-12-10 12:59:20 +0300155 mgmtnet_defined_in_vim = True
sousaedu80135b92021-02-17 15:05:18 +0100156 vim_filter = {
157 "name": self.db_vims[ro_task["target_id"]]["config"][
158 "management_network_name"
159 ]
160 }
tierno1d213f42020-04-24 14:02:51 +0000161 else:
162 vim_filter = {"name": task["find_params"]["name"]}
163 else:
sousaedu80135b92021-02-17 15:05:18 +0100164 raise NsWorkerExceptionNotFound(
165 "Invalid find_params for new_net {}".format(task["find_params"])
166 )
tierno1d213f42020-04-24 14:02:51 +0000167
168 vim_nets = target_vim.get_network_list(vim_filter)
169 if not vim_nets and not task.get("params"):
aticig2e38b942021-12-10 12:59:20 +0300170 # If there is mgmt-network in the descriptor,
171 # there is no mapping of that network to a VIM network in the descriptor,
172 # also there is no mapping in the "--config" parameter or at VIM creation;
173 # that mgmt-network will be created.
174 if mgmtnet and not mgmtnet_defined_in_vim:
175 net_name = (
176 vim_filter.get("name")
177 if vim_filter.get("name")
178 else vim_filter.get("id")[:16]
sousaedu80135b92021-02-17 15:05:18 +0100179 )
aticig2e38b942021-12-10 12:59:20 +0300180 vim_net_id, created_items = target_vim.new_network(
181 net_name, None
182 )
183 self.logger.debug(
184 "Created mgmt network vim_net_id: {}".format(vim_net_id)
185 )
186 created = True
187 else:
188 raise NsWorkerExceptionNotFound(
189 "Network not found with this criteria: '{}'".format(
190 task.get("find_params")
191 )
192 )
tierno1d213f42020-04-24 14:02:51 +0000193 elif len(vim_nets) > 1:
194 raise NsWorkerException(
sousaedu80135b92021-02-17 15:05:18 +0100195 "More than one network found with this criteria: '{}'".format(
196 task["find_params"]
197 )
198 )
199
tierno1d213f42020-04-24 14:02:51 +0000200 if vim_nets:
201 vim_net_id = vim_nets[0]["id"]
202 else:
203 # CREATE
204 params = task["params"]
205 vim_net_id, created_items = target_vim.new_network(**params)
206 created = True
207
sousaedu80135b92021-02-17 15:05:18 +0100208 ro_vim_item_update = {
209 "vim_id": vim_net_id,
210 "vim_status": "BUILD",
211 "created": created,
212 "created_items": created_items,
213 "vim_details": None,
214 }
tierno1d213f42020-04-24 14:02:51 +0000215 self.logger.debug(
sousaedu80135b92021-02-17 15:05:18 +0100216 "task={} {} new-net={} created={}".format(
217 task_id, ro_task["target_id"], vim_net_id, created
218 )
219 )
220
tierno1d213f42020-04-24 14:02:51 +0000221 return "BUILD", ro_vim_item_update
222 except (vimconn.VimConnException, NsWorkerException) as e:
sousaedu80135b92021-02-17 15:05:18 +0100223 self.logger.error(
224 "task={} vim={} new-net: {}".format(task_id, ro_task["target_id"], e)
225 )
226 ro_vim_item_update = {
227 "vim_status": "VIM_ERROR",
228 "created": created,
229 "vim_details": str(e),
230 }
231
tierno1d213f42020-04-24 14:02:51 +0000232 return "FAILED", ro_vim_item_update
233
tierno70eeb182020-10-19 16:38:00 +0000234 def refresh(self, ro_task):
tierno1d213f42020-04-24 14:02:51 +0000235 """Call VIM to get network status"""
236 ro_task_id = ro_task["_id"]
237 target_vim = self.my_vims[ro_task["target_id"]]
tierno1d213f42020-04-24 14:02:51 +0000238 vim_id = ro_task["vim_info"]["vim_id"]
239 net_to_refresh_list = [vim_id]
sousaedu80135b92021-02-17 15:05:18 +0100240
tierno1d213f42020-04-24 14:02:51 +0000241 try:
242 vim_dict = target_vim.refresh_nets_status(net_to_refresh_list)
243 vim_info = vim_dict[vim_id]
sousaedu80135b92021-02-17 15:05:18 +0100244
tierno1d213f42020-04-24 14:02:51 +0000245 if vim_info["status"] == "ACTIVE":
246 task_status = "DONE"
247 elif vim_info["status"] == "BUILD":
248 task_status = "BUILD"
249 else:
250 task_status = "FAILED"
251 except vimconn.VimConnException as e:
252 # Mark all tasks at VIM_ERROR status
sousaedu80135b92021-02-17 15:05:18 +0100253 self.logger.error(
254 "ro_task={} vim={} get-net={}: {}".format(
255 ro_task_id, ro_task["target_id"], vim_id, e
256 )
257 )
tierno1d213f42020-04-24 14:02:51 +0000258 vim_info = {"status": "VIM_ERROR", "error_msg": str(e)}
259 task_status = "FAILED"
260
261 ro_vim_item_update = {}
262 if ro_task["vim_info"]["vim_status"] != vim_info["status"]:
263 ro_vim_item_update["vim_status"] = vim_info["status"]
sousaedu80135b92021-02-17 15:05:18 +0100264
tierno1d213f42020-04-24 14:02:51 +0000265 if ro_task["vim_info"]["vim_name"] != vim_info.get("name"):
266 ro_vim_item_update["vim_name"] = vim_info.get("name")
sousaedu80135b92021-02-17 15:05:18 +0100267
tierno1d213f42020-04-24 14:02:51 +0000268 if vim_info["status"] in ("ERROR", "VIM_ERROR"):
tierno70eeb182020-10-19 16:38:00 +0000269 if ro_task["vim_info"]["vim_details"] != vim_info.get("error_msg"):
270 ro_vim_item_update["vim_details"] = vim_info.get("error_msg")
tierno1d213f42020-04-24 14:02:51 +0000271 elif vim_info["status"] == "DELETED":
272 ro_vim_item_update["vim_id"] = None
273 ro_vim_item_update["vim_details"] = "Deleted externally"
274 else:
275 if ro_task["vim_info"]["vim_details"] != vim_info["vim_info"]:
276 ro_vim_item_update["vim_details"] = vim_info["vim_info"]
sousaedu80135b92021-02-17 15:05:18 +0100277
tierno1d213f42020-04-24 14:02:51 +0000278 if ro_vim_item_update:
sousaedu80135b92021-02-17 15:05:18 +0100279 self.logger.debug(
280 "ro_task={} {} get-net={}: status={} {}".format(
281 ro_task_id,
282 ro_task["target_id"],
283 vim_id,
284 ro_vim_item_update.get("vim_status"),
285 ro_vim_item_update.get("vim_details")
286 if ro_vim_item_update.get("vim_status") != "ACTIVE"
287 else "",
288 )
289 )
290
tierno1d213f42020-04-24 14:02:51 +0000291 return task_status, ro_vim_item_update
292
tierno70eeb182020-10-19 16:38:00 +0000293 def delete(self, ro_task, task_index):
tierno1d213f42020-04-24 14:02:51 +0000294 task = ro_task["tasks"][task_index]
295 task_id = task["task_id"]
296 net_vim_id = ro_task["vim_info"]["vim_id"]
sousaedu80135b92021-02-17 15:05:18 +0100297 ro_vim_item_update_ok = {
298 "vim_status": "DELETED",
299 "created": False,
300 "vim_details": "DELETED",
301 "vim_id": None,
302 }
303
tierno1d213f42020-04-24 14:02:51 +0000304 try:
305 if net_vim_id or ro_task["vim_info"]["created_items"]:
306 target_vim = self.my_vims[ro_task["target_id"]]
sousaedu80135b92021-02-17 15:05:18 +0100307 target_vim.delete_network(
308 net_vim_id, ro_task["vim_info"]["created_items"]
309 )
tierno1d213f42020-04-24 14:02:51 +0000310 except vimconn.VimConnNotFoundException:
311 ro_vim_item_update_ok["vim_details"] = "already deleted"
tierno1d213f42020-04-24 14:02:51 +0000312 except vimconn.VimConnException as e:
sousaedu80135b92021-02-17 15:05:18 +0100313 self.logger.error(
314 "ro_task={} vim={} del-net={}: {}".format(
315 ro_task["_id"], ro_task["target_id"], net_vim_id, e
316 )
317 )
318 ro_vim_item_update = {
319 "vim_status": "VIM_ERROR",
320 "vim_details": "Error while deleting: {}".format(e),
321 }
322
tierno1d213f42020-04-24 14:02:51 +0000323 return "FAILED", ro_vim_item_update
324
sousaedu80135b92021-02-17 15:05:18 +0100325 self.logger.debug(
326 "task={} {} del-net={} {}".format(
327 task_id,
328 ro_task["target_id"],
329 net_vim_id,
330 ro_vim_item_update_ok.get("vim_details", ""),
331 )
332 )
333
tierno1d213f42020-04-24 14:02:51 +0000334 return "DONE", ro_vim_item_update_ok
335
tierno70eeb182020-10-19 16:38:00 +0000336
337class VimInteractionVdu(VimInteractionBase):
sousaedu80135b92021-02-17 15:05:18 +0100338 max_retries_inject_ssh_key = 20 # 20 times
339 time_retries_inject_ssh_key = 30 # wevery 30 seconds
tierno70eeb182020-10-19 16:38:00 +0000340
341 def new(self, ro_task, task_index, task_depends):
tierno1d213f42020-04-24 14:02:51 +0000342 task = ro_task["tasks"][task_index]
343 task_id = task["task_id"]
344 created = False
345 created_items = {}
346 target_vim = self.my_vims[ro_task["target_id"]]
sousaedu80135b92021-02-17 15:05:18 +0100347
tierno1d213f42020-04-24 14:02:51 +0000348 try:
349 created = True
350 params = task["params"]
351 params_copy = deepcopy(params)
352 net_list = params_copy["net_list"]
sousaedu80135b92021-02-17 15:05:18 +0100353
tierno1d213f42020-04-24 14:02:51 +0000354 for net in net_list:
sousaedu80135b92021-02-17 15:05:18 +0100355 # change task_id into network_id
356 if "net_id" in net and net["net_id"].startswith("TASK-"):
tierno1d213f42020-04-24 14:02:51 +0000357 network_id = task_depends[net["net_id"]]
sousaedu80135b92021-02-17 15:05:18 +0100358
tierno1d213f42020-04-24 14:02:51 +0000359 if not network_id:
sousaedu80135b92021-02-17 15:05:18 +0100360 raise NsWorkerException(
361 "Cannot create VM because depends on a network not created or found "
362 "for {}".format(net["net_id"])
363 )
364
tierno1d213f42020-04-24 14:02:51 +0000365 net["net_id"] = network_id
sousaedu80135b92021-02-17 15:05:18 +0100366
tierno1d213f42020-04-24 14:02:51 +0000367 if params_copy["image_id"].startswith("TASK-"):
368 params_copy["image_id"] = task_depends[params_copy["image_id"]]
sousaedu80135b92021-02-17 15:05:18 +0100369
tierno1d213f42020-04-24 14:02:51 +0000370 if params_copy["flavor_id"].startswith("TASK-"):
371 params_copy["flavor_id"] = task_depends[params_copy["flavor_id"]]
372
Alexis Romero73993212022-04-13 18:03:30 +0200373 affinity_group_list = params_copy["affinity_group_list"]
374 for affinity_group in affinity_group_list:
375 # change task_id into affinity_group_id
376 if "affinity_group_id" in affinity_group and affinity_group[
377 "affinity_group_id"
378 ].startswith("TASK-"):
379 affinity_group_id = task_depends[
380 affinity_group["affinity_group_id"]
381 ]
382
383 if not affinity_group_id:
384 raise NsWorkerException(
385 "found for {}".format(affinity_group["affinity_group_id"])
386 )
387
388 affinity_group["affinity_group_id"] = affinity_group_id
389
tierno1d213f42020-04-24 14:02:51 +0000390 vim_vm_id, created_items = target_vim.new_vminstance(**params_copy)
391 interfaces = [iface["vim_id"] for iface in params_copy["net_list"]]
392
sousaedu80135b92021-02-17 15:05:18 +0100393 ro_vim_item_update = {
394 "vim_id": vim_vm_id,
395 "vim_status": "BUILD",
396 "created": created,
397 "created_items": created_items,
398 "vim_details": None,
399 "interfaces_vim_ids": interfaces,
400 "interfaces": [],
401 }
tierno1d213f42020-04-24 14:02:51 +0000402 self.logger.debug(
sousaedu80135b92021-02-17 15:05:18 +0100403 "task={} {} new-vm={} created={}".format(
404 task_id, ro_task["target_id"], vim_vm_id, created
405 )
406 )
407
tierno1d213f42020-04-24 14:02:51 +0000408 return "BUILD", ro_vim_item_update
409 except (vimconn.VimConnException, NsWorkerException) as e:
sousaedu80135b92021-02-17 15:05:18 +0100410 self.logger.error(
411 "task={} {} new-vm: {}".format(task_id, ro_task["target_id"], e)
412 )
413 ro_vim_item_update = {
414 "vim_status": "VIM_ERROR",
415 "created": created,
416 "vim_details": str(e),
417 }
418
tierno1d213f42020-04-24 14:02:51 +0000419 return "FAILED", ro_vim_item_update
420
tierno70eeb182020-10-19 16:38:00 +0000421 def delete(self, ro_task, task_index):
tierno1d213f42020-04-24 14:02:51 +0000422 task = ro_task["tasks"][task_index]
423 task_id = task["task_id"]
424 vm_vim_id = ro_task["vim_info"]["vim_id"]
sousaedu80135b92021-02-17 15:05:18 +0100425 ro_vim_item_update_ok = {
426 "vim_status": "DELETED",
427 "created": False,
428 "vim_details": "DELETED",
429 "vim_id": None,
430 }
431
tierno1d213f42020-04-24 14:02:51 +0000432 try:
433 if vm_vim_id or ro_task["vim_info"]["created_items"]:
434 target_vim = self.my_vims[ro_task["target_id"]]
sousaedu80135b92021-02-17 15:05:18 +0100435 target_vim.delete_vminstance(
436 vm_vim_id, ro_task["vim_info"]["created_items"]
437 )
tierno1d213f42020-04-24 14:02:51 +0000438 except vimconn.VimConnNotFoundException:
439 ro_vim_item_update_ok["vim_details"] = "already deleted"
tierno1d213f42020-04-24 14:02:51 +0000440 except vimconn.VimConnException as e:
sousaedu80135b92021-02-17 15:05:18 +0100441 self.logger.error(
442 "ro_task={} vim={} del-vm={}: {}".format(
443 ro_task["_id"], ro_task["target_id"], vm_vim_id, e
444 )
445 )
446 ro_vim_item_update = {
447 "vim_status": "VIM_ERROR",
448 "vim_details": "Error while deleting: {}".format(e),
449 }
450
tierno1d213f42020-04-24 14:02:51 +0000451 return "FAILED", ro_vim_item_update
452
sousaedu80135b92021-02-17 15:05:18 +0100453 self.logger.debug(
454 "task={} {} del-vm={} {}".format(
455 task_id,
456 ro_task["target_id"],
457 vm_vim_id,
458 ro_vim_item_update_ok.get("vim_details", ""),
459 )
460 )
461
tierno1d213f42020-04-24 14:02:51 +0000462 return "DONE", ro_vim_item_update_ok
463
tierno70eeb182020-10-19 16:38:00 +0000464 def refresh(self, ro_task):
tierno1d213f42020-04-24 14:02:51 +0000465 """Call VIM to get vm status"""
466 ro_task_id = ro_task["_id"]
467 target_vim = self.my_vims[ro_task["target_id"]]
tierno1d213f42020-04-24 14:02:51 +0000468 vim_id = ro_task["vim_info"]["vim_id"]
sousaedu80135b92021-02-17 15:05:18 +0100469
tierno1d213f42020-04-24 14:02:51 +0000470 if not vim_id:
471 return None, None
sousaedu80135b92021-02-17 15:05:18 +0100472
tierno1d213f42020-04-24 14:02:51 +0000473 vm_to_refresh_list = [vim_id]
474 try:
475 vim_dict = target_vim.refresh_vms_status(vm_to_refresh_list)
476 vim_info = vim_dict[vim_id]
sousaedu80135b92021-02-17 15:05:18 +0100477
tierno1d213f42020-04-24 14:02:51 +0000478 if vim_info["status"] == "ACTIVE":
479 task_status = "DONE"
480 elif vim_info["status"] == "BUILD":
481 task_status = "BUILD"
482 else:
483 task_status = "FAILED"
sousaedu80135b92021-02-17 15:05:18 +0100484
tierno70eeb182020-10-19 16:38:00 +0000485 # try to load and parse vim_information
486 try:
487 vim_info_info = yaml.safe_load(vim_info["vim_info"])
488 if vim_info_info.get("name"):
489 vim_info["name"] = vim_info_info["name"]
490 except Exception:
491 pass
tierno1d213f42020-04-24 14:02:51 +0000492 except vimconn.VimConnException as e:
493 # Mark all tasks at VIM_ERROR status
sousaedu80135b92021-02-17 15:05:18 +0100494 self.logger.error(
495 "ro_task={} vim={} get-vm={}: {}".format(
496 ro_task_id, ro_task["target_id"], vim_id, e
497 )
498 )
tierno1d213f42020-04-24 14:02:51 +0000499 vim_info = {"status": "VIM_ERROR", "error_msg": str(e)}
500 task_status = "FAILED"
501
502 ro_vim_item_update = {}
sousaedu80135b92021-02-17 15:05:18 +0100503
tierno70eeb182020-10-19 16:38:00 +0000504 # Interfaces cannot be present if e.g. VM is not present, that is status=DELETED
tierno1d213f42020-04-24 14:02:51 +0000505 vim_interfaces = []
tierno70eeb182020-10-19 16:38:00 +0000506 if vim_info.get("interfaces"):
507 for vim_iface_id in ro_task["vim_info"]["interfaces_vim_ids"]:
sousaedu80135b92021-02-17 15:05:18 +0100508 iface = next(
509 (
510 iface
511 for iface in vim_info["interfaces"]
512 if vim_iface_id == iface["vim_interface_id"]
513 ),
514 None,
515 )
tierno70eeb182020-10-19 16:38:00 +0000516 # if iface:
517 # iface.pop("vim_info", None)
518 vim_interfaces.append(iface)
tierno1d213f42020-04-24 14:02:51 +0000519
sousaedu80135b92021-02-17 15:05:18 +0100520 task_create = next(
521 t
522 for t in ro_task["tasks"]
523 if t and t["action"] == "CREATE" and t["status"] != "FINISHED"
524 )
tierno70eeb182020-10-19 16:38:00 +0000525 if vim_interfaces and task_create.get("mgmt_vnf_interface") is not None:
sousaedu80135b92021-02-17 15:05:18 +0100526 vim_interfaces[task_create["mgmt_vnf_interface"]][
527 "mgmt_vnf_interface"
528 ] = True
529
530 mgmt_vdu_iface = task_create.get(
531 "mgmt_vdu_interface", task_create.get("mgmt_vnf_interface", 0)
532 )
tierno70eeb182020-10-19 16:38:00 +0000533 if vim_interfaces:
534 vim_interfaces[mgmt_vdu_iface]["mgmt_vdu_interface"] = True
tierno1d213f42020-04-24 14:02:51 +0000535
536 if ro_task["vim_info"]["interfaces"] != vim_interfaces:
537 ro_vim_item_update["interfaces"] = vim_interfaces
sousaedu80135b92021-02-17 15:05:18 +0100538
tierno1d213f42020-04-24 14:02:51 +0000539 if ro_task["vim_info"]["vim_status"] != vim_info["status"]:
540 ro_vim_item_update["vim_status"] = vim_info["status"]
sousaedu80135b92021-02-17 15:05:18 +0100541
tierno1d213f42020-04-24 14:02:51 +0000542 if ro_task["vim_info"]["vim_name"] != vim_info.get("name"):
543 ro_vim_item_update["vim_name"] = vim_info.get("name")
sousaedu80135b92021-02-17 15:05:18 +0100544
tierno1d213f42020-04-24 14:02:51 +0000545 if vim_info["status"] in ("ERROR", "VIM_ERROR"):
tierno70eeb182020-10-19 16:38:00 +0000546 if ro_task["vim_info"]["vim_details"] != vim_info.get("error_msg"):
547 ro_vim_item_update["vim_details"] = vim_info.get("error_msg")
tierno1d213f42020-04-24 14:02:51 +0000548 elif vim_info["status"] == "DELETED":
549 ro_vim_item_update["vim_id"] = None
550 ro_vim_item_update["vim_details"] = "Deleted externally"
551 else:
552 if ro_task["vim_info"]["vim_details"] != vim_info["vim_info"]:
553 ro_vim_item_update["vim_details"] = vim_info["vim_info"]
sousaedu80135b92021-02-17 15:05:18 +0100554
tierno1d213f42020-04-24 14:02:51 +0000555 if ro_vim_item_update:
sousaedu80135b92021-02-17 15:05:18 +0100556 self.logger.debug(
557 "ro_task={} {} get-vm={}: status={} {}".format(
558 ro_task_id,
559 ro_task["target_id"],
560 vim_id,
561 ro_vim_item_update.get("vim_status"),
562 ro_vim_item_update.get("vim_details")
563 if ro_vim_item_update.get("vim_status") != "ACTIVE"
564 else "",
565 )
566 )
567
tierno1d213f42020-04-24 14:02:51 +0000568 return task_status, ro_vim_item_update
569
tierno70eeb182020-10-19 16:38:00 +0000570 def exec(self, ro_task, task_index, task_depends):
tierno1d213f42020-04-24 14:02:51 +0000571 task = ro_task["tasks"][task_index]
572 task_id = task["task_id"]
573 target_vim = self.my_vims[ro_task["target_id"]]
tierno70eeb182020-10-19 16:38:00 +0000574 db_task_update = {"retries": 0}
575 retries = task.get("retries", 0)
sousaedu80135b92021-02-17 15:05:18 +0100576
tierno1d213f42020-04-24 14:02:51 +0000577 try:
578 params = task["params"]
579 params_copy = deepcopy(params)
sousaedu80135b92021-02-17 15:05:18 +0100580 params_copy["ro_key"] = self.db.decrypt(
581 params_copy.pop("private_key"),
582 params_copy.pop("schema_version"),
583 params_copy.pop("salt"),
584 )
tierno70eeb182020-10-19 16:38:00 +0000585 params_copy["ip_addr"] = params_copy.pop("ip_address")
tierno1d213f42020-04-24 14:02:51 +0000586 target_vim.inject_user_key(**params_copy)
587 self.logger.debug(
sousaedu80135b92021-02-17 15:05:18 +0100588 "task={} {} action-vm=inject_key".format(task_id, ro_task["target_id"])
589 )
590
591 return (
592 "DONE",
593 None,
594 db_task_update,
595 ) # params_copy["key"]
tierno1d213f42020-04-24 14:02:51 +0000596 except (vimconn.VimConnException, NsWorkerException) as e:
tierno70eeb182020-10-19 16:38:00 +0000597 retries += 1
sousaedu80135b92021-02-17 15:05:18 +0100598
tierno70eeb182020-10-19 16:38:00 +0000599 if retries < self.max_retries_inject_ssh_key:
sousaedu80135b92021-02-17 15:05:18 +0100600 return (
601 "BUILD",
602 None,
603 {
604 "retries": retries,
605 "next_retry": self.time_retries_inject_ssh_key,
606 },
607 )
608
609 self.logger.error(
610 "task={} {} inject-ssh-key: {}".format(task_id, ro_task["target_id"], e)
611 )
tierno1d213f42020-04-24 14:02:51 +0000612 ro_vim_item_update = {"vim_details": str(e)}
sousaedu80135b92021-02-17 15:05:18 +0100613
tierno70eeb182020-10-19 16:38:00 +0000614 return "FAILED", ro_vim_item_update, db_task_update
615
616
617class VimInteractionImage(VimInteractionBase):
tierno70eeb182020-10-19 16:38:00 +0000618 def new(self, ro_task, task_index, task_depends):
619 task = ro_task["tasks"][task_index]
620 task_id = task["task_id"]
621 created = False
622 created_items = {}
623 target_vim = self.my_vims[ro_task["target_id"]]
sousaedu80135b92021-02-17 15:05:18 +0100624
tierno70eeb182020-10-19 16:38:00 +0000625 try:
626 # FIND
627 if task.get("find_params"):
628 vim_images = target_vim.get_image_list(**task["find_params"])
sousaedu80135b92021-02-17 15:05:18 +0100629
tierno70eeb182020-10-19 16:38:00 +0000630 if not vim_images:
sousaedu80135b92021-02-17 15:05:18 +0100631 raise NsWorkerExceptionNotFound(
632 "Image not found with this criteria: '{}'".format(
633 task["find_params"]
634 )
635 )
tierno70eeb182020-10-19 16:38:00 +0000636 elif len(vim_images) > 1:
637 raise NsWorkerException(
sousaeduee6a6202021-05-11 13:22:37 +0200638 "More than one image found with this criteria: '{}'".format(
sousaedu80135b92021-02-17 15:05:18 +0100639 task["find_params"]
640 )
641 )
tierno70eeb182020-10-19 16:38:00 +0000642 else:
643 vim_image_id = vim_images[0]["id"]
644
sousaedu80135b92021-02-17 15:05:18 +0100645 ro_vim_item_update = {
646 "vim_id": vim_image_id,
647 "vim_status": "DONE",
648 "created": created,
649 "created_items": created_items,
650 "vim_details": None,
651 }
tierno70eeb182020-10-19 16:38:00 +0000652 self.logger.debug(
sousaedu80135b92021-02-17 15:05:18 +0100653 "task={} {} new-image={} created={}".format(
654 task_id, ro_task["target_id"], vim_image_id, created
655 )
656 )
657
tierno70eeb182020-10-19 16:38:00 +0000658 return "DONE", ro_vim_item_update
659 except (NsWorkerException, vimconn.VimConnException) as e:
sousaedu80135b92021-02-17 15:05:18 +0100660 self.logger.error(
661 "task={} {} new-image: {}".format(task_id, ro_task["target_id"], e)
662 )
663 ro_vim_item_update = {
664 "vim_status": "VIM_ERROR",
665 "created": created,
666 "vim_details": str(e),
667 }
668
tierno1d213f42020-04-24 14:02:51 +0000669 return "FAILED", ro_vim_item_update
670
tierno70eeb182020-10-19 16:38:00 +0000671
672class VimInteractionFlavor(VimInteractionBase):
tierno70eeb182020-10-19 16:38:00 +0000673 def delete(self, ro_task, task_index):
674 task = ro_task["tasks"][task_index]
675 task_id = task["task_id"]
676 flavor_vim_id = ro_task["vim_info"]["vim_id"]
sousaedu80135b92021-02-17 15:05:18 +0100677 ro_vim_item_update_ok = {
678 "vim_status": "DELETED",
679 "created": False,
680 "vim_details": "DELETED",
681 "vim_id": None,
682 }
683
tierno70eeb182020-10-19 16:38:00 +0000684 try:
685 if flavor_vim_id:
686 target_vim = self.my_vims[ro_task["target_id"]]
687 target_vim.delete_flavor(flavor_vim_id)
tierno70eeb182020-10-19 16:38:00 +0000688 except vimconn.VimConnNotFoundException:
689 ro_vim_item_update_ok["vim_details"] = "already deleted"
tierno70eeb182020-10-19 16:38:00 +0000690 except vimconn.VimConnException as e:
sousaedu80135b92021-02-17 15:05:18 +0100691 self.logger.error(
692 "ro_task={} vim={} del-flavor={}: {}".format(
693 ro_task["_id"], ro_task["target_id"], flavor_vim_id, e
694 )
695 )
696 ro_vim_item_update = {
697 "vim_status": "VIM_ERROR",
698 "vim_details": "Error while deleting: {}".format(e),
699 }
700
tierno70eeb182020-10-19 16:38:00 +0000701 return "FAILED", ro_vim_item_update
702
sousaedu80135b92021-02-17 15:05:18 +0100703 self.logger.debug(
704 "task={} {} del-flavor={} {}".format(
705 task_id,
706 ro_task["target_id"],
707 flavor_vim_id,
708 ro_vim_item_update_ok.get("vim_details", ""),
709 )
710 )
711
tierno70eeb182020-10-19 16:38:00 +0000712 return "DONE", ro_vim_item_update_ok
713
714 def new(self, ro_task, task_index, task_depends):
715 task = ro_task["tasks"][task_index]
716 task_id = task["task_id"]
717 created = False
718 created_items = {}
719 target_vim = self.my_vims[ro_task["target_id"]]
sousaedu80135b92021-02-17 15:05:18 +0100720
tierno70eeb182020-10-19 16:38:00 +0000721 try:
722 # FIND
723 vim_flavor_id = None
sousaedu80135b92021-02-17 15:05:18 +0100724
tierno70eeb182020-10-19 16:38:00 +0000725 if task.get("find_params"):
726 try:
727 flavor_data = task["find_params"]["flavor_data"]
728 vim_flavor_id = target_vim.get_flavor_id_from_data(flavor_data)
729 except vimconn.VimConnNotFoundException:
730 pass
731
732 if not vim_flavor_id and task.get("params"):
733 # CREATE
734 flavor_data = task["params"]["flavor_data"]
735 vim_flavor_id = target_vim.new_flavor(flavor_data)
736 created = True
737
sousaedu80135b92021-02-17 15:05:18 +0100738 ro_vim_item_update = {
739 "vim_id": vim_flavor_id,
740 "vim_status": "DONE",
741 "created": created,
742 "created_items": created_items,
743 "vim_details": None,
744 }
tierno70eeb182020-10-19 16:38:00 +0000745 self.logger.debug(
sousaedu80135b92021-02-17 15:05:18 +0100746 "task={} {} new-flavor={} created={}".format(
747 task_id, ro_task["target_id"], vim_flavor_id, created
748 )
749 )
750
tierno70eeb182020-10-19 16:38:00 +0000751 return "DONE", ro_vim_item_update
752 except (vimconn.VimConnException, NsWorkerException) as e:
sousaedu80135b92021-02-17 15:05:18 +0100753 self.logger.error(
754 "task={} vim={} new-flavor: {}".format(task_id, ro_task["target_id"], e)
755 )
756 ro_vim_item_update = {
757 "vim_status": "VIM_ERROR",
758 "created": created,
759 "vim_details": str(e),
760 }
761
tierno70eeb182020-10-19 16:38:00 +0000762 return "FAILED", ro_vim_item_update
763
764
Alexis Romero73993212022-04-13 18:03:30 +0200765class VimInteractionAffinityGroup(VimInteractionBase):
766 def delete(self, ro_task, task_index):
767 task = ro_task["tasks"][task_index]
768 task_id = task["task_id"]
769 affinity_group_vim_id = ro_task["vim_info"]["vim_id"]
770 ro_vim_item_update_ok = {
771 "vim_status": "DELETED",
772 "created": False,
773 "vim_details": "DELETED",
774 "vim_id": None,
775 }
776
777 try:
778 if affinity_group_vim_id:
779 target_vim = self.my_vims[ro_task["target_id"]]
780 target_vim.delete_affinity_group(affinity_group_vim_id)
781 except vimconn.VimConnNotFoundException:
782 ro_vim_item_update_ok["vim_details"] = "already deleted"
783 except vimconn.VimConnException as e:
784 self.logger.error(
785 "ro_task={} vim={} del-affinity-or-anti-affinity-group={}: {}".format(
786 ro_task["_id"], ro_task["target_id"], affinity_group_vim_id, e
787 )
788 )
789 ro_vim_item_update = {
790 "vim_status": "VIM_ERROR",
791 "vim_details": "Error while deleting: {}".format(e),
792 }
793
794 return "FAILED", ro_vim_item_update
795
796 self.logger.debug(
797 "task={} {} del-affinity-or-anti-affinity-group={} {}".format(
798 task_id,
799 ro_task["target_id"],
800 affinity_group_vim_id,
801 ro_vim_item_update_ok.get("vim_details", ""),
802 )
803 )
804
805 return "DONE", ro_vim_item_update_ok
806
807 def new(self, ro_task, task_index, task_depends):
808 task = ro_task["tasks"][task_index]
809 task_id = task["task_id"]
810 created = False
811 created_items = {}
812 target_vim = self.my_vims[ro_task["target_id"]]
813
814 try:
815 affinity_group_vim_id = None
816
817 if task.get("params"):
818 affinity_group_data = task["params"]["affinity_group_data"]
819 affinity_group_vim_id = target_vim.new_affinity_group(
820 affinity_group_data
821 )
822 created = True
823
824 ro_vim_item_update = {
825 "vim_id": affinity_group_vim_id,
826 "vim_status": "DONE",
827 "created": created,
828 "created_items": created_items,
829 "vim_details": None,
830 }
831 self.logger.debug(
832 "task={} {} new-affinity-or-anti-affinity-group={} created={}".format(
833 task_id, ro_task["target_id"], affinity_group_vim_id, created
834 )
835 )
836
837 return "DONE", ro_vim_item_update
838 except (vimconn.VimConnException, NsWorkerException) as e:
839 self.logger.error(
840 "task={} vim={} new-affinity-or-anti-affinity-group:"
841 " {}".format(task_id, ro_task["target_id"], e)
842 )
843 ro_vim_item_update = {
844 "vim_status": "VIM_ERROR",
845 "created": created,
846 "vim_details": str(e),
847 }
848
849 return "FAILED", ro_vim_item_update
850
851
tierno70eeb182020-10-19 16:38:00 +0000852class VimInteractionSdnNet(VimInteractionBase):
tierno70eeb182020-10-19 16:38:00 +0000853 @staticmethod
854 def _match_pci(port_pci, mapping):
855 """
856 Check if port_pci matches with mapping
857 mapping can have brackets to indicate that several chars are accepted. e.g
858 pci '0000:af:10.1' matches with '0000:af:1[01].[1357]'
859 :param port_pci: text
860 :param mapping: text, can contain brackets to indicate several chars are available
861 :return: True if matches, False otherwise
862 """
863 if not port_pci or not mapping:
864 return False
865 if port_pci == mapping:
866 return True
867
868 mapping_index = 0
869 pci_index = 0
870 while True:
871 bracket_start = mapping.find("[", mapping_index)
sousaedu80135b92021-02-17 15:05:18 +0100872
tierno70eeb182020-10-19 16:38:00 +0000873 if bracket_start == -1:
874 break
sousaedu80135b92021-02-17 15:05:18 +0100875
tierno70eeb182020-10-19 16:38:00 +0000876 bracket_end = mapping.find("]", bracket_start)
877 if bracket_end == -1:
878 break
sousaedu80135b92021-02-17 15:05:18 +0100879
tierno70eeb182020-10-19 16:38:00 +0000880 length = bracket_start - mapping_index
sousaedu80135b92021-02-17 15:05:18 +0100881 if (
882 length
883 and port_pci[pci_index : pci_index + length]
884 != mapping[mapping_index:bracket_start]
885 ):
tierno70eeb182020-10-19 16:38:00 +0000886 return False
sousaedu80135b92021-02-17 15:05:18 +0100887
888 if (
889 port_pci[pci_index + length]
890 not in mapping[bracket_start + 1 : bracket_end]
891 ):
tierno70eeb182020-10-19 16:38:00 +0000892 return False
sousaedu80135b92021-02-17 15:05:18 +0100893
tierno70eeb182020-10-19 16:38:00 +0000894 pci_index += length + 1
895 mapping_index = bracket_end + 1
896
897 if port_pci[pci_index:] != mapping[mapping_index:]:
898 return False
sousaedu80135b92021-02-17 15:05:18 +0100899
tierno70eeb182020-10-19 16:38:00 +0000900 return True
901
902 def _get_interfaces(self, vlds_to_connect, vim_account_id):
903 """
904 :param vlds_to_connect: list with format vnfrs:<id>:vld.<vld_id> or nsrs:<id>:vld.<vld_id>
905 :param vim_account_id:
906 :return:
907 """
908 interfaces = []
sousaedu80135b92021-02-17 15:05:18 +0100909
tierno70eeb182020-10-19 16:38:00 +0000910 for vld in vlds_to_connect:
911 table, _, db_id = vld.partition(":")
912 db_id, _, vld = db_id.partition(":")
913 _, _, vld_id = vld.partition(".")
sousaedu80135b92021-02-17 15:05:18 +0100914
tierno70eeb182020-10-19 16:38:00 +0000915 if table == "vnfrs":
916 q_filter = {"vim-account-id": vim_account_id, "_id": db_id}
917 iface_key = "vnf-vld-id"
918 else: # table == "nsrs"
919 q_filter = {"vim-account-id": vim_account_id, "nsr-id-ref": db_id}
920 iface_key = "ns-vld-id"
sousaedu80135b92021-02-17 15:05:18 +0100921
tierno70eeb182020-10-19 16:38:00 +0000922 db_vnfrs = self.db.get_list("vnfrs", q_filter=q_filter)
sousaedu80135b92021-02-17 15:05:18 +0100923
tierno70eeb182020-10-19 16:38:00 +0000924 for db_vnfr in db_vnfrs:
925 for vdu_index, vdur in enumerate(db_vnfr.get("vdur", ())):
926 for iface_index, interface in enumerate(vdur["interfaces"]):
sousaedu80135b92021-02-17 15:05:18 +0100927 if interface.get(iface_key) == vld_id and interface.get(
928 "type"
929 ) in ("SR-IOV", "PCI-PASSTHROUGH"):
tierno70eeb182020-10-19 16:38:00 +0000930 # only SR-IOV o PT
931 interface_ = interface.copy()
sousaedu80135b92021-02-17 15:05:18 +0100932 interface_["id"] = "vnfrs:{}:vdu.{}.interfaces.{}".format(
933 db_vnfr["_id"], vdu_index, iface_index
934 )
935
tierno70eeb182020-10-19 16:38:00 +0000936 if vdur.get("status") == "ERROR":
937 interface_["status"] = "ERROR"
sousaedu80135b92021-02-17 15:05:18 +0100938
tierno70eeb182020-10-19 16:38:00 +0000939 interfaces.append(interface_)
sousaedu80135b92021-02-17 15:05:18 +0100940
tierno70eeb182020-10-19 16:38:00 +0000941 return interfaces
942
943 def refresh(self, ro_task):
944 # look for task create
sousaedu80135b92021-02-17 15:05:18 +0100945 task_create_index, _ = next(
946 i_t
947 for i_t in enumerate(ro_task["tasks"])
948 if i_t[1]
949 and i_t[1]["action"] == "CREATE"
950 and i_t[1]["status"] != "FINISHED"
951 )
tierno70eeb182020-10-19 16:38:00 +0000952
953 return self.new(ro_task, task_create_index, None)
954
955 def new(self, ro_task, task_index, task_depends):
956
957 task = ro_task["tasks"][task_index]
958 task_id = task["task_id"]
959 target_vim = self.my_vims[ro_task["target_id"]]
960
961 sdn_net_id = ro_task["vim_info"]["vim_id"]
962
963 created_items = ro_task["vim_info"].get("created_items")
964 connected_ports = ro_task["vim_info"].get("connected_ports", [])
965 new_connected_ports = []
966 last_update = ro_task["vim_info"].get("last_update", 0)
967 sdn_status = ro_task["vim_info"].get("vim_status", "BUILD") or "BUILD"
968 error_list = []
969 created = ro_task["vim_info"].get("created", False)
970
971 try:
tierno70eeb182020-10-19 16:38:00 +0000972 # CREATE
973 params = task["params"]
974 vlds_to_connect = params["vlds"]
975 associated_vim = params["target_vim"]
sousaedu80135b92021-02-17 15:05:18 +0100976 # external additional ports
977 additional_ports = params.get("sdn-ports") or ()
tierno70eeb182020-10-19 16:38:00 +0000978 _, _, vim_account_id = associated_vim.partition(":")
sousaedu80135b92021-02-17 15:05:18 +0100979
tierno70eeb182020-10-19 16:38:00 +0000980 if associated_vim:
981 # get associated VIM
982 if associated_vim not in self.db_vims:
sousaedu80135b92021-02-17 15:05:18 +0100983 self.db_vims[associated_vim] = self.db.get_one(
984 "vim_accounts", {"_id": vim_account_id}
985 )
986
tierno70eeb182020-10-19 16:38:00 +0000987 db_vim = self.db_vims[associated_vim]
988
989 # look for ports to connect
990 ports = self._get_interfaces(vlds_to_connect, vim_account_id)
991 # print(ports)
992
993 sdn_ports = []
994 pending_ports = error_ports = 0
995 vlan_used = None
996 sdn_need_update = False
sousaedu80135b92021-02-17 15:05:18 +0100997
tierno70eeb182020-10-19 16:38:00 +0000998 for port in ports:
999 vlan_used = port.get("vlan") or vlan_used
sousaedu80135b92021-02-17 15:05:18 +01001000
tierno70eeb182020-10-19 16:38:00 +00001001 # TODO. Do not connect if already done
1002 if not port.get("compute_node") or not port.get("pci"):
1003 if port.get("status") == "ERROR":
1004 error_ports += 1
1005 else:
1006 pending_ports += 1
1007 continue
sousaedu80135b92021-02-17 15:05:18 +01001008
tierno70eeb182020-10-19 16:38:00 +00001009 pmap = None
sousaedu80135b92021-02-17 15:05:18 +01001010 compute_node_mappings = next(
1011 (
1012 c
1013 for c in db_vim["config"].get("sdn-port-mapping", ())
1014 if c and c["compute_node"] == port["compute_node"]
1015 ),
1016 None,
1017 )
1018
tierno70eeb182020-10-19 16:38:00 +00001019 if compute_node_mappings:
1020 # process port_mapping pci of type 0000:af:1[01].[1357]
sousaedu80135b92021-02-17 15:05:18 +01001021 pmap = next(
1022 (
1023 p
1024 for p in compute_node_mappings["ports"]
1025 if self._match_pci(port["pci"], p.get("pci"))
1026 ),
1027 None,
1028 )
1029
tierno70eeb182020-10-19 16:38:00 +00001030 if not pmap:
1031 if not db_vim["config"].get("mapping_not_needed"):
sousaedu80135b92021-02-17 15:05:18 +01001032 error_list.append(
1033 "Port mapping not found for compute_node={} pci={}".format(
1034 port["compute_node"], port["pci"]
1035 )
1036 )
tierno70eeb182020-10-19 16:38:00 +00001037 continue
sousaedu80135b92021-02-17 15:05:18 +01001038
tierno70eeb182020-10-19 16:38:00 +00001039 pmap = {}
1040
1041 service_endpoint_id = "{}:{}".format(port["compute_node"], port["pci"])
1042 new_port = {
sousaedu80135b92021-02-17 15:05:18 +01001043 "service_endpoint_id": pmap.get("service_endpoint_id")
1044 or service_endpoint_id,
1045 "service_endpoint_encapsulation_type": "dot1q"
1046 if port["type"] == "SR-IOV"
1047 else None,
tierno70eeb182020-10-19 16:38:00 +00001048 "service_endpoint_encapsulation_info": {
1049 "vlan": port.get("vlan"),
lloretgalleg160fcad2021-02-19 10:57:50 +00001050 "mac": port.get("mac-address"),
sousaedu80135b92021-02-17 15:05:18 +01001051 "device_id": pmap.get("device_id") or port["compute_node"],
1052 "device_interface_id": pmap.get("device_interface_id")
1053 or port["pci"],
tierno70eeb182020-10-19 16:38:00 +00001054 "switch_dpid": pmap.get("switch_id") or pmap.get("switch_dpid"),
1055 "switch_port": pmap.get("switch_port"),
1056 "service_mapping_info": pmap.get("service_mapping_info"),
sousaedu80135b92021-02-17 15:05:18 +01001057 },
tierno70eeb182020-10-19 16:38:00 +00001058 }
1059
1060 # TODO
1061 # if port["modified_at"] > last_update:
1062 # sdn_need_update = True
1063 new_connected_ports.append(port["id"]) # TODO
1064 sdn_ports.append(new_port)
1065
1066 if error_ports:
sousaedu80135b92021-02-17 15:05:18 +01001067 error_list.append(
1068 "{} interfaces have not been created as VDU is on ERROR status".format(
1069 error_ports
1070 )
1071 )
tierno70eeb182020-10-19 16:38:00 +00001072
1073 # connect external ports
1074 for index, additional_port in enumerate(additional_ports):
sousaedu80135b92021-02-17 15:05:18 +01001075 additional_port_id = additional_port.get(
1076 "service_endpoint_id"
1077 ) or "external-{}".format(index)
1078 sdn_ports.append(
1079 {
1080 "service_endpoint_id": additional_port_id,
1081 "service_endpoint_encapsulation_type": additional_port.get(
1082 "service_endpoint_encapsulation_type", "dot1q"
1083 ),
1084 "service_endpoint_encapsulation_info": {
1085 "vlan": additional_port.get("vlan") or vlan_used,
1086 "mac": additional_port.get("mac_address"),
1087 "device_id": additional_port.get("device_id"),
1088 "device_interface_id": additional_port.get(
1089 "device_interface_id"
1090 ),
1091 "switch_dpid": additional_port.get("switch_dpid")
1092 or additional_port.get("switch_id"),
1093 "switch_port": additional_port.get("switch_port"),
1094 "service_mapping_info": additional_port.get(
1095 "service_mapping_info"
1096 ),
1097 },
1098 }
1099 )
tierno70eeb182020-10-19 16:38:00 +00001100 new_connected_ports.append(additional_port_id)
1101 sdn_info = ""
sousaedu80135b92021-02-17 15:05:18 +01001102
tierno70eeb182020-10-19 16:38:00 +00001103 # if there are more ports to connect or they have been modified, call create/update
1104 if error_list:
1105 sdn_status = "ERROR"
1106 sdn_info = "; ".join(error_list)
1107 elif set(connected_ports) != set(new_connected_ports) or sdn_need_update:
1108 last_update = time.time()
sousaedu80135b92021-02-17 15:05:18 +01001109
tierno70eeb182020-10-19 16:38:00 +00001110 if not sdn_net_id:
1111 if len(sdn_ports) < 2:
1112 sdn_status = "ACTIVE"
sousaedu80135b92021-02-17 15:05:18 +01001113
tierno70eeb182020-10-19 16:38:00 +00001114 if not pending_ports:
sousaedu80135b92021-02-17 15:05:18 +01001115 self.logger.debug(
1116 "task={} {} new-sdn-net done, less than 2 ports".format(
1117 task_id, ro_task["target_id"]
1118 )
1119 )
tierno70eeb182020-10-19 16:38:00 +00001120 else:
1121 net_type = params.get("type") or "ELAN"
sousaedu80135b92021-02-17 15:05:18 +01001122 (
1123 sdn_net_id,
1124 created_items,
1125 ) = target_vim.create_connectivity_service(net_type, sdn_ports)
tierno70eeb182020-10-19 16:38:00 +00001126 created = True
sousaedu80135b92021-02-17 15:05:18 +01001127 self.logger.debug(
1128 "task={} {} new-sdn-net={} created={}".format(
1129 task_id, ro_task["target_id"], sdn_net_id, created
1130 )
1131 )
tierno70eeb182020-10-19 16:38:00 +00001132 else:
1133 created_items = target_vim.edit_connectivity_service(
sousaedu80135b92021-02-17 15:05:18 +01001134 sdn_net_id, conn_info=created_items, connection_points=sdn_ports
1135 )
tierno70eeb182020-10-19 16:38:00 +00001136 created = True
sousaedu80135b92021-02-17 15:05:18 +01001137 self.logger.debug(
1138 "task={} {} update-sdn-net={} created={}".format(
1139 task_id, ro_task["target_id"], sdn_net_id, created
1140 )
1141 )
1142
tierno70eeb182020-10-19 16:38:00 +00001143 connected_ports = new_connected_ports
1144 elif sdn_net_id:
sousaedu80135b92021-02-17 15:05:18 +01001145 wim_status_dict = target_vim.get_connectivity_service_status(
1146 sdn_net_id, conn_info=created_items
1147 )
tierno70eeb182020-10-19 16:38:00 +00001148 sdn_status = wim_status_dict["sdn_status"]
sousaedu80135b92021-02-17 15:05:18 +01001149
tierno70eeb182020-10-19 16:38:00 +00001150 if wim_status_dict.get("sdn_info"):
1151 sdn_info = str(wim_status_dict.get("sdn_info")) or ""
sousaedu80135b92021-02-17 15:05:18 +01001152
tierno70eeb182020-10-19 16:38:00 +00001153 if wim_status_dict.get("error_msg"):
1154 sdn_info = wim_status_dict.get("error_msg") or ""
1155
1156 if pending_ports:
1157 if sdn_status != "ERROR":
1158 sdn_info = "Waiting for getting interfaces location from VIM. Obtained '{}' of {}".format(
sousaedu80135b92021-02-17 15:05:18 +01001159 len(ports) - pending_ports, len(ports)
1160 )
1161
tierno70eeb182020-10-19 16:38:00 +00001162 if sdn_status == "ACTIVE":
1163 sdn_status = "BUILD"
1164
sousaedu80135b92021-02-17 15:05:18 +01001165 ro_vim_item_update = {
1166 "vim_id": sdn_net_id,
1167 "vim_status": sdn_status,
1168 "created": created,
1169 "created_items": created_items,
1170 "connected_ports": connected_ports,
1171 "vim_details": sdn_info,
1172 "last_update": last_update,
1173 }
1174
tierno70eeb182020-10-19 16:38:00 +00001175 return sdn_status, ro_vim_item_update
1176 except Exception as e:
sousaedu80135b92021-02-17 15:05:18 +01001177 self.logger.error(
1178 "task={} vim={} new-net: {}".format(task_id, ro_task["target_id"], e),
1179 exc_info=not isinstance(
1180 e, (sdnconn.SdnConnectorError, vimconn.VimConnException)
1181 ),
1182 )
1183 ro_vim_item_update = {
1184 "vim_status": "VIM_ERROR",
1185 "created": created,
1186 "vim_details": str(e),
1187 }
1188
tierno70eeb182020-10-19 16:38:00 +00001189 return "FAILED", ro_vim_item_update
1190
1191 def delete(self, ro_task, task_index):
1192 task = ro_task["tasks"][task_index]
1193 task_id = task["task_id"]
1194 sdn_vim_id = ro_task["vim_info"].get("vim_id")
sousaedu80135b92021-02-17 15:05:18 +01001195 ro_vim_item_update_ok = {
1196 "vim_status": "DELETED",
1197 "created": False,
1198 "vim_details": "DELETED",
1199 "vim_id": None,
1200 }
1201
tierno70eeb182020-10-19 16:38:00 +00001202 try:
1203 if sdn_vim_id:
1204 target_vim = self.my_vims[ro_task["target_id"]]
sousaedu80135b92021-02-17 15:05:18 +01001205 target_vim.delete_connectivity_service(
1206 sdn_vim_id, ro_task["vim_info"].get("created_items")
1207 )
tierno70eeb182020-10-19 16:38:00 +00001208
1209 except Exception as e:
sousaedu80135b92021-02-17 15:05:18 +01001210 if (
1211 isinstance(e, sdnconn.SdnConnectorError)
1212 and e.http_code == HTTPStatus.NOT_FOUND.value
1213 ):
tierno70eeb182020-10-19 16:38:00 +00001214 ro_vim_item_update_ok["vim_details"] = "already deleted"
1215 else:
sousaedu80135b92021-02-17 15:05:18 +01001216 self.logger.error(
1217 "ro_task={} vim={} del-sdn-net={}: {}".format(
1218 ro_task["_id"], ro_task["target_id"], sdn_vim_id, e
1219 ),
1220 exc_info=not isinstance(
1221 e, (sdnconn.SdnConnectorError, vimconn.VimConnException)
1222 ),
1223 )
1224 ro_vim_item_update = {
1225 "vim_status": "VIM_ERROR",
1226 "vim_details": "Error while deleting: {}".format(e),
1227 }
1228
tierno70eeb182020-10-19 16:38:00 +00001229 return "FAILED", ro_vim_item_update
1230
sousaedu80135b92021-02-17 15:05:18 +01001231 self.logger.debug(
1232 "task={} {} del-sdn-net={} {}".format(
1233 task_id,
1234 ro_task["target_id"],
1235 sdn_vim_id,
1236 ro_vim_item_update_ok.get("vim_details", ""),
1237 )
1238 )
1239
tierno70eeb182020-10-19 16:38:00 +00001240 return "DONE", ro_vim_item_update_ok
1241
1242
1243class NsWorker(threading.Thread):
1244 REFRESH_BUILD = 5 # 5 seconds
1245 REFRESH_ACTIVE = 60 # 1 minute
1246 REFRESH_ERROR = 600
1247 REFRESH_IMAGE = 3600 * 10
1248 REFRESH_DELETE = 3600 * 10
tiernof1b640f2020-12-09 15:06:01 +00001249 QUEUE_SIZE = 100
tierno70eeb182020-10-19 16:38:00 +00001250 terminate = False
tierno70eeb182020-10-19 16:38:00 +00001251
1252 def __init__(self, worker_index, config, plugins, db):
1253 """
1254
1255 :param worker_index: thread index
1256 :param config: general configuration of RO, among others the process_id with the docker id where it runs
1257 :param plugins: global shared dict with the loaded plugins
1258 :param db: database class instance to use
1259 """
1260 threading.Thread.__init__(self)
1261 self.config = config
1262 self.plugins = plugins
1263 self.plugin_name = "unknown"
sousaedu80135b92021-02-17 15:05:18 +01001264 self.logger = logging.getLogger("ro.worker{}".format(worker_index))
tierno70eeb182020-10-19 16:38:00 +00001265 self.worker_index = worker_index
1266 self.task_queue = queue.Queue(self.QUEUE_SIZE)
sousaedu80135b92021-02-17 15:05:18 +01001267 # targetvim: vimplugin class
1268 self.my_vims = {}
1269 # targetvim: vim information from database
1270 self.db_vims = {}
1271 # targetvim list
1272 self.vim_targets = []
tierno70eeb182020-10-19 16:38:00 +00001273 self.my_id = config["process_id"] + ":" + str(worker_index)
1274 self.db = db
1275 self.item2class = {
1276 "net": VimInteractionNet(self.db, self.my_vims, self.db_vims, self.logger),
1277 "vdu": VimInteractionVdu(self.db, self.my_vims, self.db_vims, self.logger),
sousaedu80135b92021-02-17 15:05:18 +01001278 "image": VimInteractionImage(
1279 self.db, self.my_vims, self.db_vims, self.logger
1280 ),
1281 "flavor": VimInteractionFlavor(
1282 self.db, self.my_vims, self.db_vims, self.logger
1283 ),
1284 "sdn_net": VimInteractionSdnNet(
1285 self.db, self.my_vims, self.db_vims, self.logger
1286 ),
Alexis Romero73993212022-04-13 18:03:30 +02001287 "affinity-or-anti-affinity-group": VimInteractionAffinityGroup(
1288 self.db, self.my_vims, self.db_vims, self.logger
1289 ),
tierno70eeb182020-10-19 16:38:00 +00001290 }
1291 self.time_last_task_processed = None
sousaedu80135b92021-02-17 15:05:18 +01001292 # lists of tasks to delete because nsrs or vnfrs has been deleted from db
1293 self.tasks_to_delete = []
1294 # it is idle when there are not vim_targets associated
1295 self.idle = True
tiernof1b640f2020-12-09 15:06:01 +00001296 self.task_locked_time = config["global"]["task_locked_time"]
tierno70eeb182020-10-19 16:38:00 +00001297
1298 def insert_task(self, task):
1299 try:
1300 self.task_queue.put(task, False)
1301 return None
1302 except queue.Full:
1303 raise NsWorkerException("timeout inserting a task")
1304
1305 def terminate(self):
1306 self.insert_task("exit")
1307
1308 def del_task(self, task):
1309 with self.task_lock:
1310 if task["status"] == "SCHEDULED":
1311 task["status"] = "SUPERSEDED"
1312 return True
1313 else: # task["status"] == "processing"
1314 self.task_lock.release()
1315 return False
1316
1317 def _process_vim_config(self, target_id, db_vim):
1318 """
1319 Process vim config, creating vim configuration files as ca_cert
1320 :param target_id: vim/sdn/wim + id
1321 :param db_vim: Vim dictionary obtained from database
1322 :return: None. Modifies vim. Creates a folder target_id:worker_index and several files
1323 """
1324 if not db_vim.get("config"):
1325 return
sousaedu80135b92021-02-17 15:05:18 +01001326
tierno70eeb182020-10-19 16:38:00 +00001327 file_name = ""
sousaedu80135b92021-02-17 15:05:18 +01001328
tierno70eeb182020-10-19 16:38:00 +00001329 try:
1330 if db_vim["config"].get("ca_cert_content"):
1331 file_name = "{}:{}".format(target_id, self.worker_index)
sousaedu80135b92021-02-17 15:05:18 +01001332
tierno70eeb182020-10-19 16:38:00 +00001333 try:
1334 mkdir(file_name)
1335 except FileExistsError:
1336 pass
sousaedu80135b92021-02-17 15:05:18 +01001337
tierno70eeb182020-10-19 16:38:00 +00001338 file_name = file_name + "/ca_cert"
sousaedu80135b92021-02-17 15:05:18 +01001339
tierno70eeb182020-10-19 16:38:00 +00001340 with open(file_name, "w") as f:
1341 f.write(db_vim["config"]["ca_cert_content"])
1342 del db_vim["config"]["ca_cert_content"]
1343 db_vim["config"]["ca_cert"] = file_name
1344 except Exception as e:
sousaedu80135b92021-02-17 15:05:18 +01001345 raise NsWorkerException(
1346 "Error writing to file '{}': {}".format(file_name, e)
1347 )
tierno70eeb182020-10-19 16:38:00 +00001348
1349 def _load_plugin(self, name, type="vim"):
1350 # type can be vim or sdn
1351 if "rovim_dummy" not in self.plugins:
1352 self.plugins["rovim_dummy"] = VimDummyConnector
sousaedu80135b92021-02-17 15:05:18 +01001353
tierno70eeb182020-10-19 16:38:00 +00001354 if "rosdn_dummy" not in self.plugins:
1355 self.plugins["rosdn_dummy"] = SdnDummyConnector
sousaedu80135b92021-02-17 15:05:18 +01001356
tierno70eeb182020-10-19 16:38:00 +00001357 if name in self.plugins:
1358 return self.plugins[name]
sousaedu80135b92021-02-17 15:05:18 +01001359
tierno70eeb182020-10-19 16:38:00 +00001360 try:
sousaedubecd0832021-04-08 00:16:18 +02001361 for ep in entry_points(group="osm_ro{}.plugins".format(type), name=name):
1362 self.plugins[name] = ep.load()
tierno70eeb182020-10-19 16:38:00 +00001363 except Exception as e:
1364 raise NsWorkerException("Cannot load plugin osm_{}: {}".format(name, e))
sousaedu80135b92021-02-17 15:05:18 +01001365
tierno70eeb182020-10-19 16:38:00 +00001366 if name and name not in self.plugins:
sousaedu80135b92021-02-17 15:05:18 +01001367 raise NsWorkerException(
1368 "Plugin 'osm_{n}' has not been installed".format(n=name)
1369 )
1370
tierno70eeb182020-10-19 16:38:00 +00001371 return self.plugins[name]
1372
1373 def _unload_vim(self, target_id):
1374 """
1375 Unload a vim_account. Removes it from self db_vims dictionary, my_vims dictionary and vim_targets list
1376 :param target_id: Contains type:_id; where type can be 'vim', ...
1377 :return: None.
1378 """
1379 try:
tierno70eeb182020-10-19 16:38:00 +00001380 self.db_vims.pop(target_id, None)
1381 self.my_vims.pop(target_id, None)
sousaedu80135b92021-02-17 15:05:18 +01001382
tierno86153522020-12-06 18:27:16 +00001383 if target_id in self.vim_targets:
1384 self.vim_targets.remove(target_id)
sousaedu80135b92021-02-17 15:05:18 +01001385
tierno86153522020-12-06 18:27:16 +00001386 self.logger.info("Unloaded {}".format(target_id))
tierno70eeb182020-10-19 16:38:00 +00001387 rmtree("{}:{}".format(target_id, self.worker_index))
1388 except FileNotFoundError:
1389 pass # this is raised by rmtree if folder does not exist
1390 except Exception as e:
1391 self.logger.error("Cannot unload {}: {}".format(target_id, e))
1392
1393 def _check_vim(self, target_id):
1394 """
1395 Load a VIM/SDN/WIM (if not loaded) and check connectivity, updating database with ENABLE or ERROR
1396 :param target_id: Contains type:_id; type can be 'vim', 'sdn' or 'wim'
1397 :return: None.
1398 """
1399 target, _, _id = target_id.partition(":")
1400 now = time.time()
1401 update_dict = {}
1402 unset_dict = {}
1403 op_text = ""
1404 step = ""
tierno86153522020-12-06 18:27:16 +00001405 loaded = target_id in self.vim_targets
sousaedu80135b92021-02-17 15:05:18 +01001406 target_database = (
1407 "vim_accounts"
1408 if target == "vim"
1409 else "wim_accounts"
1410 if target == "wim"
1411 else "sdns"
1412 )
1413
tierno70eeb182020-10-19 16:38:00 +00001414 try:
1415 step = "Getting {} from db".format(target_id)
1416 db_vim = self.db.get_one(target_database, {"_id": _id})
sousaedu80135b92021-02-17 15:05:18 +01001417
1418 for op_index, operation in enumerate(
1419 db_vim["_admin"].get("operations", ())
1420 ):
tierno70eeb182020-10-19 16:38:00 +00001421 if operation["operationState"] != "PROCESSING":
1422 continue
sousaedu80135b92021-02-17 15:05:18 +01001423
tierno70eeb182020-10-19 16:38:00 +00001424 locked_at = operation.get("locked_at")
sousaedu80135b92021-02-17 15:05:18 +01001425
tiernof1b640f2020-12-09 15:06:01 +00001426 if locked_at is not None and locked_at >= now - self.task_locked_time:
tierno70eeb182020-10-19 16:38:00 +00001427 # some other thread is doing this operation
1428 return
sousaedu80135b92021-02-17 15:05:18 +01001429
tierno70eeb182020-10-19 16:38:00 +00001430 # lock
1431 op_text = "_admin.operations.{}.".format(op_index)
sousaedu80135b92021-02-17 15:05:18 +01001432
1433 if not self.db.set_one(
1434 target_database,
1435 q_filter={
1436 "_id": _id,
1437 op_text + "operationState": "PROCESSING",
1438 op_text + "locked_at": locked_at,
1439 },
1440 update_dict={
1441 op_text + "locked_at": now,
1442 "admin.current_operation": op_index,
1443 },
1444 fail_on_empty=False,
1445 ):
tierno70eeb182020-10-19 16:38:00 +00001446 return
sousaedu80135b92021-02-17 15:05:18 +01001447
tierno70eeb182020-10-19 16:38:00 +00001448 unset_dict[op_text + "locked_at"] = None
1449 unset_dict["current_operation"] = None
1450 step = "Loading " + target_id
1451 error_text = self._load_vim(target_id)
sousaedu80135b92021-02-17 15:05:18 +01001452
tierno70eeb182020-10-19 16:38:00 +00001453 if not error_text:
1454 step = "Checking connectivity"
sousaedu80135b92021-02-17 15:05:18 +01001455
1456 if target == "vim":
tierno70eeb182020-10-19 16:38:00 +00001457 self.my_vims[target_id].check_vim_connectivity()
1458 else:
1459 self.my_vims[target_id].check_credentials()
sousaedu80135b92021-02-17 15:05:18 +01001460
tierno70eeb182020-10-19 16:38:00 +00001461 update_dict["_admin.operationalState"] = "ENABLED"
1462 update_dict["_admin.detailed-status"] = ""
1463 unset_dict[op_text + "detailed-status"] = None
1464 update_dict[op_text + "operationState"] = "COMPLETED"
sousaedu80135b92021-02-17 15:05:18 +01001465
tierno70eeb182020-10-19 16:38:00 +00001466 return
1467
1468 except Exception as e:
1469 error_text = "{}: {}".format(step, e)
1470 self.logger.error("{} for {}: {}".format(step, target_id, e))
1471
1472 finally:
1473 if update_dict or unset_dict:
1474 if error_text:
1475 update_dict[op_text + "operationState"] = "FAILED"
1476 update_dict[op_text + "detailed-status"] = error_text
1477 unset_dict.pop(op_text + "detailed-status", None)
1478 update_dict["_admin.operationalState"] = "ERROR"
1479 update_dict["_admin.detailed-status"] = error_text
sousaedu80135b92021-02-17 15:05:18 +01001480
tierno70eeb182020-10-19 16:38:00 +00001481 if op_text:
1482 update_dict[op_text + "statusEnteredTime"] = now
sousaedu80135b92021-02-17 15:05:18 +01001483
1484 self.db.set_one(
1485 target_database,
1486 q_filter={"_id": _id},
1487 update_dict=update_dict,
1488 unset=unset_dict,
1489 fail_on_empty=False,
1490 )
1491
tierno70eeb182020-10-19 16:38:00 +00001492 if not loaded:
1493 self._unload_vim(target_id)
1494
1495 def _reload_vim(self, target_id):
1496 if target_id in self.vim_targets:
1497 self._load_vim(target_id)
1498 else:
1499 # if the vim is not loaded, but database information of VIM is cached at self.db_vims,
1500 # just remove it to force load again next time it is needed
1501 self.db_vims.pop(target_id, None)
1502
1503 def _load_vim(self, target_id):
1504 """
1505 Load or reload a vim_account, sdn_controller or wim_account.
1506 Read content from database, load the plugin if not loaded.
1507 In case of error loading the plugin, it load a failing VIM_connector
1508 It fills self db_vims dictionary, my_vims dictionary and vim_targets list
1509 :param target_id: Contains type:_id; where type can be 'vim', ...
1510 :return: None if ok, descriptive text if error
1511 """
1512 target, _, _id = target_id.partition(":")
sousaedu80135b92021-02-17 15:05:18 +01001513 target_database = (
1514 "vim_accounts"
1515 if target == "vim"
1516 else "wim_accounts"
1517 if target == "wim"
1518 else "sdns"
1519 )
tierno70eeb182020-10-19 16:38:00 +00001520 plugin_name = ""
1521 vim = None
sousaedu80135b92021-02-17 15:05:18 +01001522
tierno70eeb182020-10-19 16:38:00 +00001523 try:
1524 step = "Getting {}={} from db".format(target, _id)
1525 # TODO process for wim, sdnc, ...
1526 vim = self.db.get_one(target_database, {"_id": _id})
1527
1528 # if deep_get(vim, "config", "sdn-controller"):
1529 # step = "Getting sdn-controller-id='{}' from db".format(vim["config"]["sdn-controller"])
1530 # db_sdn = self.db.get_one("sdns", {"_id": vim["config"]["sdn-controller"]})
1531
1532 step = "Decrypting password"
1533 schema_version = vim.get("schema_version")
sousaedu80135b92021-02-17 15:05:18 +01001534 self.db.encrypt_decrypt_fields(
1535 vim,
1536 "decrypt",
1537 fields=("password", "secret"),
1538 schema_version=schema_version,
1539 salt=_id,
1540 )
tierno70eeb182020-10-19 16:38:00 +00001541 self._process_vim_config(target_id, vim)
sousaedu80135b92021-02-17 15:05:18 +01001542
tierno70eeb182020-10-19 16:38:00 +00001543 if target == "vim":
1544 plugin_name = "rovim_" + vim["vim_type"]
1545 step = "Loading plugin '{}'".format(plugin_name)
1546 vim_module_conn = self._load_plugin(plugin_name)
1547 step = "Loading {}'".format(target_id)
1548 self.my_vims[target_id] = vim_module_conn(
sousaedu80135b92021-02-17 15:05:18 +01001549 uuid=vim["_id"],
1550 name=vim["name"],
1551 tenant_id=vim.get("vim_tenant_id"),
1552 tenant_name=vim.get("vim_tenant_name"),
1553 url=vim["vim_url"],
1554 url_admin=None,
1555 user=vim["vim_user"],
1556 passwd=vim["vim_password"],
1557 config=vim.get("config") or {},
1558 persistent_info={},
tierno70eeb182020-10-19 16:38:00 +00001559 )
1560 else: # sdn
1561 plugin_name = "rosdn_" + vim["type"]
1562 step = "Loading plugin '{}'".format(plugin_name)
1563 vim_module_conn = self._load_plugin(plugin_name, "sdn")
1564 step = "Loading {}'".format(target_id)
1565 wim = deepcopy(vim)
1566 wim_config = wim.pop("config", {}) or {}
1567 wim["uuid"] = wim["_id"]
1568 wim["wim_url"] = wim["url"]
sousaedu80135b92021-02-17 15:05:18 +01001569
tierno70eeb182020-10-19 16:38:00 +00001570 if wim.get("dpid"):
1571 wim_config["dpid"] = wim.pop("dpid")
sousaedu80135b92021-02-17 15:05:18 +01001572
tierno70eeb182020-10-19 16:38:00 +00001573 if wim.get("switch_id"):
1574 wim_config["switch_id"] = wim.pop("switch_id")
sousaedu80135b92021-02-17 15:05:18 +01001575
1576 # wim, wim_account, config
1577 self.my_vims[target_id] = vim_module_conn(wim, wim, wim_config)
tierno70eeb182020-10-19 16:38:00 +00001578 self.db_vims[target_id] = vim
1579 self.error_status = None
sousaedu80135b92021-02-17 15:05:18 +01001580
1581 self.logger.info(
1582 "Connector loaded for {}, plugin={}".format(target_id, plugin_name)
1583 )
tierno70eeb182020-10-19 16:38:00 +00001584 except Exception as e:
sousaedu80135b92021-02-17 15:05:18 +01001585 self.logger.error(
1586 "Cannot load {} plugin={}: {} {}".format(
1587 target_id, plugin_name, step, e
1588 )
1589 )
1590
tierno70eeb182020-10-19 16:38:00 +00001591 self.db_vims[target_id] = vim or {}
1592 self.db_vims[target_id] = FailingConnector(str(e))
1593 error_status = "{} Error: {}".format(step, e)
sousaedu80135b92021-02-17 15:05:18 +01001594
tierno70eeb182020-10-19 16:38:00 +00001595 return error_status
1596 finally:
1597 if target_id not in self.vim_targets:
1598 self.vim_targets.append(target_id)
1599
1600 def _get_db_task(self):
1601 """
1602 Read actions from database and reload them at memory. Fill self.refresh_list, pending_list, vim_actions
1603 :return: None
1604 """
1605 now = time.time()
sousaedu80135b92021-02-17 15:05:18 +01001606
tierno70eeb182020-10-19 16:38:00 +00001607 if not self.time_last_task_processed:
1608 self.time_last_task_processed = now
sousaedu80135b92021-02-17 15:05:18 +01001609
tierno70eeb182020-10-19 16:38:00 +00001610 try:
1611 while True:
gallardo88312d52022-01-31 16:50:48 +00001612 """
1613 # Log RO tasks only when loglevel is DEBUG
1614 if self.logger.getEffectiveLevel() == logging.DEBUG:
1615 self._log_ro_task(
1616 None,
1617 None,
1618 None,
1619 "TASK_WF",
1620 "task_locked_time="
1621 + str(self.task_locked_time)
1622 + " "
1623 + "time_last_task_processed="
1624 + str(self.time_last_task_processed)
1625 + " "
1626 + "now="
1627 + str(now),
1628 )
1629 """
tierno70eeb182020-10-19 16:38:00 +00001630 locked = self.db.set_one(
1631 "ro_tasks",
sousaedu80135b92021-02-17 15:05:18 +01001632 q_filter={
1633 "target_id": self.vim_targets,
1634 "tasks.status": ["SCHEDULED", "BUILD", "DONE", "FAILED"],
1635 "locked_at.lt": now - self.task_locked_time,
1636 "to_check_at.lt": self.time_last_task_processed,
1637 },
tierno70eeb182020-10-19 16:38:00 +00001638 update_dict={"locked_by": self.my_id, "locked_at": now},
sousaedu80135b92021-02-17 15:05:18 +01001639 fail_on_empty=False,
1640 )
1641
tierno70eeb182020-10-19 16:38:00 +00001642 if locked:
1643 # read and return
1644 ro_task = self.db.get_one(
1645 "ro_tasks",
sousaedu80135b92021-02-17 15:05:18 +01001646 q_filter={
1647 "target_id": self.vim_targets,
1648 "tasks.status": ["SCHEDULED", "BUILD", "DONE", "FAILED"],
1649 "locked_at": now,
1650 },
1651 )
tierno70eeb182020-10-19 16:38:00 +00001652 return ro_task
sousaedu80135b92021-02-17 15:05:18 +01001653
tierno70eeb182020-10-19 16:38:00 +00001654 if self.time_last_task_processed == now:
1655 self.time_last_task_processed = None
1656 return None
1657 else:
1658 self.time_last_task_processed = now
1659 # self.time_last_task_processed = min(self.time_last_task_processed + 1000, now)
1660
1661 except DbException as e:
1662 self.logger.error("Database exception at _get_db_task: {}".format(e))
1663 except Exception as e:
sousaedu80135b92021-02-17 15:05:18 +01001664 self.logger.critical(
1665 "Unexpected exception at _get_db_task: {}".format(e), exc_info=True
1666 )
1667
tierno70eeb182020-10-19 16:38:00 +00001668 return None
1669
gallardo88312d52022-01-31 16:50:48 +00001670 def _get_db_all_tasks(self):
1671 """
1672 Read all content of table ro_tasks to log it
1673 :return: None
1674 """
1675 try:
1676 # Checking the content of the BD:
1677
1678 # read and return
1679 ro_task = self.db.get_list("ro_tasks")
1680 for rt in ro_task:
1681 self._log_ro_task(rt, None, None, "TASK_WF", "GET_ALL_TASKS")
1682 return ro_task
1683
1684 except DbException as e:
1685 self.logger.error("Database exception at _get_db_all_tasks: {}".format(e))
1686 except Exception as e:
1687 self.logger.critical(
1688 "Unexpected exception at _get_db_all_tasks: {}".format(e), exc_info=True
1689 )
1690
1691 return None
1692
1693 def _log_ro_task(self, ro_task, db_ro_task_update, db_ro_task_delete, mark, event):
1694 """
1695 Generate a log with the following format:
1696
1697 Mark;Event;ro_task_id;locked_at;modified_at;created_at;to_check_at;locked_by;
1698 target_id;vim_info.refresh_at;vim_info;no_of_tasks;task_status;action_id;
1699 task_array_index;task_id;task_action;task_item;task_args
1700
1701 Example:
1702
1703 TASK_WF;GET_TASK;888f1864-749a-4fc2-bc1a-97c0fffd6a6f:2;1642158724.8210013;
1704 1642158640.7986135;1642158640.7986135;1642158640.7986135;b134c9494e75:0a
1705 ;vim:b7ff9e24-8868-4d68-8a57-a59dc11d0327;None;{'created': False,
1706 'created_items': None, 'vim_id': None, 'vim_name': None, 'vim_status': None,
1707 'vim_details': None, 'refresh_at': None};1;SCHEDULED;
1708 888f1864-749a-4fc2-bc1a-97c0fffd6a6f;0;888f1864-749a-4fc2-bc1a-97c0fffd6a6f:2;
1709 CREATE;image;{'filter_dict': {'name': 'ubuntu-os-cloud:image-family:ubuntu-1804-lts'}}
1710 """
1711 try:
1712 line = []
1713 i = 0
1714 if ro_task is not None and isinstance(ro_task, dict):
1715 for t in ro_task["tasks"]:
1716 line.clear()
1717 line.append(mark)
1718 line.append(event)
1719 line.append(ro_task.get("_id", ""))
1720 line.append(str(ro_task.get("locked_at", "")))
1721 line.append(str(ro_task.get("modified_at", "")))
1722 line.append(str(ro_task.get("created_at", "")))
1723 line.append(str(ro_task.get("to_check_at", "")))
1724 line.append(str(ro_task.get("locked_by", "")))
1725 line.append(str(ro_task.get("target_id", "")))
1726 line.append(str(ro_task.get("vim_info", {}).get("refresh_at", "")))
1727 line.append(str(ro_task.get("vim_info", "")))
1728 line.append(str(ro_task.get("tasks", "")))
1729 if isinstance(t, dict):
1730 line.append(str(t.get("status", "")))
1731 line.append(str(t.get("action_id", "")))
1732 line.append(str(i))
1733 line.append(str(t.get("task_id", "")))
1734 line.append(str(t.get("action", "")))
1735 line.append(str(t.get("item", "")))
1736 line.append(str(t.get("find_params", "")))
1737 line.append(str(t.get("params", "")))
1738 else:
1739 line.extend([""] * 2)
1740 line.append(str(i))
1741 line.extend([""] * 5)
1742
1743 i += 1
1744 self.logger.debug(";".join(line))
1745 elif db_ro_task_update is not None and isinstance(db_ro_task_update, dict):
1746 i = 0
1747 while True:
1748 st = "tasks.{}.status".format(i)
1749 if st not in db_ro_task_update:
1750 break
1751 line.clear()
1752 line.append(mark)
1753 line.append(event)
1754 line.append(db_ro_task_update.get("_id", ""))
1755 line.append(str(db_ro_task_update.get("locked_at", "")))
1756 line.append(str(db_ro_task_update.get("modified_at", "")))
1757 line.append("")
1758 line.append(str(db_ro_task_update.get("to_check_at", "")))
1759 line.append(str(db_ro_task_update.get("locked_by", "")))
1760 line.append("")
1761 line.append(str(db_ro_task_update.get("vim_info.refresh_at", "")))
1762 line.append("")
1763 line.append(str(db_ro_task_update.get("vim_info", "")))
1764 line.append(str(str(db_ro_task_update).count(".status")))
1765 line.append(db_ro_task_update.get(st, ""))
1766 line.append("")
1767 line.append(str(i))
1768 line.extend([""] * 3)
1769 i += 1
1770 self.logger.debug(";".join(line))
1771
1772 elif db_ro_task_delete is not None and isinstance(db_ro_task_delete, dict):
1773 line.clear()
1774 line.append(mark)
1775 line.append(event)
1776 line.append(db_ro_task_delete.get("_id", ""))
1777 line.append("")
1778 line.append(db_ro_task_delete.get("modified_at", ""))
1779 line.extend([""] * 13)
1780 self.logger.debug(";".join(line))
1781
1782 else:
1783 line.clear()
1784 line.append(mark)
1785 line.append(event)
1786 line.extend([""] * 16)
1787 self.logger.debug(";".join(line))
1788
1789 except Exception as e:
1790 self.logger.error("Error logging ro_task: {}".format(e))
1791
tierno70eeb182020-10-19 16:38:00 +00001792 def _delete_task(self, ro_task, task_index, task_depends, db_update):
1793 """
1794 Determine if this task need to be done or superseded
1795 :return: None
1796 """
1797 my_task = ro_task["tasks"][task_index]
1798 task_id = my_task["task_id"]
sousaedu80135b92021-02-17 15:05:18 +01001799 needed_delete = ro_task["vim_info"]["created"] or ro_task["vim_info"].get(
1800 "created_items", False
1801 )
1802
tierno70eeb182020-10-19 16:38:00 +00001803 if my_task["status"] == "FAILED":
1804 return None, None # TODO need to be retry??
sousaedu80135b92021-02-17 15:05:18 +01001805
tierno70eeb182020-10-19 16:38:00 +00001806 try:
1807 for index, task in enumerate(ro_task["tasks"]):
1808 if index == task_index or not task:
1809 continue # own task
sousaedu80135b92021-02-17 15:05:18 +01001810
1811 if (
1812 my_task["target_record"] == task["target_record"]
1813 and task["action"] == "CREATE"
1814 ):
tierno70eeb182020-10-19 16:38:00 +00001815 # set to finished
sousaedu80135b92021-02-17 15:05:18 +01001816 db_update["tasks.{}.status".format(index)] = task[
1817 "status"
1818 ] = "FINISHED"
1819 elif task["action"] == "CREATE" and task["status"] not in (
1820 "FINISHED",
1821 "SUPERSEDED",
1822 ):
tierno70eeb182020-10-19 16:38:00 +00001823 needed_delete = False
sousaedu80135b92021-02-17 15:05:18 +01001824
tierno70eeb182020-10-19 16:38:00 +00001825 if needed_delete:
1826 return self.item2class[my_task["item"]].delete(ro_task, task_index)
1827 else:
1828 return "SUPERSEDED", None
1829 except Exception as e:
1830 if not isinstance(e, NsWorkerException):
sousaedu80135b92021-02-17 15:05:18 +01001831 self.logger.critical(
1832 "Unexpected exception at _delete_task task={}: {}".format(
1833 task_id, e
1834 ),
1835 exc_info=True,
1836 )
1837
tierno70eeb182020-10-19 16:38:00 +00001838 return "FAILED", {"vim_status": "VIM_ERROR", "vim_details": str(e)}
1839
1840 def _create_task(self, ro_task, task_index, task_depends, db_update):
1841 """
1842 Determine if this task need to create something at VIM
1843 :return: None
1844 """
1845 my_task = ro_task["tasks"][task_index]
1846 task_id = my_task["task_id"]
1847 task_status = None
sousaedu80135b92021-02-17 15:05:18 +01001848
tierno70eeb182020-10-19 16:38:00 +00001849 if my_task["status"] == "FAILED":
1850 return None, None # TODO need to be retry??
1851 elif my_task["status"] == "SCHEDULED":
1852 # check if already created by another task
1853 for index, task in enumerate(ro_task["tasks"]):
1854 if index == task_index or not task:
1855 continue # own task
sousaedu80135b92021-02-17 15:05:18 +01001856
1857 if task["action"] == "CREATE" and task["status"] not in (
1858 "SCHEDULED",
1859 "FINISHED",
1860 "SUPERSEDED",
1861 ):
tierno70eeb182020-10-19 16:38:00 +00001862 return task["status"], "COPY_VIM_INFO"
1863
1864 try:
1865 task_status, ro_vim_item_update = self.item2class[my_task["item"]].new(
sousaedu80135b92021-02-17 15:05:18 +01001866 ro_task, task_index, task_depends
1867 )
tierno70eeb182020-10-19 16:38:00 +00001868 # TODO update other CREATE tasks
1869 except Exception as e:
1870 if not isinstance(e, NsWorkerException):
sousaedu80135b92021-02-17 15:05:18 +01001871 self.logger.error(
1872 "Error executing task={}: {}".format(task_id, e), exc_info=True
1873 )
1874
tierno70eeb182020-10-19 16:38:00 +00001875 task_status = "FAILED"
1876 ro_vim_item_update = {"vim_status": "VIM_ERROR", "vim_details": str(e)}
1877 # TODO update ro_vim_item_update
sousaedu80135b92021-02-17 15:05:18 +01001878
tierno70eeb182020-10-19 16:38:00 +00001879 return task_status, ro_vim_item_update
1880 else:
1881 return None, None
1882
1883 def _get_dependency(self, task_id, ro_task=None, target_id=None):
1884 """
1885 Look for dependency task
1886 :param task_id: Can be one of
1887 1. target_vim+blank+task.target_record_id: "(vim|sdn|wim):<id> (vnfrs|nsrs):(vld|vdu|flavor|image).<id>"
1888 2. task.target_record_id: "(vnfrs|nsrs):(vld|vdu|flavor|image).<id>"
1889 3. task.task_id: "<action_id>:number"
1890 :param ro_task:
1891 :param target_id:
1892 :return: database ro_task plus index of task
1893 """
sousaedu80135b92021-02-17 15:05:18 +01001894 if (
1895 task_id.startswith("vim:")
1896 or task_id.startswith("sdn:")
1897 or task_id.startswith("wim:")
1898 ):
tierno70eeb182020-10-19 16:38:00 +00001899 target_id, _, task_id = task_id.partition(" ")
1900
1901 if task_id.startswith("nsrs:") or task_id.startswith("vnfrs:"):
1902 ro_task_dependency = self.db.get_one(
1903 "ro_tasks",
sousaedu80135b92021-02-17 15:05:18 +01001904 q_filter={"target_id": target_id, "tasks.target_record_id": task_id},
1905 fail_on_empty=False,
1906 )
1907
tierno70eeb182020-10-19 16:38:00 +00001908 if ro_task_dependency:
1909 for task_index, task in enumerate(ro_task_dependency["tasks"]):
1910 if task["target_record_id"] == task_id:
1911 return ro_task_dependency, task_index
1912
1913 else:
1914 if ro_task:
1915 for task_index, task in enumerate(ro_task["tasks"]):
1916 if task and task["task_id"] == task_id:
1917 return ro_task, task_index
sousaedu80135b92021-02-17 15:05:18 +01001918
tierno70eeb182020-10-19 16:38:00 +00001919 ro_task_dependency = self.db.get_one(
1920 "ro_tasks",
sousaedu80135b92021-02-17 15:05:18 +01001921 q_filter={
1922 "tasks.ANYINDEX.task_id": task_id,
1923 "tasks.ANYINDEX.target_record.ne": None,
1924 },
1925 fail_on_empty=False,
1926 )
1927
tierno70eeb182020-10-19 16:38:00 +00001928 if ro_task_dependency:
1929 for task_index, task in ro_task_dependency["tasks"]:
1930 if task["task_id"] == task_id:
1931 return ro_task_dependency, task_index
1932 raise NsWorkerException("Cannot get depending task {}".format(task_id))
1933
1934 def _process_pending_tasks(self, ro_task):
1935 ro_task_id = ro_task["_id"]
1936 now = time.time()
sousaedu80135b92021-02-17 15:05:18 +01001937 # one day
1938 next_check_at = now + (24 * 60 * 60)
tierno70eeb182020-10-19 16:38:00 +00001939 db_ro_task_update = {}
1940
1941 def _update_refresh(new_status):
1942 # compute next_refresh
1943 nonlocal task
1944 nonlocal next_check_at
1945 nonlocal db_ro_task_update
1946 nonlocal ro_task
1947
1948 next_refresh = time.time()
sousaedu80135b92021-02-17 15:05:18 +01001949
tierno70eeb182020-10-19 16:38:00 +00001950 if task["item"] in ("image", "flavor"):
1951 next_refresh += self.REFRESH_IMAGE
1952 elif new_status == "BUILD":
1953 next_refresh += self.REFRESH_BUILD
1954 elif new_status == "DONE":
1955 next_refresh += self.REFRESH_ACTIVE
1956 else:
1957 next_refresh += self.REFRESH_ERROR
sousaedu80135b92021-02-17 15:05:18 +01001958
tierno70eeb182020-10-19 16:38:00 +00001959 next_check_at = min(next_check_at, next_refresh)
1960 db_ro_task_update["vim_info.refresh_at"] = next_refresh
1961 ro_task["vim_info"]["refresh_at"] = next_refresh
1962
1963 try:
gallardo88312d52022-01-31 16:50:48 +00001964 """
1965 # Log RO tasks only when loglevel is DEBUG
1966 if self.logger.getEffectiveLevel() == logging.DEBUG:
1967 self._log_ro_task(ro_task, None, None, "TASK_WF", "GET_TASK")
1968 """
tiernof1b640f2020-12-09 15:06:01 +00001969 # 0: get task_status_create
1970 lock_object = None
tierno70eeb182020-10-19 16:38:00 +00001971 task_status_create = None
sousaedu80135b92021-02-17 15:05:18 +01001972 task_create = next(
1973 (
1974 t
1975 for t in ro_task["tasks"]
1976 if t
1977 and t["action"] == "CREATE"
1978 and t["status"] in ("BUILD", "DONE")
1979 ),
1980 None,
1981 )
1982
tierno70eeb182020-10-19 16:38:00 +00001983 if task_create:
1984 task_status_create = task_create["status"]
sousaedu80135b92021-02-17 15:05:18 +01001985
tiernof1b640f2020-12-09 15:06:01 +00001986 # 1: look for tasks in status SCHEDULED, or in status CREATE if action is DONE or BUILD
tierno70eeb182020-10-19 16:38:00 +00001987 for task_action in ("DELETE", "CREATE", "EXEC"):
1988 db_vim_update = None
1989 new_status = None
sousaedu80135b92021-02-17 15:05:18 +01001990
tierno70eeb182020-10-19 16:38:00 +00001991 for task_index, task in enumerate(ro_task["tasks"]):
1992 if not task:
1993 continue # task deleted
sousaedu80135b92021-02-17 15:05:18 +01001994
tierno55fa0bb2020-12-08 23:11:53 +00001995 task_depends = {}
tierno70eeb182020-10-19 16:38:00 +00001996 target_update = None
sousaedu80135b92021-02-17 15:05:18 +01001997
1998 if (
1999 (
2000 task_action in ("DELETE", "EXEC")
2001 and task["status"] not in ("SCHEDULED", "BUILD")
2002 )
2003 or task["action"] != task_action
2004 or (
2005 task_action == "CREATE"
2006 and task["status"] in ("FINISHED", "SUPERSEDED")
2007 )
2008 ):
tierno70eeb182020-10-19 16:38:00 +00002009 continue
sousaedu80135b92021-02-17 15:05:18 +01002010
tierno70eeb182020-10-19 16:38:00 +00002011 task_path = "tasks.{}.status".format(task_index)
2012 try:
2013 db_vim_info_update = None
sousaedu80135b92021-02-17 15:05:18 +01002014
tierno70eeb182020-10-19 16:38:00 +00002015 if task["status"] == "SCHEDULED":
tierno70eeb182020-10-19 16:38:00 +00002016 # check if tasks that this depends on have been completed
2017 dependency_not_completed = False
sousaedu80135b92021-02-17 15:05:18 +01002018
2019 for dependency_task_id in task.get("depends_on") or ():
2020 (
2021 dependency_ro_task,
2022 dependency_task_index,
2023 ) = self._get_dependency(
2024 dependency_task_id, target_id=ro_task["target_id"]
2025 )
2026 dependency_task = dependency_ro_task["tasks"][
2027 dependency_task_index
2028 ]
2029
tierno70eeb182020-10-19 16:38:00 +00002030 if dependency_task["status"] == "SCHEDULED":
2031 dependency_not_completed = True
sousaedu80135b92021-02-17 15:05:18 +01002032 next_check_at = min(
2033 next_check_at, dependency_ro_task["to_check_at"]
2034 )
lloretgalleg88486222021-02-19 12:06:52 +00002035 # must allow dependent task to be processed first
2036 # to do this set time after last_task_processed
2037 next_check_at = max(
2038 self.time_last_task_processed, next_check_at
2039 )
tierno70eeb182020-10-19 16:38:00 +00002040 break
2041 elif dependency_task["status"] == "FAILED":
2042 error_text = "Cannot {} {} because depends on failed {} {} id={}): {}".format(
sousaedu80135b92021-02-17 15:05:18 +01002043 task["action"],
2044 task["item"],
2045 dependency_task["action"],
2046 dependency_task["item"],
2047 dependency_task_id,
2048 dependency_ro_task["vim_info"].get(
2049 "vim_details"
2050 ),
2051 )
2052 self.logger.error(
2053 "task={} {}".format(task["task_id"], error_text)
2054 )
tierno70eeb182020-10-19 16:38:00 +00002055 raise NsWorkerException(error_text)
2056
sousaedu80135b92021-02-17 15:05:18 +01002057 task_depends[dependency_task_id] = dependency_ro_task[
2058 "vim_info"
2059 ]["vim_id"]
2060 task_depends[
2061 "TASK-{}".format(dependency_task_id)
2062 ] = dependency_ro_task["vim_info"]["vim_id"]
2063
tierno70eeb182020-10-19 16:38:00 +00002064 if dependency_not_completed:
2065 # TODO set at vim_info.vim_details that it is waiting
2066 continue
sousaedu80135b92021-02-17 15:05:18 +01002067
tiernof1b640f2020-12-09 15:06:01 +00002068 # before calling VIM-plugin as it can take more than task_locked_time, insert to LockRenew
2069 # the task of renew this locking. It will update database locket_at periodically
2070 if not lock_object:
sousaedu80135b92021-02-17 15:05:18 +01002071 lock_object = LockRenew.add_lock_object(
2072 "ro_tasks", ro_task, self
2073 )
2074
tierno70eeb182020-10-19 16:38:00 +00002075 if task["action"] == "DELETE":
sousaedu80135b92021-02-17 15:05:18 +01002076 (new_status, db_vim_info_update,) = self._delete_task(
2077 ro_task, task_index, task_depends, db_ro_task_update
2078 )
2079 new_status = (
2080 "FINISHED" if new_status == "DONE" else new_status
2081 )
tierno70eeb182020-10-19 16:38:00 +00002082 # ^with FINISHED instead of DONE it will not be refreshing
sousaedu80135b92021-02-17 15:05:18 +01002083
tierno70eeb182020-10-19 16:38:00 +00002084 if new_status in ("FINISHED", "SUPERSEDED"):
2085 target_update = "DELETE"
2086 elif task["action"] == "EXEC":
sousaedu80135b92021-02-17 15:05:18 +01002087 (
2088 new_status,
2089 db_vim_info_update,
2090 db_task_update,
2091 ) = self.item2class[task["item"]].exec(
2092 ro_task, task_index, task_depends
2093 )
2094 new_status = (
2095 "FINISHED" if new_status == "DONE" else new_status
2096 )
tierno70eeb182020-10-19 16:38:00 +00002097 # ^with FINISHED instead of DONE it will not be refreshing
sousaedu80135b92021-02-17 15:05:18 +01002098
tierno70eeb182020-10-19 16:38:00 +00002099 if db_task_update:
2100 # load into database the modified db_task_update "retries" and "next_retry"
2101 if db_task_update.get("retries"):
sousaedu80135b92021-02-17 15:05:18 +01002102 db_ro_task_update[
2103 "tasks.{}.retries".format(task_index)
2104 ] = db_task_update["retries"]
2105
2106 next_check_at = time.time() + db_task_update.get(
2107 "next_retry", 60
2108 )
tierno70eeb182020-10-19 16:38:00 +00002109 target_update = None
2110 elif task["action"] == "CREATE":
2111 if task["status"] == "SCHEDULED":
2112 if task_status_create:
2113 new_status = task_status_create
2114 target_update = "COPY_VIM_INFO"
2115 else:
sousaedu80135b92021-02-17 15:05:18 +01002116 new_status, db_vim_info_update = self.item2class[
2117 task["item"]
2118 ].new(ro_task, task_index, task_depends)
tierno70eeb182020-10-19 16:38:00 +00002119 # self._create_task(ro_task, task_index, task_depends, db_ro_task_update)
2120 _update_refresh(new_status)
2121 else:
sousaedu80135b92021-02-17 15:05:18 +01002122 if (
2123 ro_task["vim_info"]["refresh_at"]
2124 and now > ro_task["vim_info"]["refresh_at"]
2125 ):
2126 new_status, db_vim_info_update = self.item2class[
2127 task["item"]
2128 ].refresh(ro_task)
tierno70eeb182020-10-19 16:38:00 +00002129 _update_refresh(new_status)
gallardo4d5c8142022-01-20 09:07:08 +00002130 else:
2131 # The refresh is updated to avoid set the value of "refresh_at" to
2132 # default value (next_check_at = now + (24 * 60 * 60)) when status is BUILD,
2133 # because it can happen that in this case the task is never processed
2134 _update_refresh(task["status"])
sousaedu80135b92021-02-17 15:05:18 +01002135
tierno70eeb182020-10-19 16:38:00 +00002136 except Exception as e:
2137 new_status = "FAILED"
sousaedu80135b92021-02-17 15:05:18 +01002138 db_vim_info_update = {
2139 "vim_status": "VIM_ERROR",
2140 "vim_details": str(e),
2141 }
2142
2143 if not isinstance(
2144 e, (NsWorkerException, vimconn.VimConnException)
2145 ):
2146 self.logger.error(
2147 "Unexpected exception at _delete_task task={}: {}".format(
2148 task["task_id"], e
2149 ),
2150 exc_info=True,
2151 )
tierno70eeb182020-10-19 16:38:00 +00002152
2153 try:
2154 if db_vim_info_update:
2155 db_vim_update = db_vim_info_update.copy()
sousaedu80135b92021-02-17 15:05:18 +01002156 db_ro_task_update.update(
2157 {
2158 "vim_info." + k: v
2159 for k, v in db_vim_info_update.items()
2160 }
2161 )
tierno70eeb182020-10-19 16:38:00 +00002162 ro_task["vim_info"].update(db_vim_info_update)
2163
2164 if new_status:
2165 if task_action == "CREATE":
2166 task_status_create = new_status
2167 db_ro_task_update[task_path] = new_status
tierno70eeb182020-10-19 16:38:00 +00002168
sousaedu80135b92021-02-17 15:05:18 +01002169 if target_update or db_vim_update:
tierno70eeb182020-10-19 16:38:00 +00002170 if target_update == "DELETE":
2171 self._update_target(task, None)
2172 elif target_update == "COPY_VIM_INFO":
2173 self._update_target(task, ro_task["vim_info"])
2174 else:
2175 self._update_target(task, db_vim_update)
2176
2177 except Exception as e:
sousaedu80135b92021-02-17 15:05:18 +01002178 if (
2179 isinstance(e, DbException)
2180 and e.http_code == HTTPStatus.NOT_FOUND
2181 ):
tierno70eeb182020-10-19 16:38:00 +00002182 # if the vnfrs or nsrs has been removed from database, this task must be removed
sousaedu80135b92021-02-17 15:05:18 +01002183 self.logger.debug(
2184 "marking to delete task={}".format(task["task_id"])
2185 )
tierno70eeb182020-10-19 16:38:00 +00002186 self.tasks_to_delete.append(task)
2187 else:
sousaedu80135b92021-02-17 15:05:18 +01002188 self.logger.error(
2189 "Unexpected exception at _update_target task={}: {}".format(
2190 task["task_id"], e
2191 ),
2192 exc_info=True,
2193 )
tierno70eeb182020-10-19 16:38:00 +00002194
tiernof1b640f2020-12-09 15:06:01 +00002195 locked_at = ro_task["locked_at"]
sousaedu80135b92021-02-17 15:05:18 +01002196
tiernof1b640f2020-12-09 15:06:01 +00002197 if lock_object:
sousaedu80135b92021-02-17 15:05:18 +01002198 locked_at = [
2199 lock_object["locked_at"],
2200 lock_object["locked_at"] + self.task_locked_time,
2201 ]
tiernof1b640f2020-12-09 15:06:01 +00002202 # locked_at contains two times to avoid race condition. In case the lock has been renew, it will
2203 # contain exactly locked_at + self.task_locked_time
2204 LockRenew.remove_lock_object(lock_object)
sousaedu80135b92021-02-17 15:05:18 +01002205
2206 q_filter = {
2207 "_id": ro_task["_id"],
2208 "to_check_at": ro_task["to_check_at"],
2209 "locked_at": locked_at,
2210 }
tierno70eeb182020-10-19 16:38:00 +00002211 # modify own task. Try filtering by to_next_check. For race condition if to_check_at has been modified,
2212 # outside this task (by ro_nbi) do not update it
2213 db_ro_task_update["locked_by"] = None
2214 # locked_at converted to int only for debugging. When has not decimals it means it has been unlocked
tiernof1b640f2020-12-09 15:06:01 +00002215 db_ro_task_update["locked_at"] = int(now - self.task_locked_time)
2216 db_ro_task_update["modified_at"] = now
tierno70eeb182020-10-19 16:38:00 +00002217 db_ro_task_update["to_check_at"] = next_check_at
sousaedu80135b92021-02-17 15:05:18 +01002218
gallardo88312d52022-01-31 16:50:48 +00002219 """
2220 # Log RO tasks only when loglevel is DEBUG
2221 if self.logger.getEffectiveLevel() == logging.DEBUG:
2222 db_ro_task_update_log = db_ro_task_update.copy()
2223 db_ro_task_update_log["_id"] = q_filter["_id"]
2224 self._log_ro_task(None, db_ro_task_update_log, None, "TASK_WF", "SET_TASK")
2225 """
2226
sousaedu80135b92021-02-17 15:05:18 +01002227 if not self.db.set_one(
2228 "ro_tasks",
2229 update_dict=db_ro_task_update,
2230 q_filter=q_filter,
2231 fail_on_empty=False,
2232 ):
tierno70eeb182020-10-19 16:38:00 +00002233 del db_ro_task_update["to_check_at"]
2234 del q_filter["to_check_at"]
gallardo88312d52022-01-31 16:50:48 +00002235 """
2236 # Log RO tasks only when loglevel is DEBUG
2237 if self.logger.getEffectiveLevel() == logging.DEBUG:
2238 self._log_ro_task(
2239 None,
2240 db_ro_task_update_log,
2241 None,
2242 "TASK_WF",
2243 "SET_TASK " + str(q_filter),
2244 )
2245 """
sousaedu80135b92021-02-17 15:05:18 +01002246 self.db.set_one(
2247 "ro_tasks",
2248 q_filter=q_filter,
2249 update_dict=db_ro_task_update,
2250 fail_on_empty=True,
2251 )
tierno70eeb182020-10-19 16:38:00 +00002252 except DbException as e:
sousaedu80135b92021-02-17 15:05:18 +01002253 self.logger.error(
2254 "ro_task={} Error updating database {}".format(ro_task_id, e)
2255 )
tierno70eeb182020-10-19 16:38:00 +00002256 except Exception as e:
sousaedu80135b92021-02-17 15:05:18 +01002257 self.logger.error(
2258 "Error executing ro_task={}: {}".format(ro_task_id, e), exc_info=True
2259 )
tierno70eeb182020-10-19 16:38:00 +00002260
2261 def _update_target(self, task, ro_vim_item_update):
2262 table, _, temp = task["target_record"].partition(":")
2263 _id, _, path_vim_status = temp.partition(":")
sousaedu80135b92021-02-17 15:05:18 +01002264 path_item = path_vim_status[: path_vim_status.rfind(".")]
2265 path_item = path_item[: path_item.rfind(".")]
tierno70eeb182020-10-19 16:38:00 +00002266 # path_vim_status: dot separated list targeting vim information, e.g. "vdur.10.vim_info.vim:id"
2267 # path_item: dot separated list targeting record information, e.g. "vdur.10"
sousaedu80135b92021-02-17 15:05:18 +01002268
tierno70eeb182020-10-19 16:38:00 +00002269 if ro_vim_item_update:
sousaedu80135b92021-02-17 15:05:18 +01002270 update_dict = {
2271 path_vim_status + "." + k: v
2272 for k, v in ro_vim_item_update.items()
2273 if k
2274 in ("vim_id", "vim_details", "vim_name", "vim_status", "interfaces")
2275 }
2276
tierno70eeb182020-10-19 16:38:00 +00002277 if path_vim_status.startswith("vdur."):
2278 # for backward compatibility, add vdur.name apart from vdur.vim_name
2279 if ro_vim_item_update.get("vim_name"):
2280 update_dict[path_item + ".name"] = ro_vim_item_update["vim_name"]
sousaedu80135b92021-02-17 15:05:18 +01002281
tierno70eeb182020-10-19 16:38:00 +00002282 # for backward compatibility, add vdur.vim-id apart from vdur.vim_id
2283 if ro_vim_item_update.get("vim_id"):
2284 update_dict[path_item + ".vim-id"] = ro_vim_item_update["vim_id"]
sousaedu80135b92021-02-17 15:05:18 +01002285
tierno70eeb182020-10-19 16:38:00 +00002286 # update general status
2287 if ro_vim_item_update.get("vim_status"):
sousaedu80135b92021-02-17 15:05:18 +01002288 update_dict[path_item + ".status"] = ro_vim_item_update[
2289 "vim_status"
2290 ]
2291
tierno70eeb182020-10-19 16:38:00 +00002292 if ro_vim_item_update.get("interfaces"):
2293 path_interfaces = path_item + ".interfaces"
sousaedu80135b92021-02-17 15:05:18 +01002294
tierno70eeb182020-10-19 16:38:00 +00002295 for i, iface in enumerate(ro_vim_item_update.get("interfaces")):
2296 if iface:
sousaedu80135b92021-02-17 15:05:18 +01002297 update_dict.update(
2298 {
2299 path_interfaces + ".{}.".format(i) + k: v
2300 for k, v in iface.items()
2301 if k in ("vlan", "compute_node", "pci")
2302 }
2303 )
2304
tierno70eeb182020-10-19 16:38:00 +00002305 # put ip_address and mac_address with ip-address and mac-address
sousaedu80135b92021-02-17 15:05:18 +01002306 if iface.get("ip_address"):
2307 update_dict[
2308 path_interfaces + ".{}.".format(i) + "ip-address"
2309 ] = iface["ip_address"]
2310
2311 if iface.get("mac_address"):
2312 update_dict[
2313 path_interfaces + ".{}.".format(i) + "mac-address"
2314 ] = iface["mac_address"]
2315
tierno70eeb182020-10-19 16:38:00 +00002316 if iface.get("mgmt_vnf_interface") and iface.get("ip_address"):
sousaedu80135b92021-02-17 15:05:18 +01002317 update_dict["ip-address"] = iface.get("ip_address").split(
2318 ";"
2319 )[0]
2320
tierno70eeb182020-10-19 16:38:00 +00002321 if iface.get("mgmt_vdu_interface") and iface.get("ip_address"):
sousaedu80135b92021-02-17 15:05:18 +01002322 update_dict[path_item + ".ip-address"] = iface.get(
2323 "ip_address"
2324 ).split(";")[0]
tierno70eeb182020-10-19 16:38:00 +00002325
2326 self.db.set_one(table, q_filter={"_id": _id}, update_dict=update_dict)
2327 else:
2328 update_dict = {path_item + ".status": "DELETED"}
sousaedu80135b92021-02-17 15:05:18 +01002329 self.db.set_one(
2330 table,
2331 q_filter={"_id": _id},
2332 update_dict=update_dict,
2333 unset={path_vim_status: None},
2334 )
tierno70eeb182020-10-19 16:38:00 +00002335
2336 def _process_delete_db_tasks(self):
2337 """
2338 Delete task from database because vnfrs or nsrs or both have been deleted
2339 :return: None. Uses and modify self.tasks_to_delete
2340 """
2341 while self.tasks_to_delete:
2342 task = self.tasks_to_delete[0]
2343 vnfrs_deleted = None
2344 nsr_id = task["nsr_id"]
sousaedu80135b92021-02-17 15:05:18 +01002345
tierno70eeb182020-10-19 16:38:00 +00002346 if task["target_record"].startswith("vnfrs:"):
2347 # check if nsrs is present
2348 if self.db.get_one("nsrs", {"_id": nsr_id}, fail_on_empty=False):
2349 vnfrs_deleted = task["target_record"].split(":")[1]
sousaedu80135b92021-02-17 15:05:18 +01002350
tierno70eeb182020-10-19 16:38:00 +00002351 try:
2352 self.delete_db_tasks(self.db, nsr_id, vnfrs_deleted)
2353 except Exception as e:
sousaedu80135b92021-02-17 15:05:18 +01002354 self.logger.error(
2355 "Error deleting task={}: {}".format(task["task_id"], e)
2356 )
tierno70eeb182020-10-19 16:38:00 +00002357 self.tasks_to_delete.pop(0)
2358
2359 @staticmethod
2360 def delete_db_tasks(db, nsr_id, vnfrs_deleted):
2361 """
2362 Static method because it is called from osm_ng_ro.ns
2363 :param db: instance of database to use
2364 :param nsr_id: affected nsrs id
2365 :param vnfrs_deleted: only tasks with this vnfr id. If None, all affected by nsr_id
2366 :return: None, exception is fails
2367 """
2368 retries = 5
2369 for retry in range(retries):
2370 ro_tasks = db.get_list("ro_tasks", {"tasks.nsr_id": nsr_id})
2371 now = time.time()
2372 conflict = False
sousaedu80135b92021-02-17 15:05:18 +01002373
tierno70eeb182020-10-19 16:38:00 +00002374 for ro_task in ro_tasks:
2375 db_update = {}
2376 to_delete_ro_task = True
sousaedu80135b92021-02-17 15:05:18 +01002377
tierno70eeb182020-10-19 16:38:00 +00002378 for index, task in enumerate(ro_task["tasks"]):
2379 if not task:
2380 pass
sousaedu80135b92021-02-17 15:05:18 +01002381 elif (not vnfrs_deleted and task["nsr_id"] == nsr_id) or (
2382 vnfrs_deleted
2383 and task["target_record"].startswith("vnfrs:" + vnfrs_deleted)
2384 ):
tierno70eeb182020-10-19 16:38:00 +00002385 db_update["tasks.{}".format(index)] = None
2386 else:
sousaedu80135b92021-02-17 15:05:18 +01002387 # used by other nsr, ro_task cannot be deleted
2388 to_delete_ro_task = False
2389
tierno70eeb182020-10-19 16:38:00 +00002390 # delete or update if nobody has changed ro_task meanwhile. Used modified_at for known if changed
2391 if to_delete_ro_task:
sousaedu80135b92021-02-17 15:05:18 +01002392 if not db.del_one(
2393 "ro_tasks",
2394 q_filter={
2395 "_id": ro_task["_id"],
2396 "modified_at": ro_task["modified_at"],
2397 },
2398 fail_on_empty=False,
2399 ):
tierno70eeb182020-10-19 16:38:00 +00002400 conflict = True
2401 elif db_update:
2402 db_update["modified_at"] = now
sousaedu80135b92021-02-17 15:05:18 +01002403 if not db.set_one(
2404 "ro_tasks",
2405 q_filter={
2406 "_id": ro_task["_id"],
2407 "modified_at": ro_task["modified_at"],
2408 },
2409 update_dict=db_update,
2410 fail_on_empty=False,
2411 ):
tierno70eeb182020-10-19 16:38:00 +00002412 conflict = True
2413 if not conflict:
2414 return
2415 else:
2416 raise NsWorkerException("Exceeded {} retries".format(retries))
2417
tierno1d213f42020-04-24 14:02:51 +00002418 def run(self):
2419 # load database
tierno86153522020-12-06 18:27:16 +00002420 self.logger.info("Starting")
tierno1d213f42020-04-24 14:02:51 +00002421 while True:
tierno70eeb182020-10-19 16:38:00 +00002422 # step 1: get commands from queue
tierno1d213f42020-04-24 14:02:51 +00002423 try:
tierno86153522020-12-06 18:27:16 +00002424 if self.vim_targets:
2425 task = self.task_queue.get(block=False)
2426 else:
2427 if not self.idle:
2428 self.logger.debug("enters in idle state")
2429 self.idle = True
2430 task = self.task_queue.get(block=True)
2431 self.idle = False
2432
tierno1d213f42020-04-24 14:02:51 +00002433 if task[0] == "terminate":
2434 break
tierno70eeb182020-10-19 16:38:00 +00002435 elif task[0] == "load_vim":
tierno86153522020-12-06 18:27:16 +00002436 self.logger.info("order to load vim {}".format(task[1]))
tierno1d213f42020-04-24 14:02:51 +00002437 self._load_vim(task[1])
tierno70eeb182020-10-19 16:38:00 +00002438 elif task[0] == "unload_vim":
tierno86153522020-12-06 18:27:16 +00002439 self.logger.info("order to unload vim {}".format(task[1]))
tierno70eeb182020-10-19 16:38:00 +00002440 self._unload_vim(task[1])
2441 elif task[0] == "reload_vim":
2442 self._reload_vim(task[1])
2443 elif task[0] == "check_vim":
tierno86153522020-12-06 18:27:16 +00002444 self.logger.info("order to check vim {}".format(task[1]))
tierno70eeb182020-10-19 16:38:00 +00002445 self._check_vim(task[1])
tierno1d213f42020-04-24 14:02:51 +00002446 continue
tierno70eeb182020-10-19 16:38:00 +00002447 except Exception as e:
2448 if isinstance(e, queue.Empty):
2449 pass
2450 else:
sousaedu80135b92021-02-17 15:05:18 +01002451 self.logger.critical(
2452 "Error processing task: {}".format(e), exc_info=True
2453 )
tierno1d213f42020-04-24 14:02:51 +00002454
tierno70eeb182020-10-19 16:38:00 +00002455 # step 2: process pending_tasks, delete not needed tasks
tierno1d213f42020-04-24 14:02:51 +00002456 try:
tierno70eeb182020-10-19 16:38:00 +00002457 if self.tasks_to_delete:
2458 self._process_delete_db_tasks()
tierno1d213f42020-04-24 14:02:51 +00002459 busy = False
gallardo88312d52022-01-31 16:50:48 +00002460 """
2461 # Log RO tasks only when loglevel is DEBUG
2462 if self.logger.getEffectiveLevel() == logging.DEBUG:
2463 _ = self._get_db_all_tasks()
2464 """
tierno1d213f42020-04-24 14:02:51 +00002465 ro_task = self._get_db_task()
2466 if ro_task:
tierno70eeb182020-10-19 16:38:00 +00002467 self._process_pending_tasks(ro_task)
tierno1d213f42020-04-24 14:02:51 +00002468 busy = True
2469 if not busy:
2470 time.sleep(5)
2471 except Exception as e:
sousaedu80135b92021-02-17 15:05:18 +01002472 self.logger.critical(
2473 "Unexpected exception at run: " + str(e), exc_info=True
2474 )
tierno1d213f42020-04-24 14:02:51 +00002475
tierno86153522020-12-06 18:27:16 +00002476 self.logger.info("Finishing")