X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_lcm%2Fns.py;h=ec0fe0ea1b97df6f348c77c52d314355781aaed4;hb=8a5188754662c19e2471e5e7a1172a3bccea669e;hp=1aaf2c56dfa91fac22e71adf89a0c66c384f8965;hpb=7c76ad9242e92a917b700db41e044982666c9bda;p=osm%2FLCM.git diff --git a/osm_lcm/ns.py b/osm_lcm/ns.py index 1aaf2c5..ec0fe0e 100644 --- a/osm_lcm/ns.py +++ b/osm_lcm/ns.py @@ -22,6 +22,7 @@ import logging import logging.handlers import functools import traceback +from jinja2 import Environment, Template, meta, TemplateError, TemplateNotFound, TemplateSyntaxError import ROclient from lcm_utils import LcmException, LcmExceptionNoMgmtIP, LcmBase @@ -99,46 +100,61 @@ class NsLcm(LcmBase): artifacts=None, ) - def vnfd2RO(self, vnfd, new_id=None): + def vnfd2RO(self, vnfd, new_id=None, additionalParams=None, nsrId=None): """ Converts creates a new vnfd descriptor for RO base on input OSM IM vnfd :param vnfd: input vnfd :param new_id: overrides vnf id if provided + :param additionalParams: Instantiation params for VNFs provided + :param nsrId: Id of the NSR :return: copy of vnfd """ - ci_file = None try: vnfd_RO = deepcopy(vnfd) + # remove unused by RO configuration, monitoring, scaling and internal keys vnfd_RO.pop("_id", None) vnfd_RO.pop("_admin", None) + vnfd_RO.pop("vnf-configuration", None) + vnfd_RO.pop("monitoring-param", None) + vnfd_RO.pop("scaling-group-descriptor", None) if new_id: vnfd_RO["id"] = new_id - for vdu in vnfd_RO.get("vdu", ()): - if "cloud-init-file" in vdu: + + # parse cloud-init or cloud-init-file with the provided variables using Jinja2 + for vdu in get_iterable(vnfd_RO, "vdu"): + cloud_init_file = None + if vdu.get("cloud-init-file"): base_folder = vnfd["_admin"]["storage"] - clout_init_file = "{}/{}/cloud_init/{}".format( - base_folder["folder"], - base_folder["pkg-dir"], - vdu["cloud-init-file"] - ) - ci_file = self.fs.file_open(clout_init_file, "r") - # TODO: detect if binary or text. Propose to read as binary and try to decode to utf8. If fails - # convert to base 64 or similar - clout_init_content = ci_file.read() - ci_file.close() - ci_file = None + cloud_init_file = "{}/{}/cloud_init/{}".format(base_folder["folder"], base_folder["pkg-dir"], + vdu["cloud-init-file"]) + with self.fs.file_open(cloud_init_file, "r") as ci_file: + cloud_init_content = ci_file.read() vdu.pop("cloud-init-file", None) - vdu["cloud-init"] = clout_init_content - # remnove unused by RO configuration, monitoring, scaling - vnfd_RO.pop("vnf-configuration", None) - vnfd_RO.pop("monitoring-param", None) - vnfd_RO.pop("scaling-group-descriptor", None) + elif vdu.get("cloud-init"): + cloud_init_content = vdu["cloud-init"] + else: + continue + + env = Environment() + ast = env.parse(cloud_init_content) + mandatory_vars = meta.find_undeclared_variables(ast) + if mandatory_vars: + for var in mandatory_vars: + if not additionalParams or var not in additionalParams.keys(): + raise LcmException("Variable '{}' defined at vnfd[id={}]:vdu[id={}]:cloud-init/cloud-init-" + "file, must be provided in the instantiation parameters inside the " + "'additionalParamsForVnf' block".format(var, vnfd["id"], vdu["id"])) + template = Template(cloud_init_content) + cloud_init_content = template.render(additionalParams) + vdu["cloud-init"] = cloud_init_content + return vnfd_RO except FsException as e: - raise LcmException("Error reading file at vnfd {}: {} ".format(vnfd["_id"], e)) - finally: - if ci_file: - ci_file.close() + raise LcmException("Error reading vnfd[id={}]:vdu[id={}]:cloud-init-file={}: {}". + format(vnfd["id"], vdu[id], cloud_init_file, e)) + except (TemplateError, TemplateNotFound, TemplateSyntaxError) as e: + raise LcmException("Error parsing Jinja2 to cloud-init content at vnfd[id={}]:vdu[id={}]: {}". + format(vnfd["id"], vdu["id"], e)) def n2vc_callback(self, model_name, application_name, status, message, n2vc_info, task=None): """ @@ -589,7 +605,7 @@ class NsLcm(LcmBase): nslcmop_operation_state = None db_vnfrs = {} RO_descriptor_number = 0 # number of descriptors created at RO - descriptor_id_2_RO = {} # map between vnfd/nsd id to the id used at RO + vnf_index_2_RO_id = {} # map between vnfd/nsd id to the id used at RO n2vc_info = {} exc = None try: @@ -648,12 +664,13 @@ class NsLcm(LcmBase): self.update_db_2("nsrs", nsr_id, db_nsr_update) # get vnfds, instantiate at RO - for vnfd_id, vnfd in db_vnfds.items(): + for c_vnf in nsd.get("constituent-vnfd", ()): + member_vnf_index = c_vnf["member-vnf-index"] vnfd_ref = vnfd["id"] step = db_nsr_update["detailed-status"] = "Creating vnfd={} at RO".format(vnfd_ref) # self.logger.debug(logging_text + step) - vnfd_id_RO = "{}.{}.{}".format(nsr_id, RO_descriptor_number, vnfd_ref[:23]) - descriptor_id_2_RO[vnfd_ref] = vnfd_id_RO + 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 if present @@ -663,7 +680,8 @@ class NsLcm(LcmBase): self.logger.debug(logging_text + "vnfd={} exists at RO. Using RO_id={}".format( vnfd_ref, vnfd_list[0]["uuid"])) else: - vnfd_RO = self.vnfd2RO(vnfd, vnfd_id_RO) + vnfd_RO = self.vnfd2RO(vnfd, vnfd_id_RO, db_vnfrs[c_vnf["member-vnf-index"]]. + get("additionalParamsForVnf"), nsr_id) desc = await RO.create("vnfd", descriptor=vnfd_RO) db_nsr_update["_admin.deployed.RO.vnfd_id.{}".format(vnfd_id)] = desc["uuid"] self.logger.debug(logging_text + "vnfd={} created at RO. RO_id={}".format( @@ -676,7 +694,6 @@ class NsLcm(LcmBase): # self.logger.debug(logging_text + step) RO_osm_nsd_id = "{}.{}.{}".format(nsr_id, RO_descriptor_number, nsd_ref[:23]) - descriptor_id_2_RO[nsd_ref] = RO_osm_nsd_id RO_descriptor_number += 1 nsd_list = await RO.get_list("nsd", filter_by={"osm_id": RO_osm_nsd_id}) if nsd_list: @@ -689,8 +706,13 @@ class NsLcm(LcmBase): nsd_RO.pop("_id", None) nsd_RO.pop("_admin", None) for c_vnf in nsd_RO.get("constituent-vnfd", ()): - vnfd_id = c_vnf["vnfd-id-ref"] - c_vnf["vnfd-id-ref"] = descriptor_id_2_RO[vnfd_id] + 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 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"] @@ -791,12 +813,11 @@ class NsLcm(LcmBase): # The parameters we'll need to deploy a charm number_to_configure = 0 - def deploy_charm(vnf_index, vdu_id, vdu_name, vdu_count_index, mgmt_ip_address, n2vc_info, - config_primitive=None): + def deploy_charm(vnf_index, vdu_id, vdu_name, vdu_count_index, charm_params, n2vc_info): """An inner function to deploy the charm from either vnf or vdu vnf_index is mandatory. vdu_id can be None for a vnf configuration or the id for vdu configuration """ - if not mgmt_ip_address: + if not charm_params["rw_mgmt_ip"]: raise LcmException("vnfd/vdu has not management ip address to configure it") # Login to the VCA. # if number_to_configure == 0: @@ -818,11 +839,6 @@ class NsLcm(LcmBase): proxy_charm ) - # Setup the runtime parameters for this VNF - params = {'rw_mgmt_ip': mgmt_ip_address} - if config_primitive: - params["initial-config-primitive"] = config_primitive - # ns_name will be ignored in the current version of N2VC # but will be implemented for the next point release. model_name = "default" # TODO bug 585 nsr_id @@ -867,10 +883,10 @@ class NsLcm(LcmBase): application_name, # The application name vnfd, # The vnf descriptor charm_path, # Path to charm - params, # Runtime params, like mgmt ip + charm_params, # Runtime params, like mgmt ip {}, # for native charms only self.n2vc_callback, # Callback for status changes - n2vc_info, # Callback parameter + n2vc_info, # Callback parameter None, # Callback parameter (task) ) ) @@ -886,37 +902,42 @@ class NsLcm(LcmBase): vnf_index = str(c_vnf["member-vnf-index"]) vnfd = db_vnfds_ref[vnfd_id] + # Get additional parameters + vnfr_params = {} + if db_vnfrs[vnf_index].get("additionalParamsForVnf"): + vnfr_params = db_vnfrs[vnf_index]["additionalParamsForVnf"].copy() + for k, v in vnfr_params.items(): + if isinstance(v, str) and v.startswith("!!yaml "): + vnfr_params[k] = yaml.safe_load(v[7:]) + # Check if this VNF has a charm configuration vnf_config = vnfd.get("vnf-configuration") - if vnf_config and vnf_config.get("juju"): proxy_charm = vnf_config["juju"]["charm"] - config_primitive = None if proxy_charm: - if 'initial-config-primitive' in vnf_config: - config_primitive = vnf_config['initial-config-primitive'] + step = "connecting to N2VC to configure vnf {}".format(vnf_index) + vnfr_params["rw_mgmt_ip"] = db_vnfrs[vnf_index]["ip-address"] + charm_params = { + "user_values": vnfr_params, + "rw_mgmt_ip": db_vnfrs[vnf_index]["ip-address"], + "initial-config-primitive": vnf_config.get('initial-config-primitive') or {} + } # Login to the VCA. If there are multiple calls to login(), # subsequent calls will be a nop and return immediately. - step = "connecting to N2VC to configure vnf {}".format(vnf_index) await self.n2vc.login() - deploy_charm(vnf_index, None, None, None, db_vnfrs[vnf_index]["ip-address"], n2vc_info, - config_primitive) + deploy_charm(vnf_index, None, None, None, charm_params, n2vc_info) number_to_configure += 1 # Deploy charms for each VDU that supports one. for vdu_index, vdu in enumerate(get_iterable(vnfd, 'vdu')): vdu_config = vdu.get('vdu-configuration') proxy_charm = None - config_primitive = None if vdu_config and vdu_config.get("juju"): proxy_charm = vdu_config["juju"]["charm"] - if 'initial-config-primitive' in vdu_config: - config_primitive = vdu_config['initial-config-primitive'] - if proxy_charm: step = "connecting to N2VC to configure vdu {} from vnf {}".format(vdu["id"], vnf_index) await self.n2vc.login() @@ -925,8 +946,14 @@ class NsLcm(LcmBase): if vdur["vdu-id-ref"] != vdu["id"]: raise LcmException("Mismatch vdur {}, vdu {} at index {} for vnf {}" .format(vdur["vdu-id-ref"], vdu["id"], vdu_index, vnf_index)) + vnfr_params["rw_mgmt_ip"] = vdur["ip-address"] + charm_params = { + "user_values": vnfr_params, + "rw_mgmt_ip": vdur["ip-address"], + "initial-config-primitive": vdu_config.get('initial-config-primitive') or {} + } deploy_charm(vnf_index, vdu["id"], vdur.get("name"), vdur["count-index"], - vdur["ip-address"], n2vc_info, config_primitive) + charm_params, n2vc_info) number_to_configure += 1 db_nsr_update["operational-status"] = "running" @@ -943,7 +970,7 @@ class NsLcm(LcmBase): self.update_db_2("nsrs", nsr_id, db_nsr_update) if db_nslcmop_update: self.update_db_2("nslcmops", nslcmop_id, db_nslcmop_update) - # TODO add a fake tast that set n2vc_event after some time + # TODO add a fake task that set n2vc_event after some time await n2vc_info["n2vc_event"].wait() n2vc_info["n2vc_event"].clear() all_active = True @@ -1041,7 +1068,8 @@ class NsLcm(LcmBase): if nslcmop_operation_state: try: await self.msg.aiowrite("ns", "instantiated", {"nsr_id": nsr_id, "nslcmop_id": nslcmop_id, - "operationState": nslcmop_operation_state}) + "operationState": nslcmop_operation_state}, + loop=self.loop) except Exception as e: self.logger.error(logging_text + "kafka_write notification Exception {}".format(e)) @@ -1304,7 +1332,8 @@ class NsLcm(LcmBase): if nslcmop_operation_state: try: await self.msg.aiowrite("ns", "terminated", {"nsr_id": nsr_id, "nslcmop_id": nslcmop_id, - "operationState": nslcmop_operation_state}) + "operationState": nslcmop_operation_state}, + loop=self.loop) except Exception as e: self.logger.error(logging_text + "kafka_write notification Exception {}".format(e)) self.logger.debug(logging_text + "Exit") @@ -1446,7 +1475,8 @@ class NsLcm(LcmBase): if nslcmop_operation_state: try: await self.msg.aiowrite("ns", "actioned", {"nsr_id": nsr_id, "nslcmop_id": nslcmop_id, - "operationState": nslcmop_operation_state}) + "operationState": nslcmop_operation_state}, + loop=self.loop) except Exception as e: self.logger.error(logging_text + "kafka_write notification Exception {}".format(e)) self.logger.debug(logging_text + "Exit") @@ -1790,7 +1820,8 @@ class NsLcm(LcmBase): if nslcmop_operation_state: try: await self.msg.aiowrite("ns", "scaled", {"nsr_id": nsr_id, "nslcmop_id": nslcmop_id, - "operationState": nslcmop_operation_state}) + "operationState": nslcmop_operation_state}, + loop=self.loop) # if cooldown_time: # await asyncio.sleep(cooldown_time) # await self.msg.aiowrite("ns","scaled-cooldown-time", {"nsr_id": nsr_id, "nslcmop_id": nslcmop_id})