1 # -*- coding: utf-8 -*-
4 # Copyright 2020 Telefonica Investigacion y Desarrollo, S.A.U.
6 # Licensed under the Apache License, Version 2.0 (the "License"); you may
7 # not use this file except in compliance with the License. You may obtain
8 # a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 # License for the specific language governing permissions and limitations
21 This is thread that interacts with a VIM. It processes TASKs sequentially against a single VIM.
22 The tasks are stored at database in table ro_tasks
23 A single ro_task refers to a VIM element (flavor, image, network, ...).
24 A ro_task can contain several 'tasks', each one with a target, where to store the results
27 from copy
import deepcopy
28 from http
import HTTPStatus
32 from shutil
import rmtree
35 from unittest
.mock
import Mock
37 from importlib_metadata
import entry_points
38 from osm_common
.dbbase
import DbException
39 from osm_ng_ro
.vim_admin
import LockRenew
40 from osm_ro_plugin
import sdnconn
, vimconn
41 from osm_ro_plugin
.sdn_dummy
import SdnDummyConnector
42 from osm_ro_plugin
.vim_dummy
import VimDummyConnector
45 __author__
= "Alfonso Tierno"
46 __date__
= "$28-Sep-2017 12:07:15$"
49 def deep_get(target_dict
, *args
, **kwargs
):
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
59 if not isinstance(target_dict
, dict) or key
not in target_dict
:
60 return kwargs
.get("default")
61 target_dict
= target_dict
[key
]
65 class NsWorkerException(Exception):
69 class FailingConnector
:
70 def __init__(self
, error_msg
):
71 self
.error_msg
= error_msg
73 for method
in dir(vimconn
.VimConnector
):
76 self
, method
, Mock(side_effect
=vimconn
.VimConnException(error_msg
))
79 for method
in dir(sdnconn
.SdnConnectorBase
):
82 self
, method
, Mock(side_effect
=sdnconn
.SdnConnectorError(error_msg
))
86 class NsWorkerExceptionNotFound(NsWorkerException
):
90 class VimInteractionBase
:
91 """Base class to call VIM/SDN for creating, deleting and refresh networks, VMs, flavors, ...
92 It implements methods that does nothing and return ok"""
94 def __init__(self
, db
, my_vims
, db_vims
, logger
):
97 self
.my_vims
= my_vims
98 self
.db_vims
= db_vims
100 def new(self
, ro_task
, task_index
, task_depends
):
103 def refresh(self
, ro_task
):
104 """skip calling VIM to get image, flavor status. Assumes ok"""
105 if ro_task
["vim_info"]["vim_status"] == "VIM_ERROR":
110 def delete(self
, ro_task
, task_index
):
111 """skip calling VIM to delete image. Assumes ok"""
114 def exec(self
, ro_task
, task_index
, task_depends
):
115 return "DONE", None, None
118 class VimInteractionNet(VimInteractionBase
):
119 def new(self
, ro_task
, task_index
, task_depends
):
121 task
= ro_task
["tasks"][task_index
]
122 task_id
= task
["task_id"]
125 target_vim
= self
.my_vims
[ro_task
["target_id"]]
127 mgmtnet_defined_in_vim
= False
131 if task
.get("find_params"):
132 # if management, get configuration of VIM
133 if task
["find_params"].get("filter_dict"):
134 vim_filter
= task
["find_params"]["filter_dict"]
136 elif task
["find_params"].get("mgmt"):
139 self
.db_vims
[ro_task
["target_id"]],
141 "management_network_id",
143 mgmtnet_defined_in_vim
= True
145 "id": self
.db_vims
[ro_task
["target_id"]]["config"][
146 "management_network_id"
150 self
.db_vims
[ro_task
["target_id"]],
152 "management_network_name",
154 mgmtnet_defined_in_vim
= True
156 "name": self
.db_vims
[ro_task
["target_id"]]["config"][
157 "management_network_name"
161 vim_filter
= {"name": task
["find_params"]["name"]}
163 raise NsWorkerExceptionNotFound(
164 "Invalid find_params for new_net {}".format(task
["find_params"])
167 vim_nets
= target_vim
.get_network_list(vim_filter
)
168 if not vim_nets
and not task
.get("params"):
169 # If there is mgmt-network in the descriptor,
170 # there is no mapping of that network to a VIM network in the descriptor,
171 # also there is no mapping in the "--config" parameter or at VIM creation;
172 # that mgmt-network will be created.
173 if mgmtnet
and not mgmtnet_defined_in_vim
:
175 vim_filter
.get("name")
176 if vim_filter
.get("name")
177 else vim_filter
.get("id")[:16]
179 vim_net_id
, created_items
= target_vim
.new_network(
183 "Created mgmt network vim_net_id: {}".format(vim_net_id
)
187 raise NsWorkerExceptionNotFound(
188 "Network not found with this criteria: '{}'".format(
189 task
.get("find_params")
192 elif len(vim_nets
) > 1:
193 raise NsWorkerException(
194 "More than one network found with this criteria: '{}'".format(
200 vim_net_id
= vim_nets
[0]["id"]
203 params
= task
["params"]
204 vim_net_id
, created_items
= target_vim
.new_network(**params
)
207 ro_vim_item_update
= {
208 "vim_id": vim_net_id
,
209 "vim_status": "BUILD",
211 "created_items": created_items
,
215 "task={} {} new-net={} created={}".format(
216 task_id
, ro_task
["target_id"], vim_net_id
, created
220 return "BUILD", ro_vim_item_update
221 except (vimconn
.VimConnException
, NsWorkerException
) as e
:
223 "task={} vim={} new-net: {}".format(task_id
, ro_task
["target_id"], e
)
225 ro_vim_item_update
= {
226 "vim_status": "VIM_ERROR",
228 "vim_details": str(e
),
231 return "FAILED", ro_vim_item_update
233 def refresh(self
, ro_task
):
234 """Call VIM to get network status"""
235 ro_task_id
= ro_task
["_id"]
236 target_vim
= self
.my_vims
[ro_task
["target_id"]]
237 vim_id
= ro_task
["vim_info"]["vim_id"]
238 net_to_refresh_list
= [vim_id
]
241 vim_dict
= target_vim
.refresh_nets_status(net_to_refresh_list
)
242 vim_info
= vim_dict
[vim_id
]
244 if vim_info
["status"] == "ACTIVE":
246 elif vim_info
["status"] == "BUILD":
247 task_status
= "BUILD"
249 task_status
= "FAILED"
250 except vimconn
.VimConnException
as e
:
251 # Mark all tasks at VIM_ERROR status
253 "ro_task={} vim={} get-net={}: {}".format(
254 ro_task_id
, ro_task
["target_id"], vim_id
, e
257 vim_info
= {"status": "VIM_ERROR", "error_msg": str(e
)}
258 task_status
= "FAILED"
260 ro_vim_item_update
= {}
261 if ro_task
["vim_info"]["vim_status"] != vim_info
["status"]:
262 ro_vim_item_update
["vim_status"] = vim_info
["status"]
264 if ro_task
["vim_info"]["vim_name"] != vim_info
.get("name"):
265 ro_vim_item_update
["vim_name"] = vim_info
.get("name")
267 if vim_info
["status"] in ("ERROR", "VIM_ERROR"):
268 if ro_task
["vim_info"]["vim_details"] != vim_info
.get("error_msg"):
269 ro_vim_item_update
["vim_details"] = vim_info
.get("error_msg")
270 elif vim_info
["status"] == "DELETED":
271 ro_vim_item_update
["vim_id"] = None
272 ro_vim_item_update
["vim_details"] = "Deleted externally"
274 if ro_task
["vim_info"]["vim_details"] != vim_info
["vim_info"]:
275 ro_vim_item_update
["vim_details"] = vim_info
["vim_info"]
277 if ro_vim_item_update
:
279 "ro_task={} {} get-net={}: status={} {}".format(
281 ro_task
["target_id"],
283 ro_vim_item_update
.get("vim_status"),
284 ro_vim_item_update
.get("vim_details")
285 if ro_vim_item_update
.get("vim_status") != "ACTIVE"
290 return task_status
, ro_vim_item_update
292 def delete(self
, ro_task
, task_index
):
293 task
= ro_task
["tasks"][task_index
]
294 task_id
= task
["task_id"]
295 net_vim_id
= ro_task
["vim_info"]["vim_id"]
296 ro_vim_item_update_ok
= {
297 "vim_status": "DELETED",
299 "vim_details": "DELETED",
304 if net_vim_id
or ro_task
["vim_info"]["created_items"]:
305 target_vim
= self
.my_vims
[ro_task
["target_id"]]
306 target_vim
.delete_network(
307 net_vim_id
, ro_task
["vim_info"]["created_items"]
309 except vimconn
.VimConnNotFoundException
:
310 ro_vim_item_update_ok
["vim_details"] = "already deleted"
311 except vimconn
.VimConnException
as e
:
313 "ro_task={} vim={} del-net={}: {}".format(
314 ro_task
["_id"], ro_task
["target_id"], net_vim_id
, e
317 ro_vim_item_update
= {
318 "vim_status": "VIM_ERROR",
319 "vim_details": "Error while deleting: {}".format(e
),
322 return "FAILED", ro_vim_item_update
325 "task={} {} del-net={} {}".format(
327 ro_task
["target_id"],
329 ro_vim_item_update_ok
.get("vim_details", ""),
333 return "DONE", ro_vim_item_update_ok
336 class VimInteractionVdu(VimInteractionBase
):
337 max_retries_inject_ssh_key
= 20 # 20 times
338 time_retries_inject_ssh_key
= 30 # wevery 30 seconds
340 def new(self
, ro_task
, task_index
, task_depends
):
341 task
= ro_task
["tasks"][task_index
]
342 task_id
= task
["task_id"]
345 target_vim
= self
.my_vims
[ro_task
["target_id"]]
349 params
= task
["params"]
350 params_copy
= deepcopy(params
)
351 net_list
= params_copy
["net_list"]
354 # change task_id into network_id
355 if "net_id" in net
and net
["net_id"].startswith("TASK-"):
356 network_id
= task_depends
[net
["net_id"]]
359 raise NsWorkerException(
360 "Cannot create VM because depends on a network not created or found "
361 "for {}".format(net
["net_id"])
364 net
["net_id"] = network_id
366 if params_copy
["image_id"].startswith("TASK-"):
367 params_copy
["image_id"] = task_depends
[params_copy
["image_id"]]
369 if params_copy
["flavor_id"].startswith("TASK-"):
370 params_copy
["flavor_id"] = task_depends
[params_copy
["flavor_id"]]
372 vim_vm_id
, created_items
= target_vim
.new_vminstance(**params_copy
)
373 interfaces
= [iface
["vim_id"] for iface
in params_copy
["net_list"]]
375 ro_vim_item_update
= {
377 "vim_status": "BUILD",
379 "created_items": created_items
,
381 "interfaces_vim_ids": interfaces
,
385 "task={} {} new-vm={} created={}".format(
386 task_id
, ro_task
["target_id"], vim_vm_id
, created
390 return "BUILD", ro_vim_item_update
391 except (vimconn
.VimConnException
, NsWorkerException
) as e
:
393 "task={} {} new-vm: {}".format(task_id
, ro_task
["target_id"], e
)
395 ro_vim_item_update
= {
396 "vim_status": "VIM_ERROR",
398 "vim_details": str(e
),
401 return "FAILED", ro_vim_item_update
403 def delete(self
, ro_task
, task_index
):
404 task
= ro_task
["tasks"][task_index
]
405 task_id
= task
["task_id"]
406 vm_vim_id
= ro_task
["vim_info"]["vim_id"]
407 ro_vim_item_update_ok
= {
408 "vim_status": "DELETED",
410 "vim_details": "DELETED",
415 if vm_vim_id
or ro_task
["vim_info"]["created_items"]:
416 target_vim
= self
.my_vims
[ro_task
["target_id"]]
417 target_vim
.delete_vminstance(
418 vm_vim_id
, ro_task
["vim_info"]["created_items"]
420 except vimconn
.VimConnNotFoundException
:
421 ro_vim_item_update_ok
["vim_details"] = "already deleted"
422 except vimconn
.VimConnException
as e
:
424 "ro_task={} vim={} del-vm={}: {}".format(
425 ro_task
["_id"], ro_task
["target_id"], vm_vim_id
, e
428 ro_vim_item_update
= {
429 "vim_status": "VIM_ERROR",
430 "vim_details": "Error while deleting: {}".format(e
),
433 return "FAILED", ro_vim_item_update
436 "task={} {} del-vm={} {}".format(
438 ro_task
["target_id"],
440 ro_vim_item_update_ok
.get("vim_details", ""),
444 return "DONE", ro_vim_item_update_ok
446 def refresh(self
, ro_task
):
447 """Call VIM to get vm status"""
448 ro_task_id
= ro_task
["_id"]
449 target_vim
= self
.my_vims
[ro_task
["target_id"]]
450 vim_id
= ro_task
["vim_info"]["vim_id"]
455 vm_to_refresh_list
= [vim_id
]
457 vim_dict
= target_vim
.refresh_vms_status(vm_to_refresh_list
)
458 vim_info
= vim_dict
[vim_id
]
460 if vim_info
["status"] == "ACTIVE":
462 elif vim_info
["status"] == "BUILD":
463 task_status
= "BUILD"
465 task_status
= "FAILED"
467 # try to load and parse vim_information
469 vim_info_info
= yaml
.safe_load(vim_info
["vim_info"])
470 if vim_info_info
.get("name"):
471 vim_info
["name"] = vim_info_info
["name"]
474 except vimconn
.VimConnException
as e
:
475 # Mark all tasks at VIM_ERROR status
477 "ro_task={} vim={} get-vm={}: {}".format(
478 ro_task_id
, ro_task
["target_id"], vim_id
, e
481 vim_info
= {"status": "VIM_ERROR", "error_msg": str(e
)}
482 task_status
= "FAILED"
484 ro_vim_item_update
= {}
486 # Interfaces cannot be present if e.g. VM is not present, that is status=DELETED
488 if vim_info
.get("interfaces"):
489 for vim_iface_id
in ro_task
["vim_info"]["interfaces_vim_ids"]:
493 for iface
in vim_info
["interfaces"]
494 if vim_iface_id
== iface
["vim_interface_id"]
499 # iface.pop("vim_info", None)
500 vim_interfaces
.append(iface
)
504 for t
in ro_task
["tasks"]
505 if t
and t
["action"] == "CREATE" and t
["status"] != "FINISHED"
507 if vim_interfaces
and task_create
.get("mgmt_vnf_interface") is not None:
508 vim_interfaces
[task_create
["mgmt_vnf_interface"]][
512 mgmt_vdu_iface
= task_create
.get(
513 "mgmt_vdu_interface", task_create
.get("mgmt_vnf_interface", 0)
516 vim_interfaces
[mgmt_vdu_iface
]["mgmt_vdu_interface"] = True
518 if ro_task
["vim_info"]["interfaces"] != vim_interfaces
:
519 ro_vim_item_update
["interfaces"] = vim_interfaces
521 if ro_task
["vim_info"]["vim_status"] != vim_info
["status"]:
522 ro_vim_item_update
["vim_status"] = vim_info
["status"]
524 if ro_task
["vim_info"]["vim_name"] != vim_info
.get("name"):
525 ro_vim_item_update
["vim_name"] = vim_info
.get("name")
527 if vim_info
["status"] in ("ERROR", "VIM_ERROR"):
528 if ro_task
["vim_info"]["vim_details"] != vim_info
.get("error_msg"):
529 ro_vim_item_update
["vim_details"] = vim_info
.get("error_msg")
530 elif vim_info
["status"] == "DELETED":
531 ro_vim_item_update
["vim_id"] = None
532 ro_vim_item_update
["vim_details"] = "Deleted externally"
534 if ro_task
["vim_info"]["vim_details"] != vim_info
["vim_info"]:
535 ro_vim_item_update
["vim_details"] = vim_info
["vim_info"]
537 if ro_vim_item_update
:
539 "ro_task={} {} get-vm={}: status={} {}".format(
541 ro_task
["target_id"],
543 ro_vim_item_update
.get("vim_status"),
544 ro_vim_item_update
.get("vim_details")
545 if ro_vim_item_update
.get("vim_status") != "ACTIVE"
550 return task_status
, ro_vim_item_update
552 def exec(self
, ro_task
, task_index
, task_depends
):
553 task
= ro_task
["tasks"][task_index
]
554 task_id
= task
["task_id"]
555 target_vim
= self
.my_vims
[ro_task
["target_id"]]
556 db_task_update
= {"retries": 0}
557 retries
= task
.get("retries", 0)
560 params
= task
["params"]
561 params_copy
= deepcopy(params
)
562 params_copy
["ro_key"] = self
.db
.decrypt(
563 params_copy
.pop("private_key"),
564 params_copy
.pop("schema_version"),
565 params_copy
.pop("salt"),
567 params_copy
["ip_addr"] = params_copy
.pop("ip_address")
568 target_vim
.inject_user_key(**params_copy
)
570 "task={} {} action-vm=inject_key".format(task_id
, ro_task
["target_id"])
577 ) # params_copy["key"]
578 except (vimconn
.VimConnException
, NsWorkerException
) as e
:
581 if retries
< self
.max_retries_inject_ssh_key
:
587 "next_retry": self
.time_retries_inject_ssh_key
,
592 "task={} {} inject-ssh-key: {}".format(task_id
, ro_task
["target_id"], e
)
594 ro_vim_item_update
= {"vim_details": str(e
)}
596 return "FAILED", ro_vim_item_update
, db_task_update
599 class VimInteractionImage(VimInteractionBase
):
600 def new(self
, ro_task
, task_index
, task_depends
):
601 task
= ro_task
["tasks"][task_index
]
602 task_id
= task
["task_id"]
605 target_vim
= self
.my_vims
[ro_task
["target_id"]]
609 if task
.get("find_params"):
610 vim_images
= target_vim
.get_image_list(**task
["find_params"])
613 raise NsWorkerExceptionNotFound(
614 "Image not found with this criteria: '{}'".format(
618 elif len(vim_images
) > 1:
619 raise NsWorkerException(
620 "More than one image found with this criteria: '{}'".format(
625 vim_image_id
= vim_images
[0]["id"]
627 ro_vim_item_update
= {
628 "vim_id": vim_image_id
,
629 "vim_status": "DONE",
631 "created_items": created_items
,
635 "task={} {} new-image={} created={}".format(
636 task_id
, ro_task
["target_id"], vim_image_id
, created
640 return "DONE", ro_vim_item_update
641 except (NsWorkerException
, vimconn
.VimConnException
) as e
:
643 "task={} {} new-image: {}".format(task_id
, ro_task
["target_id"], e
)
645 ro_vim_item_update
= {
646 "vim_status": "VIM_ERROR",
648 "vim_details": str(e
),
651 return "FAILED", ro_vim_item_update
654 class VimInteractionFlavor(VimInteractionBase
):
655 def delete(self
, ro_task
, task_index
):
656 task
= ro_task
["tasks"][task_index
]
657 task_id
= task
["task_id"]
658 flavor_vim_id
= ro_task
["vim_info"]["vim_id"]
659 ro_vim_item_update_ok
= {
660 "vim_status": "DELETED",
662 "vim_details": "DELETED",
668 target_vim
= self
.my_vims
[ro_task
["target_id"]]
669 target_vim
.delete_flavor(flavor_vim_id
)
670 except vimconn
.VimConnNotFoundException
:
671 ro_vim_item_update_ok
["vim_details"] = "already deleted"
672 except vimconn
.VimConnException
as e
:
674 "ro_task={} vim={} del-flavor={}: {}".format(
675 ro_task
["_id"], ro_task
["target_id"], flavor_vim_id
, e
678 ro_vim_item_update
= {
679 "vim_status": "VIM_ERROR",
680 "vim_details": "Error while deleting: {}".format(e
),
683 return "FAILED", ro_vim_item_update
686 "task={} {} del-flavor={} {}".format(
688 ro_task
["target_id"],
690 ro_vim_item_update_ok
.get("vim_details", ""),
694 return "DONE", ro_vim_item_update_ok
696 def new(self
, ro_task
, task_index
, task_depends
):
697 task
= ro_task
["tasks"][task_index
]
698 task_id
= task
["task_id"]
701 target_vim
= self
.my_vims
[ro_task
["target_id"]]
707 if task
.get("find_params"):
709 flavor_data
= task
["find_params"]["flavor_data"]
710 vim_flavor_id
= target_vim
.get_flavor_id_from_data(flavor_data
)
711 except vimconn
.VimConnNotFoundException
:
714 if not vim_flavor_id
and task
.get("params"):
716 flavor_data
= task
["params"]["flavor_data"]
717 vim_flavor_id
= target_vim
.new_flavor(flavor_data
)
720 ro_vim_item_update
= {
721 "vim_id": vim_flavor_id
,
722 "vim_status": "DONE",
724 "created_items": created_items
,
728 "task={} {} new-flavor={} created={}".format(
729 task_id
, ro_task
["target_id"], vim_flavor_id
, created
733 return "DONE", ro_vim_item_update
734 except (vimconn
.VimConnException
, NsWorkerException
) as e
:
736 "task={} vim={} new-flavor: {}".format(task_id
, ro_task
["target_id"], e
)
738 ro_vim_item_update
= {
739 "vim_status": "VIM_ERROR",
741 "vim_details": str(e
),
744 return "FAILED", ro_vim_item_update
747 class VimInteractionSdnNet(VimInteractionBase
):
749 def _match_pci(port_pci
, mapping
):
751 Check if port_pci matches with mapping
752 mapping can have brackets to indicate that several chars are accepted. e.g
753 pci '0000:af:10.1' matches with '0000:af:1[01].[1357]'
754 :param port_pci: text
755 :param mapping: text, can contain brackets to indicate several chars are available
756 :return: True if matches, False otherwise
758 if not port_pci
or not mapping
:
760 if port_pci
== mapping
:
766 bracket_start
= mapping
.find("[", mapping_index
)
768 if bracket_start
== -1:
771 bracket_end
= mapping
.find("]", bracket_start
)
772 if bracket_end
== -1:
775 length
= bracket_start
- mapping_index
778 and port_pci
[pci_index
: pci_index
+ length
]
779 != mapping
[mapping_index
:bracket_start
]
784 port_pci
[pci_index
+ length
]
785 not in mapping
[bracket_start
+ 1 : bracket_end
]
789 pci_index
+= length
+ 1
790 mapping_index
= bracket_end
+ 1
792 if port_pci
[pci_index
:] != mapping
[mapping_index
:]:
797 def _get_interfaces(self
, vlds_to_connect
, vim_account_id
):
799 :param vlds_to_connect: list with format vnfrs:<id>:vld.<vld_id> or nsrs:<id>:vld.<vld_id>
800 :param vim_account_id:
805 for vld
in vlds_to_connect
:
806 table
, _
, db_id
= vld
.partition(":")
807 db_id
, _
, vld
= db_id
.partition(":")
808 _
, _
, vld_id
= vld
.partition(".")
811 q_filter
= {"vim-account-id": vim_account_id
, "_id": db_id
}
812 iface_key
= "vnf-vld-id"
813 else: # table == "nsrs"
814 q_filter
= {"vim-account-id": vim_account_id
, "nsr-id-ref": db_id
}
815 iface_key
= "ns-vld-id"
817 db_vnfrs
= self
.db
.get_list("vnfrs", q_filter
=q_filter
)
819 for db_vnfr
in db_vnfrs
:
820 for vdu_index
, vdur
in enumerate(db_vnfr
.get("vdur", ())):
821 for iface_index
, interface
in enumerate(vdur
["interfaces"]):
822 if interface
.get(iface_key
) == vld_id
and interface
.get(
824 ) in ("SR-IOV", "PCI-PASSTHROUGH"):
826 interface_
= interface
.copy()
827 interface_
["id"] = "vnfrs:{}:vdu.{}.interfaces.{}".format(
828 db_vnfr
["_id"], vdu_index
, iface_index
831 if vdur
.get("status") == "ERROR":
832 interface_
["status"] = "ERROR"
834 interfaces
.append(interface_
)
838 def refresh(self
, ro_task
):
839 # look for task create
840 task_create_index
, _
= next(
842 for i_t
in enumerate(ro_task
["tasks"])
844 and i_t
[1]["action"] == "CREATE"
845 and i_t
[1]["status"] != "FINISHED"
848 return self
.new(ro_task
, task_create_index
, None)
850 def new(self
, ro_task
, task_index
, task_depends
):
852 task
= ro_task
["tasks"][task_index
]
853 task_id
= task
["task_id"]
854 target_vim
= self
.my_vims
[ro_task
["target_id"]]
856 sdn_net_id
= ro_task
["vim_info"]["vim_id"]
858 created_items
= ro_task
["vim_info"].get("created_items")
859 connected_ports
= ro_task
["vim_info"].get("connected_ports", [])
860 new_connected_ports
= []
861 last_update
= ro_task
["vim_info"].get("last_update", 0)
862 sdn_status
= ro_task
["vim_info"].get("vim_status", "BUILD") or "BUILD"
864 created
= ro_task
["vim_info"].get("created", False)
868 params
= task
["params"]
869 vlds_to_connect
= params
["vlds"]
870 associated_vim
= params
["target_vim"]
871 # external additional ports
872 additional_ports
= params
.get("sdn-ports") or ()
873 _
, _
, vim_account_id
= associated_vim
.partition(":")
877 if associated_vim
not in self
.db_vims
:
878 self
.db_vims
[associated_vim
] = self
.db
.get_one(
879 "vim_accounts", {"_id": vim_account_id
}
882 db_vim
= self
.db_vims
[associated_vim
]
884 # look for ports to connect
885 ports
= self
._get
_interfaces
(vlds_to_connect
, vim_account_id
)
889 pending_ports
= error_ports
= 0
891 sdn_need_update
= False
894 vlan_used
= port
.get("vlan") or vlan_used
896 # TODO. Do not connect if already done
897 if not port
.get("compute_node") or not port
.get("pci"):
898 if port
.get("status") == "ERROR":
905 compute_node_mappings
= next(
908 for c
in db_vim
["config"].get("sdn-port-mapping", ())
909 if c
and c
["compute_node"] == port
["compute_node"]
914 if compute_node_mappings
:
915 # process port_mapping pci of type 0000:af:1[01].[1357]
919 for p
in compute_node_mappings
["ports"]
920 if self
._match
_pci
(port
["pci"], p
.get("pci"))
926 if not db_vim
["config"].get("mapping_not_needed"):
928 "Port mapping not found for compute_node={} pci={}".format(
929 port
["compute_node"], port
["pci"]
936 service_endpoint_id
= "{}:{}".format(port
["compute_node"], port
["pci"])
938 "service_endpoint_id": pmap
.get("service_endpoint_id")
939 or service_endpoint_id
,
940 "service_endpoint_encapsulation_type": "dot1q"
941 if port
["type"] == "SR-IOV"
943 "service_endpoint_encapsulation_info": {
944 "vlan": port
.get("vlan"),
945 "mac": port
.get("mac-address"),
946 "device_id": pmap
.get("device_id") or port
["compute_node"],
947 "device_interface_id": pmap
.get("device_interface_id")
949 "switch_dpid": pmap
.get("switch_id") or pmap
.get("switch_dpid"),
950 "switch_port": pmap
.get("switch_port"),
951 "service_mapping_info": pmap
.get("service_mapping_info"),
956 # if port["modified_at"] > last_update:
957 # sdn_need_update = True
958 new_connected_ports
.append(port
["id"]) # TODO
959 sdn_ports
.append(new_port
)
963 "{} interfaces have not been created as VDU is on ERROR status".format(
968 # connect external ports
969 for index
, additional_port
in enumerate(additional_ports
):
970 additional_port_id
= additional_port
.get(
971 "service_endpoint_id"
972 ) or "external-{}".format(index
)
975 "service_endpoint_id": additional_port_id
,
976 "service_endpoint_encapsulation_type": additional_port
.get(
977 "service_endpoint_encapsulation_type", "dot1q"
979 "service_endpoint_encapsulation_info": {
980 "vlan": additional_port
.get("vlan") or vlan_used
,
981 "mac": additional_port
.get("mac_address"),
982 "device_id": additional_port
.get("device_id"),
983 "device_interface_id": additional_port
.get(
984 "device_interface_id"
986 "switch_dpid": additional_port
.get("switch_dpid")
987 or additional_port
.get("switch_id"),
988 "switch_port": additional_port
.get("switch_port"),
989 "service_mapping_info": additional_port
.get(
990 "service_mapping_info"
995 new_connected_ports
.append(additional_port_id
)
998 # if there are more ports to connect or they have been modified, call create/update
1000 sdn_status
= "ERROR"
1001 sdn_info
= "; ".join(error_list
)
1002 elif set(connected_ports
) != set(new_connected_ports
) or sdn_need_update
:
1003 last_update
= time
.time()
1006 if len(sdn_ports
) < 2:
1007 sdn_status
= "ACTIVE"
1009 if not pending_ports
:
1011 "task={} {} new-sdn-net done, less than 2 ports".format(
1012 task_id
, ro_task
["target_id"]
1016 net_type
= params
.get("type") or "ELAN"
1020 ) = target_vim
.create_connectivity_service(net_type
, sdn_ports
)
1023 "task={} {} new-sdn-net={} created={}".format(
1024 task_id
, ro_task
["target_id"], sdn_net_id
, created
1028 created_items
= target_vim
.edit_connectivity_service(
1029 sdn_net_id
, conn_info
=created_items
, connection_points
=sdn_ports
1033 "task={} {} update-sdn-net={} created={}".format(
1034 task_id
, ro_task
["target_id"], sdn_net_id
, created
1038 connected_ports
= new_connected_ports
1040 wim_status_dict
= target_vim
.get_connectivity_service_status(
1041 sdn_net_id
, conn_info
=created_items
1043 sdn_status
= wim_status_dict
["sdn_status"]
1045 if wim_status_dict
.get("sdn_info"):
1046 sdn_info
= str(wim_status_dict
.get("sdn_info")) or ""
1048 if wim_status_dict
.get("error_msg"):
1049 sdn_info
= wim_status_dict
.get("error_msg") or ""
1052 if sdn_status
!= "ERROR":
1053 sdn_info
= "Waiting for getting interfaces location from VIM. Obtained '{}' of {}".format(
1054 len(ports
) - pending_ports
, len(ports
)
1057 if sdn_status
== "ACTIVE":
1058 sdn_status
= "BUILD"
1060 ro_vim_item_update
= {
1061 "vim_id": sdn_net_id
,
1062 "vim_status": sdn_status
,
1064 "created_items": created_items
,
1065 "connected_ports": connected_ports
,
1066 "vim_details": sdn_info
,
1067 "last_update": last_update
,
1070 return sdn_status
, ro_vim_item_update
1071 except Exception as e
:
1073 "task={} vim={} new-net: {}".format(task_id
, ro_task
["target_id"], e
),
1074 exc_info
=not isinstance(
1075 e
, (sdnconn
.SdnConnectorError
, vimconn
.VimConnException
)
1078 ro_vim_item_update
= {
1079 "vim_status": "VIM_ERROR",
1081 "vim_details": str(e
),
1084 return "FAILED", ro_vim_item_update
1086 def delete(self
, ro_task
, task_index
):
1087 task
= ro_task
["tasks"][task_index
]
1088 task_id
= task
["task_id"]
1089 sdn_vim_id
= ro_task
["vim_info"].get("vim_id")
1090 ro_vim_item_update_ok
= {
1091 "vim_status": "DELETED",
1093 "vim_details": "DELETED",
1099 target_vim
= self
.my_vims
[ro_task
["target_id"]]
1100 target_vim
.delete_connectivity_service(
1101 sdn_vim_id
, ro_task
["vim_info"].get("created_items")
1104 except Exception as e
:
1106 isinstance(e
, sdnconn
.SdnConnectorError
)
1107 and e
.http_code
== HTTPStatus
.NOT_FOUND
.value
1109 ro_vim_item_update_ok
["vim_details"] = "already deleted"
1112 "ro_task={} vim={} del-sdn-net={}: {}".format(
1113 ro_task
["_id"], ro_task
["target_id"], sdn_vim_id
, e
1115 exc_info
=not isinstance(
1116 e
, (sdnconn
.SdnConnectorError
, vimconn
.VimConnException
)
1119 ro_vim_item_update
= {
1120 "vim_status": "VIM_ERROR",
1121 "vim_details": "Error while deleting: {}".format(e
),
1124 return "FAILED", ro_vim_item_update
1127 "task={} {} del-sdn-net={} {}".format(
1129 ro_task
["target_id"],
1131 ro_vim_item_update_ok
.get("vim_details", ""),
1135 return "DONE", ro_vim_item_update_ok
1138 class NsWorker(threading
.Thread
):
1139 REFRESH_BUILD
= 5 # 5 seconds
1140 REFRESH_ACTIVE
= 60 # 1 minute
1142 REFRESH_IMAGE
= 3600 * 10
1143 REFRESH_DELETE
= 3600 * 10
1147 def __init__(self
, worker_index
, config
, plugins
, db
):
1150 :param worker_index: thread index
1151 :param config: general configuration of RO, among others the process_id with the docker id where it runs
1152 :param plugins: global shared dict with the loaded plugins
1153 :param db: database class instance to use
1155 threading
.Thread
.__init
__(self
)
1156 self
.config
= config
1157 self
.plugins
= plugins
1158 self
.plugin_name
= "unknown"
1159 self
.logger
= logging
.getLogger("ro.worker{}".format(worker_index
))
1160 self
.worker_index
= worker_index
1161 self
.task_queue
= queue
.Queue(self
.QUEUE_SIZE
)
1162 # targetvim: vimplugin class
1164 # targetvim: vim information from database
1167 self
.vim_targets
= []
1168 self
.my_id
= config
["process_id"] + ":" + str(worker_index
)
1171 "net": VimInteractionNet(self
.db
, self
.my_vims
, self
.db_vims
, self
.logger
),
1172 "vdu": VimInteractionVdu(self
.db
, self
.my_vims
, self
.db_vims
, self
.logger
),
1173 "image": VimInteractionImage(
1174 self
.db
, self
.my_vims
, self
.db_vims
, self
.logger
1176 "flavor": VimInteractionFlavor(
1177 self
.db
, self
.my_vims
, self
.db_vims
, self
.logger
1179 "sdn_net": VimInteractionSdnNet(
1180 self
.db
, self
.my_vims
, self
.db_vims
, self
.logger
1183 self
.time_last_task_processed
= None
1184 # lists of tasks to delete because nsrs or vnfrs has been deleted from db
1185 self
.tasks_to_delete
= []
1186 # it is idle when there are not vim_targets associated
1188 self
.task_locked_time
= config
["global"]["task_locked_time"]
1190 def insert_task(self
, task
):
1192 self
.task_queue
.put(task
, False)
1195 raise NsWorkerException("timeout inserting a task")
1197 def terminate(self
):
1198 self
.insert_task("exit")
1200 def del_task(self
, task
):
1201 with self
.task_lock
:
1202 if task
["status"] == "SCHEDULED":
1203 task
["status"] = "SUPERSEDED"
1205 else: # task["status"] == "processing"
1206 self
.task_lock
.release()
1209 def _process_vim_config(self
, target_id
, db_vim
):
1211 Process vim config, creating vim configuration files as ca_cert
1212 :param target_id: vim/sdn/wim + id
1213 :param db_vim: Vim dictionary obtained from database
1214 :return: None. Modifies vim. Creates a folder target_id:worker_index and several files
1216 if not db_vim
.get("config"):
1222 if db_vim
["config"].get("ca_cert_content"):
1223 file_name
= "{}:{}".format(target_id
, self
.worker_index
)
1227 except FileExistsError
:
1230 file_name
= file_name
+ "/ca_cert"
1232 with
open(file_name
, "w") as f
:
1233 f
.write(db_vim
["config"]["ca_cert_content"])
1234 del db_vim
["config"]["ca_cert_content"]
1235 db_vim
["config"]["ca_cert"] = file_name
1236 except Exception as e
:
1237 raise NsWorkerException(
1238 "Error writing to file '{}': {}".format(file_name
, e
)
1241 def _load_plugin(self
, name
, type="vim"):
1242 # type can be vim or sdn
1243 if "rovim_dummy" not in self
.plugins
:
1244 self
.plugins
["rovim_dummy"] = VimDummyConnector
1246 if "rosdn_dummy" not in self
.plugins
:
1247 self
.plugins
["rosdn_dummy"] = SdnDummyConnector
1249 if name
in self
.plugins
:
1250 return self
.plugins
[name
]
1253 for ep
in entry_points(group
="osm_ro{}.plugins".format(type), name
=name
):
1254 self
.plugins
[name
] = ep
.load()
1255 except Exception as e
:
1256 raise NsWorkerException("Cannot load plugin osm_{}: {}".format(name
, e
))
1258 if name
and name
not in self
.plugins
:
1259 raise NsWorkerException(
1260 "Plugin 'osm_{n}' has not been installed".format(n
=name
)
1263 return self
.plugins
[name
]
1265 def _unload_vim(self
, target_id
):
1267 Unload a vim_account. Removes it from self db_vims dictionary, my_vims dictionary and vim_targets list
1268 :param target_id: Contains type:_id; where type can be 'vim', ...
1272 self
.db_vims
.pop(target_id
, None)
1273 self
.my_vims
.pop(target_id
, None)
1275 if target_id
in self
.vim_targets
:
1276 self
.vim_targets
.remove(target_id
)
1278 self
.logger
.info("Unloaded {}".format(target_id
))
1279 rmtree("{}:{}".format(target_id
, self
.worker_index
))
1280 except FileNotFoundError
:
1281 pass # this is raised by rmtree if folder does not exist
1282 except Exception as e
:
1283 self
.logger
.error("Cannot unload {}: {}".format(target_id
, e
))
1285 def _check_vim(self
, target_id
):
1287 Load a VIM/SDN/WIM (if not loaded) and check connectivity, updating database with ENABLE or ERROR
1288 :param target_id: Contains type:_id; type can be 'vim', 'sdn' or 'wim'
1291 target
, _
, _id
= target_id
.partition(":")
1297 loaded
= target_id
in self
.vim_targets
1307 step
= "Getting {} from db".format(target_id
)
1308 db_vim
= self
.db
.get_one(target_database
, {"_id": _id
})
1310 for op_index
, operation
in enumerate(
1311 db_vim
["_admin"].get("operations", ())
1313 if operation
["operationState"] != "PROCESSING":
1316 locked_at
= operation
.get("locked_at")
1318 if locked_at
is not None and locked_at
>= now
- self
.task_locked_time
:
1319 # some other thread is doing this operation
1323 op_text
= "_admin.operations.{}.".format(op_index
)
1325 if not self
.db
.set_one(
1329 op_text
+ "operationState": "PROCESSING",
1330 op_text
+ "locked_at": locked_at
,
1333 op_text
+ "locked_at": now
,
1334 "admin.current_operation": op_index
,
1336 fail_on_empty
=False,
1340 unset_dict
[op_text
+ "locked_at"] = None
1341 unset_dict
["current_operation"] = None
1342 step
= "Loading " + target_id
1343 error_text
= self
._load
_vim
(target_id
)
1346 step
= "Checking connectivity"
1349 self
.my_vims
[target_id
].check_vim_connectivity()
1351 self
.my_vims
[target_id
].check_credentials()
1353 update_dict
["_admin.operationalState"] = "ENABLED"
1354 update_dict
["_admin.detailed-status"] = ""
1355 unset_dict
[op_text
+ "detailed-status"] = None
1356 update_dict
[op_text
+ "operationState"] = "COMPLETED"
1360 except Exception as e
:
1361 error_text
= "{}: {}".format(step
, e
)
1362 self
.logger
.error("{} for {}: {}".format(step
, target_id
, e
))
1365 if update_dict
or unset_dict
:
1367 update_dict
[op_text
+ "operationState"] = "FAILED"
1368 update_dict
[op_text
+ "detailed-status"] = error_text
1369 unset_dict
.pop(op_text
+ "detailed-status", None)
1370 update_dict
["_admin.operationalState"] = "ERROR"
1371 update_dict
["_admin.detailed-status"] = error_text
1374 update_dict
[op_text
+ "statusEnteredTime"] = now
1378 q_filter
={"_id": _id
},
1379 update_dict
=update_dict
,
1381 fail_on_empty
=False,
1385 self
._unload
_vim
(target_id
)
1387 def _reload_vim(self
, target_id
):
1388 if target_id
in self
.vim_targets
:
1389 self
._load
_vim
(target_id
)
1391 # if the vim is not loaded, but database information of VIM is cached at self.db_vims,
1392 # just remove it to force load again next time it is needed
1393 self
.db_vims
.pop(target_id
, None)
1395 def _load_vim(self
, target_id
):
1397 Load or reload a vim_account, sdn_controller or wim_account.
1398 Read content from database, load the plugin if not loaded.
1399 In case of error loading the plugin, it load a failing VIM_connector
1400 It fills self db_vims dictionary, my_vims dictionary and vim_targets list
1401 :param target_id: Contains type:_id; where type can be 'vim', ...
1402 :return: None if ok, descriptive text if error
1404 target
, _
, _id
= target_id
.partition(":")
1416 step
= "Getting {}={} from db".format(target
, _id
)
1417 # TODO process for wim, sdnc, ...
1418 vim
= self
.db
.get_one(target_database
, {"_id": _id
})
1420 # if deep_get(vim, "config", "sdn-controller"):
1421 # step = "Getting sdn-controller-id='{}' from db".format(vim["config"]["sdn-controller"])
1422 # db_sdn = self.db.get_one("sdns", {"_id": vim["config"]["sdn-controller"]})
1424 step
= "Decrypting password"
1425 schema_version
= vim
.get("schema_version")
1426 self
.db
.encrypt_decrypt_fields(
1429 fields
=("password", "secret"),
1430 schema_version
=schema_version
,
1433 self
._process
_vim
_config
(target_id
, vim
)
1436 plugin_name
= "rovim_" + vim
["vim_type"]
1437 step
= "Loading plugin '{}'".format(plugin_name
)
1438 vim_module_conn
= self
._load
_plugin
(plugin_name
)
1439 step
= "Loading {}'".format(target_id
)
1440 self
.my_vims
[target_id
] = vim_module_conn(
1443 tenant_id
=vim
.get("vim_tenant_id"),
1444 tenant_name
=vim
.get("vim_tenant_name"),
1447 user
=vim
["vim_user"],
1448 passwd
=vim
["vim_password"],
1449 config
=vim
.get("config") or {},
1453 plugin_name
= "rosdn_" + vim
["type"]
1454 step
= "Loading plugin '{}'".format(plugin_name
)
1455 vim_module_conn
= self
._load
_plugin
(plugin_name
, "sdn")
1456 step
= "Loading {}'".format(target_id
)
1458 wim_config
= wim
.pop("config", {}) or {}
1459 wim
["uuid"] = wim
["_id"]
1460 wim
["wim_url"] = wim
["url"]
1463 wim_config
["dpid"] = wim
.pop("dpid")
1465 if wim
.get("switch_id"):
1466 wim_config
["switch_id"] = wim
.pop("switch_id")
1468 # wim, wim_account, config
1469 self
.my_vims
[target_id
] = vim_module_conn(wim
, wim
, wim_config
)
1470 self
.db_vims
[target_id
] = vim
1471 self
.error_status
= None
1474 "Connector loaded for {}, plugin={}".format(target_id
, plugin_name
)
1476 except Exception as e
:
1478 "Cannot load {} plugin={}: {} {}".format(
1479 target_id
, plugin_name
, step
, e
1483 self
.db_vims
[target_id
] = vim
or {}
1484 self
.db_vims
[target_id
] = FailingConnector(str(e
))
1485 error_status
= "{} Error: {}".format(step
, e
)
1489 if target_id
not in self
.vim_targets
:
1490 self
.vim_targets
.append(target_id
)
1492 def _get_db_task(self
):
1494 Read actions from database and reload them at memory. Fill self.refresh_list, pending_list, vim_actions
1499 if not self
.time_last_task_processed
:
1500 self
.time_last_task_processed
= now
1504 locked
= self
.db
.set_one(
1507 "target_id": self
.vim_targets
,
1508 "tasks.status": ["SCHEDULED", "BUILD", "DONE", "FAILED"],
1509 "locked_at.lt": now
- self
.task_locked_time
,
1510 "to_check_at.lt": self
.time_last_task_processed
,
1512 update_dict
={"locked_by": self
.my_id
, "locked_at": now
},
1513 fail_on_empty
=False,
1518 ro_task
= self
.db
.get_one(
1521 "target_id": self
.vim_targets
,
1522 "tasks.status": ["SCHEDULED", "BUILD", "DONE", "FAILED"],
1528 if self
.time_last_task_processed
== now
:
1529 self
.time_last_task_processed
= None
1532 self
.time_last_task_processed
= now
1533 # self.time_last_task_processed = min(self.time_last_task_processed + 1000, now)
1535 except DbException
as e
:
1536 self
.logger
.error("Database exception at _get_db_task: {}".format(e
))
1537 except Exception as e
:
1538 self
.logger
.critical(
1539 "Unexpected exception at _get_db_task: {}".format(e
), exc_info
=True
1544 def _delete_task(self
, ro_task
, task_index
, task_depends
, db_update
):
1546 Determine if this task need to be done or superseded
1549 my_task
= ro_task
["tasks"][task_index
]
1550 task_id
= my_task
["task_id"]
1551 needed_delete
= ro_task
["vim_info"]["created"] or ro_task
["vim_info"].get(
1552 "created_items", False
1555 if my_task
["status"] == "FAILED":
1556 return None, None # TODO need to be retry??
1559 for index
, task
in enumerate(ro_task
["tasks"]):
1560 if index
== task_index
or not task
:
1564 my_task
["target_record"] == task
["target_record"]
1565 and task
["action"] == "CREATE"
1568 db_update
["tasks.{}.status".format(index
)] = task
[
1571 elif task
["action"] == "CREATE" and task
["status"] not in (
1575 needed_delete
= False
1578 return self
.item2class
[my_task
["item"]].delete(ro_task
, task_index
)
1580 return "SUPERSEDED", None
1581 except Exception as e
:
1582 if not isinstance(e
, NsWorkerException
):
1583 self
.logger
.critical(
1584 "Unexpected exception at _delete_task task={}: {}".format(
1590 return "FAILED", {"vim_status": "VIM_ERROR", "vim_details": str(e
)}
1592 def _create_task(self
, ro_task
, task_index
, task_depends
, db_update
):
1594 Determine if this task need to create something at VIM
1597 my_task
= ro_task
["tasks"][task_index
]
1598 task_id
= my_task
["task_id"]
1601 if my_task
["status"] == "FAILED":
1602 return None, None # TODO need to be retry??
1603 elif my_task
["status"] == "SCHEDULED":
1604 # check if already created by another task
1605 for index
, task
in enumerate(ro_task
["tasks"]):
1606 if index
== task_index
or not task
:
1609 if task
["action"] == "CREATE" and task
["status"] not in (
1614 return task
["status"], "COPY_VIM_INFO"
1617 task_status
, ro_vim_item_update
= self
.item2class
[my_task
["item"]].new(
1618 ro_task
, task_index
, task_depends
1620 # TODO update other CREATE tasks
1621 except Exception as e
:
1622 if not isinstance(e
, NsWorkerException
):
1624 "Error executing task={}: {}".format(task_id
, e
), exc_info
=True
1627 task_status
= "FAILED"
1628 ro_vim_item_update
= {"vim_status": "VIM_ERROR", "vim_details": str(e
)}
1629 # TODO update ro_vim_item_update
1631 return task_status
, ro_vim_item_update
1635 def _get_dependency(self
, task_id
, ro_task
=None, target_id
=None):
1637 Look for dependency task
1638 :param task_id: Can be one of
1639 1. target_vim+blank+task.target_record_id: "(vim|sdn|wim):<id> (vnfrs|nsrs):(vld|vdu|flavor|image).<id>"
1640 2. task.target_record_id: "(vnfrs|nsrs):(vld|vdu|flavor|image).<id>"
1641 3. task.task_id: "<action_id>:number"
1644 :return: database ro_task plus index of task
1647 task_id
.startswith("vim:")
1648 or task_id
.startswith("sdn:")
1649 or task_id
.startswith("wim:")
1651 target_id
, _
, task_id
= task_id
.partition(" ")
1653 if task_id
.startswith("nsrs:") or task_id
.startswith("vnfrs:"):
1654 ro_task_dependency
= self
.db
.get_one(
1656 q_filter
={"target_id": target_id
, "tasks.target_record_id": task_id
},
1657 fail_on_empty
=False,
1660 if ro_task_dependency
:
1661 for task_index
, task
in enumerate(ro_task_dependency
["tasks"]):
1662 if task
["target_record_id"] == task_id
:
1663 return ro_task_dependency
, task_index
1667 for task_index
, task
in enumerate(ro_task
["tasks"]):
1668 if task
and task
["task_id"] == task_id
:
1669 return ro_task
, task_index
1671 ro_task_dependency
= self
.db
.get_one(
1674 "tasks.ANYINDEX.task_id": task_id
,
1675 "tasks.ANYINDEX.target_record.ne": None,
1677 fail_on_empty
=False,
1680 if ro_task_dependency
:
1681 for task_index
, task
in ro_task_dependency
["tasks"]:
1682 if task
["task_id"] == task_id
:
1683 return ro_task_dependency
, task_index
1684 raise NsWorkerException("Cannot get depending task {}".format(task_id
))
1686 def _process_pending_tasks(self
, ro_task
):
1687 ro_task_id
= ro_task
["_id"]
1690 next_check_at
= now
+ (24 * 60 * 60)
1691 db_ro_task_update
= {}
1693 def _update_refresh(new_status
):
1694 # compute next_refresh
1696 nonlocal next_check_at
1697 nonlocal db_ro_task_update
1700 next_refresh
= time
.time()
1702 if task
["item"] in ("image", "flavor"):
1703 next_refresh
+= self
.REFRESH_IMAGE
1704 elif new_status
== "BUILD":
1705 next_refresh
+= self
.REFRESH_BUILD
1706 elif new_status
== "DONE":
1707 next_refresh
+= self
.REFRESH_ACTIVE
1709 next_refresh
+= self
.REFRESH_ERROR
1711 next_check_at
= min(next_check_at
, next_refresh
)
1712 db_ro_task_update
["vim_info.refresh_at"] = next_refresh
1713 ro_task
["vim_info"]["refresh_at"] = next_refresh
1716 # 0: get task_status_create
1718 task_status_create
= None
1722 for t
in ro_task
["tasks"]
1724 and t
["action"] == "CREATE"
1725 and t
["status"] in ("BUILD", "DONE")
1731 task_status_create
= task_create
["status"]
1733 # 1: look for tasks in status SCHEDULED, or in status CREATE if action is DONE or BUILD
1734 for task_action
in ("DELETE", "CREATE", "EXEC"):
1735 db_vim_update
= None
1738 for task_index
, task
in enumerate(ro_task
["tasks"]):
1740 continue # task deleted
1743 target_update
= None
1747 task_action
in ("DELETE", "EXEC")
1748 and task
["status"] not in ("SCHEDULED", "BUILD")
1750 or task
["action"] != task_action
1752 task_action
== "CREATE"
1753 and task
["status"] in ("FINISHED", "SUPERSEDED")
1758 task_path
= "tasks.{}.status".format(task_index
)
1760 db_vim_info_update
= None
1762 if task
["status"] == "SCHEDULED":
1763 # check if tasks that this depends on have been completed
1764 dependency_not_completed
= False
1766 for dependency_task_id
in task
.get("depends_on") or ():
1769 dependency_task_index
,
1770 ) = self
._get
_dependency
(
1771 dependency_task_id
, target_id
=ro_task
["target_id"]
1773 dependency_task
= dependency_ro_task
["tasks"][
1774 dependency_task_index
1777 if dependency_task
["status"] == "SCHEDULED":
1778 dependency_not_completed
= True
1779 next_check_at
= min(
1780 next_check_at
, dependency_ro_task
["to_check_at"]
1782 # must allow dependent task to be processed first
1783 # to do this set time after last_task_processed
1784 next_check_at
= max(
1785 self
.time_last_task_processed
, next_check_at
1788 elif dependency_task
["status"] == "FAILED":
1789 error_text
= "Cannot {} {} because depends on failed {} {} id={}): {}".format(
1792 dependency_task
["action"],
1793 dependency_task
["item"],
1795 dependency_ro_task
["vim_info"].get(
1800 "task={} {}".format(task
["task_id"], error_text
)
1802 raise NsWorkerException(error_text
)
1804 task_depends
[dependency_task_id
] = dependency_ro_task
[
1808 "TASK-{}".format(dependency_task_id
)
1809 ] = dependency_ro_task
["vim_info"]["vim_id"]
1811 if dependency_not_completed
:
1812 # TODO set at vim_info.vim_details that it is waiting
1815 # before calling VIM-plugin as it can take more than task_locked_time, insert to LockRenew
1816 # the task of renew this locking. It will update database locket_at periodically
1818 lock_object
= LockRenew
.add_lock_object(
1819 "ro_tasks", ro_task
, self
1822 if task
["action"] == "DELETE":
1823 (new_status
, db_vim_info_update
,) = self
._delete
_task
(
1824 ro_task
, task_index
, task_depends
, db_ro_task_update
1827 "FINISHED" if new_status
== "DONE" else new_status
1829 # ^with FINISHED instead of DONE it will not be refreshing
1831 if new_status
in ("FINISHED", "SUPERSEDED"):
1832 target_update
= "DELETE"
1833 elif task
["action"] == "EXEC":
1838 ) = self
.item2class
[task
["item"]].exec(
1839 ro_task
, task_index
, task_depends
1842 "FINISHED" if new_status
== "DONE" else new_status
1844 # ^with FINISHED instead of DONE it will not be refreshing
1847 # load into database the modified db_task_update "retries" and "next_retry"
1848 if db_task_update
.get("retries"):
1850 "tasks.{}.retries".format(task_index
)
1851 ] = db_task_update
["retries"]
1853 next_check_at
= time
.time() + db_task_update
.get(
1856 target_update
= None
1857 elif task
["action"] == "CREATE":
1858 if task
["status"] == "SCHEDULED":
1859 if task_status_create
:
1860 new_status
= task_status_create
1861 target_update
= "COPY_VIM_INFO"
1863 new_status
, db_vim_info_update
= self
.item2class
[
1865 ].new(ro_task
, task_index
, task_depends
)
1866 # self._create_task(ro_task, task_index, task_depends, db_ro_task_update)
1867 _update_refresh(new_status
)
1870 ro_task
["vim_info"]["refresh_at"]
1871 and now
> ro_task
["vim_info"]["refresh_at"]
1873 new_status
, db_vim_info_update
= self
.item2class
[
1876 _update_refresh(new_status
)
1878 except Exception as e
:
1879 new_status
= "FAILED"
1880 db_vim_info_update
= {
1881 "vim_status": "VIM_ERROR",
1882 "vim_details": str(e
),
1886 e
, (NsWorkerException
, vimconn
.VimConnException
)
1889 "Unexpected exception at _delete_task task={}: {}".format(
1896 if db_vim_info_update
:
1897 db_vim_update
= db_vim_info_update
.copy()
1898 db_ro_task_update
.update(
1901 for k
, v
in db_vim_info_update
.items()
1904 ro_task
["vim_info"].update(db_vim_info_update
)
1907 if task_action
== "CREATE":
1908 task_status_create
= new_status
1909 db_ro_task_update
[task_path
] = new_status
1911 if target_update
or db_vim_update
:
1912 if target_update
== "DELETE":
1913 self
._update
_target
(task
, None)
1914 elif target_update
== "COPY_VIM_INFO":
1915 self
._update
_target
(task
, ro_task
["vim_info"])
1917 self
._update
_target
(task
, db_vim_update
)
1919 except Exception as e
:
1921 isinstance(e
, DbException
)
1922 and e
.http_code
== HTTPStatus
.NOT_FOUND
1924 # if the vnfrs or nsrs has been removed from database, this task must be removed
1926 "marking to delete task={}".format(task
["task_id"])
1928 self
.tasks_to_delete
.append(task
)
1931 "Unexpected exception at _update_target task={}: {}".format(
1937 locked_at
= ro_task
["locked_at"]
1941 lock_object
["locked_at"],
1942 lock_object
["locked_at"] + self
.task_locked_time
,
1944 # locked_at contains two times to avoid race condition. In case the lock has been renew, it will
1945 # contain exactly locked_at + self.task_locked_time
1946 LockRenew
.remove_lock_object(lock_object
)
1949 "_id": ro_task
["_id"],
1950 "to_check_at": ro_task
["to_check_at"],
1951 "locked_at": locked_at
,
1953 # modify own task. Try filtering by to_next_check. For race condition if to_check_at has been modified,
1954 # outside this task (by ro_nbi) do not update it
1955 db_ro_task_update
["locked_by"] = None
1956 # locked_at converted to int only for debugging. When has not decimals it means it has been unlocked
1957 db_ro_task_update
["locked_at"] = int(now
- self
.task_locked_time
)
1958 db_ro_task_update
["modified_at"] = now
1959 db_ro_task_update
["to_check_at"] = next_check_at
1961 if not self
.db
.set_one(
1963 update_dict
=db_ro_task_update
,
1965 fail_on_empty
=False,
1967 del db_ro_task_update
["to_check_at"]
1968 del q_filter
["to_check_at"]
1972 update_dict
=db_ro_task_update
,
1975 except DbException
as e
:
1977 "ro_task={} Error updating database {}".format(ro_task_id
, e
)
1979 except Exception as e
:
1981 "Error executing ro_task={}: {}".format(ro_task_id
, e
), exc_info
=True
1984 def _update_target(self
, task
, ro_vim_item_update
):
1985 table
, _
, temp
= task
["target_record"].partition(":")
1986 _id
, _
, path_vim_status
= temp
.partition(":")
1987 path_item
= path_vim_status
[: path_vim_status
.rfind(".")]
1988 path_item
= path_item
[: path_item
.rfind(".")]
1989 # path_vim_status: dot separated list targeting vim information, e.g. "vdur.10.vim_info.vim:id"
1990 # path_item: dot separated list targeting record information, e.g. "vdur.10"
1992 if ro_vim_item_update
:
1994 path_vim_status
+ "." + k
: v
1995 for k
, v
in ro_vim_item_update
.items()
1997 in ("vim_id", "vim_details", "vim_name", "vim_status", "interfaces")
2000 if path_vim_status
.startswith("vdur."):
2001 # for backward compatibility, add vdur.name apart from vdur.vim_name
2002 if ro_vim_item_update
.get("vim_name"):
2003 update_dict
[path_item
+ ".name"] = ro_vim_item_update
["vim_name"]
2005 # for backward compatibility, add vdur.vim-id apart from vdur.vim_id
2006 if ro_vim_item_update
.get("vim_id"):
2007 update_dict
[path_item
+ ".vim-id"] = ro_vim_item_update
["vim_id"]
2009 # update general status
2010 if ro_vim_item_update
.get("vim_status"):
2011 update_dict
[path_item
+ ".status"] = ro_vim_item_update
[
2015 if ro_vim_item_update
.get("interfaces"):
2016 path_interfaces
= path_item
+ ".interfaces"
2018 for i
, iface
in enumerate(ro_vim_item_update
.get("interfaces")):
2022 path_interfaces
+ ".{}.".format(i
) + k
: v
2023 for k
, v
in iface
.items()
2024 if k
in ("vlan", "compute_node", "pci")
2028 # put ip_address and mac_address with ip-address and mac-address
2029 if iface
.get("ip_address"):
2031 path_interfaces
+ ".{}.".format(i
) + "ip-address"
2032 ] = iface
["ip_address"]
2034 if iface
.get("mac_address"):
2036 path_interfaces
+ ".{}.".format(i
) + "mac-address"
2037 ] = iface
["mac_address"]
2039 if iface
.get("mgmt_vnf_interface") and iface
.get("ip_address"):
2040 update_dict
["ip-address"] = iface
.get("ip_address").split(
2044 if iface
.get("mgmt_vdu_interface") and iface
.get("ip_address"):
2045 update_dict
[path_item
+ ".ip-address"] = iface
.get(
2049 self
.db
.set_one(table
, q_filter
={"_id": _id
}, update_dict
=update_dict
)
2051 update_dict
= {path_item
+ ".status": "DELETED"}
2054 q_filter
={"_id": _id
},
2055 update_dict
=update_dict
,
2056 unset
={path_vim_status
: None},
2059 def _process_delete_db_tasks(self
):
2061 Delete task from database because vnfrs or nsrs or both have been deleted
2062 :return: None. Uses and modify self.tasks_to_delete
2064 while self
.tasks_to_delete
:
2065 task
= self
.tasks_to_delete
[0]
2066 vnfrs_deleted
= None
2067 nsr_id
= task
["nsr_id"]
2069 if task
["target_record"].startswith("vnfrs:"):
2070 # check if nsrs is present
2071 if self
.db
.get_one("nsrs", {"_id": nsr_id
}, fail_on_empty
=False):
2072 vnfrs_deleted
= task
["target_record"].split(":")[1]
2075 self
.delete_db_tasks(self
.db
, nsr_id
, vnfrs_deleted
)
2076 except Exception as e
:
2078 "Error deleting task={}: {}".format(task
["task_id"], e
)
2080 self
.tasks_to_delete
.pop(0)
2083 def delete_db_tasks(db
, nsr_id
, vnfrs_deleted
):
2085 Static method because it is called from osm_ng_ro.ns
2086 :param db: instance of database to use
2087 :param nsr_id: affected nsrs id
2088 :param vnfrs_deleted: only tasks with this vnfr id. If None, all affected by nsr_id
2089 :return: None, exception is fails
2092 for retry
in range(retries
):
2093 ro_tasks
= db
.get_list("ro_tasks", {"tasks.nsr_id": nsr_id
})
2097 for ro_task
in ro_tasks
:
2099 to_delete_ro_task
= True
2101 for index
, task
in enumerate(ro_task
["tasks"]):
2104 elif (not vnfrs_deleted
and task
["nsr_id"] == nsr_id
) or (
2106 and task
["target_record"].startswith("vnfrs:" + vnfrs_deleted
)
2108 db_update
["tasks.{}".format(index
)] = None
2110 # used by other nsr, ro_task cannot be deleted
2111 to_delete_ro_task
= False
2113 # delete or update if nobody has changed ro_task meanwhile. Used modified_at for known if changed
2114 if to_delete_ro_task
:
2118 "_id": ro_task
["_id"],
2119 "modified_at": ro_task
["modified_at"],
2121 fail_on_empty
=False,
2125 db_update
["modified_at"] = now
2129 "_id": ro_task
["_id"],
2130 "modified_at": ro_task
["modified_at"],
2132 update_dict
=db_update
,
2133 fail_on_empty
=False,
2139 raise NsWorkerException("Exceeded {} retries".format(retries
))
2143 self
.logger
.info("Starting")
2145 # step 1: get commands from queue
2147 if self
.vim_targets
:
2148 task
= self
.task_queue
.get(block
=False)
2151 self
.logger
.debug("enters in idle state")
2153 task
= self
.task_queue
.get(block
=True)
2156 if task
[0] == "terminate":
2158 elif task
[0] == "load_vim":
2159 self
.logger
.info("order to load vim {}".format(task
[1]))
2160 self
._load
_vim
(task
[1])
2161 elif task
[0] == "unload_vim":
2162 self
.logger
.info("order to unload vim {}".format(task
[1]))
2163 self
._unload
_vim
(task
[1])
2164 elif task
[0] == "reload_vim":
2165 self
._reload
_vim
(task
[1])
2166 elif task
[0] == "check_vim":
2167 self
.logger
.info("order to check vim {}".format(task
[1]))
2168 self
._check
_vim
(task
[1])
2170 except Exception as e
:
2171 if isinstance(e
, queue
.Empty
):
2174 self
.logger
.critical(
2175 "Error processing task: {}".format(e
), exc_info
=True
2178 # step 2: process pending_tasks, delete not needed tasks
2180 if self
.tasks_to_delete
:
2181 self
._process
_delete
_db
_tasks
()
2183 ro_task
= self
._get
_db
_task
()
2185 self
._process
_pending
_tasks
(ro_task
)
2189 except Exception as e
:
2190 self
.logger
.critical(
2191 "Unexpected exception at run: " + str(e
), exc_info
=True
2194 self
.logger
.info("Finishing")