blob: 01c894ddefb4a06fe0ee3b3e009864667d53340d [file] [log] [blame]
tierno1d213f42020-04-24 14:02:51 +00001# -*- coding: utf-8 -*-
2
3##
4# Copyright 2020 Telefonica Investigacion y Desarrollo, S.A.U.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17#
18##
19
20""""
21This is thread that interacts with a VIM. It processes TASKs sequentially against a single VIM.
22The tasks are stored at database in table ro_tasks
23A single ro_task refers to a VIM element (flavor, image, network, ...).
24A ro_task can contain several 'tasks', each one with a target, where to store the results
25"""
26
sousaedu80135b92021-02-17 15:05:18 +010027from copy import deepcopy
28from http import HTTPStatus
sousaedu049cbb12022-01-05 11:39:35 +000029import logging
sousaedu80135b92021-02-17 15:05:18 +010030from os import mkdir
sousaedu049cbb12022-01-05 11:39:35 +000031import queue
sousaedu80135b92021-02-17 15:05:18 +010032from shutil import rmtree
sousaedu049cbb12022-01-05 11:39:35 +000033import threading
34import time
sousaedu80135b92021-02-17 15:05:18 +010035from unittest.mock import Mock
36
sousaedu049cbb12022-01-05 11:39:35 +000037from importlib_metadata import entry_points
tierno1d213f42020-04-24 14:02:51 +000038from osm_common.dbbase import DbException
tiernof1b640f2020-12-09 15:06:01 +000039from osm_ng_ro.vim_admin import LockRenew
sousaedu049cbb12022-01-05 11:39:35 +000040from osm_ro_plugin import sdnconn, vimconn
41from osm_ro_plugin.sdn_dummy import SdnDummyConnector
42from osm_ro_plugin.vim_dummy import VimDummyConnector
43import yaml
tierno1d213f42020-04-24 14:02:51 +000044
45__author__ = "Alfonso Tierno"
46__date__ = "$28-Sep-2017 12:07:15$"
47
48
49def deep_get(target_dict, *args, **kwargs):
50 """
51 Get a value from target_dict entering in the nested keys. If keys does not exist, it returns None
52 Example target_dict={a: {b: 5}}; key_list=[a,b] returns 5; both key_list=[a,b,c] and key_list=[f,h] return None
53 :param target_dict: dictionary to be read
54 :param args: list of keys to read from target_dict
55 :param kwargs: only can contain default=value to return if key is not present in the nested dictionary
56 :return: The wanted value if exist, None or default otherwise
57 """
58 for key in args:
59 if not isinstance(target_dict, dict) or key not in target_dict:
60 return kwargs.get("default")
61 target_dict = target_dict[key]
62 return target_dict
63
64
65class NsWorkerException(Exception):
66 pass
67
68
69class FailingConnector:
70 def __init__(self, error_msg):
71 self.error_msg = error_msg
sousaedu80135b92021-02-17 15:05:18 +010072
tierno1d213f42020-04-24 14:02:51 +000073 for method in dir(vimconn.VimConnector):
74 if method[0] != "_":
sousaedu80135b92021-02-17 15:05:18 +010075 setattr(
76 self, method, Mock(side_effect=vimconn.VimConnException(error_msg))
77 )
78
tierno70eeb182020-10-19 16:38:00 +000079 for method in dir(sdnconn.SdnConnectorBase):
80 if method[0] != "_":
sousaedu80135b92021-02-17 15:05:18 +010081 setattr(
82 self, method, Mock(side_effect=sdnconn.SdnConnectorError(error_msg))
83 )
tierno1d213f42020-04-24 14:02:51 +000084
85
86class NsWorkerExceptionNotFound(NsWorkerException):
87 pass
88
89
tierno70eeb182020-10-19 16:38:00 +000090class VimInteractionBase:
sousaedu80135b92021-02-17 15:05:18 +010091 """Base class to call VIM/SDN for creating, deleting and refresh networks, VMs, flavors, ...
tierno70eeb182020-10-19 16:38:00 +000092 It implements methods that does nothing and return ok"""
sousaedu80135b92021-02-17 15:05:18 +010093
tierno70eeb182020-10-19 16:38:00 +000094 def __init__(self, db, my_vims, db_vims, logger):
tierno1d213f42020-04-24 14:02:51 +000095 self.db = db
tierno70eeb182020-10-19 16:38:00 +000096 self.logger = logger
97 self.my_vims = my_vims
98 self.db_vims = db_vims
tierno1d213f42020-04-24 14:02:51 +000099
tierno70eeb182020-10-19 16:38:00 +0000100 def new(self, ro_task, task_index, task_depends):
101 return "BUILD", {}
tierno1d213f42020-04-24 14:02:51 +0000102
tierno70eeb182020-10-19 16:38:00 +0000103 def refresh(self, ro_task):
104 """skip calling VIM to get image, flavor status. Assumes ok"""
tierno1d213f42020-04-24 14:02:51 +0000105 if ro_task["vim_info"]["vim_status"] == "VIM_ERROR":
106 return "FAILED", {}
sousaedu80135b92021-02-17 15:05:18 +0100107
tierno1d213f42020-04-24 14:02:51 +0000108 return "DONE", {}
109
tierno70eeb182020-10-19 16:38:00 +0000110 def delete(self, ro_task, task_index):
111 """skip calling VIM to delete image. Assumes ok"""
tierno1d213f42020-04-24 14:02:51 +0000112 return "DONE", {}
113
tierno70eeb182020-10-19 16:38:00 +0000114 def exec(self, ro_task, task_index, task_depends):
115 return "DONE", None, None
tierno1d213f42020-04-24 14:02:51 +0000116
tierno1d213f42020-04-24 14:02:51 +0000117
tierno70eeb182020-10-19 16:38:00 +0000118class VimInteractionNet(VimInteractionBase):
tierno70eeb182020-10-19 16:38:00 +0000119 def new(self, ro_task, task_index, task_depends):
tierno1d213f42020-04-24 14:02:51 +0000120 vim_net_id = None
121 task = ro_task["tasks"][task_index]
122 task_id = task["task_id"]
123 created = False
124 created_items = {}
125 target_vim = self.my_vims[ro_task["target_id"]]
sousaedu80135b92021-02-17 15:05:18 +0100126
tierno1d213f42020-04-24 14:02:51 +0000127 try:
128 # FIND
129 if task.get("find_params"):
130 # if management, get configuration of VIM
131 if task["find_params"].get("filter_dict"):
132 vim_filter = task["find_params"]["filter_dict"]
sousaedu80135b92021-02-17 15:05:18 +0100133 # mamagement network
134 elif task["find_params"].get("mgmt"):
135 if deep_get(
136 self.db_vims[ro_task["target_id"]],
137 "config",
138 "management_network_id",
139 ):
140 vim_filter = {
141 "id": self.db_vims[ro_task["target_id"]]["config"][
142 "management_network_id"
143 ]
144 }
145 elif deep_get(
146 self.db_vims[ro_task["target_id"]],
147 "config",
148 "management_network_name",
149 ):
150 vim_filter = {
151 "name": self.db_vims[ro_task["target_id"]]["config"][
152 "management_network_name"
153 ]
154 }
tierno1d213f42020-04-24 14:02:51 +0000155 else:
156 vim_filter = {"name": task["find_params"]["name"]}
157 else:
sousaedu80135b92021-02-17 15:05:18 +0100158 raise NsWorkerExceptionNotFound(
159 "Invalid find_params for new_net {}".format(task["find_params"])
160 )
tierno1d213f42020-04-24 14:02:51 +0000161
162 vim_nets = target_vim.get_network_list(vim_filter)
163 if not vim_nets and not task.get("params"):
sousaedu80135b92021-02-17 15:05:18 +0100164 raise NsWorkerExceptionNotFound(
165 "Network not found with this criteria: '{}'".format(
166 task.get("find_params")
167 )
168 )
tierno1d213f42020-04-24 14:02:51 +0000169 elif len(vim_nets) > 1:
170 raise NsWorkerException(
sousaedu80135b92021-02-17 15:05:18 +0100171 "More than one network found with this criteria: '{}'".format(
172 task["find_params"]
173 )
174 )
175
tierno1d213f42020-04-24 14:02:51 +0000176 if vim_nets:
177 vim_net_id = vim_nets[0]["id"]
178 else:
179 # CREATE
180 params = task["params"]
181 vim_net_id, created_items = target_vim.new_network(**params)
182 created = True
183
sousaedu80135b92021-02-17 15:05:18 +0100184 ro_vim_item_update = {
185 "vim_id": vim_net_id,
186 "vim_status": "BUILD",
187 "created": created,
188 "created_items": created_items,
189 "vim_details": None,
190 }
tierno1d213f42020-04-24 14:02:51 +0000191 self.logger.debug(
sousaedu80135b92021-02-17 15:05:18 +0100192 "task={} {} new-net={} created={}".format(
193 task_id, ro_task["target_id"], vim_net_id, created
194 )
195 )
196
tierno1d213f42020-04-24 14:02:51 +0000197 return "BUILD", ro_vim_item_update
198 except (vimconn.VimConnException, NsWorkerException) as e:
sousaedu80135b92021-02-17 15:05:18 +0100199 self.logger.error(
200 "task={} vim={} new-net: {}".format(task_id, ro_task["target_id"], e)
201 )
202 ro_vim_item_update = {
203 "vim_status": "VIM_ERROR",
204 "created": created,
205 "vim_details": str(e),
206 }
207
tierno1d213f42020-04-24 14:02:51 +0000208 return "FAILED", ro_vim_item_update
209
tierno70eeb182020-10-19 16:38:00 +0000210 def refresh(self, ro_task):
tierno1d213f42020-04-24 14:02:51 +0000211 """Call VIM to get network status"""
212 ro_task_id = ro_task["_id"]
213 target_vim = self.my_vims[ro_task["target_id"]]
tierno1d213f42020-04-24 14:02:51 +0000214 vim_id = ro_task["vim_info"]["vim_id"]
215 net_to_refresh_list = [vim_id]
sousaedu80135b92021-02-17 15:05:18 +0100216
tierno1d213f42020-04-24 14:02:51 +0000217 try:
218 vim_dict = target_vim.refresh_nets_status(net_to_refresh_list)
219 vim_info = vim_dict[vim_id]
sousaedu80135b92021-02-17 15:05:18 +0100220
tierno1d213f42020-04-24 14:02:51 +0000221 if vim_info["status"] == "ACTIVE":
222 task_status = "DONE"
223 elif vim_info["status"] == "BUILD":
224 task_status = "BUILD"
225 else:
226 task_status = "FAILED"
227 except vimconn.VimConnException as e:
228 # Mark all tasks at VIM_ERROR status
sousaedu80135b92021-02-17 15:05:18 +0100229 self.logger.error(
230 "ro_task={} vim={} get-net={}: {}".format(
231 ro_task_id, ro_task["target_id"], vim_id, e
232 )
233 )
tierno1d213f42020-04-24 14:02:51 +0000234 vim_info = {"status": "VIM_ERROR", "error_msg": str(e)}
235 task_status = "FAILED"
236
237 ro_vim_item_update = {}
238 if ro_task["vim_info"]["vim_status"] != vim_info["status"]:
239 ro_vim_item_update["vim_status"] = vim_info["status"]
sousaedu80135b92021-02-17 15:05:18 +0100240
tierno1d213f42020-04-24 14:02:51 +0000241 if ro_task["vim_info"]["vim_name"] != vim_info.get("name"):
242 ro_vim_item_update["vim_name"] = vim_info.get("name")
sousaedu80135b92021-02-17 15:05:18 +0100243
tierno1d213f42020-04-24 14:02:51 +0000244 if vim_info["status"] in ("ERROR", "VIM_ERROR"):
tierno70eeb182020-10-19 16:38:00 +0000245 if ro_task["vim_info"]["vim_details"] != vim_info.get("error_msg"):
246 ro_vim_item_update["vim_details"] = vim_info.get("error_msg")
tierno1d213f42020-04-24 14:02:51 +0000247 elif vim_info["status"] == "DELETED":
248 ro_vim_item_update["vim_id"] = None
249 ro_vim_item_update["vim_details"] = "Deleted externally"
250 else:
251 if ro_task["vim_info"]["vim_details"] != vim_info["vim_info"]:
252 ro_vim_item_update["vim_details"] = vim_info["vim_info"]
sousaedu80135b92021-02-17 15:05:18 +0100253
tierno1d213f42020-04-24 14:02:51 +0000254 if ro_vim_item_update:
sousaedu80135b92021-02-17 15:05:18 +0100255 self.logger.debug(
256 "ro_task={} {} get-net={}: status={} {}".format(
257 ro_task_id,
258 ro_task["target_id"],
259 vim_id,
260 ro_vim_item_update.get("vim_status"),
261 ro_vim_item_update.get("vim_details")
262 if ro_vim_item_update.get("vim_status") != "ACTIVE"
263 else "",
264 )
265 )
266
tierno1d213f42020-04-24 14:02:51 +0000267 return task_status, ro_vim_item_update
268
tierno70eeb182020-10-19 16:38:00 +0000269 def delete(self, ro_task, task_index):
tierno1d213f42020-04-24 14:02:51 +0000270 task = ro_task["tasks"][task_index]
271 task_id = task["task_id"]
272 net_vim_id = ro_task["vim_info"]["vim_id"]
sousaedu80135b92021-02-17 15:05:18 +0100273 ro_vim_item_update_ok = {
274 "vim_status": "DELETED",
275 "created": False,
276 "vim_details": "DELETED",
277 "vim_id": None,
278 }
279
tierno1d213f42020-04-24 14:02:51 +0000280 try:
281 if net_vim_id or ro_task["vim_info"]["created_items"]:
282 target_vim = self.my_vims[ro_task["target_id"]]
sousaedu80135b92021-02-17 15:05:18 +0100283 target_vim.delete_network(
284 net_vim_id, ro_task["vim_info"]["created_items"]
285 )
tierno1d213f42020-04-24 14:02:51 +0000286 except vimconn.VimConnNotFoundException:
287 ro_vim_item_update_ok["vim_details"] = "already deleted"
tierno1d213f42020-04-24 14:02:51 +0000288 except vimconn.VimConnException as e:
sousaedu80135b92021-02-17 15:05:18 +0100289 self.logger.error(
290 "ro_task={} vim={} del-net={}: {}".format(
291 ro_task["_id"], ro_task["target_id"], net_vim_id, e
292 )
293 )
294 ro_vim_item_update = {
295 "vim_status": "VIM_ERROR",
296 "vim_details": "Error while deleting: {}".format(e),
297 }
298
tierno1d213f42020-04-24 14:02:51 +0000299 return "FAILED", ro_vim_item_update
300
sousaedu80135b92021-02-17 15:05:18 +0100301 self.logger.debug(
302 "task={} {} del-net={} {}".format(
303 task_id,
304 ro_task["target_id"],
305 net_vim_id,
306 ro_vim_item_update_ok.get("vim_details", ""),
307 )
308 )
309
tierno1d213f42020-04-24 14:02:51 +0000310 return "DONE", ro_vim_item_update_ok
311
tierno70eeb182020-10-19 16:38:00 +0000312
313class VimInteractionVdu(VimInteractionBase):
sousaedu80135b92021-02-17 15:05:18 +0100314 max_retries_inject_ssh_key = 20 # 20 times
315 time_retries_inject_ssh_key = 30 # wevery 30 seconds
tierno70eeb182020-10-19 16:38:00 +0000316
317 def new(self, ro_task, task_index, task_depends):
tierno1d213f42020-04-24 14:02:51 +0000318 task = ro_task["tasks"][task_index]
319 task_id = task["task_id"]
320 created = False
321 created_items = {}
322 target_vim = self.my_vims[ro_task["target_id"]]
sousaedu80135b92021-02-17 15:05:18 +0100323
tierno1d213f42020-04-24 14:02:51 +0000324 try:
325 created = True
326 params = task["params"]
327 params_copy = deepcopy(params)
328 net_list = params_copy["net_list"]
sousaedu80135b92021-02-17 15:05:18 +0100329
tierno1d213f42020-04-24 14:02:51 +0000330 for net in net_list:
sousaedu80135b92021-02-17 15:05:18 +0100331 # change task_id into network_id
332 if "net_id" in net and net["net_id"].startswith("TASK-"):
tierno1d213f42020-04-24 14:02:51 +0000333 network_id = task_depends[net["net_id"]]
sousaedu80135b92021-02-17 15:05:18 +0100334
tierno1d213f42020-04-24 14:02:51 +0000335 if not network_id:
sousaedu80135b92021-02-17 15:05:18 +0100336 raise NsWorkerException(
337 "Cannot create VM because depends on a network not created or found "
338 "for {}".format(net["net_id"])
339 )
340
tierno1d213f42020-04-24 14:02:51 +0000341 net["net_id"] = network_id
sousaedu80135b92021-02-17 15:05:18 +0100342
tierno1d213f42020-04-24 14:02:51 +0000343 if params_copy["image_id"].startswith("TASK-"):
344 params_copy["image_id"] = task_depends[params_copy["image_id"]]
sousaedu80135b92021-02-17 15:05:18 +0100345
tierno1d213f42020-04-24 14:02:51 +0000346 if params_copy["flavor_id"].startswith("TASK-"):
347 params_copy["flavor_id"] = task_depends[params_copy["flavor_id"]]
348
349 vim_vm_id, created_items = target_vim.new_vminstance(**params_copy)
350 interfaces = [iface["vim_id"] for iface in params_copy["net_list"]]
351
sousaedu80135b92021-02-17 15:05:18 +0100352 ro_vim_item_update = {
353 "vim_id": vim_vm_id,
354 "vim_status": "BUILD",
355 "created": created,
356 "created_items": created_items,
357 "vim_details": None,
358 "interfaces_vim_ids": interfaces,
359 "interfaces": [],
360 }
tierno1d213f42020-04-24 14:02:51 +0000361 self.logger.debug(
sousaedu80135b92021-02-17 15:05:18 +0100362 "task={} {} new-vm={} created={}".format(
363 task_id, ro_task["target_id"], vim_vm_id, created
364 )
365 )
366
tierno1d213f42020-04-24 14:02:51 +0000367 return "BUILD", ro_vim_item_update
368 except (vimconn.VimConnException, NsWorkerException) as e:
sousaedu80135b92021-02-17 15:05:18 +0100369 self.logger.error(
370 "task={} {} new-vm: {}".format(task_id, ro_task["target_id"], e)
371 )
372 ro_vim_item_update = {
373 "vim_status": "VIM_ERROR",
374 "created": created,
375 "vim_details": str(e),
376 }
377
tierno1d213f42020-04-24 14:02:51 +0000378 return "FAILED", ro_vim_item_update
379
tierno70eeb182020-10-19 16:38:00 +0000380 def delete(self, ro_task, task_index):
tierno1d213f42020-04-24 14:02:51 +0000381 task = ro_task["tasks"][task_index]
382 task_id = task["task_id"]
383 vm_vim_id = ro_task["vim_info"]["vim_id"]
sousaedu80135b92021-02-17 15:05:18 +0100384 ro_vim_item_update_ok = {
385 "vim_status": "DELETED",
386 "created": False,
387 "vim_details": "DELETED",
388 "vim_id": None,
389 }
390
tierno1d213f42020-04-24 14:02:51 +0000391 try:
392 if vm_vim_id or ro_task["vim_info"]["created_items"]:
393 target_vim = self.my_vims[ro_task["target_id"]]
sousaedu80135b92021-02-17 15:05:18 +0100394 target_vim.delete_vminstance(
395 vm_vim_id, ro_task["vim_info"]["created_items"]
396 )
tierno1d213f42020-04-24 14:02:51 +0000397 except vimconn.VimConnNotFoundException:
398 ro_vim_item_update_ok["vim_details"] = "already deleted"
tierno1d213f42020-04-24 14:02:51 +0000399 except vimconn.VimConnException as e:
sousaedu80135b92021-02-17 15:05:18 +0100400 self.logger.error(
401 "ro_task={} vim={} del-vm={}: {}".format(
402 ro_task["_id"], ro_task["target_id"], vm_vim_id, e
403 )
404 )
405 ro_vim_item_update = {
406 "vim_status": "VIM_ERROR",
407 "vim_details": "Error while deleting: {}".format(e),
408 }
409
tierno1d213f42020-04-24 14:02:51 +0000410 return "FAILED", ro_vim_item_update
411
sousaedu80135b92021-02-17 15:05:18 +0100412 self.logger.debug(
413 "task={} {} del-vm={} {}".format(
414 task_id,
415 ro_task["target_id"],
416 vm_vim_id,
417 ro_vim_item_update_ok.get("vim_details", ""),
418 )
419 )
420
tierno1d213f42020-04-24 14:02:51 +0000421 return "DONE", ro_vim_item_update_ok
422
tierno70eeb182020-10-19 16:38:00 +0000423 def refresh(self, ro_task):
tierno1d213f42020-04-24 14:02:51 +0000424 """Call VIM to get vm status"""
425 ro_task_id = ro_task["_id"]
426 target_vim = self.my_vims[ro_task["target_id"]]
tierno1d213f42020-04-24 14:02:51 +0000427 vim_id = ro_task["vim_info"]["vim_id"]
sousaedu80135b92021-02-17 15:05:18 +0100428
tierno1d213f42020-04-24 14:02:51 +0000429 if not vim_id:
430 return None, None
sousaedu80135b92021-02-17 15:05:18 +0100431
tierno1d213f42020-04-24 14:02:51 +0000432 vm_to_refresh_list = [vim_id]
433 try:
434 vim_dict = target_vim.refresh_vms_status(vm_to_refresh_list)
435 vim_info = vim_dict[vim_id]
sousaedu80135b92021-02-17 15:05:18 +0100436
tierno1d213f42020-04-24 14:02:51 +0000437 if vim_info["status"] == "ACTIVE":
438 task_status = "DONE"
439 elif vim_info["status"] == "BUILD":
440 task_status = "BUILD"
441 else:
442 task_status = "FAILED"
sousaedu80135b92021-02-17 15:05:18 +0100443
tierno70eeb182020-10-19 16:38:00 +0000444 # try to load and parse vim_information
445 try:
446 vim_info_info = yaml.safe_load(vim_info["vim_info"])
447 if vim_info_info.get("name"):
448 vim_info["name"] = vim_info_info["name"]
449 except Exception:
450 pass
tierno1d213f42020-04-24 14:02:51 +0000451 except vimconn.VimConnException as e:
452 # Mark all tasks at VIM_ERROR status
sousaedu80135b92021-02-17 15:05:18 +0100453 self.logger.error(
454 "ro_task={} vim={} get-vm={}: {}".format(
455 ro_task_id, ro_task["target_id"], vim_id, e
456 )
457 )
tierno1d213f42020-04-24 14:02:51 +0000458 vim_info = {"status": "VIM_ERROR", "error_msg": str(e)}
459 task_status = "FAILED"
460
461 ro_vim_item_update = {}
sousaedu80135b92021-02-17 15:05:18 +0100462
tierno70eeb182020-10-19 16:38:00 +0000463 # Interfaces cannot be present if e.g. VM is not present, that is status=DELETED
tierno1d213f42020-04-24 14:02:51 +0000464 vim_interfaces = []
tierno70eeb182020-10-19 16:38:00 +0000465 if vim_info.get("interfaces"):
466 for vim_iface_id in ro_task["vim_info"]["interfaces_vim_ids"]:
sousaedu80135b92021-02-17 15:05:18 +0100467 iface = next(
468 (
469 iface
470 for iface in vim_info["interfaces"]
471 if vim_iface_id == iface["vim_interface_id"]
472 ),
473 None,
474 )
tierno70eeb182020-10-19 16:38:00 +0000475 # if iface:
476 # iface.pop("vim_info", None)
477 vim_interfaces.append(iface)
tierno1d213f42020-04-24 14:02:51 +0000478
sousaedu80135b92021-02-17 15:05:18 +0100479 task_create = next(
480 t
481 for t in ro_task["tasks"]
482 if t and t["action"] == "CREATE" and t["status"] != "FINISHED"
483 )
tierno70eeb182020-10-19 16:38:00 +0000484 if vim_interfaces and task_create.get("mgmt_vnf_interface") is not None:
sousaedu80135b92021-02-17 15:05:18 +0100485 vim_interfaces[task_create["mgmt_vnf_interface"]][
486 "mgmt_vnf_interface"
487 ] = True
488
489 mgmt_vdu_iface = task_create.get(
490 "mgmt_vdu_interface", task_create.get("mgmt_vnf_interface", 0)
491 )
tierno70eeb182020-10-19 16:38:00 +0000492 if vim_interfaces:
493 vim_interfaces[mgmt_vdu_iface]["mgmt_vdu_interface"] = True
tierno1d213f42020-04-24 14:02:51 +0000494
495 if ro_task["vim_info"]["interfaces"] != vim_interfaces:
496 ro_vim_item_update["interfaces"] = vim_interfaces
sousaedu80135b92021-02-17 15:05:18 +0100497
tierno1d213f42020-04-24 14:02:51 +0000498 if ro_task["vim_info"]["vim_status"] != vim_info["status"]:
499 ro_vim_item_update["vim_status"] = vim_info["status"]
sousaedu80135b92021-02-17 15:05:18 +0100500
tierno1d213f42020-04-24 14:02:51 +0000501 if ro_task["vim_info"]["vim_name"] != vim_info.get("name"):
502 ro_vim_item_update["vim_name"] = vim_info.get("name")
sousaedu80135b92021-02-17 15:05:18 +0100503
tierno1d213f42020-04-24 14:02:51 +0000504 if vim_info["status"] in ("ERROR", "VIM_ERROR"):
tierno70eeb182020-10-19 16:38:00 +0000505 if ro_task["vim_info"]["vim_details"] != vim_info.get("error_msg"):
506 ro_vim_item_update["vim_details"] = vim_info.get("error_msg")
tierno1d213f42020-04-24 14:02:51 +0000507 elif vim_info["status"] == "DELETED":
508 ro_vim_item_update["vim_id"] = None
509 ro_vim_item_update["vim_details"] = "Deleted externally"
510 else:
511 if ro_task["vim_info"]["vim_details"] != vim_info["vim_info"]:
512 ro_vim_item_update["vim_details"] = vim_info["vim_info"]
sousaedu80135b92021-02-17 15:05:18 +0100513
tierno1d213f42020-04-24 14:02:51 +0000514 if ro_vim_item_update:
sousaedu80135b92021-02-17 15:05:18 +0100515 self.logger.debug(
516 "ro_task={} {} get-vm={}: status={} {}".format(
517 ro_task_id,
518 ro_task["target_id"],
519 vim_id,
520 ro_vim_item_update.get("vim_status"),
521 ro_vim_item_update.get("vim_details")
522 if ro_vim_item_update.get("vim_status") != "ACTIVE"
523 else "",
524 )
525 )
526
tierno1d213f42020-04-24 14:02:51 +0000527 return task_status, ro_vim_item_update
528
tierno70eeb182020-10-19 16:38:00 +0000529 def exec(self, ro_task, task_index, task_depends):
tierno1d213f42020-04-24 14:02:51 +0000530 task = ro_task["tasks"][task_index]
531 task_id = task["task_id"]
532 target_vim = self.my_vims[ro_task["target_id"]]
tierno70eeb182020-10-19 16:38:00 +0000533 db_task_update = {"retries": 0}
534 retries = task.get("retries", 0)
sousaedu80135b92021-02-17 15:05:18 +0100535
tierno1d213f42020-04-24 14:02:51 +0000536 try:
537 params = task["params"]
538 params_copy = deepcopy(params)
sousaedu80135b92021-02-17 15:05:18 +0100539 params_copy["ro_key"] = self.db.decrypt(
540 params_copy.pop("private_key"),
541 params_copy.pop("schema_version"),
542 params_copy.pop("salt"),
543 )
tierno70eeb182020-10-19 16:38:00 +0000544 params_copy["ip_addr"] = params_copy.pop("ip_address")
tierno1d213f42020-04-24 14:02:51 +0000545 target_vim.inject_user_key(**params_copy)
546 self.logger.debug(
sousaedu80135b92021-02-17 15:05:18 +0100547 "task={} {} action-vm=inject_key".format(task_id, ro_task["target_id"])
548 )
549
550 return (
551 "DONE",
552 None,
553 db_task_update,
554 ) # params_copy["key"]
tierno1d213f42020-04-24 14:02:51 +0000555 except (vimconn.VimConnException, NsWorkerException) as e:
tierno70eeb182020-10-19 16:38:00 +0000556 retries += 1
sousaedu80135b92021-02-17 15:05:18 +0100557
tierno70eeb182020-10-19 16:38:00 +0000558 if retries < self.max_retries_inject_ssh_key:
sousaedu80135b92021-02-17 15:05:18 +0100559 return (
560 "BUILD",
561 None,
562 {
563 "retries": retries,
564 "next_retry": self.time_retries_inject_ssh_key,
565 },
566 )
567
568 self.logger.error(
569 "task={} {} inject-ssh-key: {}".format(task_id, ro_task["target_id"], e)
570 )
tierno1d213f42020-04-24 14:02:51 +0000571 ro_vim_item_update = {"vim_details": str(e)}
sousaedu80135b92021-02-17 15:05:18 +0100572
tierno70eeb182020-10-19 16:38:00 +0000573 return "FAILED", ro_vim_item_update, db_task_update
574
575
576class VimInteractionImage(VimInteractionBase):
tierno70eeb182020-10-19 16:38:00 +0000577 def new(self, ro_task, task_index, task_depends):
578 task = ro_task["tasks"][task_index]
579 task_id = task["task_id"]
580 created = False
581 created_items = {}
582 target_vim = self.my_vims[ro_task["target_id"]]
sousaedu80135b92021-02-17 15:05:18 +0100583
tierno70eeb182020-10-19 16:38:00 +0000584 try:
585 # FIND
586 if task.get("find_params"):
587 vim_images = target_vim.get_image_list(**task["find_params"])
sousaedu80135b92021-02-17 15:05:18 +0100588
tierno70eeb182020-10-19 16:38:00 +0000589 if not vim_images:
sousaedu80135b92021-02-17 15:05:18 +0100590 raise NsWorkerExceptionNotFound(
591 "Image not found with this criteria: '{}'".format(
592 task["find_params"]
593 )
594 )
tierno70eeb182020-10-19 16:38:00 +0000595 elif len(vim_images) > 1:
596 raise NsWorkerException(
sousaeduee6a6202021-05-11 13:22:37 +0200597 "More than one image found with this criteria: '{}'".format(
sousaedu80135b92021-02-17 15:05:18 +0100598 task["find_params"]
599 )
600 )
tierno70eeb182020-10-19 16:38:00 +0000601 else:
602 vim_image_id = vim_images[0]["id"]
603
sousaedu80135b92021-02-17 15:05:18 +0100604 ro_vim_item_update = {
605 "vim_id": vim_image_id,
606 "vim_status": "DONE",
607 "created": created,
608 "created_items": created_items,
609 "vim_details": None,
610 }
tierno70eeb182020-10-19 16:38:00 +0000611 self.logger.debug(
sousaedu80135b92021-02-17 15:05:18 +0100612 "task={} {} new-image={} created={}".format(
613 task_id, ro_task["target_id"], vim_image_id, created
614 )
615 )
616
tierno70eeb182020-10-19 16:38:00 +0000617 return "DONE", ro_vim_item_update
618 except (NsWorkerException, vimconn.VimConnException) as e:
sousaedu80135b92021-02-17 15:05:18 +0100619 self.logger.error(
620 "task={} {} new-image: {}".format(task_id, ro_task["target_id"], e)
621 )
622 ro_vim_item_update = {
623 "vim_status": "VIM_ERROR",
624 "created": created,
625 "vim_details": str(e),
626 }
627
tierno1d213f42020-04-24 14:02:51 +0000628 return "FAILED", ro_vim_item_update
629
tierno70eeb182020-10-19 16:38:00 +0000630
631class VimInteractionFlavor(VimInteractionBase):
tierno70eeb182020-10-19 16:38:00 +0000632 def delete(self, ro_task, task_index):
633 task = ro_task["tasks"][task_index]
634 task_id = task["task_id"]
635 flavor_vim_id = ro_task["vim_info"]["vim_id"]
sousaedu80135b92021-02-17 15:05:18 +0100636 ro_vim_item_update_ok = {
637 "vim_status": "DELETED",
638 "created": False,
639 "vim_details": "DELETED",
640 "vim_id": None,
641 }
642
tierno70eeb182020-10-19 16:38:00 +0000643 try:
644 if flavor_vim_id:
645 target_vim = self.my_vims[ro_task["target_id"]]
646 target_vim.delete_flavor(flavor_vim_id)
tierno70eeb182020-10-19 16:38:00 +0000647 except vimconn.VimConnNotFoundException:
648 ro_vim_item_update_ok["vim_details"] = "already deleted"
tierno70eeb182020-10-19 16:38:00 +0000649 except vimconn.VimConnException as e:
sousaedu80135b92021-02-17 15:05:18 +0100650 self.logger.error(
651 "ro_task={} vim={} del-flavor={}: {}".format(
652 ro_task["_id"], ro_task["target_id"], flavor_vim_id, e
653 )
654 )
655 ro_vim_item_update = {
656 "vim_status": "VIM_ERROR",
657 "vim_details": "Error while deleting: {}".format(e),
658 }
659
tierno70eeb182020-10-19 16:38:00 +0000660 return "FAILED", ro_vim_item_update
661
sousaedu80135b92021-02-17 15:05:18 +0100662 self.logger.debug(
663 "task={} {} del-flavor={} {}".format(
664 task_id,
665 ro_task["target_id"],
666 flavor_vim_id,
667 ro_vim_item_update_ok.get("vim_details", ""),
668 )
669 )
670
tierno70eeb182020-10-19 16:38:00 +0000671 return "DONE", ro_vim_item_update_ok
672
673 def new(self, ro_task, task_index, task_depends):
674 task = ro_task["tasks"][task_index]
675 task_id = task["task_id"]
676 created = False
677 created_items = {}
678 target_vim = self.my_vims[ro_task["target_id"]]
sousaedu80135b92021-02-17 15:05:18 +0100679
tierno70eeb182020-10-19 16:38:00 +0000680 try:
681 # FIND
682 vim_flavor_id = None
sousaedu80135b92021-02-17 15:05:18 +0100683
tierno70eeb182020-10-19 16:38:00 +0000684 if task.get("find_params"):
685 try:
686 flavor_data = task["find_params"]["flavor_data"]
687 vim_flavor_id = target_vim.get_flavor_id_from_data(flavor_data)
688 except vimconn.VimConnNotFoundException:
689 pass
690
691 if not vim_flavor_id and task.get("params"):
692 # CREATE
693 flavor_data = task["params"]["flavor_data"]
694 vim_flavor_id = target_vim.new_flavor(flavor_data)
695 created = True
696
sousaedu80135b92021-02-17 15:05:18 +0100697 ro_vim_item_update = {
698 "vim_id": vim_flavor_id,
699 "vim_status": "DONE",
700 "created": created,
701 "created_items": created_items,
702 "vim_details": None,
703 }
tierno70eeb182020-10-19 16:38:00 +0000704 self.logger.debug(
sousaedu80135b92021-02-17 15:05:18 +0100705 "task={} {} new-flavor={} created={}".format(
706 task_id, ro_task["target_id"], vim_flavor_id, created
707 )
708 )
709
tierno70eeb182020-10-19 16:38:00 +0000710 return "DONE", ro_vim_item_update
711 except (vimconn.VimConnException, NsWorkerException) as e:
sousaedu80135b92021-02-17 15:05:18 +0100712 self.logger.error(
713 "task={} vim={} new-flavor: {}".format(task_id, ro_task["target_id"], e)
714 )
715 ro_vim_item_update = {
716 "vim_status": "VIM_ERROR",
717 "created": created,
718 "vim_details": str(e),
719 }
720
tierno70eeb182020-10-19 16:38:00 +0000721 return "FAILED", ro_vim_item_update
722
723
724class VimInteractionSdnNet(VimInteractionBase):
tierno70eeb182020-10-19 16:38:00 +0000725 @staticmethod
726 def _match_pci(port_pci, mapping):
727 """
728 Check if port_pci matches with mapping
729 mapping can have brackets to indicate that several chars are accepted. e.g
730 pci '0000:af:10.1' matches with '0000:af:1[01].[1357]'
731 :param port_pci: text
732 :param mapping: text, can contain brackets to indicate several chars are available
733 :return: True if matches, False otherwise
734 """
735 if not port_pci or not mapping:
736 return False
737 if port_pci == mapping:
738 return True
739
740 mapping_index = 0
741 pci_index = 0
742 while True:
743 bracket_start = mapping.find("[", mapping_index)
sousaedu80135b92021-02-17 15:05:18 +0100744
tierno70eeb182020-10-19 16:38:00 +0000745 if bracket_start == -1:
746 break
sousaedu80135b92021-02-17 15:05:18 +0100747
tierno70eeb182020-10-19 16:38:00 +0000748 bracket_end = mapping.find("]", bracket_start)
749 if bracket_end == -1:
750 break
sousaedu80135b92021-02-17 15:05:18 +0100751
tierno70eeb182020-10-19 16:38:00 +0000752 length = bracket_start - mapping_index
sousaedu80135b92021-02-17 15:05:18 +0100753 if (
754 length
755 and port_pci[pci_index : pci_index + length]
756 != mapping[mapping_index:bracket_start]
757 ):
tierno70eeb182020-10-19 16:38:00 +0000758 return False
sousaedu80135b92021-02-17 15:05:18 +0100759
760 if (
761 port_pci[pci_index + length]
762 not in mapping[bracket_start + 1 : bracket_end]
763 ):
tierno70eeb182020-10-19 16:38:00 +0000764 return False
sousaedu80135b92021-02-17 15:05:18 +0100765
tierno70eeb182020-10-19 16:38:00 +0000766 pci_index += length + 1
767 mapping_index = bracket_end + 1
768
769 if port_pci[pci_index:] != mapping[mapping_index:]:
770 return False
sousaedu80135b92021-02-17 15:05:18 +0100771
tierno70eeb182020-10-19 16:38:00 +0000772 return True
773
774 def _get_interfaces(self, vlds_to_connect, vim_account_id):
775 """
776 :param vlds_to_connect: list with format vnfrs:<id>:vld.<vld_id> or nsrs:<id>:vld.<vld_id>
777 :param vim_account_id:
778 :return:
779 """
780 interfaces = []
sousaedu80135b92021-02-17 15:05:18 +0100781
tierno70eeb182020-10-19 16:38:00 +0000782 for vld in vlds_to_connect:
783 table, _, db_id = vld.partition(":")
784 db_id, _, vld = db_id.partition(":")
785 _, _, vld_id = vld.partition(".")
sousaedu80135b92021-02-17 15:05:18 +0100786
tierno70eeb182020-10-19 16:38:00 +0000787 if table == "vnfrs":
788 q_filter = {"vim-account-id": vim_account_id, "_id": db_id}
789 iface_key = "vnf-vld-id"
790 else: # table == "nsrs"
791 q_filter = {"vim-account-id": vim_account_id, "nsr-id-ref": db_id}
792 iface_key = "ns-vld-id"
sousaedu80135b92021-02-17 15:05:18 +0100793
tierno70eeb182020-10-19 16:38:00 +0000794 db_vnfrs = self.db.get_list("vnfrs", q_filter=q_filter)
sousaedu80135b92021-02-17 15:05:18 +0100795
tierno70eeb182020-10-19 16:38:00 +0000796 for db_vnfr in db_vnfrs:
797 for vdu_index, vdur in enumerate(db_vnfr.get("vdur", ())):
798 for iface_index, interface in enumerate(vdur["interfaces"]):
sousaedu80135b92021-02-17 15:05:18 +0100799 if interface.get(iface_key) == vld_id and interface.get(
800 "type"
801 ) in ("SR-IOV", "PCI-PASSTHROUGH"):
tierno70eeb182020-10-19 16:38:00 +0000802 # only SR-IOV o PT
803 interface_ = interface.copy()
sousaedu80135b92021-02-17 15:05:18 +0100804 interface_["id"] = "vnfrs:{}:vdu.{}.interfaces.{}".format(
805 db_vnfr["_id"], vdu_index, iface_index
806 )
807
tierno70eeb182020-10-19 16:38:00 +0000808 if vdur.get("status") == "ERROR":
809 interface_["status"] = "ERROR"
sousaedu80135b92021-02-17 15:05:18 +0100810
tierno70eeb182020-10-19 16:38:00 +0000811 interfaces.append(interface_)
sousaedu80135b92021-02-17 15:05:18 +0100812
tierno70eeb182020-10-19 16:38:00 +0000813 return interfaces
814
815 def refresh(self, ro_task):
816 # look for task create
sousaedu80135b92021-02-17 15:05:18 +0100817 task_create_index, _ = next(
818 i_t
819 for i_t in enumerate(ro_task["tasks"])
820 if i_t[1]
821 and i_t[1]["action"] == "CREATE"
822 and i_t[1]["status"] != "FINISHED"
823 )
tierno70eeb182020-10-19 16:38:00 +0000824
825 return self.new(ro_task, task_create_index, None)
826
827 def new(self, ro_task, task_index, task_depends):
828
829 task = ro_task["tasks"][task_index]
830 task_id = task["task_id"]
831 target_vim = self.my_vims[ro_task["target_id"]]
832
833 sdn_net_id = ro_task["vim_info"]["vim_id"]
834
835 created_items = ro_task["vim_info"].get("created_items")
836 connected_ports = ro_task["vim_info"].get("connected_ports", [])
837 new_connected_ports = []
838 last_update = ro_task["vim_info"].get("last_update", 0)
839 sdn_status = ro_task["vim_info"].get("vim_status", "BUILD") or "BUILD"
840 error_list = []
841 created = ro_task["vim_info"].get("created", False)
842
843 try:
tierno70eeb182020-10-19 16:38:00 +0000844 # CREATE
845 params = task["params"]
846 vlds_to_connect = params["vlds"]
847 associated_vim = params["target_vim"]
sousaedu80135b92021-02-17 15:05:18 +0100848 # external additional ports
849 additional_ports = params.get("sdn-ports") or ()
tierno70eeb182020-10-19 16:38:00 +0000850 _, _, vim_account_id = associated_vim.partition(":")
sousaedu80135b92021-02-17 15:05:18 +0100851
tierno70eeb182020-10-19 16:38:00 +0000852 if associated_vim:
853 # get associated VIM
854 if associated_vim not in self.db_vims:
sousaedu80135b92021-02-17 15:05:18 +0100855 self.db_vims[associated_vim] = self.db.get_one(
856 "vim_accounts", {"_id": vim_account_id}
857 )
858
tierno70eeb182020-10-19 16:38:00 +0000859 db_vim = self.db_vims[associated_vim]
860
861 # look for ports to connect
862 ports = self._get_interfaces(vlds_to_connect, vim_account_id)
863 # print(ports)
864
865 sdn_ports = []
866 pending_ports = error_ports = 0
867 vlan_used = None
868 sdn_need_update = False
sousaedu80135b92021-02-17 15:05:18 +0100869
tierno70eeb182020-10-19 16:38:00 +0000870 for port in ports:
871 vlan_used = port.get("vlan") or vlan_used
sousaedu80135b92021-02-17 15:05:18 +0100872
tierno70eeb182020-10-19 16:38:00 +0000873 # TODO. Do not connect if already done
874 if not port.get("compute_node") or not port.get("pci"):
875 if port.get("status") == "ERROR":
876 error_ports += 1
877 else:
878 pending_ports += 1
879 continue
sousaedu80135b92021-02-17 15:05:18 +0100880
tierno70eeb182020-10-19 16:38:00 +0000881 pmap = None
sousaedu80135b92021-02-17 15:05:18 +0100882 compute_node_mappings = next(
883 (
884 c
885 for c in db_vim["config"].get("sdn-port-mapping", ())
886 if c and c["compute_node"] == port["compute_node"]
887 ),
888 None,
889 )
890
tierno70eeb182020-10-19 16:38:00 +0000891 if compute_node_mappings:
892 # process port_mapping pci of type 0000:af:1[01].[1357]
sousaedu80135b92021-02-17 15:05:18 +0100893 pmap = next(
894 (
895 p
896 for p in compute_node_mappings["ports"]
897 if self._match_pci(port["pci"], p.get("pci"))
898 ),
899 None,
900 )
901
tierno70eeb182020-10-19 16:38:00 +0000902 if not pmap:
903 if not db_vim["config"].get("mapping_not_needed"):
sousaedu80135b92021-02-17 15:05:18 +0100904 error_list.append(
905 "Port mapping not found for compute_node={} pci={}".format(
906 port["compute_node"], port["pci"]
907 )
908 )
tierno70eeb182020-10-19 16:38:00 +0000909 continue
sousaedu80135b92021-02-17 15:05:18 +0100910
tierno70eeb182020-10-19 16:38:00 +0000911 pmap = {}
912
913 service_endpoint_id = "{}:{}".format(port["compute_node"], port["pci"])
914 new_port = {
sousaedu80135b92021-02-17 15:05:18 +0100915 "service_endpoint_id": pmap.get("service_endpoint_id")
916 or service_endpoint_id,
917 "service_endpoint_encapsulation_type": "dot1q"
918 if port["type"] == "SR-IOV"
919 else None,
tierno70eeb182020-10-19 16:38:00 +0000920 "service_endpoint_encapsulation_info": {
921 "vlan": port.get("vlan"),
lloretgalleg160fcad2021-02-19 10:57:50 +0000922 "mac": port.get("mac-address"),
sousaedu80135b92021-02-17 15:05:18 +0100923 "device_id": pmap.get("device_id") or port["compute_node"],
924 "device_interface_id": pmap.get("device_interface_id")
925 or port["pci"],
tierno70eeb182020-10-19 16:38:00 +0000926 "switch_dpid": pmap.get("switch_id") or pmap.get("switch_dpid"),
927 "switch_port": pmap.get("switch_port"),
928 "service_mapping_info": pmap.get("service_mapping_info"),
sousaedu80135b92021-02-17 15:05:18 +0100929 },
tierno70eeb182020-10-19 16:38:00 +0000930 }
931
932 # TODO
933 # if port["modified_at"] > last_update:
934 # sdn_need_update = True
935 new_connected_ports.append(port["id"]) # TODO
936 sdn_ports.append(new_port)
937
938 if error_ports:
sousaedu80135b92021-02-17 15:05:18 +0100939 error_list.append(
940 "{} interfaces have not been created as VDU is on ERROR status".format(
941 error_ports
942 )
943 )
tierno70eeb182020-10-19 16:38:00 +0000944
945 # connect external ports
946 for index, additional_port in enumerate(additional_ports):
sousaedu80135b92021-02-17 15:05:18 +0100947 additional_port_id = additional_port.get(
948 "service_endpoint_id"
949 ) or "external-{}".format(index)
950 sdn_ports.append(
951 {
952 "service_endpoint_id": additional_port_id,
953 "service_endpoint_encapsulation_type": additional_port.get(
954 "service_endpoint_encapsulation_type", "dot1q"
955 ),
956 "service_endpoint_encapsulation_info": {
957 "vlan": additional_port.get("vlan") or vlan_used,
958 "mac": additional_port.get("mac_address"),
959 "device_id": additional_port.get("device_id"),
960 "device_interface_id": additional_port.get(
961 "device_interface_id"
962 ),
963 "switch_dpid": additional_port.get("switch_dpid")
964 or additional_port.get("switch_id"),
965 "switch_port": additional_port.get("switch_port"),
966 "service_mapping_info": additional_port.get(
967 "service_mapping_info"
968 ),
969 },
970 }
971 )
tierno70eeb182020-10-19 16:38:00 +0000972 new_connected_ports.append(additional_port_id)
973 sdn_info = ""
sousaedu80135b92021-02-17 15:05:18 +0100974
tierno70eeb182020-10-19 16:38:00 +0000975 # if there are more ports to connect or they have been modified, call create/update
976 if error_list:
977 sdn_status = "ERROR"
978 sdn_info = "; ".join(error_list)
979 elif set(connected_ports) != set(new_connected_ports) or sdn_need_update:
980 last_update = time.time()
sousaedu80135b92021-02-17 15:05:18 +0100981
tierno70eeb182020-10-19 16:38:00 +0000982 if not sdn_net_id:
983 if len(sdn_ports) < 2:
984 sdn_status = "ACTIVE"
sousaedu80135b92021-02-17 15:05:18 +0100985
tierno70eeb182020-10-19 16:38:00 +0000986 if not pending_ports:
sousaedu80135b92021-02-17 15:05:18 +0100987 self.logger.debug(
988 "task={} {} new-sdn-net done, less than 2 ports".format(
989 task_id, ro_task["target_id"]
990 )
991 )
tierno70eeb182020-10-19 16:38:00 +0000992 else:
993 net_type = params.get("type") or "ELAN"
sousaedu80135b92021-02-17 15:05:18 +0100994 (
995 sdn_net_id,
996 created_items,
997 ) = target_vim.create_connectivity_service(net_type, sdn_ports)
tierno70eeb182020-10-19 16:38:00 +0000998 created = True
sousaedu80135b92021-02-17 15:05:18 +0100999 self.logger.debug(
1000 "task={} {} new-sdn-net={} created={}".format(
1001 task_id, ro_task["target_id"], sdn_net_id, created
1002 )
1003 )
tierno70eeb182020-10-19 16:38:00 +00001004 else:
1005 created_items = target_vim.edit_connectivity_service(
sousaedu80135b92021-02-17 15:05:18 +01001006 sdn_net_id, conn_info=created_items, connection_points=sdn_ports
1007 )
tierno70eeb182020-10-19 16:38:00 +00001008 created = True
sousaedu80135b92021-02-17 15:05:18 +01001009 self.logger.debug(
1010 "task={} {} update-sdn-net={} created={}".format(
1011 task_id, ro_task["target_id"], sdn_net_id, created
1012 )
1013 )
1014
tierno70eeb182020-10-19 16:38:00 +00001015 connected_ports = new_connected_ports
1016 elif sdn_net_id:
sousaedu80135b92021-02-17 15:05:18 +01001017 wim_status_dict = target_vim.get_connectivity_service_status(
1018 sdn_net_id, conn_info=created_items
1019 )
tierno70eeb182020-10-19 16:38:00 +00001020 sdn_status = wim_status_dict["sdn_status"]
sousaedu80135b92021-02-17 15:05:18 +01001021
tierno70eeb182020-10-19 16:38:00 +00001022 if wim_status_dict.get("sdn_info"):
1023 sdn_info = str(wim_status_dict.get("sdn_info")) or ""
sousaedu80135b92021-02-17 15:05:18 +01001024
tierno70eeb182020-10-19 16:38:00 +00001025 if wim_status_dict.get("error_msg"):
1026 sdn_info = wim_status_dict.get("error_msg") or ""
1027
1028 if pending_ports:
1029 if sdn_status != "ERROR":
1030 sdn_info = "Waiting for getting interfaces location from VIM. Obtained '{}' of {}".format(
sousaedu80135b92021-02-17 15:05:18 +01001031 len(ports) - pending_ports, len(ports)
1032 )
1033
tierno70eeb182020-10-19 16:38:00 +00001034 if sdn_status == "ACTIVE":
1035 sdn_status = "BUILD"
1036
sousaedu80135b92021-02-17 15:05:18 +01001037 ro_vim_item_update = {
1038 "vim_id": sdn_net_id,
1039 "vim_status": sdn_status,
1040 "created": created,
1041 "created_items": created_items,
1042 "connected_ports": connected_ports,
1043 "vim_details": sdn_info,
1044 "last_update": last_update,
1045 }
1046
tierno70eeb182020-10-19 16:38:00 +00001047 return sdn_status, ro_vim_item_update
1048 except Exception as e:
sousaedu80135b92021-02-17 15:05:18 +01001049 self.logger.error(
1050 "task={} vim={} new-net: {}".format(task_id, ro_task["target_id"], e),
1051 exc_info=not isinstance(
1052 e, (sdnconn.SdnConnectorError, vimconn.VimConnException)
1053 ),
1054 )
1055 ro_vim_item_update = {
1056 "vim_status": "VIM_ERROR",
1057 "created": created,
1058 "vim_details": str(e),
1059 }
1060
tierno70eeb182020-10-19 16:38:00 +00001061 return "FAILED", ro_vim_item_update
1062
1063 def delete(self, ro_task, task_index):
1064 task = ro_task["tasks"][task_index]
1065 task_id = task["task_id"]
1066 sdn_vim_id = ro_task["vim_info"].get("vim_id")
sousaedu80135b92021-02-17 15:05:18 +01001067 ro_vim_item_update_ok = {
1068 "vim_status": "DELETED",
1069 "created": False,
1070 "vim_details": "DELETED",
1071 "vim_id": None,
1072 }
1073
tierno70eeb182020-10-19 16:38:00 +00001074 try:
1075 if sdn_vim_id:
1076 target_vim = self.my_vims[ro_task["target_id"]]
sousaedu80135b92021-02-17 15:05:18 +01001077 target_vim.delete_connectivity_service(
1078 sdn_vim_id, ro_task["vim_info"].get("created_items")
1079 )
tierno70eeb182020-10-19 16:38:00 +00001080
1081 except Exception as e:
sousaedu80135b92021-02-17 15:05:18 +01001082 if (
1083 isinstance(e, sdnconn.SdnConnectorError)
1084 and e.http_code == HTTPStatus.NOT_FOUND.value
1085 ):
tierno70eeb182020-10-19 16:38:00 +00001086 ro_vim_item_update_ok["vim_details"] = "already deleted"
1087 else:
sousaedu80135b92021-02-17 15:05:18 +01001088 self.logger.error(
1089 "ro_task={} vim={} del-sdn-net={}: {}".format(
1090 ro_task["_id"], ro_task["target_id"], sdn_vim_id, e
1091 ),
1092 exc_info=not isinstance(
1093 e, (sdnconn.SdnConnectorError, vimconn.VimConnException)
1094 ),
1095 )
1096 ro_vim_item_update = {
1097 "vim_status": "VIM_ERROR",
1098 "vim_details": "Error while deleting: {}".format(e),
1099 }
1100
tierno70eeb182020-10-19 16:38:00 +00001101 return "FAILED", ro_vim_item_update
1102
sousaedu80135b92021-02-17 15:05:18 +01001103 self.logger.debug(
1104 "task={} {} del-sdn-net={} {}".format(
1105 task_id,
1106 ro_task["target_id"],
1107 sdn_vim_id,
1108 ro_vim_item_update_ok.get("vim_details", ""),
1109 )
1110 )
1111
tierno70eeb182020-10-19 16:38:00 +00001112 return "DONE", ro_vim_item_update_ok
1113
1114
1115class NsWorker(threading.Thread):
1116 REFRESH_BUILD = 5 # 5 seconds
1117 REFRESH_ACTIVE = 60 # 1 minute
1118 REFRESH_ERROR = 600
1119 REFRESH_IMAGE = 3600 * 10
1120 REFRESH_DELETE = 3600 * 10
tiernof1b640f2020-12-09 15:06:01 +00001121 QUEUE_SIZE = 100
tierno70eeb182020-10-19 16:38:00 +00001122 terminate = False
tierno70eeb182020-10-19 16:38:00 +00001123
1124 def __init__(self, worker_index, config, plugins, db):
1125 """
1126
1127 :param worker_index: thread index
1128 :param config: general configuration of RO, among others the process_id with the docker id where it runs
1129 :param plugins: global shared dict with the loaded plugins
1130 :param db: database class instance to use
1131 """
1132 threading.Thread.__init__(self)
1133 self.config = config
1134 self.plugins = plugins
1135 self.plugin_name = "unknown"
sousaedu80135b92021-02-17 15:05:18 +01001136 self.logger = logging.getLogger("ro.worker{}".format(worker_index))
tierno70eeb182020-10-19 16:38:00 +00001137 self.worker_index = worker_index
1138 self.task_queue = queue.Queue(self.QUEUE_SIZE)
sousaedu80135b92021-02-17 15:05:18 +01001139 # targetvim: vimplugin class
1140 self.my_vims = {}
1141 # targetvim: vim information from database
1142 self.db_vims = {}
1143 # targetvim list
1144 self.vim_targets = []
tierno70eeb182020-10-19 16:38:00 +00001145 self.my_id = config["process_id"] + ":" + str(worker_index)
1146 self.db = db
1147 self.item2class = {
1148 "net": VimInteractionNet(self.db, self.my_vims, self.db_vims, self.logger),
1149 "vdu": VimInteractionVdu(self.db, self.my_vims, self.db_vims, self.logger),
sousaedu80135b92021-02-17 15:05:18 +01001150 "image": VimInteractionImage(
1151 self.db, self.my_vims, self.db_vims, self.logger
1152 ),
1153 "flavor": VimInteractionFlavor(
1154 self.db, self.my_vims, self.db_vims, self.logger
1155 ),
1156 "sdn_net": VimInteractionSdnNet(
1157 self.db, self.my_vims, self.db_vims, self.logger
1158 ),
tierno70eeb182020-10-19 16:38:00 +00001159 }
1160 self.time_last_task_processed = None
sousaedu80135b92021-02-17 15:05:18 +01001161 # lists of tasks to delete because nsrs or vnfrs has been deleted from db
1162 self.tasks_to_delete = []
1163 # it is idle when there are not vim_targets associated
1164 self.idle = True
tiernof1b640f2020-12-09 15:06:01 +00001165 self.task_locked_time = config["global"]["task_locked_time"]
tierno70eeb182020-10-19 16:38:00 +00001166
1167 def insert_task(self, task):
1168 try:
1169 self.task_queue.put(task, False)
1170 return None
1171 except queue.Full:
1172 raise NsWorkerException("timeout inserting a task")
1173
1174 def terminate(self):
1175 self.insert_task("exit")
1176
1177 def del_task(self, task):
1178 with self.task_lock:
1179 if task["status"] == "SCHEDULED":
1180 task["status"] = "SUPERSEDED"
1181 return True
1182 else: # task["status"] == "processing"
1183 self.task_lock.release()
1184 return False
1185
1186 def _process_vim_config(self, target_id, db_vim):
1187 """
1188 Process vim config, creating vim configuration files as ca_cert
1189 :param target_id: vim/sdn/wim + id
1190 :param db_vim: Vim dictionary obtained from database
1191 :return: None. Modifies vim. Creates a folder target_id:worker_index and several files
1192 """
1193 if not db_vim.get("config"):
1194 return
sousaedu80135b92021-02-17 15:05:18 +01001195
tierno70eeb182020-10-19 16:38:00 +00001196 file_name = ""
sousaedu80135b92021-02-17 15:05:18 +01001197
tierno70eeb182020-10-19 16:38:00 +00001198 try:
1199 if db_vim["config"].get("ca_cert_content"):
1200 file_name = "{}:{}".format(target_id, self.worker_index)
sousaedu80135b92021-02-17 15:05:18 +01001201
tierno70eeb182020-10-19 16:38:00 +00001202 try:
1203 mkdir(file_name)
1204 except FileExistsError:
1205 pass
sousaedu80135b92021-02-17 15:05:18 +01001206
tierno70eeb182020-10-19 16:38:00 +00001207 file_name = file_name + "/ca_cert"
sousaedu80135b92021-02-17 15:05:18 +01001208
tierno70eeb182020-10-19 16:38:00 +00001209 with open(file_name, "w") as f:
1210 f.write(db_vim["config"]["ca_cert_content"])
1211 del db_vim["config"]["ca_cert_content"]
1212 db_vim["config"]["ca_cert"] = file_name
1213 except Exception as e:
sousaedu80135b92021-02-17 15:05:18 +01001214 raise NsWorkerException(
1215 "Error writing to file '{}': {}".format(file_name, e)
1216 )
tierno70eeb182020-10-19 16:38:00 +00001217
1218 def _load_plugin(self, name, type="vim"):
1219 # type can be vim or sdn
1220 if "rovim_dummy" not in self.plugins:
1221 self.plugins["rovim_dummy"] = VimDummyConnector
sousaedu80135b92021-02-17 15:05:18 +01001222
tierno70eeb182020-10-19 16:38:00 +00001223 if "rosdn_dummy" not in self.plugins:
1224 self.plugins["rosdn_dummy"] = SdnDummyConnector
sousaedu80135b92021-02-17 15:05:18 +01001225
tierno70eeb182020-10-19 16:38:00 +00001226 if name in self.plugins:
1227 return self.plugins[name]
sousaedu80135b92021-02-17 15:05:18 +01001228
tierno70eeb182020-10-19 16:38:00 +00001229 try:
sousaedubecd0832021-04-08 00:16:18 +02001230 for ep in entry_points(group="osm_ro{}.plugins".format(type), name=name):
1231 self.plugins[name] = ep.load()
tierno70eeb182020-10-19 16:38:00 +00001232 except Exception as e:
1233 raise NsWorkerException("Cannot load plugin osm_{}: {}".format(name, e))
sousaedu80135b92021-02-17 15:05:18 +01001234
tierno70eeb182020-10-19 16:38:00 +00001235 if name and name not in self.plugins:
sousaedu80135b92021-02-17 15:05:18 +01001236 raise NsWorkerException(
1237 "Plugin 'osm_{n}' has not been installed".format(n=name)
1238 )
1239
tierno70eeb182020-10-19 16:38:00 +00001240 return self.plugins[name]
1241
1242 def _unload_vim(self, target_id):
1243 """
1244 Unload a vim_account. Removes it from self db_vims dictionary, my_vims dictionary and vim_targets list
1245 :param target_id: Contains type:_id; where type can be 'vim', ...
1246 :return: None.
1247 """
1248 try:
tierno70eeb182020-10-19 16:38:00 +00001249 self.db_vims.pop(target_id, None)
1250 self.my_vims.pop(target_id, None)
sousaedu80135b92021-02-17 15:05:18 +01001251
tierno86153522020-12-06 18:27:16 +00001252 if target_id in self.vim_targets:
1253 self.vim_targets.remove(target_id)
sousaedu80135b92021-02-17 15:05:18 +01001254
tierno86153522020-12-06 18:27:16 +00001255 self.logger.info("Unloaded {}".format(target_id))
tierno70eeb182020-10-19 16:38:00 +00001256 rmtree("{}:{}".format(target_id, self.worker_index))
1257 except FileNotFoundError:
1258 pass # this is raised by rmtree if folder does not exist
1259 except Exception as e:
1260 self.logger.error("Cannot unload {}: {}".format(target_id, e))
1261
1262 def _check_vim(self, target_id):
1263 """
1264 Load a VIM/SDN/WIM (if not loaded) and check connectivity, updating database with ENABLE or ERROR
1265 :param target_id: Contains type:_id; type can be 'vim', 'sdn' or 'wim'
1266 :return: None.
1267 """
1268 target, _, _id = target_id.partition(":")
1269 now = time.time()
1270 update_dict = {}
1271 unset_dict = {}
1272 op_text = ""
1273 step = ""
tierno86153522020-12-06 18:27:16 +00001274 loaded = target_id in self.vim_targets
sousaedu80135b92021-02-17 15:05:18 +01001275 target_database = (
1276 "vim_accounts"
1277 if target == "vim"
1278 else "wim_accounts"
1279 if target == "wim"
1280 else "sdns"
1281 )
1282
tierno70eeb182020-10-19 16:38:00 +00001283 try:
1284 step = "Getting {} from db".format(target_id)
1285 db_vim = self.db.get_one(target_database, {"_id": _id})
sousaedu80135b92021-02-17 15:05:18 +01001286
1287 for op_index, operation in enumerate(
1288 db_vim["_admin"].get("operations", ())
1289 ):
tierno70eeb182020-10-19 16:38:00 +00001290 if operation["operationState"] != "PROCESSING":
1291 continue
sousaedu80135b92021-02-17 15:05:18 +01001292
tierno70eeb182020-10-19 16:38:00 +00001293 locked_at = operation.get("locked_at")
sousaedu80135b92021-02-17 15:05:18 +01001294
tiernof1b640f2020-12-09 15:06:01 +00001295 if locked_at is not None and locked_at >= now - self.task_locked_time:
tierno70eeb182020-10-19 16:38:00 +00001296 # some other thread is doing this operation
1297 return
sousaedu80135b92021-02-17 15:05:18 +01001298
tierno70eeb182020-10-19 16:38:00 +00001299 # lock
1300 op_text = "_admin.operations.{}.".format(op_index)
sousaedu80135b92021-02-17 15:05:18 +01001301
1302 if not self.db.set_one(
1303 target_database,
1304 q_filter={
1305 "_id": _id,
1306 op_text + "operationState": "PROCESSING",
1307 op_text + "locked_at": locked_at,
1308 },
1309 update_dict={
1310 op_text + "locked_at": now,
1311 "admin.current_operation": op_index,
1312 },
1313 fail_on_empty=False,
1314 ):
tierno70eeb182020-10-19 16:38:00 +00001315 return
sousaedu80135b92021-02-17 15:05:18 +01001316
tierno70eeb182020-10-19 16:38:00 +00001317 unset_dict[op_text + "locked_at"] = None
1318 unset_dict["current_operation"] = None
1319 step = "Loading " + target_id
1320 error_text = self._load_vim(target_id)
sousaedu80135b92021-02-17 15:05:18 +01001321
tierno70eeb182020-10-19 16:38:00 +00001322 if not error_text:
1323 step = "Checking connectivity"
sousaedu80135b92021-02-17 15:05:18 +01001324
1325 if target == "vim":
tierno70eeb182020-10-19 16:38:00 +00001326 self.my_vims[target_id].check_vim_connectivity()
1327 else:
1328 self.my_vims[target_id].check_credentials()
sousaedu80135b92021-02-17 15:05:18 +01001329
tierno70eeb182020-10-19 16:38:00 +00001330 update_dict["_admin.operationalState"] = "ENABLED"
1331 update_dict["_admin.detailed-status"] = ""
1332 unset_dict[op_text + "detailed-status"] = None
1333 update_dict[op_text + "operationState"] = "COMPLETED"
sousaedu80135b92021-02-17 15:05:18 +01001334
tierno70eeb182020-10-19 16:38:00 +00001335 return
1336
1337 except Exception as e:
1338 error_text = "{}: {}".format(step, e)
1339 self.logger.error("{} for {}: {}".format(step, target_id, e))
1340
1341 finally:
1342 if update_dict or unset_dict:
1343 if error_text:
1344 update_dict[op_text + "operationState"] = "FAILED"
1345 update_dict[op_text + "detailed-status"] = error_text
1346 unset_dict.pop(op_text + "detailed-status", None)
1347 update_dict["_admin.operationalState"] = "ERROR"
1348 update_dict["_admin.detailed-status"] = error_text
sousaedu80135b92021-02-17 15:05:18 +01001349
tierno70eeb182020-10-19 16:38:00 +00001350 if op_text:
1351 update_dict[op_text + "statusEnteredTime"] = now
sousaedu80135b92021-02-17 15:05:18 +01001352
1353 self.db.set_one(
1354 target_database,
1355 q_filter={"_id": _id},
1356 update_dict=update_dict,
1357 unset=unset_dict,
1358 fail_on_empty=False,
1359 )
1360
tierno70eeb182020-10-19 16:38:00 +00001361 if not loaded:
1362 self._unload_vim(target_id)
1363
1364 def _reload_vim(self, target_id):
1365 if target_id in self.vim_targets:
1366 self._load_vim(target_id)
1367 else:
1368 # if the vim is not loaded, but database information of VIM is cached at self.db_vims,
1369 # just remove it to force load again next time it is needed
1370 self.db_vims.pop(target_id, None)
1371
1372 def _load_vim(self, target_id):
1373 """
1374 Load or reload a vim_account, sdn_controller or wim_account.
1375 Read content from database, load the plugin if not loaded.
1376 In case of error loading the plugin, it load a failing VIM_connector
1377 It fills self db_vims dictionary, my_vims dictionary and vim_targets list
1378 :param target_id: Contains type:_id; where type can be 'vim', ...
1379 :return: None if ok, descriptive text if error
1380 """
1381 target, _, _id = target_id.partition(":")
sousaedu80135b92021-02-17 15:05:18 +01001382 target_database = (
1383 "vim_accounts"
1384 if target == "vim"
1385 else "wim_accounts"
1386 if target == "wim"
1387 else "sdns"
1388 )
tierno70eeb182020-10-19 16:38:00 +00001389 plugin_name = ""
1390 vim = None
sousaedu80135b92021-02-17 15:05:18 +01001391
tierno70eeb182020-10-19 16:38:00 +00001392 try:
1393 step = "Getting {}={} from db".format(target, _id)
1394 # TODO process for wim, sdnc, ...
1395 vim = self.db.get_one(target_database, {"_id": _id})
1396
1397 # if deep_get(vim, "config", "sdn-controller"):
1398 # step = "Getting sdn-controller-id='{}' from db".format(vim["config"]["sdn-controller"])
1399 # db_sdn = self.db.get_one("sdns", {"_id": vim["config"]["sdn-controller"]})
1400
1401 step = "Decrypting password"
1402 schema_version = vim.get("schema_version")
sousaedu80135b92021-02-17 15:05:18 +01001403 self.db.encrypt_decrypt_fields(
1404 vim,
1405 "decrypt",
1406 fields=("password", "secret"),
1407 schema_version=schema_version,
1408 salt=_id,
1409 )
tierno70eeb182020-10-19 16:38:00 +00001410 self._process_vim_config(target_id, vim)
sousaedu80135b92021-02-17 15:05:18 +01001411
tierno70eeb182020-10-19 16:38:00 +00001412 if target == "vim":
1413 plugin_name = "rovim_" + vim["vim_type"]
1414 step = "Loading plugin '{}'".format(plugin_name)
1415 vim_module_conn = self._load_plugin(plugin_name)
1416 step = "Loading {}'".format(target_id)
1417 self.my_vims[target_id] = vim_module_conn(
sousaedu80135b92021-02-17 15:05:18 +01001418 uuid=vim["_id"],
1419 name=vim["name"],
1420 tenant_id=vim.get("vim_tenant_id"),
1421 tenant_name=vim.get("vim_tenant_name"),
1422 url=vim["vim_url"],
1423 url_admin=None,
1424 user=vim["vim_user"],
1425 passwd=vim["vim_password"],
1426 config=vim.get("config") or {},
1427 persistent_info={},
tierno70eeb182020-10-19 16:38:00 +00001428 )
1429 else: # sdn
1430 plugin_name = "rosdn_" + vim["type"]
1431 step = "Loading plugin '{}'".format(plugin_name)
1432 vim_module_conn = self._load_plugin(plugin_name, "sdn")
1433 step = "Loading {}'".format(target_id)
1434 wim = deepcopy(vim)
1435 wim_config = wim.pop("config", {}) or {}
1436 wim["uuid"] = wim["_id"]
1437 wim["wim_url"] = wim["url"]
sousaedu80135b92021-02-17 15:05:18 +01001438
tierno70eeb182020-10-19 16:38:00 +00001439 if wim.get("dpid"):
1440 wim_config["dpid"] = wim.pop("dpid")
sousaedu80135b92021-02-17 15:05:18 +01001441
tierno70eeb182020-10-19 16:38:00 +00001442 if wim.get("switch_id"):
1443 wim_config["switch_id"] = wim.pop("switch_id")
sousaedu80135b92021-02-17 15:05:18 +01001444
1445 # wim, wim_account, config
1446 self.my_vims[target_id] = vim_module_conn(wim, wim, wim_config)
tierno70eeb182020-10-19 16:38:00 +00001447 self.db_vims[target_id] = vim
1448 self.error_status = None
sousaedu80135b92021-02-17 15:05:18 +01001449
1450 self.logger.info(
1451 "Connector loaded for {}, plugin={}".format(target_id, plugin_name)
1452 )
tierno70eeb182020-10-19 16:38:00 +00001453 except Exception as e:
sousaedu80135b92021-02-17 15:05:18 +01001454 self.logger.error(
1455 "Cannot load {} plugin={}: {} {}".format(
1456 target_id, plugin_name, step, e
1457 )
1458 )
1459
tierno70eeb182020-10-19 16:38:00 +00001460 self.db_vims[target_id] = vim or {}
1461 self.db_vims[target_id] = FailingConnector(str(e))
1462 error_status = "{} Error: {}".format(step, e)
sousaedu80135b92021-02-17 15:05:18 +01001463
tierno70eeb182020-10-19 16:38:00 +00001464 return error_status
1465 finally:
1466 if target_id not in self.vim_targets:
1467 self.vim_targets.append(target_id)
1468
1469 def _get_db_task(self):
1470 """
1471 Read actions from database and reload them at memory. Fill self.refresh_list, pending_list, vim_actions
1472 :return: None
1473 """
1474 now = time.time()
sousaedu80135b92021-02-17 15:05:18 +01001475
tierno70eeb182020-10-19 16:38:00 +00001476 if not self.time_last_task_processed:
1477 self.time_last_task_processed = now
sousaedu80135b92021-02-17 15:05:18 +01001478
tierno70eeb182020-10-19 16:38:00 +00001479 try:
1480 while True:
1481 locked = self.db.set_one(
1482 "ro_tasks",
sousaedu80135b92021-02-17 15:05:18 +01001483 q_filter={
1484 "target_id": self.vim_targets,
1485 "tasks.status": ["SCHEDULED", "BUILD", "DONE", "FAILED"],
1486 "locked_at.lt": now - self.task_locked_time,
1487 "to_check_at.lt": self.time_last_task_processed,
1488 },
tierno70eeb182020-10-19 16:38:00 +00001489 update_dict={"locked_by": self.my_id, "locked_at": now},
sousaedu80135b92021-02-17 15:05:18 +01001490 fail_on_empty=False,
1491 )
1492
tierno70eeb182020-10-19 16:38:00 +00001493 if locked:
1494 # read and return
1495 ro_task = self.db.get_one(
1496 "ro_tasks",
sousaedu80135b92021-02-17 15:05:18 +01001497 q_filter={
1498 "target_id": self.vim_targets,
1499 "tasks.status": ["SCHEDULED", "BUILD", "DONE", "FAILED"],
1500 "locked_at": now,
1501 },
1502 )
tierno70eeb182020-10-19 16:38:00 +00001503 return ro_task
sousaedu80135b92021-02-17 15:05:18 +01001504
tierno70eeb182020-10-19 16:38:00 +00001505 if self.time_last_task_processed == now:
1506 self.time_last_task_processed = None
1507 return None
1508 else:
1509 self.time_last_task_processed = now
1510 # self.time_last_task_processed = min(self.time_last_task_processed + 1000, now)
1511
1512 except DbException as e:
1513 self.logger.error("Database exception at _get_db_task: {}".format(e))
1514 except Exception as e:
sousaedu80135b92021-02-17 15:05:18 +01001515 self.logger.critical(
1516 "Unexpected exception at _get_db_task: {}".format(e), exc_info=True
1517 )
1518
tierno70eeb182020-10-19 16:38:00 +00001519 return None
1520
1521 def _delete_task(self, ro_task, task_index, task_depends, db_update):
1522 """
1523 Determine if this task need to be done or superseded
1524 :return: None
1525 """
1526 my_task = ro_task["tasks"][task_index]
1527 task_id = my_task["task_id"]
sousaedu80135b92021-02-17 15:05:18 +01001528 needed_delete = ro_task["vim_info"]["created"] or ro_task["vim_info"].get(
1529 "created_items", False
1530 )
1531
tierno70eeb182020-10-19 16:38:00 +00001532 if my_task["status"] == "FAILED":
1533 return None, None # TODO need to be retry??
sousaedu80135b92021-02-17 15:05:18 +01001534
tierno70eeb182020-10-19 16:38:00 +00001535 try:
1536 for index, task in enumerate(ro_task["tasks"]):
1537 if index == task_index or not task:
1538 continue # own task
sousaedu80135b92021-02-17 15:05:18 +01001539
1540 if (
1541 my_task["target_record"] == task["target_record"]
1542 and task["action"] == "CREATE"
1543 ):
tierno70eeb182020-10-19 16:38:00 +00001544 # set to finished
sousaedu80135b92021-02-17 15:05:18 +01001545 db_update["tasks.{}.status".format(index)] = task[
1546 "status"
1547 ] = "FINISHED"
1548 elif task["action"] == "CREATE" and task["status"] not in (
1549 "FINISHED",
1550 "SUPERSEDED",
1551 ):
tierno70eeb182020-10-19 16:38:00 +00001552 needed_delete = False
sousaedu80135b92021-02-17 15:05:18 +01001553
tierno70eeb182020-10-19 16:38:00 +00001554 if needed_delete:
1555 return self.item2class[my_task["item"]].delete(ro_task, task_index)
1556 else:
1557 return "SUPERSEDED", None
1558 except Exception as e:
1559 if not isinstance(e, NsWorkerException):
sousaedu80135b92021-02-17 15:05:18 +01001560 self.logger.critical(
1561 "Unexpected exception at _delete_task task={}: {}".format(
1562 task_id, e
1563 ),
1564 exc_info=True,
1565 )
1566
tierno70eeb182020-10-19 16:38:00 +00001567 return "FAILED", {"vim_status": "VIM_ERROR", "vim_details": str(e)}
1568
1569 def _create_task(self, ro_task, task_index, task_depends, db_update):
1570 """
1571 Determine if this task need to create something at VIM
1572 :return: None
1573 """
1574 my_task = ro_task["tasks"][task_index]
1575 task_id = my_task["task_id"]
1576 task_status = None
sousaedu80135b92021-02-17 15:05:18 +01001577
tierno70eeb182020-10-19 16:38:00 +00001578 if my_task["status"] == "FAILED":
1579 return None, None # TODO need to be retry??
1580 elif my_task["status"] == "SCHEDULED":
1581 # check if already created by another task
1582 for index, task in enumerate(ro_task["tasks"]):
1583 if index == task_index or not task:
1584 continue # own task
sousaedu80135b92021-02-17 15:05:18 +01001585
1586 if task["action"] == "CREATE" and task["status"] not in (
1587 "SCHEDULED",
1588 "FINISHED",
1589 "SUPERSEDED",
1590 ):
tierno70eeb182020-10-19 16:38:00 +00001591 return task["status"], "COPY_VIM_INFO"
1592
1593 try:
1594 task_status, ro_vim_item_update = self.item2class[my_task["item"]].new(
sousaedu80135b92021-02-17 15:05:18 +01001595 ro_task, task_index, task_depends
1596 )
tierno70eeb182020-10-19 16:38:00 +00001597 # TODO update other CREATE tasks
1598 except Exception as e:
1599 if not isinstance(e, NsWorkerException):
sousaedu80135b92021-02-17 15:05:18 +01001600 self.logger.error(
1601 "Error executing task={}: {}".format(task_id, e), exc_info=True
1602 )
1603
tierno70eeb182020-10-19 16:38:00 +00001604 task_status = "FAILED"
1605 ro_vim_item_update = {"vim_status": "VIM_ERROR", "vim_details": str(e)}
1606 # TODO update ro_vim_item_update
sousaedu80135b92021-02-17 15:05:18 +01001607
tierno70eeb182020-10-19 16:38:00 +00001608 return task_status, ro_vim_item_update
1609 else:
1610 return None, None
1611
1612 def _get_dependency(self, task_id, ro_task=None, target_id=None):
1613 """
1614 Look for dependency task
1615 :param task_id: Can be one of
1616 1. target_vim+blank+task.target_record_id: "(vim|sdn|wim):<id> (vnfrs|nsrs):(vld|vdu|flavor|image).<id>"
1617 2. task.target_record_id: "(vnfrs|nsrs):(vld|vdu|flavor|image).<id>"
1618 3. task.task_id: "<action_id>:number"
1619 :param ro_task:
1620 :param target_id:
1621 :return: database ro_task plus index of task
1622 """
sousaedu80135b92021-02-17 15:05:18 +01001623 if (
1624 task_id.startswith("vim:")
1625 or task_id.startswith("sdn:")
1626 or task_id.startswith("wim:")
1627 ):
tierno70eeb182020-10-19 16:38:00 +00001628 target_id, _, task_id = task_id.partition(" ")
1629
1630 if task_id.startswith("nsrs:") or task_id.startswith("vnfrs:"):
1631 ro_task_dependency = self.db.get_one(
1632 "ro_tasks",
sousaedu80135b92021-02-17 15:05:18 +01001633 q_filter={"target_id": target_id, "tasks.target_record_id": task_id},
1634 fail_on_empty=False,
1635 )
1636
tierno70eeb182020-10-19 16:38:00 +00001637 if ro_task_dependency:
1638 for task_index, task in enumerate(ro_task_dependency["tasks"]):
1639 if task["target_record_id"] == task_id:
1640 return ro_task_dependency, task_index
1641
1642 else:
1643 if ro_task:
1644 for task_index, task in enumerate(ro_task["tasks"]):
1645 if task and task["task_id"] == task_id:
1646 return ro_task, task_index
sousaedu80135b92021-02-17 15:05:18 +01001647
tierno70eeb182020-10-19 16:38:00 +00001648 ro_task_dependency = self.db.get_one(
1649 "ro_tasks",
sousaedu80135b92021-02-17 15:05:18 +01001650 q_filter={
1651 "tasks.ANYINDEX.task_id": task_id,
1652 "tasks.ANYINDEX.target_record.ne": None,
1653 },
1654 fail_on_empty=False,
1655 )
1656
tierno70eeb182020-10-19 16:38:00 +00001657 if ro_task_dependency:
1658 for task_index, task in ro_task_dependency["tasks"]:
1659 if task["task_id"] == task_id:
1660 return ro_task_dependency, task_index
1661 raise NsWorkerException("Cannot get depending task {}".format(task_id))
1662
1663 def _process_pending_tasks(self, ro_task):
1664 ro_task_id = ro_task["_id"]
1665 now = time.time()
sousaedu80135b92021-02-17 15:05:18 +01001666 # one day
1667 next_check_at = now + (24 * 60 * 60)
tierno70eeb182020-10-19 16:38:00 +00001668 db_ro_task_update = {}
1669
1670 def _update_refresh(new_status):
1671 # compute next_refresh
1672 nonlocal task
1673 nonlocal next_check_at
1674 nonlocal db_ro_task_update
1675 nonlocal ro_task
1676
1677 next_refresh = time.time()
sousaedu80135b92021-02-17 15:05:18 +01001678
tierno70eeb182020-10-19 16:38:00 +00001679 if task["item"] in ("image", "flavor"):
1680 next_refresh += self.REFRESH_IMAGE
1681 elif new_status == "BUILD":
1682 next_refresh += self.REFRESH_BUILD
1683 elif new_status == "DONE":
1684 next_refresh += self.REFRESH_ACTIVE
1685 else:
1686 next_refresh += self.REFRESH_ERROR
sousaedu80135b92021-02-17 15:05:18 +01001687
tierno70eeb182020-10-19 16:38:00 +00001688 next_check_at = min(next_check_at, next_refresh)
1689 db_ro_task_update["vim_info.refresh_at"] = next_refresh
1690 ro_task["vim_info"]["refresh_at"] = next_refresh
1691
1692 try:
tiernof1b640f2020-12-09 15:06:01 +00001693 # 0: get task_status_create
1694 lock_object = None
tierno70eeb182020-10-19 16:38:00 +00001695 task_status_create = None
sousaedu80135b92021-02-17 15:05:18 +01001696 task_create = next(
1697 (
1698 t
1699 for t in ro_task["tasks"]
1700 if t
1701 and t["action"] == "CREATE"
1702 and t["status"] in ("BUILD", "DONE")
1703 ),
1704 None,
1705 )
1706
tierno70eeb182020-10-19 16:38:00 +00001707 if task_create:
1708 task_status_create = task_create["status"]
sousaedu80135b92021-02-17 15:05:18 +01001709
tiernof1b640f2020-12-09 15:06:01 +00001710 # 1: look for tasks in status SCHEDULED, or in status CREATE if action is DONE or BUILD
tierno70eeb182020-10-19 16:38:00 +00001711 for task_action in ("DELETE", "CREATE", "EXEC"):
1712 db_vim_update = None
1713 new_status = None
sousaedu80135b92021-02-17 15:05:18 +01001714
tierno70eeb182020-10-19 16:38:00 +00001715 for task_index, task in enumerate(ro_task["tasks"]):
1716 if not task:
1717 continue # task deleted
sousaedu80135b92021-02-17 15:05:18 +01001718
tierno55fa0bb2020-12-08 23:11:53 +00001719 task_depends = {}
tierno70eeb182020-10-19 16:38:00 +00001720 target_update = None
sousaedu80135b92021-02-17 15:05:18 +01001721
1722 if (
1723 (
1724 task_action in ("DELETE", "EXEC")
1725 and task["status"] not in ("SCHEDULED", "BUILD")
1726 )
1727 or task["action"] != task_action
1728 or (
1729 task_action == "CREATE"
1730 and task["status"] in ("FINISHED", "SUPERSEDED")
1731 )
1732 ):
tierno70eeb182020-10-19 16:38:00 +00001733 continue
sousaedu80135b92021-02-17 15:05:18 +01001734
tierno70eeb182020-10-19 16:38:00 +00001735 task_path = "tasks.{}.status".format(task_index)
1736 try:
1737 db_vim_info_update = None
sousaedu80135b92021-02-17 15:05:18 +01001738
tierno70eeb182020-10-19 16:38:00 +00001739 if task["status"] == "SCHEDULED":
tierno70eeb182020-10-19 16:38:00 +00001740 # check if tasks that this depends on have been completed
1741 dependency_not_completed = False
sousaedu80135b92021-02-17 15:05:18 +01001742
1743 for dependency_task_id in task.get("depends_on") or ():
1744 (
1745 dependency_ro_task,
1746 dependency_task_index,
1747 ) = self._get_dependency(
1748 dependency_task_id, target_id=ro_task["target_id"]
1749 )
1750 dependency_task = dependency_ro_task["tasks"][
1751 dependency_task_index
1752 ]
1753
tierno70eeb182020-10-19 16:38:00 +00001754 if dependency_task["status"] == "SCHEDULED":
1755 dependency_not_completed = True
sousaedu80135b92021-02-17 15:05:18 +01001756 next_check_at = min(
1757 next_check_at, dependency_ro_task["to_check_at"]
1758 )
lloretgalleg88486222021-02-19 12:06:52 +00001759 # must allow dependent task to be processed first
1760 # to do this set time after last_task_processed
1761 next_check_at = max(
1762 self.time_last_task_processed, next_check_at
1763 )
tierno70eeb182020-10-19 16:38:00 +00001764 break
1765 elif dependency_task["status"] == "FAILED":
1766 error_text = "Cannot {} {} because depends on failed {} {} id={}): {}".format(
sousaedu80135b92021-02-17 15:05:18 +01001767 task["action"],
1768 task["item"],
1769 dependency_task["action"],
1770 dependency_task["item"],
1771 dependency_task_id,
1772 dependency_ro_task["vim_info"].get(
1773 "vim_details"
1774 ),
1775 )
1776 self.logger.error(
1777 "task={} {}".format(task["task_id"], error_text)
1778 )
tierno70eeb182020-10-19 16:38:00 +00001779 raise NsWorkerException(error_text)
1780
sousaedu80135b92021-02-17 15:05:18 +01001781 task_depends[dependency_task_id] = dependency_ro_task[
1782 "vim_info"
1783 ]["vim_id"]
1784 task_depends[
1785 "TASK-{}".format(dependency_task_id)
1786 ] = dependency_ro_task["vim_info"]["vim_id"]
1787
tierno70eeb182020-10-19 16:38:00 +00001788 if dependency_not_completed:
1789 # TODO set at vim_info.vim_details that it is waiting
1790 continue
sousaedu80135b92021-02-17 15:05:18 +01001791
tiernof1b640f2020-12-09 15:06:01 +00001792 # before calling VIM-plugin as it can take more than task_locked_time, insert to LockRenew
1793 # the task of renew this locking. It will update database locket_at periodically
1794 if not lock_object:
sousaedu80135b92021-02-17 15:05:18 +01001795 lock_object = LockRenew.add_lock_object(
1796 "ro_tasks", ro_task, self
1797 )
1798
tierno70eeb182020-10-19 16:38:00 +00001799 if task["action"] == "DELETE":
sousaedu80135b92021-02-17 15:05:18 +01001800 (new_status, db_vim_info_update,) = self._delete_task(
1801 ro_task, task_index, task_depends, db_ro_task_update
1802 )
1803 new_status = (
1804 "FINISHED" if new_status == "DONE" else new_status
1805 )
tierno70eeb182020-10-19 16:38:00 +00001806 # ^with FINISHED instead of DONE it will not be refreshing
sousaedu80135b92021-02-17 15:05:18 +01001807
tierno70eeb182020-10-19 16:38:00 +00001808 if new_status in ("FINISHED", "SUPERSEDED"):
1809 target_update = "DELETE"
1810 elif task["action"] == "EXEC":
sousaedu80135b92021-02-17 15:05:18 +01001811 (
1812 new_status,
1813 db_vim_info_update,
1814 db_task_update,
1815 ) = self.item2class[task["item"]].exec(
1816 ro_task, task_index, task_depends
1817 )
1818 new_status = (
1819 "FINISHED" if new_status == "DONE" else new_status
1820 )
tierno70eeb182020-10-19 16:38:00 +00001821 # ^with FINISHED instead of DONE it will not be refreshing
sousaedu80135b92021-02-17 15:05:18 +01001822
tierno70eeb182020-10-19 16:38:00 +00001823 if db_task_update:
1824 # load into database the modified db_task_update "retries" and "next_retry"
1825 if db_task_update.get("retries"):
sousaedu80135b92021-02-17 15:05:18 +01001826 db_ro_task_update[
1827 "tasks.{}.retries".format(task_index)
1828 ] = db_task_update["retries"]
1829
1830 next_check_at = time.time() + db_task_update.get(
1831 "next_retry", 60
1832 )
tierno70eeb182020-10-19 16:38:00 +00001833 target_update = None
1834 elif task["action"] == "CREATE":
1835 if task["status"] == "SCHEDULED":
1836 if task_status_create:
1837 new_status = task_status_create
1838 target_update = "COPY_VIM_INFO"
1839 else:
sousaedu80135b92021-02-17 15:05:18 +01001840 new_status, db_vim_info_update = self.item2class[
1841 task["item"]
1842 ].new(ro_task, task_index, task_depends)
tierno70eeb182020-10-19 16:38:00 +00001843 # self._create_task(ro_task, task_index, task_depends, db_ro_task_update)
1844 _update_refresh(new_status)
1845 else:
sousaedu80135b92021-02-17 15:05:18 +01001846 if (
1847 ro_task["vim_info"]["refresh_at"]
1848 and now > ro_task["vim_info"]["refresh_at"]
1849 ):
1850 new_status, db_vim_info_update = self.item2class[
1851 task["item"]
1852 ].refresh(ro_task)
tierno70eeb182020-10-19 16:38:00 +00001853 _update_refresh(new_status)
sousaedu80135b92021-02-17 15:05:18 +01001854
tierno70eeb182020-10-19 16:38:00 +00001855 except Exception as e:
1856 new_status = "FAILED"
sousaedu80135b92021-02-17 15:05:18 +01001857 db_vim_info_update = {
1858 "vim_status": "VIM_ERROR",
1859 "vim_details": str(e),
1860 }
1861
1862 if not isinstance(
1863 e, (NsWorkerException, vimconn.VimConnException)
1864 ):
1865 self.logger.error(
1866 "Unexpected exception at _delete_task task={}: {}".format(
1867 task["task_id"], e
1868 ),
1869 exc_info=True,
1870 )
tierno70eeb182020-10-19 16:38:00 +00001871
1872 try:
1873 if db_vim_info_update:
1874 db_vim_update = db_vim_info_update.copy()
sousaedu80135b92021-02-17 15:05:18 +01001875 db_ro_task_update.update(
1876 {
1877 "vim_info." + k: v
1878 for k, v in db_vim_info_update.items()
1879 }
1880 )
tierno70eeb182020-10-19 16:38:00 +00001881 ro_task["vim_info"].update(db_vim_info_update)
1882
1883 if new_status:
1884 if task_action == "CREATE":
1885 task_status_create = new_status
1886 db_ro_task_update[task_path] = new_status
tierno70eeb182020-10-19 16:38:00 +00001887
sousaedu80135b92021-02-17 15:05:18 +01001888 if target_update or db_vim_update:
tierno70eeb182020-10-19 16:38:00 +00001889 if target_update == "DELETE":
1890 self._update_target(task, None)
1891 elif target_update == "COPY_VIM_INFO":
1892 self._update_target(task, ro_task["vim_info"])
1893 else:
1894 self._update_target(task, db_vim_update)
1895
1896 except Exception as e:
sousaedu80135b92021-02-17 15:05:18 +01001897 if (
1898 isinstance(e, DbException)
1899 and e.http_code == HTTPStatus.NOT_FOUND
1900 ):
tierno70eeb182020-10-19 16:38:00 +00001901 # if the vnfrs or nsrs has been removed from database, this task must be removed
sousaedu80135b92021-02-17 15:05:18 +01001902 self.logger.debug(
1903 "marking to delete task={}".format(task["task_id"])
1904 )
tierno70eeb182020-10-19 16:38:00 +00001905 self.tasks_to_delete.append(task)
1906 else:
sousaedu80135b92021-02-17 15:05:18 +01001907 self.logger.error(
1908 "Unexpected exception at _update_target task={}: {}".format(
1909 task["task_id"], e
1910 ),
1911 exc_info=True,
1912 )
tierno70eeb182020-10-19 16:38:00 +00001913
tiernof1b640f2020-12-09 15:06:01 +00001914 locked_at = ro_task["locked_at"]
sousaedu80135b92021-02-17 15:05:18 +01001915
tiernof1b640f2020-12-09 15:06:01 +00001916 if lock_object:
sousaedu80135b92021-02-17 15:05:18 +01001917 locked_at = [
1918 lock_object["locked_at"],
1919 lock_object["locked_at"] + self.task_locked_time,
1920 ]
tiernof1b640f2020-12-09 15:06:01 +00001921 # locked_at contains two times to avoid race condition. In case the lock has been renew, it will
1922 # contain exactly locked_at + self.task_locked_time
1923 LockRenew.remove_lock_object(lock_object)
sousaedu80135b92021-02-17 15:05:18 +01001924
1925 q_filter = {
1926 "_id": ro_task["_id"],
1927 "to_check_at": ro_task["to_check_at"],
1928 "locked_at": locked_at,
1929 }
tierno70eeb182020-10-19 16:38:00 +00001930 # modify own task. Try filtering by to_next_check. For race condition if to_check_at has been modified,
1931 # outside this task (by ro_nbi) do not update it
1932 db_ro_task_update["locked_by"] = None
1933 # locked_at converted to int only for debugging. When has not decimals it means it has been unlocked
tiernof1b640f2020-12-09 15:06:01 +00001934 db_ro_task_update["locked_at"] = int(now - self.task_locked_time)
1935 db_ro_task_update["modified_at"] = now
tierno70eeb182020-10-19 16:38:00 +00001936 db_ro_task_update["to_check_at"] = next_check_at
sousaedu80135b92021-02-17 15:05:18 +01001937
1938 if not self.db.set_one(
1939 "ro_tasks",
1940 update_dict=db_ro_task_update,
1941 q_filter=q_filter,
1942 fail_on_empty=False,
1943 ):
tierno70eeb182020-10-19 16:38:00 +00001944 del db_ro_task_update["to_check_at"]
1945 del q_filter["to_check_at"]
sousaedu80135b92021-02-17 15:05:18 +01001946 self.db.set_one(
1947 "ro_tasks",
1948 q_filter=q_filter,
1949 update_dict=db_ro_task_update,
1950 fail_on_empty=True,
1951 )
tierno70eeb182020-10-19 16:38:00 +00001952 except DbException as e:
sousaedu80135b92021-02-17 15:05:18 +01001953 self.logger.error(
1954 "ro_task={} Error updating database {}".format(ro_task_id, e)
1955 )
tierno70eeb182020-10-19 16:38:00 +00001956 except Exception as e:
sousaedu80135b92021-02-17 15:05:18 +01001957 self.logger.error(
1958 "Error executing ro_task={}: {}".format(ro_task_id, e), exc_info=True
1959 )
tierno70eeb182020-10-19 16:38:00 +00001960
1961 def _update_target(self, task, ro_vim_item_update):
1962 table, _, temp = task["target_record"].partition(":")
1963 _id, _, path_vim_status = temp.partition(":")
sousaedu80135b92021-02-17 15:05:18 +01001964 path_item = path_vim_status[: path_vim_status.rfind(".")]
1965 path_item = path_item[: path_item.rfind(".")]
tierno70eeb182020-10-19 16:38:00 +00001966 # path_vim_status: dot separated list targeting vim information, e.g. "vdur.10.vim_info.vim:id"
1967 # path_item: dot separated list targeting record information, e.g. "vdur.10"
sousaedu80135b92021-02-17 15:05:18 +01001968
tierno70eeb182020-10-19 16:38:00 +00001969 if ro_vim_item_update:
sousaedu80135b92021-02-17 15:05:18 +01001970 update_dict = {
1971 path_vim_status + "." + k: v
1972 for k, v in ro_vim_item_update.items()
1973 if k
1974 in ("vim_id", "vim_details", "vim_name", "vim_status", "interfaces")
1975 }
1976
tierno70eeb182020-10-19 16:38:00 +00001977 if path_vim_status.startswith("vdur."):
1978 # for backward compatibility, add vdur.name apart from vdur.vim_name
1979 if ro_vim_item_update.get("vim_name"):
1980 update_dict[path_item + ".name"] = ro_vim_item_update["vim_name"]
sousaedu80135b92021-02-17 15:05:18 +01001981
tierno70eeb182020-10-19 16:38:00 +00001982 # for backward compatibility, add vdur.vim-id apart from vdur.vim_id
1983 if ro_vim_item_update.get("vim_id"):
1984 update_dict[path_item + ".vim-id"] = ro_vim_item_update["vim_id"]
sousaedu80135b92021-02-17 15:05:18 +01001985
tierno70eeb182020-10-19 16:38:00 +00001986 # update general status
1987 if ro_vim_item_update.get("vim_status"):
sousaedu80135b92021-02-17 15:05:18 +01001988 update_dict[path_item + ".status"] = ro_vim_item_update[
1989 "vim_status"
1990 ]
1991
tierno70eeb182020-10-19 16:38:00 +00001992 if ro_vim_item_update.get("interfaces"):
1993 path_interfaces = path_item + ".interfaces"
sousaedu80135b92021-02-17 15:05:18 +01001994
tierno70eeb182020-10-19 16:38:00 +00001995 for i, iface in enumerate(ro_vim_item_update.get("interfaces")):
1996 if iface:
sousaedu80135b92021-02-17 15:05:18 +01001997 update_dict.update(
1998 {
1999 path_interfaces + ".{}.".format(i) + k: v
2000 for k, v in iface.items()
2001 if k in ("vlan", "compute_node", "pci")
2002 }
2003 )
2004
tierno70eeb182020-10-19 16:38:00 +00002005 # put ip_address and mac_address with ip-address and mac-address
sousaedu80135b92021-02-17 15:05:18 +01002006 if iface.get("ip_address"):
2007 update_dict[
2008 path_interfaces + ".{}.".format(i) + "ip-address"
2009 ] = iface["ip_address"]
2010
2011 if iface.get("mac_address"):
2012 update_dict[
2013 path_interfaces + ".{}.".format(i) + "mac-address"
2014 ] = iface["mac_address"]
2015
tierno70eeb182020-10-19 16:38:00 +00002016 if iface.get("mgmt_vnf_interface") and iface.get("ip_address"):
sousaedu80135b92021-02-17 15:05:18 +01002017 update_dict["ip-address"] = iface.get("ip_address").split(
2018 ";"
2019 )[0]
2020
tierno70eeb182020-10-19 16:38:00 +00002021 if iface.get("mgmt_vdu_interface") and iface.get("ip_address"):
sousaedu80135b92021-02-17 15:05:18 +01002022 update_dict[path_item + ".ip-address"] = iface.get(
2023 "ip_address"
2024 ).split(";")[0]
tierno70eeb182020-10-19 16:38:00 +00002025
2026 self.db.set_one(table, q_filter={"_id": _id}, update_dict=update_dict)
2027 else:
2028 update_dict = {path_item + ".status": "DELETED"}
sousaedu80135b92021-02-17 15:05:18 +01002029 self.db.set_one(
2030 table,
2031 q_filter={"_id": _id},
2032 update_dict=update_dict,
2033 unset={path_vim_status: None},
2034 )
tierno70eeb182020-10-19 16:38:00 +00002035
2036 def _process_delete_db_tasks(self):
2037 """
2038 Delete task from database because vnfrs or nsrs or both have been deleted
2039 :return: None. Uses and modify self.tasks_to_delete
2040 """
2041 while self.tasks_to_delete:
2042 task = self.tasks_to_delete[0]
2043 vnfrs_deleted = None
2044 nsr_id = task["nsr_id"]
sousaedu80135b92021-02-17 15:05:18 +01002045
tierno70eeb182020-10-19 16:38:00 +00002046 if task["target_record"].startswith("vnfrs:"):
2047 # check if nsrs is present
2048 if self.db.get_one("nsrs", {"_id": nsr_id}, fail_on_empty=False):
2049 vnfrs_deleted = task["target_record"].split(":")[1]
sousaedu80135b92021-02-17 15:05:18 +01002050
tierno70eeb182020-10-19 16:38:00 +00002051 try:
2052 self.delete_db_tasks(self.db, nsr_id, vnfrs_deleted)
2053 except Exception as e:
sousaedu80135b92021-02-17 15:05:18 +01002054 self.logger.error(
2055 "Error deleting task={}: {}".format(task["task_id"], e)
2056 )
tierno70eeb182020-10-19 16:38:00 +00002057 self.tasks_to_delete.pop(0)
2058
2059 @staticmethod
2060 def delete_db_tasks(db, nsr_id, vnfrs_deleted):
2061 """
2062 Static method because it is called from osm_ng_ro.ns
2063 :param db: instance of database to use
2064 :param nsr_id: affected nsrs id
2065 :param vnfrs_deleted: only tasks with this vnfr id. If None, all affected by nsr_id
2066 :return: None, exception is fails
2067 """
2068 retries = 5
2069 for retry in range(retries):
2070 ro_tasks = db.get_list("ro_tasks", {"tasks.nsr_id": nsr_id})
2071 now = time.time()
2072 conflict = False
sousaedu80135b92021-02-17 15:05:18 +01002073
tierno70eeb182020-10-19 16:38:00 +00002074 for ro_task in ro_tasks:
2075 db_update = {}
2076 to_delete_ro_task = True
sousaedu80135b92021-02-17 15:05:18 +01002077
tierno70eeb182020-10-19 16:38:00 +00002078 for index, task in enumerate(ro_task["tasks"]):
2079 if not task:
2080 pass
sousaedu80135b92021-02-17 15:05:18 +01002081 elif (not vnfrs_deleted and task["nsr_id"] == nsr_id) or (
2082 vnfrs_deleted
2083 and task["target_record"].startswith("vnfrs:" + vnfrs_deleted)
2084 ):
tierno70eeb182020-10-19 16:38:00 +00002085 db_update["tasks.{}".format(index)] = None
2086 else:
sousaedu80135b92021-02-17 15:05:18 +01002087 # used by other nsr, ro_task cannot be deleted
2088 to_delete_ro_task = False
2089
tierno70eeb182020-10-19 16:38:00 +00002090 # delete or update if nobody has changed ro_task meanwhile. Used modified_at for known if changed
2091 if to_delete_ro_task:
sousaedu80135b92021-02-17 15:05:18 +01002092 if not db.del_one(
2093 "ro_tasks",
2094 q_filter={
2095 "_id": ro_task["_id"],
2096 "modified_at": ro_task["modified_at"],
2097 },
2098 fail_on_empty=False,
2099 ):
tierno70eeb182020-10-19 16:38:00 +00002100 conflict = True
2101 elif db_update:
2102 db_update["modified_at"] = now
sousaedu80135b92021-02-17 15:05:18 +01002103 if not db.set_one(
2104 "ro_tasks",
2105 q_filter={
2106 "_id": ro_task["_id"],
2107 "modified_at": ro_task["modified_at"],
2108 },
2109 update_dict=db_update,
2110 fail_on_empty=False,
2111 ):
tierno70eeb182020-10-19 16:38:00 +00002112 conflict = True
2113 if not conflict:
2114 return
2115 else:
2116 raise NsWorkerException("Exceeded {} retries".format(retries))
2117
tierno1d213f42020-04-24 14:02:51 +00002118 def run(self):
2119 # load database
tierno86153522020-12-06 18:27:16 +00002120 self.logger.info("Starting")
tierno1d213f42020-04-24 14:02:51 +00002121 while True:
tierno70eeb182020-10-19 16:38:00 +00002122 # step 1: get commands from queue
tierno1d213f42020-04-24 14:02:51 +00002123 try:
tierno86153522020-12-06 18:27:16 +00002124 if self.vim_targets:
2125 task = self.task_queue.get(block=False)
2126 else:
2127 if not self.idle:
2128 self.logger.debug("enters in idle state")
2129 self.idle = True
2130 task = self.task_queue.get(block=True)
2131 self.idle = False
2132
tierno1d213f42020-04-24 14:02:51 +00002133 if task[0] == "terminate":
2134 break
tierno70eeb182020-10-19 16:38:00 +00002135 elif task[0] == "load_vim":
tierno86153522020-12-06 18:27:16 +00002136 self.logger.info("order to load vim {}".format(task[1]))
tierno1d213f42020-04-24 14:02:51 +00002137 self._load_vim(task[1])
tierno70eeb182020-10-19 16:38:00 +00002138 elif task[0] == "unload_vim":
tierno86153522020-12-06 18:27:16 +00002139 self.logger.info("order to unload vim {}".format(task[1]))
tierno70eeb182020-10-19 16:38:00 +00002140 self._unload_vim(task[1])
2141 elif task[0] == "reload_vim":
2142 self._reload_vim(task[1])
2143 elif task[0] == "check_vim":
tierno86153522020-12-06 18:27:16 +00002144 self.logger.info("order to check vim {}".format(task[1]))
tierno70eeb182020-10-19 16:38:00 +00002145 self._check_vim(task[1])
tierno1d213f42020-04-24 14:02:51 +00002146 continue
tierno70eeb182020-10-19 16:38:00 +00002147 except Exception as e:
2148 if isinstance(e, queue.Empty):
2149 pass
2150 else:
sousaedu80135b92021-02-17 15:05:18 +01002151 self.logger.critical(
2152 "Error processing task: {}".format(e), exc_info=True
2153 )
tierno1d213f42020-04-24 14:02:51 +00002154
tierno70eeb182020-10-19 16:38:00 +00002155 # step 2: process pending_tasks, delete not needed tasks
tierno1d213f42020-04-24 14:02:51 +00002156 try:
tierno70eeb182020-10-19 16:38:00 +00002157 if self.tasks_to_delete:
2158 self._process_delete_db_tasks()
tierno1d213f42020-04-24 14:02:51 +00002159 busy = False
2160 ro_task = self._get_db_task()
2161 if ro_task:
tierno70eeb182020-10-19 16:38:00 +00002162 self._process_pending_tasks(ro_task)
tierno1d213f42020-04-24 14:02:51 +00002163 busy = True
2164 if not busy:
2165 time.sleep(5)
2166 except Exception as e:
sousaedu80135b92021-02-17 15:05:18 +01002167 self.logger.critical(
2168 "Unexpected exception at run: " + str(e), exc_info=True
2169 )
tierno1d213f42020-04-24 14:02:51 +00002170
tierno86153522020-12-06 18:27:16 +00002171 self.logger.info("Finishing")