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
46 __author__
= "Alfonso Tierno"
47 __date__
= "$28-Sep-2017 12:07:15$"
50 def deep_get(target_dict
, *args
, **kwargs
):
52 Get a value from target_dict entering in the nested keys. If keys does not exist, it returns None
53 Example target_dict={a: {b: 5}}; key_list=[a,b] returns 5; both key_list=[a,b,c] and key_list=[f,h] return None
54 :param target_dict: dictionary to be read
55 :param args: list of keys to read from target_dict
56 :param kwargs: only can contain default=value to return if key is not present in the nested dictionary
57 :return: The wanted value if exist, None or default otherwise
60 if not isinstance(target_dict
, dict) or key
not in target_dict
:
61 return kwargs
.get("default")
62 target_dict
= target_dict
[key
]
66 class NsWorkerException(Exception):
70 class FailingConnector
:
71 def __init__(self
, error_msg
):
72 self
.error_msg
= error_msg
74 for method
in dir(vimconn
.VimConnector
):
77 self
, method
, Mock(side_effect
=vimconn
.VimConnException(error_msg
))
80 for method
in dir(sdnconn
.SdnConnectorBase
):
83 self
, method
, Mock(side_effect
=sdnconn
.SdnConnectorError(error_msg
))
87 class NsWorkerExceptionNotFound(NsWorkerException
):
91 class VimInteractionBase
:
92 """Base class to call VIM/SDN for creating, deleting and refresh networks, VMs, flavors, ...
93 It implements methods that does nothing and return ok"""
95 def __init__(self
, db
, my_vims
, db_vims
, logger
):
98 self
.my_vims
= my_vims
99 self
.db_vims
= db_vims
101 def new(self
, ro_task
, task_index
, task_depends
):
104 def refresh(self
, ro_task
):
105 """skip calling VIM to get image, flavor status. Assumes ok"""
106 if ro_task
["vim_info"]["vim_status"] == "VIM_ERROR":
111 def delete(self
, ro_task
, task_index
):
112 """skip calling VIM to delete image. Assumes ok"""
115 def exec(self
, ro_task
, task_index
, task_depends
):
116 return "DONE", None, None
119 class VimInteractionNet(VimInteractionBase
):
120 def new(self
, ro_task
, task_index
, task_depends
):
122 task
= ro_task
["tasks"][task_index
]
123 task_id
= task
["task_id"]
126 target_vim
= self
.my_vims
[ro_task
["target_id"]]
128 mgmtnet_defined_in_vim
= False
132 if task
.get("find_params"):
133 # if management, get configuration of VIM
134 if task
["find_params"].get("filter_dict"):
135 vim_filter
= task
["find_params"]["filter_dict"]
137 elif task
["find_params"].get("mgmt"):
140 self
.db_vims
[ro_task
["target_id"]],
142 "management_network_id",
144 mgmtnet_defined_in_vim
= True
146 "id": self
.db_vims
[ro_task
["target_id"]]["config"][
147 "management_network_id"
151 self
.db_vims
[ro_task
["target_id"]],
153 "management_network_name",
155 mgmtnet_defined_in_vim
= True
157 "name": self
.db_vims
[ro_task
["target_id"]]["config"][
158 "management_network_name"
162 vim_filter
= {"name": task
["find_params"]["name"]}
164 raise NsWorkerExceptionNotFound(
165 "Invalid find_params for new_net {}".format(task
["find_params"])
168 vim_nets
= target_vim
.get_network_list(vim_filter
)
169 if not vim_nets
and not task
.get("params"):
170 # If there is mgmt-network in the descriptor,
171 # there is no mapping of that network to a VIM network in the descriptor,
172 # also there is no mapping in the "--config" parameter or at VIM creation;
173 # that mgmt-network will be created.
174 if mgmtnet
and not mgmtnet_defined_in_vim
:
176 vim_filter
.get("name")
177 if vim_filter
.get("name")
178 else vim_filter
.get("id")[:16]
180 vim_net_id
, created_items
= target_vim
.new_network(
184 "Created mgmt network vim_net_id: {}".format(vim_net_id
)
188 raise NsWorkerExceptionNotFound(
189 "Network not found with this criteria: '{}'".format(
190 task
.get("find_params")
193 elif len(vim_nets
) > 1:
194 raise NsWorkerException(
195 "More than one network found with this criteria: '{}'".format(
201 vim_net_id
= vim_nets
[0]["id"]
204 params
= task
["params"]
205 vim_net_id
, created_items
= target_vim
.new_network(**params
)
208 ro_vim_item_update
= {
209 "vim_id": vim_net_id
,
210 "vim_status": "BUILD",
212 "created_items": created_items
,
216 "task={} {} new-net={} created={}".format(
217 task_id
, ro_task
["target_id"], vim_net_id
, created
221 return "BUILD", ro_vim_item_update
222 except (vimconn
.VimConnException
, NsWorkerException
) as e
:
224 "task={} vim={} new-net: {}".format(task_id
, ro_task
["target_id"], e
)
226 ro_vim_item_update
= {
227 "vim_status": "VIM_ERROR",
229 "vim_details": str(e
),
232 return "FAILED", ro_vim_item_update
234 def refresh(self
, ro_task
):
235 """Call VIM to get network status"""
236 ro_task_id
= ro_task
["_id"]
237 target_vim
= self
.my_vims
[ro_task
["target_id"]]
238 vim_id
= ro_task
["vim_info"]["vim_id"]
239 net_to_refresh_list
= [vim_id
]
242 vim_dict
= target_vim
.refresh_nets_status(net_to_refresh_list
)
243 vim_info
= vim_dict
[vim_id
]
245 if vim_info
["status"] == "ACTIVE":
247 elif vim_info
["status"] == "BUILD":
248 task_status
= "BUILD"
250 task_status
= "FAILED"
251 except vimconn
.VimConnException
as e
:
252 # Mark all tasks at VIM_ERROR status
254 "ro_task={} vim={} get-net={}: {}".format(
255 ro_task_id
, ro_task
["target_id"], vim_id
, e
258 vim_info
= {"status": "VIM_ERROR", "error_msg": str(e
)}
259 task_status
= "FAILED"
261 ro_vim_item_update
= {}
262 if ro_task
["vim_info"]["vim_status"] != vim_info
["status"]:
263 ro_vim_item_update
["vim_status"] = vim_info
["status"]
265 if ro_task
["vim_info"]["vim_name"] != vim_info
.get("name"):
266 ro_vim_item_update
["vim_name"] = vim_info
.get("name")
268 if vim_info
["status"] in ("ERROR", "VIM_ERROR"):
269 if ro_task
["vim_info"]["vim_details"] != vim_info
.get("error_msg"):
270 ro_vim_item_update
["vim_details"] = vim_info
.get("error_msg")
271 elif vim_info
["status"] == "DELETED":
272 ro_vim_item_update
["vim_id"] = None
273 ro_vim_item_update
["vim_details"] = "Deleted externally"
275 if ro_task
["vim_info"]["vim_details"] != vim_info
["vim_info"]:
276 ro_vim_item_update
["vim_details"] = vim_info
["vim_info"]
278 if ro_vim_item_update
:
280 "ro_task={} {} get-net={}: status={} {}".format(
282 ro_task
["target_id"],
284 ro_vim_item_update
.get("vim_status"),
285 ro_vim_item_update
.get("vim_details")
286 if ro_vim_item_update
.get("vim_status") != "ACTIVE"
291 return task_status
, ro_vim_item_update
293 def delete(self
, ro_task
, task_index
):
294 task
= ro_task
["tasks"][task_index
]
295 task_id
= task
["task_id"]
296 net_vim_id
= ro_task
["vim_info"]["vim_id"]
297 ro_vim_item_update_ok
= {
298 "vim_status": "DELETED",
300 "vim_details": "DELETED",
305 if net_vim_id
or ro_task
["vim_info"]["created_items"]:
306 target_vim
= self
.my_vims
[ro_task
["target_id"]]
307 target_vim
.delete_network(
308 net_vim_id
, ro_task
["vim_info"]["created_items"]
310 except vimconn
.VimConnNotFoundException
:
311 ro_vim_item_update_ok
["vim_details"] = "already deleted"
312 except vimconn
.VimConnException
as e
:
314 "ro_task={} vim={} del-net={}: {}".format(
315 ro_task
["_id"], ro_task
["target_id"], net_vim_id
, e
318 ro_vim_item_update
= {
319 "vim_status": "VIM_ERROR",
320 "vim_details": "Error while deleting: {}".format(e
),
323 return "FAILED", ro_vim_item_update
326 "task={} {} del-net={} {}".format(
328 ro_task
["target_id"],
330 ro_vim_item_update_ok
.get("vim_details", ""),
334 return "DONE", ro_vim_item_update_ok
337 class VimInteractionVdu(VimInteractionBase
):
338 max_retries_inject_ssh_key
= 20 # 20 times
339 time_retries_inject_ssh_key
= 30 # wevery 30 seconds
341 def new(self
, ro_task
, task_index
, task_depends
):
342 task
= ro_task
["tasks"][task_index
]
343 task_id
= task
["task_id"]
346 target_vim
= self
.my_vims
[ro_task
["target_id"]]
350 params
= task
["params"]
351 params_copy
= deepcopy(params
)
352 net_list
= params_copy
["net_list"]
355 # change task_id into network_id
356 if "net_id" in net
and net
["net_id"].startswith("TASK-"):
357 network_id
= task_depends
[net
["net_id"]]
360 raise NsWorkerException(
361 "Cannot create VM because depends on a network not created or found "
362 "for {}".format(net
["net_id"])
365 net
["net_id"] = network_id
367 if params_copy
["image_id"].startswith("TASK-"):
368 params_copy
["image_id"] = task_depends
[params_copy
["image_id"]]
370 if params_copy
["flavor_id"].startswith("TASK-"):
371 params_copy
["flavor_id"] = task_depends
[params_copy
["flavor_id"]]
373 vim_vm_id
, created_items
= target_vim
.new_vminstance(**params_copy
)
374 interfaces
= [iface
["vim_id"] for iface
in params_copy
["net_list"]]
376 ro_vim_item_update
= {
378 "vim_status": "BUILD",
380 "created_items": created_items
,
382 "interfaces_vim_ids": interfaces
,
386 "task={} {} new-vm={} created={}".format(
387 task_id
, ro_task
["target_id"], vim_vm_id
, created
391 return "BUILD", ro_vim_item_update
392 except (vimconn
.VimConnException
, NsWorkerException
) as e
:
394 "task={} {} new-vm: {}".format(task_id
, ro_task
["target_id"], e
)
396 ro_vim_item_update
= {
397 "vim_status": "VIM_ERROR",
399 "vim_details": str(e
),
402 return "FAILED", ro_vim_item_update
404 def delete(self
, ro_task
, task_index
):
405 task
= ro_task
["tasks"][task_index
]
406 task_id
= task
["task_id"]
407 vm_vim_id
= ro_task
["vim_info"]["vim_id"]
408 ro_vim_item_update_ok
= {
409 "vim_status": "DELETED",
411 "vim_details": "DELETED",
416 if vm_vim_id
or ro_task
["vim_info"]["created_items"]:
417 target_vim
= self
.my_vims
[ro_task
["target_id"]]
418 target_vim
.delete_vminstance(
419 vm_vim_id
, ro_task
["vim_info"]["created_items"]
421 except vimconn
.VimConnNotFoundException
:
422 ro_vim_item_update_ok
["vim_details"] = "already deleted"
423 except vimconn
.VimConnException
as e
:
425 "ro_task={} vim={} del-vm={}: {}".format(
426 ro_task
["_id"], ro_task
["target_id"], vm_vim_id
, e
429 ro_vim_item_update
= {
430 "vim_status": "VIM_ERROR",
431 "vim_details": "Error while deleting: {}".format(e
),
434 return "FAILED", ro_vim_item_update
437 "task={} {} del-vm={} {}".format(
439 ro_task
["target_id"],
441 ro_vim_item_update_ok
.get("vim_details", ""),
445 return "DONE", ro_vim_item_update_ok
447 def refresh(self
, ro_task
):
448 """Call VIM to get vm status"""
449 ro_task_id
= ro_task
["_id"]
450 target_vim
= self
.my_vims
[ro_task
["target_id"]]
451 vim_id
= ro_task
["vim_info"]["vim_id"]
456 vm_to_refresh_list
= [vim_id
]
458 vim_dict
= target_vim
.refresh_vms_status(vm_to_refresh_list
)
459 vim_info
= vim_dict
[vim_id
]
461 if vim_info
["status"] == "ACTIVE":
463 elif vim_info
["status"] == "BUILD":
464 task_status
= "BUILD"
466 task_status
= "FAILED"
468 # try to load and parse vim_information
470 vim_info_info
= yaml
.safe_load(vim_info
["vim_info"])
471 if vim_info_info
.get("name"):
472 vim_info
["name"] = vim_info_info
["name"]
475 except vimconn
.VimConnException
as e
:
476 # Mark all tasks at VIM_ERROR status
478 "ro_task={} vim={} get-vm={}: {}".format(
479 ro_task_id
, ro_task
["target_id"], vim_id
, e
482 vim_info
= {"status": "VIM_ERROR", "error_msg": str(e
)}
483 task_status
= "FAILED"
485 ro_vim_item_update
= {}
487 # Interfaces cannot be present if e.g. VM is not present, that is status=DELETED
489 if vim_info
.get("interfaces"):
490 for vim_iface_id
in ro_task
["vim_info"]["interfaces_vim_ids"]:
494 for iface
in vim_info
["interfaces"]
495 if vim_iface_id
== iface
["vim_interface_id"]
500 # iface.pop("vim_info", None)
501 vim_interfaces
.append(iface
)
505 for t
in ro_task
["tasks"]
506 if t
and t
["action"] == "CREATE" and t
["status"] != "FINISHED"
508 if vim_interfaces
and task_create
.get("mgmt_vnf_interface") is not None:
509 vim_interfaces
[task_create
["mgmt_vnf_interface"]][
513 mgmt_vdu_iface
= task_create
.get(
514 "mgmt_vdu_interface", task_create
.get("mgmt_vnf_interface", 0)
517 vim_interfaces
[mgmt_vdu_iface
]["mgmt_vdu_interface"] = True
519 if ro_task
["vim_info"]["interfaces"] != vim_interfaces
:
520 ro_vim_item_update
["interfaces"] = vim_interfaces
522 if ro_task
["vim_info"]["vim_status"] != vim_info
["status"]:
523 ro_vim_item_update
["vim_status"] = vim_info
["status"]
525 if ro_task
["vim_info"]["vim_name"] != vim_info
.get("name"):
526 ro_vim_item_update
["vim_name"] = vim_info
.get("name")
528 if vim_info
["status"] in ("ERROR", "VIM_ERROR"):
529 if ro_task
["vim_info"]["vim_details"] != vim_info
.get("error_msg"):
530 ro_vim_item_update
["vim_details"] = vim_info
.get("error_msg")
531 elif vim_info
["status"] == "DELETED":
532 ro_vim_item_update
["vim_id"] = None
533 ro_vim_item_update
["vim_details"] = "Deleted externally"
535 if ro_task
["vim_info"]["vim_details"] != vim_info
["vim_info"]:
536 ro_vim_item_update
["vim_details"] = vim_info
["vim_info"]
538 if ro_vim_item_update
:
540 "ro_task={} {} get-vm={}: status={} {}".format(
542 ro_task
["target_id"],
544 ro_vim_item_update
.get("vim_status"),
545 ro_vim_item_update
.get("vim_details")
546 if ro_vim_item_update
.get("vim_status") != "ACTIVE"
551 return task_status
, ro_vim_item_update
553 def exec(self
, ro_task
, task_index
, task_depends
):
554 task
= ro_task
["tasks"][task_index
]
555 task_id
= task
["task_id"]
556 target_vim
= self
.my_vims
[ro_task
["target_id"]]
557 db_task_update
= {"retries": 0}
558 retries
= task
.get("retries", 0)
561 params
= task
["params"]
562 params_copy
= deepcopy(params
)
563 params_copy
["ro_key"] = self
.db
.decrypt(
564 params_copy
.pop("private_key"),
565 params_copy
.pop("schema_version"),
566 params_copy
.pop("salt"),
568 params_copy
["ip_addr"] = params_copy
.pop("ip_address")
569 target_vim
.inject_user_key(**params_copy
)
571 "task={} {} action-vm=inject_key".format(task_id
, ro_task
["target_id"])
578 ) # params_copy["key"]
579 except (vimconn
.VimConnException
, NsWorkerException
) as e
:
582 if retries
< self
.max_retries_inject_ssh_key
:
588 "next_retry": self
.time_retries_inject_ssh_key
,
593 "task={} {} inject-ssh-key: {}".format(task_id
, ro_task
["target_id"], e
)
595 ro_vim_item_update
= {"vim_details": str(e
)}
597 return "FAILED", ro_vim_item_update
, db_task_update
600 class VimInteractionImage(VimInteractionBase
):
601 def new(self
, ro_task
, task_index
, task_depends
):
602 task
= ro_task
["tasks"][task_index
]
603 task_id
= task
["task_id"]
606 target_vim
= self
.my_vims
[ro_task
["target_id"]]
610 if task
.get("find_params"):
611 vim_images
= target_vim
.get_image_list(**task
["find_params"])
614 raise NsWorkerExceptionNotFound(
615 "Image not found with this criteria: '{}'".format(
619 elif len(vim_images
) > 1:
620 raise NsWorkerException(
621 "More than one image found with this criteria: '{}'".format(
626 vim_image_id
= vim_images
[0]["id"]
628 ro_vim_item_update
= {
629 "vim_id": vim_image_id
,
630 "vim_status": "DONE",
632 "created_items": created_items
,
636 "task={} {} new-image={} created={}".format(
637 task_id
, ro_task
["target_id"], vim_image_id
, created
641 return "DONE", ro_vim_item_update
642 except (NsWorkerException
, vimconn
.VimConnException
) as e
:
644 "task={} {} new-image: {}".format(task_id
, ro_task
["target_id"], e
)
646 ro_vim_item_update
= {
647 "vim_status": "VIM_ERROR",
649 "vim_details": str(e
),
652 return "FAILED", ro_vim_item_update
655 class VimInteractionFlavor(VimInteractionBase
):
656 def delete(self
, ro_task
, task_index
):
657 task
= ro_task
["tasks"][task_index
]
658 task_id
= task
["task_id"]
659 flavor_vim_id
= ro_task
["vim_info"]["vim_id"]
660 ro_vim_item_update_ok
= {
661 "vim_status": "DELETED",
663 "vim_details": "DELETED",
669 target_vim
= self
.my_vims
[ro_task
["target_id"]]
670 target_vim
.delete_flavor(flavor_vim_id
)
671 except vimconn
.VimConnNotFoundException
:
672 ro_vim_item_update_ok
["vim_details"] = "already deleted"
673 except vimconn
.VimConnException
as e
:
675 "ro_task={} vim={} del-flavor={}: {}".format(
676 ro_task
["_id"], ro_task
["target_id"], flavor_vim_id
, e
679 ro_vim_item_update
= {
680 "vim_status": "VIM_ERROR",
681 "vim_details": "Error while deleting: {}".format(e
),
684 return "FAILED", ro_vim_item_update
687 "task={} {} del-flavor={} {}".format(
689 ro_task
["target_id"],
691 ro_vim_item_update_ok
.get("vim_details", ""),
695 return "DONE", ro_vim_item_update_ok
697 def new(self
, ro_task
, task_index
, task_depends
):
698 task
= ro_task
["tasks"][task_index
]
699 task_id
= task
["task_id"]
702 target_vim
= self
.my_vims
[ro_task
["target_id"]]
708 if task
.get("find_params"):
710 flavor_data
= task
["find_params"]["flavor_data"]
711 vim_flavor_id
= target_vim
.get_flavor_id_from_data(flavor_data
)
712 except vimconn
.VimConnNotFoundException
:
715 if not vim_flavor_id
and task
.get("params"):
717 flavor_data
= task
["params"]["flavor_data"]
718 vim_flavor_id
= target_vim
.new_flavor(flavor_data
)
721 ro_vim_item_update
= {
722 "vim_id": vim_flavor_id
,
723 "vim_status": "DONE",
725 "created_items": created_items
,
729 "task={} {} new-flavor={} created={}".format(
730 task_id
, ro_task
["target_id"], vim_flavor_id
, created
734 return "DONE", ro_vim_item_update
735 except (vimconn
.VimConnException
, NsWorkerException
) as e
:
737 "task={} vim={} new-flavor: {}".format(task_id
, ro_task
["target_id"], e
)
739 ro_vim_item_update
= {
740 "vim_status": "VIM_ERROR",
742 "vim_details": str(e
),
745 return "FAILED", ro_vim_item_update
748 class VimInteractionSdnNet(VimInteractionBase
):
750 def _match_pci(port_pci
, mapping
):
752 Check if port_pci matches with mapping
753 mapping can have brackets to indicate that several chars are accepted. e.g
754 pci '0000:af:10.1' matches with '0000:af:1[01].[1357]'
755 :param port_pci: text
756 :param mapping: text, can contain brackets to indicate several chars are available
757 :return: True if matches, False otherwise
759 if not port_pci
or not mapping
:
761 if port_pci
== mapping
:
767 bracket_start
= mapping
.find("[", mapping_index
)
769 if bracket_start
== -1:
772 bracket_end
= mapping
.find("]", bracket_start
)
773 if bracket_end
== -1:
776 length
= bracket_start
- mapping_index
779 and port_pci
[pci_index
: pci_index
+ length
]
780 != mapping
[mapping_index
:bracket_start
]
785 port_pci
[pci_index
+ length
]
786 not in mapping
[bracket_start
+ 1 : bracket_end
]
790 pci_index
+= length
+ 1
791 mapping_index
= bracket_end
+ 1
793 if port_pci
[pci_index
:] != mapping
[mapping_index
:]:
798 def _get_interfaces(self
, vlds_to_connect
, vim_account_id
):
800 :param vlds_to_connect: list with format vnfrs:<id>:vld.<vld_id> or nsrs:<id>:vld.<vld_id>
801 :param vim_account_id:
806 for vld
in vlds_to_connect
:
807 table
, _
, db_id
= vld
.partition(":")
808 db_id
, _
, vld
= db_id
.partition(":")
809 _
, _
, vld_id
= vld
.partition(".")
812 q_filter
= {"vim-account-id": vim_account_id
, "_id": db_id
}
813 iface_key
= "vnf-vld-id"
814 else: # table == "nsrs"
815 q_filter
= {"vim-account-id": vim_account_id
, "nsr-id-ref": db_id
}
816 iface_key
= "ns-vld-id"
818 db_vnfrs
= self
.db
.get_list("vnfrs", q_filter
=q_filter
)
820 for db_vnfr
in db_vnfrs
:
821 for vdu_index
, vdur
in enumerate(db_vnfr
.get("vdur", ())):
822 for iface_index
, interface
in enumerate(vdur
["interfaces"]):
823 if interface
.get(iface_key
) == vld_id
and interface
.get(
825 ) in ("SR-IOV", "PCI-PASSTHROUGH"):
827 interface_
= interface
.copy()
828 interface_
["id"] = "vnfrs:{}:vdu.{}.interfaces.{}".format(
829 db_vnfr
["_id"], vdu_index
, iface_index
832 if vdur
.get("status") == "ERROR":
833 interface_
["status"] = "ERROR"
835 interfaces
.append(interface_
)
839 def refresh(self
, ro_task
):
840 # look for task create
841 task_create_index
, _
= next(
843 for i_t
in enumerate(ro_task
["tasks"])
845 and i_t
[1]["action"] == "CREATE"
846 and i_t
[1]["status"] != "FINISHED"
849 return self
.new(ro_task
, task_create_index
, None)
851 def new(self
, ro_task
, task_index
, task_depends
):
853 task
= ro_task
["tasks"][task_index
]
854 task_id
= task
["task_id"]
855 target_vim
= self
.my_vims
[ro_task
["target_id"]]
857 sdn_net_id
= ro_task
["vim_info"]["vim_id"]
859 created_items
= ro_task
["vim_info"].get("created_items")
860 connected_ports
= ro_task
["vim_info"].get("connected_ports", [])
861 new_connected_ports
= []
862 last_update
= ro_task
["vim_info"].get("last_update", 0)
863 sdn_status
= ro_task
["vim_info"].get("vim_status", "BUILD") or "BUILD"
865 created
= ro_task
["vim_info"].get("created", False)
869 params
= task
["params"]
870 vlds_to_connect
= params
.get("vlds", [])
871 associated_vim
= params
.get("target_vim")
872 # external additional ports
873 additional_ports
= params
.get("sdn-ports") or ()
874 _
, _
, vim_account_id
= (
876 if associated_vim
is None
877 else associated_vim
.partition(":")
882 if associated_vim
not in self
.db_vims
:
883 self
.db_vims
[associated_vim
] = self
.db
.get_one(
884 "vim_accounts", {"_id": vim_account_id
}
887 db_vim
= self
.db_vims
[associated_vim
]
889 # look for ports to connect
890 ports
= self
._get
_interfaces
(vlds_to_connect
, vim_account_id
)
894 pending_ports
= error_ports
= 0
896 sdn_need_update
= False
899 vlan_used
= port
.get("vlan") or vlan_used
901 # TODO. Do not connect if already done
902 if not port
.get("compute_node") or not port
.get("pci"):
903 if port
.get("status") == "ERROR":
910 compute_node_mappings
= next(
913 for c
in db_vim
["config"].get("sdn-port-mapping", ())
914 if c
and c
["compute_node"] == port
["compute_node"]
919 if compute_node_mappings
:
920 # process port_mapping pci of type 0000:af:1[01].[1357]
924 for p
in compute_node_mappings
["ports"]
925 if self
._match
_pci
(port
["pci"], p
.get("pci"))
931 if not db_vim
["config"].get("mapping_not_needed"):
933 "Port mapping not found for compute_node={} pci={}".format(
934 port
["compute_node"], port
["pci"]
941 service_endpoint_id
= "{}:{}".format(port
["compute_node"], port
["pci"])
943 "service_endpoint_id": pmap
.get("service_endpoint_id")
944 or service_endpoint_id
,
945 "service_endpoint_encapsulation_type": "dot1q"
946 if port
["type"] == "SR-IOV"
948 "service_endpoint_encapsulation_info": {
949 "vlan": port
.get("vlan"),
950 "mac": port
.get("mac-address"),
951 "device_id": pmap
.get("device_id") or port
["compute_node"],
952 "device_interface_id": pmap
.get("device_interface_id")
954 "switch_dpid": pmap
.get("switch_id") or pmap
.get("switch_dpid"),
955 "switch_port": pmap
.get("switch_port"),
956 "service_mapping_info": pmap
.get("service_mapping_info"),
961 # if port["modified_at"] > last_update:
962 # sdn_need_update = True
963 new_connected_ports
.append(port
["id"]) # TODO
964 sdn_ports
.append(new_port
)
968 "{} interfaces have not been created as VDU is on ERROR status".format(
973 # connect external ports
974 for index
, additional_port
in enumerate(additional_ports
):
975 additional_port_id
= additional_port
.get(
976 "service_endpoint_id"
977 ) or "external-{}".format(index
)
980 "service_endpoint_id": additional_port_id
,
981 "service_endpoint_encapsulation_type": additional_port
.get(
982 "service_endpoint_encapsulation_type", "dot1q"
984 "service_endpoint_encapsulation_info": {
985 "vlan": additional_port
.get("vlan") or vlan_used
,
986 "mac": additional_port
.get("mac_address"),
987 "device_id": additional_port
.get("device_id"),
988 "device_interface_id": additional_port
.get(
989 "device_interface_id"
991 "switch_dpid": additional_port
.get("switch_dpid")
992 or additional_port
.get("switch_id"),
993 "switch_port": additional_port
.get("switch_port"),
994 "service_mapping_info": additional_port
.get(
995 "service_mapping_info"
1000 new_connected_ports
.append(additional_port_id
)
1003 # if there are more ports to connect or they have been modified, call create/update
1005 sdn_status
= "ERROR"
1006 sdn_info
= "; ".join(error_list
)
1007 elif set(connected_ports
) != set(new_connected_ports
) or sdn_need_update
:
1008 last_update
= time
.time()
1011 if len(sdn_ports
) < 2:
1012 sdn_status
= "ACTIVE"
1014 if not pending_ports
:
1016 "task={} {} new-sdn-net done, less than 2 ports".format(
1017 task_id
, ro_task
["target_id"]
1021 net_type
= params
.get("type") or "ELAN"
1025 ) = target_vim
.create_connectivity_service(net_type
, sdn_ports
)
1028 "task={} {} new-sdn-net={} created={}".format(
1029 task_id
, ro_task
["target_id"], sdn_net_id
, created
1033 created_items
= target_vim
.edit_connectivity_service(
1034 sdn_net_id
, conn_info
=created_items
, connection_points
=sdn_ports
1038 "task={} {} update-sdn-net={} created={}".format(
1039 task_id
, ro_task
["target_id"], sdn_net_id
, created
1043 connected_ports
= new_connected_ports
1045 wim_status_dict
= target_vim
.get_connectivity_service_status(
1046 sdn_net_id
, conn_info
=created_items
1048 sdn_status
= wim_status_dict
["sdn_status"]
1050 if wim_status_dict
.get("sdn_info"):
1051 sdn_info
= str(wim_status_dict
.get("sdn_info")) or ""
1053 if wim_status_dict
.get("error_msg"):
1054 sdn_info
= wim_status_dict
.get("error_msg") or ""
1057 if sdn_status
!= "ERROR":
1058 sdn_info
= "Waiting for getting interfaces location from VIM. Obtained '{}' of {}".format(
1059 len(ports
) - pending_ports
, len(ports
)
1062 if sdn_status
== "ACTIVE":
1063 sdn_status
= "BUILD"
1065 ro_vim_item_update
= {
1066 "vim_id": sdn_net_id
,
1067 "vim_status": sdn_status
,
1069 "created_items": created_items
,
1070 "connected_ports": connected_ports
,
1071 "vim_details": sdn_info
,
1072 "last_update": last_update
,
1075 return sdn_status
, ro_vim_item_update
1076 except Exception as e
:
1078 "task={} vim={} new-net: {}".format(task_id
, ro_task
["target_id"], e
),
1079 exc_info
=not isinstance(
1080 e
, (sdnconn
.SdnConnectorError
, vimconn
.VimConnException
)
1083 ro_vim_item_update
= {
1084 "vim_status": "VIM_ERROR",
1086 "vim_details": str(e
),
1089 return "FAILED", ro_vim_item_update
1091 def delete(self
, ro_task
, task_index
):
1092 task
= ro_task
["tasks"][task_index
]
1093 task_id
= task
["task_id"]
1094 sdn_vim_id
= ro_task
["vim_info"].get("vim_id")
1095 ro_vim_item_update_ok
= {
1096 "vim_status": "DELETED",
1098 "vim_details": "DELETED",
1104 target_vim
= self
.my_vims
[ro_task
["target_id"]]
1105 target_vim
.delete_connectivity_service(
1106 sdn_vim_id
, ro_task
["vim_info"].get("created_items")
1109 except Exception as e
:
1111 isinstance(e
, sdnconn
.SdnConnectorError
)
1112 and e
.http_code
== HTTPStatus
.NOT_FOUND
.value
1114 ro_vim_item_update_ok
["vim_details"] = "already deleted"
1117 "ro_task={} vim={} del-sdn-net={}: {}".format(
1118 ro_task
["_id"], ro_task
["target_id"], sdn_vim_id
, e
1120 exc_info
=not isinstance(
1121 e
, (sdnconn
.SdnConnectorError
, vimconn
.VimConnException
)
1124 ro_vim_item_update
= {
1125 "vim_status": "VIM_ERROR",
1126 "vim_details": "Error while deleting: {}".format(e
),
1129 return "FAILED", ro_vim_item_update
1132 "task={} {} del-sdn-net={} {}".format(
1134 ro_task
["target_id"],
1136 ro_vim_item_update_ok
.get("vim_details", ""),
1140 return "DONE", ro_vim_item_update_ok
1143 class NsWorker(threading
.Thread
):
1144 REFRESH_BUILD
= 5 # 5 seconds
1145 REFRESH_ACTIVE
= 60 # 1 minute
1147 REFRESH_IMAGE
= 3600 * 10
1148 REFRESH_DELETE
= 3600 * 10
1152 def __init__(self
, worker_index
, config
, plugins
, db
):
1155 :param worker_index: thread index
1156 :param config: general configuration of RO, among others the process_id with the docker id where it runs
1157 :param plugins: global shared dict with the loaded plugins
1158 :param db: database class instance to use
1160 threading
.Thread
.__init
__(self
)
1161 self
.config
= config
1162 self
.plugins
= plugins
1163 self
.plugin_name
= "unknown"
1164 self
.logger
= logging
.getLogger("ro.worker{}".format(worker_index
))
1165 self
.worker_index
= worker_index
1166 self
.task_queue
= queue
.Queue(self
.QUEUE_SIZE
)
1167 # targetvim: vimplugin class
1169 # targetvim: vim information from database
1172 self
.vim_targets
= []
1173 self
.my_id
= config
["process_id"] + ":" + str(worker_index
)
1176 "net": VimInteractionNet(self
.db
, self
.my_vims
, self
.db_vims
, self
.logger
),
1177 "vdu": VimInteractionVdu(self
.db
, self
.my_vims
, self
.db_vims
, self
.logger
),
1178 "image": VimInteractionImage(
1179 self
.db
, self
.my_vims
, self
.db_vims
, self
.logger
1181 "flavor": VimInteractionFlavor(
1182 self
.db
, self
.my_vims
, self
.db_vims
, self
.logger
1184 "sdn_net": VimInteractionSdnNet(
1185 self
.db
, self
.my_vims
, self
.db_vims
, self
.logger
1188 self
.time_last_task_processed
= None
1189 # lists of tasks to delete because nsrs or vnfrs has been deleted from db
1190 self
.tasks_to_delete
= []
1191 # it is idle when there are not vim_targets associated
1193 self
.task_locked_time
= config
["global"]["task_locked_time"]
1195 def insert_task(self
, task
):
1197 self
.task_queue
.put(task
, False)
1200 raise NsWorkerException("timeout inserting a task")
1202 def terminate(self
):
1203 self
.insert_task("exit")
1205 def del_task(self
, task
):
1206 with self
.task_lock
:
1207 if task
["status"] == "SCHEDULED":
1208 task
["status"] = "SUPERSEDED"
1210 else: # task["status"] == "processing"
1211 self
.task_lock
.release()
1214 def _process_vim_config(self
, target_id
, db_vim
):
1216 Process vim config, creating vim configuration files as ca_cert
1217 :param target_id: vim/sdn/wim + id
1218 :param db_vim: Vim dictionary obtained from database
1219 :return: None. Modifies vim. Creates a folder target_id:worker_index and several files
1221 if not db_vim
.get("config"):
1227 if db_vim
["config"].get("ca_cert_content"):
1228 file_name
= "{}:{}".format(target_id
, self
.worker_index
)
1232 except FileExistsError
:
1235 file_name
= file_name
+ "/ca_cert"
1237 with
open(file_name
, "w") as f
:
1238 f
.write(db_vim
["config"]["ca_cert_content"])
1239 del db_vim
["config"]["ca_cert_content"]
1240 db_vim
["config"]["ca_cert"] = file_name
1241 except Exception as e
:
1242 raise NsWorkerException(
1243 "Error writing to file '{}': {}".format(file_name
, e
)
1246 def _load_plugin(self
, name
, type="vim"):
1247 # type can be vim or sdn
1248 if "rovim_dummy" not in self
.plugins
:
1249 self
.plugins
["rovim_dummy"] = VimDummyConnector
1251 if "rosdn_dummy" not in self
.plugins
:
1252 self
.plugins
["rosdn_dummy"] = SdnDummyConnector
1254 if name
in self
.plugins
:
1255 return self
.plugins
[name
]
1258 for ep
in entry_points(group
="osm_ro{}.plugins".format(type), name
=name
):
1259 self
.plugins
[name
] = ep
.load()
1260 except Exception as e
:
1261 raise NsWorkerException("Cannot load plugin osm_{}: {}".format(name
, e
))
1263 if name
and name
not in self
.plugins
:
1264 raise NsWorkerException(
1265 "Plugin 'osm_{n}' has not been installed".format(n
=name
)
1268 return self
.plugins
[name
]
1270 def _unload_vim(self
, target_id
):
1272 Unload a vim_account. Removes it from self db_vims dictionary, my_vims dictionary and vim_targets list
1273 :param target_id: Contains type:_id; where type can be 'vim', ...
1277 self
.db_vims
.pop(target_id
, None)
1278 self
.my_vims
.pop(target_id
, None)
1280 if target_id
in self
.vim_targets
:
1281 self
.vim_targets
.remove(target_id
)
1283 self
.logger
.info("Unloaded {}".format(target_id
))
1284 rmtree("{}:{}".format(target_id
, self
.worker_index
))
1285 except FileNotFoundError
:
1286 pass # this is raised by rmtree if folder does not exist
1287 except Exception as e
:
1288 self
.logger
.error("Cannot unload {}: {}".format(target_id
, e
))
1290 def _check_vim(self
, target_id
):
1292 Load a VIM/SDN/WIM (if not loaded) and check connectivity, updating database with ENABLE or ERROR
1293 :param target_id: Contains type:_id; type can be 'vim', 'sdn' or 'wim'
1296 target
, _
, _id
= target_id
.partition(":")
1302 loaded
= target_id
in self
.vim_targets
1312 step
= "Getting {} from db".format(target_id
)
1313 db_vim
= self
.db
.get_one(target_database
, {"_id": _id
})
1315 for op_index
, operation
in enumerate(
1316 db_vim
["_admin"].get("operations", ())
1318 if operation
["operationState"] != "PROCESSING":
1321 locked_at
= operation
.get("locked_at")
1323 if locked_at
is not None and locked_at
>= now
- self
.task_locked_time
:
1324 # some other thread is doing this operation
1328 op_text
= "_admin.operations.{}.".format(op_index
)
1330 if not self
.db
.set_one(
1334 op_text
+ "operationState": "PROCESSING",
1335 op_text
+ "locked_at": locked_at
,
1338 op_text
+ "locked_at": now
,
1339 "admin.current_operation": op_index
,
1341 fail_on_empty
=False,
1345 unset_dict
[op_text
+ "locked_at"] = None
1346 unset_dict
["current_operation"] = None
1347 step
= "Loading " + target_id
1348 error_text
= self
._load
_vim
(target_id
)
1351 step
= "Checking connectivity"
1354 self
.my_vims
[target_id
].check_vim_connectivity()
1356 self
.my_vims
[target_id
].check_credentials()
1358 update_dict
["_admin.operationalState"] = "ENABLED"
1359 update_dict
["_admin.detailed-status"] = ""
1360 unset_dict
[op_text
+ "detailed-status"] = None
1361 update_dict
[op_text
+ "operationState"] = "COMPLETED"
1365 except Exception as e
:
1366 error_text
= "{}: {}".format(step
, e
)
1367 self
.logger
.error("{} for {}: {}".format(step
, target_id
, e
))
1370 if update_dict
or unset_dict
:
1372 update_dict
[op_text
+ "operationState"] = "FAILED"
1373 update_dict
[op_text
+ "detailed-status"] = error_text
1374 unset_dict
.pop(op_text
+ "detailed-status", None)
1375 update_dict
["_admin.operationalState"] = "ERROR"
1376 update_dict
["_admin.detailed-status"] = error_text
1379 update_dict
[op_text
+ "statusEnteredTime"] = now
1383 q_filter
={"_id": _id
},
1384 update_dict
=update_dict
,
1386 fail_on_empty
=False,
1390 self
._unload
_vim
(target_id
)
1392 def _reload_vim(self
, target_id
):
1393 if target_id
in self
.vim_targets
:
1394 self
._load
_vim
(target_id
)
1396 # if the vim is not loaded, but database information of VIM is cached at self.db_vims,
1397 # just remove it to force load again next time it is needed
1398 self
.db_vims
.pop(target_id
, None)
1400 def _load_vim(self
, target_id
):
1402 Load or reload a vim_account, sdn_controller or wim_account.
1403 Read content from database, load the plugin if not loaded.
1404 In case of error loading the plugin, it load a failing VIM_connector
1405 It fills self db_vims dictionary, my_vims dictionary and vim_targets list
1406 :param target_id: Contains type:_id; where type can be 'vim', ...
1407 :return: None if ok, descriptive text if error
1409 target
, _
, _id
= target_id
.partition(":")
1421 step
= "Getting {}={} from db".format(target
, _id
)
1422 # TODO process for wim, sdnc, ...
1423 vim
= self
.db
.get_one(target_database
, {"_id": _id
})
1425 # if deep_get(vim, "config", "sdn-controller"):
1426 # step = "Getting sdn-controller-id='{}' from db".format(vim["config"]["sdn-controller"])
1427 # db_sdn = self.db.get_one("sdns", {"_id": vim["config"]["sdn-controller"]})
1429 step
= "Decrypting password"
1430 schema_version
= vim
.get("schema_version")
1431 self
.db
.encrypt_decrypt_fields(
1434 fields
=("password", "secret"),
1435 schema_version
=schema_version
,
1438 self
._process
_vim
_config
(target_id
, vim
)
1441 plugin_name
= "rovim_" + vim
["vim_type"]
1442 step
= "Loading plugin '{}'".format(plugin_name
)
1443 vim_module_conn
= self
._load
_plugin
(plugin_name
)
1444 step
= "Loading {}'".format(target_id
)
1445 self
.my_vims
[target_id
] = vim_module_conn(
1448 tenant_id
=vim
.get("vim_tenant_id"),
1449 tenant_name
=vim
.get("vim_tenant_name"),
1452 user
=vim
["vim_user"],
1453 passwd
=vim
["vim_password"],
1454 config
=vim
.get("config") or {},
1458 plugin_name
= "rosdn_" + (vim
.get("type") or vim
.get("wim_type"))
1459 step
= "Loading plugin '{}'".format(plugin_name
)
1460 vim_module_conn
= self
._load
_plugin
(plugin_name
, "sdn")
1461 step
= "Loading {}'".format(target_id
)
1463 wim_config
= wim
.pop("config", {}) or {}
1464 wim
["uuid"] = wim
["_id"]
1465 if "url" in wim
and "wim_url" not in wim
:
1466 wim
["wim_url"] = wim
["url"]
1467 elif "url" not in wim
and "wim_url" in wim
:
1468 wim
["url"] = wim
["wim_url"]
1471 wim_config
["dpid"] = wim
.pop("dpid")
1473 if wim
.get("switch_id"):
1474 wim_config
["switch_id"] = wim
.pop("switch_id")
1476 # wim, wim_account, config
1477 self
.my_vims
[target_id
] = vim_module_conn(wim
, wim
, wim_config
)
1478 self
.db_vims
[target_id
] = vim
1479 self
.error_status
= None
1482 "Connector loaded for {}, plugin={}".format(target_id
, plugin_name
)
1484 except Exception as e
:
1486 "Cannot load {} plugin={}: {} {}".format(
1487 target_id
, plugin_name
, step
, e
1491 self
.db_vims
[target_id
] = vim
or {}
1492 self
.db_vims
[target_id
] = FailingConnector(str(e
))
1493 error_status
= "{} Error: {}".format(step
, e
)
1497 if target_id
not in self
.vim_targets
:
1498 self
.vim_targets
.append(target_id
)
1500 def _get_db_task(self
):
1502 Read actions from database and reload them at memory. Fill self.refresh_list, pending_list, vim_actions
1507 if not self
.time_last_task_processed
:
1508 self
.time_last_task_processed
= now
1513 # Log RO tasks only when loglevel is DEBUG
1514 if self.logger.getEffectiveLevel() == logging.DEBUG:
1521 + str(self.task_locked_time)
1523 + "time_last_task_processed="
1524 + str(self.time_last_task_processed)
1530 locked
= self
.db
.set_one(
1533 "target_id": self
.vim_targets
,
1534 "tasks.status": ["SCHEDULED", "BUILD", "DONE", "FAILED"],
1535 "locked_at.lt": now
- self
.task_locked_time
,
1536 "to_check_at.lt": self
.time_last_task_processed
,
1538 update_dict
={"locked_by": self
.my_id
, "locked_at": now
},
1539 fail_on_empty
=False,
1544 ro_task
= self
.db
.get_one(
1547 "target_id": self
.vim_targets
,
1548 "tasks.status": ["SCHEDULED", "BUILD", "DONE", "FAILED"],
1554 if self
.time_last_task_processed
== now
:
1555 self
.time_last_task_processed
= None
1558 self
.time_last_task_processed
= now
1559 # self.time_last_task_processed = min(self.time_last_task_processed + 1000, now)
1561 except DbException
as e
:
1562 self
.logger
.error("Database exception at _get_db_task: {}".format(e
))
1563 except Exception as e
:
1564 self
.logger
.critical(
1565 "Unexpected exception at _get_db_task: {}".format(e
), exc_info
=True
1570 def _get_db_all_tasks(self
):
1572 Read all content of table ro_tasks to log it
1576 # Checking the content of the BD:
1579 ro_task
= self
.db
.get_list("ro_tasks")
1581 self
._log
_ro
_task
(rt
, None, None, "TASK_WF", "GET_ALL_TASKS")
1584 except DbException
as e
:
1585 self
.logger
.error("Database exception at _get_db_all_tasks: {}".format(e
))
1586 except Exception as e
:
1587 self
.logger
.critical(
1588 "Unexpected exception at _get_db_all_tasks: {}".format(e
), exc_info
=True
1593 def _log_ro_task(self
, ro_task
, db_ro_task_update
, db_ro_task_delete
, mark
, event
):
1595 Generate a log with the following format:
1597 Mark;Event;ro_task_id;locked_at;modified_at;created_at;to_check_at;locked_by;
1598 target_id;vim_info.refresh_at;vim_info;no_of_tasks;task_status;action_id;
1599 task_array_index;task_id;task_action;task_item;task_args
1603 TASK_WF;GET_TASK;888f1864-749a-4fc2-bc1a-97c0fffd6a6f:2;1642158724.8210013;
1604 1642158640.7986135;1642158640.7986135;1642158640.7986135;b134c9494e75:0a
1605 ;vim:b7ff9e24-8868-4d68-8a57-a59dc11d0327;None;{'created': False,
1606 'created_items': None, 'vim_id': None, 'vim_name': None, 'vim_status': None,
1607 'vim_details': None, 'refresh_at': None};1;SCHEDULED;
1608 888f1864-749a-4fc2-bc1a-97c0fffd6a6f;0;888f1864-749a-4fc2-bc1a-97c0fffd6a6f:2;
1609 CREATE;image;{'filter_dict': {'name': 'ubuntu-os-cloud:image-family:ubuntu-1804-lts'}}
1614 if ro_task
is not None and isinstance(ro_task
, dict):
1615 for t
in ro_task
["tasks"]:
1619 line
.append(ro_task
.get("_id", ""))
1620 line
.append(str(ro_task
.get("locked_at", "")))
1621 line
.append(str(ro_task
.get("modified_at", "")))
1622 line
.append(str(ro_task
.get("created_at", "")))
1623 line
.append(str(ro_task
.get("to_check_at", "")))
1624 line
.append(str(ro_task
.get("locked_by", "")))
1625 line
.append(str(ro_task
.get("target_id", "")))
1626 line
.append(str(ro_task
.get("vim_info", {}).get("refresh_at", "")))
1627 line
.append(str(ro_task
.get("vim_info", "")))
1628 line
.append(str(ro_task
.get("tasks", "")))
1629 if isinstance(t
, dict):
1630 line
.append(str(t
.get("status", "")))
1631 line
.append(str(t
.get("action_id", "")))
1633 line
.append(str(t
.get("task_id", "")))
1634 line
.append(str(t
.get("action", "")))
1635 line
.append(str(t
.get("item", "")))
1636 line
.append(str(t
.get("find_params", "")))
1637 line
.append(str(t
.get("params", "")))
1639 line
.extend([""] * 2)
1641 line
.extend([""] * 5)
1644 self
.logger
.debug(";".join(line
))
1645 elif db_ro_task_update
is not None and isinstance(db_ro_task_update
, dict):
1648 st
= "tasks.{}.status".format(i
)
1649 if st
not in db_ro_task_update
:
1654 line
.append(db_ro_task_update
.get("_id", ""))
1655 line
.append(str(db_ro_task_update
.get("locked_at", "")))
1656 line
.append(str(db_ro_task_update
.get("modified_at", "")))
1658 line
.append(str(db_ro_task_update
.get("to_check_at", "")))
1659 line
.append(str(db_ro_task_update
.get("locked_by", "")))
1661 line
.append(str(db_ro_task_update
.get("vim_info.refresh_at", "")))
1663 line
.append(str(db_ro_task_update
.get("vim_info", "")))
1664 line
.append(str(str(db_ro_task_update
).count(".status")))
1665 line
.append(db_ro_task_update
.get(st
, ""))
1668 line
.extend([""] * 3)
1670 self
.logger
.debug(";".join(line
))
1672 elif db_ro_task_delete
is not None and isinstance(db_ro_task_delete
, dict):
1676 line
.append(db_ro_task_delete
.get("_id", ""))
1678 line
.append(db_ro_task_delete
.get("modified_at", ""))
1679 line
.extend([""] * 13)
1680 self
.logger
.debug(";".join(line
))
1686 line
.extend([""] * 16)
1687 self
.logger
.debug(";".join(line
))
1689 except Exception as e
:
1690 self
.logger
.error("Error logging ro_task: {}".format(e
))
1692 def _delete_task(self
, ro_task
, task_index
, task_depends
, db_update
):
1694 Determine if this task need to be done or superseded
1697 my_task
= ro_task
["tasks"][task_index
]
1698 task_id
= my_task
["task_id"]
1699 needed_delete
= ro_task
["vim_info"]["created"] or ro_task
["vim_info"].get(
1700 "created_items", False
1703 if my_task
["status"] == "FAILED":
1704 return None, None # TODO need to be retry??
1707 for index
, task
in enumerate(ro_task
["tasks"]):
1708 if index
== task_index
or not task
:
1712 my_task
["target_record"] == task
["target_record"]
1713 and task
["action"] == "CREATE"
1716 db_update
["tasks.{}.status".format(index
)] = task
[
1719 elif task
["action"] == "CREATE" and task
["status"] not in (
1723 needed_delete
= False
1726 return self
.item2class
[my_task
["item"]].delete(ro_task
, task_index
)
1728 return "SUPERSEDED", None
1729 except Exception as e
:
1730 if not isinstance(e
, NsWorkerException
):
1731 self
.logger
.critical(
1732 "Unexpected exception at _delete_task task={}: {}".format(
1738 return "FAILED", {"vim_status": "VIM_ERROR", "vim_details": str(e
)}
1740 def _create_task(self
, ro_task
, task_index
, task_depends
, db_update
):
1742 Determine if this task need to create something at VIM
1745 my_task
= ro_task
["tasks"][task_index
]
1746 task_id
= my_task
["task_id"]
1749 if my_task
["status"] == "FAILED":
1750 return None, None # TODO need to be retry??
1751 elif my_task
["status"] == "SCHEDULED":
1752 # check if already created by another task
1753 for index
, task
in enumerate(ro_task
["tasks"]):
1754 if index
== task_index
or not task
:
1757 if task
["action"] == "CREATE" and task
["status"] not in (
1762 return task
["status"], "COPY_VIM_INFO"
1765 task_status
, ro_vim_item_update
= self
.item2class
[my_task
["item"]].new(
1766 ro_task
, task_index
, task_depends
1768 # TODO update other CREATE tasks
1769 except Exception as e
:
1770 if not isinstance(e
, NsWorkerException
):
1772 "Error executing task={}: {}".format(task_id
, e
), exc_info
=True
1775 task_status
= "FAILED"
1776 ro_vim_item_update
= {"vim_status": "VIM_ERROR", "vim_details": str(e
)}
1777 # TODO update ro_vim_item_update
1779 return task_status
, ro_vim_item_update
1783 def _get_dependency(self
, task_id
, ro_task
=None, target_id
=None):
1785 Look for dependency task
1786 :param task_id: Can be one of
1787 1. target_vim+blank+task.target_record_id: "(vim|sdn|wim):<id> (vnfrs|nsrs):(vld|vdu|flavor|image).<id>"
1788 2. task.target_record_id: "(vnfrs|nsrs):(vld|vdu|flavor|image).<id>"
1789 3. task.task_id: "<action_id>:number"
1792 :return: database ro_task plus index of task
1795 task_id
.startswith("vim:")
1796 or task_id
.startswith("sdn:")
1797 or task_id
.startswith("wim:")
1799 target_id
, _
, task_id
= task_id
.partition(" ")
1801 if task_id
.startswith("nsrs:") or task_id
.startswith("vnfrs:"):
1802 ro_task_dependency
= self
.db
.get_one(
1804 q_filter
={"target_id": target_id
, "tasks.target_record_id": task_id
},
1805 fail_on_empty
=False,
1808 if ro_task_dependency
:
1809 for task_index
, task
in enumerate(ro_task_dependency
["tasks"]):
1810 if task
["target_record_id"] == task_id
:
1811 return ro_task_dependency
, task_index
1815 for task_index
, task
in enumerate(ro_task
["tasks"]):
1816 if task
and task
["task_id"] == task_id
:
1817 return ro_task
, task_index
1819 ro_task_dependency
= self
.db
.get_one(
1822 "tasks.ANYINDEX.task_id": task_id
,
1823 "tasks.ANYINDEX.target_record.ne": None,
1825 fail_on_empty
=False,
1828 if ro_task_dependency
:
1829 for task_index
, task
in ro_task_dependency
["tasks"]:
1830 if task
["task_id"] == task_id
:
1831 return ro_task_dependency
, task_index
1832 raise NsWorkerException("Cannot get depending task {}".format(task_id
))
1834 def _process_pending_tasks(self
, ro_task
):
1835 ro_task_id
= ro_task
["_id"]
1838 next_check_at
= now
+ (24 * 60 * 60)
1839 db_ro_task_update
= {}
1841 def _update_refresh(new_status
):
1842 # compute next_refresh
1844 nonlocal next_check_at
1845 nonlocal db_ro_task_update
1848 next_refresh
= time
.time()
1850 if task
["item"] in ("image", "flavor"):
1851 next_refresh
+= self
.REFRESH_IMAGE
1852 elif new_status
== "BUILD":
1853 next_refresh
+= self
.REFRESH_BUILD
1854 elif new_status
== "DONE":
1855 next_refresh
+= self
.REFRESH_ACTIVE
1857 next_refresh
+= self
.REFRESH_ERROR
1859 next_check_at
= min(next_check_at
, next_refresh
)
1860 db_ro_task_update
["vim_info.refresh_at"] = next_refresh
1861 ro_task
["vim_info"]["refresh_at"] = next_refresh
1865 # Log RO tasks only when loglevel is DEBUG
1866 if self.logger.getEffectiveLevel() == logging.DEBUG:
1867 self._log_ro_task(ro_task, None, None, "TASK_WF", "GET_TASK")
1869 # 0: get task_status_create
1871 task_status_create
= None
1875 for t
in ro_task
["tasks"]
1877 and t
["action"] == "CREATE"
1878 and t
["status"] in ("BUILD", "DONE")
1884 task_status_create
= task_create
["status"]
1886 # 1: look for tasks in status SCHEDULED, or in status CREATE if action is DONE or BUILD
1887 for task_action
in ("DELETE", "CREATE", "EXEC"):
1888 db_vim_update
= None
1891 for task_index
, task
in enumerate(ro_task
["tasks"]):
1893 continue # task deleted
1896 target_update
= None
1900 task_action
in ("DELETE", "EXEC")
1901 and task
["status"] not in ("SCHEDULED", "BUILD")
1903 or task
["action"] != task_action
1905 task_action
== "CREATE"
1906 and task
["status"] in ("FINISHED", "SUPERSEDED")
1911 task_path
= "tasks.{}.status".format(task_index
)
1913 db_vim_info_update
= None
1915 if task
["status"] == "SCHEDULED":
1916 # check if tasks that this depends on have been completed
1917 dependency_not_completed
= False
1919 for dependency_task_id
in task
.get("depends_on") or ():
1922 dependency_task_index
,
1923 ) = self
._get
_dependency
(
1924 dependency_task_id
, target_id
=ro_task
["target_id"]
1926 dependency_task
= dependency_ro_task
["tasks"][
1927 dependency_task_index
1930 if dependency_task
["status"] == "SCHEDULED":
1931 dependency_not_completed
= True
1932 next_check_at
= min(
1933 next_check_at
, dependency_ro_task
["to_check_at"]
1935 # must allow dependent task to be processed first
1936 # to do this set time after last_task_processed
1937 next_check_at
= max(
1938 self
.time_last_task_processed
, next_check_at
1941 elif dependency_task
["status"] == "FAILED":
1942 error_text
= "Cannot {} {} because depends on failed {} {} id={}): {}".format(
1945 dependency_task
["action"],
1946 dependency_task
["item"],
1948 dependency_ro_task
["vim_info"].get(
1953 "task={} {}".format(task
["task_id"], error_text
)
1955 raise NsWorkerException(error_text
)
1957 task_depends
[dependency_task_id
] = dependency_ro_task
[
1961 "TASK-{}".format(dependency_task_id
)
1962 ] = dependency_ro_task
["vim_info"]["vim_id"]
1964 if dependency_not_completed
:
1965 # TODO set at vim_info.vim_details that it is waiting
1968 # before calling VIM-plugin as it can take more than task_locked_time, insert to LockRenew
1969 # the task of renew this locking. It will update database locket_at periodically
1971 lock_object
= LockRenew
.add_lock_object(
1972 "ro_tasks", ro_task
, self
1975 if task
["action"] == "DELETE":
1976 (new_status
, db_vim_info_update
,) = self
._delete
_task
(
1977 ro_task
, task_index
, task_depends
, db_ro_task_update
1980 "FINISHED" if new_status
== "DONE" else new_status
1982 # ^with FINISHED instead of DONE it will not be refreshing
1984 if new_status
in ("FINISHED", "SUPERSEDED"):
1985 target_update
= "DELETE"
1986 elif task
["action"] == "EXEC":
1991 ) = self
.item2class
[task
["item"]].exec(
1992 ro_task
, task_index
, task_depends
1995 "FINISHED" if new_status
== "DONE" else new_status
1997 # ^with FINISHED instead of DONE it will not be refreshing
2000 # load into database the modified db_task_update "retries" and "next_retry"
2001 if db_task_update
.get("retries"):
2003 "tasks.{}.retries".format(task_index
)
2004 ] = db_task_update
["retries"]
2006 next_check_at
= time
.time() + db_task_update
.get(
2009 target_update
= None
2010 elif task
["action"] == "CREATE":
2011 if task
["status"] == "SCHEDULED":
2012 if task_status_create
:
2013 new_status
= task_status_create
2014 target_update
= "COPY_VIM_INFO"
2016 new_status
, db_vim_info_update
= self
.item2class
[
2018 ].new(ro_task
, task_index
, task_depends
)
2019 # self._create_task(ro_task, task_index, task_depends, db_ro_task_update)
2020 _update_refresh(new_status
)
2023 ro_task
["vim_info"]["refresh_at"]
2024 and now
> ro_task
["vim_info"]["refresh_at"]
2026 new_status
, db_vim_info_update
= self
.item2class
[
2029 _update_refresh(new_status
)
2031 # The refresh is updated to avoid set the value of "refresh_at" to
2032 # default value (next_check_at = now + (24 * 60 * 60)) when status is BUILD,
2033 # because it can happen that in this case the task is never processed
2034 _update_refresh(task
["status"])
2036 except Exception as e
:
2037 new_status
= "FAILED"
2038 db_vim_info_update
= {
2039 "vim_status": "VIM_ERROR",
2040 "vim_details": str(e
),
2044 e
, (NsWorkerException
, vimconn
.VimConnException
)
2047 "Unexpected exception at _delete_task task={}: {}".format(
2054 if db_vim_info_update
:
2055 db_vim_update
= db_vim_info_update
.copy()
2056 db_ro_task_update
.update(
2059 for k
, v
in db_vim_info_update
.items()
2062 ro_task
["vim_info"].update(db_vim_info_update
)
2065 if task_action
== "CREATE":
2066 task_status_create
= new_status
2067 db_ro_task_update
[task_path
] = new_status
2069 if target_update
or db_vim_update
:
2070 if target_update
== "DELETE":
2071 self
._update
_target
(task
, None)
2072 elif target_update
== "COPY_VIM_INFO":
2073 self
._update
_target
(task
, ro_task
["vim_info"])
2075 self
._update
_target
(task
, db_vim_update
)
2077 except Exception as e
:
2079 isinstance(e
, DbException
)
2080 and e
.http_code
== HTTPStatus
.NOT_FOUND
2082 # if the vnfrs or nsrs has been removed from database, this task must be removed
2084 "marking to delete task={}".format(task
["task_id"])
2086 self
.tasks_to_delete
.append(task
)
2089 "Unexpected exception at _update_target task={}: {}".format(
2095 locked_at
= ro_task
["locked_at"]
2099 lock_object
["locked_at"],
2100 lock_object
["locked_at"] + self
.task_locked_time
,
2102 # locked_at contains two times to avoid race condition. In case the lock has been renew, it will
2103 # contain exactly locked_at + self.task_locked_time
2104 LockRenew
.remove_lock_object(lock_object
)
2107 "_id": ro_task
["_id"],
2108 "to_check_at": ro_task
["to_check_at"],
2109 "locked_at": locked_at
,
2111 # modify own task. Try filtering by to_next_check. For race condition if to_check_at has been modified,
2112 # outside this task (by ro_nbi) do not update it
2113 db_ro_task_update
["locked_by"] = None
2114 # locked_at converted to int only for debugging. When has not decimals it means it has been unlocked
2115 db_ro_task_update
["locked_at"] = int(now
- self
.task_locked_time
)
2116 db_ro_task_update
["modified_at"] = now
2117 db_ro_task_update
["to_check_at"] = next_check_at
2120 # Log RO tasks only when loglevel is DEBUG
2121 if self.logger.getEffectiveLevel() == logging.DEBUG:
2122 db_ro_task_update_log = db_ro_task_update.copy()
2123 db_ro_task_update_log["_id"] = q_filter["_id"]
2124 self._log_ro_task(None, db_ro_task_update_log, None, "TASK_WF", "SET_TASK")
2127 if not self
.db
.set_one(
2129 update_dict
=db_ro_task_update
,
2131 fail_on_empty
=False,
2133 del db_ro_task_update
["to_check_at"]
2134 del q_filter
["to_check_at"]
2136 # Log RO tasks only when loglevel is DEBUG
2137 if self.logger.getEffectiveLevel() == logging.DEBUG:
2140 db_ro_task_update_log,
2143 "SET_TASK " + str(q_filter),
2149 update_dict
=db_ro_task_update
,
2152 except DbException
as e
:
2154 "ro_task={} Error updating database {}".format(ro_task_id
, e
)
2156 except Exception as e
:
2158 "Error executing ro_task={}: {}".format(ro_task_id
, e
), exc_info
=True
2161 def _update_target(self
, task
, ro_vim_item_update
):
2162 table
, _
, temp
= task
["target_record"].partition(":")
2163 _id
, _
, path_vim_status
= temp
.partition(":")
2164 path_item
= path_vim_status
[: path_vim_status
.rfind(".")]
2165 path_item
= path_item
[: path_item
.rfind(".")]
2166 # path_vim_status: dot separated list targeting vim information, e.g. "vdur.10.vim_info.vim:id"
2167 # path_item: dot separated list targeting record information, e.g. "vdur.10"
2169 if ro_vim_item_update
:
2171 path_vim_status
+ "." + k
: v
2172 for k
, v
in ro_vim_item_update
.items()
2174 in ("vim_id", "vim_details", "vim_name", "vim_status", "interfaces")
2177 if path_vim_status
.startswith("vdur."):
2178 # for backward compatibility, add vdur.name apart from vdur.vim_name
2179 if ro_vim_item_update
.get("vim_name"):
2180 update_dict
[path_item
+ ".name"] = ro_vim_item_update
["vim_name"]
2182 # for backward compatibility, add vdur.vim-id apart from vdur.vim_id
2183 if ro_vim_item_update
.get("vim_id"):
2184 update_dict
[path_item
+ ".vim-id"] = ro_vim_item_update
["vim_id"]
2186 # update general status
2187 if ro_vim_item_update
.get("vim_status"):
2188 update_dict
[path_item
+ ".status"] = ro_vim_item_update
[
2192 if ro_vim_item_update
.get("interfaces"):
2193 path_interfaces
= path_item
+ ".interfaces"
2195 for i
, iface
in enumerate(ro_vim_item_update
.get("interfaces")):
2199 path_interfaces
+ ".{}.".format(i
) + k
: v
2200 for k
, v
in iface
.items()
2201 if k
in ("vlan", "compute_node", "pci")
2205 # put ip_address and mac_address with ip-address and mac-address
2206 if iface
.get("ip_address"):
2208 path_interfaces
+ ".{}.".format(i
) + "ip-address"
2209 ] = iface
["ip_address"]
2211 if iface
.get("mac_address"):
2213 path_interfaces
+ ".{}.".format(i
) + "mac-address"
2214 ] = iface
["mac_address"]
2216 if iface
.get("mgmt_vnf_interface") and iface
.get("ip_address"):
2217 update_dict
["ip-address"] = iface
.get("ip_address").split(
2221 if iface
.get("mgmt_vdu_interface") and iface
.get("ip_address"):
2222 update_dict
[path_item
+ ".ip-address"] = iface
.get(
2226 self
.db
.set_one(table
, q_filter
={"_id": _id
}, update_dict
=update_dict
)
2228 update_dict
= {path_item
+ ".status": "DELETED"}
2231 q_filter
={"_id": _id
},
2232 update_dict
=update_dict
,
2233 unset
={path_vim_status
: None},
2236 def _process_delete_db_tasks(self
):
2238 Delete task from database because vnfrs or nsrs or both have been deleted
2239 :return: None. Uses and modify self.tasks_to_delete
2241 while self
.tasks_to_delete
:
2242 task
= self
.tasks_to_delete
[0]
2243 vnfrs_deleted
= None
2244 nsr_id
= task
["nsr_id"]
2246 if task
["target_record"].startswith("vnfrs:"):
2247 # check if nsrs is present
2248 if self
.db
.get_one("nsrs", {"_id": nsr_id
}, fail_on_empty
=False):
2249 vnfrs_deleted
= task
["target_record"].split(":")[1]
2252 self
.delete_db_tasks(self
.db
, nsr_id
, vnfrs_deleted
)
2253 except Exception as e
:
2255 "Error deleting task={}: {}".format(task
["task_id"], e
)
2257 self
.tasks_to_delete
.pop(0)
2260 def delete_db_tasks(db
, nsr_id
, vnfrs_deleted
):
2262 Static method because it is called from osm_ng_ro.ns
2263 :param db: instance of database to use
2264 :param nsr_id: affected nsrs id
2265 :param vnfrs_deleted: only tasks with this vnfr id. If None, all affected by nsr_id
2266 :return: None, exception is fails
2269 for retry
in range(retries
):
2270 ro_tasks
= db
.get_list("ro_tasks", {"tasks.nsr_id": nsr_id
})
2274 for ro_task
in ro_tasks
:
2276 to_delete_ro_task
= True
2278 for index
, task
in enumerate(ro_task
["tasks"]):
2281 elif (not vnfrs_deleted
and task
["nsr_id"] == nsr_id
) or (
2283 and task
["target_record"].startswith("vnfrs:" + vnfrs_deleted
)
2285 db_update
["tasks.{}".format(index
)] = None
2287 # used by other nsr, ro_task cannot be deleted
2288 to_delete_ro_task
= False
2290 # delete or update if nobody has changed ro_task meanwhile. Used modified_at for known if changed
2291 if to_delete_ro_task
:
2295 "_id": ro_task
["_id"],
2296 "modified_at": ro_task
["modified_at"],
2298 fail_on_empty
=False,
2302 db_update
["modified_at"] = now
2306 "_id": ro_task
["_id"],
2307 "modified_at": ro_task
["modified_at"],
2309 update_dict
=db_update
,
2310 fail_on_empty
=False,
2316 raise NsWorkerException("Exceeded {} retries".format(retries
))
2320 self
.logger
.info("Starting")
2322 # step 1: get commands from queue
2324 if self
.vim_targets
:
2325 task
= self
.task_queue
.get(block
=False)
2328 self
.logger
.debug("enters in idle state")
2330 task
= self
.task_queue
.get(block
=True)
2333 if task
[0] == "terminate":
2335 elif task
[0] == "load_vim":
2336 self
.logger
.info("order to load vim {}".format(task
[1]))
2337 self
._load
_vim
(task
[1])
2338 elif task
[0] == "unload_vim":
2339 self
.logger
.info("order to unload vim {}".format(task
[1]))
2340 self
._unload
_vim
(task
[1])
2341 elif task
[0] == "reload_vim":
2342 self
._reload
_vim
(task
[1])
2343 elif task
[0] == "check_vim":
2344 self
.logger
.info("order to check vim {}".format(task
[1]))
2345 self
._check
_vim
(task
[1])
2347 except Exception as e
:
2348 if isinstance(e
, queue
.Empty
):
2351 self
.logger
.critical(
2352 "Error processing task: {}".format(e
), exc_info
=True
2355 # step 2: process pending_tasks, delete not needed tasks
2357 if self
.tasks_to_delete
:
2358 self
._process
_delete
_db
_tasks
()
2361 # Log RO tasks only when loglevel is DEBUG
2362 if self.logger.getEffectiveLevel() == logging.DEBUG:
2363 _ = self._get_db_all_tasks()
2365 ro_task
= self
._get
_db
_task
()
2367 self
._process
_pending
_tasks
(ro_task
)
2371 except Exception as e
:
2372 self
.logger
.critical(
2373 "Unexpected exception at run: " + str(e
), exc_info
=True
2376 self
.logger
.info("Finishing")