+ raise LcmException("ns_update_vnfr: Not found member_vnf_index={} from VIM info".format(vnf_index))
+
+ async def instantiate_RO(self, logging_text, nsr_id, nsd, db_nsr, db_nslcmop, db_vnfrs, db_vnfds_ref,
+ n2vc_key_list):
+ db_nsr_update = {}
+ RO_descriptor_number = 0 # number of descriptors created at RO
+ vnf_index_2_RO_id = {} # map between vnfd/nsd id to the id used at RO
+ start_deploy = time()
+ ns_params = db_nslcmop.get("operationParams")
+
+ # deploy RO
+ # get vnfds, instantiate at RO
+ for c_vnf in nsd.get("constituent-vnfd", ()):
+ member_vnf_index = c_vnf["member-vnf-index"]
+ vnfd = db_vnfds_ref[c_vnf['vnfd-id-ref']]
+ vnfd_ref = vnfd["id"]
+ step = db_nsr_update["detailed-status"] = "Creating vnfd='{}' member_vnf_index='{}' at RO".format(
+ vnfd_ref, member_vnf_index)
+ # self.logger.debug(logging_text + step)
+ vnfd_id_RO = "{}.{}.{}".format(nsr_id, RO_descriptor_number, member_vnf_index[:23])
+ vnf_index_2_RO_id[member_vnf_index] = vnfd_id_RO
+ RO_descriptor_number += 1
+
+ # look position at deployed.RO.vnfd if not present it will be appended at the end
+ for index, vnf_deployed in enumerate(db_nsr["_admin"]["deployed"]["RO"]["vnfd"]):
+ if vnf_deployed["member-vnf-index"] == member_vnf_index:
+ break
+ else:
+ index = len(db_nsr["_admin"]["deployed"]["RO"]["vnfd"])
+ db_nsr["_admin"]["deployed"]["RO"]["vnfd"].append(None)
+
+ # look if present
+ RO_update = {"member-vnf-index": member_vnf_index}
+ vnfd_list = await self.RO.get_list("vnfd", filter_by={"osm_id": vnfd_id_RO})
+ if vnfd_list:
+ RO_update["id"] = vnfd_list[0]["uuid"]
+ self.logger.debug(logging_text + "vnfd='{}' member_vnf_index='{}' exists at RO. Using RO_id={}".
+ format(vnfd_ref, member_vnf_index, vnfd_list[0]["uuid"]))
+ else:
+ vnfd_RO = self.vnfd2RO(vnfd, vnfd_id_RO, db_vnfrs[c_vnf["member-vnf-index"]].
+ get("additionalParamsForVnf"), nsr_id)
+ desc = await self.RO.create("vnfd", descriptor=vnfd_RO)
+ RO_update["id"] = desc["uuid"]
+ self.logger.debug(logging_text + "vnfd='{}' member_vnf_index='{}' created at RO. RO_id={}".format(
+ vnfd_ref, member_vnf_index, desc["uuid"]))
+ db_nsr_update["_admin.deployed.RO.vnfd.{}".format(index)] = RO_update
+ db_nsr["_admin"]["deployed"]["RO"]["vnfd"][index] = RO_update
+ self.update_db_2("nsrs", nsr_id, db_nsr_update)
+
+ # create nsd at RO
+ nsd_ref = nsd["id"]
+ step = db_nsr_update["detailed-status"] = "Creating nsd={} at RO".format(nsd_ref)
+ # self.logger.debug(logging_text + step)
+
+ RO_osm_nsd_id = "{}.{}.{}".format(nsr_id, RO_descriptor_number, nsd_ref[:23])
+ RO_descriptor_number += 1
+ nsd_list = await self.RO.get_list("nsd", filter_by={"osm_id": RO_osm_nsd_id})
+ if nsd_list:
+ db_nsr_update["_admin.deployed.RO.nsd_id"] = RO_nsd_uuid = nsd_list[0]["uuid"]
+ self.logger.debug(logging_text + "nsd={} exists at RO. Using RO_id={}".format(
+ nsd_ref, RO_nsd_uuid))
+ else:
+ nsd_RO = deepcopy(nsd)
+ nsd_RO["id"] = RO_osm_nsd_id
+ nsd_RO.pop("_id", None)
+ nsd_RO.pop("_admin", None)
+ for c_vnf in nsd_RO.get("constituent-vnfd", ()):
+ member_vnf_index = c_vnf["member-vnf-index"]
+ c_vnf["vnfd-id-ref"] = vnf_index_2_RO_id[member_vnf_index]
+ for c_vld in nsd_RO.get("vld", ()):
+ for cp in c_vld.get("vnfd-connection-point-ref", ()):
+ member_vnf_index = cp["member-vnf-index-ref"]
+ cp["vnfd-id-ref"] = vnf_index_2_RO_id[member_vnf_index]
+
+ desc = await self.RO.create("nsd", descriptor=nsd_RO)
+ db_nsr_update["_admin.nsState"] = "INSTANTIATED"
+ db_nsr_update["_admin.deployed.RO.nsd_id"] = RO_nsd_uuid = desc["uuid"]
+ self.logger.debug(logging_text + "nsd={} created at RO. RO_id={}".format(nsd_ref, RO_nsd_uuid))
+ self.update_db_2("nsrs", nsr_id, db_nsr_update)
+
+ # Crate ns at RO
+ # if present use it unless in error status
+ RO_nsr_id = deep_get(db_nsr, ("_admin", "deployed", "RO", "nsr_id"))
+ if RO_nsr_id:
+ try:
+ step = db_nsr_update["detailed-status"] = "Looking for existing ns at RO"
+ # self.logger.debug(logging_text + step + " RO_ns_id={}".format(RO_nsr_id))
+ desc = await self.RO.show("ns", RO_nsr_id)
+ except ROclient.ROClientException as e:
+ if e.http_code != HTTPStatus.NOT_FOUND:
+ raise
+ RO_nsr_id = db_nsr_update["_admin.deployed.RO.nsr_id"] = None
+ if RO_nsr_id:
+ ns_status, ns_status_info = self.RO.check_ns_status(desc)
+ db_nsr_update["_admin.deployed.RO.nsr_status"] = ns_status
+ if ns_status == "ERROR":
+ step = db_nsr_update["detailed-status"] = "Deleting ns at RO. RO_ns_id={}".format(RO_nsr_id)
+ self.logger.debug(logging_text + step)
+ await self.RO.delete("ns", RO_nsr_id)
+ RO_nsr_id = db_nsr_update["_admin.deployed.RO.nsr_id"] = None
+ if not RO_nsr_id:
+ step = db_nsr_update["detailed-status"] = "Checking dependencies"
+ # self.logger.debug(logging_text + step)
+
+ # check if VIM is creating and wait look if previous tasks in process
+ task_name, task_dependency = self.lcm_tasks.lookfor_related("vim_account", ns_params["vimAccountId"])
+ if task_dependency:
+ step = "Waiting for related tasks to be completed: {}".format(task_name)
+ self.logger.debug(logging_text + step)
+ await asyncio.wait(task_dependency, timeout=3600)
+ if ns_params.get("vnf"):
+ for vnf in ns_params["vnf"]:
+ if "vimAccountId" in vnf:
+ task_name, task_dependency = self.lcm_tasks.lookfor_related("vim_account",
+ vnf["vimAccountId"])
+ if task_dependency:
+ step = "Waiting for related tasks to be completed: {}".format(task_name)
+ self.logger.debug(logging_text + step)
+ await asyncio.wait(task_dependency, timeout=3600)
+
+ step = db_nsr_update["detailed-status"] = "Checking instantiation parameters"
+
+ RO_ns_params = self.ns_params_2_RO(ns_params, nsd, db_vnfds_ref, n2vc_key_list)
+
+ step = db_nsr_update["detailed-status"] = "Deploying ns at VIM"
+ desc = await self.RO.create("ns", descriptor=RO_ns_params,
+ name=db_nsr["name"],
+ scenario=RO_nsd_uuid)
+ RO_nsr_id = db_nsr_update["_admin.deployed.RO.nsr_id"] = desc["uuid"]
+ db_nsr_update["_admin.nsState"] = "INSTANTIATED"
+ db_nsr_update["_admin.deployed.RO.nsr_status"] = "BUILD"
+ self.logger.debug(logging_text + "ns created at RO. RO_id={}".format(desc["uuid"]))
+ self.update_db_2("nsrs", nsr_id, db_nsr_update)
+
+ # wait until NS is ready
+ step = ns_status_detailed = detailed_status = "Waiting VIM to deploy ns. RO_ns_id={}".format(RO_nsr_id)
+ detailed_status_old = None
+ self.logger.debug(logging_text + step)
+
+ while time() <= start_deploy + self.total_deploy_timeout:
+ desc = await self.RO.show("ns", RO_nsr_id)
+ ns_status, ns_status_info = self.RO.check_ns_status(desc)
+ db_nsr_update["_admin.deployed.RO.nsr_status"] = ns_status
+ if ns_status == "ERROR":
+ raise ROclient.ROClientException(ns_status_info)
+ elif ns_status == "BUILD":
+ detailed_status = ns_status_detailed + "; {}".format(ns_status_info)
+ elif ns_status == "ACTIVE":
+ step = detailed_status = "Waiting for management IP address reported by the VIM. Updating VNFRs"
+ try:
+ self.ns_update_vnfr(db_vnfrs, desc)
+ break
+ except LcmExceptionNoMgmtIP:
+ pass
+ else:
+ assert False, "ROclient.check_ns_status returns unknown {}".format(ns_status)
+ if detailed_status != detailed_status_old:
+ detailed_status_old = db_nsr_update["detailed-status"] = detailed_status
+ self.update_db_2("nsrs", nsr_id, db_nsr_update)
+ await asyncio.sleep(5, loop=self.loop)
+ else: # total_deploy_timeout
+ raise ROclient.ROClientException("Timeout waiting ns to be ready")
+
+ step = "Updating NSR"
+ self.ns_update_nsr(db_nsr_update, db_nsr, desc)
+
+ db_nsr_update["operational-status"] = "running"
+ db_nsr["detailed-status"] = "Configuring vnfr"
+ self.update_db_2("nsrs", nsr_id, db_nsr_update)
+
+ async def insert_key_ro(self, logging_text, pub_key, nsr_id, vnfr_id, vdu_id, vdu_index, user):
+ ro_nsr_id = None
+ ip_address = None
+ nb_tries = 0
+ target_vdu_id = None
+ while True:
+ await asyncio.sleep(10, loop=self.loop)
+ # 1 wait until NS is deployed at RO
+ if not ro_nsr_id:
+ db_nsrs = self.db.get_one("nsrs", {"_id": nsr_id})
+ ro_nsr_id = deep_get(db_nsrs, ("_admin", "deployed", "RO", "nsr_id"))
+ if not ro_nsr_id:
+ continue
+ if not target_vdu_id:
+ db_vnfr = self.db.get_one("vnfrs", {"_id": vnfr_id})
+ if not vdu_id:
+ ip_address = db_vnfr.get("ip-address")
+ if not ip_address:
+ continue
+ for vdur in get_iterable(db_vnfr, "vdur"):
+ if (vdur["vdu-id-ref"] == vdu_id and vdur["count-index"] == vdu_index) or \
+ (ip_address and vdur.get("ip-address") == ip_address):
+ if vdur["status"] == "ACTIVE":
+ target_vdu_id = vdur["vdu-id-ref"]
+ elif vdur["status"] == "ERROR":
+ raise LcmException("Cannot inject ssh-key because target VM is in error state")
+ break
+ else:
+ raise LcmException("Not found vnfr_id={}, vdu_index={}, vdu_index={}".format(
+ vnfr_id, vdu_id, vdu_index
+ ))
+ if not target_vdu_id:
+ continue
+ try:
+ ro_vm_id = "{}-{}".format(db_vnfr["member-vnf-index-ref"], target_vdu_id) # TODO add vdu_index
+ result_dict = await self.RO.create_action("ns", ro_nsr_id,
+ {"add_public_key": pub_key, "vms": [ro_vm_id], "user": user})
+ # result_dict contains the format {VM-id: {vim_result: 200, description: text}}
+ if not result_dict or not isinstance(result_dict, dict):
+ raise LcmException("Unknown response from RO when injecting key")
+ for result in result_dict.values():
+ if result.get("vim_result") == 200:
+ break
+ else:
+ raise ROclient.ROClientException("error injecting key: {}".format(
+ result.get("description")))
+ return
+ except ROclient.ROClientException as e:
+ nb_tries += 1
+ if nb_tries >= 10:
+ raise LcmException("Reaching max tries injecting key. Error: {}".format(e))
+ self.logger.debug(logging_text + "error injecting key: {}".format(e))
+
+ async def instantiate_N2VC(self, logging_text, vca_index, nsi_id, db_nsr, db_vnfr, vdu_id, vdu_index,
+ deploy_params, config_descriptor, base_folder):
+ nsr_id = db_nsr["_id"]
+ db_update_entry = "_admin.deployed.VCA.{}.".format(vca_index)
+ vca_deployed = db_nsr["_admin"]["deployed"]["VCA"][vca_index]
+ logging_text += "member_vnf_index={} vdu_id={}, vdu_index={} ".format(db_vnfr["member-vnf-index-ref"],
+ vdu_id, vdu_index)
+ step = ""
+ try:
+ vnfr_id = None
+ if db_vnfr:
+ vnfr_id = db_vnfr["_id"]
+
+ namespace = "{nsi}.{ns}".format(
+ nsi=nsi_id if nsi_id else "",
+ ns=nsr_id)
+ if vnfr_id:
+ namespace += "." + vnfr_id
+ if vdu_id:
+ namespace += ".{}-{}".format(vdu_id, vdu_index or 0)
+
+ # Get artifact path
+ storage_params = self.fs.get_params()
+ artifact_path = "{}{}/{}/charms/{}".format(
+ storage_params["path"],
+ base_folder["folder"],
+ base_folder["pkg-dir"],
+ config_descriptor["juju"]["charm"]
+ )
+
+ is_proxy_charm = True if config_descriptor["juju"]["charm"] else False
+ if config_descriptor["juju"].get("proxy") is False:
+ is_proxy_charm = False
+
+ # n2vc_redesign STEP 3.1
+ ee_id = vca_deployed.get("ee_id")
+ if not ee_id:
+ if is_proxy_charm:
+ step = "create execution envioronment"
+ self.logger.debug(logging_text + step)
+ ee_id = await self.n2vc.CreateExecutionEnvironment(namespace)
+
+ else:
+ step = "register execution envioronment"
+ # TODO wait until deployed by RO, when IP address has been filled. By pooling????
+ credentials = {} # TODO db_credentials["ip_address"]
+ # get username
+ # TODO remove this when changes on IM regarding config-access:ssh-access:default-user were
+ # merged. Meanwhile let's get username from initial-config-primitive
+ if config_descriptor.get("initial-config-primitive"):
+ for param in config_descriptor["initial-config-primitive"][0].get("parameter", ()):
+ if param["name"] == "ssh-username":
+ credentials["username"] = param["value"]
+ if config_descriptor.get("config-access") and config_descriptor["config-access"].get("ssh-access"):
+ if config_descriptor["config-access"]["ssh-access"].get("required"):
+ credentials["username"] = \
+ config_descriptor["config-access"]["ssh-access"].get("default-user")
+
+ # n2vc_redesign STEP 3.2
+ self.logger.debug(logging_text + step)
+ ee_id = await self.n2vc.RegisterExecutionEnvioronment(credentials)
+ # for compatibility with MON/POL modules, the need model and application name at database
+ # TODO ask to N2VC instead of assuming the format "model_name.application_name"
+ model_name, _, application_name = ee_id.partition(".")
+ self.update_db_2("nsrs", nsr_id, {db_update_entry + "model": model_name,
+ db_update_entry + "application": application_name,
+ db_update_entry + "ee_id": ee_id})
+
+ # n2vc_redesign STEP 3.3
+ # TODO check if already done
+ step = "Install configuration Software"
+ # TODO db_dict with filter: 'nsr_id', path:'db_update_entry'
+ pub_key = await self.n2vc.InstallConfigurationSW(self, ee_id, artifact_path)
+ if is_proxy_charm and pub_key:
+ # check if ssh key must be injected
+ required = deep_get(config_descriptor, ("config-access", "ssh-access", "required"))
+ # TODO get from config primitive: if required not user:
+ if required:
+ user = deep_get(config_descriptor, ("config-access", "ssh-access", "default-user"))
+ # insert pub_key into VM
+ # n2vc_redesign STEP 5.1
+ step = "Insert public key into VM"
+ await self.insert_key_ro(logging_text, pub_key, nsr_id, vnfr_id, vdu_id, vdu_index, user)
+ # TODO get ip_address and add to deploy params
+ deploy_params["rw_mgmt_ip"] = "TODO-ip-address"
+
+ # n2vc_redesign STEP 6 Execute initial config primitive
+ initial_config_primitive_list = config_descriptor.get('initial-config-primitive', [])
+ for initial_config_primitive in initial_config_primitive_list:
+ # TODO check if already done
+ primitive_params_ = self._map_primitive_params(initial_config_primitive, {}, deploy_params)
+ step = "execute primitive '{}' params '{}'".format(initial_config_primitive["name"], primitive_params_)
+ self.logger.debug(logging_text + step)
+ await self.n2vc.ExecutePrimitive(ee_id, initial_config_primitive["name"], primitive_params_)
+ # TODO register in database that primitive is done
+ except Exception as e: # TODO not use Exception but N2VC exception
+ raise Exception("{} {}".format(step, e))
+ # TODO raise N2VC exception with 'step' extra information