blob: 5c606654e5822d97eeab00b580195df09d8f8db0 [file] [log] [blame]
tierno59d22d22018-09-25 18:10:19 +02001# -*- coding: utf-8 -*-
2
tierno2e215512018-11-28 09:37:52 +00003##
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
tierno59d22d22018-09-25 18:10:19 +020019import asyncio
20import yaml
21import logging
22import logging.handlers
tierno59d22d22018-09-25 18:10:19 +020023import traceback
David Garciad4816682019-12-09 14:57:43 +010024import json
gcalvino35be9152018-12-20 09:33:12 +010025from jinja2 import Environment, Template, meta, TemplateError, TemplateNotFound, TemplateSyntaxError
tierno59d22d22018-09-25 18:10:19 +020026
tierno77677d92019-08-22 13:46:35 +000027from osm_lcm import ROclient
tierno744303e2020-01-13 16:46:31 +000028from osm_lcm.lcm_utils import LcmException, LcmExceptionNoMgmtIP, LcmBase, deep_get, get_iterable, populate_dict
calvinosanch9f9c6f22019-11-04 13:37:39 +010029from n2vc.k8s_helm_conn import K8sHelmConnector
Adam Israelbaacc302019-12-01 12:41:39 -050030from n2vc.k8s_juju_conn import K8sJujuConnector
tierno59d22d22018-09-25 18:10:19 +020031
tierno27246d82018-09-27 15:59:09 +020032from osm_common.dbbase import DbException
tierno59d22d22018-09-25 18:10:19 +020033from osm_common.fsbase import FsException
quilesj7e13aeb2019-10-08 13:34:55 +020034
35from n2vc.n2vc_juju_conn import N2VCJujuConnector
tierno055a7ea2019-12-04 21:25:38 +000036from n2vc.exceptions import N2VCException
tierno59d22d22018-09-25 18:10:19 +020037
tierno27246d82018-09-27 15:59:09 +020038from copy import copy, deepcopy
tierno59d22d22018-09-25 18:10:19 +020039from http import HTTPStatus
40from time import time
tierno27246d82018-09-27 15:59:09 +020041from uuid import uuid4
tierno59d22d22018-09-25 18:10:19 +020042
43__author__ = "Alfonso Tierno"
44
45
46class NsLcm(LcmBase):
tierno63de62e2018-10-31 16:38:52 +010047 timeout_vca_on_error = 5 * 60 # Time for charm from first time at blocked,error status to mark as failed
tierno744303e2020-01-13 16:46:31 +000048 timeout_ns_deploy = 2 * 3600 # default global timeout for deployment a ns
tiernoe876f672020-02-13 14:34:48 +000049 timeout_ns_terminate = 1800 # default global timeout for un deployment a ns
garciadeblasf9b04952019-04-09 18:53:58 +020050 timeout_charm_delete = 10 * 60
51 timeout_primitive = 10 * 60 # timeout for primitive execution
tierno59d22d22018-09-25 18:10:19 +020052
kuuseac3a8882019-10-03 10:48:06 +020053 SUBOPERATION_STATUS_NOT_FOUND = -1
54 SUBOPERATION_STATUS_NEW = -2
55 SUBOPERATION_STATUS_SKIP = -3
tiernoa2143262020-03-27 16:20:40 +000056 task_name_deploy_vca = "Deploying VCA"
kuuseac3a8882019-10-03 10:48:06 +020057
tierno744303e2020-01-13 16:46:31 +000058 def __init__(self, db, msg, fs, lcm_tasks, config, loop):
tierno59d22d22018-09-25 18:10:19 +020059 """
60 Init, Connect to database, filesystem storage, and messaging
61 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
62 :return: None
63 """
quilesj7e13aeb2019-10-08 13:34:55 +020064 super().__init__(
65 db=db,
66 msg=msg,
67 fs=fs,
68 logger=logging.getLogger('lcm.ns')
69 )
70
tierno59d22d22018-09-25 18:10:19 +020071 self.loop = loop
72 self.lcm_tasks = lcm_tasks
tierno744303e2020-01-13 16:46:31 +000073 self.timeout = config["timeout"]
74 self.ro_config = config["ro_config"]
75 self.vca_config = config["VCA"].copy()
tierno59d22d22018-09-25 18:10:19 +020076
quilesj7e13aeb2019-10-08 13:34:55 +020077 # create N2VC connector
78 self.n2vc = N2VCJujuConnector(
79 db=self.db,
80 fs=self.fs,
tierno59d22d22018-09-25 18:10:19 +020081 log=self.logger,
quilesj7e13aeb2019-10-08 13:34:55 +020082 loop=self.loop,
83 url='{}:{}'.format(self.vca_config['host'], self.vca_config['port']),
84 username=self.vca_config.get('user', None),
85 vca_config=self.vca_config,
quilesj3655ae02019-12-12 16:08:35 +000086 on_update_db=self._on_update_n2vc_db
tierno59d22d22018-09-25 18:10:19 +020087 )
quilesj7e13aeb2019-10-08 13:34:55 +020088
calvinosanch9f9c6f22019-11-04 13:37:39 +010089 self.k8sclusterhelm = K8sHelmConnector(
90 kubectl_command=self.vca_config.get("kubectlpath"),
91 helm_command=self.vca_config.get("helmpath"),
92 fs=self.fs,
93 log=self.logger,
94 db=self.db,
95 on_update_db=None,
96 )
97
Adam Israelbaacc302019-12-01 12:41:39 -050098 self.k8sclusterjuju = K8sJujuConnector(
99 kubectl_command=self.vca_config.get("kubectlpath"),
100 juju_command=self.vca_config.get("jujupath"),
101 fs=self.fs,
102 log=self.logger,
103 db=self.db,
104 on_update_db=None,
105 )
106
tiernoa2143262020-03-27 16:20:40 +0000107 self.k8scluster_map = {
108 "helm-chart": self.k8sclusterhelm,
109 "chart": self.k8sclusterhelm,
110 "juju-bundle": self.k8sclusterjuju,
111 "juju": self.k8sclusterjuju,
112 }
quilesj7e13aeb2019-10-08 13:34:55 +0200113 # create RO client
tierno77677d92019-08-22 13:46:35 +0000114 self.RO = ROclient.ROClient(self.loop, **self.ro_config)
tierno59d22d22018-09-25 18:10:19 +0200115
quilesj3655ae02019-12-12 16:08:35 +0000116 def _on_update_ro_db(self, nsrs_id, ro_descriptor):
quilesj7e13aeb2019-10-08 13:34:55 +0200117
quilesj3655ae02019-12-12 16:08:35 +0000118 # self.logger.debug('_on_update_ro_db(nsrs_id={}'.format(nsrs_id))
119
120 try:
121 # TODO filter RO descriptor fields...
122
123 # write to database
124 db_dict = dict()
125 # db_dict['deploymentStatus'] = yaml.dump(ro_descriptor, default_flow_style=False, indent=2)
126 db_dict['deploymentStatus'] = ro_descriptor
127 self.update_db_2("nsrs", nsrs_id, db_dict)
128
129 except Exception as e:
130 self.logger.warn('Cannot write database RO deployment for ns={} -> {}'.format(nsrs_id, e))
131
132 async def _on_update_n2vc_db(self, table, filter, path, updated_data):
133
quilesj69a722c2020-01-09 08:30:17 +0000134 # remove last dot from path (if exists)
135 if path.endswith('.'):
136 path = path[:-1]
137
quilesj3655ae02019-12-12 16:08:35 +0000138 # self.logger.debug('_on_update_n2vc_db(table={}, filter={}, path={}, updated_data={}'
139 # .format(table, filter, path, updated_data))
140
141 try:
142
143 nsr_id = filter.get('_id')
144
145 # read ns record from database
146 nsr = self.db.get_one(table='nsrs', q_filter=filter)
147 current_ns_status = nsr.get('nsState')
148
149 # get vca status for NS
quilesj69a722c2020-01-09 08:30:17 +0000150 status_dict = await self.n2vc.get_status(namespace='.' + nsr_id, yaml_format=False)
quilesj3655ae02019-12-12 16:08:35 +0000151
152 # vcaStatus
153 db_dict = dict()
154 db_dict['vcaStatus'] = status_dict
155
156 # update configurationStatus for this VCA
157 try:
158 vca_index = int(path[path.rfind(".")+1:])
159
160 vca_list = deep_get(target_dict=nsr, key_list=('_admin', 'deployed', 'VCA'))
161 vca_status = vca_list[vca_index].get('status')
162
163 configuration_status_list = nsr.get('configurationStatus')
164 config_status = configuration_status_list[vca_index].get('status')
165
166 if config_status == 'BROKEN' and vca_status != 'failed':
167 db_dict['configurationStatus'][vca_index] = 'READY'
168 elif config_status != 'BROKEN' and vca_status == 'failed':
169 db_dict['configurationStatus'][vca_index] = 'BROKEN'
170 except Exception as e:
171 # not update configurationStatus
172 self.logger.debug('Error updating vca_index (ignore): {}'.format(e))
173
174 # if nsState = 'READY' check if juju is reporting some error => nsState = 'DEGRADED'
175 # if nsState = 'DEGRADED' check if all is OK
176 is_degraded = False
177 if current_ns_status in ('READY', 'DEGRADED'):
178 error_description = ''
179 # check machines
180 if status_dict.get('machines'):
181 for machine_id in status_dict.get('machines'):
182 machine = status_dict.get('machines').get(machine_id)
183 # check machine agent-status
184 if machine.get('agent-status'):
185 s = machine.get('agent-status').get('status')
186 if s != 'started':
187 is_degraded = True
188 error_description += 'machine {} agent-status={} ; '.format(machine_id, s)
189 # check machine instance status
190 if machine.get('instance-status'):
191 s = machine.get('instance-status').get('status')
192 if s != 'running':
193 is_degraded = True
194 error_description += 'machine {} instance-status={} ; '.format(machine_id, s)
195 # check applications
196 if status_dict.get('applications'):
197 for app_id in status_dict.get('applications'):
198 app = status_dict.get('applications').get(app_id)
199 # check application status
200 if app.get('status'):
201 s = app.get('status').get('status')
202 if s != 'active':
203 is_degraded = True
204 error_description += 'application {} status={} ; '.format(app_id, s)
205
206 if error_description:
207 db_dict['errorDescription'] = error_description
208 if current_ns_status == 'READY' and is_degraded:
209 db_dict['nsState'] = 'DEGRADED'
210 if current_ns_status == 'DEGRADED' and not is_degraded:
211 db_dict['nsState'] = 'READY'
212
213 # write to database
214 self.update_db_2("nsrs", nsr_id, db_dict)
215
216 except Exception as e:
217 self.logger.warn('Error updating NS state for ns={}: {}'.format(nsr_id, e))
quilesj7e13aeb2019-10-08 13:34:55 +0200218
calvinosanch9f9c6f22019-11-04 13:37:39 +0100219 return
quilesj7e13aeb2019-10-08 13:34:55 +0200220
gcalvino35be9152018-12-20 09:33:12 +0100221 def vnfd2RO(self, vnfd, new_id=None, additionalParams=None, nsrId=None):
tierno59d22d22018-09-25 18:10:19 +0200222 """
223 Converts creates a new vnfd descriptor for RO base on input OSM IM vnfd
224 :param vnfd: input vnfd
225 :param new_id: overrides vnf id if provided
tierno8a518872018-12-21 13:42:14 +0000226 :param additionalParams: Instantiation params for VNFs provided
gcalvino35be9152018-12-20 09:33:12 +0100227 :param nsrId: Id of the NSR
tierno59d22d22018-09-25 18:10:19 +0200228 :return: copy of vnfd
229 """
tierno59d22d22018-09-25 18:10:19 +0200230 try:
231 vnfd_RO = deepcopy(vnfd)
tierno8a518872018-12-21 13:42:14 +0000232 # remove unused by RO configuration, monitoring, scaling and internal keys
tierno59d22d22018-09-25 18:10:19 +0200233 vnfd_RO.pop("_id", None)
234 vnfd_RO.pop("_admin", None)
tierno8a518872018-12-21 13:42:14 +0000235 vnfd_RO.pop("vnf-configuration", None)
236 vnfd_RO.pop("monitoring-param", None)
237 vnfd_RO.pop("scaling-group-descriptor", None)
calvinosanch9f9c6f22019-11-04 13:37:39 +0100238 vnfd_RO.pop("kdu", None)
239 vnfd_RO.pop("k8s-cluster", None)
tierno59d22d22018-09-25 18:10:19 +0200240 if new_id:
241 vnfd_RO["id"] = new_id
tierno8a518872018-12-21 13:42:14 +0000242
243 # parse cloud-init or cloud-init-file with the provided variables using Jinja2
244 for vdu in get_iterable(vnfd_RO, "vdu"):
245 cloud_init_file = None
246 if vdu.get("cloud-init-file"):
tierno59d22d22018-09-25 18:10:19 +0200247 base_folder = vnfd["_admin"]["storage"]
gcalvino35be9152018-12-20 09:33:12 +0100248 cloud_init_file = "{}/{}/cloud_init/{}".format(base_folder["folder"], base_folder["pkg-dir"],
249 vdu["cloud-init-file"])
250 with self.fs.file_open(cloud_init_file, "r") as ci_file:
251 cloud_init_content = ci_file.read()
tierno59d22d22018-09-25 18:10:19 +0200252 vdu.pop("cloud-init-file", None)
tierno8a518872018-12-21 13:42:14 +0000253 elif vdu.get("cloud-init"):
gcalvino35be9152018-12-20 09:33:12 +0100254 cloud_init_content = vdu["cloud-init"]
tierno8a518872018-12-21 13:42:14 +0000255 else:
256 continue
257
258 env = Environment()
259 ast = env.parse(cloud_init_content)
260 mandatory_vars = meta.find_undeclared_variables(ast)
261 if mandatory_vars:
262 for var in mandatory_vars:
263 if not additionalParams or var not in additionalParams.keys():
264 raise LcmException("Variable '{}' defined at vnfd[id={}]:vdu[id={}]:cloud-init/cloud-init-"
265 "file, must be provided in the instantiation parameters inside the "
266 "'additionalParamsForVnf' block".format(var, vnfd["id"], vdu["id"]))
267 template = Template(cloud_init_content)
tierno2b611dd2019-01-11 10:30:57 +0000268 cloud_init_content = template.render(additionalParams or {})
gcalvino35be9152018-12-20 09:33:12 +0100269 vdu["cloud-init"] = cloud_init_content
tierno8a518872018-12-21 13:42:14 +0000270
tierno59d22d22018-09-25 18:10:19 +0200271 return vnfd_RO
272 except FsException as e:
tierno8a518872018-12-21 13:42:14 +0000273 raise LcmException("Error reading vnfd[id={}]:vdu[id={}]:cloud-init-file={}: {}".
tiernoda964822019-01-14 15:53:47 +0000274 format(vnfd["id"], vdu["id"], cloud_init_file, e))
tierno8a518872018-12-21 13:42:14 +0000275 except (TemplateError, TemplateNotFound, TemplateSyntaxError) as e:
276 raise LcmException("Error parsing Jinja2 to cloud-init content at vnfd[id={}]:vdu[id={}]: {}".
277 format(vnfd["id"], vdu["id"], e))
tierno59d22d22018-09-25 18:10:19 +0200278
tierno27246d82018-09-27 15:59:09 +0200279 def ns_params_2_RO(self, ns_params, nsd, vnfd_dict, n2vc_key_list):
tierno59d22d22018-09-25 18:10:19 +0200280 """
tierno27246d82018-09-27 15:59:09 +0200281 Creates a RO ns descriptor from OSM ns_instantiate params
tierno59d22d22018-09-25 18:10:19 +0200282 :param ns_params: OSM instantiate params
283 :return: The RO ns descriptor
284 """
285 vim_2_RO = {}
tiernob7f3f0d2019-03-20 17:17:21 +0000286 wim_2_RO = {}
tierno27246d82018-09-27 15:59:09 +0200287 # TODO feature 1417: Check that no instantiation is set over PDU
288 # check if PDU forces a concrete vim-network-id and add it
289 # check if PDU contains a SDN-assist info (dpid, switch, port) and pass it to RO
tierno59d22d22018-09-25 18:10:19 +0200290
291 def vim_account_2_RO(vim_account):
292 if vim_account in vim_2_RO:
293 return vim_2_RO[vim_account]
294
295 db_vim = self.db.get_one("vim_accounts", {"_id": vim_account})
296 if db_vim["_admin"]["operationalState"] != "ENABLED":
297 raise LcmException("VIM={} is not available. operationalState={}".format(
298 vim_account, db_vim["_admin"]["operationalState"]))
299 RO_vim_id = db_vim["_admin"]["deployed"]["RO"]
300 vim_2_RO[vim_account] = RO_vim_id
301 return RO_vim_id
302
tiernob7f3f0d2019-03-20 17:17:21 +0000303 def wim_account_2_RO(wim_account):
304 if isinstance(wim_account, str):
305 if wim_account in wim_2_RO:
306 return wim_2_RO[wim_account]
307
308 db_wim = self.db.get_one("wim_accounts", {"_id": wim_account})
309 if db_wim["_admin"]["operationalState"] != "ENABLED":
310 raise LcmException("WIM={} is not available. operationalState={}".format(
311 wim_account, db_wim["_admin"]["operationalState"]))
312 RO_wim_id = db_wim["_admin"]["deployed"]["RO-account"]
313 wim_2_RO[wim_account] = RO_wim_id
314 return RO_wim_id
315 else:
316 return wim_account
317
tierno59d22d22018-09-25 18:10:19 +0200318 def ip_profile_2_RO(ip_profile):
319 RO_ip_profile = deepcopy((ip_profile))
320 if "dns-server" in RO_ip_profile:
321 if isinstance(RO_ip_profile["dns-server"], list):
322 RO_ip_profile["dns-address"] = []
323 for ds in RO_ip_profile.pop("dns-server"):
324 RO_ip_profile["dns-address"].append(ds['address'])
325 else:
326 RO_ip_profile["dns-address"] = RO_ip_profile.pop("dns-server")
327 if RO_ip_profile.get("ip-version") == "ipv4":
328 RO_ip_profile["ip-version"] = "IPv4"
329 if RO_ip_profile.get("ip-version") == "ipv6":
330 RO_ip_profile["ip-version"] = "IPv6"
331 if "dhcp-params" in RO_ip_profile:
332 RO_ip_profile["dhcp"] = RO_ip_profile.pop("dhcp-params")
333 return RO_ip_profile
334
335 if not ns_params:
336 return None
337 RO_ns_params = {
338 # "name": ns_params["nsName"],
339 # "description": ns_params.get("nsDescription"),
340 "datacenter": vim_account_2_RO(ns_params["vimAccountId"]),
tiernob7f3f0d2019-03-20 17:17:21 +0000341 "wim_account": wim_account_2_RO(ns_params.get("wimAccountId")),
tierno59d22d22018-09-25 18:10:19 +0200342 # "scenario": ns_params["nsdId"],
tierno59d22d22018-09-25 18:10:19 +0200343 }
quilesj7e13aeb2019-10-08 13:34:55 +0200344
tiernoe64f7fb2019-09-11 08:55:52 +0000345 n2vc_key_list = n2vc_key_list or []
346 for vnfd_ref, vnfd in vnfd_dict.items():
347 vdu_needed_access = []
348 mgmt_cp = None
349 if vnfd.get("vnf-configuration"):
tierno6cf25f52019-09-12 09:33:40 +0000350 ssh_required = deep_get(vnfd, ("vnf-configuration", "config-access", "ssh-access", "required"))
tiernoe64f7fb2019-09-11 08:55:52 +0000351 if ssh_required and vnfd.get("mgmt-interface"):
352 if vnfd["mgmt-interface"].get("vdu-id"):
353 vdu_needed_access.append(vnfd["mgmt-interface"]["vdu-id"])
354 elif vnfd["mgmt-interface"].get("cp"):
355 mgmt_cp = vnfd["mgmt-interface"]["cp"]
tierno27246d82018-09-27 15:59:09 +0200356
tiernoe64f7fb2019-09-11 08:55:52 +0000357 for vdu in vnfd.get("vdu", ()):
358 if vdu.get("vdu-configuration"):
tierno6cf25f52019-09-12 09:33:40 +0000359 ssh_required = deep_get(vdu, ("vdu-configuration", "config-access", "ssh-access", "required"))
tiernoe64f7fb2019-09-11 08:55:52 +0000360 if ssh_required:
tierno27246d82018-09-27 15:59:09 +0200361 vdu_needed_access.append(vdu["id"])
tiernoe64f7fb2019-09-11 08:55:52 +0000362 elif mgmt_cp:
363 for vdu_interface in vdu.get("interface"):
364 if vdu_interface.get("external-connection-point-ref") and \
365 vdu_interface["external-connection-point-ref"] == mgmt_cp:
366 vdu_needed_access.append(vdu["id"])
367 mgmt_cp = None
368 break
tierno27246d82018-09-27 15:59:09 +0200369
tiernoe64f7fb2019-09-11 08:55:52 +0000370 if vdu_needed_access:
371 for vnf_member in nsd.get("constituent-vnfd"):
372 if vnf_member["vnfd-id-ref"] != vnfd_ref:
373 continue
374 for vdu in vdu_needed_access:
375 populate_dict(RO_ns_params,
376 ("vnfs", vnf_member["member-vnf-index"], "vdus", vdu, "mgmt_keys"),
377 n2vc_key_list)
tierno27246d82018-09-27 15:59:09 +0200378
tierno25ec7732018-10-24 18:47:11 +0200379 if ns_params.get("vduImage"):
380 RO_ns_params["vduImage"] = ns_params["vduImage"]
381
tiernoc255a822018-10-31 09:41:53 +0100382 if ns_params.get("ssh_keys"):
383 RO_ns_params["cloud-config"] = {"key-pairs": ns_params["ssh_keys"]}
tierno27246d82018-09-27 15:59:09 +0200384 for vnf_params in get_iterable(ns_params, "vnf"):
385 for constituent_vnfd in nsd["constituent-vnfd"]:
386 if constituent_vnfd["member-vnf-index"] == vnf_params["member-vnf-index"]:
387 vnf_descriptor = vnfd_dict[constituent_vnfd["vnfd-id-ref"]]
388 break
389 else:
390 raise LcmException("Invalid instantiate parameter vnf:member-vnf-index={} is not present at nsd:"
391 "constituent-vnfd".format(vnf_params["member-vnf-index"]))
392 if vnf_params.get("vimAccountId"):
393 populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "datacenter"),
394 vim_account_2_RO(vnf_params["vimAccountId"]))
tierno59d22d22018-09-25 18:10:19 +0200395
tierno27246d82018-09-27 15:59:09 +0200396 for vdu_params in get_iterable(vnf_params, "vdu"):
397 # TODO feature 1417: check that this VDU exist and it is not a PDU
398 if vdu_params.get("volume"):
399 for volume_params in vdu_params["volume"]:
400 if volume_params.get("vim-volume-id"):
401 populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "vdus",
402 vdu_params["id"], "devices", volume_params["name"], "vim_id"),
403 volume_params["vim-volume-id"])
404 if vdu_params.get("interface"):
405 for interface_params in vdu_params["interface"]:
406 if interface_params.get("ip-address"):
407 populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "vdus",
408 vdu_params["id"], "interfaces", interface_params["name"],
409 "ip_address"),
410 interface_params["ip-address"])
411 if interface_params.get("mac-address"):
412 populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "vdus",
413 vdu_params["id"], "interfaces", interface_params["name"],
414 "mac_address"),
415 interface_params["mac-address"])
416 if interface_params.get("floating-ip-required"):
417 populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "vdus",
418 vdu_params["id"], "interfaces", interface_params["name"],
419 "floating-ip"),
420 interface_params["floating-ip-required"])
421
422 for internal_vld_params in get_iterable(vnf_params, "internal-vld"):
423 if internal_vld_params.get("vim-network-name"):
424 populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "networks",
425 internal_vld_params["name"], "vim-network-name"),
426 internal_vld_params["vim-network-name"])
gcalvino0d7ac8d2018-12-17 16:24:08 +0100427 if internal_vld_params.get("vim-network-id"):
428 populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "networks",
429 internal_vld_params["name"], "vim-network-id"),
430 internal_vld_params["vim-network-id"])
tierno27246d82018-09-27 15:59:09 +0200431 if internal_vld_params.get("ip-profile"):
432 populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "networks",
433 internal_vld_params["name"], "ip-profile"),
434 ip_profile_2_RO(internal_vld_params["ip-profile"]))
kbsub4d761eb2019-10-17 16:28:48 +0000435 if internal_vld_params.get("provider-network"):
436
437 populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "networks",
438 internal_vld_params["name"], "provider-network"),
439 internal_vld_params["provider-network"].copy())
tierno27246d82018-09-27 15:59:09 +0200440
441 for icp_params in get_iterable(internal_vld_params, "internal-connection-point"):
442 # look for interface
443 iface_found = False
444 for vdu_descriptor in vnf_descriptor["vdu"]:
445 for vdu_interface in vdu_descriptor["interface"]:
446 if vdu_interface.get("internal-connection-point-ref") == icp_params["id-ref"]:
447 if icp_params.get("ip-address"):
448 populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "vdus",
449 vdu_descriptor["id"], "interfaces",
450 vdu_interface["name"], "ip_address"),
451 icp_params["ip-address"])
452
453 if icp_params.get("mac-address"):
454 populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "vdus",
455 vdu_descriptor["id"], "interfaces",
456 vdu_interface["name"], "mac_address"),
457 icp_params["mac-address"])
458 iface_found = True
tierno59d22d22018-09-25 18:10:19 +0200459 break
tierno27246d82018-09-27 15:59:09 +0200460 if iface_found:
461 break
462 else:
463 raise LcmException("Invalid instantiate parameter vnf:member-vnf-index[{}]:"
464 "internal-vld:id-ref={} is not present at vnfd:internal-"
465 "connection-point".format(vnf_params["member-vnf-index"],
466 icp_params["id-ref"]))
467
468 for vld_params in get_iterable(ns_params, "vld"):
469 if "ip-profile" in vld_params:
470 populate_dict(RO_ns_params, ("networks", vld_params["name"], "ip-profile"),
471 ip_profile_2_RO(vld_params["ip-profile"]))
tiernob7f3f0d2019-03-20 17:17:21 +0000472
kbsub4d761eb2019-10-17 16:28:48 +0000473 if vld_params.get("provider-network"):
474
475 populate_dict(RO_ns_params, ("networks", vld_params["name"], "provider-network"),
476 vld_params["provider-network"].copy())
477
tiernob7f3f0d2019-03-20 17:17:21 +0000478 if "wimAccountId" in vld_params and vld_params["wimAccountId"] is not None:
479 populate_dict(RO_ns_params, ("networks", vld_params["name"], "wim_account"),
480 wim_account_2_RO(vld_params["wimAccountId"])),
tierno27246d82018-09-27 15:59:09 +0200481 if vld_params.get("vim-network-name"):
482 RO_vld_sites = []
483 if isinstance(vld_params["vim-network-name"], dict):
484 for vim_account, vim_net in vld_params["vim-network-name"].items():
485 RO_vld_sites.append({
486 "netmap-use": vim_net,
487 "datacenter": vim_account_2_RO(vim_account)
488 })
489 else: # isinstance str
490 RO_vld_sites.append({"netmap-use": vld_params["vim-network-name"]})
491 if RO_vld_sites:
492 populate_dict(RO_ns_params, ("networks", vld_params["name"], "sites"), RO_vld_sites)
kbsub4d761eb2019-10-17 16:28:48 +0000493
gcalvino0d7ac8d2018-12-17 16:24:08 +0100494 if vld_params.get("vim-network-id"):
495 RO_vld_sites = []
496 if isinstance(vld_params["vim-network-id"], dict):
497 for vim_account, vim_net in vld_params["vim-network-id"].items():
498 RO_vld_sites.append({
499 "netmap-use": vim_net,
500 "datacenter": vim_account_2_RO(vim_account)
501 })
502 else: # isinstance str
503 RO_vld_sites.append({"netmap-use": vld_params["vim-network-id"]})
504 if RO_vld_sites:
505 populate_dict(RO_ns_params, ("networks", vld_params["name"], "sites"), RO_vld_sites)
Felipe Vicens720b07a2019-01-31 02:32:09 +0100506 if vld_params.get("ns-net"):
507 if isinstance(vld_params["ns-net"], dict):
508 for vld_id, instance_scenario_id in vld_params["ns-net"].items():
509 RO_vld_ns_net = {"instance_scenario_id": instance_scenario_id, "osm_id": vld_id}
Felipe Vicensb0e5fe42019-12-05 10:30:38 +0100510 populate_dict(RO_ns_params, ("networks", vld_params["name"], "use-network"), RO_vld_ns_net)
tierno27246d82018-09-27 15:59:09 +0200511 if "vnfd-connection-point-ref" in vld_params:
512 for cp_params in vld_params["vnfd-connection-point-ref"]:
513 # look for interface
514 for constituent_vnfd in nsd["constituent-vnfd"]:
515 if constituent_vnfd["member-vnf-index"] == cp_params["member-vnf-index-ref"]:
516 vnf_descriptor = vnfd_dict[constituent_vnfd["vnfd-id-ref"]]
517 break
518 else:
519 raise LcmException(
520 "Invalid instantiate parameter vld:vnfd-connection-point-ref:member-vnf-index-ref={} "
521 "is not present at nsd:constituent-vnfd".format(cp_params["member-vnf-index-ref"]))
522 match_cp = False
523 for vdu_descriptor in vnf_descriptor["vdu"]:
524 for interface_descriptor in vdu_descriptor["interface"]:
525 if interface_descriptor.get("external-connection-point-ref") == \
526 cp_params["vnfd-connection-point-ref"]:
527 match_cp = True
tierno59d22d22018-09-25 18:10:19 +0200528 break
tierno27246d82018-09-27 15:59:09 +0200529 if match_cp:
530 break
531 else:
532 raise LcmException(
533 "Invalid instantiate parameter vld:vnfd-connection-point-ref:member-vnf-index-ref={}:"
534 "vnfd-connection-point-ref={} is not present at vnfd={}".format(
535 cp_params["member-vnf-index-ref"],
536 cp_params["vnfd-connection-point-ref"],
537 vnf_descriptor["id"]))
538 if cp_params.get("ip-address"):
539 populate_dict(RO_ns_params, ("vnfs", cp_params["member-vnf-index-ref"], "vdus",
540 vdu_descriptor["id"], "interfaces",
541 interface_descriptor["name"], "ip_address"),
542 cp_params["ip-address"])
543 if cp_params.get("mac-address"):
544 populate_dict(RO_ns_params, ("vnfs", cp_params["member-vnf-index-ref"], "vdus",
545 vdu_descriptor["id"], "interfaces",
546 interface_descriptor["name"], "mac_address"),
547 cp_params["mac-address"])
tierno59d22d22018-09-25 18:10:19 +0200548 return RO_ns_params
549
tierno27246d82018-09-27 15:59:09 +0200550 def scale_vnfr(self, db_vnfr, vdu_create=None, vdu_delete=None):
551 # make a copy to do not change
552 vdu_create = copy(vdu_create)
553 vdu_delete = copy(vdu_delete)
554
555 vdurs = db_vnfr.get("vdur")
556 if vdurs is None:
557 vdurs = []
558 vdu_index = len(vdurs)
559 while vdu_index:
560 vdu_index -= 1
561 vdur = vdurs[vdu_index]
562 if vdur.get("pdu-type"):
563 continue
564 vdu_id_ref = vdur["vdu-id-ref"]
565 if vdu_create and vdu_create.get(vdu_id_ref):
566 for index in range(0, vdu_create[vdu_id_ref]):
567 vdur = deepcopy(vdur)
568 vdur["_id"] = str(uuid4())
569 vdur["count-index"] += 1
570 vdurs.insert(vdu_index+1+index, vdur)
571 del vdu_create[vdu_id_ref]
572 if vdu_delete and vdu_delete.get(vdu_id_ref):
573 del vdurs[vdu_index]
574 vdu_delete[vdu_id_ref] -= 1
575 if not vdu_delete[vdu_id_ref]:
576 del vdu_delete[vdu_id_ref]
577 # check all operations are done
578 if vdu_create or vdu_delete:
579 raise LcmException("Error scaling OUT VNFR for {}. There is not any existing vnfr. Scaled to 0?".format(
580 vdu_create))
581 if vdu_delete:
582 raise LcmException("Error scaling IN VNFR for {}. There is not any existing vnfr. Scaled to 0?".format(
583 vdu_delete))
584
585 vnfr_update = {"vdur": vdurs}
586 db_vnfr["vdur"] = vdurs
587 self.update_db_2("vnfrs", db_vnfr["_id"], vnfr_update)
588
tiernof578e552018-11-08 19:07:20 +0100589 def ns_update_nsr(self, ns_update_nsr, db_nsr, nsr_desc_RO):
590 """
591 Updates database nsr with the RO info for the created vld
592 :param ns_update_nsr: dictionary to be filled with the updated info
593 :param db_nsr: content of db_nsr. This is also modified
594 :param nsr_desc_RO: nsr descriptor from RO
595 :return: Nothing, LcmException is raised on errors
596 """
597
598 for vld_index, vld in enumerate(get_iterable(db_nsr, "vld")):
599 for net_RO in get_iterable(nsr_desc_RO, "nets"):
600 if vld["id"] != net_RO.get("ns_net_osm_id"):
601 continue
602 vld["vim-id"] = net_RO.get("vim_net_id")
603 vld["name"] = net_RO.get("vim_name")
604 vld["status"] = net_RO.get("status")
605 vld["status-detailed"] = net_RO.get("error_msg")
606 ns_update_nsr["vld.{}".format(vld_index)] = vld
607 break
608 else:
609 raise LcmException("ns_update_nsr: Not found vld={} at RO info".format(vld["id"]))
610
tiernoe876f672020-02-13 14:34:48 +0000611 def set_vnfr_at_error(self, db_vnfrs, error_text):
612 try:
613 for db_vnfr in db_vnfrs.values():
614 vnfr_update = {"status": "ERROR"}
615 for vdu_index, vdur in enumerate(get_iterable(db_vnfr, "vdur")):
616 if "status" not in vdur:
617 vdur["status"] = "ERROR"
618 vnfr_update["vdur.{}.status".format(vdu_index)] = "ERROR"
619 if error_text:
620 vdur["status-detailed"] = str(error_text)
621 vnfr_update["vdur.{}.status-detailed".format(vdu_index)] = "ERROR"
622 self.update_db_2("vnfrs", db_vnfr["_id"], vnfr_update)
623 except DbException as e:
624 self.logger.error("Cannot update vnf. {}".format(e))
625
tierno59d22d22018-09-25 18:10:19 +0200626 def ns_update_vnfr(self, db_vnfrs, nsr_desc_RO):
627 """
628 Updates database vnfr with the RO info, e.g. ip_address, vim_id... Descriptor db_vnfrs is also updated
tierno27246d82018-09-27 15:59:09 +0200629 :param db_vnfrs: dictionary with member-vnf-index: vnfr-content
630 :param nsr_desc_RO: nsr descriptor from RO
631 :return: Nothing, LcmException is raised on errors
tierno59d22d22018-09-25 18:10:19 +0200632 """
633 for vnf_index, db_vnfr in db_vnfrs.items():
634 for vnf_RO in nsr_desc_RO["vnfs"]:
tierno27246d82018-09-27 15:59:09 +0200635 if vnf_RO["member_vnf_index"] != vnf_index:
636 continue
637 vnfr_update = {}
tiernof578e552018-11-08 19:07:20 +0100638 if vnf_RO.get("ip_address"):
tierno1674de82019-04-09 13:03:14 +0000639 db_vnfr["ip-address"] = vnfr_update["ip-address"] = vnf_RO["ip_address"].split(";")[0]
tiernof578e552018-11-08 19:07:20 +0100640 elif not db_vnfr.get("ip-address"):
tierno0ec0c272020-02-19 17:43:01 +0000641 if db_vnfr.get("vdur"): # if not VDUs, there is not ip_address
642 raise LcmExceptionNoMgmtIP("ns member_vnf_index '{}' has no IP address".format(vnf_index))
tierno59d22d22018-09-25 18:10:19 +0200643
tierno27246d82018-09-27 15:59:09 +0200644 for vdu_index, vdur in enumerate(get_iterable(db_vnfr, "vdur")):
645 vdur_RO_count_index = 0
646 if vdur.get("pdu-type"):
647 continue
648 for vdur_RO in get_iterable(vnf_RO, "vms"):
649 if vdur["vdu-id-ref"] != vdur_RO["vdu_osm_id"]:
650 continue
651 if vdur["count-index"] != vdur_RO_count_index:
652 vdur_RO_count_index += 1
653 continue
654 vdur["vim-id"] = vdur_RO.get("vim_vm_id")
tierno1674de82019-04-09 13:03:14 +0000655 if vdur_RO.get("ip_address"):
656 vdur["ip-address"] = vdur_RO["ip_address"].split(";")[0]
tierno274ed572019-04-04 13:33:27 +0000657 else:
658 vdur["ip-address"] = None
tierno27246d82018-09-27 15:59:09 +0200659 vdur["vdu-id-ref"] = vdur_RO.get("vdu_osm_id")
660 vdur["name"] = vdur_RO.get("vim_name")
661 vdur["status"] = vdur_RO.get("status")
662 vdur["status-detailed"] = vdur_RO.get("error_msg")
663 for ifacer in get_iterable(vdur, "interfaces"):
664 for interface_RO in get_iterable(vdur_RO, "interfaces"):
665 if ifacer["name"] == interface_RO.get("internal_name"):
666 ifacer["ip-address"] = interface_RO.get("ip_address")
667 ifacer["mac-address"] = interface_RO.get("mac_address")
668 break
669 else:
670 raise LcmException("ns_update_vnfr: Not found member_vnf_index={} vdur={} interface={} "
quilesj7e13aeb2019-10-08 13:34:55 +0200671 "from VIM info"
672 .format(vnf_index, vdur["vdu-id-ref"], ifacer["name"]))
tierno27246d82018-09-27 15:59:09 +0200673 vnfr_update["vdur.{}".format(vdu_index)] = vdur
674 break
675 else:
tierno15b1cf12019-08-29 13:21:40 +0000676 raise LcmException("ns_update_vnfr: Not found member_vnf_index={} vdur={} count_index={} from "
677 "VIM info".format(vnf_index, vdur["vdu-id-ref"], vdur["count-index"]))
tiernof578e552018-11-08 19:07:20 +0100678
679 for vld_index, vld in enumerate(get_iterable(db_vnfr, "vld")):
680 for net_RO in get_iterable(nsr_desc_RO, "nets"):
681 if vld["id"] != net_RO.get("vnf_net_osm_id"):
682 continue
683 vld["vim-id"] = net_RO.get("vim_net_id")
684 vld["name"] = net_RO.get("vim_name")
685 vld["status"] = net_RO.get("status")
686 vld["status-detailed"] = net_RO.get("error_msg")
687 vnfr_update["vld.{}".format(vld_index)] = vld
688 break
689 else:
tierno15b1cf12019-08-29 13:21:40 +0000690 raise LcmException("ns_update_vnfr: Not found member_vnf_index={} vld={} from VIM info".format(
tiernof578e552018-11-08 19:07:20 +0100691 vnf_index, vld["id"]))
692
tierno27246d82018-09-27 15:59:09 +0200693 self.update_db_2("vnfrs", db_vnfr["_id"], vnfr_update)
694 break
tierno59d22d22018-09-25 18:10:19 +0200695
696 else:
tierno15b1cf12019-08-29 13:21:40 +0000697 raise LcmException("ns_update_vnfr: Not found member_vnf_index={} from VIM info".format(vnf_index))
tierno59d22d22018-09-25 18:10:19 +0200698
tierno5ee02052019-12-05 19:55:02 +0000699 def _get_ns_config_info(self, nsr_id):
tiernoc3f2a822019-11-05 13:45:04 +0000700 """
701 Generates a mapping between vnf,vdu elements and the N2VC id
tierno5ee02052019-12-05 19:55:02 +0000702 :param nsr_id: id of nsr to get last database _admin.deployed.VCA that contains this list
tiernoc3f2a822019-11-05 13:45:04 +0000703 :return: a dictionary with {osm-config-mapping: {}} where its element contains:
704 "<member-vnf-index>": <N2VC-id> for a vnf configuration, or
705 "<member-vnf-index>.<vdu.id>.<vdu replica(0, 1,..)>": <N2VC-id> for a vdu configuration
706 """
tierno5ee02052019-12-05 19:55:02 +0000707 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
708 vca_deployed_list = db_nsr["_admin"]["deployed"]["VCA"]
tiernoc3f2a822019-11-05 13:45:04 +0000709 mapping = {}
710 ns_config_info = {"osm-config-mapping": mapping}
711 for vca in vca_deployed_list:
712 if not vca["member-vnf-index"]:
713 continue
714 if not vca["vdu_id"]:
715 mapping[vca["member-vnf-index"]] = vca["application"]
716 else:
717 mapping["{}.{}.{}".format(vca["member-vnf-index"], vca["vdu_id"], vca["vdu_count_index"])] =\
718 vca["application"]
719 return ns_config_info
720
721 @staticmethod
722 def _get_initial_config_primitive_list(desc_primitive_list, vca_deployed):
723 """
724 Generates a list of initial-config-primitive based on the list provided by the descriptor. It includes internal
725 primitives as verify-ssh-credentials, or config when needed
726 :param desc_primitive_list: information of the descriptor
727 :param vca_deployed: information of the deployed, needed for known if it is related to an NS, VNF, VDU and if
728 this element contains a ssh public key
729 :return: The modified list. Can ba an empty list, but always a list
730 """
731 if desc_primitive_list:
732 primitive_list = desc_primitive_list.copy()
733 else:
734 primitive_list = []
735 # look for primitive config, and get the position. None if not present
736 config_position = None
737 for index, primitive in enumerate(primitive_list):
738 if primitive["name"] == "config":
739 config_position = index
740 break
741
742 # for NS, add always a config primitive if not present (bug 874)
743 if not vca_deployed["member-vnf-index"] and config_position is None:
744 primitive_list.insert(0, {"name": "config", "parameter": []})
745 config_position = 0
746 # for VNF/VDU add verify-ssh-credentials after config
747 if vca_deployed["member-vnf-index"] and config_position is not None and vca_deployed.get("ssh-public-key"):
748 primitive_list.insert(config_position + 1, {"name": "verify-ssh-credentials", "parameter": []})
749 return primitive_list
750
tiernoe876f672020-02-13 14:34:48 +0000751 async def instantiate_RO(self, logging_text, nsr_id, nsd, db_nsr, db_nslcmop, db_vnfrs, db_vnfds_ref,
752 n2vc_key_list, stage):
753 try:
754 db_nsr_update = {}
755 RO_descriptor_number = 0 # number of descriptors created at RO
756 vnf_index_2_RO_id = {} # map between vnfd/nsd id to the id used at RO
757 nslcmop_id = db_nslcmop["_id"]
758 start_deploy = time()
759 ns_params = db_nslcmop.get("operationParams")
760 if ns_params and ns_params.get("timeout_ns_deploy"):
761 timeout_ns_deploy = ns_params["timeout_ns_deploy"]
762 else:
763 timeout_ns_deploy = self.timeout.get("ns_deploy", self.timeout_ns_deploy)
quilesj7e13aeb2019-10-08 13:34:55 +0200764
tiernoe876f672020-02-13 14:34:48 +0000765 # Check for and optionally request placement optimization. Database will be updated if placement activated
766 stage[2] = "Waiting for Placement."
767 await self.do_placement(logging_text, db_nslcmop, db_vnfrs)
quilesj7e13aeb2019-10-08 13:34:55 +0200768
tiernoe876f672020-02-13 14:34:48 +0000769 # deploy RO
magnussonle9198bb2020-01-21 13:00:51 +0100770
tiernoe876f672020-02-13 14:34:48 +0000771 # get vnfds, instantiate at RO
772 for c_vnf in nsd.get("constituent-vnfd", ()):
773 member_vnf_index = c_vnf["member-vnf-index"]
774 vnfd = db_vnfds_ref[c_vnf['vnfd-id-ref']]
775 vnfd_ref = vnfd["id"]
quilesj7e13aeb2019-10-08 13:34:55 +0200776
tiernoe876f672020-02-13 14:34:48 +0000777 stage[2] = "Creating vnfd='{}' member_vnf_index='{}' at RO".format(vnfd_ref, member_vnf_index)
778 db_nsr_update["detailed-status"] = " ".join(stage)
779 self.update_db_2("nsrs", nsr_id, db_nsr_update)
780 self._write_op_status(nslcmop_id, stage)
calvinosanch9f9c6f22019-11-04 13:37:39 +0100781
tiernoe876f672020-02-13 14:34:48 +0000782 # self.logger.debug(logging_text + stage[2])
783 vnfd_id_RO = "{}.{}.{}".format(nsr_id, RO_descriptor_number, member_vnf_index[:23])
784 vnf_index_2_RO_id[member_vnf_index] = vnfd_id_RO
785 RO_descriptor_number += 1
786
787 # look position at deployed.RO.vnfd if not present it will be appended at the end
788 for index, vnf_deployed in enumerate(db_nsr["_admin"]["deployed"]["RO"]["vnfd"]):
789 if vnf_deployed["member-vnf-index"] == member_vnf_index:
790 break
791 else:
792 index = len(db_nsr["_admin"]["deployed"]["RO"]["vnfd"])
793 db_nsr["_admin"]["deployed"]["RO"]["vnfd"].append(None)
794
795 # look if present
796 RO_update = {"member-vnf-index": member_vnf_index}
797 vnfd_list = await self.RO.get_list("vnfd", filter_by={"osm_id": vnfd_id_RO})
798 if vnfd_list:
799 RO_update["id"] = vnfd_list[0]["uuid"]
800 self.logger.debug(logging_text + "vnfd='{}' member_vnf_index='{}' exists at RO. Using RO_id={}".
801 format(vnfd_ref, member_vnf_index, vnfd_list[0]["uuid"]))
802 else:
803 vnfd_RO = self.vnfd2RO(vnfd, vnfd_id_RO, db_vnfrs[c_vnf["member-vnf-index"]].
804 get("additionalParamsForVnf"), nsr_id)
805 desc = await self.RO.create("vnfd", descriptor=vnfd_RO)
806 RO_update["id"] = desc["uuid"]
807 self.logger.debug(logging_text + "vnfd='{}' member_vnf_index='{}' created at RO. RO_id={}".format(
808 vnfd_ref, member_vnf_index, desc["uuid"]))
809 db_nsr_update["_admin.deployed.RO.vnfd.{}".format(index)] = RO_update
810 db_nsr["_admin"]["deployed"]["RO"]["vnfd"][index] = RO_update
811
812 # create nsd at RO
813 nsd_ref = nsd["id"]
814
815 stage[2] = "Creating nsd={} at RO".format(nsd_ref)
816 db_nsr_update["detailed-status"] = " ".join(stage)
817 self.update_db_2("nsrs", nsr_id, db_nsr_update)
818 self._write_op_status(nslcmop_id, stage)
819
820 # self.logger.debug(logging_text + stage[2])
821 RO_osm_nsd_id = "{}.{}.{}".format(nsr_id, RO_descriptor_number, nsd_ref[:23])
tiernod8323042019-08-09 11:32:23 +0000822 RO_descriptor_number += 1
tiernoe876f672020-02-13 14:34:48 +0000823 nsd_list = await self.RO.get_list("nsd", filter_by={"osm_id": RO_osm_nsd_id})
824 if nsd_list:
825 db_nsr_update["_admin.deployed.RO.nsd_id"] = RO_nsd_uuid = nsd_list[0]["uuid"]
826 self.logger.debug(logging_text + "nsd={} exists at RO. Using RO_id={}".format(
827 nsd_ref, RO_nsd_uuid))
tiernod8323042019-08-09 11:32:23 +0000828 else:
tiernoe876f672020-02-13 14:34:48 +0000829 nsd_RO = deepcopy(nsd)
830 nsd_RO["id"] = RO_osm_nsd_id
831 nsd_RO.pop("_id", None)
832 nsd_RO.pop("_admin", None)
833 for c_vnf in nsd_RO.get("constituent-vnfd", ()):
834 member_vnf_index = c_vnf["member-vnf-index"]
835 c_vnf["vnfd-id-ref"] = vnf_index_2_RO_id[member_vnf_index]
836 for c_vld in nsd_RO.get("vld", ()):
837 for cp in c_vld.get("vnfd-connection-point-ref", ()):
838 member_vnf_index = cp["member-vnf-index-ref"]
839 cp["vnfd-id-ref"] = vnf_index_2_RO_id[member_vnf_index]
tiernod8323042019-08-09 11:32:23 +0000840
tiernoe876f672020-02-13 14:34:48 +0000841 desc = await self.RO.create("nsd", descriptor=nsd_RO)
842 db_nsr_update["_admin.nsState"] = "INSTANTIATED"
843 db_nsr_update["_admin.deployed.RO.nsd_id"] = RO_nsd_uuid = desc["uuid"]
844 self.logger.debug(logging_text + "nsd={} created at RO. RO_id={}".format(nsd_ref, RO_nsd_uuid))
tiernod8323042019-08-09 11:32:23 +0000845 self.update_db_2("nsrs", nsr_id, db_nsr_update)
846
tiernoe876f672020-02-13 14:34:48 +0000847 # Crate ns at RO
848 stage[2] = "Creating nsd={} at RO".format(nsd_ref)
849 db_nsr_update["detailed-status"] = " ".join(stage)
850 self.update_db_2("nsrs", nsr_id, db_nsr_update)
851 self._write_op_status(nslcmop_id, stage)
tiernod8323042019-08-09 11:32:23 +0000852
tiernoe876f672020-02-13 14:34:48 +0000853 # if present use it unless in error status
854 RO_nsr_id = deep_get(db_nsr, ("_admin", "deployed", "RO", "nsr_id"))
855 if RO_nsr_id:
856 try:
857 stage[2] = "Looking for existing ns at RO"
858 db_nsr_update["detailed-status"] = " ".join(stage)
859 self.update_db_2("nsrs", nsr_id, db_nsr_update)
860 self._write_op_status(nslcmop_id, stage)
861 # self.logger.debug(logging_text + stage[2] + " RO_ns_id={}".format(RO_nsr_id))
862 desc = await self.RO.show("ns", RO_nsr_id)
tiernod8323042019-08-09 11:32:23 +0000863
tiernoe876f672020-02-13 14:34:48 +0000864 except ROclient.ROClientException as e:
865 if e.http_code != HTTPStatus.NOT_FOUND:
866 raise
867 RO_nsr_id = db_nsr_update["_admin.deployed.RO.nsr_id"] = None
868 if RO_nsr_id:
869 ns_status, ns_status_info = self.RO.check_ns_status(desc)
870 db_nsr_update["_admin.deployed.RO.nsr_status"] = ns_status
871 if ns_status == "ERROR":
872 stage[2] = "Deleting ns at RO. RO_ns_id={}".format(RO_nsr_id)
873 self.logger.debug(logging_text + stage[2])
874 await self.RO.delete("ns", RO_nsr_id)
875 RO_nsr_id = db_nsr_update["_admin.deployed.RO.nsr_id"] = None
876 if not RO_nsr_id:
877 stage[2] = "Checking dependencies"
878 db_nsr_update["detailed-status"] = " ".join(stage)
879 self.update_db_2("nsrs", nsr_id, db_nsr_update)
880 self._write_op_status(nslcmop_id, stage)
881 # self.logger.debug(logging_text + stage[2])
tiernod8323042019-08-09 11:32:23 +0000882
tiernoe876f672020-02-13 14:34:48 +0000883 # check if VIM is creating and wait look if previous tasks in process
884 task_name, task_dependency = self.lcm_tasks.lookfor_related("vim_account", ns_params["vimAccountId"])
885 if task_dependency:
886 stage[2] = "Waiting for related tasks '{}' to be completed".format(task_name)
887 self.logger.debug(logging_text + stage[2])
888 await asyncio.wait(task_dependency, timeout=3600)
889 if ns_params.get("vnf"):
890 for vnf in ns_params["vnf"]:
891 if "vimAccountId" in vnf:
892 task_name, task_dependency = self.lcm_tasks.lookfor_related("vim_account",
893 vnf["vimAccountId"])
894 if task_dependency:
895 stage[2] = "Waiting for related tasks '{}' to be completed.".format(task_name)
896 self.logger.debug(logging_text + stage[2])
897 await asyncio.wait(task_dependency, timeout=3600)
898
899 stage[2] = "Checking instantiation parameters."
900 RO_ns_params = self.ns_params_2_RO(ns_params, nsd, db_vnfds_ref, n2vc_key_list)
901 stage[2] = "Deploying ns at VIM."
902 db_nsr_update["detailed-status"] = " ".join(stage)
903 self.update_db_2("nsrs", nsr_id, db_nsr_update)
904 self._write_op_status(nslcmop_id, stage)
905
906 desc = await self.RO.create("ns", descriptor=RO_ns_params, name=db_nsr["name"], scenario=RO_nsd_uuid)
907 RO_nsr_id = db_nsr_update["_admin.deployed.RO.nsr_id"] = desc["uuid"]
908 db_nsr_update["_admin.nsState"] = "INSTANTIATED"
909 db_nsr_update["_admin.deployed.RO.nsr_status"] = "BUILD"
910 self.logger.debug(logging_text + "ns created at RO. RO_id={}".format(desc["uuid"]))
911
912 # wait until NS is ready
913 stage[2] = "Waiting VIM to deploy ns."
914 db_nsr_update["detailed-status"] = " ".join(stage)
915 self.update_db_2("nsrs", nsr_id, db_nsr_update)
916 self._write_op_status(nslcmop_id, stage)
917 detailed_status_old = None
918 self.logger.debug(logging_text + stage[2] + " RO_ns_id={}".format(RO_nsr_id))
919
920 old_desc = None
921 while time() <= start_deploy + timeout_ns_deploy:
tiernod8323042019-08-09 11:32:23 +0000922 desc = await self.RO.show("ns", RO_nsr_id)
quilesj3655ae02019-12-12 16:08:35 +0000923
tiernoe876f672020-02-13 14:34:48 +0000924 # deploymentStatus
925 if desc != old_desc:
926 # desc has changed => update db
927 self._on_update_ro_db(nsrs_id=nsr_id, ro_descriptor=desc)
928 old_desc = desc
tiernod8323042019-08-09 11:32:23 +0000929
tiernoe876f672020-02-13 14:34:48 +0000930 ns_status, ns_status_info = self.RO.check_ns_status(desc)
931 db_nsr_update["_admin.deployed.RO.nsr_status"] = ns_status
932 if ns_status == "ERROR":
933 raise ROclient.ROClientException(ns_status_info)
934 elif ns_status == "BUILD":
935 stage[2] = "VIM: ({})".format(ns_status_info)
936 elif ns_status == "ACTIVE":
937 stage[2] = "Waiting for management IP address reported by the VIM. Updating VNFRs."
938 try:
939 self.ns_update_vnfr(db_vnfrs, desc)
940 break
941 except LcmExceptionNoMgmtIP:
942 pass
943 else:
944 assert False, "ROclient.check_ns_status returns unknown {}".format(ns_status)
945 if stage[2] != detailed_status_old:
946 detailed_status_old = stage[2]
947 db_nsr_update["detailed-status"] = " ".join(stage)
948 self.update_db_2("nsrs", nsr_id, db_nsr_update)
949 self._write_op_status(nslcmop_id, stage)
950 await asyncio.sleep(5, loop=self.loop)
951 else: # timeout_ns_deploy
952 raise ROclient.ROClientException("Timeout waiting ns to be ready")
tiernod8323042019-08-09 11:32:23 +0000953
tiernoe876f672020-02-13 14:34:48 +0000954 # Updating NSR
955 self.ns_update_nsr(db_nsr_update, db_nsr, desc)
tiernod8323042019-08-09 11:32:23 +0000956
tiernoe876f672020-02-13 14:34:48 +0000957 db_nsr_update["_admin.deployed.RO.operational-status"] = "running"
958 # db_nsr["_admin.deployed.RO.detailed-status"] = "Deployed at VIM"
959 stage[2] = "Deployed at VIM"
960 db_nsr_update["detailed-status"] = " ".join(stage)
961 self.update_db_2("nsrs", nsr_id, db_nsr_update)
962 self._write_op_status(nslcmop_id, stage)
963 # await self._on_update_n2vc_db("nsrs", {"_id": nsr_id}, "_admin.deployed", db_nsr_update)
964 # self.logger.debug(logging_text + "Deployed at VIM")
965 except (ROclient.ROClientException, LcmException, DbException) as e:
tiernoa2143262020-03-27 16:20:40 +0000966 stage[2] = "ERROR deployig at VIM"
tiernoe876f672020-02-13 14:34:48 +0000967 self.set_vnfr_at_error(db_vnfrs, str(e))
968 raise
quilesj7e13aeb2019-10-08 13:34:55 +0200969
tiernoa5088192019-11-26 16:12:53 +0000970 async def wait_vm_up_insert_key_ro(self, logging_text, nsr_id, vnfr_id, vdu_id, vdu_index, pub_key=None, user=None):
971 """
972 Wait for ip addres at RO, and optionally, insert public key in virtual machine
973 :param logging_text: prefix use for logging
974 :param nsr_id:
975 :param vnfr_id:
976 :param vdu_id:
977 :param vdu_index:
978 :param pub_key: public ssh key to inject, None to skip
979 :param user: user to apply the public ssh key
980 :return: IP address
981 """
quilesj7e13aeb2019-10-08 13:34:55 +0200982
tiernoa5088192019-11-26 16:12:53 +0000983 # self.logger.debug(logging_text + "Starting wait_vm_up_insert_key_ro")
tiernod8323042019-08-09 11:32:23 +0000984 ro_nsr_id = None
985 ip_address = None
986 nb_tries = 0
987 target_vdu_id = None
quilesj3149f262019-12-03 10:58:10 +0000988 ro_retries = 0
quilesj7e13aeb2019-10-08 13:34:55 +0200989
tiernod8323042019-08-09 11:32:23 +0000990 while True:
quilesj7e13aeb2019-10-08 13:34:55 +0200991
quilesj3149f262019-12-03 10:58:10 +0000992 ro_retries += 1
993 if ro_retries >= 360: # 1 hour
994 raise LcmException("Not found _admin.deployed.RO.nsr_id for nsr_id: {}".format(nsr_id))
995
tiernod8323042019-08-09 11:32:23 +0000996 await asyncio.sleep(10, loop=self.loop)
quilesj7e13aeb2019-10-08 13:34:55 +0200997
998 # get ip address
tiernod8323042019-08-09 11:32:23 +0000999 if not target_vdu_id:
1000 db_vnfr = self.db.get_one("vnfrs", {"_id": vnfr_id})
quilesj3149f262019-12-03 10:58:10 +00001001
1002 if not vdu_id: # for the VNF case
tiernoe876f672020-02-13 14:34:48 +00001003 if db_vnfr.get("status") == "ERROR":
1004 raise LcmException("Cannot inject ssh-key because target VNF is in error state")
tiernod8323042019-08-09 11:32:23 +00001005 ip_address = db_vnfr.get("ip-address")
1006 if not ip_address:
1007 continue
quilesj3149f262019-12-03 10:58:10 +00001008 vdur = next((x for x in get_iterable(db_vnfr, "vdur") if x.get("ip-address") == ip_address), None)
1009 else: # VDU case
1010 vdur = next((x for x in get_iterable(db_vnfr, "vdur")
1011 if x.get("vdu-id-ref") == vdu_id and x.get("count-index") == vdu_index), None)
1012
tierno0e8c3f02020-03-12 17:18:21 +00001013 if not vdur and len(db_vnfr.get("vdur", ())) == 1: # If only one, this should be the target vdu
1014 vdur = db_vnfr["vdur"][0]
quilesj3149f262019-12-03 10:58:10 +00001015 if not vdur:
tierno0e8c3f02020-03-12 17:18:21 +00001016 raise LcmException("Not found vnfr_id={}, vdu_id={}, vdu_index={}".format(vnfr_id, vdu_id,
1017 vdu_index))
quilesj7e13aeb2019-10-08 13:34:55 +02001018
tierno0e8c3f02020-03-12 17:18:21 +00001019 if vdur.get("pdu-type") or vdur.get("status") == "ACTIVE":
quilesj3149f262019-12-03 10:58:10 +00001020 ip_address = vdur.get("ip-address")
1021 if not ip_address:
1022 continue
1023 target_vdu_id = vdur["vdu-id-ref"]
1024 elif vdur.get("status") == "ERROR":
1025 raise LcmException("Cannot inject ssh-key because target VM is in error state")
1026
tiernod8323042019-08-09 11:32:23 +00001027 if not target_vdu_id:
1028 continue
tiernod8323042019-08-09 11:32:23 +00001029
quilesj7e13aeb2019-10-08 13:34:55 +02001030 # inject public key into machine
1031 if pub_key and user:
tiernoe876f672020-02-13 14:34:48 +00001032 # wait until NS is deployed at RO
1033 if not ro_nsr_id:
1034 db_nsrs = self.db.get_one("nsrs", {"_id": nsr_id})
1035 ro_nsr_id = deep_get(db_nsrs, ("_admin", "deployed", "RO", "nsr_id"))
1036 if not ro_nsr_id:
1037 continue
1038
tiernoa5088192019-11-26 16:12:53 +00001039 # self.logger.debug(logging_text + "Inserting RO key")
tierno0e8c3f02020-03-12 17:18:21 +00001040 if vdur.get("pdu-type"):
1041 self.logger.error(logging_text + "Cannot inject ssh-ky to a PDU")
1042 return ip_address
quilesj7e13aeb2019-10-08 13:34:55 +02001043 try:
1044 ro_vm_id = "{}-{}".format(db_vnfr["member-vnf-index-ref"], target_vdu_id) # TODO add vdu_index
1045 result_dict = await self.RO.create_action(
1046 item="ns",
1047 item_id_name=ro_nsr_id,
1048 descriptor={"add_public_key": pub_key, "vms": [ro_vm_id], "user": user}
1049 )
1050 # result_dict contains the format {VM-id: {vim_result: 200, description: text}}
1051 if not result_dict or not isinstance(result_dict, dict):
1052 raise LcmException("Unknown response from RO when injecting key")
1053 for result in result_dict.values():
1054 if result.get("vim_result") == 200:
1055 break
1056 else:
1057 raise ROclient.ROClientException("error injecting key: {}".format(
1058 result.get("description")))
1059 break
1060 except ROclient.ROClientException as e:
tiernoa5088192019-11-26 16:12:53 +00001061 if not nb_tries:
1062 self.logger.debug(logging_text + "error injecting key: {}. Retrying until {} seconds".
1063 format(e, 20*10))
quilesj7e13aeb2019-10-08 13:34:55 +02001064 nb_tries += 1
tiernoa5088192019-11-26 16:12:53 +00001065 if nb_tries >= 20:
quilesj7e13aeb2019-10-08 13:34:55 +02001066 raise LcmException("Reaching max tries injecting key. Error: {}".format(e))
quilesj7e13aeb2019-10-08 13:34:55 +02001067 else:
quilesj7e13aeb2019-10-08 13:34:55 +02001068 break
1069
1070 return ip_address
1071
tierno5ee02052019-12-05 19:55:02 +00001072 async def _wait_dependent_n2vc(self, nsr_id, vca_deployed_list, vca_index):
1073 """
1074 Wait until dependent VCA deployments have been finished. NS wait for VNFs and VDUs. VNFs for VDUs
1075 """
1076 my_vca = vca_deployed_list[vca_index]
1077 if my_vca.get("vdu_id") or my_vca.get("kdu_name"):
quilesj3655ae02019-12-12 16:08:35 +00001078 # vdu or kdu: no dependencies
tierno5ee02052019-12-05 19:55:02 +00001079 return
1080 timeout = 300
1081 while timeout >= 0:
quilesj3655ae02019-12-12 16:08:35 +00001082 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
1083 vca_deployed_list = db_nsr["_admin"]["deployed"]["VCA"]
1084 configuration_status_list = db_nsr["configurationStatus"]
1085 for index, vca_deployed in enumerate(configuration_status_list):
tierno5ee02052019-12-05 19:55:02 +00001086 if index == vca_index:
quilesj3655ae02019-12-12 16:08:35 +00001087 # myself
tierno5ee02052019-12-05 19:55:02 +00001088 continue
1089 if not my_vca.get("member-vnf-index") or \
1090 (vca_deployed.get("member-vnf-index") == my_vca.get("member-vnf-index")):
quilesj3655ae02019-12-12 16:08:35 +00001091 internal_status = configuration_status_list[index].get("status")
1092 if internal_status == 'READY':
1093 continue
1094 elif internal_status == 'BROKEN':
tierno5ee02052019-12-05 19:55:02 +00001095 raise LcmException("Configuration aborted because dependent charm/s has failed")
quilesj3655ae02019-12-12 16:08:35 +00001096 else:
1097 break
tierno5ee02052019-12-05 19:55:02 +00001098 else:
quilesj3655ae02019-12-12 16:08:35 +00001099 # no dependencies, return
tierno5ee02052019-12-05 19:55:02 +00001100 return
1101 await asyncio.sleep(10)
1102 timeout -= 1
tierno5ee02052019-12-05 19:55:02 +00001103
1104 raise LcmException("Configuration aborted because dependent charm/s timeout")
1105
tiernoe876f672020-02-13 14:34:48 +00001106 async def instantiate_N2VC(self, logging_text, vca_index, nsi_id, db_nsr, db_vnfr, vdu_id, kdu_name, vdu_index,
1107 config_descriptor, deploy_params, base_folder, nslcmop_id, stage):
tiernod8323042019-08-09 11:32:23 +00001108 nsr_id = db_nsr["_id"]
1109 db_update_entry = "_admin.deployed.VCA.{}.".format(vca_index)
tiernoda6fb102019-11-23 00:36:52 +00001110 vca_deployed_list = db_nsr["_admin"]["deployed"]["VCA"]
tiernod8323042019-08-09 11:32:23 +00001111 vca_deployed = db_nsr["_admin"]["deployed"]["VCA"][vca_index]
quilesj7e13aeb2019-10-08 13:34:55 +02001112 db_dict = {
1113 'collection': 'nsrs',
1114 'filter': {'_id': nsr_id},
1115 'path': db_update_entry
1116 }
tiernod8323042019-08-09 11:32:23 +00001117 step = ""
1118 try:
quilesj3655ae02019-12-12 16:08:35 +00001119
1120 element_type = 'NS'
1121 element_under_configuration = nsr_id
1122
tiernod8323042019-08-09 11:32:23 +00001123 vnfr_id = None
1124 if db_vnfr:
1125 vnfr_id = db_vnfr["_id"]
1126
1127 namespace = "{nsi}.{ns}".format(
1128 nsi=nsi_id if nsi_id else "",
1129 ns=nsr_id)
quilesj3655ae02019-12-12 16:08:35 +00001130
tiernod8323042019-08-09 11:32:23 +00001131 if vnfr_id:
quilesj3655ae02019-12-12 16:08:35 +00001132 element_type = 'VNF'
1133 element_under_configuration = vnfr_id
quilesjb8a35dd2020-01-09 15:10:14 +00001134 namespace += ".{}".format(vnfr_id)
tiernod8323042019-08-09 11:32:23 +00001135 if vdu_id:
1136 namespace += ".{}-{}".format(vdu_id, vdu_index or 0)
quilesj3655ae02019-12-12 16:08:35 +00001137 element_type = 'VDU'
quilesjb8a35dd2020-01-09 15:10:14 +00001138 element_under_configuration = "{}-{}".format(vdu_id, vdu_index or 0)
tiernod8323042019-08-09 11:32:23 +00001139
1140 # Get artifact path
David Garcia56522772020-01-20 13:19:29 +01001141 self.fs.sync() # Sync from FSMongo
David Garcia485b2912019-12-04 14:01:50 +01001142 artifact_path = "{}/{}/charms/{}".format(
tiernod8323042019-08-09 11:32:23 +00001143 base_folder["folder"],
1144 base_folder["pkg-dir"],
1145 config_descriptor["juju"]["charm"]
1146 )
1147
quilesj7e13aeb2019-10-08 13:34:55 +02001148 is_proxy_charm = deep_get(config_descriptor, ('juju', 'charm')) is not None
1149 if deep_get(config_descriptor, ('juju', 'proxy')) is False:
tiernod8323042019-08-09 11:32:23 +00001150 is_proxy_charm = False
1151
1152 # n2vc_redesign STEP 3.1
quilesj7e13aeb2019-10-08 13:34:55 +02001153
1154 # find old ee_id if exists
tiernod8323042019-08-09 11:32:23 +00001155 ee_id = vca_deployed.get("ee_id")
tiernod8323042019-08-09 11:32:23 +00001156
quilesj7e13aeb2019-10-08 13:34:55 +02001157 # create or register execution environment in VCA
1158 if is_proxy_charm:
quilesj3655ae02019-12-12 16:08:35 +00001159
tiernoc231a872020-01-21 08:49:05 +00001160 self._write_configuration_status(
quilesj3655ae02019-12-12 16:08:35 +00001161 nsr_id=nsr_id,
1162 vca_index=vca_index,
1163 status='CREATING',
1164 element_under_configuration=element_under_configuration,
1165 element_type=element_type
1166 )
1167
quilesj7e13aeb2019-10-08 13:34:55 +02001168 step = "create execution environment"
1169 self.logger.debug(logging_text + step)
tierno3bedc9b2019-11-27 15:46:57 +00001170 ee_id, credentials = await self.n2vc.create_execution_environment(namespace=namespace,
1171 reuse_ee_id=ee_id,
1172 db_dict=db_dict)
quilesj3655ae02019-12-12 16:08:35 +00001173
quilesj7e13aeb2019-10-08 13:34:55 +02001174 else:
tierno3bedc9b2019-11-27 15:46:57 +00001175 step = "Waiting to VM being up and getting IP address"
1176 self.logger.debug(logging_text + step)
1177 rw_mgmt_ip = await self.wait_vm_up_insert_key_ro(logging_text, nsr_id, vnfr_id, vdu_id, vdu_index,
1178 user=None, pub_key=None)
1179 credentials = {"hostname": rw_mgmt_ip}
quilesj7e13aeb2019-10-08 13:34:55 +02001180 # get username
tierno3bedc9b2019-11-27 15:46:57 +00001181 username = deep_get(config_descriptor, ("config-access", "ssh-access", "default-user"))
quilesj7e13aeb2019-10-08 13:34:55 +02001182 # TODO remove this when changes on IM regarding config-access:ssh-access:default-user were
1183 # merged. Meanwhile let's get username from initial-config-primitive
tierno3bedc9b2019-11-27 15:46:57 +00001184 if not username and config_descriptor.get("initial-config-primitive"):
1185 for config_primitive in config_descriptor["initial-config-primitive"]:
1186 for param in config_primitive.get("parameter", ()):
1187 if param["name"] == "ssh-username":
1188 username = param["value"]
1189 break
1190 if not username:
1191 raise LcmException("Cannot determine the username neither with 'initial-config-promitive' nor with "
1192 "'config-access.ssh-access.default-user'")
1193 credentials["username"] = username
quilesj7e13aeb2019-10-08 13:34:55 +02001194 # n2vc_redesign STEP 3.2
tierno3bedc9b2019-11-27 15:46:57 +00001195
tiernoc231a872020-01-21 08:49:05 +00001196 self._write_configuration_status(
quilesj3655ae02019-12-12 16:08:35 +00001197 nsr_id=nsr_id,
1198 vca_index=vca_index,
1199 status='REGISTERING',
1200 element_under_configuration=element_under_configuration,
1201 element_type=element_type
1202 )
1203
tierno3bedc9b2019-11-27 15:46:57 +00001204 step = "register execution environment {}".format(credentials)
quilesj7e13aeb2019-10-08 13:34:55 +02001205 self.logger.debug(logging_text + step)
tierno3bedc9b2019-11-27 15:46:57 +00001206 ee_id = await self.n2vc.register_execution_environment(credentials=credentials, namespace=namespace,
1207 db_dict=db_dict)
quilesj7e13aeb2019-10-08 13:34:55 +02001208
1209 # for compatibility with MON/POL modules, the need model and application name at database
1210 # TODO ask to N2VC instead of assuming the format "model_name.application_name"
1211 ee_id_parts = ee_id.split('.')
1212 model_name = ee_id_parts[0]
1213 application_name = ee_id_parts[1]
1214 self.update_db_2("nsrs", nsr_id, {db_update_entry + "model": model_name,
1215 db_update_entry + "application": application_name,
1216 db_update_entry + "ee_id": ee_id})
tiernod8323042019-08-09 11:32:23 +00001217
1218 # n2vc_redesign STEP 3.3
tierno3bedc9b2019-11-27 15:46:57 +00001219
tiernod8323042019-08-09 11:32:23 +00001220 step = "Install configuration Software"
quilesj3655ae02019-12-12 16:08:35 +00001221
tiernoc231a872020-01-21 08:49:05 +00001222 self._write_configuration_status(
quilesj3655ae02019-12-12 16:08:35 +00001223 nsr_id=nsr_id,
1224 vca_index=vca_index,
1225 status='INSTALLING SW',
1226 element_under_configuration=element_under_configuration,
1227 element_type=element_type
1228 )
1229
tierno3bedc9b2019-11-27 15:46:57 +00001230 # TODO check if already done
quilesj7e13aeb2019-10-08 13:34:55 +02001231 self.logger.debug(logging_text + step)
tierno3bedc9b2019-11-27 15:46:57 +00001232 await self.n2vc.install_configuration_sw(ee_id=ee_id, artifact_path=artifact_path, db_dict=db_dict)
quilesj7e13aeb2019-10-08 13:34:55 +02001233
quilesj63f90042020-01-17 09:53:55 +00001234 # write in db flag of configuration_sw already installed
1235 self.update_db_2("nsrs", nsr_id, {db_update_entry + "config_sw_installed": True})
1236
1237 # add relations for this VCA (wait for other peers related with this VCA)
1238 await self._add_vca_relations(logging_text=logging_text, nsr_id=nsr_id, vca_index=vca_index)
1239
quilesj7e13aeb2019-10-08 13:34:55 +02001240 # if SSH access is required, then get execution environment SSH public
tierno3bedc9b2019-11-27 15:46:57 +00001241 if is_proxy_charm: # if native charm we have waited already to VM be UP
1242 pub_key = None
1243 user = None
1244 if deep_get(config_descriptor, ("config-access", "ssh-access", "required")):
1245 # Needed to inject a ssh key
1246 user = deep_get(config_descriptor, ("config-access", "ssh-access", "default-user"))
1247 step = "Install configuration Software, getting public ssh key"
1248 pub_key = await self.n2vc.get_ee_ssh_public__key(ee_id=ee_id, db_dict=db_dict)
quilesj7e13aeb2019-10-08 13:34:55 +02001249
tiernoacc90452019-12-10 11:06:54 +00001250 step = "Insert public key into VM user={} ssh_key={}".format(user, pub_key)
tierno3bedc9b2019-11-27 15:46:57 +00001251 else:
1252 step = "Waiting to VM being up and getting IP address"
1253 self.logger.debug(logging_text + step)
quilesj7e13aeb2019-10-08 13:34:55 +02001254
tierno3bedc9b2019-11-27 15:46:57 +00001255 # n2vc_redesign STEP 5.1
1256 # wait for RO (ip-address) Insert pub_key into VM
tierno5ee02052019-12-05 19:55:02 +00001257 if vnfr_id:
1258 rw_mgmt_ip = await self.wait_vm_up_insert_key_ro(logging_text, nsr_id, vnfr_id, vdu_id, vdu_index,
1259 user=user, pub_key=pub_key)
1260 else:
1261 rw_mgmt_ip = None # This is for a NS configuration
tierno3bedc9b2019-11-27 15:46:57 +00001262
1263 self.logger.debug(logging_text + ' VM_ip_address={}'.format(rw_mgmt_ip))
quilesj7e13aeb2019-10-08 13:34:55 +02001264
tiernoa5088192019-11-26 16:12:53 +00001265 # store rw_mgmt_ip in deploy params for later replacement
quilesj7e13aeb2019-10-08 13:34:55 +02001266 deploy_params["rw_mgmt_ip"] = rw_mgmt_ip
tiernod8323042019-08-09 11:32:23 +00001267
1268 # n2vc_redesign STEP 6 Execute initial config primitive
quilesj7e13aeb2019-10-08 13:34:55 +02001269 step = 'execute initial config primitive'
tiernoa5088192019-11-26 16:12:53 +00001270 initial_config_primitive_list = config_descriptor.get('initial-config-primitive')
quilesj7e13aeb2019-10-08 13:34:55 +02001271
1272 # sort initial config primitives by 'seq'
quilesj63f90042020-01-17 09:53:55 +00001273 if initial_config_primitive_list:
1274 try:
1275 initial_config_primitive_list.sort(key=lambda val: int(val['seq']))
1276 except Exception as e:
1277 self.logger.error(logging_text + step + ": " + str(e))
1278 else:
1279 self.logger.debug(logging_text + step + ": No initial-config-primitive")
quilesj7e13aeb2019-10-08 13:34:55 +02001280
tiernoda6fb102019-11-23 00:36:52 +00001281 # add config if not present for NS charm
1282 initial_config_primitive_list = self._get_initial_config_primitive_list(initial_config_primitive_list,
1283 vca_deployed)
quilesj3655ae02019-12-12 16:08:35 +00001284
1285 # wait for dependent primitives execution (NS -> VNF -> VDU)
tierno5ee02052019-12-05 19:55:02 +00001286 if initial_config_primitive_list:
1287 await self._wait_dependent_n2vc(nsr_id, vca_deployed_list, vca_index)
quilesj3655ae02019-12-12 16:08:35 +00001288
1289 # stage, in function of element type: vdu, kdu, vnf or ns
1290 my_vca = vca_deployed_list[vca_index]
1291 if my_vca.get("vdu_id") or my_vca.get("kdu_name"):
1292 # VDU or KDU
tiernoe876f672020-02-13 14:34:48 +00001293 stage[0] = 'Stage 3/5: running Day-1 primitives for VDU.'
quilesj3655ae02019-12-12 16:08:35 +00001294 elif my_vca.get("member-vnf-index"):
1295 # VNF
tiernoe876f672020-02-13 14:34:48 +00001296 stage[0] = 'Stage 4/5: running Day-1 primitives for VNF.'
quilesj3655ae02019-12-12 16:08:35 +00001297 else:
1298 # NS
tiernoe876f672020-02-13 14:34:48 +00001299 stage[0] = 'Stage 5/5: running Day-1 primitives for NS.'
quilesj3655ae02019-12-12 16:08:35 +00001300
tiernoc231a872020-01-21 08:49:05 +00001301 self._write_configuration_status(
quilesj3655ae02019-12-12 16:08:35 +00001302 nsr_id=nsr_id,
1303 vca_index=vca_index,
1304 status='EXECUTING PRIMITIVE'
1305 )
1306
1307 self._write_op_status(
1308 op_id=nslcmop_id,
1309 stage=stage
1310 )
1311
tiernoe876f672020-02-13 14:34:48 +00001312 check_if_terminated_needed = True
tiernod8323042019-08-09 11:32:23 +00001313 for initial_config_primitive in initial_config_primitive_list:
tiernoda6fb102019-11-23 00:36:52 +00001314 # adding information on the vca_deployed if it is a NS execution environment
1315 if not vca_deployed["member-vnf-index"]:
David Garciad4816682019-12-09 14:57:43 +01001316 deploy_params["ns_config_info"] = json.dumps(self._get_ns_config_info(nsr_id))
tiernod8323042019-08-09 11:32:23 +00001317 # TODO check if already done
1318 primitive_params_ = self._map_primitive_params(initial_config_primitive, {}, deploy_params)
tierno3bedc9b2019-11-27 15:46:57 +00001319
tiernod8323042019-08-09 11:32:23 +00001320 step = "execute primitive '{}' params '{}'".format(initial_config_primitive["name"], primitive_params_)
1321 self.logger.debug(logging_text + step)
quilesj7e13aeb2019-10-08 13:34:55 +02001322 await self.n2vc.exec_primitive(
1323 ee_id=ee_id,
1324 primitive_name=initial_config_primitive["name"],
1325 params_dict=primitive_params_,
1326 db_dict=db_dict
1327 )
tiernoe876f672020-02-13 14:34:48 +00001328 # Once some primitive has been exec, check and write at db if it needs to exec terminated primitives
1329 if check_if_terminated_needed:
1330 if config_descriptor.get('terminate-config-primitive'):
1331 self.update_db_2("nsrs", nsr_id, {db_update_entry + "needed_terminate": True})
1332 check_if_terminated_needed = False
quilesj3655ae02019-12-12 16:08:35 +00001333
tiernod8323042019-08-09 11:32:23 +00001334 # TODO register in database that primitive is done
quilesj7e13aeb2019-10-08 13:34:55 +02001335
1336 step = "instantiated at VCA"
1337 self.logger.debug(logging_text + step)
1338
tiernoc231a872020-01-21 08:49:05 +00001339 self._write_configuration_status(
quilesj3655ae02019-12-12 16:08:35 +00001340 nsr_id=nsr_id,
1341 vca_index=vca_index,
1342 status='READY'
1343 )
1344
tiernod8323042019-08-09 11:32:23 +00001345 except Exception as e: # TODO not use Exception but N2VC exception
quilesj3655ae02019-12-12 16:08:35 +00001346 # self.update_db_2("nsrs", nsr_id, {db_update_entry + "instantiation": "FAILED"})
tiernoe876f672020-02-13 14:34:48 +00001347 if not isinstance(e, (DbException, N2VCException, LcmException, asyncio.CancelledError)):
1348 self.logger.error("Exception while {} : {}".format(step, e), exc_info=True)
tiernoc231a872020-01-21 08:49:05 +00001349 self._write_configuration_status(
quilesj3655ae02019-12-12 16:08:35 +00001350 nsr_id=nsr_id,
1351 vca_index=vca_index,
1352 status='BROKEN'
1353 )
tiernoe876f672020-02-13 14:34:48 +00001354 raise LcmException("{} {}".format(step, e)) from e
tiernod8323042019-08-09 11:32:23 +00001355
quilesj4cda56b2019-12-05 10:02:20 +00001356 def _write_ns_status(self, nsr_id: str, ns_state: str, current_operation: str, current_operation_id: str,
tiernoa2143262020-03-27 16:20:40 +00001357 error_description: str = None, error_detail: str = None, other_update: dict = None):
tiernoe876f672020-02-13 14:34:48 +00001358 """
1359 Update db_nsr fields.
1360 :param nsr_id:
1361 :param ns_state:
1362 :param current_operation:
1363 :param current_operation_id:
1364 :param error_description:
tiernoa2143262020-03-27 16:20:40 +00001365 :param error_detail:
tiernoe876f672020-02-13 14:34:48 +00001366 :param other_update: Other required changes at database if provided, will be cleared
1367 :return:
1368 """
quilesj4cda56b2019-12-05 10:02:20 +00001369 try:
tiernoe876f672020-02-13 14:34:48 +00001370 db_dict = other_update or {}
1371 db_dict["_admin.nslcmop"] = current_operation_id # for backward compatibility
1372 db_dict["_admin.current-operation"] = current_operation_id
1373 db_dict["_admin.operation-type"] = current_operation if current_operation != "IDLE" else None
quilesj4cda56b2019-12-05 10:02:20 +00001374 db_dict["currentOperation"] = current_operation
1375 db_dict["currentOperationID"] = current_operation_id
1376 db_dict["errorDescription"] = error_description
tiernoa2143262020-03-27 16:20:40 +00001377 db_dict["errorDetail"] = error_detail
tiernoe876f672020-02-13 14:34:48 +00001378
1379 if ns_state:
1380 db_dict["nsState"] = ns_state
quilesj4cda56b2019-12-05 10:02:20 +00001381 self.update_db_2("nsrs", nsr_id, db_dict)
tiernoe876f672020-02-13 14:34:48 +00001382 except DbException as e:
quilesj3655ae02019-12-12 16:08:35 +00001383 self.logger.warn('Error writing NS status, ns={}: {}'.format(nsr_id, e))
1384
tiernoe876f672020-02-13 14:34:48 +00001385 def _write_op_status(self, op_id: str, stage: list = None, error_message: str = None, queuePosition: int = 0,
1386 operation_state: str = None, other_update: dict = None):
quilesj3655ae02019-12-12 16:08:35 +00001387 try:
tiernoe876f672020-02-13 14:34:48 +00001388 db_dict = other_update or {}
quilesj3655ae02019-12-12 16:08:35 +00001389 db_dict['queuePosition'] = queuePosition
tiernoe876f672020-02-13 14:34:48 +00001390 if isinstance(stage, list):
1391 db_dict['stage'] = stage[0]
1392 db_dict['detailed-status'] = " ".join(stage)
1393 elif stage is not None:
1394 db_dict['stage'] = str(stage)
1395
1396 if error_message is not None:
quilesj3655ae02019-12-12 16:08:35 +00001397 db_dict['errorMessage'] = error_message
tiernoe876f672020-02-13 14:34:48 +00001398 if operation_state is not None:
1399 db_dict['operationState'] = operation_state
1400 db_dict["statusEnteredTime"] = time()
quilesj3655ae02019-12-12 16:08:35 +00001401 self.update_db_2("nslcmops", op_id, db_dict)
tiernoe876f672020-02-13 14:34:48 +00001402 except DbException as e:
quilesj3655ae02019-12-12 16:08:35 +00001403 self.logger.warn('Error writing OPERATION status for op_id: {} -> {}'.format(op_id, e))
1404
1405 def _write_all_config_status(self, nsr_id: str, status: str):
1406 try:
1407 # nsrs record
1408 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
1409 # configurationStatus
1410 config_status = db_nsr.get('configurationStatus')
1411 if config_status:
1412 # update status
1413 db_dict = dict()
1414 db_dict['configurationStatus'] = list()
1415 for c in config_status:
1416 c['status'] = status
1417 db_dict['configurationStatus'].append(c)
1418 self.update_db_2("nsrs", nsr_id, db_dict)
1419
tiernoe876f672020-02-13 14:34:48 +00001420 except DbException as e:
quilesj3655ae02019-12-12 16:08:35 +00001421 self.logger.warn('Error writing all configuration status, ns={}: {}'.format(nsr_id, e))
1422
quilesj63f90042020-01-17 09:53:55 +00001423 def _write_configuration_status(self, nsr_id: str, vca_index: int, status: str = None,
tiernoc231a872020-01-21 08:49:05 +00001424 element_under_configuration: str = None, element_type: str = None):
quilesj3655ae02019-12-12 16:08:35 +00001425
1426 # self.logger.debug('_write_configuration_status(): vca_index={}, status={}'
1427 # .format(vca_index, status))
1428
1429 try:
1430 db_path = 'configurationStatus.{}.'.format(vca_index)
1431 db_dict = dict()
quilesj63f90042020-01-17 09:53:55 +00001432 if status:
1433 db_dict[db_path + 'status'] = status
quilesj3655ae02019-12-12 16:08:35 +00001434 if element_under_configuration:
1435 db_dict[db_path + 'elementUnderConfiguration'] = element_under_configuration
1436 if element_type:
1437 db_dict[db_path + 'elementType'] = element_type
1438 self.update_db_2("nsrs", nsr_id, db_dict)
tiernoe876f672020-02-13 14:34:48 +00001439 except DbException as e:
quilesj3655ae02019-12-12 16:08:35 +00001440 self.logger.warn('Error writing configuration status={}, ns={}, vca_index={}: {}'
1441 .format(status, nsr_id, vca_index, e))
quilesj4cda56b2019-12-05 10:02:20 +00001442
magnussonle9198bb2020-01-21 13:00:51 +01001443 async def do_placement(self, logging_text, db_nslcmop, db_vnfrs):
1444 placement_engine = deep_get(db_nslcmop, ('operationParams', 'placement-engine'))
1445 if placement_engine == "PLA":
1446 self.logger.debug(logging_text + "Invoke placement optimization for nslcmopId={}".format(db_nslcmop['id']))
1447 await self.msg.aiowrite("pla", "get_placement", {'nslcmopId': db_nslcmop['_id']}, loop=self.loop)
1448 db_poll_interval = 5
1449 wait = db_poll_interval * 4
1450 pla_result = None
1451 while not pla_result and wait >= 0:
1452 await asyncio.sleep(db_poll_interval)
1453 wait -= db_poll_interval
1454 db_nslcmop = self.db.get_one("nslcmops", {"_id": db_nslcmop["_id"]})
1455 pla_result = deep_get(db_nslcmop, ('_admin', 'pla'))
1456
1457 if not pla_result:
1458 raise LcmException("Placement timeout for nslcmopId={}".format(db_nslcmop['id']))
1459
1460 for pla_vnf in pla_result['vnf']:
1461 vnfr = db_vnfrs.get(pla_vnf['member-vnf-index'])
1462 if not pla_vnf.get('vimAccountId') or not vnfr:
1463 continue
1464 self.db.set_one("vnfrs", {"_id": vnfr["_id"]}, {"vim-account-id": pla_vnf['vimAccountId']})
1465 return
1466
1467 def update_nsrs_with_pla_result(self, params):
1468 try:
1469 nslcmop_id = deep_get(params, ('placement', 'nslcmopId'))
1470 self.update_db_2("nslcmops", nslcmop_id, {"_admin.pla": params.get('placement')})
1471 except Exception as e:
1472 self.logger.warn('Update failed for nslcmop_id={}:{}'.format(nslcmop_id, e))
1473
tierno59d22d22018-09-25 18:10:19 +02001474 async def instantiate(self, nsr_id, nslcmop_id):
quilesj7e13aeb2019-10-08 13:34:55 +02001475 """
1476
1477 :param nsr_id: ns instance to deploy
1478 :param nslcmop_id: operation to run
1479 :return:
1480 """
kuused124bfe2019-06-18 12:09:24 +02001481
1482 # Try to lock HA task here
1483 task_is_locked_by_me = self.lcm_tasks.lock_HA('ns', 'nslcmops', nslcmop_id)
1484 if not task_is_locked_by_me:
quilesj3655ae02019-12-12 16:08:35 +00001485 self.logger.debug('instantiate() task is not locked by me, ns={}'.format(nsr_id))
kuused124bfe2019-06-18 12:09:24 +02001486 return
1487
tierno59d22d22018-09-25 18:10:19 +02001488 logging_text = "Task ns={} instantiate={} ".format(nsr_id, nslcmop_id)
1489 self.logger.debug(logging_text + "Enter")
quilesj7e13aeb2019-10-08 13:34:55 +02001490
tierno59d22d22018-09-25 18:10:19 +02001491 # get all needed from database
quilesj7e13aeb2019-10-08 13:34:55 +02001492
1493 # database nsrs record
tierno59d22d22018-09-25 18:10:19 +02001494 db_nsr = None
quilesj7e13aeb2019-10-08 13:34:55 +02001495
1496 # database nslcmops record
tierno59d22d22018-09-25 18:10:19 +02001497 db_nslcmop = None
quilesj7e13aeb2019-10-08 13:34:55 +02001498
1499 # update operation on nsrs
tiernoe876f672020-02-13 14:34:48 +00001500 db_nsr_update = {}
quilesj7e13aeb2019-10-08 13:34:55 +02001501 # update operation on nslcmops
tierno59d22d22018-09-25 18:10:19 +02001502 db_nslcmop_update = {}
quilesj7e13aeb2019-10-08 13:34:55 +02001503
tierno59d22d22018-09-25 18:10:19 +02001504 nslcmop_operation_state = None
quilesj7e13aeb2019-10-08 13:34:55 +02001505 db_vnfrs = {} # vnf's info indexed by member-index
1506 # n2vc_info = {}
tiernoe876f672020-02-13 14:34:48 +00001507 tasks_dict_info = {} # from task to info text
tierno59d22d22018-09-25 18:10:19 +02001508 exc = None
tiernoe876f672020-02-13 14:34:48 +00001509 error_list = []
1510 stage = ['Stage 1/5: preparation of the environment.', "Waiting for previous operations to terminate.", ""]
1511 # ^ stage, step, VIM progress
tierno59d22d22018-09-25 18:10:19 +02001512 try:
kuused124bfe2019-06-18 12:09:24 +02001513 # wait for any previous tasks in process
1514 await self.lcm_tasks.waitfor_related_HA('ns', 'nslcmops', nslcmop_id)
1515
quilesj7e13aeb2019-10-08 13:34:55 +02001516 # STEP 0: Reading database (nslcmops, nsrs, nsds, vnfrs, vnfds)
tiernoe876f672020-02-13 14:34:48 +00001517 stage[1] = "Reading from database,"
quilesj4cda56b2019-12-05 10:02:20 +00001518 # nsState="BUILDING", currentOperation="INSTANTIATING", currentOperationID=nslcmop_id
tiernoe876f672020-02-13 14:34:48 +00001519 db_nsr_update["detailed-status"] = "creating"
1520 db_nsr_update["operational-status"] = "init"
quilesj4cda56b2019-12-05 10:02:20 +00001521 self._write_ns_status(
1522 nsr_id=nsr_id,
1523 ns_state="BUILDING",
1524 current_operation="INSTANTIATING",
tiernoe876f672020-02-13 14:34:48 +00001525 current_operation_id=nslcmop_id,
1526 other_update=db_nsr_update
1527 )
1528 self._write_op_status(
1529 op_id=nslcmop_id,
1530 stage=stage,
1531 queuePosition=0
quilesj4cda56b2019-12-05 10:02:20 +00001532 )
1533
quilesj7e13aeb2019-10-08 13:34:55 +02001534 # read from db: operation
tiernoe876f672020-02-13 14:34:48 +00001535 stage[1] = "Getting nslcmop={} from db".format(nslcmop_id)
tierno59d22d22018-09-25 18:10:19 +02001536 db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
tierno744303e2020-01-13 16:46:31 +00001537 ns_params = db_nslcmop.get("operationParams")
1538 if ns_params and ns_params.get("timeout_ns_deploy"):
1539 timeout_ns_deploy = ns_params["timeout_ns_deploy"]
1540 else:
1541 timeout_ns_deploy = self.timeout.get("ns_deploy", self.timeout_ns_deploy)
quilesj7e13aeb2019-10-08 13:34:55 +02001542
1543 # read from db: ns
tiernoe876f672020-02-13 14:34:48 +00001544 stage[1] = "Getting nsr={} from db".format(nsr_id)
tierno59d22d22018-09-25 18:10:19 +02001545 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
quilesj7e13aeb2019-10-08 13:34:55 +02001546 # nsd is replicated into ns (no db read)
tierno59d22d22018-09-25 18:10:19 +02001547 nsd = db_nsr["nsd"]
tiernod8323042019-08-09 11:32:23 +00001548 # nsr_name = db_nsr["name"] # TODO short-name??
tierno47e86b52018-10-10 14:05:55 +02001549
quilesj7e13aeb2019-10-08 13:34:55 +02001550 # read from db: vnf's of this ns
tiernoe876f672020-02-13 14:34:48 +00001551 stage[1] = "Getting vnfrs from db"
1552 self.logger.debug(logging_text + stage[1])
tierno27246d82018-09-27 15:59:09 +02001553 db_vnfrs_list = self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id})
tierno27246d82018-09-27 15:59:09 +02001554
quilesj7e13aeb2019-10-08 13:34:55 +02001555 # read from db: vnfd's for every vnf
1556 db_vnfds_ref = {} # every vnfd data indexed by vnf name
1557 db_vnfds = {} # every vnfd data indexed by vnf id
1558 db_vnfds_index = {} # every vnfd data indexed by vnf member-index
1559
1560 # for each vnf in ns, read vnfd
tierno27246d82018-09-27 15:59:09 +02001561 for vnfr in db_vnfrs_list:
quilesj7e13aeb2019-10-08 13:34:55 +02001562 db_vnfrs[vnfr["member-vnf-index-ref"]] = vnfr # vnf's dict indexed by member-index: '1', '2', etc
1563 vnfd_id = vnfr["vnfd-id"] # vnfd uuid for this vnf
1564 vnfd_ref = vnfr["vnfd-ref"] # vnfd name for this vnf
1565 # if we haven't this vnfd, read it from db
tierno27246d82018-09-27 15:59:09 +02001566 if vnfd_id not in db_vnfds:
quilesj63f90042020-01-17 09:53:55 +00001567 # read from db
tiernoe876f672020-02-13 14:34:48 +00001568 stage[1] = "Getting vnfd={} id='{}' from db".format(vnfd_id, vnfd_ref)
1569 self.logger.debug(logging_text + stage[1])
tierno27246d82018-09-27 15:59:09 +02001570 vnfd = self.db.get_one("vnfds", {"_id": vnfd_id})
tierno27246d82018-09-27 15:59:09 +02001571
quilesj7e13aeb2019-10-08 13:34:55 +02001572 # store vnfd
1573 db_vnfds_ref[vnfd_ref] = vnfd # vnfd's indexed by name
1574 db_vnfds[vnfd_id] = vnfd # vnfd's indexed by id
1575 db_vnfds_index[vnfr["member-vnf-index-ref"]] = db_vnfds[vnfd_id] # vnfd's indexed by member-index
1576
1577 # Get or generates the _admin.deployed.VCA list
tiernoe4f7e6c2018-11-27 14:55:30 +00001578 vca_deployed_list = None
1579 if db_nsr["_admin"].get("deployed"):
1580 vca_deployed_list = db_nsr["_admin"]["deployed"].get("VCA")
1581 if vca_deployed_list is None:
1582 vca_deployed_list = []
quilesj3655ae02019-12-12 16:08:35 +00001583 configuration_status_list = []
tiernoe4f7e6c2018-11-27 14:55:30 +00001584 db_nsr_update["_admin.deployed.VCA"] = vca_deployed_list
quilesj3655ae02019-12-12 16:08:35 +00001585 db_nsr_update["configurationStatus"] = configuration_status_list
quilesj7e13aeb2019-10-08 13:34:55 +02001586 # add _admin.deployed.VCA to db_nsr dictionary, value=vca_deployed_list
tierno98ad6ea2019-05-30 17:16:28 +00001587 populate_dict(db_nsr, ("_admin", "deployed", "VCA"), vca_deployed_list)
tiernoe4f7e6c2018-11-27 14:55:30 +00001588 elif isinstance(vca_deployed_list, dict):
1589 # maintain backward compatibility. Change a dict to list at database
1590 vca_deployed_list = list(vca_deployed_list.values())
1591 db_nsr_update["_admin.deployed.VCA"] = vca_deployed_list
tierno98ad6ea2019-05-30 17:16:28 +00001592 populate_dict(db_nsr, ("_admin", "deployed", "VCA"), vca_deployed_list)
tiernoe4f7e6c2018-11-27 14:55:30 +00001593
tierno6cf25f52019-09-12 09:33:40 +00001594 if not isinstance(deep_get(db_nsr, ("_admin", "deployed", "RO", "vnfd")), list):
tiernoa009e552019-01-30 16:45:44 +00001595 populate_dict(db_nsr, ("_admin", "deployed", "RO", "vnfd"), [])
1596 db_nsr_update["_admin.deployed.RO.vnfd"] = []
tierno59d22d22018-09-25 18:10:19 +02001597
tiernobaa51102018-12-14 13:16:18 +00001598 # set state to INSTANTIATED. When instantiated NBI will not delete directly
1599 db_nsr_update["_admin.nsState"] = "INSTANTIATED"
1600 self.update_db_2("nsrs", nsr_id, db_nsr_update)
quilesj3655ae02019-12-12 16:08:35 +00001601
1602 # n2vc_redesign STEP 2 Deploy Network Scenario
tiernoe876f672020-02-13 14:34:48 +00001603 stage[0] = 'Stage 2/5: deployment of KDUs, VMs and execution environments.'
quilesj3655ae02019-12-12 16:08:35 +00001604 self._write_op_status(
1605 op_id=nslcmop_id,
tiernoe876f672020-02-13 14:34:48 +00001606 stage=stage
quilesj3655ae02019-12-12 16:08:35 +00001607 )
1608
tiernoe876f672020-02-13 14:34:48 +00001609 stage[1] = "Deploying KDUs,"
1610 # self.logger.debug(logging_text + "Before deploy_kdus")
calvinosanch9f9c6f22019-11-04 13:37:39 +01001611 # Call to deploy_kdus in case exists the "vdu:kdu" param
tiernoe876f672020-02-13 14:34:48 +00001612 await self.deploy_kdus(
1613 logging_text=logging_text,
1614 nsr_id=nsr_id,
1615 nslcmop_id=nslcmop_id,
1616 db_vnfrs=db_vnfrs,
1617 db_vnfds=db_vnfds,
1618 task_instantiation_info=tasks_dict_info,
calvinosanch9f9c6f22019-11-04 13:37:39 +01001619 )
tiernoe876f672020-02-13 14:34:48 +00001620
1621 stage[1] = "Getting VCA public key."
tiernod8323042019-08-09 11:32:23 +00001622 # n2vc_redesign STEP 1 Get VCA public ssh-key
1623 # feature 1429. Add n2vc public key to needed VMs
tierno3bedc9b2019-11-27 15:46:57 +00001624 n2vc_key = self.n2vc.get_public_key()
tiernoa5088192019-11-26 16:12:53 +00001625 n2vc_key_list = [n2vc_key]
1626 if self.vca_config.get("public_key"):
1627 n2vc_key_list.append(self.vca_config["public_key"])
tierno98ad6ea2019-05-30 17:16:28 +00001628
tiernoe876f672020-02-13 14:34:48 +00001629 stage[1] = "Deploying NS at VIM."
tiernod8323042019-08-09 11:32:23 +00001630 task_ro = asyncio.ensure_future(
quilesj7e13aeb2019-10-08 13:34:55 +02001631 self.instantiate_RO(
1632 logging_text=logging_text,
1633 nsr_id=nsr_id,
1634 nsd=nsd,
1635 db_nsr=db_nsr,
1636 db_nslcmop=db_nslcmop,
1637 db_vnfrs=db_vnfrs,
1638 db_vnfds_ref=db_vnfds_ref,
tiernoe876f672020-02-13 14:34:48 +00001639 n2vc_key_list=n2vc_key_list,
1640 stage=stage
tierno98ad6ea2019-05-30 17:16:28 +00001641 )
tiernod8323042019-08-09 11:32:23 +00001642 )
1643 self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "instantiate_RO", task_ro)
tiernoa2143262020-03-27 16:20:40 +00001644 tasks_dict_info[task_ro] = "Deploying at VIM"
tierno98ad6ea2019-05-30 17:16:28 +00001645
tiernod8323042019-08-09 11:32:23 +00001646 # n2vc_redesign STEP 3 to 6 Deploy N2VC
tiernoe876f672020-02-13 14:34:48 +00001647 stage[1] = "Deploying Execution Environments."
1648 self.logger.debug(logging_text + stage[1])
tierno98ad6ea2019-05-30 17:16:28 +00001649
tiernod8323042019-08-09 11:32:23 +00001650 nsi_id = None # TODO put nsi_id when this nsr belongs to a NSI
quilesj7e13aeb2019-10-08 13:34:55 +02001651 # get_iterable() returns a value from a dict or empty tuple if key does not exist
tierno98ad6ea2019-05-30 17:16:28 +00001652 for c_vnf in get_iterable(nsd, "constituent-vnfd"):
1653 vnfd_id = c_vnf["vnfd-id-ref"]
tierno98ad6ea2019-05-30 17:16:28 +00001654 vnfd = db_vnfds_ref[vnfd_id]
tiernod8323042019-08-09 11:32:23 +00001655 member_vnf_index = str(c_vnf["member-vnf-index"])
1656 db_vnfr = db_vnfrs[member_vnf_index]
1657 base_folder = vnfd["_admin"]["storage"]
1658 vdu_id = None
1659 vdu_index = 0
tierno98ad6ea2019-05-30 17:16:28 +00001660 vdu_name = None
calvinosanch9f9c6f22019-11-04 13:37:39 +01001661 kdu_name = None
tierno59d22d22018-09-25 18:10:19 +02001662
tierno8a518872018-12-21 13:42:14 +00001663 # Get additional parameters
tiernod8323042019-08-09 11:32:23 +00001664 deploy_params = {}
1665 if db_vnfr.get("additionalParamsForVnf"):
tierno626e0152019-11-29 14:16:16 +00001666 deploy_params = self._format_additional_params(db_vnfr["additionalParamsForVnf"].copy())
tierno8a518872018-12-21 13:42:14 +00001667
tiernod8323042019-08-09 11:32:23 +00001668 descriptor_config = vnfd.get("vnf-configuration")
1669 if descriptor_config and descriptor_config.get("juju"):
quilesj7e13aeb2019-10-08 13:34:55 +02001670 self._deploy_n2vc(
tiernoa54150d2019-12-05 17:15:10 +00001671 logging_text=logging_text + "member_vnf_index={} ".format(member_vnf_index),
quilesj7e13aeb2019-10-08 13:34:55 +02001672 db_nsr=db_nsr,
1673 db_vnfr=db_vnfr,
1674 nslcmop_id=nslcmop_id,
1675 nsr_id=nsr_id,
1676 nsi_id=nsi_id,
1677 vnfd_id=vnfd_id,
1678 vdu_id=vdu_id,
calvinosanch9f9c6f22019-11-04 13:37:39 +01001679 kdu_name=kdu_name,
quilesj7e13aeb2019-10-08 13:34:55 +02001680 member_vnf_index=member_vnf_index,
1681 vdu_index=vdu_index,
1682 vdu_name=vdu_name,
1683 deploy_params=deploy_params,
1684 descriptor_config=descriptor_config,
1685 base_folder=base_folder,
tiernoe876f672020-02-13 14:34:48 +00001686 task_instantiation_info=tasks_dict_info,
1687 stage=stage
quilesj7e13aeb2019-10-08 13:34:55 +02001688 )
tierno59d22d22018-09-25 18:10:19 +02001689
1690 # Deploy charms for each VDU that supports one.
tiernod8323042019-08-09 11:32:23 +00001691 for vdud in get_iterable(vnfd, 'vdu'):
1692 vdu_id = vdud["id"]
1693 descriptor_config = vdud.get('vdu-configuration')
tierno626e0152019-11-29 14:16:16 +00001694 vdur = next((x for x in db_vnfr["vdur"] if x["vdu-id-ref"] == vdu_id), None)
1695 if vdur.get("additionalParams"):
1696 deploy_params_vdu = self._format_additional_params(vdur["additionalParams"])
1697 else:
1698 deploy_params_vdu = deploy_params
tiernod8323042019-08-09 11:32:23 +00001699 if descriptor_config and descriptor_config.get("juju"):
1700 # look for vdu index in the db_vnfr["vdu"] section
1701 # for vdur_index, vdur in enumerate(db_vnfr["vdur"]):
1702 # if vdur["vdu-id-ref"] == vdu_id:
1703 # break
1704 # else:
1705 # raise LcmException("Mismatch vdu_id={} not found in the vnfr['vdur'] list for "
1706 # "member_vnf_index={}".format(vdu_id, member_vnf_index))
1707 # vdu_name = vdur.get("name")
1708 vdu_name = None
calvinosanch9f9c6f22019-11-04 13:37:39 +01001709 kdu_name = None
tiernod8323042019-08-09 11:32:23 +00001710 for vdu_index in range(int(vdud.get("count", 1))):
1711 # TODO vnfr_params["rw_mgmt_ip"] = vdur["ip-address"]
quilesj7e13aeb2019-10-08 13:34:55 +02001712 self._deploy_n2vc(
tiernoa54150d2019-12-05 17:15:10 +00001713 logging_text=logging_text + "member_vnf_index={}, vdu_id={}, vdu_index={} ".format(
1714 member_vnf_index, vdu_id, vdu_index),
quilesj7e13aeb2019-10-08 13:34:55 +02001715 db_nsr=db_nsr,
1716 db_vnfr=db_vnfr,
1717 nslcmop_id=nslcmop_id,
1718 nsr_id=nsr_id,
1719 nsi_id=nsi_id,
1720 vnfd_id=vnfd_id,
1721 vdu_id=vdu_id,
calvinosanch9f9c6f22019-11-04 13:37:39 +01001722 kdu_name=kdu_name,
quilesj7e13aeb2019-10-08 13:34:55 +02001723 member_vnf_index=member_vnf_index,
1724 vdu_index=vdu_index,
1725 vdu_name=vdu_name,
tierno626e0152019-11-29 14:16:16 +00001726 deploy_params=deploy_params_vdu,
quilesj7e13aeb2019-10-08 13:34:55 +02001727 descriptor_config=descriptor_config,
1728 base_folder=base_folder,
tierno8e2fae72020-04-01 15:21:15 +00001729 task_instantiation_info=tasks_dict_info,
1730 stage=stage
quilesj7e13aeb2019-10-08 13:34:55 +02001731 )
calvinosanch9f9c6f22019-11-04 13:37:39 +01001732 for kdud in get_iterable(vnfd, 'kdu'):
1733 kdu_name = kdud["name"]
1734 descriptor_config = kdud.get('kdu-configuration')
1735 if descriptor_config and descriptor_config.get("juju"):
1736 vdu_id = None
1737 vdu_index = 0
1738 vdu_name = None
1739 # look for vdu index in the db_vnfr["vdu"] section
1740 # for vdur_index, vdur in enumerate(db_vnfr["vdur"]):
1741 # if vdur["vdu-id-ref"] == vdu_id:
1742 # break
1743 # else:
1744 # raise LcmException("Mismatch vdu_id={} not found in the vnfr['vdur'] list for "
1745 # "member_vnf_index={}".format(vdu_id, member_vnf_index))
1746 # vdu_name = vdur.get("name")
1747 # vdu_name = None
tierno59d22d22018-09-25 18:10:19 +02001748
calvinosanch9f9c6f22019-11-04 13:37:39 +01001749 self._deploy_n2vc(
1750 logging_text=logging_text,
1751 db_nsr=db_nsr,
1752 db_vnfr=db_vnfr,
1753 nslcmop_id=nslcmop_id,
1754 nsr_id=nsr_id,
1755 nsi_id=nsi_id,
1756 vnfd_id=vnfd_id,
1757 vdu_id=vdu_id,
1758 kdu_name=kdu_name,
1759 member_vnf_index=member_vnf_index,
1760 vdu_index=vdu_index,
1761 vdu_name=vdu_name,
1762 deploy_params=deploy_params,
1763 descriptor_config=descriptor_config,
1764 base_folder=base_folder,
tierno8e2fae72020-04-01 15:21:15 +00001765 task_instantiation_info=tasks_dict_info,
1766 stage=stage
calvinosanch9f9c6f22019-11-04 13:37:39 +01001767 )
tierno59d22d22018-09-25 18:10:19 +02001768
tierno1b633412019-02-25 16:48:23 +00001769 # Check if this NS has a charm configuration
tiernod8323042019-08-09 11:32:23 +00001770 descriptor_config = nsd.get("ns-configuration")
1771 if descriptor_config and descriptor_config.get("juju"):
1772 vnfd_id = None
1773 db_vnfr = None
1774 member_vnf_index = None
1775 vdu_id = None
calvinosanch9f9c6f22019-11-04 13:37:39 +01001776 kdu_name = None
tiernod8323042019-08-09 11:32:23 +00001777 vdu_index = 0
1778 vdu_name = None
tierno1b633412019-02-25 16:48:23 +00001779
tiernod8323042019-08-09 11:32:23 +00001780 # Get additional parameters
1781 deploy_params = {}
1782 if db_nsr.get("additionalParamsForNs"):
tierno626e0152019-11-29 14:16:16 +00001783 deploy_params = self._format_additional_params(db_nsr["additionalParamsForNs"].copy())
tiernod8323042019-08-09 11:32:23 +00001784 base_folder = nsd["_admin"]["storage"]
quilesj7e13aeb2019-10-08 13:34:55 +02001785 self._deploy_n2vc(
1786 logging_text=logging_text,
1787 db_nsr=db_nsr,
1788 db_vnfr=db_vnfr,
1789 nslcmop_id=nslcmop_id,
1790 nsr_id=nsr_id,
1791 nsi_id=nsi_id,
1792 vnfd_id=vnfd_id,
1793 vdu_id=vdu_id,
calvinosanch9f9c6f22019-11-04 13:37:39 +01001794 kdu_name=kdu_name,
quilesj7e13aeb2019-10-08 13:34:55 +02001795 member_vnf_index=member_vnf_index,
1796 vdu_index=vdu_index,
1797 vdu_name=vdu_name,
1798 deploy_params=deploy_params,
1799 descriptor_config=descriptor_config,
1800 base_folder=base_folder,
tierno8e2fae72020-04-01 15:21:15 +00001801 task_instantiation_info=tasks_dict_info,
1802 stage=stage
quilesj7e13aeb2019-10-08 13:34:55 +02001803 )
tierno1b633412019-02-25 16:48:23 +00001804
tiernoe876f672020-02-13 14:34:48 +00001805 # rest of staff will be done at finally
tierno1b633412019-02-25 16:48:23 +00001806
tiernoe876f672020-02-13 14:34:48 +00001807 except (ROclient.ROClientException, DbException, LcmException, N2VCException) as e:
1808 self.logger.error(logging_text + "Exit Exception while '{}': {}".format(stage[1], e))
tierno59d22d22018-09-25 18:10:19 +02001809 exc = e
1810 except asyncio.CancelledError:
tiernoe876f672020-02-13 14:34:48 +00001811 self.logger.error(logging_text + "Cancelled Exception while '{}'".format(stage[1]))
tierno59d22d22018-09-25 18:10:19 +02001812 exc = "Operation was cancelled"
1813 except Exception as e:
1814 exc = traceback.format_exc()
tiernoe876f672020-02-13 14:34:48 +00001815 self.logger.critical(logging_text + "Exit Exception while '{}': {}".format(stage[1], e), exc_info=True)
tierno59d22d22018-09-25 18:10:19 +02001816 finally:
1817 if exc:
tiernoe876f672020-02-13 14:34:48 +00001818 error_list.append(str(exc))
tiernobaa51102018-12-14 13:16:18 +00001819 try:
tiernoe876f672020-02-13 14:34:48 +00001820 # wait for pending tasks
1821 if tasks_dict_info:
1822 stage[1] = "Waiting for instantiate pending tasks."
1823 self.logger.debug(logging_text + stage[1])
1824 error_list += await self._wait_for_tasks(logging_text, tasks_dict_info, timeout_ns_deploy,
1825 stage, nslcmop_id, nsr_id=nsr_id)
1826 stage[1] = stage[2] = ""
1827 except asyncio.CancelledError:
1828 error_list.append("Cancelled")
1829 # TODO cancel all tasks
1830 except Exception as exc:
1831 error_list.append(str(exc))
quilesj4cda56b2019-12-05 10:02:20 +00001832
tiernoe876f672020-02-13 14:34:48 +00001833 # update operation-status
1834 db_nsr_update["operational-status"] = "running"
1835 # let's begin with VCA 'configured' status (later we can change it)
1836 db_nsr_update["config-status"] = "configured"
1837 for task, task_name in tasks_dict_info.items():
1838 if not task.done() or task.cancelled() or task.exception():
1839 if task_name.startswith(self.task_name_deploy_vca):
1840 # A N2VC task is pending
1841 db_nsr_update["config-status"] = "failed"
quilesj4cda56b2019-12-05 10:02:20 +00001842 else:
tiernoe876f672020-02-13 14:34:48 +00001843 # RO or KDU task is pending
1844 db_nsr_update["operational-status"] = "failed"
quilesj3655ae02019-12-12 16:08:35 +00001845
tiernoe876f672020-02-13 14:34:48 +00001846 # update status at database
1847 if error_list:
tiernoa2143262020-03-27 16:20:40 +00001848 error_detail = ". ".join(error_list)
tiernoe876f672020-02-13 14:34:48 +00001849 self.logger.error(logging_text + error_detail)
tiernoa2143262020-03-27 16:20:40 +00001850 error_description_nslcmop = 'Stage: {}. Detail: {}'.format(stage[0], error_detail)
1851 error_description_nsr = 'Operation: INSTANTIATING.{}, Stage {}'.format(nslcmop_id, stage[0])
quilesj3655ae02019-12-12 16:08:35 +00001852
tiernoa2143262020-03-27 16:20:40 +00001853 db_nsr_update["detailed-status"] = error_description_nsr + " Detail: " + error_detail
tiernoe876f672020-02-13 14:34:48 +00001854 db_nslcmop_update["detailed-status"] = error_detail
1855 nslcmop_operation_state = "FAILED"
1856 ns_state = "BROKEN"
1857 else:
tiernoa2143262020-03-27 16:20:40 +00001858 error_detail = None
tiernoe876f672020-02-13 14:34:48 +00001859 error_description_nsr = error_description_nslcmop = None
1860 ns_state = "READY"
1861 db_nsr_update["detailed-status"] = "Done"
1862 db_nslcmop_update["detailed-status"] = "Done"
1863 nslcmop_operation_state = "COMPLETED"
quilesj4cda56b2019-12-05 10:02:20 +00001864
tiernoe876f672020-02-13 14:34:48 +00001865 if db_nsr:
1866 self._write_ns_status(
1867 nsr_id=nsr_id,
1868 ns_state=ns_state,
1869 current_operation="IDLE",
1870 current_operation_id=None,
1871 error_description=error_description_nsr,
tiernoa2143262020-03-27 16:20:40 +00001872 error_detail=error_detail,
tiernoe876f672020-02-13 14:34:48 +00001873 other_update=db_nsr_update
1874 )
1875 if db_nslcmop:
1876 self._write_op_status(
1877 op_id=nslcmop_id,
1878 stage="",
1879 error_message=error_description_nslcmop,
1880 operation_state=nslcmop_operation_state,
1881 other_update=db_nslcmop_update,
1882 )
quilesj3655ae02019-12-12 16:08:35 +00001883
tierno59d22d22018-09-25 18:10:19 +02001884 if nslcmop_operation_state:
1885 try:
1886 await self.msg.aiowrite("ns", "instantiated", {"nsr_id": nsr_id, "nslcmop_id": nslcmop_id,
tierno8a518872018-12-21 13:42:14 +00001887 "operationState": nslcmop_operation_state},
1888 loop=self.loop)
tierno59d22d22018-09-25 18:10:19 +02001889 except Exception as e:
1890 self.logger.error(logging_text + "kafka_write notification Exception {}".format(e))
1891
1892 self.logger.debug(logging_text + "Exit")
1893 self.lcm_tasks.remove("ns", nsr_id, nslcmop_id, "ns_instantiate")
1894
quilesj63f90042020-01-17 09:53:55 +00001895 async def _add_vca_relations(self, logging_text, nsr_id, vca_index: int, timeout: int = 3600) -> bool:
1896
1897 # steps:
1898 # 1. find all relations for this VCA
1899 # 2. wait for other peers related
1900 # 3. add relations
1901
1902 try:
1903
1904 # STEP 1: find all relations for this VCA
1905
1906 # read nsr record
1907 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
1908
1909 # this VCA data
1910 my_vca = deep_get(db_nsr, ('_admin', 'deployed', 'VCA'))[vca_index]
1911
1912 # read all ns-configuration relations
1913 ns_relations = list()
1914 db_ns_relations = deep_get(db_nsr, ('nsd', 'ns-configuration', 'relation'))
1915 if db_ns_relations:
1916 for r in db_ns_relations:
1917 # check if this VCA is in the relation
1918 if my_vca.get('member-vnf-index') in\
1919 (r.get('entities')[0].get('id'), r.get('entities')[1].get('id')):
1920 ns_relations.append(r)
1921
1922 # read all vnf-configuration relations
1923 vnf_relations = list()
1924 db_vnfd_list = db_nsr.get('vnfd-id')
1925 if db_vnfd_list:
1926 for vnfd in db_vnfd_list:
1927 db_vnfd = self.db.get_one("vnfds", {"_id": vnfd})
1928 db_vnf_relations = deep_get(db_vnfd, ('vnf-configuration', 'relation'))
1929 if db_vnf_relations:
1930 for r in db_vnf_relations:
1931 # check if this VCA is in the relation
1932 if my_vca.get('vdu_id') in (r.get('entities')[0].get('id'), r.get('entities')[1].get('id')):
1933 vnf_relations.append(r)
1934
1935 # if no relations, terminate
1936 if not ns_relations and not vnf_relations:
1937 self.logger.debug(logging_text + ' No relations')
1938 return True
1939
1940 self.logger.debug(logging_text + ' adding relations\n {}\n {}'.format(ns_relations, vnf_relations))
1941
1942 # add all relations
1943 start = time()
1944 while True:
1945 # check timeout
1946 now = time()
1947 if now - start >= timeout:
1948 self.logger.error(logging_text + ' : timeout adding relations')
1949 return False
1950
1951 # reload nsr from database (we need to update record: _admin.deloyed.VCA)
1952 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
1953
1954 # for each defined NS relation, find the VCA's related
1955 for r in ns_relations:
1956 from_vca_ee_id = None
1957 to_vca_ee_id = None
1958 from_vca_endpoint = None
1959 to_vca_endpoint = None
1960 vca_list = deep_get(db_nsr, ('_admin', 'deployed', 'VCA'))
1961 for vca in vca_list:
1962 if vca.get('member-vnf-index') == r.get('entities')[0].get('id') \
1963 and vca.get('config_sw_installed'):
1964 from_vca_ee_id = vca.get('ee_id')
1965 from_vca_endpoint = r.get('entities')[0].get('endpoint')
1966 if vca.get('member-vnf-index') == r.get('entities')[1].get('id') \
1967 and vca.get('config_sw_installed'):
1968 to_vca_ee_id = vca.get('ee_id')
1969 to_vca_endpoint = r.get('entities')[1].get('endpoint')
1970 if from_vca_ee_id and to_vca_ee_id:
1971 # add relation
1972 await self.n2vc.add_relation(
1973 ee_id_1=from_vca_ee_id,
1974 ee_id_2=to_vca_ee_id,
1975 endpoint_1=from_vca_endpoint,
1976 endpoint_2=to_vca_endpoint)
1977 # remove entry from relations list
1978 ns_relations.remove(r)
1979 else:
1980 # check failed peers
1981 try:
1982 vca_status_list = db_nsr.get('configurationStatus')
1983 if vca_status_list:
1984 for i in range(len(vca_list)):
1985 vca = vca_list[i]
1986 vca_status = vca_status_list[i]
1987 if vca.get('member-vnf-index') == r.get('entities')[0].get('id'):
1988 if vca_status.get('status') == 'BROKEN':
1989 # peer broken: remove relation from list
1990 ns_relations.remove(r)
1991 if vca.get('member-vnf-index') == r.get('entities')[1].get('id'):
1992 if vca_status.get('status') == 'BROKEN':
1993 # peer broken: remove relation from list
1994 ns_relations.remove(r)
1995 except Exception:
1996 # ignore
1997 pass
1998
1999 # for each defined VNF relation, find the VCA's related
2000 for r in vnf_relations:
2001 from_vca_ee_id = None
2002 to_vca_ee_id = None
2003 from_vca_endpoint = None
2004 to_vca_endpoint = None
2005 vca_list = deep_get(db_nsr, ('_admin', 'deployed', 'VCA'))
2006 for vca in vca_list:
2007 if vca.get('vdu_id') == r.get('entities')[0].get('id') and vca.get('config_sw_installed'):
2008 from_vca_ee_id = vca.get('ee_id')
2009 from_vca_endpoint = r.get('entities')[0].get('endpoint')
2010 if vca.get('vdu_id') == r.get('entities')[1].get('id') and vca.get('config_sw_installed'):
2011 to_vca_ee_id = vca.get('ee_id')
2012 to_vca_endpoint = r.get('entities')[1].get('endpoint')
2013 if from_vca_ee_id and to_vca_ee_id:
2014 # add relation
2015 await self.n2vc.add_relation(
2016 ee_id_1=from_vca_ee_id,
2017 ee_id_2=to_vca_ee_id,
2018 endpoint_1=from_vca_endpoint,
2019 endpoint_2=to_vca_endpoint)
2020 # remove entry from relations list
2021 vnf_relations.remove(r)
2022 else:
2023 # check failed peers
2024 try:
2025 vca_status_list = db_nsr.get('configurationStatus')
2026 if vca_status_list:
2027 for i in range(len(vca_list)):
2028 vca = vca_list[i]
2029 vca_status = vca_status_list[i]
2030 if vca.get('vdu_id') == r.get('entities')[0].get('id'):
2031 if vca_status.get('status') == 'BROKEN':
2032 # peer broken: remove relation from list
2033 ns_relations.remove(r)
2034 if vca.get('vdu_id') == r.get('entities')[1].get('id'):
2035 if vca_status.get('status') == 'BROKEN':
2036 # peer broken: remove relation from list
2037 ns_relations.remove(r)
2038 except Exception:
2039 # ignore
2040 pass
2041
2042 # wait for next try
2043 await asyncio.sleep(5.0)
2044
2045 if not ns_relations and not vnf_relations:
2046 self.logger.debug('Relations added')
2047 break
2048
2049 return True
2050
2051 except Exception as e:
2052 self.logger.warn(logging_text + ' ERROR adding relations: {}'.format(e))
2053 return False
2054
tiernoe876f672020-02-13 14:34:48 +00002055 async def deploy_kdus(self, logging_text, nsr_id, nslcmop_id, db_vnfrs, db_vnfds, task_instantiation_info):
calvinosanch9f9c6f22019-11-04 13:37:39 +01002056 # Launch kdus if present in the descriptor
tierno626e0152019-11-29 14:16:16 +00002057
2058 k8scluster_id_2_uuic = {"helm-chart": {}, "juju-bundle": {}}
2059
2060 def _get_cluster_id(cluster_id, cluster_type):
2061 nonlocal k8scluster_id_2_uuic
2062 if cluster_id in k8scluster_id_2_uuic[cluster_type]:
2063 return k8scluster_id_2_uuic[cluster_type][cluster_id]
2064
2065 db_k8scluster = self.db.get_one("k8sclusters", {"_id": cluster_id}, fail_on_empty=False)
2066 if not db_k8scluster:
2067 raise LcmException("K8s cluster {} cannot be found".format(cluster_id))
2068 k8s_id = deep_get(db_k8scluster, ("_admin", cluster_type, "id"))
2069 if not k8s_id:
2070 raise LcmException("K8s cluster '{}' has not been initilized for '{}'".format(cluster_id, cluster_type))
2071 k8scluster_id_2_uuic[cluster_type][cluster_id] = k8s_id
2072 return k8s_id
2073
2074 logging_text += "Deploy kdus: "
tiernoe876f672020-02-13 14:34:48 +00002075 step = ""
calvinosanch9f9c6f22019-11-04 13:37:39 +01002076 try:
tierno626e0152019-11-29 14:16:16 +00002077 db_nsr_update = {"_admin.deployed.K8s": []}
calvinosanch9f9c6f22019-11-04 13:37:39 +01002078 self.update_db_2("nsrs", nsr_id, db_nsr_update)
calvinosanch9f9c6f22019-11-04 13:37:39 +01002079
tierno626e0152019-11-29 14:16:16 +00002080 index = 0
tiernoe876f672020-02-13 14:34:48 +00002081 updated_cluster_list = []
2082
tierno626e0152019-11-29 14:16:16 +00002083 for vnfr_data in db_vnfrs.values():
2084 for kdur in get_iterable(vnfr_data, "kdur"):
2085 desc_params = self._format_additional_params(kdur.get("additionalParams"))
quilesjacde94f2020-01-23 10:07:08 +00002086 vnfd_id = vnfr_data.get('vnfd-id')
2087 pkgdir = deep_get(db_vnfds.get(vnfd_id), ('_admin', 'storage', 'pkg-dir'))
tierno626e0152019-11-29 14:16:16 +00002088 if kdur.get("helm-chart"):
2089 kdumodel = kdur["helm-chart"]
tiernoe876f672020-02-13 14:34:48 +00002090 k8sclustertype = "helm-chart"
tierno626e0152019-11-29 14:16:16 +00002091 elif kdur.get("juju-bundle"):
2092 kdumodel = kdur["juju-bundle"]
tiernoe876f672020-02-13 14:34:48 +00002093 k8sclustertype = "juju-bundle"
tierno626e0152019-11-29 14:16:16 +00002094 else:
tiernoe876f672020-02-13 14:34:48 +00002095 raise LcmException("kdu type for kdu='{}.{}' is neither helm-chart nor "
2096 "juju-bundle. Maybe an old NBI version is running".
2097 format(vnfr_data["member-vnf-index-ref"], kdur["kdu-name"]))
quilesjacde94f2020-01-23 10:07:08 +00002098 # check if kdumodel is a file and exists
2099 try:
2100 # path format: /vnfdid/pkkdir/kdumodel
tiernoe876f672020-02-13 14:34:48 +00002101 filename = '{}/{}/{}s/{}'.format(vnfd_id, pkgdir, k8sclustertype, kdumodel)
quilesjacde94f2020-01-23 10:07:08 +00002102 if self.fs.file_exists(filename, mode='file') or self.fs.file_exists(filename, mode='dir'):
2103 kdumodel = self.fs.path + filename
tiernoe876f672020-02-13 14:34:48 +00002104 except asyncio.CancelledError:
2105 raise
2106 except Exception: # it is not a file
quilesjacde94f2020-01-23 10:07:08 +00002107 pass
lloretgallegedc5f332020-02-20 11:50:50 +01002108
tiernoe876f672020-02-13 14:34:48 +00002109 k8s_cluster_id = kdur["k8s-cluster"]["id"]
2110 step = "Synchronize repos for k8s cluster '{}'".format(k8s_cluster_id)
2111 cluster_uuid = _get_cluster_id(k8s_cluster_id, k8sclustertype)
lloretgallegedc5f332020-02-20 11:50:50 +01002112
tiernoe876f672020-02-13 14:34:48 +00002113 if k8sclustertype == "helm-chart" and cluster_uuid not in updated_cluster_list:
2114 del_repo_list, added_repo_dict = await asyncio.ensure_future(
2115 self.k8sclusterhelm.synchronize_repos(cluster_uuid=cluster_uuid))
2116 if del_repo_list or added_repo_dict:
2117 unset = {'_admin.helm_charts_added.' + item: None for item in del_repo_list}
2118 updated = {'_admin.helm_charts_added.' +
2119 item: name for item, name in added_repo_dict.items()}
2120 self.logger.debug(logging_text + "repos synchronized on k8s cluster '{}' to_delete: {}, "
2121 "to_add: {}".format(k8s_cluster_id, del_repo_list,
2122 added_repo_dict))
2123 self.db.set_one("k8sclusters", {"_id": k8s_cluster_id}, updated, unset=unset)
2124 updated_cluster_list.append(cluster_uuid)
lloretgallegedc5f332020-02-20 11:50:50 +01002125
tiernoe876f672020-02-13 14:34:48 +00002126 step = "Instantiating KDU {}.{} in k8s cluster {}".format(vnfr_data["member-vnf-index-ref"],
2127 kdur["kdu-name"], k8s_cluster_id)
tierno626e0152019-11-29 14:16:16 +00002128
2129 k8s_instace_info = {"kdu-instance": None, "k8scluster-uuid": cluster_uuid,
2130 "k8scluster-type": k8sclustertype,
2131 "kdu-name": kdur["kdu-name"], "kdu-model": kdumodel}
tierno626e0152019-11-29 14:16:16 +00002132 db_nsr_update["_admin.deployed.K8s.{}".format(index)] = k8s_instace_info
2133 self.update_db_2("nsrs", nsr_id, db_nsr_update)
tierno626e0152019-11-29 14:16:16 +00002134
tiernoe876f672020-02-13 14:34:48 +00002135 db_dict = {"collection": "nsrs",
2136 "filter": {"_id": nsr_id},
2137 "path": "_admin.deployed.K8s.{}".format(index)}
lloretgallegedc5f332020-02-20 11:50:50 +01002138
tiernoa2143262020-03-27 16:20:40 +00002139 task = asyncio.ensure_future(
2140 self.k8scluster_map[k8sclustertype].install(cluster_uuid=cluster_uuid, kdu_model=kdumodel,
2141 atomic=True, params=desc_params,
2142 db_dict=db_dict, timeout=600,
2143 kdu_name=kdur["kdu-name"]))
Adam Israelbaacc302019-12-01 12:41:39 -05002144
tiernoe876f672020-02-13 14:34:48 +00002145 self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "instantiate_KDU-{}".format(index), task)
tiernoa2143262020-03-27 16:20:40 +00002146 task_instantiation_info[task] = "Deploying KDU {}".format(kdur["kdu-name"])
tiernoe876f672020-02-13 14:34:48 +00002147
tierno626e0152019-11-29 14:16:16 +00002148 index += 1
quilesjdd799ac2020-01-23 16:31:11 +00002149
tiernoe876f672020-02-13 14:34:48 +00002150 except (LcmException, asyncio.CancelledError):
2151 raise
calvinosanch9f9c6f22019-11-04 13:37:39 +01002152 except Exception as e:
tiernoe876f672020-02-13 14:34:48 +00002153 msg = "Exception {} while {}: {}".format(type(e).__name__, step, e)
2154 if isinstance(e, (N2VCException, DbException)):
2155 self.logger.error(logging_text + msg)
2156 else:
2157 self.logger.critical(logging_text + msg, exc_info=True)
quilesjdd799ac2020-01-23 16:31:11 +00002158 raise LcmException(msg)
calvinosanch9f9c6f22019-11-04 13:37:39 +01002159 finally:
calvinosanch9f9c6f22019-11-04 13:37:39 +01002160 if db_nsr_update:
2161 self.update_db_2("nsrs", nsr_id, db_nsr_update)
tiernoda6fb102019-11-23 00:36:52 +00002162
quilesj7e13aeb2019-10-08 13:34:55 +02002163 def _deploy_n2vc(self, logging_text, db_nsr, db_vnfr, nslcmop_id, nsr_id, nsi_id, vnfd_id, vdu_id,
calvinosanch9f9c6f22019-11-04 13:37:39 +01002164 kdu_name, member_vnf_index, vdu_index, vdu_name, deploy_params, descriptor_config,
tiernoe876f672020-02-13 14:34:48 +00002165 base_folder, task_instantiation_info, stage):
quilesj7e13aeb2019-10-08 13:34:55 +02002166 # launch instantiate_N2VC in a asyncio task and register task object
2167 # Look where information of this charm is at database <nsrs>._admin.deployed.VCA
2168 # if not found, create one entry and update database
tiernobaa51102018-12-14 13:16:18 +00002169
quilesj7e13aeb2019-10-08 13:34:55 +02002170 # fill db_nsr._admin.deployed.VCA.<index>
2171 vca_index = -1
2172 for vca_index, vca_deployed in enumerate(db_nsr["_admin"]["deployed"]["VCA"]):
2173 if not vca_deployed:
2174 continue
2175 if vca_deployed.get("member-vnf-index") == member_vnf_index and \
2176 vca_deployed.get("vdu_id") == vdu_id and \
calvinosanch9f9c6f22019-11-04 13:37:39 +01002177 vca_deployed.get("kdu_name") == kdu_name and \
quilesj7e13aeb2019-10-08 13:34:55 +02002178 vca_deployed.get("vdu_count_index", 0) == vdu_index:
2179 break
2180 else:
2181 # not found, create one.
2182 vca_deployed = {
2183 "member-vnf-index": member_vnf_index,
2184 "vdu_id": vdu_id,
calvinosanch9f9c6f22019-11-04 13:37:39 +01002185 "kdu_name": kdu_name,
quilesj7e13aeb2019-10-08 13:34:55 +02002186 "vdu_count_index": vdu_index,
2187 "operational-status": "init", # TODO revise
2188 "detailed-status": "", # TODO revise
2189 "step": "initial-deploy", # TODO revise
2190 "vnfd_id": vnfd_id,
2191 "vdu_name": vdu_name,
2192 }
2193 vca_index += 1
quilesj3655ae02019-12-12 16:08:35 +00002194
2195 # create VCA and configurationStatus in db
2196 db_dict = {
2197 "_admin.deployed.VCA.{}".format(vca_index): vca_deployed,
2198 "configurationStatus.{}".format(vca_index): dict()
2199 }
2200 self.update_db_2("nsrs", nsr_id, db_dict)
2201
quilesj7e13aeb2019-10-08 13:34:55 +02002202 db_nsr["_admin"]["deployed"]["VCA"].append(vca_deployed)
2203
2204 # Launch task
2205 task_n2vc = asyncio.ensure_future(
2206 self.instantiate_N2VC(
2207 logging_text=logging_text,
2208 vca_index=vca_index,
2209 nsi_id=nsi_id,
2210 db_nsr=db_nsr,
2211 db_vnfr=db_vnfr,
2212 vdu_id=vdu_id,
calvinosanch9f9c6f22019-11-04 13:37:39 +01002213 kdu_name=kdu_name,
quilesj7e13aeb2019-10-08 13:34:55 +02002214 vdu_index=vdu_index,
2215 deploy_params=deploy_params,
2216 config_descriptor=descriptor_config,
2217 base_folder=base_folder,
tiernoe876f672020-02-13 14:34:48 +00002218 nslcmop_id=nslcmop_id,
2219 stage=stage
quilesj7e13aeb2019-10-08 13:34:55 +02002220 )
2221 )
2222 self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "instantiate_N2VC-{}".format(vca_index), task_n2vc)
tiernoe876f672020-02-13 14:34:48 +00002223 task_instantiation_info[task_n2vc] = self.task_name_deploy_vca + " {}.{}".format(
2224 member_vnf_index or "", vdu_id or "")
tiernobaa51102018-12-14 13:16:18 +00002225
kuuse0ca67472019-05-13 15:59:27 +02002226 # Check if this VNFD has a configured terminate action
2227 def _has_terminate_config_primitive(self, vnfd):
2228 vnf_config = vnfd.get("vnf-configuration")
2229 if vnf_config and vnf_config.get("terminate-config-primitive"):
2230 return True
2231 else:
2232 return False
2233
tiernoc9556972019-07-05 15:25:25 +00002234 @staticmethod
2235 def _get_terminate_config_primitive_seq_list(vnfd):
2236 """ Get a numerically sorted list of the sequences for this VNFD's terminate action """
kuuse0ca67472019-05-13 15:59:27 +02002237 # No need to check for existing primitive twice, already done before
2238 vnf_config = vnfd.get("vnf-configuration")
2239 seq_list = vnf_config.get("terminate-config-primitive")
2240 # Get all 'seq' tags in seq_list, order sequences numerically, ascending.
2241 seq_list_sorted = sorted(seq_list, key=lambda x: int(x['seq']))
2242 return seq_list_sorted
2243
2244 @staticmethod
2245 def _create_nslcmop(nsr_id, operation, params):
2246 """
2247 Creates a ns-lcm-opp content to be stored at database.
2248 :param nsr_id: internal id of the instance
2249 :param operation: instantiate, terminate, scale, action, ...
2250 :param params: user parameters for the operation
2251 :return: dictionary following SOL005 format
2252 """
2253 # Raise exception if invalid arguments
2254 if not (nsr_id and operation and params):
2255 raise LcmException(
2256 "Parameters 'nsr_id', 'operation' and 'params' needed to create primitive not provided")
2257 now = time()
2258 _id = str(uuid4())
2259 nslcmop = {
2260 "id": _id,
2261 "_id": _id,
2262 # COMPLETED,PARTIALLY_COMPLETED,FAILED_TEMP,FAILED,ROLLING_BACK,ROLLED_BACK
2263 "operationState": "PROCESSING",
2264 "statusEnteredTime": now,
2265 "nsInstanceId": nsr_id,
2266 "lcmOperationType": operation,
2267 "startTime": now,
2268 "isAutomaticInvocation": False,
2269 "operationParams": params,
2270 "isCancelPending": False,
2271 "links": {
2272 "self": "/osm/nslcm/v1/ns_lcm_op_occs/" + _id,
2273 "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsr_id,
2274 }
2275 }
2276 return nslcmop
2277
calvinosanch9f9c6f22019-11-04 13:37:39 +01002278 def _format_additional_params(self, params):
tierno626e0152019-11-29 14:16:16 +00002279 params = params or {}
calvinosanch9f9c6f22019-11-04 13:37:39 +01002280 for key, value in params.items():
2281 if str(value).startswith("!!yaml "):
2282 params[key] = yaml.safe_load(value[7:])
calvinosanch9f9c6f22019-11-04 13:37:39 +01002283 return params
2284
kuuse8b998e42019-07-30 15:22:16 +02002285 def _get_terminate_primitive_params(self, seq, vnf_index):
2286 primitive = seq.get('name')
2287 primitive_params = {}
2288 params = {
2289 "member_vnf_index": vnf_index,
2290 "primitive": primitive,
2291 "primitive_params": primitive_params,
2292 }
2293 desc_params = {}
2294 return self._map_primitive_params(seq, params, desc_params)
2295
kuuseac3a8882019-10-03 10:48:06 +02002296 # sub-operations
2297
2298 def _reintent_or_skip_suboperation(self, db_nslcmop, op_index):
2299 op = db_nslcmop.get('_admin', {}).get('operations', [])[op_index]
2300 if (op.get('operationState') == 'COMPLETED'):
2301 # b. Skip sub-operation
2302 # _ns_execute_primitive() or RO.create_action() will NOT be executed
2303 return self.SUBOPERATION_STATUS_SKIP
2304 else:
2305 # c. Reintent executing sub-operation
2306 # The sub-operation exists, and operationState != 'COMPLETED'
2307 # Update operationState = 'PROCESSING' to indicate a reintent.
2308 operationState = 'PROCESSING'
2309 detailed_status = 'In progress'
2310 self._update_suboperation_status(
2311 db_nslcmop, op_index, operationState, detailed_status)
2312 # Return the sub-operation index
2313 # _ns_execute_primitive() or RO.create_action() will be called from scale()
2314 # with arguments extracted from the sub-operation
2315 return op_index
2316
2317 # Find a sub-operation where all keys in a matching dictionary must match
2318 # Returns the index of the matching sub-operation, or SUBOPERATION_STATUS_NOT_FOUND if no match
2319 def _find_suboperation(self, db_nslcmop, match):
2320 if (db_nslcmop and match):
2321 op_list = db_nslcmop.get('_admin', {}).get('operations', [])
2322 for i, op in enumerate(op_list):
2323 if all(op.get(k) == match[k] for k in match):
2324 return i
2325 return self.SUBOPERATION_STATUS_NOT_FOUND
2326
2327 # Update status for a sub-operation given its index
2328 def _update_suboperation_status(self, db_nslcmop, op_index, operationState, detailed_status):
2329 # Update DB for HA tasks
2330 q_filter = {'_id': db_nslcmop['_id']}
2331 update_dict = {'_admin.operations.{}.operationState'.format(op_index): operationState,
2332 '_admin.operations.{}.detailed-status'.format(op_index): detailed_status}
2333 self.db.set_one("nslcmops",
2334 q_filter=q_filter,
2335 update_dict=update_dict,
2336 fail_on_empty=False)
2337
2338 # Add sub-operation, return the index of the added sub-operation
2339 # Optionally, set operationState, detailed-status, and operationType
2340 # Status and type are currently set for 'scale' sub-operations:
2341 # 'operationState' : 'PROCESSING' | 'COMPLETED' | 'FAILED'
2342 # 'detailed-status' : status message
2343 # 'operationType': may be any type, in the case of scaling: 'PRE-SCALE' | 'POST-SCALE'
2344 # Status and operation type are currently only used for 'scale', but NOT for 'terminate' sub-operations.
quilesj7e13aeb2019-10-08 13:34:55 +02002345 def _add_suboperation(self, db_nslcmop, vnf_index, vdu_id, vdu_count_index, vdu_name, primitive,
2346 mapped_primitive_params, operationState=None, detailed_status=None, operationType=None,
kuuseac3a8882019-10-03 10:48:06 +02002347 RO_nsr_id=None, RO_scaling_info=None):
tiernoe876f672020-02-13 14:34:48 +00002348 if not db_nslcmop:
kuuseac3a8882019-10-03 10:48:06 +02002349 return self.SUBOPERATION_STATUS_NOT_FOUND
2350 # Get the "_admin.operations" list, if it exists
2351 db_nslcmop_admin = db_nslcmop.get('_admin', {})
2352 op_list = db_nslcmop_admin.get('operations')
2353 # Create or append to the "_admin.operations" list
kuuse8b998e42019-07-30 15:22:16 +02002354 new_op = {'member_vnf_index': vnf_index,
2355 'vdu_id': vdu_id,
2356 'vdu_count_index': vdu_count_index,
2357 'primitive': primitive,
2358 'primitive_params': mapped_primitive_params}
kuuseac3a8882019-10-03 10:48:06 +02002359 if operationState:
2360 new_op['operationState'] = operationState
2361 if detailed_status:
2362 new_op['detailed-status'] = detailed_status
2363 if operationType:
2364 new_op['lcmOperationType'] = operationType
2365 if RO_nsr_id:
2366 new_op['RO_nsr_id'] = RO_nsr_id
2367 if RO_scaling_info:
2368 new_op['RO_scaling_info'] = RO_scaling_info
2369 if not op_list:
2370 # No existing operations, create key 'operations' with current operation as first list element
2371 db_nslcmop_admin.update({'operations': [new_op]})
2372 op_list = db_nslcmop_admin.get('operations')
2373 else:
2374 # Existing operations, append operation to list
2375 op_list.append(new_op)
kuuse8b998e42019-07-30 15:22:16 +02002376
kuuseac3a8882019-10-03 10:48:06 +02002377 db_nslcmop_update = {'_admin.operations': op_list}
2378 self.update_db_2("nslcmops", db_nslcmop['_id'], db_nslcmop_update)
2379 op_index = len(op_list) - 1
2380 return op_index
2381
2382 # Helper methods for scale() sub-operations
2383
2384 # pre-scale/post-scale:
2385 # Check for 3 different cases:
2386 # a. New: First time execution, return SUBOPERATION_STATUS_NEW
2387 # b. Skip: Existing sub-operation exists, operationState == 'COMPLETED', return SUBOPERATION_STATUS_SKIP
2388 # c. Reintent: Existing sub-operation exists, operationState != 'COMPLETED', return op_index to re-execute
quilesj7e13aeb2019-10-08 13:34:55 +02002389 def _check_or_add_scale_suboperation(self, db_nslcmop, vnf_index, vnf_config_primitive, primitive_params,
2390 operationType, RO_nsr_id=None, RO_scaling_info=None):
kuuseac3a8882019-10-03 10:48:06 +02002391 # Find this sub-operation
2392 if (RO_nsr_id and RO_scaling_info):
2393 operationType = 'SCALE-RO'
2394 match = {
2395 'member_vnf_index': vnf_index,
2396 'RO_nsr_id': RO_nsr_id,
2397 'RO_scaling_info': RO_scaling_info,
2398 }
2399 else:
2400 match = {
2401 'member_vnf_index': vnf_index,
2402 'primitive': vnf_config_primitive,
2403 'primitive_params': primitive_params,
2404 'lcmOperationType': operationType
2405 }
2406 op_index = self._find_suboperation(db_nslcmop, match)
2407 if (op_index == self.SUBOPERATION_STATUS_NOT_FOUND):
2408 # a. New sub-operation
2409 # The sub-operation does not exist, add it.
2410 # _ns_execute_primitive() will be called from scale() as usual, with non-modified arguments
2411 # The following parameters are set to None for all kind of scaling:
2412 vdu_id = None
2413 vdu_count_index = None
2414 vdu_name = None
2415 if (RO_nsr_id and RO_scaling_info):
2416 vnf_config_primitive = None
2417 primitive_params = None
2418 else:
2419 RO_nsr_id = None
2420 RO_scaling_info = None
2421 # Initial status for sub-operation
2422 operationState = 'PROCESSING'
2423 detailed_status = 'In progress'
2424 # Add sub-operation for pre/post-scaling (zero or more operations)
2425 self._add_suboperation(db_nslcmop,
2426 vnf_index,
2427 vdu_id,
2428 vdu_count_index,
2429 vdu_name,
2430 vnf_config_primitive,
2431 primitive_params,
2432 operationState,
2433 detailed_status,
2434 operationType,
2435 RO_nsr_id,
2436 RO_scaling_info)
2437 return self.SUBOPERATION_STATUS_NEW
2438 else:
2439 # Return either SUBOPERATION_STATUS_SKIP (operationState == 'COMPLETED'),
2440 # or op_index (operationState != 'COMPLETED')
2441 return self._reintent_or_skip_suboperation(db_nslcmop, op_index)
2442
preethika.pdf7d8e02019-12-10 13:10:48 +00002443 # Function to return execution_environment id
2444
2445 def _get_ee_id(self, vnf_index, vdu_id, vca_deployed_list):
tiernoe876f672020-02-13 14:34:48 +00002446 # TODO vdu_index_count
preethika.pdf7d8e02019-12-10 13:10:48 +00002447 for vca in vca_deployed_list:
2448 if vca["member-vnf-index"] == vnf_index and vca["vdu_id"] == vdu_id:
2449 return vca["ee_id"]
2450
tiernoe876f672020-02-13 14:34:48 +00002451 async def destroy_N2VC(self, logging_text, db_nslcmop, vca_deployed, config_descriptor, vca_index, destroy_ee=True):
2452 """
2453 Execute the terminate primitives and destroy the execution environment (if destroy_ee=False
2454 :param logging_text:
2455 :param db_nslcmop:
2456 :param vca_deployed: Dictionary of deployment info at db_nsr._admin.depoloyed.VCA.<INDEX>
2457 :param config_descriptor: Configuration descriptor of the NSD, VNFD, VNFD.vdu or VNFD.kdu
2458 :param vca_index: index in the database _admin.deployed.VCA
2459 :param destroy_ee: False to do not destroy, because it will be destroyed all of then at once
2460 :return: None or exception
2461 """
2462 # execute terminate_primitives
2463 terminate_primitives = config_descriptor.get("terminate-config-primitive")
2464 vdu_id = vca_deployed.get("vdu_id")
2465 vdu_count_index = vca_deployed.get("vdu_count_index")
2466 vdu_name = vca_deployed.get("vdu_name")
2467 vnf_index = vca_deployed.get("member-vnf-index")
2468 if terminate_primitives and vca_deployed.get("needed_terminate"):
2469 # Get all 'seq' tags in seq_list, order sequences numerically, ascending.
2470 terminate_primitives = sorted(terminate_primitives, key=lambda x: int(x['seq']))
2471 for seq in terminate_primitives:
kuuse8b998e42019-07-30 15:22:16 +02002472 # For each sequence in list, get primitive and call _ns_execute_primitive()
kuuse0ca67472019-05-13 15:59:27 +02002473 step = "Calling terminate action for vnf_member_index={} primitive={}".format(
2474 vnf_index, seq.get("name"))
2475 self.logger.debug(logging_text + step)
kuuse8b998e42019-07-30 15:22:16 +02002476 # Create the primitive for each sequence, i.e. "primitive": "touch"
kuuse0ca67472019-05-13 15:59:27 +02002477 primitive = seq.get('name')
kuuse8b998e42019-07-30 15:22:16 +02002478 mapped_primitive_params = self._get_terminate_primitive_params(seq, vnf_index)
2479 # The following 3 parameters are currently set to None for 'terminate':
2480 # vdu_id, vdu_count_index, vdu_name
tiernoe876f672020-02-13 14:34:48 +00002481
kuuseac3a8882019-10-03 10:48:06 +02002482 # Add sub-operation
kuuse8b998e42019-07-30 15:22:16 +02002483 self._add_suboperation(db_nslcmop,
kuuse8b998e42019-07-30 15:22:16 +02002484 vnf_index,
2485 vdu_id,
2486 vdu_count_index,
2487 vdu_name,
2488 primitive,
2489 mapped_primitive_params)
kuuseac3a8882019-10-03 10:48:06 +02002490 # Sub-operations: Call _ns_execute_primitive() instead of action()
quilesj7e13aeb2019-10-08 13:34:55 +02002491 try:
tiernoe876f672020-02-13 14:34:48 +00002492 result, result_detail = await self._ns_execute_primitive(vca_deployed["ee_id"], primitive,
2493 mapped_primitive_params)
2494 except LcmException:
2495 # this happens when VCA is not deployed. In this case it is not needed to terminate
2496 continue
2497 result_ok = ['COMPLETED', 'PARTIALLY_COMPLETED']
2498 if result not in result_ok:
2499 raise LcmException("terminate_primitive {} for vnf_member_index={} fails with "
2500 "error {}".format(seq.get("name"), vnf_index, result_detail))
2501 # set that this VCA do not need terminated
2502 db_update_entry = "_admin.deployed.VCA.{}.needed_terminate".format(vca_index)
2503 self.update_db_2("nsrs", db_nslcmop["nsInstanceId"], {db_update_entry: False})
2504
2505 if destroy_ee:
2506 await self.n2vc.delete_execution_environment(vca_deployed["ee_id"])
kuuse0ca67472019-05-13 15:59:27 +02002507
quilesj3655ae02019-12-12 16:08:35 +00002508 async def _delete_N2VC(self, nsr_id: str):
2509 self._write_all_config_status(nsr_id=nsr_id, status='TERMINATING')
2510 namespace = "." + nsr_id
2511 await self.n2vc.delete_namespace(namespace=namespace)
2512 self._write_all_config_status(nsr_id=nsr_id, status='DELETED')
2513
tiernoe876f672020-02-13 14:34:48 +00002514 async def _terminate_RO(self, logging_text, nsr_deployed, nsr_id, nslcmop_id, stage):
2515 """
2516 Terminates a deployment from RO
2517 :param logging_text:
2518 :param nsr_deployed: db_nsr._admin.deployed
2519 :param nsr_id:
2520 :param nslcmop_id:
2521 :param stage: list of string with the content to write on db_nslcmop.detailed-status.
2522 this method will update only the index 2, but it will write on database the concatenated content of the list
2523 :return:
2524 """
2525 db_nsr_update = {}
2526 failed_detail = []
2527 ro_nsr_id = ro_delete_action = None
2528 if nsr_deployed and nsr_deployed.get("RO"):
2529 ro_nsr_id = nsr_deployed["RO"].get("nsr_id")
2530 ro_delete_action = nsr_deployed["RO"].get("nsr_delete_action_id")
2531 try:
2532 if ro_nsr_id:
2533 stage[2] = "Deleting ns from VIM."
2534 db_nsr_update["detailed-status"] = " ".join(stage)
2535 self._write_op_status(nslcmop_id, stage)
2536 self.logger.debug(logging_text + stage[2])
2537 self.update_db_2("nsrs", nsr_id, db_nsr_update)
2538 self._write_op_status(nslcmop_id, stage)
2539 desc = await self.RO.delete("ns", ro_nsr_id)
2540 ro_delete_action = desc["action_id"]
2541 db_nsr_update["_admin.deployed.RO.nsr_delete_action_id"] = ro_delete_action
2542 db_nsr_update["_admin.deployed.RO.nsr_id"] = None
2543 db_nsr_update["_admin.deployed.RO.nsr_status"] = "DELETED"
2544 if ro_delete_action:
2545 # wait until NS is deleted from VIM
2546 stage[2] = "Waiting ns deleted from VIM."
2547 detailed_status_old = None
2548 self.logger.debug(logging_text + stage[2] + " RO_id={} ro_delete_action={}".format(ro_nsr_id,
2549 ro_delete_action))
2550 self.update_db_2("nsrs", nsr_id, db_nsr_update)
2551 self._write_op_status(nslcmop_id, stage)
kuused124bfe2019-06-18 12:09:24 +02002552
tiernoe876f672020-02-13 14:34:48 +00002553 delete_timeout = 20 * 60 # 20 minutes
2554 while delete_timeout > 0:
2555 desc = await self.RO.show(
2556 "ns",
2557 item_id_name=ro_nsr_id,
2558 extra_item="action",
2559 extra_item_id=ro_delete_action)
2560
2561 # deploymentStatus
2562 self._on_update_ro_db(nsrs_id=nsr_id, ro_descriptor=desc)
2563
2564 ns_status, ns_status_info = self.RO.check_action_status(desc)
2565 if ns_status == "ERROR":
2566 raise ROclient.ROClientException(ns_status_info)
2567 elif ns_status == "BUILD":
2568 stage[2] = "Deleting from VIM {}".format(ns_status_info)
2569 elif ns_status == "ACTIVE":
2570 db_nsr_update["_admin.deployed.RO.nsr_delete_action_id"] = None
2571 db_nsr_update["_admin.deployed.RO.nsr_status"] = "DELETED"
2572 break
2573 else:
2574 assert False, "ROclient.check_action_status returns unknown {}".format(ns_status)
2575 if stage[2] != detailed_status_old:
2576 detailed_status_old = stage[2]
2577 db_nsr_update["detailed-status"] = " ".join(stage)
2578 self._write_op_status(nslcmop_id, stage)
2579 self.update_db_2("nsrs", nsr_id, db_nsr_update)
2580 await asyncio.sleep(5, loop=self.loop)
2581 delete_timeout -= 5
2582 else: # delete_timeout <= 0:
2583 raise ROclient.ROClientException("Timeout waiting ns deleted from VIM")
2584
2585 except Exception as e:
2586 self.update_db_2("nsrs", nsr_id, db_nsr_update)
2587 if isinstance(e, ROclient.ROClientException) and e.http_code == 404: # not found
2588 db_nsr_update["_admin.deployed.RO.nsr_id"] = None
2589 db_nsr_update["_admin.deployed.RO.nsr_status"] = "DELETED"
2590 db_nsr_update["_admin.deployed.RO.nsr_delete_action_id"] = None
2591 self.logger.debug(logging_text + "RO_ns_id={} already deleted".format(ro_nsr_id))
2592 elif isinstance(e, ROclient.ROClientException) and e.http_code == 409: # conflict
tiernoa2143262020-03-27 16:20:40 +00002593 failed_detail.append("delete conflict: {}".format(e))
2594 self.logger.debug(logging_text + "RO_ns_id={} delete conflict: {}".format(ro_nsr_id, e))
tiernoe876f672020-02-13 14:34:48 +00002595 else:
tiernoa2143262020-03-27 16:20:40 +00002596 failed_detail.append("delete error: {}".format(e))
2597 self.logger.error(logging_text + "RO_ns_id={} delete error: {}".format(ro_nsr_id, e))
tiernoe876f672020-02-13 14:34:48 +00002598
2599 # Delete nsd
2600 if not failed_detail and deep_get(nsr_deployed, ("RO", "nsd_id")):
2601 ro_nsd_id = nsr_deployed["RO"]["nsd_id"]
2602 try:
2603 stage[2] = "Deleting nsd from RO."
2604 db_nsr_update["detailed-status"] = " ".join(stage)
2605 self.update_db_2("nsrs", nsr_id, db_nsr_update)
2606 self._write_op_status(nslcmop_id, stage)
2607 await self.RO.delete("nsd", ro_nsd_id)
2608 self.logger.debug(logging_text + "ro_nsd_id={} deleted".format(ro_nsd_id))
2609 db_nsr_update["_admin.deployed.RO.nsd_id"] = None
2610 except Exception as e:
2611 if isinstance(e, ROclient.ROClientException) and e.http_code == 404: # not found
2612 db_nsr_update["_admin.deployed.RO.nsd_id"] = None
2613 self.logger.debug(logging_text + "ro_nsd_id={} already deleted".format(ro_nsd_id))
2614 elif isinstance(e, ROclient.ROClientException) and e.http_code == 409: # conflict
2615 failed_detail.append("ro_nsd_id={} delete conflict: {}".format(ro_nsd_id, e))
2616 self.logger.debug(logging_text + failed_detail[-1])
2617 else:
2618 failed_detail.append("ro_nsd_id={} delete error: {}".format(ro_nsd_id, e))
2619 self.logger.error(logging_text + failed_detail[-1])
2620
2621 if not failed_detail and deep_get(nsr_deployed, ("RO", "vnfd")):
2622 for index, vnf_deployed in enumerate(nsr_deployed["RO"]["vnfd"]):
2623 if not vnf_deployed or not vnf_deployed["id"]:
2624 continue
2625 try:
2626 ro_vnfd_id = vnf_deployed["id"]
2627 stage[2] = "Deleting member_vnf_index={} ro_vnfd_id={} from RO.".format(
2628 vnf_deployed["member-vnf-index"], ro_vnfd_id)
2629 db_nsr_update["detailed-status"] = " ".join(stage)
2630 self.update_db_2("nsrs", nsr_id, db_nsr_update)
2631 self._write_op_status(nslcmop_id, stage)
2632 await self.RO.delete("vnfd", ro_vnfd_id)
2633 self.logger.debug(logging_text + "ro_vnfd_id={} deleted".format(ro_vnfd_id))
2634 db_nsr_update["_admin.deployed.RO.vnfd.{}.id".format(index)] = None
2635 except Exception as e:
2636 if isinstance(e, ROclient.ROClientException) and e.http_code == 404: # not found
2637 db_nsr_update["_admin.deployed.RO.vnfd.{}.id".format(index)] = None
2638 self.logger.debug(logging_text + "ro_vnfd_id={} already deleted ".format(ro_vnfd_id))
2639 elif isinstance(e, ROclient.ROClientException) and e.http_code == 409: # conflict
2640 failed_detail.append("ro_vnfd_id={} delete conflict: {}".format(ro_vnfd_id, e))
2641 self.logger.debug(logging_text + failed_detail[-1])
2642 else:
2643 failed_detail.append("ro_vnfd_id={} delete error: {}".format(ro_vnfd_id, e))
2644 self.logger.error(logging_text + failed_detail[-1])
2645
tiernoa2143262020-03-27 16:20:40 +00002646 if failed_detail:
2647 stage[2] = "Error deleting from VIM"
2648 else:
2649 stage[2] = "Deleted from VIM"
tiernoe876f672020-02-13 14:34:48 +00002650 db_nsr_update["detailed-status"] = " ".join(stage)
2651 self.update_db_2("nsrs", nsr_id, db_nsr_update)
2652 self._write_op_status(nslcmop_id, stage)
2653
2654 if failed_detail:
tiernoa2143262020-03-27 16:20:40 +00002655 raise LcmException("; ".join(failed_detail))
tiernoe876f672020-02-13 14:34:48 +00002656
2657 async def terminate(self, nsr_id, nslcmop_id):
kuused124bfe2019-06-18 12:09:24 +02002658 # Try to lock HA task here
2659 task_is_locked_by_me = self.lcm_tasks.lock_HA('ns', 'nslcmops', nslcmop_id)
2660 if not task_is_locked_by_me:
2661 return
2662
tierno59d22d22018-09-25 18:10:19 +02002663 logging_text = "Task ns={} terminate={} ".format(nsr_id, nslcmop_id)
2664 self.logger.debug(logging_text + "Enter")
tiernoe876f672020-02-13 14:34:48 +00002665 timeout_ns_terminate = self.timeout_ns_terminate
tierno59d22d22018-09-25 18:10:19 +02002666 db_nsr = None
2667 db_nslcmop = None
2668 exc = None
tiernoe876f672020-02-13 14:34:48 +00002669 error_list = [] # annotates all failed error messages
tierno59d22d22018-09-25 18:10:19 +02002670 db_nslcmop_update = {}
tiernoc2564fe2019-01-28 16:18:56 +00002671 autoremove = False # autoremove after terminated
tiernoe876f672020-02-13 14:34:48 +00002672 tasks_dict_info = {}
2673 db_nsr_update = {}
2674 stage = ["Stage 1/3: Preparing task.", "Waiting for previous operations to terminate.", ""]
2675 # ^ contains [stage, step, VIM-status]
tierno59d22d22018-09-25 18:10:19 +02002676 try:
kuused124bfe2019-06-18 12:09:24 +02002677 # wait for any previous tasks in process
2678 await self.lcm_tasks.waitfor_related_HA("ns", 'nslcmops', nslcmop_id)
2679
tiernoe876f672020-02-13 14:34:48 +00002680 stage[1] = "Getting nslcmop={} from db.".format(nslcmop_id)
2681 db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
2682 operation_params = db_nslcmop.get("operationParams") or {}
2683 if operation_params.get("timeout_ns_terminate"):
2684 timeout_ns_terminate = operation_params["timeout_ns_terminate"]
2685 stage[1] = "Getting nsr={} from db.".format(nsr_id)
2686 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
2687
2688 db_nsr_update["operational-status"] = "terminating"
2689 db_nsr_update["config-status"] = "terminating"
quilesj4cda56b2019-12-05 10:02:20 +00002690 self._write_ns_status(
2691 nsr_id=nsr_id,
2692 ns_state="TERMINATING",
2693 current_operation="TERMINATING",
tiernoe876f672020-02-13 14:34:48 +00002694 current_operation_id=nslcmop_id,
2695 other_update=db_nsr_update
quilesj4cda56b2019-12-05 10:02:20 +00002696 )
quilesj3655ae02019-12-12 16:08:35 +00002697 self._write_op_status(
2698 op_id=nslcmop_id,
tiernoe876f672020-02-13 14:34:48 +00002699 queuePosition=0,
2700 stage=stage
quilesj3655ae02019-12-12 16:08:35 +00002701 )
tiernoe876f672020-02-13 14:34:48 +00002702 nsr_deployed = deepcopy(db_nsr["_admin"].get("deployed")) or {}
tierno59d22d22018-09-25 18:10:19 +02002703 if db_nsr["_admin"]["nsState"] == "NOT_INSTANTIATED":
2704 return
tierno59d22d22018-09-25 18:10:19 +02002705
tiernoe876f672020-02-13 14:34:48 +00002706 stage[1] = "Getting vnf descriptors from db."
2707 db_vnfrs_list = self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id})
2708 db_vnfds_from_id = {}
2709 db_vnfds_from_member_index = {}
2710 # Loop over VNFRs
2711 for vnfr in db_vnfrs_list:
2712 vnfd_id = vnfr["vnfd-id"]
2713 if vnfd_id not in db_vnfds_from_id:
2714 vnfd = self.db.get_one("vnfds", {"_id": vnfd_id})
2715 db_vnfds_from_id[vnfd_id] = vnfd
2716 db_vnfds_from_member_index[vnfr["member-vnf-index-ref"]] = db_vnfds_from_id[vnfd_id]
calvinosanch9f9c6f22019-11-04 13:37:39 +01002717
tiernoe876f672020-02-13 14:34:48 +00002718 # Destroy individual execution environments when there are terminating primitives.
2719 # Rest of EE will be deleted at once
2720 if not operation_params.get("skip_terminate_primitives"):
2721 stage[0] = "Stage 2/3 execute terminating primitives."
2722 stage[1] = "Looking execution environment that needs terminate."
2723 self.logger.debug(logging_text + stage[1])
2724 for vca_index, vca in enumerate(get_iterable(nsr_deployed, "VCA")):
2725 config_descriptor = None
2726 if not vca or not vca.get("ee_id") or not vca.get("needed_terminate"):
2727 continue
2728 if not vca.get("member-vnf-index"):
2729 # ns
2730 config_descriptor = db_nsr.get("ns-configuration")
2731 elif vca.get("vdu_id"):
2732 db_vnfd = db_vnfds_from_member_index[vca["member-vnf-index"]]
2733 vdud = next((vdu for vdu in db_vnfd.get("vdu", ()) if vdu["id"] == vca.get("vdu_id")), None)
2734 if vdud:
2735 config_descriptor = vdud.get("vdu-configuration")
2736 elif vca.get("kdu_name"):
2737 db_vnfd = db_vnfds_from_member_index[vca["member-vnf-index"]]
2738 kdud = next((kdu for kdu in db_vnfd.get("kdu", ()) if kdu["name"] == vca.get("kdu_name")), None)
2739 if kdud:
2740 config_descriptor = kdud.get("kdu-configuration")
2741 else:
2742 config_descriptor = db_vnfds_from_member_index[vca["member-vnf-index"]].get("vnf-configuration")
2743 task = asyncio.ensure_future(self.destroy_N2VC(logging_text, db_nslcmop, vca, config_descriptor,
2744 vca_index, False))
2745 tasks_dict_info[task] = "Terminating VCA {}".format(vca.get("ee_id"))
tierno59d22d22018-09-25 18:10:19 +02002746
tiernoe876f672020-02-13 14:34:48 +00002747 # wait for pending tasks of terminate primitives
2748 if tasks_dict_info:
2749 self.logger.debug(logging_text + 'Waiting for terminate primitive pending tasks...')
2750 error_list = await self._wait_for_tasks(logging_text, tasks_dict_info,
2751 min(self.timeout_charm_delete, timeout_ns_terminate),
2752 stage, nslcmop_id)
2753 if error_list:
2754 return # raise LcmException("; ".join(error_list))
2755 tasks_dict_info.clear()
tierno82974b22018-11-27 21:55:36 +00002756
tiernoe876f672020-02-13 14:34:48 +00002757 # remove All execution environments at once
2758 stage[0] = "Stage 3/3 delete all."
2759 stage[1] = "Deleting all execution environments."
2760 self.logger.debug(logging_text + stage[1])
quilesj3655ae02019-12-12 16:08:35 +00002761
tiernoe876f672020-02-13 14:34:48 +00002762 task_delete_ee = asyncio.ensure_future(self._delete_N2VC(nsr_id=nsr_id))
2763 # task_delete_ee = asyncio.ensure_future(self.n2vc.delete_namespace(namespace="." + nsr_id))
2764 tasks_dict_info[task_delete_ee] = "Terminating all VCA"
tierno59d22d22018-09-25 18:10:19 +02002765
tiernoe876f672020-02-13 14:34:48 +00002766 # Delete from k8scluster
2767 stage[1] = "Deleting KDUs."
2768 self.logger.debug(logging_text + stage[1])
2769 # print(nsr_deployed)
2770 for kdu in get_iterable(nsr_deployed, "K8s"):
2771 if not kdu or not kdu.get("kdu-instance"):
2772 continue
2773 kdu_instance = kdu.get("kdu-instance")
tiernoa2143262020-03-27 16:20:40 +00002774 if kdu.get("k8scluster-type") in self.k8scluster_map:
tiernoe876f672020-02-13 14:34:48 +00002775 task_delete_kdu_instance = asyncio.ensure_future(
tiernoa2143262020-03-27 16:20:40 +00002776 self.k8scluster_map[kdu["k8scluster-type"]].uninstall(
2777 cluster_uuid=kdu.get("k8scluster-uuid"),
2778 kdu_instance=kdu_instance))
tiernoe876f672020-02-13 14:34:48 +00002779 else:
2780 self.logger.error(logging_text + "Unknown k8s deployment type {}".
2781 format(kdu.get("k8scluster-type")))
2782 continue
2783 tasks_dict_info[task_delete_kdu_instance] = "Terminating KDU '{}'".format(kdu.get("kdu-name"))
tierno59d22d22018-09-25 18:10:19 +02002784
2785 # remove from RO
tiernoe876f672020-02-13 14:34:48 +00002786 stage[1] = "Deleting ns from VIM."
2787 task_delete_ro = asyncio.ensure_future(
2788 self._terminate_RO(logging_text, nsr_deployed, nsr_id, nslcmop_id, stage))
2789 tasks_dict_info[task_delete_ro] = "Removing deployment from VIM"
tierno59d22d22018-09-25 18:10:19 +02002790
tiernoe876f672020-02-13 14:34:48 +00002791 # rest of staff will be done at finally
2792
2793 except (ROclient.ROClientException, DbException, LcmException, N2VCException) as e:
2794 self.logger.error(logging_text + "Exit Exception {}".format(e))
2795 exc = e
2796 except asyncio.CancelledError:
2797 self.logger.error(logging_text + "Cancelled Exception while '{}'".format(stage[1]))
2798 exc = "Operation was cancelled"
2799 except Exception as e:
2800 exc = traceback.format_exc()
2801 self.logger.critical(logging_text + "Exit Exception while '{}': {}".format(stage[1], e), exc_info=True)
2802 finally:
2803 if exc:
2804 error_list.append(str(exc))
tierno59d22d22018-09-25 18:10:19 +02002805 try:
tiernoe876f672020-02-13 14:34:48 +00002806 # wait for pending tasks
2807 if tasks_dict_info:
2808 stage[1] = "Waiting for terminate pending tasks."
2809 self.logger.debug(logging_text + stage[1])
2810 error_list += await self._wait_for_tasks(logging_text, tasks_dict_info, timeout_ns_terminate,
2811 stage, nslcmop_id)
2812 stage[1] = stage[2] = ""
2813 except asyncio.CancelledError:
2814 error_list.append("Cancelled")
2815 # TODO cancell all tasks
2816 except Exception as exc:
2817 error_list.append(str(exc))
2818 # update status at database
2819 if error_list:
2820 error_detail = "; ".join(error_list)
2821 # self.logger.error(logging_text + error_detail)
tiernoa2143262020-03-27 16:20:40 +00002822 error_description_nslcmop = 'Stage: {}. Detail: {}'.format(stage[0], error_detail)
2823 error_description_nsr = 'Operation: TERMINATING.{}, Stage {}.'.format(nslcmop_id, stage[0])
tierno59d22d22018-09-25 18:10:19 +02002824
tierno59d22d22018-09-25 18:10:19 +02002825 db_nsr_update["operational-status"] = "failed"
tiernoa2143262020-03-27 16:20:40 +00002826 db_nsr_update["detailed-status"] = error_description_nsr + " Detail: " + error_detail
tiernoe876f672020-02-13 14:34:48 +00002827 db_nslcmop_update["detailed-status"] = error_detail
2828 nslcmop_operation_state = "FAILED"
2829 ns_state = "BROKEN"
tierno59d22d22018-09-25 18:10:19 +02002830 else:
tiernoa2143262020-03-27 16:20:40 +00002831 error_detail = None
tiernoe876f672020-02-13 14:34:48 +00002832 error_description_nsr = error_description_nslcmop = None
2833 ns_state = "NOT_INSTANTIATED"
tierno59d22d22018-09-25 18:10:19 +02002834 db_nsr_update["operational-status"] = "terminated"
2835 db_nsr_update["detailed-status"] = "Done"
2836 db_nsr_update["_admin.nsState"] = "NOT_INSTANTIATED"
2837 db_nslcmop_update["detailed-status"] = "Done"
tiernoe876f672020-02-13 14:34:48 +00002838 nslcmop_operation_state = "COMPLETED"
tierno59d22d22018-09-25 18:10:19 +02002839
tiernoe876f672020-02-13 14:34:48 +00002840 if db_nsr:
2841 self._write_ns_status(
2842 nsr_id=nsr_id,
2843 ns_state=ns_state,
2844 current_operation="IDLE",
2845 current_operation_id=None,
2846 error_description=error_description_nsr,
tiernoa2143262020-03-27 16:20:40 +00002847 error_detail=error_detail,
tiernoe876f672020-02-13 14:34:48 +00002848 other_update=db_nsr_update
2849 )
2850 if db_nslcmop:
2851 self._write_op_status(
2852 op_id=nslcmop_id,
2853 stage="",
2854 error_message=error_description_nslcmop,
2855 operation_state=nslcmop_operation_state,
2856 other_update=db_nslcmop_update,
2857 )
2858 autoremove = operation_params.get("autoremove", False)
tierno59d22d22018-09-25 18:10:19 +02002859 if nslcmop_operation_state:
2860 try:
2861 await self.msg.aiowrite("ns", "terminated", {"nsr_id": nsr_id, "nslcmop_id": nslcmop_id,
tiernoc2564fe2019-01-28 16:18:56 +00002862 "operationState": nslcmop_operation_state,
2863 "autoremove": autoremove},
tierno8a518872018-12-21 13:42:14 +00002864 loop=self.loop)
tierno59d22d22018-09-25 18:10:19 +02002865 except Exception as e:
2866 self.logger.error(logging_text + "kafka_write notification Exception {}".format(e))
quilesj7e13aeb2019-10-08 13:34:55 +02002867
tierno59d22d22018-09-25 18:10:19 +02002868 self.logger.debug(logging_text + "Exit")
2869 self.lcm_tasks.remove("ns", nsr_id, nslcmop_id, "ns_terminate")
2870
tiernoe876f672020-02-13 14:34:48 +00002871 async def _wait_for_tasks(self, logging_text, created_tasks_info, timeout, stage, nslcmop_id, nsr_id=None):
2872 time_start = time()
tiernoa2143262020-03-27 16:20:40 +00002873 error_detail_list = []
tiernoe876f672020-02-13 14:34:48 +00002874 error_list = []
2875 pending_tasks = list(created_tasks_info.keys())
2876 num_tasks = len(pending_tasks)
2877 num_done = 0
2878 stage[1] = "{}/{}.".format(num_done, num_tasks)
2879 self._write_op_status(nslcmop_id, stage)
tiernoe876f672020-02-13 14:34:48 +00002880 while pending_tasks:
tiernoa2143262020-03-27 16:20:40 +00002881 new_error = None
tiernoe876f672020-02-13 14:34:48 +00002882 _timeout = timeout + time_start - time()
2883 done, pending_tasks = await asyncio.wait(pending_tasks, timeout=_timeout,
2884 return_when=asyncio.FIRST_COMPLETED)
2885 num_done += len(done)
2886 if not done: # Timeout
2887 for task in pending_tasks:
tiernoa2143262020-03-27 16:20:40 +00002888 new_error = created_tasks_info[task] + ": Timeout"
2889 error_detail_list.append(new_error)
2890 error_list.append(new_error)
tiernoe876f672020-02-13 14:34:48 +00002891 break
2892 for task in done:
2893 if task.cancelled():
tiernoa2143262020-03-27 16:20:40 +00002894 new_error = created_tasks_info[task] + ": Cancelled"
2895 self.logger.warn(logging_text + new_error)
2896 error_detail_list.append(new_error)
2897 error_list.append(new_error)
tiernoe876f672020-02-13 14:34:48 +00002898 else:
2899 exc = task.exception()
2900 if exc:
tiernoa2143262020-03-27 16:20:40 +00002901 new_error = created_tasks_info[task] + ": {}".format(exc)
2902 error_list.append(created_tasks_info[task])
2903 error_detail_list.append(new_error)
tiernoe876f672020-02-13 14:34:48 +00002904 if isinstance(exc, (DbException, N2VCException, ROclient.ROClientException, LcmException)):
tiernoa2143262020-03-27 16:20:40 +00002905 self.logger.error(logging_text + new_error)
tiernoe876f672020-02-13 14:34:48 +00002906 else:
2907 exc_traceback = "".join(traceback.format_exception(None, exc, exc.__traceback__))
2908 self.logger.error(logging_text + created_tasks_info[task] + exc_traceback)
2909 else:
2910 self.logger.debug(logging_text + created_tasks_info[task] + ": Done")
2911 stage[1] = "{}/{}.".format(num_done, num_tasks)
2912 if new_error:
tiernoa2143262020-03-27 16:20:40 +00002913 stage[1] += " Errors: " + ". ".join(error_detail_list) + "."
tiernoe876f672020-02-13 14:34:48 +00002914 if nsr_id: # update also nsr
tiernoa2143262020-03-27 16:20:40 +00002915 self.update_db_2("nsrs", nsr_id, {"errorDescription": "Error at: " + ", ".join(error_list),
2916 "errorDetail": ". ".join(error_detail_list)})
tiernoe876f672020-02-13 14:34:48 +00002917 self._write_op_status(nslcmop_id, stage)
tiernoa2143262020-03-27 16:20:40 +00002918 return error_detail_list
tiernoe876f672020-02-13 14:34:48 +00002919
tiernoda964822019-01-14 15:53:47 +00002920 @staticmethod
2921 def _map_primitive_params(primitive_desc, params, instantiation_params):
2922 """
2923 Generates the params to be provided to charm before executing primitive. If user does not provide a parameter,
2924 The default-value is used. If it is between < > it look for a value at instantiation_params
2925 :param primitive_desc: portion of VNFD/NSD that describes primitive
2926 :param params: Params provided by user
2927 :param instantiation_params: Instantiation params provided by user
2928 :return: a dictionary with the calculated params
2929 """
2930 calculated_params = {}
2931 for parameter in primitive_desc.get("parameter", ()):
2932 param_name = parameter["name"]
2933 if param_name in params:
2934 calculated_params[param_name] = params[param_name]
tierno98ad6ea2019-05-30 17:16:28 +00002935 elif "default-value" in parameter or "value" in parameter:
2936 if "value" in parameter:
2937 calculated_params[param_name] = parameter["value"]
2938 else:
2939 calculated_params[param_name] = parameter["default-value"]
2940 if isinstance(calculated_params[param_name], str) and calculated_params[param_name].startswith("<") \
2941 and calculated_params[param_name].endswith(">"):
2942 if calculated_params[param_name][1:-1] in instantiation_params:
2943 calculated_params[param_name] = instantiation_params[calculated_params[param_name][1:-1]]
tiernoda964822019-01-14 15:53:47 +00002944 else:
2945 raise LcmException("Parameter {} needed to execute primitive {} not provided".
tiernod8323042019-08-09 11:32:23 +00002946 format(calculated_params[param_name], primitive_desc["name"]))
tiernoda964822019-01-14 15:53:47 +00002947 else:
2948 raise LcmException("Parameter {} needed to execute primitive {} not provided".
2949 format(param_name, primitive_desc["name"]))
tierno59d22d22018-09-25 18:10:19 +02002950
tiernoda964822019-01-14 15:53:47 +00002951 if isinstance(calculated_params[param_name], (dict, list, tuple)):
2952 calculated_params[param_name] = yaml.safe_dump(calculated_params[param_name], default_flow_style=True,
2953 width=256)
2954 elif isinstance(calculated_params[param_name], str) and calculated_params[param_name].startswith("!!yaml "):
2955 calculated_params[param_name] = calculated_params[param_name][7:]
tiernoc3f2a822019-11-05 13:45:04 +00002956
2957 # add always ns_config_info if primitive name is config
2958 if primitive_desc["name"] == "config":
2959 if "ns_config_info" in instantiation_params:
2960 calculated_params["ns_config_info"] = instantiation_params["ns_config_info"]
tiernoda964822019-01-14 15:53:47 +00002961 return calculated_params
2962
tiernoe876f672020-02-13 14:34:48 +00002963 def _look_for_deployed_vca(self, deployed_vca, member_vnf_index, vdu_id, vdu_name, vdu_count_index, kdu_name=None):
2964 # find vca_deployed record for this action. Raise LcmException if not found or there is not any id.
2965 for vca in deployed_vca:
2966 if not vca:
2967 continue
2968 if member_vnf_index != vca["member-vnf-index"] or vdu_id != vca["vdu_id"]:
2969 continue
2970 if vdu_name and vdu_name != vca["vdu_name"]:
2971 continue
2972 if vdu_count_index is not None and vdu_count_index != vca["vdu_count_index"]:
2973 continue
2974 if kdu_name and kdu_name != vca["kdu_name"]:
2975 continue
2976 break
2977 else:
2978 # vca_deployed not found
2979 raise LcmException("charm for member_vnf_index={} vdu_id={} vdu_name={} vdu_count_index={} is not "
2980 "deployed".format(member_vnf_index, vdu_id, vdu_name, vdu_count_index))
quilesj7e13aeb2019-10-08 13:34:55 +02002981
tiernoe876f672020-02-13 14:34:48 +00002982 # get ee_id
2983 ee_id = vca.get("ee_id")
2984 if not ee_id:
2985 raise LcmException("charm for member_vnf_index={} vdu_id={} vdu_name={} vdu_count_index={} has not "
2986 "execution environment"
2987 .format(member_vnf_index, vdu_id, vdu_name, vdu_count_index))
2988 return ee_id
2989
2990 async def _ns_execute_primitive(self, ee_id, primitive, primitive_params, retries=0,
2991 retries_interval=30) -> (str, str):
tiernoda964822019-01-14 15:53:47 +00002992 try:
tierno98ad6ea2019-05-30 17:16:28 +00002993 if primitive == "config":
2994 primitive_params = {"params": primitive_params}
tierno2fc7ce52019-06-11 22:50:01 +00002995
quilesj7e13aeb2019-10-08 13:34:55 +02002996 while retries >= 0:
2997 try:
2998 output = await self.n2vc.exec_primitive(
2999 ee_id=ee_id,
3000 primitive_name=primitive,
3001 params_dict=primitive_params
3002 )
3003 # execution was OK
3004 break
3005 except Exception as e:
quilesj7e13aeb2019-10-08 13:34:55 +02003006 retries -= 1
3007 if retries >= 0:
tierno73d8bd02019-11-18 17:33:27 +00003008 self.logger.debug('Error executing action {} on {} -> {}'.format(primitive, ee_id, e))
quilesj7e13aeb2019-10-08 13:34:55 +02003009 # wait and retry
3010 await asyncio.sleep(retries_interval, loop=self.loop)
tierno73d8bd02019-11-18 17:33:27 +00003011 else:
tiernoe876f672020-02-13 14:34:48 +00003012 return 'FAIL', 'Cannot execute action {} on {}: {}'.format(primitive, ee_id, e)
quilesj7e13aeb2019-10-08 13:34:55 +02003013
tiernoe876f672020-02-13 14:34:48 +00003014 return 'COMPLETED', output
quilesj7e13aeb2019-10-08 13:34:55 +02003015
tiernoe876f672020-02-13 14:34:48 +00003016 except LcmException:
3017 raise
quilesj7e13aeb2019-10-08 13:34:55 +02003018 except Exception as e:
tiernoe876f672020-02-13 14:34:48 +00003019 return 'FAIL', 'Error executing action {}: {}'.format(primitive, e)
tierno59d22d22018-09-25 18:10:19 +02003020
3021 async def action(self, nsr_id, nslcmop_id):
kuused124bfe2019-06-18 12:09:24 +02003022
3023 # Try to lock HA task here
3024 task_is_locked_by_me = self.lcm_tasks.lock_HA('ns', 'nslcmops', nslcmop_id)
3025 if not task_is_locked_by_me:
3026 return
3027
tierno59d22d22018-09-25 18:10:19 +02003028 logging_text = "Task ns={} action={} ".format(nsr_id, nslcmop_id)
3029 self.logger.debug(logging_text + "Enter")
3030 # get all needed from database
3031 db_nsr = None
3032 db_nslcmop = None
tiernoe876f672020-02-13 14:34:48 +00003033 db_nsr_update = {}
tierno59d22d22018-09-25 18:10:19 +02003034 db_nslcmop_update = {}
3035 nslcmop_operation_state = None
kuuse0ca67472019-05-13 15:59:27 +02003036 nslcmop_operation_state_detail = None
tierno59d22d22018-09-25 18:10:19 +02003037 exc = None
3038 try:
kuused124bfe2019-06-18 12:09:24 +02003039 # wait for any previous tasks in process
tierno3cf81a32019-11-11 17:07:00 +00003040 step = "Waiting for previous operations to terminate"
kuused124bfe2019-06-18 12:09:24 +02003041 await self.lcm_tasks.waitfor_related_HA('ns', 'nslcmops', nslcmop_id)
3042
quilesj4cda56b2019-12-05 10:02:20 +00003043 self._write_ns_status(
3044 nsr_id=nsr_id,
3045 ns_state=None,
3046 current_operation="RUNNING ACTION",
3047 current_operation_id=nslcmop_id
3048 )
3049
tierno59d22d22018-09-25 18:10:19 +02003050 step = "Getting information from database"
3051 db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
3052 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
tiernoda964822019-01-14 15:53:47 +00003053
tiernoe4f7e6c2018-11-27 14:55:30 +00003054 nsr_deployed = db_nsr["_admin"].get("deployed")
tierno1b633412019-02-25 16:48:23 +00003055 vnf_index = db_nslcmop["operationParams"].get("member_vnf_index")
tierno59d22d22018-09-25 18:10:19 +02003056 vdu_id = db_nslcmop["operationParams"].get("vdu_id")
calvinosanch9f9c6f22019-11-04 13:37:39 +01003057 kdu_name = db_nslcmop["operationParams"].get("kdu_name")
tiernoe4f7e6c2018-11-27 14:55:30 +00003058 vdu_count_index = db_nslcmop["operationParams"].get("vdu_count_index")
3059 vdu_name = db_nslcmop["operationParams"].get("vdu_name")
tierno59d22d22018-09-25 18:10:19 +02003060
tierno1b633412019-02-25 16:48:23 +00003061 if vnf_index:
3062 step = "Getting vnfr from database"
3063 db_vnfr = self.db.get_one("vnfrs", {"member-vnf-index-ref": vnf_index, "nsr-id-ref": nsr_id})
3064 step = "Getting vnfd from database"
3065 db_vnfd = self.db.get_one("vnfds", {"_id": db_vnfr["vnfd-id"]})
3066 else:
3067 if db_nsr.get("nsd"):
3068 db_nsd = db_nsr.get("nsd") # TODO this will be removed
3069 else:
3070 step = "Getting nsd from database"
3071 db_nsd = self.db.get_one("nsds", {"_id": db_nsr["nsd-id"]})
tiernoda964822019-01-14 15:53:47 +00003072
tierno82974b22018-11-27 21:55:36 +00003073 # for backward compatibility
3074 if nsr_deployed and isinstance(nsr_deployed.get("VCA"), dict):
3075 nsr_deployed["VCA"] = list(nsr_deployed["VCA"].values())
3076 db_nsr_update["_admin.deployed.VCA"] = nsr_deployed["VCA"]
3077 self.update_db_2("nsrs", nsr_id, db_nsr_update)
3078
tierno59d22d22018-09-25 18:10:19 +02003079 primitive = db_nslcmop["operationParams"]["primitive"]
3080 primitive_params = db_nslcmop["operationParams"]["primitive_params"]
tiernoda964822019-01-14 15:53:47 +00003081
3082 # look for primitive
3083 config_primitive_desc = None
3084 if vdu_id:
3085 for vdu in get_iterable(db_vnfd, "vdu"):
3086 if vdu_id == vdu["id"]:
3087 for config_primitive in vdu.get("vdu-configuration", {}).get("config-primitive", ()):
3088 if config_primitive["name"] == primitive:
3089 config_primitive_desc = config_primitive
3090 break
calvinosanch9f9c6f22019-11-04 13:37:39 +01003091 elif kdu_name:
3092 self.logger.debug(logging_text + "Checking actions in KDUs")
tierno5705ed32019-12-05 15:04:39 +00003093 kdur = next((x for x in db_vnfr["kdur"] if x["kdu-name"] == kdu_name), None)
tierno626e0152019-11-29 14:16:16 +00003094 desc_params = self._format_additional_params(kdur.get("additionalParams")) or {}
3095 if primitive_params:
3096 desc_params.update(primitive_params)
calvinosanch9f9c6f22019-11-04 13:37:39 +01003097 # TODO Check if we will need something at vnf level
3098 index = 0
3099 for kdu in get_iterable(nsr_deployed, "K8s"):
3100 if kdu_name == kdu["kdu-name"]:
3101 db_dict = {"collection": "nsrs", "filter": {"_id": nsr_id},
3102 "path": "_admin.deployed.K8s.{}".format(index)}
3103 if primitive == "upgrade":
3104 if desc_params.get("kdu_model"):
3105 kdu_model = desc_params.get("kdu_model")
3106 del desc_params["kdu_model"]
3107 else:
3108 kdu_model = kdu.get("kdu-model")
3109 parts = kdu_model.split(sep=":")
3110 if len(parts) == 2:
3111 kdu_model = parts[0]
3112
tiernoa2143262020-03-27 16:20:40 +00003113 if kdu.get("k8scluster-type") in self.k8scluster_map:
3114 output = await self.k8scluster_map[kdu["k8scluster-type"]].upgrade(
3115 cluster_uuid=kdu.get("k8scluster-uuid"),
3116 kdu_instance=kdu.get("kdu-instance"),
3117 atomic=True, kdu_model=kdu_model,
3118 params=desc_params, db_dict=db_dict,
3119 timeout=300)
Adam Israelbaacc302019-12-01 12:41:39 -05003120
calvinosanch9f9c6f22019-11-04 13:37:39 +01003121 else:
tiernoa2143262020-03-27 16:20:40 +00003122 msg = "unknown k8scluster-type '{}'".format(kdu.get("k8scluster-type"))
calvinosanch9f9c6f22019-11-04 13:37:39 +01003123 raise LcmException(msg)
3124
3125 self.logger.debug(logging_text + " Upgrade of kdu {} done".format(output))
3126 break
3127 elif primitive == "rollback":
tiernoa2143262020-03-27 16:20:40 +00003128 if kdu.get("k8scluster-type") in self.k8scluster_map:
3129 output = await self.k8scluster_map[kdu["k8scluster-type"]].rollback(
3130 cluster_uuid=kdu.get("k8scluster-uuid"),
3131 kdu_instance=kdu.get("kdu-instance"),
3132 db_dict=db_dict)
calvinosanch9f9c6f22019-11-04 13:37:39 +01003133 else:
tiernoa2143262020-03-27 16:20:40 +00003134 msg = "unknown k8scluster-type '{}'".format(kdu.get("k8scluster-type"))
calvinosanch9f9c6f22019-11-04 13:37:39 +01003135 raise LcmException(msg)
3136 break
3137 elif primitive == "status":
tiernoa2143262020-03-27 16:20:40 +00003138 if kdu.get("k8scluster-type") in self.k8scluster_map:
3139 output = await self.k8scluster_map[kdu["k8scluster-type"]].status_kdu(
3140 cluster_uuid=kdu.get("k8scluster-uuid"),
3141 kdu_instance=kdu.get("kdu-instance"))
calvinosanch9f9c6f22019-11-04 13:37:39 +01003142 else:
tiernoa2143262020-03-27 16:20:40 +00003143 msg = "unknown k8scluster-type '{}'".format(kdu.get("k8scluster-type"))
calvinosanch9f9c6f22019-11-04 13:37:39 +01003144 raise LcmException(msg)
3145 break
3146 index += 1
3147
3148 else:
3149 raise LcmException("KDU '{}' not found".format(kdu_name))
3150 if output:
3151 db_nslcmop_update["detailed-status"] = output
3152 db_nslcmop_update["operationState"] = 'COMPLETED'
3153 db_nslcmop_update["statusEnteredTime"] = time()
3154 else:
3155 db_nslcmop_update["detailed-status"] = ''
3156 db_nslcmop_update["operationState"] = 'FAILED'
3157 db_nslcmop_update["statusEnteredTime"] = time()
3158 return
tierno1b633412019-02-25 16:48:23 +00003159 elif vnf_index:
3160 for config_primitive in db_vnfd.get("vnf-configuration", {}).get("config-primitive", ()):
3161 if config_primitive["name"] == primitive:
3162 config_primitive_desc = config_primitive
3163 break
3164 else:
3165 for config_primitive in db_nsd.get("ns-configuration", {}).get("config-primitive", ()):
3166 if config_primitive["name"] == primitive:
3167 config_primitive_desc = config_primitive
3168 break
tiernoda964822019-01-14 15:53:47 +00003169
tierno1b633412019-02-25 16:48:23 +00003170 if not config_primitive_desc:
3171 raise LcmException("Primitive {} not found at [ns|vnf|vdu]-configuration:config-primitive ".
3172 format(primitive))
3173
3174 desc_params = {}
3175 if vnf_index:
3176 if db_vnfr.get("additionalParamsForVnf"):
tierno626e0152019-11-29 14:16:16 +00003177 desc_params = self._format_additional_params(db_vnfr["additionalParamsForVnf"])
3178 if vdu_id:
3179 vdur = next((x for x in db_vnfr["vdur"] if x["vdu-id-ref"] == vdu_id), None)
3180 if vdur.get("additionalParams"):
3181 desc_params = self._format_additional_params(vdur["additionalParams"])
tierno1b633412019-02-25 16:48:23 +00003182 else:
calvinosanch9f9c6f22019-11-04 13:37:39 +01003183 if db_nsr.get("additionalParamsForNs"):
tierno626e0152019-11-29 14:16:16 +00003184 desc_params.update(self._format_additional_params(db_nsr["additionalParamsForNs"]))
tiernoda964822019-01-14 15:53:47 +00003185
3186 # TODO check if ns is in a proper status
tiernoe876f672020-02-13 14:34:48 +00003187 result, detailed_status = await self._ns_execute_primitive(
3188 self._look_for_deployed_vca(nsr_deployed["VCA"],
3189 member_vnf_index=vnf_index,
3190 vdu_id=vdu_id,
3191 vdu_name=vdu_name,
3192 vdu_count_index=vdu_count_index),
quilesj7e13aeb2019-10-08 13:34:55 +02003193 primitive=primitive,
3194 primitive_params=self._map_primitive_params(config_primitive_desc, primitive_params, desc_params))
3195
quilesj7e13aeb2019-10-08 13:34:55 +02003196 db_nslcmop_update["detailed-status"] = nslcmop_operation_state_detail = detailed_status
tierno59d22d22018-09-25 18:10:19 +02003197 db_nslcmop_update["operationState"] = nslcmop_operation_state = result
3198 db_nslcmop_update["statusEnteredTime"] = time()
quilesj7e13aeb2019-10-08 13:34:55 +02003199 self.logger.debug(logging_text + " task Done with result {} {}".format(result, detailed_status))
tierno59d22d22018-09-25 18:10:19 +02003200 return # database update is called inside finally
3201
3202 except (DbException, LcmException) as e:
3203 self.logger.error(logging_text + "Exit Exception {}".format(e))
3204 exc = e
3205 except asyncio.CancelledError:
3206 self.logger.error(logging_text + "Cancelled Exception while '{}'".format(step))
3207 exc = "Operation was cancelled"
3208 except Exception as e:
3209 exc = traceback.format_exc()
3210 self.logger.critical(logging_text + "Exit Exception {} {}".format(type(e).__name__, e), exc_info=True)
3211 finally:
3212 if exc and db_nslcmop:
kuuse0ca67472019-05-13 15:59:27 +02003213 db_nslcmop_update["detailed-status"] = nslcmop_operation_state_detail = \
3214 "FAILED {}: {}".format(step, exc)
tierno59d22d22018-09-25 18:10:19 +02003215 db_nslcmop_update["operationState"] = nslcmop_operation_state = "FAILED"
3216 db_nslcmop_update["statusEnteredTime"] = time()
tiernobaa51102018-12-14 13:16:18 +00003217 try:
3218 if db_nslcmop_update:
3219 self.update_db_2("nslcmops", nslcmop_id, db_nslcmop_update)
3220 if db_nsr:
quilesj4cda56b2019-12-05 10:02:20 +00003221 self._write_ns_status(
3222 nsr_id=nsr_id,
3223 ns_state=None,
3224 current_operation="IDLE",
tiernoe876f672020-02-13 14:34:48 +00003225 current_operation_id=None,
3226 other_update=db_nsr_update
quilesj4cda56b2019-12-05 10:02:20 +00003227 )
quilesj3655ae02019-12-12 16:08:35 +00003228 if exc:
3229 self._write_op_status(
3230 op_id=nslcmop_id,
3231 error_message=nslcmop_operation_state_detail
3232 )
tiernobaa51102018-12-14 13:16:18 +00003233 except DbException as e:
3234 self.logger.error(logging_text + "Cannot update database: {}".format(e))
tierno59d22d22018-09-25 18:10:19 +02003235 self.logger.debug(logging_text + "Exit")
3236 if nslcmop_operation_state:
3237 try:
3238 await self.msg.aiowrite("ns", "actioned", {"nsr_id": nsr_id, "nslcmop_id": nslcmop_id,
tierno8a518872018-12-21 13:42:14 +00003239 "operationState": nslcmop_operation_state},
3240 loop=self.loop)
tierno59d22d22018-09-25 18:10:19 +02003241 except Exception as e:
3242 self.logger.error(logging_text + "kafka_write notification Exception {}".format(e))
3243 self.logger.debug(logging_text + "Exit")
3244 self.lcm_tasks.remove("ns", nsr_id, nslcmop_id, "ns_action")
kuuse0ca67472019-05-13 15:59:27 +02003245 return nslcmop_operation_state, nslcmop_operation_state_detail
tierno59d22d22018-09-25 18:10:19 +02003246
3247 async def scale(self, nsr_id, nslcmop_id):
kuused124bfe2019-06-18 12:09:24 +02003248
3249 # Try to lock HA task here
3250 task_is_locked_by_me = self.lcm_tasks.lock_HA('ns', 'nslcmops', nslcmop_id)
3251 if not task_is_locked_by_me:
3252 return
3253
tierno59d22d22018-09-25 18:10:19 +02003254 logging_text = "Task ns={} scale={} ".format(nsr_id, nslcmop_id)
3255 self.logger.debug(logging_text + "Enter")
3256 # get all needed from database
3257 db_nsr = None
3258 db_nslcmop = None
3259 db_nslcmop_update = {}
3260 nslcmop_operation_state = None
tiernoe876f672020-02-13 14:34:48 +00003261 db_nsr_update = {}
tierno59d22d22018-09-25 18:10:19 +02003262 exc = None
tierno9ab95942018-10-10 16:44:22 +02003263 # in case of error, indicates what part of scale was failed to put nsr at error status
3264 scale_process = None
tiernod6de1992018-10-11 13:05:52 +02003265 old_operational_status = ""
3266 old_config_status = ""
tiernof578e552018-11-08 19:07:20 +01003267 vnfr_scaled = False
tierno59d22d22018-09-25 18:10:19 +02003268 try:
kuused124bfe2019-06-18 12:09:24 +02003269 # wait for any previous tasks in process
tierno3cf81a32019-11-11 17:07:00 +00003270 step = "Waiting for previous operations to terminate"
kuused124bfe2019-06-18 12:09:24 +02003271 await self.lcm_tasks.waitfor_related_HA('ns', 'nslcmops', nslcmop_id)
tierno47e86b52018-10-10 14:05:55 +02003272
quilesj4cda56b2019-12-05 10:02:20 +00003273 self._write_ns_status(
3274 nsr_id=nsr_id,
3275 ns_state=None,
3276 current_operation="SCALING",
3277 current_operation_id=nslcmop_id
3278 )
3279
ikalyvas02d9e7b2019-05-27 18:16:01 +03003280 step = "Getting nslcmop from database"
ikalyvas02d9e7b2019-05-27 18:16:01 +03003281 self.logger.debug(step + " after having waited for previous tasks to be completed")
3282 db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
3283 step = "Getting nsr from database"
3284 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
3285
3286 old_operational_status = db_nsr["operational-status"]
3287 old_config_status = db_nsr["config-status"]
tierno59d22d22018-09-25 18:10:19 +02003288 step = "Parsing scaling parameters"
tierno9babfda2019-06-07 12:36:50 +00003289 # self.logger.debug(step)
tierno59d22d22018-09-25 18:10:19 +02003290 db_nsr_update["operational-status"] = "scaling"
3291 self.update_db_2("nsrs", nsr_id, db_nsr_update)
tiernoe4f7e6c2018-11-27 14:55:30 +00003292 nsr_deployed = db_nsr["_admin"].get("deployed")
calvinosanch9f9c6f22019-11-04 13:37:39 +01003293
3294 #######
3295 nsr_deployed = db_nsr["_admin"].get("deployed")
3296 vnf_index = db_nslcmop["operationParams"].get("member_vnf_index")
tiernoda6fb102019-11-23 00:36:52 +00003297 # vdu_id = db_nslcmop["operationParams"].get("vdu_id")
3298 # vdu_count_index = db_nslcmop["operationParams"].get("vdu_count_index")
3299 # vdu_name = db_nslcmop["operationParams"].get("vdu_name")
calvinosanch9f9c6f22019-11-04 13:37:39 +01003300 #######
3301
tiernoe4f7e6c2018-11-27 14:55:30 +00003302 RO_nsr_id = nsr_deployed["RO"]["nsr_id"]
tierno59d22d22018-09-25 18:10:19 +02003303 vnf_index = db_nslcmop["operationParams"]["scaleVnfData"]["scaleByStepData"]["member-vnf-index"]
3304 scaling_group = db_nslcmop["operationParams"]["scaleVnfData"]["scaleByStepData"]["scaling-group-descriptor"]
3305 scaling_type = db_nslcmop["operationParams"]["scaleVnfData"]["scaleVnfType"]
3306 # scaling_policy = db_nslcmop["operationParams"]["scaleVnfData"]["scaleByStepData"].get("scaling-policy")
3307
tierno82974b22018-11-27 21:55:36 +00003308 # for backward compatibility
3309 if nsr_deployed and isinstance(nsr_deployed.get("VCA"), dict):
3310 nsr_deployed["VCA"] = list(nsr_deployed["VCA"].values())
3311 db_nsr_update["_admin.deployed.VCA"] = nsr_deployed["VCA"]
3312 self.update_db_2("nsrs", nsr_id, db_nsr_update)
3313
tierno59d22d22018-09-25 18:10:19 +02003314 step = "Getting vnfr from database"
3315 db_vnfr = self.db.get_one("vnfrs", {"member-vnf-index-ref": vnf_index, "nsr-id-ref": nsr_id})
3316 step = "Getting vnfd from database"
3317 db_vnfd = self.db.get_one("vnfds", {"_id": db_vnfr["vnfd-id"]})
ikalyvas02d9e7b2019-05-27 18:16:01 +03003318
tierno59d22d22018-09-25 18:10:19 +02003319 step = "Getting scaling-group-descriptor"
3320 for scaling_descriptor in db_vnfd["scaling-group-descriptor"]:
3321 if scaling_descriptor["name"] == scaling_group:
3322 break
3323 else:
3324 raise LcmException("input parameter 'scaleByStepData':'scaling-group-descriptor':'{}' is not present "
3325 "at vnfd:scaling-group-descriptor".format(scaling_group))
ikalyvas02d9e7b2019-05-27 18:16:01 +03003326
tierno59d22d22018-09-25 18:10:19 +02003327 # cooldown_time = 0
3328 # for scaling_policy_descriptor in scaling_descriptor.get("scaling-policy", ()):
3329 # cooldown_time = scaling_policy_descriptor.get("cooldown-time", 0)
3330 # if scaling_policy and scaling_policy == scaling_policy_descriptor.get("name"):
3331 # break
3332
3333 # TODO check if ns is in a proper status
tierno15b1cf12019-08-29 13:21:40 +00003334 step = "Sending scale order to VIM"
tierno59d22d22018-09-25 18:10:19 +02003335 nb_scale_op = 0
3336 if not db_nsr["_admin"].get("scaling-group"):
3337 self.update_db_2("nsrs", nsr_id, {"_admin.scaling-group": [{"name": scaling_group, "nb-scale-op": 0}]})
3338 admin_scale_index = 0
3339 else:
3340 for admin_scale_index, admin_scale_info in enumerate(db_nsr["_admin"]["scaling-group"]):
3341 if admin_scale_info["name"] == scaling_group:
3342 nb_scale_op = admin_scale_info.get("nb-scale-op", 0)
3343 break
tierno9ab95942018-10-10 16:44:22 +02003344 else: # not found, set index one plus last element and add new entry with the name
3345 admin_scale_index += 1
3346 db_nsr_update["_admin.scaling-group.{}.name".format(admin_scale_index)] = scaling_group
tierno59d22d22018-09-25 18:10:19 +02003347 RO_scaling_info = []
3348 vdu_scaling_info = {"scaling_group_name": scaling_group, "vdu": []}
3349 if scaling_type == "SCALE_OUT":
3350 # count if max-instance-count is reached
kuuse818d70c2019-08-07 14:43:44 +02003351 max_instance_count = scaling_descriptor.get("max-instance-count", 10)
3352 # self.logger.debug("MAX_INSTANCE_COUNT is {}".format(max_instance_count))
3353 if nb_scale_op >= max_instance_count:
3354 raise LcmException("reached the limit of {} (max-instance-count) "
3355 "scaling-out operations for the "
3356 "scaling-group-descriptor '{}'".format(nb_scale_op, scaling_group))
kuuse8b998e42019-07-30 15:22:16 +02003357
ikalyvas02d9e7b2019-05-27 18:16:01 +03003358 nb_scale_op += 1
tierno59d22d22018-09-25 18:10:19 +02003359 vdu_scaling_info["scaling_direction"] = "OUT"
3360 vdu_scaling_info["vdu-create"] = {}
3361 for vdu_scale_info in scaling_descriptor["vdu"]:
3362 RO_scaling_info.append({"osm_vdu_id": vdu_scale_info["vdu-id-ref"], "member-vnf-index": vnf_index,
3363 "type": "create", "count": vdu_scale_info.get("count", 1)})
3364 vdu_scaling_info["vdu-create"][vdu_scale_info["vdu-id-ref"]] = vdu_scale_info.get("count", 1)
ikalyvas02d9e7b2019-05-27 18:16:01 +03003365
tierno59d22d22018-09-25 18:10:19 +02003366 elif scaling_type == "SCALE_IN":
3367 # count if min-instance-count is reached
tierno27246d82018-09-27 15:59:09 +02003368 min_instance_count = 0
tierno59d22d22018-09-25 18:10:19 +02003369 if "min-instance-count" in scaling_descriptor and scaling_descriptor["min-instance-count"] is not None:
3370 min_instance_count = int(scaling_descriptor["min-instance-count"])
tierno9babfda2019-06-07 12:36:50 +00003371 if nb_scale_op <= min_instance_count:
3372 raise LcmException("reached the limit of {} (min-instance-count) scaling-in operations for the "
3373 "scaling-group-descriptor '{}'".format(nb_scale_op, scaling_group))
ikalyvas02d9e7b2019-05-27 18:16:01 +03003374 nb_scale_op -= 1
tierno59d22d22018-09-25 18:10:19 +02003375 vdu_scaling_info["scaling_direction"] = "IN"
3376 vdu_scaling_info["vdu-delete"] = {}
3377 for vdu_scale_info in scaling_descriptor["vdu"]:
3378 RO_scaling_info.append({"osm_vdu_id": vdu_scale_info["vdu-id-ref"], "member-vnf-index": vnf_index,
3379 "type": "delete", "count": vdu_scale_info.get("count", 1)})
3380 vdu_scaling_info["vdu-delete"][vdu_scale_info["vdu-id-ref"]] = vdu_scale_info.get("count", 1)
3381
3382 # update VDU_SCALING_INFO with the VDUs to delete ip_addresses
tierno27246d82018-09-27 15:59:09 +02003383 vdu_create = vdu_scaling_info.get("vdu-create")
3384 vdu_delete = copy(vdu_scaling_info.get("vdu-delete"))
tierno59d22d22018-09-25 18:10:19 +02003385 if vdu_scaling_info["scaling_direction"] == "IN":
3386 for vdur in reversed(db_vnfr["vdur"]):
tierno27246d82018-09-27 15:59:09 +02003387 if vdu_delete.get(vdur["vdu-id-ref"]):
3388 vdu_delete[vdur["vdu-id-ref"]] -= 1
tierno59d22d22018-09-25 18:10:19 +02003389 vdu_scaling_info["vdu"].append({
3390 "name": vdur["name"],
3391 "vdu_id": vdur["vdu-id-ref"],
3392 "interface": []
3393 })
3394 for interface in vdur["interfaces"]:
3395 vdu_scaling_info["vdu"][-1]["interface"].append({
3396 "name": interface["name"],
3397 "ip_address": interface["ip-address"],
3398 "mac_address": interface.get("mac-address"),
3399 })
tierno27246d82018-09-27 15:59:09 +02003400 vdu_delete = vdu_scaling_info.pop("vdu-delete")
tierno59d22d22018-09-25 18:10:19 +02003401
kuuseac3a8882019-10-03 10:48:06 +02003402 # PRE-SCALE BEGIN
tierno59d22d22018-09-25 18:10:19 +02003403 step = "Executing pre-scale vnf-config-primitive"
3404 if scaling_descriptor.get("scaling-config-action"):
3405 for scaling_config_action in scaling_descriptor["scaling-config-action"]:
kuuseac3a8882019-10-03 10:48:06 +02003406 if (scaling_config_action.get("trigger") == "pre-scale-in" and scaling_type == "SCALE_IN") \
3407 or (scaling_config_action.get("trigger") == "pre-scale-out" and scaling_type == "SCALE_OUT"):
tierno59d22d22018-09-25 18:10:19 +02003408 vnf_config_primitive = scaling_config_action["vnf-config-primitive-name-ref"]
3409 step = db_nslcmop_update["detailed-status"] = \
3410 "executing pre-scale scaling-config-action '{}'".format(vnf_config_primitive)
tiernoda964822019-01-14 15:53:47 +00003411
tierno59d22d22018-09-25 18:10:19 +02003412 # look for primitive
tierno59d22d22018-09-25 18:10:19 +02003413 for config_primitive in db_vnfd.get("vnf-configuration", {}).get("config-primitive", ()):
3414 if config_primitive["name"] == vnf_config_primitive:
tierno59d22d22018-09-25 18:10:19 +02003415 break
3416 else:
3417 raise LcmException(
3418 "Invalid vnfd descriptor at scaling-group-descriptor[name='{}']:scaling-config-action"
tiernoda964822019-01-14 15:53:47 +00003419 "[vnf-config-primitive-name-ref='{}'] does not match any vnf-configuration:config-"
tierno59d22d22018-09-25 18:10:19 +02003420 "primitive".format(scaling_group, config_primitive))
tiernoda964822019-01-14 15:53:47 +00003421
tierno16fedf52019-05-24 08:38:26 +00003422 vnfr_params = {"VDU_SCALE_INFO": vdu_scaling_info}
tiernoda964822019-01-14 15:53:47 +00003423 if db_vnfr.get("additionalParamsForVnf"):
3424 vnfr_params.update(db_vnfr["additionalParamsForVnf"])
quilesj7e13aeb2019-10-08 13:34:55 +02003425
tierno9ab95942018-10-10 16:44:22 +02003426 scale_process = "VCA"
tiernod6de1992018-10-11 13:05:52 +02003427 db_nsr_update["config-status"] = "configuring pre-scaling"
kuuseac3a8882019-10-03 10:48:06 +02003428 primitive_params = self._map_primitive_params(config_primitive, {}, vnfr_params)
3429
3430 # Pre-scale reintent check: Check if this sub-operation has been executed before
3431 op_index = self._check_or_add_scale_suboperation(
3432 db_nslcmop, nslcmop_id, vnf_index, vnf_config_primitive, primitive_params, 'PRE-SCALE')
3433 if (op_index == self.SUBOPERATION_STATUS_SKIP):
3434 # Skip sub-operation
3435 result = 'COMPLETED'
3436 result_detail = 'Done'
3437 self.logger.debug(logging_text +
3438 "vnf_config_primitive={} Skipped sub-operation, result {} {}".format(
3439 vnf_config_primitive, result, result_detail))
3440 else:
3441 if (op_index == self.SUBOPERATION_STATUS_NEW):
3442 # New sub-operation: Get index of this sub-operation
3443 op_index = len(db_nslcmop.get('_admin', {}).get('operations')) - 1
3444 self.logger.debug(logging_text + "vnf_config_primitive={} New sub-operation".
3445 format(vnf_config_primitive))
3446 else:
3447 # Reintent: Get registered params for this existing sub-operation
3448 op = db_nslcmop.get('_admin', {}).get('operations', [])[op_index]
3449 vnf_index = op.get('member_vnf_index')
3450 vnf_config_primitive = op.get('primitive')
3451 primitive_params = op.get('primitive_params')
3452 self.logger.debug(logging_text + "vnf_config_primitive={} Sub-operation reintent".
3453 format(vnf_config_primitive))
3454 # Execute the primitive, either with new (first-time) or registered (reintent) args
3455 result, result_detail = await self._ns_execute_primitive(
tiernoe876f672020-02-13 14:34:48 +00003456 self._look_for_deployed_vca(nsr_deployed["VCA"],
3457 member_vnf_index=vnf_index,
3458 vdu_id=None,
3459 vdu_name=None,
3460 vdu_count_index=None),
3461 vnf_config_primitive, primitive_params)
kuuseac3a8882019-10-03 10:48:06 +02003462 self.logger.debug(logging_text + "vnf_config_primitive={} Done with result {} {}".format(
3463 vnf_config_primitive, result, result_detail))
3464 # Update operationState = COMPLETED | FAILED
3465 self._update_suboperation_status(
3466 db_nslcmop, op_index, result, result_detail)
3467
tierno59d22d22018-09-25 18:10:19 +02003468 if result == "FAILED":
3469 raise LcmException(result_detail)
tiernod6de1992018-10-11 13:05:52 +02003470 db_nsr_update["config-status"] = old_config_status
3471 scale_process = None
kuuseac3a8882019-10-03 10:48:06 +02003472 # PRE-SCALE END
tierno59d22d22018-09-25 18:10:19 +02003473
kuuseac3a8882019-10-03 10:48:06 +02003474 # SCALE RO - BEGIN
3475 # Should this block be skipped if 'RO_nsr_id' == None ?
3476 # if (RO_nsr_id and RO_scaling_info):
tierno59d22d22018-09-25 18:10:19 +02003477 if RO_scaling_info:
tierno9ab95942018-10-10 16:44:22 +02003478 scale_process = "RO"
kuuseac3a8882019-10-03 10:48:06 +02003479 # Scale RO reintent check: Check if this sub-operation has been executed before
3480 op_index = self._check_or_add_scale_suboperation(
3481 db_nslcmop, vnf_index, None, None, 'SCALE-RO', RO_nsr_id, RO_scaling_info)
3482 if (op_index == self.SUBOPERATION_STATUS_SKIP):
3483 # Skip sub-operation
3484 result = 'COMPLETED'
3485 result_detail = 'Done'
3486 self.logger.debug(logging_text + "Skipped sub-operation RO, result {} {}".format(
3487 result, result_detail))
3488 else:
3489 if (op_index == self.SUBOPERATION_STATUS_NEW):
3490 # New sub-operation: Get index of this sub-operation
3491 op_index = len(db_nslcmop.get('_admin', {}).get('operations')) - 1
3492 self.logger.debug(logging_text + "New sub-operation RO")
tierno59d22d22018-09-25 18:10:19 +02003493 else:
kuuseac3a8882019-10-03 10:48:06 +02003494 # Reintent: Get registered params for this existing sub-operation
3495 op = db_nslcmop.get('_admin', {}).get('operations', [])[op_index]
3496 RO_nsr_id = op.get('RO_nsr_id')
3497 RO_scaling_info = op.get('RO_scaling_info')
3498 self.logger.debug(logging_text + "Sub-operation RO reintent".format(
3499 vnf_config_primitive))
3500
3501 RO_desc = await self.RO.create_action("ns", RO_nsr_id, {"vdu-scaling": RO_scaling_info})
3502 db_nsr_update["_admin.scaling-group.{}.nb-scale-op".format(admin_scale_index)] = nb_scale_op
3503 db_nsr_update["_admin.scaling-group.{}.time".format(admin_scale_index)] = time()
3504 # wait until ready
3505 RO_nslcmop_id = RO_desc["instance_action_id"]
3506 db_nslcmop_update["_admin.deploy.RO"] = RO_nslcmop_id
3507
3508 RO_task_done = False
3509 step = detailed_status = "Waiting RO_task_id={} to complete the scale action.".format(RO_nslcmop_id)
3510 detailed_status_old = None
3511 self.logger.debug(logging_text + step)
3512
3513 deployment_timeout = 1 * 3600 # One hour
3514 while deployment_timeout > 0:
3515 if not RO_task_done:
3516 desc = await self.RO.show("ns", item_id_name=RO_nsr_id, extra_item="action",
3517 extra_item_id=RO_nslcmop_id)
quilesj3655ae02019-12-12 16:08:35 +00003518
3519 # deploymentStatus
3520 self._on_update_ro_db(nsrs_id=nsr_id, ro_descriptor=desc)
3521
kuuseac3a8882019-10-03 10:48:06 +02003522 ns_status, ns_status_info = self.RO.check_action_status(desc)
3523 if ns_status == "ERROR":
3524 raise ROclient.ROClientException(ns_status_info)
3525 elif ns_status == "BUILD":
3526 detailed_status = step + "; {}".format(ns_status_info)
3527 elif ns_status == "ACTIVE":
3528 RO_task_done = True
3529 step = detailed_status = "Waiting ns ready at RO. RO_id={}".format(RO_nsr_id)
3530 self.logger.debug(logging_text + step)
3531 else:
3532 assert False, "ROclient.check_action_status returns unknown {}".format(ns_status)
tierno59d22d22018-09-25 18:10:19 +02003533 else:
quilesj7e13aeb2019-10-08 13:34:55 +02003534
kuuseac3a8882019-10-03 10:48:06 +02003535 if ns_status == "ERROR":
3536 raise ROclient.ROClientException(ns_status_info)
3537 elif ns_status == "BUILD":
3538 detailed_status = step + "; {}".format(ns_status_info)
3539 elif ns_status == "ACTIVE":
3540 step = detailed_status = \
3541 "Waiting for management IP address reported by the VIM. Updating VNFRs"
3542 if not vnfr_scaled:
3543 self.scale_vnfr(db_vnfr, vdu_create=vdu_create, vdu_delete=vdu_delete)
3544 vnfr_scaled = True
3545 try:
3546 desc = await self.RO.show("ns", RO_nsr_id)
quilesj3655ae02019-12-12 16:08:35 +00003547
3548 # deploymentStatus
3549 self._on_update_ro_db(nsrs_id=nsr_id, ro_descriptor=desc)
3550
kuuseac3a8882019-10-03 10:48:06 +02003551 # nsr_deployed["nsr_ip"] = RO.get_ns_vnf_info(desc)
3552 self.ns_update_vnfr({db_vnfr["member-vnf-index-ref"]: db_vnfr}, desc)
3553 break
3554 except LcmExceptionNoMgmtIP:
3555 pass
3556 else:
3557 assert False, "ROclient.check_ns_status returns unknown {}".format(ns_status)
3558 if detailed_status != detailed_status_old:
3559 self._update_suboperation_status(
3560 db_nslcmop, op_index, 'COMPLETED', detailed_status)
3561 detailed_status_old = db_nslcmop_update["detailed-status"] = detailed_status
3562 self.update_db_2("nslcmops", nslcmop_id, db_nslcmop_update)
tierno59d22d22018-09-25 18:10:19 +02003563
kuuseac3a8882019-10-03 10:48:06 +02003564 await asyncio.sleep(5, loop=self.loop)
3565 deployment_timeout -= 5
3566 if deployment_timeout <= 0:
3567 self._update_suboperation_status(
3568 db_nslcmop, nslcmop_id, op_index, 'FAILED', "Timeout when waiting for ns to get ready")
3569 raise ROclient.ROClientException("Timeout waiting ns to be ready")
tierno59d22d22018-09-25 18:10:19 +02003570
kuuseac3a8882019-10-03 10:48:06 +02003571 # update VDU_SCALING_INFO with the obtained ip_addresses
3572 if vdu_scaling_info["scaling_direction"] == "OUT":
3573 for vdur in reversed(db_vnfr["vdur"]):
3574 if vdu_scaling_info["vdu-create"].get(vdur["vdu-id-ref"]):
3575 vdu_scaling_info["vdu-create"][vdur["vdu-id-ref"]] -= 1
3576 vdu_scaling_info["vdu"].append({
3577 "name": vdur["name"],
3578 "vdu_id": vdur["vdu-id-ref"],
3579 "interface": []
tierno59d22d22018-09-25 18:10:19 +02003580 })
kuuseac3a8882019-10-03 10:48:06 +02003581 for interface in vdur["interfaces"]:
3582 vdu_scaling_info["vdu"][-1]["interface"].append({
3583 "name": interface["name"],
3584 "ip_address": interface["ip-address"],
3585 "mac_address": interface.get("mac-address"),
3586 })
3587 del vdu_scaling_info["vdu-create"]
3588
3589 self._update_suboperation_status(db_nslcmop, op_index, 'COMPLETED', 'Done')
3590 # SCALE RO - END
tierno59d22d22018-09-25 18:10:19 +02003591
tierno9ab95942018-10-10 16:44:22 +02003592 scale_process = None
tierno59d22d22018-09-25 18:10:19 +02003593 if db_nsr_update:
3594 self.update_db_2("nsrs", nsr_id, db_nsr_update)
3595
kuuseac3a8882019-10-03 10:48:06 +02003596 # POST-SCALE BEGIN
tierno59d22d22018-09-25 18:10:19 +02003597 # execute primitive service POST-SCALING
3598 step = "Executing post-scale vnf-config-primitive"
3599 if scaling_descriptor.get("scaling-config-action"):
3600 for scaling_config_action in scaling_descriptor["scaling-config-action"]:
kuuseac3a8882019-10-03 10:48:06 +02003601 if (scaling_config_action.get("trigger") == "post-scale-in" and scaling_type == "SCALE_IN") \
3602 or (scaling_config_action.get("trigger") == "post-scale-out" and scaling_type == "SCALE_OUT"):
tierno59d22d22018-09-25 18:10:19 +02003603 vnf_config_primitive = scaling_config_action["vnf-config-primitive-name-ref"]
3604 step = db_nslcmop_update["detailed-status"] = \
3605 "executing post-scale scaling-config-action '{}'".format(vnf_config_primitive)
tiernoda964822019-01-14 15:53:47 +00003606
tierno589befb2019-05-29 07:06:23 +00003607 vnfr_params = {"VDU_SCALE_INFO": vdu_scaling_info}
tiernoda964822019-01-14 15:53:47 +00003608 if db_vnfr.get("additionalParamsForVnf"):
3609 vnfr_params.update(db_vnfr["additionalParamsForVnf"])
3610
tierno59d22d22018-09-25 18:10:19 +02003611 # look for primitive
tierno59d22d22018-09-25 18:10:19 +02003612 for config_primitive in db_vnfd.get("vnf-configuration", {}).get("config-primitive", ()):
3613 if config_primitive["name"] == vnf_config_primitive:
tierno59d22d22018-09-25 18:10:19 +02003614 break
3615 else:
3616 raise LcmException("Invalid vnfd descriptor at scaling-group-descriptor[name='{}']:"
3617 "scaling-config-action[vnf-config-primitive-name-ref='{}'] does not "
tierno47e86b52018-10-10 14:05:55 +02003618 "match any vnf-configuration:config-primitive".format(scaling_group,
3619 config_primitive))
tierno9ab95942018-10-10 16:44:22 +02003620 scale_process = "VCA"
tiernod6de1992018-10-11 13:05:52 +02003621 db_nsr_update["config-status"] = "configuring post-scaling"
kuuseac3a8882019-10-03 10:48:06 +02003622 primitive_params = self._map_primitive_params(config_primitive, {}, vnfr_params)
tiernod6de1992018-10-11 13:05:52 +02003623
kuuseac3a8882019-10-03 10:48:06 +02003624 # Post-scale reintent check: Check if this sub-operation has been executed before
3625 op_index = self._check_or_add_scale_suboperation(
3626 db_nslcmop, nslcmop_id, vnf_index, vnf_config_primitive, primitive_params, 'POST-SCALE')
quilesj4cda56b2019-12-05 10:02:20 +00003627 if op_index == self.SUBOPERATION_STATUS_SKIP:
kuuseac3a8882019-10-03 10:48:06 +02003628 # Skip sub-operation
3629 result = 'COMPLETED'
3630 result_detail = 'Done'
3631 self.logger.debug(logging_text +
3632 "vnf_config_primitive={} Skipped sub-operation, result {} {}".
3633 format(vnf_config_primitive, result, result_detail))
3634 else:
quilesj4cda56b2019-12-05 10:02:20 +00003635 if op_index == self.SUBOPERATION_STATUS_NEW:
kuuseac3a8882019-10-03 10:48:06 +02003636 # New sub-operation: Get index of this sub-operation
3637 op_index = len(db_nslcmop.get('_admin', {}).get('operations')) - 1
3638 self.logger.debug(logging_text + "vnf_config_primitive={} New sub-operation".
3639 format(vnf_config_primitive))
3640 else:
3641 # Reintent: Get registered params for this existing sub-operation
3642 op = db_nslcmop.get('_admin', {}).get('operations', [])[op_index]
3643 vnf_index = op.get('member_vnf_index')
3644 vnf_config_primitive = op.get('primitive')
3645 primitive_params = op.get('primitive_params')
3646 self.logger.debug(logging_text + "vnf_config_primitive={} Sub-operation reintent".
3647 format(vnf_config_primitive))
3648 # Execute the primitive, either with new (first-time) or registered (reintent) args
3649 result, result_detail = await self._ns_execute_primitive(
tiernoe876f672020-02-13 14:34:48 +00003650 self._look_for_deployed_vca(nsr_deployed["VCA"],
3651 member_vnf_index=vnf_index,
3652 vdu_id=None,
3653 vdu_name=None,
3654 vdu_count_index=None),
3655 vnf_config_primitive, primitive_params)
kuuseac3a8882019-10-03 10:48:06 +02003656 self.logger.debug(logging_text + "vnf_config_primitive={} Done with result {} {}".format(
3657 vnf_config_primitive, result, result_detail))
3658 # Update operationState = COMPLETED | FAILED
3659 self._update_suboperation_status(
3660 db_nslcmop, op_index, result, result_detail)
3661
tierno59d22d22018-09-25 18:10:19 +02003662 if result == "FAILED":
3663 raise LcmException(result_detail)
tiernod6de1992018-10-11 13:05:52 +02003664 db_nsr_update["config-status"] = old_config_status
3665 scale_process = None
kuuseac3a8882019-10-03 10:48:06 +02003666 # POST-SCALE END
tierno59d22d22018-09-25 18:10:19 +02003667
3668 db_nslcmop_update["operationState"] = nslcmop_operation_state = "COMPLETED"
3669 db_nslcmop_update["statusEnteredTime"] = time()
3670 db_nslcmop_update["detailed-status"] = "done"
tiernod6de1992018-10-11 13:05:52 +02003671 db_nsr_update["detailed-status"] = "" # "scaled {} {}".format(scaling_group, scaling_type)
ikalyvas02d9e7b2019-05-27 18:16:01 +03003672 db_nsr_update["operational-status"] = "running" if old_operational_status == "failed" \
3673 else old_operational_status
tiernod6de1992018-10-11 13:05:52 +02003674 db_nsr_update["config-status"] = old_config_status
tierno59d22d22018-09-25 18:10:19 +02003675 return
3676 except (ROclient.ROClientException, DbException, LcmException) as e:
3677 self.logger.error(logging_text + "Exit Exception {}".format(e))
3678 exc = e
3679 except asyncio.CancelledError:
3680 self.logger.error(logging_text + "Cancelled Exception while '{}'".format(step))
3681 exc = "Operation was cancelled"
3682 except Exception as e:
3683 exc = traceback.format_exc()
3684 self.logger.critical(logging_text + "Exit Exception {} {}".format(type(e).__name__, e), exc_info=True)
3685 finally:
quilesj3655ae02019-12-12 16:08:35 +00003686 self._write_ns_status(
3687 nsr_id=nsr_id,
3688 ns_state=None,
3689 current_operation="IDLE",
3690 current_operation_id=None
3691 )
tierno59d22d22018-09-25 18:10:19 +02003692 if exc:
3693 if db_nslcmop:
3694 db_nslcmop_update["detailed-status"] = "FAILED {}: {}".format(step, exc)
3695 db_nslcmop_update["operationState"] = nslcmop_operation_state = "FAILED"
3696 db_nslcmop_update["statusEnteredTime"] = time()
3697 if db_nsr:
tiernod6de1992018-10-11 13:05:52 +02003698 db_nsr_update["operational-status"] = old_operational_status
3699 db_nsr_update["config-status"] = old_config_status
3700 db_nsr_update["detailed-status"] = ""
3701 if scale_process:
3702 if "VCA" in scale_process:
3703 db_nsr_update["config-status"] = "failed"
3704 if "RO" in scale_process:
3705 db_nsr_update["operational-status"] = "failed"
3706 db_nsr_update["detailed-status"] = "FAILED scaling nslcmop={} {}: {}".format(nslcmop_id, step,
3707 exc)
tiernobaa51102018-12-14 13:16:18 +00003708 try:
3709 if db_nslcmop and db_nslcmop_update:
3710 self.update_db_2("nslcmops", nslcmop_id, db_nslcmop_update)
3711 if db_nsr:
quilesj4cda56b2019-12-05 10:02:20 +00003712 self._write_ns_status(
3713 nsr_id=nsr_id,
3714 ns_state=None,
3715 current_operation="IDLE",
tiernoe876f672020-02-13 14:34:48 +00003716 current_operation_id=None,
3717 other_update=db_nsr_update
quilesj4cda56b2019-12-05 10:02:20 +00003718 )
3719
tiernobaa51102018-12-14 13:16:18 +00003720 except DbException as e:
3721 self.logger.error(logging_text + "Cannot update database: {}".format(e))
tierno59d22d22018-09-25 18:10:19 +02003722 if nslcmop_operation_state:
3723 try:
3724 await self.msg.aiowrite("ns", "scaled", {"nsr_id": nsr_id, "nslcmop_id": nslcmop_id,
tierno8a518872018-12-21 13:42:14 +00003725 "operationState": nslcmop_operation_state},
3726 loop=self.loop)
tierno59d22d22018-09-25 18:10:19 +02003727 # if cooldown_time:
tiernod8323042019-08-09 11:32:23 +00003728 # await asyncio.sleep(cooldown_time, loop=self.loop)
tierno59d22d22018-09-25 18:10:19 +02003729 # await self.msg.aiowrite("ns","scaled-cooldown-time", {"nsr_id": nsr_id, "nslcmop_id": nslcmop_id})
3730 except Exception as e:
3731 self.logger.error(logging_text + "kafka_write notification Exception {}".format(e))
3732 self.logger.debug(logging_text + "Exit")
3733 self.lcm_tasks.remove("ns", nsr_id, nslcmop_id, "ns_scale")