blob: 724b22dbede0e46cde89a040bf6e5e68ed715e88 [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
23import functools
24import traceback
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
28from osm_lcm.lcm_utils import LcmException, LcmExceptionNoMgmtIP, LcmBase
tierno59d22d22018-09-25 18:10:19 +020029
tierno27246d82018-09-27 15:59:09 +020030from osm_common.dbbase import DbException
tierno59d22d22018-09-25 18:10:19 +020031from osm_common.fsbase import FsException
tierno46b75f12019-07-04 17:17:06 +000032from n2vc.vnf import N2VC, N2VCPrimitiveExecutionFailed, NetworkServiceDoesNotExist, PrimitiveDoesNotExist
tierno59d22d22018-09-25 18:10:19 +020033
tierno27246d82018-09-27 15:59:09 +020034from copy import copy, deepcopy
tierno59d22d22018-09-25 18:10:19 +020035from http import HTTPStatus
36from time import time
tierno27246d82018-09-27 15:59:09 +020037from uuid import uuid4
tierno59d22d22018-09-25 18:10:19 +020038
39__author__ = "Alfonso Tierno"
40
41
tierno27246d82018-09-27 15:59:09 +020042def get_iterable(in_dict, in_key):
43 """
44 Similar to <dict>.get(), but if value is None, False, ..., An empty tuple is returned instead
45 :param in_dict: a dictionary
46 :param in_key: the key to look for at in_dict
47 :return: in_dict[in_var] or () if it is None or not present
48 """
49 if not in_dict.get(in_key):
50 return ()
51 return in_dict[in_key]
52
53
54def populate_dict(target_dict, key_list, value):
55 """
Adam Israel78770df2019-09-04 11:55:55 -040056 Update target_dict creating nested dictionaries with the key_list. Last key_list item is asigned the value.
tierno27246d82018-09-27 15:59:09 +020057 Example target_dict={K: J}; key_list=[a,b,c]; target_dict will be {K: J, a: {b: {c: value}}}
58 :param target_dict: dictionary to be changed
59 :param key_list: list of keys to insert at target_dict
60 :param value:
61 :return: None
62 """
63 for key in key_list[0:-1]:
64 if key not in target_dict:
65 target_dict[key] = {}
66 target_dict = target_dict[key]
67 target_dict[key_list[-1]] = value
68
69
tierno6cf25f52019-09-12 09:33:40 +000070def deep_get(target_dict, key_list):
71 """
72 Get a value from target_dict entering in the nested keys. If keys does not exist, it returns None
73 Example target_dict={a: {b: 5}}; key_list=[a,b] returns 5; both key_list=[a,b,c] and key_list=[f,h] return None
74 :param target_dict: dictionary to be read
75 :param key_list: list of keys to read from target_dict
76 :return: The wanted value if exist, None otherwise
77 """
78 for key in key_list:
79 if not isinstance(target_dict, dict) or key not in target_dict:
80 return None
81 target_dict = target_dict[key]
82 return target_dict
83
84
tierno59d22d22018-09-25 18:10:19 +020085class NsLcm(LcmBase):
tierno63de62e2018-10-31 16:38:52 +010086 timeout_vca_on_error = 5 * 60 # Time for charm from first time at blocked,error status to mark as failed
garciadeblasf9b04952019-04-09 18:53:58 +020087 total_deploy_timeout = 2 * 3600 # global timeout for deployment
88 timeout_charm_delete = 10 * 60
89 timeout_primitive = 10 * 60 # timeout for primitive execution
tierno59d22d22018-09-25 18:10:19 +020090
kuuseac3a8882019-10-03 10:48:06 +020091 SUBOPERATION_STATUS_NOT_FOUND = -1
92 SUBOPERATION_STATUS_NEW = -2
93 SUBOPERATION_STATUS_SKIP = -3
94
tierno59d22d22018-09-25 18:10:19 +020095 def __init__(self, db, msg, fs, lcm_tasks, ro_config, vca_config, loop):
96 """
97 Init, Connect to database, filesystem storage, and messaging
98 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
99 :return: None
100 """
101 # logging
102 self.logger = logging.getLogger('lcm.ns')
103 self.loop = loop
104 self.lcm_tasks = lcm_tasks
105
106 super().__init__(db, msg, fs, self.logger)
107
108 self.ro_config = ro_config
109
110 self.n2vc = N2VC(
111 log=self.logger,
112 server=vca_config['host'],
113 port=vca_config['port'],
114 user=vca_config['user'],
115 secret=vca_config['secret'],
116 # TODO: This should point to the base folder where charms are stored,
117 # if there is a common one (like object storage). Otherwise, leave
118 # it unset and pass it via DeployCharms
119 # artifacts=vca_config[''],
120 artifacts=None,
tierno9597c292019-02-06 09:21:27 +0000121 juju_public_key=vca_config.get('pubkey'),
122 ca_cert=vca_config.get('cacert'),
Adam Israel78770df2019-09-04 11:55:55 -0400123 api_proxy=vca_config.get('apiproxy')
tierno59d22d22018-09-25 18:10:19 +0200124 )
tierno77677d92019-08-22 13:46:35 +0000125 self.RO = ROclient.ROClient(self.loop, **self.ro_config)
tierno59d22d22018-09-25 18:10:19 +0200126
gcalvino35be9152018-12-20 09:33:12 +0100127 def vnfd2RO(self, vnfd, new_id=None, additionalParams=None, nsrId=None):
tierno59d22d22018-09-25 18:10:19 +0200128 """
129 Converts creates a new vnfd descriptor for RO base on input OSM IM vnfd
130 :param vnfd: input vnfd
131 :param new_id: overrides vnf id if provided
tierno8a518872018-12-21 13:42:14 +0000132 :param additionalParams: Instantiation params for VNFs provided
gcalvino35be9152018-12-20 09:33:12 +0100133 :param nsrId: Id of the NSR
tierno59d22d22018-09-25 18:10:19 +0200134 :return: copy of vnfd
135 """
tierno59d22d22018-09-25 18:10:19 +0200136 try:
137 vnfd_RO = deepcopy(vnfd)
tierno8a518872018-12-21 13:42:14 +0000138 # remove unused by RO configuration, monitoring, scaling and internal keys
tierno59d22d22018-09-25 18:10:19 +0200139 vnfd_RO.pop("_id", None)
140 vnfd_RO.pop("_admin", None)
tierno8a518872018-12-21 13:42:14 +0000141 vnfd_RO.pop("vnf-configuration", None)
142 vnfd_RO.pop("monitoring-param", None)
143 vnfd_RO.pop("scaling-group-descriptor", None)
tierno59d22d22018-09-25 18:10:19 +0200144 if new_id:
145 vnfd_RO["id"] = new_id
tierno8a518872018-12-21 13:42:14 +0000146
147 # parse cloud-init or cloud-init-file with the provided variables using Jinja2
148 for vdu in get_iterable(vnfd_RO, "vdu"):
149 cloud_init_file = None
150 if vdu.get("cloud-init-file"):
tierno59d22d22018-09-25 18:10:19 +0200151 base_folder = vnfd["_admin"]["storage"]
gcalvino35be9152018-12-20 09:33:12 +0100152 cloud_init_file = "{}/{}/cloud_init/{}".format(base_folder["folder"], base_folder["pkg-dir"],
153 vdu["cloud-init-file"])
154 with self.fs.file_open(cloud_init_file, "r") as ci_file:
155 cloud_init_content = ci_file.read()
tierno59d22d22018-09-25 18:10:19 +0200156 vdu.pop("cloud-init-file", None)
tierno8a518872018-12-21 13:42:14 +0000157 elif vdu.get("cloud-init"):
gcalvino35be9152018-12-20 09:33:12 +0100158 cloud_init_content = vdu["cloud-init"]
tierno8a518872018-12-21 13:42:14 +0000159 else:
160 continue
161
162 env = Environment()
163 ast = env.parse(cloud_init_content)
164 mandatory_vars = meta.find_undeclared_variables(ast)
165 if mandatory_vars:
166 for var in mandatory_vars:
167 if not additionalParams or var not in additionalParams.keys():
168 raise LcmException("Variable '{}' defined at vnfd[id={}]:vdu[id={}]:cloud-init/cloud-init-"
169 "file, must be provided in the instantiation parameters inside the "
170 "'additionalParamsForVnf' block".format(var, vnfd["id"], vdu["id"]))
171 template = Template(cloud_init_content)
tierno2b611dd2019-01-11 10:30:57 +0000172 cloud_init_content = template.render(additionalParams or {})
gcalvino35be9152018-12-20 09:33:12 +0100173 vdu["cloud-init"] = cloud_init_content
tierno8a518872018-12-21 13:42:14 +0000174
tierno59d22d22018-09-25 18:10:19 +0200175 return vnfd_RO
176 except FsException as e:
tierno8a518872018-12-21 13:42:14 +0000177 raise LcmException("Error reading vnfd[id={}]:vdu[id={}]:cloud-init-file={}: {}".
tiernoda964822019-01-14 15:53:47 +0000178 format(vnfd["id"], vdu["id"], cloud_init_file, e))
tierno8a518872018-12-21 13:42:14 +0000179 except (TemplateError, TemplateNotFound, TemplateSyntaxError) as e:
180 raise LcmException("Error parsing Jinja2 to cloud-init content at vnfd[id={}]:vdu[id={}]: {}".
181 format(vnfd["id"], vdu["id"], e))
tierno59d22d22018-09-25 18:10:19 +0200182
183 def n2vc_callback(self, model_name, application_name, status, message, n2vc_info, task=None):
184 """
185 Callback both for charm status change and task completion
186 :param model_name: Charm model name
187 :param application_name: Charm application name
188 :param status: Can be
189 - blocked: The unit needs manual intervention
190 - maintenance: The unit is actively deploying/configuring
191 - waiting: The unit is waiting for another charm to be ready
192 - active: The unit is deployed, configured, and ready
193 - error: The charm has failed and needs attention.
194 - terminated: The charm has been destroyed
195 - removing,
196 - removed
197 :param message: detailed message error
tierno27246d82018-09-27 15:59:09 +0200198 :param n2vc_info: dictionary with information shared with instantiate task. It contains:
tierno59d22d22018-09-25 18:10:19 +0200199 nsr_id:
200 nslcmop_id:
201 lcmOperationType: currently "instantiate"
202 deployed: dictionary with {<application>: {operational-status: <status>, detailed-status: <text>}}
203 db_update: dictionary to be filled with the changes to be wrote to database with format key.key.key: value
204 n2vc_event: event used to notify instantiation task that some change has been produced
205 :param task: None for charm status change, or task for completion task callback
206 :return:
207 """
208 try:
209 nsr_id = n2vc_info["nsr_id"]
210 deployed = n2vc_info["deployed"]
211 db_nsr_update = n2vc_info["db_update"]
212 nslcmop_id = n2vc_info["nslcmop_id"]
213 ns_operation = n2vc_info["lcmOperationType"]
214 n2vc_event = n2vc_info["n2vc_event"]
215 logging_text = "Task ns={} {}={} [n2vc_callback] application={}".format(nsr_id, ns_operation, nslcmop_id,
216 application_name)
tiernoe4f7e6c2018-11-27 14:55:30 +0000217 for vca_index, vca_deployed in enumerate(deployed):
218 if not vca_deployed:
219 continue
220 if model_name == vca_deployed["model"] and application_name == vca_deployed["application"]:
221 break
222 else:
tierno9597c292019-02-06 09:21:27 +0000223 self.logger.error(logging_text + " Not present at nsr._admin.deployed.VCA. Received model_name={}".
224 format(model_name))
tierno59d22d22018-09-25 18:10:19 +0200225 return
tierno59d22d22018-09-25 18:10:19 +0200226 if task:
227 if task.cancelled():
228 self.logger.debug(logging_text + " task Cancelled")
229 vca_deployed['operational-status'] = "error"
tiernoe4f7e6c2018-11-27 14:55:30 +0000230 db_nsr_update["_admin.deployed.VCA.{}.operational-status".format(vca_index)] = "error"
tierno59d22d22018-09-25 18:10:19 +0200231 vca_deployed['detailed-status'] = "Task Cancelled"
tiernoe4f7e6c2018-11-27 14:55:30 +0000232 db_nsr_update["_admin.deployed.VCA.{}.detailed-status".format(vca_index)] = "Task Cancelled"
tierno59d22d22018-09-25 18:10:19 +0200233
234 elif task.done():
235 exc = task.exception()
236 if exc:
237 self.logger.error(logging_text + " task Exception={}".format(exc))
238 vca_deployed['operational-status'] = "error"
tiernoe4f7e6c2018-11-27 14:55:30 +0000239 db_nsr_update["_admin.deployed.VCA.{}.operational-status".format(vca_index)] = "error"
tierno59d22d22018-09-25 18:10:19 +0200240 vca_deployed['detailed-status'] = str(exc)
tiernoe4f7e6c2018-11-27 14:55:30 +0000241 db_nsr_update["_admin.deployed.VCA.{}.detailed-status".format(vca_index)] = str(exc)
tierno59d22d22018-09-25 18:10:19 +0200242 else:
243 self.logger.debug(logging_text + " task Done")
244 # task is Done, but callback is still ongoing. So ignore
245 return
246 elif status:
tierno27246d82018-09-27 15:59:09 +0200247 self.logger.debug(logging_text + " Enter status={} message={}".format(status, message))
tierno59d22d22018-09-25 18:10:19 +0200248 if vca_deployed['operational-status'] == status:
249 return # same status, ignore
250 vca_deployed['operational-status'] = status
tiernoe4f7e6c2018-11-27 14:55:30 +0000251 db_nsr_update["_admin.deployed.VCA.{}.operational-status".format(vca_index)] = status
tierno59d22d22018-09-25 18:10:19 +0200252 vca_deployed['detailed-status'] = str(message)
tiernoe4f7e6c2018-11-27 14:55:30 +0000253 db_nsr_update["_admin.deployed.VCA.{}.detailed-status".format(vca_index)] = str(message)
tierno59d22d22018-09-25 18:10:19 +0200254 else:
255 self.logger.critical(logging_text + " Enter with bad parameters", exc_info=True)
256 return
257 # wake up instantiate task
258 n2vc_event.set()
259 except Exception as e:
260 self.logger.critical(logging_text + " Exception {}".format(e), exc_info=True)
261
tierno27246d82018-09-27 15:59:09 +0200262 def ns_params_2_RO(self, ns_params, nsd, vnfd_dict, n2vc_key_list):
tierno59d22d22018-09-25 18:10:19 +0200263 """
tierno27246d82018-09-27 15:59:09 +0200264 Creates a RO ns descriptor from OSM ns_instantiate params
tierno59d22d22018-09-25 18:10:19 +0200265 :param ns_params: OSM instantiate params
266 :return: The RO ns descriptor
267 """
268 vim_2_RO = {}
tiernob7f3f0d2019-03-20 17:17:21 +0000269 wim_2_RO = {}
tierno27246d82018-09-27 15:59:09 +0200270 # TODO feature 1417: Check that no instantiation is set over PDU
271 # check if PDU forces a concrete vim-network-id and add it
272 # check if PDU contains a SDN-assist info (dpid, switch, port) and pass it to RO
tierno59d22d22018-09-25 18:10:19 +0200273
274 def vim_account_2_RO(vim_account):
275 if vim_account in vim_2_RO:
276 return vim_2_RO[vim_account]
277
278 db_vim = self.db.get_one("vim_accounts", {"_id": vim_account})
279 if db_vim["_admin"]["operationalState"] != "ENABLED":
280 raise LcmException("VIM={} is not available. operationalState={}".format(
281 vim_account, db_vim["_admin"]["operationalState"]))
282 RO_vim_id = db_vim["_admin"]["deployed"]["RO"]
283 vim_2_RO[vim_account] = RO_vim_id
284 return RO_vim_id
285
tiernob7f3f0d2019-03-20 17:17:21 +0000286 def wim_account_2_RO(wim_account):
287 if isinstance(wim_account, str):
288 if wim_account in wim_2_RO:
289 return wim_2_RO[wim_account]
290
291 db_wim = self.db.get_one("wim_accounts", {"_id": wim_account})
292 if db_wim["_admin"]["operationalState"] != "ENABLED":
293 raise LcmException("WIM={} is not available. operationalState={}".format(
294 wim_account, db_wim["_admin"]["operationalState"]))
295 RO_wim_id = db_wim["_admin"]["deployed"]["RO-account"]
296 wim_2_RO[wim_account] = RO_wim_id
297 return RO_wim_id
298 else:
299 return wim_account
300
tierno59d22d22018-09-25 18:10:19 +0200301 def ip_profile_2_RO(ip_profile):
302 RO_ip_profile = deepcopy((ip_profile))
303 if "dns-server" in RO_ip_profile:
304 if isinstance(RO_ip_profile["dns-server"], list):
305 RO_ip_profile["dns-address"] = []
306 for ds in RO_ip_profile.pop("dns-server"):
307 RO_ip_profile["dns-address"].append(ds['address'])
308 else:
309 RO_ip_profile["dns-address"] = RO_ip_profile.pop("dns-server")
310 if RO_ip_profile.get("ip-version") == "ipv4":
311 RO_ip_profile["ip-version"] = "IPv4"
312 if RO_ip_profile.get("ip-version") == "ipv6":
313 RO_ip_profile["ip-version"] = "IPv6"
314 if "dhcp-params" in RO_ip_profile:
315 RO_ip_profile["dhcp"] = RO_ip_profile.pop("dhcp-params")
316 return RO_ip_profile
317
318 if not ns_params:
319 return None
320 RO_ns_params = {
321 # "name": ns_params["nsName"],
322 # "description": ns_params.get("nsDescription"),
323 "datacenter": vim_account_2_RO(ns_params["vimAccountId"]),
tiernob7f3f0d2019-03-20 17:17:21 +0000324 "wim_account": wim_account_2_RO(ns_params.get("wimAccountId")),
tierno59d22d22018-09-25 18:10:19 +0200325 # "scenario": ns_params["nsdId"],
tierno59d22d22018-09-25 18:10:19 +0200326 }
tiernoe64f7fb2019-09-11 08:55:52 +0000327 n2vc_key_list = n2vc_key_list or []
328 for vnfd_ref, vnfd in vnfd_dict.items():
329 vdu_needed_access = []
330 mgmt_cp = None
331 if vnfd.get("vnf-configuration"):
tierno6cf25f52019-09-12 09:33:40 +0000332 ssh_required = deep_get(vnfd, ("vnf-configuration", "config-access", "ssh-access", "required"))
tiernoe64f7fb2019-09-11 08:55:52 +0000333 if ssh_required and vnfd.get("mgmt-interface"):
334 if vnfd["mgmt-interface"].get("vdu-id"):
335 vdu_needed_access.append(vnfd["mgmt-interface"]["vdu-id"])
336 elif vnfd["mgmt-interface"].get("cp"):
337 mgmt_cp = vnfd["mgmt-interface"]["cp"]
tierno27246d82018-09-27 15:59:09 +0200338
tiernoe64f7fb2019-09-11 08:55:52 +0000339 for vdu in vnfd.get("vdu", ()):
340 if vdu.get("vdu-configuration"):
tierno6cf25f52019-09-12 09:33:40 +0000341 ssh_required = deep_get(vdu, ("vdu-configuration", "config-access", "ssh-access", "required"))
tiernoe64f7fb2019-09-11 08:55:52 +0000342 if ssh_required:
tierno27246d82018-09-27 15:59:09 +0200343 vdu_needed_access.append(vdu["id"])
tiernoe64f7fb2019-09-11 08:55:52 +0000344 elif mgmt_cp:
345 for vdu_interface in vdu.get("interface"):
346 if vdu_interface.get("external-connection-point-ref") and \
347 vdu_interface["external-connection-point-ref"] == mgmt_cp:
348 vdu_needed_access.append(vdu["id"])
349 mgmt_cp = None
350 break
tierno27246d82018-09-27 15:59:09 +0200351
tiernoe64f7fb2019-09-11 08:55:52 +0000352 if vdu_needed_access:
353 for vnf_member in nsd.get("constituent-vnfd"):
354 if vnf_member["vnfd-id-ref"] != vnfd_ref:
355 continue
356 for vdu in vdu_needed_access:
357 populate_dict(RO_ns_params,
358 ("vnfs", vnf_member["member-vnf-index"], "vdus", vdu, "mgmt_keys"),
359 n2vc_key_list)
tierno27246d82018-09-27 15:59:09 +0200360
tierno25ec7732018-10-24 18:47:11 +0200361 if ns_params.get("vduImage"):
362 RO_ns_params["vduImage"] = ns_params["vduImage"]
363
tiernoc255a822018-10-31 09:41:53 +0100364 if ns_params.get("ssh_keys"):
365 RO_ns_params["cloud-config"] = {"key-pairs": ns_params["ssh_keys"]}
tierno27246d82018-09-27 15:59:09 +0200366 for vnf_params in get_iterable(ns_params, "vnf"):
367 for constituent_vnfd in nsd["constituent-vnfd"]:
368 if constituent_vnfd["member-vnf-index"] == vnf_params["member-vnf-index"]:
369 vnf_descriptor = vnfd_dict[constituent_vnfd["vnfd-id-ref"]]
370 break
371 else:
372 raise LcmException("Invalid instantiate parameter vnf:member-vnf-index={} is not present at nsd:"
373 "constituent-vnfd".format(vnf_params["member-vnf-index"]))
374 if vnf_params.get("vimAccountId"):
375 populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "datacenter"),
376 vim_account_2_RO(vnf_params["vimAccountId"]))
tierno59d22d22018-09-25 18:10:19 +0200377
tierno27246d82018-09-27 15:59:09 +0200378 for vdu_params in get_iterable(vnf_params, "vdu"):
379 # TODO feature 1417: check that this VDU exist and it is not a PDU
380 if vdu_params.get("volume"):
381 for volume_params in vdu_params["volume"]:
382 if volume_params.get("vim-volume-id"):
383 populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "vdus",
384 vdu_params["id"], "devices", volume_params["name"], "vim_id"),
385 volume_params["vim-volume-id"])
386 if vdu_params.get("interface"):
387 for interface_params in vdu_params["interface"]:
388 if interface_params.get("ip-address"):
389 populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "vdus",
390 vdu_params["id"], "interfaces", interface_params["name"],
391 "ip_address"),
392 interface_params["ip-address"])
393 if interface_params.get("mac-address"):
394 populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "vdus",
395 vdu_params["id"], "interfaces", interface_params["name"],
396 "mac_address"),
397 interface_params["mac-address"])
398 if interface_params.get("floating-ip-required"):
399 populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "vdus",
400 vdu_params["id"], "interfaces", interface_params["name"],
401 "floating-ip"),
402 interface_params["floating-ip-required"])
403
404 for internal_vld_params in get_iterable(vnf_params, "internal-vld"):
405 if internal_vld_params.get("vim-network-name"):
406 populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "networks",
407 internal_vld_params["name"], "vim-network-name"),
408 internal_vld_params["vim-network-name"])
gcalvino0d7ac8d2018-12-17 16:24:08 +0100409 if internal_vld_params.get("vim-network-id"):
410 populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "networks",
411 internal_vld_params["name"], "vim-network-id"),
412 internal_vld_params["vim-network-id"])
tierno27246d82018-09-27 15:59:09 +0200413 if internal_vld_params.get("ip-profile"):
414 populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "networks",
415 internal_vld_params["name"], "ip-profile"),
416 ip_profile_2_RO(internal_vld_params["ip-profile"]))
417
418 for icp_params in get_iterable(internal_vld_params, "internal-connection-point"):
419 # look for interface
420 iface_found = False
421 for vdu_descriptor in vnf_descriptor["vdu"]:
422 for vdu_interface in vdu_descriptor["interface"]:
423 if vdu_interface.get("internal-connection-point-ref") == icp_params["id-ref"]:
424 if icp_params.get("ip-address"):
425 populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "vdus",
426 vdu_descriptor["id"], "interfaces",
427 vdu_interface["name"], "ip_address"),
428 icp_params["ip-address"])
429
430 if icp_params.get("mac-address"):
431 populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "vdus",
432 vdu_descriptor["id"], "interfaces",
433 vdu_interface["name"], "mac_address"),
434 icp_params["mac-address"])
435 iface_found = True
tierno59d22d22018-09-25 18:10:19 +0200436 break
tierno27246d82018-09-27 15:59:09 +0200437 if iface_found:
438 break
439 else:
440 raise LcmException("Invalid instantiate parameter vnf:member-vnf-index[{}]:"
441 "internal-vld:id-ref={} is not present at vnfd:internal-"
442 "connection-point".format(vnf_params["member-vnf-index"],
443 icp_params["id-ref"]))
444
445 for vld_params in get_iterable(ns_params, "vld"):
446 if "ip-profile" in vld_params:
447 populate_dict(RO_ns_params, ("networks", vld_params["name"], "ip-profile"),
448 ip_profile_2_RO(vld_params["ip-profile"]))
tiernob7f3f0d2019-03-20 17:17:21 +0000449
450 if "wimAccountId" in vld_params and vld_params["wimAccountId"] is not None:
451 populate_dict(RO_ns_params, ("networks", vld_params["name"], "wim_account"),
452 wim_account_2_RO(vld_params["wimAccountId"])),
tierno27246d82018-09-27 15:59:09 +0200453 if vld_params.get("vim-network-name"):
454 RO_vld_sites = []
455 if isinstance(vld_params["vim-network-name"], dict):
456 for vim_account, vim_net in vld_params["vim-network-name"].items():
457 RO_vld_sites.append({
458 "netmap-use": vim_net,
459 "datacenter": vim_account_2_RO(vim_account)
460 })
461 else: # isinstance str
462 RO_vld_sites.append({"netmap-use": vld_params["vim-network-name"]})
463 if RO_vld_sites:
464 populate_dict(RO_ns_params, ("networks", vld_params["name"], "sites"), RO_vld_sites)
gcalvino0d7ac8d2018-12-17 16:24:08 +0100465 if vld_params.get("vim-network-id"):
466 RO_vld_sites = []
467 if isinstance(vld_params["vim-network-id"], dict):
468 for vim_account, vim_net in vld_params["vim-network-id"].items():
469 RO_vld_sites.append({
470 "netmap-use": vim_net,
471 "datacenter": vim_account_2_RO(vim_account)
472 })
473 else: # isinstance str
474 RO_vld_sites.append({"netmap-use": vld_params["vim-network-id"]})
475 if RO_vld_sites:
476 populate_dict(RO_ns_params, ("networks", vld_params["name"], "sites"), RO_vld_sites)
Felipe Vicens720b07a2019-01-31 02:32:09 +0100477 if vld_params.get("ns-net"):
478 if isinstance(vld_params["ns-net"], dict):
479 for vld_id, instance_scenario_id in vld_params["ns-net"].items():
480 RO_vld_ns_net = {"instance_scenario_id": instance_scenario_id, "osm_id": vld_id}
481 if RO_vld_ns_net:
Adam Israel78770df2019-09-04 11:55:55 -0400482 populate_dict(RO_ns_params, ("networks", vld_params["name"], "use-network"), RO_vld_ns_net)
tierno27246d82018-09-27 15:59:09 +0200483 if "vnfd-connection-point-ref" in vld_params:
484 for cp_params in vld_params["vnfd-connection-point-ref"]:
485 # look for interface
486 for constituent_vnfd in nsd["constituent-vnfd"]:
487 if constituent_vnfd["member-vnf-index"] == cp_params["member-vnf-index-ref"]:
488 vnf_descriptor = vnfd_dict[constituent_vnfd["vnfd-id-ref"]]
489 break
490 else:
491 raise LcmException(
492 "Invalid instantiate parameter vld:vnfd-connection-point-ref:member-vnf-index-ref={} "
493 "is not present at nsd:constituent-vnfd".format(cp_params["member-vnf-index-ref"]))
494 match_cp = False
495 for vdu_descriptor in vnf_descriptor["vdu"]:
496 for interface_descriptor in vdu_descriptor["interface"]:
497 if interface_descriptor.get("external-connection-point-ref") == \
498 cp_params["vnfd-connection-point-ref"]:
499 match_cp = True
tierno59d22d22018-09-25 18:10:19 +0200500 break
tierno27246d82018-09-27 15:59:09 +0200501 if match_cp:
502 break
503 else:
504 raise LcmException(
505 "Invalid instantiate parameter vld:vnfd-connection-point-ref:member-vnf-index-ref={}:"
506 "vnfd-connection-point-ref={} is not present at vnfd={}".format(
507 cp_params["member-vnf-index-ref"],
508 cp_params["vnfd-connection-point-ref"],
509 vnf_descriptor["id"]))
510 if cp_params.get("ip-address"):
511 populate_dict(RO_ns_params, ("vnfs", cp_params["member-vnf-index-ref"], "vdus",
512 vdu_descriptor["id"], "interfaces",
513 interface_descriptor["name"], "ip_address"),
514 cp_params["ip-address"])
515 if cp_params.get("mac-address"):
516 populate_dict(RO_ns_params, ("vnfs", cp_params["member-vnf-index-ref"], "vdus",
517 vdu_descriptor["id"], "interfaces",
518 interface_descriptor["name"], "mac_address"),
519 cp_params["mac-address"])
tierno59d22d22018-09-25 18:10:19 +0200520 return RO_ns_params
521
tierno27246d82018-09-27 15:59:09 +0200522 def scale_vnfr(self, db_vnfr, vdu_create=None, vdu_delete=None):
523 # make a copy to do not change
524 vdu_create = copy(vdu_create)
525 vdu_delete = copy(vdu_delete)
526
527 vdurs = db_vnfr.get("vdur")
528 if vdurs is None:
529 vdurs = []
530 vdu_index = len(vdurs)
531 while vdu_index:
532 vdu_index -= 1
533 vdur = vdurs[vdu_index]
534 if vdur.get("pdu-type"):
535 continue
536 vdu_id_ref = vdur["vdu-id-ref"]
537 if vdu_create and vdu_create.get(vdu_id_ref):
538 for index in range(0, vdu_create[vdu_id_ref]):
539 vdur = deepcopy(vdur)
540 vdur["_id"] = str(uuid4())
541 vdur["count-index"] += 1
542 vdurs.insert(vdu_index+1+index, vdur)
543 del vdu_create[vdu_id_ref]
544 if vdu_delete and vdu_delete.get(vdu_id_ref):
545 del vdurs[vdu_index]
546 vdu_delete[vdu_id_ref] -= 1
547 if not vdu_delete[vdu_id_ref]:
548 del vdu_delete[vdu_id_ref]
549 # check all operations are done
550 if vdu_create or vdu_delete:
551 raise LcmException("Error scaling OUT VNFR for {}. There is not any existing vnfr. Scaled to 0?".format(
552 vdu_create))
553 if vdu_delete:
554 raise LcmException("Error scaling IN VNFR for {}. There is not any existing vnfr. Scaled to 0?".format(
555 vdu_delete))
556
557 vnfr_update = {"vdur": vdurs}
558 db_vnfr["vdur"] = vdurs
559 self.update_db_2("vnfrs", db_vnfr["_id"], vnfr_update)
560
tiernof578e552018-11-08 19:07:20 +0100561 def ns_update_nsr(self, ns_update_nsr, db_nsr, nsr_desc_RO):
562 """
563 Updates database nsr with the RO info for the created vld
564 :param ns_update_nsr: dictionary to be filled with the updated info
565 :param db_nsr: content of db_nsr. This is also modified
566 :param nsr_desc_RO: nsr descriptor from RO
567 :return: Nothing, LcmException is raised on errors
568 """
569
570 for vld_index, vld in enumerate(get_iterable(db_nsr, "vld")):
571 for net_RO in get_iterable(nsr_desc_RO, "nets"):
572 if vld["id"] != net_RO.get("ns_net_osm_id"):
573 continue
574 vld["vim-id"] = net_RO.get("vim_net_id")
575 vld["name"] = net_RO.get("vim_name")
576 vld["status"] = net_RO.get("status")
577 vld["status-detailed"] = net_RO.get("error_msg")
578 ns_update_nsr["vld.{}".format(vld_index)] = vld
579 break
580 else:
581 raise LcmException("ns_update_nsr: Not found vld={} at RO info".format(vld["id"]))
582
tierno59d22d22018-09-25 18:10:19 +0200583 def ns_update_vnfr(self, db_vnfrs, nsr_desc_RO):
584 """
585 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 +0200586 :param db_vnfrs: dictionary with member-vnf-index: vnfr-content
587 :param nsr_desc_RO: nsr descriptor from RO
588 :return: Nothing, LcmException is raised on errors
tierno59d22d22018-09-25 18:10:19 +0200589 """
590 for vnf_index, db_vnfr in db_vnfrs.items():
591 for vnf_RO in nsr_desc_RO["vnfs"]:
tierno27246d82018-09-27 15:59:09 +0200592 if vnf_RO["member_vnf_index"] != vnf_index:
593 continue
594 vnfr_update = {}
tiernof578e552018-11-08 19:07:20 +0100595 if vnf_RO.get("ip_address"):
tierno1674de82019-04-09 13:03:14 +0000596 db_vnfr["ip-address"] = vnfr_update["ip-address"] = vnf_RO["ip_address"].split(";")[0]
tiernof578e552018-11-08 19:07:20 +0100597 elif not db_vnfr.get("ip-address"):
598 raise LcmExceptionNoMgmtIP("ns member_vnf_index '{}' has no IP address".format(vnf_index))
tierno59d22d22018-09-25 18:10:19 +0200599
tierno27246d82018-09-27 15:59:09 +0200600 for vdu_index, vdur in enumerate(get_iterable(db_vnfr, "vdur")):
601 vdur_RO_count_index = 0
602 if vdur.get("pdu-type"):
603 continue
604 for vdur_RO in get_iterable(vnf_RO, "vms"):
605 if vdur["vdu-id-ref"] != vdur_RO["vdu_osm_id"]:
606 continue
607 if vdur["count-index"] != vdur_RO_count_index:
608 vdur_RO_count_index += 1
609 continue
610 vdur["vim-id"] = vdur_RO.get("vim_vm_id")
tierno1674de82019-04-09 13:03:14 +0000611 if vdur_RO.get("ip_address"):
612 vdur["ip-address"] = vdur_RO["ip_address"].split(";")[0]
tierno274ed572019-04-04 13:33:27 +0000613 else:
614 vdur["ip-address"] = None
tierno27246d82018-09-27 15:59:09 +0200615 vdur["vdu-id-ref"] = vdur_RO.get("vdu_osm_id")
616 vdur["name"] = vdur_RO.get("vim_name")
617 vdur["status"] = vdur_RO.get("status")
618 vdur["status-detailed"] = vdur_RO.get("error_msg")
619 for ifacer in get_iterable(vdur, "interfaces"):
620 for interface_RO in get_iterable(vdur_RO, "interfaces"):
621 if ifacer["name"] == interface_RO.get("internal_name"):
622 ifacer["ip-address"] = interface_RO.get("ip_address")
623 ifacer["mac-address"] = interface_RO.get("mac_address")
624 break
625 else:
626 raise LcmException("ns_update_vnfr: Not found member_vnf_index={} vdur={} interface={} "
tierno15b1cf12019-08-29 13:21:40 +0000627 "from VIM info".format(vnf_index, vdur["vdu-id-ref"],
628 ifacer["name"]))
tierno27246d82018-09-27 15:59:09 +0200629 vnfr_update["vdur.{}".format(vdu_index)] = vdur
630 break
631 else:
tierno15b1cf12019-08-29 13:21:40 +0000632 raise LcmException("ns_update_vnfr: Not found member_vnf_index={} vdur={} count_index={} from "
633 "VIM info".format(vnf_index, vdur["vdu-id-ref"], vdur["count-index"]))
tiernof578e552018-11-08 19:07:20 +0100634
635 for vld_index, vld in enumerate(get_iterable(db_vnfr, "vld")):
636 for net_RO in get_iterable(nsr_desc_RO, "nets"):
637 if vld["id"] != net_RO.get("vnf_net_osm_id"):
638 continue
639 vld["vim-id"] = net_RO.get("vim_net_id")
640 vld["name"] = net_RO.get("vim_name")
641 vld["status"] = net_RO.get("status")
642 vld["status-detailed"] = net_RO.get("error_msg")
643 vnfr_update["vld.{}".format(vld_index)] = vld
644 break
645 else:
tierno15b1cf12019-08-29 13:21:40 +0000646 raise LcmException("ns_update_vnfr: Not found member_vnf_index={} vld={} from VIM info".format(
tiernof578e552018-11-08 19:07:20 +0100647 vnf_index, vld["id"]))
648
tierno27246d82018-09-27 15:59:09 +0200649 self.update_db_2("vnfrs", db_vnfr["_id"], vnfr_update)
650 break
tierno59d22d22018-09-25 18:10:19 +0200651
652 else:
tierno15b1cf12019-08-29 13:21:40 +0000653 raise LcmException("ns_update_vnfr: Not found member_vnf_index={} from VIM info".format(vnf_index))
tierno59d22d22018-09-25 18:10:19 +0200654
tierno59d22d22018-09-25 18:10:19 +0200655 async def instantiate(self, nsr_id, nslcmop_id):
kuused124bfe2019-06-18 12:09:24 +0200656
657 # Try to lock HA task here
658 task_is_locked_by_me = self.lcm_tasks.lock_HA('ns', 'nslcmops', nslcmop_id)
659 if not task_is_locked_by_me:
660 return
661
tierno59d22d22018-09-25 18:10:19 +0200662 logging_text = "Task ns={} instantiate={} ".format(nsr_id, nslcmop_id)
663 self.logger.debug(logging_text + "Enter")
664 # get all needed from database
tierno63de62e2018-10-31 16:38:52 +0100665 start_deploy = time()
tierno59d22d22018-09-25 18:10:19 +0200666 db_nsr = None
667 db_nslcmop = None
tierno47e86b52018-10-10 14:05:55 +0200668 db_nsr_update = {"_admin.nslcmop": nslcmop_id}
tierno59d22d22018-09-25 18:10:19 +0200669 db_nslcmop_update = {}
670 nslcmop_operation_state = None
671 db_vnfrs = {}
672 RO_descriptor_number = 0 # number of descriptors created at RO
gcalvino35be9152018-12-20 09:33:12 +0100673 vnf_index_2_RO_id = {} # map between vnfd/nsd id to the id used at RO
tierno59d22d22018-09-25 18:10:19 +0200674 n2vc_info = {}
tierno98ad6ea2019-05-30 17:16:28 +0000675 n2vc_key_list = [] # list of public keys to be injected as authorized to VMs
tierno59d22d22018-09-25 18:10:19 +0200676 exc = None
677 try:
kuused124bfe2019-06-18 12:09:24 +0200678 # wait for any previous tasks in process
Adam Israel78770df2019-09-04 11:55:55 -0400679 step = "Waiting for previous tasks"
kuused124bfe2019-06-18 12:09:24 +0200680 await self.lcm_tasks.waitfor_related_HA('ns', 'nslcmops', nslcmop_id)
681
tierno59d22d22018-09-25 18:10:19 +0200682 step = "Getting nslcmop={} from db".format(nslcmop_id)
683 db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
684 step = "Getting nsr={} from db".format(nsr_id)
685 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
tiernof578e552018-11-08 19:07:20 +0100686 ns_params = db_nslcmop.get("operationParams")
tierno59d22d22018-09-25 18:10:19 +0200687 nsd = db_nsr["nsd"]
688 nsr_name = db_nsr["name"] # TODO short-name??
tierno47e86b52018-10-10 14:05:55 +0200689
tierno27246d82018-09-27 15:59:09 +0200690 step = "Getting vnfrs from db"
691 db_vnfrs_list = self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id})
692 db_vnfds_ref = {}
693 db_vnfds = {}
tierno98ad6ea2019-05-30 17:16:28 +0000694 db_vnfds_index = {}
tierno27246d82018-09-27 15:59:09 +0200695 for vnfr in db_vnfrs_list:
696 db_vnfrs[vnfr["member-vnf-index-ref"]] = vnfr
697 vnfd_id = vnfr["vnfd-id"]
698 vnfd_ref = vnfr["vnfd-ref"]
699 if vnfd_id not in db_vnfds:
700 step = "Getting vnfd={} id='{}' from db".format(vnfd_id, vnfd_ref)
701 vnfd = self.db.get_one("vnfds", {"_id": vnfd_id})
702 db_vnfds_ref[vnfd_ref] = vnfd
703 db_vnfds[vnfd_id] = vnfd
tierno98ad6ea2019-05-30 17:16:28 +0000704 db_vnfds_index[vnfr["member-vnf-index-ref"]] = db_vnfds[vnfd_id]
tierno27246d82018-09-27 15:59:09 +0200705
tiernoe4f7e6c2018-11-27 14:55:30 +0000706 # Get or generates the _admin.deployed,VCA list
707 vca_deployed_list = None
tierno9597c292019-02-06 09:21:27 +0000708 vca_model_name = None
tiernoe4f7e6c2018-11-27 14:55:30 +0000709 if db_nsr["_admin"].get("deployed"):
710 vca_deployed_list = db_nsr["_admin"]["deployed"].get("VCA")
tierno9597c292019-02-06 09:21:27 +0000711 vca_model_name = db_nsr["_admin"]["deployed"].get("VCA-model-name")
tiernoe4f7e6c2018-11-27 14:55:30 +0000712 if vca_deployed_list is None:
713 vca_deployed_list = []
714 db_nsr_update["_admin.deployed.VCA"] = vca_deployed_list
tierno98ad6ea2019-05-30 17:16:28 +0000715 populate_dict(db_nsr, ("_admin", "deployed", "VCA"), vca_deployed_list)
tiernoe4f7e6c2018-11-27 14:55:30 +0000716 elif isinstance(vca_deployed_list, dict):
717 # maintain backward compatibility. Change a dict to list at database
718 vca_deployed_list = list(vca_deployed_list.values())
719 db_nsr_update["_admin.deployed.VCA"] = vca_deployed_list
tierno98ad6ea2019-05-30 17:16:28 +0000720 populate_dict(db_nsr, ("_admin", "deployed", "VCA"), vca_deployed_list)
tiernoe4f7e6c2018-11-27 14:55:30 +0000721
tierno59d22d22018-09-25 18:10:19 +0200722 db_nsr_update["detailed-status"] = "creating"
723 db_nsr_update["operational-status"] = "init"
tierno6cf25f52019-09-12 09:33:40 +0000724 if not isinstance(deep_get(db_nsr, ("_admin", "deployed", "RO", "vnfd")), list):
tiernoa009e552019-01-30 16:45:44 +0000725 populate_dict(db_nsr, ("_admin", "deployed", "RO", "vnfd"), [])
726 db_nsr_update["_admin.deployed.RO.vnfd"] = []
tierno59d22d22018-09-25 18:10:19 +0200727
tiernobaa51102018-12-14 13:16:18 +0000728 # set state to INSTANTIATED. When instantiated NBI will not delete directly
729 db_nsr_update["_admin.nsState"] = "INSTANTIATED"
730 self.update_db_2("nsrs", nsr_id, db_nsr_update)
731
tierno98ad6ea2019-05-30 17:16:28 +0000732 # Deploy charms
733 # The parameters we'll need to deploy a charm
734 number_to_configure = 0
735
736 def deploy_charm(vnf_index, vdu_id, vdu_name, vdu_count_index, charm_params, n2vc_info, native_charm=False):
737 """An inner function to deploy the charm from either ns, vnf or vdu
738 For ns both vnf_index and vdu_id are None.
739 For vnf only vdu_id is None
740 For vdu both vnf_index and vdu_id contain a value
741 """
742 # if not charm_params.get("rw_mgmt_ip") and vnf_index: # if NS skip mgmt_ip checking
743 # raise LcmException("ns/vnfd/vdu has not management ip address to configure it")
744
745 machine_spec = {}
746 if native_charm:
Adam Israel78770df2019-09-04 11:55:55 -0400747 machine_spec["username"] = charm_params.get("username")
tierno240fcac2019-06-03 13:39:39 +0000748 machine_spec["hostname"] = charm_params.get("rw_mgmt_ip")
tierno98ad6ea2019-05-30 17:16:28 +0000749
750 # Note: The charm needs to exist on disk at the location
751 # specified by charm_path.
752 descriptor = vnfd if vnf_index else nsd
753 base_folder = descriptor["_admin"]["storage"]
754 storage_params = self.fs.get_params()
755 charm_path = "{}{}/{}/charms/{}".format(
756 storage_params["path"],
757 base_folder["folder"],
758 base_folder["pkg-dir"],
759 proxy_charm
760 )
761
762 # ns_name will be ignored in the current version of N2VC
763 # but will be implemented for the next point release.
764 model_name = nsr_id
765 vdu_id_text = (str(vdu_id) if vdu_id else "") + "-"
766 vnf_index_text = (str(vnf_index) if vnf_index else "") + "-"
767 application_name = self.n2vc.FormatApplicationName(nsr_name, vnf_index_text, vdu_id_text)
768
769 vca_index = len(vca_deployed_list)
770 # trunk name and add two char index at the end to ensure that it is unique. It is assumed no more than
771 # 26*26 charm in the same NS
772 application_name = application_name[0:48]
773 application_name += chr(97 + vca_index // 26) + chr(97 + vca_index % 26)
774 vca_deployed_ = {
775 "member-vnf-index": vnf_index,
776 "vdu_id": vdu_id,
777 "model": model_name,
778 "application": application_name,
779 "operational-status": "init",
780 "detailed-status": "",
781 "step": "initial-deploy",
782 "vnfd_id": vnfd_id,
783 "vdu_name": vdu_name,
784 "vdu_count_index": vdu_count_index,
785 }
786 vca_deployed_list.append(vca_deployed_)
787 db_nsr_update["_admin.deployed.VCA.{}".format(vca_index)] = vca_deployed_
788 self.update_db_2("nsrs", nsr_id, db_nsr_update)
789
790 self.logger.debug("Task create_ns={} Passing artifacts path '{}' for {}".format(nsr_id, charm_path,
791 proxy_charm))
792 if not n2vc_info:
793 n2vc_info["nsr_id"] = nsr_id
794 n2vc_info["nslcmop_id"] = nslcmop_id
795 n2vc_info["n2vc_event"] = asyncio.Event(loop=self.loop)
796 n2vc_info["lcmOperationType"] = "instantiate"
797 n2vc_info["deployed"] = vca_deployed_list
798 n2vc_info["db_update"] = db_nsr_update
799 task = asyncio.ensure_future(
800 self.n2vc.DeployCharms(
801 model_name, # The network service name
802 application_name, # The application name
803 descriptor, # The vnf/nsd descriptor
804 charm_path, # Path to charm
805 charm_params, # Runtime params, like mgmt ip
806 machine_spec, # for native charms only
807 self.n2vc_callback, # Callback for status changes
808 n2vc_info, # Callback parameter
809 None, # Callback parameter (task)
810 )
811 )
812 task.add_done_callback(functools.partial(self.n2vc_callback, model_name, application_name, None, None,
813 n2vc_info))
814 self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "create_charm:" + application_name, task)
815
816 step = "Looking for needed vnfd to configure with proxy charm"
817 self.logger.debug(logging_text + step)
818
819 for c_vnf in get_iterable(nsd, "constituent-vnfd"):
820 vnfd_id = c_vnf["vnfd-id-ref"]
821 vnf_index = str(c_vnf["member-vnf-index"])
822 vnfd = db_vnfds_ref[vnfd_id]
823
824 # Get additional parameters
825 vnfr_params = {}
826 if db_vnfrs[vnf_index].get("additionalParamsForVnf"):
827 vnfr_params = db_vnfrs[vnf_index]["additionalParamsForVnf"].copy()
828 for k, v in vnfr_params.items():
829 if isinstance(v, str) and v.startswith("!!yaml "):
830 vnfr_params[k] = yaml.safe_load(v[7:])
831
832 step = "deploying proxy charms for configuration"
833 # Check if this VNF has a charm configuration
834 vnf_config = vnfd.get("vnf-configuration")
835 if vnf_config and vnf_config.get("juju"):
836 proxy_charm = vnf_config["juju"]["charm"]
837 if vnf_config["juju"].get("proxy") is False:
838 # native_charm, will be deployed after VM. Skip
839 proxy_charm = None
840
841 if proxy_charm:
842 if not vca_model_name:
843 step = "creating VCA model name '{}'".format(nsr_id)
844 self.logger.debug(logging_text + step)
845 await self.n2vc.CreateNetworkService(nsr_id)
846 vca_model_name = nsr_id
847 db_nsr_update["_admin.deployed.VCA-model-name"] = nsr_id
848 self.update_db_2("nsrs", nsr_id, db_nsr_update)
849 step = "deploying proxy charm to configure vnf {}".format(vnf_index)
850 vnfr_params["rw_mgmt_ip"] = db_vnfrs[vnf_index]["ip-address"]
851 charm_params = {
852 "user_values": vnfr_params,
853 "rw_mgmt_ip": db_vnfrs[vnf_index]["ip-address"],
854 "initial-config-primitive": {} # vnf_config.get('initial-config-primitive') or {}
855 }
856
857 # Login to the VCA. If there are multiple calls to login(),
858 # subsequent calls will be a nop and return immediately.
859 await self.n2vc.login()
860
861 deploy_charm(vnf_index, None, None, None, charm_params, n2vc_info)
862 number_to_configure += 1
863
864 # Deploy charms for each VDU that supports one.
865 for vdu_index, vdu in enumerate(get_iterable(vnfd, 'vdu')):
866 vdu_config = vdu.get('vdu-configuration')
867 proxy_charm = None
868
869 if vdu_config and vdu_config.get("juju"):
870 proxy_charm = vdu_config["juju"]["charm"]
tierno240fcac2019-06-03 13:39:39 +0000871 if vdu_config["juju"].get("proxy") is False:
tierno98ad6ea2019-05-30 17:16:28 +0000872 # native_charm, will be deployed after VM. Skip
873 proxy_charm = None
tierno98ad6ea2019-05-30 17:16:28 +0000874 if proxy_charm:
875 if not vca_model_name:
876 step = "creating VCA model name"
877 await self.n2vc.CreateNetworkService(nsr_id)
878 vca_model_name = nsr_id
879 db_nsr_update["_admin.deployed.VCA-model-name"] = nsr_id
880 self.update_db_2("nsrs", nsr_id, db_nsr_update)
881 step = "deploying proxy charm to configure member_vnf_index={} vdu={}".format(vnf_index,
882 vdu["id"])
883 await self.n2vc.login()
884 vdur = db_vnfrs[vnf_index]["vdur"][vdu_index]
885 # TODO for the moment only first vdu_id contains a charm deployed
886 if vdur["vdu-id-ref"] != vdu["id"]:
887 raise LcmException("Mismatch vdur {}, vdu {} at index {} for member_vnf_index={}"
888 .format(vdur["vdu-id-ref"], vdu["id"], vdu_index, vnf_index))
889 vnfr_params["rw_mgmt_ip"] = vdur["ip-address"]
890 charm_params = {
891 "user_values": vnfr_params,
892 "rw_mgmt_ip": vdur["ip-address"],
893 "initial-config-primitive": {} # vdu_config.get('initial-config-primitive') or {}
894 }
895 deploy_charm(vnf_index, vdu["id"], vdur.get("name"), vdur["count-index"],
896 charm_params, n2vc_info)
897 number_to_configure += 1
898
899 # Check if this NS has a charm configuration
900
901 ns_config = nsd.get("ns-configuration")
902 if ns_config and ns_config.get("juju"):
903 proxy_charm = ns_config["juju"]["charm"]
tierno240fcac2019-06-03 13:39:39 +0000904 if ns_config["juju"].get("proxy") is False:
tierno98ad6ea2019-05-30 17:16:28 +0000905 # native_charm, will be deployed after VM. Skip
906 proxy_charm = None
tierno98ad6ea2019-05-30 17:16:28 +0000907 if proxy_charm:
908 step = "deploying proxy charm to configure ns"
909 # TODO is NS magmt IP address needed?
910
911 # Get additional parameters
912 additional_params = {}
913 if db_nsr.get("additionalParamsForNs"):
914 additional_params = db_nsr["additionalParamsForNs"].copy()
915 for k, v in additional_params.items():
916 if isinstance(v, str) and v.startswith("!!yaml "):
917 additional_params[k] = yaml.safe_load(v[7:])
918
919 # additional_params["rw_mgmt_ip"] = db_nsr["ip-address"]
920 charm_params = {
921 "user_values": additional_params,
922 # "rw_mgmt_ip": db_nsr["ip-address"],
923 "initial-config-primitive": {} # ns_config.get('initial-config-primitive') or {}
924 }
925
926 # Login to the VCA. If there are multiple calls to login(),
927 # subsequent calls will be a nop and return immediately.
928 await self.n2vc.login()
929 deploy_charm(None, None, None, None, charm_params, n2vc_info)
930 number_to_configure += 1
931
932 db_nsr_update["operational-status"] = "running"
933
934 # Wait until all charms has reached blocked or active status
935 step = "waiting proxy charms to be ready"
936 if number_to_configure:
937 # wait until all charms are configured.
938 # steps are:
939 # initial-deploy
940 # get-ssh-public-key
941 # generate-ssh-key
942 # retry-get-ssh-public-key
943 # ssh-public-key-obtained
944 while time() <= start_deploy + self.total_deploy_timeout:
945 if db_nsr_update:
946 self.update_db_2("nsrs", nsr_id, db_nsr_update)
947 if db_nslcmop_update:
948 self.update_db_2("nslcmops", nslcmop_id, db_nslcmop_update)
949
950 all_active = True
951 for vca_index, vca_deployed in enumerate(vca_deployed_list):
952 database_entry = "_admin.deployed.VCA.{}.".format(vca_index)
953 if vca_deployed["step"] == "initial-deploy":
954 if vca_deployed["operational-status"] in ("active", "blocked"):
955 step = "execute charm primitive get-ssh-public-key for member_vnf_index={} vdu_id={}" \
956 .format(vca_deployed["member-vnf-index"],
957 vca_deployed["vdu_id"])
958 self.logger.debug(logging_text + step)
tierno46b75f12019-07-04 17:17:06 +0000959 try:
960 primitive_id = await self.n2vc.ExecutePrimitive(
961 vca_deployed["model"],
962 vca_deployed["application"],
963 "get-ssh-public-key",
964 None,
965 )
966 vca_deployed["step"] = db_nsr_update[database_entry + "step"] = "get-ssh-public-key"
967 vca_deployed["primitive_id"] = db_nsr_update[database_entry + "primitive_id"] =\
968 primitive_id
969 db_nsr_update[database_entry + "operational-status"] =\
970 vca_deployed["operational-status"]
971 except PrimitiveDoesNotExist:
972 ssh_public_key = None
973 vca_deployed["step"] = db_nsr_update[database_entry + "step"] =\
974 "ssh-public-key-obtained"
975 vca_deployed["ssh-public-key"] = db_nsr_update[database_entry + "ssh-public-key"] =\
976 ssh_public_key
977 step = "charm ssh-public-key for member_vnf_index={} vdu_id={} not needed".format(
978 vca_deployed["member-vnf-index"], vca_deployed["vdu_id"])
979 self.logger.debug(logging_text + step)
980
tierno98ad6ea2019-05-30 17:16:28 +0000981 elif vca_deployed["step"] in ("get-ssh-public-key", "retry-get-ssh-public-key"):
982 primitive_id = vca_deployed["primitive_id"]
983 primitive_status = await self.n2vc.GetPrimitiveStatus(vca_deployed["model"],
984 primitive_id)
985 if primitive_status in ("completed", "failed"):
986 primitive_result = await self.n2vc.GetPrimitiveOutput(vca_deployed["model"],
987 primitive_id)
988 vca_deployed["primitive_id"] = db_nsr_update[database_entry + "primitive_id"] = None
989 if primitive_status == "completed" and isinstance(primitive_result, dict) and \
990 primitive_result.get("pubkey"):
991 ssh_public_key = primitive_result.get("pubkey")
992 vca_deployed["step"] = db_nsr_update[database_entry + "step"] =\
993 "ssh-public-key-obtained"
994 vca_deployed["ssh-public-key"] = db_nsr_update[database_entry + "ssh-public-key"] =\
995 ssh_public_key
996 n2vc_key_list.append(ssh_public_key)
997 step = "charm ssh-public-key for member_vnf_index={} vdu_id={} is '{}'".format(
tierno240fcac2019-06-03 13:39:39 +0000998 vca_deployed["member-vnf-index"], vca_deployed["vdu_id"], ssh_public_key)
tierno98ad6ea2019-05-30 17:16:28 +0000999 self.logger.debug(logging_text + step)
1000 else: # primitive_status == "failed":
1001 if vca_deployed["step"] == "get-ssh-public-key":
1002 step = "execute charm primitive generate-ssh-public-key for member_vnf_index="\
1003 "{} vdu_id={}".format(vca_deployed["member-vnf-index"],
1004 vca_deployed["vdu_id"])
1005 self.logger.debug(logging_text + step)
1006 vca_deployed["step"] = db_nsr_update[database_entry + "step"] =\
1007 "generate-ssh-key"
1008 primitive_id = await self.n2vc.ExecutePrimitive(
1009 vca_deployed["model"],
1010 vca_deployed["application"],
1011 "generate-ssh-key",
1012 None,
1013 )
1014 vca_deployed["primitive_id"] = db_nsr_update[database_entry + "primitive_id"] =\
1015 primitive_id
1016 else: # failed for second time
1017 raise LcmException(
1018 "error executing primitive get-ssh-public-key: {}".format(primitive_result))
1019
1020 elif vca_deployed["step"] == "generate-ssh-key":
1021 primitive_id = vca_deployed["primitive_id"]
1022 primitive_status = await self.n2vc.GetPrimitiveStatus(vca_deployed["model"],
1023 primitive_id)
1024 if primitive_status in ("completed", "failed"):
1025 primitive_result = await self.n2vc.GetPrimitiveOutput(vca_deployed["model"],
1026 primitive_id)
1027 vca_deployed["primitive_id"] = db_nsr_update[
1028 database_entry + "primitive_id"] = None
1029 if primitive_status == "completed":
1030 step = "execute primitive get-ssh-public-key again for member_vnf_index={} "\
1031 "vdu_id={}".format(vca_deployed["member-vnf-index"],
1032 vca_deployed["vdu_id"])
1033 self.logger.debug(logging_text + step)
1034 vca_deployed["step"] = db_nsr_update[database_entry + "step"] = \
1035 "retry-get-ssh-public-key"
1036 primitive_id = await self.n2vc.ExecutePrimitive(
1037 vca_deployed["model"],
1038 vca_deployed["application"],
1039 "get-ssh-public-key",
1040 None,
1041 )
1042 vca_deployed["primitive_id"] = db_nsr_update[database_entry + "primitive_id"] =\
1043 primitive_id
1044
1045 else: # primitive_status == "failed":
1046 raise LcmException("error executing primitive generate-ssh-key: {}"
1047 .format(primitive_result))
1048
1049 if vca_deployed["step"] != "ssh-public-key-obtained":
1050 all_active = False
1051
1052 if all_active:
1053 break
1054 await asyncio.sleep(5)
1055 else: # total_deploy_timeout
1056 raise LcmException("Timeout waiting charm to be initialized for member_vnf_index={} vdu_id={}"
1057 .format(vca_deployed["member-vnf-index"], vca_deployed["vdu_id"]))
1058
1059 # deploy RO
tierno59d22d22018-09-25 18:10:19 +02001060 # get vnfds, instantiate at RO
gcalvino35be9152018-12-20 09:33:12 +01001061 for c_vnf in nsd.get("constituent-vnfd", ()):
1062 member_vnf_index = c_vnf["member-vnf-index"]
tiernoa009e552019-01-30 16:45:44 +00001063 vnfd = db_vnfds_ref[c_vnf['vnfd-id-ref']]
tierno27246d82018-09-27 15:59:09 +02001064 vnfd_ref = vnfd["id"]
tierno98ad6ea2019-05-30 17:16:28 +00001065 step = db_nsr_update["detailed-status"] = "Creating vnfd='{}' member_vnf_index='{}' at RO".format(
tiernoa009e552019-01-30 16:45:44 +00001066 vnfd_ref, member_vnf_index)
tierno59d22d22018-09-25 18:10:19 +02001067 # self.logger.debug(logging_text + step)
gcalvino35be9152018-12-20 09:33:12 +01001068 vnfd_id_RO = "{}.{}.{}".format(nsr_id, RO_descriptor_number, member_vnf_index[:23])
1069 vnf_index_2_RO_id[member_vnf_index] = vnfd_id_RO
tierno59d22d22018-09-25 18:10:19 +02001070 RO_descriptor_number += 1
1071
tiernoa009e552019-01-30 16:45:44 +00001072 # look position at deployed.RO.vnfd if not present it will be appended at the end
1073 for index, vnf_deployed in enumerate(db_nsr["_admin"]["deployed"]["RO"]["vnfd"]):
1074 if vnf_deployed["member-vnf-index"] == member_vnf_index:
1075 break
1076 else:
1077 index = len(db_nsr["_admin"]["deployed"]["RO"]["vnfd"])
1078 db_nsr["_admin"]["deployed"]["RO"]["vnfd"].append(None)
1079
tierno59d22d22018-09-25 18:10:19 +02001080 # look if present
tiernoa009e552019-01-30 16:45:44 +00001081 RO_update = {"member-vnf-index": member_vnf_index}
tierno77677d92019-08-22 13:46:35 +00001082 vnfd_list = await self.RO.get_list("vnfd", filter_by={"osm_id": vnfd_id_RO})
tierno59d22d22018-09-25 18:10:19 +02001083 if vnfd_list:
tiernoa009e552019-01-30 16:45:44 +00001084 RO_update["id"] = vnfd_list[0]["uuid"]
tierno98ad6ea2019-05-30 17:16:28 +00001085 self.logger.debug(logging_text + "vnfd='{}' member_vnf_index='{}' exists at RO. Using RO_id={}".
tiernoa009e552019-01-30 16:45:44 +00001086 format(vnfd_ref, member_vnf_index, vnfd_list[0]["uuid"]))
tierno59d22d22018-09-25 18:10:19 +02001087 else:
gcalvino35be9152018-12-20 09:33:12 +01001088 vnfd_RO = self.vnfd2RO(vnfd, vnfd_id_RO, db_vnfrs[c_vnf["member-vnf-index"]].
1089 get("additionalParamsForVnf"), nsr_id)
tierno77677d92019-08-22 13:46:35 +00001090 desc = await self.RO.create("vnfd", descriptor=vnfd_RO)
tiernoa009e552019-01-30 16:45:44 +00001091 RO_update["id"] = desc["uuid"]
tierno98ad6ea2019-05-30 17:16:28 +00001092 self.logger.debug(logging_text + "vnfd='{}' member_vnf_index='{}' created at RO. RO_id={}".format(
tiernoa009e552019-01-30 16:45:44 +00001093 vnfd_ref, member_vnf_index, desc["uuid"]))
1094 db_nsr_update["_admin.deployed.RO.vnfd.{}".format(index)] = RO_update
1095 db_nsr["_admin"]["deployed"]["RO"]["vnfd"][index] = RO_update
tierno59d22d22018-09-25 18:10:19 +02001096 self.update_db_2("nsrs", nsr_id, db_nsr_update)
1097
1098 # create nsd at RO
tierno27246d82018-09-27 15:59:09 +02001099 nsd_ref = nsd["id"]
1100 step = db_nsr_update["detailed-status"] = "Creating nsd={} at RO".format(nsd_ref)
tierno59d22d22018-09-25 18:10:19 +02001101 # self.logger.debug(logging_text + step)
1102
tierno27246d82018-09-27 15:59:09 +02001103 RO_osm_nsd_id = "{}.{}.{}".format(nsr_id, RO_descriptor_number, nsd_ref[:23])
tierno59d22d22018-09-25 18:10:19 +02001104 RO_descriptor_number += 1
tierno77677d92019-08-22 13:46:35 +00001105 nsd_list = await self.RO.get_list("nsd", filter_by={"osm_id": RO_osm_nsd_id})
tierno59d22d22018-09-25 18:10:19 +02001106 if nsd_list:
1107 db_nsr_update["_admin.deployed.RO.nsd_id"] = RO_nsd_uuid = nsd_list[0]["uuid"]
1108 self.logger.debug(logging_text + "nsd={} exists at RO. Using RO_id={}".format(
tierno27246d82018-09-27 15:59:09 +02001109 nsd_ref, RO_nsd_uuid))
tierno59d22d22018-09-25 18:10:19 +02001110 else:
1111 nsd_RO = deepcopy(nsd)
1112 nsd_RO["id"] = RO_osm_nsd_id
1113 nsd_RO.pop("_id", None)
1114 nsd_RO.pop("_admin", None)
gcalvinoea0cc0a2018-11-06 13:20:29 +01001115 for c_vnf in nsd_RO.get("constituent-vnfd", ()):
gcalvino35be9152018-12-20 09:33:12 +01001116 member_vnf_index = c_vnf["member-vnf-index"]
1117 c_vnf["vnfd-id-ref"] = vnf_index_2_RO_id[member_vnf_index]
1118 for c_vld in nsd_RO.get("vld", ()):
1119 for cp in c_vld.get("vnfd-connection-point-ref", ()):
1120 member_vnf_index = cp["member-vnf-index-ref"]
1121 cp["vnfd-id-ref"] = vnf_index_2_RO_id[member_vnf_index]
1122
tierno77677d92019-08-22 13:46:35 +00001123 desc = await self.RO.create("nsd", descriptor=nsd_RO)
tierno59d22d22018-09-25 18:10:19 +02001124 db_nsr_update["_admin.nsState"] = "INSTANTIATED"
1125 db_nsr_update["_admin.deployed.RO.nsd_id"] = RO_nsd_uuid = desc["uuid"]
tierno27246d82018-09-27 15:59:09 +02001126 self.logger.debug(logging_text + "nsd={} created at RO. RO_id={}".format(nsd_ref, RO_nsd_uuid))
tierno59d22d22018-09-25 18:10:19 +02001127 self.update_db_2("nsrs", nsr_id, db_nsr_update)
1128
1129 # Crate ns at RO
1130 # if present use it unless in error status
tierno6cf25f52019-09-12 09:33:40 +00001131 RO_nsr_id = deep_get(db_nsr, ("_admin", "deployed", "RO", "nsr_id"))
tierno59d22d22018-09-25 18:10:19 +02001132 if RO_nsr_id:
1133 try:
1134 step = db_nsr_update["detailed-status"] = "Looking for existing ns at RO"
1135 # self.logger.debug(logging_text + step + " RO_ns_id={}".format(RO_nsr_id))
tierno77677d92019-08-22 13:46:35 +00001136 desc = await self.RO.show("ns", RO_nsr_id)
tierno59d22d22018-09-25 18:10:19 +02001137 except ROclient.ROClientException as e:
1138 if e.http_code != HTTPStatus.NOT_FOUND:
1139 raise
1140 RO_nsr_id = db_nsr_update["_admin.deployed.RO.nsr_id"] = None
1141 if RO_nsr_id:
tierno77677d92019-08-22 13:46:35 +00001142 ns_status, ns_status_info = self.RO.check_ns_status(desc)
tierno59d22d22018-09-25 18:10:19 +02001143 db_nsr_update["_admin.deployed.RO.nsr_status"] = ns_status
1144 if ns_status == "ERROR":
1145 step = db_nsr_update["detailed-status"] = "Deleting ns at RO. RO_ns_id={}".format(RO_nsr_id)
1146 self.logger.debug(logging_text + step)
tierno77677d92019-08-22 13:46:35 +00001147 await self.RO.delete("ns", RO_nsr_id)
tierno59d22d22018-09-25 18:10:19 +02001148 RO_nsr_id = db_nsr_update["_admin.deployed.RO.nsr_id"] = None
1149 if not RO_nsr_id:
1150 step = db_nsr_update["detailed-status"] = "Checking dependencies"
1151 # self.logger.debug(logging_text + step)
1152
1153 # check if VIM is creating and wait look if previous tasks in process
1154 task_name, task_dependency = self.lcm_tasks.lookfor_related("vim_account", ns_params["vimAccountId"])
1155 if task_dependency:
1156 step = "Waiting for related tasks to be completed: {}".format(task_name)
1157 self.logger.debug(logging_text + step)
1158 await asyncio.wait(task_dependency, timeout=3600)
1159 if ns_params.get("vnf"):
1160 for vnf in ns_params["vnf"]:
1161 if "vimAccountId" in vnf:
1162 task_name, task_dependency = self.lcm_tasks.lookfor_related("vim_account",
1163 vnf["vimAccountId"])
1164 if task_dependency:
1165 step = "Waiting for related tasks to be completed: {}".format(task_name)
1166 self.logger.debug(logging_text + step)
1167 await asyncio.wait(task_dependency, timeout=3600)
1168
1169 step = db_nsr_update["detailed-status"] = "Checking instantiation parameters"
tierno25ec7732018-10-24 18:47:11 +02001170
tierno27246d82018-09-27 15:59:09 +02001171 # feature 1429. Add n2vc public key to needed VMs
tierno25ec7732018-10-24 18:47:11 +02001172 n2vc_key = await self.n2vc.GetPublicKey()
tierno98ad6ea2019-05-30 17:16:28 +00001173 n2vc_key_list.append(n2vc_key)
1174 RO_ns_params = self.ns_params_2_RO(ns_params, nsd, db_vnfds_ref, n2vc_key_list)
tierno25ec7732018-10-24 18:47:11 +02001175
tierno15b1cf12019-08-29 13:21:40 +00001176 step = db_nsr_update["detailed-status"] = "Deploying ns at VIM"
tierno77677d92019-08-22 13:46:35 +00001177 desc = await self.RO.create("ns", descriptor=RO_ns_params,
1178 name=db_nsr["name"],
1179 scenario=RO_nsd_uuid)
tierno59d22d22018-09-25 18:10:19 +02001180 RO_nsr_id = db_nsr_update["_admin.deployed.RO.nsr_id"] = desc["uuid"]
1181 db_nsr_update["_admin.nsState"] = "INSTANTIATED"
1182 db_nsr_update["_admin.deployed.RO.nsr_status"] = "BUILD"
1183 self.logger.debug(logging_text + "ns created at RO. RO_id={}".format(desc["uuid"]))
1184 self.update_db_2("nsrs", nsr_id, db_nsr_update)
1185
tierno59d22d22018-09-25 18:10:19 +02001186 # wait until NS is ready
tierno15b1cf12019-08-29 13:21:40 +00001187 step = ns_status_detailed = detailed_status = "Waiting VIM to deploy ns. RO_id={}".format(RO_nsr_id)
tierno59d22d22018-09-25 18:10:19 +02001188 detailed_status_old = None
1189 self.logger.debug(logging_text + step)
1190
tierno63de62e2018-10-31 16:38:52 +01001191 while time() <= start_deploy + self.total_deploy_timeout:
tierno77677d92019-08-22 13:46:35 +00001192 desc = await self.RO.show("ns", RO_nsr_id)
1193 ns_status, ns_status_info = self.RO.check_ns_status(desc)
tierno12650ec2019-03-01 09:26:30 +00001194 db_nsr_update["_admin.deployed.RO.nsr_status"] = ns_status
tierno59d22d22018-09-25 18:10:19 +02001195 if ns_status == "ERROR":
1196 raise ROclient.ROClientException(ns_status_info)
1197 elif ns_status == "BUILD":
1198 detailed_status = ns_status_detailed + "; {}".format(ns_status_info)
1199 elif ns_status == "ACTIVE":
tiernof578e552018-11-08 19:07:20 +01001200 step = detailed_status = "Waiting for management IP address reported by the VIM. Updating VNFRs"
tierno59d22d22018-09-25 18:10:19 +02001201 try:
tiernof578e552018-11-08 19:07:20 +01001202 self.ns_update_vnfr(db_vnfrs, desc)
tierno59d22d22018-09-25 18:10:19 +02001203 break
tiernof578e552018-11-08 19:07:20 +01001204 except LcmExceptionNoMgmtIP:
1205 pass
tierno59d22d22018-09-25 18:10:19 +02001206 else:
1207 assert False, "ROclient.check_ns_status returns unknown {}".format(ns_status)
1208 if detailed_status != detailed_status_old:
1209 detailed_status_old = db_nsr_update["detailed-status"] = detailed_status
1210 self.update_db_2("nsrs", nsr_id, db_nsr_update)
1211 await asyncio.sleep(5, loop=self.loop)
tierno63de62e2018-10-31 16:38:52 +01001212 else: # total_deploy_timeout
tierno59d22d22018-09-25 18:10:19 +02001213 raise ROclient.ROClientException("Timeout waiting ns to be ready")
1214
tiernof578e552018-11-08 19:07:20 +01001215 step = "Updating NSR"
1216 self.ns_update_nsr(db_nsr_update, db_nsr, desc)
tierno59d22d22018-09-25 18:10:19 +02001217
tierno98ad6ea2019-05-30 17:16:28 +00001218 db_nsr_update["operational-status"] = "running"
tierno59d22d22018-09-25 18:10:19 +02001219 db_nsr["detailed-status"] = "Configuring vnfr"
1220 self.update_db_2("nsrs", nsr_id, db_nsr_update)
1221
tierno98ad6ea2019-05-30 17:16:28 +00001222 # Configure proxy charms once VMs are up
1223 for vca_index, vca_deployed in enumerate(vca_deployed_list):
1224 vnf_index = vca_deployed.get("member-vnf-index")
1225 vdu_id = vca_deployed.get("vdu_id")
1226 vdu_name = None
1227 vdu_count_index = None
tierno59d22d22018-09-25 18:10:19 +02001228
tierno98ad6ea2019-05-30 17:16:28 +00001229 step = "executing proxy charm initial primitives for member_vnf_index={} vdu_id={}".format(vnf_index,
1230 vdu_id)
1231 add_params = {}
1232 initial_config_primitive_list = []
1233 if vnf_index:
1234 if db_vnfrs[vnf_index].get("additionalParamsForVnf"):
1235 add_params = db_vnfrs[vnf_index]["additionalParamsForVnf"].copy()
1236 vnfd = db_vnfds_index[vnf_index]
tiernod5102d22019-05-29 11:34:57 +00001237
tierno98ad6ea2019-05-30 17:16:28 +00001238 if vdu_id:
1239 for vdu_index, vdu in enumerate(get_iterable(vnfd, 'vdu')):
1240 if vdu["id"] == vdu_id:
1241 initial_config_primitive_list = vdu['vdu-configuration'].get(
tierno14cca5a2019-06-13 16:05:28 +00001242 'initial-config-primitive', [])
1243 break
tierno98ad6ea2019-05-30 17:16:28 +00001244 else:
1245 raise LcmException("Not found vdu_id={} at vnfd:vdu".format(vdu_id))
1246 vdur = db_vnfrs[vnf_index]["vdur"][vdu_index]
1247 # TODO for the moment only first vdu_id contains a charm deployed
1248 if vdur["vdu-id-ref"] != vdu["id"]:
1249 raise LcmException("Mismatch vdur {}, vdu {} at index {} for vnf {}"
1250 .format(vdur["vdu-id-ref"], vdu["id"], vdu_index, vnf_index))
1251 add_params["rw_mgmt_ip"] = vdur["ip-address"]
1252 else:
1253 add_params["rw_mgmt_ip"] = db_vnfrs[vnf_index]["ip-address"]
tierno14cca5a2019-06-13 16:05:28 +00001254 initial_config_primitive_list = vnfd["vnf-configuration"].get('initial-config-primitive', [])
tierno98ad6ea2019-05-30 17:16:28 +00001255 else:
1256 if db_nsr.get("additionalParamsForNs"):
1257 add_params = db_nsr["additionalParamsForNs"].copy()
1258 for k, v in add_params.items():
1259 if isinstance(v, str) and v.startswith("!!yaml "):
1260 add_params[k] = yaml.safe_load(v[7:])
1261 add_params["rw_mgmt_ip"] = None
tierno29cfa602019-06-19 08:46:07 +00001262 initial_config_primitive_list = nsd["ns-configuration"].get('initial-config-primitive', [])
tiernod5102d22019-05-29 11:34:57 +00001263
tierno98ad6ea2019-05-30 17:16:28 +00001264 # add primitive verify-ssh-credentials to the list after config only when is a vnf or vdu charm
1265 initial_config_primitive_list = initial_config_primitive_list.copy()
tierno46b75f12019-07-04 17:17:06 +00001266 if initial_config_primitive_list and vnf_index and vca_deployed.get("ssh-public-key"):
tierno2fc7ce52019-06-11 22:50:01 +00001267 initial_config_primitive_list.insert(1, {"name": "verify-ssh-credentials", "parameter": []})
tierno59d22d22018-09-25 18:10:19 +02001268
tierno98ad6ea2019-05-30 17:16:28 +00001269 for initial_config_primitive in initial_config_primitive_list:
tierno14cca5a2019-06-13 16:05:28 +00001270 primitive_params_ = self._map_primitive_params(initial_config_primitive, {}, add_params)
1271 self.logger.debug(logging_text + step + " primitive '{}' params '{}'"
1272 .format(initial_config_primitive["name"], primitive_params_))
tierno98ad6ea2019-05-30 17:16:28 +00001273 primitive_result, primitive_detail = await self._ns_execute_primitive(
1274 db_nsr["_admin"]["deployed"], vnf_index, vdu_id, vdu_name, vdu_count_index,
1275 initial_config_primitive["name"],
tierno14cca5a2019-06-13 16:05:28 +00001276 primitive_params_,
tierno2fc7ce52019-06-11 22:50:01 +00001277 retries=10 if initial_config_primitive["name"] == "verify-ssh-credentials" else 0,
1278 retries_interval=30)
tierno98ad6ea2019-05-30 17:16:28 +00001279 if primitive_result != "COMPLETED":
tierno14cca5a2019-06-13 16:05:28 +00001280 raise LcmException("charm error executing primitive {} for member_vnf_index={} vdu_id={}: '{}'"
tierno98ad6ea2019-05-30 17:16:28 +00001281 .format(initial_config_primitive["name"], vca_deployed["member-vnf-index"],
1282 vca_deployed["vdu_id"], primitive_detail))
tierno59d22d22018-09-25 18:10:19 +02001283
tierno98ad6ea2019-05-30 17:16:28 +00001284 # Deploy native charms
1285 step = "Looking for needed vnfd to configure with native charm"
tierno59d22d22018-09-25 18:10:19 +02001286 self.logger.debug(logging_text + step)
1287
tiernoe4f7e6c2018-11-27 14:55:30 +00001288 for c_vnf in get_iterable(nsd, "constituent-vnfd"):
tierno59d22d22018-09-25 18:10:19 +02001289 vnfd_id = c_vnf["vnfd-id-ref"]
1290 vnf_index = str(c_vnf["member-vnf-index"])
tierno27246d82018-09-27 15:59:09 +02001291 vnfd = db_vnfds_ref[vnfd_id]
tierno59d22d22018-09-25 18:10:19 +02001292
tierno8a518872018-12-21 13:42:14 +00001293 # Get additional parameters
1294 vnfr_params = {}
1295 if db_vnfrs[vnf_index].get("additionalParamsForVnf"):
1296 vnfr_params = db_vnfrs[vnf_index]["additionalParamsForVnf"].copy()
1297 for k, v in vnfr_params.items():
1298 if isinstance(v, str) and v.startswith("!!yaml "):
1299 vnfr_params[k] = yaml.safe_load(v[7:])
1300
tierno59d22d22018-09-25 18:10:19 +02001301 # Check if this VNF has a charm configuration
1302 vnf_config = vnfd.get("vnf-configuration")
tierno59d22d22018-09-25 18:10:19 +02001303 if vnf_config and vnf_config.get("juju"):
tiernod5102d22019-05-29 11:34:57 +00001304 native_charm = vnf_config["juju"].get("proxy") is False
Adam Israel78770df2019-09-04 11:55:55 -04001305 proxy_charm = vnf_config["juju"]["charm"]
1306 if native_charm and proxy_charm:
tierno9597c292019-02-06 09:21:27 +00001307 if not vca_model_name:
1308 step = "creating VCA model name '{}'".format(nsr_id)
1309 self.logger.debug(logging_text + step)
1310 await self.n2vc.CreateNetworkService(nsr_id)
1311 vca_model_name = nsr_id
1312 db_nsr_update["_admin.deployed.VCA-model-name"] = nsr_id
1313 self.update_db_2("nsrs", nsr_id, db_nsr_update)
tierno98ad6ea2019-05-30 17:16:28 +00001314 step = "deploying native charm for vnf_member_index={}".format(vnf_index)
Adam Israel78770df2019-09-04 11:55:55 -04001315 self.logger.debug(logging_text + step)
1316
tierno8a518872018-12-21 13:42:14 +00001317 vnfr_params["rw_mgmt_ip"] = db_vnfrs[vnf_index]["ip-address"]
1318 charm_params = {
1319 "user_values": vnfr_params,
1320 "rw_mgmt_ip": db_vnfrs[vnf_index]["ip-address"],
tiernod5102d22019-05-29 11:34:57 +00001321 "initial-config-primitive": vnf_config.get('initial-config-primitive') or {},
tierno8a518872018-12-21 13:42:14 +00001322 }
tierno59d22d22018-09-25 18:10:19 +02001323
tiernod5102d22019-05-29 11:34:57 +00001324 # get username
1325 # TODO remove this when changes on IM regarding config-access:ssh-access:default-user were
1326 # merged. Meanwhile let's get username from initial-config-primitive
1327 if vnf_config.get("initial-config-primitive"):
1328 for param in vnf_config["initial-config-primitive"][0].get("parameter", ()):
1329 if param["name"] == "ssh-username":
1330 charm_params["username"] = param["value"]
1331 if vnf_config.get("config-access") and vnf_config["config-access"].get("ssh-access"):
1332 if vnf_config["config-access"]["ssh-access"].get("required"):
1333 charm_params["username"] = vnf_config["config-access"]["ssh-access"].get("default-user")
1334
tierno59d22d22018-09-25 18:10:19 +02001335 # Login to the VCA. If there are multiple calls to login(),
1336 # subsequent calls will be a nop and return immediately.
tierno59d22d22018-09-25 18:10:19 +02001337 await self.n2vc.login()
tierno9597c292019-02-06 09:21:27 +00001338
tiernod5102d22019-05-29 11:34:57 +00001339 deploy_charm(vnf_index, None, None, None, charm_params, n2vc_info, native_charm)
tierno59d22d22018-09-25 18:10:19 +02001340 number_to_configure += 1
1341
1342 # Deploy charms for each VDU that supports one.
tiernoe4f7e6c2018-11-27 14:55:30 +00001343 for vdu_index, vdu in enumerate(get_iterable(vnfd, 'vdu')):
tierno59d22d22018-09-25 18:10:19 +02001344 vdu_config = vdu.get('vdu-configuration')
tierno98ad6ea2019-05-30 17:16:28 +00001345 native_charm = False
tierno59d22d22018-09-25 18:10:19 +02001346
1347 if vdu_config and vdu_config.get("juju"):
tiernod5102d22019-05-29 11:34:57 +00001348 native_charm = vdu_config["juju"].get("proxy") is False
Adam Israel78770df2019-09-04 11:55:55 -04001349 proxy_charm = vdu_config["juju"]["charm"]
1350 if native_charm and proxy_charm:
tierno9597c292019-02-06 09:21:27 +00001351 if not vca_model_name:
1352 step = "creating VCA model name"
1353 await self.n2vc.CreateNetworkService(nsr_id)
1354 vca_model_name = nsr_id
1355 db_nsr_update["_admin.deployed.VCA-model-name"] = nsr_id
1356 self.update_db_2("nsrs", nsr_id, db_nsr_update)
tierno98ad6ea2019-05-30 17:16:28 +00001357 step = "deploying native charm for vnf_member_index={} vdu_id={}".format(vnf_index,
1358 vdu["id"])
Adam Israel78770df2019-09-04 11:55:55 -04001359
1360 self.logger.debug(logging_text + step)
1361
tiernoe4f7e6c2018-11-27 14:55:30 +00001362 vdur = db_vnfrs[vnf_index]["vdur"][vdu_index]
Adam Israel78770df2019-09-04 11:55:55 -04001363
tiernoe4f7e6c2018-11-27 14:55:30 +00001364 # TODO for the moment only first vdu_id contains a charm deployed
1365 if vdur["vdu-id-ref"] != vdu["id"]:
1366 raise LcmException("Mismatch vdur {}, vdu {} at index {} for vnf {}"
1367 .format(vdur["vdu-id-ref"], vdu["id"], vdu_index, vnf_index))
tierno8a518872018-12-21 13:42:14 +00001368 vnfr_params["rw_mgmt_ip"] = vdur["ip-address"]
1369 charm_params = {
1370 "user_values": vnfr_params,
1371 "rw_mgmt_ip": vdur["ip-address"],
1372 "initial-config-primitive": vdu_config.get('initial-config-primitive') or {}
1373 }
tiernod5102d22019-05-29 11:34:57 +00001374
1375 # get username
1376 # TODO remove this when changes on IM regarding config-access:ssh-access:default-user were
1377 # merged. Meanwhile let's get username from initial-config-primitive
1378 if vdu_config.get("initial-config-primitive"):
1379 for param in vdu_config["initial-config-primitive"][0].get("parameter", ()):
1380 if param["name"] == "ssh-username":
1381 charm_params["username"] = param["value"]
1382 if vdu_config.get("config-access") and vdu_config["config-access"].get("ssh-access"):
1383 if vdu_config["config-access"]["ssh-access"].get("required"):
1384 charm_params["username"] = vdu_config["config-access"]["ssh-access"].get(
1385 "default-user")
1386
Adam Israel78770df2019-09-04 11:55:55 -04001387 await self.n2vc.login()
1388
tiernoe4f7e6c2018-11-27 14:55:30 +00001389 deploy_charm(vnf_index, vdu["id"], vdur.get("name"), vdur["count-index"],
tiernod5102d22019-05-29 11:34:57 +00001390 charm_params, n2vc_info, native_charm)
tierno59d22d22018-09-25 18:10:19 +02001391 number_to_configure += 1
tierno59d22d22018-09-25 18:10:19 +02001392
tierno1b633412019-02-25 16:48:23 +00001393 # Check if this NS has a charm configuration
1394
1395 ns_config = nsd.get("ns-configuration")
1396 if ns_config and ns_config.get("juju"):
tiernod5102d22019-05-29 11:34:57 +00001397 native_charm = ns_config["juju"].get("proxy") is False
Adam Israel78770df2019-09-04 11:55:55 -04001398 proxy_charm = ns_config["juju"]["charm"]
1399 if native_charm and proxy_charm:
tierno98ad6ea2019-05-30 17:16:28 +00001400 step = "deploying native charm to configure ns"
tierno1b633412019-02-25 16:48:23 +00001401 # TODO is NS magmt IP address needed?
1402
1403 # Get additional parameters
1404 additional_params = {}
1405 if db_nsr.get("additionalParamsForNs"):
1406 additional_params = db_nsr["additionalParamsForNs"].copy()
1407 for k, v in additional_params.items():
1408 if isinstance(v, str) and v.startswith("!!yaml "):
1409 additional_params[k] = yaml.safe_load(v[7:])
1410
1411 # additional_params["rw_mgmt_ip"] = db_nsr["ip-address"]
1412 charm_params = {
1413 "user_values": additional_params,
tiernod5102d22019-05-29 11:34:57 +00001414 "rw_mgmt_ip": db_nsr.get("ip-address"),
tierno1b633412019-02-25 16:48:23 +00001415 "initial-config-primitive": ns_config.get('initial-config-primitive') or {}
1416 }
1417
tiernod5102d22019-05-29 11:34:57 +00001418 # get username
1419 # TODO remove this when changes on IM regarding config-access:ssh-access:default-user were
1420 # merged. Meanwhile let's get username from initial-config-primitive
1421 if ns_config.get("initial-config-primitive"):
1422 for param in ns_config["initial-config-primitive"][0].get("parameter", ()):
1423 if param["name"] == "ssh-username":
1424 charm_params["username"] = param["value"]
1425 if ns_config.get("config-access") and ns_config["config-access"].get("ssh-access"):
1426 if ns_config["config-access"]["ssh-access"].get("required"):
1427 charm_params["username"] = ns_config["config-access"]["ssh-access"].get("default-user")
1428
tierno1b633412019-02-25 16:48:23 +00001429 # Login to the VCA. If there are multiple calls to login(),
1430 # subsequent calls will be a nop and return immediately.
1431 await self.n2vc.login()
tiernod5102d22019-05-29 11:34:57 +00001432 deploy_charm(None, None, None, None, charm_params, n2vc_info, native_charm)
tierno1b633412019-02-25 16:48:23 +00001433 number_to_configure += 1
1434
tierno98ad6ea2019-05-30 17:16:28 +00001435 # waiting all charms are ok
tierno59d22d22018-09-25 18:10:19 +02001436 configuration_failed = False
1437 if number_to_configure:
tierno29cfa602019-06-19 08:46:07 +00001438 step = "Waiting all charms are active"
tierno59d22d22018-09-25 18:10:19 +02001439 old_status = "configuring: init: {}".format(number_to_configure)
1440 db_nsr_update["config-status"] = old_status
1441 db_nsr_update["detailed-status"] = old_status
1442 db_nslcmop_update["detailed-status"] = old_status
1443
1444 # wait until all are configured.
tierno63de62e2018-10-31 16:38:52 +01001445 while time() <= start_deploy + self.total_deploy_timeout:
tierno59d22d22018-09-25 18:10:19 +02001446 if db_nsr_update:
1447 self.update_db_2("nsrs", nsr_id, db_nsr_update)
1448 if db_nslcmop_update:
1449 self.update_db_2("nslcmops", nslcmop_id, db_nslcmop_update)
tierno8a518872018-12-21 13:42:14 +00001450 # TODO add a fake task that set n2vc_event after some time
tierno59d22d22018-09-25 18:10:19 +02001451 await n2vc_info["n2vc_event"].wait()
1452 n2vc_info["n2vc_event"].clear()
1453 all_active = True
1454 status_map = {}
1455 n2vc_error_text = [] # contain text error list. If empty no one is in error status
tierno63de62e2018-10-31 16:38:52 +01001456 now = time()
tiernoe4f7e6c2018-11-27 14:55:30 +00001457 for vca_deployed in vca_deployed_list:
1458 vca_status = vca_deployed["operational-status"]
tierno59d22d22018-09-25 18:10:19 +02001459 if vca_status not in status_map:
1460 # Initialize it
1461 status_map[vca_status] = 0
1462 status_map[vca_status] += 1
1463
tierno63de62e2018-10-31 16:38:52 +01001464 if vca_status == "active":
tiernoe4f7e6c2018-11-27 14:55:30 +00001465 vca_deployed.pop("time_first_error", None)
1466 vca_deployed.pop("status_first_error", None)
tierno63de62e2018-10-31 16:38:52 +01001467 continue
1468
1469 all_active = False
tierno59d22d22018-09-25 18:10:19 +02001470 if vca_status in ("error", "blocked"):
tiernoe4f7e6c2018-11-27 14:55:30 +00001471 vca_deployed["detailed-status-error"] = vca_deployed["detailed-status"]
tierno63de62e2018-10-31 16:38:52 +01001472 # if not first time in this status error
tiernoe4f7e6c2018-11-27 14:55:30 +00001473 if not vca_deployed.get("time_first_error"):
1474 vca_deployed["time_first_error"] = now
tierno63de62e2018-10-31 16:38:52 +01001475 continue
tiernoe4f7e6c2018-11-27 14:55:30 +00001476 if vca_deployed.get("time_first_error") and \
1477 now <= vca_deployed["time_first_error"] + self.timeout_vca_on_error:
tierno63de62e2018-10-31 16:38:52 +01001478 n2vc_error_text.append("member_vnf_index={} vdu_id={} {}: {}"
tiernoe4f7e6c2018-11-27 14:55:30 +00001479 .format(vca_deployed["member-vnf-index"],
1480 vca_deployed["vdu_id"], vca_status,
1481 vca_deployed["detailed-status-error"]))
tierno59d22d22018-09-25 18:10:19 +02001482
1483 if all_active:
1484 break
1485 elif n2vc_error_text:
1486 db_nsr_update["config-status"] = "failed"
1487 error_text = "fail configuring " + ";".join(n2vc_error_text)
1488 db_nsr_update["detailed-status"] = error_text
1489 db_nslcmop_update["operationState"] = nslcmop_operation_state = "FAILED_TEMP"
1490 db_nslcmop_update["detailed-status"] = error_text
1491 db_nslcmop_update["statusEnteredTime"] = time()
1492 configuration_failed = True
1493 break
1494 else:
1495 cs = "configuring: "
1496 separator = ""
1497 for status, num in status_map.items():
1498 cs += separator + "{}: {}".format(status, num)
1499 separator = ", "
1500 if old_status != cs:
1501 db_nsr_update["config-status"] = cs
1502 db_nsr_update["detailed-status"] = cs
1503 db_nslcmop_update["detailed-status"] = cs
1504 old_status = cs
tierno63de62e2018-10-31 16:38:52 +01001505 else: # total_deploy_timeout
1506 raise LcmException("Timeout waiting ns to be configured")
tierno59d22d22018-09-25 18:10:19 +02001507
1508 if not configuration_failed:
1509 # all is done
1510 db_nslcmop_update["operationState"] = nslcmop_operation_state = "COMPLETED"
1511 db_nslcmop_update["statusEnteredTime"] = time()
1512 db_nslcmop_update["detailed-status"] = "done"
1513 db_nsr_update["config-status"] = "configured"
1514 db_nsr_update["detailed-status"] = "done"
1515
tierno59d22d22018-09-25 18:10:19 +02001516 return
1517
1518 except (ROclient.ROClientException, DbException, LcmException) as e:
1519 self.logger.error(logging_text + "Exit Exception while '{}': {}".format(step, e))
1520 exc = e
1521 except asyncio.CancelledError:
1522 self.logger.error(logging_text + "Cancelled Exception while '{}'".format(step))
1523 exc = "Operation was cancelled"
1524 except Exception as e:
1525 exc = traceback.format_exc()
1526 self.logger.critical(logging_text + "Exit Exception {} while '{}': {}".format(type(e).__name__, step, e),
1527 exc_info=True)
1528 finally:
1529 if exc:
1530 if db_nsr:
1531 db_nsr_update["detailed-status"] = "ERROR {}: {}".format(step, exc)
1532 db_nsr_update["operational-status"] = "failed"
1533 if db_nslcmop:
1534 db_nslcmop_update["detailed-status"] = "FAILED {}: {}".format(step, exc)
1535 db_nslcmop_update["operationState"] = nslcmop_operation_state = "FAILED"
1536 db_nslcmop_update["statusEnteredTime"] = time()
tiernobaa51102018-12-14 13:16:18 +00001537 try:
1538 if db_nsr:
1539 db_nsr_update["_admin.nslcmop"] = None
1540 self.update_db_2("nsrs", nsr_id, db_nsr_update)
1541 if db_nslcmop_update:
1542 self.update_db_2("nslcmops", nslcmop_id, db_nslcmop_update)
1543 except DbException as e:
1544 self.logger.error(logging_text + "Cannot update database: {}".format(e))
tierno59d22d22018-09-25 18:10:19 +02001545 if nslcmop_operation_state:
1546 try:
1547 await self.msg.aiowrite("ns", "instantiated", {"nsr_id": nsr_id, "nslcmop_id": nslcmop_id,
tierno8a518872018-12-21 13:42:14 +00001548 "operationState": nslcmop_operation_state},
1549 loop=self.loop)
tierno59d22d22018-09-25 18:10:19 +02001550 except Exception as e:
1551 self.logger.error(logging_text + "kafka_write notification Exception {}".format(e))
1552
1553 self.logger.debug(logging_text + "Exit")
1554 self.lcm_tasks.remove("ns", nsr_id, nslcmop_id, "ns_instantiate")
1555
tiernobaa51102018-12-14 13:16:18 +00001556 async def _destroy_charm(self, model, application):
1557 """
1558 Order N2VC destroy a charm
1559 :param model:
1560 :param application:
1561 :return: True if charm does not exist. False if it exist
1562 """
1563 if not await self.n2vc.HasApplication(model, application):
1564 return True # Already removed
1565 await self.n2vc.RemoveCharms(model, application)
1566 return False
1567
1568 async def _wait_charm_destroyed(self, model, application, timeout):
1569 """
1570 Wait until charm does not exist
1571 :param model:
1572 :param application:
1573 :param timeout:
1574 :return: True if not exist, False if timeout
1575 """
1576 while True:
1577 if not await self.n2vc.HasApplication(model, application):
1578 return True
1579 if timeout < 0:
1580 return False
1581 await asyncio.sleep(10)
1582 timeout -= 10
1583
kuuse0ca67472019-05-13 15:59:27 +02001584 # Check if this VNFD has a configured terminate action
1585 def _has_terminate_config_primitive(self, vnfd):
1586 vnf_config = vnfd.get("vnf-configuration")
1587 if vnf_config and vnf_config.get("terminate-config-primitive"):
1588 return True
1589 else:
1590 return False
1591
tiernoc9556972019-07-05 15:25:25 +00001592 @staticmethod
1593 def _get_terminate_config_primitive_seq_list(vnfd):
1594 """ Get a numerically sorted list of the sequences for this VNFD's terminate action """
kuuse0ca67472019-05-13 15:59:27 +02001595 # No need to check for existing primitive twice, already done before
1596 vnf_config = vnfd.get("vnf-configuration")
1597 seq_list = vnf_config.get("terminate-config-primitive")
1598 # Get all 'seq' tags in seq_list, order sequences numerically, ascending.
1599 seq_list_sorted = sorted(seq_list, key=lambda x: int(x['seq']))
1600 return seq_list_sorted
1601
1602 @staticmethod
1603 def _create_nslcmop(nsr_id, operation, params):
1604 """
1605 Creates a ns-lcm-opp content to be stored at database.
1606 :param nsr_id: internal id of the instance
1607 :param operation: instantiate, terminate, scale, action, ...
1608 :param params: user parameters for the operation
1609 :return: dictionary following SOL005 format
1610 """
1611 # Raise exception if invalid arguments
1612 if not (nsr_id and operation and params):
1613 raise LcmException(
1614 "Parameters 'nsr_id', 'operation' and 'params' needed to create primitive not provided")
1615 now = time()
1616 _id = str(uuid4())
1617 nslcmop = {
1618 "id": _id,
1619 "_id": _id,
1620 # COMPLETED,PARTIALLY_COMPLETED,FAILED_TEMP,FAILED,ROLLING_BACK,ROLLED_BACK
1621 "operationState": "PROCESSING",
1622 "statusEnteredTime": now,
1623 "nsInstanceId": nsr_id,
1624 "lcmOperationType": operation,
1625 "startTime": now,
1626 "isAutomaticInvocation": False,
1627 "operationParams": params,
1628 "isCancelPending": False,
1629 "links": {
1630 "self": "/osm/nslcm/v1/ns_lcm_op_occs/" + _id,
1631 "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsr_id,
1632 }
1633 }
1634 return nslcmop
1635
kuuse8b998e42019-07-30 15:22:16 +02001636 def _get_terminate_primitive_params(self, seq, vnf_index):
1637 primitive = seq.get('name')
1638 primitive_params = {}
1639 params = {
1640 "member_vnf_index": vnf_index,
1641 "primitive": primitive,
1642 "primitive_params": primitive_params,
1643 }
1644 desc_params = {}
1645 return self._map_primitive_params(seq, params, desc_params)
1646
kuuseac3a8882019-10-03 10:48:06 +02001647 # sub-operations
1648
1649 def _reintent_or_skip_suboperation(self, db_nslcmop, op_index):
1650 op = db_nslcmop.get('_admin', {}).get('operations', [])[op_index]
1651 if (op.get('operationState') == 'COMPLETED'):
1652 # b. Skip sub-operation
1653 # _ns_execute_primitive() or RO.create_action() will NOT be executed
1654 return self.SUBOPERATION_STATUS_SKIP
1655 else:
1656 # c. Reintent executing sub-operation
1657 # The sub-operation exists, and operationState != 'COMPLETED'
1658 # Update operationState = 'PROCESSING' to indicate a reintent.
1659 operationState = 'PROCESSING'
1660 detailed_status = 'In progress'
1661 self._update_suboperation_status(
1662 db_nslcmop, op_index, operationState, detailed_status)
1663 # Return the sub-operation index
1664 # _ns_execute_primitive() or RO.create_action() will be called from scale()
1665 # with arguments extracted from the sub-operation
1666 return op_index
1667
1668 # Find a sub-operation where all keys in a matching dictionary must match
1669 # Returns the index of the matching sub-operation, or SUBOPERATION_STATUS_NOT_FOUND if no match
1670 def _find_suboperation(self, db_nslcmop, match):
1671 if (db_nslcmop and match):
1672 op_list = db_nslcmop.get('_admin', {}).get('operations', [])
1673 for i, op in enumerate(op_list):
1674 if all(op.get(k) == match[k] for k in match):
1675 return i
1676 return self.SUBOPERATION_STATUS_NOT_FOUND
1677
1678 # Update status for a sub-operation given its index
1679 def _update_suboperation_status(self, db_nslcmop, op_index, operationState, detailed_status):
1680 # Update DB for HA tasks
1681 q_filter = {'_id': db_nslcmop['_id']}
1682 update_dict = {'_admin.operations.{}.operationState'.format(op_index): operationState,
1683 '_admin.operations.{}.detailed-status'.format(op_index): detailed_status}
1684 self.db.set_one("nslcmops",
1685 q_filter=q_filter,
1686 update_dict=update_dict,
1687 fail_on_empty=False)
1688
1689 # Add sub-operation, return the index of the added sub-operation
1690 # Optionally, set operationState, detailed-status, and operationType
1691 # Status and type are currently set for 'scale' sub-operations:
1692 # 'operationState' : 'PROCESSING' | 'COMPLETED' | 'FAILED'
1693 # 'detailed-status' : status message
1694 # 'operationType': may be any type, in the case of scaling: 'PRE-SCALE' | 'POST-SCALE'
1695 # Status and operation type are currently only used for 'scale', but NOT for 'terminate' sub-operations.
1696 def _add_suboperation(self, db_nslcmop, vnf_index, vdu_id, vdu_count_index,
1697 vdu_name, primitive, mapped_primitive_params,
1698 operationState=None, detailed_status=None, operationType=None,
1699 RO_nsr_id=None, RO_scaling_info=None):
1700 if not (db_nslcmop):
1701 return self.SUBOPERATION_STATUS_NOT_FOUND
1702 # Get the "_admin.operations" list, if it exists
1703 db_nslcmop_admin = db_nslcmop.get('_admin', {})
1704 op_list = db_nslcmop_admin.get('operations')
1705 # Create or append to the "_admin.operations" list
kuuse8b998e42019-07-30 15:22:16 +02001706 new_op = {'member_vnf_index': vnf_index,
1707 'vdu_id': vdu_id,
1708 'vdu_count_index': vdu_count_index,
1709 'primitive': primitive,
1710 'primitive_params': mapped_primitive_params}
kuuseac3a8882019-10-03 10:48:06 +02001711 if operationState:
1712 new_op['operationState'] = operationState
1713 if detailed_status:
1714 new_op['detailed-status'] = detailed_status
1715 if operationType:
1716 new_op['lcmOperationType'] = operationType
1717 if RO_nsr_id:
1718 new_op['RO_nsr_id'] = RO_nsr_id
1719 if RO_scaling_info:
1720 new_op['RO_scaling_info'] = RO_scaling_info
1721 if not op_list:
1722 # No existing operations, create key 'operations' with current operation as first list element
1723 db_nslcmop_admin.update({'operations': [new_op]})
1724 op_list = db_nslcmop_admin.get('operations')
1725 else:
1726 # Existing operations, append operation to list
1727 op_list.append(new_op)
kuuse8b998e42019-07-30 15:22:16 +02001728
kuuseac3a8882019-10-03 10:48:06 +02001729 db_nslcmop_update = {'_admin.operations': op_list}
1730 self.update_db_2("nslcmops", db_nslcmop['_id'], db_nslcmop_update)
1731 op_index = len(op_list) - 1
1732 return op_index
1733
1734 # Helper methods for scale() sub-operations
1735
1736 # pre-scale/post-scale:
1737 # Check for 3 different cases:
1738 # a. New: First time execution, return SUBOPERATION_STATUS_NEW
1739 # b. Skip: Existing sub-operation exists, operationState == 'COMPLETED', return SUBOPERATION_STATUS_SKIP
1740 # c. Reintent: Existing sub-operation exists, operationState != 'COMPLETED', return op_index to re-execute
1741 def _check_or_add_scale_suboperation(self, db_nslcmop, vnf_index,
1742 vnf_config_primitive, primitive_params, operationType,
1743 RO_nsr_id=None, RO_scaling_info=None):
1744 # Find this sub-operation
1745 if (RO_nsr_id and RO_scaling_info):
1746 operationType = 'SCALE-RO'
1747 match = {
1748 'member_vnf_index': vnf_index,
1749 'RO_nsr_id': RO_nsr_id,
1750 'RO_scaling_info': RO_scaling_info,
1751 }
1752 else:
1753 match = {
1754 'member_vnf_index': vnf_index,
1755 'primitive': vnf_config_primitive,
1756 'primitive_params': primitive_params,
1757 'lcmOperationType': operationType
1758 }
1759 op_index = self._find_suboperation(db_nslcmop, match)
1760 if (op_index == self.SUBOPERATION_STATUS_NOT_FOUND):
1761 # a. New sub-operation
1762 # The sub-operation does not exist, add it.
1763 # _ns_execute_primitive() will be called from scale() as usual, with non-modified arguments
1764 # The following parameters are set to None for all kind of scaling:
1765 vdu_id = None
1766 vdu_count_index = None
1767 vdu_name = None
1768 if (RO_nsr_id and RO_scaling_info):
1769 vnf_config_primitive = None
1770 primitive_params = None
1771 else:
1772 RO_nsr_id = None
1773 RO_scaling_info = None
1774 # Initial status for sub-operation
1775 operationState = 'PROCESSING'
1776 detailed_status = 'In progress'
1777 # Add sub-operation for pre/post-scaling (zero or more operations)
1778 self._add_suboperation(db_nslcmop,
1779 vnf_index,
1780 vdu_id,
1781 vdu_count_index,
1782 vdu_name,
1783 vnf_config_primitive,
1784 primitive_params,
1785 operationState,
1786 detailed_status,
1787 operationType,
1788 RO_nsr_id,
1789 RO_scaling_info)
1790 return self.SUBOPERATION_STATUS_NEW
1791 else:
1792 # Return either SUBOPERATION_STATUS_SKIP (operationState == 'COMPLETED'),
1793 # or op_index (operationState != 'COMPLETED')
1794 return self._reintent_or_skip_suboperation(db_nslcmop, op_index)
1795
1796 # Helper methods for terminate()
kuuse8b998e42019-07-30 15:22:16 +02001797
kuuse0ca67472019-05-13 15:59:27 +02001798 async def _terminate_action(self, db_nslcmop, nslcmop_id, nsr_id):
tiernoc9556972019-07-05 15:25:25 +00001799 """ Create a primitive with params from VNFD
1800 Called from terminate() before deleting instance
1801 Calls action() to execute the primitive """
kuuse0ca67472019-05-13 15:59:27 +02001802 logging_text = "Task ns={} _terminate_action={} ".format(nsr_id, nslcmop_id)
kuuse0ca67472019-05-13 15:59:27 +02001803 db_vnfrs_list = self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id})
kuuse8b998e42019-07-30 15:22:16 +02001804 db_vnfds = {}
kuuse0ca67472019-05-13 15:59:27 +02001805 # Loop over VNFRs
1806 for vnfr in db_vnfrs_list:
1807 vnfd_id = vnfr["vnfd-id"]
1808 vnf_index = vnfr["member-vnf-index-ref"]
1809 if vnfd_id not in db_vnfds:
1810 step = "Getting vnfd={} id='{}' from db".format(vnfd_id, vnfd_id)
1811 vnfd = self.db.get_one("vnfds", {"_id": vnfd_id})
1812 db_vnfds[vnfd_id] = vnfd
1813 vnfd = db_vnfds[vnfd_id]
1814 if not self._has_terminate_config_primitive(vnfd):
1815 continue
1816 # Get the primitive's sorted sequence list
1817 seq_list = self._get_terminate_config_primitive_seq_list(vnfd)
1818 for seq in seq_list:
kuuse8b998e42019-07-30 15:22:16 +02001819 # For each sequence in list, get primitive and call _ns_execute_primitive()
kuuse0ca67472019-05-13 15:59:27 +02001820 step = "Calling terminate action for vnf_member_index={} primitive={}".format(
1821 vnf_index, seq.get("name"))
1822 self.logger.debug(logging_text + step)
kuuse8b998e42019-07-30 15:22:16 +02001823 # Create the primitive for each sequence, i.e. "primitive": "touch"
kuuse0ca67472019-05-13 15:59:27 +02001824 primitive = seq.get('name')
kuuse8b998e42019-07-30 15:22:16 +02001825 mapped_primitive_params = self._get_terminate_primitive_params(seq, vnf_index)
1826 # The following 3 parameters are currently set to None for 'terminate':
1827 # vdu_id, vdu_count_index, vdu_name
1828 vdu_id = db_nslcmop["operationParams"].get("vdu_id")
1829 vdu_count_index = db_nslcmop["operationParams"].get("vdu_count_index")
1830 vdu_name = db_nslcmop["operationParams"].get("vdu_name")
kuuseac3a8882019-10-03 10:48:06 +02001831 # Add sub-operation
kuuse8b998e42019-07-30 15:22:16 +02001832 self._add_suboperation(db_nslcmop,
1833 nslcmop_id,
1834 vnf_index,
1835 vdu_id,
1836 vdu_count_index,
1837 vdu_name,
1838 primitive,
1839 mapped_primitive_params)
kuuseac3a8882019-10-03 10:48:06 +02001840 # Sub-operations: Call _ns_execute_primitive() instead of action()
kuuse8b998e42019-07-30 15:22:16 +02001841 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
1842 nsr_deployed = db_nsr["_admin"]["deployed"]
1843 result, result_detail = await self._ns_execute_primitive(
1844 nsr_deployed, vnf_index, vdu_id, vdu_name, vdu_count_index, primitive,
1845 mapped_primitive_params)
1846
1847 # nslcmop_operation_state, nslcmop_operation_state_detail = await self.action(
1848 # nsr_id, nslcmop_terminate_action_id)
kuuse0ca67472019-05-13 15:59:27 +02001849 # Launch Exception if action() returns other than ['COMPLETED', 'PARTIALLY_COMPLETED']
kuuse8b998e42019-07-30 15:22:16 +02001850 result_ok = ['COMPLETED', 'PARTIALLY_COMPLETED']
1851 if result not in result_ok:
kuuse0ca67472019-05-13 15:59:27 +02001852 raise LcmException(
1853 "terminate_primitive_action for vnf_member_index={}",
1854 " primitive={} fails with error {}".format(
kuuse8b998e42019-07-30 15:22:16 +02001855 vnf_index, seq.get("name"), result_detail))
kuuse0ca67472019-05-13 15:59:27 +02001856
tierno59d22d22018-09-25 18:10:19 +02001857 async def terminate(self, nsr_id, nslcmop_id):
kuused124bfe2019-06-18 12:09:24 +02001858
1859 # Try to lock HA task here
1860 task_is_locked_by_me = self.lcm_tasks.lock_HA('ns', 'nslcmops', nslcmop_id)
1861 if not task_is_locked_by_me:
1862 return
1863
tierno59d22d22018-09-25 18:10:19 +02001864 logging_text = "Task ns={} terminate={} ".format(nsr_id, nslcmop_id)
1865 self.logger.debug(logging_text + "Enter")
1866 db_nsr = None
1867 db_nslcmop = None
1868 exc = None
1869 failed_detail = [] # annotates all failed error messages
tiernobaa51102018-12-14 13:16:18 +00001870 vca_time_destroy = None # time of where destroy charm order
tierno47e86b52018-10-10 14:05:55 +02001871 db_nsr_update = {"_admin.nslcmop": nslcmop_id}
tierno59d22d22018-09-25 18:10:19 +02001872 db_nslcmop_update = {}
1873 nslcmop_operation_state = None
tiernoc2564fe2019-01-28 16:18:56 +00001874 autoremove = False # autoremove after terminated
tierno59d22d22018-09-25 18:10:19 +02001875 try:
kuused124bfe2019-06-18 12:09:24 +02001876 # wait for any previous tasks in process
1877 await self.lcm_tasks.waitfor_related_HA("ns", 'nslcmops', nslcmop_id)
1878
tierno59d22d22018-09-25 18:10:19 +02001879 step = "Getting nslcmop={} from db".format(nslcmop_id)
1880 db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
1881 step = "Getting nsr={} from db".format(nsr_id)
1882 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
1883 # nsd = db_nsr["nsd"]
tiernoe4f7e6c2018-11-27 14:55:30 +00001884 nsr_deployed = deepcopy(db_nsr["_admin"].get("deployed"))
tierno59d22d22018-09-25 18:10:19 +02001885 if db_nsr["_admin"]["nsState"] == "NOT_INSTANTIATED":
1886 return
tierno59d22d22018-09-25 18:10:19 +02001887 # #TODO check if VIM is creating and wait
1888 # RO_vim_id = db_vim["_admin"]["deployed"]["RO"]
kuuse0ca67472019-05-13 15:59:27 +02001889 # Call internal terminate action
1890 await self._terminate_action(db_nslcmop, nslcmop_id, nsr_id)
tierno59d22d22018-09-25 18:10:19 +02001891
1892 db_nsr_update["operational-status"] = "terminating"
1893 db_nsr_update["config-status"] = "terminating"
1894
tierno9597c292019-02-06 09:21:27 +00001895 if nsr_deployed and nsr_deployed.get("VCA-model-name"):
1896 vca_model_name = nsr_deployed["VCA-model-name"]
1897 step = "deleting VCA model name '{}' and all charms".format(vca_model_name)
1898 self.logger.debug(logging_text + step)
1899 try:
1900 await self.n2vc.DestroyNetworkService(vca_model_name)
1901 except NetworkServiceDoesNotExist:
1902 pass
1903 db_nsr_update["_admin.deployed.VCA-model-name"] = None
1904 if nsr_deployed.get("VCA"):
1905 for vca_index in range(0, len(nsr_deployed["VCA"])):
1906 db_nsr_update["_admin.deployed.VCA.{}".format(vca_index)] = None
1907 self.update_db_2("nsrs", nsr_id, db_nsr_update)
1908 # for backward compatibility if charm have been created with "default" model name delete one by one
1909 elif nsr_deployed and nsr_deployed.get("VCA"):
tierno59d22d22018-09-25 18:10:19 +02001910 try:
1911 step = "Scheduling configuration charms removing"
1912 db_nsr_update["detailed-status"] = "Deleting charms"
1913 self.logger.debug(logging_text + step)
1914 self.update_db_2("nsrs", nsr_id, db_nsr_update)
tierno82974b22018-11-27 21:55:36 +00001915 # for backward compatibility
1916 if isinstance(nsr_deployed["VCA"], dict):
1917 nsr_deployed["VCA"] = list(nsr_deployed["VCA"].values())
1918 db_nsr_update["_admin.deployed.VCA"] = nsr_deployed["VCA"]
1919 self.update_db_2("nsrs", nsr_id, db_nsr_update)
1920
tiernoe4f7e6c2018-11-27 14:55:30 +00001921 for vca_index, vca_deployed in enumerate(nsr_deployed["VCA"]):
tiernobaa51102018-12-14 13:16:18 +00001922 if vca_deployed:
1923 if await self._destroy_charm(vca_deployed['model'], vca_deployed["application"]):
1924 vca_deployed.clear()
1925 db_nsr["_admin.deployed.VCA.{}".format(vca_index)] = None
1926 else:
1927 vca_time_destroy = time()
tierno59d22d22018-09-25 18:10:19 +02001928 except Exception as e:
1929 self.logger.debug(logging_text + "Failed while deleting charms: {}".format(e))
1930
1931 # remove from RO
1932 RO_fail = False
tierno59d22d22018-09-25 18:10:19 +02001933
1934 # Delete ns
1935 RO_nsr_id = RO_delete_action = None
tiernoe4f7e6c2018-11-27 14:55:30 +00001936 if nsr_deployed and nsr_deployed.get("RO"):
1937 RO_nsr_id = nsr_deployed["RO"].get("nsr_id")
1938 RO_delete_action = nsr_deployed["RO"].get("nsr_delete_action_id")
tierno59d22d22018-09-25 18:10:19 +02001939 try:
1940 if RO_nsr_id:
tierno15b1cf12019-08-29 13:21:40 +00001941 step = db_nsr_update["detailed-status"] = db_nslcmop_update["detailed-status"] = \
1942 "Deleting ns from VIM"
tiernoeb55b012018-11-29 08:10:04 +00001943 self.update_db_2("nslcmops", nslcmop_id, db_nslcmop_update)
1944 self.update_db_2("nsrs", nsr_id, db_nsr_update)
tierno59d22d22018-09-25 18:10:19 +02001945 self.logger.debug(logging_text + step)
tierno77677d92019-08-22 13:46:35 +00001946 desc = await self.RO.delete("ns", RO_nsr_id)
tierno59d22d22018-09-25 18:10:19 +02001947 RO_delete_action = desc["action_id"]
1948 db_nsr_update["_admin.deployed.RO.nsr_delete_action_id"] = RO_delete_action
1949 db_nsr_update["_admin.deployed.RO.nsr_id"] = None
1950 db_nsr_update["_admin.deployed.RO.nsr_status"] = "DELETED"
1951 if RO_delete_action:
1952 # wait until NS is deleted from VIM
tiernobaa51102018-12-14 13:16:18 +00001953 step = detailed_status = "Waiting ns deleted from VIM. RO_id={} RO_delete_action={}".\
1954 format(RO_nsr_id, RO_delete_action)
tierno59d22d22018-09-25 18:10:19 +02001955 detailed_status_old = None
1956 self.logger.debug(logging_text + step)
1957
1958 delete_timeout = 20 * 60 # 20 minutes
1959 while delete_timeout > 0:
tierno77677d92019-08-22 13:46:35 +00001960 desc = await self.RO.show("ns", item_id_name=RO_nsr_id, extra_item="action",
1961 extra_item_id=RO_delete_action)
1962 ns_status, ns_status_info = self.RO.check_action_status(desc)
tierno59d22d22018-09-25 18:10:19 +02001963 if ns_status == "ERROR":
1964 raise ROclient.ROClientException(ns_status_info)
1965 elif ns_status == "BUILD":
1966 detailed_status = step + "; {}".format(ns_status_info)
1967 elif ns_status == "ACTIVE":
tiernobaa51102018-12-14 13:16:18 +00001968 db_nsr_update["_admin.deployed.RO.nsr_delete_action_id"] = None
1969 db_nsr_update["_admin.deployed.RO.nsr_status"] = "DELETED"
tierno59d22d22018-09-25 18:10:19 +02001970 break
1971 else:
1972 assert False, "ROclient.check_action_status returns unknown {}".format(ns_status)
tiernoeb55b012018-11-29 08:10:04 +00001973 if detailed_status != detailed_status_old:
1974 detailed_status_old = db_nslcmop_update["detailed-status"] = \
1975 db_nsr_update["detailed-status"] = detailed_status
1976 self.update_db_2("nslcmops", nslcmop_id, db_nslcmop_update)
1977 self.update_db_2("nsrs", nsr_id, db_nsr_update)
tierno59d22d22018-09-25 18:10:19 +02001978 await asyncio.sleep(5, loop=self.loop)
1979 delete_timeout -= 5
tierno59d22d22018-09-25 18:10:19 +02001980 else: # delete_timeout <= 0:
1981 raise ROclient.ROClientException("Timeout waiting ns deleted from VIM")
1982
1983 except ROclient.ROClientException as e:
1984 if e.http_code == 404: # not found
1985 db_nsr_update["_admin.deployed.RO.nsr_id"] = None
1986 db_nsr_update["_admin.deployed.RO.nsr_status"] = "DELETED"
tiernobaa51102018-12-14 13:16:18 +00001987 db_nsr_update["_admin.deployed.RO.nsr_delete_action_id"] = None
tierno59d22d22018-09-25 18:10:19 +02001988 self.logger.debug(logging_text + "RO_ns_id={} already deleted".format(RO_nsr_id))
1989 elif e.http_code == 409: # conflict
1990 failed_detail.append("RO_ns_id={} delete conflict: {}".format(RO_nsr_id, e))
1991 self.logger.debug(logging_text + failed_detail[-1])
1992 RO_fail = True
1993 else:
1994 failed_detail.append("RO_ns_id={} delete error: {}".format(RO_nsr_id, e))
1995 self.logger.error(logging_text + failed_detail[-1])
1996 RO_fail = True
1997
1998 # Delete nsd
tiernoe4f7e6c2018-11-27 14:55:30 +00001999 if not RO_fail and nsr_deployed and nsr_deployed.get("RO") and nsr_deployed["RO"].get("nsd_id"):
2000 RO_nsd_id = nsr_deployed["RO"]["nsd_id"]
tierno59d22d22018-09-25 18:10:19 +02002001 try:
2002 step = db_nsr_update["detailed-status"] = db_nslcmop_update["detailed-status"] =\
tierno15b1cf12019-08-29 13:21:40 +00002003 "Deleting nsd from RO"
tierno77677d92019-08-22 13:46:35 +00002004 await self.RO.delete("nsd", RO_nsd_id)
tierno59d22d22018-09-25 18:10:19 +02002005 self.logger.debug(logging_text + "RO_nsd_id={} deleted".format(RO_nsd_id))
2006 db_nsr_update["_admin.deployed.RO.nsd_id"] = None
2007 except ROclient.ROClientException as e:
2008 if e.http_code == 404: # not found
2009 db_nsr_update["_admin.deployed.RO.nsd_id"] = None
2010 self.logger.debug(logging_text + "RO_nsd_id={} already deleted".format(RO_nsd_id))
2011 elif e.http_code == 409: # conflict
2012 failed_detail.append("RO_nsd_id={} delete conflict: {}".format(RO_nsd_id, e))
2013 self.logger.debug(logging_text + failed_detail[-1])
2014 RO_fail = True
2015 else:
2016 failed_detail.append("RO_nsd_id={} delete error: {}".format(RO_nsd_id, e))
2017 self.logger.error(logging_text + failed_detail[-1])
2018 RO_fail = True
2019
tiernoa009e552019-01-30 16:45:44 +00002020 if not RO_fail and nsr_deployed and nsr_deployed.get("RO") and nsr_deployed["RO"].get("vnfd"):
2021 for index, vnf_deployed in enumerate(nsr_deployed["RO"]["vnfd"]):
2022 if not vnf_deployed or not vnf_deployed["id"]:
tierno59d22d22018-09-25 18:10:19 +02002023 continue
2024 try:
tiernoa009e552019-01-30 16:45:44 +00002025 RO_vnfd_id = vnf_deployed["id"]
tierno59d22d22018-09-25 18:10:19 +02002026 step = db_nsr_update["detailed-status"] = db_nslcmop_update["detailed-status"] =\
tierno98ad6ea2019-05-30 17:16:28 +00002027 "Deleting member_vnf_index={} RO_vnfd_id={} from RO".format(
tiernoa009e552019-01-30 16:45:44 +00002028 vnf_deployed["member-vnf-index"], RO_vnfd_id)
tierno77677d92019-08-22 13:46:35 +00002029 await self.RO.delete("vnfd", RO_vnfd_id)
tierno59d22d22018-09-25 18:10:19 +02002030 self.logger.debug(logging_text + "RO_vnfd_id={} deleted".format(RO_vnfd_id))
tiernoa009e552019-01-30 16:45:44 +00002031 db_nsr_update["_admin.deployed.RO.vnfd.{}.id".format(index)] = None
tierno59d22d22018-09-25 18:10:19 +02002032 except ROclient.ROClientException as e:
2033 if e.http_code == 404: # not found
tiernoa009e552019-01-30 16:45:44 +00002034 db_nsr_update["_admin.deployed.RO.vnfd.{}.id".format(index)] = None
tierno59d22d22018-09-25 18:10:19 +02002035 self.logger.debug(logging_text + "RO_vnfd_id={} already deleted ".format(RO_vnfd_id))
2036 elif e.http_code == 409: # conflict
2037 failed_detail.append("RO_vnfd_id={} delete conflict: {}".format(RO_vnfd_id, e))
2038 self.logger.debug(logging_text + failed_detail[-1])
2039 else:
2040 failed_detail.append("RO_vnfd_id={} delete error: {}".format(RO_vnfd_id, e))
2041 self.logger.error(logging_text + failed_detail[-1])
2042
tiernobaa51102018-12-14 13:16:18 +00002043 # wait until charm deleted
2044 if vca_time_destroy:
2045 db_nsr_update["detailed-status"] = db_nslcmop_update["detailed-status"] = step = \
tierno59d22d22018-09-25 18:10:19 +02002046 "Waiting for deletion of configuration charms"
2047 self.update_db_2("nslcmops", nslcmop_id, db_nslcmop_update)
2048 self.update_db_2("nsrs", nsr_id, db_nsr_update)
tiernobaa51102018-12-14 13:16:18 +00002049 for vca_index, vca_deployed in enumerate(nsr_deployed["VCA"]):
2050 if not vca_deployed:
2051 continue
2052 step = "Waiting for deletion of charm application_name={}".format(vca_deployed["application"])
2053 timeout = self.timeout_charm_delete - int(time() - vca_time_destroy)
2054 if not await self._wait_charm_destroyed(vca_deployed['model'], vca_deployed["application"],
2055 timeout):
2056 failed_detail.append("VCA[application_name={}] Deletion timeout".format(
2057 vca_deployed["application"]))
tierno59d22d22018-09-25 18:10:19 +02002058 else:
tiernobaa51102018-12-14 13:16:18 +00002059 db_nsr["_admin.deployed.VCA.{}".format(vca_index)] = None
tierno59d22d22018-09-25 18:10:19 +02002060
2061 if failed_detail:
2062 self.logger.error(logging_text + " ;".join(failed_detail))
2063 db_nsr_update["operational-status"] = "failed"
2064 db_nsr_update["detailed-status"] = "Deletion errors " + "; ".join(failed_detail)
2065 db_nslcmop_update["detailed-status"] = "; ".join(failed_detail)
2066 db_nslcmop_update["operationState"] = nslcmop_operation_state = "FAILED"
2067 db_nslcmop_update["statusEnteredTime"] = time()
tierno59d22d22018-09-25 18:10:19 +02002068 else:
2069 db_nsr_update["operational-status"] = "terminated"
2070 db_nsr_update["detailed-status"] = "Done"
2071 db_nsr_update["_admin.nsState"] = "NOT_INSTANTIATED"
2072 db_nslcmop_update["detailed-status"] = "Done"
2073 db_nslcmop_update["operationState"] = nslcmop_operation_state = "COMPLETED"
2074 db_nslcmop_update["statusEnteredTime"] = time()
tiernoc2564fe2019-01-28 16:18:56 +00002075 if db_nslcmop["operationParams"].get("autoremove"):
2076 autoremove = True
tierno59d22d22018-09-25 18:10:19 +02002077
kuuse0ca67472019-05-13 15:59:27 +02002078 except (ROclient.ROClientException, DbException, LcmException) as e:
tierno59d22d22018-09-25 18:10:19 +02002079 self.logger.error(logging_text + "Exit Exception {}".format(e))
2080 exc = e
2081 except asyncio.CancelledError:
2082 self.logger.error(logging_text + "Cancelled Exception while '{}'".format(step))
2083 exc = "Operation was cancelled"
2084 except Exception as e:
2085 exc = traceback.format_exc()
2086 self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True)
2087 finally:
2088 if exc and db_nslcmop:
2089 db_nslcmop_update["detailed-status"] = "FAILED {}: {}".format(step, exc)
2090 db_nslcmop_update["operationState"] = nslcmop_operation_state = "FAILED"
2091 db_nslcmop_update["statusEnteredTime"] = time()
tiernobaa51102018-12-14 13:16:18 +00002092 try:
2093 if db_nslcmop and db_nslcmop_update:
2094 self.update_db_2("nslcmops", nslcmop_id, db_nslcmop_update)
2095 if db_nsr:
2096 db_nsr_update["_admin.nslcmop"] = None
2097 self.update_db_2("nsrs", nsr_id, db_nsr_update)
2098 except DbException as e:
2099 self.logger.error(logging_text + "Cannot update database: {}".format(e))
tierno59d22d22018-09-25 18:10:19 +02002100 if nslcmop_operation_state:
2101 try:
2102 await self.msg.aiowrite("ns", "terminated", {"nsr_id": nsr_id, "nslcmop_id": nslcmop_id,
tiernoc2564fe2019-01-28 16:18:56 +00002103 "operationState": nslcmop_operation_state,
2104 "autoremove": autoremove},
tierno8a518872018-12-21 13:42:14 +00002105 loop=self.loop)
tierno59d22d22018-09-25 18:10:19 +02002106 except Exception as e:
2107 self.logger.error(logging_text + "kafka_write notification Exception {}".format(e))
2108 self.logger.debug(logging_text + "Exit")
2109 self.lcm_tasks.remove("ns", nsr_id, nslcmop_id, "ns_terminate")
2110
tiernoda964822019-01-14 15:53:47 +00002111 @staticmethod
2112 def _map_primitive_params(primitive_desc, params, instantiation_params):
2113 """
2114 Generates the params to be provided to charm before executing primitive. If user does not provide a parameter,
2115 The default-value is used. If it is between < > it look for a value at instantiation_params
2116 :param primitive_desc: portion of VNFD/NSD that describes primitive
2117 :param params: Params provided by user
2118 :param instantiation_params: Instantiation params provided by user
2119 :return: a dictionary with the calculated params
2120 """
2121 calculated_params = {}
2122 for parameter in primitive_desc.get("parameter", ()):
2123 param_name = parameter["name"]
2124 if param_name in params:
2125 calculated_params[param_name] = params[param_name]
tierno98ad6ea2019-05-30 17:16:28 +00002126 elif "default-value" in parameter or "value" in parameter:
2127 if "value" in parameter:
2128 calculated_params[param_name] = parameter["value"]
2129 else:
2130 calculated_params[param_name] = parameter["default-value"]
2131 if isinstance(calculated_params[param_name], str) and calculated_params[param_name].startswith("<") \
2132 and calculated_params[param_name].endswith(">"):
2133 if calculated_params[param_name][1:-1] in instantiation_params:
2134 calculated_params[param_name] = instantiation_params[calculated_params[param_name][1:-1]]
tiernoda964822019-01-14 15:53:47 +00002135 else:
2136 raise LcmException("Parameter {} needed to execute primitive {} not provided".
2137 format(parameter["default-value"], primitive_desc["name"]))
2138 else:
2139 raise LcmException("Parameter {} needed to execute primitive {} not provided".
2140 format(param_name, primitive_desc["name"]))
tierno59d22d22018-09-25 18:10:19 +02002141
tiernoda964822019-01-14 15:53:47 +00002142 if isinstance(calculated_params[param_name], (dict, list, tuple)):
2143 calculated_params[param_name] = yaml.safe_dump(calculated_params[param_name], default_flow_style=True,
2144 width=256)
2145 elif isinstance(calculated_params[param_name], str) and calculated_params[param_name].startswith("!!yaml "):
2146 calculated_params[param_name] = calculated_params[param_name][7:]
2147 return calculated_params
2148
2149 async def _ns_execute_primitive(self, db_deployed, member_vnf_index, vdu_id, vdu_name, vdu_count_index,
tierno2fc7ce52019-06-11 22:50:01 +00002150 primitive, primitive_params, retries=0, retries_interval=30):
tiernoda964822019-01-14 15:53:47 +00002151 start_primitive_time = time()
2152 try:
2153 for vca_deployed in db_deployed["VCA"]:
2154 if not vca_deployed:
2155 continue
2156 if member_vnf_index != vca_deployed["member-vnf-index"] or vdu_id != vca_deployed["vdu_id"]:
2157 continue
2158 if vdu_name and vdu_name != vca_deployed["vdu_name"]:
2159 continue
2160 if vdu_count_index and vdu_count_index != vca_deployed["vdu_count_index"]:
2161 continue
2162 break
2163 else:
2164 raise LcmException("charm for member_vnf_index={} vdu_id={} vdu_name={} vdu_count_index={} is not "
2165 "deployed".format(member_vnf_index, vdu_id, vdu_name, vdu_count_index))
2166 model_name = vca_deployed.get("model")
2167 application_name = vca_deployed.get("application")
2168 if not model_name or not application_name:
2169 raise LcmException("charm for member_vnf_index={} vdu_id={} vdu_name={} vdu_count_index={} has not "
2170 "model or application name" .format(member_vnf_index, vdu_id, vdu_name,
2171 vdu_count_index))
tierno98ad6ea2019-05-30 17:16:28 +00002172 # if vca_deployed["operational-status"] != "active":
2173 # raise LcmException("charm for member_vnf_index={} vdu_id={} operational_status={} not 'active'".format(
2174 # member_vnf_index, vdu_id, vca_deployed["operational-status"]))
tiernoda964822019-01-14 15:53:47 +00002175 callback = None # self.n2vc_callback
2176 callback_args = () # [db_nsr, db_nslcmop, member_vnf_index, None]
2177 await self.n2vc.login()
tierno98ad6ea2019-05-30 17:16:28 +00002178 if primitive == "config":
2179 primitive_params = {"params": primitive_params}
tierno2fc7ce52019-06-11 22:50:01 +00002180 while retries >= 0:
2181 primitive_id = await self.n2vc.ExecutePrimitive(
2182 model_name,
2183 application_name,
2184 primitive,
2185 callback,
2186 *callback_args,
2187 **primitive_params
2188 )
2189 while time() - start_primitive_time < self.timeout_primitive:
2190 primitive_result_ = await self.n2vc.GetPrimitiveStatus(model_name, primitive_id)
2191 if primitive_result_ in ("completed", "failed"):
2192 primitive_result = "COMPLETED" if primitive_result_ == "completed" else "FAILED"
2193 detailed_result = await self.n2vc.GetPrimitiveOutput(model_name, primitive_id)
2194 break
2195 elif primitive_result_ is None and primitive == "config":
2196 primitive_result = "COMPLETED"
2197 detailed_result = None
2198 break
2199 else: # ("running", "pending", None):
2200 pass
2201 await asyncio.sleep(5)
2202 else:
2203 raise LcmException("timeout after {} seconds".format(self.timeout_primitive))
2204 if primitive_result == "COMPLETED":
tiernoda964822019-01-14 15:53:47 +00002205 break
tierno2fc7ce52019-06-11 22:50:01 +00002206 retries -= 1
2207 if retries >= 0:
2208 await asyncio.sleep(retries_interval)
2209
tiernoda964822019-01-14 15:53:47 +00002210 return primitive_result, detailed_result
2211 except (N2VCPrimitiveExecutionFailed, LcmException) as e:
2212 return "FAILED", str(e)
tierno59d22d22018-09-25 18:10:19 +02002213
2214 async def action(self, nsr_id, nslcmop_id):
kuused124bfe2019-06-18 12:09:24 +02002215
2216 # Try to lock HA task here
2217 task_is_locked_by_me = self.lcm_tasks.lock_HA('ns', 'nslcmops', nslcmop_id)
2218 if not task_is_locked_by_me:
2219 return
2220
tierno59d22d22018-09-25 18:10:19 +02002221 logging_text = "Task ns={} action={} ".format(nsr_id, nslcmop_id)
2222 self.logger.debug(logging_text + "Enter")
2223 # get all needed from database
2224 db_nsr = None
2225 db_nslcmop = None
tierno47e86b52018-10-10 14:05:55 +02002226 db_nsr_update = {"_admin.nslcmop": nslcmop_id}
tierno59d22d22018-09-25 18:10:19 +02002227 db_nslcmop_update = {}
2228 nslcmop_operation_state = None
kuuse0ca67472019-05-13 15:59:27 +02002229 nslcmop_operation_state_detail = None
tierno59d22d22018-09-25 18:10:19 +02002230 exc = None
2231 try:
kuused124bfe2019-06-18 12:09:24 +02002232 # wait for any previous tasks in process
2233 await self.lcm_tasks.waitfor_related_HA('ns', 'nslcmops', nslcmop_id)
2234
tierno59d22d22018-09-25 18:10:19 +02002235 step = "Getting information from database"
2236 db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
2237 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
tiernoda964822019-01-14 15:53:47 +00002238
tiernoe4f7e6c2018-11-27 14:55:30 +00002239 nsr_deployed = db_nsr["_admin"].get("deployed")
tierno1b633412019-02-25 16:48:23 +00002240 vnf_index = db_nslcmop["operationParams"].get("member_vnf_index")
tierno59d22d22018-09-25 18:10:19 +02002241 vdu_id = db_nslcmop["operationParams"].get("vdu_id")
tiernoe4f7e6c2018-11-27 14:55:30 +00002242 vdu_count_index = db_nslcmop["operationParams"].get("vdu_count_index")
2243 vdu_name = db_nslcmop["operationParams"].get("vdu_name")
tierno59d22d22018-09-25 18:10:19 +02002244
tierno1b633412019-02-25 16:48:23 +00002245 if vnf_index:
2246 step = "Getting vnfr from database"
2247 db_vnfr = self.db.get_one("vnfrs", {"member-vnf-index-ref": vnf_index, "nsr-id-ref": nsr_id})
2248 step = "Getting vnfd from database"
2249 db_vnfd = self.db.get_one("vnfds", {"_id": db_vnfr["vnfd-id"]})
2250 else:
2251 if db_nsr.get("nsd"):
2252 db_nsd = db_nsr.get("nsd") # TODO this will be removed
2253 else:
2254 step = "Getting nsd from database"
2255 db_nsd = self.db.get_one("nsds", {"_id": db_nsr["nsd-id"]})
tiernoda964822019-01-14 15:53:47 +00002256
tierno82974b22018-11-27 21:55:36 +00002257 # for backward compatibility
2258 if nsr_deployed and isinstance(nsr_deployed.get("VCA"), dict):
2259 nsr_deployed["VCA"] = list(nsr_deployed["VCA"].values())
2260 db_nsr_update["_admin.deployed.VCA"] = nsr_deployed["VCA"]
2261 self.update_db_2("nsrs", nsr_id, db_nsr_update)
2262
tierno59d22d22018-09-25 18:10:19 +02002263 primitive = db_nslcmop["operationParams"]["primitive"]
2264 primitive_params = db_nslcmop["operationParams"]["primitive_params"]
tiernoda964822019-01-14 15:53:47 +00002265
2266 # look for primitive
2267 config_primitive_desc = None
2268 if vdu_id:
2269 for vdu in get_iterable(db_vnfd, "vdu"):
2270 if vdu_id == vdu["id"]:
2271 for config_primitive in vdu.get("vdu-configuration", {}).get("config-primitive", ()):
2272 if config_primitive["name"] == primitive:
2273 config_primitive_desc = config_primitive
2274 break
tierno1b633412019-02-25 16:48:23 +00002275 elif vnf_index:
2276 for config_primitive in db_vnfd.get("vnf-configuration", {}).get("config-primitive", ()):
2277 if config_primitive["name"] == primitive:
2278 config_primitive_desc = config_primitive
2279 break
2280 else:
2281 for config_primitive in db_nsd.get("ns-configuration", {}).get("config-primitive", ()):
2282 if config_primitive["name"] == primitive:
2283 config_primitive_desc = config_primitive
2284 break
tiernoda964822019-01-14 15:53:47 +00002285
tierno1b633412019-02-25 16:48:23 +00002286 if not config_primitive_desc:
2287 raise LcmException("Primitive {} not found at [ns|vnf|vdu]-configuration:config-primitive ".
2288 format(primitive))
2289
2290 desc_params = {}
2291 if vnf_index:
2292 if db_vnfr.get("additionalParamsForVnf"):
2293 desc_params.update(db_vnfr["additionalParamsForVnf"])
2294 else:
2295 if db_nsr.get("additionalParamsForVnf"):
2296 desc_params.update(db_nsr["additionalParamsForNs"])
tiernoda964822019-01-14 15:53:47 +00002297
2298 # TODO check if ns is in a proper status
2299 result, result_detail = await self._ns_execute_primitive(
2300 nsr_deployed, vnf_index, vdu_id, vdu_name, vdu_count_index, primitive,
tierno1b633412019-02-25 16:48:23 +00002301 self._map_primitive_params(config_primitive_desc, primitive_params, desc_params))
kuuse0ca67472019-05-13 15:59:27 +02002302 db_nslcmop_update["detailed-status"] = nslcmop_operation_state_detail = result_detail
tierno59d22d22018-09-25 18:10:19 +02002303 db_nslcmop_update["operationState"] = nslcmop_operation_state = result
2304 db_nslcmop_update["statusEnteredTime"] = time()
2305 self.logger.debug(logging_text + " task Done with result {} {}".format(result, result_detail))
2306 return # database update is called inside finally
2307
2308 except (DbException, LcmException) as e:
2309 self.logger.error(logging_text + "Exit Exception {}".format(e))
2310 exc = e
2311 except asyncio.CancelledError:
2312 self.logger.error(logging_text + "Cancelled Exception while '{}'".format(step))
2313 exc = "Operation was cancelled"
2314 except Exception as e:
2315 exc = traceback.format_exc()
2316 self.logger.critical(logging_text + "Exit Exception {} {}".format(type(e).__name__, e), exc_info=True)
2317 finally:
2318 if exc and db_nslcmop:
kuuse0ca67472019-05-13 15:59:27 +02002319 db_nslcmop_update["detailed-status"] = nslcmop_operation_state_detail = \
2320 "FAILED {}: {}".format(step, exc)
tierno59d22d22018-09-25 18:10:19 +02002321 db_nslcmop_update["operationState"] = nslcmop_operation_state = "FAILED"
2322 db_nslcmop_update["statusEnteredTime"] = time()
tiernobaa51102018-12-14 13:16:18 +00002323 try:
2324 if db_nslcmop_update:
2325 self.update_db_2("nslcmops", nslcmop_id, db_nslcmop_update)
2326 if db_nsr:
2327 db_nsr_update["_admin.nslcmop"] = None
2328 self.update_db_2("nsrs", nsr_id, db_nsr_update)
2329 except DbException as e:
2330 self.logger.error(logging_text + "Cannot update database: {}".format(e))
tierno59d22d22018-09-25 18:10:19 +02002331 self.logger.debug(logging_text + "Exit")
2332 if nslcmop_operation_state:
2333 try:
2334 await self.msg.aiowrite("ns", "actioned", {"nsr_id": nsr_id, "nslcmop_id": nslcmop_id,
tierno8a518872018-12-21 13:42:14 +00002335 "operationState": nslcmop_operation_state},
2336 loop=self.loop)
tierno59d22d22018-09-25 18:10:19 +02002337 except Exception as e:
2338 self.logger.error(logging_text + "kafka_write notification Exception {}".format(e))
2339 self.logger.debug(logging_text + "Exit")
2340 self.lcm_tasks.remove("ns", nsr_id, nslcmop_id, "ns_action")
kuuse0ca67472019-05-13 15:59:27 +02002341 return nslcmop_operation_state, nslcmop_operation_state_detail
tierno59d22d22018-09-25 18:10:19 +02002342
2343 async def scale(self, nsr_id, nslcmop_id):
kuused124bfe2019-06-18 12:09:24 +02002344
2345 # Try to lock HA task here
2346 task_is_locked_by_me = self.lcm_tasks.lock_HA('ns', 'nslcmops', nslcmop_id)
2347 if not task_is_locked_by_me:
2348 return
2349
tierno59d22d22018-09-25 18:10:19 +02002350 logging_text = "Task ns={} scale={} ".format(nsr_id, nslcmop_id)
2351 self.logger.debug(logging_text + "Enter")
2352 # get all needed from database
2353 db_nsr = None
2354 db_nslcmop = None
2355 db_nslcmop_update = {}
2356 nslcmop_operation_state = None
tierno47e86b52018-10-10 14:05:55 +02002357 db_nsr_update = {"_admin.nslcmop": nslcmop_id}
tierno59d22d22018-09-25 18:10:19 +02002358 exc = None
tierno9ab95942018-10-10 16:44:22 +02002359 # in case of error, indicates what part of scale was failed to put nsr at error status
2360 scale_process = None
tiernod6de1992018-10-11 13:05:52 +02002361 old_operational_status = ""
2362 old_config_status = ""
tiernof578e552018-11-08 19:07:20 +01002363 vnfr_scaled = False
tierno59d22d22018-09-25 18:10:19 +02002364 try:
kuused124bfe2019-06-18 12:09:24 +02002365 # wait for any previous tasks in process
2366 await self.lcm_tasks.waitfor_related_HA('ns', 'nslcmops', nslcmop_id)
tierno47e86b52018-10-10 14:05:55 +02002367
ikalyvas02d9e7b2019-05-27 18:16:01 +03002368 step = "Getting nslcmop from database"
ikalyvas02d9e7b2019-05-27 18:16:01 +03002369 self.logger.debug(step + " after having waited for previous tasks to be completed")
2370 db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
2371 step = "Getting nsr from database"
2372 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
2373
2374 old_operational_status = db_nsr["operational-status"]
2375 old_config_status = db_nsr["config-status"]
tierno59d22d22018-09-25 18:10:19 +02002376 step = "Parsing scaling parameters"
tierno9babfda2019-06-07 12:36:50 +00002377 # self.logger.debug(step)
tierno59d22d22018-09-25 18:10:19 +02002378 db_nsr_update["operational-status"] = "scaling"
2379 self.update_db_2("nsrs", nsr_id, db_nsr_update)
tiernoe4f7e6c2018-11-27 14:55:30 +00002380 nsr_deployed = db_nsr["_admin"].get("deployed")
2381 RO_nsr_id = nsr_deployed["RO"]["nsr_id"]
tierno59d22d22018-09-25 18:10:19 +02002382 vnf_index = db_nslcmop["operationParams"]["scaleVnfData"]["scaleByStepData"]["member-vnf-index"]
2383 scaling_group = db_nslcmop["operationParams"]["scaleVnfData"]["scaleByStepData"]["scaling-group-descriptor"]
2384 scaling_type = db_nslcmop["operationParams"]["scaleVnfData"]["scaleVnfType"]
2385 # scaling_policy = db_nslcmop["operationParams"]["scaleVnfData"]["scaleByStepData"].get("scaling-policy")
2386
tierno82974b22018-11-27 21:55:36 +00002387 # for backward compatibility
2388 if nsr_deployed and isinstance(nsr_deployed.get("VCA"), dict):
2389 nsr_deployed["VCA"] = list(nsr_deployed["VCA"].values())
2390 db_nsr_update["_admin.deployed.VCA"] = nsr_deployed["VCA"]
2391 self.update_db_2("nsrs", nsr_id, db_nsr_update)
2392
tierno59d22d22018-09-25 18:10:19 +02002393 step = "Getting vnfr from database"
2394 db_vnfr = self.db.get_one("vnfrs", {"member-vnf-index-ref": vnf_index, "nsr-id-ref": nsr_id})
2395 step = "Getting vnfd from database"
2396 db_vnfd = self.db.get_one("vnfds", {"_id": db_vnfr["vnfd-id"]})
ikalyvas02d9e7b2019-05-27 18:16:01 +03002397
tierno59d22d22018-09-25 18:10:19 +02002398 step = "Getting scaling-group-descriptor"
2399 for scaling_descriptor in db_vnfd["scaling-group-descriptor"]:
2400 if scaling_descriptor["name"] == scaling_group:
2401 break
2402 else:
2403 raise LcmException("input parameter 'scaleByStepData':'scaling-group-descriptor':'{}' is not present "
2404 "at vnfd:scaling-group-descriptor".format(scaling_group))
ikalyvas02d9e7b2019-05-27 18:16:01 +03002405
tierno59d22d22018-09-25 18:10:19 +02002406 # cooldown_time = 0
2407 # for scaling_policy_descriptor in scaling_descriptor.get("scaling-policy", ()):
2408 # cooldown_time = scaling_policy_descriptor.get("cooldown-time", 0)
2409 # if scaling_policy and scaling_policy == scaling_policy_descriptor.get("name"):
2410 # break
2411
2412 # TODO check if ns is in a proper status
tierno15b1cf12019-08-29 13:21:40 +00002413 step = "Sending scale order to VIM"
tierno59d22d22018-09-25 18:10:19 +02002414 nb_scale_op = 0
2415 if not db_nsr["_admin"].get("scaling-group"):
2416 self.update_db_2("nsrs", nsr_id, {"_admin.scaling-group": [{"name": scaling_group, "nb-scale-op": 0}]})
2417 admin_scale_index = 0
2418 else:
2419 for admin_scale_index, admin_scale_info in enumerate(db_nsr["_admin"]["scaling-group"]):
2420 if admin_scale_info["name"] == scaling_group:
2421 nb_scale_op = admin_scale_info.get("nb-scale-op", 0)
2422 break
tierno9ab95942018-10-10 16:44:22 +02002423 else: # not found, set index one plus last element and add new entry with the name
2424 admin_scale_index += 1
2425 db_nsr_update["_admin.scaling-group.{}.name".format(admin_scale_index)] = scaling_group
tierno59d22d22018-09-25 18:10:19 +02002426 RO_scaling_info = []
2427 vdu_scaling_info = {"scaling_group_name": scaling_group, "vdu": []}
2428 if scaling_type == "SCALE_OUT":
2429 # count if max-instance-count is reached
kuuse818d70c2019-08-07 14:43:44 +02002430 max_instance_count = scaling_descriptor.get("max-instance-count", 10)
2431 # self.logger.debug("MAX_INSTANCE_COUNT is {}".format(max_instance_count))
2432 if nb_scale_op >= max_instance_count:
2433 raise LcmException("reached the limit of {} (max-instance-count) "
2434 "scaling-out operations for the "
2435 "scaling-group-descriptor '{}'".format(nb_scale_op, scaling_group))
kuuse8b998e42019-07-30 15:22:16 +02002436
ikalyvas02d9e7b2019-05-27 18:16:01 +03002437 nb_scale_op += 1
tierno59d22d22018-09-25 18:10:19 +02002438 vdu_scaling_info["scaling_direction"] = "OUT"
2439 vdu_scaling_info["vdu-create"] = {}
2440 for vdu_scale_info in scaling_descriptor["vdu"]:
2441 RO_scaling_info.append({"osm_vdu_id": vdu_scale_info["vdu-id-ref"], "member-vnf-index": vnf_index,
2442 "type": "create", "count": vdu_scale_info.get("count", 1)})
2443 vdu_scaling_info["vdu-create"][vdu_scale_info["vdu-id-ref"]] = vdu_scale_info.get("count", 1)
ikalyvas02d9e7b2019-05-27 18:16:01 +03002444
tierno59d22d22018-09-25 18:10:19 +02002445 elif scaling_type == "SCALE_IN":
2446 # count if min-instance-count is reached
tierno27246d82018-09-27 15:59:09 +02002447 min_instance_count = 0
tierno59d22d22018-09-25 18:10:19 +02002448 if "min-instance-count" in scaling_descriptor and scaling_descriptor["min-instance-count"] is not None:
2449 min_instance_count = int(scaling_descriptor["min-instance-count"])
tierno9babfda2019-06-07 12:36:50 +00002450 if nb_scale_op <= min_instance_count:
2451 raise LcmException("reached the limit of {} (min-instance-count) scaling-in operations for the "
2452 "scaling-group-descriptor '{}'".format(nb_scale_op, scaling_group))
ikalyvas02d9e7b2019-05-27 18:16:01 +03002453 nb_scale_op -= 1
tierno59d22d22018-09-25 18:10:19 +02002454 vdu_scaling_info["scaling_direction"] = "IN"
2455 vdu_scaling_info["vdu-delete"] = {}
2456 for vdu_scale_info in scaling_descriptor["vdu"]:
2457 RO_scaling_info.append({"osm_vdu_id": vdu_scale_info["vdu-id-ref"], "member-vnf-index": vnf_index,
2458 "type": "delete", "count": vdu_scale_info.get("count", 1)})
2459 vdu_scaling_info["vdu-delete"][vdu_scale_info["vdu-id-ref"]] = vdu_scale_info.get("count", 1)
2460
2461 # update VDU_SCALING_INFO with the VDUs to delete ip_addresses
tierno27246d82018-09-27 15:59:09 +02002462 vdu_create = vdu_scaling_info.get("vdu-create")
2463 vdu_delete = copy(vdu_scaling_info.get("vdu-delete"))
tierno59d22d22018-09-25 18:10:19 +02002464 if vdu_scaling_info["scaling_direction"] == "IN":
2465 for vdur in reversed(db_vnfr["vdur"]):
tierno27246d82018-09-27 15:59:09 +02002466 if vdu_delete.get(vdur["vdu-id-ref"]):
2467 vdu_delete[vdur["vdu-id-ref"]] -= 1
tierno59d22d22018-09-25 18:10:19 +02002468 vdu_scaling_info["vdu"].append({
2469 "name": vdur["name"],
2470 "vdu_id": vdur["vdu-id-ref"],
2471 "interface": []
2472 })
2473 for interface in vdur["interfaces"]:
2474 vdu_scaling_info["vdu"][-1]["interface"].append({
2475 "name": interface["name"],
2476 "ip_address": interface["ip-address"],
2477 "mac_address": interface.get("mac-address"),
2478 })
tierno27246d82018-09-27 15:59:09 +02002479 vdu_delete = vdu_scaling_info.pop("vdu-delete")
tierno59d22d22018-09-25 18:10:19 +02002480
kuuseac3a8882019-10-03 10:48:06 +02002481 # PRE-SCALE BEGIN
tierno59d22d22018-09-25 18:10:19 +02002482 step = "Executing pre-scale vnf-config-primitive"
2483 if scaling_descriptor.get("scaling-config-action"):
2484 for scaling_config_action in scaling_descriptor["scaling-config-action"]:
kuuseac3a8882019-10-03 10:48:06 +02002485 if (scaling_config_action.get("trigger") == "pre-scale-in" and scaling_type == "SCALE_IN") \
2486 or (scaling_config_action.get("trigger") == "pre-scale-out" and scaling_type == "SCALE_OUT"):
tierno59d22d22018-09-25 18:10:19 +02002487 vnf_config_primitive = scaling_config_action["vnf-config-primitive-name-ref"]
2488 step = db_nslcmop_update["detailed-status"] = \
2489 "executing pre-scale scaling-config-action '{}'".format(vnf_config_primitive)
tiernoda964822019-01-14 15:53:47 +00002490
tierno59d22d22018-09-25 18:10:19 +02002491 # look for primitive
tierno59d22d22018-09-25 18:10:19 +02002492 for config_primitive in db_vnfd.get("vnf-configuration", {}).get("config-primitive", ()):
2493 if config_primitive["name"] == vnf_config_primitive:
tierno59d22d22018-09-25 18:10:19 +02002494 break
2495 else:
2496 raise LcmException(
2497 "Invalid vnfd descriptor at scaling-group-descriptor[name='{}']:scaling-config-action"
tiernoda964822019-01-14 15:53:47 +00002498 "[vnf-config-primitive-name-ref='{}'] does not match any vnf-configuration:config-"
tierno59d22d22018-09-25 18:10:19 +02002499 "primitive".format(scaling_group, config_primitive))
tiernoda964822019-01-14 15:53:47 +00002500
tierno16fedf52019-05-24 08:38:26 +00002501 vnfr_params = {"VDU_SCALE_INFO": vdu_scaling_info}
tiernoda964822019-01-14 15:53:47 +00002502 if db_vnfr.get("additionalParamsForVnf"):
2503 vnfr_params.update(db_vnfr["additionalParamsForVnf"])
tierno9ab95942018-10-10 16:44:22 +02002504 scale_process = "VCA"
tiernod6de1992018-10-11 13:05:52 +02002505 db_nsr_update["config-status"] = "configuring pre-scaling"
kuuseac3a8882019-10-03 10:48:06 +02002506 primitive_params = self._map_primitive_params(config_primitive, {}, vnfr_params)
2507
2508 # Pre-scale reintent check: Check if this sub-operation has been executed before
2509 op_index = self._check_or_add_scale_suboperation(
2510 db_nslcmop, nslcmop_id, vnf_index, vnf_config_primitive, primitive_params, 'PRE-SCALE')
2511 if (op_index == self.SUBOPERATION_STATUS_SKIP):
2512 # Skip sub-operation
2513 result = 'COMPLETED'
2514 result_detail = 'Done'
2515 self.logger.debug(logging_text +
2516 "vnf_config_primitive={} Skipped sub-operation, result {} {}".format(
2517 vnf_config_primitive, result, result_detail))
2518 else:
2519 if (op_index == self.SUBOPERATION_STATUS_NEW):
2520 # New sub-operation: Get index of this sub-operation
2521 op_index = len(db_nslcmop.get('_admin', {}).get('operations')) - 1
2522 self.logger.debug(logging_text + "vnf_config_primitive={} New sub-operation".
2523 format(vnf_config_primitive))
2524 else:
2525 # Reintent: Get registered params for this existing sub-operation
2526 op = db_nslcmop.get('_admin', {}).get('operations', [])[op_index]
2527 vnf_index = op.get('member_vnf_index')
2528 vnf_config_primitive = op.get('primitive')
2529 primitive_params = op.get('primitive_params')
2530 self.logger.debug(logging_text + "vnf_config_primitive={} Sub-operation reintent".
2531 format(vnf_config_primitive))
2532 # Execute the primitive, either with new (first-time) or registered (reintent) args
2533 result, result_detail = await self._ns_execute_primitive(
2534 nsr_deployed, vnf_index, None, None, None, vnf_config_primitive, primitive_params)
2535 self.logger.debug(logging_text + "vnf_config_primitive={} Done with result {} {}".format(
2536 vnf_config_primitive, result, result_detail))
2537 # Update operationState = COMPLETED | FAILED
2538 self._update_suboperation_status(
2539 db_nslcmop, op_index, result, result_detail)
2540
tierno59d22d22018-09-25 18:10:19 +02002541 if result == "FAILED":
2542 raise LcmException(result_detail)
tiernod6de1992018-10-11 13:05:52 +02002543 db_nsr_update["config-status"] = old_config_status
2544 scale_process = None
kuuseac3a8882019-10-03 10:48:06 +02002545 # PRE-SCALE END
tierno59d22d22018-09-25 18:10:19 +02002546
kuuseac3a8882019-10-03 10:48:06 +02002547 # SCALE RO - BEGIN
2548 # Should this block be skipped if 'RO_nsr_id' == None ?
2549 # if (RO_nsr_id and RO_scaling_info):
tierno59d22d22018-09-25 18:10:19 +02002550 if RO_scaling_info:
tierno9ab95942018-10-10 16:44:22 +02002551 scale_process = "RO"
kuuseac3a8882019-10-03 10:48:06 +02002552 # Scale RO reintent check: Check if this sub-operation has been executed before
2553 op_index = self._check_or_add_scale_suboperation(
2554 db_nslcmop, vnf_index, None, None, 'SCALE-RO', RO_nsr_id, RO_scaling_info)
2555 if (op_index == self.SUBOPERATION_STATUS_SKIP):
2556 # Skip sub-operation
2557 result = 'COMPLETED'
2558 result_detail = 'Done'
2559 self.logger.debug(logging_text + "Skipped sub-operation RO, result {} {}".format(
2560 result, result_detail))
2561 else:
2562 if (op_index == self.SUBOPERATION_STATUS_NEW):
2563 # New sub-operation: Get index of this sub-operation
2564 op_index = len(db_nslcmop.get('_admin', {}).get('operations')) - 1
2565 self.logger.debug(logging_text + "New sub-operation RO")
tierno59d22d22018-09-25 18:10:19 +02002566 else:
kuuseac3a8882019-10-03 10:48:06 +02002567 # Reintent: Get registered params for this existing sub-operation
2568 op = db_nslcmop.get('_admin', {}).get('operations', [])[op_index]
2569 RO_nsr_id = op.get('RO_nsr_id')
2570 RO_scaling_info = op.get('RO_scaling_info')
2571 self.logger.debug(logging_text + "Sub-operation RO reintent".format(
2572 vnf_config_primitive))
2573
2574 RO_desc = await self.RO.create_action("ns", RO_nsr_id, {"vdu-scaling": RO_scaling_info})
2575 db_nsr_update["_admin.scaling-group.{}.nb-scale-op".format(admin_scale_index)] = nb_scale_op
2576 db_nsr_update["_admin.scaling-group.{}.time".format(admin_scale_index)] = time()
2577 # wait until ready
2578 RO_nslcmop_id = RO_desc["instance_action_id"]
2579 db_nslcmop_update["_admin.deploy.RO"] = RO_nslcmop_id
2580
2581 RO_task_done = False
2582 step = detailed_status = "Waiting RO_task_id={} to complete the scale action.".format(RO_nslcmop_id)
2583 detailed_status_old = None
2584 self.logger.debug(logging_text + step)
2585
2586 deployment_timeout = 1 * 3600 # One hour
2587 while deployment_timeout > 0:
2588 if not RO_task_done:
2589 desc = await self.RO.show("ns", item_id_name=RO_nsr_id, extra_item="action",
2590 extra_item_id=RO_nslcmop_id)
2591 ns_status, ns_status_info = self.RO.check_action_status(desc)
2592 if ns_status == "ERROR":
2593 raise ROclient.ROClientException(ns_status_info)
2594 elif ns_status == "BUILD":
2595 detailed_status = step + "; {}".format(ns_status_info)
2596 elif ns_status == "ACTIVE":
2597 RO_task_done = True
2598 step = detailed_status = "Waiting ns ready at RO. RO_id={}".format(RO_nsr_id)
2599 self.logger.debug(logging_text + step)
2600 else:
2601 assert False, "ROclient.check_action_status returns unknown {}".format(ns_status)
tierno59d22d22018-09-25 18:10:19 +02002602 else:
kuuseac3a8882019-10-03 10:48:06 +02002603 if ns_status == "ERROR":
2604 raise ROclient.ROClientException(ns_status_info)
2605 elif ns_status == "BUILD":
2606 detailed_status = step + "; {}".format(ns_status_info)
2607 elif ns_status == "ACTIVE":
2608 step = detailed_status = \
2609 "Waiting for management IP address reported by the VIM. Updating VNFRs"
2610 if not vnfr_scaled:
2611 self.scale_vnfr(db_vnfr, vdu_create=vdu_create, vdu_delete=vdu_delete)
2612 vnfr_scaled = True
2613 try:
2614 desc = await self.RO.show("ns", RO_nsr_id)
2615 # nsr_deployed["nsr_ip"] = RO.get_ns_vnf_info(desc)
2616 self.ns_update_vnfr({db_vnfr["member-vnf-index-ref"]: db_vnfr}, desc)
2617 break
2618 except LcmExceptionNoMgmtIP:
2619 pass
2620 else:
2621 assert False, "ROclient.check_ns_status returns unknown {}".format(ns_status)
2622 if detailed_status != detailed_status_old:
2623 self._update_suboperation_status(
2624 db_nslcmop, op_index, 'COMPLETED', detailed_status)
2625 detailed_status_old = db_nslcmop_update["detailed-status"] = detailed_status
2626 self.update_db_2("nslcmops", nslcmop_id, db_nslcmop_update)
tierno59d22d22018-09-25 18:10:19 +02002627
kuuseac3a8882019-10-03 10:48:06 +02002628 await asyncio.sleep(5, loop=self.loop)
2629 deployment_timeout -= 5
2630 if deployment_timeout <= 0:
2631 self._update_suboperation_status(
2632 db_nslcmop, nslcmop_id, op_index, 'FAILED', "Timeout when waiting for ns to get ready")
2633 raise ROclient.ROClientException("Timeout waiting ns to be ready")
tierno59d22d22018-09-25 18:10:19 +02002634
kuuseac3a8882019-10-03 10:48:06 +02002635 # update VDU_SCALING_INFO with the obtained ip_addresses
2636 if vdu_scaling_info["scaling_direction"] == "OUT":
2637 for vdur in reversed(db_vnfr["vdur"]):
2638 if vdu_scaling_info["vdu-create"].get(vdur["vdu-id-ref"]):
2639 vdu_scaling_info["vdu-create"][vdur["vdu-id-ref"]] -= 1
2640 vdu_scaling_info["vdu"].append({
2641 "name": vdur["name"],
2642 "vdu_id": vdur["vdu-id-ref"],
2643 "interface": []
tierno59d22d22018-09-25 18:10:19 +02002644 })
kuuseac3a8882019-10-03 10:48:06 +02002645 for interface in vdur["interfaces"]:
2646 vdu_scaling_info["vdu"][-1]["interface"].append({
2647 "name": interface["name"],
2648 "ip_address": interface["ip-address"],
2649 "mac_address": interface.get("mac-address"),
2650 })
2651 del vdu_scaling_info["vdu-create"]
2652
2653 self._update_suboperation_status(db_nslcmop, op_index, 'COMPLETED', 'Done')
2654 # SCALE RO - END
tierno59d22d22018-09-25 18:10:19 +02002655
tierno9ab95942018-10-10 16:44:22 +02002656 scale_process = None
tierno59d22d22018-09-25 18:10:19 +02002657 if db_nsr_update:
2658 self.update_db_2("nsrs", nsr_id, db_nsr_update)
2659
kuuseac3a8882019-10-03 10:48:06 +02002660 # POST-SCALE BEGIN
tierno59d22d22018-09-25 18:10:19 +02002661 # execute primitive service POST-SCALING
2662 step = "Executing post-scale vnf-config-primitive"
2663 if scaling_descriptor.get("scaling-config-action"):
2664 for scaling_config_action in scaling_descriptor["scaling-config-action"]:
kuuseac3a8882019-10-03 10:48:06 +02002665 if (scaling_config_action.get("trigger") == "post-scale-in" and scaling_type == "SCALE_IN") \
2666 or (scaling_config_action.get("trigger") == "post-scale-out" and scaling_type == "SCALE_OUT"):
tierno59d22d22018-09-25 18:10:19 +02002667 vnf_config_primitive = scaling_config_action["vnf-config-primitive-name-ref"]
2668 step = db_nslcmop_update["detailed-status"] = \
2669 "executing post-scale scaling-config-action '{}'".format(vnf_config_primitive)
tiernoda964822019-01-14 15:53:47 +00002670
tierno589befb2019-05-29 07:06:23 +00002671 vnfr_params = {"VDU_SCALE_INFO": vdu_scaling_info}
tiernoda964822019-01-14 15:53:47 +00002672 if db_vnfr.get("additionalParamsForVnf"):
2673 vnfr_params.update(db_vnfr["additionalParamsForVnf"])
2674
tierno59d22d22018-09-25 18:10:19 +02002675 # look for primitive
tierno59d22d22018-09-25 18:10:19 +02002676 for config_primitive in db_vnfd.get("vnf-configuration", {}).get("config-primitive", ()):
2677 if config_primitive["name"] == vnf_config_primitive:
tierno59d22d22018-09-25 18:10:19 +02002678 break
2679 else:
2680 raise LcmException("Invalid vnfd descriptor at scaling-group-descriptor[name='{}']:"
2681 "scaling-config-action[vnf-config-primitive-name-ref='{}'] does not "
tierno47e86b52018-10-10 14:05:55 +02002682 "match any vnf-configuration:config-primitive".format(scaling_group,
2683 config_primitive))
tierno9ab95942018-10-10 16:44:22 +02002684 scale_process = "VCA"
tiernod6de1992018-10-11 13:05:52 +02002685 db_nsr_update["config-status"] = "configuring post-scaling"
kuuseac3a8882019-10-03 10:48:06 +02002686 primitive_params = self._map_primitive_params(config_primitive, {}, vnfr_params)
tiernod6de1992018-10-11 13:05:52 +02002687
kuuseac3a8882019-10-03 10:48:06 +02002688 # Post-scale reintent check: Check if this sub-operation has been executed before
2689 op_index = self._check_or_add_scale_suboperation(
2690 db_nslcmop, nslcmop_id, vnf_index, vnf_config_primitive, primitive_params, 'POST-SCALE')
2691 if (op_index == self.SUBOPERATION_STATUS_SKIP):
2692 # Skip sub-operation
2693 result = 'COMPLETED'
2694 result_detail = 'Done'
2695 self.logger.debug(logging_text +
2696 "vnf_config_primitive={} Skipped sub-operation, result {} {}".
2697 format(vnf_config_primitive, result, result_detail))
2698 else:
2699 if (op_index == self.SUBOPERATION_STATUS_NEW):
2700 # New sub-operation: Get index of this sub-operation
2701 op_index = len(db_nslcmop.get('_admin', {}).get('operations')) - 1
2702 self.logger.debug(logging_text + "vnf_config_primitive={} New sub-operation".
2703 format(vnf_config_primitive))
2704 else:
2705 # Reintent: Get registered params for this existing sub-operation
2706 op = db_nslcmop.get('_admin', {}).get('operations', [])[op_index]
2707 vnf_index = op.get('member_vnf_index')
2708 vnf_config_primitive = op.get('primitive')
2709 primitive_params = op.get('primitive_params')
2710 self.logger.debug(logging_text + "vnf_config_primitive={} Sub-operation reintent".
2711 format(vnf_config_primitive))
2712 # Execute the primitive, either with new (first-time) or registered (reintent) args
2713 result, result_detail = await self._ns_execute_primitive(
2714 nsr_deployed, vnf_index, None, None, None, vnf_config_primitive, primitive_params)
2715 self.logger.debug(logging_text + "vnf_config_primitive={} Done with result {} {}".format(
2716 vnf_config_primitive, result, result_detail))
2717 # Update operationState = COMPLETED | FAILED
2718 self._update_suboperation_status(
2719 db_nslcmop, op_index, result, result_detail)
2720
tierno59d22d22018-09-25 18:10:19 +02002721 if result == "FAILED":
2722 raise LcmException(result_detail)
tiernod6de1992018-10-11 13:05:52 +02002723 db_nsr_update["config-status"] = old_config_status
2724 scale_process = None
kuuseac3a8882019-10-03 10:48:06 +02002725 # POST-SCALE END
tierno59d22d22018-09-25 18:10:19 +02002726
2727 db_nslcmop_update["operationState"] = nslcmop_operation_state = "COMPLETED"
2728 db_nslcmop_update["statusEnteredTime"] = time()
2729 db_nslcmop_update["detailed-status"] = "done"
tiernod6de1992018-10-11 13:05:52 +02002730 db_nsr_update["detailed-status"] = "" # "scaled {} {}".format(scaling_group, scaling_type)
ikalyvas02d9e7b2019-05-27 18:16:01 +03002731 db_nsr_update["operational-status"] = "running" if old_operational_status == "failed" \
2732 else old_operational_status
tiernod6de1992018-10-11 13:05:52 +02002733 db_nsr_update["config-status"] = old_config_status
tierno59d22d22018-09-25 18:10:19 +02002734 return
2735 except (ROclient.ROClientException, DbException, LcmException) as e:
2736 self.logger.error(logging_text + "Exit Exception {}".format(e))
2737 exc = e
2738 except asyncio.CancelledError:
2739 self.logger.error(logging_text + "Cancelled Exception while '{}'".format(step))
2740 exc = "Operation was cancelled"
2741 except Exception as e:
2742 exc = traceback.format_exc()
2743 self.logger.critical(logging_text + "Exit Exception {} {}".format(type(e).__name__, e), exc_info=True)
2744 finally:
2745 if exc:
2746 if db_nslcmop:
2747 db_nslcmop_update["detailed-status"] = "FAILED {}: {}".format(step, exc)
2748 db_nslcmop_update["operationState"] = nslcmop_operation_state = "FAILED"
2749 db_nslcmop_update["statusEnteredTime"] = time()
2750 if db_nsr:
tiernod6de1992018-10-11 13:05:52 +02002751 db_nsr_update["operational-status"] = old_operational_status
2752 db_nsr_update["config-status"] = old_config_status
2753 db_nsr_update["detailed-status"] = ""
tierno47e86b52018-10-10 14:05:55 +02002754 db_nsr_update["_admin.nslcmop"] = None
tiernod6de1992018-10-11 13:05:52 +02002755 if scale_process:
2756 if "VCA" in scale_process:
2757 db_nsr_update["config-status"] = "failed"
2758 if "RO" in scale_process:
2759 db_nsr_update["operational-status"] = "failed"
2760 db_nsr_update["detailed-status"] = "FAILED scaling nslcmop={} {}: {}".format(nslcmop_id, step,
2761 exc)
tiernobaa51102018-12-14 13:16:18 +00002762 try:
2763 if db_nslcmop and db_nslcmop_update:
2764 self.update_db_2("nslcmops", nslcmop_id, db_nslcmop_update)
2765 if db_nsr:
2766 db_nsr_update["_admin.nslcmop"] = None
2767 self.update_db_2("nsrs", nsr_id, db_nsr_update)
2768 except DbException as e:
2769 self.logger.error(logging_text + "Cannot update database: {}".format(e))
tierno59d22d22018-09-25 18:10:19 +02002770 if nslcmop_operation_state:
2771 try:
2772 await self.msg.aiowrite("ns", "scaled", {"nsr_id": nsr_id, "nslcmop_id": nslcmop_id,
tierno8a518872018-12-21 13:42:14 +00002773 "operationState": nslcmop_operation_state},
2774 loop=self.loop)
tierno59d22d22018-09-25 18:10:19 +02002775 # if cooldown_time:
2776 # await asyncio.sleep(cooldown_time)
2777 # await self.msg.aiowrite("ns","scaled-cooldown-time", {"nsr_id": nsr_id, "nslcmop_id": nslcmop_id})
2778 except Exception as e:
2779 self.logger.error(logging_text + "kafka_write notification Exception {}".format(e))
2780 self.logger.debug(logging_text + "Exit")
2781 self.lcm_tasks.remove("ns", nsr_id, nslcmop_id, "ns_scale")