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
22 import logging
.handlers
33 from osm_lcm
import ROclient
34 from osm_lcm
.data_utils
.nsr
import get_deployed_kdu
35 from osm_lcm
.ng_ro
import NgRoClient
, NgRoException
36 from osm_lcm
.lcm_utils
import (
44 from osm_lcm
.data_utils
.nsd
import get_vnf_profiles
45 from osm_lcm
.data_utils
.vnfd
import (
48 get_ee_sorted_initial_config_primitive_list
,
49 get_ee_sorted_terminate_config_primitive_list
,
51 get_virtual_link_profiles
,
56 get_number_of_instances
,
60 from osm_lcm
.data_utils
.list_utils
import find_in_list
61 from osm_lcm
.data_utils
.vnfr
import get_osm_params
, get_vdur_index
, get_kdur
62 from osm_lcm
.data_utils
.dict_utils
import parse_yaml_strings
63 from osm_lcm
.data_utils
.database
.vim_account
import VimAccountDB
64 from n2vc
.k8s_helm_conn
import K8sHelmConnector
65 from n2vc
.k8s_helm3_conn
import K8sHelm3Connector
66 from n2vc
.k8s_juju_conn
import K8sJujuConnector
68 from osm_common
.dbbase
import DbException
69 from osm_common
.fsbase
import FsException
71 from osm_lcm
.data_utils
.database
.database
import Database
72 from osm_lcm
.data_utils
.filesystem
.filesystem
import Filesystem
74 from n2vc
.n2vc_juju_conn
import N2VCJujuConnector
75 from n2vc
.exceptions
import N2VCException
, N2VCNotFound
, K8sException
77 from osm_lcm
.lcm_helm_conn
import LCMHelmConn
79 from copy
import copy
, deepcopy
81 from uuid
import uuid4
83 from random
import randint
85 __author__
= "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
89 timeout_vca_on_error
= (
91 ) # Time for charm from first time at blocked,error status to mark as failed
92 timeout_ns_deploy
= 2 * 3600 # default global timeout for deployment a ns
93 timeout_ns_terminate
= 1800 # default global timeout for un deployment a ns
94 timeout_charm_delete
= 10 * 60
95 timeout_primitive
= 30 * 60 # timeout for primitive execution
96 timeout_progress_primitive
= (
98 ) # timeout for some progress in a primitive execution
100 SUBOPERATION_STATUS_NOT_FOUND
= -1
101 SUBOPERATION_STATUS_NEW
= -2
102 SUBOPERATION_STATUS_SKIP
= -3
103 task_name_deploy_vca
= "Deploying VCA"
105 def __init__(self
, msg
, lcm_tasks
, config
, loop
, prometheus
=None):
107 Init, Connect to database, filesystem storage, and messaging
108 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
111 super().__init
__(msg
=msg
, logger
=logging
.getLogger("lcm.ns"))
113 self
.db
= Database().instance
.db
114 self
.fs
= Filesystem().instance
.fs
116 self
.lcm_tasks
= lcm_tasks
117 self
.timeout
= config
["timeout"]
118 self
.ro_config
= config
["ro_config"]
119 self
.ng_ro
= config
["ro_config"].get("ng")
120 self
.vca_config
= config
["VCA"].copy()
122 # create N2VC connector
123 self
.n2vc
= N2VCJujuConnector(
126 on_update_db
=self
._on
_update
_n
2vc
_db
,
131 self
.conn_helm_ee
= LCMHelmConn(
134 vca_config
=self
.vca_config
,
135 on_update_db
=self
._on
_update
_n
2vc
_db
,
138 self
.k8sclusterhelm2
= K8sHelmConnector(
139 kubectl_command
=self
.vca_config
.get("kubectlpath"),
140 helm_command
=self
.vca_config
.get("helmpath"),
147 self
.k8sclusterhelm3
= K8sHelm3Connector(
148 kubectl_command
=self
.vca_config
.get("kubectlpath"),
149 helm_command
=self
.vca_config
.get("helm3path"),
156 self
.k8sclusterjuju
= K8sJujuConnector(
157 kubectl_command
=self
.vca_config
.get("kubectlpath"),
158 juju_command
=self
.vca_config
.get("jujupath"),
161 on_update_db
=self
._on
_update
_k
8s
_db
,
166 self
.k8scluster_map
= {
167 "helm-chart": self
.k8sclusterhelm2
,
168 "helm-chart-v3": self
.k8sclusterhelm3
,
169 "chart": self
.k8sclusterhelm3
,
170 "juju-bundle": self
.k8sclusterjuju
,
171 "juju": self
.k8sclusterjuju
,
175 "lxc_proxy_charm": self
.n2vc
,
176 "native_charm": self
.n2vc
,
177 "k8s_proxy_charm": self
.n2vc
,
178 "helm": self
.conn_helm_ee
,
179 "helm-v3": self
.conn_helm_ee
,
182 self
.prometheus
= prometheus
185 self
.RO
= NgRoClient(self
.loop
, **self
.ro_config
)
188 def increment_ip_mac(ip_mac
, vm_index
=1):
189 if not isinstance(ip_mac
, str):
192 # try with ipv4 look for last dot
193 i
= ip_mac
.rfind(".")
196 return "{}{}".format(ip_mac
[:i
], int(ip_mac
[i
:]) + vm_index
)
197 # try with ipv6 or mac look for last colon. Operate in hex
198 i
= ip_mac
.rfind(":")
201 # format in hex, len can be 2 for mac or 4 for ipv6
202 return ("{}{:0" + str(len(ip_mac
) - i
) + "x}").format(
203 ip_mac
[:i
], int(ip_mac
[i
:], 16) + vm_index
209 def _on_update_ro_db(self
, nsrs_id
, ro_descriptor
):
211 # self.logger.debug('_on_update_ro_db(nsrs_id={}'.format(nsrs_id))
214 # TODO filter RO descriptor fields...
218 # db_dict['deploymentStatus'] = yaml.dump(ro_descriptor, default_flow_style=False, indent=2)
219 db_dict
["deploymentStatus"] = ro_descriptor
220 self
.update_db_2("nsrs", nsrs_id
, db_dict
)
222 except Exception as e
:
224 "Cannot write database RO deployment for ns={} -> {}".format(nsrs_id
, e
)
227 async def _on_update_n2vc_db(self
, table
, filter, path
, updated_data
, vca_id
=None):
229 # remove last dot from path (if exists)
230 if path
.endswith("."):
233 # self.logger.debug('_on_update_n2vc_db(table={}, filter={}, path={}, updated_data={}'
234 # .format(table, filter, path, updated_data))
237 nsr_id
= filter.get("_id")
239 # read ns record from database
240 nsr
= self
.db
.get_one(table
="nsrs", q_filter
=filter)
241 current_ns_status
= nsr
.get("nsState")
243 # get vca status for NS
244 status_dict
= await self
.n2vc
.get_status(
245 namespace
="." + nsr_id
, yaml_format
=False, vca_id
=vca_id
250 db_dict
["vcaStatus"] = status_dict
251 await self
.n2vc
.update_vca_status(db_dict
["vcaStatus"], vca_id
=vca_id
)
253 # update configurationStatus for this VCA
255 vca_index
= int(path
[path
.rfind(".") + 1 :])
258 target_dict
=nsr
, key_list
=("_admin", "deployed", "VCA")
260 vca_status
= vca_list
[vca_index
].get("status")
262 configuration_status_list
= nsr
.get("configurationStatus")
263 config_status
= configuration_status_list
[vca_index
].get("status")
265 if config_status
== "BROKEN" and vca_status
!= "failed":
266 db_dict
["configurationStatus"][vca_index
] = "READY"
267 elif config_status
!= "BROKEN" and vca_status
== "failed":
268 db_dict
["configurationStatus"][vca_index
] = "BROKEN"
269 except Exception as e
:
270 # not update configurationStatus
271 self
.logger
.debug("Error updating vca_index (ignore): {}".format(e
))
273 # if nsState = 'READY' check if juju is reporting some error => nsState = 'DEGRADED'
274 # if nsState = 'DEGRADED' check if all is OK
276 if current_ns_status
in ("READY", "DEGRADED"):
277 error_description
= ""
279 if status_dict
.get("machines"):
280 for machine_id
in status_dict
.get("machines"):
281 machine
= status_dict
.get("machines").get(machine_id
)
282 # check machine agent-status
283 if machine
.get("agent-status"):
284 s
= machine
.get("agent-status").get("status")
287 error_description
+= (
288 "machine {} agent-status={} ; ".format(
292 # check machine instance status
293 if machine
.get("instance-status"):
294 s
= machine
.get("instance-status").get("status")
297 error_description
+= (
298 "machine {} instance-status={} ; ".format(
303 if status_dict
.get("applications"):
304 for app_id
in status_dict
.get("applications"):
305 app
= status_dict
.get("applications").get(app_id
)
306 # check application status
307 if app
.get("status"):
308 s
= app
.get("status").get("status")
311 error_description
+= (
312 "application {} status={} ; ".format(app_id
, s
)
315 if error_description
:
316 db_dict
["errorDescription"] = error_description
317 if current_ns_status
== "READY" and is_degraded
:
318 db_dict
["nsState"] = "DEGRADED"
319 if current_ns_status
== "DEGRADED" and not is_degraded
:
320 db_dict
["nsState"] = "READY"
323 self
.update_db_2("nsrs", nsr_id
, db_dict
)
325 except (asyncio
.CancelledError
, asyncio
.TimeoutError
):
327 except Exception as e
:
328 self
.logger
.warn("Error updating NS state for ns={}: {}".format(nsr_id
, e
))
330 async def _on_update_k8s_db(
331 self
, cluster_uuid
, kdu_instance
, filter=None, vca_id
=None, cluster_type
="juju"
334 Updating vca status in NSR record
335 :param cluster_uuid: UUID of a k8s cluster
336 :param kdu_instance: The unique name of the KDU instance
337 :param filter: To get nsr_id
338 :cluster_type: The cluster type (juju, k8s)
342 # self.logger.debug("_on_update_k8s_db(cluster_uuid={}, kdu_instance={}, filter={}"
343 # .format(cluster_uuid, kdu_instance, filter))
345 nsr_id
= filter.get("_id")
347 vca_status
= await self
.k8scluster_map
[cluster_type
].status_kdu(
348 cluster_uuid
=cluster_uuid
,
349 kdu_instance
=kdu_instance
,
351 complete_status
=True,
357 db_dict
["vcaStatus"] = {nsr_id
: vca_status
}
359 if cluster_type
in ("juju-bundle", "juju"):
360 # TODO -> this should be done in a more uniform way, I think in N2VC, in order to update the K8s VCA
361 # status in a similar way between Juju Bundles and Helm Charts on this side
362 await self
.k8sclusterjuju
.update_vca_status(
363 db_dict
["vcaStatus"],
369 f
"Obtained VCA status for cluster type '{cluster_type}': {vca_status}"
373 self
.update_db_2("nsrs", nsr_id
, db_dict
)
374 except (asyncio
.CancelledError
, asyncio
.TimeoutError
):
376 except Exception as e
:
377 self
.logger
.warn("Error updating NS state for ns={}: {}".format(nsr_id
, e
))
380 def _parse_cloud_init(cloud_init_text
, additional_params
, vnfd_id
, vdu_id
):
382 env
= Environment(undefined
=StrictUndefined
)
383 template
= env
.from_string(cloud_init_text
)
384 return template
.render(additional_params
or {})
385 except UndefinedError
as e
:
387 "Variable {} at vnfd[id={}]:vdu[id={}]:cloud-init/cloud-init-"
388 "file, must be provided in the instantiation parameters inside the "
389 "'additionalParamsForVnf/Vdu' block".format(e
, vnfd_id
, vdu_id
)
391 except (TemplateError
, TemplateNotFound
) as e
:
393 "Error parsing Jinja2 to cloud-init content at vnfd[id={}]:vdu[id={}]: {}".format(
398 def _get_vdu_cloud_init_content(self
, vdu
, vnfd
):
399 cloud_init_content
= cloud_init_file
= None
401 if vdu
.get("cloud-init-file"):
402 base_folder
= vnfd
["_admin"]["storage"]
403 cloud_init_file
= "{}/{}/cloud_init/{}".format(
404 base_folder
["folder"],
405 base_folder
["pkg-dir"],
406 vdu
["cloud-init-file"],
408 with self
.fs
.file_open(cloud_init_file
, "r") as ci_file
:
409 cloud_init_content
= ci_file
.read()
410 elif vdu
.get("cloud-init"):
411 cloud_init_content
= vdu
["cloud-init"]
413 return cloud_init_content
414 except FsException
as e
:
416 "Error reading vnfd[id={}]:vdu[id={}]:cloud-init-file={}: {}".format(
417 vnfd
["id"], vdu
["id"], cloud_init_file
, e
421 def _get_vdu_additional_params(self
, db_vnfr
, vdu_id
):
423 (vdur
for vdur
in db_vnfr
.get("vdur") if vdu_id
== vdur
["vdu-id-ref"]),
426 additional_params
= vdur
.get("additionalParams")
427 return parse_yaml_strings(additional_params
)
429 def vnfd2RO(self
, vnfd
, new_id
=None, additionalParams
=None, nsrId
=None):
431 Converts creates a new vnfd descriptor for RO base on input OSM IM vnfd
432 :param vnfd: input vnfd
433 :param new_id: overrides vnf id if provided
434 :param additionalParams: Instantiation params for VNFs provided
435 :param nsrId: Id of the NSR
436 :return: copy of vnfd
438 vnfd_RO
= deepcopy(vnfd
)
439 # remove unused by RO configuration, monitoring, scaling and internal keys
440 vnfd_RO
.pop("_id", None)
441 vnfd_RO
.pop("_admin", None)
442 vnfd_RO
.pop("monitoring-param", None)
443 vnfd_RO
.pop("scaling-group-descriptor", None)
444 vnfd_RO
.pop("kdu", None)
445 vnfd_RO
.pop("k8s-cluster", None)
447 vnfd_RO
["id"] = new_id
449 # parse cloud-init or cloud-init-file with the provided variables using Jinja2
450 for vdu
in get_iterable(vnfd_RO
, "vdu"):
451 vdu
.pop("cloud-init-file", None)
452 vdu
.pop("cloud-init", None)
456 def ip_profile_2_RO(ip_profile
):
457 RO_ip_profile
= deepcopy(ip_profile
)
458 if "dns-server" in RO_ip_profile
:
459 if isinstance(RO_ip_profile
["dns-server"], list):
460 RO_ip_profile
["dns-address"] = []
461 for ds
in RO_ip_profile
.pop("dns-server"):
462 RO_ip_profile
["dns-address"].append(ds
["address"])
464 RO_ip_profile
["dns-address"] = RO_ip_profile
.pop("dns-server")
465 if RO_ip_profile
.get("ip-version") == "ipv4":
466 RO_ip_profile
["ip-version"] = "IPv4"
467 if RO_ip_profile
.get("ip-version") == "ipv6":
468 RO_ip_profile
["ip-version"] = "IPv6"
469 if "dhcp-params" in RO_ip_profile
:
470 RO_ip_profile
["dhcp"] = RO_ip_profile
.pop("dhcp-params")
473 def _get_ro_vim_id_for_vim_account(self
, vim_account
):
474 db_vim
= self
.db
.get_one("vim_accounts", {"_id": vim_account
})
475 if db_vim
["_admin"]["operationalState"] != "ENABLED":
477 "VIM={} is not available. operationalState={}".format(
478 vim_account
, db_vim
["_admin"]["operationalState"]
481 RO_vim_id
= db_vim
["_admin"]["deployed"]["RO"]
484 def get_ro_wim_id_for_wim_account(self
, wim_account
):
485 if isinstance(wim_account
, str):
486 db_wim
= self
.db
.get_one("wim_accounts", {"_id": wim_account
})
487 if db_wim
["_admin"]["operationalState"] != "ENABLED":
489 "WIM={} is not available. operationalState={}".format(
490 wim_account
, db_wim
["_admin"]["operationalState"]
493 RO_wim_id
= db_wim
["_admin"]["deployed"]["RO-account"]
498 def scale_vnfr(self
, db_vnfr
, vdu_create
=None, vdu_delete
=None, mark_delete
=False):
500 db_vdu_push_list
= []
502 db_update
= {"_admin.modified": time()}
504 for vdu_id
, vdu_count
in vdu_create
.items():
508 for vdur
in reversed(db_vnfr
["vdur"])
509 if vdur
["vdu-id-ref"] == vdu_id
514 # Read the template saved in the db:
515 self
.logger
.debug(f
"No vdur in the database. Using the vdur-template to scale")
516 vdur_template
= db_vnfr
.get("vdur-template")
517 if not vdur_template
:
519 "Error scaling OUT VNFR for {}. No vnfr or template exists".format(
523 vdur
= vdur_template
[0]
524 #Delete a template from the database after using it
525 self
.db
.set_one("vnfrs",
526 {"_id": db_vnfr
["_id"]},
528 pull
={"vdur-template": {"_id": vdur
['_id']}}
530 for count
in range(vdu_count
):
531 vdur_copy
= deepcopy(vdur
)
532 vdur_copy
["status"] = "BUILD"
533 vdur_copy
["status-detailed"] = None
534 vdur_copy
["ip-address"] = None
535 vdur_copy
["_id"] = str(uuid4())
536 vdur_copy
["count-index"] += count
+ 1
537 vdur_copy
["id"] = "{}-{}".format(
538 vdur_copy
["vdu-id-ref"], vdur_copy
["count-index"]
540 vdur_copy
.pop("vim_info", None)
541 for iface
in vdur_copy
["interfaces"]:
542 if iface
.get("fixed-ip"):
543 iface
["ip-address"] = self
.increment_ip_mac(
544 iface
["ip-address"], count
+ 1
547 iface
.pop("ip-address", None)
548 if iface
.get("fixed-mac"):
549 iface
["mac-address"] = self
.increment_ip_mac(
550 iface
["mac-address"], count
+ 1
553 iface
.pop("mac-address", None)
557 ) # only first vdu can be managment of vnf
558 db_vdu_push_list
.append(vdur_copy
)
559 # self.logger.debug("scale out, adding vdu={}".format(vdur_copy))
561 if len(db_vnfr
["vdur"]) == 1:
562 # The scale will move to 0 instances
563 self
.logger
.debug(f
"Scaling to 0 !, creating the template with the last vdur")
564 template_vdur
= [db_vnfr
["vdur"][0]]
565 for vdu_id
, vdu_count
in vdu_delete
.items():
567 indexes_to_delete
= [
569 for iv
in enumerate(db_vnfr
["vdur"])
570 if iv
[1]["vdu-id-ref"] == vdu_id
574 "vdur.{}.status".format(i
): "DELETING"
575 for i
in indexes_to_delete
[-vdu_count
:]
579 # it must be deleted one by one because common.db does not allow otherwise
582 for v
in reversed(db_vnfr
["vdur"])
583 if v
["vdu-id-ref"] == vdu_id
585 for vdu
in vdus_to_delete
[:vdu_count
]:
588 {"_id": db_vnfr
["_id"]},
590 pull
={"vdur": {"_id": vdu
["_id"]}},
594 db_push
["vdur"] = db_vdu_push_list
596 db_push
["vdur-template"] = template_vdur
599 db_vnfr
["vdur-template"] = template_vdur
600 self
.db
.set_one("vnfrs", {"_id": db_vnfr
["_id"]}, db_update
, push_list
=db_push
)
601 # modify passed dictionary db_vnfr
602 db_vnfr_
= self
.db
.get_one("vnfrs", {"_id": db_vnfr
["_id"]})
603 db_vnfr
["vdur"] = db_vnfr_
["vdur"]
605 def ns_update_nsr(self
, ns_update_nsr
, db_nsr
, nsr_desc_RO
):
607 Updates database nsr with the RO info for the created vld
608 :param ns_update_nsr: dictionary to be filled with the updated info
609 :param db_nsr: content of db_nsr. This is also modified
610 :param nsr_desc_RO: nsr descriptor from RO
611 :return: Nothing, LcmException is raised on errors
614 for vld_index
, vld
in enumerate(get_iterable(db_nsr
, "vld")):
615 for net_RO
in get_iterable(nsr_desc_RO
, "nets"):
616 if vld
["id"] != net_RO
.get("ns_net_osm_id"):
618 vld
["vim-id"] = net_RO
.get("vim_net_id")
619 vld
["name"] = net_RO
.get("vim_name")
620 vld
["status"] = net_RO
.get("status")
621 vld
["status-detailed"] = net_RO
.get("error_msg")
622 ns_update_nsr
["vld.{}".format(vld_index
)] = vld
626 "ns_update_nsr: Not found vld={} at RO info".format(vld
["id"])
629 def set_vnfr_at_error(self
, db_vnfrs
, error_text
):
631 for db_vnfr
in db_vnfrs
.values():
632 vnfr_update
= {"status": "ERROR"}
633 for vdu_index
, vdur
in enumerate(get_iterable(db_vnfr
, "vdur")):
634 if "status" not in vdur
:
635 vdur
["status"] = "ERROR"
636 vnfr_update
["vdur.{}.status".format(vdu_index
)] = "ERROR"
638 vdur
["status-detailed"] = str(error_text
)
640 "vdur.{}.status-detailed".format(vdu_index
)
642 self
.update_db_2("vnfrs", db_vnfr
["_id"], vnfr_update
)
643 except DbException
as e
:
644 self
.logger
.error("Cannot update vnf. {}".format(e
))
646 def ns_update_vnfr(self
, db_vnfrs
, nsr_desc_RO
):
648 Updates database vnfr with the RO info, e.g. ip_address, vim_id... Descriptor db_vnfrs is also updated
649 :param db_vnfrs: dictionary with member-vnf-index: vnfr-content
650 :param nsr_desc_RO: nsr descriptor from RO
651 :return: Nothing, LcmException is raised on errors
653 for vnf_index
, db_vnfr
in db_vnfrs
.items():
654 for vnf_RO
in nsr_desc_RO
["vnfs"]:
655 if vnf_RO
["member_vnf_index"] != vnf_index
:
658 if vnf_RO
.get("ip_address"):
659 db_vnfr
["ip-address"] = vnfr_update
["ip-address"] = vnf_RO
[
662 elif not db_vnfr
.get("ip-address"):
663 if db_vnfr
.get("vdur"): # if not VDUs, there is not ip_address
664 raise LcmExceptionNoMgmtIP(
665 "ns member_vnf_index '{}' has no IP address".format(
670 for vdu_index
, vdur
in enumerate(get_iterable(db_vnfr
, "vdur")):
671 vdur_RO_count_index
= 0
672 if vdur
.get("pdu-type"):
674 for vdur_RO
in get_iterable(vnf_RO
, "vms"):
675 if vdur
["vdu-id-ref"] != vdur_RO
["vdu_osm_id"]:
677 if vdur
["count-index"] != vdur_RO_count_index
:
678 vdur_RO_count_index
+= 1
680 vdur
["vim-id"] = vdur_RO
.get("vim_vm_id")
681 if vdur_RO
.get("ip_address"):
682 vdur
["ip-address"] = vdur_RO
["ip_address"].split(";")[0]
684 vdur
["ip-address"] = None
685 vdur
["vdu-id-ref"] = vdur_RO
.get("vdu_osm_id")
686 vdur
["name"] = vdur_RO
.get("vim_name")
687 vdur
["status"] = vdur_RO
.get("status")
688 vdur
["status-detailed"] = vdur_RO
.get("error_msg")
689 for ifacer
in get_iterable(vdur
, "interfaces"):
690 for interface_RO
in get_iterable(vdur_RO
, "interfaces"):
691 if ifacer
["name"] == interface_RO
.get("internal_name"):
692 ifacer
["ip-address"] = interface_RO
.get(
695 ifacer
["mac-address"] = interface_RO
.get(
701 "ns_update_vnfr: Not found member_vnf_index={} vdur={} interface={} "
702 "from VIM info".format(
703 vnf_index
, vdur
["vdu-id-ref"], ifacer
["name"]
706 vnfr_update
["vdur.{}".format(vdu_index
)] = vdur
710 "ns_update_vnfr: Not found member_vnf_index={} vdur={} count_index={} from "
712 vnf_index
, vdur
["vdu-id-ref"], vdur
["count-index"]
716 for vld_index
, vld
in enumerate(get_iterable(db_vnfr
, "vld")):
717 for net_RO
in get_iterable(nsr_desc_RO
, "nets"):
718 if vld
["id"] != net_RO
.get("vnf_net_osm_id"):
720 vld
["vim-id"] = net_RO
.get("vim_net_id")
721 vld
["name"] = net_RO
.get("vim_name")
722 vld
["status"] = net_RO
.get("status")
723 vld
["status-detailed"] = net_RO
.get("error_msg")
724 vnfr_update
["vld.{}".format(vld_index
)] = vld
728 "ns_update_vnfr: Not found member_vnf_index={} vld={} from VIM info".format(
733 self
.update_db_2("vnfrs", db_vnfr
["_id"], vnfr_update
)
738 "ns_update_vnfr: Not found member_vnf_index={} from VIM info".format(
743 def _get_ns_config_info(self
, nsr_id
):
745 Generates a mapping between vnf,vdu elements and the N2VC id
746 :param nsr_id: id of nsr to get last database _admin.deployed.VCA that contains this list
747 :return: a dictionary with {osm-config-mapping: {}} where its element contains:
748 "<member-vnf-index>": <N2VC-id> for a vnf configuration, or
749 "<member-vnf-index>.<vdu.id>.<vdu replica(0, 1,..)>": <N2VC-id> for a vdu configuration
751 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
752 vca_deployed_list
= db_nsr
["_admin"]["deployed"]["VCA"]
754 ns_config_info
= {"osm-config-mapping": mapping
}
755 for vca
in vca_deployed_list
:
756 if not vca
["member-vnf-index"]:
758 if not vca
["vdu_id"]:
759 mapping
[vca
["member-vnf-index"]] = vca
["application"]
763 vca
["member-vnf-index"], vca
["vdu_id"], vca
["vdu_count_index"]
765 ] = vca
["application"]
766 return ns_config_info
768 async def _instantiate_ng_ro(
785 def get_vim_account(vim_account_id
):
787 if vim_account_id
in db_vims
:
788 return db_vims
[vim_account_id
]
789 db_vim
= self
.db
.get_one("vim_accounts", {"_id": vim_account_id
})
790 db_vims
[vim_account_id
] = db_vim
793 # modify target_vld info with instantiation parameters
794 def parse_vld_instantiation_params(
795 target_vim
, target_vld
, vld_params
, target_sdn
797 if vld_params
.get("ip-profile"):
798 target_vld
["vim_info"][target_vim
]["ip_profile"] = vld_params
[
801 if vld_params
.get("provider-network"):
802 target_vld
["vim_info"][target_vim
]["provider_network"] = vld_params
[
805 if "sdn-ports" in vld_params
["provider-network"] and target_sdn
:
806 target_vld
["vim_info"][target_sdn
]["sdn-ports"] = vld_params
[
809 if vld_params
.get("wimAccountId"):
810 target_wim
= "wim:{}".format(vld_params
["wimAccountId"])
811 target_vld
["vim_info"][target_wim
] = {}
812 for param
in ("vim-network-name", "vim-network-id"):
813 if vld_params
.get(param
):
814 if isinstance(vld_params
[param
], dict):
815 for vim
, vim_net
in vld_params
[param
].items():
816 other_target_vim
= "vim:" + vim
818 target_vld
["vim_info"],
819 (other_target_vim
, param
.replace("-", "_")),
822 else: # isinstance str
823 target_vld
["vim_info"][target_vim
][
824 param
.replace("-", "_")
825 ] = vld_params
[param
]
826 if vld_params
.get("common_id"):
827 target_vld
["common_id"] = vld_params
.get("common_id")
829 # modify target["ns"]["vld"] with instantiation parameters to override vnf vim-account
830 def update_ns_vld_target(target
, ns_params
):
831 for vnf_params
in ns_params
.get("vnf", ()):
832 if vnf_params
.get("vimAccountId"):
836 for vnfr
in db_vnfrs
.values()
837 if vnf_params
["member-vnf-index"]
838 == vnfr
["member-vnf-index-ref"]
842 vdur
= next((vdur
for vdur
in target_vnf
.get("vdur", ())), None)
845 for a_index
, a_vld
in enumerate(target
["ns"]["vld"]):
846 target_vld
= find_in_list(
847 get_iterable(vdur
, "interfaces"),
848 lambda iface
: iface
.get("ns-vld-id") == a_vld
["name"],
851 if vnf_params
.get("vimAccountId") not in a_vld
.get(
854 target
["ns"]["vld"][a_index
].get("vim_info").update(
856 "vim:{}".format(vnf_params
["vimAccountId"]): {
857 "vim_network_name": ""
862 nslcmop_id
= db_nslcmop
["_id"]
864 "name": db_nsr
["name"],
867 "image": deepcopy(db_nsr
["image"]),
868 "flavor": deepcopy(db_nsr
["flavor"]),
869 "action_id": nslcmop_id
,
870 "cloud_init_content": {},
872 for image
in target
["image"]:
873 image
["vim_info"] = {}
874 for flavor
in target
["flavor"]:
875 flavor
["vim_info"] = {}
876 if db_nsr
.get("affinity-or-anti-affinity-group"):
877 target
["affinity-or-anti-affinity-group"] = deepcopy(db_nsr
["affinity-or-anti-affinity-group"])
878 for affinity_or_anti_affinity_group
in target
["affinity-or-anti-affinity-group"]:
879 affinity_or_anti_affinity_group
["vim_info"] = {}
881 if db_nslcmop
.get("lcmOperationType") != "instantiate":
882 # get parameters of instantiation:
883 db_nslcmop_instantiate
= self
.db
.get_list(
886 "nsInstanceId": db_nslcmop
["nsInstanceId"],
887 "lcmOperationType": "instantiate",
890 ns_params
= db_nslcmop_instantiate
.get("operationParams")
892 ns_params
= db_nslcmop
.get("operationParams")
893 ssh_keys_instantiation
= ns_params
.get("ssh_keys") or []
894 ssh_keys_all
= ssh_keys_instantiation
+ (n2vc_key_list
or [])
897 for vld_index
, vld
in enumerate(db_nsr
.get("vld")):
898 target_vim
= "vim:{}".format(ns_params
["vimAccountId"])
902 "mgmt-network": vld
.get("mgmt-network", False),
903 "type": vld
.get("type"),
906 "vim_network_name": vld
.get("vim-network-name"),
907 "vim_account_id": ns_params
["vimAccountId"],
911 # check if this network needs SDN assist
912 if vld
.get("pci-interfaces"):
913 db_vim
= get_vim_account(ns_params
["vimAccountId"])
914 if vim_config
:= db_vim
.get("config"):
915 if sdnc_id
:= vim_config
.get("sdn-controller"):
916 sdn_vld
= "nsrs:{}:vld.{}".format(nsr_id
, vld
["id"])
917 target_sdn
= "sdn:{}".format(sdnc_id
)
918 target_vld
["vim_info"][target_sdn
] = {
920 "target_vim": target_vim
,
922 "type": vld
.get("type"),
925 nsd_vnf_profiles
= get_vnf_profiles(nsd
)
926 for nsd_vnf_profile
in nsd_vnf_profiles
:
927 for cp
in nsd_vnf_profile
["virtual-link-connectivity"]:
928 if cp
["virtual-link-profile-id"] == vld
["id"]:
930 "member_vnf:{}.{}".format(
931 cp
["constituent-cpd-id"][0][
932 "constituent-base-element-id"
934 cp
["constituent-cpd-id"][0]["constituent-cpd-id"],
936 ] = "nsrs:{}:vld.{}".format(nsr_id
, vld_index
)
938 # check at nsd descriptor, if there is an ip-profile
940 nsd_vlp
= find_in_list(
941 get_virtual_link_profiles(nsd
),
942 lambda a_link_profile
: a_link_profile
["virtual-link-desc-id"]
947 and nsd_vlp
.get("virtual-link-protocol-data")
948 and nsd_vlp
["virtual-link-protocol-data"].get("l3-protocol-data")
950 ip_profile_source_data
= nsd_vlp
["virtual-link-protocol-data"][
953 ip_profile_dest_data
= {}
954 if "ip-version" in ip_profile_source_data
:
955 ip_profile_dest_data
["ip-version"] = ip_profile_source_data
[
958 if "cidr" in ip_profile_source_data
:
959 ip_profile_dest_data
["subnet-address"] = ip_profile_source_data
[
962 if "gateway-ip" in ip_profile_source_data
:
963 ip_profile_dest_data
["gateway-address"] = ip_profile_source_data
[
966 if "dhcp-enabled" in ip_profile_source_data
:
967 ip_profile_dest_data
["dhcp-params"] = {
968 "enabled": ip_profile_source_data
["dhcp-enabled"]
970 vld_params
["ip-profile"] = ip_profile_dest_data
972 # update vld_params with instantiation params
973 vld_instantiation_params
= find_in_list(
974 get_iterable(ns_params
, "vld"),
975 lambda a_vld
: a_vld
["name"] in (vld
["name"], vld
["id"]),
977 if vld_instantiation_params
:
978 vld_params
.update(vld_instantiation_params
)
979 parse_vld_instantiation_params(target_vim
, target_vld
, vld_params
, None)
980 target
["ns"]["vld"].append(target_vld
)
981 # Update the target ns_vld if vnf vim_account is overriden by instantiation params
982 update_ns_vld_target(target
, ns_params
)
984 for vnfr
in db_vnfrs
.values():
986 db_vnfds
, lambda db_vnf
: db_vnf
["id"] == vnfr
["vnfd-ref"]
988 vnf_params
= find_in_list(
989 get_iterable(ns_params
, "vnf"),
990 lambda a_vnf
: a_vnf
["member-vnf-index"] == vnfr
["member-vnf-index-ref"],
992 target_vnf
= deepcopy(vnfr
)
993 target_vim
= "vim:{}".format(vnfr
["vim-account-id"])
994 for vld
in target_vnf
.get("vld", ()):
995 # check if connected to a ns.vld, to fill target'
996 vnf_cp
= find_in_list(
997 vnfd
.get("int-virtual-link-desc", ()),
998 lambda cpd
: cpd
.get("id") == vld
["id"],
1001 ns_cp
= "member_vnf:{}.{}".format(
1002 vnfr
["member-vnf-index-ref"], vnf_cp
["id"]
1004 if cp2target
.get(ns_cp
):
1005 vld
["target"] = cp2target
[ns_cp
]
1008 target_vim
: {"vim_network_name": vld
.get("vim-network-name")}
1010 # check if this network needs SDN assist
1012 if vld
.get("pci-interfaces"):
1013 db_vim
= get_vim_account(vnfr
["vim-account-id"])
1014 sdnc_id
= db_vim
["config"].get("sdn-controller")
1016 sdn_vld
= "vnfrs:{}:vld.{}".format(target_vnf
["_id"], vld
["id"])
1017 target_sdn
= "sdn:{}".format(sdnc_id
)
1018 vld
["vim_info"][target_sdn
] = {
1020 "target_vim": target_vim
,
1022 "type": vld
.get("type"),
1025 # check at vnfd descriptor, if there is an ip-profile
1027 vnfd_vlp
= find_in_list(
1028 get_virtual_link_profiles(vnfd
),
1029 lambda a_link_profile
: a_link_profile
["id"] == vld
["id"],
1033 and vnfd_vlp
.get("virtual-link-protocol-data")
1034 and vnfd_vlp
["virtual-link-protocol-data"].get("l3-protocol-data")
1036 ip_profile_source_data
= vnfd_vlp
["virtual-link-protocol-data"][
1039 ip_profile_dest_data
= {}
1040 if "ip-version" in ip_profile_source_data
:
1041 ip_profile_dest_data
["ip-version"] = ip_profile_source_data
[
1044 if "cidr" in ip_profile_source_data
:
1045 ip_profile_dest_data
["subnet-address"] = ip_profile_source_data
[
1048 if "gateway-ip" in ip_profile_source_data
:
1049 ip_profile_dest_data
[
1051 ] = ip_profile_source_data
["gateway-ip"]
1052 if "dhcp-enabled" in ip_profile_source_data
:
1053 ip_profile_dest_data
["dhcp-params"] = {
1054 "enabled": ip_profile_source_data
["dhcp-enabled"]
1057 vld_params
["ip-profile"] = ip_profile_dest_data
1058 # update vld_params with instantiation params
1060 vld_instantiation_params
= find_in_list(
1061 get_iterable(vnf_params
, "internal-vld"),
1062 lambda i_vld
: i_vld
["name"] == vld
["id"],
1064 if vld_instantiation_params
:
1065 vld_params
.update(vld_instantiation_params
)
1066 parse_vld_instantiation_params(target_vim
, vld
, vld_params
, target_sdn
)
1069 for vdur
in target_vnf
.get("vdur", ()):
1070 if vdur
.get("status") == "DELETING" or vdur
.get("pdu-type"):
1071 continue # This vdu must not be created
1072 vdur
["vim_info"] = {"vim_account_id": vnfr
["vim-account-id"]}
1074 self
.logger
.debug("NS > ssh_keys > {}".format(ssh_keys_all
))
1077 vdu_configuration
= get_configuration(vnfd
, vdur
["vdu-id-ref"])
1078 vnf_configuration
= get_configuration(vnfd
, vnfd
["id"])
1081 and vdu_configuration
.get("config-access")
1082 and vdu_configuration
.get("config-access").get("ssh-access")
1084 vdur
["ssh-keys"] = ssh_keys_all
1085 vdur
["ssh-access-required"] = vdu_configuration
[
1087 ]["ssh-access"]["required"]
1090 and vnf_configuration
.get("config-access")
1091 and vnf_configuration
.get("config-access").get("ssh-access")
1092 and any(iface
.get("mgmt-vnf") for iface
in vdur
["interfaces"])
1094 vdur
["ssh-keys"] = ssh_keys_all
1095 vdur
["ssh-access-required"] = vnf_configuration
[
1097 ]["ssh-access"]["required"]
1098 elif ssh_keys_instantiation
and find_in_list(
1099 vdur
["interfaces"], lambda iface
: iface
.get("mgmt-vnf")
1101 vdur
["ssh-keys"] = ssh_keys_instantiation
1103 self
.logger
.debug("NS > vdur > {}".format(vdur
))
1105 vdud
= get_vdu(vnfd
, vdur
["vdu-id-ref"])
1107 if vdud
.get("cloud-init-file"):
1108 vdur
["cloud-init"] = "{}:file:{}".format(
1109 vnfd
["_id"], vdud
.get("cloud-init-file")
1111 # read file and put content at target.cloul_init_content. Avoid ng_ro to use shared package system
1112 if vdur
["cloud-init"] not in target
["cloud_init_content"]:
1113 base_folder
= vnfd
["_admin"]["storage"]
1114 cloud_init_file
= "{}/{}/cloud_init/{}".format(
1115 base_folder
["folder"],
1116 base_folder
["pkg-dir"],
1117 vdud
.get("cloud-init-file"),
1119 with self
.fs
.file_open(cloud_init_file
, "r") as ci_file
:
1120 target
["cloud_init_content"][
1123 elif vdud
.get("cloud-init"):
1124 vdur
["cloud-init"] = "{}:vdu:{}".format(
1125 vnfd
["_id"], get_vdu_index(vnfd
, vdur
["vdu-id-ref"])
1127 # put content at target.cloul_init_content. Avoid ng_ro read vnfd descriptor
1128 target
["cloud_init_content"][vdur
["cloud-init"]] = vdud
[
1131 vdur
["additionalParams"] = vdur
.get("additionalParams") or {}
1132 deploy_params_vdu
= self
._format
_additional
_params
(
1133 vdur
.get("additionalParams") or {}
1135 deploy_params_vdu
["OSM"] = get_osm_params(
1136 vnfr
, vdur
["vdu-id-ref"], vdur
["count-index"]
1138 vdur
["additionalParams"] = deploy_params_vdu
1141 ns_flavor
= target
["flavor"][int(vdur
["ns-flavor-id"])]
1142 if target_vim
not in ns_flavor
["vim_info"]:
1143 ns_flavor
["vim_info"][target_vim
] = {}
1146 # in case alternative images are provided we must check if they should be applied
1147 # for the vim_type, modify the vim_type taking into account
1148 ns_image_id
= int(vdur
["ns-image-id"])
1149 if vdur
.get("alt-image-ids"):
1150 db_vim
= get_vim_account(vnfr
["vim-account-id"])
1151 vim_type
= db_vim
["vim_type"]
1152 for alt_image_id
in vdur
.get("alt-image-ids"):
1153 ns_alt_image
= target
["image"][int(alt_image_id
)]
1154 if vim_type
== ns_alt_image
.get("vim-type"):
1155 # must use alternative image
1157 "use alternative image id: {}".format(alt_image_id
)
1159 ns_image_id
= alt_image_id
1160 vdur
["ns-image-id"] = ns_image_id
1162 ns_image
= target
["image"][int(ns_image_id
)]
1163 if target_vim
not in ns_image
["vim_info"]:
1164 ns_image
["vim_info"][target_vim
] = {}
1167 if vdur
.get("affinity-or-anti-affinity-group-id"):
1168 for ags_id
in vdur
["affinity-or-anti-affinity-group-id"]:
1169 ns_ags
= target
["affinity-or-anti-affinity-group"][int(ags_id
)]
1170 if target_vim
not in ns_ags
["vim_info"]:
1171 ns_ags
["vim_info"][target_vim
] = {}
1173 vdur
["vim_info"] = {target_vim
: {}}
1174 # instantiation parameters
1176 # vdu_instantiation_params = next((v for v in get_iterable(vnf_params, "vdu") if v["id"] ==
1177 # vdud["id"]), None)
1178 vdur_list
.append(vdur
)
1179 target_vnf
["vdur"] = vdur_list
1180 target
["vnf"].append(target_vnf
)
1182 desc
= await self
.RO
.deploy(nsr_id
, target
)
1183 self
.logger
.debug("RO return > {}".format(desc
))
1184 action_id
= desc
["action_id"]
1185 await self
._wait
_ng
_ro
(
1186 nsr_id
, action_id
, nslcmop_id
, start_deploy
, timeout_ns_deploy
, stage
1191 "_admin.deployed.RO.operational-status": "running",
1192 "detailed-status": " ".join(stage
),
1194 # db_nsr["_admin.deployed.RO.detailed-status"] = "Deployed at VIM"
1195 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
1196 self
._write
_op
_status
(nslcmop_id
, stage
)
1198 logging_text
+ "ns deployed at RO. RO_id={}".format(action_id
)
1202 async def _wait_ng_ro(
1211 detailed_status_old
= None
1213 start_time
= start_time
or time()
1214 while time() <= start_time
+ timeout
:
1215 desc_status
= await self
.RO
.status(nsr_id
, action_id
)
1216 self
.logger
.debug("Wait NG RO > {}".format(desc_status
))
1217 if desc_status
["status"] == "FAILED":
1218 raise NgRoException(desc_status
["details"])
1219 elif desc_status
["status"] == "BUILD":
1221 stage
[2] = "VIM: ({})".format(desc_status
["details"])
1222 elif desc_status
["status"] == "DONE":
1224 stage
[2] = "Deployed at VIM"
1227 assert False, "ROclient.check_ns_status returns unknown {}".format(
1228 desc_status
["status"]
1230 if stage
and nslcmop_id
and stage
[2] != detailed_status_old
:
1231 detailed_status_old
= stage
[2]
1232 db_nsr_update
["detailed-status"] = " ".join(stage
)
1233 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
1234 self
._write
_op
_status
(nslcmop_id
, stage
)
1235 await asyncio
.sleep(15, loop
=self
.loop
)
1236 else: # timeout_ns_deploy
1237 raise NgRoException("Timeout waiting ns to deploy")
1239 async def _terminate_ng_ro(
1240 self
, logging_text
, nsr_deployed
, nsr_id
, nslcmop_id
, stage
1245 start_deploy
= time()
1252 "action_id": nslcmop_id
,
1254 desc
= await self
.RO
.deploy(nsr_id
, target
)
1255 action_id
= desc
["action_id"]
1256 db_nsr_update
["_admin.deployed.RO.nsr_delete_action_id"] = action_id
1257 db_nsr_update
["_admin.deployed.RO.nsr_status"] = "DELETING"
1260 + "ns terminate action at RO. action_id={}".format(action_id
)
1264 delete_timeout
= 20 * 60 # 20 minutes
1265 await self
._wait
_ng
_ro
(
1266 nsr_id
, action_id
, nslcmop_id
, start_deploy
, delete_timeout
, stage
1269 db_nsr_update
["_admin.deployed.RO.nsr_delete_action_id"] = None
1270 db_nsr_update
["_admin.deployed.RO.nsr_status"] = "DELETED"
1272 await self
.RO
.delete(nsr_id
)
1273 except Exception as e
:
1274 if isinstance(e
, NgRoException
) and e
.http_code
== 404: # not found
1275 db_nsr_update
["_admin.deployed.RO.nsr_id"] = None
1276 db_nsr_update
["_admin.deployed.RO.nsr_status"] = "DELETED"
1277 db_nsr_update
["_admin.deployed.RO.nsr_delete_action_id"] = None
1279 logging_text
+ "RO_action_id={} already deleted".format(action_id
)
1281 elif isinstance(e
, NgRoException
) and e
.http_code
== 409: # conflict
1282 failed_detail
.append("delete conflict: {}".format(e
))
1285 + "RO_action_id={} delete conflict: {}".format(action_id
, e
)
1288 failed_detail
.append("delete error: {}".format(e
))
1291 + "RO_action_id={} delete error: {}".format(action_id
, e
)
1295 stage
[2] = "Error deleting from VIM"
1297 stage
[2] = "Deleted from VIM"
1298 db_nsr_update
["detailed-status"] = " ".join(stage
)
1299 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
1300 self
._write
_op
_status
(nslcmop_id
, stage
)
1303 raise LcmException("; ".join(failed_detail
))
1306 async def instantiate_RO(
1320 :param logging_text: preffix text to use at logging
1321 :param nsr_id: nsr identity
1322 :param nsd: database content of ns descriptor
1323 :param db_nsr: database content of ns record
1324 :param db_nslcmop: database content of ns operation, in this case, 'instantiate'
1326 :param db_vnfds: database content of vnfds, indexed by id (not _id). {id: {vnfd_object}, ...}
1327 :param n2vc_key_list: ssh-public-key list to be inserted to management vdus via cloud-init
1328 :param stage: list with 3 items: [general stage, tasks, vim_specific]. This task will write over vim_specific
1329 :return: None or exception
1332 start_deploy
= time()
1333 ns_params
= db_nslcmop
.get("operationParams")
1334 if ns_params
and ns_params
.get("timeout_ns_deploy"):
1335 timeout_ns_deploy
= ns_params
["timeout_ns_deploy"]
1337 timeout_ns_deploy
= self
.timeout
.get(
1338 "ns_deploy", self
.timeout_ns_deploy
1341 # Check for and optionally request placement optimization. Database will be updated if placement activated
1342 stage
[2] = "Waiting for Placement."
1343 if await self
._do
_placement
(logging_text
, db_nslcmop
, db_vnfrs
):
1344 # in case of placement change ns_params[vimAcountId) if not present at any vnfrs
1345 for vnfr
in db_vnfrs
.values():
1346 if ns_params
["vimAccountId"] == vnfr
["vim-account-id"]:
1349 ns_params
["vimAccountId"] == vnfr
["vim-account-id"]
1351 return await self
._instantiate
_ng
_ro
(
1364 except Exception as e
:
1365 stage
[2] = "ERROR deploying at VIM"
1366 self
.set_vnfr_at_error(db_vnfrs
, str(e
))
1368 "Error deploying at VIM {}".format(e
),
1369 exc_info
=not isinstance(
1372 ROclient
.ROClientException
,
1381 async def wait_kdu_up(self
, logging_text
, nsr_id
, vnfr_id
, kdu_name
):
1383 Wait for kdu to be up, get ip address
1384 :param logging_text: prefix use for logging
1391 # self.logger.debug(logging_text + "Starting wait_kdu_up")
1394 while nb_tries
< 360:
1395 db_vnfr
= self
.db
.get_one("vnfrs", {"_id": vnfr_id
})
1399 for x
in get_iterable(db_vnfr
, "kdur")
1400 if x
.get("kdu-name") == kdu_name
1406 "Not found vnfr_id={}, kdu_name={}".format(vnfr_id
, kdu_name
)
1408 if kdur
.get("status"):
1409 if kdur
["status"] in ("READY", "ENABLED"):
1410 return kdur
.get("ip-address")
1413 "target KDU={} is in error state".format(kdu_name
)
1416 await asyncio
.sleep(10, loop
=self
.loop
)
1418 raise LcmException("Timeout waiting KDU={} instantiated".format(kdu_name
))
1420 async def wait_vm_up_insert_key_ro(
1421 self
, logging_text
, nsr_id
, vnfr_id
, vdu_id
, vdu_index
, pub_key
=None, user
=None
1424 Wait for ip addres at RO, and optionally, insert public key in virtual machine
1425 :param logging_text: prefix use for logging
1430 :param pub_key: public ssh key to inject, None to skip
1431 :param user: user to apply the public ssh key
1435 self
.logger
.debug(logging_text
+ "Starting wait_vm_up_insert_key_ro")
1439 target_vdu_id
= None
1445 if ro_retries
>= 360: # 1 hour
1447 "Not found _admin.deployed.RO.nsr_id for nsr_id: {}".format(nsr_id
)
1450 await asyncio
.sleep(10, loop
=self
.loop
)
1453 if not target_vdu_id
:
1454 db_vnfr
= self
.db
.get_one("vnfrs", {"_id": vnfr_id
})
1456 if not vdu_id
: # for the VNF case
1457 if db_vnfr
.get("status") == "ERROR":
1459 "Cannot inject ssh-key because target VNF is in error state"
1461 ip_address
= db_vnfr
.get("ip-address")
1467 for x
in get_iterable(db_vnfr
, "vdur")
1468 if x
.get("ip-address") == ip_address
1476 for x
in get_iterable(db_vnfr
, "vdur")
1477 if x
.get("vdu-id-ref") == vdu_id
1478 and x
.get("count-index") == vdu_index
1484 not vdur
and len(db_vnfr
.get("vdur", ())) == 1
1485 ): # If only one, this should be the target vdu
1486 vdur
= db_vnfr
["vdur"][0]
1489 "Not found vnfr_id={}, vdu_id={}, vdu_index={}".format(
1490 vnfr_id
, vdu_id
, vdu_index
1493 # New generation RO stores information at "vim_info"
1496 if vdur
.get("vim_info"):
1498 t
for t
in vdur
["vim_info"]
1499 ) # there should be only one key
1500 ng_ro_status
= vdur
["vim_info"][target_vim
].get("vim_status")
1502 vdur
.get("pdu-type")
1503 or vdur
.get("status") == "ACTIVE"
1504 or ng_ro_status
== "ACTIVE"
1506 ip_address
= vdur
.get("ip-address")
1509 target_vdu_id
= vdur
["vdu-id-ref"]
1510 elif vdur
.get("status") == "ERROR" or ng_ro_status
== "ERROR":
1512 "Cannot inject ssh-key because target VM is in error state"
1515 if not target_vdu_id
:
1518 # inject public key into machine
1519 if pub_key
and user
:
1520 self
.logger
.debug(logging_text
+ "Inserting RO key")
1521 self
.logger
.debug("SSH > PubKey > {}".format(pub_key
))
1522 if vdur
.get("pdu-type"):
1523 self
.logger
.error(logging_text
+ "Cannot inject ssh-ky to a PDU")
1526 ro_vm_id
= "{}-{}".format(
1527 db_vnfr
["member-vnf-index-ref"], target_vdu_id
1528 ) # TODO add vdu_index
1532 "action": "inject_ssh_key",
1536 "vnf": [{"_id": vnfr_id
, "vdur": [{"id": vdur
["id"]}]}],
1538 desc
= await self
.RO
.deploy(nsr_id
, target
)
1539 action_id
= desc
["action_id"]
1540 await self
._wait
_ng
_ro
(nsr_id
, action_id
, timeout
=600)
1543 # wait until NS is deployed at RO
1545 db_nsrs
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
1546 ro_nsr_id
= deep_get(
1547 db_nsrs
, ("_admin", "deployed", "RO", "nsr_id")
1551 result_dict
= await self
.RO
.create_action(
1553 item_id_name
=ro_nsr_id
,
1555 "add_public_key": pub_key
,
1560 # result_dict contains the format {VM-id: {vim_result: 200, description: text}}
1561 if not result_dict
or not isinstance(result_dict
, dict):
1563 "Unknown response from RO when injecting key"
1565 for result
in result_dict
.values():
1566 if result
.get("vim_result") == 200:
1569 raise ROclient
.ROClientException(
1570 "error injecting key: {}".format(
1571 result
.get("description")
1575 except NgRoException
as e
:
1577 "Reaching max tries injecting key. Error: {}".format(e
)
1579 except ROclient
.ROClientException
as e
:
1583 + "error injecting key: {}. Retrying until {} seconds".format(
1590 "Reaching max tries injecting key. Error: {}".format(e
)
1597 async def _wait_dependent_n2vc(self
, nsr_id
, vca_deployed_list
, vca_index
):
1599 Wait until dependent VCA deployments have been finished. NS wait for VNFs and VDUs. VNFs for VDUs
1601 my_vca
= vca_deployed_list
[vca_index
]
1602 if my_vca
.get("vdu_id") or my_vca
.get("kdu_name"):
1603 # vdu or kdu: no dependencies
1607 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
1608 vca_deployed_list
= db_nsr
["_admin"]["deployed"]["VCA"]
1609 configuration_status_list
= db_nsr
["configurationStatus"]
1610 for index
, vca_deployed
in enumerate(configuration_status_list
):
1611 if index
== vca_index
:
1614 if not my_vca
.get("member-vnf-index") or (
1615 vca_deployed
.get("member-vnf-index")
1616 == my_vca
.get("member-vnf-index")
1618 internal_status
= configuration_status_list
[index
].get("status")
1619 if internal_status
== "READY":
1621 elif internal_status
== "BROKEN":
1623 "Configuration aborted because dependent charm/s has failed"
1628 # no dependencies, return
1630 await asyncio
.sleep(10)
1633 raise LcmException("Configuration aborted because dependent charm/s timeout")
1635 def get_vca_id(self
, db_vnfr
: dict, db_nsr
: dict):
1638 vca_id
= deep_get(db_vnfr
, ("vca-id",))
1640 vim_account_id
= deep_get(db_nsr
, ("instantiate_params", "vimAccountId"))
1641 vca_id
= VimAccountDB
.get_vim_account_with_id(vim_account_id
).get("vca")
1644 async def instantiate_N2VC(
1661 ee_config_descriptor
,
1663 nsr_id
= db_nsr
["_id"]
1664 db_update_entry
= "_admin.deployed.VCA.{}.".format(vca_index
)
1665 vca_deployed_list
= db_nsr
["_admin"]["deployed"]["VCA"]
1666 vca_deployed
= db_nsr
["_admin"]["deployed"]["VCA"][vca_index
]
1667 osm_config
= {"osm": {"ns_id": db_nsr
["_id"]}}
1669 "collection": "nsrs",
1670 "filter": {"_id": nsr_id
},
1671 "path": db_update_entry
,
1677 element_under_configuration
= nsr_id
1681 vnfr_id
= db_vnfr
["_id"]
1682 osm_config
["osm"]["vnf_id"] = vnfr_id
1684 namespace
= "{nsi}.{ns}".format(nsi
=nsi_id
if nsi_id
else "", ns
=nsr_id
)
1686 if vca_type
== "native_charm":
1689 index_number
= vdu_index
or 0
1692 element_type
= "VNF"
1693 element_under_configuration
= vnfr_id
1694 namespace
+= ".{}-{}".format(vnfr_id
, index_number
)
1696 namespace
+= ".{}-{}".format(vdu_id
, index_number
)
1697 element_type
= "VDU"
1698 element_under_configuration
= "{}-{}".format(vdu_id
, index_number
)
1699 osm_config
["osm"]["vdu_id"] = vdu_id
1701 namespace
+= ".{}".format(kdu_name
)
1702 element_type
= "KDU"
1703 element_under_configuration
= kdu_name
1704 osm_config
["osm"]["kdu_name"] = kdu_name
1707 artifact_path
= "{}/{}/{}/{}".format(
1708 base_folder
["folder"],
1709 base_folder
["pkg-dir"],
1711 if vca_type
in ("native_charm", "lxc_proxy_charm", "k8s_proxy_charm")
1716 self
.logger
.debug("Artifact path > {}".format(artifact_path
))
1718 # get initial_config_primitive_list that applies to this element
1719 initial_config_primitive_list
= config_descriptor
.get(
1720 "initial-config-primitive"
1724 "Initial config primitive list > {}".format(
1725 initial_config_primitive_list
1729 # add config if not present for NS charm
1730 ee_descriptor_id
= ee_config_descriptor
.get("id")
1731 self
.logger
.debug("EE Descriptor > {}".format(ee_descriptor_id
))
1732 initial_config_primitive_list
= get_ee_sorted_initial_config_primitive_list(
1733 initial_config_primitive_list
, vca_deployed
, ee_descriptor_id
1737 "Initial config primitive list #2 > {}".format(
1738 initial_config_primitive_list
1741 # n2vc_redesign STEP 3.1
1742 # find old ee_id if exists
1743 ee_id
= vca_deployed
.get("ee_id")
1745 vca_id
= self
.get_vca_id(db_vnfr
, db_nsr
)
1746 # create or register execution environment in VCA
1747 if vca_type
in ("lxc_proxy_charm", "k8s_proxy_charm", "helm", "helm-v3"):
1749 self
._write
_configuration
_status
(
1751 vca_index
=vca_index
,
1753 element_under_configuration
=element_under_configuration
,
1754 element_type
=element_type
,
1757 step
= "create execution environment"
1758 self
.logger
.debug(logging_text
+ step
)
1762 if vca_type
== "k8s_proxy_charm":
1763 ee_id
= await self
.vca_map
[vca_type
].install_k8s_proxy_charm(
1764 charm_name
=artifact_path
[artifact_path
.rfind("/") + 1 :],
1765 namespace
=namespace
,
1766 artifact_path
=artifact_path
,
1770 elif vca_type
== "helm" or vca_type
== "helm-v3":
1771 ee_id
, credentials
= await self
.vca_map
[
1773 ].create_execution_environment(
1774 namespace
=namespace
,
1778 artifact_path
=artifact_path
,
1782 ee_id
, credentials
= await self
.vca_map
[
1784 ].create_execution_environment(
1785 namespace
=namespace
,
1791 elif vca_type
== "native_charm":
1792 step
= "Waiting to VM being up and getting IP address"
1793 self
.logger
.debug(logging_text
+ step
)
1794 rw_mgmt_ip
= await self
.wait_vm_up_insert_key_ro(
1803 credentials
= {"hostname": rw_mgmt_ip
}
1805 username
= deep_get(
1806 config_descriptor
, ("config-access", "ssh-access", "default-user")
1808 # TODO remove this when changes on IM regarding config-access:ssh-access:default-user were
1809 # merged. Meanwhile let's get username from initial-config-primitive
1810 if not username
and initial_config_primitive_list
:
1811 for config_primitive
in initial_config_primitive_list
:
1812 for param
in config_primitive
.get("parameter", ()):
1813 if param
["name"] == "ssh-username":
1814 username
= param
["value"]
1818 "Cannot determine the username neither with 'initial-config-primitive' nor with "
1819 "'config-access.ssh-access.default-user'"
1821 credentials
["username"] = username
1822 # n2vc_redesign STEP 3.2
1824 self
._write
_configuration
_status
(
1826 vca_index
=vca_index
,
1827 status
="REGISTERING",
1828 element_under_configuration
=element_under_configuration
,
1829 element_type
=element_type
,
1832 step
= "register execution environment {}".format(credentials
)
1833 self
.logger
.debug(logging_text
+ step
)
1834 ee_id
= await self
.vca_map
[vca_type
].register_execution_environment(
1835 credentials
=credentials
,
1836 namespace
=namespace
,
1841 # for compatibility with MON/POL modules, the need model and application name at database
1842 # TODO ask MON/POL if needed to not assuming anymore the format "model_name.application_name"
1843 ee_id_parts
= ee_id
.split(".")
1844 db_nsr_update
= {db_update_entry
+ "ee_id": ee_id
}
1845 if len(ee_id_parts
) >= 2:
1846 model_name
= ee_id_parts
[0]
1847 application_name
= ee_id_parts
[1]
1848 db_nsr_update
[db_update_entry
+ "model"] = model_name
1849 db_nsr_update
[db_update_entry
+ "application"] = application_name
1851 # n2vc_redesign STEP 3.3
1852 step
= "Install configuration Software"
1854 self
._write
_configuration
_status
(
1856 vca_index
=vca_index
,
1857 status
="INSTALLING SW",
1858 element_under_configuration
=element_under_configuration
,
1859 element_type
=element_type
,
1860 other_update
=db_nsr_update
,
1863 # TODO check if already done
1864 self
.logger
.debug(logging_text
+ step
)
1866 if vca_type
== "native_charm":
1867 config_primitive
= next(
1868 (p
for p
in initial_config_primitive_list
if p
["name"] == "config"),
1871 if config_primitive
:
1872 config
= self
._map
_primitive
_params
(
1873 config_primitive
, {}, deploy_params
1876 if vca_type
== "lxc_proxy_charm":
1877 if element_type
== "NS":
1878 num_units
= db_nsr
.get("config-units") or 1
1879 elif element_type
== "VNF":
1880 num_units
= db_vnfr
.get("config-units") or 1
1881 elif element_type
== "VDU":
1882 for v
in db_vnfr
["vdur"]:
1883 if vdu_id
== v
["vdu-id-ref"]:
1884 num_units
= v
.get("config-units") or 1
1886 if vca_type
!= "k8s_proxy_charm":
1887 await self
.vca_map
[vca_type
].install_configuration_sw(
1889 artifact_path
=artifact_path
,
1892 num_units
=num_units
,
1897 # write in db flag of configuration_sw already installed
1899 "nsrs", nsr_id
, {db_update_entry
+ "config_sw_installed": True}
1902 # add relations for this VCA (wait for other peers related with this VCA)
1903 await self
._add
_vca
_relations
(
1904 logging_text
=logging_text
,
1906 vca_index
=vca_index
,
1911 # if SSH access is required, then get execution environment SSH public
1912 # if native charm we have waited already to VM be UP
1913 if vca_type
in ("k8s_proxy_charm", "lxc_proxy_charm", "helm", "helm-v3"):
1916 # self.logger.debug("get ssh key block")
1918 config_descriptor
, ("config-access", "ssh-access", "required")
1920 # self.logger.debug("ssh key needed")
1921 # Needed to inject a ssh key
1924 ("config-access", "ssh-access", "default-user"),
1926 step
= "Install configuration Software, getting public ssh key"
1927 pub_key
= await self
.vca_map
[vca_type
].get_ee_ssh_public__key(
1928 ee_id
=ee_id
, db_dict
=db_dict
, vca_id
=vca_id
1931 step
= "Insert public key into VM user={} ssh_key={}".format(
1935 # self.logger.debug("no need to get ssh key")
1936 step
= "Waiting to VM being up and getting IP address"
1937 self
.logger
.debug(logging_text
+ step
)
1939 # default rw_mgmt_ip to None, avoiding the non definition of the variable
1942 # n2vc_redesign STEP 5.1
1943 # wait for RO (ip-address) Insert pub_key into VM
1946 rw_mgmt_ip
= await self
.wait_kdu_up(
1947 logging_text
, nsr_id
, vnfr_id
, kdu_name
1950 # This verification is needed in order to avoid trying to add a public key
1951 # to a VM, when the VNF is a KNF (in the edge case where the user creates a VCA
1952 # for a KNF and not for its KDUs, the previous verification gives False, and the code
1953 # jumps to this block, meaning that there is the need to verify if the VNF is actually a VNF
1955 elif db_vnfr
.get('vdur'):
1956 rw_mgmt_ip
= await self
.wait_vm_up_insert_key_ro(
1966 self
.logger
.debug(logging_text
+ " VM_ip_address={}".format(rw_mgmt_ip
))
1968 # store rw_mgmt_ip in deploy params for later replacement
1969 deploy_params
["rw_mgmt_ip"] = rw_mgmt_ip
1971 # n2vc_redesign STEP 6 Execute initial config primitive
1972 step
= "execute initial config primitive"
1974 # wait for dependent primitives execution (NS -> VNF -> VDU)
1975 if initial_config_primitive_list
:
1976 await self
._wait
_dependent
_n
2vc
(nsr_id
, vca_deployed_list
, vca_index
)
1978 # stage, in function of element type: vdu, kdu, vnf or ns
1979 my_vca
= vca_deployed_list
[vca_index
]
1980 if my_vca
.get("vdu_id") or my_vca
.get("kdu_name"):
1982 stage
[0] = "Stage 3/5: running Day-1 primitives for VDU."
1983 elif my_vca
.get("member-vnf-index"):
1985 stage
[0] = "Stage 4/5: running Day-1 primitives for VNF."
1988 stage
[0] = "Stage 5/5: running Day-1 primitives for NS."
1990 self
._write
_configuration
_status
(
1991 nsr_id
=nsr_id
, vca_index
=vca_index
, status
="EXECUTING PRIMITIVE"
1994 self
._write
_op
_status
(op_id
=nslcmop_id
, stage
=stage
)
1996 check_if_terminated_needed
= True
1997 for initial_config_primitive
in initial_config_primitive_list
:
1998 # adding information on the vca_deployed if it is a NS execution environment
1999 if not vca_deployed
["member-vnf-index"]:
2000 deploy_params
["ns_config_info"] = json
.dumps(
2001 self
._get
_ns
_config
_info
(nsr_id
)
2003 # TODO check if already done
2004 primitive_params_
= self
._map
_primitive
_params
(
2005 initial_config_primitive
, {}, deploy_params
2008 step
= "execute primitive '{}' params '{}'".format(
2009 initial_config_primitive
["name"], primitive_params_
2011 self
.logger
.debug(logging_text
+ step
)
2012 await self
.vca_map
[vca_type
].exec_primitive(
2014 primitive_name
=initial_config_primitive
["name"],
2015 params_dict
=primitive_params_
,
2020 # Once some primitive has been exec, check and write at db if it needs to exec terminated primitives
2021 if check_if_terminated_needed
:
2022 if config_descriptor
.get("terminate-config-primitive"):
2024 "nsrs", nsr_id
, {db_update_entry
+ "needed_terminate": True}
2026 check_if_terminated_needed
= False
2028 # TODO register in database that primitive is done
2030 # STEP 7 Configure metrics
2031 if vca_type
== "helm" or vca_type
== "helm-v3":
2032 prometheus_jobs
= await self
.add_prometheus_metrics(
2034 artifact_path
=artifact_path
,
2035 ee_config_descriptor
=ee_config_descriptor
,
2038 target_ip
=rw_mgmt_ip
,
2044 {db_update_entry
+ "prometheus_jobs": prometheus_jobs
},
2047 step
= "instantiated at VCA"
2048 self
.logger
.debug(logging_text
+ step
)
2050 self
._write
_configuration
_status
(
2051 nsr_id
=nsr_id
, vca_index
=vca_index
, status
="READY"
2054 except Exception as e
: # TODO not use Exception but N2VC exception
2055 # self.update_db_2("nsrs", nsr_id, {db_update_entry + "instantiation": "FAILED"})
2057 e
, (DbException
, N2VCException
, LcmException
, asyncio
.CancelledError
)
2060 "Exception while {} : {}".format(step
, e
), exc_info
=True
2062 self
._write
_configuration
_status
(
2063 nsr_id
=nsr_id
, vca_index
=vca_index
, status
="BROKEN"
2065 raise LcmException("{} {}".format(step
, e
)) from e
2067 def _write_ns_status(
2071 current_operation
: str,
2072 current_operation_id
: str,
2073 error_description
: str = None,
2074 error_detail
: str = None,
2075 other_update
: dict = None,
2078 Update db_nsr fields.
2081 :param current_operation:
2082 :param current_operation_id:
2083 :param error_description:
2084 :param error_detail:
2085 :param other_update: Other required changes at database if provided, will be cleared
2089 db_dict
= other_update
or {}
2092 ] = current_operation_id
# for backward compatibility
2093 db_dict
["_admin.current-operation"] = current_operation_id
2094 db_dict
["_admin.operation-type"] = (
2095 current_operation
if current_operation
!= "IDLE" else None
2097 db_dict
["currentOperation"] = current_operation
2098 db_dict
["currentOperationID"] = current_operation_id
2099 db_dict
["errorDescription"] = error_description
2100 db_dict
["errorDetail"] = error_detail
2103 db_dict
["nsState"] = ns_state
2104 self
.update_db_2("nsrs", nsr_id
, db_dict
)
2105 except DbException
as e
:
2106 self
.logger
.warn("Error writing NS status, ns={}: {}".format(nsr_id
, e
))
2108 def _write_op_status(
2112 error_message
: str = None,
2113 queuePosition
: int = 0,
2114 operation_state
: str = None,
2115 other_update
: dict = None,
2118 db_dict
= other_update
or {}
2119 db_dict
["queuePosition"] = queuePosition
2120 if isinstance(stage
, list):
2121 db_dict
["stage"] = stage
[0]
2122 db_dict
["detailed-status"] = " ".join(stage
)
2123 elif stage
is not None:
2124 db_dict
["stage"] = str(stage
)
2126 if error_message
is not None:
2127 db_dict
["errorMessage"] = error_message
2128 if operation_state
is not None:
2129 db_dict
["operationState"] = operation_state
2130 db_dict
["statusEnteredTime"] = time()
2131 self
.update_db_2("nslcmops", op_id
, db_dict
)
2132 except DbException
as e
:
2134 "Error writing OPERATION status for op_id: {} -> {}".format(op_id
, e
)
2137 def _write_all_config_status(self
, db_nsr
: dict, status
: str):
2139 nsr_id
= db_nsr
["_id"]
2140 # configurationStatus
2141 config_status
= db_nsr
.get("configurationStatus")
2144 "configurationStatus.{}.status".format(index
): status
2145 for index
, v
in enumerate(config_status
)
2149 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
2151 except DbException
as e
:
2153 "Error writing all configuration status, ns={}: {}".format(nsr_id
, e
)
2156 def _write_configuration_status(
2161 element_under_configuration
: str = None,
2162 element_type
: str = None,
2163 other_update
: dict = None,
2166 # self.logger.debug('_write_configuration_status(): vca_index={}, status={}'
2167 # .format(vca_index, status))
2170 db_path
= "configurationStatus.{}.".format(vca_index
)
2171 db_dict
= other_update
or {}
2173 db_dict
[db_path
+ "status"] = status
2174 if element_under_configuration
:
2176 db_path
+ "elementUnderConfiguration"
2177 ] = element_under_configuration
2179 db_dict
[db_path
+ "elementType"] = element_type
2180 self
.update_db_2("nsrs", nsr_id
, db_dict
)
2181 except DbException
as e
:
2183 "Error writing configuration status={}, ns={}, vca_index={}: {}".format(
2184 status
, nsr_id
, vca_index
, e
2188 async def _do_placement(self
, logging_text
, db_nslcmop
, db_vnfrs
):
2190 Check and computes the placement, (vim account where to deploy). If it is decided by an external tool, it
2191 sends the request via kafka and wait until the result is wrote at database (nslcmops _admin.plca).
2192 Database is used because the result can be obtained from a different LCM worker in case of HA.
2193 :param logging_text: contains the prefix for logging, with the ns and nslcmop identifiers
2194 :param db_nslcmop: database content of nslcmop
2195 :param db_vnfrs: database content of vnfrs, indexed by member-vnf-index.
2196 :return: True if some modification is done. Modifies database vnfrs and parameter db_vnfr with the
2197 computed 'vim-account-id'
2200 nslcmop_id
= db_nslcmop
["_id"]
2201 placement_engine
= deep_get(db_nslcmop
, ("operationParams", "placement-engine"))
2202 if placement_engine
== "PLA":
2204 logging_text
+ "Invoke and wait for placement optimization"
2206 await self
.msg
.aiowrite(
2207 "pla", "get_placement", {"nslcmopId": nslcmop_id
}, loop
=self
.loop
2209 db_poll_interval
= 5
2210 wait
= db_poll_interval
* 10
2212 while not pla_result
and wait
>= 0:
2213 await asyncio
.sleep(db_poll_interval
)
2214 wait
-= db_poll_interval
2215 db_nslcmop
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
})
2216 pla_result
= deep_get(db_nslcmop
, ("_admin", "pla"))
2220 "Placement timeout for nslcmopId={}".format(nslcmop_id
)
2223 for pla_vnf
in pla_result
["vnf"]:
2224 vnfr
= db_vnfrs
.get(pla_vnf
["member-vnf-index"])
2225 if not pla_vnf
.get("vimAccountId") or not vnfr
:
2230 {"_id": vnfr
["_id"]},
2231 {"vim-account-id": pla_vnf
["vimAccountId"]},
2234 vnfr
["vim-account-id"] = pla_vnf
["vimAccountId"]
2237 def update_nsrs_with_pla_result(self
, params
):
2239 nslcmop_id
= deep_get(params
, ("placement", "nslcmopId"))
2241 "nslcmops", nslcmop_id
, {"_admin.pla": params
.get("placement")}
2243 except Exception as e
:
2244 self
.logger
.warn("Update failed for nslcmop_id={}:{}".format(nslcmop_id
, e
))
2246 async def instantiate(self
, nsr_id
, nslcmop_id
):
2249 :param nsr_id: ns instance to deploy
2250 :param nslcmop_id: operation to run
2254 # Try to lock HA task here
2255 task_is_locked_by_me
= self
.lcm_tasks
.lock_HA("ns", "nslcmops", nslcmop_id
)
2256 if not task_is_locked_by_me
:
2258 "instantiate() task is not locked by me, ns={}".format(nsr_id
)
2262 logging_text
= "Task ns={} instantiate={} ".format(nsr_id
, nslcmop_id
)
2263 self
.logger
.debug(logging_text
+ "Enter")
2265 # get all needed from database
2267 # database nsrs record
2270 # database nslcmops record
2273 # update operation on nsrs
2275 # update operation on nslcmops
2276 db_nslcmop_update
= {}
2278 nslcmop_operation_state
= None
2279 db_vnfrs
= {} # vnf's info indexed by member-index
2281 tasks_dict_info
= {} # from task to info text
2285 "Stage 1/5: preparation of the environment.",
2286 "Waiting for previous operations to terminate.",
2289 # ^ stage, step, VIM progress
2291 # wait for any previous tasks in process
2292 await self
.lcm_tasks
.waitfor_related_HA("ns", "nslcmops", nslcmop_id
)
2294 # STEP 0: Reading database (nslcmops, nsrs, nsds, vnfrs, vnfds)
2295 stage
[1] = "Reading from database."
2296 # nsState="BUILDING", currentOperation="INSTANTIATING", currentOperationID=nslcmop_id
2297 db_nsr_update
["detailed-status"] = "creating"
2298 db_nsr_update
["operational-status"] = "init"
2299 self
._write
_ns
_status
(
2301 ns_state
="BUILDING",
2302 current_operation
="INSTANTIATING",
2303 current_operation_id
=nslcmop_id
,
2304 other_update
=db_nsr_update
,
2306 self
._write
_op
_status
(op_id
=nslcmop_id
, stage
=stage
, queuePosition
=0)
2308 # read from db: operation
2309 stage
[1] = "Getting nslcmop={} from db.".format(nslcmop_id
)
2310 db_nslcmop
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
})
2311 if db_nslcmop
["operationParams"].get("additionalParamsForVnf"):
2312 db_nslcmop
["operationParams"]["additionalParamsForVnf"] = json
.loads(
2313 db_nslcmop
["operationParams"]["additionalParamsForVnf"]
2315 ns_params
= db_nslcmop
.get("operationParams")
2316 if ns_params
and ns_params
.get("timeout_ns_deploy"):
2317 timeout_ns_deploy
= ns_params
["timeout_ns_deploy"]
2319 timeout_ns_deploy
= self
.timeout
.get(
2320 "ns_deploy", self
.timeout_ns_deploy
2324 stage
[1] = "Getting nsr={} from db.".format(nsr_id
)
2325 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
2326 stage
[1] = "Getting nsd={} from db.".format(db_nsr
["nsd-id"])
2327 nsd
= self
.db
.get_one("nsds", {"_id": db_nsr
["nsd-id"]})
2328 self
.fs
.sync(db_nsr
["nsd-id"])
2330 # nsr_name = db_nsr["name"] # TODO short-name??
2332 # read from db: vnf's of this ns
2333 stage
[1] = "Getting vnfrs from db."
2334 self
.logger
.debug(logging_text
+ stage
[1])
2335 db_vnfrs_list
= self
.db
.get_list("vnfrs", {"nsr-id-ref": nsr_id
})
2337 # read from db: vnfd's for every vnf
2338 db_vnfds
= [] # every vnfd data
2340 # for each vnf in ns, read vnfd
2341 for vnfr
in db_vnfrs_list
:
2342 if vnfr
.get("kdur"):
2344 for kdur
in vnfr
["kdur"]:
2345 if kdur
.get("additionalParams"):
2346 kdur
["additionalParams"] = json
.loads(
2347 kdur
["additionalParams"]
2349 kdur_list
.append(kdur
)
2350 vnfr
["kdur"] = kdur_list
2352 db_vnfrs
[vnfr
["member-vnf-index-ref"]] = vnfr
2353 vnfd_id
= vnfr
["vnfd-id"]
2354 vnfd_ref
= vnfr
["vnfd-ref"]
2355 self
.fs
.sync(vnfd_id
)
2357 # if we haven't this vnfd, read it from db
2358 if vnfd_id
not in db_vnfds
:
2360 stage
[1] = "Getting vnfd={} id='{}' from db.".format(
2363 self
.logger
.debug(logging_text
+ stage
[1])
2364 vnfd
= self
.db
.get_one("vnfds", {"_id": vnfd_id
})
2367 db_vnfds
.append(vnfd
)
2369 # Get or generates the _admin.deployed.VCA list
2370 vca_deployed_list
= None
2371 if db_nsr
["_admin"].get("deployed"):
2372 vca_deployed_list
= db_nsr
["_admin"]["deployed"].get("VCA")
2373 if vca_deployed_list
is None:
2374 vca_deployed_list
= []
2375 configuration_status_list
= []
2376 db_nsr_update
["_admin.deployed.VCA"] = vca_deployed_list
2377 db_nsr_update
["configurationStatus"] = configuration_status_list
2378 # add _admin.deployed.VCA to db_nsr dictionary, value=vca_deployed_list
2379 populate_dict(db_nsr
, ("_admin", "deployed", "VCA"), vca_deployed_list
)
2380 elif isinstance(vca_deployed_list
, dict):
2381 # maintain backward compatibility. Change a dict to list at database
2382 vca_deployed_list
= list(vca_deployed_list
.values())
2383 db_nsr_update
["_admin.deployed.VCA"] = vca_deployed_list
2384 populate_dict(db_nsr
, ("_admin", "deployed", "VCA"), vca_deployed_list
)
2387 deep_get(db_nsr
, ("_admin", "deployed", "RO", "vnfd")), list
2389 populate_dict(db_nsr
, ("_admin", "deployed", "RO", "vnfd"), [])
2390 db_nsr_update
["_admin.deployed.RO.vnfd"] = []
2392 # set state to INSTANTIATED. When instantiated NBI will not delete directly
2393 db_nsr_update
["_admin.nsState"] = "INSTANTIATED"
2394 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
2396 "vnfrs", {"nsr-id-ref": nsr_id
}, {"_admin.nsState": "INSTANTIATED"}
2399 # n2vc_redesign STEP 2 Deploy Network Scenario
2400 stage
[0] = "Stage 2/5: deployment of KDUs, VMs and execution environments."
2401 self
._write
_op
_status
(op_id
=nslcmop_id
, stage
=stage
)
2403 stage
[1] = "Deploying KDUs."
2404 # self.logger.debug(logging_text + "Before deploy_kdus")
2405 # Call to deploy_kdus in case exists the "vdu:kdu" param
2406 await self
.deploy_kdus(
2407 logging_text
=logging_text
,
2409 nslcmop_id
=nslcmop_id
,
2412 task_instantiation_info
=tasks_dict_info
,
2415 stage
[1] = "Getting VCA public key."
2416 # n2vc_redesign STEP 1 Get VCA public ssh-key
2417 # feature 1429. Add n2vc public key to needed VMs
2418 n2vc_key
= self
.n2vc
.get_public_key()
2419 n2vc_key_list
= [n2vc_key
]
2420 if self
.vca_config
.get("public_key"):
2421 n2vc_key_list
.append(self
.vca_config
["public_key"])
2423 stage
[1] = "Deploying NS at VIM."
2424 task_ro
= asyncio
.ensure_future(
2425 self
.instantiate_RO(
2426 logging_text
=logging_text
,
2430 db_nslcmop
=db_nslcmop
,
2433 n2vc_key_list
=n2vc_key_list
,
2437 self
.lcm_tasks
.register("ns", nsr_id
, nslcmop_id
, "instantiate_RO", task_ro
)
2438 tasks_dict_info
[task_ro
] = "Deploying at VIM"
2440 # n2vc_redesign STEP 3 to 6 Deploy N2VC
2441 stage
[1] = "Deploying Execution Environments."
2442 self
.logger
.debug(logging_text
+ stage
[1])
2444 nsi_id
= None # TODO put nsi_id when this nsr belongs to a NSI
2445 for vnf_profile
in get_vnf_profiles(nsd
):
2446 vnfd_id
= vnf_profile
["vnfd-id"]
2447 vnfd
= find_in_list(db_vnfds
, lambda a_vnf
: a_vnf
["id"] == vnfd_id
)
2448 member_vnf_index
= str(vnf_profile
["id"])
2449 db_vnfr
= db_vnfrs
[member_vnf_index
]
2450 base_folder
= vnfd
["_admin"]["storage"]
2456 # Get additional parameters
2457 deploy_params
= {"OSM": get_osm_params(db_vnfr
)}
2458 if db_vnfr
.get("additionalParamsForVnf"):
2459 deploy_params
.update(
2460 parse_yaml_strings(db_vnfr
["additionalParamsForVnf"].copy())
2463 descriptor_config
= get_configuration(vnfd
, vnfd
["id"])
2464 if descriptor_config
:
2466 logging_text
=logging_text
2467 + "member_vnf_index={} ".format(member_vnf_index
),
2470 nslcmop_id
=nslcmop_id
,
2476 member_vnf_index
=member_vnf_index
,
2477 vdu_index
=vdu_index
,
2479 deploy_params
=deploy_params
,
2480 descriptor_config
=descriptor_config
,
2481 base_folder
=base_folder
,
2482 task_instantiation_info
=tasks_dict_info
,
2486 # Deploy charms for each VDU that supports one.
2487 for vdud
in get_vdu_list(vnfd
):
2489 descriptor_config
= get_configuration(vnfd
, vdu_id
)
2490 vdur
= find_in_list(
2491 db_vnfr
["vdur"], lambda vdu
: vdu
["vdu-id-ref"] == vdu_id
2494 if vdur
.get("additionalParams"):
2495 deploy_params_vdu
= parse_yaml_strings(vdur
["additionalParams"])
2497 deploy_params_vdu
= deploy_params
2498 deploy_params_vdu
["OSM"] = get_osm_params(
2499 db_vnfr
, vdu_id
, vdu_count_index
=0
2501 vdud_count
= get_number_of_instances(vnfd
, vdu_id
)
2503 self
.logger
.debug("VDUD > {}".format(vdud
))
2505 "Descriptor config > {}".format(descriptor_config
)
2507 if descriptor_config
:
2510 for vdu_index
in range(vdud_count
):
2511 # TODO vnfr_params["rw_mgmt_ip"] = vdur["ip-address"]
2513 logging_text
=logging_text
2514 + "member_vnf_index={}, vdu_id={}, vdu_index={} ".format(
2515 member_vnf_index
, vdu_id
, vdu_index
2519 nslcmop_id
=nslcmop_id
,
2525 member_vnf_index
=member_vnf_index
,
2526 vdu_index
=vdu_index
,
2528 deploy_params
=deploy_params_vdu
,
2529 descriptor_config
=descriptor_config
,
2530 base_folder
=base_folder
,
2531 task_instantiation_info
=tasks_dict_info
,
2534 for kdud
in get_kdu_list(vnfd
):
2535 kdu_name
= kdud
["name"]
2536 descriptor_config
= get_configuration(vnfd
, kdu_name
)
2537 if descriptor_config
:
2542 x
for x
in db_vnfr
["kdur"] if x
["kdu-name"] == kdu_name
2544 deploy_params_kdu
= {"OSM": get_osm_params(db_vnfr
)}
2545 if kdur
.get("additionalParams"):
2546 deploy_params_kdu
.update(
2547 parse_yaml_strings(kdur
["additionalParams"].copy())
2551 logging_text
=logging_text
,
2554 nslcmop_id
=nslcmop_id
,
2560 member_vnf_index
=member_vnf_index
,
2561 vdu_index
=vdu_index
,
2563 deploy_params
=deploy_params_kdu
,
2564 descriptor_config
=descriptor_config
,
2565 base_folder
=base_folder
,
2566 task_instantiation_info
=tasks_dict_info
,
2570 # Check if this NS has a charm configuration
2571 descriptor_config
= nsd
.get("ns-configuration")
2572 if descriptor_config
and descriptor_config
.get("juju"):
2575 member_vnf_index
= None
2581 # Get additional parameters
2582 deploy_params
= {"OSM": {"vim_account_id": ns_params
["vimAccountId"]}}
2583 if db_nsr
.get("additionalParamsForNs"):
2584 deploy_params
.update(
2585 parse_yaml_strings(db_nsr
["additionalParamsForNs"].copy())
2587 base_folder
= nsd
["_admin"]["storage"]
2589 logging_text
=logging_text
,
2592 nslcmop_id
=nslcmop_id
,
2598 member_vnf_index
=member_vnf_index
,
2599 vdu_index
=vdu_index
,
2601 deploy_params
=deploy_params
,
2602 descriptor_config
=descriptor_config
,
2603 base_folder
=base_folder
,
2604 task_instantiation_info
=tasks_dict_info
,
2608 # rest of staff will be done at finally
2611 ROclient
.ROClientException
,
2617 logging_text
+ "Exit Exception while '{}': {}".format(stage
[1], e
)
2620 except asyncio
.CancelledError
:
2622 logging_text
+ "Cancelled Exception while '{}'".format(stage
[1])
2624 exc
= "Operation was cancelled"
2625 except Exception as e
:
2626 exc
= traceback
.format_exc()
2627 self
.logger
.critical(
2628 logging_text
+ "Exit Exception while '{}': {}".format(stage
[1], e
),
2633 error_list
.append(str(exc
))
2635 # wait for pending tasks
2637 stage
[1] = "Waiting for instantiate pending tasks."
2638 self
.logger
.debug(logging_text
+ stage
[1])
2639 error_list
+= await self
._wait
_for
_tasks
(
2647 stage
[1] = stage
[2] = ""
2648 except asyncio
.CancelledError
:
2649 error_list
.append("Cancelled")
2650 # TODO cancel all tasks
2651 except Exception as exc
:
2652 error_list
.append(str(exc
))
2654 # update operation-status
2655 db_nsr_update
["operational-status"] = "running"
2656 # let's begin with VCA 'configured' status (later we can change it)
2657 db_nsr_update
["config-status"] = "configured"
2658 for task
, task_name
in tasks_dict_info
.items():
2659 if not task
.done() or task
.cancelled() or task
.exception():
2660 if task_name
.startswith(self
.task_name_deploy_vca
):
2661 # A N2VC task is pending
2662 db_nsr_update
["config-status"] = "failed"
2664 # RO or KDU task is pending
2665 db_nsr_update
["operational-status"] = "failed"
2667 # update status at database
2669 error_detail
= ". ".join(error_list
)
2670 self
.logger
.error(logging_text
+ error_detail
)
2671 error_description_nslcmop
= "{} Detail: {}".format(
2672 stage
[0], error_detail
2674 error_description_nsr
= "Operation: INSTANTIATING.{}, {}".format(
2675 nslcmop_id
, stage
[0]
2678 db_nsr_update
["detailed-status"] = (
2679 error_description_nsr
+ " Detail: " + error_detail
2681 db_nslcmop_update
["detailed-status"] = error_detail
2682 nslcmop_operation_state
= "FAILED"
2686 error_description_nsr
= error_description_nslcmop
= None
2688 db_nsr_update
["detailed-status"] = "Done"
2689 db_nslcmop_update
["detailed-status"] = "Done"
2690 nslcmop_operation_state
= "COMPLETED"
2693 self
._write
_ns
_status
(
2696 current_operation
="IDLE",
2697 current_operation_id
=None,
2698 error_description
=error_description_nsr
,
2699 error_detail
=error_detail
,
2700 other_update
=db_nsr_update
,
2702 self
._write
_op
_status
(
2705 error_message
=error_description_nslcmop
,
2706 operation_state
=nslcmop_operation_state
,
2707 other_update
=db_nslcmop_update
,
2710 if nslcmop_operation_state
:
2712 await self
.msg
.aiowrite(
2717 "nslcmop_id": nslcmop_id
,
2718 "operationState": nslcmop_operation_state
,
2722 except Exception as e
:
2724 logging_text
+ "kafka_write notification Exception {}".format(e
)
2727 self
.logger
.debug(logging_text
+ "Exit")
2728 self
.lcm_tasks
.remove("ns", nsr_id
, nslcmop_id
, "ns_instantiate")
2730 async def _add_vca_relations(
2735 timeout
: int = 3600,
2736 vca_type
: str = None,
2741 # 1. find all relations for this VCA
2742 # 2. wait for other peers related
2746 vca_type
= vca_type
or "lxc_proxy_charm"
2748 # STEP 1: find all relations for this VCA
2751 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
2752 nsd
= self
.db
.get_one("nsds", {"_id": db_nsr
["nsd-id"]})
2755 my_vca
= deep_get(db_nsr
, ("_admin", "deployed", "VCA"))[vca_index
]
2757 # read all ns-configuration relations
2758 ns_relations
= list()
2759 db_ns_relations
= deep_get(nsd
, ("ns-configuration", "relation"))
2761 for r
in db_ns_relations
:
2762 # check if this VCA is in the relation
2763 if my_vca
.get("member-vnf-index") in (
2764 r
.get("entities")[0].get("id"),
2765 r
.get("entities")[1].get("id"),
2767 ns_relations
.append(r
)
2769 # read all vnf-configuration relations
2770 vnf_relations
= list()
2771 db_vnfd_list
= db_nsr
.get("vnfd-id")
2773 for vnfd
in db_vnfd_list
:
2774 db_vnf_relations
= None
2775 db_vnfd
= self
.db
.get_one("vnfds", {"_id": vnfd
})
2776 db_vnf_configuration
= get_configuration(db_vnfd
, db_vnfd
["id"])
2777 if db_vnf_configuration
:
2778 db_vnf_relations
= db_vnf_configuration
.get("relation", [])
2779 if db_vnf_relations
:
2780 for r
in db_vnf_relations
:
2781 # check if this VCA is in the relation
2782 if my_vca
.get("vdu_id") in (
2783 r
.get("entities")[0].get("id"),
2784 r
.get("entities")[1].get("id"),
2786 vnf_relations
.append(r
)
2788 # if no relations, terminate
2789 if not ns_relations
and not vnf_relations
:
2790 self
.logger
.debug(logging_text
+ " No relations")
2795 + " adding relations\n {}\n {}".format(
2796 ns_relations
, vnf_relations
2805 if now
- start
>= timeout
:
2806 self
.logger
.error(logging_text
+ " : timeout adding relations")
2809 # reload nsr from database (we need to update record: _admin.deloyed.VCA)
2810 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
2812 # for each defined NS relation, find the VCA's related
2813 for r
in ns_relations
.copy():
2814 from_vca_ee_id
= None
2816 from_vca_endpoint
= None
2817 to_vca_endpoint
= None
2818 vca_list
= deep_get(db_nsr
, ("_admin", "deployed", "VCA"))
2819 for vca
in vca_list
:
2820 if vca
.get("member-vnf-index") == r
.get("entities")[0].get(
2822 ) and vca
.get("config_sw_installed"):
2823 from_vca_ee_id
= vca
.get("ee_id")
2824 from_vca_endpoint
= r
.get("entities")[0].get("endpoint")
2825 if vca
.get("member-vnf-index") == r
.get("entities")[1].get(
2827 ) and vca
.get("config_sw_installed"):
2828 to_vca_ee_id
= vca
.get("ee_id")
2829 to_vca_endpoint
= r
.get("entities")[1].get("endpoint")
2830 if from_vca_ee_id
and to_vca_ee_id
:
2832 await self
.vca_map
[vca_type
].add_relation(
2833 ee_id_1
=from_vca_ee_id
,
2834 ee_id_2
=to_vca_ee_id
,
2835 endpoint_1
=from_vca_endpoint
,
2836 endpoint_2
=to_vca_endpoint
,
2839 # remove entry from relations list
2840 ns_relations
.remove(r
)
2842 # check failed peers
2844 vca_status_list
= db_nsr
.get("configurationStatus")
2846 for i
in range(len(vca_list
)):
2848 vca_status
= vca_status_list
[i
]
2849 if vca
.get("member-vnf-index") == r
.get("entities")[
2852 if vca_status
.get("status") == "BROKEN":
2853 # peer broken: remove relation from list
2854 ns_relations
.remove(r
)
2855 if vca
.get("member-vnf-index") == r
.get("entities")[
2858 if vca_status
.get("status") == "BROKEN":
2859 # peer broken: remove relation from list
2860 ns_relations
.remove(r
)
2865 # for each defined VNF relation, find the VCA's related
2866 for r
in vnf_relations
.copy():
2867 from_vca_ee_id
= None
2869 from_vca_endpoint
= None
2870 to_vca_endpoint
= None
2871 vca_list
= deep_get(db_nsr
, ("_admin", "deployed", "VCA"))
2872 for vca
in vca_list
:
2873 key_to_check
= "vdu_id"
2874 if vca
.get("vdu_id") is None:
2875 key_to_check
= "vnfd_id"
2876 if vca
.get(key_to_check
) == r
.get("entities")[0].get(
2878 ) and vca
.get("config_sw_installed"):
2879 from_vca_ee_id
= vca
.get("ee_id")
2880 from_vca_endpoint
= r
.get("entities")[0].get("endpoint")
2881 if vca
.get(key_to_check
) == r
.get("entities")[1].get(
2883 ) and vca
.get("config_sw_installed"):
2884 to_vca_ee_id
= vca
.get("ee_id")
2885 to_vca_endpoint
= r
.get("entities")[1].get("endpoint")
2886 if from_vca_ee_id
and to_vca_ee_id
:
2888 await self
.vca_map
[vca_type
].add_relation(
2889 ee_id_1
=from_vca_ee_id
,
2890 ee_id_2
=to_vca_ee_id
,
2891 endpoint_1
=from_vca_endpoint
,
2892 endpoint_2
=to_vca_endpoint
,
2895 # remove entry from relations list
2896 vnf_relations
.remove(r
)
2898 # check failed peers
2900 vca_status_list
= db_nsr
.get("configurationStatus")
2902 for i
in range(len(vca_list
)):
2904 vca_status
= vca_status_list
[i
]
2905 if vca
.get("vdu_id") == r
.get("entities")[0].get(
2908 if vca_status
.get("status") == "BROKEN":
2909 # peer broken: remove relation from list
2910 vnf_relations
.remove(r
)
2911 if vca
.get("vdu_id") == r
.get("entities")[1].get(
2914 if vca_status
.get("status") == "BROKEN":
2915 # peer broken: remove relation from list
2916 vnf_relations
.remove(r
)
2922 await asyncio
.sleep(5.0)
2924 if not ns_relations
and not vnf_relations
:
2925 self
.logger
.debug("Relations added")
2930 except Exception as e
:
2931 self
.logger
.warn(logging_text
+ " ERROR adding relations: {}".format(e
))
2934 async def _install_kdu(
2942 k8s_instance_info
: dict,
2943 k8params
: dict = None,
2949 k8sclustertype
= k8s_instance_info
["k8scluster-type"]
2952 "collection": "nsrs",
2953 "filter": {"_id": nsr_id
},
2954 "path": nsr_db_path
,
2957 if k8s_instance_info
.get("kdu-deployment-name"):
2958 kdu_instance
= k8s_instance_info
.get("kdu-deployment-name")
2960 kdu_instance
= self
.k8scluster_map
[
2962 ].generate_kdu_instance_name(
2963 db_dict
=db_dict_install
,
2964 kdu_model
=k8s_instance_info
["kdu-model"],
2965 kdu_name
=k8s_instance_info
["kdu-name"],
2968 # Update the nsrs table with the kdu-instance value
2972 _desc
={nsr_db_path
+ ".kdu-instance": kdu_instance
},
2975 # Update the nsrs table with the actual namespace being used, if the k8scluster-type is `juju` or
2976 # `juju-bundle`. This verification is needed because there is not a standard/homogeneous namespace
2977 # between the Helm Charts and Juju Bundles-based KNFs. If we found a way of having an homogeneous
2978 # namespace, this first verification could be removed, and the next step would be done for any kind
2980 # TODO -> find a way to have an homogeneous namespace between the Helm Charts and Juju Bundles-based
2981 # KNFs (Bug 2027: https://osm.etsi.org/bugzilla/show_bug.cgi?id=2027)
2982 if k8sclustertype
in ("juju", "juju-bundle"):
2983 # First, verify if the current namespace is present in the `_admin.projects_read` (if not, it means
2984 # that the user passed a namespace which he wants its KDU to be deployed in)
2990 "_admin.projects_write": k8s_instance_info
["namespace"],
2991 "_admin.projects_read": k8s_instance_info
["namespace"],
2997 f
"Updating namespace/model for Juju Bundle from {k8s_instance_info['namespace']} to {kdu_instance}"
3002 _desc
={f
"{nsr_db_path}.namespace": kdu_instance
},
3004 k8s_instance_info
["namespace"] = kdu_instance
3006 await self
.k8scluster_map
[k8sclustertype
].install(
3007 cluster_uuid
=k8s_instance_info
["k8scluster-uuid"],
3008 kdu_model
=k8s_instance_info
["kdu-model"],
3011 db_dict
=db_dict_install
,
3013 kdu_name
=k8s_instance_info
["kdu-name"],
3014 namespace
=k8s_instance_info
["namespace"],
3015 kdu_instance
=kdu_instance
,
3019 # Obtain services to obtain management service ip
3020 services
= await self
.k8scluster_map
[k8sclustertype
].get_services(
3021 cluster_uuid
=k8s_instance_info
["k8scluster-uuid"],
3022 kdu_instance
=kdu_instance
,
3023 namespace
=k8s_instance_info
["namespace"],
3026 # Obtain management service info (if exists)
3027 vnfr_update_dict
= {}
3028 kdu_config
= get_configuration(vnfd
, kdud
["name"])
3030 target_ee_list
= kdu_config
.get("execution-environment-list", [])
3035 vnfr_update_dict
["kdur.{}.services".format(kdu_index
)] = services
3038 for service
in kdud
.get("service", [])
3039 if service
.get("mgmt-service")
3041 for mgmt_service
in mgmt_services
:
3042 for service
in services
:
3043 if service
["name"].startswith(mgmt_service
["name"]):
3044 # Mgmt service found, Obtain service ip
3045 ip
= service
.get("external_ip", service
.get("cluster_ip"))
3046 if isinstance(ip
, list) and len(ip
) == 1:
3050 "kdur.{}.ip-address".format(kdu_index
)
3053 # Check if must update also mgmt ip at the vnf
3054 service_external_cp
= mgmt_service
.get(
3055 "external-connection-point-ref"
3057 if service_external_cp
:
3059 deep_get(vnfd
, ("mgmt-interface", "cp"))
3060 == service_external_cp
3062 vnfr_update_dict
["ip-address"] = ip
3067 "external-connection-point-ref", ""
3069 == service_external_cp
,
3072 "kdur.{}.ip-address".format(kdu_index
)
3077 "Mgmt service name: {} not found".format(
3078 mgmt_service
["name"]
3082 vnfr_update_dict
["kdur.{}.status".format(kdu_index
)] = "READY"
3083 self
.update_db_2("vnfrs", vnfr_data
.get("_id"), vnfr_update_dict
)
3085 kdu_config
= get_configuration(vnfd
, k8s_instance_info
["kdu-name"])
3088 and kdu_config
.get("initial-config-primitive")
3089 and get_juju_ee_ref(vnfd
, k8s_instance_info
["kdu-name"]) is None
3091 initial_config_primitive_list
= kdu_config
.get(
3092 "initial-config-primitive"
3094 initial_config_primitive_list
.sort(key
=lambda val
: int(val
["seq"]))
3096 for initial_config_primitive
in initial_config_primitive_list
:
3097 primitive_params_
= self
._map
_primitive
_params
(
3098 initial_config_primitive
, {}, {}
3101 await asyncio
.wait_for(
3102 self
.k8scluster_map
[k8sclustertype
].exec_primitive(
3103 cluster_uuid
=k8s_instance_info
["k8scluster-uuid"],
3104 kdu_instance
=kdu_instance
,
3105 primitive_name
=initial_config_primitive
["name"],
3106 params
=primitive_params_
,
3107 db_dict
=db_dict_install
,
3113 except Exception as e
:
3114 # Prepare update db with error and raise exception
3117 "nsrs", nsr_id
, {nsr_db_path
+ ".detailed-status": str(e
)}
3121 vnfr_data
.get("_id"),
3122 {"kdur.{}.status".format(kdu_index
): "ERROR"},
3125 # ignore to keep original exception
3127 # reraise original error
3132 async def deploy_kdus(
3139 task_instantiation_info
,
3141 # Launch kdus if present in the descriptor
3143 k8scluster_id_2_uuic
= {
3144 "helm-chart-v3": {},
3149 async def _get_cluster_id(cluster_id
, cluster_type
):
3150 nonlocal k8scluster_id_2_uuic
3151 if cluster_id
in k8scluster_id_2_uuic
[cluster_type
]:
3152 return k8scluster_id_2_uuic
[cluster_type
][cluster_id
]
3154 # check if K8scluster is creating and wait look if previous tasks in process
3155 task_name
, task_dependency
= self
.lcm_tasks
.lookfor_related(
3156 "k8scluster", cluster_id
3159 text
= "Waiting for related tasks '{}' on k8scluster {} to be completed".format(
3160 task_name
, cluster_id
3162 self
.logger
.debug(logging_text
+ text
)
3163 await asyncio
.wait(task_dependency
, timeout
=3600)
3165 db_k8scluster
= self
.db
.get_one(
3166 "k8sclusters", {"_id": cluster_id
}, fail_on_empty
=False
3168 if not db_k8scluster
:
3169 raise LcmException("K8s cluster {} cannot be found".format(cluster_id
))
3171 k8s_id
= deep_get(db_k8scluster
, ("_admin", cluster_type
, "id"))
3173 if cluster_type
== "helm-chart-v3":
3175 # backward compatibility for existing clusters that have not been initialized for helm v3
3176 k8s_credentials
= yaml
.safe_dump(
3177 db_k8scluster
.get("credentials")
3179 k8s_id
, uninstall_sw
= await self
.k8sclusterhelm3
.init_env(
3180 k8s_credentials
, reuse_cluster_uuid
=cluster_id
3182 db_k8scluster_update
= {}
3183 db_k8scluster_update
["_admin.helm-chart-v3.error_msg"] = None
3184 db_k8scluster_update
["_admin.helm-chart-v3.id"] = k8s_id
3185 db_k8scluster_update
[
3186 "_admin.helm-chart-v3.created"
3188 db_k8scluster_update
[
3189 "_admin.helm-chart-v3.operationalState"
3192 "k8sclusters", cluster_id
, db_k8scluster_update
3194 except Exception as e
:
3197 + "error initializing helm-v3 cluster: {}".format(str(e
))
3200 "K8s cluster '{}' has not been initialized for '{}'".format(
3201 cluster_id
, cluster_type
3206 "K8s cluster '{}' has not been initialized for '{}'".format(
3207 cluster_id
, cluster_type
3210 k8scluster_id_2_uuic
[cluster_type
][cluster_id
] = k8s_id
3213 logging_text
+= "Deploy kdus: "
3216 db_nsr_update
= {"_admin.deployed.K8s": []}
3217 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
3220 updated_cluster_list
= []
3221 updated_v3_cluster_list
= []
3223 for vnfr_data
in db_vnfrs
.values():
3224 vca_id
= self
.get_vca_id(vnfr_data
, {})
3225 for kdu_index
, kdur
in enumerate(get_iterable(vnfr_data
, "kdur")):
3226 # Step 0: Prepare and set parameters
3227 desc_params
= parse_yaml_strings(kdur
.get("additionalParams"))
3228 vnfd_id
= vnfr_data
.get("vnfd-id")
3229 vnfd_with_id
= find_in_list(
3230 db_vnfds
, lambda vnfd
: vnfd
["_id"] == vnfd_id
3234 for kdud
in vnfd_with_id
["kdu"]
3235 if kdud
["name"] == kdur
["kdu-name"]
3237 namespace
= kdur
.get("k8s-namespace")
3238 kdu_deployment_name
= kdur
.get("kdu-deployment-name")
3239 if kdur
.get("helm-chart"):
3240 kdumodel
= kdur
["helm-chart"]
3241 # Default version: helm3, if helm-version is v2 assign v2
3242 k8sclustertype
= "helm-chart-v3"
3243 self
.logger
.debug("kdur: {}".format(kdur
))
3245 kdur
.get("helm-version")
3246 and kdur
.get("helm-version") == "v2"
3248 k8sclustertype
= "helm-chart"
3249 elif kdur
.get("juju-bundle"):
3250 kdumodel
= kdur
["juju-bundle"]
3251 k8sclustertype
= "juju-bundle"
3254 "kdu type for kdu='{}.{}' is neither helm-chart nor "
3255 "juju-bundle. Maybe an old NBI version is running".format(
3256 vnfr_data
["member-vnf-index-ref"], kdur
["kdu-name"]
3259 # check if kdumodel is a file and exists
3261 vnfd_with_id
= find_in_list(
3262 db_vnfds
, lambda vnfd
: vnfd
["_id"] == vnfd_id
3264 storage
= deep_get(vnfd_with_id
, ("_admin", "storage"))
3265 if storage
and storage
.get(
3267 ): # may be not present if vnfd has not artifacts
3268 # path format: /vnfdid/pkkdir/helm-charts|juju-bundles/kdumodel
3269 filename
= "{}/{}/{}s/{}".format(
3275 if self
.fs
.file_exists(
3276 filename
, mode
="file"
3277 ) or self
.fs
.file_exists(filename
, mode
="dir"):
3278 kdumodel
= self
.fs
.path
+ filename
3279 except (asyncio
.TimeoutError
, asyncio
.CancelledError
):
3281 except Exception: # it is not a file
3284 k8s_cluster_id
= kdur
["k8s-cluster"]["id"]
3285 step
= "Synchronize repos for k8s cluster '{}'".format(
3288 cluster_uuid
= await _get_cluster_id(k8s_cluster_id
, k8sclustertype
)
3292 k8sclustertype
== "helm-chart"
3293 and cluster_uuid
not in updated_cluster_list
3295 k8sclustertype
== "helm-chart-v3"
3296 and cluster_uuid
not in updated_v3_cluster_list
3298 del_repo_list
, added_repo_dict
= await asyncio
.ensure_future(
3299 self
.k8scluster_map
[k8sclustertype
].synchronize_repos(
3300 cluster_uuid
=cluster_uuid
3303 if del_repo_list
or added_repo_dict
:
3304 if k8sclustertype
== "helm-chart":
3306 "_admin.helm_charts_added." + item
: None
3307 for item
in del_repo_list
3310 "_admin.helm_charts_added." + item
: name
3311 for item
, name
in added_repo_dict
.items()
3313 updated_cluster_list
.append(cluster_uuid
)
3314 elif k8sclustertype
== "helm-chart-v3":
3316 "_admin.helm_charts_v3_added." + item
: None
3317 for item
in del_repo_list
3320 "_admin.helm_charts_v3_added." + item
: name
3321 for item
, name
in added_repo_dict
.items()
3323 updated_v3_cluster_list
.append(cluster_uuid
)
3325 logging_text
+ "repos synchronized on k8s cluster "
3326 "'{}' to_delete: {}, to_add: {}".format(
3327 k8s_cluster_id
, del_repo_list
, added_repo_dict
3332 {"_id": k8s_cluster_id
},
3338 step
= "Instantiating KDU {}.{} in k8s cluster {}".format(
3339 vnfr_data
["member-vnf-index-ref"],
3343 k8s_instance_info
= {
3344 "kdu-instance": None,
3345 "k8scluster-uuid": cluster_uuid
,
3346 "k8scluster-type": k8sclustertype
,
3347 "member-vnf-index": vnfr_data
["member-vnf-index-ref"],
3348 "kdu-name": kdur
["kdu-name"],
3349 "kdu-model": kdumodel
,
3350 "namespace": namespace
,
3351 "kdu-deployment-name": kdu_deployment_name
,
3353 db_path
= "_admin.deployed.K8s.{}".format(index
)
3354 db_nsr_update
[db_path
] = k8s_instance_info
3355 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
3356 vnfd_with_id
= find_in_list(
3357 db_vnfds
, lambda vnf
: vnf
["_id"] == vnfd_id
3359 task
= asyncio
.ensure_future(
3368 k8params
=desc_params
,
3373 self
.lcm_tasks
.register(
3377 "instantiate_KDU-{}".format(index
),
3380 task_instantiation_info
[task
] = "Deploying KDU {}".format(
3386 except (LcmException
, asyncio
.CancelledError
):
3388 except Exception as e
:
3389 msg
= "Exception {} while {}: {}".format(type(e
).__name
__, step
, e
)
3390 if isinstance(e
, (N2VCException
, DbException
)):
3391 self
.logger
.error(logging_text
+ msg
)
3393 self
.logger
.critical(logging_text
+ msg
, exc_info
=True)
3394 raise LcmException(msg
)
3397 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
3416 task_instantiation_info
,
3419 # launch instantiate_N2VC in a asyncio task and register task object
3420 # Look where information of this charm is at database <nsrs>._admin.deployed.VCA
3421 # if not found, create one entry and update database
3422 # fill db_nsr._admin.deployed.VCA.<index>
3425 logging_text
+ "_deploy_n2vc vnfd_id={}, vdu_id={}".format(vnfd_id
, vdu_id
)
3427 if "execution-environment-list" in descriptor_config
:
3428 ee_list
= descriptor_config
.get("execution-environment-list", [])
3429 elif "juju" in descriptor_config
:
3430 ee_list
= [descriptor_config
] # ns charms
3431 else: # other types as script are not supported
3434 for ee_item
in ee_list
:
3437 + "_deploy_n2vc ee_item juju={}, helm={}".format(
3438 ee_item
.get("juju"), ee_item
.get("helm-chart")
3441 ee_descriptor_id
= ee_item
.get("id")
3442 if ee_item
.get("juju"):
3443 vca_name
= ee_item
["juju"].get("charm")
3446 if ee_item
["juju"].get("charm") is not None
3449 if ee_item
["juju"].get("cloud") == "k8s":
3450 vca_type
= "k8s_proxy_charm"
3451 elif ee_item
["juju"].get("proxy") is False:
3452 vca_type
= "native_charm"
3453 elif ee_item
.get("helm-chart"):
3454 vca_name
= ee_item
["helm-chart"]
3455 if ee_item
.get("helm-version") and ee_item
.get("helm-version") == "v2":
3458 vca_type
= "helm-v3"
3461 logging_text
+ "skipping non juju neither charm configuration"
3466 for vca_index
, vca_deployed
in enumerate(
3467 db_nsr
["_admin"]["deployed"]["VCA"]
3469 if not vca_deployed
:
3472 vca_deployed
.get("member-vnf-index") == member_vnf_index
3473 and vca_deployed
.get("vdu_id") == vdu_id
3474 and vca_deployed
.get("kdu_name") == kdu_name
3475 and vca_deployed
.get("vdu_count_index", 0) == vdu_index
3476 and vca_deployed
.get("ee_descriptor_id") == ee_descriptor_id
3480 # not found, create one.
3482 "ns" if not member_vnf_index
else "vnf/{}".format(member_vnf_index
)
3485 target
+= "/vdu/{}/{}".format(vdu_id
, vdu_index
or 0)
3487 target
+= "/kdu/{}".format(kdu_name
)
3489 "target_element": target
,
3490 # ^ target_element will replace member-vnf-index, kdu_name, vdu_id ... in a single string
3491 "member-vnf-index": member_vnf_index
,
3493 "kdu_name": kdu_name
,
3494 "vdu_count_index": vdu_index
,
3495 "operational-status": "init", # TODO revise
3496 "detailed-status": "", # TODO revise
3497 "step": "initial-deploy", # TODO revise
3499 "vdu_name": vdu_name
,
3501 "ee_descriptor_id": ee_descriptor_id
,
3505 # create VCA and configurationStatus in db
3507 "_admin.deployed.VCA.{}".format(vca_index
): vca_deployed
,
3508 "configurationStatus.{}".format(vca_index
): dict(),
3510 self
.update_db_2("nsrs", nsr_id
, db_dict
)
3512 db_nsr
["_admin"]["deployed"]["VCA"].append(vca_deployed
)
3514 self
.logger
.debug("N2VC > NSR_ID > {}".format(nsr_id
))
3515 self
.logger
.debug("N2VC > DB_NSR > {}".format(db_nsr
))
3516 self
.logger
.debug("N2VC > VCA_DEPLOYED > {}".format(vca_deployed
))
3519 task_n2vc
= asyncio
.ensure_future(
3520 self
.instantiate_N2VC(
3521 logging_text
=logging_text
,
3522 vca_index
=vca_index
,
3528 vdu_index
=vdu_index
,
3529 deploy_params
=deploy_params
,
3530 config_descriptor
=descriptor_config
,
3531 base_folder
=base_folder
,
3532 nslcmop_id
=nslcmop_id
,
3536 ee_config_descriptor
=ee_item
,
3539 self
.lcm_tasks
.register(
3543 "instantiate_N2VC-{}".format(vca_index
),
3546 task_instantiation_info
[
3548 ] = self
.task_name_deploy_vca
+ " {}.{}".format(
3549 member_vnf_index
or "", vdu_id
or ""
3553 def _create_nslcmop(nsr_id
, operation
, params
):
3555 Creates a ns-lcm-opp content to be stored at database.
3556 :param nsr_id: internal id of the instance
3557 :param operation: instantiate, terminate, scale, action, ...
3558 :param params: user parameters for the operation
3559 :return: dictionary following SOL005 format
3561 # Raise exception if invalid arguments
3562 if not (nsr_id
and operation
and params
):
3564 "Parameters 'nsr_id', 'operation' and 'params' needed to create primitive not provided"
3571 # COMPLETED,PARTIALLY_COMPLETED,FAILED_TEMP,FAILED,ROLLING_BACK,ROLLED_BACK
3572 "operationState": "PROCESSING",
3573 "statusEnteredTime": now
,
3574 "nsInstanceId": nsr_id
,
3575 "lcmOperationType": operation
,
3577 "isAutomaticInvocation": False,
3578 "operationParams": params
,
3579 "isCancelPending": False,
3581 "self": "/osm/nslcm/v1/ns_lcm_op_occs/" + _id
,
3582 "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsr_id
,
3587 def _format_additional_params(self
, params
):
3588 params
= params
or {}
3589 for key
, value
in params
.items():
3590 if str(value
).startswith("!!yaml "):
3591 params
[key
] = yaml
.safe_load(value
[7:])
3594 def _get_terminate_primitive_params(self
, seq
, vnf_index
):
3595 primitive
= seq
.get("name")
3596 primitive_params
= {}
3598 "member_vnf_index": vnf_index
,
3599 "primitive": primitive
,
3600 "primitive_params": primitive_params
,
3603 return self
._map
_primitive
_params
(seq
, params
, desc_params
)
3607 def _retry_or_skip_suboperation(self
, db_nslcmop
, op_index
):
3608 op
= deep_get(db_nslcmop
, ("_admin", "operations"), [])[op_index
]
3609 if op
.get("operationState") == "COMPLETED":
3610 # b. Skip sub-operation
3611 # _ns_execute_primitive() or RO.create_action() will NOT be executed
3612 return self
.SUBOPERATION_STATUS_SKIP
3614 # c. retry executing sub-operation
3615 # The sub-operation exists, and operationState != 'COMPLETED'
3616 # Update operationState = 'PROCESSING' to indicate a retry.
3617 operationState
= "PROCESSING"
3618 detailed_status
= "In progress"
3619 self
._update
_suboperation
_status
(
3620 db_nslcmop
, op_index
, operationState
, detailed_status
3622 # Return the sub-operation index
3623 # _ns_execute_primitive() or RO.create_action() will be called from scale()
3624 # with arguments extracted from the sub-operation
3627 # Find a sub-operation where all keys in a matching dictionary must match
3628 # Returns the index of the matching sub-operation, or SUBOPERATION_STATUS_NOT_FOUND if no match
3629 def _find_suboperation(self
, db_nslcmop
, match
):
3630 if db_nslcmop
and match
:
3631 op_list
= db_nslcmop
.get("_admin", {}).get("operations", [])
3632 for i
, op
in enumerate(op_list
):
3633 if all(op
.get(k
) == match
[k
] for k
in match
):
3635 return self
.SUBOPERATION_STATUS_NOT_FOUND
3637 # Update status for a sub-operation given its index
3638 def _update_suboperation_status(
3639 self
, db_nslcmop
, op_index
, operationState
, detailed_status
3641 # Update DB for HA tasks
3642 q_filter
= {"_id": db_nslcmop
["_id"]}
3644 "_admin.operations.{}.operationState".format(op_index
): operationState
,
3645 "_admin.operations.{}.detailed-status".format(op_index
): detailed_status
,
3648 "nslcmops", q_filter
=q_filter
, update_dict
=update_dict
, fail_on_empty
=False
3651 # Add sub-operation, return the index of the added sub-operation
3652 # Optionally, set operationState, detailed-status, and operationType
3653 # Status and type are currently set for 'scale' sub-operations:
3654 # 'operationState' : 'PROCESSING' | 'COMPLETED' | 'FAILED'
3655 # 'detailed-status' : status message
3656 # 'operationType': may be any type, in the case of scaling: 'PRE-SCALE' | 'POST-SCALE'
3657 # Status and operation type are currently only used for 'scale', but NOT for 'terminate' sub-operations.
3658 def _add_suboperation(
3666 mapped_primitive_params
,
3667 operationState
=None,
3668 detailed_status
=None,
3671 RO_scaling_info
=None,
3674 return self
.SUBOPERATION_STATUS_NOT_FOUND
3675 # Get the "_admin.operations" list, if it exists
3676 db_nslcmop_admin
= db_nslcmop
.get("_admin", {})
3677 op_list
= db_nslcmop_admin
.get("operations")
3678 # Create or append to the "_admin.operations" list
3680 "member_vnf_index": vnf_index
,
3682 "vdu_count_index": vdu_count_index
,
3683 "primitive": primitive
,
3684 "primitive_params": mapped_primitive_params
,
3687 new_op
["operationState"] = operationState
3689 new_op
["detailed-status"] = detailed_status
3691 new_op
["lcmOperationType"] = operationType
3693 new_op
["RO_nsr_id"] = RO_nsr_id
3695 new_op
["RO_scaling_info"] = RO_scaling_info
3697 # No existing operations, create key 'operations' with current operation as first list element
3698 db_nslcmop_admin
.update({"operations": [new_op
]})
3699 op_list
= db_nslcmop_admin
.get("operations")
3701 # Existing operations, append operation to list
3702 op_list
.append(new_op
)
3704 db_nslcmop_update
= {"_admin.operations": op_list
}
3705 self
.update_db_2("nslcmops", db_nslcmop
["_id"], db_nslcmop_update
)
3706 op_index
= len(op_list
) - 1
3709 # Helper methods for scale() sub-operations
3711 # pre-scale/post-scale:
3712 # Check for 3 different cases:
3713 # a. New: First time execution, return SUBOPERATION_STATUS_NEW
3714 # b. Skip: Existing sub-operation exists, operationState == 'COMPLETED', return SUBOPERATION_STATUS_SKIP
3715 # c. retry: Existing sub-operation exists, operationState != 'COMPLETED', return op_index to re-execute
3716 def _check_or_add_scale_suboperation(
3720 vnf_config_primitive
,
3724 RO_scaling_info
=None,
3726 # Find this sub-operation
3727 if RO_nsr_id
and RO_scaling_info
:
3728 operationType
= "SCALE-RO"
3730 "member_vnf_index": vnf_index
,
3731 "RO_nsr_id": RO_nsr_id
,
3732 "RO_scaling_info": RO_scaling_info
,
3736 "member_vnf_index": vnf_index
,
3737 "primitive": vnf_config_primitive
,
3738 "primitive_params": primitive_params
,
3739 "lcmOperationType": operationType
,
3741 op_index
= self
._find
_suboperation
(db_nslcmop
, match
)
3742 if op_index
== self
.SUBOPERATION_STATUS_NOT_FOUND
:
3743 # a. New sub-operation
3744 # The sub-operation does not exist, add it.
3745 # _ns_execute_primitive() will be called from scale() as usual, with non-modified arguments
3746 # The following parameters are set to None for all kind of scaling:
3748 vdu_count_index
= None
3750 if RO_nsr_id
and RO_scaling_info
:
3751 vnf_config_primitive
= None
3752 primitive_params
= None
3755 RO_scaling_info
= None
3756 # Initial status for sub-operation
3757 operationState
= "PROCESSING"
3758 detailed_status
= "In progress"
3759 # Add sub-operation for pre/post-scaling (zero or more operations)
3760 self
._add
_suboperation
(
3766 vnf_config_primitive
,
3774 return self
.SUBOPERATION_STATUS_NEW
3776 # Return either SUBOPERATION_STATUS_SKIP (operationState == 'COMPLETED'),
3777 # or op_index (operationState != 'COMPLETED')
3778 return self
._retry
_or
_skip
_suboperation
(db_nslcmop
, op_index
)
3780 # Function to return execution_environment id
3782 def _get_ee_id(self
, vnf_index
, vdu_id
, vca_deployed_list
):
3783 # TODO vdu_index_count
3784 for vca
in vca_deployed_list
:
3785 if vca
["member-vnf-index"] == vnf_index
and vca
["vdu_id"] == vdu_id
:
3788 async def destroy_N2VC(
3796 exec_primitives
=True,
3801 Execute the terminate primitives and destroy the execution environment (if destroy_ee=False
3802 :param logging_text:
3804 :param vca_deployed: Dictionary of deployment info at db_nsr._admin.depoloyed.VCA.<INDEX>
3805 :param config_descriptor: Configuration descriptor of the NSD, VNFD, VNFD.vdu or VNFD.kdu
3806 :param vca_index: index in the database _admin.deployed.VCA
3807 :param destroy_ee: False to do not destroy, because it will be destroyed all of then at once
3808 :param exec_primitives: False to do not execute terminate primitives, because the config is not completed or has
3809 not executed properly
3810 :param scaling_in: True destroys the application, False destroys the model
3811 :return: None or exception
3816 + " vca_index: {}, vca_deployed: {}, config_descriptor: {}, destroy_ee: {}".format(
3817 vca_index
, vca_deployed
, config_descriptor
, destroy_ee
3821 vca_type
= vca_deployed
.get("type", "lxc_proxy_charm")
3823 # execute terminate_primitives
3825 terminate_primitives
= get_ee_sorted_terminate_config_primitive_list(
3826 config_descriptor
.get("terminate-config-primitive"),
3827 vca_deployed
.get("ee_descriptor_id"),
3829 vdu_id
= vca_deployed
.get("vdu_id")
3830 vdu_count_index
= vca_deployed
.get("vdu_count_index")
3831 vdu_name
= vca_deployed
.get("vdu_name")
3832 vnf_index
= vca_deployed
.get("member-vnf-index")
3833 if terminate_primitives
and vca_deployed
.get("needed_terminate"):
3834 for seq
in terminate_primitives
:
3835 # For each sequence in list, get primitive and call _ns_execute_primitive()
3836 step
= "Calling terminate action for vnf_member_index={} primitive={}".format(
3837 vnf_index
, seq
.get("name")
3839 self
.logger
.debug(logging_text
+ step
)
3840 # Create the primitive for each sequence, i.e. "primitive": "touch"
3841 primitive
= seq
.get("name")
3842 mapped_primitive_params
= self
._get
_terminate
_primitive
_params
(
3847 self
._add
_suboperation
(
3854 mapped_primitive_params
,
3856 # Sub-operations: Call _ns_execute_primitive() instead of action()
3858 result
, result_detail
= await self
._ns
_execute
_primitive
(
3859 vca_deployed
["ee_id"],
3861 mapped_primitive_params
,
3865 except LcmException
:
3866 # this happens when VCA is not deployed. In this case it is not needed to terminate
3868 result_ok
= ["COMPLETED", "PARTIALLY_COMPLETED"]
3869 if result
not in result_ok
:
3871 "terminate_primitive {} for vnf_member_index={} fails with "
3872 "error {}".format(seq
.get("name"), vnf_index
, result_detail
)
3874 # set that this VCA do not need terminated
3875 db_update_entry
= "_admin.deployed.VCA.{}.needed_terminate".format(
3879 "nsrs", db_nslcmop
["nsInstanceId"], {db_update_entry
: False}
3882 if vca_deployed
.get("prometheus_jobs") and self
.prometheus
:
3883 await self
.prometheus
.update(remove_jobs
=vca_deployed
["prometheus_jobs"])
3886 await self
.vca_map
[vca_type
].delete_execution_environment(
3887 vca_deployed
["ee_id"],
3888 scaling_in
=scaling_in
,
3893 async def _delete_all_N2VC(self
, db_nsr
: dict, vca_id
: str = None):
3894 self
._write
_all
_config
_status
(db_nsr
=db_nsr
, status
="TERMINATING")
3895 namespace
= "." + db_nsr
["_id"]
3897 await self
.n2vc
.delete_namespace(
3898 namespace
=namespace
,
3899 total_timeout
=self
.timeout_charm_delete
,
3902 except N2VCNotFound
: # already deleted. Skip
3904 self
._write
_all
_config
_status
(db_nsr
=db_nsr
, status
="DELETED")
3906 async def _terminate_RO(
3907 self
, logging_text
, nsr_deployed
, nsr_id
, nslcmop_id
, stage
3910 Terminates a deployment from RO
3911 :param logging_text:
3912 :param nsr_deployed: db_nsr._admin.deployed
3915 :param stage: list of string with the content to write on db_nslcmop.detailed-status.
3916 this method will update only the index 2, but it will write on database the concatenated content of the list
3921 ro_nsr_id
= ro_delete_action
= None
3922 if nsr_deployed
and nsr_deployed
.get("RO"):
3923 ro_nsr_id
= nsr_deployed
["RO"].get("nsr_id")
3924 ro_delete_action
= nsr_deployed
["RO"].get("nsr_delete_action_id")
3927 stage
[2] = "Deleting ns from VIM."
3928 db_nsr_update
["detailed-status"] = " ".join(stage
)
3929 self
._write
_op
_status
(nslcmop_id
, stage
)
3930 self
.logger
.debug(logging_text
+ stage
[2])
3931 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
3932 self
._write
_op
_status
(nslcmop_id
, stage
)
3933 desc
= await self
.RO
.delete("ns", ro_nsr_id
)
3934 ro_delete_action
= desc
["action_id"]
3936 "_admin.deployed.RO.nsr_delete_action_id"
3937 ] = ro_delete_action
3938 db_nsr_update
["_admin.deployed.RO.nsr_id"] = None
3939 db_nsr_update
["_admin.deployed.RO.nsr_status"] = "DELETED"
3940 if ro_delete_action
:
3941 # wait until NS is deleted from VIM
3942 stage
[2] = "Waiting ns deleted from VIM."
3943 detailed_status_old
= None
3947 + " RO_id={} ro_delete_action={}".format(
3948 ro_nsr_id
, ro_delete_action
3951 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
3952 self
._write
_op
_status
(nslcmop_id
, stage
)
3954 delete_timeout
= 20 * 60 # 20 minutes
3955 while delete_timeout
> 0:
3956 desc
= await self
.RO
.show(
3958 item_id_name
=ro_nsr_id
,
3959 extra_item
="action",
3960 extra_item_id
=ro_delete_action
,
3964 self
._on
_update
_ro
_db
(nsrs_id
=nsr_id
, ro_descriptor
=desc
)
3966 ns_status
, ns_status_info
= self
.RO
.check_action_status(desc
)
3967 if ns_status
== "ERROR":
3968 raise ROclient
.ROClientException(ns_status_info
)
3969 elif ns_status
== "BUILD":
3970 stage
[2] = "Deleting from VIM {}".format(ns_status_info
)
3971 elif ns_status
== "ACTIVE":
3972 db_nsr_update
["_admin.deployed.RO.nsr_delete_action_id"] = None
3973 db_nsr_update
["_admin.deployed.RO.nsr_status"] = "DELETED"
3978 ), "ROclient.check_action_status returns unknown {}".format(
3981 if stage
[2] != detailed_status_old
:
3982 detailed_status_old
= stage
[2]
3983 db_nsr_update
["detailed-status"] = " ".join(stage
)
3984 self
._write
_op
_status
(nslcmop_id
, stage
)
3985 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
3986 await asyncio
.sleep(5, loop
=self
.loop
)
3988 else: # delete_timeout <= 0:
3989 raise ROclient
.ROClientException(
3990 "Timeout waiting ns deleted from VIM"
3993 except Exception as e
:
3994 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
3996 isinstance(e
, ROclient
.ROClientException
) and e
.http_code
== 404
3998 db_nsr_update
["_admin.deployed.RO.nsr_id"] = None
3999 db_nsr_update
["_admin.deployed.RO.nsr_status"] = "DELETED"
4000 db_nsr_update
["_admin.deployed.RO.nsr_delete_action_id"] = None
4002 logging_text
+ "RO_ns_id={} already deleted".format(ro_nsr_id
)
4005 isinstance(e
, ROclient
.ROClientException
) and e
.http_code
== 409
4007 failed_detail
.append("delete conflict: {}".format(e
))
4010 + "RO_ns_id={} delete conflict: {}".format(ro_nsr_id
, e
)
4013 failed_detail
.append("delete error: {}".format(e
))
4015 logging_text
+ "RO_ns_id={} delete error: {}".format(ro_nsr_id
, e
)
4019 if not failed_detail
and deep_get(nsr_deployed
, ("RO", "nsd_id")):
4020 ro_nsd_id
= nsr_deployed
["RO"]["nsd_id"]
4022 stage
[2] = "Deleting nsd from RO."
4023 db_nsr_update
["detailed-status"] = " ".join(stage
)
4024 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
4025 self
._write
_op
_status
(nslcmop_id
, stage
)
4026 await self
.RO
.delete("nsd", ro_nsd_id
)
4028 logging_text
+ "ro_nsd_id={} deleted".format(ro_nsd_id
)
4030 db_nsr_update
["_admin.deployed.RO.nsd_id"] = None
4031 except Exception as e
:
4033 isinstance(e
, ROclient
.ROClientException
) and e
.http_code
== 404
4035 db_nsr_update
["_admin.deployed.RO.nsd_id"] = None
4037 logging_text
+ "ro_nsd_id={} already deleted".format(ro_nsd_id
)
4040 isinstance(e
, ROclient
.ROClientException
) and e
.http_code
== 409
4042 failed_detail
.append(
4043 "ro_nsd_id={} delete conflict: {}".format(ro_nsd_id
, e
)
4045 self
.logger
.debug(logging_text
+ failed_detail
[-1])
4047 failed_detail
.append(
4048 "ro_nsd_id={} delete error: {}".format(ro_nsd_id
, e
)
4050 self
.logger
.error(logging_text
+ failed_detail
[-1])
4052 if not failed_detail
and deep_get(nsr_deployed
, ("RO", "vnfd")):
4053 for index
, vnf_deployed
in enumerate(nsr_deployed
["RO"]["vnfd"]):
4054 if not vnf_deployed
or not vnf_deployed
["id"]:
4057 ro_vnfd_id
= vnf_deployed
["id"]
4060 ] = "Deleting member_vnf_index={} ro_vnfd_id={} from RO.".format(
4061 vnf_deployed
["member-vnf-index"], ro_vnfd_id
4063 db_nsr_update
["detailed-status"] = " ".join(stage
)
4064 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
4065 self
._write
_op
_status
(nslcmop_id
, stage
)
4066 await self
.RO
.delete("vnfd", ro_vnfd_id
)
4068 logging_text
+ "ro_vnfd_id={} deleted".format(ro_vnfd_id
)
4070 db_nsr_update
["_admin.deployed.RO.vnfd.{}.id".format(index
)] = None
4071 except Exception as e
:
4073 isinstance(e
, ROclient
.ROClientException
) and e
.http_code
== 404
4076 "_admin.deployed.RO.vnfd.{}.id".format(index
)
4080 + "ro_vnfd_id={} already deleted ".format(ro_vnfd_id
)
4083 isinstance(e
, ROclient
.ROClientException
) and e
.http_code
== 409
4085 failed_detail
.append(
4086 "ro_vnfd_id={} delete conflict: {}".format(ro_vnfd_id
, e
)
4088 self
.logger
.debug(logging_text
+ failed_detail
[-1])
4090 failed_detail
.append(
4091 "ro_vnfd_id={} delete error: {}".format(ro_vnfd_id
, e
)
4093 self
.logger
.error(logging_text
+ failed_detail
[-1])
4096 stage
[2] = "Error deleting from VIM"
4098 stage
[2] = "Deleted from VIM"
4099 db_nsr_update
["detailed-status"] = " ".join(stage
)
4100 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
4101 self
._write
_op
_status
(nslcmop_id
, stage
)
4104 raise LcmException("; ".join(failed_detail
))
4106 async def terminate(self
, nsr_id
, nslcmop_id
):
4107 # Try to lock HA task here
4108 task_is_locked_by_me
= self
.lcm_tasks
.lock_HA("ns", "nslcmops", nslcmop_id
)
4109 if not task_is_locked_by_me
:
4112 logging_text
= "Task ns={} terminate={} ".format(nsr_id
, nslcmop_id
)
4113 self
.logger
.debug(logging_text
+ "Enter")
4114 timeout_ns_terminate
= self
.timeout_ns_terminate
4117 operation_params
= None
4119 error_list
= [] # annotates all failed error messages
4120 db_nslcmop_update
= {}
4121 autoremove
= False # autoremove after terminated
4122 tasks_dict_info
= {}
4125 "Stage 1/3: Preparing task.",
4126 "Waiting for previous operations to terminate.",
4129 # ^ contains [stage, step, VIM-status]
4131 # wait for any previous tasks in process
4132 await self
.lcm_tasks
.waitfor_related_HA("ns", "nslcmops", nslcmop_id
)
4134 stage
[1] = "Getting nslcmop={} from db.".format(nslcmop_id
)
4135 db_nslcmop
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
})
4136 operation_params
= db_nslcmop
.get("operationParams") or {}
4137 if operation_params
.get("timeout_ns_terminate"):
4138 timeout_ns_terminate
= operation_params
["timeout_ns_terminate"]
4139 stage
[1] = "Getting nsr={} from db.".format(nsr_id
)
4140 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
4142 db_nsr_update
["operational-status"] = "terminating"
4143 db_nsr_update
["config-status"] = "terminating"
4144 self
._write
_ns
_status
(
4146 ns_state
="TERMINATING",
4147 current_operation
="TERMINATING",
4148 current_operation_id
=nslcmop_id
,
4149 other_update
=db_nsr_update
,
4151 self
._write
_op
_status
(op_id
=nslcmop_id
, queuePosition
=0, stage
=stage
)
4152 nsr_deployed
= deepcopy(db_nsr
["_admin"].get("deployed")) or {}
4153 if db_nsr
["_admin"]["nsState"] == "NOT_INSTANTIATED":
4156 stage
[1] = "Getting vnf descriptors from db."
4157 db_vnfrs_list
= self
.db
.get_list("vnfrs", {"nsr-id-ref": nsr_id
})
4159 db_vnfr
["member-vnf-index-ref"]: db_vnfr
for db_vnfr
in db_vnfrs_list
4161 db_vnfds_from_id
= {}
4162 db_vnfds_from_member_index
= {}
4164 for vnfr
in db_vnfrs_list
:
4165 vnfd_id
= vnfr
["vnfd-id"]
4166 if vnfd_id
not in db_vnfds_from_id
:
4167 vnfd
= self
.db
.get_one("vnfds", {"_id": vnfd_id
})
4168 db_vnfds_from_id
[vnfd_id
] = vnfd
4169 db_vnfds_from_member_index
[
4170 vnfr
["member-vnf-index-ref"]
4171 ] = db_vnfds_from_id
[vnfd_id
]
4173 # Destroy individual execution environments when there are terminating primitives.
4174 # Rest of EE will be deleted at once
4175 # TODO - check before calling _destroy_N2VC
4176 # if not operation_params.get("skip_terminate_primitives"):#
4177 # or not vca.get("needed_terminate"):
4178 stage
[0] = "Stage 2/3 execute terminating primitives."
4179 self
.logger
.debug(logging_text
+ stage
[0])
4180 stage
[1] = "Looking execution environment that needs terminate."
4181 self
.logger
.debug(logging_text
+ stage
[1])
4183 for vca_index
, vca
in enumerate(get_iterable(nsr_deployed
, "VCA")):
4184 config_descriptor
= None
4185 vca_member_vnf_index
= vca
.get("member-vnf-index")
4186 vca_id
= self
.get_vca_id(
4187 db_vnfrs_dict
.get(vca_member_vnf_index
)
4188 if vca_member_vnf_index
4192 if not vca
or not vca
.get("ee_id"):
4194 if not vca
.get("member-vnf-index"):
4196 config_descriptor
= db_nsr
.get("ns-configuration")
4197 elif vca
.get("vdu_id"):
4198 db_vnfd
= db_vnfds_from_member_index
[vca
["member-vnf-index"]]
4199 config_descriptor
= get_configuration(db_vnfd
, vca
.get("vdu_id"))
4200 elif vca
.get("kdu_name"):
4201 db_vnfd
= db_vnfds_from_member_index
[vca
["member-vnf-index"]]
4202 config_descriptor
= get_configuration(db_vnfd
, vca
.get("kdu_name"))
4204 db_vnfd
= db_vnfds_from_member_index
[vca
["member-vnf-index"]]
4205 config_descriptor
= get_configuration(db_vnfd
, db_vnfd
["id"])
4206 vca_type
= vca
.get("type")
4207 exec_terminate_primitives
= not operation_params
.get(
4208 "skip_terminate_primitives"
4209 ) and vca
.get("needed_terminate")
4210 # For helm we must destroy_ee. Also for native_charm, as juju_model cannot be deleted if there are
4211 # pending native charms
4213 True if vca_type
in ("helm", "helm-v3", "native_charm") else False
4215 # self.logger.debug(logging_text + "vca_index: {}, ee_id: {}, vca_type: {} destroy_ee: {}".format(
4216 # vca_index, vca.get("ee_id"), vca_type, destroy_ee))
4217 task
= asyncio
.ensure_future(
4225 exec_terminate_primitives
,
4229 tasks_dict_info
[task
] = "Terminating VCA {}".format(vca
.get("ee_id"))
4231 # wait for pending tasks of terminate primitives
4235 + "Waiting for tasks {}".format(list(tasks_dict_info
.keys()))
4237 error_list
= await self
._wait
_for
_tasks
(
4240 min(self
.timeout_charm_delete
, timeout_ns_terminate
),
4244 tasks_dict_info
.clear()
4246 return # raise LcmException("; ".join(error_list))
4248 # remove All execution environments at once
4249 stage
[0] = "Stage 3/3 delete all."
4251 if nsr_deployed
.get("VCA"):
4252 stage
[1] = "Deleting all execution environments."
4253 self
.logger
.debug(logging_text
+ stage
[1])
4254 vca_id
= self
.get_vca_id({}, db_nsr
)
4255 task_delete_ee
= asyncio
.ensure_future(
4257 self
._delete
_all
_N
2VC
(db_nsr
=db_nsr
, vca_id
=vca_id
),
4258 timeout
=self
.timeout_charm_delete
,
4261 # task_delete_ee = asyncio.ensure_future(self.n2vc.delete_namespace(namespace="." + nsr_id))
4262 tasks_dict_info
[task_delete_ee
] = "Terminating all VCA"
4264 # Delete from k8scluster
4265 stage
[1] = "Deleting KDUs."
4266 self
.logger
.debug(logging_text
+ stage
[1])
4267 # print(nsr_deployed)
4268 for kdu
in get_iterable(nsr_deployed
, "K8s"):
4269 if not kdu
or not kdu
.get("kdu-instance"):
4271 kdu_instance
= kdu
.get("kdu-instance")
4272 if kdu
.get("k8scluster-type") in self
.k8scluster_map
:
4273 # TODO: Uninstall kdu instances taking into account they could be deployed in different VIMs
4274 vca_id
= self
.get_vca_id({}, db_nsr
)
4275 task_delete_kdu_instance
= asyncio
.ensure_future(
4276 self
.k8scluster_map
[kdu
["k8scluster-type"]].uninstall(
4277 cluster_uuid
=kdu
.get("k8scluster-uuid"),
4278 kdu_instance
=kdu_instance
,
4285 + "Unknown k8s deployment type {}".format(
4286 kdu
.get("k8scluster-type")
4291 task_delete_kdu_instance
4292 ] = "Terminating KDU '{}'".format(kdu
.get("kdu-name"))
4295 stage
[1] = "Deleting ns from VIM."
4297 task_delete_ro
= asyncio
.ensure_future(
4298 self
._terminate
_ng
_ro
(
4299 logging_text
, nsr_deployed
, nsr_id
, nslcmop_id
, stage
4303 task_delete_ro
= asyncio
.ensure_future(
4305 logging_text
, nsr_deployed
, nsr_id
, nslcmop_id
, stage
4308 tasks_dict_info
[task_delete_ro
] = "Removing deployment from VIM"
4310 # rest of staff will be done at finally
4313 ROclient
.ROClientException
,
4318 self
.logger
.error(logging_text
+ "Exit Exception {}".format(e
))
4320 except asyncio
.CancelledError
:
4322 logging_text
+ "Cancelled Exception while '{}'".format(stage
[1])
4324 exc
= "Operation was cancelled"
4325 except Exception as e
:
4326 exc
= traceback
.format_exc()
4327 self
.logger
.critical(
4328 logging_text
+ "Exit Exception while '{}': {}".format(stage
[1], e
),
4333 error_list
.append(str(exc
))
4335 # wait for pending tasks
4337 stage
[1] = "Waiting for terminate pending tasks."
4338 self
.logger
.debug(logging_text
+ stage
[1])
4339 error_list
+= await self
._wait
_for
_tasks
(
4342 timeout_ns_terminate
,
4346 stage
[1] = stage
[2] = ""
4347 except asyncio
.CancelledError
:
4348 error_list
.append("Cancelled")
4349 # TODO cancell all tasks
4350 except Exception as exc
:
4351 error_list
.append(str(exc
))
4352 # update status at database
4354 error_detail
= "; ".join(error_list
)
4355 # self.logger.error(logging_text + error_detail)
4356 error_description_nslcmop
= "{} Detail: {}".format(
4357 stage
[0], error_detail
4359 error_description_nsr
= "Operation: TERMINATING.{}, {}.".format(
4360 nslcmop_id
, stage
[0]
4363 db_nsr_update
["operational-status"] = "failed"
4364 db_nsr_update
["detailed-status"] = (
4365 error_description_nsr
+ " Detail: " + error_detail
4367 db_nslcmop_update
["detailed-status"] = error_detail
4368 nslcmop_operation_state
= "FAILED"
4372 error_description_nsr
= error_description_nslcmop
= None
4373 ns_state
= "NOT_INSTANTIATED"
4374 db_nsr_update
["operational-status"] = "terminated"
4375 db_nsr_update
["detailed-status"] = "Done"
4376 db_nsr_update
["_admin.nsState"] = "NOT_INSTANTIATED"
4377 db_nslcmop_update
["detailed-status"] = "Done"
4378 nslcmop_operation_state
= "COMPLETED"
4381 self
._write
_ns
_status
(
4384 current_operation
="IDLE",
4385 current_operation_id
=None,
4386 error_description
=error_description_nsr
,
4387 error_detail
=error_detail
,
4388 other_update
=db_nsr_update
,
4390 self
._write
_op
_status
(
4393 error_message
=error_description_nslcmop
,
4394 operation_state
=nslcmop_operation_state
,
4395 other_update
=db_nslcmop_update
,
4397 if ns_state
== "NOT_INSTANTIATED":
4401 {"nsr-id-ref": nsr_id
},
4402 {"_admin.nsState": "NOT_INSTANTIATED"},
4404 except DbException
as e
:
4407 + "Error writing VNFR status for nsr-id-ref: {} -> {}".format(
4411 if operation_params
:
4412 autoremove
= operation_params
.get("autoremove", False)
4413 if nslcmop_operation_state
:
4415 await self
.msg
.aiowrite(
4420 "nslcmop_id": nslcmop_id
,
4421 "operationState": nslcmop_operation_state
,
4422 "autoremove": autoremove
,
4426 except Exception as e
:
4428 logging_text
+ "kafka_write notification Exception {}".format(e
)
4431 self
.logger
.debug(logging_text
+ "Exit")
4432 self
.lcm_tasks
.remove("ns", nsr_id
, nslcmop_id
, "ns_terminate")
4434 async def _wait_for_tasks(
4435 self
, logging_text
, created_tasks_info
, timeout
, stage
, nslcmop_id
, nsr_id
=None
4438 error_detail_list
= []
4440 pending_tasks
= list(created_tasks_info
.keys())
4441 num_tasks
= len(pending_tasks
)
4443 stage
[1] = "{}/{}.".format(num_done
, num_tasks
)
4444 self
._write
_op
_status
(nslcmop_id
, stage
)
4445 while pending_tasks
:
4447 _timeout
= timeout
+ time_start
- time()
4448 done
, pending_tasks
= await asyncio
.wait(
4449 pending_tasks
, timeout
=_timeout
, return_when
=asyncio
.FIRST_COMPLETED
4451 num_done
+= len(done
)
4452 if not done
: # Timeout
4453 for task
in pending_tasks
:
4454 new_error
= created_tasks_info
[task
] + ": Timeout"
4455 error_detail_list
.append(new_error
)
4456 error_list
.append(new_error
)
4459 if task
.cancelled():
4462 exc
= task
.exception()
4464 if isinstance(exc
, asyncio
.TimeoutError
):
4466 new_error
= created_tasks_info
[task
] + ": {}".format(exc
)
4467 error_list
.append(created_tasks_info
[task
])
4468 error_detail_list
.append(new_error
)
4475 ROclient
.ROClientException
,
4481 self
.logger
.error(logging_text
+ new_error
)
4483 exc_traceback
= "".join(
4484 traceback
.format_exception(None, exc
, exc
.__traceback
__)
4488 + created_tasks_info
[task
]
4494 logging_text
+ created_tasks_info
[task
] + ": Done"
4496 stage
[1] = "{}/{}.".format(num_done
, num_tasks
)
4498 stage
[1] += " Errors: " + ". ".join(error_detail_list
) + "."
4499 if nsr_id
: # update also nsr
4504 "errorDescription": "Error at: " + ", ".join(error_list
),
4505 "errorDetail": ". ".join(error_detail_list
),
4508 self
._write
_op
_status
(nslcmop_id
, stage
)
4509 return error_detail_list
4512 def _map_primitive_params(primitive_desc
, params
, instantiation_params
):
4514 Generates the params to be provided to charm before executing primitive. If user does not provide a parameter,
4515 The default-value is used. If it is between < > it look for a value at instantiation_params
4516 :param primitive_desc: portion of VNFD/NSD that describes primitive
4517 :param params: Params provided by user
4518 :param instantiation_params: Instantiation params provided by user
4519 :return: a dictionary with the calculated params
4521 calculated_params
= {}
4522 for parameter
in primitive_desc
.get("parameter", ()):
4523 param_name
= parameter
["name"]
4524 if param_name
in params
:
4525 calculated_params
[param_name
] = params
[param_name
]
4526 elif "default-value" in parameter
or "value" in parameter
:
4527 if "value" in parameter
:
4528 calculated_params
[param_name
] = parameter
["value"]
4530 calculated_params
[param_name
] = parameter
["default-value"]
4532 isinstance(calculated_params
[param_name
], str)
4533 and calculated_params
[param_name
].startswith("<")
4534 and calculated_params
[param_name
].endswith(">")
4536 if calculated_params
[param_name
][1:-1] in instantiation_params
:
4537 calculated_params
[param_name
] = instantiation_params
[
4538 calculated_params
[param_name
][1:-1]
4542 "Parameter {} needed to execute primitive {} not provided".format(
4543 calculated_params
[param_name
], primitive_desc
["name"]
4548 "Parameter {} needed to execute primitive {} not provided".format(
4549 param_name
, primitive_desc
["name"]
4553 if isinstance(calculated_params
[param_name
], (dict, list, tuple)):
4554 calculated_params
[param_name
] = yaml
.safe_dump(
4555 calculated_params
[param_name
], default_flow_style
=True, width
=256
4557 elif isinstance(calculated_params
[param_name
], str) and calculated_params
[
4559 ].startswith("!!yaml "):
4560 calculated_params
[param_name
] = calculated_params
[param_name
][7:]
4561 if parameter
.get("data-type") == "INTEGER":
4563 calculated_params
[param_name
] = int(calculated_params
[param_name
])
4564 except ValueError: # error converting string to int
4566 "Parameter {} of primitive {} must be integer".format(
4567 param_name
, primitive_desc
["name"]
4570 elif parameter
.get("data-type") == "BOOLEAN":
4571 calculated_params
[param_name
] = not (
4572 (str(calculated_params
[param_name
])).lower() == "false"
4575 # add always ns_config_info if primitive name is config
4576 if primitive_desc
["name"] == "config":
4577 if "ns_config_info" in instantiation_params
:
4578 calculated_params
["ns_config_info"] = instantiation_params
[
4581 return calculated_params
4583 def _look_for_deployed_vca(
4590 ee_descriptor_id
=None,
4592 # find vca_deployed record for this action. Raise LcmException if not found or there is not any id.
4593 for vca
in deployed_vca
:
4596 if member_vnf_index
!= vca
["member-vnf-index"] or vdu_id
!= vca
["vdu_id"]:
4599 vdu_count_index
is not None
4600 and vdu_count_index
!= vca
["vdu_count_index"]
4603 if kdu_name
and kdu_name
!= vca
["kdu_name"]:
4605 if ee_descriptor_id
and ee_descriptor_id
!= vca
["ee_descriptor_id"]:
4609 # vca_deployed not found
4611 "charm for member_vnf_index={} vdu_id={}.{} kdu_name={} execution-environment-list.id={}"
4612 " is not deployed".format(
4621 ee_id
= vca
.get("ee_id")
4623 "type", "lxc_proxy_charm"
4624 ) # default value for backward compatibility - proxy charm
4627 "charm for member_vnf_index={} vdu_id={} kdu_name={} vdu_count_index={} has not "
4628 "execution environment".format(
4629 member_vnf_index
, vdu_id
, kdu_name
, vdu_count_index
4632 return ee_id
, vca_type
4634 async def _ns_execute_primitive(
4640 retries_interval
=30,
4647 if primitive
== "config":
4648 primitive_params
= {"params": primitive_params
}
4650 vca_type
= vca_type
or "lxc_proxy_charm"
4654 output
= await asyncio
.wait_for(
4655 self
.vca_map
[vca_type
].exec_primitive(
4657 primitive_name
=primitive
,
4658 params_dict
=primitive_params
,
4659 progress_timeout
=self
.timeout_progress_primitive
,
4660 total_timeout
=self
.timeout_primitive
,
4665 timeout
=timeout
or self
.timeout_primitive
,
4669 except asyncio
.CancelledError
:
4671 except Exception as e
: # asyncio.TimeoutError
4672 if isinstance(e
, asyncio
.TimeoutError
):
4677 "Error executing action {} on {} -> {}".format(
4682 await asyncio
.sleep(retries_interval
, loop
=self
.loop
)
4684 return "FAILED", str(e
)
4686 return "COMPLETED", output
4688 except (LcmException
, asyncio
.CancelledError
):
4690 except Exception as e
:
4691 return "FAIL", "Error executing action {}: {}".format(primitive
, e
)
4693 async def vca_status_refresh(self
, nsr_id
, nslcmop_id
):
4695 Updating the vca_status with latest juju information in nsrs record
4696 :param: nsr_id: Id of the nsr
4697 :param: nslcmop_id: Id of the nslcmop
4701 self
.logger
.debug("Task ns={} action={} Enter".format(nsr_id
, nslcmop_id
))
4702 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
4703 vca_id
= self
.get_vca_id({}, db_nsr
)
4704 if db_nsr
["_admin"]["deployed"]["K8s"]:
4705 for _
, k8s
in enumerate(db_nsr
["_admin"]["deployed"]["K8s"]):
4706 cluster_uuid
, kdu_instance
, cluster_type
= (
4707 k8s
["k8scluster-uuid"],
4708 k8s
["kdu-instance"],
4709 k8s
["k8scluster-type"],
4711 await self
._on
_update
_k
8s
_db
(
4712 cluster_uuid
=cluster_uuid
,
4713 kdu_instance
=kdu_instance
,
4714 filter={"_id": nsr_id
},
4716 cluster_type
=cluster_type
,
4719 for vca_index
, _
in enumerate(db_nsr
["_admin"]["deployed"]["VCA"]):
4720 table
, filter = "nsrs", {"_id": nsr_id
}
4721 path
= "_admin.deployed.VCA.{}.".format(vca_index
)
4722 await self
._on
_update
_n
2vc
_db
(table
, filter, path
, {})
4724 self
.logger
.debug("Task ns={} action={} Exit".format(nsr_id
, nslcmop_id
))
4725 self
.lcm_tasks
.remove("ns", nsr_id
, nslcmop_id
, "ns_vca_status_refresh")
4727 async def action(self
, nsr_id
, nslcmop_id
):
4728 # Try to lock HA task here
4729 task_is_locked_by_me
= self
.lcm_tasks
.lock_HA("ns", "nslcmops", nslcmop_id
)
4730 if not task_is_locked_by_me
:
4733 logging_text
= "Task ns={} action={} ".format(nsr_id
, nslcmop_id
)
4734 self
.logger
.debug(logging_text
+ "Enter")
4735 # get all needed from database
4739 db_nslcmop_update
= {}
4740 nslcmop_operation_state
= None
4741 error_description_nslcmop
= None
4744 # wait for any previous tasks in process
4745 step
= "Waiting for previous operations to terminate"
4746 await self
.lcm_tasks
.waitfor_related_HA("ns", "nslcmops", nslcmop_id
)
4748 self
._write
_ns
_status
(
4751 current_operation
="RUNNING ACTION",
4752 current_operation_id
=nslcmop_id
,
4755 step
= "Getting information from database"
4756 db_nslcmop
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
})
4757 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
4758 if db_nslcmop
["operationParams"].get("primitive_params"):
4759 db_nslcmop
["operationParams"]["primitive_params"] = json
.loads(
4760 db_nslcmop
["operationParams"]["primitive_params"]
4763 nsr_deployed
= db_nsr
["_admin"].get("deployed")
4764 vnf_index
= db_nslcmop
["operationParams"].get("member_vnf_index")
4765 vdu_id
= db_nslcmop
["operationParams"].get("vdu_id")
4766 kdu_name
= db_nslcmop
["operationParams"].get("kdu_name")
4767 vdu_count_index
= db_nslcmop
["operationParams"].get("vdu_count_index")
4768 primitive
= db_nslcmop
["operationParams"]["primitive"]
4769 primitive_params
= db_nslcmop
["operationParams"]["primitive_params"]
4770 timeout_ns_action
= db_nslcmop
["operationParams"].get(
4771 "timeout_ns_action", self
.timeout_primitive
4775 step
= "Getting vnfr from database"
4776 db_vnfr
= self
.db
.get_one(
4777 "vnfrs", {"member-vnf-index-ref": vnf_index
, "nsr-id-ref": nsr_id
}
4779 if db_vnfr
.get("kdur"):
4781 for kdur
in db_vnfr
["kdur"]:
4782 if kdur
.get("additionalParams"):
4783 kdur
["additionalParams"] = json
.loads(
4784 kdur
["additionalParams"]
4786 kdur_list
.append(kdur
)
4787 db_vnfr
["kdur"] = kdur_list
4788 step
= "Getting vnfd from database"
4789 db_vnfd
= self
.db
.get_one("vnfds", {"_id": db_vnfr
["vnfd-id"]})
4791 step
= "Getting nsd from database"
4792 db_nsd
= self
.db
.get_one("nsds", {"_id": db_nsr
["nsd-id"]})
4794 vca_id
= self
.get_vca_id(db_vnfr
, db_nsr
)
4795 # for backward compatibility
4796 if nsr_deployed
and isinstance(nsr_deployed
.get("VCA"), dict):
4797 nsr_deployed
["VCA"] = list(nsr_deployed
["VCA"].values())
4798 db_nsr_update
["_admin.deployed.VCA"] = nsr_deployed
["VCA"]
4799 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
4801 # look for primitive
4802 config_primitive_desc
= descriptor_configuration
= None
4804 descriptor_configuration
= get_configuration(db_vnfd
, vdu_id
)
4806 descriptor_configuration
= get_configuration(db_vnfd
, kdu_name
)
4808 descriptor_configuration
= get_configuration(db_vnfd
, db_vnfd
["id"])
4810 descriptor_configuration
= db_nsd
.get("ns-configuration")
4812 if descriptor_configuration
and descriptor_configuration
.get(
4815 for config_primitive
in descriptor_configuration
["config-primitive"]:
4816 if config_primitive
["name"] == primitive
:
4817 config_primitive_desc
= config_primitive
4820 if not config_primitive_desc
:
4821 if not (kdu_name
and primitive
in ("upgrade", "rollback", "status")):
4823 "Primitive {} not found at [ns|vnf|vdu]-configuration:config-primitive ".format(
4827 primitive_name
= primitive
4828 ee_descriptor_id
= None
4830 primitive_name
= config_primitive_desc
.get(
4831 "execution-environment-primitive", primitive
4833 ee_descriptor_id
= config_primitive_desc
.get(
4834 "execution-environment-ref"
4840 (x
for x
in db_vnfr
["vdur"] if x
["vdu-id-ref"] == vdu_id
), None
4842 desc_params
= parse_yaml_strings(vdur
.get("additionalParams"))
4845 (x
for x
in db_vnfr
["kdur"] if x
["kdu-name"] == kdu_name
), None
4847 desc_params
= parse_yaml_strings(kdur
.get("additionalParams"))
4849 desc_params
= parse_yaml_strings(
4850 db_vnfr
.get("additionalParamsForVnf")
4853 desc_params
= parse_yaml_strings(db_nsr
.get("additionalParamsForNs"))
4854 if kdu_name
and get_configuration(db_vnfd
, kdu_name
):
4855 kdu_configuration
= get_configuration(db_vnfd
, kdu_name
)
4857 for primitive
in kdu_configuration
.get("initial-config-primitive", []):
4858 actions
.add(primitive
["name"])
4859 for primitive
in kdu_configuration
.get("config-primitive", []):
4860 actions
.add(primitive
["name"])
4861 kdu_action
= True if primitive_name
in actions
else False
4863 # TODO check if ns is in a proper status
4865 primitive_name
in ("upgrade", "rollback", "status") or kdu_action
4867 # kdur and desc_params already set from before
4868 if primitive_params
:
4869 desc_params
.update(primitive_params
)
4870 # TODO Check if we will need something at vnf level
4871 for index
, kdu
in enumerate(get_iterable(nsr_deployed
, "K8s")):
4873 kdu_name
== kdu
["kdu-name"]
4874 and kdu
["member-vnf-index"] == vnf_index
4879 "KDU '{}' for vnf '{}' not deployed".format(kdu_name
, vnf_index
)
4882 if kdu
.get("k8scluster-type") not in self
.k8scluster_map
:
4883 msg
= "unknown k8scluster-type '{}'".format(
4884 kdu
.get("k8scluster-type")
4886 raise LcmException(msg
)
4889 "collection": "nsrs",
4890 "filter": {"_id": nsr_id
},
4891 "path": "_admin.deployed.K8s.{}".format(index
),
4895 + "Exec k8s {} on {}.{}".format(primitive_name
, vnf_index
, kdu_name
)
4897 step
= "Executing kdu {}".format(primitive_name
)
4898 if primitive_name
== "upgrade":
4899 if desc_params
.get("kdu_model"):
4900 kdu_model
= desc_params
.get("kdu_model")
4901 del desc_params
["kdu_model"]
4903 kdu_model
= kdu
.get("kdu-model")
4904 parts
= kdu_model
.split(sep
=":")
4906 kdu_model
= parts
[0]
4907 if desc_params
.get("kdu_atomic_upgrade"):
4908 atomic_upgrade
= desc_params
.get("kdu_atomic_upgrade").lower() in ("yes", "true", "1")
4909 del desc_params
["kdu_atomic_upgrade"]
4911 atomic_upgrade
= True
4913 detailed_status
= await asyncio
.wait_for(
4914 self
.k8scluster_map
[kdu
["k8scluster-type"]].upgrade(
4915 cluster_uuid
=kdu
.get("k8scluster-uuid"),
4916 kdu_instance
=kdu
.get("kdu-instance"),
4917 atomic
=atomic_upgrade
,
4918 kdu_model
=kdu_model
,
4921 timeout
=timeout_ns_action
,
4923 timeout
=timeout_ns_action
+ 10,
4926 logging_text
+ " Upgrade of kdu {} done".format(detailed_status
)
4928 elif primitive_name
== "rollback":
4929 detailed_status
= await asyncio
.wait_for(
4930 self
.k8scluster_map
[kdu
["k8scluster-type"]].rollback(
4931 cluster_uuid
=kdu
.get("k8scluster-uuid"),
4932 kdu_instance
=kdu
.get("kdu-instance"),
4935 timeout
=timeout_ns_action
,
4937 elif primitive_name
== "status":
4938 detailed_status
= await asyncio
.wait_for(
4939 self
.k8scluster_map
[kdu
["k8scluster-type"]].status_kdu(
4940 cluster_uuid
=kdu
.get("k8scluster-uuid"),
4941 kdu_instance
=kdu
.get("kdu-instance"),
4944 timeout
=timeout_ns_action
,
4947 kdu_instance
= kdu
.get("kdu-instance") or "{}-{}".format(
4948 kdu
["kdu-name"], nsr_id
4950 params
= self
._map
_primitive
_params
(
4951 config_primitive_desc
, primitive_params
, desc_params
4954 detailed_status
= await asyncio
.wait_for(
4955 self
.k8scluster_map
[kdu
["k8scluster-type"]].exec_primitive(
4956 cluster_uuid
=kdu
.get("k8scluster-uuid"),
4957 kdu_instance
=kdu_instance
,
4958 primitive_name
=primitive_name
,
4961 timeout
=timeout_ns_action
,
4964 timeout
=timeout_ns_action
,
4968 nslcmop_operation_state
= "COMPLETED"
4970 detailed_status
= ""
4971 nslcmop_operation_state
= "FAILED"
4973 ee_id
, vca_type
= self
._look
_for
_deployed
_vca
(
4974 nsr_deployed
["VCA"],
4975 member_vnf_index
=vnf_index
,
4977 vdu_count_index
=vdu_count_index
,
4978 ee_descriptor_id
=ee_descriptor_id
,
4980 for vca_index
, vca_deployed
in enumerate(
4981 db_nsr
["_admin"]["deployed"]["VCA"]
4983 if vca_deployed
.get("member-vnf-index") == vnf_index
:
4985 "collection": "nsrs",
4986 "filter": {"_id": nsr_id
},
4987 "path": "_admin.deployed.VCA.{}.".format(vca_index
),
4991 nslcmop_operation_state
,
4993 ) = await self
._ns
_execute
_primitive
(
4995 primitive
=primitive_name
,
4996 primitive_params
=self
._map
_primitive
_params
(
4997 config_primitive_desc
, primitive_params
, desc_params
4999 timeout
=timeout_ns_action
,
5005 db_nslcmop_update
["detailed-status"] = detailed_status
5006 error_description_nslcmop
= (
5007 detailed_status
if nslcmop_operation_state
== "FAILED" else ""
5011 + " task Done with result {} {}".format(
5012 nslcmop_operation_state
, detailed_status
5015 return # database update is called inside finally
5017 except (DbException
, LcmException
, N2VCException
, K8sException
) as e
:
5018 self
.logger
.error(logging_text
+ "Exit Exception {}".format(e
))
5020 except asyncio
.CancelledError
:
5022 logging_text
+ "Cancelled Exception while '{}'".format(step
)
5024 exc
= "Operation was cancelled"
5025 except asyncio
.TimeoutError
:
5026 self
.logger
.error(logging_text
+ "Timeout while '{}'".format(step
))
5028 except Exception as e
:
5029 exc
= traceback
.format_exc()
5030 self
.logger
.critical(
5031 logging_text
+ "Exit Exception {} {}".format(type(e
).__name
__, e
),
5040 ) = error_description_nslcmop
= "FAILED {}: {}".format(step
, exc
)
5041 nslcmop_operation_state
= "FAILED"
5043 self
._write
_ns
_status
(
5047 ], # TODO check if degraded. For the moment use previous status
5048 current_operation
="IDLE",
5049 current_operation_id
=None,
5050 # error_description=error_description_nsr,
5051 # error_detail=error_detail,
5052 other_update
=db_nsr_update
,
5055 self
._write
_op
_status
(
5058 error_message
=error_description_nslcmop
,
5059 operation_state
=nslcmop_operation_state
,
5060 other_update
=db_nslcmop_update
,
5063 if nslcmop_operation_state
:
5065 await self
.msg
.aiowrite(
5070 "nslcmop_id": nslcmop_id
,
5071 "operationState": nslcmop_operation_state
,
5075 except Exception as e
:
5077 logging_text
+ "kafka_write notification Exception {}".format(e
)
5079 self
.logger
.debug(logging_text
+ "Exit")
5080 self
.lcm_tasks
.remove("ns", nsr_id
, nslcmop_id
, "ns_action")
5081 return nslcmop_operation_state
, detailed_status
5083 async def scale(self
, nsr_id
, nslcmop_id
):
5084 # Try to lock HA task here
5085 task_is_locked_by_me
= self
.lcm_tasks
.lock_HA("ns", "nslcmops", nslcmop_id
)
5086 if not task_is_locked_by_me
:
5089 logging_text
= "Task ns={} scale={} ".format(nsr_id
, nslcmop_id
)
5090 stage
= ["", "", ""]
5091 tasks_dict_info
= {}
5092 # ^ stage, step, VIM progress
5093 self
.logger
.debug(logging_text
+ "Enter")
5094 # get all needed from database
5096 db_nslcmop_update
= {}
5099 # in case of error, indicates what part of scale was failed to put nsr at error status
5100 scale_process
= None
5101 old_operational_status
= ""
5102 old_config_status
= ""
5105 # wait for any previous tasks in process
5106 step
= "Waiting for previous operations to terminate"
5107 await self
.lcm_tasks
.waitfor_related_HA("ns", "nslcmops", nslcmop_id
)
5108 self
._write
_ns
_status
(
5111 current_operation
="SCALING",
5112 current_operation_id
=nslcmop_id
,
5115 step
= "Getting nslcmop from database"
5117 step
+ " after having waited for previous tasks to be completed"
5119 db_nslcmop
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
})
5121 step
= "Getting nsr from database"
5122 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
5123 old_operational_status
= db_nsr
["operational-status"]
5124 old_config_status
= db_nsr
["config-status"]
5126 step
= "Parsing scaling parameters"
5127 db_nsr_update
["operational-status"] = "scaling"
5128 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
5129 nsr_deployed
= db_nsr
["_admin"].get("deployed")
5131 vnf_index
= db_nslcmop
["operationParams"]["scaleVnfData"][
5133 ]["member-vnf-index"]
5134 scaling_group
= db_nslcmop
["operationParams"]["scaleVnfData"][
5136 ]["scaling-group-descriptor"]
5137 scaling_type
= db_nslcmop
["operationParams"]["scaleVnfData"]["scaleVnfType"]
5138 # for backward compatibility
5139 if nsr_deployed
and isinstance(nsr_deployed
.get("VCA"), dict):
5140 nsr_deployed
["VCA"] = list(nsr_deployed
["VCA"].values())
5141 db_nsr_update
["_admin.deployed.VCA"] = nsr_deployed
["VCA"]
5142 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
5144 step
= "Getting vnfr from database"
5145 db_vnfr
= self
.db
.get_one(
5146 "vnfrs", {"member-vnf-index-ref": vnf_index
, "nsr-id-ref": nsr_id
}
5149 vca_id
= self
.get_vca_id(db_vnfr
, db_nsr
)
5151 step
= "Getting vnfd from database"
5152 db_vnfd
= self
.db
.get_one("vnfds", {"_id": db_vnfr
["vnfd-id"]})
5154 base_folder
= db_vnfd
["_admin"]["storage"]
5156 step
= "Getting scaling-group-descriptor"
5157 scaling_descriptor
= find_in_list(
5158 get_scaling_aspect(db_vnfd
),
5159 lambda scale_desc
: scale_desc
["name"] == scaling_group
,
5161 if not scaling_descriptor
:
5163 "input parameter 'scaleByStepData':'scaling-group-descriptor':'{}' is not present "
5164 "at vnfd:scaling-group-descriptor".format(scaling_group
)
5167 step
= "Sending scale order to VIM"
5168 # TODO check if ns is in a proper status
5170 if not db_nsr
["_admin"].get("scaling-group"):
5175 "_admin.scaling-group": [
5176 {"name": scaling_group
, "nb-scale-op": 0}
5180 admin_scale_index
= 0
5182 for admin_scale_index
, admin_scale_info
in enumerate(
5183 db_nsr
["_admin"]["scaling-group"]
5185 if admin_scale_info
["name"] == scaling_group
:
5186 nb_scale_op
= admin_scale_info
.get("nb-scale-op", 0)
5188 else: # not found, set index one plus last element and add new entry with the name
5189 admin_scale_index
+= 1
5191 "_admin.scaling-group.{}.name".format(admin_scale_index
)
5194 vca_scaling_info
= []
5195 scaling_info
= {"scaling_group_name": scaling_group
, "vdu": [], "kdu": []}
5196 if scaling_type
== "SCALE_OUT":
5197 if "aspect-delta-details" not in scaling_descriptor
:
5199 "Aspect delta details not fount in scaling descriptor {}".format(
5200 scaling_descriptor
["name"]
5203 # count if max-instance-count is reached
5204 deltas
= scaling_descriptor
.get("aspect-delta-details")["deltas"]
5206 scaling_info
["scaling_direction"] = "OUT"
5207 scaling_info
["vdu-create"] = {}
5208 scaling_info
["kdu-create"] = {}
5209 for delta
in deltas
:
5210 for vdu_delta
in delta
.get("vdu-delta", {}):
5211 vdud
= get_vdu(db_vnfd
, vdu_delta
["id"])
5212 # vdu_index also provides the number of instance of the targeted vdu
5213 vdu_count
= vdu_index
= get_vdur_index(db_vnfr
, vdu_delta
)
5214 cloud_init_text
= self
._get
_vdu
_cloud
_init
_content
(
5218 additional_params
= (
5219 self
._get
_vdu
_additional
_params
(db_vnfr
, vdud
["id"])
5222 cloud_init_list
= []
5224 vdu_profile
= get_vdu_profile(db_vnfd
, vdu_delta
["id"])
5225 max_instance_count
= 10
5226 if vdu_profile
and "max-number-of-instances" in vdu_profile
:
5227 max_instance_count
= vdu_profile
.get(
5228 "max-number-of-instances", 10
5231 default_instance_num
= get_number_of_instances(
5234 instances_number
= vdu_delta
.get("number-of-instances", 1)
5235 nb_scale_op
+= instances_number
5237 new_instance_count
= nb_scale_op
+ default_instance_num
5238 # Control if new count is over max and vdu count is less than max.
5239 # Then assign new instance count
5240 if new_instance_count
> max_instance_count
> vdu_count
:
5241 instances_number
= new_instance_count
- max_instance_count
5243 instances_number
= instances_number
5245 if new_instance_count
> max_instance_count
:
5247 "reached the limit of {} (max-instance-count) "
5248 "scaling-out operations for the "
5249 "scaling-group-descriptor '{}'".format(
5250 nb_scale_op
, scaling_group
5253 for x
in range(vdu_delta
.get("number-of-instances", 1)):
5255 # TODO Information of its own ip is not available because db_vnfr is not updated.
5256 additional_params
["OSM"] = get_osm_params(
5257 db_vnfr
, vdu_delta
["id"], vdu_index
+ x
5259 cloud_init_list
.append(
5260 self
._parse
_cloud
_init
(
5267 vca_scaling_info
.append(
5269 "osm_vdu_id": vdu_delta
["id"],
5270 "member-vnf-index": vnf_index
,
5272 "vdu_index": vdu_index
+ x
,
5275 scaling_info
["vdu-create"][vdu_delta
["id"]] = instances_number
5276 for kdu_delta
in delta
.get("kdu-resource-delta", {}):
5277 kdu_profile
= get_kdu_profile(db_vnfd
, kdu_delta
["id"])
5278 kdu_name
= kdu_profile
["kdu-name"]
5279 resource_name
= kdu_profile
["resource-name"]
5281 # Might have different kdus in the same delta
5282 # Should have list for each kdu
5283 if not scaling_info
["kdu-create"].get(kdu_name
, None):
5284 scaling_info
["kdu-create"][kdu_name
] = []
5286 kdur
= get_kdur(db_vnfr
, kdu_name
)
5287 if kdur
.get("helm-chart"):
5288 k8s_cluster_type
= "helm-chart-v3"
5289 self
.logger
.debug("kdur: {}".format(kdur
))
5291 kdur
.get("helm-version")
5292 and kdur
.get("helm-version") == "v2"
5294 k8s_cluster_type
= "helm-chart"
5295 raise NotImplementedError
5296 elif kdur
.get("juju-bundle"):
5297 k8s_cluster_type
= "juju-bundle"
5300 "kdu type for kdu='{}.{}' is neither helm-chart nor "
5301 "juju-bundle. Maybe an old NBI version is running".format(
5302 db_vnfr
["member-vnf-index-ref"], kdu_name
5306 max_instance_count
= 10
5307 if kdu_profile
and "max-number-of-instances" in kdu_profile
:
5308 max_instance_count
= kdu_profile
.get(
5309 "max-number-of-instances", 10
5312 nb_scale_op
+= kdu_delta
.get("number-of-instances", 1)
5313 deployed_kdu
, _
= get_deployed_kdu(
5314 nsr_deployed
, kdu_name
, vnf_index
5316 if deployed_kdu
is None:
5318 "KDU '{}' for vnf '{}' not deployed".format(
5322 kdu_instance
= deployed_kdu
.get("kdu-instance")
5323 instance_num
= await self
.k8scluster_map
[
5325 ].get_scale_count(resource_name
, kdu_instance
, vca_id
=vca_id
)
5326 kdu_replica_count
= instance_num
+ kdu_delta
.get(
5327 "number-of-instances", 1
5330 # Control if new count is over max and instance_num is less than max.
5331 # Then assign max instance number to kdu replica count
5332 if kdu_replica_count
> max_instance_count
> instance_num
:
5333 kdu_replica_count
= max_instance_count
5334 if kdu_replica_count
> max_instance_count
:
5336 "reached the limit of {} (max-instance-count) "
5337 "scaling-out operations for the "
5338 "scaling-group-descriptor '{}'".format(
5339 instance_num
, scaling_group
5343 for x
in range(kdu_delta
.get("number-of-instances", 1)):
5344 vca_scaling_info
.append(
5346 "osm_kdu_id": kdu_name
,
5347 "member-vnf-index": vnf_index
,
5349 "kdu_index": instance_num
+ x
- 1,
5352 scaling_info
["kdu-create"][kdu_name
].append(
5354 "member-vnf-index": vnf_index
,
5356 "k8s-cluster-type": k8s_cluster_type
,
5357 "resource-name": resource_name
,
5358 "scale": kdu_replica_count
,
5361 elif scaling_type
== "SCALE_IN":
5362 deltas
= scaling_descriptor
.get("aspect-delta-details")["deltas"]
5364 scaling_info
["scaling_direction"] = "IN"
5365 scaling_info
["vdu-delete"] = {}
5366 scaling_info
["kdu-delete"] = {}
5368 for delta
in deltas
:
5369 for vdu_delta
in delta
.get("vdu-delta", {}):
5370 vdu_count
= vdu_index
= get_vdur_index(db_vnfr
, vdu_delta
)
5371 min_instance_count
= 0
5372 vdu_profile
= get_vdu_profile(db_vnfd
, vdu_delta
["id"])
5373 if vdu_profile
and "min-number-of-instances" in vdu_profile
:
5374 min_instance_count
= vdu_profile
["min-number-of-instances"]
5376 default_instance_num
= get_number_of_instances(
5377 db_vnfd
, vdu_delta
["id"]
5379 instance_num
= vdu_delta
.get("number-of-instances", 1)
5380 nb_scale_op
-= instance_num
5382 new_instance_count
= nb_scale_op
+ default_instance_num
5384 if new_instance_count
< min_instance_count
< vdu_count
:
5385 instances_number
= min_instance_count
- new_instance_count
5387 instances_number
= instance_num
5389 if new_instance_count
< min_instance_count
:
5391 "reached the limit of {} (min-instance-count) scaling-in operations for the "
5392 "scaling-group-descriptor '{}'".format(
5393 nb_scale_op
, scaling_group
5396 for x
in range(vdu_delta
.get("number-of-instances", 1)):
5397 vca_scaling_info
.append(
5399 "osm_vdu_id": vdu_delta
["id"],
5400 "member-vnf-index": vnf_index
,
5402 "vdu_index": vdu_index
- 1 - x
,
5405 scaling_info
["vdu-delete"][vdu_delta
["id"]] = instances_number
5406 for kdu_delta
in delta
.get("kdu-resource-delta", {}):
5407 kdu_profile
= get_kdu_profile(db_vnfd
, kdu_delta
["id"])
5408 kdu_name
= kdu_profile
["kdu-name"]
5409 resource_name
= kdu_profile
["resource-name"]
5411 if not scaling_info
["kdu-delete"].get(kdu_name
, None):
5412 scaling_info
["kdu-delete"][kdu_name
] = []
5414 kdur
= get_kdur(db_vnfr
, kdu_name
)
5415 if kdur
.get("helm-chart"):
5416 k8s_cluster_type
= "helm-chart-v3"
5417 self
.logger
.debug("kdur: {}".format(kdur
))
5419 kdur
.get("helm-version")
5420 and kdur
.get("helm-version") == "v2"
5422 k8s_cluster_type
= "helm-chart"
5423 raise NotImplementedError
5424 elif kdur
.get("juju-bundle"):
5425 k8s_cluster_type
= "juju-bundle"
5428 "kdu type for kdu='{}.{}' is neither helm-chart nor "
5429 "juju-bundle. Maybe an old NBI version is running".format(
5430 db_vnfr
["member-vnf-index-ref"], kdur
["kdu-name"]
5434 min_instance_count
= 0
5435 if kdu_profile
and "min-number-of-instances" in kdu_profile
:
5436 min_instance_count
= kdu_profile
["min-number-of-instances"]
5438 nb_scale_op
-= kdu_delta
.get("number-of-instances", 1)
5439 deployed_kdu
, _
= get_deployed_kdu(
5440 nsr_deployed
, kdu_name
, vnf_index
5442 if deployed_kdu
is None:
5444 "KDU '{}' for vnf '{}' not deployed".format(
5448 kdu_instance
= deployed_kdu
.get("kdu-instance")
5449 instance_num
= await self
.k8scluster_map
[
5451 ].get_scale_count(resource_name
, kdu_instance
, vca_id
=vca_id
)
5452 kdu_replica_count
= instance_num
- kdu_delta
.get(
5453 "number-of-instances", 1
5456 if kdu_replica_count
< min_instance_count
< instance_num
:
5457 kdu_replica_count
= min_instance_count
5458 if kdu_replica_count
< min_instance_count
:
5460 "reached the limit of {} (min-instance-count) scaling-in operations for the "
5461 "scaling-group-descriptor '{}'".format(
5462 instance_num
, scaling_group
5466 for x
in range(kdu_delta
.get("number-of-instances", 1)):
5467 vca_scaling_info
.append(
5469 "osm_kdu_id": kdu_name
,
5470 "member-vnf-index": vnf_index
,
5472 "kdu_index": instance_num
- x
- 1,
5475 scaling_info
["kdu-delete"][kdu_name
].append(
5477 "member-vnf-index": vnf_index
,
5479 "k8s-cluster-type": k8s_cluster_type
,
5480 "resource-name": resource_name
,
5481 "scale": kdu_replica_count
,
5485 # update VDU_SCALING_INFO with the VDUs to delete ip_addresses
5486 vdu_delete
= copy(scaling_info
.get("vdu-delete"))
5487 if scaling_info
["scaling_direction"] == "IN":
5488 for vdur
in reversed(db_vnfr
["vdur"]):
5489 if vdu_delete
.get(vdur
["vdu-id-ref"]):
5490 vdu_delete
[vdur
["vdu-id-ref"]] -= 1
5491 scaling_info
["vdu"].append(
5493 "name": vdur
.get("name") or vdur
.get("vdu-name"),
5494 "vdu_id": vdur
["vdu-id-ref"],
5498 for interface
in vdur
["interfaces"]:
5499 scaling_info
["vdu"][-1]["interface"].append(
5501 "name": interface
["name"],
5502 "ip_address": interface
["ip-address"],
5503 "mac_address": interface
.get("mac-address"),
5506 # vdu_delete = vdu_scaling_info.pop("vdu-delete")
5509 step
= "Executing pre-scale vnf-config-primitive"
5510 if scaling_descriptor
.get("scaling-config-action"):
5511 for scaling_config_action
in scaling_descriptor
[
5512 "scaling-config-action"
5515 scaling_config_action
.get("trigger") == "pre-scale-in"
5516 and scaling_type
== "SCALE_IN"
5518 scaling_config_action
.get("trigger") == "pre-scale-out"
5519 and scaling_type
== "SCALE_OUT"
5521 vnf_config_primitive
= scaling_config_action
[
5522 "vnf-config-primitive-name-ref"
5524 step
= db_nslcmop_update
[
5526 ] = "executing pre-scale scaling-config-action '{}'".format(
5527 vnf_config_primitive
5530 # look for primitive
5531 for config_primitive
in (
5532 get_configuration(db_vnfd
, db_vnfd
["id"]) or {}
5533 ).get("config-primitive", ()):
5534 if config_primitive
["name"] == vnf_config_primitive
:
5538 "Invalid vnfd descriptor at scaling-group-descriptor[name='{}']:scaling-config-action"
5539 "[vnf-config-primitive-name-ref='{}'] does not match any vnf-configuration:config-"
5540 "primitive".format(scaling_group
, vnf_config_primitive
)
5543 vnfr_params
= {"VDU_SCALE_INFO": scaling_info
}
5544 if db_vnfr
.get("additionalParamsForVnf"):
5545 vnfr_params
.update(db_vnfr
["additionalParamsForVnf"])
5547 scale_process
= "VCA"
5548 db_nsr_update
["config-status"] = "configuring pre-scaling"
5549 primitive_params
= self
._map
_primitive
_params
(
5550 config_primitive
, {}, vnfr_params
5553 # Pre-scale retry check: Check if this sub-operation has been executed before
5554 op_index
= self
._check
_or
_add
_scale
_suboperation
(
5557 vnf_config_primitive
,
5561 if op_index
== self
.SUBOPERATION_STATUS_SKIP
:
5562 # Skip sub-operation
5563 result
= "COMPLETED"
5564 result_detail
= "Done"
5567 + "vnf_config_primitive={} Skipped sub-operation, result {} {}".format(
5568 vnf_config_primitive
, result
, result_detail
5572 if op_index
== self
.SUBOPERATION_STATUS_NEW
:
5573 # New sub-operation: Get index of this sub-operation
5575 len(db_nslcmop
.get("_admin", {}).get("operations"))
5580 + "vnf_config_primitive={} New sub-operation".format(
5581 vnf_config_primitive
5585 # retry: Get registered params for this existing sub-operation
5586 op
= db_nslcmop
.get("_admin", {}).get("operations", [])[
5589 vnf_index
= op
.get("member_vnf_index")
5590 vnf_config_primitive
= op
.get("primitive")
5591 primitive_params
= op
.get("primitive_params")
5594 + "vnf_config_primitive={} Sub-operation retry".format(
5595 vnf_config_primitive
5598 # Execute the primitive, either with new (first-time) or registered (reintent) args
5599 ee_descriptor_id
= config_primitive
.get(
5600 "execution-environment-ref"
5602 primitive_name
= config_primitive
.get(
5603 "execution-environment-primitive", vnf_config_primitive
5605 ee_id
, vca_type
= self
._look
_for
_deployed
_vca
(
5606 nsr_deployed
["VCA"],
5607 member_vnf_index
=vnf_index
,
5609 vdu_count_index
=None,
5610 ee_descriptor_id
=ee_descriptor_id
,
5612 result
, result_detail
= await self
._ns
_execute
_primitive
(
5621 + "vnf_config_primitive={} Done with result {} {}".format(
5622 vnf_config_primitive
, result
, result_detail
5625 # Update operationState = COMPLETED | FAILED
5626 self
._update
_suboperation
_status
(
5627 db_nslcmop
, op_index
, result
, result_detail
5630 if result
== "FAILED":
5631 raise LcmException(result_detail
)
5632 db_nsr_update
["config-status"] = old_config_status
5633 scale_process
= None
5637 "_admin.scaling-group.{}.nb-scale-op".format(admin_scale_index
)
5640 "_admin.scaling-group.{}.time".format(admin_scale_index
)
5643 # SCALE-IN VCA - BEGIN
5644 if vca_scaling_info
:
5645 step
= db_nslcmop_update
[
5647 ] = "Deleting the execution environments"
5648 scale_process
= "VCA"
5649 for vca_info
in vca_scaling_info
:
5650 if vca_info
["type"] == "delete":
5651 member_vnf_index
= str(vca_info
["member-vnf-index"])
5653 logging_text
+ "vdu info: {}".format(vca_info
)
5655 if vca_info
.get("osm_vdu_id"):
5656 vdu_id
= vca_info
["osm_vdu_id"]
5657 vdu_index
= int(vca_info
["vdu_index"])
5660 ] = "Scaling member_vnf_index={}, vdu_id={}, vdu_index={} ".format(
5661 member_vnf_index
, vdu_id
, vdu_index
5665 kdu_id
= vca_info
["osm_kdu_id"]
5668 ] = "Scaling member_vnf_index={}, kdu_id={}, vdu_index={} ".format(
5669 member_vnf_index
, kdu_id
, vdu_index
5671 stage
[2] = step
= "Scaling in VCA"
5672 self
._write
_op
_status
(op_id
=nslcmop_id
, stage
=stage
)
5673 vca_update
= db_nsr
["_admin"]["deployed"]["VCA"]
5674 config_update
= db_nsr
["configurationStatus"]
5675 for vca_index
, vca
in enumerate(vca_update
):
5677 (vca
or vca
.get("ee_id"))
5678 and vca
["member-vnf-index"] == member_vnf_index
5679 and vca
["vdu_count_index"] == vdu_index
5681 if vca
.get("vdu_id"):
5682 config_descriptor
= get_configuration(
5683 db_vnfd
, vca
.get("vdu_id")
5685 elif vca
.get("kdu_name"):
5686 config_descriptor
= get_configuration(
5687 db_vnfd
, vca
.get("kdu_name")
5690 config_descriptor
= get_configuration(
5691 db_vnfd
, db_vnfd
["id"]
5693 operation_params
= (
5694 db_nslcmop
.get("operationParams") or {}
5696 exec_terminate_primitives
= not operation_params
.get(
5697 "skip_terminate_primitives"
5698 ) and vca
.get("needed_terminate")
5699 task
= asyncio
.ensure_future(
5708 exec_primitives
=exec_terminate_primitives
,
5712 timeout
=self
.timeout_charm_delete
,
5715 tasks_dict_info
[task
] = "Terminating VCA {}".format(
5718 del vca_update
[vca_index
]
5719 del config_update
[vca_index
]
5720 # wait for pending tasks of terminate primitives
5724 + "Waiting for tasks {}".format(
5725 list(tasks_dict_info
.keys())
5728 error_list
= await self
._wait
_for
_tasks
(
5732 self
.timeout_charm_delete
, self
.timeout_ns_terminate
5737 tasks_dict_info
.clear()
5739 raise LcmException("; ".join(error_list
))
5741 db_vca_and_config_update
= {
5742 "_admin.deployed.VCA": vca_update
,
5743 "configurationStatus": config_update
,
5746 "nsrs", db_nsr
["_id"], db_vca_and_config_update
5748 scale_process
= None
5749 # SCALE-IN VCA - END
5752 if scaling_info
.get("vdu-create") or scaling_info
.get("vdu-delete"):
5753 scale_process
= "RO"
5754 if self
.ro_config
.get("ng"):
5755 await self
._scale
_ng
_ro
(
5756 logging_text
, db_nsr
, db_nslcmop
, db_vnfr
, scaling_info
, stage
5758 scaling_info
.pop("vdu-create", None)
5759 scaling_info
.pop("vdu-delete", None)
5761 scale_process
= None
5765 if scaling_info
.get("kdu-create") or scaling_info
.get("kdu-delete"):
5766 scale_process
= "KDU"
5767 await self
._scale
_kdu
(
5768 logging_text
, nsr_id
, nsr_deployed
, db_vnfd
, vca_id
, scaling_info
5770 scaling_info
.pop("kdu-create", None)
5771 scaling_info
.pop("kdu-delete", None)
5773 scale_process
= None
5777 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
5779 # SCALE-UP VCA - BEGIN
5780 if vca_scaling_info
:
5781 step
= db_nslcmop_update
[
5783 ] = "Creating new execution environments"
5784 scale_process
= "VCA"
5785 for vca_info
in vca_scaling_info
:
5786 if vca_info
["type"] == "create":
5787 member_vnf_index
= str(vca_info
["member-vnf-index"])
5789 logging_text
+ "vdu info: {}".format(vca_info
)
5791 vnfd_id
= db_vnfr
["vnfd-ref"]
5792 if vca_info
.get("osm_vdu_id"):
5793 vdu_index
= int(vca_info
["vdu_index"])
5794 deploy_params
= {"OSM": get_osm_params(db_vnfr
)}
5795 if db_vnfr
.get("additionalParamsForVnf"):
5796 deploy_params
.update(
5798 db_vnfr
["additionalParamsForVnf"].copy()
5801 descriptor_config
= get_configuration(
5802 db_vnfd
, db_vnfd
["id"]
5804 if descriptor_config
:
5809 logging_text
=logging_text
5810 + "member_vnf_index={} ".format(member_vnf_index
),
5813 nslcmop_id
=nslcmop_id
,
5819 member_vnf_index
=member_vnf_index
,
5820 vdu_index
=vdu_index
,
5822 deploy_params
=deploy_params
,
5823 descriptor_config
=descriptor_config
,
5824 base_folder
=base_folder
,
5825 task_instantiation_info
=tasks_dict_info
,
5828 vdu_id
= vca_info
["osm_vdu_id"]
5829 vdur
= find_in_list(
5830 db_vnfr
["vdur"], lambda vdu
: vdu
["vdu-id-ref"] == vdu_id
5832 descriptor_config
= get_configuration(db_vnfd
, vdu_id
)
5833 if vdur
.get("additionalParams"):
5834 deploy_params_vdu
= parse_yaml_strings(
5835 vdur
["additionalParams"]
5838 deploy_params_vdu
= deploy_params
5839 deploy_params_vdu
["OSM"] = get_osm_params(
5840 db_vnfr
, vdu_id
, vdu_count_index
=vdu_index
5842 if descriptor_config
:
5847 ] = "Scaling member_vnf_index={}, vdu_id={}, vdu_index={} ".format(
5848 member_vnf_index
, vdu_id
, vdu_index
5850 stage
[2] = step
= "Scaling out VCA"
5851 self
._write
_op
_status
(op_id
=nslcmop_id
, stage
=stage
)
5853 logging_text
=logging_text
5854 + "member_vnf_index={}, vdu_id={}, vdu_index={} ".format(
5855 member_vnf_index
, vdu_id
, vdu_index
5859 nslcmop_id
=nslcmop_id
,
5865 member_vnf_index
=member_vnf_index
,
5866 vdu_index
=vdu_index
,
5868 deploy_params
=deploy_params_vdu
,
5869 descriptor_config
=descriptor_config
,
5870 base_folder
=base_folder
,
5871 task_instantiation_info
=tasks_dict_info
,
5875 kdu_name
= vca_info
["osm_kdu_id"]
5876 descriptor_config
= get_configuration(db_vnfd
, kdu_name
)
5877 if descriptor_config
:
5879 kdu_index
= int(vca_info
["kdu_index"])
5883 for x
in db_vnfr
["kdur"]
5884 if x
["kdu-name"] == kdu_name
5886 deploy_params_kdu
= {"OSM": get_osm_params(db_vnfr
)}
5887 if kdur
.get("additionalParams"):
5888 deploy_params_kdu
= parse_yaml_strings(
5889 kdur
["additionalParams"]
5893 logging_text
=logging_text
,
5896 nslcmop_id
=nslcmop_id
,
5902 member_vnf_index
=member_vnf_index
,
5903 vdu_index
=kdu_index
,
5905 deploy_params
=deploy_params_kdu
,
5906 descriptor_config
=descriptor_config
,
5907 base_folder
=base_folder
,
5908 task_instantiation_info
=tasks_dict_info
,
5911 # SCALE-UP VCA - END
5912 scale_process
= None
5915 # execute primitive service POST-SCALING
5916 step
= "Executing post-scale vnf-config-primitive"
5917 if scaling_descriptor
.get("scaling-config-action"):
5918 for scaling_config_action
in scaling_descriptor
[
5919 "scaling-config-action"
5922 scaling_config_action
.get("trigger") == "post-scale-in"
5923 and scaling_type
== "SCALE_IN"
5925 scaling_config_action
.get("trigger") == "post-scale-out"
5926 and scaling_type
== "SCALE_OUT"
5928 vnf_config_primitive
= scaling_config_action
[
5929 "vnf-config-primitive-name-ref"
5931 step
= db_nslcmop_update
[
5933 ] = "executing post-scale scaling-config-action '{}'".format(
5934 vnf_config_primitive
5937 vnfr_params
= {"VDU_SCALE_INFO": scaling_info
}
5938 if db_vnfr
.get("additionalParamsForVnf"):
5939 vnfr_params
.update(db_vnfr
["additionalParamsForVnf"])
5941 # look for primitive
5942 for config_primitive
in (
5943 get_configuration(db_vnfd
, db_vnfd
["id"]) or {}
5944 ).get("config-primitive", ()):
5945 if config_primitive
["name"] == vnf_config_primitive
:
5949 "Invalid vnfd descriptor at scaling-group-descriptor[name='{}']:scaling-config-"
5950 "action[vnf-config-primitive-name-ref='{}'] does not match any vnf-configuration:"
5951 "config-primitive".format(
5952 scaling_group
, vnf_config_primitive
5955 scale_process
= "VCA"
5956 db_nsr_update
["config-status"] = "configuring post-scaling"
5957 primitive_params
= self
._map
_primitive
_params
(
5958 config_primitive
, {}, vnfr_params
5961 # Post-scale retry check: Check if this sub-operation has been executed before
5962 op_index
= self
._check
_or
_add
_scale
_suboperation
(
5965 vnf_config_primitive
,
5969 if op_index
== self
.SUBOPERATION_STATUS_SKIP
:
5970 # Skip sub-operation
5971 result
= "COMPLETED"
5972 result_detail
= "Done"
5975 + "vnf_config_primitive={} Skipped sub-operation, result {} {}".format(
5976 vnf_config_primitive
, result
, result_detail
5980 if op_index
== self
.SUBOPERATION_STATUS_NEW
:
5981 # New sub-operation: Get index of this sub-operation
5983 len(db_nslcmop
.get("_admin", {}).get("operations"))
5988 + "vnf_config_primitive={} New sub-operation".format(
5989 vnf_config_primitive
5993 # retry: Get registered params for this existing sub-operation
5994 op
= db_nslcmop
.get("_admin", {}).get("operations", [])[
5997 vnf_index
= op
.get("member_vnf_index")
5998 vnf_config_primitive
= op
.get("primitive")
5999 primitive_params
= op
.get("primitive_params")
6002 + "vnf_config_primitive={} Sub-operation retry".format(
6003 vnf_config_primitive
6006 # Execute the primitive, either with new (first-time) or registered (reintent) args
6007 ee_descriptor_id
= config_primitive
.get(
6008 "execution-environment-ref"
6010 primitive_name
= config_primitive
.get(
6011 "execution-environment-primitive", vnf_config_primitive
6013 ee_id
, vca_type
= self
._look
_for
_deployed
_vca
(
6014 nsr_deployed
["VCA"],
6015 member_vnf_index
=vnf_index
,
6017 vdu_count_index
=None,
6018 ee_descriptor_id
=ee_descriptor_id
,
6020 result
, result_detail
= await self
._ns
_execute
_primitive
(
6029 + "vnf_config_primitive={} Done with result {} {}".format(
6030 vnf_config_primitive
, result
, result_detail
6033 # Update operationState = COMPLETED | FAILED
6034 self
._update
_suboperation
_status
(
6035 db_nslcmop
, op_index
, result
, result_detail
6038 if result
== "FAILED":
6039 raise LcmException(result_detail
)
6040 db_nsr_update
["config-status"] = old_config_status
6041 scale_process
= None
6046 ] = "" # "scaled {} {}".format(scaling_group, scaling_type)
6047 db_nsr_update
["operational-status"] = (
6049 if old_operational_status
== "failed"
6050 else old_operational_status
6052 db_nsr_update
["config-status"] = old_config_status
6055 ROclient
.ROClientException
,
6060 self
.logger
.error(logging_text
+ "Exit Exception {}".format(e
))
6062 except asyncio
.CancelledError
:
6064 logging_text
+ "Cancelled Exception while '{}'".format(step
)
6066 exc
= "Operation was cancelled"
6067 except Exception as e
:
6068 exc
= traceback
.format_exc()
6069 self
.logger
.critical(
6070 logging_text
+ "Exit Exception {} {}".format(type(e
).__name
__, e
),
6074 self
._write
_ns
_status
(
6077 current_operation
="IDLE",
6078 current_operation_id
=None,
6081 stage
[1] = "Waiting for instantiate pending tasks."
6082 self
.logger
.debug(logging_text
+ stage
[1])
6083 exc
= await self
._wait
_for
_tasks
(
6086 self
.timeout_ns_deploy
,
6094 ] = error_description_nslcmop
= "FAILED {}: {}".format(step
, exc
)
6095 nslcmop_operation_state
= "FAILED"
6097 db_nsr_update
["operational-status"] = old_operational_status
6098 db_nsr_update
["config-status"] = old_config_status
6099 db_nsr_update
["detailed-status"] = ""
6101 if "VCA" in scale_process
:
6102 db_nsr_update
["config-status"] = "failed"
6103 if "RO" in scale_process
:
6104 db_nsr_update
["operational-status"] = "failed"
6107 ] = "FAILED scaling nslcmop={} {}: {}".format(
6108 nslcmop_id
, step
, exc
6111 error_description_nslcmop
= None
6112 nslcmop_operation_state
= "COMPLETED"
6113 db_nslcmop_update
["detailed-status"] = "Done"
6115 self
._write
_op
_status
(
6118 error_message
=error_description_nslcmop
,
6119 operation_state
=nslcmop_operation_state
,
6120 other_update
=db_nslcmop_update
,
6123 self
._write
_ns
_status
(
6126 current_operation
="IDLE",
6127 current_operation_id
=None,
6128 other_update
=db_nsr_update
,
6131 if nslcmop_operation_state
:
6135 "nslcmop_id": nslcmop_id
,
6136 "operationState": nslcmop_operation_state
,
6138 await self
.msg
.aiowrite("ns", "scaled", msg
, loop
=self
.loop
)
6139 except Exception as e
:
6141 logging_text
+ "kafka_write notification Exception {}".format(e
)
6143 self
.logger
.debug(logging_text
+ "Exit")
6144 self
.lcm_tasks
.remove("ns", nsr_id
, nslcmop_id
, "ns_scale")
6146 async def _scale_kdu(
6147 self
, logging_text
, nsr_id
, nsr_deployed
, db_vnfd
, vca_id
, scaling_info
6149 _scaling_info
= scaling_info
.get("kdu-create") or scaling_info
.get("kdu-delete")
6150 for kdu_name
in _scaling_info
:
6151 for kdu_scaling_info
in _scaling_info
[kdu_name
]:
6152 deployed_kdu
, index
= get_deployed_kdu(
6153 nsr_deployed
, kdu_name
, kdu_scaling_info
["member-vnf-index"]
6155 cluster_uuid
= deployed_kdu
["k8scluster-uuid"]
6156 kdu_instance
= deployed_kdu
["kdu-instance"]
6157 scale
= int(kdu_scaling_info
["scale"])
6158 k8s_cluster_type
= kdu_scaling_info
["k8s-cluster-type"]
6161 "collection": "nsrs",
6162 "filter": {"_id": nsr_id
},
6163 "path": "_admin.deployed.K8s.{}".format(index
),
6166 step
= "scaling application {}".format(
6167 kdu_scaling_info
["resource-name"]
6169 self
.logger
.debug(logging_text
+ step
)
6171 if kdu_scaling_info
["type"] == "delete":
6172 kdu_config
= get_configuration(db_vnfd
, kdu_name
)
6175 and kdu_config
.get("terminate-config-primitive")
6176 and get_juju_ee_ref(db_vnfd
, kdu_name
) is None
6178 terminate_config_primitive_list
= kdu_config
.get(
6179 "terminate-config-primitive"
6181 terminate_config_primitive_list
.sort(
6182 key
=lambda val
: int(val
["seq"])
6186 terminate_config_primitive
6187 ) in terminate_config_primitive_list
:
6188 primitive_params_
= self
._map
_primitive
_params
(
6189 terminate_config_primitive
, {}, {}
6191 step
= "execute terminate config primitive"
6192 self
.logger
.debug(logging_text
+ step
)
6193 await asyncio
.wait_for(
6194 self
.k8scluster_map
[k8s_cluster_type
].exec_primitive(
6195 cluster_uuid
=cluster_uuid
,
6196 kdu_instance
=kdu_instance
,
6197 primitive_name
=terminate_config_primitive
["name"],
6198 params
=primitive_params_
,
6205 await asyncio
.wait_for(
6206 self
.k8scluster_map
[k8s_cluster_type
].scale(
6209 kdu_scaling_info
["resource-name"],
6212 timeout
=self
.timeout_vca_on_error
,
6215 if kdu_scaling_info
["type"] == "create":
6216 kdu_config
= get_configuration(db_vnfd
, kdu_name
)
6219 and kdu_config
.get("initial-config-primitive")
6220 and get_juju_ee_ref(db_vnfd
, kdu_name
) is None
6222 initial_config_primitive_list
= kdu_config
.get(
6223 "initial-config-primitive"
6225 initial_config_primitive_list
.sort(
6226 key
=lambda val
: int(val
["seq"])
6229 for initial_config_primitive
in initial_config_primitive_list
:
6230 primitive_params_
= self
._map
_primitive
_params
(
6231 initial_config_primitive
, {}, {}
6233 step
= "execute initial config primitive"
6234 self
.logger
.debug(logging_text
+ step
)
6235 await asyncio
.wait_for(
6236 self
.k8scluster_map
[k8s_cluster_type
].exec_primitive(
6237 cluster_uuid
=cluster_uuid
,
6238 kdu_instance
=kdu_instance
,
6239 primitive_name
=initial_config_primitive
["name"],
6240 params
=primitive_params_
,
6247 async def _scale_ng_ro(
6248 self
, logging_text
, db_nsr
, db_nslcmop
, db_vnfr
, vdu_scaling_info
, stage
6250 nsr_id
= db_nslcmop
["nsInstanceId"]
6251 db_nsd
= self
.db
.get_one("nsds", {"_id": db_nsr
["nsd-id"]})
6254 # read from db: vnfd's for every vnf
6257 # for each vnf in ns, read vnfd
6258 for vnfr
in self
.db
.get_list("vnfrs", {"nsr-id-ref": nsr_id
}):
6259 db_vnfrs
[vnfr
["member-vnf-index-ref"]] = vnfr
6260 vnfd_id
= vnfr
["vnfd-id"] # vnfd uuid for this vnf
6261 # if we haven't this vnfd, read it from db
6262 if not find_in_list(db_vnfds
, lambda a_vnfd
: a_vnfd
["id"] == vnfd_id
):
6264 vnfd
= self
.db
.get_one("vnfds", {"_id": vnfd_id
})
6265 db_vnfds
.append(vnfd
)
6266 n2vc_key
= self
.n2vc
.get_public_key()
6267 n2vc_key_list
= [n2vc_key
]
6270 vdu_scaling_info
.get("vdu-create"),
6271 vdu_scaling_info
.get("vdu-delete"),
6274 # db_vnfr has been updated, update db_vnfrs to use it
6275 db_vnfrs
[db_vnfr
["member-vnf-index-ref"]] = db_vnfr
6276 await self
._instantiate
_ng
_ro
(
6286 start_deploy
=time(),
6287 timeout_ns_deploy
=self
.timeout_ns_deploy
,
6289 if vdu_scaling_info
.get("vdu-delete"):
6291 db_vnfr
, None, vdu_scaling_info
["vdu-delete"], mark_delete
=False
6294 async def add_prometheus_metrics(
6295 self
, ee_id
, artifact_path
, ee_config_descriptor
, vnfr_id
, nsr_id
, target_ip
6297 if not self
.prometheus
:
6299 # look if exist a file called 'prometheus*.j2' and
6300 artifact_content
= self
.fs
.dir_ls(artifact_path
)
6304 for f
in artifact_content
6305 if f
.startswith("prometheus") and f
.endswith(".j2")
6311 with self
.fs
.file_open((artifact_path
, job_file
), "r") as f
:
6315 _
, _
, service
= ee_id
.partition(".") # remove prefix "namespace."
6316 host_name
= "{}-{}".format(service
, ee_config_descriptor
["metric-service"])
6318 vnfr_id
= vnfr_id
.replace("-", "")
6320 "JOB_NAME": vnfr_id
,
6321 "TARGET_IP": target_ip
,
6322 "EXPORTER_POD_IP": host_name
,
6323 "EXPORTER_POD_PORT": host_port
,
6325 job_list
= self
.prometheus
.parse_job(job_data
, variables
)
6326 # ensure job_name is using the vnfr_id. Adding the metadata nsr_id
6327 for job
in job_list
:
6329 not isinstance(job
.get("job_name"), str)
6330 or vnfr_id
not in job
["job_name"]
6332 job
["job_name"] = vnfr_id
+ "_" + str(randint(1, 10000))
6333 job
["nsr_id"] = nsr_id
6334 job_dict
= {jl
["job_name"]: jl
for jl
in job_list
}
6335 if await self
.prometheus
.update(job_dict
):
6336 return list(job_dict
.keys())
6338 def get_vca_cloud_and_credentials(self
, vim_account_id
: str) -> (str, str):
6340 Get VCA Cloud and VCA Cloud Credentials for the VIM account
6342 :param: vim_account_id: VIM Account ID
6344 :return: (cloud_name, cloud_credential)
6346 config
= VimAccountDB
.get_vim_account_with_id(vim_account_id
).get("config", {})
6347 return config
.get("vca_cloud"), config
.get("vca_cloud_credential")
6349 def get_vca_k8s_cloud_and_credentials(self
, vim_account_id
: str) -> (str, str):
6351 Get VCA K8s Cloud and VCA K8s Cloud Credentials for the VIM account
6353 :param: vim_account_id: VIM Account ID
6355 :return: (cloud_name, cloud_credential)
6357 config
= VimAccountDB
.get_vim_account_with_id(vim_account_id
).get("config", {})
6358 return config
.get("vca_k8s_cloud"), config
.get("vca_k8s_cloud_credential")