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_helm_conn
import K8sHelmConnector
102 from n2vc
.k8s_helm3_conn
import K8sHelm3Connector
103 from n2vc
.k8s_juju_conn
import K8sJujuConnector
105 from osm_common
.dbbase
import DbException
106 from osm_common
.fsbase
import FsException
108 from osm_lcm
.data_utils
.database
.database
import Database
109 from osm_lcm
.data_utils
.filesystem
.filesystem
import Filesystem
110 from osm_lcm
.data_utils
.wim
import (
112 get_target_wim_attrs
,
113 select_feasible_wim_account
,
116 from n2vc
.n2vc_juju_conn
import N2VCJujuConnector
117 from n2vc
.exceptions
import N2VCException
, N2VCNotFound
, K8sException
119 from osm_lcm
.lcm_helm_conn
import LCMHelmConn
120 from osm_lcm
.osm_config
import OsmConfigBuilder
121 from osm_lcm
.prometheus
import parse_job
123 from copy
import copy
, deepcopy
124 from time
import time
125 from uuid
import uuid4
127 from random
import SystemRandom
129 __author__
= "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
132 class NsLcm(LcmBase
):
133 SUBOPERATION_STATUS_NOT_FOUND
= -1
134 SUBOPERATION_STATUS_NEW
= -2
135 SUBOPERATION_STATUS_SKIP
= -3
136 EE_TLS_NAME
= "ee-tls"
137 task_name_deploy_vca
= "Deploying VCA"
139 def __init__(self
, msg
, lcm_tasks
, config
: LcmCfg
):
141 Init, Connect to database, filesystem storage, and messaging
142 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
145 super().__init
__(msg
=msg
, logger
=logging
.getLogger("lcm.ns"))
147 self
.db
= Database().instance
.db
148 self
.fs
= Filesystem().instance
.fs
149 self
.lcm_tasks
= lcm_tasks
150 self
.timeout
= config
.timeout
151 self
.ro_config
= config
.RO
152 self
.vca_config
= config
.VCA
154 # create N2VC connector
155 self
.n2vc
= N2VCJujuConnector(
157 on_update_db
=self
._on
_update
_n
2vc
_db
,
162 self
.conn_helm_ee
= LCMHelmConn(
164 vca_config
=self
.vca_config
,
165 on_update_db
=self
._on
_update
_n
2vc
_db
,
168 self
.k8sclusterhelm2
= K8sHelmConnector(
169 kubectl_command
=self
.vca_config
.kubectlpath
,
170 helm_command
=self
.vca_config
.helmpath
,
177 self
.k8sclusterhelm3
= K8sHelm3Connector(
178 kubectl_command
=self
.vca_config
.kubectlpath
,
179 helm_command
=self
.vca_config
.helm3path
,
186 self
.k8sclusterjuju
= K8sJujuConnector(
187 kubectl_command
=self
.vca_config
.kubectlpath
,
188 juju_command
=self
.vca_config
.jujupath
,
190 on_update_db
=self
._on
_update
_k
8s
_db
,
195 self
.k8scluster_map
= {
196 "helm-chart": self
.k8sclusterhelm2
,
197 "helm-chart-v3": self
.k8sclusterhelm3
,
198 "chart": self
.k8sclusterhelm3
,
199 "juju-bundle": self
.k8sclusterjuju
,
200 "juju": self
.k8sclusterjuju
,
204 "lxc_proxy_charm": self
.n2vc
,
205 "native_charm": self
.n2vc
,
206 "k8s_proxy_charm": self
.n2vc
,
207 "helm": self
.conn_helm_ee
,
208 "helm-v3": self
.conn_helm_ee
,
212 self
.RO
= NgRoClient(**self
.ro_config
.to_dict())
214 self
.op_status_map
= {
215 "instantiation": self
.RO
.status
,
216 "termination": self
.RO
.status
,
217 "migrate": self
.RO
.status
,
218 "healing": self
.RO
.recreate_status
,
219 "verticalscale": self
.RO
.status
,
220 "start_stop_rebuild": self
.RO
.status
,
224 def increment_ip_mac(ip_mac
, vm_index
=1):
225 if not isinstance(ip_mac
, str):
228 # try with ipv4 look for last dot
229 i
= ip_mac
.rfind(".")
232 return "{}{}".format(ip_mac
[:i
], int(ip_mac
[i
:]) + vm_index
)
233 # try with ipv6 or mac look for last colon. Operate in hex
234 i
= ip_mac
.rfind(":")
237 # format in hex, len can be 2 for mac or 4 for ipv6
238 return ("{}{:0" + str(len(ip_mac
) - i
) + "x}").format(
239 ip_mac
[:i
], int(ip_mac
[i
:], 16) + vm_index
245 def _on_update_ro_db(self
, nsrs_id
, ro_descriptor
):
246 # self.logger.debug('_on_update_ro_db(nsrs_id={}'.format(nsrs_id))
249 # TODO filter RO descriptor fields...
253 # db_dict['deploymentStatus'] = yaml.dump(ro_descriptor, default_flow_style=False, indent=2)
254 db_dict
["deploymentStatus"] = ro_descriptor
255 self
.update_db_2("nsrs", nsrs_id
, db_dict
)
257 except Exception as e
:
259 "Cannot write database RO deployment for ns={} -> {}".format(nsrs_id
, e
)
262 async def _on_update_n2vc_db(self
, table
, filter, path
, updated_data
, vca_id
=None):
263 # remove last dot from path (if exists)
264 if path
.endswith("."):
267 # self.logger.debug('_on_update_n2vc_db(table={}, filter={}, path={}, updated_data={}'
268 # .format(table, filter, path, updated_data))
270 nsr_id
= filter.get("_id")
272 # read ns record from database
273 nsr
= self
.db
.get_one(table
="nsrs", q_filter
=filter)
274 current_ns_status
= nsr
.get("nsState")
276 # get vca status for NS
277 status_dict
= await self
.n2vc
.get_status(
278 namespace
="." + nsr_id
, yaml_format
=False, vca_id
=vca_id
283 db_dict
["vcaStatus"] = status_dict
285 # update configurationStatus for this VCA
287 vca_index
= int(path
[path
.rfind(".") + 1 :])
290 target_dict
=nsr
, key_list
=("_admin", "deployed", "VCA")
292 vca_status
= vca_list
[vca_index
].get("status")
294 configuration_status_list
= nsr
.get("configurationStatus")
295 config_status
= configuration_status_list
[vca_index
].get("status")
297 if config_status
== "BROKEN" and vca_status
!= "failed":
298 db_dict
["configurationStatus"][vca_index
] = "READY"
299 elif config_status
!= "BROKEN" and vca_status
== "failed":
300 db_dict
["configurationStatus"][vca_index
] = "BROKEN"
301 except Exception as e
:
302 # not update configurationStatus
303 self
.logger
.debug("Error updating vca_index (ignore): {}".format(e
))
305 # if nsState = 'READY' check if juju is reporting some error => nsState = 'DEGRADED'
306 # if nsState = 'DEGRADED' check if all is OK
308 if current_ns_status
in ("READY", "DEGRADED"):
309 error_description
= ""
311 if status_dict
.get("machines"):
312 for machine_id
in status_dict
.get("machines"):
313 machine
= status_dict
.get("machines").get(machine_id
)
314 # check machine agent-status
315 if machine
.get("agent-status"):
316 s
= machine
.get("agent-status").get("status")
319 error_description
+= (
320 "machine {} agent-status={} ; ".format(
324 # check machine instance status
325 if machine
.get("instance-status"):
326 s
= machine
.get("instance-status").get("status")
329 error_description
+= (
330 "machine {} instance-status={} ; ".format(
335 if status_dict
.get("applications"):
336 for app_id
in status_dict
.get("applications"):
337 app
= status_dict
.get("applications").get(app_id
)
338 # check application status
339 if app
.get("status"):
340 s
= app
.get("status").get("status")
343 error_description
+= (
344 "application {} status={} ; ".format(app_id
, s
)
347 if error_description
:
348 db_dict
["errorDescription"] = error_description
349 if current_ns_status
== "READY" and is_degraded
:
350 db_dict
["nsState"] = "DEGRADED"
351 if current_ns_status
== "DEGRADED" and not is_degraded
:
352 db_dict
["nsState"] = "READY"
355 self
.update_db_2("nsrs", nsr_id
, db_dict
)
357 except (asyncio
.CancelledError
, asyncio
.TimeoutError
):
359 except Exception as e
:
360 self
.logger
.warn("Error updating NS state for ns={}: {}".format(nsr_id
, e
))
362 async def _on_update_k8s_db(
363 self
, cluster_uuid
, kdu_instance
, filter=None, vca_id
=None, cluster_type
="juju"
366 Updating vca status in NSR record
367 :param cluster_uuid: UUID of a k8s cluster
368 :param kdu_instance: The unique name of the KDU instance
369 :param filter: To get nsr_id
370 :cluster_type: The cluster type (juju, k8s)
374 # self.logger.debug("_on_update_k8s_db(cluster_uuid={}, kdu_instance={}, filter={}"
375 # .format(cluster_uuid, kdu_instance, filter))
377 nsr_id
= filter.get("_id")
379 vca_status
= await self
.k8scluster_map
[cluster_type
].status_kdu(
380 cluster_uuid
=cluster_uuid
,
381 kdu_instance
=kdu_instance
,
383 complete_status
=True,
389 db_dict
["vcaStatus"] = {nsr_id
: vca_status
}
392 f
"Obtained VCA status for cluster type '{cluster_type}': {vca_status}"
396 self
.update_db_2("nsrs", nsr_id
, db_dict
)
397 except (asyncio
.CancelledError
, asyncio
.TimeoutError
):
399 except Exception as e
:
400 self
.logger
.warn("Error updating NS state for ns={}: {}".format(nsr_id
, e
))
403 def _parse_cloud_init(cloud_init_text
, additional_params
, vnfd_id
, vdu_id
):
406 undefined
=StrictUndefined
,
407 autoescape
=select_autoescape(default_for_string
=True, default
=True),
409 template
= env
.from_string(cloud_init_text
)
410 return template
.render(additional_params
or {})
411 except UndefinedError
as e
:
413 "Variable {} at vnfd[id={}]:vdu[id={}]:cloud-init/cloud-init-"
414 "file, must be provided in the instantiation parameters inside the "
415 "'additionalParamsForVnf/Vdu' block".format(e
, vnfd_id
, vdu_id
)
417 except (TemplateError
, TemplateNotFound
) as e
:
419 "Error parsing Jinja2 to cloud-init content at vnfd[id={}]:vdu[id={}]: {}".format(
424 def _get_vdu_cloud_init_content(self
, vdu
, vnfd
):
425 cloud_init_content
= cloud_init_file
= None
427 if vdu
.get("cloud-init-file"):
428 base_folder
= vnfd
["_admin"]["storage"]
429 if base_folder
["pkg-dir"]:
430 cloud_init_file
= "{}/{}/cloud_init/{}".format(
431 base_folder
["folder"],
432 base_folder
["pkg-dir"],
433 vdu
["cloud-init-file"],
436 cloud_init_file
= "{}/Scripts/cloud_init/{}".format(
437 base_folder
["folder"],
438 vdu
["cloud-init-file"],
440 with self
.fs
.file_open(cloud_init_file
, "r") as ci_file
:
441 cloud_init_content
= ci_file
.read()
442 elif vdu
.get("cloud-init"):
443 cloud_init_content
= vdu
["cloud-init"]
445 return cloud_init_content
446 except FsException
as e
:
448 "Error reading vnfd[id={}]:vdu[id={}]:cloud-init-file={}: {}".format(
449 vnfd
["id"], vdu
["id"], cloud_init_file
, e
453 def _get_vdu_additional_params(self
, db_vnfr
, vdu_id
):
455 (vdur
for vdur
in db_vnfr
.get("vdur") if vdu_id
== vdur
["vdu-id-ref"]), {}
457 additional_params
= vdur
.get("additionalParams")
458 return parse_yaml_strings(additional_params
)
460 def vnfd2RO(self
, vnfd
, new_id
=None, additionalParams
=None, nsrId
=None):
462 Converts creates a new vnfd descriptor for RO base on input OSM IM vnfd
463 :param vnfd: input vnfd
464 :param new_id: overrides vnf id if provided
465 :param additionalParams: Instantiation params for VNFs provided
466 :param nsrId: Id of the NSR
467 :return: copy of vnfd
469 vnfd_RO
= deepcopy(vnfd
)
470 # remove unused by RO configuration, monitoring, scaling and internal keys
471 vnfd_RO
.pop("_id", None)
472 vnfd_RO
.pop("_admin", None)
473 vnfd_RO
.pop("monitoring-param", None)
474 vnfd_RO
.pop("scaling-group-descriptor", None)
475 vnfd_RO
.pop("kdu", None)
476 vnfd_RO
.pop("k8s-cluster", None)
478 vnfd_RO
["id"] = new_id
480 # parse cloud-init or cloud-init-file with the provided variables using Jinja2
481 for vdu
in get_iterable(vnfd_RO
, "vdu"):
482 vdu
.pop("cloud-init-file", None)
483 vdu
.pop("cloud-init", None)
487 def ip_profile_2_RO(ip_profile
):
488 RO_ip_profile
= deepcopy(ip_profile
)
489 if "dns-server" in RO_ip_profile
:
490 if isinstance(RO_ip_profile
["dns-server"], list):
491 RO_ip_profile
["dns-address"] = []
492 for ds
in RO_ip_profile
.pop("dns-server"):
493 RO_ip_profile
["dns-address"].append(ds
["address"])
495 RO_ip_profile
["dns-address"] = RO_ip_profile
.pop("dns-server")
496 if RO_ip_profile
.get("ip-version") == "ipv4":
497 RO_ip_profile
["ip-version"] = "IPv4"
498 if RO_ip_profile
.get("ip-version") == "ipv6":
499 RO_ip_profile
["ip-version"] = "IPv6"
500 if "dhcp-params" in RO_ip_profile
:
501 RO_ip_profile
["dhcp"] = RO_ip_profile
.pop("dhcp-params")
504 def _get_ro_vim_id_for_vim_account(self
, vim_account
):
505 db_vim
= self
.db
.get_one("vim_accounts", {"_id": vim_account
})
506 if db_vim
["_admin"]["operationalState"] != "ENABLED":
508 "VIM={} is not available. operationalState={}".format(
509 vim_account
, db_vim
["_admin"]["operationalState"]
512 RO_vim_id
= db_vim
["_admin"]["deployed"]["RO"]
515 def get_ro_wim_id_for_wim_account(self
, wim_account
):
516 if isinstance(wim_account
, str):
517 db_wim
= self
.db
.get_one("wim_accounts", {"_id": wim_account
})
518 if db_wim
["_admin"]["operationalState"] != "ENABLED":
520 "WIM={} is not available. operationalState={}".format(
521 wim_account
, db_wim
["_admin"]["operationalState"]
524 RO_wim_id
= db_wim
["_admin"]["deployed"]["RO-account"]
529 def scale_vnfr(self
, db_vnfr
, vdu_create
=None, vdu_delete
=None, mark_delete
=False):
530 db_vdu_push_list
= []
532 db_update
= {"_admin.modified": time()}
534 for vdu_id
, vdu_count
in vdu_create
.items():
538 for vdur
in reversed(db_vnfr
["vdur"])
539 if vdur
["vdu-id-ref"] == vdu_id
544 # Read the template saved in the db:
546 "No vdur in the database. Using the vdur-template to scale"
548 vdur_template
= db_vnfr
.get("vdur-template")
549 if not vdur_template
:
551 "Error scaling OUT VNFR for {}. No vnfr or template exists".format(
555 vdur
= vdur_template
[0]
556 # Delete a template from the database after using it
559 {"_id": db_vnfr
["_id"]},
561 pull
={"vdur-template": {"_id": vdur
["_id"]}},
563 for count
in range(vdu_count
):
564 vdur_copy
= deepcopy(vdur
)
565 vdur_copy
["status"] = "BUILD"
566 vdur_copy
["status-detailed"] = None
567 vdur_copy
["ip-address"] = None
568 vdur_copy
["_id"] = str(uuid4())
569 vdur_copy
["count-index"] += count
+ 1
570 vdur_copy
["id"] = "{}-{}".format(
571 vdur_copy
["vdu-id-ref"], vdur_copy
["count-index"]
573 vdur_copy
.pop("vim_info", None)
574 for iface
in vdur_copy
["interfaces"]:
575 if iface
.get("fixed-ip"):
576 iface
["ip-address"] = self
.increment_ip_mac(
577 iface
["ip-address"], count
+ 1
580 iface
.pop("ip-address", None)
581 if iface
.get("fixed-mac"):
582 iface
["mac-address"] = self
.increment_ip_mac(
583 iface
["mac-address"], count
+ 1
586 iface
.pop("mac-address", None)
590 ) # only first vdu can be managment of vnf
591 db_vdu_push_list
.append(vdur_copy
)
592 # self.logger.debug("scale out, adding vdu={}".format(vdur_copy))
594 if len(db_vnfr
["vdur"]) == 1:
595 # The scale will move to 0 instances
597 "Scaling to 0 !, creating the template with the last vdur"
599 template_vdur
= [db_vnfr
["vdur"][0]]
600 for vdu_id
, vdu_count
in vdu_delete
.items():
602 indexes_to_delete
= [
604 for iv
in enumerate(db_vnfr
["vdur"])
605 if iv
[1]["vdu-id-ref"] == vdu_id
609 "vdur.{}.status".format(i
): "DELETING"
610 for i
in indexes_to_delete
[-vdu_count
:]
614 # it must be deleted one by one because common.db does not allow otherwise
617 for v
in reversed(db_vnfr
["vdur"])
618 if v
["vdu-id-ref"] == vdu_id
620 for vdu
in vdus_to_delete
[:vdu_count
]:
623 {"_id": db_vnfr
["_id"]},
625 pull
={"vdur": {"_id": vdu
["_id"]}},
629 db_push
["vdur"] = db_vdu_push_list
631 db_push
["vdur-template"] = template_vdur
634 db_vnfr
["vdur-template"] = template_vdur
635 self
.db
.set_one("vnfrs", {"_id": db_vnfr
["_id"]}, db_update
, push_list
=db_push
)
636 # modify passed dictionary db_vnfr
637 db_vnfr_
= self
.db
.get_one("vnfrs", {"_id": db_vnfr
["_id"]})
638 db_vnfr
["vdur"] = db_vnfr_
["vdur"]
640 def ns_update_nsr(self
, ns_update_nsr
, db_nsr
, nsr_desc_RO
):
642 Updates database nsr with the RO info for the created vld
643 :param ns_update_nsr: dictionary to be filled with the updated info
644 :param db_nsr: content of db_nsr. This is also modified
645 :param nsr_desc_RO: nsr descriptor from RO
646 :return: Nothing, LcmException is raised on errors
649 for vld_index
, vld
in enumerate(get_iterable(db_nsr
, "vld")):
650 for net_RO
in get_iterable(nsr_desc_RO
, "nets"):
651 if vld
["id"] != net_RO
.get("ns_net_osm_id"):
653 vld
["vim-id"] = net_RO
.get("vim_net_id")
654 vld
["name"] = net_RO
.get("vim_name")
655 vld
["status"] = net_RO
.get("status")
656 vld
["status-detailed"] = net_RO
.get("error_msg")
657 ns_update_nsr
["vld.{}".format(vld_index
)] = vld
661 "ns_update_nsr: Not found vld={} at RO info".format(vld
["id"])
664 def set_vnfr_at_error(self
, db_vnfrs
, error_text
):
666 for db_vnfr
in db_vnfrs
.values():
667 vnfr_update
= {"status": "ERROR"}
668 for vdu_index
, vdur
in enumerate(get_iterable(db_vnfr
, "vdur")):
669 if "status" not in vdur
:
670 vdur
["status"] = "ERROR"
671 vnfr_update
["vdur.{}.status".format(vdu_index
)] = "ERROR"
673 vdur
["status-detailed"] = str(error_text
)
675 "vdur.{}.status-detailed".format(vdu_index
)
677 self
.update_db_2("vnfrs", db_vnfr
["_id"], vnfr_update
)
678 except DbException
as e
:
679 self
.logger
.error("Cannot update vnf. {}".format(e
))
681 def ns_update_vnfr(self
, db_vnfrs
, nsr_desc_RO
):
683 Updates database vnfr with the RO info, e.g. ip_address, vim_id... Descriptor db_vnfrs is also updated
684 :param db_vnfrs: dictionary with member-vnf-index: vnfr-content
685 :param nsr_desc_RO: nsr descriptor from RO
686 :return: Nothing, LcmException is raised on errors
688 for vnf_index
, db_vnfr
in db_vnfrs
.items():
689 for vnf_RO
in nsr_desc_RO
["vnfs"]:
690 if vnf_RO
["member_vnf_index"] != vnf_index
:
693 if vnf_RO
.get("ip_address"):
694 db_vnfr
["ip-address"] = vnfr_update
["ip-address"] = vnf_RO
[
697 elif not db_vnfr
.get("ip-address"):
698 if db_vnfr
.get("vdur"): # if not VDUs, there is not ip_address
699 raise LcmExceptionNoMgmtIP(
700 "ns member_vnf_index '{}' has no IP address".format(
705 for vdu_index
, vdur
in enumerate(get_iterable(db_vnfr
, "vdur")):
706 vdur_RO_count_index
= 0
707 if vdur
.get("pdu-type"):
709 for vdur_RO
in get_iterable(vnf_RO
, "vms"):
710 if vdur
["vdu-id-ref"] != vdur_RO
["vdu_osm_id"]:
712 if vdur
["count-index"] != vdur_RO_count_index
:
713 vdur_RO_count_index
+= 1
715 vdur
["vim-id"] = vdur_RO
.get("vim_vm_id")
716 if vdur_RO
.get("ip_address"):
717 vdur
["ip-address"] = vdur_RO
["ip_address"].split(";")[0]
719 vdur
["ip-address"] = None
720 vdur
["vdu-id-ref"] = vdur_RO
.get("vdu_osm_id")
721 vdur
["name"] = vdur_RO
.get("vim_name")
722 vdur
["status"] = vdur_RO
.get("status")
723 vdur
["status-detailed"] = vdur_RO
.get("error_msg")
724 for ifacer
in get_iterable(vdur
, "interfaces"):
725 for interface_RO
in get_iterable(vdur_RO
, "interfaces"):
726 if ifacer
["name"] == interface_RO
.get("internal_name"):
727 ifacer
["ip-address"] = interface_RO
.get(
730 ifacer
["mac-address"] = interface_RO
.get(
736 "ns_update_vnfr: Not found member_vnf_index={} vdur={} interface={} "
737 "from VIM info".format(
738 vnf_index
, vdur
["vdu-id-ref"], ifacer
["name"]
741 vnfr_update
["vdur.{}".format(vdu_index
)] = vdur
745 "ns_update_vnfr: Not found member_vnf_index={} vdur={} count_index={} from "
747 vnf_index
, vdur
["vdu-id-ref"], vdur
["count-index"]
751 for vld_index
, vld
in enumerate(get_iterable(db_vnfr
, "vld")):
752 for net_RO
in get_iterable(nsr_desc_RO
, "nets"):
753 if vld
["id"] != net_RO
.get("vnf_net_osm_id"):
755 vld
["vim-id"] = net_RO
.get("vim_net_id")
756 vld
["name"] = net_RO
.get("vim_name")
757 vld
["status"] = net_RO
.get("status")
758 vld
["status-detailed"] = net_RO
.get("error_msg")
759 vnfr_update
["vld.{}".format(vld_index
)] = vld
763 "ns_update_vnfr: Not found member_vnf_index={} vld={} from VIM info".format(
768 self
.update_db_2("vnfrs", db_vnfr
["_id"], vnfr_update
)
773 "ns_update_vnfr: Not found member_vnf_index={} from VIM info".format(
778 def _get_ns_config_info(self
, nsr_id
):
780 Generates a mapping between vnf,vdu elements and the N2VC id
781 :param nsr_id: id of nsr to get last database _admin.deployed.VCA that contains this list
782 :return: a dictionary with {osm-config-mapping: {}} where its element contains:
783 "<member-vnf-index>": <N2VC-id> for a vnf configuration, or
784 "<member-vnf-index>.<vdu.id>.<vdu replica(0, 1,..)>": <N2VC-id> for a vdu configuration
786 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
787 vca_deployed_list
= db_nsr
["_admin"]["deployed"]["VCA"]
789 ns_config_info
= {"osm-config-mapping": mapping
}
790 for vca
in vca_deployed_list
:
791 if not vca
["member-vnf-index"]:
793 if not vca
["vdu_id"]:
794 mapping
[vca
["member-vnf-index"]] = vca
["application"]
798 vca
["member-vnf-index"], vca
["vdu_id"], vca
["vdu_count_index"]
800 ] = vca
["application"]
801 return ns_config_info
803 async def _instantiate_ng_ro(
819 def get_vim_account(vim_account_id
):
821 if vim_account_id
in db_vims
:
822 return db_vims
[vim_account_id
]
823 db_vim
= self
.db
.get_one("vim_accounts", {"_id": vim_account_id
})
824 db_vims
[vim_account_id
] = db_vim
827 # modify target_vld info with instantiation parameters
828 def parse_vld_instantiation_params(
829 target_vim
, target_vld
, vld_params
, target_sdn
831 if vld_params
.get("ip-profile"):
832 target_vld
["vim_info"][target_vim
]["ip_profile"] = vld_to_ro_ip_profile(
833 vld_params
["ip-profile"]
835 if vld_params
.get("provider-network"):
836 target_vld
["vim_info"][target_vim
]["provider_network"] = vld_params
[
839 if "sdn-ports" in vld_params
["provider-network"] and target_sdn
:
840 target_vld
["vim_info"][target_sdn
]["sdn-ports"] = vld_params
[
844 # check if WIM is needed; if needed, choose a feasible WIM able to connect VIMs
845 # if wim_account_id is specified in vld_params, validate if it is feasible.
846 wim_account_id
, db_wim
= select_feasible_wim_account(
847 db_nsr
, db_vnfrs
, target_vld
, vld_params
, self
.logger
851 # WIM is needed and a feasible one was found, populate WIM target and SDN ports
852 self
.logger
.info("WIM selected: {:s}".format(str(wim_account_id
)))
853 # update vld_params with correct WIM account Id
854 vld_params
["wimAccountId"] = wim_account_id
856 target_wim
= "wim:{}".format(wim_account_id
)
857 target_wim_attrs
= get_target_wim_attrs(nsr_id
, target_vld
, vld_params
)
858 sdn_ports
= get_sdn_ports(vld_params
, db_wim
)
859 if len(sdn_ports
) > 0:
860 target_vld
["vim_info"][target_wim
] = target_wim_attrs
861 target_vld
["vim_info"][target_wim
]["sdn-ports"] = sdn_ports
864 "Target VLD with WIM data: {:s}".format(str(target_vld
))
867 for param
in ("vim-network-name", "vim-network-id"):
868 if vld_params
.get(param
):
869 if isinstance(vld_params
[param
], dict):
870 for vim
, vim_net
in vld_params
[param
].items():
871 other_target_vim
= "vim:" + vim
873 target_vld
["vim_info"],
874 (other_target_vim
, param
.replace("-", "_")),
877 else: # isinstance str
878 target_vld
["vim_info"][target_vim
][
879 param
.replace("-", "_")
880 ] = vld_params
[param
]
881 if vld_params
.get("common_id"):
882 target_vld
["common_id"] = vld_params
.get("common_id")
884 # modify target["ns"]["vld"] with instantiation parameters to override vnf vim-account
885 def update_ns_vld_target(target
, ns_params
):
886 for vnf_params
in ns_params
.get("vnf", ()):
887 if vnf_params
.get("vimAccountId"):
891 for vnfr
in db_vnfrs
.values()
892 if vnf_params
["member-vnf-index"]
893 == vnfr
["member-vnf-index-ref"]
897 vdur
= next((vdur
for vdur
in target_vnf
.get("vdur", ())), None)
900 for a_index
, a_vld
in enumerate(target
["ns"]["vld"]):
901 target_vld
= find_in_list(
902 get_iterable(vdur
, "interfaces"),
903 lambda iface
: iface
.get("ns-vld-id") == a_vld
["name"],
906 vld_params
= find_in_list(
907 get_iterable(ns_params
, "vld"),
908 lambda v_vld
: v_vld
["name"] in (a_vld
["name"], a_vld
["id"]),
911 if vnf_params
.get("vimAccountId") not in a_vld
.get(
914 target_vim_network_list
= [
915 v
for _
, v
in a_vld
.get("vim_info").items()
917 target_vim_network_name
= next(
919 item
.get("vim_network_name", "")
920 for item
in target_vim_network_list
925 target
["ns"]["vld"][a_index
].get("vim_info").update(
927 "vim:{}".format(vnf_params
["vimAccountId"]): {
928 "vim_network_name": target_vim_network_name
,
934 for param
in ("vim-network-name", "vim-network-id"):
935 if vld_params
.get(param
) and isinstance(
936 vld_params
[param
], dict
938 for vim
, vim_net
in vld_params
[
941 other_target_vim
= "vim:" + vim
943 target
["ns"]["vld"][a_index
].get(
948 param
.replace("-", "_"),
953 nslcmop_id
= db_nslcmop
["_id"]
955 "name": db_nsr
["name"],
958 "image": deepcopy(db_nsr
["image"]),
959 "flavor": deepcopy(db_nsr
["flavor"]),
960 "action_id": nslcmop_id
,
961 "cloud_init_content": {},
963 for image
in target
["image"]:
964 image
["vim_info"] = {}
965 for flavor
in target
["flavor"]:
966 flavor
["vim_info"] = {}
967 if db_nsr
.get("shared-volumes"):
968 target
["shared-volumes"] = deepcopy(db_nsr
["shared-volumes"])
969 for shared_volumes
in target
["shared-volumes"]:
970 shared_volumes
["vim_info"] = {}
971 if db_nsr
.get("affinity-or-anti-affinity-group"):
972 target
["affinity-or-anti-affinity-group"] = deepcopy(
973 db_nsr
["affinity-or-anti-affinity-group"]
975 for affinity_or_anti_affinity_group
in target
[
976 "affinity-or-anti-affinity-group"
978 affinity_or_anti_affinity_group
["vim_info"] = {}
980 if db_nslcmop
.get("lcmOperationType") != "instantiate":
981 # get parameters of instantiation:
982 db_nslcmop_instantiate
= self
.db
.get_list(
985 "nsInstanceId": db_nslcmop
["nsInstanceId"],
986 "lcmOperationType": "instantiate",
989 ns_params
= db_nslcmop_instantiate
.get("operationParams")
991 ns_params
= db_nslcmop
.get("operationParams")
992 ssh_keys_instantiation
= ns_params
.get("ssh_keys") or []
993 ssh_keys_all
= ssh_keys_instantiation
+ (n2vc_key_list
or [])
996 for vld_index
, vld
in enumerate(db_nsr
.get("vld")):
997 target_vim
= "vim:{}".format(ns_params
["vimAccountId"])
1000 "name": vld
["name"],
1001 "mgmt-network": vld
.get("mgmt-network", False),
1002 "type": vld
.get("type"),
1005 "vim_network_name": vld
.get("vim-network-name"),
1006 "vim_account_id": ns_params
["vimAccountId"],
1010 # check if this network needs SDN assist
1011 if vld
.get("pci-interfaces"):
1012 db_vim
= get_vim_account(ns_params
["vimAccountId"])
1013 if vim_config
:= db_vim
.get("config"):
1014 if sdnc_id
:= vim_config
.get("sdn-controller"):
1015 sdn_vld
= "nsrs:{}:vld.{}".format(nsr_id
, vld
["id"])
1016 target_sdn
= "sdn:{}".format(sdnc_id
)
1017 target_vld
["vim_info"][target_sdn
] = {
1019 "target_vim": target_vim
,
1021 "type": vld
.get("type"),
1024 nsd_vnf_profiles
= get_vnf_profiles(nsd
)
1025 for nsd_vnf_profile
in nsd_vnf_profiles
:
1026 for cp
in nsd_vnf_profile
["virtual-link-connectivity"]:
1027 if cp
["virtual-link-profile-id"] == vld
["id"]:
1029 "member_vnf:{}.{}".format(
1030 cp
["constituent-cpd-id"][0][
1031 "constituent-base-element-id"
1033 cp
["constituent-cpd-id"][0]["constituent-cpd-id"],
1035 ] = "nsrs:{}:vld.{}".format(nsr_id
, vld_index
)
1037 # check at nsd descriptor, if there is an ip-profile
1039 nsd_vlp
= find_in_list(
1040 get_virtual_link_profiles(nsd
),
1041 lambda a_link_profile
: a_link_profile
["virtual-link-desc-id"]
1046 and nsd_vlp
.get("virtual-link-protocol-data")
1047 and nsd_vlp
["virtual-link-protocol-data"].get("l3-protocol-data")
1049 vld_params
["ip-profile"] = nsd_vlp
["virtual-link-protocol-data"][
1053 # update vld_params with instantiation params
1054 vld_instantiation_params
= find_in_list(
1055 get_iterable(ns_params
, "vld"),
1056 lambda a_vld
: a_vld
["name"] in (vld
["name"], vld
["id"]),
1058 if vld_instantiation_params
:
1059 vld_params
.update(vld_instantiation_params
)
1060 parse_vld_instantiation_params(target_vim
, target_vld
, vld_params
, None)
1061 target
["ns"]["vld"].append(target_vld
)
1062 # Update the target ns_vld if vnf vim_account is overriden by instantiation params
1063 update_ns_vld_target(target
, ns_params
)
1065 for vnfr
in db_vnfrs
.values():
1066 vnfd
= find_in_list(
1067 db_vnfds
, lambda db_vnf
: db_vnf
["id"] == vnfr
["vnfd-ref"]
1069 vnf_params
= find_in_list(
1070 get_iterable(ns_params
, "vnf"),
1071 lambda a_vnf
: a_vnf
["member-vnf-index"] == vnfr
["member-vnf-index-ref"],
1073 target_vnf
= deepcopy(vnfr
)
1074 target_vim
= "vim:{}".format(vnfr
["vim-account-id"])
1075 for vld
in target_vnf
.get("vld", ()):
1076 # check if connected to a ns.vld, to fill target'
1077 vnf_cp
= find_in_list(
1078 vnfd
.get("int-virtual-link-desc", ()),
1079 lambda cpd
: cpd
.get("id") == vld
["id"],
1082 ns_cp
= "member_vnf:{}.{}".format(
1083 vnfr
["member-vnf-index-ref"], vnf_cp
["id"]
1085 if cp2target
.get(ns_cp
):
1086 vld
["target"] = cp2target
[ns_cp
]
1089 target_vim
: {"vim_network_name": vld
.get("vim-network-name")}
1091 # check if this network needs SDN assist
1093 if vld
.get("pci-interfaces"):
1094 db_vim
= get_vim_account(vnfr
["vim-account-id"])
1095 sdnc_id
= db_vim
["config"].get("sdn-controller")
1097 sdn_vld
= "vnfrs:{}:vld.{}".format(target_vnf
["_id"], vld
["id"])
1098 target_sdn
= "sdn:{}".format(sdnc_id
)
1099 vld
["vim_info"][target_sdn
] = {
1101 "target_vim": target_vim
,
1103 "type": vld
.get("type"),
1106 # check at vnfd descriptor, if there is an ip-profile
1108 vnfd_vlp
= find_in_list(
1109 get_virtual_link_profiles(vnfd
),
1110 lambda a_link_profile
: a_link_profile
["id"] == vld
["id"],
1114 and vnfd_vlp
.get("virtual-link-protocol-data")
1115 and vnfd_vlp
["virtual-link-protocol-data"].get("l3-protocol-data")
1117 vld_params
["ip-profile"] = vnfd_vlp
["virtual-link-protocol-data"][
1120 # update vld_params with instantiation params
1122 vld_instantiation_params
= find_in_list(
1123 get_iterable(vnf_params
, "internal-vld"),
1124 lambda i_vld
: i_vld
["name"] == vld
["id"],
1126 if vld_instantiation_params
:
1127 vld_params
.update(vld_instantiation_params
)
1128 parse_vld_instantiation_params(target_vim
, vld
, vld_params
, target_sdn
)
1131 for vdur
in target_vnf
.get("vdur", ()):
1132 if vdur
.get("status") == "DELETING" or vdur
.get("pdu-type"):
1133 continue # This vdu must not be created
1134 vdur
["vim_info"] = {"vim_account_id": vnfr
["vim-account-id"]}
1136 self
.logger
.debug("NS > ssh_keys > {}".format(ssh_keys_all
))
1139 vdu_configuration
= get_configuration(vnfd
, vdur
["vdu-id-ref"])
1140 vnf_configuration
= get_configuration(vnfd
, vnfd
["id"])
1143 and vdu_configuration
.get("config-access")
1144 and vdu_configuration
.get("config-access").get("ssh-access")
1146 vdur
["ssh-keys"] = ssh_keys_all
1147 vdur
["ssh-access-required"] = vdu_configuration
[
1149 ]["ssh-access"]["required"]
1152 and vnf_configuration
.get("config-access")
1153 and vnf_configuration
.get("config-access").get("ssh-access")
1154 and any(iface
.get("mgmt-vnf") for iface
in vdur
["interfaces"])
1156 vdur
["ssh-keys"] = ssh_keys_all
1157 vdur
["ssh-access-required"] = vnf_configuration
[
1159 ]["ssh-access"]["required"]
1160 elif ssh_keys_instantiation
and find_in_list(
1161 vdur
["interfaces"], lambda iface
: iface
.get("mgmt-vnf")
1163 vdur
["ssh-keys"] = ssh_keys_instantiation
1165 self
.logger
.debug("NS > vdur > {}".format(vdur
))
1167 vdud
= get_vdu(vnfd
, vdur
["vdu-id-ref"])
1169 if vdud
.get("cloud-init-file"):
1170 vdur
["cloud-init"] = "{}:file:{}".format(
1171 vnfd
["_id"], vdud
.get("cloud-init-file")
1173 # read file and put content at target.cloul_init_content. Avoid ng_ro to use shared package system
1174 if vdur
["cloud-init"] not in target
["cloud_init_content"]:
1175 base_folder
= vnfd
["_admin"]["storage"]
1176 if base_folder
["pkg-dir"]:
1177 cloud_init_file
= "{}/{}/cloud_init/{}".format(
1178 base_folder
["folder"],
1179 base_folder
["pkg-dir"],
1180 vdud
.get("cloud-init-file"),
1183 cloud_init_file
= "{}/Scripts/cloud_init/{}".format(
1184 base_folder
["folder"],
1185 vdud
.get("cloud-init-file"),
1187 with self
.fs
.file_open(cloud_init_file
, "r") as ci_file
:
1188 target
["cloud_init_content"][
1191 elif vdud
.get("cloud-init"):
1192 vdur
["cloud-init"] = "{}:vdu:{}".format(
1193 vnfd
["_id"], get_vdu_index(vnfd
, vdur
["vdu-id-ref"])
1195 # put content at target.cloul_init_content. Avoid ng_ro read vnfd descriptor
1196 target
["cloud_init_content"][vdur
["cloud-init"]] = vdud
[
1199 vdur
["additionalParams"] = vdur
.get("additionalParams") or {}
1200 deploy_params_vdu
= self
._format
_additional
_params
(
1201 vdur
.get("additionalParams") or {}
1203 deploy_params_vdu
["OSM"] = get_osm_params(
1204 vnfr
, vdur
["vdu-id-ref"], vdur
["count-index"]
1206 vdur
["additionalParams"] = deploy_params_vdu
1209 ns_flavor
= target
["flavor"][int(vdur
["ns-flavor-id"])]
1210 if target_vim
not in ns_flavor
["vim_info"]:
1211 ns_flavor
["vim_info"][target_vim
] = {}
1214 # in case alternative images are provided we must check if they should be applied
1215 # for the vim_type, modify the vim_type taking into account
1216 ns_image_id
= int(vdur
["ns-image-id"])
1217 if vdur
.get("alt-image-ids"):
1218 db_vim
= get_vim_account(vnfr
["vim-account-id"])
1219 vim_type
= db_vim
["vim_type"]
1220 for alt_image_id
in vdur
.get("alt-image-ids"):
1221 ns_alt_image
= target
["image"][int(alt_image_id
)]
1222 if vim_type
== ns_alt_image
.get("vim-type"):
1223 # must use alternative image
1225 "use alternative image id: {}".format(alt_image_id
)
1227 ns_image_id
= alt_image_id
1228 vdur
["ns-image-id"] = ns_image_id
1230 ns_image
= target
["image"][int(ns_image_id
)]
1231 if target_vim
not in ns_image
["vim_info"]:
1232 ns_image
["vim_info"][target_vim
] = {}
1235 if vdur
.get("affinity-or-anti-affinity-group-id"):
1236 for ags_id
in vdur
["affinity-or-anti-affinity-group-id"]:
1237 ns_ags
= target
["affinity-or-anti-affinity-group"][int(ags_id
)]
1238 if target_vim
not in ns_ags
["vim_info"]:
1239 ns_ags
["vim_info"][target_vim
] = {}
1242 if vdur
.get("shared-volumes-id"):
1243 for sv_id
in vdur
["shared-volumes-id"]:
1244 ns_sv
= find_in_list(
1245 target
["shared-volumes"], lambda sv
: sv_id
in sv
["id"]
1248 ns_sv
["vim_info"][target_vim
] = {}
1250 vdur
["vim_info"] = {target_vim
: {}}
1251 # instantiation parameters
1253 vdu_instantiation_params
= find_in_list(
1254 get_iterable(vnf_params
, "vdu"),
1255 lambda i_vdu
: i_vdu
["id"] == vdud
["id"],
1257 if vdu_instantiation_params
:
1258 # Parse the vdu_volumes from the instantiation params
1259 vdu_volumes
= get_volumes_from_instantiation_params(
1260 vdu_instantiation_params
, vdud
1262 vdur
["additionalParams"]["OSM"]["vdu_volumes"] = vdu_volumes
1263 vdur
["additionalParams"]["OSM"][
1265 ] = vdu_instantiation_params
.get("vim-flavor-id")
1266 vdur_list
.append(vdur
)
1267 target_vnf
["vdur"] = vdur_list
1268 target
["vnf"].append(target_vnf
)
1270 self
.logger
.debug("Send to RO > nsr_id={} target={}".format(nsr_id
, target
))
1271 desc
= await self
.RO
.deploy(nsr_id
, target
)
1272 self
.logger
.debug("RO return > {}".format(desc
))
1273 action_id
= desc
["action_id"]
1274 await self
._wait
_ng
_ro
(
1281 operation
="instantiation",
1286 "_admin.deployed.RO.operational-status": "running",
1287 "detailed-status": " ".join(stage
),
1289 # db_nsr["_admin.deployed.RO.detailed-status"] = "Deployed at VIM"
1290 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
1291 self
._write
_op
_status
(nslcmop_id
, stage
)
1293 logging_text
+ "ns deployed at RO. RO_id={}".format(action_id
)
1297 async def _wait_ng_ro(
1307 detailed_status_old
= None
1309 start_time
= start_time
or time()
1310 while time() <= start_time
+ timeout
:
1311 desc_status
= await self
.op_status_map
[operation
](nsr_id
, action_id
)
1312 self
.logger
.debug("Wait NG RO > {}".format(desc_status
))
1313 if desc_status
["status"] == "FAILED":
1314 raise NgRoException(desc_status
["details"])
1315 elif desc_status
["status"] == "BUILD":
1317 stage
[2] = "VIM: ({})".format(desc_status
["details"])
1318 elif desc_status
["status"] == "DONE":
1320 stage
[2] = "Deployed at VIM"
1323 assert False, "ROclient.check_ns_status returns unknown {}".format(
1324 desc_status
["status"]
1326 if stage
and nslcmop_id
and stage
[2] != detailed_status_old
:
1327 detailed_status_old
= stage
[2]
1328 db_nsr_update
["detailed-status"] = " ".join(stage
)
1329 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
1330 self
._write
_op
_status
(nslcmop_id
, stage
)
1331 await asyncio
.sleep(15)
1332 else: # timeout_ns_deploy
1333 raise NgRoException("Timeout waiting ns to deploy")
1335 async def _terminate_ng_ro(
1336 self
, logging_text
, nsr_deployed
, nsr_id
, nslcmop_id
, stage
1341 start_deploy
= time()
1348 "action_id": nslcmop_id
,
1350 desc
= await self
.RO
.deploy(nsr_id
, target
)
1351 action_id
= desc
["action_id"]
1352 db_nsr_update
["_admin.deployed.RO.nsr_status"] = "DELETING"
1355 + "ns terminate action at RO. action_id={}".format(action_id
)
1359 delete_timeout
= 20 * 60 # 20 minutes
1360 await self
._wait
_ng
_ro
(
1367 operation
="termination",
1369 db_nsr_update
["_admin.deployed.RO.nsr_status"] = "DELETED"
1371 await self
.RO
.delete(nsr_id
)
1372 except NgRoException
as e
:
1373 if e
.http_code
== 404: # not found
1374 db_nsr_update
["_admin.deployed.RO.nsr_id"] = None
1375 db_nsr_update
["_admin.deployed.RO.nsr_status"] = "DELETED"
1377 logging_text
+ "RO_action_id={} already deleted".format(action_id
)
1379 elif e
.http_code
== 409: # conflict
1380 failed_detail
.append("delete conflict: {}".format(e
))
1383 + "RO_action_id={} delete conflict: {}".format(action_id
, e
)
1386 failed_detail
.append("delete error: {}".format(e
))
1389 + "RO_action_id={} delete error: {}".format(action_id
, e
)
1391 except Exception as e
:
1392 failed_detail
.append("delete error: {}".format(e
))
1394 logging_text
+ "RO_action_id={} delete error: {}".format(action_id
, e
)
1398 stage
[2] = "Error deleting from VIM"
1400 stage
[2] = "Deleted from VIM"
1401 db_nsr_update
["detailed-status"] = " ".join(stage
)
1402 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
1403 self
._write
_op
_status
(nslcmop_id
, stage
)
1406 raise LcmException("; ".join(failed_detail
))
1409 async def instantiate_RO(
1423 :param logging_text: preffix text to use at logging
1424 :param nsr_id: nsr identity
1425 :param nsd: database content of ns descriptor
1426 :param db_nsr: database content of ns record
1427 :param db_nslcmop: database content of ns operation, in this case, 'instantiate'
1429 :param db_vnfds: database content of vnfds, indexed by id (not _id). {id: {vnfd_object}, ...}
1430 :param n2vc_key_list: ssh-public-key list to be inserted to management vdus via cloud-init
1431 :param stage: list with 3 items: [general stage, tasks, vim_specific]. This task will write over vim_specific
1432 :return: None or exception
1435 start_deploy
= time()
1436 ns_params
= db_nslcmop
.get("operationParams")
1437 if ns_params
and ns_params
.get("timeout_ns_deploy"):
1438 timeout_ns_deploy
= ns_params
["timeout_ns_deploy"]
1440 timeout_ns_deploy
= self
.timeout
.ns_deploy
1442 # Check for and optionally request placement optimization. Database will be updated if placement activated
1443 stage
[2] = "Waiting for Placement."
1444 if await self
._do
_placement
(logging_text
, db_nslcmop
, db_vnfrs
):
1445 # in case of placement change ns_params[vimAcountId) if not present at any vnfrs
1446 for vnfr
in db_vnfrs
.values():
1447 if ns_params
["vimAccountId"] == vnfr
["vim-account-id"]:
1450 ns_params
["vimAccountId"] == vnfr
["vim-account-id"]
1452 return await self
._instantiate
_ng
_ro
(
1465 except Exception as e
:
1466 stage
[2] = "ERROR deploying at VIM"
1467 self
.set_vnfr_at_error(db_vnfrs
, str(e
))
1469 "Error deploying at VIM {}".format(e
),
1470 exc_info
=not isinstance(
1473 ROclient
.ROClientException
,
1482 async def wait_kdu_up(self
, logging_text
, nsr_id
, vnfr_id
, kdu_name
):
1484 Wait for kdu to be up, get ip address
1485 :param logging_text: prefix use for logging
1489 :return: IP address, K8s services
1492 # self.logger.debug(logging_text + "Starting wait_kdu_up")
1495 while nb_tries
< 360:
1496 db_vnfr
= self
.db
.get_one("vnfrs", {"_id": vnfr_id
})
1500 for x
in get_iterable(db_vnfr
, "kdur")
1501 if x
.get("kdu-name") == kdu_name
1507 "Not found vnfr_id={}, kdu_name={}".format(vnfr_id
, kdu_name
)
1509 if kdur
.get("status"):
1510 if kdur
["status"] in ("READY", "ENABLED"):
1511 return kdur
.get("ip-address"), kdur
.get("services")
1514 "target KDU={} is in error state".format(kdu_name
)
1517 await asyncio
.sleep(10)
1519 raise LcmException("Timeout waiting KDU={} instantiated".format(kdu_name
))
1521 async def wait_vm_up_insert_key_ro(
1522 self
, logging_text
, nsr_id
, vnfr_id
, vdu_id
, vdu_index
, pub_key
=None, user
=None
1525 Wait for ip addres at RO, and optionally, insert public key in virtual machine
1526 :param logging_text: prefix use for logging
1531 :param pub_key: public ssh key to inject, None to skip
1532 :param user: user to apply the public ssh key
1536 self
.logger
.debug(logging_text
+ "Starting wait_vm_up_insert_key_ro")
1538 target_vdu_id
= None
1543 if ro_retries
>= 360: # 1 hour
1545 "Not found _admin.deployed.RO.nsr_id for nsr_id: {}".format(nsr_id
)
1548 await asyncio
.sleep(10)
1551 if not target_vdu_id
:
1552 db_vnfr
= self
.db
.get_one("vnfrs", {"_id": vnfr_id
})
1554 if not vdu_id
: # for the VNF case
1555 if db_vnfr
.get("status") == "ERROR":
1557 "Cannot inject ssh-key because target VNF is in error state"
1559 ip_address
= db_vnfr
.get("ip-address")
1565 for x
in get_iterable(db_vnfr
, "vdur")
1566 if x
.get("ip-address") == ip_address
1574 for x
in get_iterable(db_vnfr
, "vdur")
1575 if x
.get("vdu-id-ref") == vdu_id
1576 and x
.get("count-index") == vdu_index
1582 not vdur
and len(db_vnfr
.get("vdur", ())) == 1
1583 ): # If only one, this should be the target vdu
1584 vdur
= db_vnfr
["vdur"][0]
1587 "Not found vnfr_id={}, vdu_id={}, vdu_index={}".format(
1588 vnfr_id
, vdu_id
, vdu_index
1591 # New generation RO stores information at "vim_info"
1594 if vdur
.get("vim_info"):
1596 t
for t
in vdur
["vim_info"]
1597 ) # there should be only one key
1598 ng_ro_status
= vdur
["vim_info"][target_vim
].get("vim_status")
1600 vdur
.get("pdu-type")
1601 or vdur
.get("status") == "ACTIVE"
1602 or ng_ro_status
== "ACTIVE"
1604 ip_address
= vdur
.get("ip-address")
1607 target_vdu_id
= vdur
["vdu-id-ref"]
1608 elif vdur
.get("status") == "ERROR" or ng_ro_status
== "ERROR":
1610 "Cannot inject ssh-key because target VM is in error state"
1613 if not target_vdu_id
:
1616 # inject public key into machine
1617 if pub_key
and user
:
1618 self
.logger
.debug(logging_text
+ "Inserting RO key")
1619 self
.logger
.debug("SSH > PubKey > {}".format(pub_key
))
1620 if vdur
.get("pdu-type"):
1621 self
.logger
.error(logging_text
+ "Cannot inject ssh-ky to a PDU")
1626 "action": "inject_ssh_key",
1630 "vnf": [{"_id": vnfr_id
, "vdur": [{"id": vdur
["id"]}]}],
1632 desc
= await self
.RO
.deploy(nsr_id
, target
)
1633 action_id
= desc
["action_id"]
1634 await self
._wait
_ng
_ro
(
1635 nsr_id
, action_id
, timeout
=600, operation
="instantiation"
1638 except NgRoException
as e
:
1640 "Reaching max tries injecting key. Error: {}".format(e
)
1647 async def _wait_dependent_n2vc(self
, nsr_id
, vca_deployed_list
, vca_index
):
1649 Wait until dependent VCA deployments have been finished. NS wait for VNFs and VDUs. VNFs for VDUs
1651 my_vca
= vca_deployed_list
[vca_index
]
1652 if my_vca
.get("vdu_id") or my_vca
.get("kdu_name"):
1653 # vdu or kdu: no dependencies
1657 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
1658 vca_deployed_list
= db_nsr
["_admin"]["deployed"]["VCA"]
1659 configuration_status_list
= db_nsr
["configurationStatus"]
1660 for index
, vca_deployed
in enumerate(configuration_status_list
):
1661 if index
== vca_index
:
1664 if not my_vca
.get("member-vnf-index") or (
1665 vca_deployed
.get("member-vnf-index")
1666 == my_vca
.get("member-vnf-index")
1668 internal_status
= configuration_status_list
[index
].get("status")
1669 if internal_status
== "READY":
1671 elif internal_status
== "BROKEN":
1673 "Configuration aborted because dependent charm/s has failed"
1678 # no dependencies, return
1680 await asyncio
.sleep(10)
1683 raise LcmException("Configuration aborted because dependent charm/s timeout")
1685 def get_vca_id(self
, db_vnfr
: dict, db_nsr
: dict):
1688 vca_id
= deep_get(db_vnfr
, ("vca-id",))
1690 vim_account_id
= deep_get(db_nsr
, ("instantiate_params", "vimAccountId"))
1691 vca_id
= VimAccountDB
.get_vim_account_with_id(vim_account_id
).get("vca")
1694 async def instantiate_N2VC(
1712 ee_config_descriptor
,
1714 nsr_id
= db_nsr
["_id"]
1715 db_update_entry
= "_admin.deployed.VCA.{}.".format(vca_index
)
1716 vca_deployed_list
= db_nsr
["_admin"]["deployed"]["VCA"]
1717 vca_deployed
= db_nsr
["_admin"]["deployed"]["VCA"][vca_index
]
1718 osm_config
= {"osm": {"ns_id": db_nsr
["_id"]}}
1720 "collection": "nsrs",
1721 "filter": {"_id": nsr_id
},
1722 "path": db_update_entry
,
1727 element_under_configuration
= nsr_id
1731 vnfr_id
= db_vnfr
["_id"]
1732 osm_config
["osm"]["vnf_id"] = vnfr_id
1734 namespace
= "{nsi}.{ns}".format(nsi
=nsi_id
if nsi_id
else "", ns
=nsr_id
)
1736 if vca_type
== "native_charm":
1739 index_number
= vdu_index
or 0
1742 element_type
= "VNF"
1743 element_under_configuration
= vnfr_id
1744 namespace
+= ".{}-{}".format(vnfr_id
, index_number
)
1746 namespace
+= ".{}-{}".format(vdu_id
, index_number
)
1747 element_type
= "VDU"
1748 element_under_configuration
= "{}-{}".format(vdu_id
, index_number
)
1749 osm_config
["osm"]["vdu_id"] = vdu_id
1751 namespace
+= ".{}".format(kdu_name
)
1752 element_type
= "KDU"
1753 element_under_configuration
= kdu_name
1754 osm_config
["osm"]["kdu_name"] = kdu_name
1757 if base_folder
["pkg-dir"]:
1758 artifact_path
= "{}/{}/{}/{}".format(
1759 base_folder
["folder"],
1760 base_folder
["pkg-dir"],
1763 in ("native_charm", "lxc_proxy_charm", "k8s_proxy_charm")
1768 artifact_path
= "{}/Scripts/{}/{}/".format(
1769 base_folder
["folder"],
1772 in ("native_charm", "lxc_proxy_charm", "k8s_proxy_charm")
1777 self
.logger
.debug("Artifact path > {}".format(artifact_path
))
1779 # get initial_config_primitive_list that applies to this element
1780 initial_config_primitive_list
= config_descriptor
.get(
1781 "initial-config-primitive"
1785 "Initial config primitive list > {}".format(
1786 initial_config_primitive_list
1790 # add config if not present for NS charm
1791 ee_descriptor_id
= ee_config_descriptor
.get("id")
1792 self
.logger
.debug("EE Descriptor > {}".format(ee_descriptor_id
))
1793 initial_config_primitive_list
= get_ee_sorted_initial_config_primitive_list(
1794 initial_config_primitive_list
, vca_deployed
, ee_descriptor_id
1798 "Initial config primitive list #2 > {}".format(
1799 initial_config_primitive_list
1802 # n2vc_redesign STEP 3.1
1803 # find old ee_id if exists
1804 ee_id
= vca_deployed
.get("ee_id")
1806 vca_id
= self
.get_vca_id(db_vnfr
, db_nsr
)
1807 # create or register execution environment in VCA
1808 if vca_type
in ("lxc_proxy_charm", "k8s_proxy_charm", "helm", "helm-v3"):
1809 self
._write
_configuration
_status
(
1811 vca_index
=vca_index
,
1813 element_under_configuration
=element_under_configuration
,
1814 element_type
=element_type
,
1817 step
= "create execution environment"
1818 self
.logger
.debug(logging_text
+ step
)
1822 if vca_type
== "k8s_proxy_charm":
1823 ee_id
= await self
.vca_map
[vca_type
].install_k8s_proxy_charm(
1824 charm_name
=artifact_path
[artifact_path
.rfind("/") + 1 :],
1825 namespace
=namespace
,
1826 artifact_path
=artifact_path
,
1830 elif vca_type
== "helm" or vca_type
== "helm-v3":
1831 ee_id
, credentials
= await self
.vca_map
[
1833 ].create_execution_environment(
1838 artifact_path
=artifact_path
,
1839 chart_model
=vca_name
,
1843 ee_id
, credentials
= await self
.vca_map
[
1845 ].create_execution_environment(
1846 namespace
=namespace
,
1852 elif vca_type
== "native_charm":
1853 step
= "Waiting to VM being up and getting IP address"
1854 self
.logger
.debug(logging_text
+ step
)
1855 rw_mgmt_ip
= await self
.wait_vm_up_insert_key_ro(
1864 credentials
= {"hostname": rw_mgmt_ip
}
1866 username
= deep_get(
1867 config_descriptor
, ("config-access", "ssh-access", "default-user")
1869 # TODO remove this when changes on IM regarding config-access:ssh-access:default-user were
1870 # merged. Meanwhile let's get username from initial-config-primitive
1871 if not username
and initial_config_primitive_list
:
1872 for config_primitive
in initial_config_primitive_list
:
1873 for param
in config_primitive
.get("parameter", ()):
1874 if param
["name"] == "ssh-username":
1875 username
= param
["value"]
1879 "Cannot determine the username neither with 'initial-config-primitive' nor with "
1880 "'config-access.ssh-access.default-user'"
1882 credentials
["username"] = username
1883 # n2vc_redesign STEP 3.2
1885 self
._write
_configuration
_status
(
1887 vca_index
=vca_index
,
1888 status
="REGISTERING",
1889 element_under_configuration
=element_under_configuration
,
1890 element_type
=element_type
,
1893 step
= "register execution environment {}".format(credentials
)
1894 self
.logger
.debug(logging_text
+ step
)
1895 ee_id
= await self
.vca_map
[vca_type
].register_execution_environment(
1896 credentials
=credentials
,
1897 namespace
=namespace
,
1902 # for compatibility with MON/POL modules, the need model and application name at database
1903 # TODO ask MON/POL if needed to not assuming anymore the format "model_name.application_name"
1904 ee_id_parts
= ee_id
.split(".")
1905 db_nsr_update
= {db_update_entry
+ "ee_id": ee_id
}
1906 if len(ee_id_parts
) >= 2:
1907 model_name
= ee_id_parts
[0]
1908 application_name
= ee_id_parts
[1]
1909 db_nsr_update
[db_update_entry
+ "model"] = model_name
1910 db_nsr_update
[db_update_entry
+ "application"] = application_name
1912 # n2vc_redesign STEP 3.3
1913 step
= "Install configuration Software"
1915 self
._write
_configuration
_status
(
1917 vca_index
=vca_index
,
1918 status
="INSTALLING SW",
1919 element_under_configuration
=element_under_configuration
,
1920 element_type
=element_type
,
1921 other_update
=db_nsr_update
,
1924 # TODO check if already done
1925 self
.logger
.debug(logging_text
+ step
)
1927 if vca_type
== "native_charm":
1928 config_primitive
= next(
1929 (p
for p
in initial_config_primitive_list
if p
["name"] == "config"),
1932 if config_primitive
:
1933 config
= self
._map
_primitive
_params
(
1934 config_primitive
, {}, deploy_params
1937 if vca_type
== "lxc_proxy_charm":
1938 if element_type
== "NS":
1939 num_units
= db_nsr
.get("config-units") or 1
1940 elif element_type
== "VNF":
1941 num_units
= db_vnfr
.get("config-units") or 1
1942 elif element_type
== "VDU":
1943 for v
in db_vnfr
["vdur"]:
1944 if vdu_id
== v
["vdu-id-ref"]:
1945 num_units
= v
.get("config-units") or 1
1947 if vca_type
!= "k8s_proxy_charm":
1948 await self
.vca_map
[vca_type
].install_configuration_sw(
1950 artifact_path
=artifact_path
,
1953 num_units
=num_units
,
1958 # write in db flag of configuration_sw already installed
1960 "nsrs", nsr_id
, {db_update_entry
+ "config_sw_installed": True}
1963 # add relations for this VCA (wait for other peers related with this VCA)
1964 is_relation_added
= await self
._add
_vca
_relations
(
1965 logging_text
=logging_text
,
1968 vca_index
=vca_index
,
1971 if not is_relation_added
:
1972 raise LcmException("Relations could not be added to VCA.")
1974 # if SSH access is required, then get execution environment SSH public
1975 # if native charm we have waited already to VM be UP
1976 if vca_type
in ("k8s_proxy_charm", "lxc_proxy_charm", "helm", "helm-v3"):
1979 # self.logger.debug("get ssh key block")
1981 config_descriptor
, ("config-access", "ssh-access", "required")
1983 # self.logger.debug("ssh key needed")
1984 # Needed to inject a ssh key
1987 ("config-access", "ssh-access", "default-user"),
1989 step
= "Install configuration Software, getting public ssh key"
1990 pub_key
= await self
.vca_map
[vca_type
].get_ee_ssh_public__key(
1991 ee_id
=ee_id
, db_dict
=db_dict
, vca_id
=vca_id
1994 step
= "Insert public key into VM user={} ssh_key={}".format(
1998 # self.logger.debug("no need to get ssh key")
1999 step
= "Waiting to VM being up and getting IP address"
2000 self
.logger
.debug(logging_text
+ step
)
2002 # default rw_mgmt_ip to None, avoiding the non definition of the variable
2005 # n2vc_redesign STEP 5.1
2006 # wait for RO (ip-address) Insert pub_key into VM
2009 rw_mgmt_ip
, services
= await self
.wait_kdu_up(
2010 logging_text
, nsr_id
, vnfr_id
, kdu_name
2012 vnfd
= self
.db
.get_one(
2014 {"_id": f
'{db_vnfr["vnfd-id"]}:{db_vnfr["revision"]}'},
2016 kdu
= get_kdu(vnfd
, kdu_name
)
2018 service
["name"] for service
in get_kdu_services(kdu
)
2020 exposed_services
= []
2021 for service
in services
:
2022 if any(s
in service
["name"] for s
in kdu_services
):
2023 exposed_services
.append(service
)
2024 await self
.vca_map
[vca_type
].exec_primitive(
2026 primitive_name
="config",
2028 "osm-config": json
.dumps(
2030 k8s
={"services": exposed_services
}
2037 # This verification is needed in order to avoid trying to add a public key
2038 # to a VM, when the VNF is a KNF (in the edge case where the user creates a VCA
2039 # for a KNF and not for its KDUs, the previous verification gives False, and the code
2040 # jumps to this block, meaning that there is the need to verify if the VNF is actually a VNF
2042 elif db_vnfr
.get("vdur"):
2043 rw_mgmt_ip
= await self
.wait_vm_up_insert_key_ro(
2053 self
.logger
.debug(logging_text
+ " VM_ip_address={}".format(rw_mgmt_ip
))
2055 # store rw_mgmt_ip in deploy params for later replacement
2056 deploy_params
["rw_mgmt_ip"] = rw_mgmt_ip
2058 # n2vc_redesign STEP 6 Execute initial config primitive
2059 step
= "execute initial config primitive"
2061 # wait for dependent primitives execution (NS -> VNF -> VDU)
2062 if initial_config_primitive_list
:
2063 await self
._wait
_dependent
_n
2vc
(nsr_id
, vca_deployed_list
, vca_index
)
2065 # stage, in function of element type: vdu, kdu, vnf or ns
2066 my_vca
= vca_deployed_list
[vca_index
]
2067 if my_vca
.get("vdu_id") or my_vca
.get("kdu_name"):
2069 stage
[0] = "Stage 3/5: running Day-1 primitives for VDU."
2070 elif my_vca
.get("member-vnf-index"):
2072 stage
[0] = "Stage 4/5: running Day-1 primitives for VNF."
2075 stage
[0] = "Stage 5/5: running Day-1 primitives for NS."
2077 self
._write
_configuration
_status
(
2078 nsr_id
=nsr_id
, vca_index
=vca_index
, status
="EXECUTING PRIMITIVE"
2081 self
._write
_op
_status
(op_id
=nslcmop_id
, stage
=stage
)
2083 check_if_terminated_needed
= True
2084 for initial_config_primitive
in initial_config_primitive_list
:
2085 # adding information on the vca_deployed if it is a NS execution environment
2086 if not vca_deployed
["member-vnf-index"]:
2087 deploy_params
["ns_config_info"] = json
.dumps(
2088 self
._get
_ns
_config
_info
(nsr_id
)
2090 # TODO check if already done
2091 primitive_params_
= self
._map
_primitive
_params
(
2092 initial_config_primitive
, {}, deploy_params
2095 step
= "execute primitive '{}' params '{}'".format(
2096 initial_config_primitive
["name"], primitive_params_
2098 self
.logger
.debug(logging_text
+ step
)
2099 await self
.vca_map
[vca_type
].exec_primitive(
2101 primitive_name
=initial_config_primitive
["name"],
2102 params_dict
=primitive_params_
,
2107 # Once some primitive has been exec, check and write at db if it needs to exec terminated primitives
2108 if check_if_terminated_needed
:
2109 if config_descriptor
.get("terminate-config-primitive"):
2111 "nsrs", nsr_id
, {db_update_entry
+ "needed_terminate": True}
2113 check_if_terminated_needed
= False
2115 # TODO register in database that primitive is done
2117 # STEP 7 Configure metrics
2118 if vca_type
== "helm" or vca_type
== "helm-v3":
2119 # TODO: review for those cases where the helm chart is a reference and
2120 # is not part of the NF package
2121 prometheus_jobs
= await self
.extract_prometheus_scrape_jobs(
2123 artifact_path
=artifact_path
,
2124 ee_config_descriptor
=ee_config_descriptor
,
2127 target_ip
=rw_mgmt_ip
,
2128 element_type
=element_type
,
2129 vnf_member_index
=db_vnfr
.get("member-vnf-index-ref", ""),
2131 vdu_index
=vdu_index
,
2133 kdu_index
=kdu_index
,
2139 {db_update_entry
+ "prometheus_jobs": prometheus_jobs
},
2142 for job
in prometheus_jobs
:
2145 {"job_name": job
["job_name"]},
2148 fail_on_empty
=False,
2151 step
= "instantiated at VCA"
2152 self
.logger
.debug(logging_text
+ step
)
2154 self
._write
_configuration
_status
(
2155 nsr_id
=nsr_id
, vca_index
=vca_index
, status
="READY"
2158 except Exception as e
: # TODO not use Exception but N2VC exception
2159 # self.update_db_2("nsrs", nsr_id, {db_update_entry + "instantiation": "FAILED"})
2161 e
, (DbException
, N2VCException
, LcmException
, asyncio
.CancelledError
)
2164 "Exception while {} : {}".format(step
, e
), exc_info
=True
2166 self
._write
_configuration
_status
(
2167 nsr_id
=nsr_id
, vca_index
=vca_index
, status
="BROKEN"
2169 raise LcmException("{}. {}".format(step
, e
)) from e
2171 def _write_ns_status(
2175 current_operation
: str,
2176 current_operation_id
: str,
2177 error_description
: str = None,
2178 error_detail
: str = None,
2179 other_update
: dict = None,
2182 Update db_nsr fields.
2185 :param current_operation:
2186 :param current_operation_id:
2187 :param error_description:
2188 :param error_detail:
2189 :param other_update: Other required changes at database if provided, will be cleared
2193 db_dict
= other_update
or {}
2196 ] = current_operation_id
# for backward compatibility
2197 db_dict
["_admin.current-operation"] = current_operation_id
2198 db_dict
["_admin.operation-type"] = (
2199 current_operation
if current_operation
!= "IDLE" else None
2201 db_dict
["currentOperation"] = current_operation
2202 db_dict
["currentOperationID"] = current_operation_id
2203 db_dict
["errorDescription"] = error_description
2204 db_dict
["errorDetail"] = error_detail
2207 db_dict
["nsState"] = ns_state
2208 self
.update_db_2("nsrs", nsr_id
, db_dict
)
2209 except DbException
as e
:
2210 self
.logger
.warn("Error writing NS status, ns={}: {}".format(nsr_id
, e
))
2212 def _write_op_status(
2216 error_message
: str = None,
2217 queuePosition
: int = 0,
2218 operation_state
: str = None,
2219 other_update
: dict = None,
2222 db_dict
= other_update
or {}
2223 db_dict
["queuePosition"] = queuePosition
2224 if isinstance(stage
, list):
2225 db_dict
["stage"] = stage
[0]
2226 db_dict
["detailed-status"] = " ".join(stage
)
2227 elif stage
is not None:
2228 db_dict
["stage"] = str(stage
)
2230 if error_message
is not None:
2231 db_dict
["errorMessage"] = error_message
2232 if operation_state
is not None:
2233 db_dict
["operationState"] = operation_state
2234 db_dict
["statusEnteredTime"] = time()
2235 self
.update_db_2("nslcmops", op_id
, db_dict
)
2236 except DbException
as e
:
2238 "Error writing OPERATION status for op_id: {} -> {}".format(op_id
, e
)
2241 def _write_all_config_status(self
, db_nsr
: dict, status
: str):
2243 nsr_id
= db_nsr
["_id"]
2244 # configurationStatus
2245 config_status
= db_nsr
.get("configurationStatus")
2248 "configurationStatus.{}.status".format(index
): status
2249 for index
, v
in enumerate(config_status
)
2253 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
2255 except DbException
as e
:
2257 "Error writing all configuration status, ns={}: {}".format(nsr_id
, e
)
2260 def _write_configuration_status(
2265 element_under_configuration
: str = None,
2266 element_type
: str = None,
2267 other_update
: dict = None,
2269 # self.logger.debug('_write_configuration_status(): vca_index={}, status={}'
2270 # .format(vca_index, status))
2273 db_path
= "configurationStatus.{}.".format(vca_index
)
2274 db_dict
= other_update
or {}
2276 db_dict
[db_path
+ "status"] = status
2277 if element_under_configuration
:
2279 db_path
+ "elementUnderConfiguration"
2280 ] = element_under_configuration
2282 db_dict
[db_path
+ "elementType"] = element_type
2283 self
.update_db_2("nsrs", nsr_id
, db_dict
)
2284 except DbException
as e
:
2286 "Error writing configuration status={}, ns={}, vca_index={}: {}".format(
2287 status
, nsr_id
, vca_index
, e
2291 async def _do_placement(self
, logging_text
, db_nslcmop
, db_vnfrs
):
2293 Check and computes the placement, (vim account where to deploy). If it is decided by an external tool, it
2294 sends the request via kafka and wait until the result is wrote at database (nslcmops _admin.plca).
2295 Database is used because the result can be obtained from a different LCM worker in case of HA.
2296 :param logging_text: contains the prefix for logging, with the ns and nslcmop identifiers
2297 :param db_nslcmop: database content of nslcmop
2298 :param db_vnfrs: database content of vnfrs, indexed by member-vnf-index.
2299 :return: True if some modification is done. Modifies database vnfrs and parameter db_vnfr with the
2300 computed 'vim-account-id'
2303 nslcmop_id
= db_nslcmop
["_id"]
2304 placement_engine
= deep_get(db_nslcmop
, ("operationParams", "placement-engine"))
2305 if placement_engine
== "PLA":
2307 logging_text
+ "Invoke and wait for placement optimization"
2309 await self
.msg
.aiowrite("pla", "get_placement", {"nslcmopId": nslcmop_id
})
2310 db_poll_interval
= 5
2311 wait
= db_poll_interval
* 10
2313 while not pla_result
and wait
>= 0:
2314 await asyncio
.sleep(db_poll_interval
)
2315 wait
-= db_poll_interval
2316 db_nslcmop
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
})
2317 pla_result
= deep_get(db_nslcmop
, ("_admin", "pla"))
2321 "Placement timeout for nslcmopId={}".format(nslcmop_id
)
2324 for pla_vnf
in pla_result
["vnf"]:
2325 vnfr
= db_vnfrs
.get(pla_vnf
["member-vnf-index"])
2326 if not pla_vnf
.get("vimAccountId") or not vnfr
:
2331 {"_id": vnfr
["_id"]},
2332 {"vim-account-id": pla_vnf
["vimAccountId"]},
2335 vnfr
["vim-account-id"] = pla_vnf
["vimAccountId"]
2338 def _gather_vnfr_healing_alerts(self
, vnfr
, vnfd
):
2340 nsr_id
= vnfr
["nsr-id-ref"]
2341 df
= vnfd
.get("df", [{}])[0]
2342 # Checking for auto-healing configuration
2343 if "healing-aspect" in df
:
2344 healing_aspects
= df
["healing-aspect"]
2345 for healing
in healing_aspects
:
2346 for healing_policy
in healing
.get("healing-policy", ()):
2347 vdu_id
= healing_policy
["vdu-id"]
2349 (vdur
for vdur
in vnfr
["vdur"] if vdu_id
== vdur
["vdu-id-ref"]),
2354 metric_name
= "vm_status"
2355 vdu_name
= vdur
.get("name")
2356 vnf_member_index
= vnfr
["member-vnf-index-ref"]
2358 name
= f
"healing_{uuid}"
2359 action
= healing_policy
2360 # action_on_recovery = healing.get("action-on-recovery")
2361 # cooldown_time = healing.get("cooldown-time")
2362 # day1 = healing.get("day1")
2366 "metric": metric_name
,
2369 "vnf_member_index": vnf_member_index
,
2370 "vdu_name": vdu_name
,
2372 "alarm_status": "ok",
2373 "action_type": "healing",
2376 alerts
.append(alert
)
2379 def _gather_vnfr_scaling_alerts(self
, vnfr
, vnfd
):
2381 nsr_id
= vnfr
["nsr-id-ref"]
2382 df
= vnfd
.get("df", [{}])[0]
2383 # Checking for auto-scaling configuration
2384 if "scaling-aspect" in df
:
2385 rel_operation_types
= {
2393 scaling_aspects
= df
["scaling-aspect"]
2394 all_vnfd_monitoring_params
= {}
2395 for ivld
in vnfd
.get("int-virtual-link-desc", ()):
2396 for mp
in ivld
.get("monitoring-parameters", ()):
2397 all_vnfd_monitoring_params
[mp
.get("id")] = mp
2398 for vdu
in vnfd
.get("vdu", ()):
2399 for mp
in vdu
.get("monitoring-parameter", ()):
2400 all_vnfd_monitoring_params
[mp
.get("id")] = mp
2401 for df
in vnfd
.get("df", ()):
2402 for mp
in df
.get("monitoring-parameter", ()):
2403 all_vnfd_monitoring_params
[mp
.get("id")] = mp
2404 for scaling_aspect
in scaling_aspects
:
2405 scaling_group_name
= scaling_aspect
.get("name", "")
2406 # Get monitored VDUs
2407 all_monitored_vdus
= set()
2408 for delta
in scaling_aspect
.get("aspect-delta-details", {}).get(
2411 for vdu_delta
in delta
.get("vdu-delta", ()):
2412 all_monitored_vdus
.add(vdu_delta
.get("id"))
2413 monitored_vdurs
= list(
2415 lambda vdur
: vdur
["vdu-id-ref"] in all_monitored_vdus
,
2419 if not monitored_vdurs
:
2421 "Scaling criteria is referring to a vnf-monitoring-param that does not contain a reference to a vdu or vnf metric"
2424 for scaling_policy
in scaling_aspect
.get("scaling-policy", ()):
2425 if scaling_policy
["scaling-type"] != "automatic":
2427 threshold_time
= scaling_policy
.get("threshold-time", "1")
2428 cooldown_time
= scaling_policy
.get("cooldown-time", "0")
2429 for scaling_criteria
in scaling_policy
["scaling-criteria"]:
2430 monitoring_param_ref
= scaling_criteria
.get(
2431 "vnf-monitoring-param-ref"
2433 vnf_monitoring_param
= all_vnfd_monitoring_params
[
2434 monitoring_param_ref
2436 for vdur
in monitored_vdurs
:
2437 vdu_id
= vdur
["vdu-id-ref"]
2438 metric_name
= vnf_monitoring_param
.get("performance-metric")
2439 metric_name
= f
"osm_{metric_name}"
2440 vnf_member_index
= vnfr
["member-vnf-index-ref"]
2441 scalein_threshold
= scaling_criteria
.get(
2442 "scale-in-threshold"
2444 scaleout_threshold
= scaling_criteria
.get(
2445 "scale-out-threshold"
2447 # Looking for min/max-number-of-instances
2448 instances_min_number
= 1
2449 instances_max_number
= 1
2450 vdu_profile
= df
["vdu-profile"]
2453 item
for item
in vdu_profile
if item
["id"] == vdu_id
2455 instances_min_number
= profile
.get(
2456 "min-number-of-instances", 1
2458 instances_max_number
= profile
.get(
2459 "max-number-of-instances", 1
2462 if scalein_threshold
:
2464 name
= f
"scalein_{uuid}"
2465 operation
= scaling_criteria
[
2466 "scale-in-relational-operation"
2468 rel_operator
= rel_operation_types
.get(operation
, "<=")
2469 metric_selector
= f
'{metric_name}{{ns_id="{nsr_id}", vnf_member_index="{vnf_member_index}", vdu_id="{vdu_id}"}}'
2470 expression
= f
"(count ({metric_selector}) > {instances_min_number}) and (avg({metric_selector}) {rel_operator} {scalein_threshold})"
2473 "vnf_member_index": vnf_member_index
,
2479 "for": str(threshold_time
) + "m",
2482 action
= scaling_policy
2484 "scaling-group": scaling_group_name
,
2485 "cooldown-time": cooldown_time
,
2490 "metric": metric_name
,
2493 "vnf_member_index": vnf_member_index
,
2496 "alarm_status": "ok",
2497 "action_type": "scale_in",
2499 "prometheus_config": prom_cfg
,
2501 alerts
.append(alert
)
2503 if scaleout_threshold
:
2505 name
= f
"scaleout_{uuid}"
2506 operation
= scaling_criteria
[
2507 "scale-out-relational-operation"
2509 rel_operator
= rel_operation_types
.get(operation
, "<=")
2510 metric_selector
= f
'{metric_name}{{ns_id="{nsr_id}", vnf_member_index="{vnf_member_index}", vdu_id="{vdu_id}"}}'
2511 expression
= f
"(count ({metric_selector}) < {instances_max_number}) and (avg({metric_selector}) {rel_operator} {scaleout_threshold})"
2514 "vnf_member_index": vnf_member_index
,
2520 "for": str(threshold_time
) + "m",
2523 action
= scaling_policy
2525 "scaling-group": scaling_group_name
,
2526 "cooldown-time": cooldown_time
,
2531 "metric": metric_name
,
2534 "vnf_member_index": vnf_member_index
,
2537 "alarm_status": "ok",
2538 "action_type": "scale_out",
2540 "prometheus_config": prom_cfg
,
2542 alerts
.append(alert
)
2545 def update_nsrs_with_pla_result(self
, params
):
2547 nslcmop_id
= deep_get(params
, ("placement", "nslcmopId"))
2549 "nslcmops", nslcmop_id
, {"_admin.pla": params
.get("placement")}
2551 except Exception as e
:
2552 self
.logger
.warn("Update failed for nslcmop_id={}:{}".format(nslcmop_id
, e
))
2554 async def instantiate(self
, nsr_id
, nslcmop_id
):
2557 :param nsr_id: ns instance to deploy
2558 :param nslcmop_id: operation to run
2562 # Try to lock HA task here
2563 task_is_locked_by_me
= self
.lcm_tasks
.lock_HA("ns", "nslcmops", nslcmop_id
)
2564 if not task_is_locked_by_me
:
2566 "instantiate() task is not locked by me, ns={}".format(nsr_id
)
2570 logging_text
= "Task ns={} instantiate={} ".format(nsr_id
, nslcmop_id
)
2571 self
.logger
.debug(logging_text
+ "Enter")
2573 # get all needed from database
2575 # database nsrs record
2578 # database nslcmops record
2581 # update operation on nsrs
2583 # update operation on nslcmops
2584 db_nslcmop_update
= {}
2586 timeout_ns_deploy
= self
.timeout
.ns_deploy
2588 nslcmop_operation_state
= None
2589 db_vnfrs
= {} # vnf's info indexed by member-index
2591 tasks_dict_info
= {} # from task to info text
2595 "Stage 1/5: preparation of the environment.",
2596 "Waiting for previous operations to terminate.",
2599 # ^ stage, step, VIM progress
2601 # wait for any previous tasks in process
2602 await self
.lcm_tasks
.waitfor_related_HA("ns", "nslcmops", nslcmop_id
)
2604 # STEP 0: Reading database (nslcmops, nsrs, nsds, vnfrs, vnfds)
2605 stage
[1] = "Reading from database."
2606 # nsState="BUILDING", currentOperation="INSTANTIATING", currentOperationID=nslcmop_id
2607 db_nsr_update
["detailed-status"] = "creating"
2608 db_nsr_update
["operational-status"] = "init"
2609 self
._write
_ns
_status
(
2611 ns_state
="BUILDING",
2612 current_operation
="INSTANTIATING",
2613 current_operation_id
=nslcmop_id
,
2614 other_update
=db_nsr_update
,
2616 self
._write
_op
_status
(op_id
=nslcmop_id
, stage
=stage
, queuePosition
=0)
2618 # read from db: operation
2619 stage
[1] = "Getting nslcmop={} from db.".format(nslcmop_id
)
2620 db_nslcmop
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
})
2621 if db_nslcmop
["operationParams"].get("additionalParamsForVnf"):
2622 db_nslcmop
["operationParams"]["additionalParamsForVnf"] = json
.loads(
2623 db_nslcmop
["operationParams"]["additionalParamsForVnf"]
2625 ns_params
= db_nslcmop
.get("operationParams")
2626 if ns_params
and ns_params
.get("timeout_ns_deploy"):
2627 timeout_ns_deploy
= ns_params
["timeout_ns_deploy"]
2630 stage
[1] = "Getting nsr={} from db.".format(nsr_id
)
2631 self
.logger
.debug(logging_text
+ stage
[1])
2632 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
2633 stage
[1] = "Getting nsd={} from db.".format(db_nsr
["nsd-id"])
2634 self
.logger
.debug(logging_text
+ stage
[1])
2635 nsd
= self
.db
.get_one("nsds", {"_id": db_nsr
["nsd-id"]})
2636 self
.fs
.sync(db_nsr
["nsd-id"])
2638 # nsr_name = db_nsr["name"] # TODO short-name??
2640 # read from db: vnf's of this ns
2641 stage
[1] = "Getting vnfrs from db."
2642 self
.logger
.debug(logging_text
+ stage
[1])
2643 db_vnfrs_list
= self
.db
.get_list("vnfrs", {"nsr-id-ref": nsr_id
})
2645 # read from db: vnfd's for every vnf
2646 db_vnfds
= [] # every vnfd data
2648 # for each vnf in ns, read vnfd
2649 for vnfr
in db_vnfrs_list
:
2650 if vnfr
.get("kdur"):
2652 for kdur
in vnfr
["kdur"]:
2653 if kdur
.get("additionalParams"):
2654 kdur
["additionalParams"] = json
.loads(
2655 kdur
["additionalParams"]
2657 kdur_list
.append(kdur
)
2658 vnfr
["kdur"] = kdur_list
2660 db_vnfrs
[vnfr
["member-vnf-index-ref"]] = vnfr
2661 vnfd_id
= vnfr
["vnfd-id"]
2662 vnfd_ref
= vnfr
["vnfd-ref"]
2663 self
.fs
.sync(vnfd_id
)
2665 # if we haven't this vnfd, read it from db
2666 if vnfd_id
not in db_vnfds
:
2668 stage
[1] = "Getting vnfd={} id='{}' from db.".format(
2671 self
.logger
.debug(logging_text
+ stage
[1])
2672 vnfd
= self
.db
.get_one("vnfds", {"_id": vnfd_id
})
2675 db_vnfds
.append(vnfd
)
2677 # Get or generates the _admin.deployed.VCA list
2678 vca_deployed_list
= None
2679 if db_nsr
["_admin"].get("deployed"):
2680 vca_deployed_list
= db_nsr
["_admin"]["deployed"].get("VCA")
2681 if vca_deployed_list
is None:
2682 vca_deployed_list
= []
2683 configuration_status_list
= []
2684 db_nsr_update
["_admin.deployed.VCA"] = vca_deployed_list
2685 db_nsr_update
["configurationStatus"] = configuration_status_list
2686 # add _admin.deployed.VCA to db_nsr dictionary, value=vca_deployed_list
2687 populate_dict(db_nsr
, ("_admin", "deployed", "VCA"), vca_deployed_list
)
2688 elif isinstance(vca_deployed_list
, dict):
2689 # maintain backward compatibility. Change a dict to list at database
2690 vca_deployed_list
= list(vca_deployed_list
.values())
2691 db_nsr_update
["_admin.deployed.VCA"] = vca_deployed_list
2692 populate_dict(db_nsr
, ("_admin", "deployed", "VCA"), vca_deployed_list
)
2695 deep_get(db_nsr
, ("_admin", "deployed", "RO", "vnfd")), list
2697 populate_dict(db_nsr
, ("_admin", "deployed", "RO", "vnfd"), [])
2698 db_nsr_update
["_admin.deployed.RO.vnfd"] = []
2700 # set state to INSTANTIATED. When instantiated NBI will not delete directly
2701 db_nsr_update
["_admin.nsState"] = "INSTANTIATED"
2702 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
2704 "vnfrs", {"nsr-id-ref": nsr_id
}, {"_admin.nsState": "INSTANTIATED"}
2707 # n2vc_redesign STEP 2 Deploy Network Scenario
2708 stage
[0] = "Stage 2/5: deployment of KDUs, VMs and execution environments."
2709 self
._write
_op
_status
(op_id
=nslcmop_id
, stage
=stage
)
2711 stage
[1] = "Deploying KDUs."
2712 # self.logger.debug(logging_text + "Before deploy_kdus")
2713 # Call to deploy_kdus in case exists the "vdu:kdu" param
2714 await self
.deploy_kdus(
2715 logging_text
=logging_text
,
2717 nslcmop_id
=nslcmop_id
,
2720 task_instantiation_info
=tasks_dict_info
,
2723 stage
[1] = "Getting VCA public key."
2724 # n2vc_redesign STEP 1 Get VCA public ssh-key
2725 # feature 1429. Add n2vc public key to needed VMs
2726 n2vc_key
= self
.n2vc
.get_public_key()
2727 n2vc_key_list
= [n2vc_key
]
2728 if self
.vca_config
.public_key
:
2729 n2vc_key_list
.append(self
.vca_config
.public_key
)
2731 stage
[1] = "Deploying NS at VIM."
2732 task_ro
= asyncio
.ensure_future(
2733 self
.instantiate_RO(
2734 logging_text
=logging_text
,
2738 db_nslcmop
=db_nslcmop
,
2741 n2vc_key_list
=n2vc_key_list
,
2745 self
.lcm_tasks
.register("ns", nsr_id
, nslcmop_id
, "instantiate_RO", task_ro
)
2746 tasks_dict_info
[task_ro
] = "Deploying at VIM"
2748 # n2vc_redesign STEP 3 to 6 Deploy N2VC
2749 stage
[1] = "Deploying Execution Environments."
2750 self
.logger
.debug(logging_text
+ stage
[1])
2752 # create namespace and certificate if any helm based EE is present in the NS
2753 if check_helm_ee_in_ns(db_vnfds
):
2754 await self
.vca_map
["helm-v3"].setup_ns_namespace(
2757 # create TLS certificates
2758 await self
.vca_map
["helm-v3"].create_tls_certificate(
2759 secret_name
=self
.EE_TLS_NAME
,
2762 usage
="server auth",
2766 nsi_id
= None # TODO put nsi_id when this nsr belongs to a NSI
2767 for vnf_profile
in get_vnf_profiles(nsd
):
2768 vnfd_id
= vnf_profile
["vnfd-id"]
2769 vnfd
= find_in_list(db_vnfds
, lambda a_vnf
: a_vnf
["id"] == vnfd_id
)
2770 member_vnf_index
= str(vnf_profile
["id"])
2771 db_vnfr
= db_vnfrs
[member_vnf_index
]
2772 base_folder
= vnfd
["_admin"]["storage"]
2779 # Get additional parameters
2780 deploy_params
= {"OSM": get_osm_params(db_vnfr
)}
2781 if db_vnfr
.get("additionalParamsForVnf"):
2782 deploy_params
.update(
2783 parse_yaml_strings(db_vnfr
["additionalParamsForVnf"].copy())
2786 descriptor_config
= get_configuration(vnfd
, vnfd
["id"])
2787 if descriptor_config
:
2789 logging_text
=logging_text
2790 + "member_vnf_index={} ".format(member_vnf_index
),
2793 nslcmop_id
=nslcmop_id
,
2799 member_vnf_index
=member_vnf_index
,
2800 vdu_index
=vdu_index
,
2801 kdu_index
=kdu_index
,
2803 deploy_params
=deploy_params
,
2804 descriptor_config
=descriptor_config
,
2805 base_folder
=base_folder
,
2806 task_instantiation_info
=tasks_dict_info
,
2810 # Deploy charms for each VDU that supports one.
2811 for vdud
in get_vdu_list(vnfd
):
2813 descriptor_config
= get_configuration(vnfd
, vdu_id
)
2814 vdur
= find_in_list(
2815 db_vnfr
["vdur"], lambda vdu
: vdu
["vdu-id-ref"] == vdu_id
2818 if vdur
.get("additionalParams"):
2819 deploy_params_vdu
= parse_yaml_strings(vdur
["additionalParams"])
2821 deploy_params_vdu
= deploy_params
2822 deploy_params_vdu
["OSM"] = get_osm_params(
2823 db_vnfr
, vdu_id
, vdu_count_index
=0
2825 vdud_count
= get_number_of_instances(vnfd
, vdu_id
)
2827 self
.logger
.debug("VDUD > {}".format(vdud
))
2829 "Descriptor config > {}".format(descriptor_config
)
2831 if descriptor_config
:
2835 for vdu_index
in range(vdud_count
):
2836 # TODO vnfr_params["rw_mgmt_ip"] = vdur["ip-address"]
2838 logging_text
=logging_text
2839 + "member_vnf_index={}, vdu_id={}, vdu_index={} ".format(
2840 member_vnf_index
, vdu_id
, vdu_index
2844 nslcmop_id
=nslcmop_id
,
2850 kdu_index
=kdu_index
,
2851 member_vnf_index
=member_vnf_index
,
2852 vdu_index
=vdu_index
,
2854 deploy_params
=deploy_params_vdu
,
2855 descriptor_config
=descriptor_config
,
2856 base_folder
=base_folder
,
2857 task_instantiation_info
=tasks_dict_info
,
2860 for kdud
in get_kdu_list(vnfd
):
2861 kdu_name
= kdud
["name"]
2862 descriptor_config
= get_configuration(vnfd
, kdu_name
)
2863 if descriptor_config
:
2867 kdu_index
, kdur
= next(
2869 for x
in enumerate(db_vnfr
["kdur"])
2870 if x
[1]["kdu-name"] == kdu_name
2872 deploy_params_kdu
= {"OSM": get_osm_params(db_vnfr
)}
2873 if kdur
.get("additionalParams"):
2874 deploy_params_kdu
.update(
2875 parse_yaml_strings(kdur
["additionalParams"].copy())
2879 logging_text
=logging_text
,
2882 nslcmop_id
=nslcmop_id
,
2888 member_vnf_index
=member_vnf_index
,
2889 vdu_index
=vdu_index
,
2890 kdu_index
=kdu_index
,
2892 deploy_params
=deploy_params_kdu
,
2893 descriptor_config
=descriptor_config
,
2894 base_folder
=base_folder
,
2895 task_instantiation_info
=tasks_dict_info
,
2899 # Check if each vnf has exporter for metric collection if so update prometheus job records
2900 if "exporters-endpoints" in vnfd
.get("df")[0]:
2901 exporter_config
= vnfd
.get("df")[0].get("exporters-endpoints")
2902 self
.logger
.debug("exporter config :{}".format(exporter_config
))
2903 artifact_path
= "{}/{}/{}".format(
2904 base_folder
["folder"],
2905 base_folder
["pkg-dir"],
2906 "exporter-endpoint",
2909 ee_config_descriptor
= exporter_config
2910 vnfr_id
= db_vnfr
["id"]
2911 rw_mgmt_ip
= await self
.wait_vm_up_insert_key_ro(
2920 self
.logger
.debug("rw_mgmt_ip:{}".format(rw_mgmt_ip
))
2921 self
.logger
.debug("Artifact_path:{}".format(artifact_path
))
2922 db_vnfr
= self
.db
.get_one("vnfrs", {"_id": vnfr_id
})
2923 vdu_id_for_prom
= None
2924 vdu_index_for_prom
= None
2925 for x
in get_iterable(db_vnfr
, "vdur"):
2926 vdu_id_for_prom
= x
.get("vdu-id-ref")
2927 vdu_index_for_prom
= x
.get("count-index")
2928 prometheus_jobs
= await self
.extract_prometheus_scrape_jobs(
2930 artifact_path
=artifact_path
,
2931 ee_config_descriptor
=ee_config_descriptor
,
2934 target_ip
=rw_mgmt_ip
,
2936 vdu_id
=vdu_id_for_prom
,
2937 vdu_index
=vdu_index_for_prom
,
2940 self
.logger
.debug("Prometheus job:{}".format(prometheus_jobs
))
2942 db_nsr_update
["_admin.deployed.prometheus_jobs"] = prometheus_jobs
2949 for job
in prometheus_jobs
:
2952 {"job_name": job
["job_name"]},
2955 fail_on_empty
=False,
2958 # Check if this NS has a charm configuration
2959 descriptor_config
= nsd
.get("ns-configuration")
2960 if descriptor_config
and descriptor_config
.get("juju"):
2963 member_vnf_index
= None
2970 # Get additional parameters
2971 deploy_params
= {"OSM": {"vim_account_id": ns_params
["vimAccountId"]}}
2972 if db_nsr
.get("additionalParamsForNs"):
2973 deploy_params
.update(
2974 parse_yaml_strings(db_nsr
["additionalParamsForNs"].copy())
2976 base_folder
= nsd
["_admin"]["storage"]
2978 logging_text
=logging_text
,
2981 nslcmop_id
=nslcmop_id
,
2987 member_vnf_index
=member_vnf_index
,
2988 vdu_index
=vdu_index
,
2989 kdu_index
=kdu_index
,
2991 deploy_params
=deploy_params
,
2992 descriptor_config
=descriptor_config
,
2993 base_folder
=base_folder
,
2994 task_instantiation_info
=tasks_dict_info
,
2998 # rest of staff will be done at finally
3001 ROclient
.ROClientException
,
3007 logging_text
+ "Exit Exception while '{}': {}".format(stage
[1], e
)
3010 except asyncio
.CancelledError
:
3012 logging_text
+ "Cancelled Exception while '{}'".format(stage
[1])
3014 exc
= "Operation was cancelled"
3015 except Exception as e
:
3016 exc
= traceback
.format_exc()
3017 self
.logger
.critical(
3018 logging_text
+ "Exit Exception while '{}': {}".format(stage
[1], e
),
3023 error_list
.append(str(exc
))
3025 # wait for pending tasks
3027 stage
[1] = "Waiting for instantiate pending tasks."
3028 self
.logger
.debug(logging_text
+ stage
[1])
3029 error_list
+= await self
._wait
_for
_tasks
(
3037 stage
[1] = stage
[2] = ""
3038 except asyncio
.CancelledError
:
3039 error_list
.append("Cancelled")
3040 # TODO cancel all tasks
3041 except Exception as exc
:
3042 error_list
.append(str(exc
))
3044 # update operation-status
3045 db_nsr_update
["operational-status"] = "running"
3046 # let's begin with VCA 'configured' status (later we can change it)
3047 db_nsr_update
["config-status"] = "configured"
3048 for task
, task_name
in tasks_dict_info
.items():
3049 if not task
.done() or task
.cancelled() or task
.exception():
3050 if task_name
.startswith(self
.task_name_deploy_vca
):
3051 # A N2VC task is pending
3052 db_nsr_update
["config-status"] = "failed"
3054 # RO or KDU task is pending
3055 db_nsr_update
["operational-status"] = "failed"
3057 # update status at database
3059 error_detail
= ". ".join(error_list
)
3060 self
.logger
.error(logging_text
+ error_detail
)
3061 error_description_nslcmop
= "{} Detail: {}".format(
3062 stage
[0], error_detail
3064 error_description_nsr
= "Operation: INSTANTIATING.{}, {}".format(
3065 nslcmop_id
, stage
[0]
3068 db_nsr_update
["detailed-status"] = (
3069 error_description_nsr
+ " Detail: " + error_detail
3071 db_nslcmop_update
["detailed-status"] = error_detail
3072 nslcmop_operation_state
= "FAILED"
3076 error_description_nsr
= error_description_nslcmop
= None
3078 db_nsr_update
["detailed-status"] = "Done"
3079 db_nslcmop_update
["detailed-status"] = "Done"
3080 nslcmop_operation_state
= "COMPLETED"
3081 # Gather auto-healing and auto-scaling alerts for each vnfr
3084 for vnfr
in self
.db
.get_list("vnfrs", {"nsr-id-ref": nsr_id
}):
3086 (sub
for sub
in db_vnfds
if sub
["_id"] == vnfr
["vnfd-id"]), None
3088 healing_alerts
= self
._gather
_vnfr
_healing
_alerts
(vnfr
, vnfd
)
3089 for alert
in healing_alerts
:
3090 self
.logger
.info(f
"Storing healing alert in MongoDB: {alert}")
3091 self
.db
.create("alerts", alert
)
3093 scaling_alerts
= self
._gather
_vnfr
_scaling
_alerts
(vnfr
, vnfd
)
3094 for alert
in scaling_alerts
:
3095 self
.logger
.info(f
"Storing scaling alert in MongoDB: {alert}")
3096 self
.db
.create("alerts", alert
)
3099 self
._write
_ns
_status
(
3102 current_operation
="IDLE",
3103 current_operation_id
=None,
3104 error_description
=error_description_nsr
,
3105 error_detail
=error_detail
,
3106 other_update
=db_nsr_update
,
3108 self
._write
_op
_status
(
3111 error_message
=error_description_nslcmop
,
3112 operation_state
=nslcmop_operation_state
,
3113 other_update
=db_nslcmop_update
,
3116 if nslcmop_operation_state
:
3118 await self
.msg
.aiowrite(
3123 "nslcmop_id": nslcmop_id
,
3124 "operationState": nslcmop_operation_state
,
3127 except Exception as e
:
3129 logging_text
+ "kafka_write notification Exception {}".format(e
)
3132 self
.logger
.debug(logging_text
+ "Exit")
3133 self
.lcm_tasks
.remove("ns", nsr_id
, nslcmop_id
, "ns_instantiate")
3135 def _get_vnfd(self
, vnfd_id
: str, projects_read
: str, cached_vnfds
: Dict
[str, Any
]):
3136 if vnfd_id
not in cached_vnfds
:
3137 cached_vnfds
[vnfd_id
] = self
.db
.get_one(
3138 "vnfds", {"id": vnfd_id
, "_admin.projects_read": projects_read
}
3140 return cached_vnfds
[vnfd_id
]
3142 def _get_vnfr(self
, nsr_id
: str, vnf_profile_id
: str, cached_vnfrs
: Dict
[str, Any
]):
3143 if vnf_profile_id
not in cached_vnfrs
:
3144 cached_vnfrs
[vnf_profile_id
] = self
.db
.get_one(
3147 "member-vnf-index-ref": vnf_profile_id
,
3148 "nsr-id-ref": nsr_id
,
3151 return cached_vnfrs
[vnf_profile_id
]
3153 def _is_deployed_vca_in_relation(
3154 self
, vca
: DeployedVCA
, relation
: Relation
3157 for endpoint
in (relation
.provider
, relation
.requirer
):
3158 if endpoint
["kdu-resource-profile-id"]:
3161 vca
.vnf_profile_id
== endpoint
.vnf_profile_id
3162 and vca
.vdu_profile_id
== endpoint
.vdu_profile_id
3163 and vca
.execution_environment_ref
== endpoint
.execution_environment_ref
3169 def _update_ee_relation_data_with_implicit_data(
3170 self
, nsr_id
, nsd
, ee_relation_data
, cached_vnfds
, vnf_profile_id
: str = None
3172 ee_relation_data
= safe_get_ee_relation(
3173 nsr_id
, ee_relation_data
, vnf_profile_id
=vnf_profile_id
3175 ee_relation_level
= EELevel
.get_level(ee_relation_data
)
3176 if (ee_relation_level
in (EELevel
.VNF
, EELevel
.VDU
)) and not ee_relation_data
[
3177 "execution-environment-ref"
3179 vnf_profile
= get_vnf_profile(nsd
, ee_relation_data
["vnf-profile-id"])
3180 vnfd_id
= vnf_profile
["vnfd-id"]
3181 project
= nsd
["_admin"]["projects_read"][0]
3182 db_vnfd
= self
._get
_vnfd
(vnfd_id
, project
, cached_vnfds
)
3185 if ee_relation_level
== EELevel
.VNF
3186 else ee_relation_data
["vdu-profile-id"]
3188 ee
= get_juju_ee_ref(db_vnfd
, entity_id
)
3191 f
"not execution environments found for ee_relation {ee_relation_data}"
3193 ee_relation_data
["execution-environment-ref"] = ee
["id"]
3194 return ee_relation_data
3196 def _get_ns_relations(
3199 nsd
: Dict
[str, Any
],
3201 cached_vnfds
: Dict
[str, Any
],
3202 ) -> List
[Relation
]:
3204 db_ns_relations
= get_ns_configuration_relation_list(nsd
)
3205 for r
in db_ns_relations
:
3206 provider_dict
= None
3207 requirer_dict
= None
3208 if all(key
in r
for key
in ("provider", "requirer")):
3209 provider_dict
= r
["provider"]
3210 requirer_dict
= r
["requirer"]
3211 elif "entities" in r
:
3212 provider_id
= r
["entities"][0]["id"]
3215 "endpoint": r
["entities"][0]["endpoint"],
3217 if provider_id
!= nsd
["id"]:
3218 provider_dict
["vnf-profile-id"] = provider_id
3219 requirer_id
= r
["entities"][1]["id"]
3222 "endpoint": r
["entities"][1]["endpoint"],
3224 if requirer_id
!= nsd
["id"]:
3225 requirer_dict
["vnf-profile-id"] = requirer_id
3228 "provider/requirer or entities must be included in the relation."
3230 relation_provider
= self
._update
_ee
_relation
_data
_with
_implicit
_data
(
3231 nsr_id
, nsd
, provider_dict
, cached_vnfds
3233 relation_requirer
= self
._update
_ee
_relation
_data
_with
_implicit
_data
(
3234 nsr_id
, nsd
, requirer_dict
, cached_vnfds
3236 provider
= EERelation(relation_provider
)
3237 requirer
= EERelation(relation_requirer
)
3238 relation
= Relation(r
["name"], provider
, requirer
)
3239 vca_in_relation
= self
._is
_deployed
_vca
_in
_relation
(vca
, relation
)
3241 relations
.append(relation
)
3244 def _get_vnf_relations(
3247 nsd
: Dict
[str, Any
],
3249 cached_vnfds
: Dict
[str, Any
],
3250 ) -> List
[Relation
]:
3252 if vca
.target_element
== "ns":
3253 self
.logger
.debug("VCA is a NS charm, not a VNF.")
3255 vnf_profile
= get_vnf_profile(nsd
, vca
.vnf_profile_id
)
3256 vnf_profile_id
= vnf_profile
["id"]
3257 vnfd_id
= vnf_profile
["vnfd-id"]
3258 project
= nsd
["_admin"]["projects_read"][0]
3259 db_vnfd
= self
._get
_vnfd
(vnfd_id
, project
, cached_vnfds
)
3260 db_vnf_relations
= get_relation_list(db_vnfd
, vnfd_id
)
3261 for r
in db_vnf_relations
:
3262 provider_dict
= None
3263 requirer_dict
= None
3264 if all(key
in r
for key
in ("provider", "requirer")):
3265 provider_dict
= r
["provider"]
3266 requirer_dict
= r
["requirer"]
3267 elif "entities" in r
:
3268 provider_id
= r
["entities"][0]["id"]
3271 "vnf-profile-id": vnf_profile_id
,
3272 "endpoint": r
["entities"][0]["endpoint"],
3274 if provider_id
!= vnfd_id
:
3275 provider_dict
["vdu-profile-id"] = provider_id
3276 requirer_id
= r
["entities"][1]["id"]
3279 "vnf-profile-id": vnf_profile_id
,
3280 "endpoint": r
["entities"][1]["endpoint"],
3282 if requirer_id
!= vnfd_id
:
3283 requirer_dict
["vdu-profile-id"] = requirer_id
3286 "provider/requirer or entities must be included in the relation."
3288 relation_provider
= self
._update
_ee
_relation
_data
_with
_implicit
_data
(
3289 nsr_id
, nsd
, provider_dict
, cached_vnfds
, vnf_profile_id
=vnf_profile_id
3291 relation_requirer
= self
._update
_ee
_relation
_data
_with
_implicit
_data
(
3292 nsr_id
, nsd
, requirer_dict
, cached_vnfds
, vnf_profile_id
=vnf_profile_id
3294 provider
= EERelation(relation_provider
)
3295 requirer
= EERelation(relation_requirer
)
3296 relation
= Relation(r
["name"], provider
, requirer
)
3297 vca_in_relation
= self
._is
_deployed
_vca
_in
_relation
(vca
, relation
)
3299 relations
.append(relation
)
3302 def _get_kdu_resource_data(
3304 ee_relation
: EERelation
,
3305 db_nsr
: Dict
[str, Any
],
3306 cached_vnfds
: Dict
[str, Any
],
3307 ) -> DeployedK8sResource
:
3308 nsd
= get_nsd(db_nsr
)
3309 vnf_profiles
= get_vnf_profiles(nsd
)
3310 vnfd_id
= find_in_list(
3312 lambda vnf_profile
: vnf_profile
["id"] == ee_relation
.vnf_profile_id
,
3314 project
= nsd
["_admin"]["projects_read"][0]
3315 db_vnfd
= self
._get
_vnfd
(vnfd_id
, project
, cached_vnfds
)
3316 kdu_resource_profile
= get_kdu_resource_profile(
3317 db_vnfd
, ee_relation
.kdu_resource_profile_id
3319 kdu_name
= kdu_resource_profile
["kdu-name"]
3320 deployed_kdu
, _
= get_deployed_kdu(
3321 db_nsr
.get("_admin", ()).get("deployed", ()),
3323 ee_relation
.vnf_profile_id
,
3325 deployed_kdu
.update({"resource-name": kdu_resource_profile
["resource-name"]})
3328 def _get_deployed_component(
3330 ee_relation
: EERelation
,
3331 db_nsr
: Dict
[str, Any
],
3332 cached_vnfds
: Dict
[str, Any
],
3333 ) -> DeployedComponent
:
3334 nsr_id
= db_nsr
["_id"]
3335 deployed_component
= None
3336 ee_level
= EELevel
.get_level(ee_relation
)
3337 if ee_level
== EELevel
.NS
:
3338 vca
= get_deployed_vca(db_nsr
, {"vdu_id": None, "member-vnf-index": None})
3340 deployed_component
= DeployedVCA(nsr_id
, vca
)
3341 elif ee_level
== EELevel
.VNF
:
3342 vca
= get_deployed_vca(
3346 "member-vnf-index": ee_relation
.vnf_profile_id
,
3347 "ee_descriptor_id": ee_relation
.execution_environment_ref
,
3351 deployed_component
= DeployedVCA(nsr_id
, vca
)
3352 elif ee_level
== EELevel
.VDU
:
3353 vca
= get_deployed_vca(
3356 "vdu_id": ee_relation
.vdu_profile_id
,
3357 "member-vnf-index": ee_relation
.vnf_profile_id
,
3358 "ee_descriptor_id": ee_relation
.execution_environment_ref
,
3362 deployed_component
= DeployedVCA(nsr_id
, vca
)
3363 elif ee_level
== EELevel
.KDU
:
3364 kdu_resource_data
= self
._get
_kdu
_resource
_data
(
3365 ee_relation
, db_nsr
, cached_vnfds
3367 if kdu_resource_data
:
3368 deployed_component
= DeployedK8sResource(kdu_resource_data
)
3369 return deployed_component
3371 async def _add_relation(
3375 db_nsr
: Dict
[str, Any
],
3376 cached_vnfds
: Dict
[str, Any
],
3377 cached_vnfrs
: Dict
[str, Any
],
3379 deployed_provider
= self
._get
_deployed
_component
(
3380 relation
.provider
, db_nsr
, cached_vnfds
3382 deployed_requirer
= self
._get
_deployed
_component
(
3383 relation
.requirer
, db_nsr
, cached_vnfds
3387 and deployed_requirer
3388 and deployed_provider
.config_sw_installed
3389 and deployed_requirer
.config_sw_installed
3391 provider_db_vnfr
= (
3393 relation
.provider
.nsr_id
,
3394 relation
.provider
.vnf_profile_id
,
3397 if relation
.provider
.vnf_profile_id
3400 requirer_db_vnfr
= (
3402 relation
.requirer
.nsr_id
,
3403 relation
.requirer
.vnf_profile_id
,
3406 if relation
.requirer
.vnf_profile_id
3409 provider_vca_id
= self
.get_vca_id(provider_db_vnfr
, db_nsr
)
3410 requirer_vca_id
= self
.get_vca_id(requirer_db_vnfr
, db_nsr
)
3411 provider_relation_endpoint
= RelationEndpoint(
3412 deployed_provider
.ee_id
,
3414 relation
.provider
.endpoint
,
3416 requirer_relation_endpoint
= RelationEndpoint(
3417 deployed_requirer
.ee_id
,
3419 relation
.requirer
.endpoint
,
3422 await self
.vca_map
[vca_type
].add_relation(
3423 provider
=provider_relation_endpoint
,
3424 requirer
=requirer_relation_endpoint
,
3426 except N2VCException
as exception
:
3427 self
.logger
.error(exception
)
3428 raise LcmException(exception
)
3432 async def _add_vca_relations(
3438 timeout
: int = 3600,
3441 # 1. find all relations for this VCA
3442 # 2. wait for other peers related
3446 # STEP 1: find all relations for this VCA
3449 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
3450 nsd
= get_nsd(db_nsr
)
3453 deployed_vca_dict
= get_deployed_vca_list(db_nsr
)[vca_index
]
3454 my_vca
= DeployedVCA(nsr_id
, deployed_vca_dict
)
3459 relations
.extend(self
._get
_ns
_relations
(nsr_id
, nsd
, my_vca
, cached_vnfds
))
3460 relations
.extend(self
._get
_vnf
_relations
(nsr_id
, nsd
, my_vca
, cached_vnfds
))
3462 # if no relations, terminate
3464 self
.logger
.debug(logging_text
+ " No relations")
3467 self
.logger
.debug(logging_text
+ " adding relations {}".format(relations
))
3474 if now
- start
>= timeout
:
3475 self
.logger
.error(logging_text
+ " : timeout adding relations")
3478 # reload nsr from database (we need to update record: _admin.deployed.VCA)
3479 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
3481 # for each relation, find the VCA's related
3482 for relation
in relations
.copy():
3483 added
= await self
._add
_relation
(
3491 relations
.remove(relation
)
3494 self
.logger
.debug("Relations added")
3496 await asyncio
.sleep(5.0)
3500 except Exception as e
:
3501 self
.logger
.warn(logging_text
+ " ERROR adding relations: {}".format(e
))
3504 async def _install_kdu(
3512 k8s_instance_info
: dict,
3513 k8params
: dict = None,
3518 k8sclustertype
= k8s_instance_info
["k8scluster-type"]
3521 "collection": "nsrs",
3522 "filter": {"_id": nsr_id
},
3523 "path": nsr_db_path
,
3526 if k8s_instance_info
.get("kdu-deployment-name"):
3527 kdu_instance
= k8s_instance_info
.get("kdu-deployment-name")
3529 kdu_instance
= self
.k8scluster_map
[
3531 ].generate_kdu_instance_name(
3532 db_dict
=db_dict_install
,
3533 kdu_model
=k8s_instance_info
["kdu-model"],
3534 kdu_name
=k8s_instance_info
["kdu-name"],
3537 # Update the nsrs table with the kdu-instance value
3541 _desc
={nsr_db_path
+ ".kdu-instance": kdu_instance
},
3544 # Update the nsrs table with the actual namespace being used, if the k8scluster-type is `juju` or
3545 # `juju-bundle`. This verification is needed because there is not a standard/homogeneous namespace
3546 # between the Helm Charts and Juju Bundles-based KNFs. If we found a way of having an homogeneous
3547 # namespace, this first verification could be removed, and the next step would be done for any kind
3549 # TODO -> find a way to have an homogeneous namespace between the Helm Charts and Juju Bundles-based
3550 # KNFs (Bug 2027: https://osm.etsi.org/bugzilla/show_bug.cgi?id=2027)
3551 if k8sclustertype
in ("juju", "juju-bundle"):
3552 # First, verify if the current namespace is present in the `_admin.projects_read` (if not, it means
3553 # that the user passed a namespace which he wants its KDU to be deployed in)
3559 "_admin.projects_write": k8s_instance_info
["namespace"],
3560 "_admin.projects_read": k8s_instance_info
["namespace"],
3566 f
"Updating namespace/model for Juju Bundle from {k8s_instance_info['namespace']} to {kdu_instance}"
3571 _desc
={f
"{nsr_db_path}.namespace": kdu_instance
},
3573 k8s_instance_info
["namespace"] = kdu_instance
3575 await self
.k8scluster_map
[k8sclustertype
].install(
3576 cluster_uuid
=k8s_instance_info
["k8scluster-uuid"],
3577 kdu_model
=k8s_instance_info
["kdu-model"],
3580 db_dict
=db_dict_install
,
3582 kdu_name
=k8s_instance_info
["kdu-name"],
3583 namespace
=k8s_instance_info
["namespace"],
3584 kdu_instance
=kdu_instance
,
3588 # Obtain services to obtain management service ip
3589 services
= await self
.k8scluster_map
[k8sclustertype
].get_services(
3590 cluster_uuid
=k8s_instance_info
["k8scluster-uuid"],
3591 kdu_instance
=kdu_instance
,
3592 namespace
=k8s_instance_info
["namespace"],
3595 # Obtain management service info (if exists)
3596 vnfr_update_dict
= {}
3597 kdu_config
= get_configuration(vnfd
, kdud
["name"])
3599 target_ee_list
= kdu_config
.get("execution-environment-list", [])
3604 vnfr_update_dict
["kdur.{}.services".format(kdu_index
)] = services
3607 for service
in kdud
.get("service", [])
3608 if service
.get("mgmt-service")
3610 for mgmt_service
in mgmt_services
:
3611 for service
in services
:
3612 if service
["name"].startswith(mgmt_service
["name"]):
3613 # Mgmt service found, Obtain service ip
3614 ip
= service
.get("external_ip", service
.get("cluster_ip"))
3615 if isinstance(ip
, list) and len(ip
) == 1:
3619 "kdur.{}.ip-address".format(kdu_index
)
3622 # Check if must update also mgmt ip at the vnf
3623 service_external_cp
= mgmt_service
.get(
3624 "external-connection-point-ref"
3626 if service_external_cp
:
3628 deep_get(vnfd
, ("mgmt-interface", "cp"))
3629 == service_external_cp
3631 vnfr_update_dict
["ip-address"] = ip
3636 "external-connection-point-ref", ""
3638 == service_external_cp
,
3641 "kdur.{}.ip-address".format(kdu_index
)
3646 "Mgmt service name: {} not found".format(
3647 mgmt_service
["name"]
3651 vnfr_update_dict
["kdur.{}.status".format(kdu_index
)] = "READY"
3652 self
.update_db_2("vnfrs", vnfr_data
.get("_id"), vnfr_update_dict
)
3654 kdu_config
= get_configuration(vnfd
, k8s_instance_info
["kdu-name"])
3657 and kdu_config
.get("initial-config-primitive")
3658 and get_juju_ee_ref(vnfd
, k8s_instance_info
["kdu-name"]) is None
3660 initial_config_primitive_list
= kdu_config
.get(
3661 "initial-config-primitive"
3663 initial_config_primitive_list
.sort(key
=lambda val
: int(val
["seq"]))
3665 for initial_config_primitive
in initial_config_primitive_list
:
3666 primitive_params_
= self
._map
_primitive
_params
(
3667 initial_config_primitive
, {}, {}
3670 await asyncio
.wait_for(
3671 self
.k8scluster_map
[k8sclustertype
].exec_primitive(
3672 cluster_uuid
=k8s_instance_info
["k8scluster-uuid"],
3673 kdu_instance
=kdu_instance
,
3674 primitive_name
=initial_config_primitive
["name"],
3675 params
=primitive_params_
,
3676 db_dict
=db_dict_install
,
3682 except Exception as e
:
3683 # Prepare update db with error and raise exception
3686 "nsrs", nsr_id
, {nsr_db_path
+ ".detailed-status": str(e
)}
3690 vnfr_data
.get("_id"),
3691 {"kdur.{}.status".format(kdu_index
): "ERROR"},
3694 # ignore to keep original exception
3696 # reraise original error
3701 async def deploy_kdus(
3708 task_instantiation_info
,
3710 # Launch kdus if present in the descriptor
3712 k8scluster_id_2_uuic
= {
3713 "helm-chart-v3": {},
3718 async def _get_cluster_id(cluster_id
, cluster_type
):
3719 nonlocal k8scluster_id_2_uuic
3720 if cluster_id
in k8scluster_id_2_uuic
[cluster_type
]:
3721 return k8scluster_id_2_uuic
[cluster_type
][cluster_id
]
3723 # check if K8scluster is creating and wait look if previous tasks in process
3724 task_name
, task_dependency
= self
.lcm_tasks
.lookfor_related(
3725 "k8scluster", cluster_id
3728 text
= "Waiting for related tasks '{}' on k8scluster {} to be completed".format(
3729 task_name
, cluster_id
3731 self
.logger
.debug(logging_text
+ text
)
3732 await asyncio
.wait(task_dependency
, timeout
=3600)
3734 db_k8scluster
= self
.db
.get_one(
3735 "k8sclusters", {"_id": cluster_id
}, fail_on_empty
=False
3737 if not db_k8scluster
:
3738 raise LcmException("K8s cluster {} cannot be found".format(cluster_id
))
3740 k8s_id
= deep_get(db_k8scluster
, ("_admin", cluster_type
, "id"))
3742 if cluster_type
== "helm-chart-v3":
3744 # backward compatibility for existing clusters that have not been initialized for helm v3
3745 k8s_credentials
= yaml
.safe_dump(
3746 db_k8scluster
.get("credentials")
3748 k8s_id
, uninstall_sw
= await self
.k8sclusterhelm3
.init_env(
3749 k8s_credentials
, reuse_cluster_uuid
=cluster_id
3751 db_k8scluster_update
= {}
3752 db_k8scluster_update
["_admin.helm-chart-v3.error_msg"] = None
3753 db_k8scluster_update
["_admin.helm-chart-v3.id"] = k8s_id
3754 db_k8scluster_update
[
3755 "_admin.helm-chart-v3.created"
3757 db_k8scluster_update
[
3758 "_admin.helm-chart-v3.operationalState"
3761 "k8sclusters", cluster_id
, db_k8scluster_update
3763 except Exception as e
:
3766 + "error initializing helm-v3 cluster: {}".format(str(e
))
3769 "K8s cluster '{}' has not been initialized for '{}'".format(
3770 cluster_id
, cluster_type
3775 "K8s cluster '{}' has not been initialized for '{}'".format(
3776 cluster_id
, cluster_type
3779 k8scluster_id_2_uuic
[cluster_type
][cluster_id
] = k8s_id
3782 logging_text
+= "Deploy kdus: "
3785 db_nsr_update
= {"_admin.deployed.K8s": []}
3786 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
3789 updated_cluster_list
= []
3790 updated_v3_cluster_list
= []
3792 for vnfr_data
in db_vnfrs
.values():
3793 vca_id
= self
.get_vca_id(vnfr_data
, {})
3794 for kdu_index
, kdur
in enumerate(get_iterable(vnfr_data
, "kdur")):
3795 # Step 0: Prepare and set parameters
3796 desc_params
= parse_yaml_strings(kdur
.get("additionalParams"))
3797 vnfd_id
= vnfr_data
.get("vnfd-id")
3798 vnfd_with_id
= find_in_list(
3799 db_vnfds
, lambda vnfd
: vnfd
["_id"] == vnfd_id
3803 for kdud
in vnfd_with_id
["kdu"]
3804 if kdud
["name"] == kdur
["kdu-name"]
3806 namespace
= kdur
.get("k8s-namespace")
3807 kdu_deployment_name
= kdur
.get("kdu-deployment-name")
3808 if kdur
.get("helm-chart"):
3809 kdumodel
= kdur
["helm-chart"]
3810 # Default version: helm3, if helm-version is v2 assign v2
3811 k8sclustertype
= "helm-chart-v3"
3812 self
.logger
.debug("kdur: {}".format(kdur
))
3814 kdur
.get("helm-version")
3815 and kdur
.get("helm-version") == "v2"
3817 k8sclustertype
= "helm-chart"
3818 elif kdur
.get("juju-bundle"):
3819 kdumodel
= kdur
["juju-bundle"]
3820 k8sclustertype
= "juju-bundle"
3823 "kdu type for kdu='{}.{}' is neither helm-chart nor "
3824 "juju-bundle. Maybe an old NBI version is running".format(
3825 vnfr_data
["member-vnf-index-ref"], kdur
["kdu-name"]
3828 # check if kdumodel is a file and exists
3830 vnfd_with_id
= find_in_list(
3831 db_vnfds
, lambda vnfd
: vnfd
["_id"] == vnfd_id
3833 storage
= deep_get(vnfd_with_id
, ("_admin", "storage"))
3834 if storage
: # may be not present if vnfd has not artifacts
3835 # path format: /vnfdid/pkkdir/helm-charts|juju-bundles/kdumodel
3836 if storage
["pkg-dir"]:
3837 filename
= "{}/{}/{}s/{}".format(
3844 filename
= "{}/Scripts/{}s/{}".format(
3849 if self
.fs
.file_exists(
3850 filename
, mode
="file"
3851 ) or self
.fs
.file_exists(filename
, mode
="dir"):
3852 kdumodel
= self
.fs
.path
+ filename
3853 except (asyncio
.TimeoutError
, asyncio
.CancelledError
):
3855 except Exception: # it is not a file
3858 k8s_cluster_id
= kdur
["k8s-cluster"]["id"]
3859 step
= "Synchronize repos for k8s cluster '{}'".format(
3862 cluster_uuid
= await _get_cluster_id(k8s_cluster_id
, k8sclustertype
)
3866 k8sclustertype
== "helm-chart"
3867 and cluster_uuid
not in updated_cluster_list
3869 k8sclustertype
== "helm-chart-v3"
3870 and cluster_uuid
not in updated_v3_cluster_list
3872 del_repo_list
, added_repo_dict
= await asyncio
.ensure_future(
3873 self
.k8scluster_map
[k8sclustertype
].synchronize_repos(
3874 cluster_uuid
=cluster_uuid
3877 if del_repo_list
or added_repo_dict
:
3878 if k8sclustertype
== "helm-chart":
3880 "_admin.helm_charts_added." + item
: None
3881 for item
in del_repo_list
3884 "_admin.helm_charts_added." + item
: name
3885 for item
, name
in added_repo_dict
.items()
3887 updated_cluster_list
.append(cluster_uuid
)
3888 elif k8sclustertype
== "helm-chart-v3":
3890 "_admin.helm_charts_v3_added." + item
: None
3891 for item
in del_repo_list
3894 "_admin.helm_charts_v3_added." + item
: name
3895 for item
, name
in added_repo_dict
.items()
3897 updated_v3_cluster_list
.append(cluster_uuid
)
3899 logging_text
+ "repos synchronized on k8s cluster "
3900 "'{}' to_delete: {}, to_add: {}".format(
3901 k8s_cluster_id
, del_repo_list
, added_repo_dict
3906 {"_id": k8s_cluster_id
},
3912 step
= "Instantiating KDU {}.{} in k8s cluster {}".format(
3913 vnfr_data
["member-vnf-index-ref"],
3917 k8s_instance_info
= {
3918 "kdu-instance": None,
3919 "k8scluster-uuid": cluster_uuid
,
3920 "k8scluster-type": k8sclustertype
,
3921 "member-vnf-index": vnfr_data
["member-vnf-index-ref"],
3922 "kdu-name": kdur
["kdu-name"],
3923 "kdu-model": kdumodel
,
3924 "namespace": namespace
,
3925 "kdu-deployment-name": kdu_deployment_name
,
3927 db_path
= "_admin.deployed.K8s.{}".format(index
)
3928 db_nsr_update
[db_path
] = k8s_instance_info
3929 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
3930 vnfd_with_id
= find_in_list(
3931 db_vnfds
, lambda vnf
: vnf
["_id"] == vnfd_id
3933 task
= asyncio
.ensure_future(
3942 k8params
=desc_params
,
3947 self
.lcm_tasks
.register(
3951 "instantiate_KDU-{}".format(index
),
3954 task_instantiation_info
[task
] = "Deploying KDU {}".format(
3960 except (LcmException
, asyncio
.CancelledError
):
3962 except Exception as e
:
3963 msg
= "Exception {} while {}: {}".format(type(e
).__name
__, step
, e
)
3964 if isinstance(e
, (N2VCException
, DbException
)):
3965 self
.logger
.error(logging_text
+ msg
)
3967 self
.logger
.critical(logging_text
+ msg
, exc_info
=True)
3968 raise LcmException(msg
)
3971 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
3991 task_instantiation_info
,
3994 # launch instantiate_N2VC in a asyncio task and register task object
3995 # Look where information of this charm is at database <nsrs>._admin.deployed.VCA
3996 # if not found, create one entry and update database
3997 # fill db_nsr._admin.deployed.VCA.<index>
4000 logging_text
+ "_deploy_n2vc vnfd_id={}, vdu_id={}".format(vnfd_id
, vdu_id
)
4004 get_charm_name
= False
4005 if "execution-environment-list" in descriptor_config
:
4006 ee_list
= descriptor_config
.get("execution-environment-list", [])
4007 elif "juju" in descriptor_config
:
4008 ee_list
= [descriptor_config
] # ns charms
4009 if "execution-environment-list" not in descriptor_config
:
4010 # charm name is only required for ns charms
4011 get_charm_name
= True
4012 else: # other types as script are not supported
4015 for ee_item
in ee_list
:
4018 + "_deploy_n2vc ee_item juju={}, helm={}".format(
4019 ee_item
.get("juju"), ee_item
.get("helm-chart")
4022 ee_descriptor_id
= ee_item
.get("id")
4023 if ee_item
.get("juju"):
4024 vca_name
= ee_item
["juju"].get("charm")
4026 charm_name
= self
.find_charm_name(db_nsr
, str(vca_name
))
4029 if ee_item
["juju"].get("charm") is not None
4032 if ee_item
["juju"].get("cloud") == "k8s":
4033 vca_type
= "k8s_proxy_charm"
4034 elif ee_item
["juju"].get("proxy") is False:
4035 vca_type
= "native_charm"
4036 elif ee_item
.get("helm-chart"):
4037 vca_name
= ee_item
["helm-chart"]
4038 if ee_item
.get("helm-version") and ee_item
.get("helm-version") == "v2":
4041 vca_type
= "helm-v3"
4044 logging_text
+ "skipping non juju neither charm configuration"
4049 for vca_index
, vca_deployed
in enumerate(
4050 db_nsr
["_admin"]["deployed"]["VCA"]
4052 if not vca_deployed
:
4055 vca_deployed
.get("member-vnf-index") == member_vnf_index
4056 and vca_deployed
.get("vdu_id") == vdu_id
4057 and vca_deployed
.get("kdu_name") == kdu_name
4058 and vca_deployed
.get("vdu_count_index", 0) == vdu_index
4059 and vca_deployed
.get("ee_descriptor_id") == ee_descriptor_id
4063 # not found, create one.
4065 "ns" if not member_vnf_index
else "vnf/{}".format(member_vnf_index
)
4068 target
+= "/vdu/{}/{}".format(vdu_id
, vdu_index
or 0)
4070 target
+= "/kdu/{}".format(kdu_name
)
4072 "target_element": target
,
4073 # ^ target_element will replace member-vnf-index, kdu_name, vdu_id ... in a single string
4074 "member-vnf-index": member_vnf_index
,
4076 "kdu_name": kdu_name
,
4077 "vdu_count_index": vdu_index
,
4078 "operational-status": "init", # TODO revise
4079 "detailed-status": "", # TODO revise
4080 "step": "initial-deploy", # TODO revise
4082 "vdu_name": vdu_name
,
4084 "ee_descriptor_id": ee_descriptor_id
,
4085 "charm_name": charm_name
,
4089 # create VCA and configurationStatus in db
4091 "_admin.deployed.VCA.{}".format(vca_index
): vca_deployed
,
4092 "configurationStatus.{}".format(vca_index
): dict(),
4094 self
.update_db_2("nsrs", nsr_id
, db_dict
)
4096 db_nsr
["_admin"]["deployed"]["VCA"].append(vca_deployed
)
4098 self
.logger
.debug("N2VC > NSR_ID > {}".format(nsr_id
))
4099 self
.logger
.debug("N2VC > DB_NSR > {}".format(db_nsr
))
4100 self
.logger
.debug("N2VC > VCA_DEPLOYED > {}".format(vca_deployed
))
4103 task_n2vc
= asyncio
.ensure_future(
4104 self
.instantiate_N2VC(
4105 logging_text
=logging_text
,
4106 vca_index
=vca_index
,
4112 vdu_index
=vdu_index
,
4113 kdu_index
=kdu_index
,
4114 deploy_params
=deploy_params
,
4115 config_descriptor
=descriptor_config
,
4116 base_folder
=base_folder
,
4117 nslcmop_id
=nslcmop_id
,
4121 ee_config_descriptor
=ee_item
,
4124 self
.lcm_tasks
.register(
4128 "instantiate_N2VC-{}".format(vca_index
),
4131 task_instantiation_info
[
4133 ] = self
.task_name_deploy_vca
+ " {}.{}".format(
4134 member_vnf_index
or "", vdu_id
or ""
4138 def _create_nslcmop(nsr_id
, operation
, params
):
4140 Creates a ns-lcm-opp content to be stored at database.
4141 :param nsr_id: internal id of the instance
4142 :param operation: instantiate, terminate, scale, action, ...
4143 :param params: user parameters for the operation
4144 :return: dictionary following SOL005 format
4146 # Raise exception if invalid arguments
4147 if not (nsr_id
and operation
and params
):
4149 "Parameters 'nsr_id', 'operation' and 'params' needed to create primitive not provided"
4156 # COMPLETED,PARTIALLY_COMPLETED,FAILED_TEMP,FAILED,ROLLING_BACK,ROLLED_BACK
4157 "operationState": "PROCESSING",
4158 "statusEnteredTime": now
,
4159 "nsInstanceId": nsr_id
,
4160 "lcmOperationType": operation
,
4162 "isAutomaticInvocation": False,
4163 "operationParams": params
,
4164 "isCancelPending": False,
4166 "self": "/osm/nslcm/v1/ns_lcm_op_occs/" + _id
,
4167 "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsr_id
,
4172 def _format_additional_params(self
, params
):
4173 params
= params
or {}
4174 for key
, value
in params
.items():
4175 if str(value
).startswith("!!yaml "):
4176 params
[key
] = yaml
.safe_load(value
[7:])
4179 def _get_terminate_primitive_params(self
, seq
, vnf_index
):
4180 primitive
= seq
.get("name")
4181 primitive_params
= {}
4183 "member_vnf_index": vnf_index
,
4184 "primitive": primitive
,
4185 "primitive_params": primitive_params
,
4188 return self
._map
_primitive
_params
(seq
, params
, desc_params
)
4192 def _retry_or_skip_suboperation(self
, db_nslcmop
, op_index
):
4193 op
= deep_get(db_nslcmop
, ("_admin", "operations"), [])[op_index
]
4194 if op
.get("operationState") == "COMPLETED":
4195 # b. Skip sub-operation
4196 # _ns_execute_primitive() or RO.create_action() will NOT be executed
4197 return self
.SUBOPERATION_STATUS_SKIP
4199 # c. retry executing sub-operation
4200 # The sub-operation exists, and operationState != 'COMPLETED'
4201 # Update operationState = 'PROCESSING' to indicate a retry.
4202 operationState
= "PROCESSING"
4203 detailed_status
= "In progress"
4204 self
._update
_suboperation
_status
(
4205 db_nslcmop
, op_index
, operationState
, detailed_status
4207 # Return the sub-operation index
4208 # _ns_execute_primitive() or RO.create_action() will be called from scale()
4209 # with arguments extracted from the sub-operation
4212 # Find a sub-operation where all keys in a matching dictionary must match
4213 # Returns the index of the matching sub-operation, or SUBOPERATION_STATUS_NOT_FOUND if no match
4214 def _find_suboperation(self
, db_nslcmop
, match
):
4215 if db_nslcmop
and match
:
4216 op_list
= db_nslcmop
.get("_admin", {}).get("operations", [])
4217 for i
, op
in enumerate(op_list
):
4218 if all(op
.get(k
) == match
[k
] for k
in match
):
4220 return self
.SUBOPERATION_STATUS_NOT_FOUND
4222 # Update status for a sub-operation given its index
4223 def _update_suboperation_status(
4224 self
, db_nslcmop
, op_index
, operationState
, detailed_status
4226 # Update DB for HA tasks
4227 q_filter
= {"_id": db_nslcmop
["_id"]}
4229 "_admin.operations.{}.operationState".format(op_index
): operationState
,
4230 "_admin.operations.{}.detailed-status".format(op_index
): detailed_status
,
4233 "nslcmops", q_filter
=q_filter
, update_dict
=update_dict
, fail_on_empty
=False
4236 # Add sub-operation, return the index of the added sub-operation
4237 # Optionally, set operationState, detailed-status, and operationType
4238 # Status and type are currently set for 'scale' sub-operations:
4239 # 'operationState' : 'PROCESSING' | 'COMPLETED' | 'FAILED'
4240 # 'detailed-status' : status message
4241 # 'operationType': may be any type, in the case of scaling: 'PRE-SCALE' | 'POST-SCALE'
4242 # Status and operation type are currently only used for 'scale', but NOT for 'terminate' sub-operations.
4243 def _add_suboperation(
4251 mapped_primitive_params
,
4252 operationState
=None,
4253 detailed_status
=None,
4256 RO_scaling_info
=None,
4259 return self
.SUBOPERATION_STATUS_NOT_FOUND
4260 # Get the "_admin.operations" list, if it exists
4261 db_nslcmop_admin
= db_nslcmop
.get("_admin", {})
4262 op_list
= db_nslcmop_admin
.get("operations")
4263 # Create or append to the "_admin.operations" list
4265 "member_vnf_index": vnf_index
,
4267 "vdu_count_index": vdu_count_index
,
4268 "primitive": primitive
,
4269 "primitive_params": mapped_primitive_params
,
4272 new_op
["operationState"] = operationState
4274 new_op
["detailed-status"] = detailed_status
4276 new_op
["lcmOperationType"] = operationType
4278 new_op
["RO_nsr_id"] = RO_nsr_id
4280 new_op
["RO_scaling_info"] = RO_scaling_info
4282 # No existing operations, create key 'operations' with current operation as first list element
4283 db_nslcmop_admin
.update({"operations": [new_op
]})
4284 op_list
= db_nslcmop_admin
.get("operations")
4286 # Existing operations, append operation to list
4287 op_list
.append(new_op
)
4289 db_nslcmop_update
= {"_admin.operations": op_list
}
4290 self
.update_db_2("nslcmops", db_nslcmop
["_id"], db_nslcmop_update
)
4291 op_index
= len(op_list
) - 1
4294 # Helper methods for scale() sub-operations
4296 # pre-scale/post-scale:
4297 # Check for 3 different cases:
4298 # a. New: First time execution, return SUBOPERATION_STATUS_NEW
4299 # b. Skip: Existing sub-operation exists, operationState == 'COMPLETED', return SUBOPERATION_STATUS_SKIP
4300 # c. retry: Existing sub-operation exists, operationState != 'COMPLETED', return op_index to re-execute
4301 def _check_or_add_scale_suboperation(
4305 vnf_config_primitive
,
4309 RO_scaling_info
=None,
4311 # Find this sub-operation
4312 if RO_nsr_id
and RO_scaling_info
:
4313 operationType
= "SCALE-RO"
4315 "member_vnf_index": vnf_index
,
4316 "RO_nsr_id": RO_nsr_id
,
4317 "RO_scaling_info": RO_scaling_info
,
4321 "member_vnf_index": vnf_index
,
4322 "primitive": vnf_config_primitive
,
4323 "primitive_params": primitive_params
,
4324 "lcmOperationType": operationType
,
4326 op_index
= self
._find
_suboperation
(db_nslcmop
, match
)
4327 if op_index
== self
.SUBOPERATION_STATUS_NOT_FOUND
:
4328 # a. New sub-operation
4329 # The sub-operation does not exist, add it.
4330 # _ns_execute_primitive() will be called from scale() as usual, with non-modified arguments
4331 # The following parameters are set to None for all kind of scaling:
4333 vdu_count_index
= None
4335 if RO_nsr_id
and RO_scaling_info
:
4336 vnf_config_primitive
= None
4337 primitive_params
= None
4340 RO_scaling_info
= None
4341 # Initial status for sub-operation
4342 operationState
= "PROCESSING"
4343 detailed_status
= "In progress"
4344 # Add sub-operation for pre/post-scaling (zero or more operations)
4345 self
._add
_suboperation
(
4351 vnf_config_primitive
,
4359 return self
.SUBOPERATION_STATUS_NEW
4361 # Return either SUBOPERATION_STATUS_SKIP (operationState == 'COMPLETED'),
4362 # or op_index (operationState != 'COMPLETED')
4363 return self
._retry
_or
_skip
_suboperation
(db_nslcmop
, op_index
)
4365 # Function to return execution_environment id
4367 def _get_ee_id(self
, vnf_index
, vdu_id
, vca_deployed_list
):
4368 # TODO vdu_index_count
4369 for vca
in vca_deployed_list
:
4370 if vca
["member-vnf-index"] == vnf_index
and vca
["vdu_id"] == vdu_id
:
4371 return vca
.get("ee_id")
4373 async def destroy_N2VC(
4381 exec_primitives
=True,
4386 Execute the terminate primitives and destroy the execution environment (if destroy_ee=False
4387 :param logging_text:
4389 :param vca_deployed: Dictionary of deployment info at db_nsr._admin.depoloyed.VCA.<INDEX>
4390 :param config_descriptor: Configuration descriptor of the NSD, VNFD, VNFD.vdu or VNFD.kdu
4391 :param vca_index: index in the database _admin.deployed.VCA
4392 :param destroy_ee: False to do not destroy, because it will be destroyed all of then at once
4393 :param exec_primitives: False to do not execute terminate primitives, because the config is not completed or has
4394 not executed properly
4395 :param scaling_in: True destroys the application, False destroys the model
4396 :return: None or exception
4401 + " vca_index: {}, vca_deployed: {}, config_descriptor: {}, destroy_ee: {}".format(
4402 vca_index
, vca_deployed
, config_descriptor
, destroy_ee
4406 vca_type
= vca_deployed
.get("type", "lxc_proxy_charm")
4408 # execute terminate_primitives
4410 terminate_primitives
= get_ee_sorted_terminate_config_primitive_list(
4411 config_descriptor
.get("terminate-config-primitive"),
4412 vca_deployed
.get("ee_descriptor_id"),
4414 vdu_id
= vca_deployed
.get("vdu_id")
4415 vdu_count_index
= vca_deployed
.get("vdu_count_index")
4416 vdu_name
= vca_deployed
.get("vdu_name")
4417 vnf_index
= vca_deployed
.get("member-vnf-index")
4418 if terminate_primitives
and vca_deployed
.get("needed_terminate"):
4419 for seq
in terminate_primitives
:
4420 # For each sequence in list, get primitive and call _ns_execute_primitive()
4421 step
= "Calling terminate action for vnf_member_index={} primitive={}".format(
4422 vnf_index
, seq
.get("name")
4424 self
.logger
.debug(logging_text
+ step
)
4425 # Create the primitive for each sequence, i.e. "primitive": "touch"
4426 primitive
= seq
.get("name")
4427 mapped_primitive_params
= self
._get
_terminate
_primitive
_params
(
4432 self
._add
_suboperation
(
4439 mapped_primitive_params
,
4441 # Sub-operations: Call _ns_execute_primitive() instead of action()
4443 result
, result_detail
= await self
._ns
_execute
_primitive
(
4444 vca_deployed
["ee_id"],
4446 mapped_primitive_params
,
4450 except LcmException
:
4451 # this happens when VCA is not deployed. In this case it is not needed to terminate
4453 result_ok
= ["COMPLETED", "PARTIALLY_COMPLETED"]
4454 if result
not in result_ok
:
4456 "terminate_primitive {} for vnf_member_index={} fails with "
4457 "error {}".format(seq
.get("name"), vnf_index
, result_detail
)
4459 # set that this VCA do not need terminated
4460 db_update_entry
= "_admin.deployed.VCA.{}.needed_terminate".format(
4464 "nsrs", db_nslcmop
["nsInstanceId"], {db_update_entry
: False}
4467 # Delete Prometheus Jobs if any
4468 # This uses NSR_ID, so it will destroy any jobs under this index
4469 self
.db
.del_list("prometheus_jobs", {"nsr_id": db_nslcmop
["nsInstanceId"]})
4472 await self
.vca_map
[vca_type
].delete_execution_environment(
4473 vca_deployed
["ee_id"],
4474 scaling_in
=scaling_in
,
4479 async def _delete_all_N2VC(self
, db_nsr
: dict, vca_id
: str = None):
4480 self
._write
_all
_config
_status
(db_nsr
=db_nsr
, status
="TERMINATING")
4481 namespace
= "." + db_nsr
["_id"]
4483 await self
.n2vc
.delete_namespace(
4484 namespace
=namespace
,
4485 total_timeout
=self
.timeout
.charm_delete
,
4488 except N2VCNotFound
: # already deleted. Skip
4490 self
._write
_all
_config
_status
(db_nsr
=db_nsr
, status
="DELETED")
4492 async def terminate(self
, nsr_id
, nslcmop_id
):
4493 # Try to lock HA task here
4494 task_is_locked_by_me
= self
.lcm_tasks
.lock_HA("ns", "nslcmops", nslcmop_id
)
4495 if not task_is_locked_by_me
:
4498 logging_text
= "Task ns={} terminate={} ".format(nsr_id
, nslcmop_id
)
4499 self
.logger
.debug(logging_text
+ "Enter")
4500 timeout_ns_terminate
= self
.timeout
.ns_terminate
4503 operation_params
= None
4505 error_list
= [] # annotates all failed error messages
4506 db_nslcmop_update
= {}
4507 autoremove
= False # autoremove after terminated
4508 tasks_dict_info
= {}
4511 "Stage 1/3: Preparing task.",
4512 "Waiting for previous operations to terminate.",
4515 # ^ contains [stage, step, VIM-status]
4517 # wait for any previous tasks in process
4518 await self
.lcm_tasks
.waitfor_related_HA("ns", "nslcmops", nslcmop_id
)
4520 stage
[1] = "Getting nslcmop={} from db.".format(nslcmop_id
)
4521 db_nslcmop
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
})
4522 operation_params
= db_nslcmop
.get("operationParams") or {}
4523 if operation_params
.get("timeout_ns_terminate"):
4524 timeout_ns_terminate
= operation_params
["timeout_ns_terminate"]
4525 stage
[1] = "Getting nsr={} from db.".format(nsr_id
)
4526 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
4528 db_nsr_update
["operational-status"] = "terminating"
4529 db_nsr_update
["config-status"] = "terminating"
4530 self
._write
_ns
_status
(
4532 ns_state
="TERMINATING",
4533 current_operation
="TERMINATING",
4534 current_operation_id
=nslcmop_id
,
4535 other_update
=db_nsr_update
,
4537 self
._write
_op
_status
(op_id
=nslcmop_id
, queuePosition
=0, stage
=stage
)
4538 nsr_deployed
= deepcopy(db_nsr
["_admin"].get("deployed")) or {}
4539 if db_nsr
["_admin"]["nsState"] == "NOT_INSTANTIATED":
4542 stage
[1] = "Getting vnf descriptors from db."
4543 db_vnfrs_list
= self
.db
.get_list("vnfrs", {"nsr-id-ref": nsr_id
})
4545 db_vnfr
["member-vnf-index-ref"]: db_vnfr
for db_vnfr
in db_vnfrs_list
4547 db_vnfds_from_id
= {}
4548 db_vnfds_from_member_index
= {}
4550 for vnfr
in db_vnfrs_list
:
4551 vnfd_id
= vnfr
["vnfd-id"]
4552 if vnfd_id
not in db_vnfds_from_id
:
4553 vnfd
= self
.db
.get_one("vnfds", {"_id": vnfd_id
})
4554 db_vnfds_from_id
[vnfd_id
] = vnfd
4555 db_vnfds_from_member_index
[
4556 vnfr
["member-vnf-index-ref"]
4557 ] = db_vnfds_from_id
[vnfd_id
]
4559 # Destroy individual execution environments when there are terminating primitives.
4560 # Rest of EE will be deleted at once
4561 # TODO - check before calling _destroy_N2VC
4562 # if not operation_params.get("skip_terminate_primitives"):#
4563 # or not vca.get("needed_terminate"):
4564 stage
[0] = "Stage 2/3 execute terminating primitives."
4565 self
.logger
.debug(logging_text
+ stage
[0])
4566 stage
[1] = "Looking execution environment that needs terminate."
4567 self
.logger
.debug(logging_text
+ stage
[1])
4569 for vca_index
, vca
in enumerate(get_iterable(nsr_deployed
, "VCA")):
4570 config_descriptor
= None
4571 vca_member_vnf_index
= vca
.get("member-vnf-index")
4572 vca_id
= self
.get_vca_id(
4573 db_vnfrs_dict
.get(vca_member_vnf_index
)
4574 if vca_member_vnf_index
4578 if not vca
or not vca
.get("ee_id"):
4580 if not vca
.get("member-vnf-index"):
4582 config_descriptor
= db_nsr
.get("ns-configuration")
4583 elif vca
.get("vdu_id"):
4584 db_vnfd
= db_vnfds_from_member_index
[vca
["member-vnf-index"]]
4585 config_descriptor
= get_configuration(db_vnfd
, vca
.get("vdu_id"))
4586 elif vca
.get("kdu_name"):
4587 db_vnfd
= db_vnfds_from_member_index
[vca
["member-vnf-index"]]
4588 config_descriptor
= get_configuration(db_vnfd
, vca
.get("kdu_name"))
4590 db_vnfd
= db_vnfds_from_member_index
[vca
["member-vnf-index"]]
4591 config_descriptor
= get_configuration(db_vnfd
, db_vnfd
["id"])
4592 vca_type
= vca
.get("type")
4593 exec_terminate_primitives
= not operation_params
.get(
4594 "skip_terminate_primitives"
4595 ) and vca
.get("needed_terminate")
4596 # For helm we must destroy_ee. Also for native_charm, as juju_model cannot be deleted if there are
4597 # pending native charms
4599 True if vca_type
in ("helm", "helm-v3", "native_charm") else False
4601 # self.logger.debug(logging_text + "vca_index: {}, ee_id: {}, vca_type: {} destroy_ee: {}".format(
4602 # vca_index, vca.get("ee_id"), vca_type, destroy_ee))
4603 task
= asyncio
.ensure_future(
4611 exec_terminate_primitives
,
4615 tasks_dict_info
[task
] = "Terminating VCA {}".format(vca
.get("ee_id"))
4617 # wait for pending tasks of terminate primitives
4621 + "Waiting for tasks {}".format(list(tasks_dict_info
.keys()))
4623 error_list
= await self
._wait
_for
_tasks
(
4626 min(self
.timeout
.charm_delete
, timeout_ns_terminate
),
4630 tasks_dict_info
.clear()
4632 return # raise LcmException("; ".join(error_list))
4634 # remove All execution environments at once
4635 stage
[0] = "Stage 3/3 delete all."
4637 if nsr_deployed
.get("VCA"):
4638 stage
[1] = "Deleting all execution environments."
4639 self
.logger
.debug(logging_text
+ stage
[1])
4640 vca_id
= self
.get_vca_id({}, db_nsr
)
4641 task_delete_ee
= asyncio
.ensure_future(
4643 self
._delete
_all
_N
2VC
(db_nsr
=db_nsr
, vca_id
=vca_id
),
4644 timeout
=self
.timeout
.charm_delete
,
4647 # task_delete_ee = asyncio.ensure_future(self.n2vc.delete_namespace(namespace="." + nsr_id))
4648 tasks_dict_info
[task_delete_ee
] = "Terminating all VCA"
4650 # Delete Namespace and Certificates if necessary
4651 if check_helm_ee_in_ns(list(db_vnfds_from_member_index
.values())):
4652 await self
.vca_map
["helm-v3"].delete_tls_certificate(
4653 namespace
=db_nslcmop
["nsInstanceId"],
4654 certificate_name
=self
.EE_TLS_NAME
,
4656 await self
.vca_map
["helm-v3"].delete_namespace(
4657 namespace
=db_nslcmop
["nsInstanceId"],
4660 # Delete from k8scluster
4661 stage
[1] = "Deleting KDUs."
4662 self
.logger
.debug(logging_text
+ stage
[1])
4663 # print(nsr_deployed)
4664 for kdu
in get_iterable(nsr_deployed
, "K8s"):
4665 if not kdu
or not kdu
.get("kdu-instance"):
4667 kdu_instance
= kdu
.get("kdu-instance")
4668 if kdu
.get("k8scluster-type") in self
.k8scluster_map
:
4669 # TODO: Uninstall kdu instances taking into account they could be deployed in different VIMs
4670 vca_id
= self
.get_vca_id({}, db_nsr
)
4671 task_delete_kdu_instance
= asyncio
.ensure_future(
4672 self
.k8scluster_map
[kdu
["k8scluster-type"]].uninstall(
4673 cluster_uuid
=kdu
.get("k8scluster-uuid"),
4674 kdu_instance
=kdu_instance
,
4676 namespace
=kdu
.get("namespace"),
4682 + "Unknown k8s deployment type {}".format(
4683 kdu
.get("k8scluster-type")
4688 task_delete_kdu_instance
4689 ] = "Terminating KDU '{}'".format(kdu
.get("kdu-name"))
4692 stage
[1] = "Deleting ns from VIM."
4693 if self
.ro_config
.ng
:
4694 task_delete_ro
= asyncio
.ensure_future(
4695 self
._terminate
_ng
_ro
(
4696 logging_text
, nsr_deployed
, nsr_id
, nslcmop_id
, stage
4699 tasks_dict_info
[task_delete_ro
] = "Removing deployment from VIM"
4701 # rest of staff will be done at finally
4704 ROclient
.ROClientException
,
4709 self
.logger
.error(logging_text
+ "Exit Exception {}".format(e
))
4711 except asyncio
.CancelledError
:
4713 logging_text
+ "Cancelled Exception while '{}'".format(stage
[1])
4715 exc
= "Operation was cancelled"
4716 except Exception as e
:
4717 exc
= traceback
.format_exc()
4718 self
.logger
.critical(
4719 logging_text
+ "Exit Exception while '{}': {}".format(stage
[1], e
),
4724 error_list
.append(str(exc
))
4726 # wait for pending tasks
4728 stage
[1] = "Waiting for terminate pending tasks."
4729 self
.logger
.debug(logging_text
+ stage
[1])
4730 error_list
+= await self
._wait
_for
_tasks
(
4733 timeout_ns_terminate
,
4737 stage
[1] = stage
[2] = ""
4738 except asyncio
.CancelledError
:
4739 error_list
.append("Cancelled")
4740 # TODO cancell all tasks
4741 except Exception as exc
:
4742 error_list
.append(str(exc
))
4743 # update status at database
4745 error_detail
= "; ".join(error_list
)
4746 # self.logger.error(logging_text + error_detail)
4747 error_description_nslcmop
= "{} Detail: {}".format(
4748 stage
[0], error_detail
4750 error_description_nsr
= "Operation: TERMINATING.{}, {}.".format(
4751 nslcmop_id
, stage
[0]
4754 db_nsr_update
["operational-status"] = "failed"
4755 db_nsr_update
["detailed-status"] = (
4756 error_description_nsr
+ " Detail: " + error_detail
4758 db_nslcmop_update
["detailed-status"] = error_detail
4759 nslcmop_operation_state
= "FAILED"
4763 error_description_nsr
= error_description_nslcmop
= None
4764 ns_state
= "NOT_INSTANTIATED"
4765 db_nsr_update
["operational-status"] = "terminated"
4766 db_nsr_update
["detailed-status"] = "Done"
4767 db_nsr_update
["_admin.nsState"] = "NOT_INSTANTIATED"
4768 db_nslcmop_update
["detailed-status"] = "Done"
4769 nslcmop_operation_state
= "COMPLETED"
4772 self
._write
_ns
_status
(
4775 current_operation
="IDLE",
4776 current_operation_id
=None,
4777 error_description
=error_description_nsr
,
4778 error_detail
=error_detail
,
4779 other_update
=db_nsr_update
,
4781 self
._write
_op
_status
(
4784 error_message
=error_description_nslcmop
,
4785 operation_state
=nslcmop_operation_state
,
4786 other_update
=db_nslcmop_update
,
4788 if ns_state
== "NOT_INSTANTIATED":
4792 {"nsr-id-ref": nsr_id
},
4793 {"_admin.nsState": "NOT_INSTANTIATED"},
4795 except DbException
as e
:
4798 + "Error writing VNFR status for nsr-id-ref: {} -> {}".format(
4802 if operation_params
:
4803 autoremove
= operation_params
.get("autoremove", False)
4804 if nslcmop_operation_state
:
4806 await self
.msg
.aiowrite(
4811 "nslcmop_id": nslcmop_id
,
4812 "operationState": nslcmop_operation_state
,
4813 "autoremove": autoremove
,
4816 except Exception as e
:
4818 logging_text
+ "kafka_write notification Exception {}".format(e
)
4820 self
.logger
.debug(f
"Deleting alerts: ns_id={nsr_id}")
4821 self
.db
.del_list("alerts", {"tags.ns_id": nsr_id
})
4823 self
.logger
.debug(logging_text
+ "Exit")
4824 self
.lcm_tasks
.remove("ns", nsr_id
, nslcmop_id
, "ns_terminate")
4826 async def _wait_for_tasks(
4827 self
, logging_text
, created_tasks_info
, timeout
, stage
, nslcmop_id
, nsr_id
=None
4830 error_detail_list
= []
4832 pending_tasks
= list(created_tasks_info
.keys())
4833 num_tasks
= len(pending_tasks
)
4835 stage
[1] = "{}/{}.".format(num_done
, num_tasks
)
4836 self
._write
_op
_status
(nslcmop_id
, stage
)
4837 while pending_tasks
:
4839 _timeout
= timeout
+ time_start
- time()
4840 done
, pending_tasks
= await asyncio
.wait(
4841 pending_tasks
, timeout
=_timeout
, return_when
=asyncio
.FIRST_COMPLETED
4843 num_done
+= len(done
)
4844 if not done
: # Timeout
4845 for task
in pending_tasks
:
4846 new_error
= created_tasks_info
[task
] + ": Timeout"
4847 error_detail_list
.append(new_error
)
4848 error_list
.append(new_error
)
4851 if task
.cancelled():
4854 exc
= task
.exception()
4856 if isinstance(exc
, asyncio
.TimeoutError
):
4858 new_error
= created_tasks_info
[task
] + ": {}".format(exc
)
4859 error_list
.append(created_tasks_info
[task
])
4860 error_detail_list
.append(new_error
)
4867 ROclient
.ROClientException
,
4873 self
.logger
.error(logging_text
+ new_error
)
4875 exc_traceback
= "".join(
4876 traceback
.format_exception(None, exc
, exc
.__traceback
__)
4880 + created_tasks_info
[task
]
4886 logging_text
+ created_tasks_info
[task
] + ": Done"
4888 stage
[1] = "{}/{}.".format(num_done
, num_tasks
)
4890 stage
[1] += " Errors: " + ". ".join(error_detail_list
) + "."
4891 if nsr_id
: # update also nsr
4896 "errorDescription": "Error at: " + ", ".join(error_list
),
4897 "errorDetail": ". ".join(error_detail_list
),
4900 self
._write
_op
_status
(nslcmop_id
, stage
)
4901 return error_detail_list
4904 def _map_primitive_params(primitive_desc
, params
, instantiation_params
):
4906 Generates the params to be provided to charm before executing primitive. If user does not provide a parameter,
4907 The default-value is used. If it is between < > it look for a value at instantiation_params
4908 :param primitive_desc: portion of VNFD/NSD that describes primitive
4909 :param params: Params provided by user
4910 :param instantiation_params: Instantiation params provided by user
4911 :return: a dictionary with the calculated params
4913 calculated_params
= {}
4914 for parameter
in primitive_desc
.get("parameter", ()):
4915 param_name
= parameter
["name"]
4916 if param_name
in params
:
4917 calculated_params
[param_name
] = params
[param_name
]
4918 elif "default-value" in parameter
or "value" in parameter
:
4919 if "value" in parameter
:
4920 calculated_params
[param_name
] = parameter
["value"]
4922 calculated_params
[param_name
] = parameter
["default-value"]
4924 isinstance(calculated_params
[param_name
], str)
4925 and calculated_params
[param_name
].startswith("<")
4926 and calculated_params
[param_name
].endswith(">")
4928 if calculated_params
[param_name
][1:-1] in instantiation_params
:
4929 calculated_params
[param_name
] = instantiation_params
[
4930 calculated_params
[param_name
][1:-1]
4934 "Parameter {} needed to execute primitive {} not provided".format(
4935 calculated_params
[param_name
], primitive_desc
["name"]
4940 "Parameter {} needed to execute primitive {} not provided".format(
4941 param_name
, primitive_desc
["name"]
4945 if isinstance(calculated_params
[param_name
], (dict, list, tuple)):
4946 calculated_params
[param_name
] = yaml
.safe_dump(
4947 calculated_params
[param_name
], default_flow_style
=True, width
=256
4949 elif isinstance(calculated_params
[param_name
], str) and calculated_params
[
4951 ].startswith("!!yaml "):
4952 calculated_params
[param_name
] = calculated_params
[param_name
][7:]
4953 if parameter
.get("data-type") == "INTEGER":
4955 calculated_params
[param_name
] = int(calculated_params
[param_name
])
4956 except ValueError: # error converting string to int
4958 "Parameter {} of primitive {} must be integer".format(
4959 param_name
, primitive_desc
["name"]
4962 elif parameter
.get("data-type") == "BOOLEAN":
4963 calculated_params
[param_name
] = not (
4964 (str(calculated_params
[param_name
])).lower() == "false"
4967 # add always ns_config_info if primitive name is config
4968 if primitive_desc
["name"] == "config":
4969 if "ns_config_info" in instantiation_params
:
4970 calculated_params
["ns_config_info"] = instantiation_params
[
4973 return calculated_params
4975 def _look_for_deployed_vca(
4982 ee_descriptor_id
=None,
4984 # find vca_deployed record for this action. Raise LcmException if not found or there is not any id.
4985 for vca
in deployed_vca
:
4988 if member_vnf_index
!= vca
["member-vnf-index"] or vdu_id
!= vca
["vdu_id"]:
4991 vdu_count_index
is not None
4992 and vdu_count_index
!= vca
["vdu_count_index"]
4995 if kdu_name
and kdu_name
!= vca
["kdu_name"]:
4997 if ee_descriptor_id
and ee_descriptor_id
!= vca
["ee_descriptor_id"]:
5001 # vca_deployed not found
5003 "charm for member_vnf_index={} vdu_id={}.{} kdu_name={} execution-environment-list.id={}"
5004 " is not deployed".format(
5013 ee_id
= vca
.get("ee_id")
5015 "type", "lxc_proxy_charm"
5016 ) # default value for backward compatibility - proxy charm
5019 "charm for member_vnf_index={} vdu_id={} kdu_name={} vdu_count_index={} has not "
5020 "execution environment".format(
5021 member_vnf_index
, vdu_id
, kdu_name
, vdu_count_index
5024 return ee_id
, vca_type
5026 async def _ns_execute_primitive(
5032 retries_interval
=30,
5039 if primitive
== "config":
5040 primitive_params
= {"params": primitive_params
}
5042 vca_type
= vca_type
or "lxc_proxy_charm"
5046 output
= await asyncio
.wait_for(
5047 self
.vca_map
[vca_type
].exec_primitive(
5049 primitive_name
=primitive
,
5050 params_dict
=primitive_params
,
5051 progress_timeout
=self
.timeout
.progress_primitive
,
5052 total_timeout
=self
.timeout
.primitive
,
5057 timeout
=timeout
or self
.timeout
.primitive
,
5061 except asyncio
.CancelledError
:
5063 except Exception as e
:
5067 "Error executing action {} on {} -> {}".format(
5072 await asyncio
.sleep(retries_interval
)
5074 if isinstance(e
, asyncio
.TimeoutError
):
5076 message
="Timed out waiting for action to complete"
5078 return "FAILED", getattr(e
, "message", repr(e
))
5080 return "COMPLETED", output
5082 except (LcmException
, asyncio
.CancelledError
):
5084 except Exception as e
:
5085 return "FAIL", "Error executing action {}: {}".format(primitive
, e
)
5087 async def vca_status_refresh(self
, nsr_id
, nslcmop_id
):
5089 Updating the vca_status with latest juju information in nsrs record
5090 :param: nsr_id: Id of the nsr
5091 :param: nslcmop_id: Id of the nslcmop
5095 self
.logger
.debug("Task ns={} action={} Enter".format(nsr_id
, nslcmop_id
))
5096 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
5097 vca_id
= self
.get_vca_id({}, db_nsr
)
5098 if db_nsr
["_admin"]["deployed"]["K8s"]:
5099 for _
, k8s
in enumerate(db_nsr
["_admin"]["deployed"]["K8s"]):
5100 cluster_uuid
, kdu_instance
, cluster_type
= (
5101 k8s
["k8scluster-uuid"],
5102 k8s
["kdu-instance"],
5103 k8s
["k8scluster-type"],
5105 await self
._on
_update
_k
8s
_db
(
5106 cluster_uuid
=cluster_uuid
,
5107 kdu_instance
=kdu_instance
,
5108 filter={"_id": nsr_id
},
5110 cluster_type
=cluster_type
,
5113 for vca_index
, _
in enumerate(db_nsr
["_admin"]["deployed"]["VCA"]):
5114 table
, filter = "nsrs", {"_id": nsr_id
}
5115 path
= "_admin.deployed.VCA.{}.".format(vca_index
)
5116 await self
._on
_update
_n
2vc
_db
(table
, filter, path
, {})
5118 self
.logger
.debug("Task ns={} action={} Exit".format(nsr_id
, nslcmop_id
))
5119 self
.lcm_tasks
.remove("ns", nsr_id
, nslcmop_id
, "ns_vca_status_refresh")
5121 async def action(self
, nsr_id
, nslcmop_id
):
5122 # Try to lock HA task here
5123 task_is_locked_by_me
= self
.lcm_tasks
.lock_HA("ns", "nslcmops", nslcmop_id
)
5124 if not task_is_locked_by_me
:
5127 logging_text
= "Task ns={} action={} ".format(nsr_id
, nslcmop_id
)
5128 self
.logger
.debug(logging_text
+ "Enter")
5129 # get all needed from database
5133 db_nslcmop_update
= {}
5134 nslcmop_operation_state
= None
5135 error_description_nslcmop
= None
5139 # wait for any previous tasks in process
5140 step
= "Waiting for previous operations to terminate"
5141 await self
.lcm_tasks
.waitfor_related_HA("ns", "nslcmops", nslcmop_id
)
5143 self
._write
_ns
_status
(
5146 current_operation
="RUNNING ACTION",
5147 current_operation_id
=nslcmop_id
,
5150 step
= "Getting information from database"
5151 db_nslcmop
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
})
5152 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
5153 if db_nslcmop
["operationParams"].get("primitive_params"):
5154 db_nslcmop
["operationParams"]["primitive_params"] = json
.loads(
5155 db_nslcmop
["operationParams"]["primitive_params"]
5158 nsr_deployed
= db_nsr
["_admin"].get("deployed")
5159 vnf_index
= db_nslcmop
["operationParams"].get("member_vnf_index")
5160 vdu_id
= db_nslcmop
["operationParams"].get("vdu_id")
5161 kdu_name
= db_nslcmop
["operationParams"].get("kdu_name")
5162 vdu_count_index
= db_nslcmop
["operationParams"].get("vdu_count_index")
5163 primitive
= db_nslcmop
["operationParams"]["primitive"]
5164 primitive_params
= db_nslcmop
["operationParams"]["primitive_params"]
5165 timeout_ns_action
= db_nslcmop
["operationParams"].get(
5166 "timeout_ns_action", self
.timeout
.primitive
5170 step
= "Getting vnfr from database"
5171 db_vnfr
= self
.db
.get_one(
5172 "vnfrs", {"member-vnf-index-ref": vnf_index
, "nsr-id-ref": nsr_id
}
5174 if db_vnfr
.get("kdur"):
5176 for kdur
in db_vnfr
["kdur"]:
5177 if kdur
.get("additionalParams"):
5178 kdur
["additionalParams"] = json
.loads(
5179 kdur
["additionalParams"]
5181 kdur_list
.append(kdur
)
5182 db_vnfr
["kdur"] = kdur_list
5183 step
= "Getting vnfd from database"
5184 db_vnfd
= self
.db
.get_one("vnfds", {"_id": db_vnfr
["vnfd-id"]})
5186 # Sync filesystem before running a primitive
5187 self
.fs
.sync(db_vnfr
["vnfd-id"])
5189 step
= "Getting nsd from database"
5190 db_nsd
= self
.db
.get_one("nsds", {"_id": db_nsr
["nsd-id"]})
5192 vca_id
= self
.get_vca_id(db_vnfr
, db_nsr
)
5193 # for backward compatibility
5194 if nsr_deployed
and isinstance(nsr_deployed
.get("VCA"), dict):
5195 nsr_deployed
["VCA"] = list(nsr_deployed
["VCA"].values())
5196 db_nsr_update
["_admin.deployed.VCA"] = nsr_deployed
["VCA"]
5197 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
5199 # look for primitive
5200 config_primitive_desc
= descriptor_configuration
= None
5202 descriptor_configuration
= get_configuration(db_vnfd
, vdu_id
)
5204 descriptor_configuration
= get_configuration(db_vnfd
, kdu_name
)
5206 descriptor_configuration
= get_configuration(db_vnfd
, db_vnfd
["id"])
5208 descriptor_configuration
= db_nsd
.get("ns-configuration")
5210 if descriptor_configuration
and descriptor_configuration
.get(
5213 for config_primitive
in descriptor_configuration
["config-primitive"]:
5214 if config_primitive
["name"] == primitive
:
5215 config_primitive_desc
= config_primitive
5218 if not config_primitive_desc
:
5219 if not (kdu_name
and primitive
in ("upgrade", "rollback", "status")):
5221 "Primitive {} not found at [ns|vnf|vdu]-configuration:config-primitive ".format(
5225 primitive_name
= primitive
5226 ee_descriptor_id
= None
5228 primitive_name
= config_primitive_desc
.get(
5229 "execution-environment-primitive", primitive
5231 ee_descriptor_id
= config_primitive_desc
.get(
5232 "execution-environment-ref"
5238 (x
for x
in db_vnfr
["vdur"] if x
["vdu-id-ref"] == vdu_id
), None
5240 desc_params
= parse_yaml_strings(vdur
.get("additionalParams"))
5243 (x
for x
in db_vnfr
["kdur"] if x
["kdu-name"] == kdu_name
), None
5245 desc_params
= parse_yaml_strings(kdur
.get("additionalParams"))
5247 desc_params
= parse_yaml_strings(
5248 db_vnfr
.get("additionalParamsForVnf")
5251 desc_params
= parse_yaml_strings(db_nsr
.get("additionalParamsForNs"))
5252 if kdu_name
and get_configuration(db_vnfd
, kdu_name
):
5253 kdu_configuration
= get_configuration(db_vnfd
, kdu_name
)
5255 for primitive
in kdu_configuration
.get("initial-config-primitive", []):
5256 actions
.add(primitive
["name"])
5257 for primitive
in kdu_configuration
.get("config-primitive", []):
5258 actions
.add(primitive
["name"])
5260 nsr_deployed
["K8s"],
5261 lambda kdu
: kdu_name
== kdu
["kdu-name"]
5262 and kdu
["member-vnf-index"] == vnf_index
,
5266 if primitive_name
in actions
5267 and kdu
["k8scluster-type"] not in ("helm-chart", "helm-chart-v3")
5271 # TODO check if ns is in a proper status
5273 primitive_name
in ("upgrade", "rollback", "status") or kdu_action
5275 # kdur and desc_params already set from before
5276 if primitive_params
:
5277 desc_params
.update(primitive_params
)
5278 # TODO Check if we will need something at vnf level
5279 for index
, kdu
in enumerate(get_iterable(nsr_deployed
, "K8s")):
5281 kdu_name
== kdu
["kdu-name"]
5282 and kdu
["member-vnf-index"] == vnf_index
5287 "KDU '{}' for vnf '{}' not deployed".format(kdu_name
, vnf_index
)
5290 if kdu
.get("k8scluster-type") not in self
.k8scluster_map
:
5291 msg
= "unknown k8scluster-type '{}'".format(
5292 kdu
.get("k8scluster-type")
5294 raise LcmException(msg
)
5297 "collection": "nsrs",
5298 "filter": {"_id": nsr_id
},
5299 "path": "_admin.deployed.K8s.{}".format(index
),
5303 + "Exec k8s {} on {}.{}".format(primitive_name
, vnf_index
, kdu_name
)
5305 step
= "Executing kdu {}".format(primitive_name
)
5306 if primitive_name
== "upgrade":
5307 if desc_params
.get("kdu_model"):
5308 kdu_model
= desc_params
.get("kdu_model")
5309 del desc_params
["kdu_model"]
5311 kdu_model
= kdu
.get("kdu-model")
5312 if kdu_model
.count("/") < 2: # helm chart is not embedded
5313 parts
= kdu_model
.split(sep
=":")
5315 kdu_model
= parts
[0]
5316 if desc_params
.get("kdu_atomic_upgrade"):
5317 atomic_upgrade
= desc_params
.get(
5318 "kdu_atomic_upgrade"
5319 ).lower() in ("yes", "true", "1")
5320 del desc_params
["kdu_atomic_upgrade"]
5322 atomic_upgrade
= True
5324 detailed_status
= await asyncio
.wait_for(
5325 self
.k8scluster_map
[kdu
["k8scluster-type"]].upgrade(
5326 cluster_uuid
=kdu
.get("k8scluster-uuid"),
5327 kdu_instance
=kdu
.get("kdu-instance"),
5328 atomic
=atomic_upgrade
,
5329 kdu_model
=kdu_model
,
5332 timeout
=timeout_ns_action
,
5334 timeout
=timeout_ns_action
+ 10,
5337 logging_text
+ " Upgrade of kdu {} done".format(detailed_status
)
5339 elif primitive_name
== "rollback":
5340 detailed_status
= await asyncio
.wait_for(
5341 self
.k8scluster_map
[kdu
["k8scluster-type"]].rollback(
5342 cluster_uuid
=kdu
.get("k8scluster-uuid"),
5343 kdu_instance
=kdu
.get("kdu-instance"),
5346 timeout
=timeout_ns_action
,
5348 elif primitive_name
== "status":
5349 detailed_status
= await asyncio
.wait_for(
5350 self
.k8scluster_map
[kdu
["k8scluster-type"]].status_kdu(
5351 cluster_uuid
=kdu
.get("k8scluster-uuid"),
5352 kdu_instance
=kdu
.get("kdu-instance"),
5355 timeout
=timeout_ns_action
,
5358 kdu_instance
= kdu
.get("kdu-instance") or "{}-{}".format(
5359 kdu
["kdu-name"], nsr_id
5361 params
= self
._map
_primitive
_params
(
5362 config_primitive_desc
, primitive_params
, desc_params
5365 detailed_status
= await asyncio
.wait_for(
5366 self
.k8scluster_map
[kdu
["k8scluster-type"]].exec_primitive(
5367 cluster_uuid
=kdu
.get("k8scluster-uuid"),
5368 kdu_instance
=kdu_instance
,
5369 primitive_name
=primitive_name
,
5372 timeout
=timeout_ns_action
,
5375 timeout
=timeout_ns_action
,
5379 nslcmop_operation_state
= "COMPLETED"
5381 detailed_status
= ""
5382 nslcmop_operation_state
= "FAILED"
5384 ee_id
, vca_type
= self
._look
_for
_deployed
_vca
(
5385 nsr_deployed
["VCA"],
5386 member_vnf_index
=vnf_index
,
5388 vdu_count_index
=vdu_count_index
,
5389 ee_descriptor_id
=ee_descriptor_id
,
5391 for vca_index
, vca_deployed
in enumerate(
5392 db_nsr
["_admin"]["deployed"]["VCA"]
5394 if vca_deployed
.get("member-vnf-index") == vnf_index
:
5396 "collection": "nsrs",
5397 "filter": {"_id": nsr_id
},
5398 "path": "_admin.deployed.VCA.{}.".format(vca_index
),
5402 nslcmop_operation_state
,
5404 ) = await self
._ns
_execute
_primitive
(
5406 primitive
=primitive_name
,
5407 primitive_params
=self
._map
_primitive
_params
(
5408 config_primitive_desc
, primitive_params
, desc_params
5410 timeout
=timeout_ns_action
,
5416 db_nslcmop_update
["detailed-status"] = detailed_status
5417 error_description_nslcmop
= (
5418 detailed_status
if nslcmop_operation_state
== "FAILED" else ""
5422 + "Done with result {} {}".format(
5423 nslcmop_operation_state
, detailed_status
5426 return # database update is called inside finally
5428 except (DbException
, LcmException
, N2VCException
, K8sException
) as e
:
5429 self
.logger
.error(logging_text
+ "Exit Exception {}".format(e
))
5431 except asyncio
.CancelledError
:
5433 logging_text
+ "Cancelled Exception while '{}'".format(step
)
5435 exc
= "Operation was cancelled"
5436 except asyncio
.TimeoutError
:
5437 self
.logger
.error(logging_text
+ "Timeout while '{}'".format(step
))
5439 except Exception as e
:
5440 exc
= traceback
.format_exc()
5441 self
.logger
.critical(
5442 logging_text
+ "Exit Exception {} {}".format(type(e
).__name
__, e
),
5451 ) = error_description_nslcmop
= "FAILED {}: {}".format(step
, exc
)
5452 nslcmop_operation_state
= "FAILED"
5454 self
._write
_ns
_status
(
5458 ], # TODO check if degraded. For the moment use previous status
5459 current_operation
="IDLE",
5460 current_operation_id
=None,
5461 # error_description=error_description_nsr,
5462 # error_detail=error_detail,
5463 other_update
=db_nsr_update
,
5466 self
._write
_op
_status
(
5469 error_message
=error_description_nslcmop
,
5470 operation_state
=nslcmop_operation_state
,
5471 other_update
=db_nslcmop_update
,
5474 if nslcmop_operation_state
:
5476 await self
.msg
.aiowrite(
5481 "nslcmop_id": nslcmop_id
,
5482 "operationState": nslcmop_operation_state
,
5485 except Exception as e
:
5487 logging_text
+ "kafka_write notification Exception {}".format(e
)
5489 self
.logger
.debug(logging_text
+ "Exit")
5490 self
.lcm_tasks
.remove("ns", nsr_id
, nslcmop_id
, "ns_action")
5491 return nslcmop_operation_state
, detailed_status
5493 async def terminate_vdus(
5494 self
, db_vnfr
, member_vnf_index
, db_nsr
, update_db_nslcmops
, stage
, logging_text
5496 """This method terminates VDUs
5499 db_vnfr: VNF instance record
5500 member_vnf_index: VNF index to identify the VDUs to be removed
5501 db_nsr: NS instance record
5502 update_db_nslcmops: Nslcmop update record
5504 vca_scaling_info
= []
5505 scaling_info
= {"scaling_group_name": "vdu_autoscale", "vdu": [], "kdu": []}
5506 scaling_info
["scaling_direction"] = "IN"
5507 scaling_info
["vdu-delete"] = {}
5508 scaling_info
["kdu-delete"] = {}
5509 db_vdur
= db_vnfr
.get("vdur")
5510 vdur_list
= copy(db_vdur
)
5512 for index
, vdu
in enumerate(vdur_list
):
5513 vca_scaling_info
.append(
5515 "osm_vdu_id": vdu
["vdu-id-ref"],
5516 "member-vnf-index": member_vnf_index
,
5518 "vdu_index": count_index
,
5521 scaling_info
["vdu-delete"][vdu
["vdu-id-ref"]] = count_index
5522 scaling_info
["vdu"].append(
5524 "name": vdu
.get("name") or vdu
.get("vdu-name"),
5525 "vdu_id": vdu
["vdu-id-ref"],
5529 for interface
in vdu
["interfaces"]:
5530 scaling_info
["vdu"][index
]["interface"].append(
5532 "name": interface
["name"],
5533 "ip_address": interface
["ip-address"],
5534 "mac_address": interface
.get("mac-address"),
5537 self
.logger
.info("NS update scaling info{}".format(scaling_info
))
5538 stage
[2] = "Terminating VDUs"
5539 if scaling_info
.get("vdu-delete"):
5540 # scale_process = "RO"
5541 if self
.ro_config
.ng
:
5542 await self
._scale
_ng
_ro
(
5551 async def remove_vnf(self
, nsr_id
, nslcmop_id
, vnf_instance_id
):
5552 """This method is to Remove VNF instances from NS.
5555 nsr_id: NS instance id
5556 nslcmop_id: nslcmop id of update
5557 vnf_instance_id: id of the VNF instance to be removed
5560 result: (str, str) COMPLETED/FAILED, details
5564 logging_text
= "Task ns={} update ".format(nsr_id
)
5565 check_vnfr_count
= len(self
.db
.get_list("vnfrs", {"nsr-id-ref": nsr_id
}))
5566 self
.logger
.info("check_vnfr_count {}".format(check_vnfr_count
))
5567 if check_vnfr_count
> 1:
5568 stage
= ["", "", ""]
5569 step
= "Getting nslcmop from database"
5571 step
+ " after having waited for previous tasks to be completed"
5573 # db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
5574 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
5575 db_vnfr
= self
.db
.get_one("vnfrs", {"_id": vnf_instance_id
})
5576 member_vnf_index
= db_vnfr
["member-vnf-index-ref"]
5577 """ db_vnfr = self.db.get_one(
5578 "vnfrs", {"member-vnf-index-ref": member_vnf_index, "nsr-id-ref": nsr_id}) """
5580 update_db_nslcmops
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
})
5581 await self
.terminate_vdus(
5590 constituent_vnfr
= db_nsr
.get("constituent-vnfr-ref")
5591 constituent_vnfr
.remove(db_vnfr
.get("_id"))
5592 db_nsr_update
["constituent-vnfr-ref"] = db_nsr
.get(
5593 "constituent-vnfr-ref"
5595 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
5596 self
.db
.del_one("vnfrs", {"_id": db_vnfr
.get("_id")})
5597 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
5598 return "COMPLETED", "Done"
5600 step
= "Terminate VNF Failed with"
5602 "{} Cannot terminate the last VNF in this NS.".format(
5606 except (LcmException
, asyncio
.CancelledError
):
5608 except Exception as e
:
5609 self
.logger
.debug("Error removing VNF {}".format(e
))
5610 return "FAILED", "Error removing VNF {}".format(e
)
5612 async def _ns_redeploy_vnf(
5620 """This method updates and redeploys VNF instances
5623 nsr_id: NS instance id
5624 nslcmop_id: nslcmop id
5625 db_vnfd: VNF descriptor
5626 db_vnfr: VNF instance record
5627 db_nsr: NS instance record
5630 result: (str, str) COMPLETED/FAILED, details
5634 stage
= ["", "", ""]
5635 logging_text
= "Task ns={} update ".format(nsr_id
)
5636 latest_vnfd_revision
= db_vnfd
["_admin"].get("revision")
5637 member_vnf_index
= db_vnfr
["member-vnf-index-ref"]
5639 # Terminate old VNF resources
5640 update_db_nslcmops
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
})
5641 await self
.terminate_vdus(
5650 # old_vnfd_id = db_vnfr["vnfd-id"]
5651 # new_db_vnfd = self.db.get_one("vnfds", {"_id": vnfd_id})
5652 new_db_vnfd
= db_vnfd
5653 # new_vnfd_ref = new_db_vnfd["id"]
5654 # new_vnfd_id = vnfd_id
5658 for cp
in new_db_vnfd
.get("ext-cpd", ()):
5660 "name": cp
.get("id"),
5661 "connection-point-id": cp
.get("int-cpd", {}).get("cpd"),
5662 "connection-point-vdu-id": cp
.get("int-cpd", {}).get("vdu-id"),
5665 new_vnfr_cp
.append(vnf_cp
)
5666 new_vdur
= update_db_nslcmops
["operationParams"]["newVdur"]
5667 # new_vdur = self._create_vdur_descriptor_from_vnfd(db_nsd, db_vnfd, old_db_vnfd, vnfd_id, db_nsr, member_vnf_index)
5668 # new_vnfr_update = {"vnfd-ref": new_vnfd_ref, "vnfd-id": new_vnfd_id, "connection-point": new_vnfr_cp, "vdur": new_vdur, "ip-address": ""}
5670 "revision": latest_vnfd_revision
,
5671 "connection-point": new_vnfr_cp
,
5675 self
.update_db_2("vnfrs", db_vnfr
["_id"], new_vnfr_update
)
5676 updated_db_vnfr
= self
.db
.get_one(
5678 {"member-vnf-index-ref": member_vnf_index
, "nsr-id-ref": nsr_id
},
5681 # Instantiate new VNF resources
5682 # update_db_nslcmops = self.db.get_one("nslcmops", {"_id": nslcmop_id})
5683 vca_scaling_info
= []
5684 scaling_info
= {"scaling_group_name": "vdu_autoscale", "vdu": [], "kdu": []}
5685 scaling_info
["scaling_direction"] = "OUT"
5686 scaling_info
["vdu-create"] = {}
5687 scaling_info
["kdu-create"] = {}
5688 vdud_instantiate_list
= db_vnfd
["vdu"]
5689 for index
, vdud
in enumerate(vdud_instantiate_list
):
5690 cloud_init_text
= self
._get
_vdu
_cloud
_init
_content
(vdud
, db_vnfd
)
5692 additional_params
= (
5693 self
._get
_vdu
_additional
_params
(updated_db_vnfr
, vdud
["id"])
5696 cloud_init_list
= []
5698 # TODO Information of its own ip is not available because db_vnfr is not updated.
5699 additional_params
["OSM"] = get_osm_params(
5700 updated_db_vnfr
, vdud
["id"], 1
5702 cloud_init_list
.append(
5703 self
._parse
_cloud
_init
(
5710 vca_scaling_info
.append(
5712 "osm_vdu_id": vdud
["id"],
5713 "member-vnf-index": member_vnf_index
,
5715 "vdu_index": count_index
,
5718 scaling_info
["vdu-create"][vdud
["id"]] = count_index
5719 if self
.ro_config
.ng
:
5721 "New Resources to be deployed: {}".format(scaling_info
)
5723 await self
._scale
_ng
_ro
(
5731 return "COMPLETED", "Done"
5732 except (LcmException
, asyncio
.CancelledError
):
5734 except Exception as e
:
5735 self
.logger
.debug("Error updating VNF {}".format(e
))
5736 return "FAILED", "Error updating VNF {}".format(e
)
5738 async def _ns_charm_upgrade(
5744 timeout
: float = None,
5746 """This method upgrade charms in VNF instances
5749 ee_id: Execution environment id
5750 path: Local path to the charm
5752 charm_type: Charm type can be lxc-proxy-charm, native-charm or k8s-proxy-charm
5753 timeout: (Float) Timeout for the ns update operation
5756 result: (str, str) COMPLETED/FAILED, details
5759 charm_type
= charm_type
or "lxc_proxy_charm"
5760 output
= await self
.vca_map
[charm_type
].upgrade_charm(
5764 charm_type
=charm_type
,
5765 timeout
=timeout
or self
.timeout
.ns_update
,
5769 return "COMPLETED", output
5771 except (LcmException
, asyncio
.CancelledError
):
5774 except Exception as e
:
5775 self
.logger
.debug("Error upgrading charm {}".format(path
))
5777 return "FAILED", "Error upgrading charm {}: {}".format(path
, e
)
5779 async def update(self
, nsr_id
, nslcmop_id
):
5780 """Update NS according to different update types
5782 This method performs upgrade of VNF instances then updates the revision
5783 number in VNF record
5786 nsr_id: Network service will be updated
5787 nslcmop_id: ns lcm operation id
5790 It may raise DbException, LcmException, N2VCException, K8sException
5793 # Try to lock HA task here
5794 task_is_locked_by_me
= self
.lcm_tasks
.lock_HA("ns", "nslcmops", nslcmop_id
)
5795 if not task_is_locked_by_me
:
5798 logging_text
= "Task ns={} update={} ".format(nsr_id
, nslcmop_id
)
5799 self
.logger
.debug(logging_text
+ "Enter")
5801 # Set the required variables to be filled up later
5803 db_nslcmop_update
= {}
5805 nslcmop_operation_state
= None
5807 error_description_nslcmop
= ""
5809 change_type
= "updated"
5810 detailed_status
= ""
5811 member_vnf_index
= None
5814 # wait for any previous tasks in process
5815 step
= "Waiting for previous operations to terminate"
5816 await self
.lcm_tasks
.waitfor_related_HA("ns", "nslcmops", nslcmop_id
)
5817 self
._write
_ns
_status
(
5820 current_operation
="UPDATING",
5821 current_operation_id
=nslcmop_id
,
5824 step
= "Getting nslcmop from database"
5825 db_nslcmop
= self
.db
.get_one(
5826 "nslcmops", {"_id": nslcmop_id
}, fail_on_empty
=False
5828 update_type
= db_nslcmop
["operationParams"]["updateType"]
5830 step
= "Getting nsr from database"
5831 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
5832 old_operational_status
= db_nsr
["operational-status"]
5833 db_nsr_update
["operational-status"] = "updating"
5834 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
5835 nsr_deployed
= db_nsr
["_admin"].get("deployed")
5837 if update_type
== "CHANGE_VNFPKG":
5838 # Get the input parameters given through update request
5839 vnf_instance_id
= db_nslcmop
["operationParams"][
5840 "changeVnfPackageData"
5841 ].get("vnfInstanceId")
5843 vnfd_id
= db_nslcmop
["operationParams"]["changeVnfPackageData"].get(
5846 timeout_seconds
= db_nslcmop
["operationParams"].get("timeout_ns_update")
5848 step
= "Getting vnfr from database"
5849 db_vnfr
= self
.db
.get_one(
5850 "vnfrs", {"_id": vnf_instance_id
}, fail_on_empty
=False
5853 step
= "Getting vnfds from database"
5855 latest_vnfd
= self
.db
.get_one(
5856 "vnfds", {"_id": vnfd_id
}, fail_on_empty
=False
5858 latest_vnfd_revision
= latest_vnfd
["_admin"].get("revision")
5861 current_vnf_revision
= db_vnfr
.get("revision", 1)
5862 current_vnfd
= self
.db
.get_one(
5864 {"_id": vnfd_id
+ ":" + str(current_vnf_revision
)},
5865 fail_on_empty
=False,
5867 # Charm artifact paths will be filled up later
5869 current_charm_artifact_path
,
5870 target_charm_artifact_path
,
5871 charm_artifact_paths
,
5873 ) = ([], [], [], [])
5875 step
= "Checking if revision has changed in VNFD"
5876 if current_vnf_revision
!= latest_vnfd_revision
:
5877 change_type
= "policy_updated"
5879 # There is new revision of VNFD, update operation is required
5880 current_vnfd_path
= vnfd_id
+ ":" + str(current_vnf_revision
)
5881 latest_vnfd_path
= vnfd_id
+ ":" + str(latest_vnfd_revision
)
5883 step
= "Removing the VNFD packages if they exist in the local path"
5884 shutil
.rmtree(self
.fs
.path
+ current_vnfd_path
, ignore_errors
=True)
5885 shutil
.rmtree(self
.fs
.path
+ latest_vnfd_path
, ignore_errors
=True)
5887 step
= "Get the VNFD packages from FSMongo"
5888 self
.fs
.sync(from_path
=latest_vnfd_path
)
5889 self
.fs
.sync(from_path
=current_vnfd_path
)
5892 "Get the charm-type, charm-id, ee-id if there is deployed VCA"
5894 current_base_folder
= current_vnfd
["_admin"]["storage"]
5895 latest_base_folder
= latest_vnfd
["_admin"]["storage"]
5897 for vca_index
, vca_deployed
in enumerate(
5898 get_iterable(nsr_deployed
, "VCA")
5900 vnf_index
= db_vnfr
.get("member-vnf-index-ref")
5902 # Getting charm-id and charm-type
5903 if vca_deployed
.get("member-vnf-index") == vnf_index
:
5904 vca_id
= self
.get_vca_id(db_vnfr
, db_nsr
)
5905 vca_type
= vca_deployed
.get("type")
5906 vdu_count_index
= vca_deployed
.get("vdu_count_index")
5909 ee_id
= vca_deployed
.get("ee_id")
5911 step
= "Getting descriptor config"
5912 if current_vnfd
.get("kdu"):
5913 search_key
= "kdu_name"
5915 search_key
= "vnfd_id"
5917 entity_id
= vca_deployed
.get(search_key
)
5919 descriptor_config
= get_configuration(
5920 current_vnfd
, entity_id
5923 if "execution-environment-list" in descriptor_config
:
5924 ee_list
= descriptor_config
.get(
5925 "execution-environment-list", []
5930 # There could be several charm used in the same VNF
5931 for ee_item
in ee_list
:
5932 if ee_item
.get("juju"):
5933 step
= "Getting charm name"
5934 charm_name
= ee_item
["juju"].get("charm")
5936 step
= "Setting Charm artifact paths"
5937 current_charm_artifact_path
.append(
5938 get_charm_artifact_path(
5939 current_base_folder
,
5942 current_vnf_revision
,
5945 target_charm_artifact_path
.append(
5946 get_charm_artifact_path(
5950 latest_vnfd_revision
,
5953 elif ee_item
.get("helm-chart"):
5954 # add chart to list and all parameters
5955 step
= "Getting helm chart name"
5956 chart_name
= ee_item
.get("helm-chart")
5958 ee_item
.get("helm-version")
5959 and ee_item
.get("helm-version") == "v2"
5963 vca_type
= "helm-v3"
5964 step
= "Setting Helm chart artifact paths"
5966 helm_artifacts
.append(
5968 "current_artifact_path": get_charm_artifact_path(
5969 current_base_folder
,
5972 current_vnf_revision
,
5974 "target_artifact_path": get_charm_artifact_path(
5978 latest_vnfd_revision
,
5981 "vca_index": vca_index
,
5982 "vdu_index": vdu_count_index
,
5986 charm_artifact_paths
= zip(
5987 current_charm_artifact_path
, target_charm_artifact_path
5990 step
= "Checking if software version has changed in VNFD"
5991 if find_software_version(current_vnfd
) != find_software_version(
5994 step
= "Checking if existing VNF has charm"
5995 for current_charm_path
, target_charm_path
in list(
5996 charm_artifact_paths
5998 if current_charm_path
:
6000 "Software version change is not supported as VNF instance {} has charm.".format(
6005 # There is no change in the charm package, then redeploy the VNF
6006 # based on new descriptor
6007 step
= "Redeploying VNF"
6008 member_vnf_index
= db_vnfr
["member-vnf-index-ref"]
6009 (result
, detailed_status
) = await self
._ns
_redeploy
_vnf
(
6010 nsr_id
, nslcmop_id
, latest_vnfd
, db_vnfr
, db_nsr
6012 if result
== "FAILED":
6013 nslcmop_operation_state
= result
6014 error_description_nslcmop
= detailed_status
6015 db_nslcmop_update
["detailed-status"] = detailed_status
6018 + " step {} Done with result {} {}".format(
6019 step
, nslcmop_operation_state
, detailed_status
6024 step
= "Checking if any charm package has changed or not"
6025 for current_charm_path
, target_charm_path
in list(
6026 charm_artifact_paths
6030 and target_charm_path
6031 and self
.check_charm_hash_changed(
6032 current_charm_path
, target_charm_path
6035 step
= "Checking whether VNF uses juju bundle"
6036 if check_juju_bundle_existence(current_vnfd
):
6038 "Charm upgrade is not supported for the instance which"
6039 " uses juju-bundle: {}".format(
6040 check_juju_bundle_existence(current_vnfd
)
6044 step
= "Upgrading Charm"
6048 ) = await self
._ns
_charm
_upgrade
(
6051 charm_type
=vca_type
,
6052 path
=self
.fs
.path
+ target_charm_path
,
6053 timeout
=timeout_seconds
,
6056 if result
== "FAILED":
6057 nslcmop_operation_state
= result
6058 error_description_nslcmop
= detailed_status
6060 db_nslcmop_update
["detailed-status"] = detailed_status
6063 + " step {} Done with result {} {}".format(
6064 step
, nslcmop_operation_state
, detailed_status
6068 step
= "Updating policies"
6069 member_vnf_index
= db_vnfr
["member-vnf-index-ref"]
6070 result
= "COMPLETED"
6071 detailed_status
= "Done"
6072 db_nslcmop_update
["detailed-status"] = "Done"
6075 for item
in helm_artifacts
:
6077 item
["current_artifact_path"]
6078 and item
["target_artifact_path"]
6079 and self
.check_charm_hash_changed(
6080 item
["current_artifact_path"],
6081 item
["target_artifact_path"],
6085 db_update_entry
= "_admin.deployed.VCA.{}.".format(
6088 vnfr_id
= db_vnfr
["_id"]
6089 osm_config
= {"osm": {"ns_id": nsr_id
, "vnf_id": vnfr_id
}}
6091 "collection": "nsrs",
6092 "filter": {"_id": nsr_id
},
6093 "path": db_update_entry
,
6095 vca_type
, namespace
, helm_id
= get_ee_id_parts(item
["ee_id"])
6096 await self
.vca_map
[vca_type
].upgrade_execution_environment(
6097 namespace
=namespace
,
6101 artifact_path
=item
["target_artifact_path"],
6104 vnf_id
= db_vnfr
.get("vnfd-ref")
6105 config_descriptor
= get_configuration(latest_vnfd
, vnf_id
)
6106 self
.logger
.debug("get ssh key block")
6110 ("config-access", "ssh-access", "required"),
6112 # Needed to inject a ssh key
6115 ("config-access", "ssh-access", "default-user"),
6118 "Install configuration Software, getting public ssh key"
6120 pub_key
= await self
.vca_map
[
6122 ].get_ee_ssh_public__key(
6123 ee_id
=ee_id
, db_dict
=db_dict
, vca_id
=vca_id
6127 "Insert public key into VM user={} ssh_key={}".format(
6131 self
.logger
.debug(logging_text
+ step
)
6133 # wait for RO (ip-address) Insert pub_key into VM
6134 rw_mgmt_ip
= await self
.wait_vm_up_insert_key_ro(
6144 initial_config_primitive_list
= config_descriptor
.get(
6145 "initial-config-primitive"
6147 config_primitive
= next(
6150 for p
in initial_config_primitive_list
6151 if p
["name"] == "config"
6155 if not config_primitive
:
6158 deploy_params
= {"OSM": get_osm_params(db_vnfr
)}
6160 deploy_params
["rw_mgmt_ip"] = rw_mgmt_ip
6161 if db_vnfr
.get("additionalParamsForVnf"):
6162 deploy_params
.update(
6164 db_vnfr
["additionalParamsForVnf"].copy()
6167 primitive_params_
= self
._map
_primitive
_params
(
6168 config_primitive
, {}, deploy_params
6171 step
= "execute primitive '{}' params '{}'".format(
6172 config_primitive
["name"], primitive_params_
6174 self
.logger
.debug(logging_text
+ step
)
6175 await self
.vca_map
[vca_type
].exec_primitive(
6177 primitive_name
=config_primitive
["name"],
6178 params_dict
=primitive_params_
,
6184 step
= "Updating policies"
6185 member_vnf_index
= db_vnfr
["member-vnf-index-ref"]
6186 detailed_status
= "Done"
6187 db_nslcmop_update
["detailed-status"] = "Done"
6189 # If nslcmop_operation_state is None, so any operation is not failed.
6190 if not nslcmop_operation_state
:
6191 nslcmop_operation_state
= "COMPLETED"
6193 # If update CHANGE_VNFPKG nslcmop_operation is successful
6194 # vnf revision need to be updated
6195 vnfr_update
["revision"] = latest_vnfd_revision
6196 self
.update_db_2("vnfrs", db_vnfr
["_id"], vnfr_update
)
6200 + " task Done with result {} {}".format(
6201 nslcmop_operation_state
, detailed_status
6204 elif update_type
== "REMOVE_VNF":
6205 # This part is included in https://osm.etsi.org/gerrit/11876
6206 vnf_instance_id
= db_nslcmop
["operationParams"]["removeVnfInstanceId"]
6207 db_vnfr
= self
.db
.get_one("vnfrs", {"_id": vnf_instance_id
})
6208 member_vnf_index
= db_vnfr
["member-vnf-index-ref"]
6209 step
= "Removing VNF"
6210 (result
, detailed_status
) = await self
.remove_vnf(
6211 nsr_id
, nslcmop_id
, vnf_instance_id
6213 if result
== "FAILED":
6214 nslcmop_operation_state
= result
6215 error_description_nslcmop
= detailed_status
6216 db_nslcmop_update
["detailed-status"] = detailed_status
6217 change_type
= "vnf_terminated"
6218 if not nslcmop_operation_state
:
6219 nslcmop_operation_state
= "COMPLETED"
6222 + " task Done with result {} {}".format(
6223 nslcmop_operation_state
, detailed_status
6227 elif update_type
== "OPERATE_VNF":
6228 vnf_id
= db_nslcmop
["operationParams"]["operateVnfData"][
6231 operation_type
= db_nslcmop
["operationParams"]["operateVnfData"][
6234 additional_param
= db_nslcmop
["operationParams"]["operateVnfData"][
6237 (result
, detailed_status
) = await self
.rebuild_start_stop(
6238 nsr_id
, nslcmop_id
, vnf_id
, additional_param
, operation_type
6240 if result
== "FAILED":
6241 nslcmop_operation_state
= result
6242 error_description_nslcmop
= detailed_status
6243 db_nslcmop_update
["detailed-status"] = detailed_status
6244 if not nslcmop_operation_state
:
6245 nslcmop_operation_state
= "COMPLETED"
6248 + " task Done with result {} {}".format(
6249 nslcmop_operation_state
, detailed_status
6253 # If nslcmop_operation_state is None, so any operation is not failed.
6254 # All operations are executed in overall.
6255 if not nslcmop_operation_state
:
6256 nslcmop_operation_state
= "COMPLETED"
6257 db_nsr_update
["operational-status"] = old_operational_status
6259 except (DbException
, LcmException
, N2VCException
, K8sException
) as e
:
6260 self
.logger
.error(logging_text
+ "Exit Exception {}".format(e
))
6262 except asyncio
.CancelledError
:
6264 logging_text
+ "Cancelled Exception while '{}'".format(step
)
6266 exc
= "Operation was cancelled"
6267 except asyncio
.TimeoutError
:
6268 self
.logger
.error(logging_text
+ "Timeout while '{}'".format(step
))
6270 except Exception as e
:
6271 exc
= traceback
.format_exc()
6272 self
.logger
.critical(
6273 logging_text
+ "Exit Exception {} {}".format(type(e
).__name
__, e
),
6282 ) = error_description_nslcmop
= "FAILED {}: {}".format(step
, exc
)
6283 nslcmop_operation_state
= "FAILED"
6284 db_nsr_update
["operational-status"] = old_operational_status
6286 self
._write
_ns
_status
(
6288 ns_state
=db_nsr
["nsState"],
6289 current_operation
="IDLE",
6290 current_operation_id
=None,
6291 other_update
=db_nsr_update
,
6294 self
._write
_op
_status
(
6297 error_message
=error_description_nslcmop
,
6298 operation_state
=nslcmop_operation_state
,
6299 other_update
=db_nslcmop_update
,
6302 if nslcmop_operation_state
:
6306 "nslcmop_id": nslcmop_id
,
6307 "operationState": nslcmop_operation_state
,
6310 change_type
in ("vnf_terminated", "policy_updated")
6311 and member_vnf_index
6313 msg
.update({"vnf_member_index": member_vnf_index
})
6314 await self
.msg
.aiowrite("ns", change_type
, msg
)
6315 except Exception as e
:
6317 logging_text
+ "kafka_write notification Exception {}".format(e
)
6319 self
.logger
.debug(logging_text
+ "Exit")
6320 self
.lcm_tasks
.remove("ns", nsr_id
, nslcmop_id
, "ns_update")
6321 return nslcmop_operation_state
, detailed_status
6323 async def scale(self
, nsr_id
, nslcmop_id
):
6324 # Try to lock HA task here
6325 task_is_locked_by_me
= self
.lcm_tasks
.lock_HA("ns", "nslcmops", nslcmop_id
)
6326 if not task_is_locked_by_me
:
6329 logging_text
= "Task ns={} scale={} ".format(nsr_id
, nslcmop_id
)
6330 stage
= ["", "", ""]
6331 tasks_dict_info
= {}
6332 # ^ stage, step, VIM progress
6333 self
.logger
.debug(logging_text
+ "Enter")
6334 # get all needed from database
6336 db_nslcmop_update
= {}
6339 # in case of error, indicates what part of scale was failed to put nsr at error status
6340 scale_process
= None
6341 old_operational_status
= ""
6342 old_config_status
= ""
6345 # wait for any previous tasks in process
6346 step
= "Waiting for previous operations to terminate"
6347 await self
.lcm_tasks
.waitfor_related_HA("ns", "nslcmops", nslcmop_id
)
6348 self
._write
_ns
_status
(
6351 current_operation
="SCALING",
6352 current_operation_id
=nslcmop_id
,
6355 step
= "Getting nslcmop from database"
6357 step
+ " after having waited for previous tasks to be completed"
6359 db_nslcmop
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
})
6361 step
= "Getting nsr from database"
6362 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
6363 old_operational_status
= db_nsr
["operational-status"]
6364 old_config_status
= db_nsr
["config-status"]
6366 step
= "Parsing scaling parameters"
6367 db_nsr_update
["operational-status"] = "scaling"
6368 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
6369 nsr_deployed
= db_nsr
["_admin"].get("deployed")
6371 vnf_index
= db_nslcmop
["operationParams"]["scaleVnfData"][
6373 ]["member-vnf-index"]
6374 scaling_group
= db_nslcmop
["operationParams"]["scaleVnfData"][
6376 ]["scaling-group-descriptor"]
6377 scaling_type
= db_nslcmop
["operationParams"]["scaleVnfData"]["scaleVnfType"]
6378 # for backward compatibility
6379 if nsr_deployed
and isinstance(nsr_deployed
.get("VCA"), dict):
6380 nsr_deployed
["VCA"] = list(nsr_deployed
["VCA"].values())
6381 db_nsr_update
["_admin.deployed.VCA"] = nsr_deployed
["VCA"]
6382 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
6384 step
= "Getting vnfr from database"
6385 db_vnfr
= self
.db
.get_one(
6386 "vnfrs", {"member-vnf-index-ref": vnf_index
, "nsr-id-ref": nsr_id
}
6389 vca_id
= self
.get_vca_id(db_vnfr
, db_nsr
)
6391 step
= "Getting vnfd from database"
6392 db_vnfd
= self
.db
.get_one("vnfds", {"_id": db_vnfr
["vnfd-id"]})
6394 base_folder
= db_vnfd
["_admin"]["storage"]
6396 step
= "Getting scaling-group-descriptor"
6397 scaling_descriptor
= find_in_list(
6398 get_scaling_aspect(db_vnfd
),
6399 lambda scale_desc
: scale_desc
["name"] == scaling_group
,
6401 if not scaling_descriptor
:
6403 "input parameter 'scaleByStepData':'scaling-group-descriptor':'{}' is not present "
6404 "at vnfd:scaling-group-descriptor".format(scaling_group
)
6407 step
= "Sending scale order to VIM"
6408 # TODO check if ns is in a proper status
6410 if not db_nsr
["_admin"].get("scaling-group"):
6415 "_admin.scaling-group": [
6416 {"name": scaling_group
, "nb-scale-op": 0}
6420 admin_scale_index
= 0
6422 for admin_scale_index
, admin_scale_info
in enumerate(
6423 db_nsr
["_admin"]["scaling-group"]
6425 if admin_scale_info
["name"] == scaling_group
:
6426 nb_scale_op
= admin_scale_info
.get("nb-scale-op", 0)
6428 else: # not found, set index one plus last element and add new entry with the name
6429 admin_scale_index
+= 1
6431 "_admin.scaling-group.{}.name".format(admin_scale_index
)
6434 vca_scaling_info
= []
6435 scaling_info
= {"scaling_group_name": scaling_group
, "vdu": [], "kdu": []}
6436 if scaling_type
== "SCALE_OUT":
6437 if "aspect-delta-details" not in scaling_descriptor
:
6439 "Aspect delta details not fount in scaling descriptor {}".format(
6440 scaling_descriptor
["name"]
6443 # count if max-instance-count is reached
6444 deltas
= scaling_descriptor
.get("aspect-delta-details")["deltas"]
6446 scaling_info
["scaling_direction"] = "OUT"
6447 scaling_info
["vdu-create"] = {}
6448 scaling_info
["kdu-create"] = {}
6449 for delta
in deltas
:
6450 for vdu_delta
in delta
.get("vdu-delta", {}):
6451 vdud
= get_vdu(db_vnfd
, vdu_delta
["id"])
6452 # vdu_index also provides the number of instance of the targeted vdu
6453 vdu_count
= vdu_index
= get_vdur_index(db_vnfr
, vdu_delta
)
6454 cloud_init_text
= self
._get
_vdu
_cloud
_init
_content
(
6458 additional_params
= (
6459 self
._get
_vdu
_additional
_params
(db_vnfr
, vdud
["id"])
6462 cloud_init_list
= []
6464 vdu_profile
= get_vdu_profile(db_vnfd
, vdu_delta
["id"])
6465 max_instance_count
= 10
6466 if vdu_profile
and "max-number-of-instances" in vdu_profile
:
6467 max_instance_count
= vdu_profile
.get(
6468 "max-number-of-instances", 10
6471 default_instance_num
= get_number_of_instances(
6474 instances_number
= vdu_delta
.get("number-of-instances", 1)
6475 nb_scale_op
+= instances_number
6477 new_instance_count
= nb_scale_op
+ default_instance_num
6478 # Control if new count is over max and vdu count is less than max.
6479 # Then assign new instance count
6480 if new_instance_count
> max_instance_count
> vdu_count
:
6481 instances_number
= new_instance_count
- max_instance_count
6483 instances_number
= instances_number
6485 if new_instance_count
> max_instance_count
:
6487 "reached the limit of {} (max-instance-count) "
6488 "scaling-out operations for the "
6489 "scaling-group-descriptor '{}'".format(
6490 nb_scale_op
, scaling_group
6493 for x
in range(vdu_delta
.get("number-of-instances", 1)):
6495 # TODO Information of its own ip is not available because db_vnfr is not updated.
6496 additional_params
["OSM"] = get_osm_params(
6497 db_vnfr
, vdu_delta
["id"], vdu_index
+ x
6499 cloud_init_list
.append(
6500 self
._parse
_cloud
_init
(
6507 vca_scaling_info
.append(
6509 "osm_vdu_id": vdu_delta
["id"],
6510 "member-vnf-index": vnf_index
,
6512 "vdu_index": vdu_index
+ x
,
6515 scaling_info
["vdu-create"][vdu_delta
["id"]] = instances_number
6516 for kdu_delta
in delta
.get("kdu-resource-delta", {}):
6517 kdu_profile
= get_kdu_resource_profile(db_vnfd
, kdu_delta
["id"])
6518 kdu_name
= kdu_profile
["kdu-name"]
6519 resource_name
= kdu_profile
.get("resource-name", "")
6521 # Might have different kdus in the same delta
6522 # Should have list for each kdu
6523 if not scaling_info
["kdu-create"].get(kdu_name
, None):
6524 scaling_info
["kdu-create"][kdu_name
] = []
6526 kdur
= get_kdur(db_vnfr
, kdu_name
)
6527 if kdur
.get("helm-chart"):
6528 k8s_cluster_type
= "helm-chart-v3"
6529 self
.logger
.debug("kdur: {}".format(kdur
))
6531 kdur
.get("helm-version")
6532 and kdur
.get("helm-version") == "v2"
6534 k8s_cluster_type
= "helm-chart"
6535 elif kdur
.get("juju-bundle"):
6536 k8s_cluster_type
= "juju-bundle"
6539 "kdu type for kdu='{}.{}' is neither helm-chart nor "
6540 "juju-bundle. Maybe an old NBI version is running".format(
6541 db_vnfr
["member-vnf-index-ref"], kdu_name
6545 max_instance_count
= 10
6546 if kdu_profile
and "max-number-of-instances" in kdu_profile
:
6547 max_instance_count
= kdu_profile
.get(
6548 "max-number-of-instances", 10
6551 nb_scale_op
+= kdu_delta
.get("number-of-instances", 1)
6552 deployed_kdu
, _
= get_deployed_kdu(
6553 nsr_deployed
, kdu_name
, vnf_index
6555 if deployed_kdu
is None:
6557 "KDU '{}' for vnf '{}' not deployed".format(
6561 kdu_instance
= deployed_kdu
.get("kdu-instance")
6562 instance_num
= await self
.k8scluster_map
[
6568 cluster_uuid
=deployed_kdu
.get("k8scluster-uuid"),
6569 kdu_model
=deployed_kdu
.get("kdu-model"),
6571 kdu_replica_count
= instance_num
+ kdu_delta
.get(
6572 "number-of-instances", 1
6575 # Control if new count is over max and instance_num is less than max.
6576 # Then assign max instance number to kdu replica count
6577 if kdu_replica_count
> max_instance_count
> instance_num
:
6578 kdu_replica_count
= max_instance_count
6579 if kdu_replica_count
> max_instance_count
:
6581 "reached the limit of {} (max-instance-count) "
6582 "scaling-out operations for the "
6583 "scaling-group-descriptor '{}'".format(
6584 instance_num
, scaling_group
6588 for x
in range(kdu_delta
.get("number-of-instances", 1)):
6589 vca_scaling_info
.append(
6591 "osm_kdu_id": kdu_name
,
6592 "member-vnf-index": vnf_index
,
6594 "kdu_index": instance_num
+ x
- 1,
6597 scaling_info
["kdu-create"][kdu_name
].append(
6599 "member-vnf-index": vnf_index
,
6601 "k8s-cluster-type": k8s_cluster_type
,
6602 "resource-name": resource_name
,
6603 "scale": kdu_replica_count
,
6606 elif scaling_type
== "SCALE_IN":
6607 deltas
= scaling_descriptor
.get("aspect-delta-details")["deltas"]
6609 scaling_info
["scaling_direction"] = "IN"
6610 scaling_info
["vdu-delete"] = {}
6611 scaling_info
["kdu-delete"] = {}
6613 for delta
in deltas
:
6614 for vdu_delta
in delta
.get("vdu-delta", {}):
6615 vdu_count
= vdu_index
= get_vdur_index(db_vnfr
, vdu_delta
)
6616 min_instance_count
= 0
6617 vdu_profile
= get_vdu_profile(db_vnfd
, vdu_delta
["id"])
6618 if vdu_profile
and "min-number-of-instances" in vdu_profile
:
6619 min_instance_count
= vdu_profile
["min-number-of-instances"]
6621 default_instance_num
= get_number_of_instances(
6622 db_vnfd
, vdu_delta
["id"]
6624 instance_num
= vdu_delta
.get("number-of-instances", 1)
6625 nb_scale_op
-= instance_num
6627 new_instance_count
= nb_scale_op
+ default_instance_num
6629 if new_instance_count
< min_instance_count
< vdu_count
:
6630 instances_number
= min_instance_count
- new_instance_count
6632 instances_number
= instance_num
6634 if new_instance_count
< min_instance_count
:
6636 "reached the limit of {} (min-instance-count) scaling-in operations for the "
6637 "scaling-group-descriptor '{}'".format(
6638 nb_scale_op
, scaling_group
6641 for x
in range(vdu_delta
.get("number-of-instances", 1)):
6642 vca_scaling_info
.append(
6644 "osm_vdu_id": vdu_delta
["id"],
6645 "member-vnf-index": vnf_index
,
6647 "vdu_index": vdu_index
- 1 - x
,
6650 scaling_info
["vdu-delete"][vdu_delta
["id"]] = instances_number
6651 for kdu_delta
in delta
.get("kdu-resource-delta", {}):
6652 kdu_profile
= get_kdu_resource_profile(db_vnfd
, kdu_delta
["id"])
6653 kdu_name
= kdu_profile
["kdu-name"]
6654 resource_name
= kdu_profile
.get("resource-name", "")
6656 if not scaling_info
["kdu-delete"].get(kdu_name
, None):
6657 scaling_info
["kdu-delete"][kdu_name
] = []
6659 kdur
= get_kdur(db_vnfr
, kdu_name
)
6660 if kdur
.get("helm-chart"):
6661 k8s_cluster_type
= "helm-chart-v3"
6662 self
.logger
.debug("kdur: {}".format(kdur
))
6664 kdur
.get("helm-version")
6665 and kdur
.get("helm-version") == "v2"
6667 k8s_cluster_type
= "helm-chart"
6668 elif kdur
.get("juju-bundle"):
6669 k8s_cluster_type
= "juju-bundle"
6672 "kdu type for kdu='{}.{}' is neither helm-chart nor "
6673 "juju-bundle. Maybe an old NBI version is running".format(
6674 db_vnfr
["member-vnf-index-ref"], kdur
["kdu-name"]
6678 min_instance_count
= 0
6679 if kdu_profile
and "min-number-of-instances" in kdu_profile
:
6680 min_instance_count
= kdu_profile
["min-number-of-instances"]
6682 nb_scale_op
-= kdu_delta
.get("number-of-instances", 1)
6683 deployed_kdu
, _
= get_deployed_kdu(
6684 nsr_deployed
, kdu_name
, vnf_index
6686 if deployed_kdu
is None:
6688 "KDU '{}' for vnf '{}' not deployed".format(
6692 kdu_instance
= deployed_kdu
.get("kdu-instance")
6693 instance_num
= await self
.k8scluster_map
[
6699 cluster_uuid
=deployed_kdu
.get("k8scluster-uuid"),
6700 kdu_model
=deployed_kdu
.get("kdu-model"),
6702 kdu_replica_count
= instance_num
- kdu_delta
.get(
6703 "number-of-instances", 1
6706 if kdu_replica_count
< min_instance_count
< instance_num
:
6707 kdu_replica_count
= min_instance_count
6708 if kdu_replica_count
< min_instance_count
:
6710 "reached the limit of {} (min-instance-count) scaling-in operations for the "
6711 "scaling-group-descriptor '{}'".format(
6712 instance_num
, scaling_group
6716 for x
in range(kdu_delta
.get("number-of-instances", 1)):
6717 vca_scaling_info
.append(
6719 "osm_kdu_id": kdu_name
,
6720 "member-vnf-index": vnf_index
,
6722 "kdu_index": instance_num
- x
- 1,
6725 scaling_info
["kdu-delete"][kdu_name
].append(
6727 "member-vnf-index": vnf_index
,
6729 "k8s-cluster-type": k8s_cluster_type
,
6730 "resource-name": resource_name
,
6731 "scale": kdu_replica_count
,
6735 # update VDU_SCALING_INFO with the VDUs to delete ip_addresses
6736 vdu_delete
= copy(scaling_info
.get("vdu-delete"))
6737 if scaling_info
["scaling_direction"] == "IN":
6738 for vdur
in reversed(db_vnfr
["vdur"]):
6739 if vdu_delete
.get(vdur
["vdu-id-ref"]):
6740 vdu_delete
[vdur
["vdu-id-ref"]] -= 1
6741 scaling_info
["vdu"].append(
6743 "name": vdur
.get("name") or vdur
.get("vdu-name"),
6744 "vdu_id": vdur
["vdu-id-ref"],
6748 for interface
in vdur
["interfaces"]:
6749 scaling_info
["vdu"][-1]["interface"].append(
6751 "name": interface
["name"],
6752 "ip_address": interface
["ip-address"],
6753 "mac_address": interface
.get("mac-address"),
6756 # vdu_delete = vdu_scaling_info.pop("vdu-delete")
6759 step
= "Executing pre-scale vnf-config-primitive"
6760 if scaling_descriptor
.get("scaling-config-action"):
6761 for scaling_config_action
in scaling_descriptor
[
6762 "scaling-config-action"
6765 scaling_config_action
.get("trigger") == "pre-scale-in"
6766 and scaling_type
== "SCALE_IN"
6768 scaling_config_action
.get("trigger") == "pre-scale-out"
6769 and scaling_type
== "SCALE_OUT"
6771 vnf_config_primitive
= scaling_config_action
[
6772 "vnf-config-primitive-name-ref"
6774 step
= db_nslcmop_update
[
6776 ] = "executing pre-scale scaling-config-action '{}'".format(
6777 vnf_config_primitive
6780 # look for primitive
6781 for config_primitive
in (
6782 get_configuration(db_vnfd
, db_vnfd
["id"]) or {}
6783 ).get("config-primitive", ()):
6784 if config_primitive
["name"] == vnf_config_primitive
:
6788 "Invalid vnfd descriptor at scaling-group-descriptor[name='{}']:scaling-config-action"
6789 "[vnf-config-primitive-name-ref='{}'] does not match any vnf-configuration:config-"
6790 "primitive".format(scaling_group
, vnf_config_primitive
)
6793 vnfr_params
= {"VDU_SCALE_INFO": scaling_info
}
6794 if db_vnfr
.get("additionalParamsForVnf"):
6795 vnfr_params
.update(db_vnfr
["additionalParamsForVnf"])
6797 scale_process
= "VCA"
6798 db_nsr_update
["config-status"] = "configuring pre-scaling"
6799 primitive_params
= self
._map
_primitive
_params
(
6800 config_primitive
, {}, vnfr_params
6803 # Pre-scale retry check: Check if this sub-operation has been executed before
6804 op_index
= self
._check
_or
_add
_scale
_suboperation
(
6807 vnf_config_primitive
,
6811 if op_index
== self
.SUBOPERATION_STATUS_SKIP
:
6812 # Skip sub-operation
6813 result
= "COMPLETED"
6814 result_detail
= "Done"
6817 + "vnf_config_primitive={} Skipped sub-operation, result {} {}".format(
6818 vnf_config_primitive
, result
, result_detail
6822 if op_index
== self
.SUBOPERATION_STATUS_NEW
:
6823 # New sub-operation: Get index of this sub-operation
6825 len(db_nslcmop
.get("_admin", {}).get("operations"))
6830 + "vnf_config_primitive={} New sub-operation".format(
6831 vnf_config_primitive
6835 # retry: Get registered params for this existing sub-operation
6836 op
= db_nslcmop
.get("_admin", {}).get("operations", [])[
6839 vnf_index
= op
.get("member_vnf_index")
6840 vnf_config_primitive
= op
.get("primitive")
6841 primitive_params
= op
.get("primitive_params")
6844 + "vnf_config_primitive={} Sub-operation retry".format(
6845 vnf_config_primitive
6848 # Execute the primitive, either with new (first-time) or registered (reintent) args
6849 ee_descriptor_id
= config_primitive
.get(
6850 "execution-environment-ref"
6852 primitive_name
= config_primitive
.get(
6853 "execution-environment-primitive", vnf_config_primitive
6855 ee_id
, vca_type
= self
._look
_for
_deployed
_vca
(
6856 nsr_deployed
["VCA"],
6857 member_vnf_index
=vnf_index
,
6859 vdu_count_index
=None,
6860 ee_descriptor_id
=ee_descriptor_id
,
6862 result
, result_detail
= await self
._ns
_execute
_primitive
(
6871 + "vnf_config_primitive={} Done with result {} {}".format(
6872 vnf_config_primitive
, result
, result_detail
6875 # Update operationState = COMPLETED | FAILED
6876 self
._update
_suboperation
_status
(
6877 db_nslcmop
, op_index
, result
, result_detail
6880 if result
== "FAILED":
6881 raise LcmException(result_detail
)
6882 db_nsr_update
["config-status"] = old_config_status
6883 scale_process
= None
6887 "_admin.scaling-group.{}.nb-scale-op".format(admin_scale_index
)
6890 "_admin.scaling-group.{}.time".format(admin_scale_index
)
6893 # SCALE-IN VCA - BEGIN
6894 if vca_scaling_info
:
6895 step
= db_nslcmop_update
[
6897 ] = "Deleting the execution environments"
6898 scale_process
= "VCA"
6899 for vca_info
in vca_scaling_info
:
6900 if vca_info
["type"] == "delete" and not vca_info
.get("osm_kdu_id"):
6901 member_vnf_index
= str(vca_info
["member-vnf-index"])
6903 logging_text
+ "vdu info: {}".format(vca_info
)
6905 if vca_info
.get("osm_vdu_id"):
6906 vdu_id
= vca_info
["osm_vdu_id"]
6907 vdu_index
= int(vca_info
["vdu_index"])
6910 ] = "Scaling member_vnf_index={}, vdu_id={}, vdu_index={} ".format(
6911 member_vnf_index
, vdu_id
, vdu_index
6913 stage
[2] = step
= "Scaling in VCA"
6914 self
._write
_op
_status
(op_id
=nslcmop_id
, stage
=stage
)
6915 vca_update
= db_nsr
["_admin"]["deployed"]["VCA"]
6916 config_update
= db_nsr
["configurationStatus"]
6917 for vca_index
, vca
in enumerate(vca_update
):
6919 (vca
or vca
.get("ee_id"))
6920 and vca
["member-vnf-index"] == member_vnf_index
6921 and vca
["vdu_count_index"] == vdu_index
6923 if vca
.get("vdu_id"):
6924 config_descriptor
= get_configuration(
6925 db_vnfd
, vca
.get("vdu_id")
6927 elif vca
.get("kdu_name"):
6928 config_descriptor
= get_configuration(
6929 db_vnfd
, vca
.get("kdu_name")
6932 config_descriptor
= get_configuration(
6933 db_vnfd
, db_vnfd
["id"]
6935 operation_params
= (
6936 db_nslcmop
.get("operationParams") or {}
6938 exec_terminate_primitives
= not operation_params
.get(
6939 "skip_terminate_primitives"
6940 ) and vca
.get("needed_terminate")
6941 task
= asyncio
.ensure_future(
6950 exec_primitives
=exec_terminate_primitives
,
6954 timeout
=self
.timeout
.charm_delete
,
6957 tasks_dict_info
[task
] = "Terminating VCA {}".format(
6960 del vca_update
[vca_index
]
6961 del config_update
[vca_index
]
6962 # wait for pending tasks of terminate primitives
6966 + "Waiting for tasks {}".format(
6967 list(tasks_dict_info
.keys())
6970 error_list
= await self
._wait
_for
_tasks
(
6974 self
.timeout
.charm_delete
, self
.timeout
.ns_terminate
6979 tasks_dict_info
.clear()
6981 raise LcmException("; ".join(error_list
))
6983 db_vca_and_config_update
= {
6984 "_admin.deployed.VCA": vca_update
,
6985 "configurationStatus": config_update
,
6988 "nsrs", db_nsr
["_id"], db_vca_and_config_update
6990 scale_process
= None
6991 # SCALE-IN VCA - END
6994 if scaling_info
.get("vdu-create") or scaling_info
.get("vdu-delete"):
6995 scale_process
= "RO"
6996 if self
.ro_config
.ng
:
6997 await self
._scale
_ng
_ro
(
6998 logging_text
, db_nsr
, db_nslcmop
, db_vnfr
, scaling_info
, stage
7000 scaling_info
.pop("vdu-create", None)
7001 scaling_info
.pop("vdu-delete", None)
7003 scale_process
= None
7007 if scaling_info
.get("kdu-create") or scaling_info
.get("kdu-delete"):
7008 scale_process
= "KDU"
7009 await self
._scale
_kdu
(
7010 logging_text
, nsr_id
, nsr_deployed
, db_vnfd
, vca_id
, scaling_info
7012 scaling_info
.pop("kdu-create", None)
7013 scaling_info
.pop("kdu-delete", None)
7015 scale_process
= None
7019 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
7021 # SCALE-UP VCA - BEGIN
7022 if vca_scaling_info
:
7023 step
= db_nslcmop_update
[
7025 ] = "Creating new execution environments"
7026 scale_process
= "VCA"
7027 for vca_info
in vca_scaling_info
:
7028 if vca_info
["type"] == "create" and not vca_info
.get("osm_kdu_id"):
7029 member_vnf_index
= str(vca_info
["member-vnf-index"])
7031 logging_text
+ "vdu info: {}".format(vca_info
)
7033 vnfd_id
= db_vnfr
["vnfd-ref"]
7034 if vca_info
.get("osm_vdu_id"):
7035 vdu_index
= int(vca_info
["vdu_index"])
7036 deploy_params
= {"OSM": get_osm_params(db_vnfr
)}
7037 if db_vnfr
.get("additionalParamsForVnf"):
7038 deploy_params
.update(
7040 db_vnfr
["additionalParamsForVnf"].copy()
7043 descriptor_config
= get_configuration(
7044 db_vnfd
, db_vnfd
["id"]
7046 if descriptor_config
:
7052 logging_text
=logging_text
7053 + "member_vnf_index={} ".format(member_vnf_index
),
7056 nslcmop_id
=nslcmop_id
,
7062 kdu_index
=kdu_index
,
7063 member_vnf_index
=member_vnf_index
,
7064 vdu_index
=vdu_index
,
7066 deploy_params
=deploy_params
,
7067 descriptor_config
=descriptor_config
,
7068 base_folder
=base_folder
,
7069 task_instantiation_info
=tasks_dict_info
,
7072 vdu_id
= vca_info
["osm_vdu_id"]
7073 vdur
= find_in_list(
7074 db_vnfr
["vdur"], lambda vdu
: vdu
["vdu-id-ref"] == vdu_id
7076 descriptor_config
= get_configuration(db_vnfd
, vdu_id
)
7077 if vdur
.get("additionalParams"):
7078 deploy_params_vdu
= parse_yaml_strings(
7079 vdur
["additionalParams"]
7082 deploy_params_vdu
= deploy_params
7083 deploy_params_vdu
["OSM"] = get_osm_params(
7084 db_vnfr
, vdu_id
, vdu_count_index
=vdu_index
7086 if descriptor_config
:
7092 ] = "Scaling member_vnf_index={}, vdu_id={}, vdu_index={} ".format(
7093 member_vnf_index
, vdu_id
, vdu_index
7095 stage
[2] = step
= "Scaling out VCA"
7096 self
._write
_op
_status
(op_id
=nslcmop_id
, stage
=stage
)
7098 logging_text
=logging_text
7099 + "member_vnf_index={}, vdu_id={}, vdu_index={} ".format(
7100 member_vnf_index
, vdu_id
, vdu_index
7104 nslcmop_id
=nslcmop_id
,
7110 member_vnf_index
=member_vnf_index
,
7111 vdu_index
=vdu_index
,
7112 kdu_index
=kdu_index
,
7114 deploy_params
=deploy_params_vdu
,
7115 descriptor_config
=descriptor_config
,
7116 base_folder
=base_folder
,
7117 task_instantiation_info
=tasks_dict_info
,
7120 # SCALE-UP VCA - END
7121 scale_process
= None
7124 # execute primitive service POST-SCALING
7125 step
= "Executing post-scale vnf-config-primitive"
7126 if scaling_descriptor
.get("scaling-config-action"):
7127 for scaling_config_action
in scaling_descriptor
[
7128 "scaling-config-action"
7131 scaling_config_action
.get("trigger") == "post-scale-in"
7132 and scaling_type
== "SCALE_IN"
7134 scaling_config_action
.get("trigger") == "post-scale-out"
7135 and scaling_type
== "SCALE_OUT"
7137 vnf_config_primitive
= scaling_config_action
[
7138 "vnf-config-primitive-name-ref"
7140 step
= db_nslcmop_update
[
7142 ] = "executing post-scale scaling-config-action '{}'".format(
7143 vnf_config_primitive
7146 vnfr_params
= {"VDU_SCALE_INFO": scaling_info
}
7147 if db_vnfr
.get("additionalParamsForVnf"):
7148 vnfr_params
.update(db_vnfr
["additionalParamsForVnf"])
7150 # look for primitive
7151 for config_primitive
in (
7152 get_configuration(db_vnfd
, db_vnfd
["id"]) or {}
7153 ).get("config-primitive", ()):
7154 if config_primitive
["name"] == vnf_config_primitive
:
7158 "Invalid vnfd descriptor at scaling-group-descriptor[name='{}']:scaling-config-"
7159 "action[vnf-config-primitive-name-ref='{}'] does not match any vnf-configuration:"
7160 "config-primitive".format(
7161 scaling_group
, vnf_config_primitive
7164 scale_process
= "VCA"
7165 db_nsr_update
["config-status"] = "configuring post-scaling"
7166 primitive_params
= self
._map
_primitive
_params
(
7167 config_primitive
, {}, vnfr_params
7170 # Post-scale retry check: Check if this sub-operation has been executed before
7171 op_index
= self
._check
_or
_add
_scale
_suboperation
(
7174 vnf_config_primitive
,
7178 if op_index
== self
.SUBOPERATION_STATUS_SKIP
:
7179 # Skip sub-operation
7180 result
= "COMPLETED"
7181 result_detail
= "Done"
7184 + "vnf_config_primitive={} Skipped sub-operation, result {} {}".format(
7185 vnf_config_primitive
, result
, result_detail
7189 if op_index
== self
.SUBOPERATION_STATUS_NEW
:
7190 # New sub-operation: Get index of this sub-operation
7192 len(db_nslcmop
.get("_admin", {}).get("operations"))
7197 + "vnf_config_primitive={} New sub-operation".format(
7198 vnf_config_primitive
7202 # retry: Get registered params for this existing sub-operation
7203 op
= db_nslcmop
.get("_admin", {}).get("operations", [])[
7206 vnf_index
= op
.get("member_vnf_index")
7207 vnf_config_primitive
= op
.get("primitive")
7208 primitive_params
= op
.get("primitive_params")
7211 + "vnf_config_primitive={} Sub-operation retry".format(
7212 vnf_config_primitive
7215 # Execute the primitive, either with new (first-time) or registered (reintent) args
7216 ee_descriptor_id
= config_primitive
.get(
7217 "execution-environment-ref"
7219 primitive_name
= config_primitive
.get(
7220 "execution-environment-primitive", vnf_config_primitive
7222 ee_id
, vca_type
= self
._look
_for
_deployed
_vca
(
7223 nsr_deployed
["VCA"],
7224 member_vnf_index
=vnf_index
,
7226 vdu_count_index
=None,
7227 ee_descriptor_id
=ee_descriptor_id
,
7229 result
, result_detail
= await self
._ns
_execute
_primitive
(
7238 + "vnf_config_primitive={} Done with result {} {}".format(
7239 vnf_config_primitive
, result
, result_detail
7242 # Update operationState = COMPLETED | FAILED
7243 self
._update
_suboperation
_status
(
7244 db_nslcmop
, op_index
, result
, result_detail
7247 if result
== "FAILED":
7248 raise LcmException(result_detail
)
7249 db_nsr_update
["config-status"] = old_config_status
7250 scale_process
= None
7255 ] = "" # "scaled {} {}".format(scaling_group, scaling_type)
7256 db_nsr_update
["operational-status"] = (
7258 if old_operational_status
== "failed"
7259 else old_operational_status
7261 db_nsr_update
["config-status"] = old_config_status
7264 ROclient
.ROClientException
,
7269 self
.logger
.error(logging_text
+ "Exit Exception {}".format(e
))
7271 except asyncio
.CancelledError
:
7273 logging_text
+ "Cancelled Exception while '{}'".format(step
)
7275 exc
= "Operation was cancelled"
7276 except Exception as e
:
7277 exc
= traceback
.format_exc()
7278 self
.logger
.critical(
7279 logging_text
+ "Exit Exception {} {}".format(type(e
).__name
__, e
),
7283 self
._write
_ns
_status
(
7286 current_operation
="IDLE",
7287 current_operation_id
=None,
7290 stage
[1] = "Waiting for instantiate pending tasks."
7291 self
.logger
.debug(logging_text
+ stage
[1])
7292 exc
= await self
._wait
_for
_tasks
(
7295 self
.timeout
.ns_deploy
,
7303 ] = error_description_nslcmop
= "FAILED {}: {}".format(step
, exc
)
7304 nslcmop_operation_state
= "FAILED"
7306 db_nsr_update
["operational-status"] = old_operational_status
7307 db_nsr_update
["config-status"] = old_config_status
7308 db_nsr_update
["detailed-status"] = ""
7310 if "VCA" in scale_process
:
7311 db_nsr_update
["config-status"] = "failed"
7312 if "RO" in scale_process
:
7313 db_nsr_update
["operational-status"] = "failed"
7316 ] = "FAILED scaling nslcmop={} {}: {}".format(
7317 nslcmop_id
, step
, exc
7320 error_description_nslcmop
= None
7321 nslcmop_operation_state
= "COMPLETED"
7322 db_nslcmop_update
["detailed-status"] = "Done"
7324 self
._write
_op
_status
(
7327 error_message
=error_description_nslcmop
,
7328 operation_state
=nslcmop_operation_state
,
7329 other_update
=db_nslcmop_update
,
7332 self
._write
_ns
_status
(
7335 current_operation
="IDLE",
7336 current_operation_id
=None,
7337 other_update
=db_nsr_update
,
7340 if nslcmop_operation_state
:
7344 "nslcmop_id": nslcmop_id
,
7345 "operationState": nslcmop_operation_state
,
7347 await self
.msg
.aiowrite("ns", "scaled", msg
)
7348 except Exception as e
:
7350 logging_text
+ "kafka_write notification Exception {}".format(e
)
7352 self
.logger
.debug(logging_text
+ "Exit")
7353 self
.lcm_tasks
.remove("ns", nsr_id
, nslcmop_id
, "ns_scale")
7355 async def _scale_kdu(
7356 self
, logging_text
, nsr_id
, nsr_deployed
, db_vnfd
, vca_id
, scaling_info
7358 _scaling_info
= scaling_info
.get("kdu-create") or scaling_info
.get("kdu-delete")
7359 for kdu_name
in _scaling_info
:
7360 for kdu_scaling_info
in _scaling_info
[kdu_name
]:
7361 deployed_kdu
, index
= get_deployed_kdu(
7362 nsr_deployed
, kdu_name
, kdu_scaling_info
["member-vnf-index"]
7364 cluster_uuid
= deployed_kdu
["k8scluster-uuid"]
7365 kdu_instance
= deployed_kdu
["kdu-instance"]
7366 kdu_model
= deployed_kdu
.get("kdu-model")
7367 scale
= int(kdu_scaling_info
["scale"])
7368 k8s_cluster_type
= kdu_scaling_info
["k8s-cluster-type"]
7371 "collection": "nsrs",
7372 "filter": {"_id": nsr_id
},
7373 "path": "_admin.deployed.K8s.{}".format(index
),
7376 step
= "scaling application {}".format(
7377 kdu_scaling_info
["resource-name"]
7379 self
.logger
.debug(logging_text
+ step
)
7381 if kdu_scaling_info
["type"] == "delete":
7382 kdu_config
= get_configuration(db_vnfd
, kdu_name
)
7385 and kdu_config
.get("terminate-config-primitive")
7386 and get_juju_ee_ref(db_vnfd
, kdu_name
) is None
7388 terminate_config_primitive_list
= kdu_config
.get(
7389 "terminate-config-primitive"
7391 terminate_config_primitive_list
.sort(
7392 key
=lambda val
: int(val
["seq"])
7396 terminate_config_primitive
7397 ) in terminate_config_primitive_list
:
7398 primitive_params_
= self
._map
_primitive
_params
(
7399 terminate_config_primitive
, {}, {}
7401 step
= "execute terminate config primitive"
7402 self
.logger
.debug(logging_text
+ step
)
7403 await asyncio
.wait_for(
7404 self
.k8scluster_map
[k8s_cluster_type
].exec_primitive(
7405 cluster_uuid
=cluster_uuid
,
7406 kdu_instance
=kdu_instance
,
7407 primitive_name
=terminate_config_primitive
["name"],
7408 params
=primitive_params_
,
7410 total_timeout
=self
.timeout
.primitive
,
7413 timeout
=self
.timeout
.primitive
7414 * self
.timeout
.primitive_outer_factor
,
7417 await asyncio
.wait_for(
7418 self
.k8scluster_map
[k8s_cluster_type
].scale(
7419 kdu_instance
=kdu_instance
,
7421 resource_name
=kdu_scaling_info
["resource-name"],
7422 total_timeout
=self
.timeout
.scale_on_error
,
7424 cluster_uuid
=cluster_uuid
,
7425 kdu_model
=kdu_model
,
7429 timeout
=self
.timeout
.scale_on_error
7430 * self
.timeout
.scale_on_error_outer_factor
,
7433 if kdu_scaling_info
["type"] == "create":
7434 kdu_config
= get_configuration(db_vnfd
, kdu_name
)
7437 and kdu_config
.get("initial-config-primitive")
7438 and get_juju_ee_ref(db_vnfd
, kdu_name
) is None
7440 initial_config_primitive_list
= kdu_config
.get(
7441 "initial-config-primitive"
7443 initial_config_primitive_list
.sort(
7444 key
=lambda val
: int(val
["seq"])
7447 for initial_config_primitive
in initial_config_primitive_list
:
7448 primitive_params_
= self
._map
_primitive
_params
(
7449 initial_config_primitive
, {}, {}
7451 step
= "execute initial config primitive"
7452 self
.logger
.debug(logging_text
+ step
)
7453 await asyncio
.wait_for(
7454 self
.k8scluster_map
[k8s_cluster_type
].exec_primitive(
7455 cluster_uuid
=cluster_uuid
,
7456 kdu_instance
=kdu_instance
,
7457 primitive_name
=initial_config_primitive
["name"],
7458 params
=primitive_params_
,
7465 async def _scale_ng_ro(
7466 self
, logging_text
, db_nsr
, db_nslcmop
, db_vnfr
, vdu_scaling_info
, stage
7468 nsr_id
= db_nslcmop
["nsInstanceId"]
7469 db_nsd
= self
.db
.get_one("nsds", {"_id": db_nsr
["nsd-id"]})
7472 # read from db: vnfd's for every vnf
7475 # for each vnf in ns, read vnfd
7476 for vnfr
in self
.db
.get_list("vnfrs", {"nsr-id-ref": nsr_id
}):
7477 db_vnfrs
[vnfr
["member-vnf-index-ref"]] = vnfr
7478 vnfd_id
= vnfr
["vnfd-id"] # vnfd uuid for this vnf
7479 # if we haven't this vnfd, read it from db
7480 if not find_in_list(db_vnfds
, lambda a_vnfd
: a_vnfd
["id"] == vnfd_id
):
7482 vnfd
= self
.db
.get_one("vnfds", {"_id": vnfd_id
})
7483 db_vnfds
.append(vnfd
)
7484 n2vc_key
= self
.n2vc
.get_public_key()
7485 n2vc_key_list
= [n2vc_key
]
7488 vdu_scaling_info
.get("vdu-create"),
7489 vdu_scaling_info
.get("vdu-delete"),
7492 # db_vnfr has been updated, update db_vnfrs to use it
7493 db_vnfrs
[db_vnfr
["member-vnf-index-ref"]] = db_vnfr
7494 await self
._instantiate
_ng
_ro
(
7504 start_deploy
=time(),
7505 timeout_ns_deploy
=self
.timeout
.ns_deploy
,
7507 if vdu_scaling_info
.get("vdu-delete"):
7509 db_vnfr
, None, vdu_scaling_info
["vdu-delete"], mark_delete
=False
7512 async def extract_prometheus_scrape_jobs(
7516 ee_config_descriptor
: dict,
7521 vnf_member_index
: str = "",
7523 vdu_index
: int = None,
7525 kdu_index
: int = None,
7527 """Method to extract prometheus scrape jobs from EE's Prometheus template job file
7528 This method will wait until the corresponding VDU or KDU is fully instantiated
7531 ee_id (str): Execution Environment ID
7532 artifact_path (str): Path where the EE's content is (including the Prometheus template file)
7533 ee_config_descriptor (dict): Execution Environment's configuration descriptor
7534 vnfr_id (str): VNFR ID where this EE applies
7535 nsr_id (str): NSR ID where this EE applies
7536 target_ip (str): VDU/KDU instance IP address
7537 element_type (str): NS or VNF or VDU or KDU
7538 vnf_member_index (str, optional): VNF index where this EE applies. Defaults to "".
7539 vdu_id (str, optional): VDU ID where this EE applies. Defaults to "".
7540 vdu_index (int, optional): VDU index where this EE applies. Defaults to None.
7541 kdu_name (str, optional): KDU name where this EE applies. Defaults to "".
7542 kdu_index (int, optional): KDU index where this EE applies. Defaults to None.
7545 LcmException: When the VDU or KDU instance was not found in an hour
7548 _type_: Prometheus jobs
7550 # default the vdur and kdur names to an empty string, to avoid any later
7551 # problem with Prometheus when the element type is not VDU or KDU
7555 # look if exist a file called 'prometheus*.j2' and
7556 artifact_content
= self
.fs
.dir_ls(artifact_path
)
7560 for f
in artifact_content
7561 if f
.startswith("prometheus") and f
.endswith(".j2")
7567 self
.logger
.debug("Artifact path{}".format(artifact_path
))
7568 self
.logger
.debug("job file{}".format(job_file
))
7569 with self
.fs
.file_open((artifact_path
, job_file
), "r") as f
:
7572 # obtain the VDUR or KDUR, if the element type is VDU or KDU
7573 if element_type
in ("VDU", "KDU"):
7574 for _
in range(360):
7575 db_vnfr
= self
.db
.get_one("vnfrs", {"_id": vnfr_id
})
7576 if vdu_id
and vdu_index
is not None:
7580 for x
in get_iterable(db_vnfr
, "vdur")
7582 x
.get("vdu-id-ref") == vdu_id
7583 and x
.get("count-index") == vdu_index
7588 if vdur
.get("name"):
7589 vdur_name
= vdur
.get("name")
7591 if kdu_name
and kdu_index
is not None:
7595 for x
in get_iterable(db_vnfr
, "kdur")
7597 x
.get("kdu-name") == kdu_name
7598 and x
.get("count-index") == kdu_index
7603 if kdur
.get("name"):
7604 kdur_name
= kdur
.get("name")
7607 await asyncio
.sleep(10)
7609 if vdu_id
and vdu_index
is not None:
7611 f
"Timeout waiting VDU with name={vdu_id} and index={vdu_index} to be intantiated"
7613 if kdu_name
and kdu_index
is not None:
7615 f
"Timeout waiting KDU with name={kdu_name} and index={kdu_index} to be intantiated"
7619 if ee_id
is not None:
7620 _
, _
, service
= ee_id
.partition(".") # remove prefix "namespace."
7621 host_name
= "{}-{}".format(service
, ee_config_descriptor
["metric-service"])
7623 vnfr_id
= vnfr_id
.replace("-", "")
7625 "JOB_NAME": vnfr_id
,
7626 "TARGET_IP": target_ip
,
7627 "EXPORTER_POD_IP": host_name
,
7628 "EXPORTER_POD_PORT": host_port
,
7630 "VNF_MEMBER_INDEX": vnf_member_index
,
7631 "VDUR_NAME": vdur_name
,
7632 "KDUR_NAME": kdur_name
,
7633 "ELEMENT_TYPE": element_type
,
7636 metric_path
= ee_config_descriptor
["metric-path"]
7637 target_port
= ee_config_descriptor
["metric-port"]
7638 vnfr_id
= vnfr_id
.replace("-", "")
7640 "JOB_NAME": vnfr_id
,
7641 "TARGET_IP": target_ip
,
7642 "TARGET_PORT": target_port
,
7643 "METRIC_PATH": metric_path
,
7646 job_list
= parse_job(job_data
, variables
)
7647 # ensure job_name is using the vnfr_id. Adding the metadata nsr_id
7648 for job
in job_list
:
7650 not isinstance(job
.get("job_name"), str)
7651 or vnfr_id
not in job
["job_name"]
7653 job
["job_name"] = vnfr_id
+ "_" + str(SystemRandom().randint(1, 10000))
7654 job
["nsr_id"] = nsr_id
7655 job
["vnfr_id"] = vnfr_id
7658 async def rebuild_start_stop(
7659 self
, nsr_id
, nslcmop_id
, vnf_id
, additional_param
, operation_type
7661 logging_text
= "Task ns={} {}={} ".format(nsr_id
, operation_type
, nslcmop_id
)
7662 self
.logger
.info(logging_text
+ "Enter")
7663 stage
= ["Preparing the environment", ""]
7664 # database nsrs record
7668 # in case of error, indicates what part of scale was failed to put nsr at error status
7669 start_deploy
= time()
7671 db_vnfr
= self
.db
.get_one("vnfrs", {"_id": vnf_id
})
7672 vim_account_id
= db_vnfr
.get("vim-account-id")
7673 vim_info_key
= "vim:" + vim_account_id
7674 vdu_id
= additional_param
["vdu_id"]
7675 vdurs
= [item
for item
in db_vnfr
["vdur"] if item
["vdu-id-ref"] == vdu_id
]
7676 vdur
= find_in_list(
7677 vdurs
, lambda vdu
: vdu
["count-index"] == additional_param
["count-index"]
7680 vdu_vim_name
= vdur
["name"]
7681 vim_vm_id
= vdur
["vim_info"][vim_info_key
]["vim_id"]
7682 target_vim
, _
= next(k_v
for k_v
in vdur
["vim_info"].items())
7684 raise LcmException("Target vdu is not found")
7685 self
.logger
.info("vdu_vim_name >> {} ".format(vdu_vim_name
))
7686 # wait for any previous tasks in process
7687 stage
[1] = "Waiting for previous operations to terminate"
7688 self
.logger
.info(stage
[1])
7689 await self
.lcm_tasks
.waitfor_related_HA("ns", "nslcmops", nslcmop_id
)
7691 stage
[1] = "Reading from database."
7692 self
.logger
.info(stage
[1])
7693 self
._write
_ns
_status
(
7696 current_operation
=operation_type
.upper(),
7697 current_operation_id
=nslcmop_id
,
7699 self
._write
_op
_status
(op_id
=nslcmop_id
, stage
=stage
, queuePosition
=0)
7702 stage
[1] = "Getting nsr={} from db.".format(nsr_id
)
7703 db_nsr_update
["operational-status"] = operation_type
7704 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
7708 "vim_vm_id": vim_vm_id
,
7710 "vdu_index": additional_param
["count-index"],
7711 "vdu_id": vdur
["id"],
7712 "target_vim": target_vim
,
7713 "vim_account_id": vim_account_id
,
7716 stage
[1] = "Sending rebuild request to RO... {}".format(desc
)
7717 self
._write
_op
_status
(op_id
=nslcmop_id
, stage
=stage
, queuePosition
=0)
7718 self
.logger
.info("ro nsr id: {}".format(nsr_id
))
7719 result_dict
= await self
.RO
.operate(nsr_id
, desc
, operation_type
)
7720 self
.logger
.info("response from RO: {}".format(result_dict
))
7721 action_id
= result_dict
["action_id"]
7722 await self
._wait
_ng
_ro
(
7727 self
.timeout
.operate
,
7729 "start_stop_rebuild",
7731 return "COMPLETED", "Done"
7732 except (ROclient
.ROClientException
, DbException
, LcmException
) as e
:
7733 self
.logger
.error("Exit Exception {}".format(e
))
7735 except asyncio
.CancelledError
:
7736 self
.logger
.error("Cancelled Exception while '{}'".format(stage
))
7737 exc
= "Operation was cancelled"
7738 except Exception as e
:
7739 exc
= traceback
.format_exc()
7740 self
.logger
.critical(
7741 "Exit Exception {} {}".format(type(e
).__name
__, e
), exc_info
=True
7743 return "FAILED", "Error in operate VNF {}".format(exc
)
7745 def get_vca_cloud_and_credentials(self
, vim_account_id
: str) -> (str, str):
7747 Get VCA Cloud and VCA Cloud Credentials for the VIM account
7749 :param: vim_account_id: VIM Account ID
7751 :return: (cloud_name, cloud_credential)
7753 config
= VimAccountDB
.get_vim_account_with_id(vim_account_id
).get("config", {})
7754 return config
.get("vca_cloud"), config
.get("vca_cloud_credential")
7756 def get_vca_k8s_cloud_and_credentials(self
, vim_account_id
: str) -> (str, str):
7758 Get VCA K8s Cloud and VCA K8s Cloud Credentials for the VIM account
7760 :param: vim_account_id: VIM Account ID
7762 :return: (cloud_name, cloud_credential)
7764 config
= VimAccountDB
.get_vim_account_with_id(vim_account_id
).get("config", {})
7765 return config
.get("vca_k8s_cloud"), config
.get("vca_k8s_cloud_credential")
7767 async def migrate(self
, nsr_id
, nslcmop_id
):
7769 Migrate VNFs and VDUs instances in a NS
7771 :param: nsr_id: NS Instance ID
7772 :param: nslcmop_id: nslcmop ID of migrate
7775 # Try to lock HA task here
7776 task_is_locked_by_me
= self
.lcm_tasks
.lock_HA("ns", "nslcmops", nslcmop_id
)
7777 if not task_is_locked_by_me
:
7779 logging_text
= "Task ns={} migrate ".format(nsr_id
)
7780 self
.logger
.debug(logging_text
+ "Enter")
7781 # get all needed from database
7783 db_nslcmop_update
= {}
7784 nslcmop_operation_state
= None
7788 # in case of error, indicates what part of scale was failed to put nsr at error status
7789 start_deploy
= time()
7792 # wait for any previous tasks in process
7793 step
= "Waiting for previous operations to terminate"
7794 await self
.lcm_tasks
.waitfor_related_HA("ns", "nslcmops", nslcmop_id
)
7796 self
._write
_ns
_status
(
7799 current_operation
="MIGRATING",
7800 current_operation_id
=nslcmop_id
,
7802 step
= "Getting nslcmop from database"
7804 step
+ " after having waited for previous tasks to be completed"
7806 db_nslcmop
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
})
7807 migrate_params
= db_nslcmop
.get("operationParams")
7810 target
.update(migrate_params
)
7811 desc
= await self
.RO
.migrate(nsr_id
, target
)
7812 self
.logger
.debug("RO return > {}".format(desc
))
7813 action_id
= desc
["action_id"]
7814 await self
._wait
_ng
_ro
(
7819 self
.timeout
.migrate
,
7820 operation
="migrate",
7822 except (ROclient
.ROClientException
, DbException
, LcmException
) as e
:
7823 self
.logger
.error("Exit Exception {}".format(e
))
7825 except asyncio
.CancelledError
:
7826 self
.logger
.error("Cancelled Exception while '{}'".format(step
))
7827 exc
= "Operation was cancelled"
7828 except Exception as e
:
7829 exc
= traceback
.format_exc()
7830 self
.logger
.critical(
7831 "Exit Exception {} {}".format(type(e
).__name
__, e
), exc_info
=True
7834 self
._write
_ns
_status
(
7837 current_operation
="IDLE",
7838 current_operation_id
=None,
7841 db_nslcmop_update
["detailed-status"] = "FAILED {}: {}".format(step
, exc
)
7842 nslcmop_operation_state
= "FAILED"
7844 nslcmop_operation_state
= "COMPLETED"
7845 db_nslcmop_update
["detailed-status"] = "Done"
7846 db_nsr_update
["detailed-status"] = "Done"
7848 self
._write
_op
_status
(
7852 operation_state
=nslcmop_operation_state
,
7853 other_update
=db_nslcmop_update
,
7855 if nslcmop_operation_state
:
7859 "nslcmop_id": nslcmop_id
,
7860 "operationState": nslcmop_operation_state
,
7862 await self
.msg
.aiowrite("ns", "migrated", msg
)
7863 except Exception as e
:
7865 logging_text
+ "kafka_write notification Exception {}".format(e
)
7867 self
.logger
.debug(logging_text
+ "Exit")
7868 self
.lcm_tasks
.remove("ns", nsr_id
, nslcmop_id
, "ns_migrate")
7870 async def heal(self
, nsr_id
, nslcmop_id
):
7874 :param nsr_id: ns instance to heal
7875 :param nslcmop_id: operation to run
7879 # Try to lock HA task here
7880 task_is_locked_by_me
= self
.lcm_tasks
.lock_HA("ns", "nslcmops", nslcmop_id
)
7881 if not task_is_locked_by_me
:
7884 logging_text
= "Task ns={} heal={} ".format(nsr_id
, nslcmop_id
)
7885 stage
= ["", "", ""]
7886 tasks_dict_info
= {}
7887 # ^ stage, step, VIM progress
7888 self
.logger
.debug(logging_text
+ "Enter")
7889 # get all needed from database
7891 db_nslcmop_update
= {}
7893 db_vnfrs
= {} # vnf's info indexed by _id
7895 old_operational_status
= ""
7896 old_config_status
= ""
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
)
7902 self
._write
_ns
_status
(
7905 current_operation
="HEALING",
7906 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
})
7915 step
= "Getting nsr from database"
7916 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
7917 old_operational_status
= db_nsr
["operational-status"]
7918 old_config_status
= db_nsr
["config-status"]
7921 "_admin.deployed.RO.operational-status": "healing",
7923 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
7925 step
= "Sending heal order to VIM"
7927 logging_text
=logging_text
,
7929 db_nslcmop
=db_nslcmop
,
7934 stage
[1] = "Getting nsd={} from db.".format(db_nsr
["nsd-id"])
7935 self
.logger
.debug(logging_text
+ stage
[1])
7936 nsd
= self
.db
.get_one("nsds", {"_id": db_nsr
["nsd-id"]})
7937 self
.fs
.sync(db_nsr
["nsd-id"])
7939 # read from db: vnfr's of this ns
7940 step
= "Getting vnfrs from db"
7941 db_vnfrs_list
= self
.db
.get_list("vnfrs", {"nsr-id-ref": nsr_id
})
7942 for vnfr
in db_vnfrs_list
:
7943 db_vnfrs
[vnfr
["_id"]] = vnfr
7944 self
.logger
.debug("ns.heal db_vnfrs={}".format(db_vnfrs
))
7946 # Check for each target VNF
7947 target_list
= db_nslcmop
.get("operationParams", {}).get("healVnfData", {})
7948 for target_vnf
in target_list
:
7949 # Find this VNF in the list from DB
7950 vnfr_id
= target_vnf
.get("vnfInstanceId", None)
7952 db_vnfr
= db_vnfrs
[vnfr_id
]
7953 vnfd_id
= db_vnfr
.get("vnfd-id")
7954 vnfd_ref
= db_vnfr
.get("vnfd-ref")
7955 vnfd
= self
.db
.get_one("vnfds", {"_id": vnfd_id
})
7956 base_folder
= vnfd
["_admin"]["storage"]
7961 nsi_id
= None # TODO put nsi_id when this nsr belongs to a NSI
7962 member_vnf_index
= db_vnfr
.get("member-vnf-index-ref")
7964 # Check each target VDU and deploy N2VC
7965 target_vdu_list
= target_vnf
.get("additionalParams", {}).get(
7968 if not target_vdu_list
:
7969 # Codigo nuevo para crear diccionario
7970 target_vdu_list
= []
7971 for existing_vdu
in db_vnfr
.get("vdur"):
7972 vdu_name
= existing_vdu
.get("vdu-name", None)
7973 vdu_index
= existing_vdu
.get("count-index", 0)
7974 vdu_run_day1
= target_vnf
.get("additionalParams", {}).get(
7977 vdu_to_be_healed
= {
7979 "count-index": vdu_index
,
7980 "run-day1": vdu_run_day1
,
7982 target_vdu_list
.append(vdu_to_be_healed
)
7983 for target_vdu
in target_vdu_list
:
7984 deploy_params_vdu
= target_vdu
7985 # Set run-day1 vnf level value if not vdu level value exists
7986 if not deploy_params_vdu
.get("run-day1") and target_vnf
.get(
7987 "additionalParams", {}
7989 deploy_params_vdu
["run-day1"] = target_vnf
[
7992 vdu_name
= target_vdu
.get("vdu-id", None)
7993 # TODO: Get vdu_id from vdud.
7995 # For multi instance VDU count-index is mandatory
7996 # For single session VDU count-indes is 0
7997 vdu_index
= target_vdu
.get("count-index", 0)
7999 # n2vc_redesign STEP 3 to 6 Deploy N2VC
8000 stage
[1] = "Deploying Execution Environments."
8001 self
.logger
.debug(logging_text
+ stage
[1])
8003 # VNF Level charm. Normal case when proxy charms.
8004 # If target instance is management machine continue with actions: recreate EE for native charms or reinject juju key for proxy charms.
8005 descriptor_config
= get_configuration(vnfd
, vnfd_ref
)
8006 if descriptor_config
:
8007 # Continue if healed machine is management machine
8008 vnf_ip_address
= db_vnfr
.get("ip-address")
8009 target_instance
= None
8010 for instance
in db_vnfr
.get("vdur", None):
8012 instance
["vdu-name"] == vdu_name
8013 and instance
["count-index"] == vdu_index
8015 target_instance
= instance
8017 if vnf_ip_address
== target_instance
.get("ip-address"):
8019 logging_text
=logging_text
8020 + "member_vnf_index={}, vdu_name={}, vdu_index={} ".format(
8021 member_vnf_index
, vdu_name
, vdu_index
8025 nslcmop_id
=nslcmop_id
,
8031 member_vnf_index
=member_vnf_index
,
8034 deploy_params
=deploy_params_vdu
,
8035 descriptor_config
=descriptor_config
,
8036 base_folder
=base_folder
,
8037 task_instantiation_info
=tasks_dict_info
,
8041 # VDU Level charm. Normal case with native charms.
8042 descriptor_config
= get_configuration(vnfd
, vdu_name
)
8043 if descriptor_config
:
8045 logging_text
=logging_text
8046 + "member_vnf_index={}, vdu_name={}, vdu_index={} ".format(
8047 member_vnf_index
, vdu_name
, vdu_index
8051 nslcmop_id
=nslcmop_id
,
8057 member_vnf_index
=member_vnf_index
,
8058 vdu_index
=vdu_index
,
8060 deploy_params
=deploy_params_vdu
,
8061 descriptor_config
=descriptor_config
,
8062 base_folder
=base_folder
,
8063 task_instantiation_info
=tasks_dict_info
,
8068 ROclient
.ROClientException
,
8073 self
.logger
.error(logging_text
+ "Exit Exception {}".format(e
))
8075 except asyncio
.CancelledError
:
8077 logging_text
+ "Cancelled Exception while '{}'".format(step
)
8079 exc
= "Operation was cancelled"
8080 except Exception as e
:
8081 exc
= traceback
.format_exc()
8082 self
.logger
.critical(
8083 logging_text
+ "Exit Exception {} {}".format(type(e
).__name
__, e
),
8088 stage
[1] = "Waiting for healing pending tasks."
8089 self
.logger
.debug(logging_text
+ stage
[1])
8090 exc
= await self
._wait
_for
_tasks
(
8093 self
.timeout
.ns_deploy
,
8101 ] = error_description_nslcmop
= "FAILED {}: {}".format(step
, exc
)
8102 nslcmop_operation_state
= "FAILED"
8104 db_nsr_update
["operational-status"] = old_operational_status
8105 db_nsr_update
["config-status"] = old_config_status
8108 ] = "FAILED healing nslcmop={} {}: {}".format(nslcmop_id
, step
, exc
)
8109 for task
, task_name
in tasks_dict_info
.items():
8110 if not task
.done() or task
.cancelled() or task
.exception():
8111 if task_name
.startswith(self
.task_name_deploy_vca
):
8112 # A N2VC task is pending
8113 db_nsr_update
["config-status"] = "failed"
8115 # RO task is pending
8116 db_nsr_update
["operational-status"] = "failed"
8118 error_description_nslcmop
= None
8119 nslcmop_operation_state
= "COMPLETED"
8120 db_nslcmop_update
["detailed-status"] = "Done"
8121 db_nsr_update
["detailed-status"] = "Done"
8122 db_nsr_update
["operational-status"] = "running"
8123 db_nsr_update
["config-status"] = "configured"
8125 self
._write
_op
_status
(
8128 error_message
=error_description_nslcmop
,
8129 operation_state
=nslcmop_operation_state
,
8130 other_update
=db_nslcmop_update
,
8133 self
._write
_ns
_status
(
8136 current_operation
="IDLE",
8137 current_operation_id
=None,
8138 other_update
=db_nsr_update
,
8141 if nslcmop_operation_state
:
8145 "nslcmop_id": nslcmop_id
,
8146 "operationState": nslcmop_operation_state
,
8148 await self
.msg
.aiowrite("ns", "healed", msg
)
8149 except Exception as e
:
8151 logging_text
+ "kafka_write notification Exception {}".format(e
)
8153 self
.logger
.debug(logging_text
+ "Exit")
8154 self
.lcm_tasks
.remove("ns", nsr_id
, nslcmop_id
, "ns_heal")
8165 :param logging_text: preffix text to use at logging
8166 :param nsr_id: nsr identity
8167 :param db_nslcmop: database content of ns operation, in this case, 'instantiate'
8168 :param stage: list with 3 items: [general stage, tasks, vim_specific]. This task will write over vim_specific
8169 :return: None or exception
8172 def get_vim_account(vim_account_id
):
8174 if vim_account_id
in db_vims
:
8175 return db_vims
[vim_account_id
]
8176 db_vim
= self
.db
.get_one("vim_accounts", {"_id": vim_account_id
})
8177 db_vims
[vim_account_id
] = db_vim
8182 ns_params
= db_nslcmop
.get("operationParams")
8183 if ns_params
and ns_params
.get("timeout_ns_heal"):
8184 timeout_ns_heal
= ns_params
["timeout_ns_heal"]
8186 timeout_ns_heal
= self
.timeout
.ns_heal
8190 nslcmop_id
= db_nslcmop
["_id"]
8192 "action_id": nslcmop_id
,
8194 self
.logger
.warning(
8195 "db_nslcmop={} and timeout_ns_heal={}".format(
8196 db_nslcmop
, timeout_ns_heal
8199 target
.update(db_nslcmop
.get("operationParams", {}))
8201 self
.logger
.debug("Send to RO > nsr_id={} target={}".format(nsr_id
, target
))
8202 desc
= await self
.RO
.recreate(nsr_id
, target
)
8203 self
.logger
.debug("RO return > {}".format(desc
))
8204 action_id
= desc
["action_id"]
8205 # waits for RO to complete because Reinjecting juju key at ro can find VM in state Deleted
8206 await self
._wait
_ng
_ro
(
8213 operation
="healing",
8218 "_admin.deployed.RO.operational-status": "running",
8219 "detailed-status": " ".join(stage
),
8221 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
8222 self
._write
_op
_status
(nslcmop_id
, stage
)
8224 logging_text
+ "ns healed at RO. RO_id={}".format(action_id
)
8227 except Exception as e
:
8228 stage
[2] = "ERROR healing at VIM"
8229 # self.set_vnfr_at_error(db_vnfrs, str(e))
8231 "Error healing at VIM {}".format(e
),
8232 exc_info
=not isinstance(
8235 ROclient
.ROClientException
,
8261 task_instantiation_info
,
8264 # launch instantiate_N2VC in a asyncio task and register task object
8265 # Look where information of this charm is at database <nsrs>._admin.deployed.VCA
8266 # if not found, create one entry and update database
8267 # fill db_nsr._admin.deployed.VCA.<index>
8270 logging_text
+ "_deploy_n2vc vnfd_id={}, vdu_id={}".format(vnfd_id
, vdu_id
)
8274 get_charm_name
= False
8275 if "execution-environment-list" in descriptor_config
:
8276 ee_list
= descriptor_config
.get("execution-environment-list", [])
8277 elif "juju" in descriptor_config
:
8278 ee_list
= [descriptor_config
] # ns charms
8279 if "execution-environment-list" not in descriptor_config
:
8280 # charm name is only required for ns charms
8281 get_charm_name
= True
8282 else: # other types as script are not supported
8285 for ee_item
in ee_list
:
8288 + "_deploy_n2vc ee_item juju={}, helm={}".format(
8289 ee_item
.get("juju"), ee_item
.get("helm-chart")
8292 ee_descriptor_id
= ee_item
.get("id")
8293 if ee_item
.get("juju"):
8294 vca_name
= ee_item
["juju"].get("charm")
8296 charm_name
= self
.find_charm_name(db_nsr
, str(vca_name
))
8299 if ee_item
["juju"].get("charm") is not None
8302 if ee_item
["juju"].get("cloud") == "k8s":
8303 vca_type
= "k8s_proxy_charm"
8304 elif ee_item
["juju"].get("proxy") is False:
8305 vca_type
= "native_charm"
8306 elif ee_item
.get("helm-chart"):
8307 vca_name
= ee_item
["helm-chart"]
8308 if ee_item
.get("helm-version") and ee_item
.get("helm-version") == "v2":
8311 vca_type
= "helm-v3"
8314 logging_text
+ "skipping non juju neither charm configuration"
8319 for vca_index
, vca_deployed
in enumerate(
8320 db_nsr
["_admin"]["deployed"]["VCA"]
8322 if not vca_deployed
:
8325 vca_deployed
.get("member-vnf-index") == member_vnf_index
8326 and vca_deployed
.get("vdu_id") == vdu_id
8327 and vca_deployed
.get("kdu_name") == kdu_name
8328 and vca_deployed
.get("vdu_count_index", 0) == vdu_index
8329 and vca_deployed
.get("ee_descriptor_id") == ee_descriptor_id
8333 # not found, create one.
8335 "ns" if not member_vnf_index
else "vnf/{}".format(member_vnf_index
)
8338 target
+= "/vdu/{}/{}".format(vdu_id
, vdu_index
or 0)
8340 target
+= "/kdu/{}".format(kdu_name
)
8342 "target_element": target
,
8343 # ^ target_element will replace member-vnf-index, kdu_name, vdu_id ... in a single string
8344 "member-vnf-index": member_vnf_index
,
8346 "kdu_name": kdu_name
,
8347 "vdu_count_index": vdu_index
,
8348 "operational-status": "init", # TODO revise
8349 "detailed-status": "", # TODO revise
8350 "step": "initial-deploy", # TODO revise
8352 "vdu_name": vdu_name
,
8354 "ee_descriptor_id": ee_descriptor_id
,
8355 "charm_name": charm_name
,
8359 # create VCA and configurationStatus in db
8361 "_admin.deployed.VCA.{}".format(vca_index
): vca_deployed
,
8362 "configurationStatus.{}".format(vca_index
): dict(),
8364 self
.update_db_2("nsrs", nsr_id
, db_dict
)
8366 db_nsr
["_admin"]["deployed"]["VCA"].append(vca_deployed
)
8368 self
.logger
.debug("N2VC > NSR_ID > {}".format(nsr_id
))
8369 self
.logger
.debug("N2VC > DB_NSR > {}".format(db_nsr
))
8370 self
.logger
.debug("N2VC > VCA_DEPLOYED > {}".format(vca_deployed
))
8373 task_n2vc
= asyncio
.ensure_future(
8375 logging_text
=logging_text
,
8376 vca_index
=vca_index
,
8382 vdu_index
=vdu_index
,
8383 deploy_params
=deploy_params
,
8384 config_descriptor
=descriptor_config
,
8385 base_folder
=base_folder
,
8386 nslcmop_id
=nslcmop_id
,
8390 ee_config_descriptor
=ee_item
,
8393 self
.lcm_tasks
.register(
8397 "instantiate_N2VC-{}".format(vca_index
),
8400 task_instantiation_info
[
8402 ] = self
.task_name_deploy_vca
+ " {}.{}".format(
8403 member_vnf_index
or "", vdu_id
or ""
8406 async def heal_N2VC(
8423 ee_config_descriptor
,
8425 nsr_id
= db_nsr
["_id"]
8426 db_update_entry
= "_admin.deployed.VCA.{}.".format(vca_index
)
8427 vca_deployed_list
= db_nsr
["_admin"]["deployed"]["VCA"]
8428 vca_deployed
= db_nsr
["_admin"]["deployed"]["VCA"][vca_index
]
8429 osm_config
= {"osm": {"ns_id": db_nsr
["_id"]}}
8431 "collection": "nsrs",
8432 "filter": {"_id": nsr_id
},
8433 "path": db_update_entry
,
8438 element_under_configuration
= nsr_id
8442 vnfr_id
= db_vnfr
["_id"]
8443 osm_config
["osm"]["vnf_id"] = vnfr_id
8445 namespace
= "{nsi}.{ns}".format(nsi
=nsi_id
if nsi_id
else "", ns
=nsr_id
)
8447 if vca_type
== "native_charm":
8450 index_number
= vdu_index
or 0
8453 element_type
= "VNF"
8454 element_under_configuration
= vnfr_id
8455 namespace
+= ".{}-{}".format(vnfr_id
, index_number
)
8457 namespace
+= ".{}-{}".format(vdu_id
, index_number
)
8458 element_type
= "VDU"
8459 element_under_configuration
= "{}-{}".format(vdu_id
, index_number
)
8460 osm_config
["osm"]["vdu_id"] = vdu_id
8462 namespace
+= ".{}".format(kdu_name
)
8463 element_type
= "KDU"
8464 element_under_configuration
= kdu_name
8465 osm_config
["osm"]["kdu_name"] = kdu_name
8468 if base_folder
["pkg-dir"]:
8469 artifact_path
= "{}/{}/{}/{}".format(
8470 base_folder
["folder"],
8471 base_folder
["pkg-dir"],
8474 in ("native_charm", "lxc_proxy_charm", "k8s_proxy_charm")
8479 artifact_path
= "{}/Scripts/{}/{}/".format(
8480 base_folder
["folder"],
8483 in ("native_charm", "lxc_proxy_charm", "k8s_proxy_charm")
8488 self
.logger
.debug("Artifact path > {}".format(artifact_path
))
8490 # get initial_config_primitive_list that applies to this element
8491 initial_config_primitive_list
= config_descriptor
.get(
8492 "initial-config-primitive"
8496 "Initial config primitive list > {}".format(
8497 initial_config_primitive_list
8501 # add config if not present for NS charm
8502 ee_descriptor_id
= ee_config_descriptor
.get("id")
8503 self
.logger
.debug("EE Descriptor > {}".format(ee_descriptor_id
))
8504 initial_config_primitive_list
= get_ee_sorted_initial_config_primitive_list(
8505 initial_config_primitive_list
, vca_deployed
, ee_descriptor_id
8509 "Initial config primitive list #2 > {}".format(
8510 initial_config_primitive_list
8513 # n2vc_redesign STEP 3.1
8514 # find old ee_id if exists
8515 ee_id
= vca_deployed
.get("ee_id")
8517 vca_id
= self
.get_vca_id(db_vnfr
, db_nsr
)
8518 # create or register execution environment in VCA. Only for native charms when healing
8519 if vca_type
== "native_charm":
8520 step
= "Waiting to VM being up and getting IP address"
8521 self
.logger
.debug(logging_text
+ step
)
8522 rw_mgmt_ip
= await self
.wait_vm_up_insert_key_ro(
8531 credentials
= {"hostname": rw_mgmt_ip
}
8533 username
= deep_get(
8534 config_descriptor
, ("config-access", "ssh-access", "default-user")
8536 # TODO remove this when changes on IM regarding config-access:ssh-access:default-user were
8537 # merged. Meanwhile let's get username from initial-config-primitive
8538 if not username
and initial_config_primitive_list
:
8539 for config_primitive
in initial_config_primitive_list
:
8540 for param
in config_primitive
.get("parameter", ()):
8541 if param
["name"] == "ssh-username":
8542 username
= param
["value"]
8546 "Cannot determine the username neither with 'initial-config-primitive' nor with "
8547 "'config-access.ssh-access.default-user'"
8549 credentials
["username"] = username
8551 # n2vc_redesign STEP 3.2
8552 # TODO: Before healing at RO it is needed to destroy native charm units to be deleted.
8553 self
._write
_configuration
_status
(
8555 vca_index
=vca_index
,
8556 status
="REGISTERING",
8557 element_under_configuration
=element_under_configuration
,
8558 element_type
=element_type
,
8561 step
= "register execution environment {}".format(credentials
)
8562 self
.logger
.debug(logging_text
+ step
)
8563 ee_id
= await self
.vca_map
[vca_type
].register_execution_environment(
8564 credentials
=credentials
,
8565 namespace
=namespace
,
8570 # update ee_id en db
8572 "_admin.deployed.VCA.{}.ee_id".format(vca_index
): ee_id
,
8574 self
.update_db_2("nsrs", nsr_id
, db_dict_ee_id
)
8576 # for compatibility with MON/POL modules, the need model and application name at database
8577 # TODO ask MON/POL if needed to not assuming anymore the format "model_name.application_name"
8578 # Not sure if this need to be done when healing
8580 ee_id_parts = ee_id.split(".")
8581 db_nsr_update = {db_update_entry + "ee_id": ee_id}
8582 if len(ee_id_parts) >= 2:
8583 model_name = ee_id_parts[0]
8584 application_name = ee_id_parts[1]
8585 db_nsr_update[db_update_entry + "model"] = model_name
8586 db_nsr_update[db_update_entry + "application"] = application_name
8589 # n2vc_redesign STEP 3.3
8590 # Install configuration software. Only for native charms.
8591 step
= "Install configuration Software"
8593 self
._write
_configuration
_status
(
8595 vca_index
=vca_index
,
8596 status
="INSTALLING SW",
8597 element_under_configuration
=element_under_configuration
,
8598 element_type
=element_type
,
8599 # other_update=db_nsr_update,
8603 # TODO check if already done
8604 self
.logger
.debug(logging_text
+ step
)
8606 if vca_type
== "native_charm":
8607 config_primitive
= next(
8608 (p
for p
in initial_config_primitive_list
if p
["name"] == "config"),
8611 if config_primitive
:
8612 config
= self
._map
_primitive
_params
(
8613 config_primitive
, {}, deploy_params
8615 await self
.vca_map
[vca_type
].install_configuration_sw(
8617 artifact_path
=artifact_path
,
8625 # write in db flag of configuration_sw already installed
8627 "nsrs", nsr_id
, {db_update_entry
+ "config_sw_installed": True}
8630 # Not sure if this need to be done when healing
8632 # add relations for this VCA (wait for other peers related with this VCA)
8633 await self._add_vca_relations(
8634 logging_text=logging_text,
8637 vca_index=vca_index,
8641 # if SSH access is required, then get execution environment SSH public
8642 # if native charm we have waited already to VM be UP
8643 if vca_type
in ("k8s_proxy_charm", "lxc_proxy_charm", "helm", "helm-v3"):
8646 # self.logger.debug("get ssh key block")
8648 config_descriptor
, ("config-access", "ssh-access", "required")
8650 # self.logger.debug("ssh key needed")
8651 # Needed to inject a ssh key
8654 ("config-access", "ssh-access", "default-user"),
8656 step
= "Install configuration Software, getting public ssh key"
8657 pub_key
= await self
.vca_map
[vca_type
].get_ee_ssh_public__key(
8658 ee_id
=ee_id
, db_dict
=db_dict
, vca_id
=vca_id
8661 step
= "Insert public key into VM user={} ssh_key={}".format(
8665 # self.logger.debug("no need to get ssh key")
8666 step
= "Waiting to VM being up and getting IP address"
8667 self
.logger
.debug(logging_text
+ step
)
8669 # n2vc_redesign STEP 5.1
8670 # wait for RO (ip-address) Insert pub_key into VM
8671 # IMPORTANT: We need do wait for RO to complete healing operation.
8672 await self
._wait
_heal
_ro
(nsr_id
, self
.timeout
.ns_heal
)
8675 rw_mgmt_ip
= await self
.wait_kdu_up(
8676 logging_text
, nsr_id
, vnfr_id
, kdu_name
8679 rw_mgmt_ip
= await self
.wait_vm_up_insert_key_ro(
8689 rw_mgmt_ip
= None # This is for a NS configuration
8691 self
.logger
.debug(logging_text
+ " VM_ip_address={}".format(rw_mgmt_ip
))
8693 # store rw_mgmt_ip in deploy params for later replacement
8694 deploy_params
["rw_mgmt_ip"] = rw_mgmt_ip
8697 # get run-day1 operation parameter
8698 runDay1
= deploy_params
.get("run-day1", False)
8700 "Healing vnf={}, vdu={}, runDay1 ={}".format(vnfr_id
, vdu_id
, runDay1
)
8703 # n2vc_redesign STEP 6 Execute initial config primitive
8704 step
= "execute initial config primitive"
8706 # wait for dependent primitives execution (NS -> VNF -> VDU)
8707 if initial_config_primitive_list
:
8708 await self
._wait
_dependent
_n
2vc
(
8709 nsr_id
, vca_deployed_list
, vca_index
8712 # stage, in function of element type: vdu, kdu, vnf or ns
8713 my_vca
= vca_deployed_list
[vca_index
]
8714 if my_vca
.get("vdu_id") or my_vca
.get("kdu_name"):
8716 stage
[0] = "Stage 3/5: running Day-1 primitives for VDU."
8717 elif my_vca
.get("member-vnf-index"):
8719 stage
[0] = "Stage 4/5: running Day-1 primitives for VNF."
8722 stage
[0] = "Stage 5/5: running Day-1 primitives for NS."
8724 self
._write
_configuration
_status
(
8725 nsr_id
=nsr_id
, vca_index
=vca_index
, status
="EXECUTING PRIMITIVE"
8728 self
._write
_op
_status
(op_id
=nslcmop_id
, stage
=stage
)
8730 check_if_terminated_needed
= True
8731 for initial_config_primitive
in initial_config_primitive_list
:
8732 # adding information on the vca_deployed if it is a NS execution environment
8733 if not vca_deployed
["member-vnf-index"]:
8734 deploy_params
["ns_config_info"] = json
.dumps(
8735 self
._get
_ns
_config
_info
(nsr_id
)
8737 # TODO check if already done
8738 primitive_params_
= self
._map
_primitive
_params
(
8739 initial_config_primitive
, {}, deploy_params
8742 step
= "execute primitive '{}' params '{}'".format(
8743 initial_config_primitive
["name"], primitive_params_
8745 self
.logger
.debug(logging_text
+ step
)
8746 await self
.vca_map
[vca_type
].exec_primitive(
8748 primitive_name
=initial_config_primitive
["name"],
8749 params_dict
=primitive_params_
,
8754 # Once some primitive has been exec, check and write at db if it needs to exec terminated primitives
8755 if check_if_terminated_needed
:
8756 if config_descriptor
.get("terminate-config-primitive"):
8760 {db_update_entry
+ "needed_terminate": True},
8762 check_if_terminated_needed
= False
8764 # TODO register in database that primitive is done
8766 # STEP 7 Configure metrics
8767 # Not sure if this need to be done when healing
8769 if vca_type == "helm" or vca_type == "helm-v3":
8770 prometheus_jobs = await self.extract_prometheus_scrape_jobs(
8772 artifact_path=artifact_path,
8773 ee_config_descriptor=ee_config_descriptor,
8776 target_ip=rw_mgmt_ip,
8782 {db_update_entry + "prometheus_jobs": prometheus_jobs},
8785 for job in prometheus_jobs:
8788 {"job_name": job["job_name"]},
8791 fail_on_empty=False,
8795 step
= "instantiated at VCA"
8796 self
.logger
.debug(logging_text
+ step
)
8798 self
._write
_configuration
_status
(
8799 nsr_id
=nsr_id
, vca_index
=vca_index
, status
="READY"
8802 except Exception as e
: # TODO not use Exception but N2VC exception
8803 # self.update_db_2("nsrs", nsr_id, {db_update_entry + "instantiation": "FAILED"})
8805 e
, (DbException
, N2VCException
, LcmException
, asyncio
.CancelledError
)
8808 "Exception while {} : {}".format(step
, e
), exc_info
=True
8810 self
._write
_configuration
_status
(
8811 nsr_id
=nsr_id
, vca_index
=vca_index
, status
="BROKEN"
8813 raise LcmException("{} {}".format(step
, e
)) from e
8815 async def _wait_heal_ro(
8821 while time() <= start_time
+ timeout
:
8822 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
8823 operational_status_ro
= db_nsr
["_admin"]["deployed"]["RO"][
8824 "operational-status"
8826 self
.logger
.debug("Wait Heal RO > {}".format(operational_status_ro
))
8827 if operational_status_ro
!= "healing":
8829 await asyncio
.sleep(15)
8830 else: # timeout_ns_deploy
8831 raise NgRoException("Timeout waiting ns to deploy")
8833 async def vertical_scale(self
, nsr_id
, nslcmop_id
):
8835 Vertical Scale the VDUs in a NS
8837 :param: nsr_id: NS Instance ID
8838 :param: nslcmop_id: nslcmop ID of migrate
8841 # Try to lock HA task here
8842 task_is_locked_by_me
= self
.lcm_tasks
.lock_HA("ns", "nslcmops", nslcmop_id
)
8843 if not task_is_locked_by_me
:
8845 logging_text
= "Task ns={} vertical scale ".format(nsr_id
)
8846 self
.logger
.debug(logging_text
+ "Enter")
8847 # get all needed from database
8849 db_nslcmop_update
= {}
8850 nslcmop_operation_state
= None
8854 # in case of error, indicates what part of scale was failed to put nsr at error status
8855 start_deploy
= time()
8858 # wait for any previous tasks in process
8859 step
= "Waiting for previous operations to terminate"
8860 await self
.lcm_tasks
.waitfor_related_HA("ns", "nslcmops", nslcmop_id
)
8862 self
._write
_ns
_status
(
8865 current_operation
="VerticalScale",
8866 current_operation_id
=nslcmop_id
,
8868 step
= "Getting nslcmop from database"
8870 step
+ " after having waited for previous tasks to be completed"
8872 db_nslcmop
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
})
8873 operationParams
= db_nslcmop
.get("operationParams")
8875 target
.update(operationParams
)
8876 desc
= await self
.RO
.vertical_scale(nsr_id
, target
)
8877 self
.logger
.debug("RO return > {}".format(desc
))
8878 action_id
= desc
["action_id"]
8879 await self
._wait
_ng
_ro
(
8884 self
.timeout
.verticalscale
,
8885 operation
="verticalscale",
8887 except (ROclient
.ROClientException
, DbException
, LcmException
) as e
:
8888 self
.logger
.error("Exit Exception {}".format(e
))
8890 except asyncio
.CancelledError
:
8891 self
.logger
.error("Cancelled Exception while '{}'".format(step
))
8892 exc
= "Operation was cancelled"
8893 except Exception as e
:
8894 exc
= traceback
.format_exc()
8895 self
.logger
.critical(
8896 "Exit Exception {} {}".format(type(e
).__name
__, e
), exc_info
=True
8899 self
._write
_ns
_status
(
8902 current_operation
="IDLE",
8903 current_operation_id
=None,
8906 db_nslcmop_update
["detailed-status"] = "FAILED {}: {}".format(step
, exc
)
8907 nslcmop_operation_state
= "FAILED"
8909 nslcmop_operation_state
= "COMPLETED"
8910 db_nslcmop_update
["detailed-status"] = "Done"
8911 db_nsr_update
["detailed-status"] = "Done"
8913 self
._write
_op
_status
(
8917 operation_state
=nslcmop_operation_state
,
8918 other_update
=db_nslcmop_update
,
8920 if nslcmop_operation_state
:
8924 "nslcmop_id": nslcmop_id
,
8925 "operationState": nslcmop_operation_state
,
8927 await self
.msg
.aiowrite("ns", "verticalscaled", msg
)
8928 except Exception as e
:
8930 logging_text
+ "kafka_write notification Exception {}".format(e
)
8932 self
.logger
.debug(logging_text
+ "Exit")
8933 self
.lcm_tasks
.remove("ns", nsr_id
, nslcmop_id
, "ns_verticalscale")