+ async def _instantiate_ng_ro(self, logging_text, nsr_id, nsd, db_nsr, db_nslcmop, db_vnfrs, db_vnfds_ref,
+ n2vc_key_list, stage, start_deploy, timeout_ns_deploy):
+
+ db_vims = {}
+
+ def get_vim_account(vim_account_id):
+ nonlocal db_vims
+ if vim_account_id in db_vims:
+ return db_vims[vim_account_id]
+ db_vim = self.db.get_one("vim_accounts", {"_id": vim_account_id})
+ db_vims[vim_account_id] = db_vim
+ return db_vim
+
+ # modify target_vld info with instantiation parameters
+ def parse_vld_instantiation_params(target_vim, target_vld, vld_params, target_sdn):
+ if vld_params.get("ip-profile"):
+ target_vld["vim_info"][target_vim]["ip_profile"] = vld_params["ip-profile"]
+ if vld_params.get("provider-network"):
+ target_vld["vim_info"][target_vim]["provider_network"] = vld_params["provider-network"]
+ if "sdn-ports" in vld_params["provider-network"] and target_sdn:
+ target_vld["vim_info"][target_sdn]["sdn-ports"] = vld_params["provider-network"]["sdn-ports"]
+ if vld_params.get("wimAccountId"):
+ target_wim = "wim:{}".format(vld_params["wimAccountId"])
+ target_vld["vim_info"][target_wim] = {}
+ for param in ("vim-network-name", "vim-network-id"):
+ if vld_params.get(param):
+ if isinstance(vld_params[param], dict):
+ pass
+ # for vim_account, vim_net in vld_params[param].items():
+ # TODO populate vim_info RO_vld_sites.append({
+ else: # isinstance str
+ target_vld["vim_info"][target_vim][param.replace("-", "_")] = vld_params[param]
+ # TODO if vld_params.get("ns-net"):
+
+ nslcmop_id = db_nslcmop["_id"]
+ target = {
+ "name": db_nsr["name"],
+ "ns": {"vld": []},
+ "vnf": [],
+ "image": deepcopy(db_nsr["image"]),
+ "flavor": deepcopy(db_nsr["flavor"]),
+ "action_id": nslcmop_id,
+ "cloud_init_content": {},
+ }
+ for image in target["image"]:
+ image["vim_info"] = {}
+ for flavor in target["flavor"]:
+ flavor["vim_info"] = {}
+
+ if db_nslcmop.get("lcmOperationType") != "instantiate":
+ # get parameters of instantiation:
+ db_nslcmop_instantiate = self.db.get_list("nslcmops", {"nsInstanceId": db_nslcmop["nsInstanceId"],
+ "lcmOperationType": "instantiate"})[-1]
+ ns_params = db_nslcmop_instantiate.get("operationParams")
+ else:
+ ns_params = db_nslcmop.get("operationParams")
+ ssh_keys = []
+ if ns_params.get("ssh_keys"):
+ ssh_keys += ns_params.get("ssh_keys")
+ if n2vc_key_list:
+ ssh_keys += n2vc_key_list
+
+ cp2target = {}
+ for vld_index, vld in enumerate(db_nsr.get("vld")):
+ target_vim = "vim:{}".format(ns_params["vimAccountId"])
+ target_vld = {
+ "id": vld["id"],
+ "name": vld["name"],
+ "mgmt-network": vld.get("mgmt-network", False),
+ "type": vld.get("type"),
+ "vim_info": {
+ target_vim: {"vim-network-name": vld.get("vim-network-name")}
+ }
+ }
+ # check if this network needs SDN assist
+ target_sdn = None
+ if vld.get("pci-interfaces"):
+ db_vim = get_vim_account(ns_params["vimAccountId"])
+ sdnc_id = db_vim["config"].get("sdn-controller")
+ if sdnc_id:
+ sdn_vld = "nsrs:{}:vld.{}".format(nsr_id, vld["id"])
+ target_sdn = "sdn:{}".format(sdnc_id)
+ target_vld["vim_info"][target_sdn] = {
+ "sdn": True, "target_vim": target_vim, "vlds": [sdn_vld], "type": vld.get("type")}
+
+ nsd_vld = next(v for v in nsd["vld"] if v["id"] == vld["id"])
+ for cp in nsd_vld["vnfd-connection-point-ref"]:
+ cp2target["member_vnf:{}.{}".format(cp["member-vnf-index-ref"], cp["vnfd-connection-point-ref"])] = \
+ "nsrs:{}:vld.{}".format(nsr_id, vld_index)
+
+ # check at nsd descriptor, if there is an ip-profile
+ vld_params = {}
+ if nsd_vld.get("ip-profile-ref"):
+ ip_profile = next(ipp for ipp in nsd["ip-profiles"] if ipp["name"] == nsd_vld["ip-profile-ref"])
+ vld_params["ip-profile"] = ip_profile["ip-profile-params"]
+ # update vld_params with instantiation params
+ vld_instantiation_params = next((v for v in get_iterable(ns_params, "vld")
+ if v["name"] in (vld["name"], vld["id"])), None)
+ if vld_instantiation_params:
+ vld_params.update(vld_instantiation_params)
+ parse_vld_instantiation_params(target_vim, target_vld, vld_params, target_sdn)
+ target["ns"]["vld"].append(target_vld)
+ for vnfr in db_vnfrs.values():
+ vnfd = db_vnfds_ref[vnfr["vnfd-ref"]]
+ vnf_params = next((v for v in get_iterable(ns_params, "vnf")
+ if v["member-vnf-index"] == vnfr["member-vnf-index-ref"]), None)
+ target_vnf = deepcopy(vnfr)
+ target_vim = "vim:{}".format(vnfr["vim-account-id"])
+ for vld in target_vnf.get("vld", ()):
+ # check if connected to a ns.vld, to fill target'
+ vnf_cp = next((cp for cp in vnfd.get("connection-point", ()) if
+ cp.get("internal-vld-ref") == vld["id"]), None)
+ if vnf_cp:
+ ns_cp = "member_vnf:{}.{}".format(vnfr["member-vnf-index-ref"], vnf_cp["id"])
+ if cp2target.get(ns_cp):
+ vld["target"] = cp2target[ns_cp]
+ vld["vim_info"] = {target_vim: {"vim-network-name": vld.get("vim-network-name")}}
+ # check if this network needs SDN assist
+ target_sdn = None
+ if vld.get("pci-interfaces"):
+ db_vim = get_vim_account(vnfr["vim-account-id"])
+ sdnc_id = db_vim["config"].get("sdn-controller")
+ if sdnc_id:
+ sdn_vld = "vnfrs:{}:vld.{}".format(target_vnf["_id"], vld["id"])
+ target_sdn = "sdn:{}".format(sdnc_id)
+ vld["vim_info"][target_sdn] = {
+ "sdn": True, "target_vim": target_vim, "vlds": [sdn_vld], "type": vld.get("type")}
+
+ # check at vnfd descriptor, if there is an ip-profile
+ vld_params = {}
+ vnfd_vld = next(v for v in vnfd["internal-vld"] if v["id"] == vld["id"])
+ if vnfd_vld.get("ip-profile-ref"):
+ ip_profile = next(ipp for ipp in vnfd["ip-profiles"] if ipp["name"] == vnfd_vld["ip-profile-ref"])
+ vld_params["ip-profile"] = ip_profile["ip-profile-params"]
+ # update vld_params with instantiation params
+ if vnf_params:
+ vld_instantiation_params = next((v for v in get_iterable(vnf_params, "internal-vld")
+ if v["name"] == vld["id"]), None)
+ if vld_instantiation_params:
+ vld_params.update(vld_instantiation_params)
+ parse_vld_instantiation_params(target_vim, vld, vld_params, target_sdn)
+
+ vdur_list = []
+ for vdur in target_vnf.get("vdur", ()):
+ if vdur.get("status") == "DELETING" or vdur.get("pdu-type"):
+ continue # This vdu must not be created
+ vdur["vim_info"] = {target_vim: {}}
+ vdud_index, vdud = next(k for k in enumerate(vnfd["vdu"]) if k[1]["id"] == vdur["vdu-id-ref"])
+
+ if ssh_keys:
+ if deep_get(vdud, ("vdu-configuration", "config-access", "ssh-access", "required")):
+ vdur["ssh-keys"] = ssh_keys
+ vdur["ssh-access-required"] = True
+ elif deep_get(vnfd, ("vnf-configuration", "config-access", "ssh-access", "required")) and \
+ any(iface.get("mgmt-vnf") for iface in vdur["interfaces"]):
+ vdur["ssh-keys"] = ssh_keys
+ vdur["ssh-access-required"] = True
+
+ # cloud-init
+ if vdud.get("cloud-init-file"):
+ vdur["cloud-init"] = "{}:file:{}".format(vnfd["_id"], vdud.get("cloud-init-file"))
+ # read file and put content at target.cloul_init_content. Avoid ng_ro to use shared package system
+ if vdur["cloud-init"] not in target["cloud_init_content"]:
+ base_folder = vnfd["_admin"]["storage"]
+ cloud_init_file = "{}/{}/cloud_init/{}".format(base_folder["folder"], base_folder["pkg-dir"],
+ vdud.get("cloud-init-file"))
+ with self.fs.file_open(cloud_init_file, "r") as ci_file:
+ target["cloud_init_content"][vdur["cloud-init"]] = ci_file.read()
+ elif vdud.get("cloud-init"):
+ vdur["cloud-init"] = "{}:vdu:{}".format(vnfd["_id"], vdud_index)
+ # put content at target.cloul_init_content. Avoid ng_ro read vnfd descriptor
+ target["cloud_init_content"][vdur["cloud-init"]] = vdud["cloud-init"]
+ vdur["additionalParams"] = vdur.get("additionalParams") or {}
+ deploy_params_vdu = self._format_additional_params(vdur.get("additionalParams") or {})
+ deploy_params_vdu["OSM"] = self._get_osm_params(vnfr, vdur["vdu-id-ref"], vdur["count-index"])
+ vdur["additionalParams"] = deploy_params_vdu
+
+ # flavor
+ ns_flavor = target["flavor"][int(vdur["ns-flavor-id"])]
+ if target_vim not in ns_flavor["vim_info"]:
+ ns_flavor["vim_info"][target_vim] = {}
+ # image
+ ns_image = target["image"][int(vdur["ns-image-id"])]
+ if target_vim not in ns_image["vim_info"]:
+ ns_image["vim_info"][target_vim] = {}
+
+ vdur["vim_info"] = {target_vim: {}}
+ # instantiation parameters
+ # if vnf_params:
+ # vdu_instantiation_params = next((v for v in get_iterable(vnf_params, "vdu") if v["id"] ==
+ # vdud["id"]), None)
+ vdur_list.append(vdur)
+ target_vnf["vdur"] = vdur_list
+ target["vnf"].append(target_vnf)
+
+ desc = await self.RO.deploy(nsr_id, target)
+ action_id = desc["action_id"]
+ await self._wait_ng_ro(nsr_id, action_id, nslcmop_id, start_deploy, timeout_ns_deploy, stage)
+
+ # Updating NSR
+ db_nsr_update = {
+ "_admin.deployed.RO.operational-status": "running",
+ "detailed-status": " ".join(stage)
+ }
+ # db_nsr["_admin.deployed.RO.detailed-status"] = "Deployed at VIM"
+ self.update_db_2("nsrs", nsr_id, db_nsr_update)
+ self._write_op_status(nslcmop_id, stage)
+ self.logger.debug(logging_text + "ns deployed at RO. RO_id={}".format(action_id))
+ return
+
+ async def _wait_ng_ro(self, nsr_id, action_id, nslcmop_id=None, start_time=None, timeout=600, stage=None):
+ detailed_status_old = None
+ db_nsr_update = {}
+ start_time = start_time or time()
+ while time() <= start_time + timeout:
+ desc_status = await self.RO.status(nsr_id, action_id)
+ if desc_status["status"] == "FAILED":
+ raise NgRoException(desc_status["details"])
+ elif desc_status["status"] == "BUILD":
+ if stage:
+ stage[2] = "VIM: ({})".format(desc_status["details"])
+ elif desc_status["status"] == "DONE":
+ if stage:
+ stage[2] = "Deployed at VIM"
+ break
+ else:
+ assert False, "ROclient.check_ns_status returns unknown {}".format(desc_status["status"])
+ if stage and nslcmop_id and stage[2] != detailed_status_old:
+ detailed_status_old = stage[2]
+ db_nsr_update["detailed-status"] = " ".join(stage)
+ self.update_db_2("nsrs", nsr_id, db_nsr_update)
+ self._write_op_status(nslcmop_id, stage)
+ await asyncio.sleep(5, loop=self.loop)
+ else: # timeout_ns_deploy
+ raise NgRoException("Timeout waiting ns to deploy")
+
+ async def _terminate_ng_ro(self, logging_text, nsr_deployed, nsr_id, nslcmop_id, stage):
+ db_nsr_update = {}
+ failed_detail = []
+ action_id = None
+ start_deploy = time()
+ try:
+ target = {
+ "ns": {"vld": []},
+ "vnf": [],
+ "image": [],
+ "flavor": [],
+ "action_id": nslcmop_id
+ }
+ desc = await self.RO.deploy(nsr_id, target)
+ action_id = desc["action_id"]
+ db_nsr_update["_admin.deployed.RO.nsr_delete_action_id"] = action_id
+ db_nsr_update["_admin.deployed.RO.nsr_status"] = "DELETING"
+ self.logger.debug(logging_text + "ns terminate action at RO. action_id={}".format(action_id))
+
+ # wait until done
+ delete_timeout = 20 * 60 # 20 minutes
+ await self._wait_ng_ro(nsr_id, action_id, nslcmop_id, start_deploy, delete_timeout, stage)
+
+ db_nsr_update["_admin.deployed.RO.nsr_delete_action_id"] = None
+ db_nsr_update["_admin.deployed.RO.nsr_status"] = "DELETED"
+ # delete all nsr
+ await self.RO.delete(nsr_id)
+ except Exception as e:
+ if isinstance(e, NgRoException) and e.http_code == 404: # not found
+ db_nsr_update["_admin.deployed.RO.nsr_id"] = None
+ db_nsr_update["_admin.deployed.RO.nsr_status"] = "DELETED"
+ db_nsr_update["_admin.deployed.RO.nsr_delete_action_id"] = None
+ self.logger.debug(logging_text + "RO_action_id={} already deleted".format(action_id))
+ elif isinstance(e, NgRoException) and e.http_code == 409: # conflict
+ failed_detail.append("delete conflict: {}".format(e))
+ self.logger.debug(logging_text + "RO_action_id={} delete conflict: {}".format(action_id, e))
+ else:
+ failed_detail.append("delete error: {}".format(e))
+ self.logger.error(logging_text + "RO_action_id={} delete error: {}".format(action_id, e))
+
+ if failed_detail:
+ stage[2] = "Error deleting from VIM"
+ else:
+ stage[2] = "Deleted from VIM"
+ db_nsr_update["detailed-status"] = " ".join(stage)
+ self.update_db_2("nsrs", nsr_id, db_nsr_update)
+ self._write_op_status(nslcmop_id, stage)
+
+ if failed_detail:
+ raise LcmException("; ".join(failed_detail))
+ return
+