2fecb5db4030aa5e18c2127197fddef2a5f44244
[osm/LCM.git] / osm_lcm / ns.py
1 # -*- coding: utf-8 -*-
2
3 ##
4 # Copyright 2018 Telefonica S.A.
5 #
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
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
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
16 # under the License.
17 ##
18
19 import asyncio
20 import shutil
21 from typing import Any, Dict, List
22 import yaml
23 import logging
24 import logging.handlers
25 import traceback
26 import ipaddress
27 import json
28 from jinja2 import (
29 Environment,
30 TemplateError,
31 TemplateNotFound,
32 StrictUndefined,
33 UndefinedError,
34 select_autoescape,
35 )
36
37 from osm_lcm import ROclient
38 from osm_lcm.data_utils.lcm_config import LcmCfg
39 from osm_lcm.data_utils.nsr import (
40 get_deployed_kdu,
41 get_deployed_vca,
42 get_deployed_vca_list,
43 get_nsd,
44 )
45 from osm_lcm.data_utils.vca import (
46 DeployedComponent,
47 DeployedK8sResource,
48 DeployedVCA,
49 EELevel,
50 Relation,
51 EERelation,
52 safe_get_ee_relation,
53 )
54 from osm_lcm.ng_ro import NgRoClient, NgRoException
55 from osm_lcm.lcm_utils import (
56 LcmException,
57 LcmBase,
58 deep_get,
59 get_iterable,
60 populate_dict,
61 check_juju_bundle_existence,
62 get_charm_artifact_path,
63 get_ee_id_parts,
64 vld_to_ro_ip_profile,
65 )
66 from osm_lcm.data_utils.nsd import (
67 get_ns_configuration_relation_list,
68 get_vnf_profile,
69 get_vnf_profiles,
70 )
71 from osm_lcm.data_utils.vnfd import (
72 get_kdu,
73 get_kdu_services,
74 get_relation_list,
75 get_vdu_list,
76 get_vdu_profile,
77 get_ee_sorted_initial_config_primitive_list,
78 get_ee_sorted_terminate_config_primitive_list,
79 get_kdu_list,
80 get_virtual_link_profiles,
81 get_vdu,
82 get_configuration,
83 get_vdu_index,
84 get_scaling_aspect,
85 get_number_of_instances,
86 get_juju_ee_ref,
87 get_kdu_resource_profile,
88 find_software_version,
89 check_helm_ee_in_ns,
90 )
91 from osm_lcm.data_utils.list_utils import find_in_list
92 from osm_lcm.data_utils.vnfr import (
93 get_osm_params,
94 get_vdur_index,
95 get_kdur,
96 get_volumes_from_instantiation_params,
97 )
98 from osm_lcm.data_utils.dict_utils import parse_yaml_strings
99 from osm_lcm.data_utils.database.vim_account import VimAccountDB
100 from n2vc.definitions import RelationEndpoint
101 from n2vc.k8s_helm3_conn import K8sHelm3Connector
102 from n2vc.k8s_juju_conn import K8sJujuConnector
103
104 from osm_common.dbbase import DbException
105 from osm_common.fsbase import FsException
106
107 from osm_lcm.data_utils.database.database import Database
108 from osm_lcm.data_utils.filesystem.filesystem import Filesystem
109 from osm_lcm.data_utils.wim import (
110 get_sdn_ports,
111 get_target_wim_attrs,
112 select_feasible_wim_account,
113 )
114
115 from n2vc.n2vc_juju_conn import N2VCJujuConnector
116 from n2vc.exceptions import N2VCException, N2VCNotFound, K8sException
117
118 from osm_lcm.lcm_helm_conn import LCMHelmConn
119 from osm_lcm.osm_config import OsmConfigBuilder
120 from osm_lcm.prometheus import parse_job
121
122 from copy import copy, deepcopy
123 from time import time
124 from uuid import uuid4
125
126 from random import SystemRandom
127
128 __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
129
130
131 class NsLcm(LcmBase):
132 SUBOPERATION_STATUS_NOT_FOUND = -1
133 SUBOPERATION_STATUS_NEW = -2
134 SUBOPERATION_STATUS_SKIP = -3
135 EE_TLS_NAME = "ee-tls"
136 task_name_deploy_vca = "Deploying VCA"
137 rel_operation_types = {
138 "GE": ">=",
139 "LE": "<=",
140 "GT": ">",
141 "LT": "<",
142 "EQ": "==",
143 "NE": "!=",
144 }
145
146 def __init__(self, msg, lcm_tasks, config: LcmCfg):
147 """
148 Init, Connect to database, filesystem storage, and messaging
149 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
150 :return: None
151 """
152 super().__init__(msg=msg, logger=logging.getLogger("lcm.ns"))
153
154 self.db = Database().instance.db
155 self.fs = Filesystem().instance.fs
156 self.lcm_tasks = lcm_tasks
157 self.timeout = config.timeout
158 self.ro_config = config.RO
159 self.vca_config = config.VCA
160
161 # create N2VC connector
162 self.n2vc = N2VCJujuConnector(
163 log=self.logger,
164 on_update_db=self._on_update_n2vc_db,
165 fs=self.fs,
166 db=self.db,
167 )
168
169 self.conn_helm_ee = LCMHelmConn(
170 log=self.logger,
171 vca_config=self.vca_config,
172 on_update_db=self._on_update_n2vc_db,
173 )
174
175 self.k8sclusterhelm3 = K8sHelm3Connector(
176 kubectl_command=self.vca_config.kubectlpath,
177 helm_command=self.vca_config.helm3path,
178 fs=self.fs,
179 log=self.logger,
180 db=self.db,
181 on_update_db=None,
182 )
183
184 self.k8sclusterjuju = K8sJujuConnector(
185 kubectl_command=self.vca_config.kubectlpath,
186 juju_command=self.vca_config.jujupath,
187 log=self.logger,
188 on_update_db=self._on_update_k8s_db,
189 fs=self.fs,
190 db=self.db,
191 )
192
193 self.k8scluster_map = {
194 "helm-chart-v3": self.k8sclusterhelm3,
195 "chart": self.k8sclusterhelm3,
196 "juju-bundle": self.k8sclusterjuju,
197 "juju": self.k8sclusterjuju,
198 }
199
200 self.vca_map = {
201 "lxc_proxy_charm": self.n2vc,
202 "native_charm": self.n2vc,
203 "k8s_proxy_charm": self.n2vc,
204 "helm": self.conn_helm_ee,
205 "helm-v3": self.conn_helm_ee,
206 }
207
208 # create RO client
209 self.RO = NgRoClient(**self.ro_config.to_dict())
210
211 self.op_status_map = {
212 "instantiation": self.RO.status,
213 "termination": self.RO.status,
214 "migrate": self.RO.status,
215 "healing": self.RO.recreate_status,
216 "verticalscale": self.RO.status,
217 "start_stop_rebuild": self.RO.status,
218 }
219
220 @staticmethod
221 def increment_ip_mac(ip_mac, vm_index=1):
222 if not isinstance(ip_mac, str):
223 return ip_mac
224 try:
225 next_ipv6 = None
226 next_ipv4 = None
227 dual_ip = ip_mac.split(";")
228 if len(dual_ip) == 2:
229 for ip in dual_ip:
230 if ipaddress.ip_address(ip).version == 6:
231 ipv6 = ipaddress.IPv6Address(ip)
232 next_ipv6 = str(ipaddress.IPv6Address(int(ipv6) + 1))
233 elif ipaddress.ip_address(ip).version == 4:
234 ipv4 = ipaddress.IPv4Address(ip)
235 next_ipv4 = str(ipaddress.IPv4Address(int(ipv4) + 1))
236 return [next_ipv4, next_ipv6]
237 # try with ipv4 look for last dot
238 i = ip_mac.rfind(".")
239 if i > 0:
240 i += 1
241 return "{}{}".format(ip_mac[:i], int(ip_mac[i:]) + vm_index)
242 # try with ipv6 or mac look for last colon. Operate in hex
243 i = ip_mac.rfind(":")
244 if i > 0:
245 i += 1
246 # format in hex, len can be 2 for mac or 4 for ipv6
247 return ("{}{:0" + str(len(ip_mac) - i) + "x}").format(
248 ip_mac[:i], int(ip_mac[i:], 16) + vm_index
249 )
250 except Exception:
251 pass
252 return None
253
254 async def _on_update_n2vc_db(self, table, filter, path, updated_data, vca_id=None):
255 # remove last dot from path (if exists)
256 if path.endswith("."):
257 path = path[:-1]
258
259 # self.logger.debug('_on_update_n2vc_db(table={}, filter={}, path={}, updated_data={}'
260 # .format(table, filter, path, updated_data))
261 try:
262 nsr_id = filter.get("_id")
263
264 # read ns record from database
265 nsr = self.db.get_one(table="nsrs", q_filter=filter)
266 current_ns_status = nsr.get("nsState")
267
268 # get vca status for NS
269 status_dict = await self.n2vc.get_status(
270 namespace="." + nsr_id, yaml_format=False, vca_id=vca_id
271 )
272
273 # vcaStatus
274 db_dict = dict()
275 db_dict["vcaStatus"] = status_dict
276
277 # update configurationStatus for this VCA
278 try:
279 vca_index = int(path[path.rfind(".") + 1 :])
280
281 vca_list = deep_get(
282 target_dict=nsr, key_list=("_admin", "deployed", "VCA")
283 )
284 vca_status = vca_list[vca_index].get("status")
285
286 configuration_status_list = nsr.get("configurationStatus")
287 config_status = configuration_status_list[vca_index].get("status")
288
289 if config_status == "BROKEN" and vca_status != "failed":
290 db_dict["configurationStatus"][vca_index] = "READY"
291 elif config_status != "BROKEN" and vca_status == "failed":
292 db_dict["configurationStatus"][vca_index] = "BROKEN"
293 except Exception as e:
294 # not update configurationStatus
295 self.logger.debug("Error updating vca_index (ignore): {}".format(e))
296
297 # if nsState = 'READY' check if juju is reporting some error => nsState = 'DEGRADED'
298 # if nsState = 'DEGRADED' check if all is OK
299 is_degraded = False
300 if current_ns_status in ("READY", "DEGRADED"):
301 error_description = ""
302 # check machines
303 if status_dict.get("machines"):
304 for machine_id in status_dict.get("machines"):
305 machine = status_dict.get("machines").get(machine_id)
306 # check machine agent-status
307 if machine.get("agent-status"):
308 s = machine.get("agent-status").get("status")
309 if s != "started":
310 is_degraded = True
311 error_description += (
312 "machine {} agent-status={} ; ".format(
313 machine_id, s
314 )
315 )
316 # check machine instance status
317 if machine.get("instance-status"):
318 s = machine.get("instance-status").get("status")
319 if s != "running":
320 is_degraded = True
321 error_description += (
322 "machine {} instance-status={} ; ".format(
323 machine_id, s
324 )
325 )
326 # check applications
327 if status_dict.get("applications"):
328 for app_id in status_dict.get("applications"):
329 app = status_dict.get("applications").get(app_id)
330 # check application status
331 if app.get("status"):
332 s = app.get("status").get("status")
333 if s != "active":
334 is_degraded = True
335 error_description += (
336 "application {} status={} ; ".format(app_id, s)
337 )
338
339 if error_description:
340 db_dict["errorDescription"] = error_description
341 if current_ns_status == "READY" and is_degraded:
342 db_dict["nsState"] = "DEGRADED"
343 if current_ns_status == "DEGRADED" and not is_degraded:
344 db_dict["nsState"] = "READY"
345
346 # write to database
347 self.update_db_2("nsrs", nsr_id, db_dict)
348
349 except (asyncio.CancelledError, asyncio.TimeoutError):
350 raise
351 except Exception as e:
352 self.logger.warn("Error updating NS state for ns={}: {}".format(nsr_id, e))
353
354 async def _on_update_k8s_db(
355 self, cluster_uuid, kdu_instance, filter=None, vca_id=None, cluster_type="juju"
356 ):
357 """
358 Updating vca status in NSR record
359 :param cluster_uuid: UUID of a k8s cluster
360 :param kdu_instance: The unique name of the KDU instance
361 :param filter: To get nsr_id
362 :cluster_type: The cluster type (juju, k8s)
363 :return: none
364 """
365
366 # self.logger.debug("_on_update_k8s_db(cluster_uuid={}, kdu_instance={}, filter={}"
367 # .format(cluster_uuid, kdu_instance, filter))
368
369 nsr_id = filter.get("_id")
370 try:
371 vca_status = await self.k8scluster_map[cluster_type].status_kdu(
372 cluster_uuid=cluster_uuid,
373 kdu_instance=kdu_instance,
374 yaml_format=False,
375 complete_status=True,
376 vca_id=vca_id,
377 )
378
379 # vcaStatus
380 db_dict = dict()
381 db_dict["vcaStatus"] = {nsr_id: vca_status}
382
383 self.logger.debug(
384 f"Obtained VCA status for cluster type '{cluster_type}': {vca_status}"
385 )
386
387 # write to database
388 self.update_db_2("nsrs", nsr_id, db_dict)
389 except (asyncio.CancelledError, asyncio.TimeoutError):
390 raise
391 except Exception as e:
392 self.logger.warn("Error updating NS state for ns={}: {}".format(nsr_id, e))
393
394 @staticmethod
395 def _parse_cloud_init(cloud_init_text, additional_params, vnfd_id, vdu_id):
396 try:
397 env = Environment(
398 undefined=StrictUndefined,
399 autoescape=select_autoescape(default_for_string=True, default=True),
400 )
401 template = env.from_string(cloud_init_text)
402 return template.render(additional_params or {})
403 except UndefinedError as e:
404 raise LcmException(
405 "Variable {} at vnfd[id={}]:vdu[id={}]:cloud-init/cloud-init-"
406 "file, must be provided in the instantiation parameters inside the "
407 "'additionalParamsForVnf/Vdu' block".format(e, vnfd_id, vdu_id)
408 )
409 except (TemplateError, TemplateNotFound) as e:
410 raise LcmException(
411 "Error parsing Jinja2 to cloud-init content at vnfd[id={}]:vdu[id={}]: {}".format(
412 vnfd_id, vdu_id, e
413 )
414 )
415
416 def _get_vdu_cloud_init_content(self, vdu, vnfd):
417 cloud_init_content = cloud_init_file = None
418 try:
419 if vdu.get("cloud-init-file"):
420 base_folder = vnfd["_admin"]["storage"]
421 if base_folder["pkg-dir"]:
422 cloud_init_file = "{}/{}/cloud_init/{}".format(
423 base_folder["folder"],
424 base_folder["pkg-dir"],
425 vdu["cloud-init-file"],
426 )
427 else:
428 cloud_init_file = "{}/Scripts/cloud_init/{}".format(
429 base_folder["folder"],
430 vdu["cloud-init-file"],
431 )
432 with self.fs.file_open(cloud_init_file, "r") as ci_file:
433 cloud_init_content = ci_file.read()
434 elif vdu.get("cloud-init"):
435 cloud_init_content = vdu["cloud-init"]
436
437 return cloud_init_content
438 except FsException as e:
439 raise LcmException(
440 "Error reading vnfd[id={}]:vdu[id={}]:cloud-init-file={}: {}".format(
441 vnfd["id"], vdu["id"], cloud_init_file, e
442 )
443 )
444
445 def _get_vdu_additional_params(self, db_vnfr, vdu_id):
446 vdur = next(
447 (vdur for vdur in db_vnfr.get("vdur") if vdu_id == vdur["vdu-id-ref"]), {}
448 )
449 additional_params = vdur.get("additionalParams")
450 return parse_yaml_strings(additional_params)
451
452 @staticmethod
453 def ip_profile_2_RO(ip_profile):
454 RO_ip_profile = deepcopy(ip_profile)
455 if "dns-server" in RO_ip_profile:
456 if isinstance(RO_ip_profile["dns-server"], list):
457 RO_ip_profile["dns-address"] = []
458 for ds in RO_ip_profile.pop("dns-server"):
459 RO_ip_profile["dns-address"].append(ds["address"])
460 else:
461 RO_ip_profile["dns-address"] = RO_ip_profile.pop("dns-server")
462 if RO_ip_profile.get("ip-version") == "ipv4":
463 RO_ip_profile["ip-version"] = "IPv4"
464 if RO_ip_profile.get("ip-version") == "ipv6":
465 RO_ip_profile["ip-version"] = "IPv6"
466 if "dhcp-params" in RO_ip_profile:
467 RO_ip_profile["dhcp"] = RO_ip_profile.pop("dhcp-params")
468 return RO_ip_profile
469
470 def scale_vnfr(self, db_vnfr, vdu_create=None, vdu_delete=None, mark_delete=False):
471 db_vdu_push_list = []
472 template_vdur = []
473 db_update = {"_admin.modified": time()}
474 if vdu_create:
475 for vdu_id, vdu_count in vdu_create.items():
476 vdur = next(
477 (
478 vdur
479 for vdur in reversed(db_vnfr["vdur"])
480 if vdur["vdu-id-ref"] == vdu_id
481 ),
482 None,
483 )
484 if not vdur:
485 # Read the template saved in the db:
486 self.logger.debug(
487 "No vdur in the database. Using the vdur-template to scale"
488 )
489 vdur_template = db_vnfr.get("vdur-template")
490 if not vdur_template:
491 raise LcmException(
492 "Error scaling OUT VNFR for {}. No vnfr or template exists".format(
493 vdu_id
494 )
495 )
496 vdur = vdur_template[0]
497 # Delete a template from the database after using it
498 self.db.set_one(
499 "vnfrs",
500 {"_id": db_vnfr["_id"]},
501 None,
502 pull={"vdur-template": {"_id": vdur["_id"]}},
503 )
504 for count in range(vdu_count):
505 vdur_copy = deepcopy(vdur)
506 vdur_copy["status"] = "BUILD"
507 vdur_copy["status-detailed"] = None
508 vdur_copy["ip-address"] = None
509 vdur_copy["_id"] = str(uuid4())
510 vdur_copy["count-index"] += count + 1
511 vdur_copy["id"] = "{}-{}".format(
512 vdur_copy["vdu-id-ref"], vdur_copy["count-index"]
513 )
514 vdur_copy.pop("vim_info", None)
515 for iface in vdur_copy["interfaces"]:
516 if iface.get("fixed-ip"):
517 iface["ip-address"] = self.increment_ip_mac(
518 iface["ip-address"], count + 1
519 )
520 else:
521 iface.pop("ip-address", None)
522 if iface.get("fixed-mac"):
523 iface["mac-address"] = self.increment_ip_mac(
524 iface["mac-address"], count + 1
525 )
526 else:
527 iface.pop("mac-address", None)
528 if db_vnfr["vdur"]:
529 iface.pop(
530 "mgmt_vnf", None
531 ) # only first vdu can be managment of vnf
532 db_vdu_push_list.append(vdur_copy)
533 # self.logger.debug("scale out, adding vdu={}".format(vdur_copy))
534 if vdu_delete:
535 if len(db_vnfr["vdur"]) == 1:
536 # The scale will move to 0 instances
537 self.logger.debug(
538 "Scaling to 0 !, creating the template with the last vdur"
539 )
540 template_vdur = [db_vnfr["vdur"][0]]
541 for vdu_id, vdu_count in vdu_delete.items():
542 if mark_delete:
543 indexes_to_delete = [
544 iv[0]
545 for iv in enumerate(db_vnfr["vdur"])
546 if iv[1]["vdu-id-ref"] == vdu_id
547 ]
548 db_update.update(
549 {
550 "vdur.{}.status".format(i): "DELETING"
551 for i in indexes_to_delete[-vdu_count:]
552 }
553 )
554 else:
555 # it must be deleted one by one because common.db does not allow otherwise
556 vdus_to_delete = [
557 v
558 for v in reversed(db_vnfr["vdur"])
559 if v["vdu-id-ref"] == vdu_id
560 ]
561 for vdu in vdus_to_delete[:vdu_count]:
562 self.db.set_one(
563 "vnfrs",
564 {"_id": db_vnfr["_id"]},
565 None,
566 pull={"vdur": {"_id": vdu["_id"]}},
567 )
568 db_push = {}
569 if db_vdu_push_list:
570 db_push["vdur"] = db_vdu_push_list
571 if template_vdur:
572 db_push["vdur-template"] = template_vdur
573 if not db_push:
574 db_push = None
575 db_vnfr["vdur-template"] = template_vdur
576 self.db.set_one("vnfrs", {"_id": db_vnfr["_id"]}, db_update, push_list=db_push)
577 # modify passed dictionary db_vnfr
578 db_vnfr_ = self.db.get_one("vnfrs", {"_id": db_vnfr["_id"]})
579 db_vnfr["vdur"] = db_vnfr_["vdur"]
580
581 def ns_update_nsr(self, ns_update_nsr, db_nsr, nsr_desc_RO):
582 """
583 Updates database nsr with the RO info for the created vld
584 :param ns_update_nsr: dictionary to be filled with the updated info
585 :param db_nsr: content of db_nsr. This is also modified
586 :param nsr_desc_RO: nsr descriptor from RO
587 :return: Nothing, LcmException is raised on errors
588 """
589
590 for vld_index, vld in enumerate(get_iterable(db_nsr, "vld")):
591 for net_RO in get_iterable(nsr_desc_RO, "nets"):
592 if vld["id"] != net_RO.get("ns_net_osm_id"):
593 continue
594 vld["vim-id"] = net_RO.get("vim_net_id")
595 vld["name"] = net_RO.get("vim_name")
596 vld["status"] = net_RO.get("status")
597 vld["status-detailed"] = net_RO.get("error_msg")
598 ns_update_nsr["vld.{}".format(vld_index)] = vld
599 break
600 else:
601 raise LcmException(
602 "ns_update_nsr: Not found vld={} at RO info".format(vld["id"])
603 )
604
605 def set_vnfr_at_error(self, db_vnfrs, error_text):
606 try:
607 for db_vnfr in db_vnfrs.values():
608 vnfr_update = {"status": "ERROR"}
609 for vdu_index, vdur in enumerate(get_iterable(db_vnfr, "vdur")):
610 if "status" not in vdur:
611 vdur["status"] = "ERROR"
612 vnfr_update["vdur.{}.status".format(vdu_index)] = "ERROR"
613 if error_text:
614 vdur["status-detailed"] = str(error_text)
615 vnfr_update[
616 "vdur.{}.status-detailed".format(vdu_index)
617 ] = "ERROR"
618 self.update_db_2("vnfrs", db_vnfr["_id"], vnfr_update)
619 except DbException as e:
620 self.logger.error("Cannot update vnf. {}".format(e))
621
622 def _get_ns_config_info(self, nsr_id):
623 """
624 Generates a mapping between vnf,vdu elements and the N2VC id
625 :param nsr_id: id of nsr to get last database _admin.deployed.VCA that contains this list
626 :return: a dictionary with {osm-config-mapping: {}} where its element contains:
627 "<member-vnf-index>": <N2VC-id> for a vnf configuration, or
628 "<member-vnf-index>.<vdu.id>.<vdu replica(0, 1,..)>": <N2VC-id> for a vdu configuration
629 """
630 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
631 vca_deployed_list = db_nsr["_admin"]["deployed"]["VCA"]
632 mapping = {}
633 ns_config_info = {"osm-config-mapping": mapping}
634 for vca in vca_deployed_list:
635 if not vca["member-vnf-index"]:
636 continue
637 if not vca["vdu_id"]:
638 mapping[vca["member-vnf-index"]] = vca["application"]
639 else:
640 mapping[
641 "{}.{}.{}".format(
642 vca["member-vnf-index"], vca["vdu_id"], vca["vdu_count_index"]
643 )
644 ] = vca["application"]
645 return ns_config_info
646
647 async def _instantiate_ng_ro(
648 self,
649 logging_text,
650 nsr_id,
651 nsd,
652 db_nsr,
653 db_nslcmop,
654 db_vnfrs,
655 db_vnfds,
656 n2vc_key_list,
657 stage,
658 start_deploy,
659 timeout_ns_deploy,
660 ):
661 db_vims = {}
662
663 def get_vim_account(vim_account_id):
664 nonlocal db_vims
665 if vim_account_id in db_vims:
666 return db_vims[vim_account_id]
667 db_vim = self.db.get_one("vim_accounts", {"_id": vim_account_id})
668 db_vims[vim_account_id] = db_vim
669 return db_vim
670
671 # modify target_vld info with instantiation parameters
672 def parse_vld_instantiation_params(
673 target_vim, target_vld, vld_params, target_sdn
674 ):
675 if vld_params.get("ip-profile"):
676 target_vld["vim_info"][target_vim]["ip_profile"] = vld_to_ro_ip_profile(
677 vld_params["ip-profile"]
678 )
679 if vld_params.get("provider-network"):
680 target_vld["vim_info"][target_vim]["provider_network"] = vld_params[
681 "provider-network"
682 ]
683 if "sdn-ports" in vld_params["provider-network"] and target_sdn:
684 target_vld["vim_info"][target_sdn]["sdn-ports"] = vld_params[
685 "provider-network"
686 ]["sdn-ports"]
687
688 # check if WIM is needed; if needed, choose a feasible WIM able to connect VIMs
689 # if wim_account_id is specified in vld_params, validate if it is feasible.
690 wim_account_id, db_wim = select_feasible_wim_account(
691 db_nsr, db_vnfrs, target_vld, vld_params, self.logger
692 )
693
694 if wim_account_id:
695 # WIM is needed and a feasible one was found, populate WIM target and SDN ports
696 self.logger.info("WIM selected: {:s}".format(str(wim_account_id)))
697 # update vld_params with correct WIM account Id
698 vld_params["wimAccountId"] = wim_account_id
699
700 target_wim = "wim:{}".format(wim_account_id)
701 target_wim_attrs = get_target_wim_attrs(nsr_id, target_vld, vld_params)
702 sdn_ports = get_sdn_ports(vld_params, db_wim)
703 if len(sdn_ports) > 0:
704 target_vld["vim_info"][target_wim] = target_wim_attrs
705 target_vld["vim_info"][target_wim]["sdn-ports"] = sdn_ports
706
707 self.logger.debug(
708 "Target VLD with WIM data: {:s}".format(str(target_vld))
709 )
710
711 for param in ("vim-network-name", "vim-network-id"):
712 if vld_params.get(param):
713 if isinstance(vld_params[param], dict):
714 for vim, vim_net in vld_params[param].items():
715 other_target_vim = "vim:" + vim
716 populate_dict(
717 target_vld["vim_info"],
718 (other_target_vim, param.replace("-", "_")),
719 vim_net,
720 )
721 else: # isinstance str
722 target_vld["vim_info"][target_vim][
723 param.replace("-", "_")
724 ] = vld_params[param]
725 if vld_params.get("common_id"):
726 target_vld["common_id"] = vld_params.get("common_id")
727
728 # modify target["ns"]["vld"] with instantiation parameters to override vnf vim-account
729 def update_ns_vld_target(target, ns_params):
730 for vnf_params in ns_params.get("vnf", ()):
731 if vnf_params.get("vimAccountId"):
732 target_vnf = next(
733 (
734 vnfr
735 for vnfr in db_vnfrs.values()
736 if vnf_params["member-vnf-index"]
737 == vnfr["member-vnf-index-ref"]
738 ),
739 None,
740 )
741 vdur = next((vdur for vdur in target_vnf.get("vdur", ())), None)
742 if not vdur:
743 continue
744 for a_index, a_vld in enumerate(target["ns"]["vld"]):
745 target_vld = find_in_list(
746 get_iterable(vdur, "interfaces"),
747 lambda iface: iface.get("ns-vld-id") == a_vld["name"],
748 )
749
750 vld_params = find_in_list(
751 get_iterable(ns_params, "vld"),
752 lambda v_vld: v_vld["name"] in (a_vld["name"], a_vld["id"]),
753 )
754 if target_vld:
755 if vnf_params.get("vimAccountId") not in a_vld.get(
756 "vim_info", {}
757 ):
758 target_vim_network_list = [
759 v for _, v in a_vld.get("vim_info").items()
760 ]
761 target_vim_network_name = next(
762 (
763 item.get("vim_network_name", "")
764 for item in target_vim_network_list
765 ),
766 "",
767 )
768
769 target["ns"]["vld"][a_index].get("vim_info").update(
770 {
771 "vim:{}".format(vnf_params["vimAccountId"]): {
772 "vim_network_name": target_vim_network_name,
773 }
774 }
775 )
776
777 if vld_params:
778 for param in ("vim-network-name", "vim-network-id"):
779 if vld_params.get(param) and isinstance(
780 vld_params[param], dict
781 ):
782 for vim, vim_net in vld_params[
783 param
784 ].items():
785 other_target_vim = "vim:" + vim
786 populate_dict(
787 target["ns"]["vld"][a_index].get(
788 "vim_info"
789 ),
790 (
791 other_target_vim,
792 param.replace("-", "_"),
793 ),
794 vim_net,
795 )
796
797 nslcmop_id = db_nslcmop["_id"]
798 target = {
799 "name": db_nsr["name"],
800 "ns": {"vld": []},
801 "vnf": [],
802 "image": deepcopy(db_nsr["image"]),
803 "flavor": deepcopy(db_nsr["flavor"]),
804 "action_id": nslcmop_id,
805 "cloud_init_content": {},
806 }
807 for image in target["image"]:
808 image["vim_info"] = {}
809 for flavor in target["flavor"]:
810 flavor["vim_info"] = {}
811 if db_nsr.get("shared-volumes"):
812 target["shared-volumes"] = deepcopy(db_nsr["shared-volumes"])
813 for shared_volumes in target["shared-volumes"]:
814 shared_volumes["vim_info"] = {}
815 if db_nsr.get("affinity-or-anti-affinity-group"):
816 target["affinity-or-anti-affinity-group"] = deepcopy(
817 db_nsr["affinity-or-anti-affinity-group"]
818 )
819 for affinity_or_anti_affinity_group in target[
820 "affinity-or-anti-affinity-group"
821 ]:
822 affinity_or_anti_affinity_group["vim_info"] = {}
823
824 if db_nslcmop.get("lcmOperationType") != "instantiate":
825 # get parameters of instantiation:
826 db_nslcmop_instantiate = self.db.get_list(
827 "nslcmops",
828 {
829 "nsInstanceId": db_nslcmop["nsInstanceId"],
830 "lcmOperationType": "instantiate",
831 },
832 )[-1]
833 ns_params = db_nslcmop_instantiate.get("operationParams")
834 else:
835 ns_params = db_nslcmop.get("operationParams")
836 ssh_keys_instantiation = ns_params.get("ssh_keys") or []
837 ssh_keys_all = ssh_keys_instantiation + (n2vc_key_list or [])
838
839 cp2target = {}
840 for vld_index, vld in enumerate(db_nsr.get("vld")):
841 target_vim = "vim:{}".format(ns_params["vimAccountId"])
842 target_vld = {
843 "id": vld["id"],
844 "name": vld["name"],
845 "mgmt-network": vld.get("mgmt-network", False),
846 "type": vld.get("type"),
847 "vim_info": {
848 target_vim: {
849 "vim_network_name": vld.get("vim-network-name"),
850 "vim_account_id": ns_params["vimAccountId"],
851 }
852 },
853 }
854 # check if this network needs SDN assist
855 if vld.get("pci-interfaces"):
856 db_vim = get_vim_account(ns_params["vimAccountId"])
857 if vim_config := db_vim.get("config"):
858 if sdnc_id := vim_config.get("sdn-controller"):
859 sdn_vld = "nsrs:{}:vld.{}".format(nsr_id, vld["id"])
860 target_sdn = "sdn:{}".format(sdnc_id)
861 target_vld["vim_info"][target_sdn] = {
862 "sdn": True,
863 "target_vim": target_vim,
864 "vlds": [sdn_vld],
865 "type": vld.get("type"),
866 }
867
868 nsd_vnf_profiles = get_vnf_profiles(nsd)
869 for nsd_vnf_profile in nsd_vnf_profiles:
870 for cp in nsd_vnf_profile["virtual-link-connectivity"]:
871 if cp["virtual-link-profile-id"] == vld["id"]:
872 cp2target[
873 "member_vnf:{}.{}".format(
874 cp["constituent-cpd-id"][0][
875 "constituent-base-element-id"
876 ],
877 cp["constituent-cpd-id"][0]["constituent-cpd-id"],
878 )
879 ] = "nsrs:{}:vld.{}".format(nsr_id, vld_index)
880
881 # check at nsd descriptor, if there is an ip-profile
882 vld_params = {}
883 nsd_vlp = find_in_list(
884 get_virtual_link_profiles(nsd),
885 lambda a_link_profile: a_link_profile["virtual-link-desc-id"]
886 == vld["id"],
887 )
888 if (
889 nsd_vlp
890 and nsd_vlp.get("virtual-link-protocol-data")
891 and nsd_vlp["virtual-link-protocol-data"].get("l3-protocol-data")
892 ):
893 vld_params["ip-profile"] = nsd_vlp["virtual-link-protocol-data"][
894 "l3-protocol-data"
895 ]
896
897 # update vld_params with instantiation params
898 vld_instantiation_params = find_in_list(
899 get_iterable(ns_params, "vld"),
900 lambda a_vld: a_vld["name"] in (vld["name"], vld["id"]),
901 )
902 if vld_instantiation_params:
903 vld_params.update(vld_instantiation_params)
904 parse_vld_instantiation_params(target_vim, target_vld, vld_params, None)
905 target["ns"]["vld"].append(target_vld)
906 # Update the target ns_vld if vnf vim_account is overriden by instantiation params
907 update_ns_vld_target(target, ns_params)
908
909 for vnfr in db_vnfrs.values():
910 vnfd = find_in_list(
911 db_vnfds, lambda db_vnf: db_vnf["id"] == vnfr["vnfd-ref"]
912 )
913 vnf_params = find_in_list(
914 get_iterable(ns_params, "vnf"),
915 lambda a_vnf: a_vnf["member-vnf-index"] == vnfr["member-vnf-index-ref"],
916 )
917 target_vnf = deepcopy(vnfr)
918 target_vim = "vim:{}".format(vnfr["vim-account-id"])
919 for vld in target_vnf.get("vld", ()):
920 # check if connected to a ns.vld, to fill target'
921 vnf_cp = find_in_list(
922 vnfd.get("int-virtual-link-desc", ()),
923 lambda cpd: cpd.get("id") == vld["id"],
924 )
925 if vnf_cp:
926 ns_cp = "member_vnf:{}.{}".format(
927 vnfr["member-vnf-index-ref"], vnf_cp["id"]
928 )
929 if cp2target.get(ns_cp):
930 vld["target"] = cp2target[ns_cp]
931
932 vld["vim_info"] = {
933 target_vim: {"vim_network_name": vld.get("vim-network-name")}
934 }
935 # check if this network needs SDN assist
936 target_sdn = None
937 if vld.get("pci-interfaces"):
938 db_vim = get_vim_account(vnfr["vim-account-id"])
939 sdnc_id = db_vim["config"].get("sdn-controller")
940 if sdnc_id:
941 sdn_vld = "vnfrs:{}:vld.{}".format(target_vnf["_id"], vld["id"])
942 target_sdn = "sdn:{}".format(sdnc_id)
943 vld["vim_info"][target_sdn] = {
944 "sdn": True,
945 "target_vim": target_vim,
946 "vlds": [sdn_vld],
947 "type": vld.get("type"),
948 }
949
950 # check at vnfd descriptor, if there is an ip-profile
951 vld_params = {}
952 vnfd_vlp = find_in_list(
953 get_virtual_link_profiles(vnfd),
954 lambda a_link_profile: a_link_profile["id"] == vld["id"],
955 )
956 if (
957 vnfd_vlp
958 and vnfd_vlp.get("virtual-link-protocol-data")
959 and vnfd_vlp["virtual-link-protocol-data"].get("l3-protocol-data")
960 ):
961 vld_params["ip-profile"] = vnfd_vlp["virtual-link-protocol-data"][
962 "l3-protocol-data"
963 ]
964 # update vld_params with instantiation params
965 if vnf_params:
966 vld_instantiation_params = find_in_list(
967 get_iterable(vnf_params, "internal-vld"),
968 lambda i_vld: i_vld["name"] == vld["id"],
969 )
970 if vld_instantiation_params:
971 vld_params.update(vld_instantiation_params)
972 parse_vld_instantiation_params(target_vim, vld, vld_params, target_sdn)
973
974 vdur_list = []
975 for vdur in target_vnf.get("vdur", ()):
976 if vdur.get("status") == "DELETING" or vdur.get("pdu-type"):
977 continue # This vdu must not be created
978 vdur["vim_info"] = {"vim_account_id": vnfr["vim-account-id"]}
979
980 self.logger.debug("NS > ssh_keys > {}".format(ssh_keys_all))
981
982 if ssh_keys_all:
983 vdu_configuration = get_configuration(vnfd, vdur["vdu-id-ref"])
984 vnf_configuration = get_configuration(vnfd, vnfd["id"])
985 if (
986 vdu_configuration
987 and vdu_configuration.get("config-access")
988 and vdu_configuration.get("config-access").get("ssh-access")
989 ):
990 vdur["ssh-keys"] = ssh_keys_all
991 vdur["ssh-access-required"] = vdu_configuration[
992 "config-access"
993 ]["ssh-access"]["required"]
994 elif (
995 vnf_configuration
996 and vnf_configuration.get("config-access")
997 and vnf_configuration.get("config-access").get("ssh-access")
998 and any(iface.get("mgmt-vnf") for iface in vdur["interfaces"])
999 ):
1000 vdur["ssh-keys"] = ssh_keys_all
1001 vdur["ssh-access-required"] = vnf_configuration[
1002 "config-access"
1003 ]["ssh-access"]["required"]
1004 elif ssh_keys_instantiation and find_in_list(
1005 vdur["interfaces"], lambda iface: iface.get("mgmt-vnf")
1006 ):
1007 vdur["ssh-keys"] = ssh_keys_instantiation
1008
1009 self.logger.debug("NS > vdur > {}".format(vdur))
1010
1011 vdud = get_vdu(vnfd, vdur["vdu-id-ref"])
1012 # cloud-init
1013 if vdud.get("cloud-init-file"):
1014 vdur["cloud-init"] = "{}:file:{}".format(
1015 vnfd["_id"], vdud.get("cloud-init-file")
1016 )
1017 # read file and put content at target.cloul_init_content. Avoid ng_ro to use shared package system
1018 if vdur["cloud-init"] not in target["cloud_init_content"]:
1019 base_folder = vnfd["_admin"]["storage"]
1020 if base_folder["pkg-dir"]:
1021 cloud_init_file = "{}/{}/cloud_init/{}".format(
1022 base_folder["folder"],
1023 base_folder["pkg-dir"],
1024 vdud.get("cloud-init-file"),
1025 )
1026 else:
1027 cloud_init_file = "{}/Scripts/cloud_init/{}".format(
1028 base_folder["folder"],
1029 vdud.get("cloud-init-file"),
1030 )
1031 with self.fs.file_open(cloud_init_file, "r") as ci_file:
1032 target["cloud_init_content"][
1033 vdur["cloud-init"]
1034 ] = ci_file.read()
1035 elif vdud.get("cloud-init"):
1036 vdur["cloud-init"] = "{}:vdu:{}".format(
1037 vnfd["_id"], get_vdu_index(vnfd, vdur["vdu-id-ref"])
1038 )
1039 # put content at target.cloul_init_content. Avoid ng_ro read vnfd descriptor
1040 target["cloud_init_content"][vdur["cloud-init"]] = vdud[
1041 "cloud-init"
1042 ]
1043 vdur["additionalParams"] = vdur.get("additionalParams") or {}
1044 deploy_params_vdu = self._format_additional_params(
1045 vdur.get("additionalParams") or {}
1046 )
1047 deploy_params_vdu["OSM"] = get_osm_params(
1048 vnfr, vdur["vdu-id-ref"], vdur["count-index"]
1049 )
1050 vdur["additionalParams"] = deploy_params_vdu
1051
1052 # flavor
1053 ns_flavor = target["flavor"][int(vdur["ns-flavor-id"])]
1054 if target_vim not in ns_flavor["vim_info"]:
1055 ns_flavor["vim_info"][target_vim] = {}
1056
1057 # deal with images
1058 # in case alternative images are provided we must check if they should be applied
1059 # for the vim_type, modify the vim_type taking into account
1060 ns_image_id = int(vdur["ns-image-id"])
1061 if vdur.get("alt-image-ids"):
1062 db_vim = get_vim_account(vnfr["vim-account-id"])
1063 vim_type = db_vim["vim_type"]
1064 for alt_image_id in vdur.get("alt-image-ids"):
1065 ns_alt_image = target["image"][int(alt_image_id)]
1066 if vim_type == ns_alt_image.get("vim-type"):
1067 # must use alternative image
1068 self.logger.debug(
1069 "use alternative image id: {}".format(alt_image_id)
1070 )
1071 ns_image_id = alt_image_id
1072 vdur["ns-image-id"] = ns_image_id
1073 break
1074 ns_image = target["image"][int(ns_image_id)]
1075 if target_vim not in ns_image["vim_info"]:
1076 ns_image["vim_info"][target_vim] = {}
1077
1078 # Affinity groups
1079 if vdur.get("affinity-or-anti-affinity-group-id"):
1080 for ags_id in vdur["affinity-or-anti-affinity-group-id"]:
1081 ns_ags = target["affinity-or-anti-affinity-group"][int(ags_id)]
1082 if target_vim not in ns_ags["vim_info"]:
1083 ns_ags["vim_info"][target_vim] = {}
1084
1085 # shared-volumes
1086 if vdur.get("shared-volumes-id"):
1087 for sv_id in vdur["shared-volumes-id"]:
1088 ns_sv = find_in_list(
1089 target["shared-volumes"], lambda sv: sv_id in sv["id"]
1090 )
1091 if ns_sv:
1092 ns_sv["vim_info"][target_vim] = {}
1093
1094 vdur["vim_info"] = {target_vim: {}}
1095 # instantiation parameters
1096 if vnf_params:
1097 vdu_instantiation_params = find_in_list(
1098 get_iterable(vnf_params, "vdu"),
1099 lambda i_vdu: i_vdu["id"] == vdud["id"],
1100 )
1101 if vdu_instantiation_params:
1102 # Parse the vdu_volumes from the instantiation params
1103 vdu_volumes = get_volumes_from_instantiation_params(
1104 vdu_instantiation_params, vdud
1105 )
1106 vdur["additionalParams"]["OSM"]["vdu_volumes"] = vdu_volumes
1107 vdur["additionalParams"]["OSM"][
1108 "vim_flavor_id"
1109 ] = vdu_instantiation_params.get("vim-flavor-id")
1110 vdur_list.append(vdur)
1111 target_vnf["vdur"] = vdur_list
1112 target["vnf"].append(target_vnf)
1113
1114 self.logger.debug("Send to RO > nsr_id={} target={}".format(nsr_id, target))
1115 desc = await self.RO.deploy(nsr_id, target)
1116 self.logger.debug("RO return > {}".format(desc))
1117 action_id = desc["action_id"]
1118 await self._wait_ng_ro(
1119 nsr_id,
1120 action_id,
1121 nslcmop_id,
1122 start_deploy,
1123 timeout_ns_deploy,
1124 stage,
1125 operation="instantiation",
1126 )
1127
1128 # Updating NSR
1129 db_nsr_update = {
1130 "_admin.deployed.RO.operational-status": "running",
1131 "detailed-status": " ".join(stage),
1132 }
1133 # db_nsr["_admin.deployed.RO.detailed-status"] = "Deployed at VIM"
1134 self.update_db_2("nsrs", nsr_id, db_nsr_update)
1135 self._write_op_status(nslcmop_id, stage)
1136 self.logger.debug(
1137 logging_text + "ns deployed at RO. RO_id={}".format(action_id)
1138 )
1139 return
1140
1141 async def _wait_ng_ro(
1142 self,
1143 nsr_id,
1144 action_id,
1145 nslcmop_id=None,
1146 start_time=None,
1147 timeout=600,
1148 stage=None,
1149 operation=None,
1150 ):
1151 detailed_status_old = None
1152 db_nsr_update = {}
1153 start_time = start_time or time()
1154 while time() <= start_time + timeout:
1155 desc_status = await self.op_status_map[operation](nsr_id, action_id)
1156 self.logger.debug("Wait NG RO > {}".format(desc_status))
1157 if desc_status["status"] == "FAILED":
1158 raise NgRoException(desc_status["details"])
1159 elif desc_status["status"] == "BUILD":
1160 if stage:
1161 stage[2] = "VIM: ({})".format(desc_status["details"])
1162 elif desc_status["status"] == "DONE":
1163 if stage:
1164 stage[2] = "Deployed at VIM"
1165 break
1166 else:
1167 assert False, "ROclient.check_ns_status returns unknown {}".format(
1168 desc_status["status"]
1169 )
1170 if stage and nslcmop_id and stage[2] != detailed_status_old:
1171 detailed_status_old = stage[2]
1172 db_nsr_update["detailed-status"] = " ".join(stage)
1173 self.update_db_2("nsrs", nsr_id, db_nsr_update)
1174 self._write_op_status(nslcmop_id, stage)
1175 await asyncio.sleep(15)
1176 else: # timeout_ns_deploy
1177 raise NgRoException("Timeout waiting ns to deploy")
1178
1179 async def _terminate_ng_ro(
1180 self, logging_text, nsr_deployed, nsr_id, nslcmop_id, stage
1181 ):
1182 db_nsr_update = {}
1183 failed_detail = []
1184 action_id = None
1185 start_deploy = time()
1186 try:
1187 target = {
1188 "ns": {"vld": []},
1189 "vnf": [],
1190 "image": [],
1191 "flavor": [],
1192 "action_id": nslcmop_id,
1193 }
1194 desc = await self.RO.deploy(nsr_id, target)
1195 action_id = desc["action_id"]
1196 db_nsr_update["_admin.deployed.RO.nsr_status"] = "DELETING"
1197 self.logger.debug(
1198 logging_text
1199 + "ns terminate action at RO. action_id={}".format(action_id)
1200 )
1201
1202 # wait until done
1203 delete_timeout = 20 * 60 # 20 minutes
1204 await self._wait_ng_ro(
1205 nsr_id,
1206 action_id,
1207 nslcmop_id,
1208 start_deploy,
1209 delete_timeout,
1210 stage,
1211 operation="termination",
1212 )
1213 db_nsr_update["_admin.deployed.RO.nsr_status"] = "DELETED"
1214 # delete all nsr
1215 await self.RO.delete(nsr_id)
1216 except NgRoException as e:
1217 if e.http_code == 404: # not found
1218 db_nsr_update["_admin.deployed.RO.nsr_id"] = None
1219 db_nsr_update["_admin.deployed.RO.nsr_status"] = "DELETED"
1220 self.logger.debug(
1221 logging_text + "RO_action_id={} already deleted".format(action_id)
1222 )
1223 elif e.http_code == 409: # conflict
1224 failed_detail.append("delete conflict: {}".format(e))
1225 self.logger.debug(
1226 logging_text
1227 + "RO_action_id={} delete conflict: {}".format(action_id, e)
1228 )
1229 else:
1230 failed_detail.append("delete error: {}".format(e))
1231 self.logger.error(
1232 logging_text
1233 + "RO_action_id={} delete error: {}".format(action_id, e)
1234 )
1235 except Exception as e:
1236 failed_detail.append("delete error: {}".format(e))
1237 self.logger.error(
1238 logging_text + "RO_action_id={} delete error: {}".format(action_id, e)
1239 )
1240
1241 if failed_detail:
1242 stage[2] = "Error deleting from VIM"
1243 else:
1244 stage[2] = "Deleted from VIM"
1245 db_nsr_update["detailed-status"] = " ".join(stage)
1246 self.update_db_2("nsrs", nsr_id, db_nsr_update)
1247 self._write_op_status(nslcmop_id, stage)
1248
1249 if failed_detail:
1250 raise LcmException("; ".join(failed_detail))
1251 return
1252
1253 async def instantiate_RO(
1254 self,
1255 logging_text,
1256 nsr_id,
1257 nsd,
1258 db_nsr,
1259 db_nslcmop,
1260 db_vnfrs,
1261 db_vnfds,
1262 n2vc_key_list,
1263 stage,
1264 ):
1265 """
1266 Instantiate at RO
1267 :param logging_text: preffix text to use at logging
1268 :param nsr_id: nsr identity
1269 :param nsd: database content of ns descriptor
1270 :param db_nsr: database content of ns record
1271 :param db_nslcmop: database content of ns operation, in this case, 'instantiate'
1272 :param db_vnfrs:
1273 :param db_vnfds: database content of vnfds, indexed by id (not _id). {id: {vnfd_object}, ...}
1274 :param n2vc_key_list: ssh-public-key list to be inserted to management vdus via cloud-init
1275 :param stage: list with 3 items: [general stage, tasks, vim_specific]. This task will write over vim_specific
1276 :return: None or exception
1277 """
1278 try:
1279 start_deploy = time()
1280 ns_params = db_nslcmop.get("operationParams")
1281 if ns_params and ns_params.get("timeout_ns_deploy"):
1282 timeout_ns_deploy = ns_params["timeout_ns_deploy"]
1283 else:
1284 timeout_ns_deploy = self.timeout.ns_deploy
1285
1286 # Check for and optionally request placement optimization. Database will be updated if placement activated
1287 stage[2] = "Waiting for Placement."
1288 if await self._do_placement(logging_text, db_nslcmop, db_vnfrs):
1289 # in case of placement change ns_params[vimAcountId) if not present at any vnfrs
1290 for vnfr in db_vnfrs.values():
1291 if ns_params["vimAccountId"] == vnfr["vim-account-id"]:
1292 break
1293 else:
1294 ns_params["vimAccountId"] == vnfr["vim-account-id"]
1295
1296 return await self._instantiate_ng_ro(
1297 logging_text,
1298 nsr_id,
1299 nsd,
1300 db_nsr,
1301 db_nslcmop,
1302 db_vnfrs,
1303 db_vnfds,
1304 n2vc_key_list,
1305 stage,
1306 start_deploy,
1307 timeout_ns_deploy,
1308 )
1309 except Exception as e:
1310 stage[2] = "ERROR deploying at VIM"
1311 self.set_vnfr_at_error(db_vnfrs, str(e))
1312 self.logger.error(
1313 "Error deploying at VIM {}".format(e),
1314 exc_info=not isinstance(
1315 e,
1316 (
1317 ROclient.ROClientException,
1318 LcmException,
1319 DbException,
1320 NgRoException,
1321 ),
1322 ),
1323 )
1324 raise
1325
1326 async def wait_kdu_up(self, logging_text, nsr_id, vnfr_id, kdu_name):
1327 """
1328 Wait for kdu to be up, get ip address
1329 :param logging_text: prefix use for logging
1330 :param nsr_id:
1331 :param vnfr_id:
1332 :param kdu_name:
1333 :return: IP address, K8s services
1334 """
1335
1336 # self.logger.debug(logging_text + "Starting wait_kdu_up")
1337 nb_tries = 0
1338
1339 while nb_tries < 360:
1340 db_vnfr = self.db.get_one("vnfrs", {"_id": vnfr_id})
1341 kdur = next(
1342 (
1343 x
1344 for x in get_iterable(db_vnfr, "kdur")
1345 if x.get("kdu-name") == kdu_name
1346 ),
1347 None,
1348 )
1349 if not kdur:
1350 raise LcmException(
1351 "Not found vnfr_id={}, kdu_name={}".format(vnfr_id, kdu_name)
1352 )
1353 if kdur.get("status"):
1354 if kdur["status"] in ("READY", "ENABLED"):
1355 return kdur.get("ip-address"), kdur.get("services")
1356 else:
1357 raise LcmException(
1358 "target KDU={} is in error state".format(kdu_name)
1359 )
1360
1361 await asyncio.sleep(10)
1362 nb_tries += 1
1363 raise LcmException("Timeout waiting KDU={} instantiated".format(kdu_name))
1364
1365 async def wait_vm_up_insert_key_ro(
1366 self, logging_text, nsr_id, vnfr_id, vdu_id, vdu_index, pub_key=None, user=None
1367 ):
1368 """
1369 Wait for ip addres at RO, and optionally, insert public key in virtual machine
1370 :param logging_text: prefix use for logging
1371 :param nsr_id:
1372 :param vnfr_id:
1373 :param vdu_id:
1374 :param vdu_index:
1375 :param pub_key: public ssh key to inject, None to skip
1376 :param user: user to apply the public ssh key
1377 :return: IP address
1378 """
1379
1380 self.logger.debug(logging_text + "Starting wait_vm_up_insert_key_ro")
1381 ip_address = None
1382 target_vdu_id = None
1383 ro_retries = 0
1384
1385 while True:
1386 ro_retries += 1
1387 if ro_retries >= 360: # 1 hour
1388 raise LcmException(
1389 "Not found _admin.deployed.RO.nsr_id for nsr_id: {}".format(nsr_id)
1390 )
1391
1392 await asyncio.sleep(10)
1393
1394 # get ip address
1395 if not target_vdu_id:
1396 db_vnfr = self.db.get_one("vnfrs", {"_id": vnfr_id})
1397
1398 if not vdu_id: # for the VNF case
1399 if db_vnfr.get("status") == "ERROR":
1400 raise LcmException(
1401 "Cannot inject ssh-key because target VNF is in error state"
1402 )
1403 ip_address = db_vnfr.get("ip-address")
1404 if not ip_address:
1405 continue
1406 vdur = next(
1407 (
1408 x
1409 for x in get_iterable(db_vnfr, "vdur")
1410 if x.get("ip-address") == ip_address
1411 ),
1412 None,
1413 )
1414 else: # VDU case
1415 vdur = next(
1416 (
1417 x
1418 for x in get_iterable(db_vnfr, "vdur")
1419 if x.get("vdu-id-ref") == vdu_id
1420 and x.get("count-index") == vdu_index
1421 ),
1422 None,
1423 )
1424
1425 if (
1426 not vdur and len(db_vnfr.get("vdur", ())) == 1
1427 ): # If only one, this should be the target vdu
1428 vdur = db_vnfr["vdur"][0]
1429 if not vdur:
1430 raise LcmException(
1431 "Not found vnfr_id={}, vdu_id={}, vdu_index={}".format(
1432 vnfr_id, vdu_id, vdu_index
1433 )
1434 )
1435 # New generation RO stores information at "vim_info"
1436 ng_ro_status = None
1437 target_vim = None
1438 if vdur.get("vim_info"):
1439 target_vim = next(
1440 t for t in vdur["vim_info"]
1441 ) # there should be only one key
1442 ng_ro_status = vdur["vim_info"][target_vim].get("vim_status")
1443 if (
1444 vdur.get("pdu-type")
1445 or vdur.get("status") == "ACTIVE"
1446 or ng_ro_status == "ACTIVE"
1447 ):
1448 ip_address = vdur.get("ip-address")
1449 if not ip_address:
1450 continue
1451 target_vdu_id = vdur["vdu-id-ref"]
1452 elif vdur.get("status") == "ERROR" or ng_ro_status == "ERROR":
1453 raise LcmException(
1454 "Cannot inject ssh-key because target VM is in error state"
1455 )
1456
1457 if not target_vdu_id:
1458 continue
1459
1460 # inject public key into machine
1461 if pub_key and user:
1462 self.logger.debug(logging_text + "Inserting RO key")
1463 self.logger.debug("SSH > PubKey > {}".format(pub_key))
1464 if vdur.get("pdu-type"):
1465 self.logger.error(logging_text + "Cannot inject ssh-ky to a PDU")
1466 return ip_address
1467 try:
1468 target = {
1469 "action": {
1470 "action": "inject_ssh_key",
1471 "key": pub_key,
1472 "user": user,
1473 },
1474 "vnf": [{"_id": vnfr_id, "vdur": [{"id": vdur["id"]}]}],
1475 }
1476 desc = await self.RO.deploy(nsr_id, target)
1477 action_id = desc["action_id"]
1478 await self._wait_ng_ro(
1479 nsr_id, action_id, timeout=600, operation="instantiation"
1480 )
1481 break
1482 except NgRoException as e:
1483 raise LcmException(
1484 "Reaching max tries injecting key. Error: {}".format(e)
1485 )
1486 else:
1487 break
1488
1489 return ip_address
1490
1491 async def _wait_dependent_n2vc(self, nsr_id, vca_deployed_list, vca_index):
1492 """
1493 Wait until dependent VCA deployments have been finished. NS wait for VNFs and VDUs. VNFs for VDUs
1494 """
1495 my_vca = vca_deployed_list[vca_index]
1496 if my_vca.get("vdu_id") or my_vca.get("kdu_name"):
1497 # vdu or kdu: no dependencies
1498 return
1499 timeout = 300
1500 while timeout >= 0:
1501 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
1502 vca_deployed_list = db_nsr["_admin"]["deployed"]["VCA"]
1503 configuration_status_list = db_nsr["configurationStatus"]
1504 for index, vca_deployed in enumerate(configuration_status_list):
1505 if index == vca_index:
1506 # myself
1507 continue
1508 if not my_vca.get("member-vnf-index") or (
1509 vca_deployed.get("member-vnf-index")
1510 == my_vca.get("member-vnf-index")
1511 ):
1512 internal_status = configuration_status_list[index].get("status")
1513 if internal_status == "READY":
1514 continue
1515 elif internal_status == "BROKEN":
1516 raise LcmException(
1517 "Configuration aborted because dependent charm/s has failed"
1518 )
1519 else:
1520 break
1521 else:
1522 # no dependencies, return
1523 return
1524 await asyncio.sleep(10)
1525 timeout -= 1
1526
1527 raise LcmException("Configuration aborted because dependent charm/s timeout")
1528
1529 def get_vca_id(self, db_vnfr: dict, db_nsr: dict):
1530 vca_id = None
1531 if db_vnfr:
1532 vca_id = deep_get(db_vnfr, ("vca-id",))
1533 elif db_nsr:
1534 vim_account_id = deep_get(db_nsr, ("instantiate_params", "vimAccountId"))
1535 vca_id = VimAccountDB.get_vim_account_with_id(vim_account_id).get("vca")
1536 return vca_id
1537
1538 async def instantiate_N2VC(
1539 self,
1540 logging_text,
1541 vca_index,
1542 nsi_id,
1543 db_nsr,
1544 db_vnfr,
1545 vdu_id,
1546 kdu_name,
1547 vdu_index,
1548 kdu_index,
1549 config_descriptor,
1550 deploy_params,
1551 base_folder,
1552 nslcmop_id,
1553 stage,
1554 vca_type,
1555 vca_name,
1556 ee_config_descriptor,
1557 ):
1558 nsr_id = db_nsr["_id"]
1559 db_update_entry = "_admin.deployed.VCA.{}.".format(vca_index)
1560 vca_deployed_list = db_nsr["_admin"]["deployed"]["VCA"]
1561 vca_deployed = db_nsr["_admin"]["deployed"]["VCA"][vca_index]
1562 osm_config = {"osm": {"ns_id": db_nsr["_id"]}}
1563 db_dict = {
1564 "collection": "nsrs",
1565 "filter": {"_id": nsr_id},
1566 "path": db_update_entry,
1567 }
1568 step = ""
1569 try:
1570 element_type = "NS"
1571 element_under_configuration = nsr_id
1572
1573 vnfr_id = None
1574 if db_vnfr:
1575 vnfr_id = db_vnfr["_id"]
1576 osm_config["osm"]["vnf_id"] = vnfr_id
1577
1578 namespace = "{nsi}.{ns}".format(nsi=nsi_id if nsi_id else "", ns=nsr_id)
1579
1580 if vca_type == "native_charm":
1581 index_number = 0
1582 else:
1583 index_number = vdu_index or 0
1584
1585 if vnfr_id:
1586 element_type = "VNF"
1587 element_under_configuration = vnfr_id
1588 namespace += ".{}-{}".format(vnfr_id, index_number)
1589 if vdu_id:
1590 namespace += ".{}-{}".format(vdu_id, index_number)
1591 element_type = "VDU"
1592 element_under_configuration = "{}-{}".format(vdu_id, index_number)
1593 osm_config["osm"]["vdu_id"] = vdu_id
1594 elif kdu_name:
1595 namespace += ".{}".format(kdu_name)
1596 element_type = "KDU"
1597 element_under_configuration = kdu_name
1598 osm_config["osm"]["kdu_name"] = kdu_name
1599
1600 # Get artifact path
1601 if base_folder["pkg-dir"]:
1602 artifact_path = "{}/{}/{}/{}".format(
1603 base_folder["folder"],
1604 base_folder["pkg-dir"],
1605 "charms"
1606 if vca_type
1607 in ("native_charm", "lxc_proxy_charm", "k8s_proxy_charm")
1608 else "helm-charts",
1609 vca_name,
1610 )
1611 else:
1612 artifact_path = "{}/Scripts/{}/{}/".format(
1613 base_folder["folder"],
1614 "charms"
1615 if vca_type
1616 in ("native_charm", "lxc_proxy_charm", "k8s_proxy_charm")
1617 else "helm-charts",
1618 vca_name,
1619 )
1620
1621 self.logger.debug("Artifact path > {}".format(artifact_path))
1622
1623 # get initial_config_primitive_list that applies to this element
1624 initial_config_primitive_list = config_descriptor.get(
1625 "initial-config-primitive"
1626 )
1627
1628 self.logger.debug(
1629 "Initial config primitive list > {}".format(
1630 initial_config_primitive_list
1631 )
1632 )
1633
1634 # add config if not present for NS charm
1635 ee_descriptor_id = ee_config_descriptor.get("id")
1636 self.logger.debug("EE Descriptor > {}".format(ee_descriptor_id))
1637 initial_config_primitive_list = get_ee_sorted_initial_config_primitive_list(
1638 initial_config_primitive_list, vca_deployed, ee_descriptor_id
1639 )
1640
1641 self.logger.debug(
1642 "Initial config primitive list #2 > {}".format(
1643 initial_config_primitive_list
1644 )
1645 )
1646 # n2vc_redesign STEP 3.1
1647 # find old ee_id if exists
1648 ee_id = vca_deployed.get("ee_id")
1649
1650 vca_id = self.get_vca_id(db_vnfr, db_nsr)
1651 # create or register execution environment in VCA
1652 if vca_type in ("lxc_proxy_charm", "k8s_proxy_charm", "helm-v3"):
1653 self._write_configuration_status(
1654 nsr_id=nsr_id,
1655 vca_index=vca_index,
1656 status="CREATING",
1657 element_under_configuration=element_under_configuration,
1658 element_type=element_type,
1659 )
1660
1661 step = "create execution environment"
1662 self.logger.debug(logging_text + step)
1663
1664 ee_id = None
1665 credentials = None
1666 if vca_type == "k8s_proxy_charm":
1667 ee_id = await self.vca_map[vca_type].install_k8s_proxy_charm(
1668 charm_name=artifact_path[artifact_path.rfind("/") + 1 :],
1669 namespace=namespace,
1670 artifact_path=artifact_path,
1671 db_dict=db_dict,
1672 vca_id=vca_id,
1673 )
1674 elif vca_type == "helm-v3":
1675 ee_id, credentials = await self.vca_map[
1676 vca_type
1677 ].create_execution_environment(
1678 namespace=nsr_id,
1679 reuse_ee_id=ee_id,
1680 db_dict=db_dict,
1681 config=osm_config,
1682 artifact_path=artifact_path,
1683 chart_model=vca_name,
1684 vca_type=vca_type,
1685 )
1686 else:
1687 ee_id, credentials = await self.vca_map[
1688 vca_type
1689 ].create_execution_environment(
1690 namespace=namespace,
1691 reuse_ee_id=ee_id,
1692 db_dict=db_dict,
1693 vca_id=vca_id,
1694 )
1695
1696 elif vca_type == "native_charm":
1697 step = "Waiting to VM being up and getting IP address"
1698 self.logger.debug(logging_text + step)
1699 rw_mgmt_ip = await self.wait_vm_up_insert_key_ro(
1700 logging_text,
1701 nsr_id,
1702 vnfr_id,
1703 vdu_id,
1704 vdu_index,
1705 user=None,
1706 pub_key=None,
1707 )
1708 credentials = {"hostname": rw_mgmt_ip}
1709 # get username
1710 username = deep_get(
1711 config_descriptor, ("config-access", "ssh-access", "default-user")
1712 )
1713 # TODO remove this when changes on IM regarding config-access:ssh-access:default-user were
1714 # merged. Meanwhile let's get username from initial-config-primitive
1715 if not username and initial_config_primitive_list:
1716 for config_primitive in initial_config_primitive_list:
1717 for param in config_primitive.get("parameter", ()):
1718 if param["name"] == "ssh-username":
1719 username = param["value"]
1720 break
1721 if not username:
1722 raise LcmException(
1723 "Cannot determine the username neither with 'initial-config-primitive' nor with "
1724 "'config-access.ssh-access.default-user'"
1725 )
1726 credentials["username"] = username
1727 # n2vc_redesign STEP 3.2
1728
1729 self._write_configuration_status(
1730 nsr_id=nsr_id,
1731 vca_index=vca_index,
1732 status="REGISTERING",
1733 element_under_configuration=element_under_configuration,
1734 element_type=element_type,
1735 )
1736
1737 step = "register execution environment {}".format(credentials)
1738 self.logger.debug(logging_text + step)
1739 ee_id = await self.vca_map[vca_type].register_execution_environment(
1740 credentials=credentials,
1741 namespace=namespace,
1742 db_dict=db_dict,
1743 vca_id=vca_id,
1744 )
1745
1746 # for compatibility with MON/POL modules, the need model and application name at database
1747 # TODO ask MON/POL if needed to not assuming anymore the format "model_name.application_name"
1748 ee_id_parts = ee_id.split(".")
1749 db_nsr_update = {db_update_entry + "ee_id": ee_id}
1750 if len(ee_id_parts) >= 2:
1751 model_name = ee_id_parts[0]
1752 application_name = ee_id_parts[1]
1753 db_nsr_update[db_update_entry + "model"] = model_name
1754 db_nsr_update[db_update_entry + "application"] = application_name
1755
1756 # n2vc_redesign STEP 3.3
1757 step = "Install configuration Software"
1758
1759 self._write_configuration_status(
1760 nsr_id=nsr_id,
1761 vca_index=vca_index,
1762 status="INSTALLING SW",
1763 element_under_configuration=element_under_configuration,
1764 element_type=element_type,
1765 other_update=db_nsr_update,
1766 )
1767
1768 # TODO check if already done
1769 self.logger.debug(logging_text + step)
1770 config = None
1771 if vca_type == "native_charm":
1772 config_primitive = next(
1773 (p for p in initial_config_primitive_list if p["name"] == "config"),
1774 None,
1775 )
1776 if config_primitive:
1777 config = self._map_primitive_params(
1778 config_primitive, {}, deploy_params
1779 )
1780 num_units = 1
1781 if vca_type == "lxc_proxy_charm":
1782 if element_type == "NS":
1783 num_units = db_nsr.get("config-units") or 1
1784 elif element_type == "VNF":
1785 num_units = db_vnfr.get("config-units") or 1
1786 elif element_type == "VDU":
1787 for v in db_vnfr["vdur"]:
1788 if vdu_id == v["vdu-id-ref"]:
1789 num_units = v.get("config-units") or 1
1790 break
1791 if vca_type != "k8s_proxy_charm":
1792 await self.vca_map[vca_type].install_configuration_sw(
1793 ee_id=ee_id,
1794 artifact_path=artifact_path,
1795 db_dict=db_dict,
1796 config=config,
1797 num_units=num_units,
1798 vca_id=vca_id,
1799 vca_type=vca_type,
1800 )
1801
1802 # write in db flag of configuration_sw already installed
1803 self.update_db_2(
1804 "nsrs", nsr_id, {db_update_entry + "config_sw_installed": True}
1805 )
1806
1807 # add relations for this VCA (wait for other peers related with this VCA)
1808 is_relation_added = await self._add_vca_relations(
1809 logging_text=logging_text,
1810 nsr_id=nsr_id,
1811 vca_type=vca_type,
1812 vca_index=vca_index,
1813 )
1814
1815 if not is_relation_added:
1816 raise LcmException("Relations could not be added to VCA.")
1817
1818 # if SSH access is required, then get execution environment SSH public
1819 # if native charm we have waited already to VM be UP
1820 if vca_type in ("k8s_proxy_charm", "lxc_proxy_charm", "helm-v3"):
1821 pub_key = None
1822 user = None
1823 # self.logger.debug("get ssh key block")
1824 if deep_get(
1825 config_descriptor, ("config-access", "ssh-access", "required")
1826 ):
1827 # self.logger.debug("ssh key needed")
1828 # Needed to inject a ssh key
1829 user = deep_get(
1830 config_descriptor,
1831 ("config-access", "ssh-access", "default-user"),
1832 )
1833 step = "Install configuration Software, getting public ssh key"
1834 pub_key = await self.vca_map[vca_type].get_ee_ssh_public__key(
1835 ee_id=ee_id, db_dict=db_dict, vca_id=vca_id
1836 )
1837
1838 step = "Insert public key into VM user={} ssh_key={}".format(
1839 user, pub_key
1840 )
1841 else:
1842 # self.logger.debug("no need to get ssh key")
1843 step = "Waiting to VM being up and getting IP address"
1844 self.logger.debug(logging_text + step)
1845
1846 # default rw_mgmt_ip to None, avoiding the non definition of the variable
1847 rw_mgmt_ip = None
1848
1849 # n2vc_redesign STEP 5.1
1850 # wait for RO (ip-address) Insert pub_key into VM
1851 if vnfr_id:
1852 if kdu_name:
1853 rw_mgmt_ip, services = await self.wait_kdu_up(
1854 logging_text, nsr_id, vnfr_id, kdu_name
1855 )
1856 vnfd = self.db.get_one(
1857 "vnfds_revisions",
1858 {"_id": f'{db_vnfr["vnfd-id"]}:{db_vnfr["revision"]}'},
1859 )
1860 kdu = get_kdu(vnfd, kdu_name)
1861 kdu_services = [
1862 service["name"] for service in get_kdu_services(kdu)
1863 ]
1864 exposed_services = []
1865 for service in services:
1866 if any(s in service["name"] for s in kdu_services):
1867 exposed_services.append(service)
1868 await self.vca_map[vca_type].exec_primitive(
1869 ee_id=ee_id,
1870 primitive_name="config",
1871 params_dict={
1872 "osm-config": json.dumps(
1873 OsmConfigBuilder(
1874 k8s={"services": exposed_services}
1875 ).build()
1876 )
1877 },
1878 vca_id=vca_id,
1879 )
1880
1881 # This verification is needed in order to avoid trying to add a public key
1882 # to a VM, when the VNF is a KNF (in the edge case where the user creates a VCA
1883 # for a KNF and not for its KDUs, the previous verification gives False, and the code
1884 # jumps to this block, meaning that there is the need to verify if the VNF is actually a VNF
1885 # or it is a KNF)
1886 elif db_vnfr.get("vdur"):
1887 rw_mgmt_ip = await self.wait_vm_up_insert_key_ro(
1888 logging_text,
1889 nsr_id,
1890 vnfr_id,
1891 vdu_id,
1892 vdu_index,
1893 user=user,
1894 pub_key=pub_key,
1895 )
1896
1897 self.logger.debug(logging_text + " VM_ip_address={}".format(rw_mgmt_ip))
1898
1899 # store rw_mgmt_ip in deploy params for later replacement
1900 deploy_params["rw_mgmt_ip"] = rw_mgmt_ip
1901
1902 # n2vc_redesign STEP 6 Execute initial config primitive
1903 step = "execute initial config primitive"
1904
1905 # wait for dependent primitives execution (NS -> VNF -> VDU)
1906 if initial_config_primitive_list:
1907 await self._wait_dependent_n2vc(nsr_id, vca_deployed_list, vca_index)
1908
1909 # stage, in function of element type: vdu, kdu, vnf or ns
1910 my_vca = vca_deployed_list[vca_index]
1911 if my_vca.get("vdu_id") or my_vca.get("kdu_name"):
1912 # VDU or KDU
1913 stage[0] = "Stage 3/5: running Day-1 primitives for VDU."
1914 elif my_vca.get("member-vnf-index"):
1915 # VNF
1916 stage[0] = "Stage 4/5: running Day-1 primitives for VNF."
1917 else:
1918 # NS
1919 stage[0] = "Stage 5/5: running Day-1 primitives for NS."
1920
1921 self._write_configuration_status(
1922 nsr_id=nsr_id, vca_index=vca_index, status="EXECUTING PRIMITIVE"
1923 )
1924
1925 self._write_op_status(op_id=nslcmop_id, stage=stage)
1926
1927 check_if_terminated_needed = True
1928 for initial_config_primitive in initial_config_primitive_list:
1929 # adding information on the vca_deployed if it is a NS execution environment
1930 if not vca_deployed["member-vnf-index"]:
1931 deploy_params["ns_config_info"] = json.dumps(
1932 self._get_ns_config_info(nsr_id)
1933 )
1934 # TODO check if already done
1935 primitive_params_ = self._map_primitive_params(
1936 initial_config_primitive, {}, deploy_params
1937 )
1938
1939 step = "execute primitive '{}' params '{}'".format(
1940 initial_config_primitive["name"], primitive_params_
1941 )
1942 self.logger.debug(logging_text + step)
1943 await self.vca_map[vca_type].exec_primitive(
1944 ee_id=ee_id,
1945 primitive_name=initial_config_primitive["name"],
1946 params_dict=primitive_params_,
1947 db_dict=db_dict,
1948 vca_id=vca_id,
1949 vca_type=vca_type,
1950 )
1951 # Once some primitive has been exec, check and write at db if it needs to exec terminated primitives
1952 if check_if_terminated_needed:
1953 if config_descriptor.get("terminate-config-primitive"):
1954 self.update_db_2(
1955 "nsrs", nsr_id, {db_update_entry + "needed_terminate": True}
1956 )
1957 check_if_terminated_needed = False
1958
1959 # TODO register in database that primitive is done
1960
1961 # STEP 7 Configure metrics
1962 if vca_type == "helm-v3":
1963 # TODO: review for those cases where the helm chart is a reference and
1964 # is not part of the NF package
1965 prometheus_jobs = await self.extract_prometheus_scrape_jobs(
1966 ee_id=ee_id,
1967 artifact_path=artifact_path,
1968 ee_config_descriptor=ee_config_descriptor,
1969 vnfr_id=vnfr_id,
1970 nsr_id=nsr_id,
1971 target_ip=rw_mgmt_ip,
1972 element_type=element_type,
1973 vnf_member_index=db_vnfr.get("member-vnf-index-ref", ""),
1974 vdu_id=vdu_id,
1975 vdu_index=vdu_index,
1976 kdu_name=kdu_name,
1977 kdu_index=kdu_index,
1978 )
1979 if prometheus_jobs:
1980 self.update_db_2(
1981 "nsrs",
1982 nsr_id,
1983 {db_update_entry + "prometheus_jobs": prometheus_jobs},
1984 )
1985
1986 for job in prometheus_jobs:
1987 self.db.set_one(
1988 "prometheus_jobs",
1989 {"job_name": job["job_name"]},
1990 job,
1991 upsert=True,
1992 fail_on_empty=False,
1993 )
1994
1995 step = "instantiated at VCA"
1996 self.logger.debug(logging_text + step)
1997
1998 self._write_configuration_status(
1999 nsr_id=nsr_id, vca_index=vca_index, status="READY"
2000 )
2001
2002 except Exception as e: # TODO not use Exception but N2VC exception
2003 # self.update_db_2("nsrs", nsr_id, {db_update_entry + "instantiation": "FAILED"})
2004 if not isinstance(
2005 e, (DbException, N2VCException, LcmException, asyncio.CancelledError)
2006 ):
2007 self.logger.error(
2008 "Exception while {} : {}".format(step, e), exc_info=True
2009 )
2010 self._write_configuration_status(
2011 nsr_id=nsr_id, vca_index=vca_index, status="BROKEN"
2012 )
2013 raise LcmException("{}. {}".format(step, e)) from e
2014
2015 def _write_ns_status(
2016 self,
2017 nsr_id: str,
2018 ns_state: str,
2019 current_operation: str,
2020 current_operation_id: str,
2021 error_description: str = None,
2022 error_detail: str = None,
2023 other_update: dict = None,
2024 ):
2025 """
2026 Update db_nsr fields.
2027 :param nsr_id:
2028 :param ns_state:
2029 :param current_operation:
2030 :param current_operation_id:
2031 :param error_description:
2032 :param error_detail:
2033 :param other_update: Other required changes at database if provided, will be cleared
2034 :return:
2035 """
2036 try:
2037 db_dict = other_update or {}
2038 db_dict[
2039 "_admin.nslcmop"
2040 ] = current_operation_id # for backward compatibility
2041 db_dict["_admin.current-operation"] = current_operation_id
2042 db_dict["_admin.operation-type"] = (
2043 current_operation if current_operation != "IDLE" else None
2044 )
2045 db_dict["currentOperation"] = current_operation
2046 db_dict["currentOperationID"] = current_operation_id
2047 db_dict["errorDescription"] = error_description
2048 db_dict["errorDetail"] = error_detail
2049
2050 if ns_state:
2051 db_dict["nsState"] = ns_state
2052 self.update_db_2("nsrs", nsr_id, db_dict)
2053 except DbException as e:
2054 self.logger.warn("Error writing NS status, ns={}: {}".format(nsr_id, e))
2055
2056 def _write_op_status(
2057 self,
2058 op_id: str,
2059 stage: list = None,
2060 error_message: str = None,
2061 queuePosition: int = 0,
2062 operation_state: str = None,
2063 other_update: dict = None,
2064 ):
2065 try:
2066 db_dict = other_update or {}
2067 db_dict["queuePosition"] = queuePosition
2068 if isinstance(stage, list):
2069 db_dict["stage"] = stage[0]
2070 db_dict["detailed-status"] = " ".join(stage)
2071 elif stage is not None:
2072 db_dict["stage"] = str(stage)
2073
2074 if error_message is not None:
2075 db_dict["errorMessage"] = error_message
2076 if operation_state is not None:
2077 db_dict["operationState"] = operation_state
2078 db_dict["statusEnteredTime"] = time()
2079 self.update_db_2("nslcmops", op_id, db_dict)
2080 except DbException as e:
2081 self.logger.warn(
2082 "Error writing OPERATION status for op_id: {} -> {}".format(op_id, e)
2083 )
2084
2085 def _write_all_config_status(self, db_nsr: dict, status: str):
2086 try:
2087 nsr_id = db_nsr["_id"]
2088 # configurationStatus
2089 config_status = db_nsr.get("configurationStatus")
2090 if config_status:
2091 db_nsr_update = {
2092 "configurationStatus.{}.status".format(index): status
2093 for index, v in enumerate(config_status)
2094 if v
2095 }
2096 # update status
2097 self.update_db_2("nsrs", nsr_id, db_nsr_update)
2098
2099 except DbException as e:
2100 self.logger.warn(
2101 "Error writing all configuration status, ns={}: {}".format(nsr_id, e)
2102 )
2103
2104 def _write_configuration_status(
2105 self,
2106 nsr_id: str,
2107 vca_index: int,
2108 status: str = None,
2109 element_under_configuration: str = None,
2110 element_type: str = None,
2111 other_update: dict = None,
2112 ):
2113 # self.logger.debug('_write_configuration_status(): vca_index={}, status={}'
2114 # .format(vca_index, status))
2115
2116 try:
2117 db_path = "configurationStatus.{}.".format(vca_index)
2118 db_dict = other_update or {}
2119 if status:
2120 db_dict[db_path + "status"] = status
2121 if element_under_configuration:
2122 db_dict[
2123 db_path + "elementUnderConfiguration"
2124 ] = element_under_configuration
2125 if element_type:
2126 db_dict[db_path + "elementType"] = element_type
2127 self.update_db_2("nsrs", nsr_id, db_dict)
2128 except DbException as e:
2129 self.logger.warn(
2130 "Error writing configuration status={}, ns={}, vca_index={}: {}".format(
2131 status, nsr_id, vca_index, e
2132 )
2133 )
2134
2135 async def _do_placement(self, logging_text, db_nslcmop, db_vnfrs):
2136 """
2137 Check and computes the placement, (vim account where to deploy). If it is decided by an external tool, it
2138 sends the request via kafka and wait until the result is wrote at database (nslcmops _admin.plca).
2139 Database is used because the result can be obtained from a different LCM worker in case of HA.
2140 :param logging_text: contains the prefix for logging, with the ns and nslcmop identifiers
2141 :param db_nslcmop: database content of nslcmop
2142 :param db_vnfrs: database content of vnfrs, indexed by member-vnf-index.
2143 :return: True if some modification is done. Modifies database vnfrs and parameter db_vnfr with the
2144 computed 'vim-account-id'
2145 """
2146 modified = False
2147 nslcmop_id = db_nslcmop["_id"]
2148 placement_engine = deep_get(db_nslcmop, ("operationParams", "placement-engine"))
2149 if placement_engine == "PLA":
2150 self.logger.debug(
2151 logging_text + "Invoke and wait for placement optimization"
2152 )
2153 await self.msg.aiowrite("pla", "get_placement", {"nslcmopId": nslcmop_id})
2154 db_poll_interval = 5
2155 wait = db_poll_interval * 10
2156 pla_result = None
2157 while not pla_result and wait >= 0:
2158 await asyncio.sleep(db_poll_interval)
2159 wait -= db_poll_interval
2160 db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
2161 pla_result = deep_get(db_nslcmop, ("_admin", "pla"))
2162
2163 if not pla_result:
2164 raise LcmException(
2165 "Placement timeout for nslcmopId={}".format(nslcmop_id)
2166 )
2167
2168 for pla_vnf in pla_result["vnf"]:
2169 vnfr = db_vnfrs.get(pla_vnf["member-vnf-index"])
2170 if not pla_vnf.get("vimAccountId") or not vnfr:
2171 continue
2172 modified = True
2173 self.db.set_one(
2174 "vnfrs",
2175 {"_id": vnfr["_id"]},
2176 {"vim-account-id": pla_vnf["vimAccountId"]},
2177 )
2178 # Modifies db_vnfrs
2179 vnfr["vim-account-id"] = pla_vnf["vimAccountId"]
2180 return modified
2181
2182 def _gather_vnfr_healing_alerts(self, vnfr, vnfd):
2183 alerts = []
2184 nsr_id = vnfr["nsr-id-ref"]
2185 df = vnfd.get("df", [{}])[0]
2186 # Checking for auto-healing configuration
2187 if "healing-aspect" in df:
2188 healing_aspects = df["healing-aspect"]
2189 for healing in healing_aspects:
2190 for healing_policy in healing.get("healing-policy", ()):
2191 vdu_id = healing_policy["vdu-id"]
2192 vdur = next(
2193 (vdur for vdur in vnfr["vdur"] if vdu_id == vdur["vdu-id-ref"]),
2194 {},
2195 )
2196 if not vdur:
2197 continue
2198 metric_name = "vm_status"
2199 vdu_name = vdur.get("name")
2200 vnf_member_index = vnfr["member-vnf-index-ref"]
2201 uuid = str(uuid4())
2202 name = f"healing_{uuid}"
2203 action = healing_policy
2204 # action_on_recovery = healing.get("action-on-recovery")
2205 # cooldown_time = healing.get("cooldown-time")
2206 # day1 = healing.get("day1")
2207 alert = {
2208 "uuid": uuid,
2209 "name": name,
2210 "metric": metric_name,
2211 "tags": {
2212 "ns_id": nsr_id,
2213 "vnf_member_index": vnf_member_index,
2214 "vdu_name": vdu_name,
2215 },
2216 "alarm_status": "ok",
2217 "action_type": "healing",
2218 "action": action,
2219 }
2220 alerts.append(alert)
2221 return alerts
2222
2223 def _gather_vnfr_scaling_alerts(self, vnfr, vnfd):
2224 alerts = []
2225 nsr_id = vnfr["nsr-id-ref"]
2226 df = vnfd.get("df", [{}])[0]
2227 # Checking for auto-scaling configuration
2228 if "scaling-aspect" in df:
2229 scaling_aspects = df["scaling-aspect"]
2230 all_vnfd_monitoring_params = {}
2231 for ivld in vnfd.get("int-virtual-link-desc", ()):
2232 for mp in ivld.get("monitoring-parameters", ()):
2233 all_vnfd_monitoring_params[mp.get("id")] = mp
2234 for vdu in vnfd.get("vdu", ()):
2235 for mp in vdu.get("monitoring-parameter", ()):
2236 all_vnfd_monitoring_params[mp.get("id")] = mp
2237 for df in vnfd.get("df", ()):
2238 for mp in df.get("monitoring-parameter", ()):
2239 all_vnfd_monitoring_params[mp.get("id")] = mp
2240 for scaling_aspect in scaling_aspects:
2241 scaling_group_name = scaling_aspect.get("name", "")
2242 # Get monitored VDUs
2243 all_monitored_vdus = set()
2244 for delta in scaling_aspect.get("aspect-delta-details", {}).get(
2245 "deltas", ()
2246 ):
2247 for vdu_delta in delta.get("vdu-delta", ()):
2248 all_monitored_vdus.add(vdu_delta.get("id"))
2249 monitored_vdurs = list(
2250 filter(
2251 lambda vdur: vdur["vdu-id-ref"] in all_monitored_vdus,
2252 vnfr["vdur"],
2253 )
2254 )
2255 if not monitored_vdurs:
2256 self.logger.error(
2257 "Scaling criteria is referring to a vnf-monitoring-param that does not contain a reference to a vdu or vnf metric"
2258 )
2259 continue
2260 for scaling_policy in scaling_aspect.get("scaling-policy", ()):
2261 if scaling_policy["scaling-type"] != "automatic":
2262 continue
2263 threshold_time = scaling_policy.get("threshold-time", "1")
2264 cooldown_time = scaling_policy.get("cooldown-time", "0")
2265 for scaling_criteria in scaling_policy["scaling-criteria"]:
2266 monitoring_param_ref = scaling_criteria.get(
2267 "vnf-monitoring-param-ref"
2268 )
2269 vnf_monitoring_param = all_vnfd_monitoring_params[
2270 monitoring_param_ref
2271 ]
2272 for vdur in monitored_vdurs:
2273 vdu_id = vdur["vdu-id-ref"]
2274 metric_name = vnf_monitoring_param.get("performance-metric")
2275 metric_name = f"osm_{metric_name}"
2276 vnf_member_index = vnfr["member-vnf-index-ref"]
2277 scalein_threshold = scaling_criteria.get(
2278 "scale-in-threshold"
2279 )
2280 scaleout_threshold = scaling_criteria.get(
2281 "scale-out-threshold"
2282 )
2283 # Looking for min/max-number-of-instances
2284 instances_min_number = 1
2285 instances_max_number = 1
2286 vdu_profile = df["vdu-profile"]
2287 if vdu_profile:
2288 profile = next(
2289 item for item in vdu_profile if item["id"] == vdu_id
2290 )
2291 instances_min_number = profile.get(
2292 "min-number-of-instances", 1
2293 )
2294 instances_max_number = profile.get(
2295 "max-number-of-instances", 1
2296 )
2297
2298 if scalein_threshold:
2299 uuid = str(uuid4())
2300 name = f"scalein_{uuid}"
2301 operation = scaling_criteria[
2302 "scale-in-relational-operation"
2303 ]
2304 rel_operator = self.rel_operation_types.get(
2305 operation, "<="
2306 )
2307 metric_selector = f'{metric_name}{{ns_id="{nsr_id}", vnf_member_index="{vnf_member_index}", vdu_id="{vdu_id}"}}'
2308 expression = f"(count ({metric_selector}) > {instances_min_number}) and (avg({metric_selector}) {rel_operator} {scalein_threshold})"
2309 labels = {
2310 "ns_id": nsr_id,
2311 "vnf_member_index": vnf_member_index,
2312 "vdu_id": vdu_id,
2313 }
2314 prom_cfg = {
2315 "alert": name,
2316 "expr": expression,
2317 "for": str(threshold_time) + "m",
2318 "labels": labels,
2319 }
2320 action = scaling_policy
2321 action = {
2322 "scaling-group": scaling_group_name,
2323 "cooldown-time": cooldown_time,
2324 }
2325 alert = {
2326 "uuid": uuid,
2327 "name": name,
2328 "metric": metric_name,
2329 "tags": {
2330 "ns_id": nsr_id,
2331 "vnf_member_index": vnf_member_index,
2332 "vdu_id": vdu_id,
2333 },
2334 "alarm_status": "ok",
2335 "action_type": "scale_in",
2336 "action": action,
2337 "prometheus_config": prom_cfg,
2338 }
2339 alerts.append(alert)
2340
2341 if scaleout_threshold:
2342 uuid = str(uuid4())
2343 name = f"scaleout_{uuid}"
2344 operation = scaling_criteria[
2345 "scale-out-relational-operation"
2346 ]
2347 rel_operator = self.rel_operation_types.get(
2348 operation, "<="
2349 )
2350 metric_selector = f'{metric_name}{{ns_id="{nsr_id}", vnf_member_index="{vnf_member_index}", vdu_id="{vdu_id}"}}'
2351 expression = f"(count ({metric_selector}) < {instances_max_number}) and (avg({metric_selector}) {rel_operator} {scaleout_threshold})"
2352 labels = {
2353 "ns_id": nsr_id,
2354 "vnf_member_index": vnf_member_index,
2355 "vdu_id": vdu_id,
2356 }
2357 prom_cfg = {
2358 "alert": name,
2359 "expr": expression,
2360 "for": str(threshold_time) + "m",
2361 "labels": labels,
2362 }
2363 action = scaling_policy
2364 action = {
2365 "scaling-group": scaling_group_name,
2366 "cooldown-time": cooldown_time,
2367 }
2368 alert = {
2369 "uuid": uuid,
2370 "name": name,
2371 "metric": metric_name,
2372 "tags": {
2373 "ns_id": nsr_id,
2374 "vnf_member_index": vnf_member_index,
2375 "vdu_id": vdu_id,
2376 },
2377 "alarm_status": "ok",
2378 "action_type": "scale_out",
2379 "action": action,
2380 "prometheus_config": prom_cfg,
2381 }
2382 alerts.append(alert)
2383 return alerts
2384
2385 def _gather_vnfr_alarm_alerts(self, vnfr, vnfd):
2386 alerts = []
2387 nsr_id = vnfr["nsr-id-ref"]
2388 vnf_member_index = vnfr["member-vnf-index-ref"]
2389
2390 # Checking for VNF alarm configuration
2391 for vdur in vnfr["vdur"]:
2392 vdu_id = vdur["vdu-id-ref"]
2393 vdu = next(filter(lambda vdu: vdu["id"] == vdu_id, vnfd["vdu"]))
2394 if "alarm" in vdu:
2395 # Get VDU monitoring params, since alerts are based on them
2396 vdu_monitoring_params = {}
2397 for mp in vdu.get("monitoring-parameter", []):
2398 vdu_monitoring_params[mp.get("id")] = mp
2399 if not vdu_monitoring_params:
2400 self.logger.error(
2401 "VDU alarm refers to a VDU monitoring param, but there are no VDU monitoring params in the VDU"
2402 )
2403 continue
2404 # Get alarms in the VDU
2405 alarm_descriptors = vdu["alarm"]
2406 # Create VDU alarms for each alarm in the VDU
2407 for alarm_descriptor in alarm_descriptors:
2408 # Check that the VDU alarm refers to a proper monitoring param
2409 alarm_monitoring_param = alarm_descriptor.get(
2410 "vnf-monitoring-param-ref", ""
2411 )
2412 vdu_specific_monitoring_param = vdu_monitoring_params.get(
2413 alarm_monitoring_param, {}
2414 )
2415 if not vdu_specific_monitoring_param:
2416 self.logger.error(
2417 "VDU alarm refers to a VDU monitoring param not present in the VDU"
2418 )
2419 continue
2420 metric_name = vdu_specific_monitoring_param.get(
2421 "performance-metric"
2422 )
2423 if not metric_name:
2424 self.logger.error(
2425 "VDU alarm refers to a VDU monitoring param that has no associated performance-metric"
2426 )
2427 continue
2428 # Set params of the alarm to be created in Prometheus
2429 metric_name = f"osm_{metric_name}"
2430 metric_threshold = alarm_descriptor.get("value")
2431 uuid = str(uuid4())
2432 alert_name = f"vdu_alarm_{uuid}"
2433 operation = alarm_descriptor["operation"]
2434 rel_operator = self.rel_operation_types.get(operation, "<=")
2435 metric_selector = f'{metric_name}{{ns_id="{nsr_id}", vnf_member_index="{vnf_member_index}", vdu_id="{vdu_id}"}}'
2436 expression = f"{metric_selector} {rel_operator} {metric_threshold}"
2437 labels = {
2438 "ns_id": nsr_id,
2439 "vnf_member_index": vnf_member_index,
2440 "vdu_id": vdu_id,
2441 "vdu_name": "{{ $labels.vdu_name }}",
2442 }
2443 prom_cfg = {
2444 "alert": alert_name,
2445 "expr": expression,
2446 "for": "1m", # default value. Ideally, this should be related to an IM param, but there is not such param
2447 "labels": labels,
2448 }
2449 alarm_action = dict()
2450 for action_type in ["ok", "insufficient-data", "alarm"]:
2451 if (
2452 "actions" in alarm_descriptor
2453 and action_type in alarm_descriptor["actions"]
2454 ):
2455 alarm_action[action_type] = alarm_descriptor["actions"][
2456 action_type
2457 ]
2458 alert = {
2459 "uuid": uuid,
2460 "name": alert_name,
2461 "metric": metric_name,
2462 "tags": {
2463 "ns_id": nsr_id,
2464 "vnf_member_index": vnf_member_index,
2465 "vdu_id": vdu_id,
2466 },
2467 "alarm_status": "ok",
2468 "action_type": "vdu_alarm",
2469 "action": alarm_action,
2470 "prometheus_config": prom_cfg,
2471 }
2472 alerts.append(alert)
2473 return alerts
2474
2475 def update_nsrs_with_pla_result(self, params):
2476 try:
2477 nslcmop_id = deep_get(params, ("placement", "nslcmopId"))
2478 self.update_db_2(
2479 "nslcmops", nslcmop_id, {"_admin.pla": params.get("placement")}
2480 )
2481 except Exception as e:
2482 self.logger.warn("Update failed for nslcmop_id={}:{}".format(nslcmop_id, e))
2483
2484 async def instantiate(self, nsr_id, nslcmop_id):
2485 """
2486
2487 :param nsr_id: ns instance to deploy
2488 :param nslcmop_id: operation to run
2489 :return:
2490 """
2491
2492 # Try to lock HA task here
2493 task_is_locked_by_me = self.lcm_tasks.lock_HA("ns", "nslcmops", nslcmop_id)
2494 if not task_is_locked_by_me:
2495 self.logger.debug(
2496 "instantiate() task is not locked by me, ns={}".format(nsr_id)
2497 )
2498 return
2499
2500 logging_text = "Task ns={} instantiate={} ".format(nsr_id, nslcmop_id)
2501 self.logger.debug(logging_text + "Enter")
2502
2503 # get all needed from database
2504
2505 # database nsrs record
2506 db_nsr = None
2507
2508 # database nslcmops record
2509 db_nslcmop = None
2510
2511 # update operation on nsrs
2512 db_nsr_update = {}
2513 # update operation on nslcmops
2514 db_nslcmop_update = {}
2515
2516 timeout_ns_deploy = self.timeout.ns_deploy
2517
2518 nslcmop_operation_state = None
2519 db_vnfrs = {} # vnf's info indexed by member-index
2520 # n2vc_info = {}
2521 tasks_dict_info = {} # from task to info text
2522 exc = None
2523 error_list = []
2524 stage = [
2525 "Stage 1/5: preparation of the environment.",
2526 "Waiting for previous operations to terminate.",
2527 "",
2528 ]
2529 # ^ stage, step, VIM progress
2530 try:
2531 # wait for any previous tasks in process
2532 await self.lcm_tasks.waitfor_related_HA("ns", "nslcmops", nslcmop_id)
2533
2534 # STEP 0: Reading database (nslcmops, nsrs, nsds, vnfrs, vnfds)
2535 stage[1] = "Reading from database."
2536 # nsState="BUILDING", currentOperation="INSTANTIATING", currentOperationID=nslcmop_id
2537 db_nsr_update["detailed-status"] = "creating"
2538 db_nsr_update["operational-status"] = "init"
2539 self._write_ns_status(
2540 nsr_id=nsr_id,
2541 ns_state="BUILDING",
2542 current_operation="INSTANTIATING",
2543 current_operation_id=nslcmop_id,
2544 other_update=db_nsr_update,
2545 )
2546 self._write_op_status(op_id=nslcmop_id, stage=stage, queuePosition=0)
2547
2548 # read from db: operation
2549 stage[1] = "Getting nslcmop={} from db.".format(nslcmop_id)
2550 db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
2551 if db_nslcmop["operationParams"].get("additionalParamsForVnf"):
2552 db_nslcmop["operationParams"]["additionalParamsForVnf"] = json.loads(
2553 db_nslcmop["operationParams"]["additionalParamsForVnf"]
2554 )
2555 ns_params = db_nslcmop.get("operationParams")
2556 if ns_params and ns_params.get("timeout_ns_deploy"):
2557 timeout_ns_deploy = ns_params["timeout_ns_deploy"]
2558
2559 # read from db: ns
2560 stage[1] = "Getting nsr={} from db.".format(nsr_id)
2561 self.logger.debug(logging_text + stage[1])
2562 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
2563 stage[1] = "Getting nsd={} from db.".format(db_nsr["nsd-id"])
2564 self.logger.debug(logging_text + stage[1])
2565 nsd = self.db.get_one("nsds", {"_id": db_nsr["nsd-id"]})
2566 self.fs.sync(db_nsr["nsd-id"])
2567 db_nsr["nsd"] = nsd
2568 # nsr_name = db_nsr["name"] # TODO short-name??
2569
2570 # read from db: vnf's of this ns
2571 stage[1] = "Getting vnfrs from db."
2572 self.logger.debug(logging_text + stage[1])
2573 db_vnfrs_list = self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id})
2574
2575 # read from db: vnfd's for every vnf
2576 db_vnfds = [] # every vnfd data
2577
2578 # for each vnf in ns, read vnfd
2579 for vnfr in db_vnfrs_list:
2580 if vnfr.get("kdur"):
2581 kdur_list = []
2582 for kdur in vnfr["kdur"]:
2583 if kdur.get("additionalParams"):
2584 kdur["additionalParams"] = json.loads(
2585 kdur["additionalParams"]
2586 )
2587 kdur_list.append(kdur)
2588 vnfr["kdur"] = kdur_list
2589
2590 db_vnfrs[vnfr["member-vnf-index-ref"]] = vnfr
2591 vnfd_id = vnfr["vnfd-id"]
2592 vnfd_ref = vnfr["vnfd-ref"]
2593 self.fs.sync(vnfd_id)
2594
2595 # if we haven't this vnfd, read it from db
2596 if vnfd_id not in db_vnfds:
2597 # read from db
2598 stage[1] = "Getting vnfd={} id='{}' from db.".format(
2599 vnfd_id, vnfd_ref
2600 )
2601 self.logger.debug(logging_text + stage[1])
2602 vnfd = self.db.get_one("vnfds", {"_id": vnfd_id})
2603
2604 # store vnfd
2605 db_vnfds.append(vnfd)
2606
2607 # Get or generates the _admin.deployed.VCA list
2608 vca_deployed_list = None
2609 if db_nsr["_admin"].get("deployed"):
2610 vca_deployed_list = db_nsr["_admin"]["deployed"].get("VCA")
2611 if vca_deployed_list is None:
2612 vca_deployed_list = []
2613 configuration_status_list = []
2614 db_nsr_update["_admin.deployed.VCA"] = vca_deployed_list
2615 db_nsr_update["configurationStatus"] = configuration_status_list
2616 # add _admin.deployed.VCA to db_nsr dictionary, value=vca_deployed_list
2617 populate_dict(db_nsr, ("_admin", "deployed", "VCA"), vca_deployed_list)
2618 elif isinstance(vca_deployed_list, dict):
2619 # maintain backward compatibility. Change a dict to list at database
2620 vca_deployed_list = list(vca_deployed_list.values())
2621 db_nsr_update["_admin.deployed.VCA"] = vca_deployed_list
2622 populate_dict(db_nsr, ("_admin", "deployed", "VCA"), vca_deployed_list)
2623
2624 if not isinstance(
2625 deep_get(db_nsr, ("_admin", "deployed", "RO", "vnfd")), list
2626 ):
2627 populate_dict(db_nsr, ("_admin", "deployed", "RO", "vnfd"), [])
2628 db_nsr_update["_admin.deployed.RO.vnfd"] = []
2629
2630 # set state to INSTANTIATED. When instantiated NBI will not delete directly
2631 db_nsr_update["_admin.nsState"] = "INSTANTIATED"
2632 self.update_db_2("nsrs", nsr_id, db_nsr_update)
2633 self.db.set_list(
2634 "vnfrs", {"nsr-id-ref": nsr_id}, {"_admin.nsState": "INSTANTIATED"}
2635 )
2636
2637 # n2vc_redesign STEP 2 Deploy Network Scenario
2638 stage[0] = "Stage 2/5: deployment of KDUs, VMs and execution environments."
2639 self._write_op_status(op_id=nslcmop_id, stage=stage)
2640
2641 stage[1] = "Deploying KDUs."
2642 # self.logger.debug(logging_text + "Before deploy_kdus")
2643 # Call to deploy_kdus in case exists the "vdu:kdu" param
2644 await self.deploy_kdus(
2645 logging_text=logging_text,
2646 nsr_id=nsr_id,
2647 nslcmop_id=nslcmop_id,
2648 db_vnfrs=db_vnfrs,
2649 db_vnfds=db_vnfds,
2650 task_instantiation_info=tasks_dict_info,
2651 )
2652
2653 stage[1] = "Getting VCA public key."
2654 # n2vc_redesign STEP 1 Get VCA public ssh-key
2655 # feature 1429. Add n2vc public key to needed VMs
2656 n2vc_key = self.n2vc.get_public_key()
2657 n2vc_key_list = [n2vc_key]
2658 if self.vca_config.public_key:
2659 n2vc_key_list.append(self.vca_config.public_key)
2660
2661 stage[1] = "Deploying NS at VIM."
2662 task_ro = asyncio.ensure_future(
2663 self.instantiate_RO(
2664 logging_text=logging_text,
2665 nsr_id=nsr_id,
2666 nsd=nsd,
2667 db_nsr=db_nsr,
2668 db_nslcmop=db_nslcmop,
2669 db_vnfrs=db_vnfrs,
2670 db_vnfds=db_vnfds,
2671 n2vc_key_list=n2vc_key_list,
2672 stage=stage,
2673 )
2674 )
2675 self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "instantiate_RO", task_ro)
2676 tasks_dict_info[task_ro] = "Deploying at VIM"
2677
2678 # n2vc_redesign STEP 3 to 6 Deploy N2VC
2679 stage[1] = "Deploying Execution Environments."
2680 self.logger.debug(logging_text + stage[1])
2681
2682 # create namespace and certificate if any helm based EE is present in the NS
2683 if check_helm_ee_in_ns(db_vnfds):
2684 await self.vca_map["helm-v3"].setup_ns_namespace(
2685 name=nsr_id,
2686 )
2687 # create TLS certificates
2688 await self.vca_map["helm-v3"].create_tls_certificate(
2689 secret_name=self.EE_TLS_NAME,
2690 dns_prefix="*",
2691 nsr_id=nsr_id,
2692 usage="server auth",
2693 namespace=nsr_id,
2694 )
2695
2696 nsi_id = None # TODO put nsi_id when this nsr belongs to a NSI
2697 for vnf_profile in get_vnf_profiles(nsd):
2698 vnfd_id = vnf_profile["vnfd-id"]
2699 vnfd = find_in_list(db_vnfds, lambda a_vnf: a_vnf["id"] == vnfd_id)
2700 member_vnf_index = str(vnf_profile["id"])
2701 db_vnfr = db_vnfrs[member_vnf_index]
2702 base_folder = vnfd["_admin"]["storage"]
2703 vdu_id = None
2704 vdu_index = 0
2705 vdu_name = None
2706 kdu_name = None
2707 kdu_index = None
2708
2709 # Get additional parameters
2710 deploy_params = {"OSM": get_osm_params(db_vnfr)}
2711 if db_vnfr.get("additionalParamsForVnf"):
2712 deploy_params.update(
2713 parse_yaml_strings(db_vnfr["additionalParamsForVnf"].copy())
2714 )
2715
2716 descriptor_config = get_configuration(vnfd, vnfd["id"])
2717 if descriptor_config:
2718 self._deploy_n2vc(
2719 logging_text=logging_text
2720 + "member_vnf_index={} ".format(member_vnf_index),
2721 db_nsr=db_nsr,
2722 db_vnfr=db_vnfr,
2723 nslcmop_id=nslcmop_id,
2724 nsr_id=nsr_id,
2725 nsi_id=nsi_id,
2726 vnfd_id=vnfd_id,
2727 vdu_id=vdu_id,
2728 kdu_name=kdu_name,
2729 member_vnf_index=member_vnf_index,
2730 vdu_index=vdu_index,
2731 kdu_index=kdu_index,
2732 vdu_name=vdu_name,
2733 deploy_params=deploy_params,
2734 descriptor_config=descriptor_config,
2735 base_folder=base_folder,
2736 task_instantiation_info=tasks_dict_info,
2737 stage=stage,
2738 )
2739
2740 # Deploy charms for each VDU that supports one.
2741 for vdud in get_vdu_list(vnfd):
2742 vdu_id = vdud["id"]
2743 descriptor_config = get_configuration(vnfd, vdu_id)
2744 vdur = find_in_list(
2745 db_vnfr["vdur"], lambda vdu: vdu["vdu-id-ref"] == vdu_id
2746 )
2747
2748 if vdur.get("additionalParams"):
2749 deploy_params_vdu = parse_yaml_strings(vdur["additionalParams"])
2750 else:
2751 deploy_params_vdu = deploy_params
2752 deploy_params_vdu["OSM"] = get_osm_params(
2753 db_vnfr, vdu_id, vdu_count_index=0
2754 )
2755 vdud_count = get_number_of_instances(vnfd, vdu_id)
2756
2757 self.logger.debug("VDUD > {}".format(vdud))
2758 self.logger.debug(
2759 "Descriptor config > {}".format(descriptor_config)
2760 )
2761 if descriptor_config:
2762 vdu_name = None
2763 kdu_name = None
2764 kdu_index = None
2765 for vdu_index in range(vdud_count):
2766 # TODO vnfr_params["rw_mgmt_ip"] = vdur["ip-address"]
2767 self._deploy_n2vc(
2768 logging_text=logging_text
2769 + "member_vnf_index={}, vdu_id={}, vdu_index={} ".format(
2770 member_vnf_index, vdu_id, vdu_index
2771 ),
2772 db_nsr=db_nsr,
2773 db_vnfr=db_vnfr,
2774 nslcmop_id=nslcmop_id,
2775 nsr_id=nsr_id,
2776 nsi_id=nsi_id,
2777 vnfd_id=vnfd_id,
2778 vdu_id=vdu_id,
2779 kdu_name=kdu_name,
2780 kdu_index=kdu_index,
2781 member_vnf_index=member_vnf_index,
2782 vdu_index=vdu_index,
2783 vdu_name=vdu_name,
2784 deploy_params=deploy_params_vdu,
2785 descriptor_config=descriptor_config,
2786 base_folder=base_folder,
2787 task_instantiation_info=tasks_dict_info,
2788 stage=stage,
2789 )
2790 for kdud in get_kdu_list(vnfd):
2791 kdu_name = kdud["name"]
2792 descriptor_config = get_configuration(vnfd, kdu_name)
2793 if descriptor_config:
2794 vdu_id = None
2795 vdu_index = 0
2796 vdu_name = None
2797 kdu_index, kdur = next(
2798 x
2799 for x in enumerate(db_vnfr["kdur"])
2800 if x[1]["kdu-name"] == kdu_name
2801 )
2802 deploy_params_kdu = {"OSM": get_osm_params(db_vnfr)}
2803 if kdur.get("additionalParams"):
2804 deploy_params_kdu.update(
2805 parse_yaml_strings(kdur["additionalParams"].copy())
2806 )
2807
2808 self._deploy_n2vc(
2809 logging_text=logging_text,
2810 db_nsr=db_nsr,
2811 db_vnfr=db_vnfr,
2812 nslcmop_id=nslcmop_id,
2813 nsr_id=nsr_id,
2814 nsi_id=nsi_id,
2815 vnfd_id=vnfd_id,
2816 vdu_id=vdu_id,
2817 kdu_name=kdu_name,
2818 member_vnf_index=member_vnf_index,
2819 vdu_index=vdu_index,
2820 kdu_index=kdu_index,
2821 vdu_name=vdu_name,
2822 deploy_params=deploy_params_kdu,
2823 descriptor_config=descriptor_config,
2824 base_folder=base_folder,
2825 task_instantiation_info=tasks_dict_info,
2826 stage=stage,
2827 )
2828
2829 # Check if each vnf has exporter for metric collection if so update prometheus job records
2830 if "exporters-endpoints" in vnfd.get("df")[0]:
2831 exporter_config = vnfd.get("df")[0].get("exporters-endpoints")
2832 self.logger.debug("exporter config :{}".format(exporter_config))
2833 artifact_path = "{}/{}/{}".format(
2834 base_folder["folder"],
2835 base_folder["pkg-dir"],
2836 "exporter-endpoint",
2837 )
2838 ee_id = None
2839 ee_config_descriptor = exporter_config
2840 vnfr_id = db_vnfr["id"]
2841 rw_mgmt_ip = await self.wait_vm_up_insert_key_ro(
2842 logging_text,
2843 nsr_id,
2844 vnfr_id,
2845 vdu_id=None,
2846 vdu_index=None,
2847 user=None,
2848 pub_key=None,
2849 )
2850 self.logger.debug("rw_mgmt_ip:{}".format(rw_mgmt_ip))
2851 self.logger.debug("Artifact_path:{}".format(artifact_path))
2852 db_vnfr = self.db.get_one("vnfrs", {"_id": vnfr_id})
2853 vdu_id_for_prom = None
2854 vdu_index_for_prom = None
2855 for x in get_iterable(db_vnfr, "vdur"):
2856 vdu_id_for_prom = x.get("vdu-id-ref")
2857 vdu_index_for_prom = x.get("count-index")
2858 prometheus_jobs = await self.extract_prometheus_scrape_jobs(
2859 ee_id=ee_id,
2860 artifact_path=artifact_path,
2861 ee_config_descriptor=ee_config_descriptor,
2862 vnfr_id=vnfr_id,
2863 nsr_id=nsr_id,
2864 target_ip=rw_mgmt_ip,
2865 element_type="VDU",
2866 vdu_id=vdu_id_for_prom,
2867 vdu_index=vdu_index_for_prom,
2868 )
2869
2870 self.logger.debug("Prometheus job:{}".format(prometheus_jobs))
2871 if prometheus_jobs:
2872 db_nsr_update["_admin.deployed.prometheus_jobs"] = prometheus_jobs
2873 self.update_db_2(
2874 "nsrs",
2875 nsr_id,
2876 db_nsr_update,
2877 )
2878
2879 for job in prometheus_jobs:
2880 self.db.set_one(
2881 "prometheus_jobs",
2882 {"job_name": job["job_name"]},
2883 job,
2884 upsert=True,
2885 fail_on_empty=False,
2886 )
2887
2888 # Check if this NS has a charm configuration
2889 descriptor_config = nsd.get("ns-configuration")
2890 if descriptor_config and descriptor_config.get("juju"):
2891 vnfd_id = None
2892 db_vnfr = None
2893 member_vnf_index = None
2894 vdu_id = None
2895 kdu_name = None
2896 kdu_index = None
2897 vdu_index = 0
2898 vdu_name = None
2899
2900 # Get additional parameters
2901 deploy_params = {"OSM": {"vim_account_id": ns_params["vimAccountId"]}}
2902 if db_nsr.get("additionalParamsForNs"):
2903 deploy_params.update(
2904 parse_yaml_strings(db_nsr["additionalParamsForNs"].copy())
2905 )
2906 base_folder = nsd["_admin"]["storage"]
2907 self._deploy_n2vc(
2908 logging_text=logging_text,
2909 db_nsr=db_nsr,
2910 db_vnfr=db_vnfr,
2911 nslcmop_id=nslcmop_id,
2912 nsr_id=nsr_id,
2913 nsi_id=nsi_id,
2914 vnfd_id=vnfd_id,
2915 vdu_id=vdu_id,
2916 kdu_name=kdu_name,
2917 member_vnf_index=member_vnf_index,
2918 vdu_index=vdu_index,
2919 kdu_index=kdu_index,
2920 vdu_name=vdu_name,
2921 deploy_params=deploy_params,
2922 descriptor_config=descriptor_config,
2923 base_folder=base_folder,
2924 task_instantiation_info=tasks_dict_info,
2925 stage=stage,
2926 )
2927
2928 # rest of staff will be done at finally
2929
2930 except (
2931 ROclient.ROClientException,
2932 DbException,
2933 LcmException,
2934 N2VCException,
2935 ) as e:
2936 self.logger.error(
2937 logging_text + "Exit Exception while '{}': {}".format(stage[1], e)
2938 )
2939 exc = e
2940 except asyncio.CancelledError:
2941 self.logger.error(
2942 logging_text + "Cancelled Exception while '{}'".format(stage[1])
2943 )
2944 exc = "Operation was cancelled"
2945 except Exception as e:
2946 exc = traceback.format_exc()
2947 self.logger.critical(
2948 logging_text + "Exit Exception while '{}': {}".format(stage[1], e),
2949 exc_info=True,
2950 )
2951 finally:
2952 if exc:
2953 error_list.append(str(exc))
2954 try:
2955 # wait for pending tasks
2956 if tasks_dict_info:
2957 stage[1] = "Waiting for instantiate pending tasks."
2958 self.logger.debug(logging_text + stage[1])
2959 error_list += await self._wait_for_tasks(
2960 logging_text,
2961 tasks_dict_info,
2962 timeout_ns_deploy,
2963 stage,
2964 nslcmop_id,
2965 nsr_id=nsr_id,
2966 )
2967 stage[1] = stage[2] = ""
2968 except asyncio.CancelledError:
2969 error_list.append("Cancelled")
2970 await self._cancel_pending_tasks(logging_text, tasks_dict_info)
2971 await self._wait_for_tasks(
2972 logging_text,
2973 tasks_dict_info,
2974 timeout_ns_deploy,
2975 stage,
2976 nslcmop_id,
2977 nsr_id=nsr_id,
2978 )
2979 except Exception as exc:
2980 error_list.append(str(exc))
2981
2982 # update operation-status
2983 db_nsr_update["operational-status"] = "running"
2984 # let's begin with VCA 'configured' status (later we can change it)
2985 db_nsr_update["config-status"] = "configured"
2986 for task, task_name in tasks_dict_info.items():
2987 if not task.done() or task.cancelled() or task.exception():
2988 if task_name.startswith(self.task_name_deploy_vca):
2989 # A N2VC task is pending
2990 db_nsr_update["config-status"] = "failed"
2991 else:
2992 # RO or KDU task is pending
2993 db_nsr_update["operational-status"] = "failed"
2994
2995 # update status at database
2996 if error_list:
2997 error_detail = ". ".join(error_list)
2998 self.logger.error(logging_text + error_detail)
2999 error_description_nslcmop = "{} Detail: {}".format(
3000 stage[0], error_detail
3001 )
3002 error_description_nsr = "Operation: INSTANTIATING.{}, {}".format(
3003 nslcmop_id, stage[0]
3004 )
3005
3006 db_nsr_update["detailed-status"] = (
3007 error_description_nsr + " Detail: " + error_detail
3008 )
3009 db_nslcmop_update["detailed-status"] = error_detail
3010 nslcmop_operation_state = "FAILED"
3011 ns_state = "BROKEN"
3012 else:
3013 error_detail = None
3014 error_description_nsr = error_description_nslcmop = None
3015 ns_state = "READY"
3016 db_nsr_update["detailed-status"] = "Done"
3017 db_nslcmop_update["detailed-status"] = "Done"
3018 nslcmop_operation_state = "COMPLETED"
3019 # Gather auto-healing and auto-scaling alerts for each vnfr
3020 healing_alerts = []
3021 scaling_alerts = []
3022 for vnfr in self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id}):
3023 vnfd = next(
3024 (sub for sub in db_vnfds if sub["_id"] == vnfr["vnfd-id"]), None
3025 )
3026 healing_alerts = self._gather_vnfr_healing_alerts(vnfr, vnfd)
3027 for alert in healing_alerts:
3028 self.logger.info(f"Storing healing alert in MongoDB: {alert}")
3029 self.db.create("alerts", alert)
3030
3031 scaling_alerts = self._gather_vnfr_scaling_alerts(vnfr, vnfd)
3032 for alert in scaling_alerts:
3033 self.logger.info(f"Storing scaling alert in MongoDB: {alert}")
3034 self.db.create("alerts", alert)
3035
3036 alarm_alerts = self._gather_vnfr_alarm_alerts(vnfr, vnfd)
3037 for alert in alarm_alerts:
3038 self.logger.info(f"Storing VNF alarm alert in MongoDB: {alert}")
3039 self.db.create("alerts", alert)
3040 if db_nsr:
3041 self._write_ns_status(
3042 nsr_id=nsr_id,
3043 ns_state=ns_state,
3044 current_operation="IDLE",
3045 current_operation_id=None,
3046 error_description=error_description_nsr,
3047 error_detail=error_detail,
3048 other_update=db_nsr_update,
3049 )
3050 self._write_op_status(
3051 op_id=nslcmop_id,
3052 stage="",
3053 error_message=error_description_nslcmop,
3054 operation_state=nslcmop_operation_state,
3055 other_update=db_nslcmop_update,
3056 )
3057
3058 if nslcmop_operation_state:
3059 try:
3060 await self.msg.aiowrite(
3061 "ns",
3062 "instantiated",
3063 {
3064 "nsr_id": nsr_id,
3065 "nslcmop_id": nslcmop_id,
3066 "operationState": nslcmop_operation_state,
3067 "startTime": db_nslcmop["startTime"],
3068 "links": db_nslcmop["links"],
3069 "operationParams": {
3070 "nsInstanceId": nsr_id,
3071 "nsdId": db_nsr["nsd-id"],
3072 },
3073 },
3074 )
3075 except Exception as e:
3076 self.logger.error(
3077 logging_text + "kafka_write notification Exception {}".format(e)
3078 )
3079
3080 self.logger.debug(logging_text + "Exit")
3081 self.lcm_tasks.remove("ns", nsr_id, nslcmop_id, "ns_instantiate")
3082
3083 def _get_vnfd(self, vnfd_id: str, projects_read: str, cached_vnfds: Dict[str, Any]):
3084 if vnfd_id not in cached_vnfds:
3085 cached_vnfds[vnfd_id] = self.db.get_one(
3086 "vnfds", {"id": vnfd_id, "_admin.projects_read": projects_read}
3087 )
3088 return cached_vnfds[vnfd_id]
3089
3090 def _get_vnfr(self, nsr_id: str, vnf_profile_id: str, cached_vnfrs: Dict[str, Any]):
3091 if vnf_profile_id not in cached_vnfrs:
3092 cached_vnfrs[vnf_profile_id] = self.db.get_one(
3093 "vnfrs",
3094 {
3095 "member-vnf-index-ref": vnf_profile_id,
3096 "nsr-id-ref": nsr_id,
3097 },
3098 )
3099 return cached_vnfrs[vnf_profile_id]
3100
3101 def _is_deployed_vca_in_relation(
3102 self, vca: DeployedVCA, relation: Relation
3103 ) -> bool:
3104 found = False
3105 for endpoint in (relation.provider, relation.requirer):
3106 if endpoint["kdu-resource-profile-id"]:
3107 continue
3108 found = (
3109 vca.vnf_profile_id == endpoint.vnf_profile_id
3110 and vca.vdu_profile_id == endpoint.vdu_profile_id
3111 and vca.execution_environment_ref == endpoint.execution_environment_ref
3112 )
3113 if found:
3114 break
3115 return found
3116
3117 def _update_ee_relation_data_with_implicit_data(
3118 self, nsr_id, nsd, ee_relation_data, cached_vnfds, vnf_profile_id: str = None
3119 ):
3120 ee_relation_data = safe_get_ee_relation(
3121 nsr_id, ee_relation_data, vnf_profile_id=vnf_profile_id
3122 )
3123 ee_relation_level = EELevel.get_level(ee_relation_data)
3124 if (ee_relation_level in (EELevel.VNF, EELevel.VDU)) and not ee_relation_data[
3125 "execution-environment-ref"
3126 ]:
3127 vnf_profile = get_vnf_profile(nsd, ee_relation_data["vnf-profile-id"])
3128 vnfd_id = vnf_profile["vnfd-id"]
3129 project = nsd["_admin"]["projects_read"][0]
3130 db_vnfd = self._get_vnfd(vnfd_id, project, cached_vnfds)
3131 entity_id = (
3132 vnfd_id
3133 if ee_relation_level == EELevel.VNF
3134 else ee_relation_data["vdu-profile-id"]
3135 )
3136 ee = get_juju_ee_ref(db_vnfd, entity_id)
3137 if not ee:
3138 raise Exception(
3139 f"not execution environments found for ee_relation {ee_relation_data}"
3140 )
3141 ee_relation_data["execution-environment-ref"] = ee["id"]
3142 return ee_relation_data
3143
3144 def _get_ns_relations(
3145 self,
3146 nsr_id: str,
3147 nsd: Dict[str, Any],
3148 vca: DeployedVCA,
3149 cached_vnfds: Dict[str, Any],
3150 ) -> List[Relation]:
3151 relations = []
3152 db_ns_relations = get_ns_configuration_relation_list(nsd)
3153 for r in db_ns_relations:
3154 provider_dict = None
3155 requirer_dict = None
3156 if all(key in r for key in ("provider", "requirer")):
3157 provider_dict = r["provider"]
3158 requirer_dict = r["requirer"]
3159 elif "entities" in r:
3160 provider_id = r["entities"][0]["id"]
3161 provider_dict = {
3162 "nsr-id": nsr_id,
3163 "endpoint": r["entities"][0]["endpoint"],
3164 }
3165 if provider_id != nsd["id"]:
3166 provider_dict["vnf-profile-id"] = provider_id
3167 requirer_id = r["entities"][1]["id"]
3168 requirer_dict = {
3169 "nsr-id": nsr_id,
3170 "endpoint": r["entities"][1]["endpoint"],
3171 }
3172 if requirer_id != nsd["id"]:
3173 requirer_dict["vnf-profile-id"] = requirer_id
3174 else:
3175 raise Exception(
3176 "provider/requirer or entities must be included in the relation."
3177 )
3178 relation_provider = self._update_ee_relation_data_with_implicit_data(
3179 nsr_id, nsd, provider_dict, cached_vnfds
3180 )
3181 relation_requirer = self._update_ee_relation_data_with_implicit_data(
3182 nsr_id, nsd, requirer_dict, cached_vnfds
3183 )
3184 provider = EERelation(relation_provider)
3185 requirer = EERelation(relation_requirer)
3186 relation = Relation(r["name"], provider, requirer)
3187 vca_in_relation = self._is_deployed_vca_in_relation(vca, relation)
3188 if vca_in_relation:
3189 relations.append(relation)
3190 return relations
3191
3192 def _get_vnf_relations(
3193 self,
3194 nsr_id: str,
3195 nsd: Dict[str, Any],
3196 vca: DeployedVCA,
3197 cached_vnfds: Dict[str, Any],
3198 ) -> List[Relation]:
3199 relations = []
3200 if vca.target_element == "ns":
3201 self.logger.debug("VCA is a NS charm, not a VNF.")
3202 return relations
3203 vnf_profile = get_vnf_profile(nsd, vca.vnf_profile_id)
3204 vnf_profile_id = vnf_profile["id"]
3205 vnfd_id = vnf_profile["vnfd-id"]
3206 project = nsd["_admin"]["projects_read"][0]
3207 db_vnfd = self._get_vnfd(vnfd_id, project, cached_vnfds)
3208 db_vnf_relations = get_relation_list(db_vnfd, vnfd_id)
3209 for r in db_vnf_relations:
3210 provider_dict = None
3211 requirer_dict = None
3212 if all(key in r for key in ("provider", "requirer")):
3213 provider_dict = r["provider"]
3214 requirer_dict = r["requirer"]
3215 elif "entities" in r:
3216 provider_id = r["entities"][0]["id"]
3217 provider_dict = {
3218 "nsr-id": nsr_id,
3219 "vnf-profile-id": vnf_profile_id,
3220 "endpoint": r["entities"][0]["endpoint"],
3221 }
3222 if provider_id != vnfd_id:
3223 provider_dict["vdu-profile-id"] = provider_id
3224 requirer_id = r["entities"][1]["id"]
3225 requirer_dict = {
3226 "nsr-id": nsr_id,
3227 "vnf-profile-id": vnf_profile_id,
3228 "endpoint": r["entities"][1]["endpoint"],
3229 }
3230 if requirer_id != vnfd_id:
3231 requirer_dict["vdu-profile-id"] = requirer_id
3232 else:
3233 raise Exception(
3234 "provider/requirer or entities must be included in the relation."
3235 )
3236 relation_provider = self._update_ee_relation_data_with_implicit_data(
3237 nsr_id, nsd, provider_dict, cached_vnfds, vnf_profile_id=vnf_profile_id
3238 )
3239 relation_requirer = self._update_ee_relation_data_with_implicit_data(
3240 nsr_id, nsd, requirer_dict, cached_vnfds, vnf_profile_id=vnf_profile_id
3241 )
3242 provider = EERelation(relation_provider)
3243 requirer = EERelation(relation_requirer)
3244 relation = Relation(r["name"], provider, requirer)
3245 vca_in_relation = self._is_deployed_vca_in_relation(vca, relation)
3246 if vca_in_relation:
3247 relations.append(relation)
3248 return relations
3249
3250 def _get_kdu_resource_data(
3251 self,
3252 ee_relation: EERelation,
3253 db_nsr: Dict[str, Any],
3254 cached_vnfds: Dict[str, Any],
3255 ) -> DeployedK8sResource:
3256 nsd = get_nsd(db_nsr)
3257 vnf_profiles = get_vnf_profiles(nsd)
3258 vnfd_id = find_in_list(
3259 vnf_profiles,
3260 lambda vnf_profile: vnf_profile["id"] == ee_relation.vnf_profile_id,
3261 )["vnfd-id"]
3262 project = nsd["_admin"]["projects_read"][0]
3263 db_vnfd = self._get_vnfd(vnfd_id, project, cached_vnfds)
3264 kdu_resource_profile = get_kdu_resource_profile(
3265 db_vnfd, ee_relation.kdu_resource_profile_id
3266 )
3267 kdu_name = kdu_resource_profile["kdu-name"]
3268 deployed_kdu, _ = get_deployed_kdu(
3269 db_nsr.get("_admin", ()).get("deployed", ()),
3270 kdu_name,
3271 ee_relation.vnf_profile_id,
3272 )
3273 deployed_kdu.update({"resource-name": kdu_resource_profile["resource-name"]})
3274 return deployed_kdu
3275
3276 def _get_deployed_component(
3277 self,
3278 ee_relation: EERelation,
3279 db_nsr: Dict[str, Any],
3280 cached_vnfds: Dict[str, Any],
3281 ) -> DeployedComponent:
3282 nsr_id = db_nsr["_id"]
3283 deployed_component = None
3284 ee_level = EELevel.get_level(ee_relation)
3285 if ee_level == EELevel.NS:
3286 vca = get_deployed_vca(db_nsr, {"vdu_id": None, "member-vnf-index": None})
3287 if vca:
3288 deployed_component = DeployedVCA(nsr_id, vca)
3289 elif ee_level == EELevel.VNF:
3290 vca = get_deployed_vca(
3291 db_nsr,
3292 {
3293 "vdu_id": None,
3294 "member-vnf-index": ee_relation.vnf_profile_id,
3295 "ee_descriptor_id": ee_relation.execution_environment_ref,
3296 },
3297 )
3298 if vca:
3299 deployed_component = DeployedVCA(nsr_id, vca)
3300 elif ee_level == EELevel.VDU:
3301 vca = get_deployed_vca(
3302 db_nsr,
3303 {
3304 "vdu_id": ee_relation.vdu_profile_id,
3305 "member-vnf-index": ee_relation.vnf_profile_id,
3306 "ee_descriptor_id": ee_relation.execution_environment_ref,
3307 },
3308 )
3309 if vca:
3310 deployed_component = DeployedVCA(nsr_id, vca)
3311 elif ee_level == EELevel.KDU:
3312 kdu_resource_data = self._get_kdu_resource_data(
3313 ee_relation, db_nsr, cached_vnfds
3314 )
3315 if kdu_resource_data:
3316 deployed_component = DeployedK8sResource(kdu_resource_data)
3317 return deployed_component
3318
3319 async def _add_relation(
3320 self,
3321 relation: Relation,
3322 vca_type: str,
3323 db_nsr: Dict[str, Any],
3324 cached_vnfds: Dict[str, Any],
3325 cached_vnfrs: Dict[str, Any],
3326 ) -> bool:
3327 deployed_provider = self._get_deployed_component(
3328 relation.provider, db_nsr, cached_vnfds
3329 )
3330 deployed_requirer = self._get_deployed_component(
3331 relation.requirer, db_nsr, cached_vnfds
3332 )
3333 if (
3334 deployed_provider
3335 and deployed_requirer
3336 and deployed_provider.config_sw_installed
3337 and deployed_requirer.config_sw_installed
3338 ):
3339 provider_db_vnfr = (
3340 self._get_vnfr(
3341 relation.provider.nsr_id,
3342 relation.provider.vnf_profile_id,
3343 cached_vnfrs,
3344 )
3345 if relation.provider.vnf_profile_id
3346 else None
3347 )
3348 requirer_db_vnfr = (
3349 self._get_vnfr(
3350 relation.requirer.nsr_id,
3351 relation.requirer.vnf_profile_id,
3352 cached_vnfrs,
3353 )
3354 if relation.requirer.vnf_profile_id
3355 else None
3356 )
3357 provider_vca_id = self.get_vca_id(provider_db_vnfr, db_nsr)
3358 requirer_vca_id = self.get_vca_id(requirer_db_vnfr, db_nsr)
3359 provider_relation_endpoint = RelationEndpoint(
3360 deployed_provider.ee_id,
3361 provider_vca_id,
3362 relation.provider.endpoint,
3363 )
3364 requirer_relation_endpoint = RelationEndpoint(
3365 deployed_requirer.ee_id,
3366 requirer_vca_id,
3367 relation.requirer.endpoint,
3368 )
3369 try:
3370 await self.vca_map[vca_type].add_relation(
3371 provider=provider_relation_endpoint,
3372 requirer=requirer_relation_endpoint,
3373 )
3374 except N2VCException as exception:
3375 self.logger.error(exception)
3376 raise LcmException(exception)
3377 return True
3378 return False
3379
3380 async def _add_vca_relations(
3381 self,
3382 logging_text,
3383 nsr_id,
3384 vca_type: str,
3385 vca_index: int,
3386 timeout: int = 3600,
3387 ) -> bool:
3388 # steps:
3389 # 1. find all relations for this VCA
3390 # 2. wait for other peers related
3391 # 3. add relations
3392
3393 try:
3394 # STEP 1: find all relations for this VCA
3395
3396 # read nsr record
3397 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
3398 nsd = get_nsd(db_nsr)
3399
3400 # this VCA data
3401 deployed_vca_dict = get_deployed_vca_list(db_nsr)[vca_index]
3402 my_vca = DeployedVCA(nsr_id, deployed_vca_dict)
3403
3404 cached_vnfds = {}
3405 cached_vnfrs = {}
3406 relations = []
3407 relations.extend(self._get_ns_relations(nsr_id, nsd, my_vca, cached_vnfds))
3408 relations.extend(self._get_vnf_relations(nsr_id, nsd, my_vca, cached_vnfds))
3409
3410 # if no relations, terminate
3411 if not relations:
3412 self.logger.debug(logging_text + " No relations")
3413 return True
3414
3415 self.logger.debug(logging_text + " adding relations {}".format(relations))
3416
3417 # add all relations
3418 start = time()
3419 while True:
3420 # check timeout
3421 now = time()
3422 if now - start >= timeout:
3423 self.logger.error(logging_text + " : timeout adding relations")
3424 return False
3425
3426 # reload nsr from database (we need to update record: _admin.deployed.VCA)
3427 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
3428
3429 # for each relation, find the VCA's related
3430 for relation in relations.copy():
3431 added = await self._add_relation(
3432 relation,
3433 vca_type,
3434 db_nsr,
3435 cached_vnfds,
3436 cached_vnfrs,
3437 )
3438 if added:
3439 relations.remove(relation)
3440
3441 if not relations:
3442 self.logger.debug("Relations added")
3443 break
3444 await asyncio.sleep(5.0)
3445
3446 return True
3447
3448 except Exception as e:
3449 self.logger.warn(logging_text + " ERROR adding relations: {}".format(e))
3450 return False
3451
3452 async def _install_kdu(
3453 self,
3454 nsr_id: str,
3455 nsr_db_path: str,
3456 vnfr_data: dict,
3457 kdu_index: int,
3458 kdud: dict,
3459 vnfd: dict,
3460 k8s_instance_info: dict,
3461 k8params: dict = None,
3462 timeout: int = 600,
3463 vca_id: str = None,
3464 ):
3465 try:
3466 k8sclustertype = k8s_instance_info["k8scluster-type"]
3467 # Instantiate kdu
3468 db_dict_install = {
3469 "collection": "nsrs",
3470 "filter": {"_id": nsr_id},
3471 "path": nsr_db_path,
3472 }
3473
3474 if k8s_instance_info.get("kdu-deployment-name"):
3475 kdu_instance = k8s_instance_info.get("kdu-deployment-name")
3476 else:
3477 kdu_instance = self.k8scluster_map[
3478 k8sclustertype
3479 ].generate_kdu_instance_name(
3480 db_dict=db_dict_install,
3481 kdu_model=k8s_instance_info["kdu-model"],
3482 kdu_name=k8s_instance_info["kdu-name"],
3483 )
3484
3485 # Update the nsrs table with the kdu-instance value
3486 self.update_db_2(
3487 item="nsrs",
3488 _id=nsr_id,
3489 _desc={nsr_db_path + ".kdu-instance": kdu_instance},
3490 )
3491
3492 # Update the nsrs table with the actual namespace being used, if the k8scluster-type is `juju` or
3493 # `juju-bundle`. This verification is needed because there is not a standard/homogeneous namespace
3494 # between the Helm Charts and Juju Bundles-based KNFs. If we found a way of having an homogeneous
3495 # namespace, this first verification could be removed, and the next step would be done for any kind
3496 # of KNF.
3497 # TODO -> find a way to have an homogeneous namespace between the Helm Charts and Juju Bundles-based
3498 # KNFs (Bug 2027: https://osm.etsi.org/bugzilla/show_bug.cgi?id=2027)
3499 if k8sclustertype in ("juju", "juju-bundle"):
3500 # First, verify if the current namespace is present in the `_admin.projects_read` (if not, it means
3501 # that the user passed a namespace which he wants its KDU to be deployed in)
3502 if (
3503 self.db.count(
3504 table="nsrs",
3505 q_filter={
3506 "_id": nsr_id,
3507 "_admin.projects_write": k8s_instance_info["namespace"],
3508 "_admin.projects_read": k8s_instance_info["namespace"],
3509 },
3510 )
3511 > 0
3512 ):
3513 self.logger.debug(
3514 f"Updating namespace/model for Juju Bundle from {k8s_instance_info['namespace']} to {kdu_instance}"
3515 )
3516 self.update_db_2(
3517 item="nsrs",
3518 _id=nsr_id,
3519 _desc={f"{nsr_db_path}.namespace": kdu_instance},
3520 )
3521 k8s_instance_info["namespace"] = kdu_instance
3522
3523 await self.k8scluster_map[k8sclustertype].install(
3524 cluster_uuid=k8s_instance_info["k8scluster-uuid"],
3525 kdu_model=k8s_instance_info["kdu-model"],
3526 atomic=True,
3527 params=k8params,
3528 db_dict=db_dict_install,
3529 timeout=timeout,
3530 kdu_name=k8s_instance_info["kdu-name"],
3531 namespace=k8s_instance_info["namespace"],
3532 kdu_instance=kdu_instance,
3533 vca_id=vca_id,
3534 )
3535
3536 # Obtain services to obtain management service ip
3537 services = await self.k8scluster_map[k8sclustertype].get_services(
3538 cluster_uuid=k8s_instance_info["k8scluster-uuid"],
3539 kdu_instance=kdu_instance,
3540 namespace=k8s_instance_info["namespace"],
3541 )
3542
3543 # Obtain management service info (if exists)
3544 vnfr_update_dict = {}
3545 kdu_config = get_configuration(vnfd, kdud["name"])
3546 if kdu_config:
3547 target_ee_list = kdu_config.get("execution-environment-list", [])
3548 else:
3549 target_ee_list = []
3550
3551 if services:
3552 vnfr_update_dict["kdur.{}.services".format(kdu_index)] = services
3553 mgmt_services = [
3554 service
3555 for service in kdud.get("service", [])
3556 if service.get("mgmt-service")
3557 ]
3558 for mgmt_service in mgmt_services:
3559 for service in services:
3560 if service["name"].startswith(mgmt_service["name"]):
3561 # Mgmt service found, Obtain service ip
3562 ip = service.get("external_ip", service.get("cluster_ip"))
3563 if isinstance(ip, list) and len(ip) == 1:
3564 ip = ip[0]
3565
3566 vnfr_update_dict[
3567 "kdur.{}.ip-address".format(kdu_index)
3568 ] = ip
3569
3570 # Check if must update also mgmt ip at the vnf
3571 service_external_cp = mgmt_service.get(
3572 "external-connection-point-ref"
3573 )
3574 if service_external_cp:
3575 if (
3576 deep_get(vnfd, ("mgmt-interface", "cp"))
3577 == service_external_cp
3578 ):
3579 vnfr_update_dict["ip-address"] = ip
3580
3581 if find_in_list(
3582 target_ee_list,
3583 lambda ee: ee.get(
3584 "external-connection-point-ref", ""
3585 )
3586 == service_external_cp,
3587 ):
3588 vnfr_update_dict[
3589 "kdur.{}.ip-address".format(kdu_index)
3590 ] = ip
3591 break
3592 else:
3593 self.logger.warn(
3594 "Mgmt service name: {} not found".format(
3595 mgmt_service["name"]
3596 )
3597 )
3598
3599 vnfr_update_dict["kdur.{}.status".format(kdu_index)] = "READY"
3600 self.update_db_2("vnfrs", vnfr_data.get("_id"), vnfr_update_dict)
3601
3602 kdu_config = get_configuration(vnfd, k8s_instance_info["kdu-name"])
3603 if (
3604 kdu_config
3605 and kdu_config.get("initial-config-primitive")
3606 and get_juju_ee_ref(vnfd, k8s_instance_info["kdu-name"]) is None
3607 ):
3608 initial_config_primitive_list = kdu_config.get(
3609 "initial-config-primitive"
3610 )
3611 initial_config_primitive_list.sort(key=lambda val: int(val["seq"]))
3612
3613 for initial_config_primitive in initial_config_primitive_list:
3614 primitive_params_ = self._map_primitive_params(
3615 initial_config_primitive, {}, {}
3616 )
3617
3618 await asyncio.wait_for(
3619 self.k8scluster_map[k8sclustertype].exec_primitive(
3620 cluster_uuid=k8s_instance_info["k8scluster-uuid"],
3621 kdu_instance=kdu_instance,
3622 primitive_name=initial_config_primitive["name"],
3623 params=primitive_params_,
3624 db_dict=db_dict_install,
3625 vca_id=vca_id,
3626 ),
3627 timeout=timeout,
3628 )
3629
3630 except Exception as e:
3631 # Prepare update db with error and raise exception
3632 try:
3633 self.update_db_2(
3634 "nsrs", nsr_id, {nsr_db_path + ".detailed-status": str(e)}
3635 )
3636 self.update_db_2(
3637 "vnfrs",
3638 vnfr_data.get("_id"),
3639 {"kdur.{}.status".format(kdu_index): "ERROR"},
3640 )
3641 except Exception as error:
3642 # ignore to keep original exception
3643 self.logger.warning(
3644 f"An exception occurred while updating DB: {str(error)}"
3645 )
3646 # reraise original error
3647 raise
3648
3649 return kdu_instance
3650
3651 async def deploy_kdus(
3652 self,
3653 logging_text,
3654 nsr_id,
3655 nslcmop_id,
3656 db_vnfrs,
3657 db_vnfds,
3658 task_instantiation_info,
3659 ):
3660 # Launch kdus if present in the descriptor
3661
3662 k8scluster_id_2_uuic = {
3663 "helm-chart-v3": {},
3664 "juju-bundle": {},
3665 }
3666
3667 async def _get_cluster_id(cluster_id, cluster_type):
3668 nonlocal k8scluster_id_2_uuic
3669 if cluster_id in k8scluster_id_2_uuic[cluster_type]:
3670 return k8scluster_id_2_uuic[cluster_type][cluster_id]
3671
3672 # check if K8scluster is creating and wait look if previous tasks in process
3673 task_name, task_dependency = self.lcm_tasks.lookfor_related(
3674 "k8scluster", cluster_id
3675 )
3676 if task_dependency:
3677 text = "Waiting for related tasks '{}' on k8scluster {} to be completed".format(
3678 task_name, cluster_id
3679 )
3680 self.logger.debug(logging_text + text)
3681 await asyncio.wait(task_dependency, timeout=3600)
3682
3683 db_k8scluster = self.db.get_one(
3684 "k8sclusters", {"_id": cluster_id}, fail_on_empty=False
3685 )
3686 if not db_k8scluster:
3687 raise LcmException("K8s cluster {} cannot be found".format(cluster_id))
3688
3689 k8s_id = deep_get(db_k8scluster, ("_admin", cluster_type, "id"))
3690 if not k8s_id:
3691 if cluster_type == "helm-chart-v3":
3692 try:
3693 # backward compatibility for existing clusters that have not been initialized for helm v3
3694 k8s_credentials = yaml.safe_dump(
3695 db_k8scluster.get("credentials")
3696 )
3697 k8s_id, uninstall_sw = await self.k8sclusterhelm3.init_env(
3698 k8s_credentials, reuse_cluster_uuid=cluster_id
3699 )
3700 db_k8scluster_update = {}
3701 db_k8scluster_update["_admin.helm-chart-v3.error_msg"] = None
3702 db_k8scluster_update["_admin.helm-chart-v3.id"] = k8s_id
3703 db_k8scluster_update[
3704 "_admin.helm-chart-v3.created"
3705 ] = uninstall_sw
3706 db_k8scluster_update[
3707 "_admin.helm-chart-v3.operationalState"
3708 ] = "ENABLED"
3709 self.update_db_2(
3710 "k8sclusters", cluster_id, db_k8scluster_update
3711 )
3712 except Exception as e:
3713 self.logger.error(
3714 logging_text
3715 + "error initializing helm-v3 cluster: {}".format(str(e))
3716 )
3717 raise LcmException(
3718 "K8s cluster '{}' has not been initialized for '{}'".format(
3719 cluster_id, cluster_type
3720 )
3721 )
3722 else:
3723 raise LcmException(
3724 "K8s cluster '{}' has not been initialized for '{}'".format(
3725 cluster_id, cluster_type
3726 )
3727 )
3728 k8scluster_id_2_uuic[cluster_type][cluster_id] = k8s_id
3729 return k8s_id
3730
3731 logging_text += "Deploy kdus: "
3732 step = ""
3733 try:
3734 db_nsr_update = {"_admin.deployed.K8s": []}
3735 self.update_db_2("nsrs", nsr_id, db_nsr_update)
3736
3737 index = 0
3738 updated_cluster_list = []
3739 updated_v3_cluster_list = []
3740
3741 for vnfr_data in db_vnfrs.values():
3742 vca_id = self.get_vca_id(vnfr_data, {})
3743 for kdu_index, kdur in enumerate(get_iterable(vnfr_data, "kdur")):
3744 # Step 0: Prepare and set parameters
3745 desc_params = parse_yaml_strings(kdur.get("additionalParams"))
3746 vnfd_id = vnfr_data.get("vnfd-id")
3747 vnfd_with_id = find_in_list(
3748 db_vnfds, lambda vnfd: vnfd["_id"] == vnfd_id
3749 )
3750 kdud = next(
3751 kdud
3752 for kdud in vnfd_with_id["kdu"]
3753 if kdud["name"] == kdur["kdu-name"]
3754 )
3755 namespace = kdur.get("k8s-namespace")
3756 kdu_deployment_name = kdur.get("kdu-deployment-name")
3757 if kdur.get("helm-chart"):
3758 kdumodel = kdur["helm-chart"]
3759 # Default version: helm3, if helm-version is v2 assign v2
3760 k8sclustertype = "helm-chart-v3"
3761 self.logger.debug("kdur: {}".format(kdur))
3762 elif kdur.get("juju-bundle"):
3763 kdumodel = kdur["juju-bundle"]
3764 k8sclustertype = "juju-bundle"
3765 else:
3766 raise LcmException(
3767 "kdu type for kdu='{}.{}' is neither helm-chart nor "
3768 "juju-bundle. Maybe an old NBI version is running".format(
3769 vnfr_data["member-vnf-index-ref"], kdur["kdu-name"]
3770 )
3771 )
3772 # check if kdumodel is a file and exists
3773 try:
3774 vnfd_with_id = find_in_list(
3775 db_vnfds, lambda vnfd: vnfd["_id"] == vnfd_id
3776 )
3777 storage = deep_get(vnfd_with_id, ("_admin", "storage"))
3778 if storage: # may be not present if vnfd has not artifacts
3779 # path format: /vnfdid/pkkdir/helm-charts|juju-bundles/kdumodel
3780 if storage["pkg-dir"]:
3781 filename = "{}/{}/{}s/{}".format(
3782 storage["folder"],
3783 storage["pkg-dir"],
3784 k8sclustertype,
3785 kdumodel,
3786 )
3787 else:
3788 filename = "{}/Scripts/{}s/{}".format(
3789 storage["folder"],
3790 k8sclustertype,
3791 kdumodel,
3792 )
3793 if self.fs.file_exists(
3794 filename, mode="file"
3795 ) or self.fs.file_exists(filename, mode="dir"):
3796 kdumodel = self.fs.path + filename
3797 except (asyncio.TimeoutError, asyncio.CancelledError):
3798 raise
3799 except Exception as e: # it is not a file
3800 self.logger.warning(f"An exception occurred: {str(e)}")
3801
3802 k8s_cluster_id = kdur["k8s-cluster"]["id"]
3803 step = "Synchronize repos for k8s cluster '{}'".format(
3804 k8s_cluster_id
3805 )
3806 cluster_uuid = await _get_cluster_id(k8s_cluster_id, k8sclustertype)
3807
3808 # Synchronize repos
3809 if (
3810 k8sclustertype == "helm-chart"
3811 and cluster_uuid not in updated_cluster_list
3812 ) or (
3813 k8sclustertype == "helm-chart-v3"
3814 and cluster_uuid not in updated_v3_cluster_list
3815 ):
3816 del_repo_list, added_repo_dict = await asyncio.ensure_future(
3817 self.k8scluster_map[k8sclustertype].synchronize_repos(
3818 cluster_uuid=cluster_uuid
3819 )
3820 )
3821 if del_repo_list or added_repo_dict:
3822 if k8sclustertype == "helm-chart":
3823 unset = {
3824 "_admin.helm_charts_added." + item: None
3825 for item in del_repo_list
3826 }
3827 updated = {
3828 "_admin.helm_charts_added." + item: name
3829 for item, name in added_repo_dict.items()
3830 }
3831 updated_cluster_list.append(cluster_uuid)
3832 elif k8sclustertype == "helm-chart-v3":
3833 unset = {
3834 "_admin.helm_charts_v3_added." + item: None
3835 for item in del_repo_list
3836 }
3837 updated = {
3838 "_admin.helm_charts_v3_added." + item: name
3839 for item, name in added_repo_dict.items()
3840 }
3841 updated_v3_cluster_list.append(cluster_uuid)
3842 self.logger.debug(
3843 logging_text + "repos synchronized on k8s cluster "
3844 "'{}' to_delete: {}, to_add: {}".format(
3845 k8s_cluster_id, del_repo_list, added_repo_dict
3846 )
3847 )
3848 self.db.set_one(
3849 "k8sclusters",
3850 {"_id": k8s_cluster_id},
3851 updated,
3852 unset=unset,
3853 )
3854
3855 # Instantiate kdu
3856 step = "Instantiating KDU {}.{} in k8s cluster {}".format(
3857 vnfr_data["member-vnf-index-ref"],
3858 kdur["kdu-name"],
3859 k8s_cluster_id,
3860 )
3861 k8s_instance_info = {
3862 "kdu-instance": None,
3863 "k8scluster-uuid": cluster_uuid,
3864 "k8scluster-type": k8sclustertype,
3865 "member-vnf-index": vnfr_data["member-vnf-index-ref"],
3866 "kdu-name": kdur["kdu-name"],
3867 "kdu-model": kdumodel,
3868 "namespace": namespace,
3869 "kdu-deployment-name": kdu_deployment_name,
3870 }
3871 db_path = "_admin.deployed.K8s.{}".format(index)
3872 db_nsr_update[db_path] = k8s_instance_info
3873 self.update_db_2("nsrs", nsr_id, db_nsr_update)
3874 vnfd_with_id = find_in_list(
3875 db_vnfds, lambda vnf: vnf["_id"] == vnfd_id
3876 )
3877 task = asyncio.ensure_future(
3878 self._install_kdu(
3879 nsr_id,
3880 db_path,
3881 vnfr_data,
3882 kdu_index,
3883 kdud,
3884 vnfd_with_id,
3885 k8s_instance_info,
3886 k8params=desc_params,
3887 timeout=1800,
3888 vca_id=vca_id,
3889 )
3890 )
3891 self.lcm_tasks.register(
3892 "ns",
3893 nsr_id,
3894 nslcmop_id,
3895 "instantiate_KDU-{}".format(index),
3896 task,
3897 )
3898 task_instantiation_info[task] = "Deploying KDU {}".format(
3899 kdur["kdu-name"]
3900 )
3901
3902 index += 1
3903
3904 except (LcmException, asyncio.CancelledError):
3905 raise
3906 except Exception as e:
3907 msg = "Exception {} while {}: {}".format(type(e).__name__, step, e)
3908 if isinstance(e, (N2VCException, DbException)):
3909 self.logger.error(logging_text + msg)
3910 else:
3911 self.logger.critical(logging_text + msg, exc_info=True)
3912 raise LcmException(msg)
3913 finally:
3914 if db_nsr_update:
3915 self.update_db_2("nsrs", nsr_id, db_nsr_update)
3916
3917 def _deploy_n2vc(
3918 self,
3919 logging_text,
3920 db_nsr,
3921 db_vnfr,
3922 nslcmop_id,
3923 nsr_id,
3924 nsi_id,
3925 vnfd_id,
3926 vdu_id,
3927 kdu_name,
3928 member_vnf_index,
3929 vdu_index,
3930 kdu_index,
3931 vdu_name,
3932 deploy_params,
3933 descriptor_config,
3934 base_folder,
3935 task_instantiation_info,
3936 stage,
3937 ):
3938 # launch instantiate_N2VC in a asyncio task and register task object
3939 # Look where information of this charm is at database <nsrs>._admin.deployed.VCA
3940 # if not found, create one entry and update database
3941 # fill db_nsr._admin.deployed.VCA.<index>
3942
3943 self.logger.debug(
3944 logging_text + "_deploy_n2vc vnfd_id={}, vdu_id={}".format(vnfd_id, vdu_id)
3945 )
3946
3947 charm_name = ""
3948 get_charm_name = False
3949 if "execution-environment-list" in descriptor_config:
3950 ee_list = descriptor_config.get("execution-environment-list", [])
3951 elif "juju" in descriptor_config:
3952 ee_list = [descriptor_config] # ns charms
3953 if "execution-environment-list" not in descriptor_config:
3954 # charm name is only required for ns charms
3955 get_charm_name = True
3956 else: # other types as script are not supported
3957 ee_list = []
3958
3959 for ee_item in ee_list:
3960 self.logger.debug(
3961 logging_text
3962 + "_deploy_n2vc ee_item juju={}, helm={}".format(
3963 ee_item.get("juju"), ee_item.get("helm-chart")
3964 )
3965 )
3966 ee_descriptor_id = ee_item.get("id")
3967 if ee_item.get("juju"):
3968 vca_name = ee_item["juju"].get("charm")
3969 if get_charm_name:
3970 charm_name = self.find_charm_name(db_nsr, str(vca_name))
3971 vca_type = (
3972 "lxc_proxy_charm"
3973 if ee_item["juju"].get("charm") is not None
3974 else "native_charm"
3975 )
3976 if ee_item["juju"].get("cloud") == "k8s":
3977 vca_type = "k8s_proxy_charm"
3978 elif ee_item["juju"].get("proxy") is False:
3979 vca_type = "native_charm"
3980 elif ee_item.get("helm-chart"):
3981 vca_name = ee_item["helm-chart"]
3982 vca_type = "helm-v3"
3983 else:
3984 self.logger.debug(
3985 logging_text + "skipping non juju neither charm configuration"
3986 )
3987 continue
3988
3989 vca_index = -1
3990 for vca_index, vca_deployed in enumerate(
3991 db_nsr["_admin"]["deployed"]["VCA"]
3992 ):
3993 if not vca_deployed:
3994 continue
3995 if (
3996 vca_deployed.get("member-vnf-index") == member_vnf_index
3997 and vca_deployed.get("vdu_id") == vdu_id
3998 and vca_deployed.get("kdu_name") == kdu_name
3999 and vca_deployed.get("vdu_count_index", 0) == vdu_index
4000 and vca_deployed.get("ee_descriptor_id") == ee_descriptor_id
4001 ):
4002 break
4003 else:
4004 # not found, create one.
4005 target = (
4006 "ns" if not member_vnf_index else "vnf/{}".format(member_vnf_index)
4007 )
4008 if vdu_id:
4009 target += "/vdu/{}/{}".format(vdu_id, vdu_index or 0)
4010 elif kdu_name:
4011 target += "/kdu/{}".format(kdu_name)
4012 vca_deployed = {
4013 "target_element": target,
4014 # ^ target_element will replace member-vnf-index, kdu_name, vdu_id ... in a single string
4015 "member-vnf-index": member_vnf_index,
4016 "vdu_id": vdu_id,
4017 "kdu_name": kdu_name,
4018 "vdu_count_index": vdu_index,
4019 "operational-status": "init", # TODO revise
4020 "detailed-status": "", # TODO revise
4021 "step": "initial-deploy", # TODO revise
4022 "vnfd_id": vnfd_id,
4023 "vdu_name": vdu_name,
4024 "type": vca_type,
4025 "ee_descriptor_id": ee_descriptor_id,
4026 "charm_name": charm_name,
4027 }
4028 vca_index += 1
4029
4030 # create VCA and configurationStatus in db
4031 db_dict = {
4032 "_admin.deployed.VCA.{}".format(vca_index): vca_deployed,
4033 "configurationStatus.{}".format(vca_index): dict(),
4034 }
4035 self.update_db_2("nsrs", nsr_id, db_dict)
4036
4037 db_nsr["_admin"]["deployed"]["VCA"].append(vca_deployed)
4038
4039 self.logger.debug("N2VC > NSR_ID > {}".format(nsr_id))
4040 self.logger.debug("N2VC > DB_NSR > {}".format(db_nsr))
4041 self.logger.debug("N2VC > VCA_DEPLOYED > {}".format(vca_deployed))
4042
4043 # Launch task
4044 task_n2vc = asyncio.ensure_future(
4045 self.instantiate_N2VC(
4046 logging_text=logging_text,
4047 vca_index=vca_index,
4048 nsi_id=nsi_id,
4049 db_nsr=db_nsr,
4050 db_vnfr=db_vnfr,
4051 vdu_id=vdu_id,
4052 kdu_name=kdu_name,
4053 vdu_index=vdu_index,
4054 kdu_index=kdu_index,
4055 deploy_params=deploy_params,
4056 config_descriptor=descriptor_config,
4057 base_folder=base_folder,
4058 nslcmop_id=nslcmop_id,
4059 stage=stage,
4060 vca_type=vca_type,
4061 vca_name=vca_name,
4062 ee_config_descriptor=ee_item,
4063 )
4064 )
4065 self.lcm_tasks.register(
4066 "ns",
4067 nsr_id,
4068 nslcmop_id,
4069 "instantiate_N2VC-{}".format(vca_index),
4070 task_n2vc,
4071 )
4072 task_instantiation_info[
4073 task_n2vc
4074 ] = self.task_name_deploy_vca + " {}.{}".format(
4075 member_vnf_index or "", vdu_id or ""
4076 )
4077
4078 def _format_additional_params(self, params):
4079 params = params or {}
4080 for key, value in params.items():
4081 if str(value).startswith("!!yaml "):
4082 params[key] = yaml.safe_load(value[7:])
4083 return params
4084
4085 def _get_terminate_primitive_params(self, seq, vnf_index):
4086 primitive = seq.get("name")
4087 primitive_params = {}
4088 params = {
4089 "member_vnf_index": vnf_index,
4090 "primitive": primitive,
4091 "primitive_params": primitive_params,
4092 }
4093 desc_params = {}
4094 return self._map_primitive_params(seq, params, desc_params)
4095
4096 # sub-operations
4097
4098 def _retry_or_skip_suboperation(self, db_nslcmop, op_index):
4099 op = deep_get(db_nslcmop, ("_admin", "operations"), [])[op_index]
4100 if op.get("operationState") == "COMPLETED":
4101 # b. Skip sub-operation
4102 # _ns_execute_primitive() or RO.create_action() will NOT be executed
4103 return self.SUBOPERATION_STATUS_SKIP
4104 else:
4105 # c. retry executing sub-operation
4106 # The sub-operation exists, and operationState != 'COMPLETED'
4107 # Update operationState = 'PROCESSING' to indicate a retry.
4108 operationState = "PROCESSING"
4109 detailed_status = "In progress"
4110 self._update_suboperation_status(
4111 db_nslcmop, op_index, operationState, detailed_status
4112 )
4113 # Return the sub-operation index
4114 # _ns_execute_primitive() or RO.create_action() will be called from scale()
4115 # with arguments extracted from the sub-operation
4116 return op_index
4117
4118 # Find a sub-operation where all keys in a matching dictionary must match
4119 # Returns the index of the matching sub-operation, or SUBOPERATION_STATUS_NOT_FOUND if no match
4120 def _find_suboperation(self, db_nslcmop, match):
4121 if db_nslcmop and match:
4122 op_list = db_nslcmop.get("_admin", {}).get("operations", [])
4123 for i, op in enumerate(op_list):
4124 if all(op.get(k) == match[k] for k in match):
4125 return i
4126 return self.SUBOPERATION_STATUS_NOT_FOUND
4127
4128 # Update status for a sub-operation given its index
4129 def _update_suboperation_status(
4130 self, db_nslcmop, op_index, operationState, detailed_status
4131 ):
4132 # Update DB for HA tasks
4133 q_filter = {"_id": db_nslcmop["_id"]}
4134 update_dict = {
4135 "_admin.operations.{}.operationState".format(op_index): operationState,
4136 "_admin.operations.{}.detailed-status".format(op_index): detailed_status,
4137 }
4138 self.db.set_one(
4139 "nslcmops", q_filter=q_filter, update_dict=update_dict, fail_on_empty=False
4140 )
4141
4142 # Add sub-operation, return the index of the added sub-operation
4143 # Optionally, set operationState, detailed-status, and operationType
4144 # Status and type are currently set for 'scale' sub-operations:
4145 # 'operationState' : 'PROCESSING' | 'COMPLETED' | 'FAILED'
4146 # 'detailed-status' : status message
4147 # 'operationType': may be any type, in the case of scaling: 'PRE-SCALE' | 'POST-SCALE'
4148 # Status and operation type are currently only used for 'scale', but NOT for 'terminate' sub-operations.
4149 def _add_suboperation(
4150 self,
4151 db_nslcmop,
4152 vnf_index,
4153 vdu_id,
4154 vdu_count_index,
4155 vdu_name,
4156 primitive,
4157 mapped_primitive_params,
4158 operationState=None,
4159 detailed_status=None,
4160 operationType=None,
4161 RO_nsr_id=None,
4162 RO_scaling_info=None,
4163 ):
4164 if not db_nslcmop:
4165 return self.SUBOPERATION_STATUS_NOT_FOUND
4166 # Get the "_admin.operations" list, if it exists
4167 db_nslcmop_admin = db_nslcmop.get("_admin", {})
4168 op_list = db_nslcmop_admin.get("operations")
4169 # Create or append to the "_admin.operations" list
4170 new_op = {
4171 "member_vnf_index": vnf_index,
4172 "vdu_id": vdu_id,
4173 "vdu_count_index": vdu_count_index,
4174 "primitive": primitive,
4175 "primitive_params": mapped_primitive_params,
4176 }
4177 if operationState:
4178 new_op["operationState"] = operationState
4179 if detailed_status:
4180 new_op["detailed-status"] = detailed_status
4181 if operationType:
4182 new_op["lcmOperationType"] = operationType
4183 if RO_nsr_id:
4184 new_op["RO_nsr_id"] = RO_nsr_id
4185 if RO_scaling_info:
4186 new_op["RO_scaling_info"] = RO_scaling_info
4187 if not op_list:
4188 # No existing operations, create key 'operations' with current operation as first list element
4189 db_nslcmop_admin.update({"operations": [new_op]})
4190 op_list = db_nslcmop_admin.get("operations")
4191 else:
4192 # Existing operations, append operation to list
4193 op_list.append(new_op)
4194
4195 db_nslcmop_update = {"_admin.operations": op_list}
4196 self.update_db_2("nslcmops", db_nslcmop["_id"], db_nslcmop_update)
4197 op_index = len(op_list) - 1
4198 return op_index
4199
4200 # Helper methods for scale() sub-operations
4201
4202 # pre-scale/post-scale:
4203 # Check for 3 different cases:
4204 # a. New: First time execution, return SUBOPERATION_STATUS_NEW
4205 # b. Skip: Existing sub-operation exists, operationState == 'COMPLETED', return SUBOPERATION_STATUS_SKIP
4206 # c. retry: Existing sub-operation exists, operationState != 'COMPLETED', return op_index to re-execute
4207 def _check_or_add_scale_suboperation(
4208 self,
4209 db_nslcmop,
4210 vnf_index,
4211 vnf_config_primitive,
4212 primitive_params,
4213 operationType,
4214 RO_nsr_id=None,
4215 RO_scaling_info=None,
4216 ):
4217 # Find this sub-operation
4218 if RO_nsr_id and RO_scaling_info:
4219 operationType = "SCALE-RO"
4220 match = {
4221 "member_vnf_index": vnf_index,
4222 "RO_nsr_id": RO_nsr_id,
4223 "RO_scaling_info": RO_scaling_info,
4224 }
4225 else:
4226 match = {
4227 "member_vnf_index": vnf_index,
4228 "primitive": vnf_config_primitive,
4229 "primitive_params": primitive_params,
4230 "lcmOperationType": operationType,
4231 }
4232 op_index = self._find_suboperation(db_nslcmop, match)
4233 if op_index == self.SUBOPERATION_STATUS_NOT_FOUND:
4234 # a. New sub-operation
4235 # The sub-operation does not exist, add it.
4236 # _ns_execute_primitive() will be called from scale() as usual, with non-modified arguments
4237 # The following parameters are set to None for all kind of scaling:
4238 vdu_id = None
4239 vdu_count_index = None
4240 vdu_name = None
4241 if RO_nsr_id and RO_scaling_info:
4242 vnf_config_primitive = None
4243 primitive_params = None
4244 else:
4245 RO_nsr_id = None
4246 RO_scaling_info = None
4247 # Initial status for sub-operation
4248 operationState = "PROCESSING"
4249 detailed_status = "In progress"
4250 # Add sub-operation for pre/post-scaling (zero or more operations)
4251 self._add_suboperation(
4252 db_nslcmop,
4253 vnf_index,
4254 vdu_id,
4255 vdu_count_index,
4256 vdu_name,
4257 vnf_config_primitive,
4258 primitive_params,
4259 operationState,
4260 detailed_status,
4261 operationType,
4262 RO_nsr_id,
4263 RO_scaling_info,
4264 )
4265 return self.SUBOPERATION_STATUS_NEW
4266 else:
4267 # Return either SUBOPERATION_STATUS_SKIP (operationState == 'COMPLETED'),
4268 # or op_index (operationState != 'COMPLETED')
4269 return self._retry_or_skip_suboperation(db_nslcmop, op_index)
4270
4271 # Function to return execution_environment id
4272
4273 async def destroy_N2VC(
4274 self,
4275 logging_text,
4276 db_nslcmop,
4277 vca_deployed,
4278 config_descriptor,
4279 vca_index,
4280 destroy_ee=True,
4281 exec_primitives=True,
4282 scaling_in=False,
4283 vca_id: str = None,
4284 ):
4285 """
4286 Execute the terminate primitives and destroy the execution environment (if destroy_ee=False
4287 :param logging_text:
4288 :param db_nslcmop:
4289 :param vca_deployed: Dictionary of deployment info at db_nsr._admin.depoloyed.VCA.<INDEX>
4290 :param config_descriptor: Configuration descriptor of the NSD, VNFD, VNFD.vdu or VNFD.kdu
4291 :param vca_index: index in the database _admin.deployed.VCA
4292 :param destroy_ee: False to do not destroy, because it will be destroyed all of then at once
4293 :param exec_primitives: False to do not execute terminate primitives, because the config is not completed or has
4294 not executed properly
4295 :param scaling_in: True destroys the application, False destroys the model
4296 :return: None or exception
4297 """
4298
4299 self.logger.debug(
4300 logging_text
4301 + " vca_index: {}, vca_deployed: {}, config_descriptor: {}, destroy_ee: {}".format(
4302 vca_index, vca_deployed, config_descriptor, destroy_ee
4303 )
4304 )
4305
4306 vca_type = vca_deployed.get("type", "lxc_proxy_charm")
4307
4308 # execute terminate_primitives
4309 if exec_primitives:
4310 terminate_primitives = get_ee_sorted_terminate_config_primitive_list(
4311 config_descriptor.get("terminate-config-primitive"),
4312 vca_deployed.get("ee_descriptor_id"),
4313 )
4314 vdu_id = vca_deployed.get("vdu_id")
4315 vdu_count_index = vca_deployed.get("vdu_count_index")
4316 vdu_name = vca_deployed.get("vdu_name")
4317 vnf_index = vca_deployed.get("member-vnf-index")
4318 if terminate_primitives and vca_deployed.get("needed_terminate"):
4319 for seq in terminate_primitives:
4320 # For each sequence in list, get primitive and call _ns_execute_primitive()
4321 step = "Calling terminate action for vnf_member_index={} primitive={}".format(
4322 vnf_index, seq.get("name")
4323 )
4324 self.logger.debug(logging_text + step)
4325 # Create the primitive for each sequence, i.e. "primitive": "touch"
4326 primitive = seq.get("name")
4327 mapped_primitive_params = self._get_terminate_primitive_params(
4328 seq, vnf_index
4329 )
4330
4331 # Add sub-operation
4332 self._add_suboperation(
4333 db_nslcmop,
4334 vnf_index,
4335 vdu_id,
4336 vdu_count_index,
4337 vdu_name,
4338 primitive,
4339 mapped_primitive_params,
4340 )
4341 # Sub-operations: Call _ns_execute_primitive() instead of action()
4342 try:
4343 result, result_detail = await self._ns_execute_primitive(
4344 vca_deployed["ee_id"],
4345 primitive,
4346 mapped_primitive_params,
4347 vca_type=vca_type,
4348 vca_id=vca_id,
4349 )
4350 except LcmException:
4351 # this happens when VCA is not deployed. In this case it is not needed to terminate
4352 continue
4353 result_ok = ["COMPLETED", "PARTIALLY_COMPLETED"]
4354 if result not in result_ok:
4355 raise LcmException(
4356 "terminate_primitive {} for vnf_member_index={} fails with "
4357 "error {}".format(seq.get("name"), vnf_index, result_detail)
4358 )
4359 # set that this VCA do not need terminated
4360 db_update_entry = "_admin.deployed.VCA.{}.needed_terminate".format(
4361 vca_index
4362 )
4363 self.update_db_2(
4364 "nsrs", db_nslcmop["nsInstanceId"], {db_update_entry: False}
4365 )
4366
4367 # Delete Prometheus Jobs if any
4368 # This uses NSR_ID, so it will destroy any jobs under this index
4369 self.db.del_list("prometheus_jobs", {"nsr_id": db_nslcmop["nsInstanceId"]})
4370
4371 if destroy_ee:
4372 await self.vca_map[vca_type].delete_execution_environment(
4373 vca_deployed["ee_id"],
4374 scaling_in=scaling_in,
4375 vca_type=vca_type,
4376 vca_id=vca_id,
4377 )
4378
4379 async def _delete_all_N2VC(self, db_nsr: dict, vca_id: str = None):
4380 self._write_all_config_status(db_nsr=db_nsr, status="TERMINATING")
4381 namespace = "." + db_nsr["_id"]
4382 try:
4383 await self.n2vc.delete_namespace(
4384 namespace=namespace,
4385 total_timeout=self.timeout.charm_delete,
4386 vca_id=vca_id,
4387 )
4388 except N2VCNotFound: # already deleted. Skip
4389 pass
4390 self._write_all_config_status(db_nsr=db_nsr, status="DELETED")
4391
4392 async def terminate(self, nsr_id, nslcmop_id):
4393 # Try to lock HA task here
4394 task_is_locked_by_me = self.lcm_tasks.lock_HA("ns", "nslcmops", nslcmop_id)
4395 if not task_is_locked_by_me:
4396 return
4397
4398 logging_text = "Task ns={} terminate={} ".format(nsr_id, nslcmop_id)
4399 self.logger.debug(logging_text + "Enter")
4400 timeout_ns_terminate = self.timeout.ns_terminate
4401 db_nsr = None
4402 db_nslcmop = None
4403 operation_params = None
4404 exc = None
4405 error_list = [] # annotates all failed error messages
4406 db_nslcmop_update = {}
4407 autoremove = False # autoremove after terminated
4408 tasks_dict_info = {}
4409 db_nsr_update = {}
4410 stage = [
4411 "Stage 1/3: Preparing task.",
4412 "Waiting for previous operations to terminate.",
4413 "",
4414 ]
4415 # ^ contains [stage, step, VIM-status]
4416 try:
4417 # wait for any previous tasks in process
4418 await self.lcm_tasks.waitfor_related_HA("ns", "nslcmops", nslcmop_id)
4419
4420 stage[1] = "Getting nslcmop={} from db.".format(nslcmop_id)
4421 db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
4422 operation_params = db_nslcmop.get("operationParams") or {}
4423 if operation_params.get("timeout_ns_terminate"):
4424 timeout_ns_terminate = operation_params["timeout_ns_terminate"]
4425 stage[1] = "Getting nsr={} from db.".format(nsr_id)
4426 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
4427
4428 db_nsr_update["operational-status"] = "terminating"
4429 db_nsr_update["config-status"] = "terminating"
4430 self._write_ns_status(
4431 nsr_id=nsr_id,
4432 ns_state="TERMINATING",
4433 current_operation="TERMINATING",
4434 current_operation_id=nslcmop_id,
4435 other_update=db_nsr_update,
4436 )
4437 self._write_op_status(op_id=nslcmop_id, queuePosition=0, stage=stage)
4438 nsr_deployed = deepcopy(db_nsr["_admin"].get("deployed")) or {}
4439 if db_nsr["_admin"]["nsState"] == "NOT_INSTANTIATED":
4440 return
4441
4442 stage[1] = "Getting vnf descriptors from db."
4443 db_vnfrs_list = self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id})
4444 db_vnfrs_dict = {
4445 db_vnfr["member-vnf-index-ref"]: db_vnfr for db_vnfr in db_vnfrs_list
4446 }
4447 db_vnfds_from_id = {}
4448 db_vnfds_from_member_index = {}
4449 # Loop over VNFRs
4450 for vnfr in db_vnfrs_list:
4451 vnfd_id = vnfr["vnfd-id"]
4452 if vnfd_id not in db_vnfds_from_id:
4453 vnfd = self.db.get_one("vnfds", {"_id": vnfd_id})
4454 db_vnfds_from_id[vnfd_id] = vnfd
4455 db_vnfds_from_member_index[
4456 vnfr["member-vnf-index-ref"]
4457 ] = db_vnfds_from_id[vnfd_id]
4458
4459 # Destroy individual execution environments when there are terminating primitives.
4460 # Rest of EE will be deleted at once
4461 # TODO - check before calling _destroy_N2VC
4462 # if not operation_params.get("skip_terminate_primitives"):#
4463 # or not vca.get("needed_terminate"):
4464 stage[0] = "Stage 2/3 execute terminating primitives."
4465 self.logger.debug(logging_text + stage[0])
4466 stage[1] = "Looking execution environment that needs terminate."
4467 self.logger.debug(logging_text + stage[1])
4468
4469 for vca_index, vca in enumerate(get_iterable(nsr_deployed, "VCA")):
4470 config_descriptor = None
4471 vca_member_vnf_index = vca.get("member-vnf-index")
4472 vca_id = self.get_vca_id(
4473 db_vnfrs_dict.get(vca_member_vnf_index)
4474 if vca_member_vnf_index
4475 else None,
4476 db_nsr,
4477 )
4478 if not vca or not vca.get("ee_id"):
4479 continue
4480 if not vca.get("member-vnf-index"):
4481 # ns
4482 config_descriptor = db_nsr.get("ns-configuration")
4483 elif vca.get("vdu_id"):
4484 db_vnfd = db_vnfds_from_member_index[vca["member-vnf-index"]]
4485 config_descriptor = get_configuration(db_vnfd, vca.get("vdu_id"))
4486 elif vca.get("kdu_name"):
4487 db_vnfd = db_vnfds_from_member_index[vca["member-vnf-index"]]
4488 config_descriptor = get_configuration(db_vnfd, vca.get("kdu_name"))
4489 else:
4490 db_vnfd = db_vnfds_from_member_index[vca["member-vnf-index"]]
4491 config_descriptor = get_configuration(db_vnfd, db_vnfd["id"])
4492 vca_type = vca.get("type")
4493 exec_terminate_primitives = not operation_params.get(
4494 "skip_terminate_primitives"
4495 ) and vca.get("needed_terminate")
4496 # For helm we must destroy_ee. Also for native_charm, as juju_model cannot be deleted if there are
4497 # pending native charms
4498 destroy_ee = True if vca_type in ("helm-v3", "native_charm") else False
4499 # self.logger.debug(logging_text + "vca_index: {}, ee_id: {}, vca_type: {} destroy_ee: {}".format(
4500 # vca_index, vca.get("ee_id"), vca_type, destroy_ee))
4501 task = asyncio.ensure_future(
4502 self.destroy_N2VC(
4503 logging_text,
4504 db_nslcmop,
4505 vca,
4506 config_descriptor,
4507 vca_index,
4508 destroy_ee,
4509 exec_terminate_primitives,
4510 vca_id=vca_id,
4511 )
4512 )
4513 tasks_dict_info[task] = "Terminating VCA {}".format(vca.get("ee_id"))
4514
4515 # wait for pending tasks of terminate primitives
4516 if tasks_dict_info:
4517 self.logger.debug(
4518 logging_text
4519 + "Waiting for tasks {}".format(list(tasks_dict_info.keys()))
4520 )
4521 error_list = await self._wait_for_tasks(
4522 logging_text,
4523 tasks_dict_info,
4524 min(self.timeout.charm_delete, timeout_ns_terminate),
4525 stage,
4526 nslcmop_id,
4527 )
4528 tasks_dict_info.clear()
4529 if error_list:
4530 return # raise LcmException("; ".join(error_list))
4531
4532 # remove All execution environments at once
4533 stage[0] = "Stage 3/3 delete all."
4534
4535 if nsr_deployed.get("VCA"):
4536 stage[1] = "Deleting all execution environments."
4537 self.logger.debug(logging_text + stage[1])
4538 vca_id = self.get_vca_id({}, db_nsr)
4539 task_delete_ee = asyncio.ensure_future(
4540 asyncio.wait_for(
4541 self._delete_all_N2VC(db_nsr=db_nsr, vca_id=vca_id),
4542 timeout=self.timeout.charm_delete,
4543 )
4544 )
4545 # task_delete_ee = asyncio.ensure_future(self.n2vc.delete_namespace(namespace="." + nsr_id))
4546 tasks_dict_info[task_delete_ee] = "Terminating all VCA"
4547
4548 # Delete Namespace and Certificates if necessary
4549 if check_helm_ee_in_ns(list(db_vnfds_from_member_index.values())):
4550 await self.vca_map["helm-v3"].delete_tls_certificate(
4551 namespace=db_nslcmop["nsInstanceId"],
4552 certificate_name=self.EE_TLS_NAME,
4553 )
4554 await self.vca_map["helm-v3"].delete_namespace(
4555 namespace=db_nslcmop["nsInstanceId"],
4556 )
4557
4558 # Delete from k8scluster
4559 stage[1] = "Deleting KDUs."
4560 self.logger.debug(logging_text + stage[1])
4561 # print(nsr_deployed)
4562 for kdu in get_iterable(nsr_deployed, "K8s"):
4563 if not kdu or not kdu.get("kdu-instance"):
4564 continue
4565 kdu_instance = kdu.get("kdu-instance")
4566 if kdu.get("k8scluster-type") in self.k8scluster_map:
4567 # TODO: Uninstall kdu instances taking into account they could be deployed in different VIMs
4568 vca_id = self.get_vca_id({}, db_nsr)
4569 task_delete_kdu_instance = asyncio.ensure_future(
4570 self.k8scluster_map[kdu["k8scluster-type"]].uninstall(
4571 cluster_uuid=kdu.get("k8scluster-uuid"),
4572 kdu_instance=kdu_instance,
4573 vca_id=vca_id,
4574 namespace=kdu.get("namespace"),
4575 )
4576 )
4577 else:
4578 self.logger.error(
4579 logging_text
4580 + "Unknown k8s deployment type {}".format(
4581 kdu.get("k8scluster-type")
4582 )
4583 )
4584 continue
4585 tasks_dict_info[
4586 task_delete_kdu_instance
4587 ] = "Terminating KDU '{}'".format(kdu.get("kdu-name"))
4588
4589 # remove from RO
4590 stage[1] = "Deleting ns from VIM."
4591 if self.ro_config.ng:
4592 task_delete_ro = asyncio.ensure_future(
4593 self._terminate_ng_ro(
4594 logging_text, nsr_deployed, nsr_id, nslcmop_id, stage
4595 )
4596 )
4597 tasks_dict_info[task_delete_ro] = "Removing deployment from VIM"
4598
4599 # rest of staff will be done at finally
4600
4601 except (
4602 ROclient.ROClientException,
4603 DbException,
4604 LcmException,
4605 N2VCException,
4606 ) as e:
4607 self.logger.error(logging_text + "Exit Exception {}".format(e))
4608 exc = e
4609 except asyncio.CancelledError:
4610 self.logger.error(
4611 logging_text + "Cancelled Exception while '{}'".format(stage[1])
4612 )
4613 exc = "Operation was cancelled"
4614 except Exception as e:
4615 exc = traceback.format_exc()
4616 self.logger.critical(
4617 logging_text + "Exit Exception while '{}': {}".format(stage[1], e),
4618 exc_info=True,
4619 )
4620 finally:
4621 if exc:
4622 error_list.append(str(exc))
4623 try:
4624 # wait for pending tasks
4625 if tasks_dict_info:
4626 stage[1] = "Waiting for terminate pending tasks."
4627 self.logger.debug(logging_text + stage[1])
4628 error_list += await self._wait_for_tasks(
4629 logging_text,
4630 tasks_dict_info,
4631 timeout_ns_terminate,
4632 stage,
4633 nslcmop_id,
4634 )
4635 stage[1] = stage[2] = ""
4636 except asyncio.CancelledError:
4637 error_list.append("Cancelled")
4638 await self._cancel_pending_tasks(logging_text, tasks_dict_info)
4639 await self._wait_for_tasks(
4640 logging_text,
4641 tasks_dict_info,
4642 timeout_ns_terminate,
4643 stage,
4644 nslcmop_id,
4645 )
4646 except Exception as exc:
4647 error_list.append(str(exc))
4648 # update status at database
4649 if error_list:
4650 error_detail = "; ".join(error_list)
4651 # self.logger.error(logging_text + error_detail)
4652 error_description_nslcmop = "{} Detail: {}".format(
4653 stage[0], error_detail
4654 )
4655 error_description_nsr = "Operation: TERMINATING.{}, {}.".format(
4656 nslcmop_id, stage[0]
4657 )
4658
4659 db_nsr_update["operational-status"] = "failed"
4660 db_nsr_update["detailed-status"] = (
4661 error_description_nsr + " Detail: " + error_detail
4662 )
4663 db_nslcmop_update["detailed-status"] = error_detail
4664 nslcmop_operation_state = "FAILED"
4665 ns_state = "BROKEN"
4666 else:
4667 error_detail = None
4668 error_description_nsr = error_description_nslcmop = None
4669 ns_state = "NOT_INSTANTIATED"
4670 db_nsr_update["operational-status"] = "terminated"
4671 db_nsr_update["detailed-status"] = "Done"
4672 db_nsr_update["_admin.nsState"] = "NOT_INSTANTIATED"
4673 db_nslcmop_update["detailed-status"] = "Done"
4674 nslcmop_operation_state = "COMPLETED"
4675
4676 if db_nsr:
4677 self._write_ns_status(
4678 nsr_id=nsr_id,
4679 ns_state=ns_state,
4680 current_operation="IDLE",
4681 current_operation_id=None,
4682 error_description=error_description_nsr,
4683 error_detail=error_detail,
4684 other_update=db_nsr_update,
4685 )
4686 self._write_op_status(
4687 op_id=nslcmop_id,
4688 stage="",
4689 error_message=error_description_nslcmop,
4690 operation_state=nslcmop_operation_state,
4691 other_update=db_nslcmop_update,
4692 )
4693 if ns_state == "NOT_INSTANTIATED":
4694 try:
4695 self.db.set_list(
4696 "vnfrs",
4697 {"nsr-id-ref": nsr_id},
4698 {"_admin.nsState": "NOT_INSTANTIATED"},
4699 )
4700 except DbException as e:
4701 self.logger.warn(
4702 logging_text
4703 + "Error writing VNFR status for nsr-id-ref: {} -> {}".format(
4704 nsr_id, e
4705 )
4706 )
4707 if operation_params:
4708 autoremove = operation_params.get("autoremove", False)
4709 if nslcmop_operation_state:
4710 try:
4711 await self.msg.aiowrite(
4712 "ns",
4713 "terminated",
4714 {
4715 "nsr_id": nsr_id,
4716 "nslcmop_id": nslcmop_id,
4717 "operationState": nslcmop_operation_state,
4718 "autoremove": autoremove,
4719 },
4720 )
4721 except Exception as e:
4722 self.logger.error(
4723 logging_text + "kafka_write notification Exception {}".format(e)
4724 )
4725 self.logger.debug(f"Deleting alerts: ns_id={nsr_id}")
4726 self.db.del_list("alerts", {"tags.ns_id": nsr_id})
4727
4728 self.logger.debug(logging_text + "Exit")
4729 self.lcm_tasks.remove("ns", nsr_id, nslcmop_id, "ns_terminate")
4730
4731 async def _wait_for_tasks(
4732 self, logging_text, created_tasks_info, timeout, stage, nslcmop_id, nsr_id=None
4733 ):
4734 time_start = time()
4735 error_detail_list = []
4736 error_list = []
4737 pending_tasks = list(created_tasks_info.keys())
4738 num_tasks = len(pending_tasks)
4739 num_done = 0
4740 stage[1] = "{}/{}.".format(num_done, num_tasks)
4741 self._write_op_status(nslcmop_id, stage)
4742 while pending_tasks:
4743 new_error = None
4744 _timeout = timeout + time_start - time()
4745 done, pending_tasks = await asyncio.wait(
4746 pending_tasks, timeout=_timeout, return_when=asyncio.FIRST_COMPLETED
4747 )
4748 num_done += len(done)
4749 if not done: # Timeout
4750 for task in pending_tasks:
4751 new_error = created_tasks_info[task] + ": Timeout"
4752 error_detail_list.append(new_error)
4753 error_list.append(new_error)
4754 break
4755 for task in done:
4756 if task.cancelled():
4757 exc = "Cancelled"
4758 else:
4759 exc = task.exception()
4760 if exc:
4761 if isinstance(exc, asyncio.TimeoutError):
4762 exc = "Timeout"
4763 new_error = created_tasks_info[task] + ": {}".format(exc)
4764 error_list.append(created_tasks_info[task])
4765 error_detail_list.append(new_error)
4766 if isinstance(
4767 exc,
4768 (
4769 str,
4770 DbException,
4771 N2VCException,
4772 ROclient.ROClientException,
4773 LcmException,
4774 K8sException,
4775 NgRoException,
4776 ),
4777 ):
4778 self.logger.error(logging_text + new_error)
4779 else:
4780 exc_traceback = "".join(
4781 traceback.format_exception(None, exc, exc.__traceback__)
4782 )
4783 self.logger.error(
4784 logging_text
4785 + created_tasks_info[task]
4786 + " "
4787 + exc_traceback
4788 )
4789 else:
4790 self.logger.debug(
4791 logging_text + created_tasks_info[task] + ": Done"
4792 )
4793 stage[1] = "{}/{}.".format(num_done, num_tasks)
4794 if new_error:
4795 stage[1] += " Errors: " + ". ".join(error_detail_list) + "."
4796 if nsr_id: # update also nsr
4797 self.update_db_2(
4798 "nsrs",
4799 nsr_id,
4800 {
4801 "errorDescription": "Error at: " + ", ".join(error_list),
4802 "errorDetail": ". ".join(error_detail_list),
4803 },
4804 )
4805 self._write_op_status(nslcmop_id, stage)
4806 return error_detail_list
4807
4808 async def _cancel_pending_tasks(self, logging_text, created_tasks_info):
4809 for task, name in created_tasks_info.items():
4810 self.logger.debug(logging_text + "Cancelling task: " + name)
4811 task.cancel()
4812
4813 @staticmethod
4814 def _map_primitive_params(primitive_desc, params, instantiation_params):
4815 """
4816 Generates the params to be provided to charm before executing primitive. If user does not provide a parameter,
4817 The default-value is used. If it is between < > it look for a value at instantiation_params
4818 :param primitive_desc: portion of VNFD/NSD that describes primitive
4819 :param params: Params provided by user
4820 :param instantiation_params: Instantiation params provided by user
4821 :return: a dictionary with the calculated params
4822 """
4823 calculated_params = {}
4824 for parameter in primitive_desc.get("parameter", ()):
4825 param_name = parameter["name"]
4826 if param_name in params:
4827 calculated_params[param_name] = params[param_name]
4828 elif "default-value" in parameter or "value" in parameter:
4829 if "value" in parameter:
4830 calculated_params[param_name] = parameter["value"]
4831 else:
4832 calculated_params[param_name] = parameter["default-value"]
4833 if (
4834 isinstance(calculated_params[param_name], str)
4835 and calculated_params[param_name].startswith("<")
4836 and calculated_params[param_name].endswith(">")
4837 ):
4838 if calculated_params[param_name][1:-1] in instantiation_params:
4839 calculated_params[param_name] = instantiation_params[
4840 calculated_params[param_name][1:-1]
4841 ]
4842 else:
4843 raise LcmException(
4844 "Parameter {} needed to execute primitive {} not provided".format(
4845 calculated_params[param_name], primitive_desc["name"]
4846 )
4847 )
4848 else:
4849 raise LcmException(
4850 "Parameter {} needed to execute primitive {} not provided".format(
4851 param_name, primitive_desc["name"]
4852 )
4853 )
4854
4855 if isinstance(calculated_params[param_name], (dict, list, tuple)):
4856 calculated_params[param_name] = yaml.safe_dump(
4857 calculated_params[param_name], default_flow_style=True, width=256
4858 )
4859 elif isinstance(calculated_params[param_name], str) and calculated_params[
4860 param_name
4861 ].startswith("!!yaml "):
4862 calculated_params[param_name] = calculated_params[param_name][7:]
4863 if parameter.get("data-type") == "INTEGER":
4864 try:
4865 calculated_params[param_name] = int(calculated_params[param_name])
4866 except ValueError: # error converting string to int
4867 raise LcmException(
4868 "Parameter {} of primitive {} must be integer".format(
4869 param_name, primitive_desc["name"]
4870 )
4871 )
4872 elif parameter.get("data-type") == "BOOLEAN":
4873 calculated_params[param_name] = not (
4874 (str(calculated_params[param_name])).lower() == "false"
4875 )
4876
4877 # add always ns_config_info if primitive name is config
4878 if primitive_desc["name"] == "config":
4879 if "ns_config_info" in instantiation_params:
4880 calculated_params["ns_config_info"] = instantiation_params[
4881 "ns_config_info"
4882 ]
4883 return calculated_params
4884
4885 def _look_for_deployed_vca(
4886 self,
4887 deployed_vca,
4888 member_vnf_index,
4889 vdu_id,
4890 vdu_count_index,
4891 kdu_name=None,
4892 ee_descriptor_id=None,
4893 ):
4894 # find vca_deployed record for this action. Raise LcmException if not found or there is not any id.
4895 for vca in deployed_vca:
4896 if not vca:
4897 continue
4898 if member_vnf_index != vca["member-vnf-index"] or vdu_id != vca["vdu_id"]:
4899 continue
4900 if (
4901 vdu_count_index is not None
4902 and vdu_count_index != vca["vdu_count_index"]
4903 ):
4904 continue
4905 if kdu_name and kdu_name != vca["kdu_name"]:
4906 continue
4907 if ee_descriptor_id and ee_descriptor_id != vca["ee_descriptor_id"]:
4908 continue
4909 break
4910 else:
4911 # vca_deployed not found
4912 raise LcmException(
4913 "charm for member_vnf_index={} vdu_id={}.{} kdu_name={} execution-environment-list.id={}"
4914 " is not deployed".format(
4915 member_vnf_index,
4916 vdu_id,
4917 vdu_count_index,
4918 kdu_name,
4919 ee_descriptor_id,
4920 )
4921 )
4922 # get ee_id
4923 ee_id = vca.get("ee_id")
4924 vca_type = vca.get(
4925 "type", "lxc_proxy_charm"
4926 ) # default value for backward compatibility - proxy charm
4927 if not ee_id:
4928 raise LcmException(
4929 "charm for member_vnf_index={} vdu_id={} kdu_name={} vdu_count_index={} has not "
4930 "execution environment".format(
4931 member_vnf_index, vdu_id, kdu_name, vdu_count_index
4932 )
4933 )
4934 return ee_id, vca_type
4935
4936 async def _ns_execute_primitive(
4937 self,
4938 ee_id,
4939 primitive,
4940 primitive_params,
4941 retries=0,
4942 retries_interval=30,
4943 timeout=None,
4944 vca_type=None,
4945 db_dict=None,
4946 vca_id: str = None,
4947 ) -> (str, str):
4948 try:
4949 if primitive == "config":
4950 primitive_params = {"params": primitive_params}
4951
4952 vca_type = vca_type or "lxc_proxy_charm"
4953
4954 while retries >= 0:
4955 try:
4956 output = await asyncio.wait_for(
4957 self.vca_map[vca_type].exec_primitive(
4958 ee_id=ee_id,
4959 primitive_name=primitive,
4960 params_dict=primitive_params,
4961 progress_timeout=self.timeout.progress_primitive,
4962 total_timeout=self.timeout.primitive,
4963 db_dict=db_dict,
4964 vca_id=vca_id,
4965 vca_type=vca_type,
4966 ),
4967 timeout=timeout or self.timeout.primitive,
4968 )
4969 # execution was OK
4970 break
4971 except asyncio.CancelledError:
4972 raise
4973 except Exception as e:
4974 retries -= 1
4975 if retries >= 0:
4976 self.logger.debug(
4977 "Error executing action {} on {} -> {}".format(
4978 primitive, ee_id, e
4979 )
4980 )
4981 # wait and retry
4982 await asyncio.sleep(retries_interval)
4983 else:
4984 if isinstance(e, asyncio.TimeoutError):
4985 e = N2VCException(
4986 message="Timed out waiting for action to complete"
4987 )
4988 return "FAILED", getattr(e, "message", repr(e))
4989
4990 return "COMPLETED", output
4991
4992 except (LcmException, asyncio.CancelledError):
4993 raise
4994 except Exception as e:
4995 return "FAIL", "Error executing action {}: {}".format(primitive, e)
4996
4997 async def vca_status_refresh(self, nsr_id, nslcmop_id):
4998 """
4999 Updating the vca_status with latest juju information in nsrs record
5000 :param: nsr_id: Id of the nsr
5001 :param: nslcmop_id: Id of the nslcmop
5002 :return: None
5003 """
5004
5005 self.logger.debug("Task ns={} action={} Enter".format(nsr_id, nslcmop_id))
5006 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
5007 vca_id = self.get_vca_id({}, db_nsr)
5008 if db_nsr["_admin"]["deployed"]["K8s"]:
5009 for _, k8s in enumerate(db_nsr["_admin"]["deployed"]["K8s"]):
5010 cluster_uuid, kdu_instance, cluster_type = (
5011 k8s["k8scluster-uuid"],
5012 k8s["kdu-instance"],
5013 k8s["k8scluster-type"],
5014 )
5015 await self._on_update_k8s_db(
5016 cluster_uuid=cluster_uuid,
5017 kdu_instance=kdu_instance,
5018 filter={"_id": nsr_id},
5019 vca_id=vca_id,
5020 cluster_type=cluster_type,
5021 )
5022 else:
5023 for vca_index, _ in enumerate(db_nsr["_admin"]["deployed"]["VCA"]):
5024 table, filter = "nsrs", {"_id": nsr_id}
5025 path = "_admin.deployed.VCA.{}.".format(vca_index)
5026 await self._on_update_n2vc_db(table, filter, path, {})
5027
5028 self.logger.debug("Task ns={} action={} Exit".format(nsr_id, nslcmop_id))
5029 self.lcm_tasks.remove("ns", nsr_id, nslcmop_id, "ns_vca_status_refresh")
5030
5031 async def action(self, nsr_id, nslcmop_id):
5032 # Try to lock HA task here
5033 task_is_locked_by_me = self.lcm_tasks.lock_HA("ns", "nslcmops", nslcmop_id)
5034 if not task_is_locked_by_me:
5035 return
5036
5037 logging_text = "Task ns={} action={} ".format(nsr_id, nslcmop_id)
5038 self.logger.debug(logging_text + "Enter")
5039 # get all needed from database
5040 db_nsr = None
5041 db_nslcmop = None
5042 db_nsr_update = {}
5043 db_nslcmop_update = {}
5044 nslcmop_operation_state = None
5045 error_description_nslcmop = None
5046 exc = None
5047 step = ""
5048 try:
5049 # wait for any previous tasks in process
5050 step = "Waiting for previous operations to terminate"
5051 await self.lcm_tasks.waitfor_related_HA("ns", "nslcmops", nslcmop_id)
5052
5053 self._write_ns_status(
5054 nsr_id=nsr_id,
5055 ns_state=None,
5056 current_operation="RUNNING ACTION",
5057 current_operation_id=nslcmop_id,
5058 )
5059
5060 step = "Getting information from database"
5061 db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
5062 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
5063 if db_nslcmop["operationParams"].get("primitive_params"):
5064 db_nslcmop["operationParams"]["primitive_params"] = json.loads(
5065 db_nslcmop["operationParams"]["primitive_params"]
5066 )
5067
5068 nsr_deployed = db_nsr["_admin"].get("deployed")
5069 vnf_index = db_nslcmop["operationParams"].get("member_vnf_index")
5070 vdu_id = db_nslcmop["operationParams"].get("vdu_id")
5071 kdu_name = db_nslcmop["operationParams"].get("kdu_name")
5072 vdu_count_index = db_nslcmop["operationParams"].get("vdu_count_index")
5073 primitive = db_nslcmop["operationParams"]["primitive"]
5074 primitive_params = db_nslcmop["operationParams"]["primitive_params"]
5075 timeout_ns_action = db_nslcmop["operationParams"].get(
5076 "timeout_ns_action", self.timeout.primitive
5077 )
5078
5079 if vnf_index:
5080 step = "Getting vnfr from database"
5081 db_vnfr = self.db.get_one(
5082 "vnfrs", {"member-vnf-index-ref": vnf_index, "nsr-id-ref": nsr_id}
5083 )
5084 if db_vnfr.get("kdur"):
5085 kdur_list = []
5086 for kdur in db_vnfr["kdur"]:
5087 if kdur.get("additionalParams"):
5088 kdur["additionalParams"] = json.loads(
5089 kdur["additionalParams"]
5090 )
5091 kdur_list.append(kdur)
5092 db_vnfr["kdur"] = kdur_list
5093 step = "Getting vnfd from database"
5094 db_vnfd = self.db.get_one("vnfds", {"_id": db_vnfr["vnfd-id"]})
5095
5096 # Sync filesystem before running a primitive
5097 self.fs.sync(db_vnfr["vnfd-id"])
5098 else:
5099 step = "Getting nsd from database"
5100 db_nsd = self.db.get_one("nsds", {"_id": db_nsr["nsd-id"]})
5101
5102 vca_id = self.get_vca_id(db_vnfr, db_nsr)
5103 # for backward compatibility
5104 if nsr_deployed and isinstance(nsr_deployed.get("VCA"), dict):
5105 nsr_deployed["VCA"] = list(nsr_deployed["VCA"].values())
5106 db_nsr_update["_admin.deployed.VCA"] = nsr_deployed["VCA"]
5107 self.update_db_2("nsrs", nsr_id, db_nsr_update)
5108
5109 # look for primitive
5110 config_primitive_desc = descriptor_configuration = None
5111 if vdu_id:
5112 descriptor_configuration = get_configuration(db_vnfd, vdu_id)
5113 elif kdu_name:
5114 descriptor_configuration = get_configuration(db_vnfd, kdu_name)
5115 elif vnf_index:
5116 descriptor_configuration = get_configuration(db_vnfd, db_vnfd["id"])
5117 else:
5118 descriptor_configuration = db_nsd.get("ns-configuration")
5119
5120 if descriptor_configuration and descriptor_configuration.get(
5121 "config-primitive"
5122 ):
5123 for config_primitive in descriptor_configuration["config-primitive"]:
5124 if config_primitive["name"] == primitive:
5125 config_primitive_desc = config_primitive
5126 break
5127
5128 if not config_primitive_desc:
5129 if not (kdu_name and primitive in ("upgrade", "rollback", "status")):
5130 raise LcmException(
5131 "Primitive {} not found at [ns|vnf|vdu]-configuration:config-primitive ".format(
5132 primitive
5133 )
5134 )
5135 primitive_name = primitive
5136 ee_descriptor_id = None
5137 else:
5138 primitive_name = config_primitive_desc.get(
5139 "execution-environment-primitive", primitive
5140 )
5141 ee_descriptor_id = config_primitive_desc.get(
5142 "execution-environment-ref"
5143 )
5144
5145 if vnf_index:
5146 if vdu_id:
5147 vdur = next(
5148 (x for x in db_vnfr["vdur"] if x["vdu-id-ref"] == vdu_id), None
5149 )
5150 desc_params = parse_yaml_strings(vdur.get("additionalParams"))
5151 elif kdu_name:
5152 kdur = next(
5153 (x for x in db_vnfr["kdur"] if x["kdu-name"] == kdu_name), None
5154 )
5155 desc_params = parse_yaml_strings(kdur.get("additionalParams"))
5156 else:
5157 desc_params = parse_yaml_strings(
5158 db_vnfr.get("additionalParamsForVnf")
5159 )
5160 else:
5161 desc_params = parse_yaml_strings(db_nsr.get("additionalParamsForNs"))
5162 if kdu_name and get_configuration(db_vnfd, kdu_name):
5163 kdu_configuration = get_configuration(db_vnfd, kdu_name)
5164 actions = set()
5165 for primitive in kdu_configuration.get("initial-config-primitive", []):
5166 actions.add(primitive["name"])
5167 for primitive in kdu_configuration.get("config-primitive", []):
5168 actions.add(primitive["name"])
5169 kdu = find_in_list(
5170 nsr_deployed["K8s"],
5171 lambda kdu: kdu_name == kdu["kdu-name"]
5172 and kdu["member-vnf-index"] == vnf_index,
5173 )
5174 kdu_action = (
5175 True
5176 if primitive_name in actions
5177 and kdu["k8scluster-type"] != "helm-chart-v3"
5178 else False
5179 )
5180
5181 # TODO check if ns is in a proper status
5182 if kdu_name and (
5183 primitive_name in ("upgrade", "rollback", "status") or kdu_action
5184 ):
5185 # kdur and desc_params already set from before
5186 if primitive_params:
5187 desc_params.update(primitive_params)
5188 # TODO Check if we will need something at vnf level
5189 for index, kdu in enumerate(get_iterable(nsr_deployed, "K8s")):
5190 if (
5191 kdu_name == kdu["kdu-name"]
5192 and kdu["member-vnf-index"] == vnf_index
5193 ):
5194 break
5195 else:
5196 raise LcmException(
5197 "KDU '{}' for vnf '{}' not deployed".format(kdu_name, vnf_index)
5198 )
5199
5200 if kdu.get("k8scluster-type") not in self.k8scluster_map:
5201 msg = "unknown k8scluster-type '{}'".format(
5202 kdu.get("k8scluster-type")
5203 )
5204 raise LcmException(msg)
5205
5206 db_dict = {
5207 "collection": "nsrs",
5208 "filter": {"_id": nsr_id},
5209 "path": "_admin.deployed.K8s.{}".format(index),
5210 }
5211 self.logger.debug(
5212 logging_text
5213 + "Exec k8s {} on {}.{}".format(primitive_name, vnf_index, kdu_name)
5214 )
5215 step = "Executing kdu {}".format(primitive_name)
5216 if primitive_name == "upgrade":
5217 if desc_params.get("kdu_model"):
5218 kdu_model = desc_params.get("kdu_model")
5219 del desc_params["kdu_model"]
5220 else:
5221 kdu_model = kdu.get("kdu-model")
5222 if kdu_model.count("/") < 2: # helm chart is not embedded
5223 parts = kdu_model.split(sep=":")
5224 if len(parts) == 2:
5225 kdu_model = parts[0]
5226 if desc_params.get("kdu_atomic_upgrade"):
5227 atomic_upgrade = desc_params.get(
5228 "kdu_atomic_upgrade"
5229 ).lower() in ("yes", "true", "1")
5230 del desc_params["kdu_atomic_upgrade"]
5231 else:
5232 atomic_upgrade = True
5233
5234 detailed_status = await asyncio.wait_for(
5235 self.k8scluster_map[kdu["k8scluster-type"]].upgrade(
5236 cluster_uuid=kdu.get("k8scluster-uuid"),
5237 kdu_instance=kdu.get("kdu-instance"),
5238 atomic=atomic_upgrade,
5239 kdu_model=kdu_model,
5240 params=desc_params,
5241 db_dict=db_dict,
5242 timeout=timeout_ns_action,
5243 ),
5244 timeout=timeout_ns_action + 10,
5245 )
5246 self.logger.debug(
5247 logging_text + " Upgrade of kdu {} done".format(detailed_status)
5248 )
5249 elif primitive_name == "rollback":
5250 detailed_status = await asyncio.wait_for(
5251 self.k8scluster_map[kdu["k8scluster-type"]].rollback(
5252 cluster_uuid=kdu.get("k8scluster-uuid"),
5253 kdu_instance=kdu.get("kdu-instance"),
5254 db_dict=db_dict,
5255 ),
5256 timeout=timeout_ns_action,
5257 )
5258 elif primitive_name == "status":
5259 detailed_status = await asyncio.wait_for(
5260 self.k8scluster_map[kdu["k8scluster-type"]].status_kdu(
5261 cluster_uuid=kdu.get("k8scluster-uuid"),
5262 kdu_instance=kdu.get("kdu-instance"),
5263 vca_id=vca_id,
5264 ),
5265 timeout=timeout_ns_action,
5266 )
5267 else:
5268 kdu_instance = kdu.get("kdu-instance") or "{}-{}".format(
5269 kdu["kdu-name"], nsr_id
5270 )
5271 params = self._map_primitive_params(
5272 config_primitive_desc, primitive_params, desc_params
5273 )
5274
5275 detailed_status = await asyncio.wait_for(
5276 self.k8scluster_map[kdu["k8scluster-type"]].exec_primitive(
5277 cluster_uuid=kdu.get("k8scluster-uuid"),
5278 kdu_instance=kdu_instance,
5279 primitive_name=primitive_name,
5280 params=params,
5281 db_dict=db_dict,
5282 timeout=timeout_ns_action,
5283 vca_id=vca_id,
5284 ),
5285 timeout=timeout_ns_action,
5286 )
5287
5288 if detailed_status:
5289 nslcmop_operation_state = "COMPLETED"
5290 else:
5291 detailed_status = ""
5292 nslcmop_operation_state = "FAILED"
5293 else:
5294 ee_id, vca_type = self._look_for_deployed_vca(
5295 nsr_deployed["VCA"],
5296 member_vnf_index=vnf_index,
5297 vdu_id=vdu_id,
5298 vdu_count_index=vdu_count_index,
5299 ee_descriptor_id=ee_descriptor_id,
5300 )
5301 for vca_index, vca_deployed in enumerate(
5302 db_nsr["_admin"]["deployed"]["VCA"]
5303 ):
5304 if vca_deployed.get("member-vnf-index") == vnf_index:
5305 db_dict = {
5306 "collection": "nsrs",
5307 "filter": {"_id": nsr_id},
5308 "path": "_admin.deployed.VCA.{}.".format(vca_index),
5309 }
5310 break
5311 (
5312 nslcmop_operation_state,
5313 detailed_status,
5314 ) = await self._ns_execute_primitive(
5315 ee_id,
5316 primitive=primitive_name,
5317 primitive_params=self._map_primitive_params(
5318 config_primitive_desc, primitive_params, desc_params
5319 ),
5320 timeout=timeout_ns_action,
5321 vca_type=vca_type,
5322 db_dict=db_dict,
5323 vca_id=vca_id,
5324 )
5325
5326 db_nslcmop_update["detailed-status"] = detailed_status
5327 error_description_nslcmop = (
5328 detailed_status if nslcmop_operation_state == "FAILED" else ""
5329 )
5330 self.logger.debug(
5331 logging_text
5332 + "Done with result {} {}".format(
5333 nslcmop_operation_state, detailed_status
5334 )
5335 )
5336 return # database update is called inside finally
5337
5338 except (DbException, LcmException, N2VCException, K8sException) as e:
5339 self.logger.error(logging_text + "Exit Exception {}".format(e))
5340 exc = e
5341 except asyncio.CancelledError:
5342 self.logger.error(
5343 logging_text + "Cancelled Exception while '{}'".format(step)
5344 )
5345 exc = "Operation was cancelled"
5346 except asyncio.TimeoutError:
5347 self.logger.error(logging_text + "Timeout while '{}'".format(step))
5348 exc = "Timeout"
5349 except Exception as e:
5350 exc = traceback.format_exc()
5351 self.logger.critical(
5352 logging_text + "Exit Exception {} {}".format(type(e).__name__, e),
5353 exc_info=True,
5354 )
5355 finally:
5356 if exc:
5357 db_nslcmop_update[
5358 "detailed-status"
5359 ] = (
5360 detailed_status
5361 ) = error_description_nslcmop = "FAILED {}: {}".format(step, exc)
5362 nslcmop_operation_state = "FAILED"
5363 if db_nsr:
5364 self._write_ns_status(
5365 nsr_id=nsr_id,
5366 ns_state=db_nsr[
5367 "nsState"
5368 ], # TODO check if degraded. For the moment use previous status
5369 current_operation="IDLE",
5370 current_operation_id=None,
5371 # error_description=error_description_nsr,
5372 # error_detail=error_detail,
5373 other_update=db_nsr_update,
5374 )
5375
5376 self._write_op_status(
5377 op_id=nslcmop_id,
5378 stage="",
5379 error_message=error_description_nslcmop,
5380 operation_state=nslcmop_operation_state,
5381 other_update=db_nslcmop_update,
5382 )
5383
5384 if nslcmop_operation_state:
5385 try:
5386 await self.msg.aiowrite(
5387 "ns",
5388 "actioned",
5389 {
5390 "nsr_id": nsr_id,
5391 "nslcmop_id": nslcmop_id,
5392 "operationState": nslcmop_operation_state,
5393 },
5394 )
5395 except Exception as e:
5396 self.logger.error(
5397 logging_text + "kafka_write notification Exception {}".format(e)
5398 )
5399 self.logger.debug(logging_text + "Exit")
5400 self.lcm_tasks.remove("ns", nsr_id, nslcmop_id, "ns_action")
5401 return nslcmop_operation_state, detailed_status
5402
5403 async def terminate_vdus(
5404 self, db_vnfr, member_vnf_index, db_nsr, update_db_nslcmops, stage, logging_text
5405 ):
5406 """This method terminates VDUs
5407
5408 Args:
5409 db_vnfr: VNF instance record
5410 member_vnf_index: VNF index to identify the VDUs to be removed
5411 db_nsr: NS instance record
5412 update_db_nslcmops: Nslcmop update record
5413 """
5414 vca_scaling_info = []
5415 scaling_info = {"scaling_group_name": "vdu_autoscale", "vdu": [], "kdu": []}
5416 scaling_info["scaling_direction"] = "IN"
5417 scaling_info["vdu-delete"] = {}
5418 scaling_info["kdu-delete"] = {}
5419 db_vdur = db_vnfr.get("vdur")
5420 vdur_list = copy(db_vdur)
5421 count_index = 0
5422 for index, vdu in enumerate(vdur_list):
5423 vca_scaling_info.append(
5424 {
5425 "osm_vdu_id": vdu["vdu-id-ref"],
5426 "member-vnf-index": member_vnf_index,
5427 "type": "delete",
5428 "vdu_index": count_index,
5429 }
5430 )
5431 scaling_info["vdu-delete"][vdu["vdu-id-ref"]] = count_index
5432 scaling_info["vdu"].append(
5433 {
5434 "name": vdu.get("name") or vdu.get("vdu-name"),
5435 "vdu_id": vdu["vdu-id-ref"],
5436 "interface": [],
5437 }
5438 )
5439 for interface in vdu["interfaces"]:
5440 scaling_info["vdu"][index]["interface"].append(
5441 {
5442 "name": interface["name"],
5443 "ip_address": interface["ip-address"],
5444 "mac_address": interface.get("mac-address"),
5445 }
5446 )
5447 self.logger.info("NS update scaling info{}".format(scaling_info))
5448 stage[2] = "Terminating VDUs"
5449 if scaling_info.get("vdu-delete"):
5450 # scale_process = "RO"
5451 if self.ro_config.ng:
5452 await self._scale_ng_ro(
5453 logging_text,
5454 db_nsr,
5455 update_db_nslcmops,
5456 db_vnfr,
5457 scaling_info,
5458 stage,
5459 )
5460
5461 async def remove_vnf(self, nsr_id, nslcmop_id, vnf_instance_id):
5462 """This method is to Remove VNF instances from NS.
5463
5464 Args:
5465 nsr_id: NS instance id
5466 nslcmop_id: nslcmop id of update
5467 vnf_instance_id: id of the VNF instance to be removed
5468
5469 Returns:
5470 result: (str, str) COMPLETED/FAILED, details
5471 """
5472 try:
5473 db_nsr_update = {}
5474 logging_text = "Task ns={} update ".format(nsr_id)
5475 check_vnfr_count = len(self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id}))
5476 self.logger.info("check_vnfr_count {}".format(check_vnfr_count))
5477 if check_vnfr_count > 1:
5478 stage = ["", "", ""]
5479 step = "Getting nslcmop from database"
5480 self.logger.debug(
5481 step + " after having waited for previous tasks to be completed"
5482 )
5483 # db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
5484 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
5485 db_vnfr = self.db.get_one("vnfrs", {"_id": vnf_instance_id})
5486 member_vnf_index = db_vnfr["member-vnf-index-ref"]
5487 """ db_vnfr = self.db.get_one(
5488 "vnfrs", {"member-vnf-index-ref": member_vnf_index, "nsr-id-ref": nsr_id}) """
5489
5490 update_db_nslcmops = self.db.get_one("nslcmops", {"_id": nslcmop_id})
5491 await self.terminate_vdus(
5492 db_vnfr,
5493 member_vnf_index,
5494 db_nsr,
5495 update_db_nslcmops,
5496 stage,
5497 logging_text,
5498 )
5499
5500 constituent_vnfr = db_nsr.get("constituent-vnfr-ref")
5501 constituent_vnfr.remove(db_vnfr.get("_id"))
5502 db_nsr_update["constituent-vnfr-ref"] = db_nsr.get(
5503 "constituent-vnfr-ref"
5504 )
5505 self.update_db_2("nsrs", nsr_id, db_nsr_update)
5506 self.db.del_one("vnfrs", {"_id": db_vnfr.get("_id")})
5507 self.update_db_2("nsrs", nsr_id, db_nsr_update)
5508 return "COMPLETED", "Done"
5509 else:
5510 step = "Terminate VNF Failed with"
5511 raise LcmException(
5512 "{} Cannot terminate the last VNF in this NS.".format(
5513 vnf_instance_id
5514 )
5515 )
5516 except (LcmException, asyncio.CancelledError):
5517 raise
5518 except Exception as e:
5519 self.logger.debug("Error removing VNF {}".format(e))
5520 return "FAILED", "Error removing VNF {}".format(e)
5521
5522 async def _ns_redeploy_vnf(
5523 self,
5524 nsr_id,
5525 nslcmop_id,
5526 db_vnfd,
5527 db_vnfr,
5528 db_nsr,
5529 ):
5530 """This method updates and redeploys VNF instances
5531
5532 Args:
5533 nsr_id: NS instance id
5534 nslcmop_id: nslcmop id
5535 db_vnfd: VNF descriptor
5536 db_vnfr: VNF instance record
5537 db_nsr: NS instance record
5538
5539 Returns:
5540 result: (str, str) COMPLETED/FAILED, details
5541 """
5542 try:
5543 count_index = 0
5544 stage = ["", "", ""]
5545 logging_text = "Task ns={} update ".format(nsr_id)
5546 latest_vnfd_revision = db_vnfd["_admin"].get("revision")
5547 member_vnf_index = db_vnfr["member-vnf-index-ref"]
5548
5549 # Terminate old VNF resources
5550 update_db_nslcmops = self.db.get_one("nslcmops", {"_id": nslcmop_id})
5551 await self.terminate_vdus(
5552 db_vnfr,
5553 member_vnf_index,
5554 db_nsr,
5555 update_db_nslcmops,
5556 stage,
5557 logging_text,
5558 )
5559
5560 # old_vnfd_id = db_vnfr["vnfd-id"]
5561 # new_db_vnfd = self.db.get_one("vnfds", {"_id": vnfd_id})
5562 new_db_vnfd = db_vnfd
5563 # new_vnfd_ref = new_db_vnfd["id"]
5564 # new_vnfd_id = vnfd_id
5565
5566 # Create VDUR
5567 new_vnfr_cp = []
5568 for cp in new_db_vnfd.get("ext-cpd", ()):
5569 vnf_cp = {
5570 "name": cp.get("id"),
5571 "connection-point-id": cp.get("int-cpd", {}).get("cpd"),
5572 "connection-point-vdu-id": cp.get("int-cpd", {}).get("vdu-id"),
5573 "id": cp.get("id"),
5574 }
5575 new_vnfr_cp.append(vnf_cp)
5576 new_vdur = update_db_nslcmops["operationParams"]["newVdur"]
5577 # new_vdur = self._create_vdur_descriptor_from_vnfd(db_nsd, db_vnfd, old_db_vnfd, vnfd_id, db_nsr, member_vnf_index)
5578 # new_vnfr_update = {"vnfd-ref": new_vnfd_ref, "vnfd-id": new_vnfd_id, "connection-point": new_vnfr_cp, "vdur": new_vdur, "ip-address": ""}
5579 new_vnfr_update = {
5580 "revision": latest_vnfd_revision,
5581 "connection-point": new_vnfr_cp,
5582 "vdur": new_vdur,
5583 "ip-address": "",
5584 }
5585 self.update_db_2("vnfrs", db_vnfr["_id"], new_vnfr_update)
5586 updated_db_vnfr = self.db.get_one(
5587 "vnfrs",
5588 {"member-vnf-index-ref": member_vnf_index, "nsr-id-ref": nsr_id},
5589 )
5590
5591 # Instantiate new VNF resources
5592 # update_db_nslcmops = self.db.get_one("nslcmops", {"_id": nslcmop_id})
5593 vca_scaling_info = []
5594 scaling_info = {"scaling_group_name": "vdu_autoscale", "vdu": [], "kdu": []}
5595 scaling_info["scaling_direction"] = "OUT"
5596 scaling_info["vdu-create"] = {}
5597 scaling_info["kdu-create"] = {}
5598 vdud_instantiate_list = db_vnfd["vdu"]
5599 for index, vdud in enumerate(vdud_instantiate_list):
5600 cloud_init_text = self._get_vdu_cloud_init_content(vdud, db_vnfd)
5601 if cloud_init_text:
5602 additional_params = (
5603 self._get_vdu_additional_params(updated_db_vnfr, vdud["id"])
5604 or {}
5605 )
5606 cloud_init_list = []
5607 if cloud_init_text:
5608 # TODO Information of its own ip is not available because db_vnfr is not updated.
5609 additional_params["OSM"] = get_osm_params(
5610 updated_db_vnfr, vdud["id"], 1
5611 )
5612 cloud_init_list.append(
5613 self._parse_cloud_init(
5614 cloud_init_text,
5615 additional_params,
5616 db_vnfd["id"],
5617 vdud["id"],
5618 )
5619 )
5620 vca_scaling_info.append(
5621 {
5622 "osm_vdu_id": vdud["id"],
5623 "member-vnf-index": member_vnf_index,
5624 "type": "create",
5625 "vdu_index": count_index,
5626 }
5627 )
5628 scaling_info["vdu-create"][vdud["id"]] = count_index
5629 if self.ro_config.ng:
5630 self.logger.debug(
5631 "New Resources to be deployed: {}".format(scaling_info)
5632 )
5633 await self._scale_ng_ro(
5634 logging_text,
5635 db_nsr,
5636 update_db_nslcmops,
5637 updated_db_vnfr,
5638 scaling_info,
5639 stage,
5640 )
5641 return "COMPLETED", "Done"
5642 except (LcmException, asyncio.CancelledError):
5643 raise
5644 except Exception as e:
5645 self.logger.debug("Error updating VNF {}".format(e))
5646 return "FAILED", "Error updating VNF {}".format(e)
5647
5648 async def _ns_charm_upgrade(
5649 self,
5650 ee_id,
5651 charm_id,
5652 charm_type,
5653 path,
5654 timeout: float = None,
5655 ) -> (str, str):
5656 """This method upgrade charms in VNF instances
5657
5658 Args:
5659 ee_id: Execution environment id
5660 path: Local path to the charm
5661 charm_id: charm-id
5662 charm_type: Charm type can be lxc-proxy-charm, native-charm or k8s-proxy-charm
5663 timeout: (Float) Timeout for the ns update operation
5664
5665 Returns:
5666 result: (str, str) COMPLETED/FAILED, details
5667 """
5668 try:
5669 charm_type = charm_type or "lxc_proxy_charm"
5670 output = await self.vca_map[charm_type].upgrade_charm(
5671 ee_id=ee_id,
5672 path=path,
5673 charm_id=charm_id,
5674 charm_type=charm_type,
5675 timeout=timeout or self.timeout.ns_update,
5676 )
5677
5678 if output:
5679 return "COMPLETED", output
5680
5681 except (LcmException, asyncio.CancelledError):
5682 raise
5683
5684 except Exception as e:
5685 self.logger.debug("Error upgrading charm {}".format(path))
5686
5687 return "FAILED", "Error upgrading charm {}: {}".format(path, e)
5688
5689 async def update(self, nsr_id, nslcmop_id):
5690 """Update NS according to different update types
5691
5692 This method performs upgrade of VNF instances then updates the revision
5693 number in VNF record
5694
5695 Args:
5696 nsr_id: Network service will be updated
5697 nslcmop_id: ns lcm operation id
5698
5699 Returns:
5700 It may raise DbException, LcmException, N2VCException, K8sException
5701
5702 """
5703 # Try to lock HA task here
5704 task_is_locked_by_me = self.lcm_tasks.lock_HA("ns", "nslcmops", nslcmop_id)
5705 if not task_is_locked_by_me:
5706 return
5707
5708 logging_text = "Task ns={} update={} ".format(nsr_id, nslcmop_id)
5709 self.logger.debug(logging_text + "Enter")
5710
5711 # Set the required variables to be filled up later
5712 db_nsr = None
5713 db_nslcmop_update = {}
5714 vnfr_update = {}
5715 nslcmop_operation_state = None
5716 db_nsr_update = {}
5717 error_description_nslcmop = ""
5718 exc = None
5719 change_type = "updated"
5720 detailed_status = ""
5721 member_vnf_index = None
5722
5723 try:
5724 # wait for any previous tasks in process
5725 step = "Waiting for previous operations to terminate"
5726 await self.lcm_tasks.waitfor_related_HA("ns", "nslcmops", nslcmop_id)
5727 self._write_ns_status(
5728 nsr_id=nsr_id,
5729 ns_state=None,
5730 current_operation="UPDATING",
5731 current_operation_id=nslcmop_id,
5732 )
5733
5734 step = "Getting nslcmop from database"
5735 db_nslcmop = self.db.get_one(
5736 "nslcmops", {"_id": nslcmop_id}, fail_on_empty=False
5737 )
5738 update_type = db_nslcmop["operationParams"]["updateType"]
5739
5740 step = "Getting nsr from database"
5741 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
5742 old_operational_status = db_nsr["operational-status"]
5743 db_nsr_update["operational-status"] = "updating"
5744 self.update_db_2("nsrs", nsr_id, db_nsr_update)
5745 nsr_deployed = db_nsr["_admin"].get("deployed")
5746
5747 if update_type == "CHANGE_VNFPKG":
5748 # Get the input parameters given through update request
5749 vnf_instance_id = db_nslcmop["operationParams"][
5750 "changeVnfPackageData"
5751 ].get("vnfInstanceId")
5752
5753 vnfd_id = db_nslcmop["operationParams"]["changeVnfPackageData"].get(
5754 "vnfdId"
5755 )
5756 timeout_seconds = db_nslcmop["operationParams"].get("timeout_ns_update")
5757
5758 step = "Getting vnfr from database"
5759 db_vnfr = self.db.get_one(
5760 "vnfrs", {"_id": vnf_instance_id}, fail_on_empty=False
5761 )
5762
5763 step = "Getting vnfds from database"
5764 # Latest VNFD
5765 latest_vnfd = self.db.get_one(
5766 "vnfds", {"_id": vnfd_id}, fail_on_empty=False
5767 )
5768 latest_vnfd_revision = latest_vnfd["_admin"].get("revision")
5769
5770 # Current VNFD
5771 current_vnf_revision = db_vnfr.get("revision", 1)
5772 current_vnfd = self.db.get_one(
5773 "vnfds_revisions",
5774 {"_id": vnfd_id + ":" + str(current_vnf_revision)},
5775 fail_on_empty=False,
5776 )
5777 # Charm artifact paths will be filled up later
5778 (
5779 current_charm_artifact_path,
5780 target_charm_artifact_path,
5781 charm_artifact_paths,
5782 helm_artifacts,
5783 ) = ([], [], [], [])
5784
5785 step = "Checking if revision has changed in VNFD"
5786 if current_vnf_revision != latest_vnfd_revision:
5787 change_type = "policy_updated"
5788
5789 # There is new revision of VNFD, update operation is required
5790 current_vnfd_path = vnfd_id + ":" + str(current_vnf_revision)
5791 latest_vnfd_path = vnfd_id + ":" + str(latest_vnfd_revision)
5792
5793 step = "Removing the VNFD packages if they exist in the local path"
5794 shutil.rmtree(self.fs.path + current_vnfd_path, ignore_errors=True)
5795 shutil.rmtree(self.fs.path + latest_vnfd_path, ignore_errors=True)
5796
5797 step = "Get the VNFD packages from FSMongo"
5798 self.fs.sync(from_path=latest_vnfd_path)
5799 self.fs.sync(from_path=current_vnfd_path)
5800
5801 step = (
5802 "Get the charm-type, charm-id, ee-id if there is deployed VCA"
5803 )
5804 current_base_folder = current_vnfd["_admin"]["storage"]
5805 latest_base_folder = latest_vnfd["_admin"]["storage"]
5806
5807 for vca_index, vca_deployed in enumerate(
5808 get_iterable(nsr_deployed, "VCA")
5809 ):
5810 vnf_index = db_vnfr.get("member-vnf-index-ref")
5811
5812 # Getting charm-id and charm-type
5813 if vca_deployed.get("member-vnf-index") == vnf_index:
5814 vca_id = self.get_vca_id(db_vnfr, db_nsr)
5815 vca_type = vca_deployed.get("type")
5816 vdu_count_index = vca_deployed.get("vdu_count_index")
5817
5818 # Getting ee-id
5819 ee_id = vca_deployed.get("ee_id")
5820
5821 step = "Getting descriptor config"
5822 if current_vnfd.get("kdu"):
5823 search_key = "kdu_name"
5824 else:
5825 search_key = "vnfd_id"
5826
5827 entity_id = vca_deployed.get(search_key)
5828
5829 descriptor_config = get_configuration(
5830 current_vnfd, entity_id
5831 )
5832
5833 if "execution-environment-list" in descriptor_config:
5834 ee_list = descriptor_config.get(
5835 "execution-environment-list", []
5836 )
5837 else:
5838 ee_list = []
5839
5840 # There could be several charm used in the same VNF
5841 for ee_item in ee_list:
5842 if ee_item.get("juju"):
5843 step = "Getting charm name"
5844 charm_name = ee_item["juju"].get("charm")
5845
5846 step = "Setting Charm artifact paths"
5847 current_charm_artifact_path.append(
5848 get_charm_artifact_path(
5849 current_base_folder,
5850 charm_name,
5851 vca_type,
5852 current_vnf_revision,
5853 )
5854 )
5855 target_charm_artifact_path.append(
5856 get_charm_artifact_path(
5857 latest_base_folder,
5858 charm_name,
5859 vca_type,
5860 latest_vnfd_revision,
5861 )
5862 )
5863 elif ee_item.get("helm-chart"):
5864 # add chart to list and all parameters
5865 step = "Getting helm chart name"
5866 chart_name = ee_item.get("helm-chart")
5867 vca_type = "helm-v3"
5868 step = "Setting Helm chart artifact paths"
5869
5870 helm_artifacts.append(
5871 {
5872 "current_artifact_path": get_charm_artifact_path(
5873 current_base_folder,
5874 chart_name,
5875 vca_type,
5876 current_vnf_revision,
5877 ),
5878 "target_artifact_path": get_charm_artifact_path(
5879 latest_base_folder,
5880 chart_name,
5881 vca_type,
5882 latest_vnfd_revision,
5883 ),
5884 "ee_id": ee_id,
5885 "vca_index": vca_index,
5886 "vdu_index": vdu_count_index,
5887 }
5888 )
5889
5890 charm_artifact_paths = zip(
5891 current_charm_artifact_path, target_charm_artifact_path
5892 )
5893
5894 step = "Checking if software version has changed in VNFD"
5895 if find_software_version(current_vnfd) != find_software_version(
5896 latest_vnfd
5897 ):
5898 step = "Checking if existing VNF has charm"
5899 for current_charm_path, target_charm_path in list(
5900 charm_artifact_paths
5901 ):
5902 if current_charm_path:
5903 raise LcmException(
5904 "Software version change is not supported as VNF instance {} has charm.".format(
5905 vnf_instance_id
5906 )
5907 )
5908
5909 step = "Checking whether the descriptor has SFC"
5910 if db_nsr.get("nsd", {}).get("vnffgd"):
5911 raise LcmException(
5912 "Ns update is not allowed for NS with SFC"
5913 )
5914
5915 # There is no change in the charm package, then redeploy the VNF
5916 # based on new descriptor
5917 step = "Redeploying VNF"
5918 member_vnf_index = db_vnfr["member-vnf-index-ref"]
5919 (result, detailed_status) = await self._ns_redeploy_vnf(
5920 nsr_id, nslcmop_id, latest_vnfd, db_vnfr, db_nsr
5921 )
5922 if result == "FAILED":
5923 nslcmop_operation_state = result
5924 error_description_nslcmop = detailed_status
5925 old_operational_status = "failed"
5926 db_nslcmop_update["detailed-status"] = detailed_status
5927 db_nsr_update["detailed-status"] = detailed_status
5928 scaling_aspect = get_scaling_aspect(latest_vnfd)
5929 scaling_group_desc = db_nsr.get("_admin").get(
5930 "scaling-group", None
5931 )
5932 if scaling_group_desc:
5933 for aspect in scaling_aspect:
5934 scaling_group_id = aspect.get("id")
5935 for scale_index, scaling_group in enumerate(
5936 scaling_group_desc
5937 ):
5938 if scaling_group.get("name") == scaling_group_id:
5939 db_nsr_update[
5940 "_admin.scaling-group.{}.nb-scale-op".format(
5941 scale_index
5942 )
5943 ] = 0
5944 self.logger.debug(
5945 logging_text
5946 + " step {} Done with result {} {}".format(
5947 step, nslcmop_operation_state, detailed_status
5948 )
5949 )
5950
5951 else:
5952 step = "Checking if any charm package has changed or not"
5953 for current_charm_path, target_charm_path in list(
5954 charm_artifact_paths
5955 ):
5956 if (
5957 current_charm_path
5958 and target_charm_path
5959 and self.check_charm_hash_changed(
5960 current_charm_path, target_charm_path
5961 )
5962 ):
5963 step = "Checking whether VNF uses juju bundle"
5964 if check_juju_bundle_existence(current_vnfd):
5965 raise LcmException(
5966 "Charm upgrade is not supported for the instance which"
5967 " uses juju-bundle: {}".format(
5968 check_juju_bundle_existence(current_vnfd)
5969 )
5970 )
5971
5972 step = "Upgrading Charm"
5973 (
5974 result,
5975 detailed_status,
5976 ) = await self._ns_charm_upgrade(
5977 ee_id=ee_id,
5978 charm_id=vca_id,
5979 charm_type=vca_type,
5980 path=self.fs.path + target_charm_path,
5981 timeout=timeout_seconds,
5982 )
5983
5984 if result == "FAILED":
5985 nslcmop_operation_state = result
5986 error_description_nslcmop = detailed_status
5987
5988 db_nslcmop_update["detailed-status"] = detailed_status
5989 self.logger.debug(
5990 logging_text
5991 + " step {} Done with result {} {}".format(
5992 step, nslcmop_operation_state, detailed_status
5993 )
5994 )
5995
5996 step = "Updating policies"
5997 member_vnf_index = db_vnfr["member-vnf-index-ref"]
5998 result = "COMPLETED"
5999 detailed_status = "Done"
6000 db_nslcmop_update["detailed-status"] = "Done"
6001
6002 # helm base EE
6003 for item in helm_artifacts:
6004 if not (
6005 item["current_artifact_path"]
6006 and item["target_artifact_path"]
6007 and self.check_charm_hash_changed(
6008 item["current_artifact_path"],
6009 item["target_artifact_path"],
6010 )
6011 ):
6012 continue
6013 db_update_entry = "_admin.deployed.VCA.{}.".format(
6014 item["vca_index"]
6015 )
6016 vnfr_id = db_vnfr["_id"]
6017 osm_config = {"osm": {"ns_id": nsr_id, "vnf_id": vnfr_id}}
6018 db_dict = {
6019 "collection": "nsrs",
6020 "filter": {"_id": nsr_id},
6021 "path": db_update_entry,
6022 }
6023 vca_type, namespace, helm_id = get_ee_id_parts(item["ee_id"])
6024 await self.vca_map[vca_type].upgrade_execution_environment(
6025 namespace=namespace,
6026 helm_id=helm_id,
6027 db_dict=db_dict,
6028 config=osm_config,
6029 artifact_path=item["target_artifact_path"],
6030 vca_type=vca_type,
6031 )
6032 vnf_id = db_vnfr.get("vnfd-ref")
6033 config_descriptor = get_configuration(latest_vnfd, vnf_id)
6034 self.logger.debug("get ssh key block")
6035 rw_mgmt_ip = None
6036 if deep_get(
6037 config_descriptor,
6038 ("config-access", "ssh-access", "required"),
6039 ):
6040 # Needed to inject a ssh key
6041 user = deep_get(
6042 config_descriptor,
6043 ("config-access", "ssh-access", "default-user"),
6044 )
6045 step = (
6046 "Install configuration Software, getting public ssh key"
6047 )
6048 pub_key = await self.vca_map[
6049 vca_type
6050 ].get_ee_ssh_public__key(
6051 ee_id=ee_id, db_dict=db_dict, vca_id=vca_id
6052 )
6053
6054 step = (
6055 "Insert public key into VM user={} ssh_key={}".format(
6056 user, pub_key
6057 )
6058 )
6059 self.logger.debug(logging_text + step)
6060
6061 # wait for RO (ip-address) Insert pub_key into VM
6062 rw_mgmt_ip = await self.wait_vm_up_insert_key_ro(
6063 logging_text,
6064 nsr_id,
6065 vnfr_id,
6066 None,
6067 item["vdu_index"],
6068 user=user,
6069 pub_key=pub_key,
6070 )
6071
6072 initial_config_primitive_list = config_descriptor.get(
6073 "initial-config-primitive"
6074 )
6075 config_primitive = next(
6076 (
6077 p
6078 for p in initial_config_primitive_list
6079 if p["name"] == "config"
6080 ),
6081 None,
6082 )
6083 if not config_primitive:
6084 continue
6085
6086 deploy_params = {"OSM": get_osm_params(db_vnfr)}
6087 if rw_mgmt_ip:
6088 deploy_params["rw_mgmt_ip"] = rw_mgmt_ip
6089 if db_vnfr.get("additionalParamsForVnf"):
6090 deploy_params.update(
6091 parse_yaml_strings(
6092 db_vnfr["additionalParamsForVnf"].copy()
6093 )
6094 )
6095 primitive_params_ = self._map_primitive_params(
6096 config_primitive, {}, deploy_params
6097 )
6098
6099 step = "execute primitive '{}' params '{}'".format(
6100 config_primitive["name"], primitive_params_
6101 )
6102 self.logger.debug(logging_text + step)
6103 await self.vca_map[vca_type].exec_primitive(
6104 ee_id=ee_id,
6105 primitive_name=config_primitive["name"],
6106 params_dict=primitive_params_,
6107 db_dict=db_dict,
6108 vca_id=vca_id,
6109 vca_type=vca_type,
6110 )
6111
6112 step = "Updating policies"
6113 member_vnf_index = db_vnfr["member-vnf-index-ref"]
6114 detailed_status = "Done"
6115 db_nslcmop_update["detailed-status"] = "Done"
6116
6117 # If nslcmop_operation_state is None, so any operation is not failed.
6118 if not nslcmop_operation_state:
6119 nslcmop_operation_state = "COMPLETED"
6120
6121 # If update CHANGE_VNFPKG nslcmop_operation is successful
6122 # vnf revision need to be updated
6123 vnfr_update["revision"] = latest_vnfd_revision
6124 self.update_db_2("vnfrs", db_vnfr["_id"], vnfr_update)
6125
6126 self.logger.debug(
6127 logging_text
6128 + " task Done with result {} {}".format(
6129 nslcmop_operation_state, detailed_status
6130 )
6131 )
6132 elif update_type == "REMOVE_VNF":
6133 # This part is included in https://osm.etsi.org/gerrit/11876
6134 vnf_instance_id = db_nslcmop["operationParams"]["removeVnfInstanceId"]
6135 db_vnfr = self.db.get_one("vnfrs", {"_id": vnf_instance_id})
6136 member_vnf_index = db_vnfr["member-vnf-index-ref"]
6137 step = "Removing VNF"
6138 (result, detailed_status) = await self.remove_vnf(
6139 nsr_id, nslcmop_id, vnf_instance_id
6140 )
6141 if result == "FAILED":
6142 nslcmop_operation_state = result
6143 error_description_nslcmop = detailed_status
6144 db_nslcmop_update["detailed-status"] = detailed_status
6145 change_type = "vnf_terminated"
6146 if not nslcmop_operation_state:
6147 nslcmop_operation_state = "COMPLETED"
6148 self.logger.debug(
6149 logging_text
6150 + " task Done with result {} {}".format(
6151 nslcmop_operation_state, detailed_status
6152 )
6153 )
6154
6155 elif update_type == "OPERATE_VNF":
6156 vnf_id = db_nslcmop["operationParams"]["operateVnfData"][
6157 "vnfInstanceId"
6158 ]
6159 operation_type = db_nslcmop["operationParams"]["operateVnfData"][
6160 "changeStateTo"
6161 ]
6162 additional_param = db_nslcmop["operationParams"]["operateVnfData"][
6163 "additionalParam"
6164 ]
6165 (result, detailed_status) = await self.rebuild_start_stop(
6166 nsr_id, nslcmop_id, vnf_id, additional_param, operation_type
6167 )
6168 if result == "FAILED":
6169 nslcmop_operation_state = result
6170 error_description_nslcmop = detailed_status
6171 db_nslcmop_update["detailed-status"] = detailed_status
6172 if not nslcmop_operation_state:
6173 nslcmop_operation_state = "COMPLETED"
6174 self.logger.debug(
6175 logging_text
6176 + " task Done with result {} {}".format(
6177 nslcmop_operation_state, detailed_status
6178 )
6179 )
6180
6181 # If nslcmop_operation_state is None, so any operation is not failed.
6182 # All operations are executed in overall.
6183 if not nslcmop_operation_state:
6184 nslcmop_operation_state = "COMPLETED"
6185 db_nsr_update["operational-status"] = old_operational_status
6186
6187 except (DbException, LcmException, N2VCException, K8sException) as e:
6188 self.logger.error(logging_text + "Exit Exception {}".format(e))
6189 exc = e
6190 except asyncio.CancelledError:
6191 self.logger.error(
6192 logging_text + "Cancelled Exception while '{}'".format(step)
6193 )
6194 exc = "Operation was cancelled"
6195 except asyncio.TimeoutError:
6196 self.logger.error(logging_text + "Timeout while '{}'".format(step))
6197 exc = "Timeout"
6198 except Exception as e:
6199 exc = traceback.format_exc()
6200 self.logger.critical(
6201 logging_text + "Exit Exception {} {}".format(type(e).__name__, e),
6202 exc_info=True,
6203 )
6204 finally:
6205 if exc:
6206 db_nslcmop_update[
6207 "detailed-status"
6208 ] = (
6209 detailed_status
6210 ) = error_description_nslcmop = "FAILED {}: {}".format(step, exc)
6211 nslcmop_operation_state = "FAILED"
6212 db_nsr_update["operational-status"] = old_operational_status
6213 if db_nsr:
6214 self._write_ns_status(
6215 nsr_id=nsr_id,
6216 ns_state=db_nsr["nsState"],
6217 current_operation="IDLE",
6218 current_operation_id=None,
6219 other_update=db_nsr_update,
6220 )
6221
6222 self._write_op_status(
6223 op_id=nslcmop_id,
6224 stage="",
6225 error_message=error_description_nslcmop,
6226 operation_state=nslcmop_operation_state,
6227 other_update=db_nslcmop_update,
6228 )
6229
6230 if nslcmop_operation_state:
6231 try:
6232 msg = {
6233 "nsr_id": nsr_id,
6234 "nslcmop_id": nslcmop_id,
6235 "operationState": nslcmop_operation_state,
6236 }
6237 if (
6238 change_type in ("vnf_terminated", "policy_updated")
6239 and member_vnf_index
6240 ):
6241 msg.update({"vnf_member_index": member_vnf_index})
6242 await self.msg.aiowrite("ns", change_type, msg)
6243 except Exception as e:
6244 self.logger.error(
6245 logging_text + "kafka_write notification Exception {}".format(e)
6246 )
6247 self.logger.debug(logging_text + "Exit")
6248 self.lcm_tasks.remove("ns", nsr_id, nslcmop_id, "ns_update")
6249 return nslcmop_operation_state, detailed_status
6250
6251 async def scale(self, nsr_id, nslcmop_id):
6252 # Try to lock HA task here
6253 task_is_locked_by_me = self.lcm_tasks.lock_HA("ns", "nslcmops", nslcmop_id)
6254 if not task_is_locked_by_me:
6255 return
6256
6257 logging_text = "Task ns={} scale={} ".format(nsr_id, nslcmop_id)
6258 stage = ["", "", ""]
6259 tasks_dict_info = {}
6260 # ^ stage, step, VIM progress
6261 self.logger.debug(logging_text + "Enter")
6262 # get all needed from database
6263 db_nsr = None
6264 db_nslcmop_update = {}
6265 db_nsr_update = {}
6266 exc = None
6267 # in case of error, indicates what part of scale was failed to put nsr at error status
6268 scale_process = None
6269 old_operational_status = ""
6270 old_config_status = ""
6271 nsi_id = None
6272 prom_job_name = ""
6273 try:
6274 # wait for any previous tasks in process
6275 step = "Waiting for previous operations to terminate"
6276 await self.lcm_tasks.waitfor_related_HA("ns", "nslcmops", nslcmop_id)
6277 self._write_ns_status(
6278 nsr_id=nsr_id,
6279 ns_state=None,
6280 current_operation="SCALING",
6281 current_operation_id=nslcmop_id,
6282 )
6283
6284 step = "Getting nslcmop from database"
6285 self.logger.debug(
6286 step + " after having waited for previous tasks to be completed"
6287 )
6288 db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
6289
6290 step = "Getting nsr from database"
6291 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
6292 old_operational_status = db_nsr["operational-status"]
6293 old_config_status = db_nsr["config-status"]
6294
6295 step = "Checking whether the descriptor has SFC"
6296 if db_nsr.get("nsd", {}).get("vnffgd"):
6297 raise LcmException("Scaling is not allowed for NS with SFC")
6298
6299 step = "Parsing scaling parameters"
6300 db_nsr_update["operational-status"] = "scaling"
6301 self.update_db_2("nsrs", nsr_id, db_nsr_update)
6302 nsr_deployed = db_nsr["_admin"].get("deployed")
6303
6304 vnf_index = db_nslcmop["operationParams"]["scaleVnfData"][
6305 "scaleByStepData"
6306 ]["member-vnf-index"]
6307 scaling_group = db_nslcmop["operationParams"]["scaleVnfData"][
6308 "scaleByStepData"
6309 ]["scaling-group-descriptor"]
6310 scaling_type = db_nslcmop["operationParams"]["scaleVnfData"]["scaleVnfType"]
6311 # for backward compatibility
6312 if nsr_deployed and isinstance(nsr_deployed.get("VCA"), dict):
6313 nsr_deployed["VCA"] = list(nsr_deployed["VCA"].values())
6314 db_nsr_update["_admin.deployed.VCA"] = nsr_deployed["VCA"]
6315 self.update_db_2("nsrs", nsr_id, db_nsr_update)
6316
6317 step = "Getting vnfr from database"
6318 db_vnfr = self.db.get_one(
6319 "vnfrs", {"member-vnf-index-ref": vnf_index, "nsr-id-ref": nsr_id}
6320 )
6321
6322 vca_id = self.get_vca_id(db_vnfr, db_nsr)
6323
6324 step = "Getting vnfd from database"
6325 db_vnfd = self.db.get_one("vnfds", {"_id": db_vnfr["vnfd-id"]})
6326
6327 base_folder = db_vnfd["_admin"]["storage"]
6328
6329 step = "Getting scaling-group-descriptor"
6330 scaling_descriptor = find_in_list(
6331 get_scaling_aspect(db_vnfd),
6332 lambda scale_desc: scale_desc["name"] == scaling_group,
6333 )
6334 if not scaling_descriptor:
6335 raise LcmException(
6336 "input parameter 'scaleByStepData':'scaling-group-descriptor':'{}' is not present "
6337 "at vnfd:scaling-group-descriptor".format(scaling_group)
6338 )
6339
6340 step = "Sending scale order to VIM"
6341 # TODO check if ns is in a proper status
6342 nb_scale_op = 0
6343 if not db_nsr["_admin"].get("scaling-group"):
6344 self.update_db_2(
6345 "nsrs",
6346 nsr_id,
6347 {
6348 "_admin.scaling-group": [
6349 {"name": scaling_group, "nb-scale-op": 0}
6350 ]
6351 },
6352 )
6353 admin_scale_index = 0
6354 else:
6355 for admin_scale_index, admin_scale_info in enumerate(
6356 db_nsr["_admin"]["scaling-group"]
6357 ):
6358 if admin_scale_info["name"] == scaling_group:
6359 nb_scale_op = admin_scale_info.get("nb-scale-op", 0)
6360 break
6361 else: # not found, set index one plus last element and add new entry with the name
6362 admin_scale_index += 1
6363 db_nsr_update[
6364 "_admin.scaling-group.{}.name".format(admin_scale_index)
6365 ] = scaling_group
6366
6367 vca_scaling_info = []
6368 scaling_info = {"scaling_group_name": scaling_group, "vdu": [], "kdu": []}
6369 if scaling_type == "SCALE_OUT":
6370 if "aspect-delta-details" not in scaling_descriptor:
6371 raise LcmException(
6372 "Aspect delta details not fount in scaling descriptor {}".format(
6373 scaling_descriptor["name"]
6374 )
6375 )
6376 # count if max-instance-count is reached
6377 deltas = scaling_descriptor.get("aspect-delta-details")["deltas"]
6378
6379 scaling_info["scaling_direction"] = "OUT"
6380 scaling_info["vdu-create"] = {}
6381 scaling_info["kdu-create"] = {}
6382 for delta in deltas:
6383 for vdu_delta in delta.get("vdu-delta", {}):
6384 vdud = get_vdu(db_vnfd, vdu_delta["id"])
6385 # vdu_index also provides the number of instance of the targeted vdu
6386 vdu_count = vdu_index = get_vdur_index(db_vnfr, vdu_delta)
6387 if vdu_index <= len(db_vnfr["vdur"]):
6388 vdu_name_id = db_vnfr["vdur"][vdu_index - 1]["vdu-name"]
6389 prom_job_name = (
6390 db_vnfr["_id"] + vdu_name_id + str(vdu_index - 1)
6391 )
6392 prom_job_name = prom_job_name.replace("_", "")
6393 prom_job_name = prom_job_name.replace("-", "")
6394 else:
6395 prom_job_name = None
6396 cloud_init_text = self._get_vdu_cloud_init_content(
6397 vdud, db_vnfd
6398 )
6399 if cloud_init_text:
6400 additional_params = (
6401 self._get_vdu_additional_params(db_vnfr, vdud["id"])
6402 or {}
6403 )
6404 cloud_init_list = []
6405
6406 vdu_profile = get_vdu_profile(db_vnfd, vdu_delta["id"])
6407 max_instance_count = 10
6408 if vdu_profile and "max-number-of-instances" in vdu_profile:
6409 max_instance_count = vdu_profile.get(
6410 "max-number-of-instances", 10
6411 )
6412
6413 default_instance_num = get_number_of_instances(
6414 db_vnfd, vdud["id"]
6415 )
6416 instances_number = vdu_delta.get("number-of-instances", 1)
6417 nb_scale_op += instances_number
6418
6419 new_instance_count = nb_scale_op + default_instance_num
6420 # Control if new count is over max and vdu count is less than max.
6421 # Then assign new instance count
6422 if new_instance_count > max_instance_count > vdu_count:
6423 instances_number = new_instance_count - max_instance_count
6424 else:
6425 instances_number = instances_number
6426
6427 if new_instance_count > max_instance_count:
6428 raise LcmException(
6429 "reached the limit of {} (max-instance-count) "
6430 "scaling-out operations for the "
6431 "scaling-group-descriptor '{}'".format(
6432 nb_scale_op, scaling_group
6433 )
6434 )
6435 for x in range(vdu_delta.get("number-of-instances", 1)):
6436 if cloud_init_text:
6437 # TODO Information of its own ip is not available because db_vnfr is not updated.
6438 additional_params["OSM"] = get_osm_params(
6439 db_vnfr, vdu_delta["id"], vdu_index + x
6440 )
6441 cloud_init_list.append(
6442 self._parse_cloud_init(
6443 cloud_init_text,
6444 additional_params,
6445 db_vnfd["id"],
6446 vdud["id"],
6447 )
6448 )
6449 vca_scaling_info.append(
6450 {
6451 "osm_vdu_id": vdu_delta["id"],
6452 "member-vnf-index": vnf_index,
6453 "type": "create",
6454 "vdu_index": vdu_index + x,
6455 }
6456 )
6457 scaling_info["vdu-create"][vdu_delta["id"]] = instances_number
6458 for kdu_delta in delta.get("kdu-resource-delta", {}):
6459 kdu_profile = get_kdu_resource_profile(db_vnfd, kdu_delta["id"])
6460 kdu_name = kdu_profile["kdu-name"]
6461 resource_name = kdu_profile.get("resource-name", "")
6462
6463 # Might have different kdus in the same delta
6464 # Should have list for each kdu
6465 if not scaling_info["kdu-create"].get(kdu_name, None):
6466 scaling_info["kdu-create"][kdu_name] = []
6467
6468 kdur = get_kdur(db_vnfr, kdu_name)
6469 if kdur.get("helm-chart"):
6470 k8s_cluster_type = "helm-chart-v3"
6471 self.logger.debug("kdur: {}".format(kdur))
6472 elif kdur.get("juju-bundle"):
6473 k8s_cluster_type = "juju-bundle"
6474 else:
6475 raise LcmException(
6476 "kdu type for kdu='{}.{}' is neither helm-chart nor "
6477 "juju-bundle. Maybe an old NBI version is running".format(
6478 db_vnfr["member-vnf-index-ref"], kdu_name
6479 )
6480 )
6481
6482 max_instance_count = 10
6483 if kdu_profile and "max-number-of-instances" in kdu_profile:
6484 max_instance_count = kdu_profile.get(
6485 "max-number-of-instances", 10
6486 )
6487
6488 nb_scale_op += kdu_delta.get("number-of-instances", 1)
6489 deployed_kdu, _ = get_deployed_kdu(
6490 nsr_deployed, kdu_name, vnf_index
6491 )
6492 if deployed_kdu is None:
6493 raise LcmException(
6494 "KDU '{}' for vnf '{}' not deployed".format(
6495 kdu_name, vnf_index
6496 )
6497 )
6498 kdu_instance = deployed_kdu.get("kdu-instance")
6499 instance_num = await self.k8scluster_map[
6500 k8s_cluster_type
6501 ].get_scale_count(
6502 resource_name,
6503 kdu_instance,
6504 vca_id=vca_id,
6505 cluster_uuid=deployed_kdu.get("k8scluster-uuid"),
6506 kdu_model=deployed_kdu.get("kdu-model"),
6507 )
6508 kdu_replica_count = instance_num + kdu_delta.get(
6509 "number-of-instances", 1
6510 )
6511
6512 # Control if new count is over max and instance_num is less than max.
6513 # Then assign max instance number to kdu replica count
6514 if kdu_replica_count > max_instance_count > instance_num:
6515 kdu_replica_count = max_instance_count
6516 if kdu_replica_count > max_instance_count:
6517 raise LcmException(
6518 "reached the limit of {} (max-instance-count) "
6519 "scaling-out operations for the "
6520 "scaling-group-descriptor '{}'".format(
6521 instance_num, scaling_group
6522 )
6523 )
6524
6525 for x in range(kdu_delta.get("number-of-instances", 1)):
6526 vca_scaling_info.append(
6527 {
6528 "osm_kdu_id": kdu_name,
6529 "member-vnf-index": vnf_index,
6530 "type": "create",
6531 "kdu_index": instance_num + x - 1,
6532 }
6533 )
6534 scaling_info["kdu-create"][kdu_name].append(
6535 {
6536 "member-vnf-index": vnf_index,
6537 "type": "create",
6538 "k8s-cluster-type": k8s_cluster_type,
6539 "resource-name": resource_name,
6540 "scale": kdu_replica_count,
6541 }
6542 )
6543 elif scaling_type == "SCALE_IN":
6544 deltas = scaling_descriptor.get("aspect-delta-details")["deltas"]
6545
6546 scaling_info["scaling_direction"] = "IN"
6547 scaling_info["vdu-delete"] = {}
6548 scaling_info["kdu-delete"] = {}
6549
6550 for delta in deltas:
6551 for vdu_delta in delta.get("vdu-delta", {}):
6552 vdu_count = vdu_index = get_vdur_index(db_vnfr, vdu_delta)
6553 min_instance_count = 0
6554 vdu_profile = get_vdu_profile(db_vnfd, vdu_delta["id"])
6555 if vdu_profile and "min-number-of-instances" in vdu_profile:
6556 min_instance_count = vdu_profile["min-number-of-instances"]
6557
6558 default_instance_num = get_number_of_instances(
6559 db_vnfd, vdu_delta["id"]
6560 )
6561 instance_num = vdu_delta.get("number-of-instances", 1)
6562 nb_scale_op -= instance_num
6563
6564 new_instance_count = nb_scale_op + default_instance_num
6565
6566 if new_instance_count < min_instance_count < vdu_count:
6567 instances_number = min_instance_count - new_instance_count
6568 else:
6569 instances_number = instance_num
6570
6571 if new_instance_count < min_instance_count:
6572 raise LcmException(
6573 "reached the limit of {} (min-instance-count) scaling-in operations for the "
6574 "scaling-group-descriptor '{}'".format(
6575 nb_scale_op, scaling_group
6576 )
6577 )
6578 for x in range(vdu_delta.get("number-of-instances", 1)):
6579 vca_scaling_info.append(
6580 {
6581 "osm_vdu_id": vdu_delta["id"],
6582 "member-vnf-index": vnf_index,
6583 "type": "delete",
6584 "vdu_index": vdu_index - 1 - x,
6585 }
6586 )
6587 scaling_info["vdu-delete"][vdu_delta["id"]] = instances_number
6588 for kdu_delta in delta.get("kdu-resource-delta", {}):
6589 kdu_profile = get_kdu_resource_profile(db_vnfd, kdu_delta["id"])
6590 kdu_name = kdu_profile["kdu-name"]
6591 resource_name = kdu_profile.get("resource-name", "")
6592
6593 if not scaling_info["kdu-delete"].get(kdu_name, None):
6594 scaling_info["kdu-delete"][kdu_name] = []
6595
6596 kdur = get_kdur(db_vnfr, kdu_name)
6597 if kdur.get("helm-chart"):
6598 k8s_cluster_type = "helm-chart-v3"
6599 self.logger.debug("kdur: {}".format(kdur))
6600 elif kdur.get("juju-bundle"):
6601 k8s_cluster_type = "juju-bundle"
6602 else:
6603 raise LcmException(
6604 "kdu type for kdu='{}.{}' is neither helm-chart nor "
6605 "juju-bundle. Maybe an old NBI version is running".format(
6606 db_vnfr["member-vnf-index-ref"], kdur["kdu-name"]
6607 )
6608 )
6609
6610 min_instance_count = 0
6611 if kdu_profile and "min-number-of-instances" in kdu_profile:
6612 min_instance_count = kdu_profile["min-number-of-instances"]
6613
6614 nb_scale_op -= kdu_delta.get("number-of-instances", 1)
6615 deployed_kdu, _ = get_deployed_kdu(
6616 nsr_deployed, kdu_name, vnf_index
6617 )
6618 if deployed_kdu is None:
6619 raise LcmException(
6620 "KDU '{}' for vnf '{}' not deployed".format(
6621 kdu_name, vnf_index
6622 )
6623 )
6624 kdu_instance = deployed_kdu.get("kdu-instance")
6625 instance_num = await self.k8scluster_map[
6626 k8s_cluster_type
6627 ].get_scale_count(
6628 resource_name,
6629 kdu_instance,
6630 vca_id=vca_id,
6631 cluster_uuid=deployed_kdu.get("k8scluster-uuid"),
6632 kdu_model=deployed_kdu.get("kdu-model"),
6633 )
6634 kdu_replica_count = instance_num - kdu_delta.get(
6635 "number-of-instances", 1
6636 )
6637
6638 if kdu_replica_count < min_instance_count < instance_num:
6639 kdu_replica_count = min_instance_count
6640 if kdu_replica_count < min_instance_count:
6641 raise LcmException(
6642 "reached the limit of {} (min-instance-count) scaling-in operations for the "
6643 "scaling-group-descriptor '{}'".format(
6644 instance_num, scaling_group
6645 )
6646 )
6647
6648 for x in range(kdu_delta.get("number-of-instances", 1)):
6649 vca_scaling_info.append(
6650 {
6651 "osm_kdu_id": kdu_name,
6652 "member-vnf-index": vnf_index,
6653 "type": "delete",
6654 "kdu_index": instance_num - x - 1,
6655 }
6656 )
6657 scaling_info["kdu-delete"][kdu_name].append(
6658 {
6659 "member-vnf-index": vnf_index,
6660 "type": "delete",
6661 "k8s-cluster-type": k8s_cluster_type,
6662 "resource-name": resource_name,
6663 "scale": kdu_replica_count,
6664 }
6665 )
6666
6667 # update VDU_SCALING_INFO with the VDUs to delete ip_addresses
6668 vdu_delete = copy(scaling_info.get("vdu-delete"))
6669 if scaling_info["scaling_direction"] == "IN":
6670 for vdur in reversed(db_vnfr["vdur"]):
6671 if vdu_delete.get(vdur["vdu-id-ref"]):
6672 vdu_delete[vdur["vdu-id-ref"]] -= 1
6673 scaling_info["vdu"].append(
6674 {
6675 "name": vdur.get("name") or vdur.get("vdu-name"),
6676 "vdu_id": vdur["vdu-id-ref"],
6677 "interface": [],
6678 }
6679 )
6680 for interface in vdur["interfaces"]:
6681 scaling_info["vdu"][-1]["interface"].append(
6682 {
6683 "name": interface["name"],
6684 "ip_address": interface["ip-address"],
6685 "mac_address": interface.get("mac-address"),
6686 }
6687 )
6688 # vdu_delete = vdu_scaling_info.pop("vdu-delete")
6689
6690 # PRE-SCALE BEGIN
6691 step = "Executing pre-scale vnf-config-primitive"
6692 if scaling_descriptor.get("scaling-config-action"):
6693 for scaling_config_action in scaling_descriptor[
6694 "scaling-config-action"
6695 ]:
6696 if (
6697 scaling_config_action.get("trigger") == "pre-scale-in"
6698 and scaling_type == "SCALE_IN"
6699 ) or (
6700 scaling_config_action.get("trigger") == "pre-scale-out"
6701 and scaling_type == "SCALE_OUT"
6702 ):
6703 vnf_config_primitive = scaling_config_action[
6704 "vnf-config-primitive-name-ref"
6705 ]
6706 step = db_nslcmop_update[
6707 "detailed-status"
6708 ] = "executing pre-scale scaling-config-action '{}'".format(
6709 vnf_config_primitive
6710 )
6711
6712 # look for primitive
6713 for config_primitive in (
6714 get_configuration(db_vnfd, db_vnfd["id"]) or {}
6715 ).get("config-primitive", ()):
6716 if config_primitive["name"] == vnf_config_primitive:
6717 break
6718 else:
6719 raise LcmException(
6720 "Invalid vnfd descriptor at scaling-group-descriptor[name='{}']:scaling-config-action"
6721 "[vnf-config-primitive-name-ref='{}'] does not match any vnf-configuration:config-"
6722 "primitive".format(scaling_group, vnf_config_primitive)
6723 )
6724
6725 vnfr_params = {"VDU_SCALE_INFO": scaling_info}
6726 if db_vnfr.get("additionalParamsForVnf"):
6727 vnfr_params.update(db_vnfr["additionalParamsForVnf"])
6728
6729 scale_process = "VCA"
6730 db_nsr_update["config-status"] = "configuring pre-scaling"
6731 primitive_params = self._map_primitive_params(
6732 config_primitive, {}, vnfr_params
6733 )
6734
6735 # Pre-scale retry check: Check if this sub-operation has been executed before
6736 op_index = self._check_or_add_scale_suboperation(
6737 db_nslcmop,
6738 vnf_index,
6739 vnf_config_primitive,
6740 primitive_params,
6741 "PRE-SCALE",
6742 )
6743 if op_index == self.SUBOPERATION_STATUS_SKIP:
6744 # Skip sub-operation
6745 result = "COMPLETED"
6746 result_detail = "Done"
6747 self.logger.debug(
6748 logging_text
6749 + "vnf_config_primitive={} Skipped sub-operation, result {} {}".format(
6750 vnf_config_primitive, result, result_detail
6751 )
6752 )
6753 else:
6754 if op_index == self.SUBOPERATION_STATUS_NEW:
6755 # New sub-operation: Get index of this sub-operation
6756 op_index = (
6757 len(db_nslcmop.get("_admin", {}).get("operations"))
6758 - 1
6759 )
6760 self.logger.debug(
6761 logging_text
6762 + "vnf_config_primitive={} New sub-operation".format(
6763 vnf_config_primitive
6764 )
6765 )
6766 else:
6767 # retry: Get registered params for this existing sub-operation
6768 op = db_nslcmop.get("_admin", {}).get("operations", [])[
6769 op_index
6770 ]
6771 vnf_index = op.get("member_vnf_index")
6772 vnf_config_primitive = op.get("primitive")
6773 primitive_params = op.get("primitive_params")
6774 self.logger.debug(
6775 logging_text
6776 + "vnf_config_primitive={} Sub-operation retry".format(
6777 vnf_config_primitive
6778 )
6779 )
6780 # Execute the primitive, either with new (first-time) or registered (reintent) args
6781 ee_descriptor_id = config_primitive.get(
6782 "execution-environment-ref"
6783 )
6784 primitive_name = config_primitive.get(
6785 "execution-environment-primitive", vnf_config_primitive
6786 )
6787 ee_id, vca_type = self._look_for_deployed_vca(
6788 nsr_deployed["VCA"],
6789 member_vnf_index=vnf_index,
6790 vdu_id=None,
6791 vdu_count_index=None,
6792 ee_descriptor_id=ee_descriptor_id,
6793 )
6794 result, result_detail = await self._ns_execute_primitive(
6795 ee_id,
6796 primitive_name,
6797 primitive_params,
6798 vca_type=vca_type,
6799 vca_id=vca_id,
6800 )
6801 self.logger.debug(
6802 logging_text
6803 + "vnf_config_primitive={} Done with result {} {}".format(
6804 vnf_config_primitive, result, result_detail
6805 )
6806 )
6807 # Update operationState = COMPLETED | FAILED
6808 self._update_suboperation_status(
6809 db_nslcmop, op_index, result, result_detail
6810 )
6811
6812 if result == "FAILED":
6813 raise LcmException(result_detail)
6814 db_nsr_update["config-status"] = old_config_status
6815 scale_process = None
6816 # PRE-SCALE END
6817
6818 db_nsr_update[
6819 "_admin.scaling-group.{}.nb-scale-op".format(admin_scale_index)
6820 ] = nb_scale_op
6821 db_nsr_update[
6822 "_admin.scaling-group.{}.time".format(admin_scale_index)
6823 ] = time()
6824
6825 # SCALE-IN VCA - BEGIN
6826 if vca_scaling_info:
6827 step = db_nslcmop_update[
6828 "detailed-status"
6829 ] = "Deleting the execution environments"
6830 scale_process = "VCA"
6831 for vca_info in vca_scaling_info:
6832 if vca_info["type"] == "delete" and not vca_info.get("osm_kdu_id"):
6833 member_vnf_index = str(vca_info["member-vnf-index"])
6834 self.logger.debug(
6835 logging_text + "vdu info: {}".format(vca_info)
6836 )
6837 if vca_info.get("osm_vdu_id"):
6838 vdu_id = vca_info["osm_vdu_id"]
6839 vdu_index = int(vca_info["vdu_index"])
6840 stage[
6841 1
6842 ] = "Scaling member_vnf_index={}, vdu_id={}, vdu_index={} ".format(
6843 member_vnf_index, vdu_id, vdu_index
6844 )
6845 stage[2] = step = "Scaling in VCA"
6846 self._write_op_status(op_id=nslcmop_id, stage=stage)
6847 vca_update = db_nsr["_admin"]["deployed"]["VCA"]
6848 config_update = db_nsr["configurationStatus"]
6849 for vca_index, vca in enumerate(vca_update):
6850 if (
6851 (vca or vca.get("ee_id"))
6852 and vca["member-vnf-index"] == member_vnf_index
6853 and vca["vdu_count_index"] == vdu_index
6854 ):
6855 if vca.get("vdu_id"):
6856 config_descriptor = get_configuration(
6857 db_vnfd, vca.get("vdu_id")
6858 )
6859 elif vca.get("kdu_name"):
6860 config_descriptor = get_configuration(
6861 db_vnfd, vca.get("kdu_name")
6862 )
6863 else:
6864 config_descriptor = get_configuration(
6865 db_vnfd, db_vnfd["id"]
6866 )
6867 operation_params = (
6868 db_nslcmop.get("operationParams") or {}
6869 )
6870 exec_terminate_primitives = not operation_params.get(
6871 "skip_terminate_primitives"
6872 ) and vca.get("needed_terminate")
6873 task = asyncio.ensure_future(
6874 asyncio.wait_for(
6875 self.destroy_N2VC(
6876 logging_text,
6877 db_nslcmop,
6878 vca,
6879 config_descriptor,
6880 vca_index,
6881 destroy_ee=True,
6882 exec_primitives=exec_terminate_primitives,
6883 scaling_in=True,
6884 vca_id=vca_id,
6885 ),
6886 timeout=self.timeout.charm_delete,
6887 )
6888 )
6889 tasks_dict_info[task] = "Terminating VCA {}".format(
6890 vca.get("ee_id")
6891 )
6892 del vca_update[vca_index]
6893 del config_update[vca_index]
6894 # wait for pending tasks of terminate primitives
6895 if tasks_dict_info:
6896 self.logger.debug(
6897 logging_text
6898 + "Waiting for tasks {}".format(
6899 list(tasks_dict_info.keys())
6900 )
6901 )
6902 error_list = await self._wait_for_tasks(
6903 logging_text,
6904 tasks_dict_info,
6905 min(
6906 self.timeout.charm_delete, self.timeout.ns_terminate
6907 ),
6908 stage,
6909 nslcmop_id,
6910 )
6911 tasks_dict_info.clear()
6912 if error_list:
6913 raise LcmException("; ".join(error_list))
6914
6915 db_vca_and_config_update = {
6916 "_admin.deployed.VCA": vca_update,
6917 "configurationStatus": config_update,
6918 }
6919 self.update_db_2(
6920 "nsrs", db_nsr["_id"], db_vca_and_config_update
6921 )
6922 scale_process = None
6923 # SCALE-IN VCA - END
6924
6925 # SCALE RO - BEGIN
6926 if scaling_info.get("vdu-create") or scaling_info.get("vdu-delete"):
6927 scale_process = "RO"
6928 if self.ro_config.ng:
6929 await self._scale_ng_ro(
6930 logging_text, db_nsr, db_nslcmop, db_vnfr, scaling_info, stage
6931 )
6932 scaling_info.pop("vdu-create", None)
6933 scaling_info.pop("vdu-delete", None)
6934
6935 scale_process = None
6936 # SCALE RO - END
6937
6938 # SCALE KDU - BEGIN
6939 if scaling_info.get("kdu-create") or scaling_info.get("kdu-delete"):
6940 scale_process = "KDU"
6941 await self._scale_kdu(
6942 logging_text, nsr_id, nsr_deployed, db_vnfd, vca_id, scaling_info
6943 )
6944 scaling_info.pop("kdu-create", None)
6945 scaling_info.pop("kdu-delete", None)
6946
6947 scale_process = None
6948 # SCALE KDU - END
6949
6950 if db_nsr_update:
6951 self.update_db_2("nsrs", nsr_id, db_nsr_update)
6952
6953 # SCALE-UP VCA - BEGIN
6954 if vca_scaling_info:
6955 step = db_nslcmop_update[
6956 "detailed-status"
6957 ] = "Creating new execution environments"
6958 scale_process = "VCA"
6959 for vca_info in vca_scaling_info:
6960 if vca_info["type"] == "create" and not vca_info.get("osm_kdu_id"):
6961 member_vnf_index = str(vca_info["member-vnf-index"])
6962 self.logger.debug(
6963 logging_text + "vdu info: {}".format(vca_info)
6964 )
6965 vnfd_id = db_vnfr["vnfd-ref"]
6966 if vca_info.get("osm_vdu_id"):
6967 vdu_index = int(vca_info["vdu_index"])
6968 deploy_params = {"OSM": get_osm_params(db_vnfr)}
6969 if db_vnfr.get("additionalParamsForVnf"):
6970 deploy_params.update(
6971 parse_yaml_strings(
6972 db_vnfr["additionalParamsForVnf"].copy()
6973 )
6974 )
6975 descriptor_config = get_configuration(
6976 db_vnfd, db_vnfd["id"]
6977 )
6978 if descriptor_config:
6979 vdu_id = None
6980 vdu_name = None
6981 kdu_name = None
6982 kdu_index = None
6983 self._deploy_n2vc(
6984 logging_text=logging_text
6985 + "member_vnf_index={} ".format(member_vnf_index),
6986 db_nsr=db_nsr,
6987 db_vnfr=db_vnfr,
6988 nslcmop_id=nslcmop_id,
6989 nsr_id=nsr_id,
6990 nsi_id=nsi_id,
6991 vnfd_id=vnfd_id,
6992 vdu_id=vdu_id,
6993 kdu_name=kdu_name,
6994 kdu_index=kdu_index,
6995 member_vnf_index=member_vnf_index,
6996 vdu_index=vdu_index,
6997 vdu_name=vdu_name,
6998 deploy_params=deploy_params,
6999 descriptor_config=descriptor_config,
7000 base_folder=base_folder,
7001 task_instantiation_info=tasks_dict_info,
7002 stage=stage,
7003 )
7004 vdu_id = vca_info["osm_vdu_id"]
7005 vdur = find_in_list(
7006 db_vnfr["vdur"], lambda vdu: vdu["vdu-id-ref"] == vdu_id
7007 )
7008 descriptor_config = get_configuration(db_vnfd, vdu_id)
7009 if vdur.get("additionalParams"):
7010 deploy_params_vdu = parse_yaml_strings(
7011 vdur["additionalParams"]
7012 )
7013 else:
7014 deploy_params_vdu = deploy_params
7015 deploy_params_vdu["OSM"] = get_osm_params(
7016 db_vnfr, vdu_id, vdu_count_index=vdu_index
7017 )
7018 if descriptor_config:
7019 vdu_name = None
7020 kdu_name = None
7021 kdu_index = None
7022 stage[
7023 1
7024 ] = "Scaling member_vnf_index={}, vdu_id={}, vdu_index={} ".format(
7025 member_vnf_index, vdu_id, vdu_index
7026 )
7027 stage[2] = step = "Scaling out VCA"
7028 self._write_op_status(op_id=nslcmop_id, stage=stage)
7029 self._deploy_n2vc(
7030 logging_text=logging_text
7031 + "member_vnf_index={}, vdu_id={}, vdu_index={} ".format(
7032 member_vnf_index, vdu_id, vdu_index
7033 ),
7034 db_nsr=db_nsr,
7035 db_vnfr=db_vnfr,
7036 nslcmop_id=nslcmop_id,
7037 nsr_id=nsr_id,
7038 nsi_id=nsi_id,
7039 vnfd_id=vnfd_id,
7040 vdu_id=vdu_id,
7041 kdu_name=kdu_name,
7042 member_vnf_index=member_vnf_index,
7043 vdu_index=vdu_index,
7044 kdu_index=kdu_index,
7045 vdu_name=vdu_name,
7046 deploy_params=deploy_params_vdu,
7047 descriptor_config=descriptor_config,
7048 base_folder=base_folder,
7049 task_instantiation_info=tasks_dict_info,
7050 stage=stage,
7051 )
7052 # SCALE-UP VCA - END
7053 scale_process = None
7054
7055 # POST-SCALE BEGIN
7056 # execute primitive service POST-SCALING
7057 step = "Executing post-scale vnf-config-primitive"
7058 if scaling_descriptor.get("scaling-config-action"):
7059 for scaling_config_action in scaling_descriptor[
7060 "scaling-config-action"
7061 ]:
7062 if (
7063 scaling_config_action.get("trigger") == "post-scale-in"
7064 and scaling_type == "SCALE_IN"
7065 ) or (
7066 scaling_config_action.get("trigger") == "post-scale-out"
7067 and scaling_type == "SCALE_OUT"
7068 ):
7069 vnf_config_primitive = scaling_config_action[
7070 "vnf-config-primitive-name-ref"
7071 ]
7072 step = db_nslcmop_update[
7073 "detailed-status"
7074 ] = "executing post-scale scaling-config-action '{}'".format(
7075 vnf_config_primitive
7076 )
7077
7078 vnfr_params = {"VDU_SCALE_INFO": scaling_info}
7079 if db_vnfr.get("additionalParamsForVnf"):
7080 vnfr_params.update(db_vnfr["additionalParamsForVnf"])
7081
7082 # look for primitive
7083 for config_primitive in (
7084 get_configuration(db_vnfd, db_vnfd["id"]) or {}
7085 ).get("config-primitive", ()):
7086 if config_primitive["name"] == vnf_config_primitive:
7087 break
7088 else:
7089 raise LcmException(
7090 "Invalid vnfd descriptor at scaling-group-descriptor[name='{}']:scaling-config-"
7091 "action[vnf-config-primitive-name-ref='{}'] does not match any vnf-configuration:"
7092 "config-primitive".format(
7093 scaling_group, vnf_config_primitive
7094 )
7095 )
7096 scale_process = "VCA"
7097 db_nsr_update["config-status"] = "configuring post-scaling"
7098 primitive_params = self._map_primitive_params(
7099 config_primitive, {}, vnfr_params
7100 )
7101
7102 # Post-scale retry check: Check if this sub-operation has been executed before
7103 op_index = self._check_or_add_scale_suboperation(
7104 db_nslcmop,
7105 vnf_index,
7106 vnf_config_primitive,
7107 primitive_params,
7108 "POST-SCALE",
7109 )
7110 if op_index == self.SUBOPERATION_STATUS_SKIP:
7111 # Skip sub-operation
7112 result = "COMPLETED"
7113 result_detail = "Done"
7114 self.logger.debug(
7115 logging_text
7116 + "vnf_config_primitive={} Skipped sub-operation, result {} {}".format(
7117 vnf_config_primitive, result, result_detail
7118 )
7119 )
7120 else:
7121 if op_index == self.SUBOPERATION_STATUS_NEW:
7122 # New sub-operation: Get index of this sub-operation
7123 op_index = (
7124 len(db_nslcmop.get("_admin", {}).get("operations"))
7125 - 1
7126 )
7127 self.logger.debug(
7128 logging_text
7129 + "vnf_config_primitive={} New sub-operation".format(
7130 vnf_config_primitive
7131 )
7132 )
7133 else:
7134 # retry: Get registered params for this existing sub-operation
7135 op = db_nslcmop.get("_admin", {}).get("operations", [])[
7136 op_index
7137 ]
7138 vnf_index = op.get("member_vnf_index")
7139 vnf_config_primitive = op.get("primitive")
7140 primitive_params = op.get("primitive_params")
7141 self.logger.debug(
7142 logging_text
7143 + "vnf_config_primitive={} Sub-operation retry".format(
7144 vnf_config_primitive
7145 )
7146 )
7147 # Execute the primitive, either with new (first-time) or registered (reintent) args
7148 ee_descriptor_id = config_primitive.get(
7149 "execution-environment-ref"
7150 )
7151 primitive_name = config_primitive.get(
7152 "execution-environment-primitive", vnf_config_primitive
7153 )
7154 ee_id, vca_type = self._look_for_deployed_vca(
7155 nsr_deployed["VCA"],
7156 member_vnf_index=vnf_index,
7157 vdu_id=None,
7158 vdu_count_index=None,
7159 ee_descriptor_id=ee_descriptor_id,
7160 )
7161 result, result_detail = await self._ns_execute_primitive(
7162 ee_id,
7163 primitive_name,
7164 primitive_params,
7165 vca_type=vca_type,
7166 vca_id=vca_id,
7167 )
7168 self.logger.debug(
7169 logging_text
7170 + "vnf_config_primitive={} Done with result {} {}".format(
7171 vnf_config_primitive, result, result_detail
7172 )
7173 )
7174 # Update operationState = COMPLETED | FAILED
7175 self._update_suboperation_status(
7176 db_nslcmop, op_index, result, result_detail
7177 )
7178
7179 if result == "FAILED":
7180 raise LcmException(result_detail)
7181 db_nsr_update["config-status"] = old_config_status
7182 scale_process = None
7183 # POST-SCALE END
7184 # Check if each vnf has exporter for metric collection if so update prometheus job records
7185 if scaling_type == "SCALE_OUT":
7186 if "exporters-endpoints" in db_vnfd.get("df")[0]:
7187 vnfr_id = db_vnfr["id"]
7188 db_vnfr = self.db.get_one("vnfrs", {"_id": vnfr_id})
7189 exporter_config = db_vnfd.get("df")[0].get("exporters-endpoints")
7190 self.logger.debug("exporter config :{}".format(exporter_config))
7191 artifact_path = "{}/{}/{}".format(
7192 base_folder["folder"],
7193 base_folder["pkg-dir"],
7194 "exporter-endpoint",
7195 )
7196 ee_id = None
7197 ee_config_descriptor = exporter_config
7198 rw_mgmt_ip = await self.wait_vm_up_insert_key_ro(
7199 logging_text,
7200 nsr_id,
7201 vnfr_id,
7202 vdu_id=db_vnfr["vdur"][-1]["vdu-id-ref"],
7203 vdu_index=db_vnfr["vdur"][-1]["count-index"],
7204 user=None,
7205 pub_key=None,
7206 )
7207 self.logger.debug("rw_mgmt_ip:{}".format(rw_mgmt_ip))
7208 self.logger.debug("Artifact_path:{}".format(artifact_path))
7209 vdu_id_for_prom = None
7210 vdu_index_for_prom = None
7211 for x in get_iterable(db_vnfr, "vdur"):
7212 vdu_id_for_prom = x.get("vdu-id-ref")
7213 vdu_index_for_prom = x.get("count-index")
7214 vnfr_id = vnfr_id + vdu_id + str(vdu_index)
7215 vnfr_id = vnfr_id.replace("_", "")
7216 prometheus_jobs = await self.extract_prometheus_scrape_jobs(
7217 ee_id=ee_id,
7218 artifact_path=artifact_path,
7219 ee_config_descriptor=ee_config_descriptor,
7220 vnfr_id=vnfr_id,
7221 nsr_id=nsr_id,
7222 target_ip=rw_mgmt_ip,
7223 element_type="VDU",
7224 vdu_id=vdu_id_for_prom,
7225 vdu_index=vdu_index_for_prom,
7226 )
7227
7228 self.logger.debug("Prometheus job:{}".format(prometheus_jobs))
7229 if prometheus_jobs:
7230 db_nsr_update[
7231 "_admin.deployed.prometheus_jobs"
7232 ] = prometheus_jobs
7233 self.update_db_2(
7234 "nsrs",
7235 nsr_id,
7236 db_nsr_update,
7237 )
7238
7239 for job in prometheus_jobs:
7240 self.db.set_one(
7241 "prometheus_jobs",
7242 {"job_name": ""},
7243 job,
7244 upsert=True,
7245 fail_on_empty=False,
7246 )
7247 db_nsr_update[
7248 "detailed-status"
7249 ] = "" # "scaled {} {}".format(scaling_group, scaling_type)
7250 db_nsr_update["operational-status"] = (
7251 "running"
7252 if old_operational_status == "failed"
7253 else old_operational_status
7254 )
7255 db_nsr_update["config-status"] = old_config_status
7256 return
7257 except (
7258 ROclient.ROClientException,
7259 DbException,
7260 LcmException,
7261 NgRoException,
7262 ) as e:
7263 self.logger.error(logging_text + "Exit Exception {}".format(e))
7264 exc = e
7265 except asyncio.CancelledError:
7266 self.logger.error(
7267 logging_text + "Cancelled Exception while '{}'".format(step)
7268 )
7269 exc = "Operation was cancelled"
7270 except Exception as e:
7271 exc = traceback.format_exc()
7272 self.logger.critical(
7273 logging_text + "Exit Exception {} {}".format(type(e).__name__, e),
7274 exc_info=True,
7275 )
7276 finally:
7277 error_list = list()
7278 if exc:
7279 error_list.append(str(exc))
7280 self._write_ns_status(
7281 nsr_id=nsr_id,
7282 ns_state=None,
7283 current_operation="IDLE",
7284 current_operation_id=None,
7285 )
7286 try:
7287 if tasks_dict_info:
7288 stage[1] = "Waiting for instantiate pending tasks."
7289 self.logger.debug(logging_text + stage[1])
7290 exc = await self._wait_for_tasks(
7291 logging_text,
7292 tasks_dict_info,
7293 self.timeout.ns_deploy,
7294 stage,
7295 nslcmop_id,
7296 nsr_id=nsr_id,
7297 )
7298 except asyncio.CancelledError:
7299 error_list.append("Cancelled")
7300 await self._cancel_pending_tasks(logging_text, tasks_dict_info)
7301 await self._wait_for_tasks(
7302 logging_text,
7303 tasks_dict_info,
7304 self.timeout.ns_deploy,
7305 stage,
7306 nslcmop_id,
7307 nsr_id=nsr_id,
7308 )
7309 if error_list:
7310 error_detail = "; ".join(error_list)
7311 db_nslcmop_update[
7312 "detailed-status"
7313 ] = error_description_nslcmop = "FAILED {}: {}".format(
7314 step, error_detail
7315 )
7316 nslcmop_operation_state = "FAILED"
7317 if db_nsr:
7318 db_nsr_update["operational-status"] = old_operational_status
7319 db_nsr_update["config-status"] = old_config_status
7320 db_nsr_update["detailed-status"] = ""
7321 if scale_process:
7322 if "VCA" in scale_process:
7323 db_nsr_update["config-status"] = "failed"
7324 if "RO" in scale_process:
7325 db_nsr_update["operational-status"] = "failed"
7326 db_nsr_update[
7327 "detailed-status"
7328 ] = "FAILED scaling nslcmop={} {}: {}".format(
7329 nslcmop_id, step, error_detail
7330 )
7331 else:
7332 error_description_nslcmop = None
7333 nslcmop_operation_state = "COMPLETED"
7334 db_nslcmop_update["detailed-status"] = "Done"
7335 if scaling_type == "SCALE_IN" and prom_job_name is not None:
7336 self.db.del_one(
7337 "prometheus_jobs",
7338 {"job_name": prom_job_name},
7339 fail_on_empty=False,
7340 )
7341
7342 self._write_op_status(
7343 op_id=nslcmop_id,
7344 stage="",
7345 error_message=error_description_nslcmop,
7346 operation_state=nslcmop_operation_state,
7347 other_update=db_nslcmop_update,
7348 )
7349 if db_nsr:
7350 self._write_ns_status(
7351 nsr_id=nsr_id,
7352 ns_state=None,
7353 current_operation="IDLE",
7354 current_operation_id=None,
7355 other_update=db_nsr_update,
7356 )
7357
7358 if nslcmop_operation_state:
7359 try:
7360 msg = {
7361 "nsr_id": nsr_id,
7362 "nslcmop_id": nslcmop_id,
7363 "operationState": nslcmop_operation_state,
7364 }
7365 await self.msg.aiowrite("ns", "scaled", msg)
7366 except Exception as e:
7367 self.logger.error(
7368 logging_text + "kafka_write notification Exception {}".format(e)
7369 )
7370 self.logger.debug(logging_text + "Exit")
7371 self.lcm_tasks.remove("ns", nsr_id, nslcmop_id, "ns_scale")
7372
7373 async def _scale_kdu(
7374 self, logging_text, nsr_id, nsr_deployed, db_vnfd, vca_id, scaling_info
7375 ):
7376 _scaling_info = scaling_info.get("kdu-create") or scaling_info.get("kdu-delete")
7377 for kdu_name in _scaling_info:
7378 for kdu_scaling_info in _scaling_info[kdu_name]:
7379 deployed_kdu, index = get_deployed_kdu(
7380 nsr_deployed, kdu_name, kdu_scaling_info["member-vnf-index"]
7381 )
7382 cluster_uuid = deployed_kdu["k8scluster-uuid"]
7383 kdu_instance = deployed_kdu["kdu-instance"]
7384 kdu_model = deployed_kdu.get("kdu-model")
7385 scale = int(kdu_scaling_info["scale"])
7386 k8s_cluster_type = kdu_scaling_info["k8s-cluster-type"]
7387
7388 db_dict = {
7389 "collection": "nsrs",
7390 "filter": {"_id": nsr_id},
7391 "path": "_admin.deployed.K8s.{}".format(index),
7392 }
7393
7394 step = "scaling application {}".format(
7395 kdu_scaling_info["resource-name"]
7396 )
7397 self.logger.debug(logging_text + step)
7398
7399 if kdu_scaling_info["type"] == "delete":
7400 kdu_config = get_configuration(db_vnfd, kdu_name)
7401 if (
7402 kdu_config
7403 and kdu_config.get("terminate-config-primitive")
7404 and get_juju_ee_ref(db_vnfd, kdu_name) is None
7405 ):
7406 terminate_config_primitive_list = kdu_config.get(
7407 "terminate-config-primitive"
7408 )
7409 terminate_config_primitive_list.sort(
7410 key=lambda val: int(val["seq"])
7411 )
7412
7413 for (
7414 terminate_config_primitive
7415 ) in terminate_config_primitive_list:
7416 primitive_params_ = self._map_primitive_params(
7417 terminate_config_primitive, {}, {}
7418 )
7419 step = "execute terminate config primitive"
7420 self.logger.debug(logging_text + step)
7421 await asyncio.wait_for(
7422 self.k8scluster_map[k8s_cluster_type].exec_primitive(
7423 cluster_uuid=cluster_uuid,
7424 kdu_instance=kdu_instance,
7425 primitive_name=terminate_config_primitive["name"],
7426 params=primitive_params_,
7427 db_dict=db_dict,
7428 total_timeout=self.timeout.primitive,
7429 vca_id=vca_id,
7430 ),
7431 timeout=self.timeout.primitive
7432 * self.timeout.primitive_outer_factor,
7433 )
7434
7435 await asyncio.wait_for(
7436 self.k8scluster_map[k8s_cluster_type].scale(
7437 kdu_instance=kdu_instance,
7438 scale=scale,
7439 resource_name=kdu_scaling_info["resource-name"],
7440 total_timeout=self.timeout.scale_on_error,
7441 vca_id=vca_id,
7442 cluster_uuid=cluster_uuid,
7443 kdu_model=kdu_model,
7444 atomic=True,
7445 db_dict=db_dict,
7446 ),
7447 timeout=self.timeout.scale_on_error
7448 * self.timeout.scale_on_error_outer_factor,
7449 )
7450
7451 if kdu_scaling_info["type"] == "create":
7452 kdu_config = get_configuration(db_vnfd, kdu_name)
7453 if (
7454 kdu_config
7455 and kdu_config.get("initial-config-primitive")
7456 and get_juju_ee_ref(db_vnfd, kdu_name) is None
7457 ):
7458 initial_config_primitive_list = kdu_config.get(
7459 "initial-config-primitive"
7460 )
7461 initial_config_primitive_list.sort(
7462 key=lambda val: int(val["seq"])
7463 )
7464
7465 for initial_config_primitive in initial_config_primitive_list:
7466 primitive_params_ = self._map_primitive_params(
7467 initial_config_primitive, {}, {}
7468 )
7469 step = "execute initial config primitive"
7470 self.logger.debug(logging_text + step)
7471 await asyncio.wait_for(
7472 self.k8scluster_map[k8s_cluster_type].exec_primitive(
7473 cluster_uuid=cluster_uuid,
7474 kdu_instance=kdu_instance,
7475 primitive_name=initial_config_primitive["name"],
7476 params=primitive_params_,
7477 db_dict=db_dict,
7478 vca_id=vca_id,
7479 ),
7480 timeout=600,
7481 )
7482
7483 async def _scale_ng_ro(
7484 self, logging_text, db_nsr, db_nslcmop, db_vnfr, vdu_scaling_info, stage
7485 ):
7486 nsr_id = db_nslcmop["nsInstanceId"]
7487 db_nsd = self.db.get_one("nsds", {"_id": db_nsr["nsd-id"]})
7488 db_vnfrs = {}
7489
7490 # read from db: vnfd's for every vnf
7491 db_vnfds = []
7492
7493 # for each vnf in ns, read vnfd
7494 for vnfr in self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id}):
7495 db_vnfrs[vnfr["member-vnf-index-ref"]] = vnfr
7496 vnfd_id = vnfr["vnfd-id"] # vnfd uuid for this vnf
7497 # if we haven't this vnfd, read it from db
7498 if not find_in_list(db_vnfds, lambda a_vnfd: a_vnfd["id"] == vnfd_id):
7499 # read from db
7500 vnfd = self.db.get_one("vnfds", {"_id": vnfd_id})
7501 db_vnfds.append(vnfd)
7502 n2vc_key = self.n2vc.get_public_key()
7503 n2vc_key_list = [n2vc_key]
7504 self.scale_vnfr(
7505 db_vnfr,
7506 vdu_scaling_info.get("vdu-create"),
7507 vdu_scaling_info.get("vdu-delete"),
7508 mark_delete=True,
7509 )
7510 # db_vnfr has been updated, update db_vnfrs to use it
7511 db_vnfrs[db_vnfr["member-vnf-index-ref"]] = db_vnfr
7512 await self._instantiate_ng_ro(
7513 logging_text,
7514 nsr_id,
7515 db_nsd,
7516 db_nsr,
7517 db_nslcmop,
7518 db_vnfrs,
7519 db_vnfds,
7520 n2vc_key_list,
7521 stage=stage,
7522 start_deploy=time(),
7523 timeout_ns_deploy=self.timeout.ns_deploy,
7524 )
7525 if vdu_scaling_info.get("vdu-delete"):
7526 self.scale_vnfr(
7527 db_vnfr, None, vdu_scaling_info["vdu-delete"], mark_delete=False
7528 )
7529
7530 async def extract_prometheus_scrape_jobs(
7531 self,
7532 ee_id: str,
7533 artifact_path: str,
7534 ee_config_descriptor: dict,
7535 vnfr_id: str,
7536 nsr_id: str,
7537 target_ip: str,
7538 element_type: str,
7539 vnf_member_index: str = "",
7540 vdu_id: str = "",
7541 vdu_index: int = None,
7542 kdu_name: str = "",
7543 kdu_index: int = None,
7544 ) -> dict:
7545 """Method to extract prometheus scrape jobs from EE's Prometheus template job file
7546 This method will wait until the corresponding VDU or KDU is fully instantiated
7547
7548 Args:
7549 ee_id (str): Execution Environment ID
7550 artifact_path (str): Path where the EE's content is (including the Prometheus template file)
7551 ee_config_descriptor (dict): Execution Environment's configuration descriptor
7552 vnfr_id (str): VNFR ID where this EE applies
7553 nsr_id (str): NSR ID where this EE applies
7554 target_ip (str): VDU/KDU instance IP address
7555 element_type (str): NS or VNF or VDU or KDU
7556 vnf_member_index (str, optional): VNF index where this EE applies. Defaults to "".
7557 vdu_id (str, optional): VDU ID where this EE applies. Defaults to "".
7558 vdu_index (int, optional): VDU index where this EE applies. Defaults to None.
7559 kdu_name (str, optional): KDU name where this EE applies. Defaults to "".
7560 kdu_index (int, optional): KDU index where this EE applies. Defaults to None.
7561
7562 Raises:
7563 LcmException: When the VDU or KDU instance was not found in an hour
7564
7565 Returns:
7566 _type_: Prometheus jobs
7567 """
7568 # default the vdur and kdur names to an empty string, to avoid any later
7569 # problem with Prometheus when the element type is not VDU or KDU
7570 vdur_name = ""
7571 kdur_name = ""
7572
7573 # look if exist a file called 'prometheus*.j2' and
7574 artifact_content = self.fs.dir_ls(artifact_path)
7575 job_file = next(
7576 (
7577 f
7578 for f in artifact_content
7579 if f.startswith("prometheus") and f.endswith(".j2")
7580 ),
7581 None,
7582 )
7583 if not job_file:
7584 return
7585 self.logger.debug("Artifact path{}".format(artifact_path))
7586 self.logger.debug("job file{}".format(job_file))
7587 with self.fs.file_open((artifact_path, job_file), "r") as f:
7588 job_data = f.read()
7589
7590 # obtain the VDUR or KDUR, if the element type is VDU or KDU
7591 if element_type in ("VDU", "KDU"):
7592 for _ in range(360):
7593 db_vnfr = self.db.get_one("vnfrs", {"_id": vnfr_id})
7594 if vdu_id and vdu_index is not None:
7595 vdur = next(
7596 (
7597 x
7598 for x in get_iterable(db_vnfr, "vdur")
7599 if (
7600 x.get("vdu-id-ref") == vdu_id
7601 and x.get("count-index") == vdu_index
7602 )
7603 ),
7604 {},
7605 )
7606 if vdur.get("name"):
7607 vdur_name = vdur.get("name")
7608 break
7609 if kdu_name and kdu_index is not None:
7610 kdur = next(
7611 (
7612 x
7613 for x in get_iterable(db_vnfr, "kdur")
7614 if (
7615 x.get("kdu-name") == kdu_name
7616 and x.get("count-index") == kdu_index
7617 )
7618 ),
7619 {},
7620 )
7621 if kdur.get("name"):
7622 kdur_name = kdur.get("name")
7623 break
7624
7625 await asyncio.sleep(10)
7626 else:
7627 if vdu_id and vdu_index is not None:
7628 raise LcmException(
7629 f"Timeout waiting VDU with name={vdu_id} and index={vdu_index} to be intantiated"
7630 )
7631 if kdu_name and kdu_index is not None:
7632 raise LcmException(
7633 f"Timeout waiting KDU with name={kdu_name} and index={kdu_index} to be intantiated"
7634 )
7635
7636 if ee_id is not None:
7637 _, namespace, helm_id = get_ee_id_parts(
7638 ee_id
7639 ) # get namespace and EE gRPC service name
7640 host_name = f'{helm_id}-{ee_config_descriptor["metric-service"]}.{namespace}.svc' # svc_name.namespace.svc
7641 host_port = "80"
7642 vnfr_id = vnfr_id.replace("-", "")
7643 variables = {
7644 "JOB_NAME": vnfr_id,
7645 "TARGET_IP": target_ip,
7646 "EXPORTER_POD_IP": host_name,
7647 "EXPORTER_POD_PORT": host_port,
7648 "NSR_ID": nsr_id,
7649 "VNF_MEMBER_INDEX": vnf_member_index,
7650 "VDUR_NAME": vdur_name,
7651 "KDUR_NAME": kdur_name,
7652 "ELEMENT_TYPE": element_type,
7653 }
7654 else:
7655 metric_path = ee_config_descriptor["metric-path"]
7656 target_port = ee_config_descriptor["metric-port"]
7657 vnfr_id = vnfr_id.replace("-", "")
7658 variables = {
7659 "JOB_NAME": vnfr_id,
7660 "TARGET_IP": target_ip,
7661 "TARGET_PORT": target_port,
7662 "METRIC_PATH": metric_path,
7663 }
7664
7665 job_list = parse_job(job_data, variables)
7666 # ensure job_name is using the vnfr_id. Adding the metadata nsr_id
7667 for job in job_list:
7668 if (
7669 not isinstance(job.get("job_name"), str)
7670 or vnfr_id not in job["job_name"]
7671 ):
7672 job["job_name"] = vnfr_id + "_" + str(SystemRandom().randint(1, 10000))
7673 job["nsr_id"] = nsr_id
7674 job["vnfr_id"] = vnfr_id
7675 return job_list
7676
7677 async def rebuild_start_stop(
7678 self, nsr_id, nslcmop_id, vnf_id, additional_param, operation_type
7679 ):
7680 logging_text = "Task ns={} {}={} ".format(nsr_id, operation_type, nslcmop_id)
7681 self.logger.info(logging_text + "Enter")
7682 stage = ["Preparing the environment", ""]
7683 # database nsrs record
7684 db_nsr_update = {}
7685 vdu_vim_name = None
7686 vim_vm_id = None
7687 # in case of error, indicates what part of scale was failed to put nsr at error status
7688 start_deploy = time()
7689 try:
7690 db_vnfr = self.db.get_one("vnfrs", {"_id": vnf_id})
7691 vim_account_id = db_vnfr.get("vim-account-id")
7692 vim_info_key = "vim:" + vim_account_id
7693 vdu_id = additional_param["vdu_id"]
7694 vdurs = [item for item in db_vnfr["vdur"] if item["vdu-id-ref"] == vdu_id]
7695 vdur = find_in_list(
7696 vdurs, lambda vdu: vdu["count-index"] == additional_param["count-index"]
7697 )
7698 if vdur:
7699 vdu_vim_name = vdur["name"]
7700 vim_vm_id = vdur["vim_info"][vim_info_key]["vim_id"]
7701 target_vim, _ = next(k_v for k_v in vdur["vim_info"].items())
7702 else:
7703 raise LcmException("Target vdu is not found")
7704 self.logger.info("vdu_vim_name >> {} ".format(vdu_vim_name))
7705 # wait for any previous tasks in process
7706 stage[1] = "Waiting for previous operations to terminate"
7707 self.logger.info(stage[1])
7708 await self.lcm_tasks.waitfor_related_HA("ns", "nslcmops", nslcmop_id)
7709
7710 stage[1] = "Reading from database."
7711 self.logger.info(stage[1])
7712 self._write_ns_status(
7713 nsr_id=nsr_id,
7714 ns_state=None,
7715 current_operation=operation_type.upper(),
7716 current_operation_id=nslcmop_id,
7717 )
7718 self._write_op_status(op_id=nslcmop_id, stage=stage, queuePosition=0)
7719
7720 # read from db: ns
7721 stage[1] = "Getting nsr={} from db.".format(nsr_id)
7722 db_nsr_update["operational-status"] = operation_type
7723 self.update_db_2("nsrs", nsr_id, db_nsr_update)
7724 # Payload for RO
7725 desc = {
7726 operation_type: {
7727 "vim_vm_id": vim_vm_id,
7728 "vnf_id": vnf_id,
7729 "vdu_index": additional_param["count-index"],
7730 "vdu_id": vdur["id"],
7731 "target_vim": target_vim,
7732 "vim_account_id": vim_account_id,
7733 }
7734 }
7735 stage[1] = "Sending rebuild request to RO... {}".format(desc)
7736 self._write_op_status(op_id=nslcmop_id, stage=stage, queuePosition=0)
7737 self.logger.info("ro nsr id: {}".format(nsr_id))
7738 result_dict = await self.RO.operate(nsr_id, desc, operation_type)
7739 self.logger.info("response from RO: {}".format(result_dict))
7740 action_id = result_dict["action_id"]
7741 await self._wait_ng_ro(
7742 nsr_id,
7743 action_id,
7744 nslcmop_id,
7745 start_deploy,
7746 self.timeout.operate,
7747 None,
7748 "start_stop_rebuild",
7749 )
7750 return "COMPLETED", "Done"
7751 except (ROclient.ROClientException, DbException, LcmException) as e:
7752 self.logger.error("Exit Exception {}".format(e))
7753 exc = e
7754 except asyncio.CancelledError:
7755 self.logger.error("Cancelled Exception while '{}'".format(stage))
7756 exc = "Operation was cancelled"
7757 except Exception as e:
7758 exc = traceback.format_exc()
7759 self.logger.critical(
7760 "Exit Exception {} {}".format(type(e).__name__, e), exc_info=True
7761 )
7762 return "FAILED", "Error in operate VNF {}".format(exc)
7763
7764 async def migrate(self, nsr_id, nslcmop_id):
7765 """
7766 Migrate VNFs and VDUs instances in a NS
7767
7768 :param: nsr_id: NS Instance ID
7769 :param: nslcmop_id: nslcmop ID of migrate
7770
7771 """
7772 # Try to lock HA task here
7773 task_is_locked_by_me = self.lcm_tasks.lock_HA("ns", "nslcmops", nslcmop_id)
7774 if not task_is_locked_by_me:
7775 return
7776 logging_text = "Task ns={} migrate ".format(nsr_id)
7777 self.logger.debug(logging_text + "Enter")
7778 # get all needed from database
7779 db_nslcmop = None
7780 db_nslcmop_update = {}
7781 nslcmop_operation_state = None
7782 db_nsr_update = {}
7783 target = {}
7784 exc = None
7785 # in case of error, indicates what part of scale was failed to put nsr at error status
7786 start_deploy = time()
7787
7788 try:
7789 # wait for any previous tasks in process
7790 step = "Waiting for previous operations to terminate"
7791 await self.lcm_tasks.waitfor_related_HA("ns", "nslcmops", nslcmop_id)
7792
7793 self._write_ns_status(
7794 nsr_id=nsr_id,
7795 ns_state=None,
7796 current_operation="MIGRATING",
7797 current_operation_id=nslcmop_id,
7798 )
7799 step = "Getting nslcmop from database"
7800 self.logger.debug(
7801 step + " after having waited for previous tasks to be completed"
7802 )
7803 db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
7804 migrate_params = db_nslcmop.get("operationParams")
7805
7806 target = {}
7807 target.update(migrate_params)
7808 desc = await self.RO.migrate(nsr_id, target)
7809 self.logger.debug("RO return > {}".format(desc))
7810 action_id = desc["action_id"]
7811 await self._wait_ng_ro(
7812 nsr_id,
7813 action_id,
7814 nslcmop_id,
7815 start_deploy,
7816 self.timeout.migrate,
7817 operation="migrate",
7818 )
7819 except (ROclient.ROClientException, DbException, LcmException) as e:
7820 self.logger.error("Exit Exception {}".format(e))
7821 exc = e
7822 except asyncio.CancelledError:
7823 self.logger.error("Cancelled Exception while '{}'".format(step))
7824 exc = "Operation was cancelled"
7825 except Exception as e:
7826 exc = traceback.format_exc()
7827 self.logger.critical(
7828 "Exit Exception {} {}".format(type(e).__name__, e), exc_info=True
7829 )
7830 finally:
7831 self._write_ns_status(
7832 nsr_id=nsr_id,
7833 ns_state=None,
7834 current_operation="IDLE",
7835 current_operation_id=None,
7836 )
7837 if exc:
7838 db_nslcmop_update["detailed-status"] = "FAILED {}: {}".format(step, exc)
7839 nslcmop_operation_state = "FAILED"
7840 else:
7841 nslcmop_operation_state = "COMPLETED"
7842 db_nslcmop_update["detailed-status"] = "Done"
7843 db_nsr_update["detailed-status"] = "Done"
7844
7845 self._write_op_status(
7846 op_id=nslcmop_id,
7847 stage="",
7848 error_message="",
7849 operation_state=nslcmop_operation_state,
7850 other_update=db_nslcmop_update,
7851 )
7852 if nslcmop_operation_state:
7853 try:
7854 msg = {
7855 "nsr_id": nsr_id,
7856 "nslcmop_id": nslcmop_id,
7857 "operationState": nslcmop_operation_state,
7858 }
7859 await self.msg.aiowrite("ns", "migrated", msg)
7860 except Exception as e:
7861 self.logger.error(
7862 logging_text + "kafka_write notification Exception {}".format(e)
7863 )
7864 self.logger.debug(logging_text + "Exit")
7865 self.lcm_tasks.remove("ns", nsr_id, nslcmop_id, "ns_migrate")
7866
7867 async def heal(self, nsr_id, nslcmop_id):
7868 """
7869 Heal NS
7870
7871 :param nsr_id: ns instance to heal
7872 :param nslcmop_id: operation to run
7873 :return:
7874 """
7875
7876 # Try to lock HA task here
7877 task_is_locked_by_me = self.lcm_tasks.lock_HA("ns", "nslcmops", nslcmop_id)
7878 if not task_is_locked_by_me:
7879 return
7880
7881 logging_text = "Task ns={} heal={} ".format(nsr_id, nslcmop_id)
7882 stage = ["", "", ""]
7883 tasks_dict_info = {}
7884 # ^ stage, step, VIM progress
7885 self.logger.debug(logging_text + "Enter")
7886 # get all needed from database
7887 db_nsr = None
7888 db_nslcmop_update = {}
7889 db_nsr_update = {}
7890 db_vnfrs = {} # vnf's info indexed by _id
7891 exc = None
7892 old_operational_status = ""
7893 old_config_status = ""
7894 nsi_id = None
7895 try:
7896 # wait for any previous tasks in process
7897 step = "Waiting for previous operations to terminate"
7898 await self.lcm_tasks.waitfor_related_HA("ns", "nslcmops", nslcmop_id)
7899 self._write_ns_status(
7900 nsr_id=nsr_id,
7901 ns_state=None,
7902 current_operation="HEALING",
7903 current_operation_id=nslcmop_id,
7904 )
7905
7906 step = "Getting nslcmop from database"
7907 self.logger.debug(
7908 step + " after having waited for previous tasks to be completed"
7909 )
7910 db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
7911
7912 step = "Getting nsr from database"
7913 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
7914 old_operational_status = db_nsr["operational-status"]
7915 old_config_status = db_nsr["config-status"]
7916
7917 db_nsr_update = {
7918 "_admin.deployed.RO.operational-status": "healing",
7919 }
7920 self.update_db_2("nsrs", nsr_id, db_nsr_update)
7921
7922 step = "Sending heal order to VIM"
7923 await self.heal_RO(
7924 logging_text=logging_text,
7925 nsr_id=nsr_id,
7926 db_nslcmop=db_nslcmop,
7927 stage=stage,
7928 )
7929 # VCA tasks
7930 # read from db: nsd
7931 stage[1] = "Getting nsd={} from db.".format(db_nsr["nsd-id"])
7932 self.logger.debug(logging_text + stage[1])
7933 nsd = self.db.get_one("nsds", {"_id": db_nsr["nsd-id"]})
7934 self.fs.sync(db_nsr["nsd-id"])
7935 db_nsr["nsd"] = nsd
7936 # read from db: vnfr's of this ns
7937 step = "Getting vnfrs from db"
7938 db_vnfrs_list = self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id})
7939 for vnfr in db_vnfrs_list:
7940 db_vnfrs[vnfr["_id"]] = vnfr
7941 self.logger.debug("ns.heal db_vnfrs={}".format(db_vnfrs))
7942
7943 # Check for each target VNF
7944 target_list = db_nslcmop.get("operationParams", {}).get("healVnfData", {})
7945 for target_vnf in target_list:
7946 # Find this VNF in the list from DB
7947 vnfr_id = target_vnf.get("vnfInstanceId", None)
7948 if vnfr_id:
7949 db_vnfr = db_vnfrs[vnfr_id]
7950 vnfd_id = db_vnfr.get("vnfd-id")
7951 vnfd_ref = db_vnfr.get("vnfd-ref")
7952 vnfd = self.db.get_one("vnfds", {"_id": vnfd_id})
7953 base_folder = vnfd["_admin"]["storage"]
7954 vdu_id = None
7955 vdu_index = 0
7956 vdu_name = None
7957 kdu_name = None
7958 nsi_id = None # TODO put nsi_id when this nsr belongs to a NSI
7959 member_vnf_index = db_vnfr.get("member-vnf-index-ref")
7960
7961 # Check each target VDU and deploy N2VC
7962 target_vdu_list = target_vnf.get("additionalParams", {}).get(
7963 "vdu", []
7964 )
7965 if not target_vdu_list:
7966 # Codigo nuevo para crear diccionario
7967 target_vdu_list = []
7968 for existing_vdu in db_vnfr.get("vdur"):
7969 vdu_name = existing_vdu.get("vdu-name", None)
7970 vdu_index = existing_vdu.get("count-index", 0)
7971 vdu_run_day1 = target_vnf.get("additionalParams", {}).get(
7972 "run-day1", False
7973 )
7974 vdu_to_be_healed = {
7975 "vdu-id": vdu_name,
7976 "count-index": vdu_index,
7977 "run-day1": vdu_run_day1,
7978 }
7979 target_vdu_list.append(vdu_to_be_healed)
7980 for target_vdu in target_vdu_list:
7981 deploy_params_vdu = target_vdu
7982 # Set run-day1 vnf level value if not vdu level value exists
7983 if not deploy_params_vdu.get("run-day1") and target_vnf.get(
7984 "additionalParams", {}
7985 ).get("run-day1"):
7986 deploy_params_vdu["run-day1"] = target_vnf[
7987 "additionalParams"
7988 ].get("run-day1")
7989 vdu_name = target_vdu.get("vdu-id", None)
7990 # TODO: Get vdu_id from vdud.
7991 vdu_id = vdu_name
7992 # For multi instance VDU count-index is mandatory
7993 # For single session VDU count-indes is 0
7994 vdu_index = target_vdu.get("count-index", 0)
7995
7996 # n2vc_redesign STEP 3 to 6 Deploy N2VC
7997 stage[1] = "Deploying Execution Environments."
7998 self.logger.debug(logging_text + stage[1])
7999
8000 # VNF Level charm. Normal case when proxy charms.
8001 # If target instance is management machine continue with actions: recreate EE for native charms or reinject juju key for proxy charms.
8002 descriptor_config = get_configuration(vnfd, vnfd_ref)
8003 if descriptor_config:
8004 # Continue if healed machine is management machine
8005 vnf_ip_address = db_vnfr.get("ip-address")
8006 target_instance = None
8007 for instance in db_vnfr.get("vdur", None):
8008 if (
8009 instance["vdu-name"] == vdu_name
8010 and instance["count-index"] == vdu_index
8011 ):
8012 target_instance = instance
8013 break
8014 if vnf_ip_address == target_instance.get("ip-address"):
8015 self._heal_n2vc(
8016 logging_text=logging_text
8017 + "member_vnf_index={}, vdu_name={}, vdu_index={} ".format(
8018 member_vnf_index, vdu_name, vdu_index
8019 ),
8020 db_nsr=db_nsr,
8021 db_vnfr=db_vnfr,
8022 nslcmop_id=nslcmop_id,
8023 nsr_id=nsr_id,
8024 nsi_id=nsi_id,
8025 vnfd_id=vnfd_ref,
8026 vdu_id=None,
8027 kdu_name=None,
8028 member_vnf_index=member_vnf_index,
8029 vdu_index=0,
8030 vdu_name=None,
8031 deploy_params=deploy_params_vdu,
8032 descriptor_config=descriptor_config,
8033 base_folder=base_folder,
8034 task_instantiation_info=tasks_dict_info,
8035 stage=stage,
8036 )
8037
8038 # VDU Level charm. Normal case with native charms.
8039 descriptor_config = get_configuration(vnfd, vdu_name)
8040 if descriptor_config:
8041 self._heal_n2vc(
8042 logging_text=logging_text
8043 + "member_vnf_index={}, vdu_name={}, vdu_index={} ".format(
8044 member_vnf_index, vdu_name, vdu_index
8045 ),
8046 db_nsr=db_nsr,
8047 db_vnfr=db_vnfr,
8048 nslcmop_id=nslcmop_id,
8049 nsr_id=nsr_id,
8050 nsi_id=nsi_id,
8051 vnfd_id=vnfd_ref,
8052 vdu_id=vdu_id,
8053 kdu_name=kdu_name,
8054 member_vnf_index=member_vnf_index,
8055 vdu_index=vdu_index,
8056 vdu_name=vdu_name,
8057 deploy_params=deploy_params_vdu,
8058 descriptor_config=descriptor_config,
8059 base_folder=base_folder,
8060 task_instantiation_info=tasks_dict_info,
8061 stage=stage,
8062 )
8063
8064 except (
8065 ROclient.ROClientException,
8066 DbException,
8067 LcmException,
8068 NgRoException,
8069 ) as e:
8070 self.logger.error(logging_text + "Exit Exception {}".format(e))
8071 exc = e
8072 except asyncio.CancelledError:
8073 self.logger.error(
8074 logging_text + "Cancelled Exception while '{}'".format(step)
8075 )
8076 exc = "Operation was cancelled"
8077 except Exception as e:
8078 exc = traceback.format_exc()
8079 self.logger.critical(
8080 logging_text + "Exit Exception {} {}".format(type(e).__name__, e),
8081 exc_info=True,
8082 )
8083 finally:
8084 error_list = list()
8085 if exc:
8086 error_list.append(str(exc))
8087 try:
8088 if tasks_dict_info:
8089 stage[1] = "Waiting for healing pending tasks."
8090 self.logger.debug(logging_text + stage[1])
8091 exc = await self._wait_for_tasks(
8092 logging_text,
8093 tasks_dict_info,
8094 self.timeout.ns_deploy,
8095 stage,
8096 nslcmop_id,
8097 nsr_id=nsr_id,
8098 )
8099 except asyncio.CancelledError:
8100 error_list.append("Cancelled")
8101 await self._cancel_pending_tasks(logging_text, tasks_dict_info)
8102 await self._wait_for_tasks(
8103 logging_text,
8104 tasks_dict_info,
8105 self.timeout.ns_deploy,
8106 stage,
8107 nslcmop_id,
8108 nsr_id=nsr_id,
8109 )
8110 if error_list:
8111 error_detail = "; ".join(error_list)
8112 db_nslcmop_update[
8113 "detailed-status"
8114 ] = error_description_nslcmop = "FAILED {}: {}".format(
8115 step, error_detail
8116 )
8117 nslcmop_operation_state = "FAILED"
8118 if db_nsr:
8119 db_nsr_update["operational-status"] = old_operational_status
8120 db_nsr_update["config-status"] = old_config_status
8121 db_nsr_update[
8122 "detailed-status"
8123 ] = "FAILED healing nslcmop={} {}: {}".format(
8124 nslcmop_id, step, error_detail
8125 )
8126 for task, task_name in tasks_dict_info.items():
8127 if not task.done() or task.cancelled() or task.exception():
8128 if task_name.startswith(self.task_name_deploy_vca):
8129 # A N2VC task is pending
8130 db_nsr_update["config-status"] = "failed"
8131 else:
8132 # RO task is pending
8133 db_nsr_update["operational-status"] = "failed"
8134 else:
8135 error_description_nslcmop = None
8136 nslcmop_operation_state = "COMPLETED"
8137 db_nslcmop_update["detailed-status"] = "Done"
8138 db_nsr_update["detailed-status"] = "Done"
8139 db_nsr_update["operational-status"] = "running"
8140 db_nsr_update["config-status"] = "configured"
8141
8142 self._write_op_status(
8143 op_id=nslcmop_id,
8144 stage="",
8145 error_message=error_description_nslcmop,
8146 operation_state=nslcmop_operation_state,
8147 other_update=db_nslcmop_update,
8148 )
8149 if db_nsr:
8150 self._write_ns_status(
8151 nsr_id=nsr_id,
8152 ns_state=None,
8153 current_operation="IDLE",
8154 current_operation_id=None,
8155 other_update=db_nsr_update,
8156 )
8157
8158 if nslcmop_operation_state:
8159 try:
8160 msg = {
8161 "nsr_id": nsr_id,
8162 "nslcmop_id": nslcmop_id,
8163 "operationState": nslcmop_operation_state,
8164 }
8165 await self.msg.aiowrite("ns", "healed", msg)
8166 except Exception as e:
8167 self.logger.error(
8168 logging_text + "kafka_write notification Exception {}".format(e)
8169 )
8170 self.logger.debug(logging_text + "Exit")
8171 self.lcm_tasks.remove("ns", nsr_id, nslcmop_id, "ns_heal")
8172
8173 async def heal_RO(
8174 self,
8175 logging_text,
8176 nsr_id,
8177 db_nslcmop,
8178 stage,
8179 ):
8180 """
8181 Heal at RO
8182 :param logging_text: preffix text to use at logging
8183 :param nsr_id: nsr identity
8184 :param db_nslcmop: database content of ns operation, in this case, 'instantiate'
8185 :param stage: list with 3 items: [general stage, tasks, vim_specific]. This task will write over vim_specific
8186 :return: None or exception
8187 """
8188
8189 def get_vim_account(vim_account_id):
8190 nonlocal db_vims
8191 if vim_account_id in db_vims:
8192 return db_vims[vim_account_id]
8193 db_vim = self.db.get_one("vim_accounts", {"_id": vim_account_id})
8194 db_vims[vim_account_id] = db_vim
8195 return db_vim
8196
8197 try:
8198 start_heal = time()
8199 ns_params = db_nslcmop.get("operationParams")
8200 if ns_params and ns_params.get("timeout_ns_heal"):
8201 timeout_ns_heal = ns_params["timeout_ns_heal"]
8202 else:
8203 timeout_ns_heal = self.timeout.ns_heal
8204
8205 db_vims = {}
8206
8207 nslcmop_id = db_nslcmop["_id"]
8208 target = {
8209 "action_id": nslcmop_id,
8210 }
8211 self.logger.warning(
8212 "db_nslcmop={} and timeout_ns_heal={}".format(
8213 db_nslcmop, timeout_ns_heal
8214 )
8215 )
8216 target.update(db_nslcmop.get("operationParams", {}))
8217
8218 self.logger.debug("Send to RO > nsr_id={} target={}".format(nsr_id, target))
8219 desc = await self.RO.recreate(nsr_id, target)
8220 self.logger.debug("RO return > {}".format(desc))
8221 action_id = desc["action_id"]
8222 # waits for RO to complete because Reinjecting juju key at ro can find VM in state Deleted
8223 await self._wait_ng_ro(
8224 nsr_id,
8225 action_id,
8226 nslcmop_id,
8227 start_heal,
8228 timeout_ns_heal,
8229 stage,
8230 operation="healing",
8231 )
8232
8233 # Updating NSR
8234 db_nsr_update = {
8235 "_admin.deployed.RO.operational-status": "running",
8236 "detailed-status": " ".join(stage),
8237 }
8238 self.update_db_2("nsrs", nsr_id, db_nsr_update)
8239 self._write_op_status(nslcmop_id, stage)
8240 self.logger.debug(
8241 logging_text + "ns healed at RO. RO_id={}".format(action_id)
8242 )
8243
8244 except Exception as e:
8245 stage[2] = "ERROR healing at VIM"
8246 # self.set_vnfr_at_error(db_vnfrs, str(e))
8247 self.logger.error(
8248 "Error healing at VIM {}".format(e),
8249 exc_info=not isinstance(
8250 e,
8251 (
8252 ROclient.ROClientException,
8253 LcmException,
8254 DbException,
8255 NgRoException,
8256 ),
8257 ),
8258 )
8259 raise
8260
8261 def _heal_n2vc(
8262 self,
8263 logging_text,
8264 db_nsr,
8265 db_vnfr,
8266 nslcmop_id,
8267 nsr_id,
8268 nsi_id,
8269 vnfd_id,
8270 vdu_id,
8271 kdu_name,
8272 member_vnf_index,
8273 vdu_index,
8274 vdu_name,
8275 deploy_params,
8276 descriptor_config,
8277 base_folder,
8278 task_instantiation_info,
8279 stage,
8280 ):
8281 # launch instantiate_N2VC in a asyncio task and register task object
8282 # Look where information of this charm is at database <nsrs>._admin.deployed.VCA
8283 # if not found, create one entry and update database
8284 # fill db_nsr._admin.deployed.VCA.<index>
8285
8286 self.logger.debug(
8287 logging_text + "_deploy_n2vc vnfd_id={}, vdu_id={}".format(vnfd_id, vdu_id)
8288 )
8289
8290 charm_name = ""
8291 get_charm_name = False
8292 if "execution-environment-list" in descriptor_config:
8293 ee_list = descriptor_config.get("execution-environment-list", [])
8294 elif "juju" in descriptor_config:
8295 ee_list = [descriptor_config] # ns charms
8296 if "execution-environment-list" not in descriptor_config:
8297 # charm name is only required for ns charms
8298 get_charm_name = True
8299 else: # other types as script are not supported
8300 ee_list = []
8301
8302 for ee_item in ee_list:
8303 self.logger.debug(
8304 logging_text
8305 + "_deploy_n2vc ee_item juju={}, helm={}".format(
8306 ee_item.get("juju"), ee_item.get("helm-chart")
8307 )
8308 )
8309 ee_descriptor_id = ee_item.get("id")
8310 if ee_item.get("juju"):
8311 vca_name = ee_item["juju"].get("charm")
8312 if get_charm_name:
8313 charm_name = self.find_charm_name(db_nsr, str(vca_name))
8314 vca_type = (
8315 "lxc_proxy_charm"
8316 if ee_item["juju"].get("charm") is not None
8317 else "native_charm"
8318 )
8319 if ee_item["juju"].get("cloud") == "k8s":
8320 vca_type = "k8s_proxy_charm"
8321 elif ee_item["juju"].get("proxy") is False:
8322 vca_type = "native_charm"
8323 elif ee_item.get("helm-chart"):
8324 vca_name = ee_item["helm-chart"]
8325 vca_type = "helm-v3"
8326 else:
8327 self.logger.debug(
8328 logging_text + "skipping non juju neither charm configuration"
8329 )
8330 continue
8331
8332 vca_index = -1
8333 for vca_index, vca_deployed in enumerate(
8334 db_nsr["_admin"]["deployed"]["VCA"]
8335 ):
8336 if not vca_deployed:
8337 continue
8338 if (
8339 vca_deployed.get("member-vnf-index") == member_vnf_index
8340 and vca_deployed.get("vdu_id") == vdu_id
8341 and vca_deployed.get("kdu_name") == kdu_name
8342 and vca_deployed.get("vdu_count_index", 0) == vdu_index
8343 and vca_deployed.get("ee_descriptor_id") == ee_descriptor_id
8344 ):
8345 break
8346 else:
8347 # not found, create one.
8348 target = (
8349 "ns" if not member_vnf_index else "vnf/{}".format(member_vnf_index)
8350 )
8351 if vdu_id:
8352 target += "/vdu/{}/{}".format(vdu_id, vdu_index or 0)
8353 elif kdu_name:
8354 target += "/kdu/{}".format(kdu_name)
8355 vca_deployed = {
8356 "target_element": target,
8357 # ^ target_element will replace member-vnf-index, kdu_name, vdu_id ... in a single string
8358 "member-vnf-index": member_vnf_index,
8359 "vdu_id": vdu_id,
8360 "kdu_name": kdu_name,
8361 "vdu_count_index": vdu_index,
8362 "operational-status": "init", # TODO revise
8363 "detailed-status": "", # TODO revise
8364 "step": "initial-deploy", # TODO revise
8365 "vnfd_id": vnfd_id,
8366 "vdu_name": vdu_name,
8367 "type": vca_type,
8368 "ee_descriptor_id": ee_descriptor_id,
8369 "charm_name": charm_name,
8370 }
8371 vca_index += 1
8372
8373 # create VCA and configurationStatus in db
8374 db_dict = {
8375 "_admin.deployed.VCA.{}".format(vca_index): vca_deployed,
8376 "configurationStatus.{}".format(vca_index): dict(),
8377 }
8378 self.update_db_2("nsrs", nsr_id, db_dict)
8379
8380 db_nsr["_admin"]["deployed"]["VCA"].append(vca_deployed)
8381
8382 self.logger.debug("N2VC > NSR_ID > {}".format(nsr_id))
8383 self.logger.debug("N2VC > DB_NSR > {}".format(db_nsr))
8384 self.logger.debug("N2VC > VCA_DEPLOYED > {}".format(vca_deployed))
8385
8386 # Launch task
8387 task_n2vc = asyncio.ensure_future(
8388 self.heal_N2VC(
8389 logging_text=logging_text,
8390 vca_index=vca_index,
8391 nsi_id=nsi_id,
8392 db_nsr=db_nsr,
8393 db_vnfr=db_vnfr,
8394 vdu_id=vdu_id,
8395 kdu_name=kdu_name,
8396 vdu_index=vdu_index,
8397 deploy_params=deploy_params,
8398 config_descriptor=descriptor_config,
8399 base_folder=base_folder,
8400 nslcmop_id=nslcmop_id,
8401 stage=stage,
8402 vca_type=vca_type,
8403 vca_name=vca_name,
8404 ee_config_descriptor=ee_item,
8405 )
8406 )
8407 self.lcm_tasks.register(
8408 "ns",
8409 nsr_id,
8410 nslcmop_id,
8411 "instantiate_N2VC-{}".format(vca_index),
8412 task_n2vc,
8413 )
8414 task_instantiation_info[
8415 task_n2vc
8416 ] = self.task_name_deploy_vca + " {}.{}".format(
8417 member_vnf_index or "", vdu_id or ""
8418 )
8419
8420 async def heal_N2VC(
8421 self,
8422 logging_text,
8423 vca_index,
8424 nsi_id,
8425 db_nsr,
8426 db_vnfr,
8427 vdu_id,
8428 kdu_name,
8429 vdu_index,
8430 config_descriptor,
8431 deploy_params,
8432 base_folder,
8433 nslcmop_id,
8434 stage,
8435 vca_type,
8436 vca_name,
8437 ee_config_descriptor,
8438 ):
8439 nsr_id = db_nsr["_id"]
8440 db_update_entry = "_admin.deployed.VCA.{}.".format(vca_index)
8441 vca_deployed_list = db_nsr["_admin"]["deployed"]["VCA"]
8442 vca_deployed = db_nsr["_admin"]["deployed"]["VCA"][vca_index]
8443 osm_config = {"osm": {"ns_id": db_nsr["_id"]}}
8444 db_dict = {
8445 "collection": "nsrs",
8446 "filter": {"_id": nsr_id},
8447 "path": db_update_entry,
8448 }
8449 step = ""
8450 try:
8451 element_type = "NS"
8452 element_under_configuration = nsr_id
8453
8454 vnfr_id = None
8455 if db_vnfr:
8456 vnfr_id = db_vnfr["_id"]
8457 osm_config["osm"]["vnf_id"] = vnfr_id
8458
8459 namespace = "{nsi}.{ns}".format(nsi=nsi_id if nsi_id else "", ns=nsr_id)
8460
8461 if vca_type == "native_charm":
8462 index_number = 0
8463 else:
8464 index_number = vdu_index or 0
8465
8466 if vnfr_id:
8467 element_type = "VNF"
8468 element_under_configuration = vnfr_id
8469 namespace += ".{}-{}".format(vnfr_id, index_number)
8470 if vdu_id:
8471 namespace += ".{}-{}".format(vdu_id, index_number)
8472 element_type = "VDU"
8473 element_under_configuration = "{}-{}".format(vdu_id, index_number)
8474 osm_config["osm"]["vdu_id"] = vdu_id
8475 elif kdu_name:
8476 namespace += ".{}".format(kdu_name)
8477 element_type = "KDU"
8478 element_under_configuration = kdu_name
8479 osm_config["osm"]["kdu_name"] = kdu_name
8480
8481 # Get artifact path
8482 if base_folder["pkg-dir"]:
8483 artifact_path = "{}/{}/{}/{}".format(
8484 base_folder["folder"],
8485 base_folder["pkg-dir"],
8486 "charms"
8487 if vca_type
8488 in ("native_charm", "lxc_proxy_charm", "k8s_proxy_charm")
8489 else "helm-charts",
8490 vca_name,
8491 )
8492 else:
8493 artifact_path = "{}/Scripts/{}/{}/".format(
8494 base_folder["folder"],
8495 "charms"
8496 if vca_type
8497 in ("native_charm", "lxc_proxy_charm", "k8s_proxy_charm")
8498 else "helm-charts",
8499 vca_name,
8500 )
8501
8502 self.logger.debug("Artifact path > {}".format(artifact_path))
8503
8504 # get initial_config_primitive_list that applies to this element
8505 initial_config_primitive_list = config_descriptor.get(
8506 "initial-config-primitive"
8507 )
8508
8509 self.logger.debug(
8510 "Initial config primitive list > {}".format(
8511 initial_config_primitive_list
8512 )
8513 )
8514
8515 # add config if not present for NS charm
8516 ee_descriptor_id = ee_config_descriptor.get("id")
8517 self.logger.debug("EE Descriptor > {}".format(ee_descriptor_id))
8518 initial_config_primitive_list = get_ee_sorted_initial_config_primitive_list(
8519 initial_config_primitive_list, vca_deployed, ee_descriptor_id
8520 )
8521
8522 self.logger.debug(
8523 "Initial config primitive list #2 > {}".format(
8524 initial_config_primitive_list
8525 )
8526 )
8527 # n2vc_redesign STEP 3.1
8528 # find old ee_id if exists
8529 ee_id = vca_deployed.get("ee_id")
8530
8531 vca_id = self.get_vca_id(db_vnfr, db_nsr)
8532 # create or register execution environment in VCA. Only for native charms when healing
8533 if vca_type == "native_charm":
8534 step = "Waiting to VM being up and getting IP address"
8535 self.logger.debug(logging_text + step)
8536 rw_mgmt_ip = await self.wait_vm_up_insert_key_ro(
8537 logging_text,
8538 nsr_id,
8539 vnfr_id,
8540 vdu_id,
8541 vdu_index,
8542 user=None,
8543 pub_key=None,
8544 )
8545 credentials = {"hostname": rw_mgmt_ip}
8546 # get username
8547 username = deep_get(
8548 config_descriptor, ("config-access", "ssh-access", "default-user")
8549 )
8550 # TODO remove this when changes on IM regarding config-access:ssh-access:default-user were
8551 # merged. Meanwhile let's get username from initial-config-primitive
8552 if not username and initial_config_primitive_list:
8553 for config_primitive in initial_config_primitive_list:
8554 for param in config_primitive.get("parameter", ()):
8555 if param["name"] == "ssh-username":
8556 username = param["value"]
8557 break
8558 if not username:
8559 raise LcmException(
8560 "Cannot determine the username neither with 'initial-config-primitive' nor with "
8561 "'config-access.ssh-access.default-user'"
8562 )
8563 credentials["username"] = username
8564
8565 # n2vc_redesign STEP 3.2
8566 # TODO: Before healing at RO it is needed to destroy native charm units to be deleted.
8567 self._write_configuration_status(
8568 nsr_id=nsr_id,
8569 vca_index=vca_index,
8570 status="REGISTERING",
8571 element_under_configuration=element_under_configuration,
8572 element_type=element_type,
8573 )
8574
8575 step = "register execution environment {}".format(credentials)
8576 self.logger.debug(logging_text + step)
8577 ee_id = await self.vca_map[vca_type].register_execution_environment(
8578 credentials=credentials,
8579 namespace=namespace,
8580 db_dict=db_dict,
8581 vca_id=vca_id,
8582 )
8583
8584 # update ee_id en db
8585 db_dict_ee_id = {
8586 "_admin.deployed.VCA.{}.ee_id".format(vca_index): ee_id,
8587 }
8588 self.update_db_2("nsrs", nsr_id, db_dict_ee_id)
8589
8590 # for compatibility with MON/POL modules, the need model and application name at database
8591 # TODO ask MON/POL if needed to not assuming anymore the format "model_name.application_name"
8592 # Not sure if this need to be done when healing
8593 """
8594 ee_id_parts = ee_id.split(".")
8595 db_nsr_update = {db_update_entry + "ee_id": ee_id}
8596 if len(ee_id_parts) >= 2:
8597 model_name = ee_id_parts[0]
8598 application_name = ee_id_parts[1]
8599 db_nsr_update[db_update_entry + "model"] = model_name
8600 db_nsr_update[db_update_entry + "application"] = application_name
8601 """
8602
8603 # n2vc_redesign STEP 3.3
8604 # Install configuration software. Only for native charms.
8605 step = "Install configuration Software"
8606
8607 self._write_configuration_status(
8608 nsr_id=nsr_id,
8609 vca_index=vca_index,
8610 status="INSTALLING SW",
8611 element_under_configuration=element_under_configuration,
8612 element_type=element_type,
8613 # other_update=db_nsr_update,
8614 other_update=None,
8615 )
8616
8617 # TODO check if already done
8618 self.logger.debug(logging_text + step)
8619 config = None
8620 if vca_type == "native_charm":
8621 config_primitive = next(
8622 (p for p in initial_config_primitive_list if p["name"] == "config"),
8623 None,
8624 )
8625 if config_primitive:
8626 config = self._map_primitive_params(
8627 config_primitive, {}, deploy_params
8628 )
8629 await self.vca_map[vca_type].install_configuration_sw(
8630 ee_id=ee_id,
8631 artifact_path=artifact_path,
8632 db_dict=db_dict,
8633 config=config,
8634 num_units=1,
8635 vca_id=vca_id,
8636 vca_type=vca_type,
8637 )
8638
8639 # write in db flag of configuration_sw already installed
8640 self.update_db_2(
8641 "nsrs", nsr_id, {db_update_entry + "config_sw_installed": True}
8642 )
8643
8644 # Not sure if this need to be done when healing
8645 """
8646 # add relations for this VCA (wait for other peers related with this VCA)
8647 await self._add_vca_relations(
8648 logging_text=logging_text,
8649 nsr_id=nsr_id,
8650 vca_type=vca_type,
8651 vca_index=vca_index,
8652 )
8653 """
8654
8655 # if SSH access is required, then get execution environment SSH public
8656 # if native charm we have waited already to VM be UP
8657 if vca_type in ("k8s_proxy_charm", "lxc_proxy_charm", "helm-v3"):
8658 pub_key = None
8659 user = None
8660 # self.logger.debug("get ssh key block")
8661 if deep_get(
8662 config_descriptor, ("config-access", "ssh-access", "required")
8663 ):
8664 # self.logger.debug("ssh key needed")
8665 # Needed to inject a ssh key
8666 user = deep_get(
8667 config_descriptor,
8668 ("config-access", "ssh-access", "default-user"),
8669 )
8670 step = "Install configuration Software, getting public ssh key"
8671 pub_key = await self.vca_map[vca_type].get_ee_ssh_public__key(
8672 ee_id=ee_id, db_dict=db_dict, vca_id=vca_id
8673 )
8674
8675 step = "Insert public key into VM user={} ssh_key={}".format(
8676 user, pub_key
8677 )
8678 else:
8679 # self.logger.debug("no need to get ssh key")
8680 step = "Waiting to VM being up and getting IP address"
8681 self.logger.debug(logging_text + step)
8682
8683 # n2vc_redesign STEP 5.1
8684 # wait for RO (ip-address) Insert pub_key into VM
8685 # IMPORTANT: We need do wait for RO to complete healing operation.
8686 await self._wait_heal_ro(nsr_id, self.timeout.ns_heal)
8687 if vnfr_id:
8688 if kdu_name:
8689 rw_mgmt_ip = await self.wait_kdu_up(
8690 logging_text, nsr_id, vnfr_id, kdu_name
8691 )
8692 else:
8693 rw_mgmt_ip = await self.wait_vm_up_insert_key_ro(
8694 logging_text,
8695 nsr_id,
8696 vnfr_id,
8697 vdu_id,
8698 vdu_index,
8699 user=user,
8700 pub_key=pub_key,
8701 )
8702 else:
8703 rw_mgmt_ip = None # This is for a NS configuration
8704
8705 self.logger.debug(logging_text + " VM_ip_address={}".format(rw_mgmt_ip))
8706
8707 # store rw_mgmt_ip in deploy params for later replacement
8708 deploy_params["rw_mgmt_ip"] = rw_mgmt_ip
8709
8710 # Day1 operations.
8711 # get run-day1 operation parameter
8712 runDay1 = deploy_params.get("run-day1", False)
8713 self.logger.debug(
8714 "Healing vnf={}, vdu={}, runDay1 ={}".format(vnfr_id, vdu_id, runDay1)
8715 )
8716 if runDay1:
8717 # n2vc_redesign STEP 6 Execute initial config primitive
8718 step = "execute initial config primitive"
8719
8720 # wait for dependent primitives execution (NS -> VNF -> VDU)
8721 if initial_config_primitive_list:
8722 await self._wait_dependent_n2vc(
8723 nsr_id, vca_deployed_list, vca_index
8724 )
8725
8726 # stage, in function of element type: vdu, kdu, vnf or ns
8727 my_vca = vca_deployed_list[vca_index]
8728 if my_vca.get("vdu_id") or my_vca.get("kdu_name"):
8729 # VDU or KDU
8730 stage[0] = "Stage 3/5: running Day-1 primitives for VDU."
8731 elif my_vca.get("member-vnf-index"):
8732 # VNF
8733 stage[0] = "Stage 4/5: running Day-1 primitives for VNF."
8734 else:
8735 # NS
8736 stage[0] = "Stage 5/5: running Day-1 primitives for NS."
8737
8738 self._write_configuration_status(
8739 nsr_id=nsr_id, vca_index=vca_index, status="EXECUTING PRIMITIVE"
8740 )
8741
8742 self._write_op_status(op_id=nslcmop_id, stage=stage)
8743
8744 check_if_terminated_needed = True
8745 for initial_config_primitive in initial_config_primitive_list:
8746 # adding information on the vca_deployed if it is a NS execution environment
8747 if not vca_deployed["member-vnf-index"]:
8748 deploy_params["ns_config_info"] = json.dumps(
8749 self._get_ns_config_info(nsr_id)
8750 )
8751 # TODO check if already done
8752 primitive_params_ = self._map_primitive_params(
8753 initial_config_primitive, {}, deploy_params
8754 )
8755
8756 step = "execute primitive '{}' params '{}'".format(
8757 initial_config_primitive["name"], primitive_params_
8758 )
8759 self.logger.debug(logging_text + step)
8760 await self.vca_map[vca_type].exec_primitive(
8761 ee_id=ee_id,
8762 primitive_name=initial_config_primitive["name"],
8763 params_dict=primitive_params_,
8764 db_dict=db_dict,
8765 vca_id=vca_id,
8766 vca_type=vca_type,
8767 )
8768 # Once some primitive has been exec, check and write at db if it needs to exec terminated primitives
8769 if check_if_terminated_needed:
8770 if config_descriptor.get("terminate-config-primitive"):
8771 self.update_db_2(
8772 "nsrs",
8773 nsr_id,
8774 {db_update_entry + "needed_terminate": True},
8775 )
8776 check_if_terminated_needed = False
8777
8778 # TODO register in database that primitive is done
8779
8780 # STEP 7 Configure metrics
8781 # Not sure if this need to be done when healing
8782 """
8783 if vca_type == "helm" or vca_type == "helm-v3":
8784 prometheus_jobs = await self.extract_prometheus_scrape_jobs(
8785 ee_id=ee_id,
8786 artifact_path=artifact_path,
8787 ee_config_descriptor=ee_config_descriptor,
8788 vnfr_id=vnfr_id,
8789 nsr_id=nsr_id,
8790 target_ip=rw_mgmt_ip,
8791 )
8792 if prometheus_jobs:
8793 self.update_db_2(
8794 "nsrs",
8795 nsr_id,
8796 {db_update_entry + "prometheus_jobs": prometheus_jobs},
8797 )
8798
8799 for job in prometheus_jobs:
8800 self.db.set_one(
8801 "prometheus_jobs",
8802 {"job_name": job["job_name"]},
8803 job,
8804 upsert=True,
8805 fail_on_empty=False,
8806 )
8807
8808 """
8809 step = "instantiated at VCA"
8810 self.logger.debug(logging_text + step)
8811
8812 self._write_configuration_status(
8813 nsr_id=nsr_id, vca_index=vca_index, status="READY"
8814 )
8815
8816 except Exception as e: # TODO not use Exception but N2VC exception
8817 # self.update_db_2("nsrs", nsr_id, {db_update_entry + "instantiation": "FAILED"})
8818 if not isinstance(
8819 e, (DbException, N2VCException, LcmException, asyncio.CancelledError)
8820 ):
8821 self.logger.error(
8822 "Exception while {} : {}".format(step, e), exc_info=True
8823 )
8824 self._write_configuration_status(
8825 nsr_id=nsr_id, vca_index=vca_index, status="BROKEN"
8826 )
8827 raise LcmException("{} {}".format(step, e)) from e
8828
8829 async def _wait_heal_ro(
8830 self,
8831 nsr_id,
8832 timeout=600,
8833 ):
8834 start_time = time()
8835 while time() <= start_time + timeout:
8836 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
8837 operational_status_ro = db_nsr["_admin"]["deployed"]["RO"][
8838 "operational-status"
8839 ]
8840 self.logger.debug("Wait Heal RO > {}".format(operational_status_ro))
8841 if operational_status_ro != "healing":
8842 break
8843 await asyncio.sleep(15)
8844 else: # timeout_ns_deploy
8845 raise NgRoException("Timeout waiting ns to deploy")
8846
8847 async def vertical_scale(self, nsr_id, nslcmop_id):
8848 """
8849 Vertical Scale the VDUs in a NS
8850
8851 :param: nsr_id: NS Instance ID
8852 :param: nslcmop_id: nslcmop ID of migrate
8853
8854 """
8855 # Try to lock HA task here
8856 task_is_locked_by_me = self.lcm_tasks.lock_HA("ns", "nslcmops", nslcmop_id)
8857 if not task_is_locked_by_me:
8858 return
8859 logging_text = "Task ns={} vertical scale ".format(nsr_id)
8860 self.logger.debug(logging_text + "Enter")
8861 # get all needed from database
8862 db_nslcmop = None
8863 db_nslcmop_update = {}
8864 nslcmop_operation_state = None
8865 old_db_update = {}
8866 q_filter = {}
8867 old_vdu_index = None
8868 old_flavor_id = None
8869 db_nsr_update = {}
8870 target = {}
8871 exc = None
8872 # in case of error, indicates what part of scale was failed to put nsr at error status
8873 start_deploy = time()
8874
8875 try:
8876 # wait for any previous tasks in process
8877 step = "Waiting for previous operations to terminate"
8878 await self.lcm_tasks.waitfor_related_HA("ns", "nslcmops", nslcmop_id)
8879
8880 self._write_ns_status(
8881 nsr_id=nsr_id,
8882 ns_state=None,
8883 current_operation="VerticalScale",
8884 current_operation_id=nslcmop_id,
8885 )
8886 step = "Getting nslcmop from database"
8887 self.logger.debug(
8888 step + " after having waited for previous tasks to be completed"
8889 )
8890 db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
8891 operationParams = db_nslcmop.get("operationParams")
8892 # Update the VNFRS and NSRS with the requested flavour detail, So that ro tasks can function properly
8893 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
8894 db_flavor = db_nsr.get("flavor")
8895 db_flavor_index = str(len(db_flavor))
8896 change_vnf_flavor_data = operationParams["changeVnfFlavorData"]
8897 flavor_dict = change_vnf_flavor_data["additionalParams"]
8898 count_index = flavor_dict["vduCountIndex"]
8899 vdu_id_ref = flavor_dict["vduid"]
8900 flavor_dict_update = {
8901 "id": db_flavor_index,
8902 "memory-mb": flavor_dict["virtualMemory"],
8903 "name": f"{vdu_id_ref}-{count_index}-flv",
8904 "storage-gb": flavor_dict["sizeOfStorage"],
8905 "vcpu-count": flavor_dict["numVirtualCpu"],
8906 }
8907 db_flavor.append(flavor_dict_update)
8908 db_update = {}
8909 db_update["flavor"] = db_flavor
8910 ns_q_filter = {
8911 "_id": nsr_id,
8912 }
8913 self.db.set_one(
8914 "nsrs",
8915 q_filter=ns_q_filter,
8916 update_dict=db_update,
8917 fail_on_empty=True,
8918 )
8919 db_vnfr = self.db.get_one(
8920 "vnfrs", {"_id": change_vnf_flavor_data["vnfInstanceId"]}
8921 )
8922 for vdu_index, vdur in enumerate(db_vnfr.get("vdur", ())):
8923 if (
8924 vdur.get("count-index") == count_index
8925 and vdur.get("vdu-id-ref") == vdu_id_ref
8926 ):
8927 old_flavor_id = vdur.get("ns-flavor-id", 0)
8928 old_vdu_index = vdu_index
8929 filter_text = {
8930 "_id": change_vnf_flavor_data["vnfInstanceId"],
8931 "vdur.count-index": count_index,
8932 "vdur.vdu-id-ref": vdu_id_ref,
8933 }
8934 q_filter.update(filter_text)
8935 db_update = {}
8936 db_update[
8937 "vdur.{}.ns-flavor-id".format(vdu_index)
8938 ] = db_flavor_index
8939 self.db.set_one(
8940 "vnfrs",
8941 q_filter=q_filter,
8942 update_dict=db_update,
8943 fail_on_empty=True,
8944 )
8945 target = {}
8946 target.update(operationParams)
8947 desc = await self.RO.vertical_scale(nsr_id, target)
8948 self.logger.debug("RO return > {}".format(desc))
8949 action_id = desc["action_id"]
8950 await self._wait_ng_ro(
8951 nsr_id,
8952 action_id,
8953 nslcmop_id,
8954 start_deploy,
8955 self.timeout.verticalscale,
8956 operation="verticalscale",
8957 )
8958 except (
8959 NgRoException,
8960 ROclient.ROClientException,
8961 DbException,
8962 LcmException,
8963 ) as e:
8964 self.logger.error("Exit Exception {}".format(e))
8965 exc = e
8966 except asyncio.CancelledError:
8967 self.logger.error("Cancelled Exception while '{}'".format(step))
8968 exc = "Operation was cancelled"
8969 except Exception as e:
8970 exc = traceback.format_exc()
8971 self.logger.critical(
8972 "Exit Exception {} {}".format(type(e).__name__, e), exc_info=True
8973 )
8974 finally:
8975 self._write_ns_status(
8976 nsr_id=nsr_id,
8977 ns_state=None,
8978 current_operation="IDLE",
8979 current_operation_id=None,
8980 )
8981 if exc:
8982 db_nslcmop_update["detailed-status"] = "FAILED {}: {}".format(step, exc)
8983 nslcmop_operation_state = "FAILED"
8984 old_db_update[
8985 "vdur.{}.ns-flavor-id".format(old_vdu_index)
8986 ] = old_flavor_id
8987 else:
8988 nslcmop_operation_state = "COMPLETED"
8989 db_nslcmop_update["detailed-status"] = "Done"
8990 db_nsr_update["detailed-status"] = "Done"
8991
8992 self._write_op_status(
8993 op_id=nslcmop_id,
8994 stage="",
8995 error_message="",
8996 operation_state=nslcmop_operation_state,
8997 other_update=db_nslcmop_update,
8998 )
8999 if old_vdu_index and old_db_update != {}:
9000 self.logger.critical(
9001 "Reverting Old Flavor -- : {}".format(old_db_update)
9002 )
9003 self.db.set_one(
9004 "vnfrs",
9005 q_filter=q_filter,
9006 update_dict=old_db_update,
9007 fail_on_empty=True,
9008 )
9009 if nslcmop_operation_state:
9010 try:
9011 msg = {
9012 "nsr_id": nsr_id,
9013 "nslcmop_id": nslcmop_id,
9014 "operationState": nslcmop_operation_state,
9015 }
9016 await self.msg.aiowrite("ns", "verticalscaled", msg)
9017 except Exception as e:
9018 self.logger.error(
9019 logging_text + "kafka_write notification Exception {}".format(e)
9020 )
9021 self.logger.debug(logging_text + "Exit")
9022 self.lcm_tasks.remove("ns", nsr_id, nslcmop_id, "ns_verticalscale")