| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | import asyncio |
| 5 | import yaml |
| 6 | import logging |
| 7 | import logging.handlers |
| 8 | import functools |
| 9 | import traceback |
| 10 | |
| 11 | import ROclient |
| tierno | f578e55 | 2018-11-08 19:07:20 +0100 | [diff] [blame] | 12 | from lcm_utils import LcmException, LcmExceptionNoMgmtIP, LcmBase |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 13 | |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 14 | from osm_common.dbbase import DbException |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 15 | from osm_common.fsbase import FsException |
| 16 | from n2vc.vnf import N2VC |
| 17 | |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 18 | from copy import copy, deepcopy |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 19 | from http import HTTPStatus |
| 20 | from time import time |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 21 | from uuid import uuid4 |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 22 | |
| 23 | __author__ = "Alfonso Tierno" |
| 24 | |
| 25 | |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 26 | def get_iterable(in_dict, in_key): |
| 27 | """ |
| 28 | Similar to <dict>.get(), but if value is None, False, ..., An empty tuple is returned instead |
| 29 | :param in_dict: a dictionary |
| 30 | :param in_key: the key to look for at in_dict |
| 31 | :return: in_dict[in_var] or () if it is None or not present |
| 32 | """ |
| 33 | if not in_dict.get(in_key): |
| 34 | return () |
| 35 | return in_dict[in_key] |
| 36 | |
| 37 | |
| 38 | def populate_dict(target_dict, key_list, value): |
| 39 | """ |
| 40 | Upate target_dict creating nested dictionaries with the key_list. Last key_list item is asigned the value. |
| 41 | Example target_dict={K: J}; key_list=[a,b,c]; target_dict will be {K: J, a: {b: {c: value}}} |
| 42 | :param target_dict: dictionary to be changed |
| 43 | :param key_list: list of keys to insert at target_dict |
| 44 | :param value: |
| 45 | :return: None |
| 46 | """ |
| 47 | for key in key_list[0:-1]: |
| 48 | if key not in target_dict: |
| 49 | target_dict[key] = {} |
| 50 | target_dict = target_dict[key] |
| 51 | target_dict[key_list[-1]] = value |
| 52 | |
| 53 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 54 | class NsLcm(LcmBase): |
| tierno | 63de62e | 2018-10-31 16:38:52 +0100 | [diff] [blame] | 55 | timeout_vca_on_error = 5 * 60 # Time for charm from first time at blocked,error status to mark as failed |
| 56 | total_deploy_timeout = 2 * 3600 # global timeout for deployment |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 57 | |
| 58 | def __init__(self, db, msg, fs, lcm_tasks, ro_config, vca_config, loop): |
| 59 | """ |
| 60 | Init, Connect to database, filesystem storage, and messaging |
| 61 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 62 | :return: None |
| 63 | """ |
| 64 | # logging |
| 65 | self.logger = logging.getLogger('lcm.ns') |
| 66 | self.loop = loop |
| 67 | self.lcm_tasks = lcm_tasks |
| 68 | |
| 69 | super().__init__(db, msg, fs, self.logger) |
| 70 | |
| 71 | self.ro_config = ro_config |
| 72 | |
| 73 | self.n2vc = N2VC( |
| 74 | log=self.logger, |
| 75 | server=vca_config['host'], |
| 76 | port=vca_config['port'], |
| 77 | user=vca_config['user'], |
| 78 | secret=vca_config['secret'], |
| 79 | # TODO: This should point to the base folder where charms are stored, |
| 80 | # if there is a common one (like object storage). Otherwise, leave |
| 81 | # it unset and pass it via DeployCharms |
| 82 | # artifacts=vca_config[''], |
| 83 | artifacts=None, |
| 84 | ) |
| 85 | |
| 86 | def vnfd2RO(self, vnfd, new_id=None): |
| 87 | """ |
| 88 | Converts creates a new vnfd descriptor for RO base on input OSM IM vnfd |
| 89 | :param vnfd: input vnfd |
| 90 | :param new_id: overrides vnf id if provided |
| 91 | :return: copy of vnfd |
| 92 | """ |
| 93 | ci_file = None |
| 94 | try: |
| 95 | vnfd_RO = deepcopy(vnfd) |
| 96 | vnfd_RO.pop("_id", None) |
| 97 | vnfd_RO.pop("_admin", None) |
| 98 | if new_id: |
| 99 | vnfd_RO["id"] = new_id |
| gcalvino | 911ff7d | 2018-11-13 17:19:32 +0100 | [diff] [blame] | 100 | for vdu in vnfd_RO.get("vdu", ()): |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 101 | if "cloud-init-file" in vdu: |
| 102 | base_folder = vnfd["_admin"]["storage"] |
| 103 | clout_init_file = "{}/{}/cloud_init/{}".format( |
| 104 | base_folder["folder"], |
| 105 | base_folder["pkg-dir"], |
| 106 | vdu["cloud-init-file"] |
| 107 | ) |
| 108 | ci_file = self.fs.file_open(clout_init_file, "r") |
| 109 | # TODO: detect if binary or text. Propose to read as binary and try to decode to utf8. If fails |
| 110 | # convert to base 64 or similar |
| 111 | clout_init_content = ci_file.read() |
| 112 | ci_file.close() |
| 113 | ci_file = None |
| 114 | vdu.pop("cloud-init-file", None) |
| 115 | vdu["cloud-init"] = clout_init_content |
| 116 | # remnove unused by RO configuration, monitoring, scaling |
| 117 | vnfd_RO.pop("vnf-configuration", None) |
| 118 | vnfd_RO.pop("monitoring-param", None) |
| 119 | vnfd_RO.pop("scaling-group-descriptor", None) |
| 120 | return vnfd_RO |
| 121 | except FsException as e: |
| 122 | raise LcmException("Error reading file at vnfd {}: {} ".format(vnfd["_id"], e)) |
| 123 | finally: |
| 124 | if ci_file: |
| 125 | ci_file.close() |
| 126 | |
| 127 | def n2vc_callback(self, model_name, application_name, status, message, n2vc_info, task=None): |
| 128 | """ |
| 129 | Callback both for charm status change and task completion |
| 130 | :param model_name: Charm model name |
| 131 | :param application_name: Charm application name |
| 132 | :param status: Can be |
| 133 | - blocked: The unit needs manual intervention |
| 134 | - maintenance: The unit is actively deploying/configuring |
| 135 | - waiting: The unit is waiting for another charm to be ready |
| 136 | - active: The unit is deployed, configured, and ready |
| 137 | - error: The charm has failed and needs attention. |
| 138 | - terminated: The charm has been destroyed |
| 139 | - removing, |
| 140 | - removed |
| 141 | :param message: detailed message error |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 142 | :param n2vc_info: dictionary with information shared with instantiate task. It contains: |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 143 | nsr_id: |
| 144 | nslcmop_id: |
| 145 | lcmOperationType: currently "instantiate" |
| 146 | deployed: dictionary with {<application>: {operational-status: <status>, detailed-status: <text>}} |
| 147 | db_update: dictionary to be filled with the changes to be wrote to database with format key.key.key: value |
| 148 | n2vc_event: event used to notify instantiation task that some change has been produced |
| 149 | :param task: None for charm status change, or task for completion task callback |
| 150 | :return: |
| 151 | """ |
| 152 | try: |
| 153 | nsr_id = n2vc_info["nsr_id"] |
| 154 | deployed = n2vc_info["deployed"] |
| 155 | db_nsr_update = n2vc_info["db_update"] |
| 156 | nslcmop_id = n2vc_info["nslcmop_id"] |
| 157 | ns_operation = n2vc_info["lcmOperationType"] |
| 158 | n2vc_event = n2vc_info["n2vc_event"] |
| 159 | logging_text = "Task ns={} {}={} [n2vc_callback] application={}".format(nsr_id, ns_operation, nslcmop_id, |
| 160 | application_name) |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 161 | for vca_index, vca_deployed in enumerate(deployed): |
| 162 | if not vca_deployed: |
| 163 | continue |
| 164 | if model_name == vca_deployed["model"] and application_name == vca_deployed["application"]: |
| 165 | break |
| 166 | else: |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 167 | self.logger.error(logging_text + " Not present at nsr._admin.deployed.VCA") |
| 168 | return |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 169 | if task: |
| 170 | if task.cancelled(): |
| 171 | self.logger.debug(logging_text + " task Cancelled") |
| 172 | vca_deployed['operational-status'] = "error" |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 173 | db_nsr_update["_admin.deployed.VCA.{}.operational-status".format(vca_index)] = "error" |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 174 | vca_deployed['detailed-status'] = "Task Cancelled" |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 175 | db_nsr_update["_admin.deployed.VCA.{}.detailed-status".format(vca_index)] = "Task Cancelled" |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 176 | |
| 177 | elif task.done(): |
| 178 | exc = task.exception() |
| 179 | if exc: |
| 180 | self.logger.error(logging_text + " task Exception={}".format(exc)) |
| 181 | vca_deployed['operational-status'] = "error" |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 182 | db_nsr_update["_admin.deployed.VCA.{}.operational-status".format(vca_index)] = "error" |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 183 | vca_deployed['detailed-status'] = str(exc) |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 184 | db_nsr_update["_admin.deployed.VCA.{}.detailed-status".format(vca_index)] = str(exc) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 185 | else: |
| 186 | self.logger.debug(logging_text + " task Done") |
| 187 | # task is Done, but callback is still ongoing. So ignore |
| 188 | return |
| 189 | elif status: |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 190 | self.logger.debug(logging_text + " Enter status={} message={}".format(status, message)) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 191 | if vca_deployed['operational-status'] == status: |
| 192 | return # same status, ignore |
| 193 | vca_deployed['operational-status'] = status |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 194 | db_nsr_update["_admin.deployed.VCA.{}.operational-status".format(vca_index)] = status |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 195 | vca_deployed['detailed-status'] = str(message) |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 196 | db_nsr_update["_admin.deployed.VCA.{}.detailed-status".format(vca_index)] = str(message) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 197 | else: |
| 198 | self.logger.critical(logging_text + " Enter with bad parameters", exc_info=True) |
| 199 | return |
| 200 | # wake up instantiate task |
| 201 | n2vc_event.set() |
| 202 | except Exception as e: |
| 203 | self.logger.critical(logging_text + " Exception {}".format(e), exc_info=True) |
| 204 | |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 205 | def ns_params_2_RO(self, ns_params, nsd, vnfd_dict, n2vc_key_list): |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 206 | """ |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 207 | Creates a RO ns descriptor from OSM ns_instantiate params |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 208 | :param ns_params: OSM instantiate params |
| 209 | :return: The RO ns descriptor |
| 210 | """ |
| 211 | vim_2_RO = {} |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 212 | # TODO feature 1417: Check that no instantiation is set over PDU |
| 213 | # check if PDU forces a concrete vim-network-id and add it |
| 214 | # check if PDU contains a SDN-assist info (dpid, switch, port) and pass it to RO |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 215 | |
| 216 | def vim_account_2_RO(vim_account): |
| 217 | if vim_account in vim_2_RO: |
| 218 | return vim_2_RO[vim_account] |
| 219 | |
| 220 | db_vim = self.db.get_one("vim_accounts", {"_id": vim_account}) |
| 221 | if db_vim["_admin"]["operationalState"] != "ENABLED": |
| 222 | raise LcmException("VIM={} is not available. operationalState={}".format( |
| 223 | vim_account, db_vim["_admin"]["operationalState"])) |
| 224 | RO_vim_id = db_vim["_admin"]["deployed"]["RO"] |
| 225 | vim_2_RO[vim_account] = RO_vim_id |
| 226 | return RO_vim_id |
| 227 | |
| 228 | def ip_profile_2_RO(ip_profile): |
| 229 | RO_ip_profile = deepcopy((ip_profile)) |
| 230 | if "dns-server" in RO_ip_profile: |
| 231 | if isinstance(RO_ip_profile["dns-server"], list): |
| 232 | RO_ip_profile["dns-address"] = [] |
| 233 | for ds in RO_ip_profile.pop("dns-server"): |
| 234 | RO_ip_profile["dns-address"].append(ds['address']) |
| 235 | else: |
| 236 | RO_ip_profile["dns-address"] = RO_ip_profile.pop("dns-server") |
| 237 | if RO_ip_profile.get("ip-version") == "ipv4": |
| 238 | RO_ip_profile["ip-version"] = "IPv4" |
| 239 | if RO_ip_profile.get("ip-version") == "ipv6": |
| 240 | RO_ip_profile["ip-version"] = "IPv6" |
| 241 | if "dhcp-params" in RO_ip_profile: |
| 242 | RO_ip_profile["dhcp"] = RO_ip_profile.pop("dhcp-params") |
| 243 | return RO_ip_profile |
| 244 | |
| 245 | if not ns_params: |
| 246 | return None |
| 247 | RO_ns_params = { |
| 248 | # "name": ns_params["nsName"], |
| 249 | # "description": ns_params.get("nsDescription"), |
| 250 | "datacenter": vim_account_2_RO(ns_params["vimAccountId"]), |
| 251 | # "scenario": ns_params["nsdId"], |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 252 | } |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 253 | if n2vc_key_list: |
| 254 | for vnfd_ref, vnfd in vnfd_dict.items(): |
| 255 | vdu_needed_access = [] |
| 256 | mgmt_cp = None |
| 257 | if vnfd.get("vnf-configuration"): |
| 258 | if vnfd.get("mgmt-interface"): |
| 259 | if vnfd["mgmt-interface"].get("vdu-id"): |
| 260 | vdu_needed_access.append(vnfd["mgmt-interface"]["vdu-id"]) |
| 261 | elif vnfd["mgmt-interface"].get("cp"): |
| 262 | mgmt_cp = vnfd["mgmt-interface"]["cp"] |
| 263 | |
| gcalvino | 911ff7d | 2018-11-13 17:19:32 +0100 | [diff] [blame] | 264 | for vdu in vnfd.get("vdu", ()): |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 265 | if vdu.get("vdu-configuration"): |
| 266 | vdu_needed_access.append(vdu["id"]) |
| 267 | elif mgmt_cp: |
| 268 | for vdu_interface in vdu.get("interface"): |
| 269 | if vdu_interface.get("external-connection-point-ref") and \ |
| 270 | vdu_interface["external-connection-point-ref"] == mgmt_cp: |
| 271 | vdu_needed_access.append(vdu["id"]) |
| 272 | mgmt_cp = None |
| 273 | break |
| 274 | |
| 275 | if vdu_needed_access: |
| 276 | for vnf_member in nsd.get("constituent-vnfd"): |
| 277 | if vnf_member["vnfd-id-ref"] != vnfd_ref: |
| 278 | continue |
| 279 | for vdu in vdu_needed_access: |
| 280 | populate_dict(RO_ns_params, |
| 281 | ("vnfs", vnf_member["member-vnf-index"], "vdus", vdu, "mgmt_keys"), |
| 282 | n2vc_key_list) |
| 283 | |
| tierno | 25ec773 | 2018-10-24 18:47:11 +0200 | [diff] [blame] | 284 | if ns_params.get("vduImage"): |
| 285 | RO_ns_params["vduImage"] = ns_params["vduImage"] |
| 286 | |
| tierno | c255a82 | 2018-10-31 09:41:53 +0100 | [diff] [blame] | 287 | if ns_params.get("ssh_keys"): |
| 288 | RO_ns_params["cloud-config"] = {"key-pairs": ns_params["ssh_keys"]} |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 289 | for vnf_params in get_iterable(ns_params, "vnf"): |
| 290 | for constituent_vnfd in nsd["constituent-vnfd"]: |
| 291 | if constituent_vnfd["member-vnf-index"] == vnf_params["member-vnf-index"]: |
| 292 | vnf_descriptor = vnfd_dict[constituent_vnfd["vnfd-id-ref"]] |
| 293 | break |
| 294 | else: |
| 295 | raise LcmException("Invalid instantiate parameter vnf:member-vnf-index={} is not present at nsd:" |
| 296 | "constituent-vnfd".format(vnf_params["member-vnf-index"])) |
| 297 | if vnf_params.get("vimAccountId"): |
| 298 | populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "datacenter"), |
| 299 | vim_account_2_RO(vnf_params["vimAccountId"])) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 300 | |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 301 | for vdu_params in get_iterable(vnf_params, "vdu"): |
| 302 | # TODO feature 1417: check that this VDU exist and it is not a PDU |
| 303 | if vdu_params.get("volume"): |
| 304 | for volume_params in vdu_params["volume"]: |
| 305 | if volume_params.get("vim-volume-id"): |
| 306 | populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "vdus", |
| 307 | vdu_params["id"], "devices", volume_params["name"], "vim_id"), |
| 308 | volume_params["vim-volume-id"]) |
| 309 | if vdu_params.get("interface"): |
| 310 | for interface_params in vdu_params["interface"]: |
| 311 | if interface_params.get("ip-address"): |
| 312 | populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "vdus", |
| 313 | vdu_params["id"], "interfaces", interface_params["name"], |
| 314 | "ip_address"), |
| 315 | interface_params["ip-address"]) |
| 316 | if interface_params.get("mac-address"): |
| 317 | populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "vdus", |
| 318 | vdu_params["id"], "interfaces", interface_params["name"], |
| 319 | "mac_address"), |
| 320 | interface_params["mac-address"]) |
| 321 | if interface_params.get("floating-ip-required"): |
| 322 | populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "vdus", |
| 323 | vdu_params["id"], "interfaces", interface_params["name"], |
| 324 | "floating-ip"), |
| 325 | interface_params["floating-ip-required"]) |
| 326 | |
| 327 | for internal_vld_params in get_iterable(vnf_params, "internal-vld"): |
| 328 | if internal_vld_params.get("vim-network-name"): |
| 329 | populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "networks", |
| 330 | internal_vld_params["name"], "vim-network-name"), |
| 331 | internal_vld_params["vim-network-name"]) |
| 332 | if internal_vld_params.get("ip-profile"): |
| 333 | populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "networks", |
| 334 | internal_vld_params["name"], "ip-profile"), |
| 335 | ip_profile_2_RO(internal_vld_params["ip-profile"])) |
| 336 | |
| 337 | for icp_params in get_iterable(internal_vld_params, "internal-connection-point"): |
| 338 | # look for interface |
| 339 | iface_found = False |
| 340 | for vdu_descriptor in vnf_descriptor["vdu"]: |
| 341 | for vdu_interface in vdu_descriptor["interface"]: |
| 342 | if vdu_interface.get("internal-connection-point-ref") == icp_params["id-ref"]: |
| 343 | if icp_params.get("ip-address"): |
| 344 | populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "vdus", |
| 345 | vdu_descriptor["id"], "interfaces", |
| 346 | vdu_interface["name"], "ip_address"), |
| 347 | icp_params["ip-address"]) |
| 348 | |
| 349 | if icp_params.get("mac-address"): |
| 350 | populate_dict(RO_ns_params, ("vnfs", vnf_params["member-vnf-index"], "vdus", |
| 351 | vdu_descriptor["id"], "interfaces", |
| 352 | vdu_interface["name"], "mac_address"), |
| 353 | icp_params["mac-address"]) |
| 354 | iface_found = True |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 355 | break |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 356 | if iface_found: |
| 357 | break |
| 358 | else: |
| 359 | raise LcmException("Invalid instantiate parameter vnf:member-vnf-index[{}]:" |
| 360 | "internal-vld:id-ref={} is not present at vnfd:internal-" |
| 361 | "connection-point".format(vnf_params["member-vnf-index"], |
| 362 | icp_params["id-ref"])) |
| 363 | |
| 364 | for vld_params in get_iterable(ns_params, "vld"): |
| 365 | if "ip-profile" in vld_params: |
| 366 | populate_dict(RO_ns_params, ("networks", vld_params["name"], "ip-profile"), |
| 367 | ip_profile_2_RO(vld_params["ip-profile"])) |
| 368 | if vld_params.get("vim-network-name"): |
| 369 | RO_vld_sites = [] |
| 370 | if isinstance(vld_params["vim-network-name"], dict): |
| 371 | for vim_account, vim_net in vld_params["vim-network-name"].items(): |
| 372 | RO_vld_sites.append({ |
| 373 | "netmap-use": vim_net, |
| 374 | "datacenter": vim_account_2_RO(vim_account) |
| 375 | }) |
| 376 | else: # isinstance str |
| 377 | RO_vld_sites.append({"netmap-use": vld_params["vim-network-name"]}) |
| 378 | if RO_vld_sites: |
| 379 | populate_dict(RO_ns_params, ("networks", vld_params["name"], "sites"), RO_vld_sites) |
| 380 | if "vnfd-connection-point-ref" in vld_params: |
| 381 | for cp_params in vld_params["vnfd-connection-point-ref"]: |
| 382 | # look for interface |
| 383 | for constituent_vnfd in nsd["constituent-vnfd"]: |
| 384 | if constituent_vnfd["member-vnf-index"] == cp_params["member-vnf-index-ref"]: |
| 385 | vnf_descriptor = vnfd_dict[constituent_vnfd["vnfd-id-ref"]] |
| 386 | break |
| 387 | else: |
| 388 | raise LcmException( |
| 389 | "Invalid instantiate parameter vld:vnfd-connection-point-ref:member-vnf-index-ref={} " |
| 390 | "is not present at nsd:constituent-vnfd".format(cp_params["member-vnf-index-ref"])) |
| 391 | match_cp = False |
| 392 | for vdu_descriptor in vnf_descriptor["vdu"]: |
| 393 | for interface_descriptor in vdu_descriptor["interface"]: |
| 394 | if interface_descriptor.get("external-connection-point-ref") == \ |
| 395 | cp_params["vnfd-connection-point-ref"]: |
| 396 | match_cp = True |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 397 | break |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 398 | if match_cp: |
| 399 | break |
| 400 | else: |
| 401 | raise LcmException( |
| 402 | "Invalid instantiate parameter vld:vnfd-connection-point-ref:member-vnf-index-ref={}:" |
| 403 | "vnfd-connection-point-ref={} is not present at vnfd={}".format( |
| 404 | cp_params["member-vnf-index-ref"], |
| 405 | cp_params["vnfd-connection-point-ref"], |
| 406 | vnf_descriptor["id"])) |
| 407 | if cp_params.get("ip-address"): |
| 408 | populate_dict(RO_ns_params, ("vnfs", cp_params["member-vnf-index-ref"], "vdus", |
| 409 | vdu_descriptor["id"], "interfaces", |
| 410 | interface_descriptor["name"], "ip_address"), |
| 411 | cp_params["ip-address"]) |
| 412 | if cp_params.get("mac-address"): |
| 413 | populate_dict(RO_ns_params, ("vnfs", cp_params["member-vnf-index-ref"], "vdus", |
| 414 | vdu_descriptor["id"], "interfaces", |
| 415 | interface_descriptor["name"], "mac_address"), |
| 416 | cp_params["mac-address"]) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 417 | return RO_ns_params |
| 418 | |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 419 | def scale_vnfr(self, db_vnfr, vdu_create=None, vdu_delete=None): |
| 420 | # make a copy to do not change |
| 421 | vdu_create = copy(vdu_create) |
| 422 | vdu_delete = copy(vdu_delete) |
| 423 | |
| 424 | vdurs = db_vnfr.get("vdur") |
| 425 | if vdurs is None: |
| 426 | vdurs = [] |
| 427 | vdu_index = len(vdurs) |
| 428 | while vdu_index: |
| 429 | vdu_index -= 1 |
| 430 | vdur = vdurs[vdu_index] |
| 431 | if vdur.get("pdu-type"): |
| 432 | continue |
| 433 | vdu_id_ref = vdur["vdu-id-ref"] |
| 434 | if vdu_create and vdu_create.get(vdu_id_ref): |
| 435 | for index in range(0, vdu_create[vdu_id_ref]): |
| 436 | vdur = deepcopy(vdur) |
| 437 | vdur["_id"] = str(uuid4()) |
| 438 | vdur["count-index"] += 1 |
| 439 | vdurs.insert(vdu_index+1+index, vdur) |
| 440 | del vdu_create[vdu_id_ref] |
| 441 | if vdu_delete and vdu_delete.get(vdu_id_ref): |
| 442 | del vdurs[vdu_index] |
| 443 | vdu_delete[vdu_id_ref] -= 1 |
| 444 | if not vdu_delete[vdu_id_ref]: |
| 445 | del vdu_delete[vdu_id_ref] |
| 446 | # check all operations are done |
| 447 | if vdu_create or vdu_delete: |
| 448 | raise LcmException("Error scaling OUT VNFR for {}. There is not any existing vnfr. Scaled to 0?".format( |
| 449 | vdu_create)) |
| 450 | if vdu_delete: |
| 451 | raise LcmException("Error scaling IN VNFR for {}. There is not any existing vnfr. Scaled to 0?".format( |
| 452 | vdu_delete)) |
| 453 | |
| 454 | vnfr_update = {"vdur": vdurs} |
| 455 | db_vnfr["vdur"] = vdurs |
| 456 | self.update_db_2("vnfrs", db_vnfr["_id"], vnfr_update) |
| 457 | |
| tierno | f578e55 | 2018-11-08 19:07:20 +0100 | [diff] [blame] | 458 | def ns_update_nsr(self, ns_update_nsr, db_nsr, nsr_desc_RO): |
| 459 | """ |
| 460 | Updates database nsr with the RO info for the created vld |
| 461 | :param ns_update_nsr: dictionary to be filled with the updated info |
| 462 | :param db_nsr: content of db_nsr. This is also modified |
| 463 | :param nsr_desc_RO: nsr descriptor from RO |
| 464 | :return: Nothing, LcmException is raised on errors |
| 465 | """ |
| 466 | |
| 467 | for vld_index, vld in enumerate(get_iterable(db_nsr, "vld")): |
| 468 | for net_RO in get_iterable(nsr_desc_RO, "nets"): |
| 469 | if vld["id"] != net_RO.get("ns_net_osm_id"): |
| 470 | continue |
| 471 | vld["vim-id"] = net_RO.get("vim_net_id") |
| 472 | vld["name"] = net_RO.get("vim_name") |
| 473 | vld["status"] = net_RO.get("status") |
| 474 | vld["status-detailed"] = net_RO.get("error_msg") |
| 475 | ns_update_nsr["vld.{}".format(vld_index)] = vld |
| 476 | break |
| 477 | else: |
| 478 | raise LcmException("ns_update_nsr: Not found vld={} at RO info".format(vld["id"])) |
| 479 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 480 | def ns_update_vnfr(self, db_vnfrs, nsr_desc_RO): |
| 481 | """ |
| 482 | Updates database vnfr with the RO info, e.g. ip_address, vim_id... Descriptor db_vnfrs is also updated |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 483 | :param db_vnfrs: dictionary with member-vnf-index: vnfr-content |
| 484 | :param nsr_desc_RO: nsr descriptor from RO |
| 485 | :return: Nothing, LcmException is raised on errors |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 486 | """ |
| 487 | for vnf_index, db_vnfr in db_vnfrs.items(): |
| 488 | for vnf_RO in nsr_desc_RO["vnfs"]: |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 489 | if vnf_RO["member_vnf_index"] != vnf_index: |
| 490 | continue |
| 491 | vnfr_update = {} |
| tierno | f578e55 | 2018-11-08 19:07:20 +0100 | [diff] [blame] | 492 | if vnf_RO.get("ip_address"): |
| 493 | db_vnfr["ip-address"] = vnfr_update["ip-address"] = vnf_RO["ip_address"] |
| 494 | elif not db_vnfr.get("ip-address"): |
| 495 | raise LcmExceptionNoMgmtIP("ns member_vnf_index '{}' has no IP address".format(vnf_index)) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 496 | |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 497 | for vdu_index, vdur in enumerate(get_iterable(db_vnfr, "vdur")): |
| 498 | vdur_RO_count_index = 0 |
| 499 | if vdur.get("pdu-type"): |
| 500 | continue |
| 501 | for vdur_RO in get_iterable(vnf_RO, "vms"): |
| 502 | if vdur["vdu-id-ref"] != vdur_RO["vdu_osm_id"]: |
| 503 | continue |
| 504 | if vdur["count-index"] != vdur_RO_count_index: |
| 505 | vdur_RO_count_index += 1 |
| 506 | continue |
| 507 | vdur["vim-id"] = vdur_RO.get("vim_vm_id") |
| 508 | vdur["ip-address"] = vdur_RO.get("ip_address") |
| 509 | vdur["vdu-id-ref"] = vdur_RO.get("vdu_osm_id") |
| 510 | vdur["name"] = vdur_RO.get("vim_name") |
| 511 | vdur["status"] = vdur_RO.get("status") |
| 512 | vdur["status-detailed"] = vdur_RO.get("error_msg") |
| 513 | for ifacer in get_iterable(vdur, "interfaces"): |
| 514 | for interface_RO in get_iterable(vdur_RO, "interfaces"): |
| 515 | if ifacer["name"] == interface_RO.get("internal_name"): |
| 516 | ifacer["ip-address"] = interface_RO.get("ip_address") |
| 517 | ifacer["mac-address"] = interface_RO.get("mac_address") |
| 518 | break |
| 519 | else: |
| 520 | raise LcmException("ns_update_vnfr: Not found member_vnf_index={} vdur={} interface={} " |
| 521 | "at RO info".format(vnf_index, vdur["vdu-id-ref"], ifacer["name"])) |
| 522 | vnfr_update["vdur.{}".format(vdu_index)] = vdur |
| 523 | break |
| 524 | else: |
| 525 | raise LcmException("ns_update_vnfr: Not found member_vnf_index={} vdur={} count_index={} at " |
| 526 | "RO info".format(vnf_index, vdur["vdu-id-ref"], vdur["count-index"])) |
| tierno | f578e55 | 2018-11-08 19:07:20 +0100 | [diff] [blame] | 527 | |
| 528 | for vld_index, vld in enumerate(get_iterable(db_vnfr, "vld")): |
| 529 | for net_RO in get_iterable(nsr_desc_RO, "nets"): |
| 530 | if vld["id"] != net_RO.get("vnf_net_osm_id"): |
| 531 | continue |
| 532 | vld["vim-id"] = net_RO.get("vim_net_id") |
| 533 | vld["name"] = net_RO.get("vim_name") |
| 534 | vld["status"] = net_RO.get("status") |
| 535 | vld["status-detailed"] = net_RO.get("error_msg") |
| 536 | vnfr_update["vld.{}".format(vld_index)] = vld |
| 537 | break |
| 538 | else: |
| 539 | raise LcmException("ns_update_vnfr: Not found member_vnf_index={} vld={} at RO info".format( |
| 540 | vnf_index, vld["id"])) |
| 541 | |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 542 | self.update_db_2("vnfrs", db_vnfr["_id"], vnfr_update) |
| 543 | break |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 544 | |
| 545 | else: |
| 546 | raise LcmException("ns_update_vnfr: Not found member_vnf_index={} at RO info".format(vnf_index)) |
| 547 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 548 | async def instantiate(self, nsr_id, nslcmop_id): |
| 549 | logging_text = "Task ns={} instantiate={} ".format(nsr_id, nslcmop_id) |
| 550 | self.logger.debug(logging_text + "Enter") |
| 551 | # get all needed from database |
| tierno | 63de62e | 2018-10-31 16:38:52 +0100 | [diff] [blame] | 552 | start_deploy = time() |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 553 | db_nsr = None |
| 554 | db_nslcmop = None |
| tierno | 47e86b5 | 2018-10-10 14:05:55 +0200 | [diff] [blame] | 555 | db_nsr_update = {"_admin.nslcmop": nslcmop_id} |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 556 | db_nslcmop_update = {} |
| 557 | nslcmop_operation_state = None |
| 558 | db_vnfrs = {} |
| 559 | RO_descriptor_number = 0 # number of descriptors created at RO |
| 560 | descriptor_id_2_RO = {} # map between vnfd/nsd id to the id used at RO |
| 561 | n2vc_info = {} |
| 562 | exc = None |
| 563 | try: |
| 564 | step = "Getting nslcmop={} from db".format(nslcmop_id) |
| 565 | db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id}) |
| 566 | step = "Getting nsr={} from db".format(nsr_id) |
| 567 | db_nsr = self.db.get_one("nsrs", {"_id": nsr_id}) |
| tierno | f578e55 | 2018-11-08 19:07:20 +0100 | [diff] [blame] | 568 | ns_params = db_nslcmop.get("operationParams") |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 569 | nsd = db_nsr["nsd"] |
| 570 | nsr_name = db_nsr["name"] # TODO short-name?? |
| tierno | 47e86b5 | 2018-10-10 14:05:55 +0200 | [diff] [blame] | 571 | |
| 572 | # look if previous tasks in process |
| 573 | task_name, task_dependency = self.lcm_tasks.lookfor_related("ns", nsr_id, nslcmop_id) |
| 574 | if task_dependency: |
| 575 | step = db_nslcmop_update["detailed-status"] = \ |
| 576 | "Waiting for related tasks to be completed: {}".format(task_name) |
| 577 | self.logger.debug(logging_text + step) |
| 578 | self.update_db_2("nslcmops", nslcmop_id, db_nslcmop_update) |
| 579 | _, pending = await asyncio.wait(task_dependency, timeout=3600) |
| 580 | if pending: |
| 581 | raise LcmException("Timeout waiting related tasks to be completed") |
| 582 | |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 583 | step = "Getting vnfrs from db" |
| 584 | db_vnfrs_list = self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id}) |
| 585 | db_vnfds_ref = {} |
| 586 | db_vnfds = {} |
| 587 | for vnfr in db_vnfrs_list: |
| 588 | db_vnfrs[vnfr["member-vnf-index-ref"]] = vnfr |
| 589 | vnfd_id = vnfr["vnfd-id"] |
| 590 | vnfd_ref = vnfr["vnfd-ref"] |
| 591 | if vnfd_id not in db_vnfds: |
| 592 | step = "Getting vnfd={} id='{}' from db".format(vnfd_id, vnfd_ref) |
| 593 | vnfd = self.db.get_one("vnfds", {"_id": vnfd_id}) |
| 594 | db_vnfds_ref[vnfd_ref] = vnfd |
| 595 | db_vnfds[vnfd_id] = vnfd |
| 596 | |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 597 | # Get or generates the _admin.deployed,VCA list |
| 598 | vca_deployed_list = None |
| 599 | if db_nsr["_admin"].get("deployed"): |
| 600 | vca_deployed_list = db_nsr["_admin"]["deployed"].get("VCA") |
| 601 | if vca_deployed_list is None: |
| 602 | vca_deployed_list = [] |
| 603 | db_nsr_update["_admin.deployed.VCA"] = vca_deployed_list |
| 604 | elif isinstance(vca_deployed_list, dict): |
| 605 | # maintain backward compatibility. Change a dict to list at database |
| 606 | vca_deployed_list = list(vca_deployed_list.values()) |
| 607 | db_nsr_update["_admin.deployed.VCA"] = vca_deployed_list |
| 608 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 609 | db_nsr_update["detailed-status"] = "creating" |
| 610 | db_nsr_update["operational-status"] = "init" |
| 611 | |
| 612 | RO = ROclient.ROClient(self.loop, **self.ro_config) |
| 613 | |
| 614 | # get vnfds, instantiate at RO |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 615 | for vnfd_id, vnfd in db_vnfds.items(): |
| 616 | vnfd_ref = vnfd["id"] |
| 617 | step = db_nsr_update["detailed-status"] = "Creating vnfd={} at RO".format(vnfd_ref) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 618 | # self.logger.debug(logging_text + step) |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 619 | vnfd_id_RO = "{}.{}.{}".format(nsr_id, RO_descriptor_number, vnfd_ref[:23]) |
| 620 | descriptor_id_2_RO[vnfd_ref] = vnfd_id_RO |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 621 | RO_descriptor_number += 1 |
| 622 | |
| 623 | # look if present |
| 624 | vnfd_list = await RO.get_list("vnfd", filter_by={"osm_id": vnfd_id_RO}) |
| 625 | if vnfd_list: |
| 626 | db_nsr_update["_admin.deployed.RO.vnfd_id.{}".format(vnfd_id)] = vnfd_list[0]["uuid"] |
| 627 | self.logger.debug(logging_text + "vnfd={} exists at RO. Using RO_id={}".format( |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 628 | vnfd_ref, vnfd_list[0]["uuid"])) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 629 | else: |
| 630 | vnfd_RO = self.vnfd2RO(vnfd, vnfd_id_RO) |
| 631 | desc = await RO.create("vnfd", descriptor=vnfd_RO) |
| 632 | db_nsr_update["_admin.deployed.RO.vnfd_id.{}".format(vnfd_id)] = desc["uuid"] |
| 633 | db_nsr_update["_admin.nsState"] = "INSTANTIATED" |
| 634 | self.logger.debug(logging_text + "vnfd={} created at RO. RO_id={}".format( |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 635 | vnfd_ref, desc["uuid"])) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 636 | self.update_db_2("nsrs", nsr_id, db_nsr_update) |
| 637 | |
| 638 | # create nsd at RO |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 639 | nsd_ref = nsd["id"] |
| 640 | step = db_nsr_update["detailed-status"] = "Creating nsd={} at RO".format(nsd_ref) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 641 | # self.logger.debug(logging_text + step) |
| 642 | |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 643 | RO_osm_nsd_id = "{}.{}.{}".format(nsr_id, RO_descriptor_number, nsd_ref[:23]) |
| 644 | descriptor_id_2_RO[nsd_ref] = RO_osm_nsd_id |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 645 | RO_descriptor_number += 1 |
| 646 | nsd_list = await RO.get_list("nsd", filter_by={"osm_id": RO_osm_nsd_id}) |
| 647 | if nsd_list: |
| 648 | db_nsr_update["_admin.deployed.RO.nsd_id"] = RO_nsd_uuid = nsd_list[0]["uuid"] |
| 649 | self.logger.debug(logging_text + "nsd={} exists at RO. Using RO_id={}".format( |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 650 | nsd_ref, RO_nsd_uuid)) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 651 | else: |
| 652 | nsd_RO = deepcopy(nsd) |
| 653 | nsd_RO["id"] = RO_osm_nsd_id |
| 654 | nsd_RO.pop("_id", None) |
| 655 | nsd_RO.pop("_admin", None) |
| gcalvino | ea0cc0a | 2018-11-06 13:20:29 +0100 | [diff] [blame] | 656 | for c_vnf in nsd_RO.get("constituent-vnfd", ()): |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 657 | vnfd_id = c_vnf["vnfd-id-ref"] |
| 658 | c_vnf["vnfd-id-ref"] = descriptor_id_2_RO[vnfd_id] |
| 659 | desc = await RO.create("nsd", descriptor=nsd_RO) |
| 660 | db_nsr_update["_admin.nsState"] = "INSTANTIATED" |
| 661 | db_nsr_update["_admin.deployed.RO.nsd_id"] = RO_nsd_uuid = desc["uuid"] |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 662 | self.logger.debug(logging_text + "nsd={} created at RO. RO_id={}".format(nsd_ref, RO_nsd_uuid)) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 663 | self.update_db_2("nsrs", nsr_id, db_nsr_update) |
| 664 | |
| 665 | # Crate ns at RO |
| 666 | # if present use it unless in error status |
| 667 | RO_nsr_id = db_nsr["_admin"].get("deployed", {}).get("RO", {}).get("nsr_id") |
| 668 | if RO_nsr_id: |
| 669 | try: |
| 670 | step = db_nsr_update["detailed-status"] = "Looking for existing ns at RO" |
| 671 | # self.logger.debug(logging_text + step + " RO_ns_id={}".format(RO_nsr_id)) |
| 672 | desc = await RO.show("ns", RO_nsr_id) |
| 673 | except ROclient.ROClientException as e: |
| 674 | if e.http_code != HTTPStatus.NOT_FOUND: |
| 675 | raise |
| 676 | RO_nsr_id = db_nsr_update["_admin.deployed.RO.nsr_id"] = None |
| 677 | if RO_nsr_id: |
| 678 | ns_status, ns_status_info = RO.check_ns_status(desc) |
| 679 | db_nsr_update["_admin.deployed.RO.nsr_status"] = ns_status |
| 680 | if ns_status == "ERROR": |
| 681 | step = db_nsr_update["detailed-status"] = "Deleting ns at RO. RO_ns_id={}".format(RO_nsr_id) |
| 682 | self.logger.debug(logging_text + step) |
| 683 | await RO.delete("ns", RO_nsr_id) |
| 684 | RO_nsr_id = db_nsr_update["_admin.deployed.RO.nsr_id"] = None |
| 685 | if not RO_nsr_id: |
| 686 | step = db_nsr_update["detailed-status"] = "Checking dependencies" |
| 687 | # self.logger.debug(logging_text + step) |
| 688 | |
| 689 | # check if VIM is creating and wait look if previous tasks in process |
| 690 | task_name, task_dependency = self.lcm_tasks.lookfor_related("vim_account", ns_params["vimAccountId"]) |
| 691 | if task_dependency: |
| 692 | step = "Waiting for related tasks to be completed: {}".format(task_name) |
| 693 | self.logger.debug(logging_text + step) |
| 694 | await asyncio.wait(task_dependency, timeout=3600) |
| 695 | if ns_params.get("vnf"): |
| 696 | for vnf in ns_params["vnf"]: |
| 697 | if "vimAccountId" in vnf: |
| 698 | task_name, task_dependency = self.lcm_tasks.lookfor_related("vim_account", |
| 699 | vnf["vimAccountId"]) |
| 700 | if task_dependency: |
| 701 | step = "Waiting for related tasks to be completed: {}".format(task_name) |
| 702 | self.logger.debug(logging_text + step) |
| 703 | await asyncio.wait(task_dependency, timeout=3600) |
| 704 | |
| 705 | step = db_nsr_update["detailed-status"] = "Checking instantiation parameters" |
| tierno | 25ec773 | 2018-10-24 18:47:11 +0200 | [diff] [blame] | 706 | |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 707 | # feature 1429. Add n2vc public key to needed VMs |
| tierno | 25ec773 | 2018-10-24 18:47:11 +0200 | [diff] [blame] | 708 | n2vc_key = await self.n2vc.GetPublicKey() |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 709 | RO_ns_params = self.ns_params_2_RO(ns_params, nsd, db_vnfds_ref, [n2vc_key]) |
| tierno | 25ec773 | 2018-10-24 18:47:11 +0200 | [diff] [blame] | 710 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 711 | step = db_nsr_update["detailed-status"] = "Creating ns at RO" |
| 712 | desc = await RO.create("ns", descriptor=RO_ns_params, |
| 713 | name=db_nsr["name"], |
| 714 | scenario=RO_nsd_uuid) |
| 715 | RO_nsr_id = db_nsr_update["_admin.deployed.RO.nsr_id"] = desc["uuid"] |
| 716 | db_nsr_update["_admin.nsState"] = "INSTANTIATED" |
| 717 | db_nsr_update["_admin.deployed.RO.nsr_status"] = "BUILD" |
| 718 | self.logger.debug(logging_text + "ns created at RO. RO_id={}".format(desc["uuid"])) |
| 719 | self.update_db_2("nsrs", nsr_id, db_nsr_update) |
| 720 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 721 | # wait until NS is ready |
| 722 | step = ns_status_detailed = detailed_status = "Waiting ns ready at RO. RO_id={}".format(RO_nsr_id) |
| 723 | detailed_status_old = None |
| 724 | self.logger.debug(logging_text + step) |
| 725 | |
| tierno | 63de62e | 2018-10-31 16:38:52 +0100 | [diff] [blame] | 726 | while time() <= start_deploy + self.total_deploy_timeout: |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 727 | desc = await RO.show("ns", RO_nsr_id) |
| 728 | ns_status, ns_status_info = RO.check_ns_status(desc) |
| 729 | db_nsr_update["admin.deployed.RO.nsr_status"] = ns_status |
| 730 | if ns_status == "ERROR": |
| 731 | raise ROclient.ROClientException(ns_status_info) |
| 732 | elif ns_status == "BUILD": |
| 733 | detailed_status = ns_status_detailed + "; {}".format(ns_status_info) |
| 734 | elif ns_status == "ACTIVE": |
| tierno | f578e55 | 2018-11-08 19:07:20 +0100 | [diff] [blame] | 735 | step = detailed_status = "Waiting for management IP address reported by the VIM. Updating VNFRs" |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 736 | try: |
| tierno | f578e55 | 2018-11-08 19:07:20 +0100 | [diff] [blame] | 737 | self.ns_update_vnfr(db_vnfrs, desc) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 738 | break |
| tierno | f578e55 | 2018-11-08 19:07:20 +0100 | [diff] [blame] | 739 | except LcmExceptionNoMgmtIP: |
| 740 | pass |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 741 | else: |
| 742 | assert False, "ROclient.check_ns_status returns unknown {}".format(ns_status) |
| 743 | if detailed_status != detailed_status_old: |
| 744 | detailed_status_old = db_nsr_update["detailed-status"] = detailed_status |
| 745 | self.update_db_2("nsrs", nsr_id, db_nsr_update) |
| 746 | await asyncio.sleep(5, loop=self.loop) |
| tierno | 63de62e | 2018-10-31 16:38:52 +0100 | [diff] [blame] | 747 | else: # total_deploy_timeout |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 748 | raise ROclient.ROClientException("Timeout waiting ns to be ready") |
| 749 | |
| tierno | f578e55 | 2018-11-08 19:07:20 +0100 | [diff] [blame] | 750 | step = "Updating NSR" |
| 751 | self.ns_update_nsr(db_nsr_update, db_nsr, desc) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 752 | |
| 753 | db_nsr["detailed-status"] = "Configuring vnfr" |
| 754 | self.update_db_2("nsrs", nsr_id, db_nsr_update) |
| 755 | |
| 756 | # The parameters we'll need to deploy a charm |
| 757 | number_to_configure = 0 |
| 758 | |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 759 | def deploy_charm(vnf_index, vdu_id, vdu_name, vdu_count_index, mgmt_ip_address, n2vc_info, |
| 760 | config_primitive=None): |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 761 | """An inner function to deploy the charm from either vnf or vdu |
| 762 | vnf_index is mandatory. vdu_id can be None for a vnf configuration or the id for vdu configuration |
| 763 | """ |
| 764 | if not mgmt_ip_address: |
| 765 | raise LcmException("vnfd/vdu has not management ip address to configure it") |
| 766 | # Login to the VCA. |
| 767 | # if number_to_configure == 0: |
| 768 | # self.logger.debug("Logging into N2VC...") |
| 769 | # task = asyncio.ensure_future(self.n2vc.login()) |
| 770 | # yield from asyncio.wait_for(task, 30.0) |
| 771 | # self.logger.debug("Logged into N2VC!") |
| 772 | |
| 773 | # # await self.n2vc.login() |
| 774 | |
| 775 | # Note: The charm needs to exist on disk at the location |
| 776 | # specified by charm_path. |
| 777 | base_folder = vnfd["_admin"]["storage"] |
| 778 | storage_params = self.fs.get_params() |
| 779 | charm_path = "{}{}/{}/charms/{}".format( |
| 780 | storage_params["path"], |
| 781 | base_folder["folder"], |
| 782 | base_folder["pkg-dir"], |
| 783 | proxy_charm |
| 784 | ) |
| 785 | |
| 786 | # Setup the runtime parameters for this VNF |
| 787 | params = {'rw_mgmt_ip': mgmt_ip_address} |
| 788 | if config_primitive: |
| 789 | params["initial-config-primitive"] = config_primitive |
| 790 | |
| 791 | # ns_name will be ignored in the current version of N2VC |
| 792 | # but will be implemented for the next point release. |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 793 | model_name = 'default' # TODO bug 581 : change to nsr_id |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 794 | if vdu_id: |
| 795 | vdu_id_text = vdu_id |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 796 | else: |
| 797 | vdu_id_text = "vnfd" # TODO bug 581 remove and add just an empty string "" |
| 798 | application_name = self.n2vc.FormatApplicationName(nsr_name, vnf_index, vdu_id_text) |
| 799 | # TODO bug 581 Add "-" as a final argument |
| 800 | |
| 801 | vca_index = len(vca_deployed_list) |
| 802 | # trunk name and add two char index at the end to ensure that it is unique. It is assumed no more than |
| 803 | # 26*26 charm in the same NS |
| 804 | # TODO bug 581 uncoment |
| 805 | # application_name = application_name[0:48] |
| 806 | # application_name += chr(97 + vca_index / 26) + chr(97 + vca_index % 26) |
| 807 | vca_deployed_ = { |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 808 | "member-vnf-index": vnf_index, |
| 809 | "vdu_id": vdu_id, |
| 810 | "model": model_name, |
| 811 | "application": application_name, |
| 812 | "operational-status": "init", |
| 813 | "detailed-status": "", |
| 814 | "vnfd_id": vnfd_id, |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 815 | "vdu_name": vdu_name, |
| 816 | "vdu_count_index": vdu_count_index, |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 817 | } |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 818 | vca_deployed_list.append(vca_deployed_) |
| 819 | db_nsr_update["_admin.deployed.VCA.{}".format(vca_index)] = vca_deployed_ |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 820 | self.update_db_2("nsrs", nsr_id, db_nsr_update) |
| 821 | |
| 822 | self.logger.debug("Task create_ns={} Passing artifacts path '{}' for {}".format(nsr_id, charm_path, |
| 823 | proxy_charm)) |
| 824 | if not n2vc_info: |
| 825 | n2vc_info["nsr_id"] = nsr_id |
| 826 | n2vc_info["nslcmop_id"] = nslcmop_id |
| 827 | n2vc_info["n2vc_event"] = asyncio.Event(loop=self.loop) |
| 828 | n2vc_info["lcmOperationType"] = "instantiate" |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 829 | n2vc_info["deployed"] = vca_deployed_list |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 830 | n2vc_info["db_update"] = db_nsr_update |
| 831 | task = asyncio.ensure_future( |
| 832 | self.n2vc.DeployCharms( |
| 833 | model_name, # The network service name |
| 834 | application_name, # The application name |
| 835 | vnfd, # The vnf descriptor |
| 836 | charm_path, # Path to charm |
| 837 | params, # Runtime params, like mgmt ip |
| 838 | {}, # for native charms only |
| 839 | self.n2vc_callback, # Callback for status changes |
| 840 | n2vc_info, # Callback parameter |
| 841 | None, # Callback parameter (task) |
| 842 | ) |
| 843 | ) |
| 844 | task.add_done_callback(functools.partial(self.n2vc_callback, model_name, application_name, None, None, |
| 845 | n2vc_info)) |
| 846 | self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "create_charm:" + application_name, task) |
| 847 | |
| 848 | step = "Looking for needed vnfd to configure" |
| 849 | self.logger.debug(logging_text + step) |
| 850 | |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 851 | for c_vnf in get_iterable(nsd, "constituent-vnfd"): |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 852 | vnfd_id = c_vnf["vnfd-id-ref"] |
| 853 | vnf_index = str(c_vnf["member-vnf-index"]) |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 854 | vnfd = db_vnfds_ref[vnfd_id] |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 855 | |
| 856 | # Check if this VNF has a charm configuration |
| 857 | vnf_config = vnfd.get("vnf-configuration") |
| 858 | |
| 859 | if vnf_config and vnf_config.get("juju"): |
| 860 | proxy_charm = vnf_config["juju"]["charm"] |
| 861 | config_primitive = None |
| 862 | |
| 863 | if proxy_charm: |
| 864 | if 'initial-config-primitive' in vnf_config: |
| 865 | config_primitive = vnf_config['initial-config-primitive'] |
| 866 | |
| 867 | # Login to the VCA. If there are multiple calls to login(), |
| 868 | # subsequent calls will be a nop and return immediately. |
| 869 | step = "connecting to N2VC to configure vnf {}".format(vnf_index) |
| 870 | await self.n2vc.login() |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 871 | deploy_charm(vnf_index, None, None, None, db_vnfrs[vnf_index]["ip-address"], n2vc_info, |
| 872 | config_primitive) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 873 | number_to_configure += 1 |
| 874 | |
| 875 | # Deploy charms for each VDU that supports one. |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 876 | for vdu_index, vdu in enumerate(get_iterable(vnfd, 'vdu')): |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 877 | vdu_config = vdu.get('vdu-configuration') |
| 878 | proxy_charm = None |
| 879 | config_primitive = None |
| 880 | |
| 881 | if vdu_config and vdu_config.get("juju"): |
| 882 | proxy_charm = vdu_config["juju"]["charm"] |
| 883 | |
| 884 | if 'initial-config-primitive' in vdu_config: |
| 885 | config_primitive = vdu_config['initial-config-primitive'] |
| 886 | |
| 887 | if proxy_charm: |
| 888 | step = "connecting to N2VC to configure vdu {} from vnf {}".format(vdu["id"], vnf_index) |
| 889 | await self.n2vc.login() |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 890 | vdur = db_vnfrs[vnf_index]["vdur"][vdu_index] |
| 891 | # TODO for the moment only first vdu_id contains a charm deployed |
| 892 | if vdur["vdu-id-ref"] != vdu["id"]: |
| 893 | raise LcmException("Mismatch vdur {}, vdu {} at index {} for vnf {}" |
| 894 | .format(vdur["vdu-id-ref"], vdu["id"], vdu_index, vnf_index)) |
| 895 | deploy_charm(vnf_index, vdu["id"], vdur.get("name"), vdur["count-index"], |
| 896 | vdur["ip-address"], n2vc_info, config_primitive) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 897 | number_to_configure += 1 |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 898 | |
| 899 | db_nsr_update["operational-status"] = "running" |
| 900 | configuration_failed = False |
| 901 | if number_to_configure: |
| 902 | old_status = "configuring: init: {}".format(number_to_configure) |
| 903 | db_nsr_update["config-status"] = old_status |
| 904 | db_nsr_update["detailed-status"] = old_status |
| 905 | db_nslcmop_update["detailed-status"] = old_status |
| 906 | |
| 907 | # wait until all are configured. |
| tierno | 63de62e | 2018-10-31 16:38:52 +0100 | [diff] [blame] | 908 | while time() <= start_deploy + self.total_deploy_timeout: |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 909 | if db_nsr_update: |
| 910 | self.update_db_2("nsrs", nsr_id, db_nsr_update) |
| 911 | if db_nslcmop_update: |
| 912 | self.update_db_2("nslcmops", nslcmop_id, db_nslcmop_update) |
| tierno | 63de62e | 2018-10-31 16:38:52 +0100 | [diff] [blame] | 913 | # TODO add a fake tast that set n2vc_event after some time |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 914 | await n2vc_info["n2vc_event"].wait() |
| 915 | n2vc_info["n2vc_event"].clear() |
| 916 | all_active = True |
| 917 | status_map = {} |
| 918 | n2vc_error_text = [] # contain text error list. If empty no one is in error status |
| tierno | 63de62e | 2018-10-31 16:38:52 +0100 | [diff] [blame] | 919 | now = time() |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 920 | for vca_deployed in vca_deployed_list: |
| 921 | vca_status = vca_deployed["operational-status"] |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 922 | if vca_status not in status_map: |
| 923 | # Initialize it |
| 924 | status_map[vca_status] = 0 |
| 925 | status_map[vca_status] += 1 |
| 926 | |
| tierno | 63de62e | 2018-10-31 16:38:52 +0100 | [diff] [blame] | 927 | if vca_status == "active": |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 928 | vca_deployed.pop("time_first_error", None) |
| 929 | vca_deployed.pop("status_first_error", None) |
| tierno | 63de62e | 2018-10-31 16:38:52 +0100 | [diff] [blame] | 930 | continue |
| 931 | |
| 932 | all_active = False |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 933 | if vca_status in ("error", "blocked"): |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 934 | vca_deployed["detailed-status-error"] = vca_deployed["detailed-status"] |
| tierno | 63de62e | 2018-10-31 16:38:52 +0100 | [diff] [blame] | 935 | # if not first time in this status error |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 936 | if not vca_deployed.get("time_first_error"): |
| 937 | vca_deployed["time_first_error"] = now |
| tierno | 63de62e | 2018-10-31 16:38:52 +0100 | [diff] [blame] | 938 | continue |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 939 | if vca_deployed.get("time_first_error") and \ |
| 940 | now <= vca_deployed["time_first_error"] + self.timeout_vca_on_error: |
| tierno | 63de62e | 2018-10-31 16:38:52 +0100 | [diff] [blame] | 941 | n2vc_error_text.append("member_vnf_index={} vdu_id={} {}: {}" |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 942 | .format(vca_deployed["member-vnf-index"], |
| 943 | vca_deployed["vdu_id"], vca_status, |
| 944 | vca_deployed["detailed-status-error"])) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 945 | |
| 946 | if all_active: |
| 947 | break |
| 948 | elif n2vc_error_text: |
| 949 | db_nsr_update["config-status"] = "failed" |
| 950 | error_text = "fail configuring " + ";".join(n2vc_error_text) |
| 951 | db_nsr_update["detailed-status"] = error_text |
| 952 | db_nslcmop_update["operationState"] = nslcmop_operation_state = "FAILED_TEMP" |
| 953 | db_nslcmop_update["detailed-status"] = error_text |
| 954 | db_nslcmop_update["statusEnteredTime"] = time() |
| 955 | configuration_failed = True |
| 956 | break |
| 957 | else: |
| 958 | cs = "configuring: " |
| 959 | separator = "" |
| 960 | for status, num in status_map.items(): |
| 961 | cs += separator + "{}: {}".format(status, num) |
| 962 | separator = ", " |
| 963 | if old_status != cs: |
| 964 | db_nsr_update["config-status"] = cs |
| 965 | db_nsr_update["detailed-status"] = cs |
| 966 | db_nslcmop_update["detailed-status"] = cs |
| 967 | old_status = cs |
| tierno | 63de62e | 2018-10-31 16:38:52 +0100 | [diff] [blame] | 968 | else: # total_deploy_timeout |
| 969 | raise LcmException("Timeout waiting ns to be configured") |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 970 | |
| 971 | if not configuration_failed: |
| 972 | # all is done |
| 973 | db_nslcmop_update["operationState"] = nslcmop_operation_state = "COMPLETED" |
| 974 | db_nslcmop_update["statusEnteredTime"] = time() |
| 975 | db_nslcmop_update["detailed-status"] = "done" |
| 976 | db_nsr_update["config-status"] = "configured" |
| 977 | db_nsr_update["detailed-status"] = "done" |
| 978 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 979 | return |
| 980 | |
| 981 | except (ROclient.ROClientException, DbException, LcmException) as e: |
| 982 | self.logger.error(logging_text + "Exit Exception while '{}': {}".format(step, e)) |
| 983 | exc = e |
| 984 | except asyncio.CancelledError: |
| 985 | self.logger.error(logging_text + "Cancelled Exception while '{}'".format(step)) |
| 986 | exc = "Operation was cancelled" |
| 987 | except Exception as e: |
| 988 | exc = traceback.format_exc() |
| 989 | self.logger.critical(logging_text + "Exit Exception {} while '{}': {}".format(type(e).__name__, step, e), |
| 990 | exc_info=True) |
| 991 | finally: |
| 992 | if exc: |
| 993 | if db_nsr: |
| 994 | db_nsr_update["detailed-status"] = "ERROR {}: {}".format(step, exc) |
| 995 | db_nsr_update["operational-status"] = "failed" |
| 996 | if db_nslcmop: |
| 997 | db_nslcmop_update["detailed-status"] = "FAILED {}: {}".format(step, exc) |
| 998 | db_nslcmop_update["operationState"] = nslcmop_operation_state = "FAILED" |
| 999 | db_nslcmop_update["statusEnteredTime"] = time() |
| tierno | 47e86b5 | 2018-10-10 14:05:55 +0200 | [diff] [blame] | 1000 | if db_nsr: |
| 1001 | db_nsr_update["_admin.nslcmop"] = None |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1002 | self.update_db_2("nsrs", nsr_id, db_nsr_update) |
| 1003 | if db_nslcmop_update: |
| 1004 | self.update_db_2("nslcmops", nslcmop_id, db_nslcmop_update) |
| 1005 | if nslcmop_operation_state: |
| 1006 | try: |
| 1007 | await self.msg.aiowrite("ns", "instantiated", {"nsr_id": nsr_id, "nslcmop_id": nslcmop_id, |
| 1008 | "operationState": nslcmop_operation_state}) |
| 1009 | except Exception as e: |
| 1010 | self.logger.error(logging_text + "kafka_write notification Exception {}".format(e)) |
| 1011 | |
| 1012 | self.logger.debug(logging_text + "Exit") |
| 1013 | self.lcm_tasks.remove("ns", nsr_id, nslcmop_id, "ns_instantiate") |
| 1014 | |
| 1015 | async def terminate(self, nsr_id, nslcmop_id): |
| 1016 | logging_text = "Task ns={} terminate={} ".format(nsr_id, nslcmop_id) |
| 1017 | self.logger.debug(logging_text + "Enter") |
| 1018 | db_nsr = None |
| 1019 | db_nslcmop = None |
| 1020 | exc = None |
| 1021 | failed_detail = [] # annotates all failed error messages |
| 1022 | vca_task_list = [] |
| 1023 | vca_task_dict = {} |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 1024 | vca_application_name2index = {} |
| tierno | 47e86b5 | 2018-10-10 14:05:55 +0200 | [diff] [blame] | 1025 | db_nsr_update = {"_admin.nslcmop": nslcmop_id} |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1026 | db_nslcmop_update = {} |
| 1027 | nslcmop_operation_state = None |
| 1028 | try: |
| 1029 | step = "Getting nslcmop={} from db".format(nslcmop_id) |
| 1030 | db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id}) |
| 1031 | step = "Getting nsr={} from db".format(nsr_id) |
| 1032 | db_nsr = self.db.get_one("nsrs", {"_id": nsr_id}) |
| 1033 | # nsd = db_nsr["nsd"] |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 1034 | nsr_deployed = deepcopy(db_nsr["_admin"].get("deployed")) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1035 | if db_nsr["_admin"]["nsState"] == "NOT_INSTANTIATED": |
| 1036 | return |
| 1037 | # TODO ALF remove |
| 1038 | # db_vim = self.db.get_one("vim_accounts", {"_id": db_nsr["datacenter"]}) |
| 1039 | # #TODO check if VIM is creating and wait |
| 1040 | # RO_vim_id = db_vim["_admin"]["deployed"]["RO"] |
| 1041 | |
| 1042 | db_nsr_update["operational-status"] = "terminating" |
| 1043 | db_nsr_update["config-status"] = "terminating" |
| 1044 | |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 1045 | if nsr_deployed and nsr_deployed.get("VCA"): |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1046 | try: |
| 1047 | step = "Scheduling configuration charms removing" |
| 1048 | db_nsr_update["detailed-status"] = "Deleting charms" |
| 1049 | self.logger.debug(logging_text + step) |
| 1050 | self.update_db_2("nsrs", nsr_id, db_nsr_update) |
| tierno | 82974b2 | 2018-11-27 21:55:36 +0000 | [diff] [blame^] | 1051 | # for backward compatibility |
| 1052 | if isinstance(nsr_deployed["VCA"], dict): |
| 1053 | nsr_deployed["VCA"] = list(nsr_deployed["VCA"].values()) |
| 1054 | db_nsr_update["_admin.deployed.VCA"] = nsr_deployed["VCA"] |
| 1055 | self.update_db_2("nsrs", nsr_id, db_nsr_update) |
| 1056 | |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 1057 | for vca_index, vca_deployed in enumerate(nsr_deployed["VCA"]): |
| 1058 | if vca_deployed: # TODO it would be desirable having a and deploy_info.get("deployed"): |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1059 | task = asyncio.ensure_future( |
| 1060 | self.n2vc.RemoveCharms( |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 1061 | vca_deployed['model'], |
| 1062 | vca_deployed["application"], |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1063 | # self.n2vc_callback, |
| 1064 | # db_nsr, |
| 1065 | # db_nslcmop, |
| 1066 | ) |
| 1067 | ) |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 1068 | vca_application_name2index[vca_deployed["application"]] = vca_index |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1069 | vca_task_list.append(task) |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 1070 | vca_task_dict[vca_deployed["application"]] = task |
| 1071 | # task.add_done_callback(functools.partial(self.n2vc_callback, vca_deployed['model'], |
| 1072 | # vca_deployed['application'], None, db_nsr, |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1073 | # db_nslcmop, vnf_index)) |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 1074 | self.lcm_tasks.register("ns", nsr_id, nslcmop_id, |
| 1075 | "delete_charm:" + vca_deployed["application"], task) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1076 | except Exception as e: |
| 1077 | self.logger.debug(logging_text + "Failed while deleting charms: {}".format(e)) |
| 1078 | |
| 1079 | # remove from RO |
| 1080 | RO_fail = False |
| 1081 | RO = ROclient.ROClient(self.loop, **self.ro_config) |
| 1082 | |
| 1083 | # Delete ns |
| 1084 | RO_nsr_id = RO_delete_action = None |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 1085 | if nsr_deployed and nsr_deployed.get("RO"): |
| 1086 | RO_nsr_id = nsr_deployed["RO"].get("nsr_id") |
| 1087 | RO_delete_action = nsr_deployed["RO"].get("nsr_delete_action_id") |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1088 | try: |
| 1089 | if RO_nsr_id: |
| 1090 | step = db_nsr_update["detailed-status"] = db_nslcmop_update["detailed-status"] = "Deleting ns at RO" |
| 1091 | self.logger.debug(logging_text + step) |
| 1092 | desc = await RO.delete("ns", RO_nsr_id) |
| 1093 | RO_delete_action = desc["action_id"] |
| 1094 | db_nsr_update["_admin.deployed.RO.nsr_delete_action_id"] = RO_delete_action |
| 1095 | db_nsr_update["_admin.deployed.RO.nsr_id"] = None |
| 1096 | db_nsr_update["_admin.deployed.RO.nsr_status"] = "DELETED" |
| 1097 | if RO_delete_action: |
| 1098 | # wait until NS is deleted from VIM |
| 1099 | step = detailed_status = "Waiting ns deleted from VIM. RO_id={}".format(RO_nsr_id) |
| 1100 | detailed_status_old = None |
| 1101 | self.logger.debug(logging_text + step) |
| 1102 | |
| 1103 | delete_timeout = 20 * 60 # 20 minutes |
| 1104 | while delete_timeout > 0: |
| 1105 | desc = await RO.show("ns", item_id_name=RO_nsr_id, extra_item="action", |
| 1106 | extra_item_id=RO_delete_action) |
| 1107 | ns_status, ns_status_info = RO.check_action_status(desc) |
| 1108 | if ns_status == "ERROR": |
| 1109 | raise ROclient.ROClientException(ns_status_info) |
| 1110 | elif ns_status == "BUILD": |
| 1111 | detailed_status = step + "; {}".format(ns_status_info) |
| 1112 | elif ns_status == "ACTIVE": |
| 1113 | break |
| 1114 | else: |
| 1115 | assert False, "ROclient.check_action_status returns unknown {}".format(ns_status) |
| 1116 | await asyncio.sleep(5, loop=self.loop) |
| 1117 | delete_timeout -= 5 |
| 1118 | if detailed_status != detailed_status_old: |
| 1119 | detailed_status_old = db_nslcmop_update["detailed-status"] = detailed_status |
| 1120 | self.update_db_2("nslcmops", nslcmop_id, db_nslcmop_update) |
| 1121 | else: # delete_timeout <= 0: |
| 1122 | raise ROclient.ROClientException("Timeout waiting ns deleted from VIM") |
| 1123 | |
| 1124 | except ROclient.ROClientException as e: |
| 1125 | if e.http_code == 404: # not found |
| 1126 | db_nsr_update["_admin.deployed.RO.nsr_id"] = None |
| 1127 | db_nsr_update["_admin.deployed.RO.nsr_status"] = "DELETED" |
| 1128 | self.logger.debug(logging_text + "RO_ns_id={} already deleted".format(RO_nsr_id)) |
| 1129 | elif e.http_code == 409: # conflict |
| 1130 | failed_detail.append("RO_ns_id={} delete conflict: {}".format(RO_nsr_id, e)) |
| 1131 | self.logger.debug(logging_text + failed_detail[-1]) |
| 1132 | RO_fail = True |
| 1133 | else: |
| 1134 | failed_detail.append("RO_ns_id={} delete error: {}".format(RO_nsr_id, e)) |
| 1135 | self.logger.error(logging_text + failed_detail[-1]) |
| 1136 | RO_fail = True |
| 1137 | |
| 1138 | # Delete nsd |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 1139 | if not RO_fail and nsr_deployed and nsr_deployed.get("RO") and nsr_deployed["RO"].get("nsd_id"): |
| 1140 | RO_nsd_id = nsr_deployed["RO"]["nsd_id"] |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1141 | try: |
| 1142 | step = db_nsr_update["detailed-status"] = db_nslcmop_update["detailed-status"] =\ |
| 1143 | "Deleting nsd at RO" |
| 1144 | await RO.delete("nsd", RO_nsd_id) |
| 1145 | self.logger.debug(logging_text + "RO_nsd_id={} deleted".format(RO_nsd_id)) |
| 1146 | db_nsr_update["_admin.deployed.RO.nsd_id"] = None |
| 1147 | except ROclient.ROClientException as e: |
| 1148 | if e.http_code == 404: # not found |
| 1149 | db_nsr_update["_admin.deployed.RO.nsd_id"] = None |
| 1150 | self.logger.debug(logging_text + "RO_nsd_id={} already deleted".format(RO_nsd_id)) |
| 1151 | elif e.http_code == 409: # conflict |
| 1152 | failed_detail.append("RO_nsd_id={} delete conflict: {}".format(RO_nsd_id, e)) |
| 1153 | self.logger.debug(logging_text + failed_detail[-1]) |
| 1154 | RO_fail = True |
| 1155 | else: |
| 1156 | failed_detail.append("RO_nsd_id={} delete error: {}".format(RO_nsd_id, e)) |
| 1157 | self.logger.error(logging_text + failed_detail[-1]) |
| 1158 | RO_fail = True |
| 1159 | |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 1160 | if not RO_fail and nsr_deployed and nsr_deployed.get("RO") and nsr_deployed["RO"].get("vnfd_id"): |
| 1161 | for vnf_id, RO_vnfd_id in nsr_deployed["RO"]["vnfd_id"].items(): |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1162 | if not RO_vnfd_id: |
| 1163 | continue |
| 1164 | try: |
| 1165 | step = db_nsr_update["detailed-status"] = db_nslcmop_update["detailed-status"] =\ |
| 1166 | "Deleting vnfd={} at RO".format(vnf_id) |
| 1167 | await RO.delete("vnfd", RO_vnfd_id) |
| 1168 | self.logger.debug(logging_text + "RO_vnfd_id={} deleted".format(RO_vnfd_id)) |
| 1169 | db_nsr_update["_admin.deployed.RO.vnfd_id.{}".format(vnf_id)] = None |
| 1170 | except ROclient.ROClientException as e: |
| 1171 | if e.http_code == 404: # not found |
| 1172 | db_nsr_update["_admin.deployed.RO.vnfd_id.{}".format(vnf_id)] = None |
| 1173 | self.logger.debug(logging_text + "RO_vnfd_id={} already deleted ".format(RO_vnfd_id)) |
| 1174 | elif e.http_code == 409: # conflict |
| 1175 | failed_detail.append("RO_vnfd_id={} delete conflict: {}".format(RO_vnfd_id, e)) |
| 1176 | self.logger.debug(logging_text + failed_detail[-1]) |
| 1177 | else: |
| 1178 | failed_detail.append("RO_vnfd_id={} delete error: {}".format(RO_vnfd_id, e)) |
| 1179 | self.logger.error(logging_text + failed_detail[-1]) |
| 1180 | |
| 1181 | if vca_task_list: |
| 1182 | db_nsr_update["detailed-status"] = db_nslcmop_update["detailed-status"] =\ |
| 1183 | "Waiting for deletion of configuration charms" |
| 1184 | self.update_db_2("nslcmops", nslcmop_id, db_nslcmop_update) |
| 1185 | self.update_db_2("nsrs", nsr_id, db_nsr_update) |
| 1186 | await asyncio.wait(vca_task_list, timeout=300) |
| 1187 | for application_name, task in vca_task_dict.items(): |
| 1188 | if task.cancelled(): |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 1189 | failed_detail.append("VCA[application_name={}] Deletion has been cancelled" |
| 1190 | .format(application_name)) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1191 | elif task.done(): |
| 1192 | exc = task.exception() |
| 1193 | if exc: |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 1194 | failed_detail.append("VCA[application_name={}] Deletion exception: {}" |
| 1195 | .format(application_name, exc)) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1196 | else: |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 1197 | vca_index = vca_application_name2index[application_name] |
| 1198 | db_nsr_update["_admin.deployed.VCA.{}".format(vca_index)] = None |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1199 | else: # timeout |
| 1200 | # TODO Should it be cancelled?!! |
| 1201 | task.cancel() |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 1202 | failed_detail.append("VCA[application_name={}] Deletion timeout".format(application_name)) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1203 | |
| 1204 | if failed_detail: |
| 1205 | self.logger.error(logging_text + " ;".join(failed_detail)) |
| 1206 | db_nsr_update["operational-status"] = "failed" |
| 1207 | db_nsr_update["detailed-status"] = "Deletion errors " + "; ".join(failed_detail) |
| 1208 | db_nslcmop_update["detailed-status"] = "; ".join(failed_detail) |
| 1209 | db_nslcmop_update["operationState"] = nslcmop_operation_state = "FAILED" |
| 1210 | db_nslcmop_update["statusEnteredTime"] = time() |
| 1211 | elif db_nslcmop["operationParams"].get("autoremove"): |
| 1212 | self.db.del_one("nsrs", {"_id": nsr_id}) |
| 1213 | db_nsr_update.clear() |
| 1214 | self.db.del_list("nslcmops", {"nsInstanceId": nsr_id}) |
| 1215 | nslcmop_operation_state = "COMPLETED" |
| 1216 | db_nslcmop_update.clear() |
| 1217 | self.db.del_list("vnfrs", {"nsr-id-ref": nsr_id}) |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 1218 | self.db.set_list("pdus", {"_admin.usage.nsr_id": nsr_id}, |
| 1219 | {"_admin.usageSate": "NOT_IN_USE", "_admin.usage": None}) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1220 | self.logger.debug(logging_text + "Delete from database") |
| 1221 | else: |
| 1222 | db_nsr_update["operational-status"] = "terminated" |
| 1223 | db_nsr_update["detailed-status"] = "Done" |
| 1224 | db_nsr_update["_admin.nsState"] = "NOT_INSTANTIATED" |
| 1225 | db_nslcmop_update["detailed-status"] = "Done" |
| 1226 | db_nslcmop_update["operationState"] = nslcmop_operation_state = "COMPLETED" |
| 1227 | db_nslcmop_update["statusEnteredTime"] = time() |
| 1228 | |
| 1229 | except (ROclient.ROClientException, DbException) as e: |
| 1230 | self.logger.error(logging_text + "Exit Exception {}".format(e)) |
| 1231 | exc = e |
| 1232 | except asyncio.CancelledError: |
| 1233 | self.logger.error(logging_text + "Cancelled Exception while '{}'".format(step)) |
| 1234 | exc = "Operation was cancelled" |
| 1235 | except Exception as e: |
| 1236 | exc = traceback.format_exc() |
| 1237 | self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True) |
| 1238 | finally: |
| 1239 | if exc and db_nslcmop: |
| 1240 | db_nslcmop_update["detailed-status"] = "FAILED {}: {}".format(step, exc) |
| 1241 | db_nslcmop_update["operationState"] = nslcmop_operation_state = "FAILED" |
| 1242 | db_nslcmop_update["statusEnteredTime"] = time() |
| 1243 | if db_nslcmop_update: |
| 1244 | self.update_db_2("nslcmops", nslcmop_id, db_nslcmop_update) |
| tierno | 47e86b5 | 2018-10-10 14:05:55 +0200 | [diff] [blame] | 1245 | if db_nsr: |
| 1246 | db_nsr_update["_admin.nslcmop"] = None |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1247 | self.update_db_2("nsrs", nsr_id, db_nsr_update) |
| 1248 | if nslcmop_operation_state: |
| 1249 | try: |
| 1250 | await self.msg.aiowrite("ns", "terminated", {"nsr_id": nsr_id, "nslcmop_id": nslcmop_id, |
| 1251 | "operationState": nslcmop_operation_state}) |
| 1252 | except Exception as e: |
| 1253 | self.logger.error(logging_text + "kafka_write notification Exception {}".format(e)) |
| 1254 | self.logger.debug(logging_text + "Exit") |
| 1255 | self.lcm_tasks.remove("ns", nsr_id, nslcmop_id, "ns_terminate") |
| 1256 | |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 1257 | async def _ns_execute_primitive(self, db_deployed, nsr_name, member_vnf_index, vdu_id, vdu_name, vdu_count_index, |
| 1258 | primitive, primitive_params): |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1259 | |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 1260 | for vca_deployed in db_deployed["VCA"]: |
| 1261 | if not vca_deployed: |
| 1262 | continue |
| 1263 | if member_vnf_index != vca_deployed["member-vnf-index"] or vdu_id != vca_deployed["vdu_id"]: |
| 1264 | continue |
| 1265 | if vdu_name and vdu_name != vca_deployed["vdu_name"]: |
| 1266 | continue |
| 1267 | if vdu_count_index and vdu_count_index != vca_deployed["vdu_count_index"]: |
| 1268 | continue |
| 1269 | break |
| 1270 | else: |
| 1271 | raise LcmException("charm for member_vnf_index={} vdu_id={} vdu_name={} vdu_count_index={} is not deployed" |
| 1272 | .format(member_vnf_index, vdu_id, vdu_name, vdu_count_index)) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1273 | model_name = vca_deployed.get("model") |
| 1274 | application_name = vca_deployed.get("application") |
| 1275 | if not model_name or not application_name: |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 1276 | raise LcmException("charm for member_vnf_index={} vdu_id={} vdu_name={} vdu_count_index={} has not model " |
| 1277 | "or application name" .format(member_vnf_index, vdu_id, vdu_name, vdu_count_index)) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1278 | if vca_deployed["operational-status"] != "active": |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 1279 | raise LcmException("charm for member_vnf_index={} vdu_id={} operational_status={} not 'active'".format( |
| 1280 | member_vnf_index, vdu_id, vca_deployed["operational-status"])) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1281 | callback = None # self.n2vc_callback |
| 1282 | callback_args = () # [db_nsr, db_nslcmop, member_vnf_index, None] |
| 1283 | await self.n2vc.login() |
| 1284 | task = asyncio.ensure_future( |
| 1285 | self.n2vc.ExecutePrimitive( |
| 1286 | model_name, |
| 1287 | application_name, |
| 1288 | primitive, callback, |
| 1289 | *callback_args, |
| 1290 | **primitive_params |
| 1291 | ) |
| 1292 | ) |
| 1293 | # task.add_done_callback(functools.partial(self.n2vc_callback, model_name, application_name, None, |
| 1294 | # db_nsr, db_nslcmop, member_vnf_index)) |
| 1295 | # self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "action:" + primitive, task) |
| 1296 | # wait until completed with timeout |
| 1297 | await asyncio.wait((task,), timeout=600) |
| 1298 | |
| 1299 | result = "FAILED" # by default |
| 1300 | result_detail = "" |
| 1301 | if task.cancelled(): |
| 1302 | result_detail = "Task has been cancelled" |
| 1303 | elif task.done(): |
| 1304 | exc = task.exception() |
| 1305 | if exc: |
| 1306 | result_detail = str(exc) |
| 1307 | else: |
| 1308 | # TODO revise with Adam if action is finished and ok when task is done or callback is needed |
| 1309 | result = "COMPLETED" |
| 1310 | result_detail = "Done" |
| 1311 | else: # timeout |
| 1312 | # TODO Should it be cancelled?!! |
| 1313 | task.cancel() |
| 1314 | result_detail = "timeout" |
| 1315 | return result, result_detail |
| 1316 | |
| 1317 | async def action(self, nsr_id, nslcmop_id): |
| 1318 | logging_text = "Task ns={} action={} ".format(nsr_id, nslcmop_id) |
| 1319 | self.logger.debug(logging_text + "Enter") |
| 1320 | # get all needed from database |
| 1321 | db_nsr = None |
| 1322 | db_nslcmop = None |
| tierno | 47e86b5 | 2018-10-10 14:05:55 +0200 | [diff] [blame] | 1323 | db_nsr_update = {"_admin.nslcmop": nslcmop_id} |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1324 | db_nslcmop_update = {} |
| 1325 | nslcmop_operation_state = None |
| 1326 | exc = None |
| 1327 | try: |
| 1328 | step = "Getting information from database" |
| 1329 | db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id}) |
| 1330 | db_nsr = self.db.get_one("nsrs", {"_id": nsr_id}) |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 1331 | nsr_deployed = db_nsr["_admin"].get("deployed") |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1332 | nsr_name = db_nsr["name"] |
| 1333 | vnf_index = db_nslcmop["operationParams"]["member_vnf_index"] |
| 1334 | vdu_id = db_nslcmop["operationParams"].get("vdu_id") |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 1335 | vdu_count_index = db_nslcmop["operationParams"].get("vdu_count_index") |
| 1336 | vdu_name = db_nslcmop["operationParams"].get("vdu_name") |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1337 | |
| tierno | 47e86b5 | 2018-10-10 14:05:55 +0200 | [diff] [blame] | 1338 | # look if previous tasks in process |
| 1339 | task_name, task_dependency = self.lcm_tasks.lookfor_related("ns", nsr_id, nslcmop_id) |
| 1340 | if task_dependency: |
| 1341 | step = db_nslcmop_update["detailed-status"] = \ |
| 1342 | "Waiting for related tasks to be completed: {}".format(task_name) |
| 1343 | self.logger.debug(logging_text + step) |
| 1344 | self.update_db_2("nslcmops", nslcmop_id, db_nslcmop_update) |
| 1345 | _, pending = await asyncio.wait(task_dependency, timeout=3600) |
| 1346 | if pending: |
| 1347 | raise LcmException("Timeout waiting related tasks to be completed") |
| 1348 | |
| tierno | 82974b2 | 2018-11-27 21:55:36 +0000 | [diff] [blame^] | 1349 | # for backward compatibility |
| 1350 | if nsr_deployed and isinstance(nsr_deployed.get("VCA"), dict): |
| 1351 | nsr_deployed["VCA"] = list(nsr_deployed["VCA"].values()) |
| 1352 | db_nsr_update["_admin.deployed.VCA"] = nsr_deployed["VCA"] |
| 1353 | self.update_db_2("nsrs", nsr_id, db_nsr_update) |
| 1354 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1355 | # TODO check if ns is in a proper status |
| 1356 | primitive = db_nslcmop["operationParams"]["primitive"] |
| 1357 | primitive_params = db_nslcmop["operationParams"]["primitive_params"] |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 1358 | result, result_detail = await self._ns_execute_primitive(nsr_deployed, nsr_name, vnf_index, vdu_id, |
| 1359 | vdu_name, vdu_count_index, primitive, |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1360 | primitive_params) |
| 1361 | db_nslcmop_update["detailed-status"] = result_detail |
| 1362 | db_nslcmop_update["operationState"] = nslcmop_operation_state = result |
| 1363 | db_nslcmop_update["statusEnteredTime"] = time() |
| 1364 | self.logger.debug(logging_text + " task Done with result {} {}".format(result, result_detail)) |
| 1365 | return # database update is called inside finally |
| 1366 | |
| 1367 | except (DbException, LcmException) as e: |
| 1368 | self.logger.error(logging_text + "Exit Exception {}".format(e)) |
| 1369 | exc = e |
| 1370 | except asyncio.CancelledError: |
| 1371 | self.logger.error(logging_text + "Cancelled Exception while '{}'".format(step)) |
| 1372 | exc = "Operation was cancelled" |
| 1373 | except Exception as e: |
| 1374 | exc = traceback.format_exc() |
| 1375 | self.logger.critical(logging_text + "Exit Exception {} {}".format(type(e).__name__, e), exc_info=True) |
| 1376 | finally: |
| 1377 | if exc and db_nslcmop: |
| 1378 | db_nslcmop_update["detailed-status"] = "FAILED {}: {}".format(step, exc) |
| 1379 | db_nslcmop_update["operationState"] = nslcmop_operation_state = "FAILED" |
| 1380 | db_nslcmop_update["statusEnteredTime"] = time() |
| 1381 | if db_nslcmop_update: |
| 1382 | self.update_db_2("nslcmops", nslcmop_id, db_nslcmop_update) |
| tierno | 47e86b5 | 2018-10-10 14:05:55 +0200 | [diff] [blame] | 1383 | if db_nsr: |
| 1384 | db_nsr_update["_admin.nslcmop"] = None |
| 1385 | self.update_db_2("nsrs", nsr_id, db_nsr_update) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1386 | self.logger.debug(logging_text + "Exit") |
| 1387 | if nslcmop_operation_state: |
| 1388 | try: |
| 1389 | await self.msg.aiowrite("ns", "actioned", {"nsr_id": nsr_id, "nslcmop_id": nslcmop_id, |
| 1390 | "operationState": nslcmop_operation_state}) |
| 1391 | except Exception as e: |
| 1392 | self.logger.error(logging_text + "kafka_write notification Exception {}".format(e)) |
| 1393 | self.logger.debug(logging_text + "Exit") |
| 1394 | self.lcm_tasks.remove("ns", nsr_id, nslcmop_id, "ns_action") |
| 1395 | |
| 1396 | async def scale(self, nsr_id, nslcmop_id): |
| 1397 | logging_text = "Task ns={} scale={} ".format(nsr_id, nslcmop_id) |
| 1398 | self.logger.debug(logging_text + "Enter") |
| 1399 | # get all needed from database |
| 1400 | db_nsr = None |
| 1401 | db_nslcmop = None |
| 1402 | db_nslcmop_update = {} |
| 1403 | nslcmop_operation_state = None |
| tierno | 47e86b5 | 2018-10-10 14:05:55 +0200 | [diff] [blame] | 1404 | db_nsr_update = {"_admin.nslcmop": nslcmop_id} |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1405 | exc = None |
| tierno | 9ab9594 | 2018-10-10 16:44:22 +0200 | [diff] [blame] | 1406 | # in case of error, indicates what part of scale was failed to put nsr at error status |
| 1407 | scale_process = None |
| tierno | d6de199 | 2018-10-11 13:05:52 +0200 | [diff] [blame] | 1408 | old_operational_status = "" |
| 1409 | old_config_status = "" |
| tierno | f578e55 | 2018-11-08 19:07:20 +0100 | [diff] [blame] | 1410 | vnfr_scaled = False |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1411 | try: |
| 1412 | step = "Getting nslcmop from database" |
| 1413 | db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id}) |
| 1414 | step = "Getting nsr from database" |
| 1415 | db_nsr = self.db.get_one("nsrs", {"_id": nsr_id}) |
| tierno | 4fa22b0 | 2018-11-20 14:56:26 +0000 | [diff] [blame] | 1416 | nsr_name = db_nsr["name"] |
| tierno | d6de199 | 2018-10-11 13:05:52 +0200 | [diff] [blame] | 1417 | old_operational_status = db_nsr["operational-status"] |
| 1418 | old_config_status = db_nsr["config-status"] |
| tierno | 47e86b5 | 2018-10-10 14:05:55 +0200 | [diff] [blame] | 1419 | |
| 1420 | # look if previous tasks in process |
| 1421 | task_name, task_dependency = self.lcm_tasks.lookfor_related("ns", nsr_id, nslcmop_id) |
| 1422 | if task_dependency: |
| 1423 | step = db_nslcmop_update["detailed-status"] = \ |
| 1424 | "Waiting for related tasks to be completed: {}".format(task_name) |
| 1425 | self.logger.debug(logging_text + step) |
| 1426 | self.update_db_2("nslcmops", nslcmop_id, db_nslcmop_update) |
| 1427 | _, pending = await asyncio.wait(task_dependency, timeout=3600) |
| 1428 | if pending: |
| 1429 | raise LcmException("Timeout waiting related tasks to be completed") |
| 1430 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1431 | step = "Parsing scaling parameters" |
| 1432 | db_nsr_update["operational-status"] = "scaling" |
| 1433 | self.update_db_2("nsrs", nsr_id, db_nsr_update) |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 1434 | nsr_deployed = db_nsr["_admin"].get("deployed") |
| 1435 | RO_nsr_id = nsr_deployed["RO"]["nsr_id"] |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1436 | vnf_index = db_nslcmop["operationParams"]["scaleVnfData"]["scaleByStepData"]["member-vnf-index"] |
| 1437 | scaling_group = db_nslcmop["operationParams"]["scaleVnfData"]["scaleByStepData"]["scaling-group-descriptor"] |
| 1438 | scaling_type = db_nslcmop["operationParams"]["scaleVnfData"]["scaleVnfType"] |
| 1439 | # scaling_policy = db_nslcmop["operationParams"]["scaleVnfData"]["scaleByStepData"].get("scaling-policy") |
| 1440 | |
| tierno | 82974b2 | 2018-11-27 21:55:36 +0000 | [diff] [blame^] | 1441 | # for backward compatibility |
| 1442 | if nsr_deployed and isinstance(nsr_deployed.get("VCA"), dict): |
| 1443 | nsr_deployed["VCA"] = list(nsr_deployed["VCA"].values()) |
| 1444 | db_nsr_update["_admin.deployed.VCA"] = nsr_deployed["VCA"] |
| 1445 | self.update_db_2("nsrs", nsr_id, db_nsr_update) |
| 1446 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1447 | step = "Getting vnfr from database" |
| 1448 | db_vnfr = self.db.get_one("vnfrs", {"member-vnf-index-ref": vnf_index, "nsr-id-ref": nsr_id}) |
| 1449 | step = "Getting vnfd from database" |
| 1450 | db_vnfd = self.db.get_one("vnfds", {"_id": db_vnfr["vnfd-id"]}) |
| 1451 | step = "Getting scaling-group-descriptor" |
| 1452 | for scaling_descriptor in db_vnfd["scaling-group-descriptor"]: |
| 1453 | if scaling_descriptor["name"] == scaling_group: |
| 1454 | break |
| 1455 | else: |
| 1456 | raise LcmException("input parameter 'scaleByStepData':'scaling-group-descriptor':'{}' is not present " |
| 1457 | "at vnfd:scaling-group-descriptor".format(scaling_group)) |
| 1458 | # cooldown_time = 0 |
| 1459 | # for scaling_policy_descriptor in scaling_descriptor.get("scaling-policy", ()): |
| 1460 | # cooldown_time = scaling_policy_descriptor.get("cooldown-time", 0) |
| 1461 | # if scaling_policy and scaling_policy == scaling_policy_descriptor.get("name"): |
| 1462 | # break |
| 1463 | |
| 1464 | # TODO check if ns is in a proper status |
| 1465 | step = "Sending scale order to RO" |
| 1466 | nb_scale_op = 0 |
| 1467 | if not db_nsr["_admin"].get("scaling-group"): |
| 1468 | self.update_db_2("nsrs", nsr_id, {"_admin.scaling-group": [{"name": scaling_group, "nb-scale-op": 0}]}) |
| 1469 | admin_scale_index = 0 |
| 1470 | else: |
| 1471 | for admin_scale_index, admin_scale_info in enumerate(db_nsr["_admin"]["scaling-group"]): |
| 1472 | if admin_scale_info["name"] == scaling_group: |
| 1473 | nb_scale_op = admin_scale_info.get("nb-scale-op", 0) |
| 1474 | break |
| tierno | 9ab9594 | 2018-10-10 16:44:22 +0200 | [diff] [blame] | 1475 | else: # not found, set index one plus last element and add new entry with the name |
| 1476 | admin_scale_index += 1 |
| 1477 | db_nsr_update["_admin.scaling-group.{}.name".format(admin_scale_index)] = scaling_group |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1478 | RO_scaling_info = [] |
| 1479 | vdu_scaling_info = {"scaling_group_name": scaling_group, "vdu": []} |
| 1480 | if scaling_type == "SCALE_OUT": |
| 1481 | # count if max-instance-count is reached |
| 1482 | if "max-instance-count" in scaling_descriptor and scaling_descriptor["max-instance-count"] is not None: |
| 1483 | max_instance_count = int(scaling_descriptor["max-instance-count"]) |
| 1484 | if nb_scale_op >= max_instance_count: |
| 1485 | raise LcmException("reached the limit of {} (max-instance-count) scaling-out operations for the" |
| 1486 | " scaling-group-descriptor '{}'".format(nb_scale_op, scaling_group)) |
| 1487 | nb_scale_op = nb_scale_op + 1 |
| 1488 | vdu_scaling_info["scaling_direction"] = "OUT" |
| 1489 | vdu_scaling_info["vdu-create"] = {} |
| 1490 | for vdu_scale_info in scaling_descriptor["vdu"]: |
| 1491 | RO_scaling_info.append({"osm_vdu_id": vdu_scale_info["vdu-id-ref"], "member-vnf-index": vnf_index, |
| 1492 | "type": "create", "count": vdu_scale_info.get("count", 1)}) |
| 1493 | vdu_scaling_info["vdu-create"][vdu_scale_info["vdu-id-ref"]] = vdu_scale_info.get("count", 1) |
| 1494 | elif scaling_type == "SCALE_IN": |
| 1495 | # count if min-instance-count is reached |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 1496 | min_instance_count = 0 |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1497 | if "min-instance-count" in scaling_descriptor and scaling_descriptor["min-instance-count"] is not None: |
| 1498 | min_instance_count = int(scaling_descriptor["min-instance-count"]) |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 1499 | if nb_scale_op <= min_instance_count: |
| 1500 | raise LcmException("reached the limit of {} (min-instance-count) scaling-in operations for the " |
| 1501 | "scaling-group-descriptor '{}'".format(nb_scale_op, scaling_group)) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1502 | nb_scale_op = nb_scale_op - 1 |
| 1503 | vdu_scaling_info["scaling_direction"] = "IN" |
| 1504 | vdu_scaling_info["vdu-delete"] = {} |
| 1505 | for vdu_scale_info in scaling_descriptor["vdu"]: |
| 1506 | RO_scaling_info.append({"osm_vdu_id": vdu_scale_info["vdu-id-ref"], "member-vnf-index": vnf_index, |
| 1507 | "type": "delete", "count": vdu_scale_info.get("count", 1)}) |
| 1508 | vdu_scaling_info["vdu-delete"][vdu_scale_info["vdu-id-ref"]] = vdu_scale_info.get("count", 1) |
| 1509 | |
| 1510 | # update VDU_SCALING_INFO with the VDUs to delete ip_addresses |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 1511 | vdu_create = vdu_scaling_info.get("vdu-create") |
| 1512 | vdu_delete = copy(vdu_scaling_info.get("vdu-delete")) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1513 | if vdu_scaling_info["scaling_direction"] == "IN": |
| 1514 | for vdur in reversed(db_vnfr["vdur"]): |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 1515 | if vdu_delete.get(vdur["vdu-id-ref"]): |
| 1516 | vdu_delete[vdur["vdu-id-ref"]] -= 1 |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1517 | vdu_scaling_info["vdu"].append({ |
| 1518 | "name": vdur["name"], |
| 1519 | "vdu_id": vdur["vdu-id-ref"], |
| 1520 | "interface": [] |
| 1521 | }) |
| 1522 | for interface in vdur["interfaces"]: |
| 1523 | vdu_scaling_info["vdu"][-1]["interface"].append({ |
| 1524 | "name": interface["name"], |
| 1525 | "ip_address": interface["ip-address"], |
| 1526 | "mac_address": interface.get("mac-address"), |
| 1527 | }) |
| tierno | 27246d8 | 2018-09-27 15:59:09 +0200 | [diff] [blame] | 1528 | vdu_delete = vdu_scaling_info.pop("vdu-delete") |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1529 | |
| 1530 | # execute primitive service PRE-SCALING |
| 1531 | step = "Executing pre-scale vnf-config-primitive" |
| 1532 | if scaling_descriptor.get("scaling-config-action"): |
| 1533 | for scaling_config_action in scaling_descriptor["scaling-config-action"]: |
| 1534 | if scaling_config_action.get("trigger") and scaling_config_action["trigger"] == "pre-scale-in" \ |
| 1535 | and scaling_type == "SCALE_IN": |
| 1536 | vnf_config_primitive = scaling_config_action["vnf-config-primitive-name-ref"] |
| 1537 | step = db_nslcmop_update["detailed-status"] = \ |
| 1538 | "executing pre-scale scaling-config-action '{}'".format(vnf_config_primitive) |
| 1539 | # look for primitive |
| 1540 | primitive_params = {} |
| 1541 | for config_primitive in db_vnfd.get("vnf-configuration", {}).get("config-primitive", ()): |
| 1542 | if config_primitive["name"] == vnf_config_primitive: |
| 1543 | for parameter in config_primitive.get("parameter", ()): |
| 1544 | if 'default-value' in parameter and \ |
| 1545 | parameter['default-value'] == "<VDU_SCALE_INFO>": |
| 1546 | primitive_params[parameter["name"]] = yaml.safe_dump(vdu_scaling_info, |
| 1547 | default_flow_style=True, |
| 1548 | width=256) |
| 1549 | break |
| 1550 | else: |
| 1551 | raise LcmException( |
| 1552 | "Invalid vnfd descriptor at scaling-group-descriptor[name='{}']:scaling-config-action" |
| 1553 | "[vnf-config-primitive-name-ref='{}'] does not match any vnf-cnfiguration:config-" |
| 1554 | "primitive".format(scaling_group, config_primitive)) |
| tierno | 9ab9594 | 2018-10-10 16:44:22 +0200 | [diff] [blame] | 1555 | scale_process = "VCA" |
| tierno | d6de199 | 2018-10-11 13:05:52 +0200 | [diff] [blame] | 1556 | db_nsr_update["config-status"] = "configuring pre-scaling" |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 1557 | result, result_detail = await self._ns_execute_primitive(nsr_deployed, nsr_name, vnf_index, |
| 1558 | None, None, None, vnf_config_primitive, |
| 1559 | primitive_params) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1560 | self.logger.debug(logging_text + "vnf_config_primitive={} Done with result {} {}".format( |
| 1561 | vnf_config_primitive, result, result_detail)) |
| 1562 | if result == "FAILED": |
| 1563 | raise LcmException(result_detail) |
| tierno | d6de199 | 2018-10-11 13:05:52 +0200 | [diff] [blame] | 1564 | db_nsr_update["config-status"] = old_config_status |
| 1565 | scale_process = None |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1566 | |
| 1567 | if RO_scaling_info: |
| tierno | 9ab9594 | 2018-10-10 16:44:22 +0200 | [diff] [blame] | 1568 | scale_process = "RO" |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1569 | RO = ROclient.ROClient(self.loop, **self.ro_config) |
| 1570 | RO_desc = await RO.create_action("ns", RO_nsr_id, {"vdu-scaling": RO_scaling_info}) |
| 1571 | db_nsr_update["_admin.scaling-group.{}.nb-scale-op".format(admin_scale_index)] = nb_scale_op |
| 1572 | db_nsr_update["_admin.scaling-group.{}.time".format(admin_scale_index)] = time() |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1573 | # wait until ready |
| 1574 | RO_nslcmop_id = RO_desc["instance_action_id"] |
| 1575 | db_nslcmop_update["_admin.deploy.RO"] = RO_nslcmop_id |
| 1576 | |
| 1577 | RO_task_done = False |
| 1578 | step = detailed_status = "Waiting RO_task_id={} to complete the scale action.".format(RO_nslcmop_id) |
| 1579 | detailed_status_old = None |
| 1580 | self.logger.debug(logging_text + step) |
| 1581 | |
| tierno | 9ab9594 | 2018-10-10 16:44:22 +0200 | [diff] [blame] | 1582 | deployment_timeout = 1 * 3600 # One hour |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1583 | while deployment_timeout > 0: |
| 1584 | if not RO_task_done: |
| 1585 | desc = await RO.show("ns", item_id_name=RO_nsr_id, extra_item="action", |
| 1586 | extra_item_id=RO_nslcmop_id) |
| 1587 | ns_status, ns_status_info = RO.check_action_status(desc) |
| 1588 | if ns_status == "ERROR": |
| 1589 | raise ROclient.ROClientException(ns_status_info) |
| 1590 | elif ns_status == "BUILD": |
| 1591 | detailed_status = step + "; {}".format(ns_status_info) |
| 1592 | elif ns_status == "ACTIVE": |
| 1593 | RO_task_done = True |
| 1594 | step = detailed_status = "Waiting ns ready at RO. RO_id={}".format(RO_nsr_id) |
| 1595 | self.logger.debug(logging_text + step) |
| 1596 | else: |
| 1597 | assert False, "ROclient.check_action_status returns unknown {}".format(ns_status) |
| 1598 | else: |
| 1599 | desc = await RO.show("ns", RO_nsr_id) |
| 1600 | ns_status, ns_status_info = RO.check_ns_status(desc) |
| 1601 | if ns_status == "ERROR": |
| 1602 | raise ROclient.ROClientException(ns_status_info) |
| 1603 | elif ns_status == "BUILD": |
| 1604 | detailed_status = step + "; {}".format(ns_status_info) |
| 1605 | elif ns_status == "ACTIVE": |
| tierno | f578e55 | 2018-11-08 19:07:20 +0100 | [diff] [blame] | 1606 | step = detailed_status = \ |
| 1607 | "Waiting for management IP address reported by the VIM. Updating VNFRs" |
| 1608 | if not vnfr_scaled: |
| 1609 | self.scale_vnfr(db_vnfr, vdu_create=vdu_create, vdu_delete=vdu_delete) |
| 1610 | vnfr_scaled = True |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1611 | try: |
| 1612 | desc = await RO.show("ns", RO_nsr_id) |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 1613 | # nsr_deployed["nsr_ip"] = RO.get_ns_vnf_info(desc) |
| tierno | f578e55 | 2018-11-08 19:07:20 +0100 | [diff] [blame] | 1614 | self.ns_update_vnfr({db_vnfr["member-vnf-index-ref"]: db_vnfr}, desc) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1615 | break |
| tierno | f578e55 | 2018-11-08 19:07:20 +0100 | [diff] [blame] | 1616 | except LcmExceptionNoMgmtIP: |
| 1617 | pass |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1618 | else: |
| 1619 | assert False, "ROclient.check_ns_status returns unknown {}".format(ns_status) |
| 1620 | if detailed_status != detailed_status_old: |
| 1621 | detailed_status_old = db_nslcmop_update["detailed-status"] = detailed_status |
| 1622 | self.update_db_2("nslcmops", nslcmop_id, db_nslcmop_update) |
| 1623 | |
| 1624 | await asyncio.sleep(5, loop=self.loop) |
| 1625 | deployment_timeout -= 5 |
| 1626 | if deployment_timeout <= 0: |
| 1627 | raise ROclient.ROClientException("Timeout waiting ns to be ready") |
| 1628 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1629 | # update VDU_SCALING_INFO with the obtained ip_addresses |
| 1630 | if vdu_scaling_info["scaling_direction"] == "OUT": |
| 1631 | for vdur in reversed(db_vnfr["vdur"]): |
| 1632 | if vdu_scaling_info["vdu-create"].get(vdur["vdu-id-ref"]): |
| 1633 | vdu_scaling_info["vdu-create"][vdur["vdu-id-ref"]] -= 1 |
| 1634 | vdu_scaling_info["vdu"].append({ |
| 1635 | "name": vdur["name"], |
| 1636 | "vdu_id": vdur["vdu-id-ref"], |
| 1637 | "interface": [] |
| 1638 | }) |
| 1639 | for interface in vdur["interfaces"]: |
| 1640 | vdu_scaling_info["vdu"][-1]["interface"].append({ |
| 1641 | "name": interface["name"], |
| 1642 | "ip_address": interface["ip-address"], |
| 1643 | "mac_address": interface.get("mac-address"), |
| 1644 | }) |
| 1645 | del vdu_scaling_info["vdu-create"] |
| 1646 | |
| tierno | 9ab9594 | 2018-10-10 16:44:22 +0200 | [diff] [blame] | 1647 | scale_process = None |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1648 | if db_nsr_update: |
| 1649 | self.update_db_2("nsrs", nsr_id, db_nsr_update) |
| 1650 | |
| 1651 | # execute primitive service POST-SCALING |
| 1652 | step = "Executing post-scale vnf-config-primitive" |
| 1653 | if scaling_descriptor.get("scaling-config-action"): |
| 1654 | for scaling_config_action in scaling_descriptor["scaling-config-action"]: |
| 1655 | if scaling_config_action.get("trigger") and scaling_config_action["trigger"] == "post-scale-out" \ |
| 1656 | and scaling_type == "SCALE_OUT": |
| 1657 | vnf_config_primitive = scaling_config_action["vnf-config-primitive-name-ref"] |
| 1658 | step = db_nslcmop_update["detailed-status"] = \ |
| 1659 | "executing post-scale scaling-config-action '{}'".format(vnf_config_primitive) |
| 1660 | # look for primitive |
| 1661 | primitive_params = {} |
| 1662 | for config_primitive in db_vnfd.get("vnf-configuration", {}).get("config-primitive", ()): |
| 1663 | if config_primitive["name"] == vnf_config_primitive: |
| 1664 | for parameter in config_primitive.get("parameter", ()): |
| 1665 | if 'default-value' in parameter and \ |
| 1666 | parameter['default-value'] == "<VDU_SCALE_INFO>": |
| 1667 | primitive_params[parameter["name"]] = yaml.safe_dump(vdu_scaling_info, |
| 1668 | default_flow_style=True, |
| 1669 | width=256) |
| 1670 | break |
| 1671 | else: |
| 1672 | raise LcmException("Invalid vnfd descriptor at scaling-group-descriptor[name='{}']:" |
| 1673 | "scaling-config-action[vnf-config-primitive-name-ref='{}'] does not " |
| tierno | 47e86b5 | 2018-10-10 14:05:55 +0200 | [diff] [blame] | 1674 | "match any vnf-configuration:config-primitive".format(scaling_group, |
| 1675 | config_primitive)) |
| tierno | 9ab9594 | 2018-10-10 16:44:22 +0200 | [diff] [blame] | 1676 | scale_process = "VCA" |
| tierno | d6de199 | 2018-10-11 13:05:52 +0200 | [diff] [blame] | 1677 | db_nsr_update["config-status"] = "configuring post-scaling" |
| 1678 | |
| tierno | e4f7e6c | 2018-11-27 14:55:30 +0000 | [diff] [blame] | 1679 | result, result_detail = await self._ns_execute_primitive(nsr_deployed, nsr_name, vnf_index, |
| 1680 | None, None, None, vnf_config_primitive, |
| 1681 | primitive_params) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1682 | self.logger.debug(logging_text + "vnf_config_primitive={} Done with result {} {}".format( |
| 1683 | vnf_config_primitive, result, result_detail)) |
| 1684 | if result == "FAILED": |
| 1685 | raise LcmException(result_detail) |
| tierno | d6de199 | 2018-10-11 13:05:52 +0200 | [diff] [blame] | 1686 | db_nsr_update["config-status"] = old_config_status |
| 1687 | scale_process = None |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1688 | |
| 1689 | db_nslcmop_update["operationState"] = nslcmop_operation_state = "COMPLETED" |
| 1690 | db_nslcmop_update["statusEnteredTime"] = time() |
| 1691 | db_nslcmop_update["detailed-status"] = "done" |
| tierno | d6de199 | 2018-10-11 13:05:52 +0200 | [diff] [blame] | 1692 | db_nsr_update["detailed-status"] = "" # "scaled {} {}".format(scaling_group, scaling_type) |
| 1693 | db_nsr_update["operational-status"] = old_operational_status |
| 1694 | db_nsr_update["config-status"] = old_config_status |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1695 | return |
| 1696 | except (ROclient.ROClientException, DbException, LcmException) as e: |
| 1697 | self.logger.error(logging_text + "Exit Exception {}".format(e)) |
| 1698 | exc = e |
| 1699 | except asyncio.CancelledError: |
| 1700 | self.logger.error(logging_text + "Cancelled Exception while '{}'".format(step)) |
| 1701 | exc = "Operation was cancelled" |
| 1702 | except Exception as e: |
| 1703 | exc = traceback.format_exc() |
| 1704 | self.logger.critical(logging_text + "Exit Exception {} {}".format(type(e).__name__, e), exc_info=True) |
| 1705 | finally: |
| 1706 | if exc: |
| 1707 | if db_nslcmop: |
| 1708 | db_nslcmop_update["detailed-status"] = "FAILED {}: {}".format(step, exc) |
| 1709 | db_nslcmop_update["operationState"] = nslcmop_operation_state = "FAILED" |
| 1710 | db_nslcmop_update["statusEnteredTime"] = time() |
| 1711 | if db_nsr: |
| tierno | d6de199 | 2018-10-11 13:05:52 +0200 | [diff] [blame] | 1712 | db_nsr_update["operational-status"] = old_operational_status |
| 1713 | db_nsr_update["config-status"] = old_config_status |
| 1714 | db_nsr_update["detailed-status"] = "" |
| tierno | 47e86b5 | 2018-10-10 14:05:55 +0200 | [diff] [blame] | 1715 | db_nsr_update["_admin.nslcmop"] = None |
| tierno | d6de199 | 2018-10-11 13:05:52 +0200 | [diff] [blame] | 1716 | if scale_process: |
| 1717 | if "VCA" in scale_process: |
| 1718 | db_nsr_update["config-status"] = "failed" |
| 1719 | if "RO" in scale_process: |
| 1720 | db_nsr_update["operational-status"] = "failed" |
| 1721 | db_nsr_update["detailed-status"] = "FAILED scaling nslcmop={} {}: {}".format(nslcmop_id, step, |
| 1722 | exc) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1723 | if db_nslcmop_update: |
| 1724 | self.update_db_2("nslcmops", nslcmop_id, db_nslcmop_update) |
| tierno | 47e86b5 | 2018-10-10 14:05:55 +0200 | [diff] [blame] | 1725 | if db_nsr: |
| 1726 | db_nsr_update["_admin.nslcmop"] = None |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1727 | self.update_db_2("nsrs", nsr_id, db_nsr_update) |
| 1728 | if nslcmop_operation_state: |
| 1729 | try: |
| 1730 | await self.msg.aiowrite("ns", "scaled", {"nsr_id": nsr_id, "nslcmop_id": nslcmop_id, |
| 1731 | "operationState": nslcmop_operation_state}) |
| 1732 | # if cooldown_time: |
| 1733 | # await asyncio.sleep(cooldown_time) |
| 1734 | # await self.msg.aiowrite("ns","scaled-cooldown-time", {"nsr_id": nsr_id, "nslcmop_id": nslcmop_id}) |
| 1735 | except Exception as e: |
| 1736 | self.logger.error(logging_text + "kafka_write notification Exception {}".format(e)) |
| 1737 | self.logger.debug(logging_text + "Exit") |
| 1738 | self.lcm_tasks.remove("ns", nsr_id, nslcmop_id, "ns_scale") |