1 # -*- coding: utf-8 -*-
4 # Copyright 2018 Telefonica S.A.
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 from typing
import Any
, Dict
, List
24 import logging
.handlers
36 from osm_lcm
import ROclient
37 from osm_lcm
.data_utils
.lcm_config
import LcmCfg
38 from osm_lcm
.data_utils
.nsr
import (
41 get_deployed_vca_list
,
44 from osm_lcm
.data_utils
.vca
import (
53 from osm_lcm
.ng_ro
import NgRoClient
, NgRoException
54 from osm_lcm
.lcm_utils
import (
61 check_juju_bundle_existence
,
62 get_charm_artifact_path
,
66 from osm_lcm
.data_utils
.nsd
import (
67 get_ns_configuration_relation_list
,
71 from osm_lcm
.data_utils
.vnfd
import (
77 get_ee_sorted_initial_config_primitive_list
,
78 get_ee_sorted_terminate_config_primitive_list
,
80 get_virtual_link_profiles
,
85 get_number_of_instances
,
87 get_kdu_resource_profile
,
88 find_software_version
,
91 from osm_lcm
.data_utils
.list_utils
import find_in_list
92 from osm_lcm
.data_utils
.vnfr
import (
96 get_volumes_from_instantiation_params
,
98 from osm_lcm
.data_utils
.dict_utils
import parse_yaml_strings
99 from osm_lcm
.data_utils
.database
.vim_account
import VimAccountDB
100 from n2vc
.definitions
import RelationEndpoint
101 from n2vc
.k8s_helm3_conn
import K8sHelm3Connector
102 from n2vc
.k8s_juju_conn
import K8sJujuConnector
104 from osm_common
.dbbase
import DbException
105 from osm_common
.fsbase
import FsException
107 from osm_lcm
.data_utils
.database
.database
import Database
108 from osm_lcm
.data_utils
.filesystem
.filesystem
import Filesystem
109 from osm_lcm
.data_utils
.wim
import (
111 get_target_wim_attrs
,
112 select_feasible_wim_account
,
115 from n2vc
.n2vc_juju_conn
import N2VCJujuConnector
116 from n2vc
.exceptions
import N2VCException
, N2VCNotFound
, K8sException
118 from osm_lcm
.lcm_helm_conn
import LCMHelmConn
119 from osm_lcm
.osm_config
import OsmConfigBuilder
120 from osm_lcm
.prometheus
import parse_job
122 from copy
import copy
, deepcopy
123 from time
import time
124 from uuid
import uuid4
126 from random
import SystemRandom
128 __author__
= "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
131 class NsLcm(LcmBase
):
132 SUBOPERATION_STATUS_NOT_FOUND
= -1
133 SUBOPERATION_STATUS_NEW
= -2
134 SUBOPERATION_STATUS_SKIP
= -3
135 EE_TLS_NAME
= "ee-tls"
136 task_name_deploy_vca
= "Deploying VCA"
137 rel_operation_types
= {
146 def __init__(self
, msg
, lcm_tasks
, config
: LcmCfg
):
148 Init, Connect to database, filesystem storage, and messaging
149 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
152 super().__init
__(msg
=msg
, logger
=logging
.getLogger("lcm.ns"))
154 self
.db
= Database().instance
.db
155 self
.fs
= Filesystem().instance
.fs
156 self
.lcm_tasks
= lcm_tasks
157 self
.timeout
= config
.timeout
158 self
.ro_config
= config
.RO
159 self
.vca_config
= config
.VCA
161 # create N2VC connector
162 self
.n2vc
= N2VCJujuConnector(
164 on_update_db
=self
._on
_update
_n
2vc
_db
,
169 self
.conn_helm_ee
= LCMHelmConn(
171 vca_config
=self
.vca_config
,
172 on_update_db
=self
._on
_update
_n
2vc
_db
,
175 self
.k8sclusterhelm3
= K8sHelm3Connector(
176 kubectl_command
=self
.vca_config
.kubectlpath
,
177 helm_command
=self
.vca_config
.helm3path
,
184 self
.k8sclusterjuju
= K8sJujuConnector(
185 kubectl_command
=self
.vca_config
.kubectlpath
,
186 juju_command
=self
.vca_config
.jujupath
,
188 on_update_db
=self
._on
_update
_k
8s
_db
,
193 self
.k8scluster_map
= {
194 "helm-chart-v3": self
.k8sclusterhelm3
,
195 "chart": self
.k8sclusterhelm3
,
196 "juju-bundle": self
.k8sclusterjuju
,
197 "juju": self
.k8sclusterjuju
,
201 "lxc_proxy_charm": self
.n2vc
,
202 "native_charm": self
.n2vc
,
203 "k8s_proxy_charm": self
.n2vc
,
204 "helm": self
.conn_helm_ee
,
205 "helm-v3": self
.conn_helm_ee
,
209 self
.RO
= NgRoClient(**self
.ro_config
.to_dict())
211 self
.op_status_map
= {
212 "instantiation": self
.RO
.status
,
213 "termination": self
.RO
.status
,
214 "migrate": self
.RO
.status
,
215 "healing": self
.RO
.recreate_status
,
216 "verticalscale": self
.RO
.status
,
217 "start_stop_rebuild": self
.RO
.status
,
221 def increment_ip_mac(ip_mac
, vm_index
=1):
222 if not isinstance(ip_mac
, str):
225 # try with ipv4 look for last dot
226 i
= ip_mac
.rfind(".")
229 return "{}{}".format(ip_mac
[:i
], int(ip_mac
[i
:]) + vm_index
)
230 # try with ipv6 or mac look for last colon. Operate in hex
231 i
= ip_mac
.rfind(":")
234 # format in hex, len can be 2 for mac or 4 for ipv6
235 return ("{}{:0" + str(len(ip_mac
) - i
) + "x}").format(
236 ip_mac
[:i
], int(ip_mac
[i
:], 16) + vm_index
242 def _on_update_ro_db(self
, nsrs_id
, ro_descriptor
):
243 # self.logger.debug('_on_update_ro_db(nsrs_id={}'.format(nsrs_id))
246 # TODO filter RO descriptor fields...
250 # db_dict['deploymentStatus'] = yaml.dump(ro_descriptor, default_flow_style=False, indent=2)
251 db_dict
["deploymentStatus"] = ro_descriptor
252 self
.update_db_2("nsrs", nsrs_id
, db_dict
)
254 except Exception as e
:
256 "Cannot write database RO deployment for ns={} -> {}".format(nsrs_id
, e
)
259 async def _on_update_n2vc_db(self
, table
, filter, path
, updated_data
, vca_id
=None):
260 # remove last dot from path (if exists)
261 if path
.endswith("."):
264 # self.logger.debug('_on_update_n2vc_db(table={}, filter={}, path={}, updated_data={}'
265 # .format(table, filter, path, updated_data))
267 nsr_id
= filter.get("_id")
269 # read ns record from database
270 nsr
= self
.db
.get_one(table
="nsrs", q_filter
=filter)
271 current_ns_status
= nsr
.get("nsState")
273 # get vca status for NS
274 status_dict
= await self
.n2vc
.get_status(
275 namespace
="." + nsr_id
, yaml_format
=False, vca_id
=vca_id
280 db_dict
["vcaStatus"] = status_dict
282 # update configurationStatus for this VCA
284 vca_index
= int(path
[path
.rfind(".") + 1 :])
287 target_dict
=nsr
, key_list
=("_admin", "deployed", "VCA")
289 vca_status
= vca_list
[vca_index
].get("status")
291 configuration_status_list
= nsr
.get("configurationStatus")
292 config_status
= configuration_status_list
[vca_index
].get("status")
294 if config_status
== "BROKEN" and vca_status
!= "failed":
295 db_dict
["configurationStatus"][vca_index
] = "READY"
296 elif config_status
!= "BROKEN" and vca_status
== "failed":
297 db_dict
["configurationStatus"][vca_index
] = "BROKEN"
298 except Exception as e
:
299 # not update configurationStatus
300 self
.logger
.debug("Error updating vca_index (ignore): {}".format(e
))
302 # if nsState = 'READY' check if juju is reporting some error => nsState = 'DEGRADED'
303 # if nsState = 'DEGRADED' check if all is OK
305 if current_ns_status
in ("READY", "DEGRADED"):
306 error_description
= ""
308 if status_dict
.get("machines"):
309 for machine_id
in status_dict
.get("machines"):
310 machine
= status_dict
.get("machines").get(machine_id
)
311 # check machine agent-status
312 if machine
.get("agent-status"):
313 s
= machine
.get("agent-status").get("status")
316 error_description
+= (
317 "machine {} agent-status={} ; ".format(
321 # check machine instance status
322 if machine
.get("instance-status"):
323 s
= machine
.get("instance-status").get("status")
326 error_description
+= (
327 "machine {} instance-status={} ; ".format(
332 if status_dict
.get("applications"):
333 for app_id
in status_dict
.get("applications"):
334 app
= status_dict
.get("applications").get(app_id
)
335 # check application status
336 if app
.get("status"):
337 s
= app
.get("status").get("status")
340 error_description
+= (
341 "application {} status={} ; ".format(app_id
, s
)
344 if error_description
:
345 db_dict
["errorDescription"] = error_description
346 if current_ns_status
== "READY" and is_degraded
:
347 db_dict
["nsState"] = "DEGRADED"
348 if current_ns_status
== "DEGRADED" and not is_degraded
:
349 db_dict
["nsState"] = "READY"
352 self
.update_db_2("nsrs", nsr_id
, db_dict
)
354 except (asyncio
.CancelledError
, asyncio
.TimeoutError
):
356 except Exception as e
:
357 self
.logger
.warn("Error updating NS state for ns={}: {}".format(nsr_id
, e
))
359 async def _on_update_k8s_db(
360 self
, cluster_uuid
, kdu_instance
, filter=None, vca_id
=None, cluster_type
="juju"
363 Updating vca status in NSR record
364 :param cluster_uuid: UUID of a k8s cluster
365 :param kdu_instance: The unique name of the KDU instance
366 :param filter: To get nsr_id
367 :cluster_type: The cluster type (juju, k8s)
371 # self.logger.debug("_on_update_k8s_db(cluster_uuid={}, kdu_instance={}, filter={}"
372 # .format(cluster_uuid, kdu_instance, filter))
374 nsr_id
= filter.get("_id")
376 vca_status
= await self
.k8scluster_map
[cluster_type
].status_kdu(
377 cluster_uuid
=cluster_uuid
,
378 kdu_instance
=kdu_instance
,
380 complete_status
=True,
386 db_dict
["vcaStatus"] = {nsr_id
: vca_status
}
389 f
"Obtained VCA status for cluster type '{cluster_type}': {vca_status}"
393 self
.update_db_2("nsrs", nsr_id
, db_dict
)
394 except (asyncio
.CancelledError
, asyncio
.TimeoutError
):
396 except Exception as e
:
397 self
.logger
.warn("Error updating NS state for ns={}: {}".format(nsr_id
, e
))
400 def _parse_cloud_init(cloud_init_text
, additional_params
, vnfd_id
, vdu_id
):
403 undefined
=StrictUndefined
,
404 autoescape
=select_autoescape(default_for_string
=True, default
=True),
406 template
= env
.from_string(cloud_init_text
)
407 return template
.render(additional_params
or {})
408 except UndefinedError
as e
:
410 "Variable {} at vnfd[id={}]:vdu[id={}]:cloud-init/cloud-init-"
411 "file, must be provided in the instantiation parameters inside the "
412 "'additionalParamsForVnf/Vdu' block".format(e
, vnfd_id
, vdu_id
)
414 except (TemplateError
, TemplateNotFound
) as e
:
416 "Error parsing Jinja2 to cloud-init content at vnfd[id={}]:vdu[id={}]: {}".format(
421 def _get_vdu_cloud_init_content(self
, vdu
, vnfd
):
422 cloud_init_content
= cloud_init_file
= None
424 if vdu
.get("cloud-init-file"):
425 base_folder
= vnfd
["_admin"]["storage"]
426 if base_folder
["pkg-dir"]:
427 cloud_init_file
= "{}/{}/cloud_init/{}".format(
428 base_folder
["folder"],
429 base_folder
["pkg-dir"],
430 vdu
["cloud-init-file"],
433 cloud_init_file
= "{}/Scripts/cloud_init/{}".format(
434 base_folder
["folder"],
435 vdu
["cloud-init-file"],
437 with self
.fs
.file_open(cloud_init_file
, "r") as ci_file
:
438 cloud_init_content
= ci_file
.read()
439 elif vdu
.get("cloud-init"):
440 cloud_init_content
= vdu
["cloud-init"]
442 return cloud_init_content
443 except FsException
as e
:
445 "Error reading vnfd[id={}]:vdu[id={}]:cloud-init-file={}: {}".format(
446 vnfd
["id"], vdu
["id"], cloud_init_file
, e
450 def _get_vdu_additional_params(self
, db_vnfr
, vdu_id
):
452 (vdur
for vdur
in db_vnfr
.get("vdur") if vdu_id
== vdur
["vdu-id-ref"]), {}
454 additional_params
= vdur
.get("additionalParams")
455 return parse_yaml_strings(additional_params
)
457 def vnfd2RO(self
, vnfd
, new_id
=None, additionalParams
=None, nsrId
=None):
459 Converts creates a new vnfd descriptor for RO base on input OSM IM vnfd
460 :param vnfd: input vnfd
461 :param new_id: overrides vnf id if provided
462 :param additionalParams: Instantiation params for VNFs provided
463 :param nsrId: Id of the NSR
464 :return: copy of vnfd
466 vnfd_RO
= deepcopy(vnfd
)
467 # remove unused by RO configuration, monitoring, scaling and internal keys
468 vnfd_RO
.pop("_id", None)
469 vnfd_RO
.pop("_admin", None)
470 vnfd_RO
.pop("monitoring-param", None)
471 vnfd_RO
.pop("scaling-group-descriptor", None)
472 vnfd_RO
.pop("kdu", None)
473 vnfd_RO
.pop("k8s-cluster", None)
475 vnfd_RO
["id"] = new_id
477 # parse cloud-init or cloud-init-file with the provided variables using Jinja2
478 for vdu
in get_iterable(vnfd_RO
, "vdu"):
479 vdu
.pop("cloud-init-file", None)
480 vdu
.pop("cloud-init", None)
484 def ip_profile_2_RO(ip_profile
):
485 RO_ip_profile
= deepcopy(ip_profile
)
486 if "dns-server" in RO_ip_profile
:
487 if isinstance(RO_ip_profile
["dns-server"], list):
488 RO_ip_profile
["dns-address"] = []
489 for ds
in RO_ip_profile
.pop("dns-server"):
490 RO_ip_profile
["dns-address"].append(ds
["address"])
492 RO_ip_profile
["dns-address"] = RO_ip_profile
.pop("dns-server")
493 if RO_ip_profile
.get("ip-version") == "ipv4":
494 RO_ip_profile
["ip-version"] = "IPv4"
495 if RO_ip_profile
.get("ip-version") == "ipv6":
496 RO_ip_profile
["ip-version"] = "IPv6"
497 if "dhcp-params" in RO_ip_profile
:
498 RO_ip_profile
["dhcp"] = RO_ip_profile
.pop("dhcp-params")
501 def _get_ro_vim_id_for_vim_account(self
, vim_account
):
502 db_vim
= self
.db
.get_one("vim_accounts", {"_id": vim_account
})
503 if db_vim
["_admin"]["operationalState"] != "ENABLED":
505 "VIM={} is not available. operationalState={}".format(
506 vim_account
, db_vim
["_admin"]["operationalState"]
509 RO_vim_id
= db_vim
["_admin"]["deployed"]["RO"]
512 def get_ro_wim_id_for_wim_account(self
, wim_account
):
513 if isinstance(wim_account
, str):
514 db_wim
= self
.db
.get_one("wim_accounts", {"_id": wim_account
})
515 if db_wim
["_admin"]["operationalState"] != "ENABLED":
517 "WIM={} is not available. operationalState={}".format(
518 wim_account
, db_wim
["_admin"]["operationalState"]
521 RO_wim_id
= db_wim
["_admin"]["deployed"]["RO-account"]
526 def scale_vnfr(self
, db_vnfr
, vdu_create
=None, vdu_delete
=None, mark_delete
=False):
527 db_vdu_push_list
= []
529 db_update
= {"_admin.modified": time()}
531 for vdu_id
, vdu_count
in vdu_create
.items():
535 for vdur
in reversed(db_vnfr
["vdur"])
536 if vdur
["vdu-id-ref"] == vdu_id
541 # Read the template saved in the db:
543 "No vdur in the database. Using the vdur-template to scale"
545 vdur_template
= db_vnfr
.get("vdur-template")
546 if not vdur_template
:
548 "Error scaling OUT VNFR for {}. No vnfr or template exists".format(
552 vdur
= vdur_template
[0]
553 # Delete a template from the database after using it
556 {"_id": db_vnfr
["_id"]},
558 pull
={"vdur-template": {"_id": vdur
["_id"]}},
560 for count
in range(vdu_count
):
561 vdur_copy
= deepcopy(vdur
)
562 vdur_copy
["status"] = "BUILD"
563 vdur_copy
["status-detailed"] = None
564 vdur_copy
["ip-address"] = None
565 vdur_copy
["_id"] = str(uuid4())
566 vdur_copy
["count-index"] += count
+ 1
567 vdur_copy
["id"] = "{}-{}".format(
568 vdur_copy
["vdu-id-ref"], vdur_copy
["count-index"]
570 vdur_copy
.pop("vim_info", None)
571 for iface
in vdur_copy
["interfaces"]:
572 if iface
.get("fixed-ip"):
573 iface
["ip-address"] = self
.increment_ip_mac(
574 iface
["ip-address"], count
+ 1
577 iface
.pop("ip-address", None)
578 if iface
.get("fixed-mac"):
579 iface
["mac-address"] = self
.increment_ip_mac(
580 iface
["mac-address"], count
+ 1
583 iface
.pop("mac-address", None)
587 ) # only first vdu can be managment of vnf
588 db_vdu_push_list
.append(vdur_copy
)
589 # self.logger.debug("scale out, adding vdu={}".format(vdur_copy))
591 if len(db_vnfr
["vdur"]) == 1:
592 # The scale will move to 0 instances
594 "Scaling to 0 !, creating the template with the last vdur"
596 template_vdur
= [db_vnfr
["vdur"][0]]
597 for vdu_id
, vdu_count
in vdu_delete
.items():
599 indexes_to_delete
= [
601 for iv
in enumerate(db_vnfr
["vdur"])
602 if iv
[1]["vdu-id-ref"] == vdu_id
606 "vdur.{}.status".format(i
): "DELETING"
607 for i
in indexes_to_delete
[-vdu_count
:]
611 # it must be deleted one by one because common.db does not allow otherwise
614 for v
in reversed(db_vnfr
["vdur"])
615 if v
["vdu-id-ref"] == vdu_id
617 for vdu
in vdus_to_delete
[:vdu_count
]:
620 {"_id": db_vnfr
["_id"]},
622 pull
={"vdur": {"_id": vdu
["_id"]}},
626 db_push
["vdur"] = db_vdu_push_list
628 db_push
["vdur-template"] = template_vdur
631 db_vnfr
["vdur-template"] = template_vdur
632 self
.db
.set_one("vnfrs", {"_id": db_vnfr
["_id"]}, db_update
, push_list
=db_push
)
633 # modify passed dictionary db_vnfr
634 db_vnfr_
= self
.db
.get_one("vnfrs", {"_id": db_vnfr
["_id"]})
635 db_vnfr
["vdur"] = db_vnfr_
["vdur"]
637 def ns_update_nsr(self
, ns_update_nsr
, db_nsr
, nsr_desc_RO
):
639 Updates database nsr with the RO info for the created vld
640 :param ns_update_nsr: dictionary to be filled with the updated info
641 :param db_nsr: content of db_nsr. This is also modified
642 :param nsr_desc_RO: nsr descriptor from RO
643 :return: Nothing, LcmException is raised on errors
646 for vld_index
, vld
in enumerate(get_iterable(db_nsr
, "vld")):
647 for net_RO
in get_iterable(nsr_desc_RO
, "nets"):
648 if vld
["id"] != net_RO
.get("ns_net_osm_id"):
650 vld
["vim-id"] = net_RO
.get("vim_net_id")
651 vld
["name"] = net_RO
.get("vim_name")
652 vld
["status"] = net_RO
.get("status")
653 vld
["status-detailed"] = net_RO
.get("error_msg")
654 ns_update_nsr
["vld.{}".format(vld_index
)] = vld
658 "ns_update_nsr: Not found vld={} at RO info".format(vld
["id"])
661 def set_vnfr_at_error(self
, db_vnfrs
, error_text
):
663 for db_vnfr
in db_vnfrs
.values():
664 vnfr_update
= {"status": "ERROR"}
665 for vdu_index
, vdur
in enumerate(get_iterable(db_vnfr
, "vdur")):
666 if "status" not in vdur
:
667 vdur
["status"] = "ERROR"
668 vnfr_update
["vdur.{}.status".format(vdu_index
)] = "ERROR"
670 vdur
["status-detailed"] = str(error_text
)
672 "vdur.{}.status-detailed".format(vdu_index
)
674 self
.update_db_2("vnfrs", db_vnfr
["_id"], vnfr_update
)
675 except DbException
as e
:
676 self
.logger
.error("Cannot update vnf. {}".format(e
))
678 def ns_update_vnfr(self
, db_vnfrs
, nsr_desc_RO
):
680 Updates database vnfr with the RO info, e.g. ip_address, vim_id... Descriptor db_vnfrs is also updated
681 :param db_vnfrs: dictionary with member-vnf-index: vnfr-content
682 :param nsr_desc_RO: nsr descriptor from RO
683 :return: Nothing, LcmException is raised on errors
685 for vnf_index
, db_vnfr
in db_vnfrs
.items():
686 for vnf_RO
in nsr_desc_RO
["vnfs"]:
687 if vnf_RO
["member_vnf_index"] != vnf_index
:
690 if vnf_RO
.get("ip_address"):
691 db_vnfr
["ip-address"] = vnfr_update
["ip-address"] = vnf_RO
[
694 elif not db_vnfr
.get("ip-address"):
695 if db_vnfr
.get("vdur"): # if not VDUs, there is not ip_address
696 raise LcmExceptionNoMgmtIP(
697 "ns member_vnf_index '{}' has no IP address".format(
702 for vdu_index
, vdur
in enumerate(get_iterable(db_vnfr
, "vdur")):
703 vdur_RO_count_index
= 0
704 if vdur
.get("pdu-type"):
706 for vdur_RO
in get_iterable(vnf_RO
, "vms"):
707 if vdur
["vdu-id-ref"] != vdur_RO
["vdu_osm_id"]:
709 if vdur
["count-index"] != vdur_RO_count_index
:
710 vdur_RO_count_index
+= 1
712 vdur
["vim-id"] = vdur_RO
.get("vim_vm_id")
713 if vdur_RO
.get("ip_address"):
714 vdur
["ip-address"] = vdur_RO
["ip_address"].split(";")[0]
716 vdur
["ip-address"] = None
717 vdur
["vdu-id-ref"] = vdur_RO
.get("vdu_osm_id")
718 vdur
["name"] = vdur_RO
.get("vim_name")
719 vdur
["status"] = vdur_RO
.get("status")
720 vdur
["status-detailed"] = vdur_RO
.get("error_msg")
721 for ifacer
in get_iterable(vdur
, "interfaces"):
722 for interface_RO
in get_iterable(vdur_RO
, "interfaces"):
723 if ifacer
["name"] == interface_RO
.get("internal_name"):
724 ifacer
["ip-address"] = interface_RO
.get(
727 ifacer
["mac-address"] = interface_RO
.get(
733 "ns_update_vnfr: Not found member_vnf_index={} vdur={} interface={} "
734 "from VIM info".format(
735 vnf_index
, vdur
["vdu-id-ref"], ifacer
["name"]
738 vnfr_update
["vdur.{}".format(vdu_index
)] = vdur
742 "ns_update_vnfr: Not found member_vnf_index={} vdur={} count_index={} from "
744 vnf_index
, vdur
["vdu-id-ref"], vdur
["count-index"]
748 for vld_index
, vld
in enumerate(get_iterable(db_vnfr
, "vld")):
749 for net_RO
in get_iterable(nsr_desc_RO
, "nets"):
750 if vld
["id"] != net_RO
.get("vnf_net_osm_id"):
752 vld
["vim-id"] = net_RO
.get("vim_net_id")
753 vld
["name"] = net_RO
.get("vim_name")
754 vld
["status"] = net_RO
.get("status")
755 vld
["status-detailed"] = net_RO
.get("error_msg")
756 vnfr_update
["vld.{}".format(vld_index
)] = vld
760 "ns_update_vnfr: Not found member_vnf_index={} vld={} from VIM info".format(
765 self
.update_db_2("vnfrs", db_vnfr
["_id"], vnfr_update
)
770 "ns_update_vnfr: Not found member_vnf_index={} from VIM info".format(
775 def _get_ns_config_info(self
, nsr_id
):
777 Generates a mapping between vnf,vdu elements and the N2VC id
778 :param nsr_id: id of nsr to get last database _admin.deployed.VCA that contains this list
779 :return: a dictionary with {osm-config-mapping: {}} where its element contains:
780 "<member-vnf-index>": <N2VC-id> for a vnf configuration, or
781 "<member-vnf-index>.<vdu.id>.<vdu replica(0, 1,..)>": <N2VC-id> for a vdu configuration
783 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
784 vca_deployed_list
= db_nsr
["_admin"]["deployed"]["VCA"]
786 ns_config_info
= {"osm-config-mapping": mapping
}
787 for vca
in vca_deployed_list
:
788 if not vca
["member-vnf-index"]:
790 if not vca
["vdu_id"]:
791 mapping
[vca
["member-vnf-index"]] = vca
["application"]
795 vca
["member-vnf-index"], vca
["vdu_id"], vca
["vdu_count_index"]
797 ] = vca
["application"]
798 return ns_config_info
800 async def _instantiate_ng_ro(
816 def get_vim_account(vim_account_id
):
818 if vim_account_id
in db_vims
:
819 return db_vims
[vim_account_id
]
820 db_vim
= self
.db
.get_one("vim_accounts", {"_id": vim_account_id
})
821 db_vims
[vim_account_id
] = db_vim
824 # modify target_vld info with instantiation parameters
825 def parse_vld_instantiation_params(
826 target_vim
, target_vld
, vld_params
, target_sdn
828 if vld_params
.get("ip-profile"):
829 target_vld
["vim_info"][target_vim
]["ip_profile"] = vld_to_ro_ip_profile(
830 vld_params
["ip-profile"]
832 if vld_params
.get("provider-network"):
833 target_vld
["vim_info"][target_vim
]["provider_network"] = vld_params
[
836 if "sdn-ports" in vld_params
["provider-network"] and target_sdn
:
837 target_vld
["vim_info"][target_sdn
]["sdn-ports"] = vld_params
[
841 # check if WIM is needed; if needed, choose a feasible WIM able to connect VIMs
842 # if wim_account_id is specified in vld_params, validate if it is feasible.
843 wim_account_id
, db_wim
= select_feasible_wim_account(
844 db_nsr
, db_vnfrs
, target_vld
, vld_params
, self
.logger
848 # WIM is needed and a feasible one was found, populate WIM target and SDN ports
849 self
.logger
.info("WIM selected: {:s}".format(str(wim_account_id
)))
850 # update vld_params with correct WIM account Id
851 vld_params
["wimAccountId"] = wim_account_id
853 target_wim
= "wim:{}".format(wim_account_id
)
854 target_wim_attrs
= get_target_wim_attrs(nsr_id
, target_vld
, vld_params
)
855 sdn_ports
= get_sdn_ports(vld_params
, db_wim
)
856 if len(sdn_ports
) > 0:
857 target_vld
["vim_info"][target_wim
] = target_wim_attrs
858 target_vld
["vim_info"][target_wim
]["sdn-ports"] = sdn_ports
861 "Target VLD with WIM data: {:s}".format(str(target_vld
))
864 for param
in ("vim-network-name", "vim-network-id"):
865 if vld_params
.get(param
):
866 if isinstance(vld_params
[param
], dict):
867 for vim
, vim_net
in vld_params
[param
].items():
868 other_target_vim
= "vim:" + vim
870 target_vld
["vim_info"],
871 (other_target_vim
, param
.replace("-", "_")),
874 else: # isinstance str
875 target_vld
["vim_info"][target_vim
][
876 param
.replace("-", "_")
877 ] = vld_params
[param
]
878 if vld_params
.get("common_id"):
879 target_vld
["common_id"] = vld_params
.get("common_id")
881 # modify target["ns"]["vld"] with instantiation parameters to override vnf vim-account
882 def update_ns_vld_target(target
, ns_params
):
883 for vnf_params
in ns_params
.get("vnf", ()):
884 if vnf_params
.get("vimAccountId"):
888 for vnfr
in db_vnfrs
.values()
889 if vnf_params
["member-vnf-index"]
890 == vnfr
["member-vnf-index-ref"]
894 vdur
= next((vdur
for vdur
in target_vnf
.get("vdur", ())), None)
897 for a_index
, a_vld
in enumerate(target
["ns"]["vld"]):
898 target_vld
= find_in_list(
899 get_iterable(vdur
, "interfaces"),
900 lambda iface
: iface
.get("ns-vld-id") == a_vld
["name"],
903 vld_params
= find_in_list(
904 get_iterable(ns_params
, "vld"),
905 lambda v_vld
: v_vld
["name"] in (a_vld
["name"], a_vld
["id"]),
908 if vnf_params
.get("vimAccountId") not in a_vld
.get(
911 target_vim_network_list
= [
912 v
for _
, v
in a_vld
.get("vim_info").items()
914 target_vim_network_name
= next(
916 item
.get("vim_network_name", "")
917 for item
in target_vim_network_list
922 target
["ns"]["vld"][a_index
].get("vim_info").update(
924 "vim:{}".format(vnf_params
["vimAccountId"]): {
925 "vim_network_name": target_vim_network_name
,
931 for param
in ("vim-network-name", "vim-network-id"):
932 if vld_params
.get(param
) and isinstance(
933 vld_params
[param
], dict
935 for vim
, vim_net
in vld_params
[
938 other_target_vim
= "vim:" + vim
940 target
["ns"]["vld"][a_index
].get(
945 param
.replace("-", "_"),
950 nslcmop_id
= db_nslcmop
["_id"]
952 "name": db_nsr
["name"],
955 "image": deepcopy(db_nsr
["image"]),
956 "flavor": deepcopy(db_nsr
["flavor"]),
957 "action_id": nslcmop_id
,
958 "cloud_init_content": {},
960 for image
in target
["image"]:
961 image
["vim_info"] = {}
962 for flavor
in target
["flavor"]:
963 flavor
["vim_info"] = {}
964 if db_nsr
.get("shared-volumes"):
965 target
["shared-volumes"] = deepcopy(db_nsr
["shared-volumes"])
966 for shared_volumes
in target
["shared-volumes"]:
967 shared_volumes
["vim_info"] = {}
968 if db_nsr
.get("affinity-or-anti-affinity-group"):
969 target
["affinity-or-anti-affinity-group"] = deepcopy(
970 db_nsr
["affinity-or-anti-affinity-group"]
972 for affinity_or_anti_affinity_group
in target
[
973 "affinity-or-anti-affinity-group"
975 affinity_or_anti_affinity_group
["vim_info"] = {}
977 if db_nslcmop
.get("lcmOperationType") != "instantiate":
978 # get parameters of instantiation:
979 db_nslcmop_instantiate
= self
.db
.get_list(
982 "nsInstanceId": db_nslcmop
["nsInstanceId"],
983 "lcmOperationType": "instantiate",
986 ns_params
= db_nslcmop_instantiate
.get("operationParams")
988 ns_params
= db_nslcmop
.get("operationParams")
989 ssh_keys_instantiation
= ns_params
.get("ssh_keys") or []
990 ssh_keys_all
= ssh_keys_instantiation
+ (n2vc_key_list
or [])
993 for vld_index
, vld
in enumerate(db_nsr
.get("vld")):
994 target_vim
= "vim:{}".format(ns_params
["vimAccountId"])
998 "mgmt-network": vld
.get("mgmt-network", False),
999 "type": vld
.get("type"),
1002 "vim_network_name": vld
.get("vim-network-name"),
1003 "vim_account_id": ns_params
["vimAccountId"],
1007 # check if this network needs SDN assist
1008 if vld
.get("pci-interfaces"):
1009 db_vim
= get_vim_account(ns_params
["vimAccountId"])
1010 if vim_config
:= db_vim
.get("config"):
1011 if sdnc_id
:= vim_config
.get("sdn-controller"):
1012 sdn_vld
= "nsrs:{}:vld.{}".format(nsr_id
, vld
["id"])
1013 target_sdn
= "sdn:{}".format(sdnc_id
)
1014 target_vld
["vim_info"][target_sdn
] = {
1016 "target_vim": target_vim
,
1018 "type": vld
.get("type"),
1021 nsd_vnf_profiles
= get_vnf_profiles(nsd
)
1022 for nsd_vnf_profile
in nsd_vnf_profiles
:
1023 for cp
in nsd_vnf_profile
["virtual-link-connectivity"]:
1024 if cp
["virtual-link-profile-id"] == vld
["id"]:
1026 "member_vnf:{}.{}".format(
1027 cp
["constituent-cpd-id"][0][
1028 "constituent-base-element-id"
1030 cp
["constituent-cpd-id"][0]["constituent-cpd-id"],
1032 ] = "nsrs:{}:vld.{}".format(nsr_id
, vld_index
)
1034 # check at nsd descriptor, if there is an ip-profile
1036 nsd_vlp
= find_in_list(
1037 get_virtual_link_profiles(nsd
),
1038 lambda a_link_profile
: a_link_profile
["virtual-link-desc-id"]
1043 and nsd_vlp
.get("virtual-link-protocol-data")
1044 and nsd_vlp
["virtual-link-protocol-data"].get("l3-protocol-data")
1046 vld_params
["ip-profile"] = nsd_vlp
["virtual-link-protocol-data"][
1050 # update vld_params with instantiation params
1051 vld_instantiation_params
= find_in_list(
1052 get_iterable(ns_params
, "vld"),
1053 lambda a_vld
: a_vld
["name"] in (vld
["name"], vld
["id"]),
1055 if vld_instantiation_params
:
1056 vld_params
.update(vld_instantiation_params
)
1057 parse_vld_instantiation_params(target_vim
, target_vld
, vld_params
, None)
1058 target
["ns"]["vld"].append(target_vld
)
1059 # Update the target ns_vld if vnf vim_account is overriden by instantiation params
1060 update_ns_vld_target(target
, ns_params
)
1062 for vnfr
in db_vnfrs
.values():
1063 vnfd
= find_in_list(
1064 db_vnfds
, lambda db_vnf
: db_vnf
["id"] == vnfr
["vnfd-ref"]
1066 vnf_params
= find_in_list(
1067 get_iterable(ns_params
, "vnf"),
1068 lambda a_vnf
: a_vnf
["member-vnf-index"] == vnfr
["member-vnf-index-ref"],
1070 target_vnf
= deepcopy(vnfr
)
1071 target_vim
= "vim:{}".format(vnfr
["vim-account-id"])
1072 for vld
in target_vnf
.get("vld", ()):
1073 # check if connected to a ns.vld, to fill target'
1074 vnf_cp
= find_in_list(
1075 vnfd
.get("int-virtual-link-desc", ()),
1076 lambda cpd
: cpd
.get("id") == vld
["id"],
1079 ns_cp
= "member_vnf:{}.{}".format(
1080 vnfr
["member-vnf-index-ref"], vnf_cp
["id"]
1082 if cp2target
.get(ns_cp
):
1083 vld
["target"] = cp2target
[ns_cp
]
1086 target_vim
: {"vim_network_name": vld
.get("vim-network-name")}
1088 # check if this network needs SDN assist
1090 if vld
.get("pci-interfaces"):
1091 db_vim
= get_vim_account(vnfr
["vim-account-id"])
1092 sdnc_id
= db_vim
["config"].get("sdn-controller")
1094 sdn_vld
= "vnfrs:{}:vld.{}".format(target_vnf
["_id"], vld
["id"])
1095 target_sdn
= "sdn:{}".format(sdnc_id
)
1096 vld
["vim_info"][target_sdn
] = {
1098 "target_vim": target_vim
,
1100 "type": vld
.get("type"),
1103 # check at vnfd descriptor, if there is an ip-profile
1105 vnfd_vlp
= find_in_list(
1106 get_virtual_link_profiles(vnfd
),
1107 lambda a_link_profile
: a_link_profile
["id"] == vld
["id"],
1111 and vnfd_vlp
.get("virtual-link-protocol-data")
1112 and vnfd_vlp
["virtual-link-protocol-data"].get("l3-protocol-data")
1114 vld_params
["ip-profile"] = vnfd_vlp
["virtual-link-protocol-data"][
1117 # update vld_params with instantiation params
1119 vld_instantiation_params
= find_in_list(
1120 get_iterable(vnf_params
, "internal-vld"),
1121 lambda i_vld
: i_vld
["name"] == vld
["id"],
1123 if vld_instantiation_params
:
1124 vld_params
.update(vld_instantiation_params
)
1125 parse_vld_instantiation_params(target_vim
, vld
, vld_params
, target_sdn
)
1128 for vdur
in target_vnf
.get("vdur", ()):
1129 if vdur
.get("status") == "DELETING" or vdur
.get("pdu-type"):
1130 continue # This vdu must not be created
1131 vdur
["vim_info"] = {"vim_account_id": vnfr
["vim-account-id"]}
1133 self
.logger
.debug("NS > ssh_keys > {}".format(ssh_keys_all
))
1136 vdu_configuration
= get_configuration(vnfd
, vdur
["vdu-id-ref"])
1137 vnf_configuration
= get_configuration(vnfd
, vnfd
["id"])
1140 and vdu_configuration
.get("config-access")
1141 and vdu_configuration
.get("config-access").get("ssh-access")
1143 vdur
["ssh-keys"] = ssh_keys_all
1144 vdur
["ssh-access-required"] = vdu_configuration
[
1146 ]["ssh-access"]["required"]
1149 and vnf_configuration
.get("config-access")
1150 and vnf_configuration
.get("config-access").get("ssh-access")
1151 and any(iface
.get("mgmt-vnf") for iface
in vdur
["interfaces"])
1153 vdur
["ssh-keys"] = ssh_keys_all
1154 vdur
["ssh-access-required"] = vnf_configuration
[
1156 ]["ssh-access"]["required"]
1157 elif ssh_keys_instantiation
and find_in_list(
1158 vdur
["interfaces"], lambda iface
: iface
.get("mgmt-vnf")
1160 vdur
["ssh-keys"] = ssh_keys_instantiation
1162 self
.logger
.debug("NS > vdur > {}".format(vdur
))
1164 vdud
= get_vdu(vnfd
, vdur
["vdu-id-ref"])
1166 if vdud
.get("cloud-init-file"):
1167 vdur
["cloud-init"] = "{}:file:{}".format(
1168 vnfd
["_id"], vdud
.get("cloud-init-file")
1170 # read file and put content at target.cloul_init_content. Avoid ng_ro to use shared package system
1171 if vdur
["cloud-init"] not in target
["cloud_init_content"]:
1172 base_folder
= vnfd
["_admin"]["storage"]
1173 if base_folder
["pkg-dir"]:
1174 cloud_init_file
= "{}/{}/cloud_init/{}".format(
1175 base_folder
["folder"],
1176 base_folder
["pkg-dir"],
1177 vdud
.get("cloud-init-file"),
1180 cloud_init_file
= "{}/Scripts/cloud_init/{}".format(
1181 base_folder
["folder"],
1182 vdud
.get("cloud-init-file"),
1184 with self
.fs
.file_open(cloud_init_file
, "r") as ci_file
:
1185 target
["cloud_init_content"][
1188 elif vdud
.get("cloud-init"):
1189 vdur
["cloud-init"] = "{}:vdu:{}".format(
1190 vnfd
["_id"], get_vdu_index(vnfd
, vdur
["vdu-id-ref"])
1192 # put content at target.cloul_init_content. Avoid ng_ro read vnfd descriptor
1193 target
["cloud_init_content"][vdur
["cloud-init"]] = vdud
[
1196 vdur
["additionalParams"] = vdur
.get("additionalParams") or {}
1197 deploy_params_vdu
= self
._format
_additional
_params
(
1198 vdur
.get("additionalParams") or {}
1200 deploy_params_vdu
["OSM"] = get_osm_params(
1201 vnfr
, vdur
["vdu-id-ref"], vdur
["count-index"]
1203 vdur
["additionalParams"] = deploy_params_vdu
1206 ns_flavor
= target
["flavor"][int(vdur
["ns-flavor-id"])]
1207 if target_vim
not in ns_flavor
["vim_info"]:
1208 ns_flavor
["vim_info"][target_vim
] = {}
1211 # in case alternative images are provided we must check if they should be applied
1212 # for the vim_type, modify the vim_type taking into account
1213 ns_image_id
= int(vdur
["ns-image-id"])
1214 if vdur
.get("alt-image-ids"):
1215 db_vim
= get_vim_account(vnfr
["vim-account-id"])
1216 vim_type
= db_vim
["vim_type"]
1217 for alt_image_id
in vdur
.get("alt-image-ids"):
1218 ns_alt_image
= target
["image"][int(alt_image_id
)]
1219 if vim_type
== ns_alt_image
.get("vim-type"):
1220 # must use alternative image
1222 "use alternative image id: {}".format(alt_image_id
)
1224 ns_image_id
= alt_image_id
1225 vdur
["ns-image-id"] = ns_image_id
1227 ns_image
= target
["image"][int(ns_image_id
)]
1228 if target_vim
not in ns_image
["vim_info"]:
1229 ns_image
["vim_info"][target_vim
] = {}
1232 if vdur
.get("affinity-or-anti-affinity-group-id"):
1233 for ags_id
in vdur
["affinity-or-anti-affinity-group-id"]:
1234 ns_ags
= target
["affinity-or-anti-affinity-group"][int(ags_id
)]
1235 if target_vim
not in ns_ags
["vim_info"]:
1236 ns_ags
["vim_info"][target_vim
] = {}
1239 if vdur
.get("shared-volumes-id"):
1240 for sv_id
in vdur
["shared-volumes-id"]:
1241 ns_sv
= find_in_list(
1242 target
["shared-volumes"], lambda sv
: sv_id
in sv
["id"]
1245 ns_sv
["vim_info"][target_vim
] = {}
1247 vdur
["vim_info"] = {target_vim
: {}}
1248 # instantiation parameters
1250 vdu_instantiation_params
= find_in_list(
1251 get_iterable(vnf_params
, "vdu"),
1252 lambda i_vdu
: i_vdu
["id"] == vdud
["id"],
1254 if vdu_instantiation_params
:
1255 # Parse the vdu_volumes from the instantiation params
1256 vdu_volumes
= get_volumes_from_instantiation_params(
1257 vdu_instantiation_params
, vdud
1259 vdur
["additionalParams"]["OSM"]["vdu_volumes"] = vdu_volumes
1260 vdur
["additionalParams"]["OSM"][
1262 ] = vdu_instantiation_params
.get("vim-flavor-id")
1263 vdur_list
.append(vdur
)
1264 target_vnf
["vdur"] = vdur_list
1265 target
["vnf"].append(target_vnf
)
1267 self
.logger
.debug("Send to RO > nsr_id={} target={}".format(nsr_id
, target
))
1268 desc
= await self
.RO
.deploy(nsr_id
, target
)
1269 self
.logger
.debug("RO return > {}".format(desc
))
1270 action_id
= desc
["action_id"]
1271 await self
._wait
_ng
_ro
(
1278 operation
="instantiation",
1283 "_admin.deployed.RO.operational-status": "running",
1284 "detailed-status": " ".join(stage
),
1286 # db_nsr["_admin.deployed.RO.detailed-status"] = "Deployed at VIM"
1287 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
1288 self
._write
_op
_status
(nslcmop_id
, stage
)
1290 logging_text
+ "ns deployed at RO. RO_id={}".format(action_id
)
1294 async def _wait_ng_ro(
1304 detailed_status_old
= None
1306 start_time
= start_time
or time()
1307 while time() <= start_time
+ timeout
:
1308 desc_status
= await self
.op_status_map
[operation
](nsr_id
, action_id
)
1309 self
.logger
.debug("Wait NG RO > {}".format(desc_status
))
1310 if desc_status
["status"] == "FAILED":
1311 raise NgRoException(desc_status
["details"])
1312 elif desc_status
["status"] == "BUILD":
1314 stage
[2] = "VIM: ({})".format(desc_status
["details"])
1315 elif desc_status
["status"] == "DONE":
1317 stage
[2] = "Deployed at VIM"
1320 assert False, "ROclient.check_ns_status returns unknown {}".format(
1321 desc_status
["status"]
1323 if stage
and nslcmop_id
and stage
[2] != detailed_status_old
:
1324 detailed_status_old
= stage
[2]
1325 db_nsr_update
["detailed-status"] = " ".join(stage
)
1326 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
1327 self
._write
_op
_status
(nslcmop_id
, stage
)
1328 await asyncio
.sleep(15)
1329 else: # timeout_ns_deploy
1330 raise NgRoException("Timeout waiting ns to deploy")
1332 async def _terminate_ng_ro(
1333 self
, logging_text
, nsr_deployed
, nsr_id
, nslcmop_id
, stage
1338 start_deploy
= time()
1345 "action_id": nslcmop_id
,
1347 desc
= await self
.RO
.deploy(nsr_id
, target
)
1348 action_id
= desc
["action_id"]
1349 db_nsr_update
["_admin.deployed.RO.nsr_status"] = "DELETING"
1352 + "ns terminate action at RO. action_id={}".format(action_id
)
1356 delete_timeout
= 20 * 60 # 20 minutes
1357 await self
._wait
_ng
_ro
(
1364 operation
="termination",
1366 db_nsr_update
["_admin.deployed.RO.nsr_status"] = "DELETED"
1368 await self
.RO
.delete(nsr_id
)
1369 except NgRoException
as e
:
1370 if e
.http_code
== 404: # not found
1371 db_nsr_update
["_admin.deployed.RO.nsr_id"] = None
1372 db_nsr_update
["_admin.deployed.RO.nsr_status"] = "DELETED"
1374 logging_text
+ "RO_action_id={} already deleted".format(action_id
)
1376 elif e
.http_code
== 409: # conflict
1377 failed_detail
.append("delete conflict: {}".format(e
))
1380 + "RO_action_id={} delete conflict: {}".format(action_id
, e
)
1383 failed_detail
.append("delete error: {}".format(e
))
1386 + "RO_action_id={} delete error: {}".format(action_id
, e
)
1388 except Exception as e
:
1389 failed_detail
.append("delete error: {}".format(e
))
1391 logging_text
+ "RO_action_id={} delete error: {}".format(action_id
, e
)
1395 stage
[2] = "Error deleting from VIM"
1397 stage
[2] = "Deleted from VIM"
1398 db_nsr_update
["detailed-status"] = " ".join(stage
)
1399 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
1400 self
._write
_op
_status
(nslcmop_id
, stage
)
1403 raise LcmException("; ".join(failed_detail
))
1406 async def instantiate_RO(
1420 :param logging_text: preffix text to use at logging
1421 :param nsr_id: nsr identity
1422 :param nsd: database content of ns descriptor
1423 :param db_nsr: database content of ns record
1424 :param db_nslcmop: database content of ns operation, in this case, 'instantiate'
1426 :param db_vnfds: database content of vnfds, indexed by id (not _id). {id: {vnfd_object}, ...}
1427 :param n2vc_key_list: ssh-public-key list to be inserted to management vdus via cloud-init
1428 :param stage: list with 3 items: [general stage, tasks, vim_specific]. This task will write over vim_specific
1429 :return: None or exception
1432 start_deploy
= time()
1433 ns_params
= db_nslcmop
.get("operationParams")
1434 if ns_params
and ns_params
.get("timeout_ns_deploy"):
1435 timeout_ns_deploy
= ns_params
["timeout_ns_deploy"]
1437 timeout_ns_deploy
= self
.timeout
.ns_deploy
1439 # Check for and optionally request placement optimization. Database will be updated if placement activated
1440 stage
[2] = "Waiting for Placement."
1441 if await self
._do
_placement
(logging_text
, db_nslcmop
, db_vnfrs
):
1442 # in case of placement change ns_params[vimAcountId) if not present at any vnfrs
1443 for vnfr
in db_vnfrs
.values():
1444 if ns_params
["vimAccountId"] == vnfr
["vim-account-id"]:
1447 ns_params
["vimAccountId"] == vnfr
["vim-account-id"]
1449 return await self
._instantiate
_ng
_ro
(
1462 except Exception as e
:
1463 stage
[2] = "ERROR deploying at VIM"
1464 self
.set_vnfr_at_error(db_vnfrs
, str(e
))
1466 "Error deploying at VIM {}".format(e
),
1467 exc_info
=not isinstance(
1470 ROclient
.ROClientException
,
1479 async def wait_kdu_up(self
, logging_text
, nsr_id
, vnfr_id
, kdu_name
):
1481 Wait for kdu to be up, get ip address
1482 :param logging_text: prefix use for logging
1486 :return: IP address, K8s services
1489 # self.logger.debug(logging_text + "Starting wait_kdu_up")
1492 while nb_tries
< 360:
1493 db_vnfr
= self
.db
.get_one("vnfrs", {"_id": vnfr_id
})
1497 for x
in get_iterable(db_vnfr
, "kdur")
1498 if x
.get("kdu-name") == kdu_name
1504 "Not found vnfr_id={}, kdu_name={}".format(vnfr_id
, kdu_name
)
1506 if kdur
.get("status"):
1507 if kdur
["status"] in ("READY", "ENABLED"):
1508 return kdur
.get("ip-address"), kdur
.get("services")
1511 "target KDU={} is in error state".format(kdu_name
)
1514 await asyncio
.sleep(10)
1516 raise LcmException("Timeout waiting KDU={} instantiated".format(kdu_name
))
1518 async def wait_vm_up_insert_key_ro(
1519 self
, logging_text
, nsr_id
, vnfr_id
, vdu_id
, vdu_index
, pub_key
=None, user
=None
1522 Wait for ip addres at RO, and optionally, insert public key in virtual machine
1523 :param logging_text: prefix use for logging
1528 :param pub_key: public ssh key to inject, None to skip
1529 :param user: user to apply the public ssh key
1533 self
.logger
.debug(logging_text
+ "Starting wait_vm_up_insert_key_ro")
1535 target_vdu_id
= None
1540 if ro_retries
>= 360: # 1 hour
1542 "Not found _admin.deployed.RO.nsr_id for nsr_id: {}".format(nsr_id
)
1545 await asyncio
.sleep(10)
1548 if not target_vdu_id
:
1549 db_vnfr
= self
.db
.get_one("vnfrs", {"_id": vnfr_id
})
1551 if not vdu_id
: # for the VNF case
1552 if db_vnfr
.get("status") == "ERROR":
1554 "Cannot inject ssh-key because target VNF is in error state"
1556 ip_address
= db_vnfr
.get("ip-address")
1562 for x
in get_iterable(db_vnfr
, "vdur")
1563 if x
.get("ip-address") == ip_address
1571 for x
in get_iterable(db_vnfr
, "vdur")
1572 if x
.get("vdu-id-ref") == vdu_id
1573 and x
.get("count-index") == vdu_index
1579 not vdur
and len(db_vnfr
.get("vdur", ())) == 1
1580 ): # If only one, this should be the target vdu
1581 vdur
= db_vnfr
["vdur"][0]
1584 "Not found vnfr_id={}, vdu_id={}, vdu_index={}".format(
1585 vnfr_id
, vdu_id
, vdu_index
1588 # New generation RO stores information at "vim_info"
1591 if vdur
.get("vim_info"):
1593 t
for t
in vdur
["vim_info"]
1594 ) # there should be only one key
1595 ng_ro_status
= vdur
["vim_info"][target_vim
].get("vim_status")
1597 vdur
.get("pdu-type")
1598 or vdur
.get("status") == "ACTIVE"
1599 or ng_ro_status
== "ACTIVE"
1601 ip_address
= vdur
.get("ip-address")
1604 target_vdu_id
= vdur
["vdu-id-ref"]
1605 elif vdur
.get("status") == "ERROR" or ng_ro_status
== "ERROR":
1607 "Cannot inject ssh-key because target VM is in error state"
1610 if not target_vdu_id
:
1613 # inject public key into machine
1614 if pub_key
and user
:
1615 self
.logger
.debug(logging_text
+ "Inserting RO key")
1616 self
.logger
.debug("SSH > PubKey > {}".format(pub_key
))
1617 if vdur
.get("pdu-type"):
1618 self
.logger
.error(logging_text
+ "Cannot inject ssh-ky to a PDU")
1623 "action": "inject_ssh_key",
1627 "vnf": [{"_id": vnfr_id
, "vdur": [{"id": vdur
["id"]}]}],
1629 desc
= await self
.RO
.deploy(nsr_id
, target
)
1630 action_id
= desc
["action_id"]
1631 await self
._wait
_ng
_ro
(
1632 nsr_id
, action_id
, timeout
=600, operation
="instantiation"
1635 except NgRoException
as e
:
1637 "Reaching max tries injecting key. Error: {}".format(e
)
1644 async def _wait_dependent_n2vc(self
, nsr_id
, vca_deployed_list
, vca_index
):
1646 Wait until dependent VCA deployments have been finished. NS wait for VNFs and VDUs. VNFs for VDUs
1648 my_vca
= vca_deployed_list
[vca_index
]
1649 if my_vca
.get("vdu_id") or my_vca
.get("kdu_name"):
1650 # vdu or kdu: no dependencies
1654 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
1655 vca_deployed_list
= db_nsr
["_admin"]["deployed"]["VCA"]
1656 configuration_status_list
= db_nsr
["configurationStatus"]
1657 for index
, vca_deployed
in enumerate(configuration_status_list
):
1658 if index
== vca_index
:
1661 if not my_vca
.get("member-vnf-index") or (
1662 vca_deployed
.get("member-vnf-index")
1663 == my_vca
.get("member-vnf-index")
1665 internal_status
= configuration_status_list
[index
].get("status")
1666 if internal_status
== "READY":
1668 elif internal_status
== "BROKEN":
1670 "Configuration aborted because dependent charm/s has failed"
1675 # no dependencies, return
1677 await asyncio
.sleep(10)
1680 raise LcmException("Configuration aborted because dependent charm/s timeout")
1682 def get_vca_id(self
, db_vnfr
: dict, db_nsr
: dict):
1685 vca_id
= deep_get(db_vnfr
, ("vca-id",))
1687 vim_account_id
= deep_get(db_nsr
, ("instantiate_params", "vimAccountId"))
1688 vca_id
= VimAccountDB
.get_vim_account_with_id(vim_account_id
).get("vca")
1691 async def instantiate_N2VC(
1709 ee_config_descriptor
,
1711 nsr_id
= db_nsr
["_id"]
1712 db_update_entry
= "_admin.deployed.VCA.{}.".format(vca_index
)
1713 vca_deployed_list
= db_nsr
["_admin"]["deployed"]["VCA"]
1714 vca_deployed
= db_nsr
["_admin"]["deployed"]["VCA"][vca_index
]
1715 osm_config
= {"osm": {"ns_id": db_nsr
["_id"]}}
1717 "collection": "nsrs",
1718 "filter": {"_id": nsr_id
},
1719 "path": db_update_entry
,
1724 element_under_configuration
= nsr_id
1728 vnfr_id
= db_vnfr
["_id"]
1729 osm_config
["osm"]["vnf_id"] = vnfr_id
1731 namespace
= "{nsi}.{ns}".format(nsi
=nsi_id
if nsi_id
else "", ns
=nsr_id
)
1733 if vca_type
== "native_charm":
1736 index_number
= vdu_index
or 0
1739 element_type
= "VNF"
1740 element_under_configuration
= vnfr_id
1741 namespace
+= ".{}-{}".format(vnfr_id
, index_number
)
1743 namespace
+= ".{}-{}".format(vdu_id
, index_number
)
1744 element_type
= "VDU"
1745 element_under_configuration
= "{}-{}".format(vdu_id
, index_number
)
1746 osm_config
["osm"]["vdu_id"] = vdu_id
1748 namespace
+= ".{}".format(kdu_name
)
1749 element_type
= "KDU"
1750 element_under_configuration
= kdu_name
1751 osm_config
["osm"]["kdu_name"] = kdu_name
1754 if base_folder
["pkg-dir"]:
1755 artifact_path
= "{}/{}/{}/{}".format(
1756 base_folder
["folder"],
1757 base_folder
["pkg-dir"],
1760 in ("native_charm", "lxc_proxy_charm", "k8s_proxy_charm")
1765 artifact_path
= "{}/Scripts/{}/{}/".format(
1766 base_folder
["folder"],
1769 in ("native_charm", "lxc_proxy_charm", "k8s_proxy_charm")
1774 self
.logger
.debug("Artifact path > {}".format(artifact_path
))
1776 # get initial_config_primitive_list that applies to this element
1777 initial_config_primitive_list
= config_descriptor
.get(
1778 "initial-config-primitive"
1782 "Initial config primitive list > {}".format(
1783 initial_config_primitive_list
1787 # add config if not present for NS charm
1788 ee_descriptor_id
= ee_config_descriptor
.get("id")
1789 self
.logger
.debug("EE Descriptor > {}".format(ee_descriptor_id
))
1790 initial_config_primitive_list
= get_ee_sorted_initial_config_primitive_list(
1791 initial_config_primitive_list
, vca_deployed
, ee_descriptor_id
1795 "Initial config primitive list #2 > {}".format(
1796 initial_config_primitive_list
1799 # n2vc_redesign STEP 3.1
1800 # find old ee_id if exists
1801 ee_id
= vca_deployed
.get("ee_id")
1803 vca_id
= self
.get_vca_id(db_vnfr
, db_nsr
)
1804 # create or register execution environment in VCA
1805 if vca_type
in ("lxc_proxy_charm", "k8s_proxy_charm", "helm-v3"):
1806 self
._write
_configuration
_status
(
1808 vca_index
=vca_index
,
1810 element_under_configuration
=element_under_configuration
,
1811 element_type
=element_type
,
1814 step
= "create execution environment"
1815 self
.logger
.debug(logging_text
+ step
)
1819 if vca_type
== "k8s_proxy_charm":
1820 ee_id
= await self
.vca_map
[vca_type
].install_k8s_proxy_charm(
1821 charm_name
=artifact_path
[artifact_path
.rfind("/") + 1 :],
1822 namespace
=namespace
,
1823 artifact_path
=artifact_path
,
1827 elif vca_type
== "helm-v3":
1828 ee_id
, credentials
= await self
.vca_map
[
1830 ].create_execution_environment(
1835 artifact_path
=artifact_path
,
1836 chart_model
=vca_name
,
1840 ee_id
, credentials
= await self
.vca_map
[
1842 ].create_execution_environment(
1843 namespace
=namespace
,
1849 elif vca_type
== "native_charm":
1850 step
= "Waiting to VM being up and getting IP address"
1851 self
.logger
.debug(logging_text
+ step
)
1852 rw_mgmt_ip
= await self
.wait_vm_up_insert_key_ro(
1861 credentials
= {"hostname": rw_mgmt_ip
}
1863 username
= deep_get(
1864 config_descriptor
, ("config-access", "ssh-access", "default-user")
1866 # TODO remove this when changes on IM regarding config-access:ssh-access:default-user were
1867 # merged. Meanwhile let's get username from initial-config-primitive
1868 if not username
and initial_config_primitive_list
:
1869 for config_primitive
in initial_config_primitive_list
:
1870 for param
in config_primitive
.get("parameter", ()):
1871 if param
["name"] == "ssh-username":
1872 username
= param
["value"]
1876 "Cannot determine the username neither with 'initial-config-primitive' nor with "
1877 "'config-access.ssh-access.default-user'"
1879 credentials
["username"] = username
1880 # n2vc_redesign STEP 3.2
1882 self
._write
_configuration
_status
(
1884 vca_index
=vca_index
,
1885 status
="REGISTERING",
1886 element_under_configuration
=element_under_configuration
,
1887 element_type
=element_type
,
1890 step
= "register execution environment {}".format(credentials
)
1891 self
.logger
.debug(logging_text
+ step
)
1892 ee_id
= await self
.vca_map
[vca_type
].register_execution_environment(
1893 credentials
=credentials
,
1894 namespace
=namespace
,
1899 # for compatibility with MON/POL modules, the need model and application name at database
1900 # TODO ask MON/POL if needed to not assuming anymore the format "model_name.application_name"
1901 ee_id_parts
= ee_id
.split(".")
1902 db_nsr_update
= {db_update_entry
+ "ee_id": ee_id
}
1903 if len(ee_id_parts
) >= 2:
1904 model_name
= ee_id_parts
[0]
1905 application_name
= ee_id_parts
[1]
1906 db_nsr_update
[db_update_entry
+ "model"] = model_name
1907 db_nsr_update
[db_update_entry
+ "application"] = application_name
1909 # n2vc_redesign STEP 3.3
1910 step
= "Install configuration Software"
1912 self
._write
_configuration
_status
(
1914 vca_index
=vca_index
,
1915 status
="INSTALLING SW",
1916 element_under_configuration
=element_under_configuration
,
1917 element_type
=element_type
,
1918 other_update
=db_nsr_update
,
1921 # TODO check if already done
1922 self
.logger
.debug(logging_text
+ step
)
1924 if vca_type
== "native_charm":
1925 config_primitive
= next(
1926 (p
for p
in initial_config_primitive_list
if p
["name"] == "config"),
1929 if config_primitive
:
1930 config
= self
._map
_primitive
_params
(
1931 config_primitive
, {}, deploy_params
1934 if vca_type
== "lxc_proxy_charm":
1935 if element_type
== "NS":
1936 num_units
= db_nsr
.get("config-units") or 1
1937 elif element_type
== "VNF":
1938 num_units
= db_vnfr
.get("config-units") or 1
1939 elif element_type
== "VDU":
1940 for v
in db_vnfr
["vdur"]:
1941 if vdu_id
== v
["vdu-id-ref"]:
1942 num_units
= v
.get("config-units") or 1
1944 if vca_type
!= "k8s_proxy_charm":
1945 await self
.vca_map
[vca_type
].install_configuration_sw(
1947 artifact_path
=artifact_path
,
1950 num_units
=num_units
,
1955 # write in db flag of configuration_sw already installed
1957 "nsrs", nsr_id
, {db_update_entry
+ "config_sw_installed": True}
1960 # add relations for this VCA (wait for other peers related with this VCA)
1961 is_relation_added
= await self
._add
_vca
_relations
(
1962 logging_text
=logging_text
,
1965 vca_index
=vca_index
,
1968 if not is_relation_added
:
1969 raise LcmException("Relations could not be added to VCA.")
1971 # if SSH access is required, then get execution environment SSH public
1972 # if native charm we have waited already to VM be UP
1973 if vca_type
in ("k8s_proxy_charm", "lxc_proxy_charm", "helm-v3"):
1976 # self.logger.debug("get ssh key block")
1978 config_descriptor
, ("config-access", "ssh-access", "required")
1980 # self.logger.debug("ssh key needed")
1981 # Needed to inject a ssh key
1984 ("config-access", "ssh-access", "default-user"),
1986 step
= "Install configuration Software, getting public ssh key"
1987 pub_key
= await self
.vca_map
[vca_type
].get_ee_ssh_public__key(
1988 ee_id
=ee_id
, db_dict
=db_dict
, vca_id
=vca_id
1991 step
= "Insert public key into VM user={} ssh_key={}".format(
1995 # self.logger.debug("no need to get ssh key")
1996 step
= "Waiting to VM being up and getting IP address"
1997 self
.logger
.debug(logging_text
+ step
)
1999 # default rw_mgmt_ip to None, avoiding the non definition of the variable
2002 # n2vc_redesign STEP 5.1
2003 # wait for RO (ip-address) Insert pub_key into VM
2006 rw_mgmt_ip
, services
= await self
.wait_kdu_up(
2007 logging_text
, nsr_id
, vnfr_id
, kdu_name
2009 vnfd
= self
.db
.get_one(
2011 {"_id": f
'{db_vnfr["vnfd-id"]}:{db_vnfr["revision"]}'},
2013 kdu
= get_kdu(vnfd
, kdu_name
)
2015 service
["name"] for service
in get_kdu_services(kdu
)
2017 exposed_services
= []
2018 for service
in services
:
2019 if any(s
in service
["name"] for s
in kdu_services
):
2020 exposed_services
.append(service
)
2021 await self
.vca_map
[vca_type
].exec_primitive(
2023 primitive_name
="config",
2025 "osm-config": json
.dumps(
2027 k8s
={"services": exposed_services
}
2034 # This verification is needed in order to avoid trying to add a public key
2035 # to a VM, when the VNF is a KNF (in the edge case where the user creates a VCA
2036 # for a KNF and not for its KDUs, the previous verification gives False, and the code
2037 # jumps to this block, meaning that there is the need to verify if the VNF is actually a VNF
2039 elif db_vnfr
.get("vdur"):
2040 rw_mgmt_ip
= await self
.wait_vm_up_insert_key_ro(
2050 self
.logger
.debug(logging_text
+ " VM_ip_address={}".format(rw_mgmt_ip
))
2052 # store rw_mgmt_ip in deploy params for later replacement
2053 deploy_params
["rw_mgmt_ip"] = rw_mgmt_ip
2055 # n2vc_redesign STEP 6 Execute initial config primitive
2056 step
= "execute initial config primitive"
2058 # wait for dependent primitives execution (NS -> VNF -> VDU)
2059 if initial_config_primitive_list
:
2060 await self
._wait
_dependent
_n
2vc
(nsr_id
, vca_deployed_list
, vca_index
)
2062 # stage, in function of element type: vdu, kdu, vnf or ns
2063 my_vca
= vca_deployed_list
[vca_index
]
2064 if my_vca
.get("vdu_id") or my_vca
.get("kdu_name"):
2066 stage
[0] = "Stage 3/5: running Day-1 primitives for VDU."
2067 elif my_vca
.get("member-vnf-index"):
2069 stage
[0] = "Stage 4/5: running Day-1 primitives for VNF."
2072 stage
[0] = "Stage 5/5: running Day-1 primitives for NS."
2074 self
._write
_configuration
_status
(
2075 nsr_id
=nsr_id
, vca_index
=vca_index
, status
="EXECUTING PRIMITIVE"
2078 self
._write
_op
_status
(op_id
=nslcmop_id
, stage
=stage
)
2080 check_if_terminated_needed
= True
2081 for initial_config_primitive
in initial_config_primitive_list
:
2082 # adding information on the vca_deployed if it is a NS execution environment
2083 if not vca_deployed
["member-vnf-index"]:
2084 deploy_params
["ns_config_info"] = json
.dumps(
2085 self
._get
_ns
_config
_info
(nsr_id
)
2087 # TODO check if already done
2088 primitive_params_
= self
._map
_primitive
_params
(
2089 initial_config_primitive
, {}, deploy_params
2092 step
= "execute primitive '{}' params '{}'".format(
2093 initial_config_primitive
["name"], primitive_params_
2095 self
.logger
.debug(logging_text
+ step
)
2096 await self
.vca_map
[vca_type
].exec_primitive(
2098 primitive_name
=initial_config_primitive
["name"],
2099 params_dict
=primitive_params_
,
2104 # Once some primitive has been exec, check and write at db if it needs to exec terminated primitives
2105 if check_if_terminated_needed
:
2106 if config_descriptor
.get("terminate-config-primitive"):
2108 "nsrs", nsr_id
, {db_update_entry
+ "needed_terminate": True}
2110 check_if_terminated_needed
= False
2112 # TODO register in database that primitive is done
2114 # STEP 7 Configure metrics
2115 if vca_type
== "helm-v3":
2116 # TODO: review for those cases where the helm chart is a reference and
2117 # is not part of the NF package
2118 prometheus_jobs
= await self
.extract_prometheus_scrape_jobs(
2120 artifact_path
=artifact_path
,
2121 ee_config_descriptor
=ee_config_descriptor
,
2124 target_ip
=rw_mgmt_ip
,
2125 element_type
=element_type
,
2126 vnf_member_index
=db_vnfr
.get("member-vnf-index-ref", ""),
2128 vdu_index
=vdu_index
,
2130 kdu_index
=kdu_index
,
2136 {db_update_entry
+ "prometheus_jobs": prometheus_jobs
},
2139 for job
in prometheus_jobs
:
2142 {"job_name": job
["job_name"]},
2145 fail_on_empty
=False,
2148 step
= "instantiated at VCA"
2149 self
.logger
.debug(logging_text
+ step
)
2151 self
._write
_configuration
_status
(
2152 nsr_id
=nsr_id
, vca_index
=vca_index
, status
="READY"
2155 except Exception as e
: # TODO not use Exception but N2VC exception
2156 # self.update_db_2("nsrs", nsr_id, {db_update_entry + "instantiation": "FAILED"})
2158 e
, (DbException
, N2VCException
, LcmException
, asyncio
.CancelledError
)
2161 "Exception while {} : {}".format(step
, e
), exc_info
=True
2163 self
._write
_configuration
_status
(
2164 nsr_id
=nsr_id
, vca_index
=vca_index
, status
="BROKEN"
2166 raise LcmException("{}. {}".format(step
, e
)) from e
2168 def _write_ns_status(
2172 current_operation
: str,
2173 current_operation_id
: str,
2174 error_description
: str = None,
2175 error_detail
: str = None,
2176 other_update
: dict = None,
2179 Update db_nsr fields.
2182 :param current_operation:
2183 :param current_operation_id:
2184 :param error_description:
2185 :param error_detail:
2186 :param other_update: Other required changes at database if provided, will be cleared
2190 db_dict
= other_update
or {}
2193 ] = current_operation_id
# for backward compatibility
2194 db_dict
["_admin.current-operation"] = current_operation_id
2195 db_dict
["_admin.operation-type"] = (
2196 current_operation
if current_operation
!= "IDLE" else None
2198 db_dict
["currentOperation"] = current_operation
2199 db_dict
["currentOperationID"] = current_operation_id
2200 db_dict
["errorDescription"] = error_description
2201 db_dict
["errorDetail"] = error_detail
2204 db_dict
["nsState"] = ns_state
2205 self
.update_db_2("nsrs", nsr_id
, db_dict
)
2206 except DbException
as e
:
2207 self
.logger
.warn("Error writing NS status, ns={}: {}".format(nsr_id
, e
))
2209 def _write_op_status(
2213 error_message
: str = None,
2214 queuePosition
: int = 0,
2215 operation_state
: str = None,
2216 other_update
: dict = None,
2219 db_dict
= other_update
or {}
2220 db_dict
["queuePosition"] = queuePosition
2221 if isinstance(stage
, list):
2222 db_dict
["stage"] = stage
[0]
2223 db_dict
["detailed-status"] = " ".join(stage
)
2224 elif stage
is not None:
2225 db_dict
["stage"] = str(stage
)
2227 if error_message
is not None:
2228 db_dict
["errorMessage"] = error_message
2229 if operation_state
is not None:
2230 db_dict
["operationState"] = operation_state
2231 db_dict
["statusEnteredTime"] = time()
2232 self
.update_db_2("nslcmops", op_id
, db_dict
)
2233 except DbException
as e
:
2235 "Error writing OPERATION status for op_id: {} -> {}".format(op_id
, e
)
2238 def _write_all_config_status(self
, db_nsr
: dict, status
: str):
2240 nsr_id
= db_nsr
["_id"]
2241 # configurationStatus
2242 config_status
= db_nsr
.get("configurationStatus")
2245 "configurationStatus.{}.status".format(index
): status
2246 for index
, v
in enumerate(config_status
)
2250 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
2252 except DbException
as e
:
2254 "Error writing all configuration status, ns={}: {}".format(nsr_id
, e
)
2257 def _write_configuration_status(
2262 element_under_configuration
: str = None,
2263 element_type
: str = None,
2264 other_update
: dict = None,
2266 # self.logger.debug('_write_configuration_status(): vca_index={}, status={}'
2267 # .format(vca_index, status))
2270 db_path
= "configurationStatus.{}.".format(vca_index
)
2271 db_dict
= other_update
or {}
2273 db_dict
[db_path
+ "status"] = status
2274 if element_under_configuration
:
2276 db_path
+ "elementUnderConfiguration"
2277 ] = element_under_configuration
2279 db_dict
[db_path
+ "elementType"] = element_type
2280 self
.update_db_2("nsrs", nsr_id
, db_dict
)
2281 except DbException
as e
:
2283 "Error writing configuration status={}, ns={}, vca_index={}: {}".format(
2284 status
, nsr_id
, vca_index
, e
2288 async def _do_placement(self
, logging_text
, db_nslcmop
, db_vnfrs
):
2290 Check and computes the placement, (vim account where to deploy). If it is decided by an external tool, it
2291 sends the request via kafka and wait until the result is wrote at database (nslcmops _admin.plca).
2292 Database is used because the result can be obtained from a different LCM worker in case of HA.
2293 :param logging_text: contains the prefix for logging, with the ns and nslcmop identifiers
2294 :param db_nslcmop: database content of nslcmop
2295 :param db_vnfrs: database content of vnfrs, indexed by member-vnf-index.
2296 :return: True if some modification is done. Modifies database vnfrs and parameter db_vnfr with the
2297 computed 'vim-account-id'
2300 nslcmop_id
= db_nslcmop
["_id"]
2301 placement_engine
= deep_get(db_nslcmop
, ("operationParams", "placement-engine"))
2302 if placement_engine
== "PLA":
2304 logging_text
+ "Invoke and wait for placement optimization"
2306 await self
.msg
.aiowrite("pla", "get_placement", {"nslcmopId": nslcmop_id
})
2307 db_poll_interval
= 5
2308 wait
= db_poll_interval
* 10
2310 while not pla_result
and wait
>= 0:
2311 await asyncio
.sleep(db_poll_interval
)
2312 wait
-= db_poll_interval
2313 db_nslcmop
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
})
2314 pla_result
= deep_get(db_nslcmop
, ("_admin", "pla"))
2318 "Placement timeout for nslcmopId={}".format(nslcmop_id
)
2321 for pla_vnf
in pla_result
["vnf"]:
2322 vnfr
= db_vnfrs
.get(pla_vnf
["member-vnf-index"])
2323 if not pla_vnf
.get("vimAccountId") or not vnfr
:
2328 {"_id": vnfr
["_id"]},
2329 {"vim-account-id": pla_vnf
["vimAccountId"]},
2332 vnfr
["vim-account-id"] = pla_vnf
["vimAccountId"]
2335 def _gather_vnfr_healing_alerts(self
, vnfr
, vnfd
):
2337 nsr_id
= vnfr
["nsr-id-ref"]
2338 df
= vnfd
.get("df", [{}])[0]
2339 # Checking for auto-healing configuration
2340 if "healing-aspect" in df
:
2341 healing_aspects
= df
["healing-aspect"]
2342 for healing
in healing_aspects
:
2343 for healing_policy
in healing
.get("healing-policy", ()):
2344 vdu_id
= healing_policy
["vdu-id"]
2346 (vdur
for vdur
in vnfr
["vdur"] if vdu_id
== vdur
["vdu-id-ref"]),
2351 metric_name
= "vm_status"
2352 vdu_name
= vdur
.get("name")
2353 vnf_member_index
= vnfr
["member-vnf-index-ref"]
2355 name
= f
"healing_{uuid}"
2356 action
= healing_policy
2357 # action_on_recovery = healing.get("action-on-recovery")
2358 # cooldown_time = healing.get("cooldown-time")
2359 # day1 = healing.get("day1")
2363 "metric": metric_name
,
2366 "vnf_member_index": vnf_member_index
,
2367 "vdu_name": vdu_name
,
2369 "alarm_status": "ok",
2370 "action_type": "healing",
2373 alerts
.append(alert
)
2376 def _gather_vnfr_scaling_alerts(self
, vnfr
, vnfd
):
2378 nsr_id
= vnfr
["nsr-id-ref"]
2379 df
= vnfd
.get("df", [{}])[0]
2380 # Checking for auto-scaling configuration
2381 if "scaling-aspect" in df
:
2382 scaling_aspects
= df
["scaling-aspect"]
2383 all_vnfd_monitoring_params
= {}
2384 for ivld
in vnfd
.get("int-virtual-link-desc", ()):
2385 for mp
in ivld
.get("monitoring-parameters", ()):
2386 all_vnfd_monitoring_params
[mp
.get("id")] = mp
2387 for vdu
in vnfd
.get("vdu", ()):
2388 for mp
in vdu
.get("monitoring-parameter", ()):
2389 all_vnfd_monitoring_params
[mp
.get("id")] = mp
2390 for df
in vnfd
.get("df", ()):
2391 for mp
in df
.get("monitoring-parameter", ()):
2392 all_vnfd_monitoring_params
[mp
.get("id")] = mp
2393 for scaling_aspect
in scaling_aspects
:
2394 scaling_group_name
= scaling_aspect
.get("name", "")
2395 # Get monitored VDUs
2396 all_monitored_vdus
= set()
2397 for delta
in scaling_aspect
.get("aspect-delta-details", {}).get(
2400 for vdu_delta
in delta
.get("vdu-delta", ()):
2401 all_monitored_vdus
.add(vdu_delta
.get("id"))
2402 monitored_vdurs
= list(
2404 lambda vdur
: vdur
["vdu-id-ref"] in all_monitored_vdus
,
2408 if not monitored_vdurs
:
2410 "Scaling criteria is referring to a vnf-monitoring-param that does not contain a reference to a vdu or vnf metric"
2413 for scaling_policy
in scaling_aspect
.get("scaling-policy", ()):
2414 if scaling_policy
["scaling-type"] != "automatic":
2416 threshold_time
= scaling_policy
.get("threshold-time", "1")
2417 cooldown_time
= scaling_policy
.get("cooldown-time", "0")
2418 for scaling_criteria
in scaling_policy
["scaling-criteria"]:
2419 monitoring_param_ref
= scaling_criteria
.get(
2420 "vnf-monitoring-param-ref"
2422 vnf_monitoring_param
= all_vnfd_monitoring_params
[
2423 monitoring_param_ref
2425 for vdur
in monitored_vdurs
:
2426 vdu_id
= vdur
["vdu-id-ref"]
2427 metric_name
= vnf_monitoring_param
.get("performance-metric")
2428 metric_name
= f
"osm_{metric_name}"
2429 vnf_member_index
= vnfr
["member-vnf-index-ref"]
2430 scalein_threshold
= scaling_criteria
.get(
2431 "scale-in-threshold"
2433 scaleout_threshold
= scaling_criteria
.get(
2434 "scale-out-threshold"
2436 # Looking for min/max-number-of-instances
2437 instances_min_number
= 1
2438 instances_max_number
= 1
2439 vdu_profile
= df
["vdu-profile"]
2442 item
for item
in vdu_profile
if item
["id"] == vdu_id
2444 instances_min_number
= profile
.get(
2445 "min-number-of-instances", 1
2447 instances_max_number
= profile
.get(
2448 "max-number-of-instances", 1
2451 if scalein_threshold
:
2453 name
= f
"scalein_{uuid}"
2454 operation
= scaling_criteria
[
2455 "scale-in-relational-operation"
2457 rel_operator
= self
.rel_operation_types
.get(
2460 metric_selector
= f
'{metric_name}{{ns_id="{nsr_id}", vnf_member_index="{vnf_member_index}", vdu_id="{vdu_id}"}}'
2461 expression
= f
"(count ({metric_selector}) > {instances_min_number}) and (avg({metric_selector}) {rel_operator} {scalein_threshold})"
2464 "vnf_member_index": vnf_member_index
,
2470 "for": str(threshold_time
) + "m",
2473 action
= scaling_policy
2475 "scaling-group": scaling_group_name
,
2476 "cooldown-time": cooldown_time
,
2481 "metric": metric_name
,
2484 "vnf_member_index": vnf_member_index
,
2487 "alarm_status": "ok",
2488 "action_type": "scale_in",
2490 "prometheus_config": prom_cfg
,
2492 alerts
.append(alert
)
2494 if scaleout_threshold
:
2496 name
= f
"scaleout_{uuid}"
2497 operation
= scaling_criteria
[
2498 "scale-out-relational-operation"
2500 rel_operator
= self
.rel_operation_types
.get(
2503 metric_selector
= f
'{metric_name}{{ns_id="{nsr_id}", vnf_member_index="{vnf_member_index}", vdu_id="{vdu_id}"}}'
2504 expression
= f
"(count ({metric_selector}) < {instances_max_number}) and (avg({metric_selector}) {rel_operator} {scaleout_threshold})"
2507 "vnf_member_index": vnf_member_index
,
2513 "for": str(threshold_time
) + "m",
2516 action
= scaling_policy
2518 "scaling-group": scaling_group_name
,
2519 "cooldown-time": cooldown_time
,
2524 "metric": metric_name
,
2527 "vnf_member_index": vnf_member_index
,
2530 "alarm_status": "ok",
2531 "action_type": "scale_out",
2533 "prometheus_config": prom_cfg
,
2535 alerts
.append(alert
)
2538 def _gather_vnfr_alarm_alerts(self
, vnfr
, vnfd
):
2540 nsr_id
= vnfr
["nsr-id-ref"]
2541 vnf_member_index
= vnfr
["member-vnf-index-ref"]
2543 # Checking for VNF alarm configuration
2544 for vdur
in vnfr
["vdur"]:
2545 vdu_id
= vdur
["vdu-id-ref"]
2546 vdu
= next(filter(lambda vdu
: vdu
["id"] == vdu_id
, vnfd
["vdu"]))
2548 # Get VDU monitoring params, since alerts are based on them
2549 vdu_monitoring_params
= {}
2550 for mp
in vdu
.get("monitoring-parameter", []):
2551 vdu_monitoring_params
[mp
.get("id")] = mp
2552 if not vdu_monitoring_params
:
2554 "VDU alarm refers to a VDU monitoring param, but there are no VDU monitoring params in the VDU"
2557 # Get alarms in the VDU
2558 alarm_descriptors
= vdu
["alarm"]
2559 # Create VDU alarms for each alarm in the VDU
2560 for alarm_descriptor
in alarm_descriptors
:
2561 # Check that the VDU alarm refers to a proper monitoring param
2562 alarm_monitoring_param
= alarm_descriptor
.get(
2563 "vnf-monitoring-param-ref", ""
2565 vdu_specific_monitoring_param
= vdu_monitoring_params
.get(
2566 alarm_monitoring_param
, {}
2568 if not vdu_specific_monitoring_param
:
2570 "VDU alarm refers to a VDU monitoring param not present in the VDU"
2573 metric_name
= vdu_specific_monitoring_param
.get(
2574 "performance-metric"
2578 "VDU alarm refers to a VDU monitoring param that has no associated performance-metric"
2581 # Set params of the alarm to be created in Prometheus
2582 metric_name
= f
"osm_{metric_name}"
2583 metric_threshold
= alarm_descriptor
.get("value")
2585 alert_name
= f
"vdu_alarm_{uuid}"
2586 operation
= alarm_descriptor
["operation"]
2587 rel_operator
= self
.rel_operation_types
.get(operation
, "<=")
2588 metric_selector
= f
'{metric_name}{{ns_id="{nsr_id}", vnf_member_index="{vnf_member_index}", vdu_id="{vdu_id}"}}'
2589 expression
= f
"{metric_selector} {rel_operator} {metric_threshold}"
2592 "vnf_member_index": vnf_member_index
,
2594 "vdu_name": "{{ $labels.vdu_name }}",
2597 "alert": alert_name
,
2599 "for": "1m", # default value. Ideally, this should be related to an IM param, but there is not such param
2602 alarm_action
= dict()
2603 for action_type
in ["ok", "insufficient-data", "alarm"]:
2605 "actions" in alarm_descriptor
2606 and action_type
in alarm_descriptor
["actions"]
2608 alarm_action
[action_type
] = alarm_descriptor
["actions"][
2614 "metric": metric_name
,
2617 "vnf_member_index": vnf_member_index
,
2620 "alarm_status": "ok",
2621 "action_type": "vdu_alarm",
2622 "action": alarm_action
,
2623 "prometheus_config": prom_cfg
,
2625 alerts
.append(alert
)
2628 def update_nsrs_with_pla_result(self
, params
):
2630 nslcmop_id
= deep_get(params
, ("placement", "nslcmopId"))
2632 "nslcmops", nslcmop_id
, {"_admin.pla": params
.get("placement")}
2634 except Exception as e
:
2635 self
.logger
.warn("Update failed for nslcmop_id={}:{}".format(nslcmop_id
, e
))
2637 async def instantiate(self
, nsr_id
, nslcmop_id
):
2640 :param nsr_id: ns instance to deploy
2641 :param nslcmop_id: operation to run
2645 # Try to lock HA task here
2646 task_is_locked_by_me
= self
.lcm_tasks
.lock_HA("ns", "nslcmops", nslcmop_id
)
2647 if not task_is_locked_by_me
:
2649 "instantiate() task is not locked by me, ns={}".format(nsr_id
)
2653 logging_text
= "Task ns={} instantiate={} ".format(nsr_id
, nslcmop_id
)
2654 self
.logger
.debug(logging_text
+ "Enter")
2656 # get all needed from database
2658 # database nsrs record
2661 # database nslcmops record
2664 # update operation on nsrs
2666 # update operation on nslcmops
2667 db_nslcmop_update
= {}
2669 timeout_ns_deploy
= self
.timeout
.ns_deploy
2671 nslcmop_operation_state
= None
2672 db_vnfrs
= {} # vnf's info indexed by member-index
2674 tasks_dict_info
= {} # from task to info text
2678 "Stage 1/5: preparation of the environment.",
2679 "Waiting for previous operations to terminate.",
2682 # ^ stage, step, VIM progress
2684 # wait for any previous tasks in process
2685 await self
.lcm_tasks
.waitfor_related_HA("ns", "nslcmops", nslcmop_id
)
2687 # STEP 0: Reading database (nslcmops, nsrs, nsds, vnfrs, vnfds)
2688 stage
[1] = "Reading from database."
2689 # nsState="BUILDING", currentOperation="INSTANTIATING", currentOperationID=nslcmop_id
2690 db_nsr_update
["detailed-status"] = "creating"
2691 db_nsr_update
["operational-status"] = "init"
2692 self
._write
_ns
_status
(
2694 ns_state
="BUILDING",
2695 current_operation
="INSTANTIATING",
2696 current_operation_id
=nslcmop_id
,
2697 other_update
=db_nsr_update
,
2699 self
._write
_op
_status
(op_id
=nslcmop_id
, stage
=stage
, queuePosition
=0)
2701 # read from db: operation
2702 stage
[1] = "Getting nslcmop={} from db.".format(nslcmop_id
)
2703 db_nslcmop
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
})
2704 if db_nslcmop
["operationParams"].get("additionalParamsForVnf"):
2705 db_nslcmop
["operationParams"]["additionalParamsForVnf"] = json
.loads(
2706 db_nslcmop
["operationParams"]["additionalParamsForVnf"]
2708 ns_params
= db_nslcmop
.get("operationParams")
2709 if ns_params
and ns_params
.get("timeout_ns_deploy"):
2710 timeout_ns_deploy
= ns_params
["timeout_ns_deploy"]
2713 stage
[1] = "Getting nsr={} from db.".format(nsr_id
)
2714 self
.logger
.debug(logging_text
+ stage
[1])
2715 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
2716 stage
[1] = "Getting nsd={} from db.".format(db_nsr
["nsd-id"])
2717 self
.logger
.debug(logging_text
+ stage
[1])
2718 nsd
= self
.db
.get_one("nsds", {"_id": db_nsr
["nsd-id"]})
2719 self
.fs
.sync(db_nsr
["nsd-id"])
2721 # nsr_name = db_nsr["name"] # TODO short-name??
2723 # read from db: vnf's of this ns
2724 stage
[1] = "Getting vnfrs from db."
2725 self
.logger
.debug(logging_text
+ stage
[1])
2726 db_vnfrs_list
= self
.db
.get_list("vnfrs", {"nsr-id-ref": nsr_id
})
2728 # read from db: vnfd's for every vnf
2729 db_vnfds
= [] # every vnfd data
2731 # for each vnf in ns, read vnfd
2732 for vnfr
in db_vnfrs_list
:
2733 if vnfr
.get("kdur"):
2735 for kdur
in vnfr
["kdur"]:
2736 if kdur
.get("additionalParams"):
2737 kdur
["additionalParams"] = json
.loads(
2738 kdur
["additionalParams"]
2740 kdur_list
.append(kdur
)
2741 vnfr
["kdur"] = kdur_list
2743 db_vnfrs
[vnfr
["member-vnf-index-ref"]] = vnfr
2744 vnfd_id
= vnfr
["vnfd-id"]
2745 vnfd_ref
= vnfr
["vnfd-ref"]
2746 self
.fs
.sync(vnfd_id
)
2748 # if we haven't this vnfd, read it from db
2749 if vnfd_id
not in db_vnfds
:
2751 stage
[1] = "Getting vnfd={} id='{}' from db.".format(
2754 self
.logger
.debug(logging_text
+ stage
[1])
2755 vnfd
= self
.db
.get_one("vnfds", {"_id": vnfd_id
})
2758 db_vnfds
.append(vnfd
)
2760 # Get or generates the _admin.deployed.VCA list
2761 vca_deployed_list
= None
2762 if db_nsr
["_admin"].get("deployed"):
2763 vca_deployed_list
= db_nsr
["_admin"]["deployed"].get("VCA")
2764 if vca_deployed_list
is None:
2765 vca_deployed_list
= []
2766 configuration_status_list
= []
2767 db_nsr_update
["_admin.deployed.VCA"] = vca_deployed_list
2768 db_nsr_update
["configurationStatus"] = configuration_status_list
2769 # add _admin.deployed.VCA to db_nsr dictionary, value=vca_deployed_list
2770 populate_dict(db_nsr
, ("_admin", "deployed", "VCA"), vca_deployed_list
)
2771 elif isinstance(vca_deployed_list
, dict):
2772 # maintain backward compatibility. Change a dict to list at database
2773 vca_deployed_list
= list(vca_deployed_list
.values())
2774 db_nsr_update
["_admin.deployed.VCA"] = vca_deployed_list
2775 populate_dict(db_nsr
, ("_admin", "deployed", "VCA"), vca_deployed_list
)
2778 deep_get(db_nsr
, ("_admin", "deployed", "RO", "vnfd")), list
2780 populate_dict(db_nsr
, ("_admin", "deployed", "RO", "vnfd"), [])
2781 db_nsr_update
["_admin.deployed.RO.vnfd"] = []
2783 # set state to INSTANTIATED. When instantiated NBI will not delete directly
2784 db_nsr_update
["_admin.nsState"] = "INSTANTIATED"
2785 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
2787 "vnfrs", {"nsr-id-ref": nsr_id
}, {"_admin.nsState": "INSTANTIATED"}
2790 # n2vc_redesign STEP 2 Deploy Network Scenario
2791 stage
[0] = "Stage 2/5: deployment of KDUs, VMs and execution environments."
2792 self
._write
_op
_status
(op_id
=nslcmop_id
, stage
=stage
)
2794 stage
[1] = "Deploying KDUs."
2795 # self.logger.debug(logging_text + "Before deploy_kdus")
2796 # Call to deploy_kdus in case exists the "vdu:kdu" param
2797 await self
.deploy_kdus(
2798 logging_text
=logging_text
,
2800 nslcmop_id
=nslcmop_id
,
2803 task_instantiation_info
=tasks_dict_info
,
2806 stage
[1] = "Getting VCA public key."
2807 # n2vc_redesign STEP 1 Get VCA public ssh-key
2808 # feature 1429. Add n2vc public key to needed VMs
2809 n2vc_key
= self
.n2vc
.get_public_key()
2810 n2vc_key_list
= [n2vc_key
]
2811 if self
.vca_config
.public_key
:
2812 n2vc_key_list
.append(self
.vca_config
.public_key
)
2814 stage
[1] = "Deploying NS at VIM."
2815 task_ro
= asyncio
.ensure_future(
2816 self
.instantiate_RO(
2817 logging_text
=logging_text
,
2821 db_nslcmop
=db_nslcmop
,
2824 n2vc_key_list
=n2vc_key_list
,
2828 self
.lcm_tasks
.register("ns", nsr_id
, nslcmop_id
, "instantiate_RO", task_ro
)
2829 tasks_dict_info
[task_ro
] = "Deploying at VIM"
2831 # n2vc_redesign STEP 3 to 6 Deploy N2VC
2832 stage
[1] = "Deploying Execution Environments."
2833 self
.logger
.debug(logging_text
+ stage
[1])
2835 # create namespace and certificate if any helm based EE is present in the NS
2836 if check_helm_ee_in_ns(db_vnfds
):
2837 await self
.vca_map
["helm-v3"].setup_ns_namespace(
2840 # create TLS certificates
2841 await self
.vca_map
["helm-v3"].create_tls_certificate(
2842 secret_name
=self
.EE_TLS_NAME
,
2845 usage
="server auth",
2849 nsi_id
= None # TODO put nsi_id when this nsr belongs to a NSI
2850 for vnf_profile
in get_vnf_profiles(nsd
):
2851 vnfd_id
= vnf_profile
["vnfd-id"]
2852 vnfd
= find_in_list(db_vnfds
, lambda a_vnf
: a_vnf
["id"] == vnfd_id
)
2853 member_vnf_index
= str(vnf_profile
["id"])
2854 db_vnfr
= db_vnfrs
[member_vnf_index
]
2855 base_folder
= vnfd
["_admin"]["storage"]
2862 # Get additional parameters
2863 deploy_params
= {"OSM": get_osm_params(db_vnfr
)}
2864 if db_vnfr
.get("additionalParamsForVnf"):
2865 deploy_params
.update(
2866 parse_yaml_strings(db_vnfr
["additionalParamsForVnf"].copy())
2869 descriptor_config
= get_configuration(vnfd
, vnfd
["id"])
2870 if descriptor_config
:
2872 logging_text
=logging_text
2873 + "member_vnf_index={} ".format(member_vnf_index
),
2876 nslcmop_id
=nslcmop_id
,
2882 member_vnf_index
=member_vnf_index
,
2883 vdu_index
=vdu_index
,
2884 kdu_index
=kdu_index
,
2886 deploy_params
=deploy_params
,
2887 descriptor_config
=descriptor_config
,
2888 base_folder
=base_folder
,
2889 task_instantiation_info
=tasks_dict_info
,
2893 # Deploy charms for each VDU that supports one.
2894 for vdud
in get_vdu_list(vnfd
):
2896 descriptor_config
= get_configuration(vnfd
, vdu_id
)
2897 vdur
= find_in_list(
2898 db_vnfr
["vdur"], lambda vdu
: vdu
["vdu-id-ref"] == vdu_id
2901 if vdur
.get("additionalParams"):
2902 deploy_params_vdu
= parse_yaml_strings(vdur
["additionalParams"])
2904 deploy_params_vdu
= deploy_params
2905 deploy_params_vdu
["OSM"] = get_osm_params(
2906 db_vnfr
, vdu_id
, vdu_count_index
=0
2908 vdud_count
= get_number_of_instances(vnfd
, vdu_id
)
2910 self
.logger
.debug("VDUD > {}".format(vdud
))
2912 "Descriptor config > {}".format(descriptor_config
)
2914 if descriptor_config
:
2918 for vdu_index
in range(vdud_count
):
2919 # TODO vnfr_params["rw_mgmt_ip"] = vdur["ip-address"]
2921 logging_text
=logging_text
2922 + "member_vnf_index={}, vdu_id={}, vdu_index={} ".format(
2923 member_vnf_index
, vdu_id
, vdu_index
2927 nslcmop_id
=nslcmop_id
,
2933 kdu_index
=kdu_index
,
2934 member_vnf_index
=member_vnf_index
,
2935 vdu_index
=vdu_index
,
2937 deploy_params
=deploy_params_vdu
,
2938 descriptor_config
=descriptor_config
,
2939 base_folder
=base_folder
,
2940 task_instantiation_info
=tasks_dict_info
,
2943 for kdud
in get_kdu_list(vnfd
):
2944 kdu_name
= kdud
["name"]
2945 descriptor_config
= get_configuration(vnfd
, kdu_name
)
2946 if descriptor_config
:
2950 kdu_index
, kdur
= next(
2952 for x
in enumerate(db_vnfr
["kdur"])
2953 if x
[1]["kdu-name"] == kdu_name
2955 deploy_params_kdu
= {"OSM": get_osm_params(db_vnfr
)}
2956 if kdur
.get("additionalParams"):
2957 deploy_params_kdu
.update(
2958 parse_yaml_strings(kdur
["additionalParams"].copy())
2962 logging_text
=logging_text
,
2965 nslcmop_id
=nslcmop_id
,
2971 member_vnf_index
=member_vnf_index
,
2972 vdu_index
=vdu_index
,
2973 kdu_index
=kdu_index
,
2975 deploy_params
=deploy_params_kdu
,
2976 descriptor_config
=descriptor_config
,
2977 base_folder
=base_folder
,
2978 task_instantiation_info
=tasks_dict_info
,
2982 # Check if each vnf has exporter for metric collection if so update prometheus job records
2983 if "exporters-endpoints" in vnfd
.get("df")[0]:
2984 exporter_config
= vnfd
.get("df")[0].get("exporters-endpoints")
2985 self
.logger
.debug("exporter config :{}".format(exporter_config
))
2986 artifact_path
= "{}/{}/{}".format(
2987 base_folder
["folder"],
2988 base_folder
["pkg-dir"],
2989 "exporter-endpoint",
2992 ee_config_descriptor
= exporter_config
2993 vnfr_id
= db_vnfr
["id"]
2994 rw_mgmt_ip
= await self
.wait_vm_up_insert_key_ro(
3003 self
.logger
.debug("rw_mgmt_ip:{}".format(rw_mgmt_ip
))
3004 self
.logger
.debug("Artifact_path:{}".format(artifact_path
))
3005 db_vnfr
= self
.db
.get_one("vnfrs", {"_id": vnfr_id
})
3006 vdu_id_for_prom
= None
3007 vdu_index_for_prom
= None
3008 for x
in get_iterable(db_vnfr
, "vdur"):
3009 vdu_id_for_prom
= x
.get("vdu-id-ref")
3010 vdu_index_for_prom
= x
.get("count-index")
3011 prometheus_jobs
= await self
.extract_prometheus_scrape_jobs(
3013 artifact_path
=artifact_path
,
3014 ee_config_descriptor
=ee_config_descriptor
,
3017 target_ip
=rw_mgmt_ip
,
3019 vdu_id
=vdu_id_for_prom
,
3020 vdu_index
=vdu_index_for_prom
,
3023 self
.logger
.debug("Prometheus job:{}".format(prometheus_jobs
))
3025 db_nsr_update
["_admin.deployed.prometheus_jobs"] = prometheus_jobs
3032 for job
in prometheus_jobs
:
3035 {"job_name": job
["job_name"]},
3038 fail_on_empty
=False,
3041 # Check if this NS has a charm configuration
3042 descriptor_config
= nsd
.get("ns-configuration")
3043 if descriptor_config
and descriptor_config
.get("juju"):
3046 member_vnf_index
= None
3053 # Get additional parameters
3054 deploy_params
= {"OSM": {"vim_account_id": ns_params
["vimAccountId"]}}
3055 if db_nsr
.get("additionalParamsForNs"):
3056 deploy_params
.update(
3057 parse_yaml_strings(db_nsr
["additionalParamsForNs"].copy())
3059 base_folder
= nsd
["_admin"]["storage"]
3061 logging_text
=logging_text
,
3064 nslcmop_id
=nslcmop_id
,
3070 member_vnf_index
=member_vnf_index
,
3071 vdu_index
=vdu_index
,
3072 kdu_index
=kdu_index
,
3074 deploy_params
=deploy_params
,
3075 descriptor_config
=descriptor_config
,
3076 base_folder
=base_folder
,
3077 task_instantiation_info
=tasks_dict_info
,
3081 # rest of staff will be done at finally
3084 ROclient
.ROClientException
,
3090 logging_text
+ "Exit Exception while '{}': {}".format(stage
[1], e
)
3093 except asyncio
.CancelledError
:
3095 logging_text
+ "Cancelled Exception while '{}'".format(stage
[1])
3097 exc
= "Operation was cancelled"
3098 except Exception as e
:
3099 exc
= traceback
.format_exc()
3100 self
.logger
.critical(
3101 logging_text
+ "Exit Exception while '{}': {}".format(stage
[1], e
),
3106 error_list
.append(str(exc
))
3108 # wait for pending tasks
3110 stage
[1] = "Waiting for instantiate pending tasks."
3111 self
.logger
.debug(logging_text
+ stage
[1])
3112 error_list
+= await self
._wait
_for
_tasks
(
3120 stage
[1] = stage
[2] = ""
3121 except asyncio
.CancelledError
:
3122 error_list
.append("Cancelled")
3123 await self
._cancel
_pending
_tasks
(logging_text
, tasks_dict_info
)
3124 await self
._wait
_for
_tasks
(
3132 except Exception as exc
:
3133 error_list
.append(str(exc
))
3135 # update operation-status
3136 db_nsr_update
["operational-status"] = "running"
3137 # let's begin with VCA 'configured' status (later we can change it)
3138 db_nsr_update
["config-status"] = "configured"
3139 for task
, task_name
in tasks_dict_info
.items():
3140 if not task
.done() or task
.cancelled() or task
.exception():
3141 if task_name
.startswith(self
.task_name_deploy_vca
):
3142 # A N2VC task is pending
3143 db_nsr_update
["config-status"] = "failed"
3145 # RO or KDU task is pending
3146 db_nsr_update
["operational-status"] = "failed"
3148 # update status at database
3150 error_detail
= ". ".join(error_list
)
3151 self
.logger
.error(logging_text
+ error_detail
)
3152 error_description_nslcmop
= "{} Detail: {}".format(
3153 stage
[0], error_detail
3155 error_description_nsr
= "Operation: INSTANTIATING.{}, {}".format(
3156 nslcmop_id
, stage
[0]
3159 db_nsr_update
["detailed-status"] = (
3160 error_description_nsr
+ " Detail: " + error_detail
3162 db_nslcmop_update
["detailed-status"] = error_detail
3163 nslcmop_operation_state
= "FAILED"
3167 error_description_nsr
= error_description_nslcmop
= None
3169 db_nsr_update
["detailed-status"] = "Done"
3170 db_nslcmop_update
["detailed-status"] = "Done"
3171 nslcmop_operation_state
= "COMPLETED"
3172 # Gather auto-healing and auto-scaling alerts for each vnfr
3175 for vnfr
in self
.db
.get_list("vnfrs", {"nsr-id-ref": nsr_id
}):
3177 (sub
for sub
in db_vnfds
if sub
["_id"] == vnfr
["vnfd-id"]), None
3179 healing_alerts
= self
._gather
_vnfr
_healing
_alerts
(vnfr
, vnfd
)
3180 for alert
in healing_alerts
:
3181 self
.logger
.info(f
"Storing healing alert in MongoDB: {alert}")
3182 self
.db
.create("alerts", alert
)
3184 scaling_alerts
= self
._gather
_vnfr
_scaling
_alerts
(vnfr
, vnfd
)
3185 for alert
in scaling_alerts
:
3186 self
.logger
.info(f
"Storing scaling alert in MongoDB: {alert}")
3187 self
.db
.create("alerts", alert
)
3189 alarm_alerts
= self
._gather
_vnfr
_alarm
_alerts
(vnfr
, vnfd
)
3190 for alert
in alarm_alerts
:
3191 self
.logger
.info(f
"Storing VNF alarm alert in MongoDB: {alert}")
3192 self
.db
.create("alerts", alert
)
3194 self
._write
_ns
_status
(
3197 current_operation
="IDLE",
3198 current_operation_id
=None,
3199 error_description
=error_description_nsr
,
3200 error_detail
=error_detail
,
3201 other_update
=db_nsr_update
,
3203 self
._write
_op
_status
(
3206 error_message
=error_description_nslcmop
,
3207 operation_state
=nslcmop_operation_state
,
3208 other_update
=db_nslcmop_update
,
3211 if nslcmop_operation_state
:
3213 await self
.msg
.aiowrite(
3218 "nslcmop_id": nslcmop_id
,
3219 "operationState": nslcmop_operation_state
,
3220 "startTime": db_nslcmop
["startTime"],
3221 "links": db_nslcmop
["links"],
3222 "operationParams": {
3223 "nsInstanceId": nsr_id
,
3224 "nsdId": db_nsr
["nsd-id"],
3228 except Exception as e
:
3230 logging_text
+ "kafka_write notification Exception {}".format(e
)
3233 self
.logger
.debug(logging_text
+ "Exit")
3234 self
.lcm_tasks
.remove("ns", nsr_id
, nslcmop_id
, "ns_instantiate")
3236 def _get_vnfd(self
, vnfd_id
: str, projects_read
: str, cached_vnfds
: Dict
[str, Any
]):
3237 if vnfd_id
not in cached_vnfds
:
3238 cached_vnfds
[vnfd_id
] = self
.db
.get_one(
3239 "vnfds", {"id": vnfd_id
, "_admin.projects_read": projects_read
}
3241 return cached_vnfds
[vnfd_id
]
3243 def _get_vnfr(self
, nsr_id
: str, vnf_profile_id
: str, cached_vnfrs
: Dict
[str, Any
]):
3244 if vnf_profile_id
not in cached_vnfrs
:
3245 cached_vnfrs
[vnf_profile_id
] = self
.db
.get_one(
3248 "member-vnf-index-ref": vnf_profile_id
,
3249 "nsr-id-ref": nsr_id
,
3252 return cached_vnfrs
[vnf_profile_id
]
3254 def _is_deployed_vca_in_relation(
3255 self
, vca
: DeployedVCA
, relation
: Relation
3258 for endpoint
in (relation
.provider
, relation
.requirer
):
3259 if endpoint
["kdu-resource-profile-id"]:
3262 vca
.vnf_profile_id
== endpoint
.vnf_profile_id
3263 and vca
.vdu_profile_id
== endpoint
.vdu_profile_id
3264 and vca
.execution_environment_ref
== endpoint
.execution_environment_ref
3270 def _update_ee_relation_data_with_implicit_data(
3271 self
, nsr_id
, nsd
, ee_relation_data
, cached_vnfds
, vnf_profile_id
: str = None
3273 ee_relation_data
= safe_get_ee_relation(
3274 nsr_id
, ee_relation_data
, vnf_profile_id
=vnf_profile_id
3276 ee_relation_level
= EELevel
.get_level(ee_relation_data
)
3277 if (ee_relation_level
in (EELevel
.VNF
, EELevel
.VDU
)) and not ee_relation_data
[
3278 "execution-environment-ref"
3280 vnf_profile
= get_vnf_profile(nsd
, ee_relation_data
["vnf-profile-id"])
3281 vnfd_id
= vnf_profile
["vnfd-id"]
3282 project
= nsd
["_admin"]["projects_read"][0]
3283 db_vnfd
= self
._get
_vnfd
(vnfd_id
, project
, cached_vnfds
)
3286 if ee_relation_level
== EELevel
.VNF
3287 else ee_relation_data
["vdu-profile-id"]
3289 ee
= get_juju_ee_ref(db_vnfd
, entity_id
)
3292 f
"not execution environments found for ee_relation {ee_relation_data}"
3294 ee_relation_data
["execution-environment-ref"] = ee
["id"]
3295 return ee_relation_data
3297 def _get_ns_relations(
3300 nsd
: Dict
[str, Any
],
3302 cached_vnfds
: Dict
[str, Any
],
3303 ) -> List
[Relation
]:
3305 db_ns_relations
= get_ns_configuration_relation_list(nsd
)
3306 for r
in db_ns_relations
:
3307 provider_dict
= None
3308 requirer_dict
= None
3309 if all(key
in r
for key
in ("provider", "requirer")):
3310 provider_dict
= r
["provider"]
3311 requirer_dict
= r
["requirer"]
3312 elif "entities" in r
:
3313 provider_id
= r
["entities"][0]["id"]
3316 "endpoint": r
["entities"][0]["endpoint"],
3318 if provider_id
!= nsd
["id"]:
3319 provider_dict
["vnf-profile-id"] = provider_id
3320 requirer_id
= r
["entities"][1]["id"]
3323 "endpoint": r
["entities"][1]["endpoint"],
3325 if requirer_id
!= nsd
["id"]:
3326 requirer_dict
["vnf-profile-id"] = requirer_id
3329 "provider/requirer or entities must be included in the relation."
3331 relation_provider
= self
._update
_ee
_relation
_data
_with
_implicit
_data
(
3332 nsr_id
, nsd
, provider_dict
, cached_vnfds
3334 relation_requirer
= self
._update
_ee
_relation
_data
_with
_implicit
_data
(
3335 nsr_id
, nsd
, requirer_dict
, cached_vnfds
3337 provider
= EERelation(relation_provider
)
3338 requirer
= EERelation(relation_requirer
)
3339 relation
= Relation(r
["name"], provider
, requirer
)
3340 vca_in_relation
= self
._is
_deployed
_vca
_in
_relation
(vca
, relation
)
3342 relations
.append(relation
)
3345 def _get_vnf_relations(
3348 nsd
: Dict
[str, Any
],
3350 cached_vnfds
: Dict
[str, Any
],
3351 ) -> List
[Relation
]:
3353 if vca
.target_element
== "ns":
3354 self
.logger
.debug("VCA is a NS charm, not a VNF.")
3356 vnf_profile
= get_vnf_profile(nsd
, vca
.vnf_profile_id
)
3357 vnf_profile_id
= vnf_profile
["id"]
3358 vnfd_id
= vnf_profile
["vnfd-id"]
3359 project
= nsd
["_admin"]["projects_read"][0]
3360 db_vnfd
= self
._get
_vnfd
(vnfd_id
, project
, cached_vnfds
)
3361 db_vnf_relations
= get_relation_list(db_vnfd
, vnfd_id
)
3362 for r
in db_vnf_relations
:
3363 provider_dict
= None
3364 requirer_dict
= None
3365 if all(key
in r
for key
in ("provider", "requirer")):
3366 provider_dict
= r
["provider"]
3367 requirer_dict
= r
["requirer"]
3368 elif "entities" in r
:
3369 provider_id
= r
["entities"][0]["id"]
3372 "vnf-profile-id": vnf_profile_id
,
3373 "endpoint": r
["entities"][0]["endpoint"],
3375 if provider_id
!= vnfd_id
:
3376 provider_dict
["vdu-profile-id"] = provider_id
3377 requirer_id
= r
["entities"][1]["id"]
3380 "vnf-profile-id": vnf_profile_id
,
3381 "endpoint": r
["entities"][1]["endpoint"],
3383 if requirer_id
!= vnfd_id
:
3384 requirer_dict
["vdu-profile-id"] = requirer_id
3387 "provider/requirer or entities must be included in the relation."
3389 relation_provider
= self
._update
_ee
_relation
_data
_with
_implicit
_data
(
3390 nsr_id
, nsd
, provider_dict
, cached_vnfds
, vnf_profile_id
=vnf_profile_id
3392 relation_requirer
= self
._update
_ee
_relation
_data
_with
_implicit
_data
(
3393 nsr_id
, nsd
, requirer_dict
, cached_vnfds
, vnf_profile_id
=vnf_profile_id
3395 provider
= EERelation(relation_provider
)
3396 requirer
= EERelation(relation_requirer
)
3397 relation
= Relation(r
["name"], provider
, requirer
)
3398 vca_in_relation
= self
._is
_deployed
_vca
_in
_relation
(vca
, relation
)
3400 relations
.append(relation
)
3403 def _get_kdu_resource_data(
3405 ee_relation
: EERelation
,
3406 db_nsr
: Dict
[str, Any
],
3407 cached_vnfds
: Dict
[str, Any
],
3408 ) -> DeployedK8sResource
:
3409 nsd
= get_nsd(db_nsr
)
3410 vnf_profiles
= get_vnf_profiles(nsd
)
3411 vnfd_id
= find_in_list(
3413 lambda vnf_profile
: vnf_profile
["id"] == ee_relation
.vnf_profile_id
,
3415 project
= nsd
["_admin"]["projects_read"][0]
3416 db_vnfd
= self
._get
_vnfd
(vnfd_id
, project
, cached_vnfds
)
3417 kdu_resource_profile
= get_kdu_resource_profile(
3418 db_vnfd
, ee_relation
.kdu_resource_profile_id
3420 kdu_name
= kdu_resource_profile
["kdu-name"]
3421 deployed_kdu
, _
= get_deployed_kdu(
3422 db_nsr
.get("_admin", ()).get("deployed", ()),
3424 ee_relation
.vnf_profile_id
,
3426 deployed_kdu
.update({"resource-name": kdu_resource_profile
["resource-name"]})
3429 def _get_deployed_component(
3431 ee_relation
: EERelation
,
3432 db_nsr
: Dict
[str, Any
],
3433 cached_vnfds
: Dict
[str, Any
],
3434 ) -> DeployedComponent
:
3435 nsr_id
= db_nsr
["_id"]
3436 deployed_component
= None
3437 ee_level
= EELevel
.get_level(ee_relation
)
3438 if ee_level
== EELevel
.NS
:
3439 vca
= get_deployed_vca(db_nsr
, {"vdu_id": None, "member-vnf-index": None})
3441 deployed_component
= DeployedVCA(nsr_id
, vca
)
3442 elif ee_level
== EELevel
.VNF
:
3443 vca
= get_deployed_vca(
3447 "member-vnf-index": ee_relation
.vnf_profile_id
,
3448 "ee_descriptor_id": ee_relation
.execution_environment_ref
,
3452 deployed_component
= DeployedVCA(nsr_id
, vca
)
3453 elif ee_level
== EELevel
.VDU
:
3454 vca
= get_deployed_vca(
3457 "vdu_id": ee_relation
.vdu_profile_id
,
3458 "member-vnf-index": ee_relation
.vnf_profile_id
,
3459 "ee_descriptor_id": ee_relation
.execution_environment_ref
,
3463 deployed_component
= DeployedVCA(nsr_id
, vca
)
3464 elif ee_level
== EELevel
.KDU
:
3465 kdu_resource_data
= self
._get
_kdu
_resource
_data
(
3466 ee_relation
, db_nsr
, cached_vnfds
3468 if kdu_resource_data
:
3469 deployed_component
= DeployedK8sResource(kdu_resource_data
)
3470 return deployed_component
3472 async def _add_relation(
3476 db_nsr
: Dict
[str, Any
],
3477 cached_vnfds
: Dict
[str, Any
],
3478 cached_vnfrs
: Dict
[str, Any
],
3480 deployed_provider
= self
._get
_deployed
_component
(
3481 relation
.provider
, db_nsr
, cached_vnfds
3483 deployed_requirer
= self
._get
_deployed
_component
(
3484 relation
.requirer
, db_nsr
, cached_vnfds
3488 and deployed_requirer
3489 and deployed_provider
.config_sw_installed
3490 and deployed_requirer
.config_sw_installed
3492 provider_db_vnfr
= (
3494 relation
.provider
.nsr_id
,
3495 relation
.provider
.vnf_profile_id
,
3498 if relation
.provider
.vnf_profile_id
3501 requirer_db_vnfr
= (
3503 relation
.requirer
.nsr_id
,
3504 relation
.requirer
.vnf_profile_id
,
3507 if relation
.requirer
.vnf_profile_id
3510 provider_vca_id
= self
.get_vca_id(provider_db_vnfr
, db_nsr
)
3511 requirer_vca_id
= self
.get_vca_id(requirer_db_vnfr
, db_nsr
)
3512 provider_relation_endpoint
= RelationEndpoint(
3513 deployed_provider
.ee_id
,
3515 relation
.provider
.endpoint
,
3517 requirer_relation_endpoint
= RelationEndpoint(
3518 deployed_requirer
.ee_id
,
3520 relation
.requirer
.endpoint
,
3523 await self
.vca_map
[vca_type
].add_relation(
3524 provider
=provider_relation_endpoint
,
3525 requirer
=requirer_relation_endpoint
,
3527 except N2VCException
as exception
:
3528 self
.logger
.error(exception
)
3529 raise LcmException(exception
)
3533 async def _add_vca_relations(
3539 timeout
: int = 3600,
3542 # 1. find all relations for this VCA
3543 # 2. wait for other peers related
3547 # STEP 1: find all relations for this VCA
3550 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
3551 nsd
= get_nsd(db_nsr
)
3554 deployed_vca_dict
= get_deployed_vca_list(db_nsr
)[vca_index
]
3555 my_vca
= DeployedVCA(nsr_id
, deployed_vca_dict
)
3560 relations
.extend(self
._get
_ns
_relations
(nsr_id
, nsd
, my_vca
, cached_vnfds
))
3561 relations
.extend(self
._get
_vnf
_relations
(nsr_id
, nsd
, my_vca
, cached_vnfds
))
3563 # if no relations, terminate
3565 self
.logger
.debug(logging_text
+ " No relations")
3568 self
.logger
.debug(logging_text
+ " adding relations {}".format(relations
))
3575 if now
- start
>= timeout
:
3576 self
.logger
.error(logging_text
+ " : timeout adding relations")
3579 # reload nsr from database (we need to update record: _admin.deployed.VCA)
3580 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
3582 # for each relation, find the VCA's related
3583 for relation
in relations
.copy():
3584 added
= await self
._add
_relation
(
3592 relations
.remove(relation
)
3595 self
.logger
.debug("Relations added")
3597 await asyncio
.sleep(5.0)
3601 except Exception as e
:
3602 self
.logger
.warn(logging_text
+ " ERROR adding relations: {}".format(e
))
3605 async def _install_kdu(
3613 k8s_instance_info
: dict,
3614 k8params
: dict = None,
3619 k8sclustertype
= k8s_instance_info
["k8scluster-type"]
3622 "collection": "nsrs",
3623 "filter": {"_id": nsr_id
},
3624 "path": nsr_db_path
,
3627 if k8s_instance_info
.get("kdu-deployment-name"):
3628 kdu_instance
= k8s_instance_info
.get("kdu-deployment-name")
3630 kdu_instance
= self
.k8scluster_map
[
3632 ].generate_kdu_instance_name(
3633 db_dict
=db_dict_install
,
3634 kdu_model
=k8s_instance_info
["kdu-model"],
3635 kdu_name
=k8s_instance_info
["kdu-name"],
3638 # Update the nsrs table with the kdu-instance value
3642 _desc
={nsr_db_path
+ ".kdu-instance": kdu_instance
},
3645 # Update the nsrs table with the actual namespace being used, if the k8scluster-type is `juju` or
3646 # `juju-bundle`. This verification is needed because there is not a standard/homogeneous namespace
3647 # between the Helm Charts and Juju Bundles-based KNFs. If we found a way of having an homogeneous
3648 # namespace, this first verification could be removed, and the next step would be done for any kind
3650 # TODO -> find a way to have an homogeneous namespace between the Helm Charts and Juju Bundles-based
3651 # KNFs (Bug 2027: https://osm.etsi.org/bugzilla/show_bug.cgi?id=2027)
3652 if k8sclustertype
in ("juju", "juju-bundle"):
3653 # First, verify if the current namespace is present in the `_admin.projects_read` (if not, it means
3654 # that the user passed a namespace which he wants its KDU to be deployed in)
3660 "_admin.projects_write": k8s_instance_info
["namespace"],
3661 "_admin.projects_read": k8s_instance_info
["namespace"],
3667 f
"Updating namespace/model for Juju Bundle from {k8s_instance_info['namespace']} to {kdu_instance}"
3672 _desc
={f
"{nsr_db_path}.namespace": kdu_instance
},
3674 k8s_instance_info
["namespace"] = kdu_instance
3676 await self
.k8scluster_map
[k8sclustertype
].install(
3677 cluster_uuid
=k8s_instance_info
["k8scluster-uuid"],
3678 kdu_model
=k8s_instance_info
["kdu-model"],
3681 db_dict
=db_dict_install
,
3683 kdu_name
=k8s_instance_info
["kdu-name"],
3684 namespace
=k8s_instance_info
["namespace"],
3685 kdu_instance
=kdu_instance
,
3689 # Obtain services to obtain management service ip
3690 services
= await self
.k8scluster_map
[k8sclustertype
].get_services(
3691 cluster_uuid
=k8s_instance_info
["k8scluster-uuid"],
3692 kdu_instance
=kdu_instance
,
3693 namespace
=k8s_instance_info
["namespace"],
3696 # Obtain management service info (if exists)
3697 vnfr_update_dict
= {}
3698 kdu_config
= get_configuration(vnfd
, kdud
["name"])
3700 target_ee_list
= kdu_config
.get("execution-environment-list", [])
3705 vnfr_update_dict
["kdur.{}.services".format(kdu_index
)] = services
3708 for service
in kdud
.get("service", [])
3709 if service
.get("mgmt-service")
3711 for mgmt_service
in mgmt_services
:
3712 for service
in services
:
3713 if service
["name"].startswith(mgmt_service
["name"]):
3714 # Mgmt service found, Obtain service ip
3715 ip
= service
.get("external_ip", service
.get("cluster_ip"))
3716 if isinstance(ip
, list) and len(ip
) == 1:
3720 "kdur.{}.ip-address".format(kdu_index
)
3723 # Check if must update also mgmt ip at the vnf
3724 service_external_cp
= mgmt_service
.get(
3725 "external-connection-point-ref"
3727 if service_external_cp
:
3729 deep_get(vnfd
, ("mgmt-interface", "cp"))
3730 == service_external_cp
3732 vnfr_update_dict
["ip-address"] = ip
3737 "external-connection-point-ref", ""
3739 == service_external_cp
,
3742 "kdur.{}.ip-address".format(kdu_index
)
3747 "Mgmt service name: {} not found".format(
3748 mgmt_service
["name"]
3752 vnfr_update_dict
["kdur.{}.status".format(kdu_index
)] = "READY"
3753 self
.update_db_2("vnfrs", vnfr_data
.get("_id"), vnfr_update_dict
)
3755 kdu_config
= get_configuration(vnfd
, k8s_instance_info
["kdu-name"])
3758 and kdu_config
.get("initial-config-primitive")
3759 and get_juju_ee_ref(vnfd
, k8s_instance_info
["kdu-name"]) is None
3761 initial_config_primitive_list
= kdu_config
.get(
3762 "initial-config-primitive"
3764 initial_config_primitive_list
.sort(key
=lambda val
: int(val
["seq"]))
3766 for initial_config_primitive
in initial_config_primitive_list
:
3767 primitive_params_
= self
._map
_primitive
_params
(
3768 initial_config_primitive
, {}, {}
3771 await asyncio
.wait_for(
3772 self
.k8scluster_map
[k8sclustertype
].exec_primitive(
3773 cluster_uuid
=k8s_instance_info
["k8scluster-uuid"],
3774 kdu_instance
=kdu_instance
,
3775 primitive_name
=initial_config_primitive
["name"],
3776 params
=primitive_params_
,
3777 db_dict
=db_dict_install
,
3783 except Exception as e
:
3784 # Prepare update db with error and raise exception
3787 "nsrs", nsr_id
, {nsr_db_path
+ ".detailed-status": str(e
)}
3791 vnfr_data
.get("_id"),
3792 {"kdur.{}.status".format(kdu_index
): "ERROR"},
3794 except Exception as error
:
3795 # ignore to keep original exception
3796 self
.logger
.warning(
3797 f
"An exception occurred while updating DB: {str(error)}"
3799 # reraise original error
3804 async def deploy_kdus(
3811 task_instantiation_info
,
3813 # Launch kdus if present in the descriptor
3815 k8scluster_id_2_uuic
= {
3816 "helm-chart-v3": {},
3820 async def _get_cluster_id(cluster_id
, cluster_type
):
3821 nonlocal k8scluster_id_2_uuic
3822 if cluster_id
in k8scluster_id_2_uuic
[cluster_type
]:
3823 return k8scluster_id_2_uuic
[cluster_type
][cluster_id
]
3825 # check if K8scluster is creating and wait look if previous tasks in process
3826 task_name
, task_dependency
= self
.lcm_tasks
.lookfor_related(
3827 "k8scluster", cluster_id
3830 text
= "Waiting for related tasks '{}' on k8scluster {} to be completed".format(
3831 task_name
, cluster_id
3833 self
.logger
.debug(logging_text
+ text
)
3834 await asyncio
.wait(task_dependency
, timeout
=3600)
3836 db_k8scluster
= self
.db
.get_one(
3837 "k8sclusters", {"_id": cluster_id
}, fail_on_empty
=False
3839 if not db_k8scluster
:
3840 raise LcmException("K8s cluster {} cannot be found".format(cluster_id
))
3842 k8s_id
= deep_get(db_k8scluster
, ("_admin", cluster_type
, "id"))
3844 if cluster_type
== "helm-chart-v3":
3846 # backward compatibility for existing clusters that have not been initialized for helm v3
3847 k8s_credentials
= yaml
.safe_dump(
3848 db_k8scluster
.get("credentials")
3850 k8s_id
, uninstall_sw
= await self
.k8sclusterhelm3
.init_env(
3851 k8s_credentials
, reuse_cluster_uuid
=cluster_id
3853 db_k8scluster_update
= {}
3854 db_k8scluster_update
["_admin.helm-chart-v3.error_msg"] = None
3855 db_k8scluster_update
["_admin.helm-chart-v3.id"] = k8s_id
3856 db_k8scluster_update
[
3857 "_admin.helm-chart-v3.created"
3859 db_k8scluster_update
[
3860 "_admin.helm-chart-v3.operationalState"
3863 "k8sclusters", cluster_id
, db_k8scluster_update
3865 except Exception as e
:
3868 + "error initializing helm-v3 cluster: {}".format(str(e
))
3871 "K8s cluster '{}' has not been initialized for '{}'".format(
3872 cluster_id
, cluster_type
3877 "K8s cluster '{}' has not been initialized for '{}'".format(
3878 cluster_id
, cluster_type
3881 k8scluster_id_2_uuic
[cluster_type
][cluster_id
] = k8s_id
3884 logging_text
+= "Deploy kdus: "
3887 db_nsr_update
= {"_admin.deployed.K8s": []}
3888 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
3891 updated_cluster_list
= []
3892 updated_v3_cluster_list
= []
3894 for vnfr_data
in db_vnfrs
.values():
3895 vca_id
= self
.get_vca_id(vnfr_data
, {})
3896 for kdu_index
, kdur
in enumerate(get_iterable(vnfr_data
, "kdur")):
3897 # Step 0: Prepare and set parameters
3898 desc_params
= parse_yaml_strings(kdur
.get("additionalParams"))
3899 vnfd_id
= vnfr_data
.get("vnfd-id")
3900 vnfd_with_id
= find_in_list(
3901 db_vnfds
, lambda vnfd
: vnfd
["_id"] == vnfd_id
3905 for kdud
in vnfd_with_id
["kdu"]
3906 if kdud
["name"] == kdur
["kdu-name"]
3908 namespace
= kdur
.get("k8s-namespace")
3909 kdu_deployment_name
= kdur
.get("kdu-deployment-name")
3910 if kdur
.get("helm-chart"):
3911 kdumodel
= kdur
["helm-chart"]
3912 # Default version: helm3, if helm-version is v2 assign v2
3913 k8sclustertype
= "helm-chart-v3"
3914 self
.logger
.debug("kdur: {}".format(kdur
))
3915 elif kdur
.get("juju-bundle"):
3916 kdumodel
= kdur
["juju-bundle"]
3917 k8sclustertype
= "juju-bundle"
3920 "kdu type for kdu='{}.{}' is neither helm-chart nor "
3921 "juju-bundle. Maybe an old NBI version is running".format(
3922 vnfr_data
["member-vnf-index-ref"], kdur
["kdu-name"]
3925 # check if kdumodel is a file and exists
3927 vnfd_with_id
= find_in_list(
3928 db_vnfds
, lambda vnfd
: vnfd
["_id"] == vnfd_id
3930 storage
= deep_get(vnfd_with_id
, ("_admin", "storage"))
3931 if storage
: # may be not present if vnfd has not artifacts
3932 # path format: /vnfdid/pkkdir/helm-charts|juju-bundles/kdumodel
3933 if storage
["pkg-dir"]:
3934 filename
= "{}/{}/{}s/{}".format(
3941 filename
= "{}/Scripts/{}s/{}".format(
3946 if self
.fs
.file_exists(
3947 filename
, mode
="file"
3948 ) or self
.fs
.file_exists(filename
, mode
="dir"):
3949 kdumodel
= self
.fs
.path
+ filename
3950 except (asyncio
.TimeoutError
, asyncio
.CancelledError
):
3952 except Exception as e
: # it is not a file
3953 self
.logger
.warning(f
"An exception occurred: {str(e)}")
3955 k8s_cluster_id
= kdur
["k8s-cluster"]["id"]
3956 step
= "Synchronize repos for k8s cluster '{}'".format(
3959 cluster_uuid
= await _get_cluster_id(k8s_cluster_id
, k8sclustertype
)
3963 k8sclustertype
== "helm-chart"
3964 and cluster_uuid
not in updated_cluster_list
3966 k8sclustertype
== "helm-chart-v3"
3967 and cluster_uuid
not in updated_v3_cluster_list
3969 del_repo_list
, added_repo_dict
= await asyncio
.ensure_future(
3970 self
.k8scluster_map
[k8sclustertype
].synchronize_repos(
3971 cluster_uuid
=cluster_uuid
3974 if del_repo_list
or added_repo_dict
:
3975 if k8sclustertype
== "helm-chart":
3977 "_admin.helm_charts_added." + item
: None
3978 for item
in del_repo_list
3981 "_admin.helm_charts_added." + item
: name
3982 for item
, name
in added_repo_dict
.items()
3984 updated_cluster_list
.append(cluster_uuid
)
3985 elif k8sclustertype
== "helm-chart-v3":
3987 "_admin.helm_charts_v3_added." + item
: None
3988 for item
in del_repo_list
3991 "_admin.helm_charts_v3_added." + item
: name
3992 for item
, name
in added_repo_dict
.items()
3994 updated_v3_cluster_list
.append(cluster_uuid
)
3996 logging_text
+ "repos synchronized on k8s cluster "
3997 "'{}' to_delete: {}, to_add: {}".format(
3998 k8s_cluster_id
, del_repo_list
, added_repo_dict
4003 {"_id": k8s_cluster_id
},
4009 step
= "Instantiating KDU {}.{} in k8s cluster {}".format(
4010 vnfr_data
["member-vnf-index-ref"],
4014 k8s_instance_info
= {
4015 "kdu-instance": None,
4016 "k8scluster-uuid": cluster_uuid
,
4017 "k8scluster-type": k8sclustertype
,
4018 "member-vnf-index": vnfr_data
["member-vnf-index-ref"],
4019 "kdu-name": kdur
["kdu-name"],
4020 "kdu-model": kdumodel
,
4021 "namespace": namespace
,
4022 "kdu-deployment-name": kdu_deployment_name
,
4024 db_path
= "_admin.deployed.K8s.{}".format(index
)
4025 db_nsr_update
[db_path
] = k8s_instance_info
4026 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
4027 vnfd_with_id
= find_in_list(
4028 db_vnfds
, lambda vnf
: vnf
["_id"] == vnfd_id
4030 task
= asyncio
.ensure_future(
4039 k8params
=desc_params
,
4044 self
.lcm_tasks
.register(
4048 "instantiate_KDU-{}".format(index
),
4051 task_instantiation_info
[task
] = "Deploying KDU {}".format(
4057 except (LcmException
, asyncio
.CancelledError
):
4059 except Exception as e
:
4060 msg
= "Exception {} while {}: {}".format(type(e
).__name
__, step
, e
)
4061 if isinstance(e
, (N2VCException
, DbException
)):
4062 self
.logger
.error(logging_text
+ msg
)
4064 self
.logger
.critical(logging_text
+ msg
, exc_info
=True)
4065 raise LcmException(msg
)
4068 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
4088 task_instantiation_info
,
4091 # launch instantiate_N2VC in a asyncio task and register task object
4092 # Look where information of this charm is at database <nsrs>._admin.deployed.VCA
4093 # if not found, create one entry and update database
4094 # fill db_nsr._admin.deployed.VCA.<index>
4097 logging_text
+ "_deploy_n2vc vnfd_id={}, vdu_id={}".format(vnfd_id
, vdu_id
)
4101 get_charm_name
= False
4102 if "execution-environment-list" in descriptor_config
:
4103 ee_list
= descriptor_config
.get("execution-environment-list", [])
4104 elif "juju" in descriptor_config
:
4105 ee_list
= [descriptor_config
] # ns charms
4106 if "execution-environment-list" not in descriptor_config
:
4107 # charm name is only required for ns charms
4108 get_charm_name
= True
4109 else: # other types as script are not supported
4112 for ee_item
in ee_list
:
4115 + "_deploy_n2vc ee_item juju={}, helm={}".format(
4116 ee_item
.get("juju"), ee_item
.get("helm-chart")
4119 ee_descriptor_id
= ee_item
.get("id")
4120 if ee_item
.get("juju"):
4121 vca_name
= ee_item
["juju"].get("charm")
4123 charm_name
= self
.find_charm_name(db_nsr
, str(vca_name
))
4126 if ee_item
["juju"].get("charm") is not None
4129 if ee_item
["juju"].get("cloud") == "k8s":
4130 vca_type
= "k8s_proxy_charm"
4131 elif ee_item
["juju"].get("proxy") is False:
4132 vca_type
= "native_charm"
4133 elif ee_item
.get("helm-chart"):
4134 vca_name
= ee_item
["helm-chart"]
4135 vca_type
= "helm-v3"
4138 logging_text
+ "skipping non juju neither charm configuration"
4143 for vca_index
, vca_deployed
in enumerate(
4144 db_nsr
["_admin"]["deployed"]["VCA"]
4146 if not vca_deployed
:
4149 vca_deployed
.get("member-vnf-index") == member_vnf_index
4150 and vca_deployed
.get("vdu_id") == vdu_id
4151 and vca_deployed
.get("kdu_name") == kdu_name
4152 and vca_deployed
.get("vdu_count_index", 0) == vdu_index
4153 and vca_deployed
.get("ee_descriptor_id") == ee_descriptor_id
4157 # not found, create one.
4159 "ns" if not member_vnf_index
else "vnf/{}".format(member_vnf_index
)
4162 target
+= "/vdu/{}/{}".format(vdu_id
, vdu_index
or 0)
4164 target
+= "/kdu/{}".format(kdu_name
)
4166 "target_element": target
,
4167 # ^ target_element will replace member-vnf-index, kdu_name, vdu_id ... in a single string
4168 "member-vnf-index": member_vnf_index
,
4170 "kdu_name": kdu_name
,
4171 "vdu_count_index": vdu_index
,
4172 "operational-status": "init", # TODO revise
4173 "detailed-status": "", # TODO revise
4174 "step": "initial-deploy", # TODO revise
4176 "vdu_name": vdu_name
,
4178 "ee_descriptor_id": ee_descriptor_id
,
4179 "charm_name": charm_name
,
4183 # create VCA and configurationStatus in db
4185 "_admin.deployed.VCA.{}".format(vca_index
): vca_deployed
,
4186 "configurationStatus.{}".format(vca_index
): dict(),
4188 self
.update_db_2("nsrs", nsr_id
, db_dict
)
4190 db_nsr
["_admin"]["deployed"]["VCA"].append(vca_deployed
)
4192 self
.logger
.debug("N2VC > NSR_ID > {}".format(nsr_id
))
4193 self
.logger
.debug("N2VC > DB_NSR > {}".format(db_nsr
))
4194 self
.logger
.debug("N2VC > VCA_DEPLOYED > {}".format(vca_deployed
))
4197 task_n2vc
= asyncio
.ensure_future(
4198 self
.instantiate_N2VC(
4199 logging_text
=logging_text
,
4200 vca_index
=vca_index
,
4206 vdu_index
=vdu_index
,
4207 kdu_index
=kdu_index
,
4208 deploy_params
=deploy_params
,
4209 config_descriptor
=descriptor_config
,
4210 base_folder
=base_folder
,
4211 nslcmop_id
=nslcmop_id
,
4215 ee_config_descriptor
=ee_item
,
4218 self
.lcm_tasks
.register(
4222 "instantiate_N2VC-{}".format(vca_index
),
4225 task_instantiation_info
[
4227 ] = self
.task_name_deploy_vca
+ " {}.{}".format(
4228 member_vnf_index
or "", vdu_id
or ""
4232 def _create_nslcmop(nsr_id
, operation
, params
):
4234 Creates a ns-lcm-opp content to be stored at database.
4235 :param nsr_id: internal id of the instance
4236 :param operation: instantiate, terminate, scale, action, ...
4237 :param params: user parameters for the operation
4238 :return: dictionary following SOL005 format
4240 # Raise exception if invalid arguments
4241 if not (nsr_id
and operation
and params
):
4243 "Parameters 'nsr_id', 'operation' and 'params' needed to create primitive not provided"
4250 # COMPLETED,PARTIALLY_COMPLETED,FAILED_TEMP,FAILED,ROLLING_BACK,ROLLED_BACK
4251 "operationState": "PROCESSING",
4252 "statusEnteredTime": now
,
4253 "nsInstanceId": nsr_id
,
4254 "lcmOperationType": operation
,
4256 "isAutomaticInvocation": False,
4257 "operationParams": params
,
4258 "isCancelPending": False,
4260 "self": "/osm/nslcm/v1/ns_lcm_op_occs/" + _id
,
4261 "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsr_id
,
4266 def _format_additional_params(self
, params
):
4267 params
= params
or {}
4268 for key
, value
in params
.items():
4269 if str(value
).startswith("!!yaml "):
4270 params
[key
] = yaml
.safe_load(value
[7:])
4273 def _get_terminate_primitive_params(self
, seq
, vnf_index
):
4274 primitive
= seq
.get("name")
4275 primitive_params
= {}
4277 "member_vnf_index": vnf_index
,
4278 "primitive": primitive
,
4279 "primitive_params": primitive_params
,
4282 return self
._map
_primitive
_params
(seq
, params
, desc_params
)
4286 def _retry_or_skip_suboperation(self
, db_nslcmop
, op_index
):
4287 op
= deep_get(db_nslcmop
, ("_admin", "operations"), [])[op_index
]
4288 if op
.get("operationState") == "COMPLETED":
4289 # b. Skip sub-operation
4290 # _ns_execute_primitive() or RO.create_action() will NOT be executed
4291 return self
.SUBOPERATION_STATUS_SKIP
4293 # c. retry executing sub-operation
4294 # The sub-operation exists, and operationState != 'COMPLETED'
4295 # Update operationState = 'PROCESSING' to indicate a retry.
4296 operationState
= "PROCESSING"
4297 detailed_status
= "In progress"
4298 self
._update
_suboperation
_status
(
4299 db_nslcmop
, op_index
, operationState
, detailed_status
4301 # Return the sub-operation index
4302 # _ns_execute_primitive() or RO.create_action() will be called from scale()
4303 # with arguments extracted from the sub-operation
4306 # Find a sub-operation where all keys in a matching dictionary must match
4307 # Returns the index of the matching sub-operation, or SUBOPERATION_STATUS_NOT_FOUND if no match
4308 def _find_suboperation(self
, db_nslcmop
, match
):
4309 if db_nslcmop
and match
:
4310 op_list
= db_nslcmop
.get("_admin", {}).get("operations", [])
4311 for i
, op
in enumerate(op_list
):
4312 if all(op
.get(k
) == match
[k
] for k
in match
):
4314 return self
.SUBOPERATION_STATUS_NOT_FOUND
4316 # Update status for a sub-operation given its index
4317 def _update_suboperation_status(
4318 self
, db_nslcmop
, op_index
, operationState
, detailed_status
4320 # Update DB for HA tasks
4321 q_filter
= {"_id": db_nslcmop
["_id"]}
4323 "_admin.operations.{}.operationState".format(op_index
): operationState
,
4324 "_admin.operations.{}.detailed-status".format(op_index
): detailed_status
,
4327 "nslcmops", q_filter
=q_filter
, update_dict
=update_dict
, fail_on_empty
=False
4330 # Add sub-operation, return the index of the added sub-operation
4331 # Optionally, set operationState, detailed-status, and operationType
4332 # Status and type are currently set for 'scale' sub-operations:
4333 # 'operationState' : 'PROCESSING' | 'COMPLETED' | 'FAILED'
4334 # 'detailed-status' : status message
4335 # 'operationType': may be any type, in the case of scaling: 'PRE-SCALE' | 'POST-SCALE'
4336 # Status and operation type are currently only used for 'scale', but NOT for 'terminate' sub-operations.
4337 def _add_suboperation(
4345 mapped_primitive_params
,
4346 operationState
=None,
4347 detailed_status
=None,
4350 RO_scaling_info
=None,
4353 return self
.SUBOPERATION_STATUS_NOT_FOUND
4354 # Get the "_admin.operations" list, if it exists
4355 db_nslcmop_admin
= db_nslcmop
.get("_admin", {})
4356 op_list
= db_nslcmop_admin
.get("operations")
4357 # Create or append to the "_admin.operations" list
4359 "member_vnf_index": vnf_index
,
4361 "vdu_count_index": vdu_count_index
,
4362 "primitive": primitive
,
4363 "primitive_params": mapped_primitive_params
,
4366 new_op
["operationState"] = operationState
4368 new_op
["detailed-status"] = detailed_status
4370 new_op
["lcmOperationType"] = operationType
4372 new_op
["RO_nsr_id"] = RO_nsr_id
4374 new_op
["RO_scaling_info"] = RO_scaling_info
4376 # No existing operations, create key 'operations' with current operation as first list element
4377 db_nslcmop_admin
.update({"operations": [new_op
]})
4378 op_list
= db_nslcmop_admin
.get("operations")
4380 # Existing operations, append operation to list
4381 op_list
.append(new_op
)
4383 db_nslcmop_update
= {"_admin.operations": op_list
}
4384 self
.update_db_2("nslcmops", db_nslcmop
["_id"], db_nslcmop_update
)
4385 op_index
= len(op_list
) - 1
4388 # Helper methods for scale() sub-operations
4390 # pre-scale/post-scale:
4391 # Check for 3 different cases:
4392 # a. New: First time execution, return SUBOPERATION_STATUS_NEW
4393 # b. Skip: Existing sub-operation exists, operationState == 'COMPLETED', return SUBOPERATION_STATUS_SKIP
4394 # c. retry: Existing sub-operation exists, operationState != 'COMPLETED', return op_index to re-execute
4395 def _check_or_add_scale_suboperation(
4399 vnf_config_primitive
,
4403 RO_scaling_info
=None,
4405 # Find this sub-operation
4406 if RO_nsr_id
and RO_scaling_info
:
4407 operationType
= "SCALE-RO"
4409 "member_vnf_index": vnf_index
,
4410 "RO_nsr_id": RO_nsr_id
,
4411 "RO_scaling_info": RO_scaling_info
,
4415 "member_vnf_index": vnf_index
,
4416 "primitive": vnf_config_primitive
,
4417 "primitive_params": primitive_params
,
4418 "lcmOperationType": operationType
,
4420 op_index
= self
._find
_suboperation
(db_nslcmop
, match
)
4421 if op_index
== self
.SUBOPERATION_STATUS_NOT_FOUND
:
4422 # a. New sub-operation
4423 # The sub-operation does not exist, add it.
4424 # _ns_execute_primitive() will be called from scale() as usual, with non-modified arguments
4425 # The following parameters are set to None for all kind of scaling:
4427 vdu_count_index
= None
4429 if RO_nsr_id
and RO_scaling_info
:
4430 vnf_config_primitive
= None
4431 primitive_params
= None
4434 RO_scaling_info
= None
4435 # Initial status for sub-operation
4436 operationState
= "PROCESSING"
4437 detailed_status
= "In progress"
4438 # Add sub-operation for pre/post-scaling (zero or more operations)
4439 self
._add
_suboperation
(
4445 vnf_config_primitive
,
4453 return self
.SUBOPERATION_STATUS_NEW
4455 # Return either SUBOPERATION_STATUS_SKIP (operationState == 'COMPLETED'),
4456 # or op_index (operationState != 'COMPLETED')
4457 return self
._retry
_or
_skip
_suboperation
(db_nslcmop
, op_index
)
4459 # Function to return execution_environment id
4461 def _get_ee_id(self
, vnf_index
, vdu_id
, vca_deployed_list
):
4462 # TODO vdu_index_count
4463 for vca
in vca_deployed_list
:
4464 if vca
["member-vnf-index"] == vnf_index
and vca
["vdu_id"] == vdu_id
:
4465 return vca
.get("ee_id")
4467 async def destroy_N2VC(
4475 exec_primitives
=True,
4480 Execute the terminate primitives and destroy the execution environment (if destroy_ee=False
4481 :param logging_text:
4483 :param vca_deployed: Dictionary of deployment info at db_nsr._admin.depoloyed.VCA.<INDEX>
4484 :param config_descriptor: Configuration descriptor of the NSD, VNFD, VNFD.vdu or VNFD.kdu
4485 :param vca_index: index in the database _admin.deployed.VCA
4486 :param destroy_ee: False to do not destroy, because it will be destroyed all of then at once
4487 :param exec_primitives: False to do not execute terminate primitives, because the config is not completed or has
4488 not executed properly
4489 :param scaling_in: True destroys the application, False destroys the model
4490 :return: None or exception
4495 + " vca_index: {}, vca_deployed: {}, config_descriptor: {}, destroy_ee: {}".format(
4496 vca_index
, vca_deployed
, config_descriptor
, destroy_ee
4500 vca_type
= vca_deployed
.get("type", "lxc_proxy_charm")
4502 # execute terminate_primitives
4504 terminate_primitives
= get_ee_sorted_terminate_config_primitive_list(
4505 config_descriptor
.get("terminate-config-primitive"),
4506 vca_deployed
.get("ee_descriptor_id"),
4508 vdu_id
= vca_deployed
.get("vdu_id")
4509 vdu_count_index
= vca_deployed
.get("vdu_count_index")
4510 vdu_name
= vca_deployed
.get("vdu_name")
4511 vnf_index
= vca_deployed
.get("member-vnf-index")
4512 if terminate_primitives
and vca_deployed
.get("needed_terminate"):
4513 for seq
in terminate_primitives
:
4514 # For each sequence in list, get primitive and call _ns_execute_primitive()
4515 step
= "Calling terminate action for vnf_member_index={} primitive={}".format(
4516 vnf_index
, seq
.get("name")
4518 self
.logger
.debug(logging_text
+ step
)
4519 # Create the primitive for each sequence, i.e. "primitive": "touch"
4520 primitive
= seq
.get("name")
4521 mapped_primitive_params
= self
._get
_terminate
_primitive
_params
(
4526 self
._add
_suboperation
(
4533 mapped_primitive_params
,
4535 # Sub-operations: Call _ns_execute_primitive() instead of action()
4537 result
, result_detail
= await self
._ns
_execute
_primitive
(
4538 vca_deployed
["ee_id"],
4540 mapped_primitive_params
,
4544 except LcmException
:
4545 # this happens when VCA is not deployed. In this case it is not needed to terminate
4547 result_ok
= ["COMPLETED", "PARTIALLY_COMPLETED"]
4548 if result
not in result_ok
:
4550 "terminate_primitive {} for vnf_member_index={} fails with "
4551 "error {}".format(seq
.get("name"), vnf_index
, result_detail
)
4553 # set that this VCA do not need terminated
4554 db_update_entry
= "_admin.deployed.VCA.{}.needed_terminate".format(
4558 "nsrs", db_nslcmop
["nsInstanceId"], {db_update_entry
: False}
4561 # Delete Prometheus Jobs if any
4562 # This uses NSR_ID, so it will destroy any jobs under this index
4563 self
.db
.del_list("prometheus_jobs", {"nsr_id": db_nslcmop
["nsInstanceId"]})
4566 await self
.vca_map
[vca_type
].delete_execution_environment(
4567 vca_deployed
["ee_id"],
4568 scaling_in
=scaling_in
,
4573 async def _delete_all_N2VC(self
, db_nsr
: dict, vca_id
: str = None):
4574 self
._write
_all
_config
_status
(db_nsr
=db_nsr
, status
="TERMINATING")
4575 namespace
= "." + db_nsr
["_id"]
4577 await self
.n2vc
.delete_namespace(
4578 namespace
=namespace
,
4579 total_timeout
=self
.timeout
.charm_delete
,
4582 except N2VCNotFound
: # already deleted. Skip
4584 self
._write
_all
_config
_status
(db_nsr
=db_nsr
, status
="DELETED")
4586 async def terminate(self
, nsr_id
, nslcmop_id
):
4587 # Try to lock HA task here
4588 task_is_locked_by_me
= self
.lcm_tasks
.lock_HA("ns", "nslcmops", nslcmop_id
)
4589 if not task_is_locked_by_me
:
4592 logging_text
= "Task ns={} terminate={} ".format(nsr_id
, nslcmop_id
)
4593 self
.logger
.debug(logging_text
+ "Enter")
4594 timeout_ns_terminate
= self
.timeout
.ns_terminate
4597 operation_params
= None
4599 error_list
= [] # annotates all failed error messages
4600 db_nslcmop_update
= {}
4601 autoremove
= False # autoremove after terminated
4602 tasks_dict_info
= {}
4605 "Stage 1/3: Preparing task.",
4606 "Waiting for previous operations to terminate.",
4609 # ^ contains [stage, step, VIM-status]
4611 # wait for any previous tasks in process
4612 await self
.lcm_tasks
.waitfor_related_HA("ns", "nslcmops", nslcmop_id
)
4614 stage
[1] = "Getting nslcmop={} from db.".format(nslcmop_id
)
4615 db_nslcmop
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
})
4616 operation_params
= db_nslcmop
.get("operationParams") or {}
4617 if operation_params
.get("timeout_ns_terminate"):
4618 timeout_ns_terminate
= operation_params
["timeout_ns_terminate"]
4619 stage
[1] = "Getting nsr={} from db.".format(nsr_id
)
4620 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
4622 db_nsr_update
["operational-status"] = "terminating"
4623 db_nsr_update
["config-status"] = "terminating"
4624 self
._write
_ns
_status
(
4626 ns_state
="TERMINATING",
4627 current_operation
="TERMINATING",
4628 current_operation_id
=nslcmop_id
,
4629 other_update
=db_nsr_update
,
4631 self
._write
_op
_status
(op_id
=nslcmop_id
, queuePosition
=0, stage
=stage
)
4632 nsr_deployed
= deepcopy(db_nsr
["_admin"].get("deployed")) or {}
4633 if db_nsr
["_admin"]["nsState"] == "NOT_INSTANTIATED":
4636 stage
[1] = "Getting vnf descriptors from db."
4637 db_vnfrs_list
= self
.db
.get_list("vnfrs", {"nsr-id-ref": nsr_id
})
4639 db_vnfr
["member-vnf-index-ref"]: db_vnfr
for db_vnfr
in db_vnfrs_list
4641 db_vnfds_from_id
= {}
4642 db_vnfds_from_member_index
= {}
4644 for vnfr
in db_vnfrs_list
:
4645 vnfd_id
= vnfr
["vnfd-id"]
4646 if vnfd_id
not in db_vnfds_from_id
:
4647 vnfd
= self
.db
.get_one("vnfds", {"_id": vnfd_id
})
4648 db_vnfds_from_id
[vnfd_id
] = vnfd
4649 db_vnfds_from_member_index
[
4650 vnfr
["member-vnf-index-ref"]
4651 ] = db_vnfds_from_id
[vnfd_id
]
4653 # Destroy individual execution environments when there are terminating primitives.
4654 # Rest of EE will be deleted at once
4655 # TODO - check before calling _destroy_N2VC
4656 # if not operation_params.get("skip_terminate_primitives"):#
4657 # or not vca.get("needed_terminate"):
4658 stage
[0] = "Stage 2/3 execute terminating primitives."
4659 self
.logger
.debug(logging_text
+ stage
[0])
4660 stage
[1] = "Looking execution environment that needs terminate."
4661 self
.logger
.debug(logging_text
+ stage
[1])
4663 for vca_index
, vca
in enumerate(get_iterable(nsr_deployed
, "VCA")):
4664 config_descriptor
= None
4665 vca_member_vnf_index
= vca
.get("member-vnf-index")
4666 vca_id
= self
.get_vca_id(
4667 db_vnfrs_dict
.get(vca_member_vnf_index
)
4668 if vca_member_vnf_index
4672 if not vca
or not vca
.get("ee_id"):
4674 if not vca
.get("member-vnf-index"):
4676 config_descriptor
= db_nsr
.get("ns-configuration")
4677 elif vca
.get("vdu_id"):
4678 db_vnfd
= db_vnfds_from_member_index
[vca
["member-vnf-index"]]
4679 config_descriptor
= get_configuration(db_vnfd
, vca
.get("vdu_id"))
4680 elif vca
.get("kdu_name"):
4681 db_vnfd
= db_vnfds_from_member_index
[vca
["member-vnf-index"]]
4682 config_descriptor
= get_configuration(db_vnfd
, vca
.get("kdu_name"))
4684 db_vnfd
= db_vnfds_from_member_index
[vca
["member-vnf-index"]]
4685 config_descriptor
= get_configuration(db_vnfd
, db_vnfd
["id"])
4686 vca_type
= vca
.get("type")
4687 exec_terminate_primitives
= not operation_params
.get(
4688 "skip_terminate_primitives"
4689 ) and vca
.get("needed_terminate")
4690 # For helm we must destroy_ee. Also for native_charm, as juju_model cannot be deleted if there are
4691 # pending native charms
4692 destroy_ee
= True if vca_type
in ("helm-v3", "native_charm") else False
4693 # self.logger.debug(logging_text + "vca_index: {}, ee_id: {}, vca_type: {} destroy_ee: {}".format(
4694 # vca_index, vca.get("ee_id"), vca_type, destroy_ee))
4695 task
= asyncio
.ensure_future(
4703 exec_terminate_primitives
,
4707 tasks_dict_info
[task
] = "Terminating VCA {}".format(vca
.get("ee_id"))
4709 # wait for pending tasks of terminate primitives
4713 + "Waiting for tasks {}".format(list(tasks_dict_info
.keys()))
4715 error_list
= await self
._wait
_for
_tasks
(
4718 min(self
.timeout
.charm_delete
, timeout_ns_terminate
),
4722 tasks_dict_info
.clear()
4724 return # raise LcmException("; ".join(error_list))
4726 # remove All execution environments at once
4727 stage
[0] = "Stage 3/3 delete all."
4729 if nsr_deployed
.get("VCA"):
4730 stage
[1] = "Deleting all execution environments."
4731 self
.logger
.debug(logging_text
+ stage
[1])
4732 vca_id
= self
.get_vca_id({}, db_nsr
)
4733 task_delete_ee
= asyncio
.ensure_future(
4735 self
._delete
_all
_N
2VC
(db_nsr
=db_nsr
, vca_id
=vca_id
),
4736 timeout
=self
.timeout
.charm_delete
,
4739 # task_delete_ee = asyncio.ensure_future(self.n2vc.delete_namespace(namespace="." + nsr_id))
4740 tasks_dict_info
[task_delete_ee
] = "Terminating all VCA"
4742 # Delete Namespace and Certificates if necessary
4743 if check_helm_ee_in_ns(list(db_vnfds_from_member_index
.values())):
4744 await self
.vca_map
["helm-v3"].delete_tls_certificate(
4745 namespace
=db_nslcmop
["nsInstanceId"],
4746 certificate_name
=self
.EE_TLS_NAME
,
4748 await self
.vca_map
["helm-v3"].delete_namespace(
4749 namespace
=db_nslcmop
["nsInstanceId"],
4752 # Delete from k8scluster
4753 stage
[1] = "Deleting KDUs."
4754 self
.logger
.debug(logging_text
+ stage
[1])
4755 # print(nsr_deployed)
4756 for kdu
in get_iterable(nsr_deployed
, "K8s"):
4757 if not kdu
or not kdu
.get("kdu-instance"):
4759 kdu_instance
= kdu
.get("kdu-instance")
4760 if kdu
.get("k8scluster-type") in self
.k8scluster_map
:
4761 # TODO: Uninstall kdu instances taking into account they could be deployed in different VIMs
4762 vca_id
= self
.get_vca_id({}, db_nsr
)
4763 task_delete_kdu_instance
= asyncio
.ensure_future(
4764 self
.k8scluster_map
[kdu
["k8scluster-type"]].uninstall(
4765 cluster_uuid
=kdu
.get("k8scluster-uuid"),
4766 kdu_instance
=kdu_instance
,
4768 namespace
=kdu
.get("namespace"),
4774 + "Unknown k8s deployment type {}".format(
4775 kdu
.get("k8scluster-type")
4780 task_delete_kdu_instance
4781 ] = "Terminating KDU '{}'".format(kdu
.get("kdu-name"))
4784 stage
[1] = "Deleting ns from VIM."
4785 if self
.ro_config
.ng
:
4786 task_delete_ro
= asyncio
.ensure_future(
4787 self
._terminate
_ng
_ro
(
4788 logging_text
, nsr_deployed
, nsr_id
, nslcmop_id
, stage
4791 tasks_dict_info
[task_delete_ro
] = "Removing deployment from VIM"
4793 # rest of staff will be done at finally
4796 ROclient
.ROClientException
,
4801 self
.logger
.error(logging_text
+ "Exit Exception {}".format(e
))
4803 except asyncio
.CancelledError
:
4805 logging_text
+ "Cancelled Exception while '{}'".format(stage
[1])
4807 exc
= "Operation was cancelled"
4808 except Exception as e
:
4809 exc
= traceback
.format_exc()
4810 self
.logger
.critical(
4811 logging_text
+ "Exit Exception while '{}': {}".format(stage
[1], e
),
4816 error_list
.append(str(exc
))
4818 # wait for pending tasks
4820 stage
[1] = "Waiting for terminate pending tasks."
4821 self
.logger
.debug(logging_text
+ stage
[1])
4822 error_list
+= await self
._wait
_for
_tasks
(
4825 timeout_ns_terminate
,
4829 stage
[1] = stage
[2] = ""
4830 except asyncio
.CancelledError
:
4831 error_list
.append("Cancelled")
4832 await self
._cancel
_pending
_tasks
(logging_text
, tasks_dict_info
)
4833 await self
._wait
_for
_tasks
(
4836 timeout_ns_terminate
,
4840 except Exception as exc
:
4841 error_list
.append(str(exc
))
4842 # update status at database
4844 error_detail
= "; ".join(error_list
)
4845 # self.logger.error(logging_text + error_detail)
4846 error_description_nslcmop
= "{} Detail: {}".format(
4847 stage
[0], error_detail
4849 error_description_nsr
= "Operation: TERMINATING.{}, {}.".format(
4850 nslcmop_id
, stage
[0]
4853 db_nsr_update
["operational-status"] = "failed"
4854 db_nsr_update
["detailed-status"] = (
4855 error_description_nsr
+ " Detail: " + error_detail
4857 db_nslcmop_update
["detailed-status"] = error_detail
4858 nslcmop_operation_state
= "FAILED"
4862 error_description_nsr
= error_description_nslcmop
= None
4863 ns_state
= "NOT_INSTANTIATED"
4864 db_nsr_update
["operational-status"] = "terminated"
4865 db_nsr_update
["detailed-status"] = "Done"
4866 db_nsr_update
["_admin.nsState"] = "NOT_INSTANTIATED"
4867 db_nslcmop_update
["detailed-status"] = "Done"
4868 nslcmop_operation_state
= "COMPLETED"
4871 self
._write
_ns
_status
(
4874 current_operation
="IDLE",
4875 current_operation_id
=None,
4876 error_description
=error_description_nsr
,
4877 error_detail
=error_detail
,
4878 other_update
=db_nsr_update
,
4880 self
._write
_op
_status
(
4883 error_message
=error_description_nslcmop
,
4884 operation_state
=nslcmop_operation_state
,
4885 other_update
=db_nslcmop_update
,
4887 if ns_state
== "NOT_INSTANTIATED":
4891 {"nsr-id-ref": nsr_id
},
4892 {"_admin.nsState": "NOT_INSTANTIATED"},
4894 except DbException
as e
:
4897 + "Error writing VNFR status for nsr-id-ref: {} -> {}".format(
4901 if operation_params
:
4902 autoremove
= operation_params
.get("autoremove", False)
4903 if nslcmop_operation_state
:
4905 await self
.msg
.aiowrite(
4910 "nslcmop_id": nslcmop_id
,
4911 "operationState": nslcmop_operation_state
,
4912 "autoremove": autoremove
,
4915 except Exception as e
:
4917 logging_text
+ "kafka_write notification Exception {}".format(e
)
4919 self
.logger
.debug(f
"Deleting alerts: ns_id={nsr_id}")
4920 self
.db
.del_list("alerts", {"tags.ns_id": nsr_id
})
4922 self
.logger
.debug(logging_text
+ "Exit")
4923 self
.lcm_tasks
.remove("ns", nsr_id
, nslcmop_id
, "ns_terminate")
4925 async def _wait_for_tasks(
4926 self
, logging_text
, created_tasks_info
, timeout
, stage
, nslcmop_id
, nsr_id
=None
4929 error_detail_list
= []
4931 pending_tasks
= list(created_tasks_info
.keys())
4932 num_tasks
= len(pending_tasks
)
4934 stage
[1] = "{}/{}.".format(num_done
, num_tasks
)
4935 self
._write
_op
_status
(nslcmop_id
, stage
)
4936 while pending_tasks
:
4938 _timeout
= timeout
+ time_start
- time()
4939 done
, pending_tasks
= await asyncio
.wait(
4940 pending_tasks
, timeout
=_timeout
, return_when
=asyncio
.FIRST_COMPLETED
4942 num_done
+= len(done
)
4943 if not done
: # Timeout
4944 for task
in pending_tasks
:
4945 new_error
= created_tasks_info
[task
] + ": Timeout"
4946 error_detail_list
.append(new_error
)
4947 error_list
.append(new_error
)
4950 if task
.cancelled():
4953 exc
= task
.exception()
4955 if isinstance(exc
, asyncio
.TimeoutError
):
4957 new_error
= created_tasks_info
[task
] + ": {}".format(exc
)
4958 error_list
.append(created_tasks_info
[task
])
4959 error_detail_list
.append(new_error
)
4966 ROclient
.ROClientException
,
4972 self
.logger
.error(logging_text
+ new_error
)
4974 exc_traceback
= "".join(
4975 traceback
.format_exception(None, exc
, exc
.__traceback
__)
4979 + created_tasks_info
[task
]
4985 logging_text
+ created_tasks_info
[task
] + ": Done"
4987 stage
[1] = "{}/{}.".format(num_done
, num_tasks
)
4989 stage
[1] += " Errors: " + ". ".join(error_detail_list
) + "."
4990 if nsr_id
: # update also nsr
4995 "errorDescription": "Error at: " + ", ".join(error_list
),
4996 "errorDetail": ". ".join(error_detail_list
),
4999 self
._write
_op
_status
(nslcmop_id
, stage
)
5000 return error_detail_list
5002 async def _cancel_pending_tasks(self
, logging_text
, created_tasks_info
):
5003 for task
, name
in created_tasks_info
.items():
5004 self
.logger
.debug(logging_text
+ "Cancelling task: " + name
)
5008 def _map_primitive_params(primitive_desc
, params
, instantiation_params
):
5010 Generates the params to be provided to charm before executing primitive. If user does not provide a parameter,
5011 The default-value is used. If it is between < > it look for a value at instantiation_params
5012 :param primitive_desc: portion of VNFD/NSD that describes primitive
5013 :param params: Params provided by user
5014 :param instantiation_params: Instantiation params provided by user
5015 :return: a dictionary with the calculated params
5017 calculated_params
= {}
5018 for parameter
in primitive_desc
.get("parameter", ()):
5019 param_name
= parameter
["name"]
5020 if param_name
in params
:
5021 calculated_params
[param_name
] = params
[param_name
]
5022 elif "default-value" in parameter
or "value" in parameter
:
5023 if "value" in parameter
:
5024 calculated_params
[param_name
] = parameter
["value"]
5026 calculated_params
[param_name
] = parameter
["default-value"]
5028 isinstance(calculated_params
[param_name
], str)
5029 and calculated_params
[param_name
].startswith("<")
5030 and calculated_params
[param_name
].endswith(">")
5032 if calculated_params
[param_name
][1:-1] in instantiation_params
:
5033 calculated_params
[param_name
] = instantiation_params
[
5034 calculated_params
[param_name
][1:-1]
5038 "Parameter {} needed to execute primitive {} not provided".format(
5039 calculated_params
[param_name
], primitive_desc
["name"]
5044 "Parameter {} needed to execute primitive {} not provided".format(
5045 param_name
, primitive_desc
["name"]
5049 if isinstance(calculated_params
[param_name
], (dict, list, tuple)):
5050 calculated_params
[param_name
] = yaml
.safe_dump(
5051 calculated_params
[param_name
], default_flow_style
=True, width
=256
5053 elif isinstance(calculated_params
[param_name
], str) and calculated_params
[
5055 ].startswith("!!yaml "):
5056 calculated_params
[param_name
] = calculated_params
[param_name
][7:]
5057 if parameter
.get("data-type") == "INTEGER":
5059 calculated_params
[param_name
] = int(calculated_params
[param_name
])
5060 except ValueError: # error converting string to int
5062 "Parameter {} of primitive {} must be integer".format(
5063 param_name
, primitive_desc
["name"]
5066 elif parameter
.get("data-type") == "BOOLEAN":
5067 calculated_params
[param_name
] = not (
5068 (str(calculated_params
[param_name
])).lower() == "false"
5071 # add always ns_config_info if primitive name is config
5072 if primitive_desc
["name"] == "config":
5073 if "ns_config_info" in instantiation_params
:
5074 calculated_params
["ns_config_info"] = instantiation_params
[
5077 return calculated_params
5079 def _look_for_deployed_vca(
5086 ee_descriptor_id
=None,
5088 # find vca_deployed record for this action. Raise LcmException if not found or there is not any id.
5089 for vca
in deployed_vca
:
5092 if member_vnf_index
!= vca
["member-vnf-index"] or vdu_id
!= vca
["vdu_id"]:
5095 vdu_count_index
is not None
5096 and vdu_count_index
!= vca
["vdu_count_index"]
5099 if kdu_name
and kdu_name
!= vca
["kdu_name"]:
5101 if ee_descriptor_id
and ee_descriptor_id
!= vca
["ee_descriptor_id"]:
5105 # vca_deployed not found
5107 "charm for member_vnf_index={} vdu_id={}.{} kdu_name={} execution-environment-list.id={}"
5108 " is not deployed".format(
5117 ee_id
= vca
.get("ee_id")
5119 "type", "lxc_proxy_charm"
5120 ) # default value for backward compatibility - proxy charm
5123 "charm for member_vnf_index={} vdu_id={} kdu_name={} vdu_count_index={} has not "
5124 "execution environment".format(
5125 member_vnf_index
, vdu_id
, kdu_name
, vdu_count_index
5128 return ee_id
, vca_type
5130 async def _ns_execute_primitive(
5136 retries_interval
=30,
5143 if primitive
== "config":
5144 primitive_params
= {"params": primitive_params
}
5146 vca_type
= vca_type
or "lxc_proxy_charm"
5150 output
= await asyncio
.wait_for(
5151 self
.vca_map
[vca_type
].exec_primitive(
5153 primitive_name
=primitive
,
5154 params_dict
=primitive_params
,
5155 progress_timeout
=self
.timeout
.progress_primitive
,
5156 total_timeout
=self
.timeout
.primitive
,
5161 timeout
=timeout
or self
.timeout
.primitive
,
5165 except asyncio
.CancelledError
:
5167 except Exception as e
:
5171 "Error executing action {} on {} -> {}".format(
5176 await asyncio
.sleep(retries_interval
)
5178 if isinstance(e
, asyncio
.TimeoutError
):
5180 message
="Timed out waiting for action to complete"
5182 return "FAILED", getattr(e
, "message", repr(e
))
5184 return "COMPLETED", output
5186 except (LcmException
, asyncio
.CancelledError
):
5188 except Exception as e
:
5189 return "FAIL", "Error executing action {}: {}".format(primitive
, e
)
5191 async def vca_status_refresh(self
, nsr_id
, nslcmop_id
):
5193 Updating the vca_status with latest juju information in nsrs record
5194 :param: nsr_id: Id of the nsr
5195 :param: nslcmop_id: Id of the nslcmop
5199 self
.logger
.debug("Task ns={} action={} Enter".format(nsr_id
, nslcmop_id
))
5200 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
5201 vca_id
= self
.get_vca_id({}, db_nsr
)
5202 if db_nsr
["_admin"]["deployed"]["K8s"]:
5203 for _
, k8s
in enumerate(db_nsr
["_admin"]["deployed"]["K8s"]):
5204 cluster_uuid
, kdu_instance
, cluster_type
= (
5205 k8s
["k8scluster-uuid"],
5206 k8s
["kdu-instance"],
5207 k8s
["k8scluster-type"],
5209 await self
._on
_update
_k
8s
_db
(
5210 cluster_uuid
=cluster_uuid
,
5211 kdu_instance
=kdu_instance
,
5212 filter={"_id": nsr_id
},
5214 cluster_type
=cluster_type
,
5217 for vca_index
, _
in enumerate(db_nsr
["_admin"]["deployed"]["VCA"]):
5218 table
, filter = "nsrs", {"_id": nsr_id
}
5219 path
= "_admin.deployed.VCA.{}.".format(vca_index
)
5220 await self
._on
_update
_n
2vc
_db
(table
, filter, path
, {})
5222 self
.logger
.debug("Task ns={} action={} Exit".format(nsr_id
, nslcmop_id
))
5223 self
.lcm_tasks
.remove("ns", nsr_id
, nslcmop_id
, "ns_vca_status_refresh")
5225 async def action(self
, nsr_id
, nslcmop_id
):
5226 # Try to lock HA task here
5227 task_is_locked_by_me
= self
.lcm_tasks
.lock_HA("ns", "nslcmops", nslcmop_id
)
5228 if not task_is_locked_by_me
:
5231 logging_text
= "Task ns={} action={} ".format(nsr_id
, nslcmop_id
)
5232 self
.logger
.debug(logging_text
+ "Enter")
5233 # get all needed from database
5237 db_nslcmop_update
= {}
5238 nslcmop_operation_state
= None
5239 error_description_nslcmop
= None
5243 # wait for any previous tasks in process
5244 step
= "Waiting for previous operations to terminate"
5245 await self
.lcm_tasks
.waitfor_related_HA("ns", "nslcmops", nslcmop_id
)
5247 self
._write
_ns
_status
(
5250 current_operation
="RUNNING ACTION",
5251 current_operation_id
=nslcmop_id
,
5254 step
= "Getting information from database"
5255 db_nslcmop
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
})
5256 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
5257 if db_nslcmop
["operationParams"].get("primitive_params"):
5258 db_nslcmop
["operationParams"]["primitive_params"] = json
.loads(
5259 db_nslcmop
["operationParams"]["primitive_params"]
5262 nsr_deployed
= db_nsr
["_admin"].get("deployed")
5263 vnf_index
= db_nslcmop
["operationParams"].get("member_vnf_index")
5264 vdu_id
= db_nslcmop
["operationParams"].get("vdu_id")
5265 kdu_name
= db_nslcmop
["operationParams"].get("kdu_name")
5266 vdu_count_index
= db_nslcmop
["operationParams"].get("vdu_count_index")
5267 primitive
= db_nslcmop
["operationParams"]["primitive"]
5268 primitive_params
= db_nslcmop
["operationParams"]["primitive_params"]
5269 timeout_ns_action
= db_nslcmop
["operationParams"].get(
5270 "timeout_ns_action", self
.timeout
.primitive
5274 step
= "Getting vnfr from database"
5275 db_vnfr
= self
.db
.get_one(
5276 "vnfrs", {"member-vnf-index-ref": vnf_index
, "nsr-id-ref": nsr_id
}
5278 if db_vnfr
.get("kdur"):
5280 for kdur
in db_vnfr
["kdur"]:
5281 if kdur
.get("additionalParams"):
5282 kdur
["additionalParams"] = json
.loads(
5283 kdur
["additionalParams"]
5285 kdur_list
.append(kdur
)
5286 db_vnfr
["kdur"] = kdur_list
5287 step
= "Getting vnfd from database"
5288 db_vnfd
= self
.db
.get_one("vnfds", {"_id": db_vnfr
["vnfd-id"]})
5290 # Sync filesystem before running a primitive
5291 self
.fs
.sync(db_vnfr
["vnfd-id"])
5293 step
= "Getting nsd from database"
5294 db_nsd
= self
.db
.get_one("nsds", {"_id": db_nsr
["nsd-id"]})
5296 vca_id
= self
.get_vca_id(db_vnfr
, db_nsr
)
5297 # for backward compatibility
5298 if nsr_deployed
and isinstance(nsr_deployed
.get("VCA"), dict):
5299 nsr_deployed
["VCA"] = list(nsr_deployed
["VCA"].values())
5300 db_nsr_update
["_admin.deployed.VCA"] = nsr_deployed
["VCA"]
5301 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
5303 # look for primitive
5304 config_primitive_desc
= descriptor_configuration
= None
5306 descriptor_configuration
= get_configuration(db_vnfd
, vdu_id
)
5308 descriptor_configuration
= get_configuration(db_vnfd
, kdu_name
)
5310 descriptor_configuration
= get_configuration(db_vnfd
, db_vnfd
["id"])
5312 descriptor_configuration
= db_nsd
.get("ns-configuration")
5314 if descriptor_configuration
and descriptor_configuration
.get(
5317 for config_primitive
in descriptor_configuration
["config-primitive"]:
5318 if config_primitive
["name"] == primitive
:
5319 config_primitive_desc
= config_primitive
5322 if not config_primitive_desc
:
5323 if not (kdu_name
and primitive
in ("upgrade", "rollback", "status")):
5325 "Primitive {} not found at [ns|vnf|vdu]-configuration:config-primitive ".format(
5329 primitive_name
= primitive
5330 ee_descriptor_id
= None
5332 primitive_name
= config_primitive_desc
.get(
5333 "execution-environment-primitive", primitive
5335 ee_descriptor_id
= config_primitive_desc
.get(
5336 "execution-environment-ref"
5342 (x
for x
in db_vnfr
["vdur"] if x
["vdu-id-ref"] == vdu_id
), None
5344 desc_params
= parse_yaml_strings(vdur
.get("additionalParams"))
5347 (x
for x
in db_vnfr
["kdur"] if x
["kdu-name"] == kdu_name
), None
5349 desc_params
= parse_yaml_strings(kdur
.get("additionalParams"))
5351 desc_params
= parse_yaml_strings(
5352 db_vnfr
.get("additionalParamsForVnf")
5355 desc_params
= parse_yaml_strings(db_nsr
.get("additionalParamsForNs"))
5356 if kdu_name
and get_configuration(db_vnfd
, kdu_name
):
5357 kdu_configuration
= get_configuration(db_vnfd
, kdu_name
)
5359 for primitive
in kdu_configuration
.get("initial-config-primitive", []):
5360 actions
.add(primitive
["name"])
5361 for primitive
in kdu_configuration
.get("config-primitive", []):
5362 actions
.add(primitive
["name"])
5364 nsr_deployed
["K8s"],
5365 lambda kdu
: kdu_name
== kdu
["kdu-name"]
5366 and kdu
["member-vnf-index"] == vnf_index
,
5370 if primitive_name
in actions
5371 and kdu
["k8scluster-type"] != "helm-chart-v3"
5375 # TODO check if ns is in a proper status
5377 primitive_name
in ("upgrade", "rollback", "status") or kdu_action
5379 # kdur and desc_params already set from before
5380 if primitive_params
:
5381 desc_params
.update(primitive_params
)
5382 # TODO Check if we will need something at vnf level
5383 for index
, kdu
in enumerate(get_iterable(nsr_deployed
, "K8s")):
5385 kdu_name
== kdu
["kdu-name"]
5386 and kdu
["member-vnf-index"] == vnf_index
5391 "KDU '{}' for vnf '{}' not deployed".format(kdu_name
, vnf_index
)
5394 if kdu
.get("k8scluster-type") not in self
.k8scluster_map
:
5395 msg
= "unknown k8scluster-type '{}'".format(
5396 kdu
.get("k8scluster-type")
5398 raise LcmException(msg
)
5401 "collection": "nsrs",
5402 "filter": {"_id": nsr_id
},
5403 "path": "_admin.deployed.K8s.{}".format(index
),
5407 + "Exec k8s {} on {}.{}".format(primitive_name
, vnf_index
, kdu_name
)
5409 step
= "Executing kdu {}".format(primitive_name
)
5410 if primitive_name
== "upgrade":
5411 if desc_params
.get("kdu_model"):
5412 kdu_model
= desc_params
.get("kdu_model")
5413 del desc_params
["kdu_model"]
5415 kdu_model
= kdu
.get("kdu-model")
5416 if kdu_model
.count("/") < 2: # helm chart is not embedded
5417 parts
= kdu_model
.split(sep
=":")
5419 kdu_model
= parts
[0]
5420 if desc_params
.get("kdu_atomic_upgrade"):
5421 atomic_upgrade
= desc_params
.get(
5422 "kdu_atomic_upgrade"
5423 ).lower() in ("yes", "true", "1")
5424 del desc_params
["kdu_atomic_upgrade"]
5426 atomic_upgrade
= True
5428 detailed_status
= await asyncio
.wait_for(
5429 self
.k8scluster_map
[kdu
["k8scluster-type"]].upgrade(
5430 cluster_uuid
=kdu
.get("k8scluster-uuid"),
5431 kdu_instance
=kdu
.get("kdu-instance"),
5432 atomic
=atomic_upgrade
,
5433 kdu_model
=kdu_model
,
5436 timeout
=timeout_ns_action
,
5438 timeout
=timeout_ns_action
+ 10,
5441 logging_text
+ " Upgrade of kdu {} done".format(detailed_status
)
5443 elif primitive_name
== "rollback":
5444 detailed_status
= await asyncio
.wait_for(
5445 self
.k8scluster_map
[kdu
["k8scluster-type"]].rollback(
5446 cluster_uuid
=kdu
.get("k8scluster-uuid"),
5447 kdu_instance
=kdu
.get("kdu-instance"),
5450 timeout
=timeout_ns_action
,
5452 elif primitive_name
== "status":
5453 detailed_status
= await asyncio
.wait_for(
5454 self
.k8scluster_map
[kdu
["k8scluster-type"]].status_kdu(
5455 cluster_uuid
=kdu
.get("k8scluster-uuid"),
5456 kdu_instance
=kdu
.get("kdu-instance"),
5459 timeout
=timeout_ns_action
,
5462 kdu_instance
= kdu
.get("kdu-instance") or "{}-{}".format(
5463 kdu
["kdu-name"], nsr_id
5465 params
= self
._map
_primitive
_params
(
5466 config_primitive_desc
, primitive_params
, desc_params
5469 detailed_status
= await asyncio
.wait_for(
5470 self
.k8scluster_map
[kdu
["k8scluster-type"]].exec_primitive(
5471 cluster_uuid
=kdu
.get("k8scluster-uuid"),
5472 kdu_instance
=kdu_instance
,
5473 primitive_name
=primitive_name
,
5476 timeout
=timeout_ns_action
,
5479 timeout
=timeout_ns_action
,
5483 nslcmop_operation_state
= "COMPLETED"
5485 detailed_status
= ""
5486 nslcmop_operation_state
= "FAILED"
5488 ee_id
, vca_type
= self
._look
_for
_deployed
_vca
(
5489 nsr_deployed
["VCA"],
5490 member_vnf_index
=vnf_index
,
5492 vdu_count_index
=vdu_count_index
,
5493 ee_descriptor_id
=ee_descriptor_id
,
5495 for vca_index
, vca_deployed
in enumerate(
5496 db_nsr
["_admin"]["deployed"]["VCA"]
5498 if vca_deployed
.get("member-vnf-index") == vnf_index
:
5500 "collection": "nsrs",
5501 "filter": {"_id": nsr_id
},
5502 "path": "_admin.deployed.VCA.{}.".format(vca_index
),
5506 nslcmop_operation_state
,
5508 ) = await self
._ns
_execute
_primitive
(
5510 primitive
=primitive_name
,
5511 primitive_params
=self
._map
_primitive
_params
(
5512 config_primitive_desc
, primitive_params
, desc_params
5514 timeout
=timeout_ns_action
,
5520 db_nslcmop_update
["detailed-status"] = detailed_status
5521 error_description_nslcmop
= (
5522 detailed_status
if nslcmop_operation_state
== "FAILED" else ""
5526 + "Done with result {} {}".format(
5527 nslcmop_operation_state
, detailed_status
5530 return # database update is called inside finally
5532 except (DbException
, LcmException
, N2VCException
, K8sException
) as e
:
5533 self
.logger
.error(logging_text
+ "Exit Exception {}".format(e
))
5535 except asyncio
.CancelledError
:
5537 logging_text
+ "Cancelled Exception while '{}'".format(step
)
5539 exc
= "Operation was cancelled"
5540 except asyncio
.TimeoutError
:
5541 self
.logger
.error(logging_text
+ "Timeout while '{}'".format(step
))
5543 except Exception as e
:
5544 exc
= traceback
.format_exc()
5545 self
.logger
.critical(
5546 logging_text
+ "Exit Exception {} {}".format(type(e
).__name
__, e
),
5555 ) = error_description_nslcmop
= "FAILED {}: {}".format(step
, exc
)
5556 nslcmop_operation_state
= "FAILED"
5558 self
._write
_ns
_status
(
5562 ], # TODO check if degraded. For the moment use previous status
5563 current_operation
="IDLE",
5564 current_operation_id
=None,
5565 # error_description=error_description_nsr,
5566 # error_detail=error_detail,
5567 other_update
=db_nsr_update
,
5570 self
._write
_op
_status
(
5573 error_message
=error_description_nslcmop
,
5574 operation_state
=nslcmop_operation_state
,
5575 other_update
=db_nslcmop_update
,
5578 if nslcmop_operation_state
:
5580 await self
.msg
.aiowrite(
5585 "nslcmop_id": nslcmop_id
,
5586 "operationState": nslcmop_operation_state
,
5589 except Exception as e
:
5591 logging_text
+ "kafka_write notification Exception {}".format(e
)
5593 self
.logger
.debug(logging_text
+ "Exit")
5594 self
.lcm_tasks
.remove("ns", nsr_id
, nslcmop_id
, "ns_action")
5595 return nslcmop_operation_state
, detailed_status
5597 async def terminate_vdus(
5598 self
, db_vnfr
, member_vnf_index
, db_nsr
, update_db_nslcmops
, stage
, logging_text
5600 """This method terminates VDUs
5603 db_vnfr: VNF instance record
5604 member_vnf_index: VNF index to identify the VDUs to be removed
5605 db_nsr: NS instance record
5606 update_db_nslcmops: Nslcmop update record
5608 vca_scaling_info
= []
5609 scaling_info
= {"scaling_group_name": "vdu_autoscale", "vdu": [], "kdu": []}
5610 scaling_info
["scaling_direction"] = "IN"
5611 scaling_info
["vdu-delete"] = {}
5612 scaling_info
["kdu-delete"] = {}
5613 db_vdur
= db_vnfr
.get("vdur")
5614 vdur_list
= copy(db_vdur
)
5616 for index
, vdu
in enumerate(vdur_list
):
5617 vca_scaling_info
.append(
5619 "osm_vdu_id": vdu
["vdu-id-ref"],
5620 "member-vnf-index": member_vnf_index
,
5622 "vdu_index": count_index
,
5625 scaling_info
["vdu-delete"][vdu
["vdu-id-ref"]] = count_index
5626 scaling_info
["vdu"].append(
5628 "name": vdu
.get("name") or vdu
.get("vdu-name"),
5629 "vdu_id": vdu
["vdu-id-ref"],
5633 for interface
in vdu
["interfaces"]:
5634 scaling_info
["vdu"][index
]["interface"].append(
5636 "name": interface
["name"],
5637 "ip_address": interface
["ip-address"],
5638 "mac_address": interface
.get("mac-address"),
5641 self
.logger
.info("NS update scaling info{}".format(scaling_info
))
5642 stage
[2] = "Terminating VDUs"
5643 if scaling_info
.get("vdu-delete"):
5644 # scale_process = "RO"
5645 if self
.ro_config
.ng
:
5646 await self
._scale
_ng
_ro
(
5655 async def remove_vnf(self
, nsr_id
, nslcmop_id
, vnf_instance_id
):
5656 """This method is to Remove VNF instances from NS.
5659 nsr_id: NS instance id
5660 nslcmop_id: nslcmop id of update
5661 vnf_instance_id: id of the VNF instance to be removed
5664 result: (str, str) COMPLETED/FAILED, details
5668 logging_text
= "Task ns={} update ".format(nsr_id
)
5669 check_vnfr_count
= len(self
.db
.get_list("vnfrs", {"nsr-id-ref": nsr_id
}))
5670 self
.logger
.info("check_vnfr_count {}".format(check_vnfr_count
))
5671 if check_vnfr_count
> 1:
5672 stage
= ["", "", ""]
5673 step
= "Getting nslcmop from database"
5675 step
+ " after having waited for previous tasks to be completed"
5677 # db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
5678 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
5679 db_vnfr
= self
.db
.get_one("vnfrs", {"_id": vnf_instance_id
})
5680 member_vnf_index
= db_vnfr
["member-vnf-index-ref"]
5681 """ db_vnfr = self.db.get_one(
5682 "vnfrs", {"member-vnf-index-ref": member_vnf_index, "nsr-id-ref": nsr_id}) """
5684 update_db_nslcmops
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
})
5685 await self
.terminate_vdus(
5694 constituent_vnfr
= db_nsr
.get("constituent-vnfr-ref")
5695 constituent_vnfr
.remove(db_vnfr
.get("_id"))
5696 db_nsr_update
["constituent-vnfr-ref"] = db_nsr
.get(
5697 "constituent-vnfr-ref"
5699 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
5700 self
.db
.del_one("vnfrs", {"_id": db_vnfr
.get("_id")})
5701 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
5702 return "COMPLETED", "Done"
5704 step
= "Terminate VNF Failed with"
5706 "{} Cannot terminate the last VNF in this NS.".format(
5710 except (LcmException
, asyncio
.CancelledError
):
5712 except Exception as e
:
5713 self
.logger
.debug("Error removing VNF {}".format(e
))
5714 return "FAILED", "Error removing VNF {}".format(e
)
5716 async def _ns_redeploy_vnf(
5724 """This method updates and redeploys VNF instances
5727 nsr_id: NS instance id
5728 nslcmop_id: nslcmop id
5729 db_vnfd: VNF descriptor
5730 db_vnfr: VNF instance record
5731 db_nsr: NS instance record
5734 result: (str, str) COMPLETED/FAILED, details
5738 stage
= ["", "", ""]
5739 logging_text
= "Task ns={} update ".format(nsr_id
)
5740 latest_vnfd_revision
= db_vnfd
["_admin"].get("revision")
5741 member_vnf_index
= db_vnfr
["member-vnf-index-ref"]
5743 # Terminate old VNF resources
5744 update_db_nslcmops
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
})
5745 await self
.terminate_vdus(
5754 # old_vnfd_id = db_vnfr["vnfd-id"]
5755 # new_db_vnfd = self.db.get_one("vnfds", {"_id": vnfd_id})
5756 new_db_vnfd
= db_vnfd
5757 # new_vnfd_ref = new_db_vnfd["id"]
5758 # new_vnfd_id = vnfd_id
5762 for cp
in new_db_vnfd
.get("ext-cpd", ()):
5764 "name": cp
.get("id"),
5765 "connection-point-id": cp
.get("int-cpd", {}).get("cpd"),
5766 "connection-point-vdu-id": cp
.get("int-cpd", {}).get("vdu-id"),
5769 new_vnfr_cp
.append(vnf_cp
)
5770 new_vdur
= update_db_nslcmops
["operationParams"]["newVdur"]
5771 # new_vdur = self._create_vdur_descriptor_from_vnfd(db_nsd, db_vnfd, old_db_vnfd, vnfd_id, db_nsr, member_vnf_index)
5772 # new_vnfr_update = {"vnfd-ref": new_vnfd_ref, "vnfd-id": new_vnfd_id, "connection-point": new_vnfr_cp, "vdur": new_vdur, "ip-address": ""}
5774 "revision": latest_vnfd_revision
,
5775 "connection-point": new_vnfr_cp
,
5779 self
.update_db_2("vnfrs", db_vnfr
["_id"], new_vnfr_update
)
5780 updated_db_vnfr
= self
.db
.get_one(
5782 {"member-vnf-index-ref": member_vnf_index
, "nsr-id-ref": nsr_id
},
5785 # Instantiate new VNF resources
5786 # update_db_nslcmops = self.db.get_one("nslcmops", {"_id": nslcmop_id})
5787 vca_scaling_info
= []
5788 scaling_info
= {"scaling_group_name": "vdu_autoscale", "vdu": [], "kdu": []}
5789 scaling_info
["scaling_direction"] = "OUT"
5790 scaling_info
["vdu-create"] = {}
5791 scaling_info
["kdu-create"] = {}
5792 vdud_instantiate_list
= db_vnfd
["vdu"]
5793 for index
, vdud
in enumerate(vdud_instantiate_list
):
5794 cloud_init_text
= self
._get
_vdu
_cloud
_init
_content
(vdud
, db_vnfd
)
5796 additional_params
= (
5797 self
._get
_vdu
_additional
_params
(updated_db_vnfr
, vdud
["id"])
5800 cloud_init_list
= []
5802 # TODO Information of its own ip is not available because db_vnfr is not updated.
5803 additional_params
["OSM"] = get_osm_params(
5804 updated_db_vnfr
, vdud
["id"], 1
5806 cloud_init_list
.append(
5807 self
._parse
_cloud
_init
(
5814 vca_scaling_info
.append(
5816 "osm_vdu_id": vdud
["id"],
5817 "member-vnf-index": member_vnf_index
,
5819 "vdu_index": count_index
,
5822 scaling_info
["vdu-create"][vdud
["id"]] = count_index
5823 if self
.ro_config
.ng
:
5825 "New Resources to be deployed: {}".format(scaling_info
)
5827 await self
._scale
_ng
_ro
(
5835 return "COMPLETED", "Done"
5836 except (LcmException
, asyncio
.CancelledError
):
5838 except Exception as e
:
5839 self
.logger
.debug("Error updating VNF {}".format(e
))
5840 return "FAILED", "Error updating VNF {}".format(e
)
5842 async def _ns_charm_upgrade(
5848 timeout
: float = None,
5850 """This method upgrade charms in VNF instances
5853 ee_id: Execution environment id
5854 path: Local path to the charm
5856 charm_type: Charm type can be lxc-proxy-charm, native-charm or k8s-proxy-charm
5857 timeout: (Float) Timeout for the ns update operation
5860 result: (str, str) COMPLETED/FAILED, details
5863 charm_type
= charm_type
or "lxc_proxy_charm"
5864 output
= await self
.vca_map
[charm_type
].upgrade_charm(
5868 charm_type
=charm_type
,
5869 timeout
=timeout
or self
.timeout
.ns_update
,
5873 return "COMPLETED", output
5875 except (LcmException
, asyncio
.CancelledError
):
5878 except Exception as e
:
5879 self
.logger
.debug("Error upgrading charm {}".format(path
))
5881 return "FAILED", "Error upgrading charm {}: {}".format(path
, e
)
5883 async def update(self
, nsr_id
, nslcmop_id
):
5884 """Update NS according to different update types
5886 This method performs upgrade of VNF instances then updates the revision
5887 number in VNF record
5890 nsr_id: Network service will be updated
5891 nslcmop_id: ns lcm operation id
5894 It may raise DbException, LcmException, N2VCException, K8sException
5897 # Try to lock HA task here
5898 task_is_locked_by_me
= self
.lcm_tasks
.lock_HA("ns", "nslcmops", nslcmop_id
)
5899 if not task_is_locked_by_me
:
5902 logging_text
= "Task ns={} update={} ".format(nsr_id
, nslcmop_id
)
5903 self
.logger
.debug(logging_text
+ "Enter")
5905 # Set the required variables to be filled up later
5907 db_nslcmop_update
= {}
5909 nslcmop_operation_state
= None
5911 error_description_nslcmop
= ""
5913 change_type
= "updated"
5914 detailed_status
= ""
5915 member_vnf_index
= None
5918 # wait for any previous tasks in process
5919 step
= "Waiting for previous operations to terminate"
5920 await self
.lcm_tasks
.waitfor_related_HA("ns", "nslcmops", nslcmop_id
)
5921 self
._write
_ns
_status
(
5924 current_operation
="UPDATING",
5925 current_operation_id
=nslcmop_id
,
5928 step
= "Getting nslcmop from database"
5929 db_nslcmop
= self
.db
.get_one(
5930 "nslcmops", {"_id": nslcmop_id
}, fail_on_empty
=False
5932 update_type
= db_nslcmop
["operationParams"]["updateType"]
5934 step
= "Getting nsr from database"
5935 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
5936 old_operational_status
= db_nsr
["operational-status"]
5937 db_nsr_update
["operational-status"] = "updating"
5938 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
5939 nsr_deployed
= db_nsr
["_admin"].get("deployed")
5941 if update_type
== "CHANGE_VNFPKG":
5942 # Get the input parameters given through update request
5943 vnf_instance_id
= db_nslcmop
["operationParams"][
5944 "changeVnfPackageData"
5945 ].get("vnfInstanceId")
5947 vnfd_id
= db_nslcmop
["operationParams"]["changeVnfPackageData"].get(
5950 timeout_seconds
= db_nslcmop
["operationParams"].get("timeout_ns_update")
5952 step
= "Getting vnfr from database"
5953 db_vnfr
= self
.db
.get_one(
5954 "vnfrs", {"_id": vnf_instance_id
}, fail_on_empty
=False
5957 step
= "Getting vnfds from database"
5959 latest_vnfd
= self
.db
.get_one(
5960 "vnfds", {"_id": vnfd_id
}, fail_on_empty
=False
5962 latest_vnfd_revision
= latest_vnfd
["_admin"].get("revision")
5965 current_vnf_revision
= db_vnfr
.get("revision", 1)
5966 current_vnfd
= self
.db
.get_one(
5968 {"_id": vnfd_id
+ ":" + str(current_vnf_revision
)},
5969 fail_on_empty
=False,
5971 # Charm artifact paths will be filled up later
5973 current_charm_artifact_path
,
5974 target_charm_artifact_path
,
5975 charm_artifact_paths
,
5977 ) = ([], [], [], [])
5979 step
= "Checking if revision has changed in VNFD"
5980 if current_vnf_revision
!= latest_vnfd_revision
:
5981 change_type
= "policy_updated"
5983 # There is new revision of VNFD, update operation is required
5984 current_vnfd_path
= vnfd_id
+ ":" + str(current_vnf_revision
)
5985 latest_vnfd_path
= vnfd_id
+ ":" + str(latest_vnfd_revision
)
5987 step
= "Removing the VNFD packages if they exist in the local path"
5988 shutil
.rmtree(self
.fs
.path
+ current_vnfd_path
, ignore_errors
=True)
5989 shutil
.rmtree(self
.fs
.path
+ latest_vnfd_path
, ignore_errors
=True)
5991 step
= "Get the VNFD packages from FSMongo"
5992 self
.fs
.sync(from_path
=latest_vnfd_path
)
5993 self
.fs
.sync(from_path
=current_vnfd_path
)
5996 "Get the charm-type, charm-id, ee-id if there is deployed VCA"
5998 current_base_folder
= current_vnfd
["_admin"]["storage"]
5999 latest_base_folder
= latest_vnfd
["_admin"]["storage"]
6001 for vca_index
, vca_deployed
in enumerate(
6002 get_iterable(nsr_deployed
, "VCA")
6004 vnf_index
= db_vnfr
.get("member-vnf-index-ref")
6006 # Getting charm-id and charm-type
6007 if vca_deployed
.get("member-vnf-index") == vnf_index
:
6008 vca_id
= self
.get_vca_id(db_vnfr
, db_nsr
)
6009 vca_type
= vca_deployed
.get("type")
6010 vdu_count_index
= vca_deployed
.get("vdu_count_index")
6013 ee_id
= vca_deployed
.get("ee_id")
6015 step
= "Getting descriptor config"
6016 if current_vnfd
.get("kdu"):
6017 search_key
= "kdu_name"
6019 search_key
= "vnfd_id"
6021 entity_id
= vca_deployed
.get(search_key
)
6023 descriptor_config
= get_configuration(
6024 current_vnfd
, entity_id
6027 if "execution-environment-list" in descriptor_config
:
6028 ee_list
= descriptor_config
.get(
6029 "execution-environment-list", []
6034 # There could be several charm used in the same VNF
6035 for ee_item
in ee_list
:
6036 if ee_item
.get("juju"):
6037 step
= "Getting charm name"
6038 charm_name
= ee_item
["juju"].get("charm")
6040 step
= "Setting Charm artifact paths"
6041 current_charm_artifact_path
.append(
6042 get_charm_artifact_path(
6043 current_base_folder
,
6046 current_vnf_revision
,
6049 target_charm_artifact_path
.append(
6050 get_charm_artifact_path(
6054 latest_vnfd_revision
,
6057 elif ee_item
.get("helm-chart"):
6058 # add chart to list and all parameters
6059 step
= "Getting helm chart name"
6060 chart_name
= ee_item
.get("helm-chart")
6061 vca_type
= "helm-v3"
6062 step
= "Setting Helm chart artifact paths"
6064 helm_artifacts
.append(
6066 "current_artifact_path": get_charm_artifact_path(
6067 current_base_folder
,
6070 current_vnf_revision
,
6072 "target_artifact_path": get_charm_artifact_path(
6076 latest_vnfd_revision
,
6079 "vca_index": vca_index
,
6080 "vdu_index": vdu_count_index
,
6084 charm_artifact_paths
= zip(
6085 current_charm_artifact_path
, target_charm_artifact_path
6088 step
= "Checking if software version has changed in VNFD"
6089 if find_software_version(current_vnfd
) != find_software_version(
6092 step
= "Checking if existing VNF has charm"
6093 for current_charm_path
, target_charm_path
in list(
6094 charm_artifact_paths
6096 if current_charm_path
:
6098 "Software version change is not supported as VNF instance {} has charm.".format(
6103 # There is no change in the charm package, then redeploy the VNF
6104 # based on new descriptor
6105 step
= "Redeploying VNF"
6106 member_vnf_index
= db_vnfr
["member-vnf-index-ref"]
6107 (result
, detailed_status
) = await self
._ns
_redeploy
_vnf
(
6108 nsr_id
, nslcmop_id
, latest_vnfd
, db_vnfr
, db_nsr
6110 if result
== "FAILED":
6111 nslcmop_operation_state
= result
6112 error_description_nslcmop
= detailed_status
6113 db_nslcmop_update
["detailed-status"] = detailed_status
6116 + " step {} Done with result {} {}".format(
6117 step
, nslcmop_operation_state
, detailed_status
6122 step
= "Checking if any charm package has changed or not"
6123 for current_charm_path
, target_charm_path
in list(
6124 charm_artifact_paths
6128 and target_charm_path
6129 and self
.check_charm_hash_changed(
6130 current_charm_path
, target_charm_path
6133 step
= "Checking whether VNF uses juju bundle"
6134 if check_juju_bundle_existence(current_vnfd
):
6136 "Charm upgrade is not supported for the instance which"
6137 " uses juju-bundle: {}".format(
6138 check_juju_bundle_existence(current_vnfd
)
6142 step
= "Upgrading Charm"
6146 ) = await self
._ns
_charm
_upgrade
(
6149 charm_type
=vca_type
,
6150 path
=self
.fs
.path
+ target_charm_path
,
6151 timeout
=timeout_seconds
,
6154 if result
== "FAILED":
6155 nslcmop_operation_state
= result
6156 error_description_nslcmop
= detailed_status
6158 db_nslcmop_update
["detailed-status"] = detailed_status
6161 + " step {} Done with result {} {}".format(
6162 step
, nslcmop_operation_state
, detailed_status
6166 step
= "Updating policies"
6167 member_vnf_index
= db_vnfr
["member-vnf-index-ref"]
6168 result
= "COMPLETED"
6169 detailed_status
= "Done"
6170 db_nslcmop_update
["detailed-status"] = "Done"
6173 for item
in helm_artifacts
:
6175 item
["current_artifact_path"]
6176 and item
["target_artifact_path"]
6177 and self
.check_charm_hash_changed(
6178 item
["current_artifact_path"],
6179 item
["target_artifact_path"],
6183 db_update_entry
= "_admin.deployed.VCA.{}.".format(
6186 vnfr_id
= db_vnfr
["_id"]
6187 osm_config
= {"osm": {"ns_id": nsr_id
, "vnf_id": vnfr_id
}}
6189 "collection": "nsrs",
6190 "filter": {"_id": nsr_id
},
6191 "path": db_update_entry
,
6193 vca_type
, namespace
, helm_id
= get_ee_id_parts(item
["ee_id"])
6194 await self
.vca_map
[vca_type
].upgrade_execution_environment(
6195 namespace
=namespace
,
6199 artifact_path
=item
["target_artifact_path"],
6202 vnf_id
= db_vnfr
.get("vnfd-ref")
6203 config_descriptor
= get_configuration(latest_vnfd
, vnf_id
)
6204 self
.logger
.debug("get ssh key block")
6208 ("config-access", "ssh-access", "required"),
6210 # Needed to inject a ssh key
6213 ("config-access", "ssh-access", "default-user"),
6216 "Install configuration Software, getting public ssh key"
6218 pub_key
= await self
.vca_map
[
6220 ].get_ee_ssh_public__key(
6221 ee_id
=ee_id
, db_dict
=db_dict
, vca_id
=vca_id
6225 "Insert public key into VM user={} ssh_key={}".format(
6229 self
.logger
.debug(logging_text
+ step
)
6231 # wait for RO (ip-address) Insert pub_key into VM
6232 rw_mgmt_ip
= await self
.wait_vm_up_insert_key_ro(
6242 initial_config_primitive_list
= config_descriptor
.get(
6243 "initial-config-primitive"
6245 config_primitive
= next(
6248 for p
in initial_config_primitive_list
6249 if p
["name"] == "config"
6253 if not config_primitive
:
6256 deploy_params
= {"OSM": get_osm_params(db_vnfr
)}
6258 deploy_params
["rw_mgmt_ip"] = rw_mgmt_ip
6259 if db_vnfr
.get("additionalParamsForVnf"):
6260 deploy_params
.update(
6262 db_vnfr
["additionalParamsForVnf"].copy()
6265 primitive_params_
= self
._map
_primitive
_params
(
6266 config_primitive
, {}, deploy_params
6269 step
= "execute primitive '{}' params '{}'".format(
6270 config_primitive
["name"], primitive_params_
6272 self
.logger
.debug(logging_text
+ step
)
6273 await self
.vca_map
[vca_type
].exec_primitive(
6275 primitive_name
=config_primitive
["name"],
6276 params_dict
=primitive_params_
,
6282 step
= "Updating policies"
6283 member_vnf_index
= db_vnfr
["member-vnf-index-ref"]
6284 detailed_status
= "Done"
6285 db_nslcmop_update
["detailed-status"] = "Done"
6287 # If nslcmop_operation_state is None, so any operation is not failed.
6288 if not nslcmop_operation_state
:
6289 nslcmop_operation_state
= "COMPLETED"
6291 # If update CHANGE_VNFPKG nslcmop_operation is successful
6292 # vnf revision need to be updated
6293 vnfr_update
["revision"] = latest_vnfd_revision
6294 self
.update_db_2("vnfrs", db_vnfr
["_id"], vnfr_update
)
6298 + " task Done with result {} {}".format(
6299 nslcmop_operation_state
, detailed_status
6302 elif update_type
== "REMOVE_VNF":
6303 # This part is included in https://osm.etsi.org/gerrit/11876
6304 vnf_instance_id
= db_nslcmop
["operationParams"]["removeVnfInstanceId"]
6305 db_vnfr
= self
.db
.get_one("vnfrs", {"_id": vnf_instance_id
})
6306 member_vnf_index
= db_vnfr
["member-vnf-index-ref"]
6307 step
= "Removing VNF"
6308 (result
, detailed_status
) = await self
.remove_vnf(
6309 nsr_id
, nslcmop_id
, vnf_instance_id
6311 if result
== "FAILED":
6312 nslcmop_operation_state
= result
6313 error_description_nslcmop
= detailed_status
6314 db_nslcmop_update
["detailed-status"] = detailed_status
6315 change_type
= "vnf_terminated"
6316 if not nslcmop_operation_state
:
6317 nslcmop_operation_state
= "COMPLETED"
6320 + " task Done with result {} {}".format(
6321 nslcmop_operation_state
, detailed_status
6325 elif update_type
== "OPERATE_VNF":
6326 vnf_id
= db_nslcmop
["operationParams"]["operateVnfData"][
6329 operation_type
= db_nslcmop
["operationParams"]["operateVnfData"][
6332 additional_param
= db_nslcmop
["operationParams"]["operateVnfData"][
6335 (result
, detailed_status
) = await self
.rebuild_start_stop(
6336 nsr_id
, nslcmop_id
, vnf_id
, additional_param
, operation_type
6338 if result
== "FAILED":
6339 nslcmop_operation_state
= result
6340 error_description_nslcmop
= detailed_status
6341 db_nslcmop_update
["detailed-status"] = detailed_status
6342 if not nslcmop_operation_state
:
6343 nslcmop_operation_state
= "COMPLETED"
6346 + " task Done with result {} {}".format(
6347 nslcmop_operation_state
, detailed_status
6351 # If nslcmop_operation_state is None, so any operation is not failed.
6352 # All operations are executed in overall.
6353 if not nslcmop_operation_state
:
6354 nslcmop_operation_state
= "COMPLETED"
6355 db_nsr_update
["operational-status"] = old_operational_status
6357 except (DbException
, LcmException
, N2VCException
, K8sException
) as e
:
6358 self
.logger
.error(logging_text
+ "Exit Exception {}".format(e
))
6360 except asyncio
.CancelledError
:
6362 logging_text
+ "Cancelled Exception while '{}'".format(step
)
6364 exc
= "Operation was cancelled"
6365 except asyncio
.TimeoutError
:
6366 self
.logger
.error(logging_text
+ "Timeout while '{}'".format(step
))
6368 except Exception as e
:
6369 exc
= traceback
.format_exc()
6370 self
.logger
.critical(
6371 logging_text
+ "Exit Exception {} {}".format(type(e
).__name
__, e
),
6380 ) = error_description_nslcmop
= "FAILED {}: {}".format(step
, exc
)
6381 nslcmop_operation_state
= "FAILED"
6382 db_nsr_update
["operational-status"] = old_operational_status
6384 self
._write
_ns
_status
(
6386 ns_state
=db_nsr
["nsState"],
6387 current_operation
="IDLE",
6388 current_operation_id
=None,
6389 other_update
=db_nsr_update
,
6392 self
._write
_op
_status
(
6395 error_message
=error_description_nslcmop
,
6396 operation_state
=nslcmop_operation_state
,
6397 other_update
=db_nslcmop_update
,
6400 if nslcmop_operation_state
:
6404 "nslcmop_id": nslcmop_id
,
6405 "operationState": nslcmop_operation_state
,
6408 change_type
in ("vnf_terminated", "policy_updated")
6409 and member_vnf_index
6411 msg
.update({"vnf_member_index": member_vnf_index
})
6412 await self
.msg
.aiowrite("ns", change_type
, msg
)
6413 except Exception as e
:
6415 logging_text
+ "kafka_write notification Exception {}".format(e
)
6417 self
.logger
.debug(logging_text
+ "Exit")
6418 self
.lcm_tasks
.remove("ns", nsr_id
, nslcmop_id
, "ns_update")
6419 return nslcmop_operation_state
, detailed_status
6421 async def scale(self
, nsr_id
, nslcmop_id
):
6422 # Try to lock HA task here
6423 task_is_locked_by_me
= self
.lcm_tasks
.lock_HA("ns", "nslcmops", nslcmop_id
)
6424 if not task_is_locked_by_me
:
6427 logging_text
= "Task ns={} scale={} ".format(nsr_id
, nslcmop_id
)
6428 stage
= ["", "", ""]
6429 tasks_dict_info
= {}
6430 # ^ stage, step, VIM progress
6431 self
.logger
.debug(logging_text
+ "Enter")
6432 # get all needed from database
6434 db_nslcmop_update
= {}
6437 # in case of error, indicates what part of scale was failed to put nsr at error status
6438 scale_process
= None
6439 old_operational_status
= ""
6440 old_config_status
= ""
6443 # wait for any previous tasks in process
6444 step
= "Waiting for previous operations to terminate"
6445 await self
.lcm_tasks
.waitfor_related_HA("ns", "nslcmops", nslcmop_id
)
6446 self
._write
_ns
_status
(
6449 current_operation
="SCALING",
6450 current_operation_id
=nslcmop_id
,
6453 step
= "Getting nslcmop from database"
6455 step
+ " after having waited for previous tasks to be completed"
6457 db_nslcmop
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
})
6459 step
= "Getting nsr from database"
6460 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
6461 old_operational_status
= db_nsr
["operational-status"]
6462 old_config_status
= db_nsr
["config-status"]
6464 step
= "Parsing scaling parameters"
6465 db_nsr_update
["operational-status"] = "scaling"
6466 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
6467 nsr_deployed
= db_nsr
["_admin"].get("deployed")
6469 vnf_index
= db_nslcmop
["operationParams"]["scaleVnfData"][
6471 ]["member-vnf-index"]
6472 scaling_group
= db_nslcmop
["operationParams"]["scaleVnfData"][
6474 ]["scaling-group-descriptor"]
6475 scaling_type
= db_nslcmop
["operationParams"]["scaleVnfData"]["scaleVnfType"]
6476 # for backward compatibility
6477 if nsr_deployed
and isinstance(nsr_deployed
.get("VCA"), dict):
6478 nsr_deployed
["VCA"] = list(nsr_deployed
["VCA"].values())
6479 db_nsr_update
["_admin.deployed.VCA"] = nsr_deployed
["VCA"]
6480 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
6482 step
= "Getting vnfr from database"
6483 db_vnfr
= self
.db
.get_one(
6484 "vnfrs", {"member-vnf-index-ref": vnf_index
, "nsr-id-ref": nsr_id
}
6487 vca_id
= self
.get_vca_id(db_vnfr
, db_nsr
)
6489 step
= "Getting vnfd from database"
6490 db_vnfd
= self
.db
.get_one("vnfds", {"_id": db_vnfr
["vnfd-id"]})
6492 base_folder
= db_vnfd
["_admin"]["storage"]
6494 step
= "Getting scaling-group-descriptor"
6495 scaling_descriptor
= find_in_list(
6496 get_scaling_aspect(db_vnfd
),
6497 lambda scale_desc
: scale_desc
["name"] == scaling_group
,
6499 if not scaling_descriptor
:
6501 "input parameter 'scaleByStepData':'scaling-group-descriptor':'{}' is not present "
6502 "at vnfd:scaling-group-descriptor".format(scaling_group
)
6505 step
= "Sending scale order to VIM"
6506 # TODO check if ns is in a proper status
6508 if not db_nsr
["_admin"].get("scaling-group"):
6513 "_admin.scaling-group": [
6514 {"name": scaling_group
, "nb-scale-op": 0}
6518 admin_scale_index
= 0
6520 for admin_scale_index
, admin_scale_info
in enumerate(
6521 db_nsr
["_admin"]["scaling-group"]
6523 if admin_scale_info
["name"] == scaling_group
:
6524 nb_scale_op
= admin_scale_info
.get("nb-scale-op", 0)
6526 else: # not found, set index one plus last element and add new entry with the name
6527 admin_scale_index
+= 1
6529 "_admin.scaling-group.{}.name".format(admin_scale_index
)
6532 vca_scaling_info
= []
6533 scaling_info
= {"scaling_group_name": scaling_group
, "vdu": [], "kdu": []}
6534 if scaling_type
== "SCALE_OUT":
6535 if "aspect-delta-details" not in scaling_descriptor
:
6537 "Aspect delta details not fount in scaling descriptor {}".format(
6538 scaling_descriptor
["name"]
6541 # count if max-instance-count is reached
6542 deltas
= scaling_descriptor
.get("aspect-delta-details")["deltas"]
6544 scaling_info
["scaling_direction"] = "OUT"
6545 scaling_info
["vdu-create"] = {}
6546 scaling_info
["kdu-create"] = {}
6547 for delta
in deltas
:
6548 for vdu_delta
in delta
.get("vdu-delta", {}):
6549 vdud
= get_vdu(db_vnfd
, vdu_delta
["id"])
6550 # vdu_index also provides the number of instance of the targeted vdu
6551 vdu_count
= vdu_index
= get_vdur_index(db_vnfr
, vdu_delta
)
6552 cloud_init_text
= self
._get
_vdu
_cloud
_init
_content
(
6556 additional_params
= (
6557 self
._get
_vdu
_additional
_params
(db_vnfr
, vdud
["id"])
6560 cloud_init_list
= []
6562 vdu_profile
= get_vdu_profile(db_vnfd
, vdu_delta
["id"])
6563 max_instance_count
= 10
6564 if vdu_profile
and "max-number-of-instances" in vdu_profile
:
6565 max_instance_count
= vdu_profile
.get(
6566 "max-number-of-instances", 10
6569 default_instance_num
= get_number_of_instances(
6572 instances_number
= vdu_delta
.get("number-of-instances", 1)
6573 nb_scale_op
+= instances_number
6575 new_instance_count
= nb_scale_op
+ default_instance_num
6576 # Control if new count is over max and vdu count is less than max.
6577 # Then assign new instance count
6578 if new_instance_count
> max_instance_count
> vdu_count
:
6579 instances_number
= new_instance_count
- max_instance_count
6581 instances_number
= instances_number
6583 if new_instance_count
> max_instance_count
:
6585 "reached the limit of {} (max-instance-count) "
6586 "scaling-out operations for the "
6587 "scaling-group-descriptor '{}'".format(
6588 nb_scale_op
, scaling_group
6591 for x
in range(vdu_delta
.get("number-of-instances", 1)):
6593 # TODO Information of its own ip is not available because db_vnfr is not updated.
6594 additional_params
["OSM"] = get_osm_params(
6595 db_vnfr
, vdu_delta
["id"], vdu_index
+ x
6597 cloud_init_list
.append(
6598 self
._parse
_cloud
_init
(
6605 vca_scaling_info
.append(
6607 "osm_vdu_id": vdu_delta
["id"],
6608 "member-vnf-index": vnf_index
,
6610 "vdu_index": vdu_index
+ x
,
6613 scaling_info
["vdu-create"][vdu_delta
["id"]] = instances_number
6614 for kdu_delta
in delta
.get("kdu-resource-delta", {}):
6615 kdu_profile
= get_kdu_resource_profile(db_vnfd
, kdu_delta
["id"])
6616 kdu_name
= kdu_profile
["kdu-name"]
6617 resource_name
= kdu_profile
.get("resource-name", "")
6619 # Might have different kdus in the same delta
6620 # Should have list for each kdu
6621 if not scaling_info
["kdu-create"].get(kdu_name
, None):
6622 scaling_info
["kdu-create"][kdu_name
] = []
6624 kdur
= get_kdur(db_vnfr
, kdu_name
)
6625 if kdur
.get("helm-chart"):
6626 k8s_cluster_type
= "helm-chart-v3"
6627 self
.logger
.debug("kdur: {}".format(kdur
))
6628 elif kdur
.get("juju-bundle"):
6629 k8s_cluster_type
= "juju-bundle"
6632 "kdu type for kdu='{}.{}' is neither helm-chart nor "
6633 "juju-bundle. Maybe an old NBI version is running".format(
6634 db_vnfr
["member-vnf-index-ref"], kdu_name
6638 max_instance_count
= 10
6639 if kdu_profile
and "max-number-of-instances" in kdu_profile
:
6640 max_instance_count
= kdu_profile
.get(
6641 "max-number-of-instances", 10
6644 nb_scale_op
+= kdu_delta
.get("number-of-instances", 1)
6645 deployed_kdu
, _
= get_deployed_kdu(
6646 nsr_deployed
, kdu_name
, vnf_index
6648 if deployed_kdu
is None:
6650 "KDU '{}' for vnf '{}' not deployed".format(
6654 kdu_instance
= deployed_kdu
.get("kdu-instance")
6655 instance_num
= await self
.k8scluster_map
[
6661 cluster_uuid
=deployed_kdu
.get("k8scluster-uuid"),
6662 kdu_model
=deployed_kdu
.get("kdu-model"),
6664 kdu_replica_count
= instance_num
+ kdu_delta
.get(
6665 "number-of-instances", 1
6668 # Control if new count is over max and instance_num is less than max.
6669 # Then assign max instance number to kdu replica count
6670 if kdu_replica_count
> max_instance_count
> instance_num
:
6671 kdu_replica_count
= max_instance_count
6672 if kdu_replica_count
> max_instance_count
:
6674 "reached the limit of {} (max-instance-count) "
6675 "scaling-out operations for the "
6676 "scaling-group-descriptor '{}'".format(
6677 instance_num
, scaling_group
6681 for x
in range(kdu_delta
.get("number-of-instances", 1)):
6682 vca_scaling_info
.append(
6684 "osm_kdu_id": kdu_name
,
6685 "member-vnf-index": vnf_index
,
6687 "kdu_index": instance_num
+ x
- 1,
6690 scaling_info
["kdu-create"][kdu_name
].append(
6692 "member-vnf-index": vnf_index
,
6694 "k8s-cluster-type": k8s_cluster_type
,
6695 "resource-name": resource_name
,
6696 "scale": kdu_replica_count
,
6699 elif scaling_type
== "SCALE_IN":
6700 deltas
= scaling_descriptor
.get("aspect-delta-details")["deltas"]
6702 scaling_info
["scaling_direction"] = "IN"
6703 scaling_info
["vdu-delete"] = {}
6704 scaling_info
["kdu-delete"] = {}
6706 for delta
in deltas
:
6707 for vdu_delta
in delta
.get("vdu-delta", {}):
6708 vdu_count
= vdu_index
= get_vdur_index(db_vnfr
, vdu_delta
)
6709 min_instance_count
= 0
6710 vdu_profile
= get_vdu_profile(db_vnfd
, vdu_delta
["id"])
6711 if vdu_profile
and "min-number-of-instances" in vdu_profile
:
6712 min_instance_count
= vdu_profile
["min-number-of-instances"]
6714 default_instance_num
= get_number_of_instances(
6715 db_vnfd
, vdu_delta
["id"]
6717 instance_num
= vdu_delta
.get("number-of-instances", 1)
6718 nb_scale_op
-= instance_num
6720 new_instance_count
= nb_scale_op
+ default_instance_num
6722 if new_instance_count
< min_instance_count
< vdu_count
:
6723 instances_number
= min_instance_count
- new_instance_count
6725 instances_number
= instance_num
6727 if new_instance_count
< min_instance_count
:
6729 "reached the limit of {} (min-instance-count) scaling-in operations for the "
6730 "scaling-group-descriptor '{}'".format(
6731 nb_scale_op
, scaling_group
6734 for x
in range(vdu_delta
.get("number-of-instances", 1)):
6735 vca_scaling_info
.append(
6737 "osm_vdu_id": vdu_delta
["id"],
6738 "member-vnf-index": vnf_index
,
6740 "vdu_index": vdu_index
- 1 - x
,
6743 scaling_info
["vdu-delete"][vdu_delta
["id"]] = instances_number
6744 for kdu_delta
in delta
.get("kdu-resource-delta", {}):
6745 kdu_profile
= get_kdu_resource_profile(db_vnfd
, kdu_delta
["id"])
6746 kdu_name
= kdu_profile
["kdu-name"]
6747 resource_name
= kdu_profile
.get("resource-name", "")
6749 if not scaling_info
["kdu-delete"].get(kdu_name
, None):
6750 scaling_info
["kdu-delete"][kdu_name
] = []
6752 kdur
= get_kdur(db_vnfr
, kdu_name
)
6753 if kdur
.get("helm-chart"):
6754 k8s_cluster_type
= "helm-chart-v3"
6755 self
.logger
.debug("kdur: {}".format(kdur
))
6756 elif kdur
.get("juju-bundle"):
6757 k8s_cluster_type
= "juju-bundle"
6760 "kdu type for kdu='{}.{}' is neither helm-chart nor "
6761 "juju-bundle. Maybe an old NBI version is running".format(
6762 db_vnfr
["member-vnf-index-ref"], kdur
["kdu-name"]
6766 min_instance_count
= 0
6767 if kdu_profile
and "min-number-of-instances" in kdu_profile
:
6768 min_instance_count
= kdu_profile
["min-number-of-instances"]
6770 nb_scale_op
-= kdu_delta
.get("number-of-instances", 1)
6771 deployed_kdu
, _
= get_deployed_kdu(
6772 nsr_deployed
, kdu_name
, vnf_index
6774 if deployed_kdu
is None:
6776 "KDU '{}' for vnf '{}' not deployed".format(
6780 kdu_instance
= deployed_kdu
.get("kdu-instance")
6781 instance_num
= await self
.k8scluster_map
[
6787 cluster_uuid
=deployed_kdu
.get("k8scluster-uuid"),
6788 kdu_model
=deployed_kdu
.get("kdu-model"),
6790 kdu_replica_count
= instance_num
- kdu_delta
.get(
6791 "number-of-instances", 1
6794 if kdu_replica_count
< min_instance_count
< instance_num
:
6795 kdu_replica_count
= min_instance_count
6796 if kdu_replica_count
< min_instance_count
:
6798 "reached the limit of {} (min-instance-count) scaling-in operations for the "
6799 "scaling-group-descriptor '{}'".format(
6800 instance_num
, scaling_group
6804 for x
in range(kdu_delta
.get("number-of-instances", 1)):
6805 vca_scaling_info
.append(
6807 "osm_kdu_id": kdu_name
,
6808 "member-vnf-index": vnf_index
,
6810 "kdu_index": instance_num
- x
- 1,
6813 scaling_info
["kdu-delete"][kdu_name
].append(
6815 "member-vnf-index": vnf_index
,
6817 "k8s-cluster-type": k8s_cluster_type
,
6818 "resource-name": resource_name
,
6819 "scale": kdu_replica_count
,
6823 # update VDU_SCALING_INFO with the VDUs to delete ip_addresses
6824 vdu_delete
= copy(scaling_info
.get("vdu-delete"))
6825 if scaling_info
["scaling_direction"] == "IN":
6826 for vdur
in reversed(db_vnfr
["vdur"]):
6827 if vdu_delete
.get(vdur
["vdu-id-ref"]):
6828 vdu_delete
[vdur
["vdu-id-ref"]] -= 1
6829 scaling_info
["vdu"].append(
6831 "name": vdur
.get("name") or vdur
.get("vdu-name"),
6832 "vdu_id": vdur
["vdu-id-ref"],
6836 for interface
in vdur
["interfaces"]:
6837 scaling_info
["vdu"][-1]["interface"].append(
6839 "name": interface
["name"],
6840 "ip_address": interface
["ip-address"],
6841 "mac_address": interface
.get("mac-address"),
6844 # vdu_delete = vdu_scaling_info.pop("vdu-delete")
6847 step
= "Executing pre-scale vnf-config-primitive"
6848 if scaling_descriptor
.get("scaling-config-action"):
6849 for scaling_config_action
in scaling_descriptor
[
6850 "scaling-config-action"
6853 scaling_config_action
.get("trigger") == "pre-scale-in"
6854 and scaling_type
== "SCALE_IN"
6856 scaling_config_action
.get("trigger") == "pre-scale-out"
6857 and scaling_type
== "SCALE_OUT"
6859 vnf_config_primitive
= scaling_config_action
[
6860 "vnf-config-primitive-name-ref"
6862 step
= db_nslcmop_update
[
6864 ] = "executing pre-scale scaling-config-action '{}'".format(
6865 vnf_config_primitive
6868 # look for primitive
6869 for config_primitive
in (
6870 get_configuration(db_vnfd
, db_vnfd
["id"]) or {}
6871 ).get("config-primitive", ()):
6872 if config_primitive
["name"] == vnf_config_primitive
:
6876 "Invalid vnfd descriptor at scaling-group-descriptor[name='{}']:scaling-config-action"
6877 "[vnf-config-primitive-name-ref='{}'] does not match any vnf-configuration:config-"
6878 "primitive".format(scaling_group
, vnf_config_primitive
)
6881 vnfr_params
= {"VDU_SCALE_INFO": scaling_info
}
6882 if db_vnfr
.get("additionalParamsForVnf"):
6883 vnfr_params
.update(db_vnfr
["additionalParamsForVnf"])
6885 scale_process
= "VCA"
6886 db_nsr_update
["config-status"] = "configuring pre-scaling"
6887 primitive_params
= self
._map
_primitive
_params
(
6888 config_primitive
, {}, vnfr_params
6891 # Pre-scale retry check: Check if this sub-operation has been executed before
6892 op_index
= self
._check
_or
_add
_scale
_suboperation
(
6895 vnf_config_primitive
,
6899 if op_index
== self
.SUBOPERATION_STATUS_SKIP
:
6900 # Skip sub-operation
6901 result
= "COMPLETED"
6902 result_detail
= "Done"
6905 + "vnf_config_primitive={} Skipped sub-operation, result {} {}".format(
6906 vnf_config_primitive
, result
, result_detail
6910 if op_index
== self
.SUBOPERATION_STATUS_NEW
:
6911 # New sub-operation: Get index of this sub-operation
6913 len(db_nslcmop
.get("_admin", {}).get("operations"))
6918 + "vnf_config_primitive={} New sub-operation".format(
6919 vnf_config_primitive
6923 # retry: Get registered params for this existing sub-operation
6924 op
= db_nslcmop
.get("_admin", {}).get("operations", [])[
6927 vnf_index
= op
.get("member_vnf_index")
6928 vnf_config_primitive
= op
.get("primitive")
6929 primitive_params
= op
.get("primitive_params")
6932 + "vnf_config_primitive={} Sub-operation retry".format(
6933 vnf_config_primitive
6936 # Execute the primitive, either with new (first-time) or registered (reintent) args
6937 ee_descriptor_id
= config_primitive
.get(
6938 "execution-environment-ref"
6940 primitive_name
= config_primitive
.get(
6941 "execution-environment-primitive", vnf_config_primitive
6943 ee_id
, vca_type
= self
._look
_for
_deployed
_vca
(
6944 nsr_deployed
["VCA"],
6945 member_vnf_index
=vnf_index
,
6947 vdu_count_index
=None,
6948 ee_descriptor_id
=ee_descriptor_id
,
6950 result
, result_detail
= await self
._ns
_execute
_primitive
(
6959 + "vnf_config_primitive={} Done with result {} {}".format(
6960 vnf_config_primitive
, result
, result_detail
6963 # Update operationState = COMPLETED | FAILED
6964 self
._update
_suboperation
_status
(
6965 db_nslcmop
, op_index
, result
, result_detail
6968 if result
== "FAILED":
6969 raise LcmException(result_detail
)
6970 db_nsr_update
["config-status"] = old_config_status
6971 scale_process
= None
6975 "_admin.scaling-group.{}.nb-scale-op".format(admin_scale_index
)
6978 "_admin.scaling-group.{}.time".format(admin_scale_index
)
6981 # SCALE-IN VCA - BEGIN
6982 if vca_scaling_info
:
6983 step
= db_nslcmop_update
[
6985 ] = "Deleting the execution environments"
6986 scale_process
= "VCA"
6987 for vca_info
in vca_scaling_info
:
6988 if vca_info
["type"] == "delete" and not vca_info
.get("osm_kdu_id"):
6989 member_vnf_index
= str(vca_info
["member-vnf-index"])
6991 logging_text
+ "vdu info: {}".format(vca_info
)
6993 if vca_info
.get("osm_vdu_id"):
6994 vdu_id
= vca_info
["osm_vdu_id"]
6995 vdu_index
= int(vca_info
["vdu_index"])
6998 ] = "Scaling member_vnf_index={}, vdu_id={}, vdu_index={} ".format(
6999 member_vnf_index
, vdu_id
, vdu_index
7001 stage
[2] = step
= "Scaling in VCA"
7002 self
._write
_op
_status
(op_id
=nslcmop_id
, stage
=stage
)
7003 vca_update
= db_nsr
["_admin"]["deployed"]["VCA"]
7004 config_update
= db_nsr
["configurationStatus"]
7005 for vca_index
, vca
in enumerate(vca_update
):
7007 (vca
or vca
.get("ee_id"))
7008 and vca
["member-vnf-index"] == member_vnf_index
7009 and vca
["vdu_count_index"] == vdu_index
7011 if vca
.get("vdu_id"):
7012 config_descriptor
= get_configuration(
7013 db_vnfd
, vca
.get("vdu_id")
7015 elif vca
.get("kdu_name"):
7016 config_descriptor
= get_configuration(
7017 db_vnfd
, vca
.get("kdu_name")
7020 config_descriptor
= get_configuration(
7021 db_vnfd
, db_vnfd
["id"]
7023 operation_params
= (
7024 db_nslcmop
.get("operationParams") or {}
7026 exec_terminate_primitives
= not operation_params
.get(
7027 "skip_terminate_primitives"
7028 ) and vca
.get("needed_terminate")
7029 task
= asyncio
.ensure_future(
7038 exec_primitives
=exec_terminate_primitives
,
7042 timeout
=self
.timeout
.charm_delete
,
7045 tasks_dict_info
[task
] = "Terminating VCA {}".format(
7048 del vca_update
[vca_index
]
7049 del config_update
[vca_index
]
7050 # wait for pending tasks of terminate primitives
7054 + "Waiting for tasks {}".format(
7055 list(tasks_dict_info
.keys())
7058 error_list
= await self
._wait
_for
_tasks
(
7062 self
.timeout
.charm_delete
, self
.timeout
.ns_terminate
7067 tasks_dict_info
.clear()
7069 raise LcmException("; ".join(error_list
))
7071 db_vca_and_config_update
= {
7072 "_admin.deployed.VCA": vca_update
,
7073 "configurationStatus": config_update
,
7076 "nsrs", db_nsr
["_id"], db_vca_and_config_update
7078 scale_process
= None
7079 # SCALE-IN VCA - END
7082 if scaling_info
.get("vdu-create") or scaling_info
.get("vdu-delete"):
7083 scale_process
= "RO"
7084 if self
.ro_config
.ng
:
7085 await self
._scale
_ng
_ro
(
7086 logging_text
, db_nsr
, db_nslcmop
, db_vnfr
, scaling_info
, stage
7088 scaling_info
.pop("vdu-create", None)
7089 scaling_info
.pop("vdu-delete", None)
7091 scale_process
= None
7095 if scaling_info
.get("kdu-create") or scaling_info
.get("kdu-delete"):
7096 scale_process
= "KDU"
7097 await self
._scale
_kdu
(
7098 logging_text
, nsr_id
, nsr_deployed
, db_vnfd
, vca_id
, scaling_info
7100 scaling_info
.pop("kdu-create", None)
7101 scaling_info
.pop("kdu-delete", None)
7103 scale_process
= None
7107 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
7109 # SCALE-UP VCA - BEGIN
7110 if vca_scaling_info
:
7111 step
= db_nslcmop_update
[
7113 ] = "Creating new execution environments"
7114 scale_process
= "VCA"
7115 for vca_info
in vca_scaling_info
:
7116 if vca_info
["type"] == "create" and not vca_info
.get("osm_kdu_id"):
7117 member_vnf_index
= str(vca_info
["member-vnf-index"])
7119 logging_text
+ "vdu info: {}".format(vca_info
)
7121 vnfd_id
= db_vnfr
["vnfd-ref"]
7122 if vca_info
.get("osm_vdu_id"):
7123 vdu_index
= int(vca_info
["vdu_index"])
7124 deploy_params
= {"OSM": get_osm_params(db_vnfr
)}
7125 if db_vnfr
.get("additionalParamsForVnf"):
7126 deploy_params
.update(
7128 db_vnfr
["additionalParamsForVnf"].copy()
7131 descriptor_config
= get_configuration(
7132 db_vnfd
, db_vnfd
["id"]
7134 if descriptor_config
:
7140 logging_text
=logging_text
7141 + "member_vnf_index={} ".format(member_vnf_index
),
7144 nslcmop_id
=nslcmop_id
,
7150 kdu_index
=kdu_index
,
7151 member_vnf_index
=member_vnf_index
,
7152 vdu_index
=vdu_index
,
7154 deploy_params
=deploy_params
,
7155 descriptor_config
=descriptor_config
,
7156 base_folder
=base_folder
,
7157 task_instantiation_info
=tasks_dict_info
,
7160 vdu_id
= vca_info
["osm_vdu_id"]
7161 vdur
= find_in_list(
7162 db_vnfr
["vdur"], lambda vdu
: vdu
["vdu-id-ref"] == vdu_id
7164 descriptor_config
= get_configuration(db_vnfd
, vdu_id
)
7165 if vdur
.get("additionalParams"):
7166 deploy_params_vdu
= parse_yaml_strings(
7167 vdur
["additionalParams"]
7170 deploy_params_vdu
= deploy_params
7171 deploy_params_vdu
["OSM"] = get_osm_params(
7172 db_vnfr
, vdu_id
, vdu_count_index
=vdu_index
7174 if descriptor_config
:
7180 ] = "Scaling member_vnf_index={}, vdu_id={}, vdu_index={} ".format(
7181 member_vnf_index
, vdu_id
, vdu_index
7183 stage
[2] = step
= "Scaling out VCA"
7184 self
._write
_op
_status
(op_id
=nslcmop_id
, stage
=stage
)
7186 logging_text
=logging_text
7187 + "member_vnf_index={}, vdu_id={}, vdu_index={} ".format(
7188 member_vnf_index
, vdu_id
, vdu_index
7192 nslcmop_id
=nslcmop_id
,
7198 member_vnf_index
=member_vnf_index
,
7199 vdu_index
=vdu_index
,
7200 kdu_index
=kdu_index
,
7202 deploy_params
=deploy_params_vdu
,
7203 descriptor_config
=descriptor_config
,
7204 base_folder
=base_folder
,
7205 task_instantiation_info
=tasks_dict_info
,
7208 # SCALE-UP VCA - END
7209 scale_process
= None
7212 # execute primitive service POST-SCALING
7213 step
= "Executing post-scale vnf-config-primitive"
7214 if scaling_descriptor
.get("scaling-config-action"):
7215 for scaling_config_action
in scaling_descriptor
[
7216 "scaling-config-action"
7219 scaling_config_action
.get("trigger") == "post-scale-in"
7220 and scaling_type
== "SCALE_IN"
7222 scaling_config_action
.get("trigger") == "post-scale-out"
7223 and scaling_type
== "SCALE_OUT"
7225 vnf_config_primitive
= scaling_config_action
[
7226 "vnf-config-primitive-name-ref"
7228 step
= db_nslcmop_update
[
7230 ] = "executing post-scale scaling-config-action '{}'".format(
7231 vnf_config_primitive
7234 vnfr_params
= {"VDU_SCALE_INFO": scaling_info
}
7235 if db_vnfr
.get("additionalParamsForVnf"):
7236 vnfr_params
.update(db_vnfr
["additionalParamsForVnf"])
7238 # look for primitive
7239 for config_primitive
in (
7240 get_configuration(db_vnfd
, db_vnfd
["id"]) or {}
7241 ).get("config-primitive", ()):
7242 if config_primitive
["name"] == vnf_config_primitive
:
7246 "Invalid vnfd descriptor at scaling-group-descriptor[name='{}']:scaling-config-"
7247 "action[vnf-config-primitive-name-ref='{}'] does not match any vnf-configuration:"
7248 "config-primitive".format(
7249 scaling_group
, vnf_config_primitive
7252 scale_process
= "VCA"
7253 db_nsr_update
["config-status"] = "configuring post-scaling"
7254 primitive_params
= self
._map
_primitive
_params
(
7255 config_primitive
, {}, vnfr_params
7258 # Post-scale retry check: Check if this sub-operation has been executed before
7259 op_index
= self
._check
_or
_add
_scale
_suboperation
(
7262 vnf_config_primitive
,
7266 if op_index
== self
.SUBOPERATION_STATUS_SKIP
:
7267 # Skip sub-operation
7268 result
= "COMPLETED"
7269 result_detail
= "Done"
7272 + "vnf_config_primitive={} Skipped sub-operation, result {} {}".format(
7273 vnf_config_primitive
, result
, result_detail
7277 if op_index
== self
.SUBOPERATION_STATUS_NEW
:
7278 # New sub-operation: Get index of this sub-operation
7280 len(db_nslcmop
.get("_admin", {}).get("operations"))
7285 + "vnf_config_primitive={} New sub-operation".format(
7286 vnf_config_primitive
7290 # retry: Get registered params for this existing sub-operation
7291 op
= db_nslcmop
.get("_admin", {}).get("operations", [])[
7294 vnf_index
= op
.get("member_vnf_index")
7295 vnf_config_primitive
= op
.get("primitive")
7296 primitive_params
= op
.get("primitive_params")
7299 + "vnf_config_primitive={} Sub-operation retry".format(
7300 vnf_config_primitive
7303 # Execute the primitive, either with new (first-time) or registered (reintent) args
7304 ee_descriptor_id
= config_primitive
.get(
7305 "execution-environment-ref"
7307 primitive_name
= config_primitive
.get(
7308 "execution-environment-primitive", vnf_config_primitive
7310 ee_id
, vca_type
= self
._look
_for
_deployed
_vca
(
7311 nsr_deployed
["VCA"],
7312 member_vnf_index
=vnf_index
,
7314 vdu_count_index
=None,
7315 ee_descriptor_id
=ee_descriptor_id
,
7317 result
, result_detail
= await self
._ns
_execute
_primitive
(
7326 + "vnf_config_primitive={} Done with result {} {}".format(
7327 vnf_config_primitive
, result
, result_detail
7330 # Update operationState = COMPLETED | FAILED
7331 self
._update
_suboperation
_status
(
7332 db_nslcmop
, op_index
, result
, result_detail
7335 if result
== "FAILED":
7336 raise LcmException(result_detail
)
7337 db_nsr_update
["config-status"] = old_config_status
7338 scale_process
= None
7343 ] = "" # "scaled {} {}".format(scaling_group, scaling_type)
7344 db_nsr_update
["operational-status"] = (
7346 if old_operational_status
== "failed"
7347 else old_operational_status
7349 db_nsr_update
["config-status"] = old_config_status
7352 ROclient
.ROClientException
,
7357 self
.logger
.error(logging_text
+ "Exit Exception {}".format(e
))
7359 except asyncio
.CancelledError
:
7361 logging_text
+ "Cancelled Exception while '{}'".format(step
)
7363 exc
= "Operation was cancelled"
7364 except Exception as e
:
7365 exc
= traceback
.format_exc()
7366 self
.logger
.critical(
7367 logging_text
+ "Exit Exception {} {}".format(type(e
).__name
__, e
),
7373 error_list
.append(str(exc
))
7374 self
._write
_ns
_status
(
7377 current_operation
="IDLE",
7378 current_operation_id
=None,
7382 stage
[1] = "Waiting for instantiate pending tasks."
7383 self
.logger
.debug(logging_text
+ stage
[1])
7384 exc
= await self
._wait
_for
_tasks
(
7387 self
.timeout
.ns_deploy
,
7392 except asyncio
.CancelledError
:
7393 error_list
.append("Cancelled")
7394 await self
._cancel
_pending
_tasks
(logging_text
, tasks_dict_info
)
7395 await self
._wait
_for
_tasks
(
7398 self
.timeout
.ns_deploy
,
7404 error_detail
= "; ".join(error_list
)
7407 ] = error_description_nslcmop
= "FAILED {}: {}".format(
7410 nslcmop_operation_state
= "FAILED"
7412 db_nsr_update
["operational-status"] = old_operational_status
7413 db_nsr_update
["config-status"] = old_config_status
7414 db_nsr_update
["detailed-status"] = ""
7416 if "VCA" in scale_process
:
7417 db_nsr_update
["config-status"] = "failed"
7418 if "RO" in scale_process
:
7419 db_nsr_update
["operational-status"] = "failed"
7422 ] = "FAILED scaling nslcmop={} {}: {}".format(
7423 nslcmop_id
, step
, error_detail
7426 error_description_nslcmop
= None
7427 nslcmop_operation_state
= "COMPLETED"
7428 db_nslcmop_update
["detailed-status"] = "Done"
7430 self
._write
_op
_status
(
7433 error_message
=error_description_nslcmop
,
7434 operation_state
=nslcmop_operation_state
,
7435 other_update
=db_nslcmop_update
,
7438 self
._write
_ns
_status
(
7441 current_operation
="IDLE",
7442 current_operation_id
=None,
7443 other_update
=db_nsr_update
,
7446 if nslcmop_operation_state
:
7450 "nslcmop_id": nslcmop_id
,
7451 "operationState": nslcmop_operation_state
,
7453 await self
.msg
.aiowrite("ns", "scaled", msg
)
7454 except Exception as e
:
7456 logging_text
+ "kafka_write notification Exception {}".format(e
)
7458 self
.logger
.debug(logging_text
+ "Exit")
7459 self
.lcm_tasks
.remove("ns", nsr_id
, nslcmop_id
, "ns_scale")
7461 async def _scale_kdu(
7462 self
, logging_text
, nsr_id
, nsr_deployed
, db_vnfd
, vca_id
, scaling_info
7464 _scaling_info
= scaling_info
.get("kdu-create") or scaling_info
.get("kdu-delete")
7465 for kdu_name
in _scaling_info
:
7466 for kdu_scaling_info
in _scaling_info
[kdu_name
]:
7467 deployed_kdu
, index
= get_deployed_kdu(
7468 nsr_deployed
, kdu_name
, kdu_scaling_info
["member-vnf-index"]
7470 cluster_uuid
= deployed_kdu
["k8scluster-uuid"]
7471 kdu_instance
= deployed_kdu
["kdu-instance"]
7472 kdu_model
= deployed_kdu
.get("kdu-model")
7473 scale
= int(kdu_scaling_info
["scale"])
7474 k8s_cluster_type
= kdu_scaling_info
["k8s-cluster-type"]
7477 "collection": "nsrs",
7478 "filter": {"_id": nsr_id
},
7479 "path": "_admin.deployed.K8s.{}".format(index
),
7482 step
= "scaling application {}".format(
7483 kdu_scaling_info
["resource-name"]
7485 self
.logger
.debug(logging_text
+ step
)
7487 if kdu_scaling_info
["type"] == "delete":
7488 kdu_config
= get_configuration(db_vnfd
, kdu_name
)
7491 and kdu_config
.get("terminate-config-primitive")
7492 and get_juju_ee_ref(db_vnfd
, kdu_name
) is None
7494 terminate_config_primitive_list
= kdu_config
.get(
7495 "terminate-config-primitive"
7497 terminate_config_primitive_list
.sort(
7498 key
=lambda val
: int(val
["seq"])
7502 terminate_config_primitive
7503 ) in terminate_config_primitive_list
:
7504 primitive_params_
= self
._map
_primitive
_params
(
7505 terminate_config_primitive
, {}, {}
7507 step
= "execute terminate config primitive"
7508 self
.logger
.debug(logging_text
+ step
)
7509 await asyncio
.wait_for(
7510 self
.k8scluster_map
[k8s_cluster_type
].exec_primitive(
7511 cluster_uuid
=cluster_uuid
,
7512 kdu_instance
=kdu_instance
,
7513 primitive_name
=terminate_config_primitive
["name"],
7514 params
=primitive_params_
,
7516 total_timeout
=self
.timeout
.primitive
,
7519 timeout
=self
.timeout
.primitive
7520 * self
.timeout
.primitive_outer_factor
,
7523 await asyncio
.wait_for(
7524 self
.k8scluster_map
[k8s_cluster_type
].scale(
7525 kdu_instance
=kdu_instance
,
7527 resource_name
=kdu_scaling_info
["resource-name"],
7528 total_timeout
=self
.timeout
.scale_on_error
,
7530 cluster_uuid
=cluster_uuid
,
7531 kdu_model
=kdu_model
,
7535 timeout
=self
.timeout
.scale_on_error
7536 * self
.timeout
.scale_on_error_outer_factor
,
7539 if kdu_scaling_info
["type"] == "create":
7540 kdu_config
= get_configuration(db_vnfd
, kdu_name
)
7543 and kdu_config
.get("initial-config-primitive")
7544 and get_juju_ee_ref(db_vnfd
, kdu_name
) is None
7546 initial_config_primitive_list
= kdu_config
.get(
7547 "initial-config-primitive"
7549 initial_config_primitive_list
.sort(
7550 key
=lambda val
: int(val
["seq"])
7553 for initial_config_primitive
in initial_config_primitive_list
:
7554 primitive_params_
= self
._map
_primitive
_params
(
7555 initial_config_primitive
, {}, {}
7557 step
= "execute initial config primitive"
7558 self
.logger
.debug(logging_text
+ step
)
7559 await asyncio
.wait_for(
7560 self
.k8scluster_map
[k8s_cluster_type
].exec_primitive(
7561 cluster_uuid
=cluster_uuid
,
7562 kdu_instance
=kdu_instance
,
7563 primitive_name
=initial_config_primitive
["name"],
7564 params
=primitive_params_
,
7571 async def _scale_ng_ro(
7572 self
, logging_text
, db_nsr
, db_nslcmop
, db_vnfr
, vdu_scaling_info
, stage
7574 nsr_id
= db_nslcmop
["nsInstanceId"]
7575 db_nsd
= self
.db
.get_one("nsds", {"_id": db_nsr
["nsd-id"]})
7578 # read from db: vnfd's for every vnf
7581 # for each vnf in ns, read vnfd
7582 for vnfr
in self
.db
.get_list("vnfrs", {"nsr-id-ref": nsr_id
}):
7583 db_vnfrs
[vnfr
["member-vnf-index-ref"]] = vnfr
7584 vnfd_id
= vnfr
["vnfd-id"] # vnfd uuid for this vnf
7585 # if we haven't this vnfd, read it from db
7586 if not find_in_list(db_vnfds
, lambda a_vnfd
: a_vnfd
["id"] == vnfd_id
):
7588 vnfd
= self
.db
.get_one("vnfds", {"_id": vnfd_id
})
7589 db_vnfds
.append(vnfd
)
7590 n2vc_key
= self
.n2vc
.get_public_key()
7591 n2vc_key_list
= [n2vc_key
]
7594 vdu_scaling_info
.get("vdu-create"),
7595 vdu_scaling_info
.get("vdu-delete"),
7598 # db_vnfr has been updated, update db_vnfrs to use it
7599 db_vnfrs
[db_vnfr
["member-vnf-index-ref"]] = db_vnfr
7600 await self
._instantiate
_ng
_ro
(
7610 start_deploy
=time(),
7611 timeout_ns_deploy
=self
.timeout
.ns_deploy
,
7613 if vdu_scaling_info
.get("vdu-delete"):
7615 db_vnfr
, None, vdu_scaling_info
["vdu-delete"], mark_delete
=False
7618 async def extract_prometheus_scrape_jobs(
7622 ee_config_descriptor
: dict,
7627 vnf_member_index
: str = "",
7629 vdu_index
: int = None,
7631 kdu_index
: int = None,
7633 """Method to extract prometheus scrape jobs from EE's Prometheus template job file
7634 This method will wait until the corresponding VDU or KDU is fully instantiated
7637 ee_id (str): Execution Environment ID
7638 artifact_path (str): Path where the EE's content is (including the Prometheus template file)
7639 ee_config_descriptor (dict): Execution Environment's configuration descriptor
7640 vnfr_id (str): VNFR ID where this EE applies
7641 nsr_id (str): NSR ID where this EE applies
7642 target_ip (str): VDU/KDU instance IP address
7643 element_type (str): NS or VNF or VDU or KDU
7644 vnf_member_index (str, optional): VNF index where this EE applies. Defaults to "".
7645 vdu_id (str, optional): VDU ID where this EE applies. Defaults to "".
7646 vdu_index (int, optional): VDU index where this EE applies. Defaults to None.
7647 kdu_name (str, optional): KDU name where this EE applies. Defaults to "".
7648 kdu_index (int, optional): KDU index where this EE applies. Defaults to None.
7651 LcmException: When the VDU or KDU instance was not found in an hour
7654 _type_: Prometheus jobs
7656 # default the vdur and kdur names to an empty string, to avoid any later
7657 # problem with Prometheus when the element type is not VDU or KDU
7661 # look if exist a file called 'prometheus*.j2' and
7662 artifact_content
= self
.fs
.dir_ls(artifact_path
)
7666 for f
in artifact_content
7667 if f
.startswith("prometheus") and f
.endswith(".j2")
7673 self
.logger
.debug("Artifact path{}".format(artifact_path
))
7674 self
.logger
.debug("job file{}".format(job_file
))
7675 with self
.fs
.file_open((artifact_path
, job_file
), "r") as f
:
7678 # obtain the VDUR or KDUR, if the element type is VDU or KDU
7679 if element_type
in ("VDU", "KDU"):
7680 for _
in range(360):
7681 db_vnfr
= self
.db
.get_one("vnfrs", {"_id": vnfr_id
})
7682 if vdu_id
and vdu_index
is not None:
7686 for x
in get_iterable(db_vnfr
, "vdur")
7688 x
.get("vdu-id-ref") == vdu_id
7689 and x
.get("count-index") == vdu_index
7694 if vdur
.get("name"):
7695 vdur_name
= vdur
.get("name")
7697 if kdu_name
and kdu_index
is not None:
7701 for x
in get_iterable(db_vnfr
, "kdur")
7703 x
.get("kdu-name") == kdu_name
7704 and x
.get("count-index") == kdu_index
7709 if kdur
.get("name"):
7710 kdur_name
= kdur
.get("name")
7713 await asyncio
.sleep(10)
7715 if vdu_id
and vdu_index
is not None:
7717 f
"Timeout waiting VDU with name={vdu_id} and index={vdu_index} to be intantiated"
7719 if kdu_name
and kdu_index
is not None:
7721 f
"Timeout waiting KDU with name={kdu_name} and index={kdu_index} to be intantiated"
7724 if ee_id
is not None:
7725 _
, namespace
, helm_id
= get_ee_id_parts(
7727 ) # get namespace and EE gRPC service name
7728 host_name
= f
'{helm_id}-{ee_config_descriptor["metric-service"]}.{namespace}.svc' # svc_name.namespace.svc
7730 vnfr_id
= vnfr_id
.replace("-", "")
7732 "JOB_NAME": vnfr_id
,
7733 "TARGET_IP": target_ip
,
7734 "EXPORTER_POD_IP": host_name
,
7735 "EXPORTER_POD_PORT": host_port
,
7737 "VNF_MEMBER_INDEX": vnf_member_index
,
7738 "VDUR_NAME": vdur_name
,
7739 "KDUR_NAME": kdur_name
,
7740 "ELEMENT_TYPE": element_type
,
7743 metric_path
= ee_config_descriptor
["metric-path"]
7744 target_port
= ee_config_descriptor
["metric-port"]
7745 vnfr_id
= vnfr_id
.replace("-", "")
7747 "JOB_NAME": vnfr_id
,
7748 "TARGET_IP": target_ip
,
7749 "TARGET_PORT": target_port
,
7750 "METRIC_PATH": metric_path
,
7753 job_list
= parse_job(job_data
, variables
)
7754 # ensure job_name is using the vnfr_id. Adding the metadata nsr_id
7755 for job
in job_list
:
7757 not isinstance(job
.get("job_name"), str)
7758 or vnfr_id
not in job
["job_name"]
7760 job
["job_name"] = vnfr_id
+ "_" + str(SystemRandom().randint(1, 10000))
7761 job
["nsr_id"] = nsr_id
7762 job
["vnfr_id"] = vnfr_id
7765 async def rebuild_start_stop(
7766 self
, nsr_id
, nslcmop_id
, vnf_id
, additional_param
, operation_type
7768 logging_text
= "Task ns={} {}={} ".format(nsr_id
, operation_type
, nslcmop_id
)
7769 self
.logger
.info(logging_text
+ "Enter")
7770 stage
= ["Preparing the environment", ""]
7771 # database nsrs record
7775 # in case of error, indicates what part of scale was failed to put nsr at error status
7776 start_deploy
= time()
7778 db_vnfr
= self
.db
.get_one("vnfrs", {"_id": vnf_id
})
7779 vim_account_id
= db_vnfr
.get("vim-account-id")
7780 vim_info_key
= "vim:" + vim_account_id
7781 vdu_id
= additional_param
["vdu_id"]
7782 vdurs
= [item
for item
in db_vnfr
["vdur"] if item
["vdu-id-ref"] == vdu_id
]
7783 vdur
= find_in_list(
7784 vdurs
, lambda vdu
: vdu
["count-index"] == additional_param
["count-index"]
7787 vdu_vim_name
= vdur
["name"]
7788 vim_vm_id
= vdur
["vim_info"][vim_info_key
]["vim_id"]
7789 target_vim
, _
= next(k_v
for k_v
in vdur
["vim_info"].items())
7791 raise LcmException("Target vdu is not found")
7792 self
.logger
.info("vdu_vim_name >> {} ".format(vdu_vim_name
))
7793 # wait for any previous tasks in process
7794 stage
[1] = "Waiting for previous operations to terminate"
7795 self
.logger
.info(stage
[1])
7796 await self
.lcm_tasks
.waitfor_related_HA("ns", "nslcmops", nslcmop_id
)
7798 stage
[1] = "Reading from database."
7799 self
.logger
.info(stage
[1])
7800 self
._write
_ns
_status
(
7803 current_operation
=operation_type
.upper(),
7804 current_operation_id
=nslcmop_id
,
7806 self
._write
_op
_status
(op_id
=nslcmop_id
, stage
=stage
, queuePosition
=0)
7809 stage
[1] = "Getting nsr={} from db.".format(nsr_id
)
7810 db_nsr_update
["operational-status"] = operation_type
7811 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
7815 "vim_vm_id": vim_vm_id
,
7817 "vdu_index": additional_param
["count-index"],
7818 "vdu_id": vdur
["id"],
7819 "target_vim": target_vim
,
7820 "vim_account_id": vim_account_id
,
7823 stage
[1] = "Sending rebuild request to RO... {}".format(desc
)
7824 self
._write
_op
_status
(op_id
=nslcmop_id
, stage
=stage
, queuePosition
=0)
7825 self
.logger
.info("ro nsr id: {}".format(nsr_id
))
7826 result_dict
= await self
.RO
.operate(nsr_id
, desc
, operation_type
)
7827 self
.logger
.info("response from RO: {}".format(result_dict
))
7828 action_id
= result_dict
["action_id"]
7829 await self
._wait
_ng
_ro
(
7834 self
.timeout
.operate
,
7836 "start_stop_rebuild",
7838 return "COMPLETED", "Done"
7839 except (ROclient
.ROClientException
, DbException
, LcmException
) as e
:
7840 self
.logger
.error("Exit Exception {}".format(e
))
7842 except asyncio
.CancelledError
:
7843 self
.logger
.error("Cancelled Exception while '{}'".format(stage
))
7844 exc
= "Operation was cancelled"
7845 except Exception as e
:
7846 exc
= traceback
.format_exc()
7847 self
.logger
.critical(
7848 "Exit Exception {} {}".format(type(e
).__name
__, e
), exc_info
=True
7850 return "FAILED", "Error in operate VNF {}".format(exc
)
7852 def get_vca_cloud_and_credentials(self
, vim_account_id
: str) -> (str, str):
7854 Get VCA Cloud and VCA Cloud Credentials for the VIM account
7856 :param: vim_account_id: VIM Account ID
7858 :return: (cloud_name, cloud_credential)
7860 config
= VimAccountDB
.get_vim_account_with_id(vim_account_id
).get("config", {})
7861 return config
.get("vca_cloud"), config
.get("vca_cloud_credential")
7863 def get_vca_k8s_cloud_and_credentials(self
, vim_account_id
: str) -> (str, str):
7865 Get VCA K8s Cloud and VCA K8s Cloud Credentials for the VIM account
7867 :param: vim_account_id: VIM Account ID
7869 :return: (cloud_name, cloud_credential)
7871 config
= VimAccountDB
.get_vim_account_with_id(vim_account_id
).get("config", {})
7872 return config
.get("vca_k8s_cloud"), config
.get("vca_k8s_cloud_credential")
7874 async def migrate(self
, nsr_id
, nslcmop_id
):
7876 Migrate VNFs and VDUs instances in a NS
7878 :param: nsr_id: NS Instance ID
7879 :param: nslcmop_id: nslcmop ID of migrate
7882 # Try to lock HA task here
7883 task_is_locked_by_me
= self
.lcm_tasks
.lock_HA("ns", "nslcmops", nslcmop_id
)
7884 if not task_is_locked_by_me
:
7886 logging_text
= "Task ns={} migrate ".format(nsr_id
)
7887 self
.logger
.debug(logging_text
+ "Enter")
7888 # get all needed from database
7890 db_nslcmop_update
= {}
7891 nslcmop_operation_state
= None
7895 # in case of error, indicates what part of scale was failed to put nsr at error status
7896 start_deploy
= time()
7899 # wait for any previous tasks in process
7900 step
= "Waiting for previous operations to terminate"
7901 await self
.lcm_tasks
.waitfor_related_HA("ns", "nslcmops", nslcmop_id
)
7903 self
._write
_ns
_status
(
7906 current_operation
="MIGRATING",
7907 current_operation_id
=nslcmop_id
,
7909 step
= "Getting nslcmop from database"
7911 step
+ " after having waited for previous tasks to be completed"
7913 db_nslcmop
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
})
7914 migrate_params
= db_nslcmop
.get("operationParams")
7917 target
.update(migrate_params
)
7918 desc
= await self
.RO
.migrate(nsr_id
, target
)
7919 self
.logger
.debug("RO return > {}".format(desc
))
7920 action_id
= desc
["action_id"]
7921 await self
._wait
_ng
_ro
(
7926 self
.timeout
.migrate
,
7927 operation
="migrate",
7929 except (ROclient
.ROClientException
, DbException
, LcmException
) as e
:
7930 self
.logger
.error("Exit Exception {}".format(e
))
7932 except asyncio
.CancelledError
:
7933 self
.logger
.error("Cancelled Exception while '{}'".format(step
))
7934 exc
= "Operation was cancelled"
7935 except Exception as e
:
7936 exc
= traceback
.format_exc()
7937 self
.logger
.critical(
7938 "Exit Exception {} {}".format(type(e
).__name
__, e
), exc_info
=True
7941 self
._write
_ns
_status
(
7944 current_operation
="IDLE",
7945 current_operation_id
=None,
7948 db_nslcmop_update
["detailed-status"] = "FAILED {}: {}".format(step
, exc
)
7949 nslcmop_operation_state
= "FAILED"
7951 nslcmop_operation_state
= "COMPLETED"
7952 db_nslcmop_update
["detailed-status"] = "Done"
7953 db_nsr_update
["detailed-status"] = "Done"
7955 self
._write
_op
_status
(
7959 operation_state
=nslcmop_operation_state
,
7960 other_update
=db_nslcmop_update
,
7962 if nslcmop_operation_state
:
7966 "nslcmop_id": nslcmop_id
,
7967 "operationState": nslcmop_operation_state
,
7969 await self
.msg
.aiowrite("ns", "migrated", msg
)
7970 except Exception as e
:
7972 logging_text
+ "kafka_write notification Exception {}".format(e
)
7974 self
.logger
.debug(logging_text
+ "Exit")
7975 self
.lcm_tasks
.remove("ns", nsr_id
, nslcmop_id
, "ns_migrate")
7977 async def heal(self
, nsr_id
, nslcmop_id
):
7981 :param nsr_id: ns instance to heal
7982 :param nslcmop_id: operation to run
7986 # Try to lock HA task here
7987 task_is_locked_by_me
= self
.lcm_tasks
.lock_HA("ns", "nslcmops", nslcmop_id
)
7988 if not task_is_locked_by_me
:
7991 logging_text
= "Task ns={} heal={} ".format(nsr_id
, nslcmop_id
)
7992 stage
= ["", "", ""]
7993 tasks_dict_info
= {}
7994 # ^ stage, step, VIM progress
7995 self
.logger
.debug(logging_text
+ "Enter")
7996 # get all needed from database
7998 db_nslcmop_update
= {}
8000 db_vnfrs
= {} # vnf's info indexed by _id
8002 old_operational_status
= ""
8003 old_config_status
= ""
8006 # wait for any previous tasks in process
8007 step
= "Waiting for previous operations to terminate"
8008 await self
.lcm_tasks
.waitfor_related_HA("ns", "nslcmops", nslcmop_id
)
8009 self
._write
_ns
_status
(
8012 current_operation
="HEALING",
8013 current_operation_id
=nslcmop_id
,
8016 step
= "Getting nslcmop from database"
8018 step
+ " after having waited for previous tasks to be completed"
8020 db_nslcmop
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
})
8022 step
= "Getting nsr from database"
8023 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
8024 old_operational_status
= db_nsr
["operational-status"]
8025 old_config_status
= db_nsr
["config-status"]
8028 "_admin.deployed.RO.operational-status": "healing",
8030 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
8032 step
= "Sending heal order to VIM"
8034 logging_text
=logging_text
,
8036 db_nslcmop
=db_nslcmop
,
8041 stage
[1] = "Getting nsd={} from db.".format(db_nsr
["nsd-id"])
8042 self
.logger
.debug(logging_text
+ stage
[1])
8043 nsd
= self
.db
.get_one("nsds", {"_id": db_nsr
["nsd-id"]})
8044 self
.fs
.sync(db_nsr
["nsd-id"])
8046 # read from db: vnfr's of this ns
8047 step
= "Getting vnfrs from db"
8048 db_vnfrs_list
= self
.db
.get_list("vnfrs", {"nsr-id-ref": nsr_id
})
8049 for vnfr
in db_vnfrs_list
:
8050 db_vnfrs
[vnfr
["_id"]] = vnfr
8051 self
.logger
.debug("ns.heal db_vnfrs={}".format(db_vnfrs
))
8053 # Check for each target VNF
8054 target_list
= db_nslcmop
.get("operationParams", {}).get("healVnfData", {})
8055 for target_vnf
in target_list
:
8056 # Find this VNF in the list from DB
8057 vnfr_id
= target_vnf
.get("vnfInstanceId", None)
8059 db_vnfr
= db_vnfrs
[vnfr_id
]
8060 vnfd_id
= db_vnfr
.get("vnfd-id")
8061 vnfd_ref
= db_vnfr
.get("vnfd-ref")
8062 vnfd
= self
.db
.get_one("vnfds", {"_id": vnfd_id
})
8063 base_folder
= vnfd
["_admin"]["storage"]
8068 nsi_id
= None # TODO put nsi_id when this nsr belongs to a NSI
8069 member_vnf_index
= db_vnfr
.get("member-vnf-index-ref")
8071 # Check each target VDU and deploy N2VC
8072 target_vdu_list
= target_vnf
.get("additionalParams", {}).get(
8075 if not target_vdu_list
:
8076 # Codigo nuevo para crear diccionario
8077 target_vdu_list
= []
8078 for existing_vdu
in db_vnfr
.get("vdur"):
8079 vdu_name
= existing_vdu
.get("vdu-name", None)
8080 vdu_index
= existing_vdu
.get("count-index", 0)
8081 vdu_run_day1
= target_vnf
.get("additionalParams", {}).get(
8084 vdu_to_be_healed
= {
8086 "count-index": vdu_index
,
8087 "run-day1": vdu_run_day1
,
8089 target_vdu_list
.append(vdu_to_be_healed
)
8090 for target_vdu
in target_vdu_list
:
8091 deploy_params_vdu
= target_vdu
8092 # Set run-day1 vnf level value if not vdu level value exists
8093 if not deploy_params_vdu
.get("run-day1") and target_vnf
.get(
8094 "additionalParams", {}
8096 deploy_params_vdu
["run-day1"] = target_vnf
[
8099 vdu_name
= target_vdu
.get("vdu-id", None)
8100 # TODO: Get vdu_id from vdud.
8102 # For multi instance VDU count-index is mandatory
8103 # For single session VDU count-indes is 0
8104 vdu_index
= target_vdu
.get("count-index", 0)
8106 # n2vc_redesign STEP 3 to 6 Deploy N2VC
8107 stage
[1] = "Deploying Execution Environments."
8108 self
.logger
.debug(logging_text
+ stage
[1])
8110 # VNF Level charm. Normal case when proxy charms.
8111 # If target instance is management machine continue with actions: recreate EE for native charms or reinject juju key for proxy charms.
8112 descriptor_config
= get_configuration(vnfd
, vnfd_ref
)
8113 if descriptor_config
:
8114 # Continue if healed machine is management machine
8115 vnf_ip_address
= db_vnfr
.get("ip-address")
8116 target_instance
= None
8117 for instance
in db_vnfr
.get("vdur", None):
8119 instance
["vdu-name"] == vdu_name
8120 and instance
["count-index"] == vdu_index
8122 target_instance
= instance
8124 if vnf_ip_address
== target_instance
.get("ip-address"):
8126 logging_text
=logging_text
8127 + "member_vnf_index={}, vdu_name={}, vdu_index={} ".format(
8128 member_vnf_index
, vdu_name
, vdu_index
8132 nslcmop_id
=nslcmop_id
,
8138 member_vnf_index
=member_vnf_index
,
8141 deploy_params
=deploy_params_vdu
,
8142 descriptor_config
=descriptor_config
,
8143 base_folder
=base_folder
,
8144 task_instantiation_info
=tasks_dict_info
,
8148 # VDU Level charm. Normal case with native charms.
8149 descriptor_config
= get_configuration(vnfd
, vdu_name
)
8150 if descriptor_config
:
8152 logging_text
=logging_text
8153 + "member_vnf_index={}, vdu_name={}, vdu_index={} ".format(
8154 member_vnf_index
, vdu_name
, vdu_index
8158 nslcmop_id
=nslcmop_id
,
8164 member_vnf_index
=member_vnf_index
,
8165 vdu_index
=vdu_index
,
8167 deploy_params
=deploy_params_vdu
,
8168 descriptor_config
=descriptor_config
,
8169 base_folder
=base_folder
,
8170 task_instantiation_info
=tasks_dict_info
,
8175 ROclient
.ROClientException
,
8180 self
.logger
.error(logging_text
+ "Exit Exception {}".format(e
))
8182 except asyncio
.CancelledError
:
8184 logging_text
+ "Cancelled Exception while '{}'".format(step
)
8186 exc
= "Operation was cancelled"
8187 except Exception as e
:
8188 exc
= traceback
.format_exc()
8189 self
.logger
.critical(
8190 logging_text
+ "Exit Exception {} {}".format(type(e
).__name
__, e
),
8196 error_list
.append(str(exc
))
8199 stage
[1] = "Waiting for healing pending tasks."
8200 self
.logger
.debug(logging_text
+ stage
[1])
8201 exc
= await self
._wait
_for
_tasks
(
8204 self
.timeout
.ns_deploy
,
8209 except asyncio
.CancelledError
:
8210 error_list
.append("Cancelled")
8211 await self
._cancel
_pending
_tasks
(logging_text
, tasks_dict_info
)
8212 await self
._wait
_for
_tasks
(
8215 self
.timeout
.ns_deploy
,
8221 error_detail
= "; ".join(error_list
)
8224 ] = error_description_nslcmop
= "FAILED {}: {}".format(
8227 nslcmop_operation_state
= "FAILED"
8229 db_nsr_update
["operational-status"] = old_operational_status
8230 db_nsr_update
["config-status"] = old_config_status
8233 ] = "FAILED healing nslcmop={} {}: {}".format(
8234 nslcmop_id
, step
, error_detail
8236 for task
, task_name
in tasks_dict_info
.items():
8237 if not task
.done() or task
.cancelled() or task
.exception():
8238 if task_name
.startswith(self
.task_name_deploy_vca
):
8239 # A N2VC task is pending
8240 db_nsr_update
["config-status"] = "failed"
8242 # RO task is pending
8243 db_nsr_update
["operational-status"] = "failed"
8245 error_description_nslcmop
= None
8246 nslcmop_operation_state
= "COMPLETED"
8247 db_nslcmop_update
["detailed-status"] = "Done"
8248 db_nsr_update
["detailed-status"] = "Done"
8249 db_nsr_update
["operational-status"] = "running"
8250 db_nsr_update
["config-status"] = "configured"
8252 self
._write
_op
_status
(
8255 error_message
=error_description_nslcmop
,
8256 operation_state
=nslcmop_operation_state
,
8257 other_update
=db_nslcmop_update
,
8260 self
._write
_ns
_status
(
8263 current_operation
="IDLE",
8264 current_operation_id
=None,
8265 other_update
=db_nsr_update
,
8268 if nslcmop_operation_state
:
8272 "nslcmop_id": nslcmop_id
,
8273 "operationState": nslcmop_operation_state
,
8275 await self
.msg
.aiowrite("ns", "healed", msg
)
8276 except Exception as e
:
8278 logging_text
+ "kafka_write notification Exception {}".format(e
)
8280 self
.logger
.debug(logging_text
+ "Exit")
8281 self
.lcm_tasks
.remove("ns", nsr_id
, nslcmop_id
, "ns_heal")
8292 :param logging_text: preffix text to use at logging
8293 :param nsr_id: nsr identity
8294 :param db_nslcmop: database content of ns operation, in this case, 'instantiate'
8295 :param stage: list with 3 items: [general stage, tasks, vim_specific]. This task will write over vim_specific
8296 :return: None or exception
8299 def get_vim_account(vim_account_id
):
8301 if vim_account_id
in db_vims
:
8302 return db_vims
[vim_account_id
]
8303 db_vim
= self
.db
.get_one("vim_accounts", {"_id": vim_account_id
})
8304 db_vims
[vim_account_id
] = db_vim
8309 ns_params
= db_nslcmop
.get("operationParams")
8310 if ns_params
and ns_params
.get("timeout_ns_heal"):
8311 timeout_ns_heal
= ns_params
["timeout_ns_heal"]
8313 timeout_ns_heal
= self
.timeout
.ns_heal
8317 nslcmop_id
= db_nslcmop
["_id"]
8319 "action_id": nslcmop_id
,
8321 self
.logger
.warning(
8322 "db_nslcmop={} and timeout_ns_heal={}".format(
8323 db_nslcmop
, timeout_ns_heal
8326 target
.update(db_nslcmop
.get("operationParams", {}))
8328 self
.logger
.debug("Send to RO > nsr_id={} target={}".format(nsr_id
, target
))
8329 desc
= await self
.RO
.recreate(nsr_id
, target
)
8330 self
.logger
.debug("RO return > {}".format(desc
))
8331 action_id
= desc
["action_id"]
8332 # waits for RO to complete because Reinjecting juju key at ro can find VM in state Deleted
8333 await self
._wait
_ng
_ro
(
8340 operation
="healing",
8345 "_admin.deployed.RO.operational-status": "running",
8346 "detailed-status": " ".join(stage
),
8348 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
8349 self
._write
_op
_status
(nslcmop_id
, stage
)
8351 logging_text
+ "ns healed at RO. RO_id={}".format(action_id
)
8354 except Exception as e
:
8355 stage
[2] = "ERROR healing at VIM"
8356 # self.set_vnfr_at_error(db_vnfrs, str(e))
8358 "Error healing at VIM {}".format(e
),
8359 exc_info
=not isinstance(
8362 ROclient
.ROClientException
,
8388 task_instantiation_info
,
8391 # launch instantiate_N2VC in a asyncio task and register task object
8392 # Look where information of this charm is at database <nsrs>._admin.deployed.VCA
8393 # if not found, create one entry and update database
8394 # fill db_nsr._admin.deployed.VCA.<index>
8397 logging_text
+ "_deploy_n2vc vnfd_id={}, vdu_id={}".format(vnfd_id
, vdu_id
)
8401 get_charm_name
= False
8402 if "execution-environment-list" in descriptor_config
:
8403 ee_list
= descriptor_config
.get("execution-environment-list", [])
8404 elif "juju" in descriptor_config
:
8405 ee_list
= [descriptor_config
] # ns charms
8406 if "execution-environment-list" not in descriptor_config
:
8407 # charm name is only required for ns charms
8408 get_charm_name
= True
8409 else: # other types as script are not supported
8412 for ee_item
in ee_list
:
8415 + "_deploy_n2vc ee_item juju={}, helm={}".format(
8416 ee_item
.get("juju"), ee_item
.get("helm-chart")
8419 ee_descriptor_id
= ee_item
.get("id")
8420 if ee_item
.get("juju"):
8421 vca_name
= ee_item
["juju"].get("charm")
8423 charm_name
= self
.find_charm_name(db_nsr
, str(vca_name
))
8426 if ee_item
["juju"].get("charm") is not None
8429 if ee_item
["juju"].get("cloud") == "k8s":
8430 vca_type
= "k8s_proxy_charm"
8431 elif ee_item
["juju"].get("proxy") is False:
8432 vca_type
= "native_charm"
8433 elif ee_item
.get("helm-chart"):
8434 vca_name
= ee_item
["helm-chart"]
8435 vca_type
= "helm-v3"
8438 logging_text
+ "skipping non juju neither charm configuration"
8443 for vca_index
, vca_deployed
in enumerate(
8444 db_nsr
["_admin"]["deployed"]["VCA"]
8446 if not vca_deployed
:
8449 vca_deployed
.get("member-vnf-index") == member_vnf_index
8450 and vca_deployed
.get("vdu_id") == vdu_id
8451 and vca_deployed
.get("kdu_name") == kdu_name
8452 and vca_deployed
.get("vdu_count_index", 0) == vdu_index
8453 and vca_deployed
.get("ee_descriptor_id") == ee_descriptor_id
8457 # not found, create one.
8459 "ns" if not member_vnf_index
else "vnf/{}".format(member_vnf_index
)
8462 target
+= "/vdu/{}/{}".format(vdu_id
, vdu_index
or 0)
8464 target
+= "/kdu/{}".format(kdu_name
)
8466 "target_element": target
,
8467 # ^ target_element will replace member-vnf-index, kdu_name, vdu_id ... in a single string
8468 "member-vnf-index": member_vnf_index
,
8470 "kdu_name": kdu_name
,
8471 "vdu_count_index": vdu_index
,
8472 "operational-status": "init", # TODO revise
8473 "detailed-status": "", # TODO revise
8474 "step": "initial-deploy", # TODO revise
8476 "vdu_name": vdu_name
,
8478 "ee_descriptor_id": ee_descriptor_id
,
8479 "charm_name": charm_name
,
8483 # create VCA and configurationStatus in db
8485 "_admin.deployed.VCA.{}".format(vca_index
): vca_deployed
,
8486 "configurationStatus.{}".format(vca_index
): dict(),
8488 self
.update_db_2("nsrs", nsr_id
, db_dict
)
8490 db_nsr
["_admin"]["deployed"]["VCA"].append(vca_deployed
)
8492 self
.logger
.debug("N2VC > NSR_ID > {}".format(nsr_id
))
8493 self
.logger
.debug("N2VC > DB_NSR > {}".format(db_nsr
))
8494 self
.logger
.debug("N2VC > VCA_DEPLOYED > {}".format(vca_deployed
))
8497 task_n2vc
= asyncio
.ensure_future(
8499 logging_text
=logging_text
,
8500 vca_index
=vca_index
,
8506 vdu_index
=vdu_index
,
8507 deploy_params
=deploy_params
,
8508 config_descriptor
=descriptor_config
,
8509 base_folder
=base_folder
,
8510 nslcmop_id
=nslcmop_id
,
8514 ee_config_descriptor
=ee_item
,
8517 self
.lcm_tasks
.register(
8521 "instantiate_N2VC-{}".format(vca_index
),
8524 task_instantiation_info
[
8526 ] = self
.task_name_deploy_vca
+ " {}.{}".format(
8527 member_vnf_index
or "", vdu_id
or ""
8530 async def heal_N2VC(
8547 ee_config_descriptor
,
8549 nsr_id
= db_nsr
["_id"]
8550 db_update_entry
= "_admin.deployed.VCA.{}.".format(vca_index
)
8551 vca_deployed_list
= db_nsr
["_admin"]["deployed"]["VCA"]
8552 vca_deployed
= db_nsr
["_admin"]["deployed"]["VCA"][vca_index
]
8553 osm_config
= {"osm": {"ns_id": db_nsr
["_id"]}}
8555 "collection": "nsrs",
8556 "filter": {"_id": nsr_id
},
8557 "path": db_update_entry
,
8562 element_under_configuration
= nsr_id
8566 vnfr_id
= db_vnfr
["_id"]
8567 osm_config
["osm"]["vnf_id"] = vnfr_id
8569 namespace
= "{nsi}.{ns}".format(nsi
=nsi_id
if nsi_id
else "", ns
=nsr_id
)
8571 if vca_type
== "native_charm":
8574 index_number
= vdu_index
or 0
8577 element_type
= "VNF"
8578 element_under_configuration
= vnfr_id
8579 namespace
+= ".{}-{}".format(vnfr_id
, index_number
)
8581 namespace
+= ".{}-{}".format(vdu_id
, index_number
)
8582 element_type
= "VDU"
8583 element_under_configuration
= "{}-{}".format(vdu_id
, index_number
)
8584 osm_config
["osm"]["vdu_id"] = vdu_id
8586 namespace
+= ".{}".format(kdu_name
)
8587 element_type
= "KDU"
8588 element_under_configuration
= kdu_name
8589 osm_config
["osm"]["kdu_name"] = kdu_name
8592 if base_folder
["pkg-dir"]:
8593 artifact_path
= "{}/{}/{}/{}".format(
8594 base_folder
["folder"],
8595 base_folder
["pkg-dir"],
8598 in ("native_charm", "lxc_proxy_charm", "k8s_proxy_charm")
8603 artifact_path
= "{}/Scripts/{}/{}/".format(
8604 base_folder
["folder"],
8607 in ("native_charm", "lxc_proxy_charm", "k8s_proxy_charm")
8612 self
.logger
.debug("Artifact path > {}".format(artifact_path
))
8614 # get initial_config_primitive_list that applies to this element
8615 initial_config_primitive_list
= config_descriptor
.get(
8616 "initial-config-primitive"
8620 "Initial config primitive list > {}".format(
8621 initial_config_primitive_list
8625 # add config if not present for NS charm
8626 ee_descriptor_id
= ee_config_descriptor
.get("id")
8627 self
.logger
.debug("EE Descriptor > {}".format(ee_descriptor_id
))
8628 initial_config_primitive_list
= get_ee_sorted_initial_config_primitive_list(
8629 initial_config_primitive_list
, vca_deployed
, ee_descriptor_id
8633 "Initial config primitive list #2 > {}".format(
8634 initial_config_primitive_list
8637 # n2vc_redesign STEP 3.1
8638 # find old ee_id if exists
8639 ee_id
= vca_deployed
.get("ee_id")
8641 vca_id
= self
.get_vca_id(db_vnfr
, db_nsr
)
8642 # create or register execution environment in VCA. Only for native charms when healing
8643 if vca_type
== "native_charm":
8644 step
= "Waiting to VM being up and getting IP address"
8645 self
.logger
.debug(logging_text
+ step
)
8646 rw_mgmt_ip
= await self
.wait_vm_up_insert_key_ro(
8655 credentials
= {"hostname": rw_mgmt_ip
}
8657 username
= deep_get(
8658 config_descriptor
, ("config-access", "ssh-access", "default-user")
8660 # TODO remove this when changes on IM regarding config-access:ssh-access:default-user were
8661 # merged. Meanwhile let's get username from initial-config-primitive
8662 if not username
and initial_config_primitive_list
:
8663 for config_primitive
in initial_config_primitive_list
:
8664 for param
in config_primitive
.get("parameter", ()):
8665 if param
["name"] == "ssh-username":
8666 username
= param
["value"]
8670 "Cannot determine the username neither with 'initial-config-primitive' nor with "
8671 "'config-access.ssh-access.default-user'"
8673 credentials
["username"] = username
8675 # n2vc_redesign STEP 3.2
8676 # TODO: Before healing at RO it is needed to destroy native charm units to be deleted.
8677 self
._write
_configuration
_status
(
8679 vca_index
=vca_index
,
8680 status
="REGISTERING",
8681 element_under_configuration
=element_under_configuration
,
8682 element_type
=element_type
,
8685 step
= "register execution environment {}".format(credentials
)
8686 self
.logger
.debug(logging_text
+ step
)
8687 ee_id
= await self
.vca_map
[vca_type
].register_execution_environment(
8688 credentials
=credentials
,
8689 namespace
=namespace
,
8694 # update ee_id en db
8696 "_admin.deployed.VCA.{}.ee_id".format(vca_index
): ee_id
,
8698 self
.update_db_2("nsrs", nsr_id
, db_dict_ee_id
)
8700 # for compatibility with MON/POL modules, the need model and application name at database
8701 # TODO ask MON/POL if needed to not assuming anymore the format "model_name.application_name"
8702 # Not sure if this need to be done when healing
8704 ee_id_parts = ee_id.split(".")
8705 db_nsr_update = {db_update_entry + "ee_id": ee_id}
8706 if len(ee_id_parts) >= 2:
8707 model_name = ee_id_parts[0]
8708 application_name = ee_id_parts[1]
8709 db_nsr_update[db_update_entry + "model"] = model_name
8710 db_nsr_update[db_update_entry + "application"] = application_name
8713 # n2vc_redesign STEP 3.3
8714 # Install configuration software. Only for native charms.
8715 step
= "Install configuration Software"
8717 self
._write
_configuration
_status
(
8719 vca_index
=vca_index
,
8720 status
="INSTALLING SW",
8721 element_under_configuration
=element_under_configuration
,
8722 element_type
=element_type
,
8723 # other_update=db_nsr_update,
8727 # TODO check if already done
8728 self
.logger
.debug(logging_text
+ step
)
8730 if vca_type
== "native_charm":
8731 config_primitive
= next(
8732 (p
for p
in initial_config_primitive_list
if p
["name"] == "config"),
8735 if config_primitive
:
8736 config
= self
._map
_primitive
_params
(
8737 config_primitive
, {}, deploy_params
8739 await self
.vca_map
[vca_type
].install_configuration_sw(
8741 artifact_path
=artifact_path
,
8749 # write in db flag of configuration_sw already installed
8751 "nsrs", nsr_id
, {db_update_entry
+ "config_sw_installed": True}
8754 # Not sure if this need to be done when healing
8756 # add relations for this VCA (wait for other peers related with this VCA)
8757 await self._add_vca_relations(
8758 logging_text=logging_text,
8761 vca_index=vca_index,
8765 # if SSH access is required, then get execution environment SSH public
8766 # if native charm we have waited already to VM be UP
8767 if vca_type
in ("k8s_proxy_charm", "lxc_proxy_charm", "helm-v3"):
8770 # self.logger.debug("get ssh key block")
8772 config_descriptor
, ("config-access", "ssh-access", "required")
8774 # self.logger.debug("ssh key needed")
8775 # Needed to inject a ssh key
8778 ("config-access", "ssh-access", "default-user"),
8780 step
= "Install configuration Software, getting public ssh key"
8781 pub_key
= await self
.vca_map
[vca_type
].get_ee_ssh_public__key(
8782 ee_id
=ee_id
, db_dict
=db_dict
, vca_id
=vca_id
8785 step
= "Insert public key into VM user={} ssh_key={}".format(
8789 # self.logger.debug("no need to get ssh key")
8790 step
= "Waiting to VM being up and getting IP address"
8791 self
.logger
.debug(logging_text
+ step
)
8793 # n2vc_redesign STEP 5.1
8794 # wait for RO (ip-address) Insert pub_key into VM
8795 # IMPORTANT: We need do wait for RO to complete healing operation.
8796 await self
._wait
_heal
_ro
(nsr_id
, self
.timeout
.ns_heal
)
8799 rw_mgmt_ip
= await self
.wait_kdu_up(
8800 logging_text
, nsr_id
, vnfr_id
, kdu_name
8803 rw_mgmt_ip
= await self
.wait_vm_up_insert_key_ro(
8813 rw_mgmt_ip
= None # This is for a NS configuration
8815 self
.logger
.debug(logging_text
+ " VM_ip_address={}".format(rw_mgmt_ip
))
8817 # store rw_mgmt_ip in deploy params for later replacement
8818 deploy_params
["rw_mgmt_ip"] = rw_mgmt_ip
8821 # get run-day1 operation parameter
8822 runDay1
= deploy_params
.get("run-day1", False)
8824 "Healing vnf={}, vdu={}, runDay1 ={}".format(vnfr_id
, vdu_id
, runDay1
)
8827 # n2vc_redesign STEP 6 Execute initial config primitive
8828 step
= "execute initial config primitive"
8830 # wait for dependent primitives execution (NS -> VNF -> VDU)
8831 if initial_config_primitive_list
:
8832 await self
._wait
_dependent
_n
2vc
(
8833 nsr_id
, vca_deployed_list
, vca_index
8836 # stage, in function of element type: vdu, kdu, vnf or ns
8837 my_vca
= vca_deployed_list
[vca_index
]
8838 if my_vca
.get("vdu_id") or my_vca
.get("kdu_name"):
8840 stage
[0] = "Stage 3/5: running Day-1 primitives for VDU."
8841 elif my_vca
.get("member-vnf-index"):
8843 stage
[0] = "Stage 4/5: running Day-1 primitives for VNF."
8846 stage
[0] = "Stage 5/5: running Day-1 primitives for NS."
8848 self
._write
_configuration
_status
(
8849 nsr_id
=nsr_id
, vca_index
=vca_index
, status
="EXECUTING PRIMITIVE"
8852 self
._write
_op
_status
(op_id
=nslcmop_id
, stage
=stage
)
8854 check_if_terminated_needed
= True
8855 for initial_config_primitive
in initial_config_primitive_list
:
8856 # adding information on the vca_deployed if it is a NS execution environment
8857 if not vca_deployed
["member-vnf-index"]:
8858 deploy_params
["ns_config_info"] = json
.dumps(
8859 self
._get
_ns
_config
_info
(nsr_id
)
8861 # TODO check if already done
8862 primitive_params_
= self
._map
_primitive
_params
(
8863 initial_config_primitive
, {}, deploy_params
8866 step
= "execute primitive '{}' params '{}'".format(
8867 initial_config_primitive
["name"], primitive_params_
8869 self
.logger
.debug(logging_text
+ step
)
8870 await self
.vca_map
[vca_type
].exec_primitive(
8872 primitive_name
=initial_config_primitive
["name"],
8873 params_dict
=primitive_params_
,
8878 # Once some primitive has been exec, check and write at db if it needs to exec terminated primitives
8879 if check_if_terminated_needed
:
8880 if config_descriptor
.get("terminate-config-primitive"):
8884 {db_update_entry
+ "needed_terminate": True},
8886 check_if_terminated_needed
= False
8888 # TODO register in database that primitive is done
8890 # STEP 7 Configure metrics
8891 # Not sure if this need to be done when healing
8893 if vca_type == "helm" or vca_type == "helm-v3":
8894 prometheus_jobs = await self.extract_prometheus_scrape_jobs(
8896 artifact_path=artifact_path,
8897 ee_config_descriptor=ee_config_descriptor,
8900 target_ip=rw_mgmt_ip,
8906 {db_update_entry + "prometheus_jobs": prometheus_jobs},
8909 for job in prometheus_jobs:
8912 {"job_name": job["job_name"]},
8915 fail_on_empty=False,
8919 step
= "instantiated at VCA"
8920 self
.logger
.debug(logging_text
+ step
)
8922 self
._write
_configuration
_status
(
8923 nsr_id
=nsr_id
, vca_index
=vca_index
, status
="READY"
8926 except Exception as e
: # TODO not use Exception but N2VC exception
8927 # self.update_db_2("nsrs", nsr_id, {db_update_entry + "instantiation": "FAILED"})
8929 e
, (DbException
, N2VCException
, LcmException
, asyncio
.CancelledError
)
8932 "Exception while {} : {}".format(step
, e
), exc_info
=True
8934 self
._write
_configuration
_status
(
8935 nsr_id
=nsr_id
, vca_index
=vca_index
, status
="BROKEN"
8937 raise LcmException("{} {}".format(step
, e
)) from e
8939 async def _wait_heal_ro(
8945 while time() <= start_time
+ timeout
:
8946 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
8947 operational_status_ro
= db_nsr
["_admin"]["deployed"]["RO"][
8948 "operational-status"
8950 self
.logger
.debug("Wait Heal RO > {}".format(operational_status_ro
))
8951 if operational_status_ro
!= "healing":
8953 await asyncio
.sleep(15)
8954 else: # timeout_ns_deploy
8955 raise NgRoException("Timeout waiting ns to deploy")
8957 async def vertical_scale(self
, nsr_id
, nslcmop_id
):
8959 Vertical Scale the VDUs in a NS
8961 :param: nsr_id: NS Instance ID
8962 :param: nslcmop_id: nslcmop ID of migrate
8965 # Try to lock HA task here
8966 task_is_locked_by_me
= self
.lcm_tasks
.lock_HA("ns", "nslcmops", nslcmop_id
)
8967 if not task_is_locked_by_me
:
8969 logging_text
= "Task ns={} vertical scale ".format(nsr_id
)
8970 self
.logger
.debug(logging_text
+ "Enter")
8971 # get all needed from database
8973 db_nslcmop_update
= {}
8974 nslcmop_operation_state
= None
8978 # in case of error, indicates what part of scale was failed to put nsr at error status
8979 start_deploy
= time()
8982 # wait for any previous tasks in process
8983 step
= "Waiting for previous operations to terminate"
8984 await self
.lcm_tasks
.waitfor_related_HA("ns", "nslcmops", nslcmop_id
)
8986 self
._write
_ns
_status
(
8989 current_operation
="VerticalScale",
8990 current_operation_id
=nslcmop_id
,
8992 step
= "Getting nslcmop from database"
8994 step
+ " after having waited for previous tasks to be completed"
8996 db_nslcmop
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
})
8997 operationParams
= db_nslcmop
.get("operationParams")
8999 target
.update(operationParams
)
9000 desc
= await self
.RO
.vertical_scale(nsr_id
, target
)
9001 self
.logger
.debug("RO return > {}".format(desc
))
9002 action_id
= desc
["action_id"]
9003 await self
._wait
_ng
_ro
(
9008 self
.timeout
.verticalscale
,
9009 operation
="verticalscale",
9011 except (ROclient
.ROClientException
, DbException
, LcmException
) as e
:
9012 self
.logger
.error("Exit Exception {}".format(e
))
9014 except asyncio
.CancelledError
:
9015 self
.logger
.error("Cancelled Exception while '{}'".format(step
))
9016 exc
= "Operation was cancelled"
9017 except Exception as e
:
9018 exc
= traceback
.format_exc()
9019 self
.logger
.critical(
9020 "Exit Exception {} {}".format(type(e
).__name
__, e
), exc_info
=True
9023 self
._write
_ns
_status
(
9026 current_operation
="IDLE",
9027 current_operation_id
=None,
9030 db_nslcmop_update
["detailed-status"] = "FAILED {}: {}".format(step
, exc
)
9031 nslcmop_operation_state
= "FAILED"
9033 nslcmop_operation_state
= "COMPLETED"
9034 db_nslcmop_update
["detailed-status"] = "Done"
9035 db_nsr_update
["detailed-status"] = "Done"
9037 self
._write
_op
_status
(
9041 operation_state
=nslcmop_operation_state
,
9042 other_update
=db_nslcmop_update
,
9044 if nslcmop_operation_state
:
9048 "nslcmop_id": nslcmop_id
,
9049 "operationState": nslcmop_operation_state
,
9051 await self
.msg
.aiowrite("ns", "verticalscaled", msg
)
9052 except Exception as e
:
9054 logging_text
+ "kafka_write notification Exception {}".format(e
)
9056 self
.logger
.debug(logging_text
+ "Exit")
9057 self
.lcm_tasks
.remove("ns", nsr_id
, nslcmop_id
, "ns_verticalscale")