X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=rwlaunchpad%2Fplugins%2Frwnsm%2Frift%2Ftasklets%2Frwnsmtasklet%2Fopenmano_nsm.py;h=3761d1bae17b929f540d228e816eccc9b6212012;hb=3552a87cfd930396cce2eda8d4cb66b71c88df68;hp=cf9b656704870f6407f9a6afbc21f15f307c4de9;hpb=d7624b3f563465323973453d273b3f30fc9d1c97;p=osm%2FSO.git diff --git a/rwlaunchpad/plugins/rwnsm/rift/tasklets/rwnsmtasklet/openmano_nsm.py b/rwlaunchpad/plugins/rwnsm/rift/tasklets/rwnsmtasklet/openmano_nsm.py index cf9b6567..3761d1ba 100644 --- a/rwlaunchpad/plugins/rwnsm/rift/tasklets/rwnsmtasklet/openmano_nsm.py +++ b/rwlaunchpad/plugins/rwnsm/rift/tasklets/rwnsmtasklet/openmano_nsm.py @@ -16,25 +16,27 @@ # import asyncio +import gi import os import sys import time import yaml -import gi gi.require_version('RwDts', '1.0') gi.require_version('RwVnfrYang', '1.0') from gi.repository import ( RwDts as rwdts, RwVnfrYang, ) +gi.require_version('RwKeyspec', '1.0') +from gi.repository.RwKeyspec import quoted_key import rift.openmano.rift2openmano as rift2openmano import rift.openmano.openmano_client as openmano_client -from . import rwnsmplugin +from . import nsmpluginbase from enum import Enum - +import ipaddress import rift.tasklets if sys.version_info < (3, 4, 4): @@ -42,7 +44,7 @@ if sys.version_info < (3, 4, 4): DUMP_OPENMANO_DIR = os.path.join( - os.environ["RIFT_ARTIFACTS"], + os.environ["RIFT_VAR_ROOT"], "openmano_descriptors" ) @@ -78,9 +80,12 @@ class VnfrConsoleOperdataDtsHandler(object): @property def vnfr_vdu_console_xpath(self): """ path for resource-mgr""" - return ("D,/rw-vnfr:vnfr-console/rw-vnfr:vnfr[rw-vnfr:id='{}']/rw-vnfr:vdur[vnfr:id='{}']".format(self._vnfr_id,self._vdur_id)) + return self._project.add_project( + "D,/rw-vnfr:vnfr-console/rw-vnfr:vnfr[rw-vnfr:id={}]/rw-vnfr:vdur[vnfr:id={}]".format( + quoted_key(self._vnfr_id), quoted_key(self._vdur_id))) - def __init__(self, dts, log, loop, nsr, vnfr_id, vdur_id, vdu_id): + def __init__(self, project, dts, log, loop, nsr, vnfr_id, vdur_id, vdu_id): + self._project = project self._dts = dts self._log = log self._loop = loop @@ -105,7 +110,7 @@ class VnfrConsoleOperdataDtsHandler(object): ) if action == rwdts.QueryAction.READ: - schema = RwVnfrYang.YangData_RwVnfr_VnfrConsole_Vnfr_Vdur.schema() + schema = RwVnfrYang.YangData_RwProject_Project_VnfrConsole_Vnfr_Vdur.schema() path_entry = schema.keyspec_to_entry(ks_path) try: @@ -117,11 +122,11 @@ class VnfrConsoleOperdataDtsHandler(object): ) self._log.debug("Got console response: %s for NSR ID %s vdur ID %s", - console_url, - self._nsr._nsr_uuid, - self._vdur_id - ) - vdur_console = RwVnfrYang.YangData_RwVnfr_VnfrConsole_Vnfr_Vdur() + console_url, + self._nsr._nsr_uuid, + self._vdur_id + ) + vdur_console = RwVnfrYang.YangData_RwProject_Project_VnfrConsole_Vnfr_Vdur() vdur_console.id = self._vdur_id if console_url: vdur_console.console_url = console_url @@ -130,8 +135,8 @@ class VnfrConsoleOperdataDtsHandler(object): self._log.debug("Recevied console URL for vdu {} is {}".format(self._vdu_id,vdur_console)) except openmano_client.InstanceStatusError as e: self._log.error("Could not get NS instance console URL: %s", - str(e)) - vdur_console = RwVnfrYang.YangData_RwVnfr_VnfrConsole_Vnfr_Vdur() + str(e)) + vdur_console = RwVnfrYang.YangData_RwProject_Project_VnfrConsole_Vnfr_Vdur() vdur_console.id = self._vdur_id vdur_console.console_url = 'none' @@ -156,10 +161,11 @@ class VnfrConsoleOperdataDtsHandler(object): class OpenmanoVnfr(object): - def __init__(self, log, loop, cli_api, vnfr, nsd): + def __init__(self, log, loop, cli_api, http_api, vnfr, nsd, ssh_key=None): self._log = log self._loop = loop self._cli_api = cli_api + self._http_api = http_api self._vnfr = vnfr self._vnfd_id = vnfr.vnfd.id @@ -168,6 +174,7 @@ class OpenmanoVnfr(object): self._created = False self.nsd = nsd + self._ssh_key = ssh_key @property def vnfd(self): @@ -188,7 +195,7 @@ class OpenmanoVnfr(object): @property def openmano_vnfd(self): self._log.debug("Converting vnfd %s from rift to openmano", self.vnfd.id) - openmano_vnfd = rift2openmano.rift2openmano_vnfd(self.vnfd, self.nsd) + openmano_vnfd = rift2openmano.rift2openmano_vnfd(self.vnfd, self.nsd, self._http_api) return openmano_vnfd @property @@ -197,36 +204,19 @@ class OpenmanoVnfr(object): @asyncio.coroutine def create(self): - self._log.debug("Creating openmano vnfd") - openmano_vnfd = self.openmano_vnfd - name = openmano_vnfd["vnf"]["name"] - - # If the name already exists, get the openmano vnfd id - name_uuid_map = yield from self._loop.run_in_executor( - None, - self._cli_api.vnf_list, - ) - - if name in name_uuid_map: - vnf_id = name_uuid_map[name] - self._log.debug("Vnf already created. Got existing openmano vnfd id: %s", vnf_id) - self._vnf_id = vnf_id - return - - self._vnf_id, _ = yield from self._loop.run_in_executor( - None, - self._cli_api.vnf_create, - self.openmano_vnfd_yaml, - ) - - fpath = dump_openmano_descriptor( - "{}_vnf".format(name), - self.openmano_vnfd_yaml - ) + try: + self._log.debug("Created openmano vnfd") + # The self.openmano_vnfd_yaml internally creates the vnf if not found. + # Assigning the yaml to a variable so that the api is not fired unnecessarily. + openmano_vnfd = self.openmano_vnfd + name = openmano_vnfd["name"] - self._log.debug("Dumped Openmano VNF descriptor to: %s", fpath) + self._vnf_id = openmano_vnfd['uuid'] - self._created = True + self._created = True + except Exception as e: + self._log.error("Failed to create vnf on Openmano RO : %s", e) + raise e def delete(self): if not self._created: @@ -236,9 +226,11 @@ class OpenmanoVnfr(object): if self._vnf_id is None: self._log.warning("Openmano vnf id not set. Cannot delete.") return - - self._cli_api.vnf_delete(self._vnf_id) - + try: + self._cli_api.vnf_delete(self._vnf_id) + except Exception as e: + self._log.error(e) + raise e class OpenmanoNSRecordState(Enum): """ Network Service Record State """ @@ -260,7 +252,9 @@ class OpenmanoNsr(object): TIMEOUT_SECS = 300 INSTANCE_TERMINATE_TIMEOUT = 60 - def __init__(self, dts, log, loop, publisher, cli_api, http_api, nsd_msg, nsr_config_msg,key_pairs,rift_vnfd_id=None ): + def __init__(self, project, dts, log, loop, publisher, cli_api, http_api, nsd_msg, + nsr_config_msg, key_pairs, ssh_key, rift_vnfd_id=None ): + self._project = project self._log = log self._dts = dts self._loop = loop @@ -275,6 +269,7 @@ class OpenmanoNsr(object): self._nsrs = {} self._vdur_console_handler = {} self._key_pairs = key_pairs + self._ssh_key = ssh_key self._nsd_uuid = None self._nsr_uuid = None @@ -288,6 +283,9 @@ class OpenmanoNsr(object): self._rift_vnfd_id = rift_vnfd_id self._state = OpenmanoNSRecordState.INIT + self._active_vms = 0 + self._active_nets = 0 + @property def nsd(self): return rift2openmano.RiftNSD(self._nsd_msg) @@ -329,17 +327,21 @@ class OpenmanoNsr(object): def vlrs(self): return self._vlrs + @property + def http_api(self): + return self._http_api + @property def openmano_nsd_yaml(self): self._log.debug("Converting nsd %s from rift to openmano", self.nsd.id) - openmano_nsd = rift2openmano.rift2openmano_nsd(self.nsd, self.vnfds,self.vnfr_ids) + openmano_nsd = rift2openmano.rift2openmano_nsd(self.nsd, self.vnfds,self.vnfr_ids, self.http_api) return yaml.safe_dump(openmano_nsd, default_flow_style=False) @property def openmano_scaling_yaml(self): self._log.debug("Creating Openmano Scaling Descriptor %s") try: - openmano_vnfd_nsd = rift2openmano.rift2openmano_vnfd_nsd(self.nsd, self.vnfds, self.vnfr_ids, self._rift_vnfd_id) + openmano_vnfd_nsd = rift2openmano.rift2openmano_vnfd_nsd(self.nsd, self.vnfds, self.vnfr_ids, self.http_api, self._rift_vnfd_id) return yaml.safe_dump(openmano_vnfd_nsd, default_flow_style=False) except Exception as e: self._log.exception("Scaling Descriptor Exception: %s", str(e)) @@ -356,6 +358,10 @@ class OpenmanoNsr(object): self._log.debug("Key pair NSD is %s",authorized_key) key_pairs.append(authorized_key.key) + if self._ssh_key['public_key']: + self._log.debug("Pub key NSD is %s", self._ssh_key['public_key']) + key_pairs.append(self._ssh_key['public_key']) + if key_pairs: cloud_config["key-pairs"] = key_pairs @@ -388,110 +394,135 @@ class OpenmanoNsr(object): @property def openmano_instance_create_yaml(self): - self._log.debug("Creating instance-scenario-create input file for nsd %s with name %s", self.nsd.id, self._nsr_config_msg.name) - openmano_instance_create = {} - openmano_instance_create["name"] = self._nsr_config_msg.name - openmano_instance_create["description"] = self._nsr_config_msg.description - openmano_instance_create["scenario"] = self._nsd_uuid - - cloud_config = self.get_ssh_key_pairs() - if cloud_config: - openmano_instance_create["cloud-config"] = cloud_config - if self._nsr_config_msg.has_field("om_datacenter"): - openmano_instance_create["datacenter"] = self._nsr_config_msg.om_datacenter - openmano_instance_create["vnfs"] = {} - for vnfr in self._vnfrs: - if "om_datacenter" in vnfr.vnfr.vnfr_msg: - vnfr_name = vnfr.vnfr.vnfd.name + "__" + str(vnfr.vnfr.vnfr_msg.member_vnf_index_ref) - openmano_instance_create["vnfs"][vnfr_name] = {"datacenter": vnfr.vnfr.vnfr_msg.om_datacenter} - openmano_instance_create["networks"] = {} - for vld_msg in self._nsd_msg.vld: - openmano_instance_create["networks"][vld_msg.name] = {} - openmano_instance_create["networks"][vld_msg.name]["sites"] = list() - for vlr in self._vlrs: - if vlr.vld_msg.name == vld_msg.name: - self._log.debug("Received VLR name %s, VLR DC: %s for VLD: %s",vlr.vld_msg.name, - vlr.om_datacenter_name,vld_msg.name) - #network["vim-network-name"] = vld_msg.name - network = {} - ip_profile = {} - if vld_msg.vim_network_name: - network["netmap-use"] = vld_msg.vim_network_name - elif vlr._ip_profile and vlr._ip_profile.has_field("ip_profile_params"): - ip_profile_params = vlr._ip_profile.ip_profile_params - if ip_profile_params.ip_version == "ipv6": - ip_profile['ip-version'] = "IPv6" + try: + self._log.debug("Creating instance-scenario-create input file for nsd %s with name %s", self.nsd.id, self._nsr_config_msg.name) + openmano_instance_create = {} + openmano_instance_create["name"] = self._nsr_config_msg.name + openmano_instance_create["description"] = self._nsr_config_msg.description + openmano_instance_create["scenario"] = self._nsd_uuid + + cloud_config = self.get_ssh_key_pairs() + if cloud_config: + openmano_instance_create["cloud-config"] = cloud_config + if self._nsr_config_msg.has_field("datacenter"): + openmano_instance_create["datacenter"] = self._nsr_config_msg.datacenter + openmano_instance_create["vnfs"] = {} + for vnfr in self._vnfrs: + if "datacenter" in vnfr.vnfr.vnfr_msg: + vnfr_name = vnfr.vnfr.vnfd.name + "." + str(vnfr.vnfr.vnfr_msg.member_vnf_index_ref) + openmano_instance_create["vnfs"][vnfr_name] = {"datacenter": vnfr.vnfr.vnfr_msg.datacenter} + openmano_instance_create["networks"] = {} + for vld_msg in self._nsd_msg.vld: + openmano_instance_create["networks"][vld_msg.name] = {} + openmano_instance_create["networks"][vld_msg.name]["sites"] = list() + for vlr in self._vlrs: + if vlr.vld_msg.name == vld_msg.name: + self._log.debug("Received VLR name %s, VLR DC: %s for VLD: %s",vlr.vld_msg.name, + vlr.datacenter_name,vld_msg.name) + #network["vim-network-name"] = vld_msg.name + network = {} + ip_profile = {} + if vld_msg.vim_network_name: + network["netmap-use"] = vld_msg.vim_network_name + elif vlr._ip_profile and vlr._ip_profile.has_field("ip_profile_params"): + ip_profile_params = vlr._ip_profile.ip_profile_params + if ip_profile_params.ip_version == "ipv6": + ip_profile['ip-version'] = "IPv6" + else: + ip_profile['ip-version'] = "IPv4" + if ip_profile_params.has_field('subnet_address'): + ip_profile['subnet-address'] = ip_profile_params.subnet_address + if ip_profile_params.has_field('gateway_address'): + ip_profile['gateway-address'] = ip_profile_params.gateway_address + if ip_profile_params.has_field('dns_server') and len(ip_profile_params.dns_server) > 0: + ip_profile['dns-address'] = ip_profile_params.dns_server[0].address + if ip_profile_params.has_field('dhcp_params'): + ip_profile['dhcp'] = {} + ip_profile['dhcp']['enabled'] = ip_profile_params.dhcp_params.enabled + ip_profile['dhcp']['start-address'] = ip_profile_params.dhcp_params.start_address + ip_profile['dhcp']['count'] = ip_profile_params.dhcp_params.count + if ip_profile['dhcp']['enabled'] is True and ip_profile['dhcp']['start-address'] is None: + addr_pool = list(ipaddress.ip_network(ip_profile['subnet-address']).hosts()) + gateway_ip_addr = ip_profile.get('gateway-address', None) + if gateway_ip_addr is None: + gateway_ip_addr = str(next(iter(addr_pool))) + ip_profile['gateway-address'] = gateway_ip_addr + + self._log.debug("Gateway Address {}".format(gateway_ip_addr)) + + if ipaddress.ip_address(gateway_ip_addr) in addr_pool: + addr_pool.remove(ipaddress.ip_address(gateway_ip_addr)) + if len(addr_pool) > 0: + ip_profile['dhcp']['start-address'] = str(next(iter(addr_pool))) + #DHCP count more than 200 is not instantiating any instances using OPENMANO RO + #So restricting it to a feasible count of 100. + dhcp_count = ip_profile['dhcp']['count'] + if dhcp_count is None or dhcp_count == 0 or dhcp_count > len(addr_pool): + ip_profile['dhcp']['count'] = min(len(addr_pool), 100) + self._log.debug("DHCP start Address {} DHCP count {}". + format(ip_profile['dhcp']['start-address'], ip_profile['dhcp']['count'])) else: - ip_profile['ip-version'] = "IPv4" - if ip_profile_params.has_field('subnet_address'): - ip_profile['subnet-address'] = ip_profile_params.subnet_address - if ip_profile_params.has_field('gateway_address'): - ip_profile['gateway-address'] = ip_profile_params.gateway_address - if ip_profile_params.has_field('dns_server') and len(ip_profile_params.dns_server) > 0: - ip_profile['dns-address'] = ip_profile_params.dns_server[0].address - if ip_profile_params.has_field('dhcp_params'): - ip_profile['dhcp'] = {} - ip_profile['dhcp']['enabled'] = ip_profile_params.dhcp_params.enabled - ip_profile['dhcp']['start-address'] = ip_profile_params.dhcp_params.start_address - ip_profile['dhcp']['count'] = ip_profile_params.dhcp_params.count - else: - network["netmap-create"] = vlr.name - if vlr.om_datacenter_name: - network["datacenter"] = vlr.om_datacenter_name - elif vld_msg.has_field("om_datacenter"): - network["datacenter"] = vld_msg.om_datacenter - elif "datacenter" in openmano_instance_create: - network["datacenter"] = openmano_instance_create["datacenter"] - if network: - openmano_instance_create["networks"][vld_msg.name]["sites"].append(network) - if ip_profile: - openmano_instance_create["networks"][vld_msg.name]['ip-profile'] = ip_profile + network["netmap-create"] = vlr.name + if vlr.datacenter_name: + network["datacenter"] = vlr.datacenter_name + elif vld_msg.has_field("datacenter"): + network["datacenter"] = vld_msg.datacenter + elif "datacenter" in openmano_instance_create: + network["datacenter"] = openmano_instance_create["datacenter"] + if network: + openmano_instance_create["networks"][vld_msg.name]["sites"].append(network) + if ip_profile: + openmano_instance_create["networks"][vld_msg.name]['ip-profile'] = ip_profile + except Exception as e: + self._log.error("Error while creating openmano_instance_yaml : {}". format(str(e))) return yaml.safe_dump(openmano_instance_create, default_flow_style=False,width=1000) @property def scaling_instance_create_yaml(self, scaleout=False): - self._log.debug("Creating instance-scenario-create input file for nsd %s with name %s", self.nsd.id, self._nsr_config_msg.name+"scal1") - scaling_instance_create = {} - for group_list in self._nsd_msg.scaling_group_descriptor: - scaling_instance_create["name"] = self._nsr_config_msg.name + "__"+group_list.name - if scaleout: - scaling_instance_create["scenario"] = self._nsd_uuid + "__" +group_list.name - else: - scaling_instance_create["scenario"] = self._nsd_uuid - scaling_instance_create["description"] = self._nsr_config_msg.description - - - if self._nsr_config_msg.has_field("om_datacenter"): - scaling_instance_create["datacenter"] = self._nsr_config_msg.om_datacenter - scaling_instance_create["vnfs"] = {} - for vnfr in self._vnfrs: - if "om_datacenter" in vnfr.vnfr.vnfr_msg: - vnfr_name = vnfr.vnfr.vnfd.name + "__" + str(vnfr.vnfr.vnfr_msg.member_vnf_index_ref) - scaling_instance_create["vnfs"][vnfr_name] = {"datacenter": vnfr.vnfr.vnfr_msg.om_datacenter} - scaling_instance_create["networks"] = {} - for vld_msg in self._nsd_msg.vld: - scaling_instance_create["networks"][vld_msg.name] = {} - scaling_instance_create["networks"][vld_msg.name]["sites"] = list() - for vlr in self._vlrs: - if vlr.vld_msg.name == vld_msg.name: - self._log.debug("Received VLR name %s, VLR DC: %s for VLD: %s",vlr.vld_msg.name, - vlr.om_datacenter_name,vld_msg.name) - #network["vim-network-name"] = vld_msg.name - network = {} - ip_profile = {} - if vld_msg.vim_network_name: - network["netmap-use"] = vld_msg.vim_network_name - #else: - # network["netmap-create"] = vlr.name - if vlr.om_datacenter_name: - network["datacenter"] = vlr.om_datacenter_name - elif vld_msg.has_field("om_datacenter"): - network["datacenter"] = vld_msg.om_datacenter - elif "datacenter" in scaling_instance_create: - network["datacenter"] = scaling_instance_create["datacenter"] - if network: - scaling_instance_create["networks"][vld_msg.name]["sites"].append(network) + try: + self._log.debug("Creating instance-scenario-create input file for nsd %s with name %s", self.nsd.id, self._nsr_config_msg.name+"scal1") + scaling_instance_create = {} + for group_list in self._nsd_msg.scaling_group_descriptor: + scaling_instance_create["name"] = self._nsr_config_msg.name + "__"+group_list.name + if scaleout: + scaling_instance_create["scenario"] = self._nsd_uuid + "__" +group_list.name + else: + scaling_instance_create["scenario"] = self._nsd_uuid + scaling_instance_create["description"] = self._nsr_config_msg.description + + if self._nsr_config_msg.has_field("datacenter"): + scaling_instance_create["datacenter"] = self._nsr_config_msg.datacenter + scaling_instance_create["vnfs"] = {} + for vnfr in self._vnfrs: + if "datacenter" in vnfr.vnfr.vnfr_msg: + vnfr_name = vnfr.vnfr.vnfd.name + "." + str(vnfr.vnfr.vnfr_msg.member_vnf_index_ref) + scaling_instance_create["vnfs"][vnfr_name] = {"datacenter": self._nsr_config_msg.datacenter} + scaling_instance_create["networks"] = {} + for vld_msg in self._nsd_msg.vld: + scaling_instance_create["networks"][vld_msg.name] = {} + scaling_instance_create["networks"][vld_msg.name]["sites"] = list() + for vlr in self._vlrs: + if vlr.vld_msg.name == vld_msg.name: + self._log.debug("Received VLR name %s, VLR DC: %s for VLD: %s",vlr.vld_msg.name, + vlr.datacenter_name,vld_msg.name) + #network["vim-network-name"] = vld_msg.name + network = {} + ip_profile = {} + if vld_msg.vim_network_name: + network["netmap-use"] = vld_msg.vim_network_name + #else: + # network["netmap-create"] = vlr.name + if vlr.datacenter_name: + network["datacenter"] = vlr.datacenter_name + elif vld_msg.has_field("datacenter"): + network["datacenter"] = vld_msg.datacenter + elif "datacenter" in scaling_instance_create: + network["datacenter"] = scaling_instance_create["datacenter"] + if network: + scaling_instance_create["networks"][vld_msg.name]["sites"].append(network) + except Exception as e: + self._log.error("Error while creating scaling_instance_yaml : {}". format(str(e))) return yaml.safe_dump(scaling_instance_create, default_flow_style=False, width=1000) @@ -510,12 +541,22 @@ class OpenmanoNsr(object): @asyncio.coroutine def remove_vnf(self,vnf): - if vnf in self._vnfrs: - self._vnfrs.remove(vnf) - yield from self._publisher.unpublish_vnfr( - None, - vnfr_msg - ) + self._log.debug("Unpublishing VNFR - {}".format(vnf)) + + delete_vnfr = None + for vnfr in self._vnfrs: + # Find the vnfr by id + if vnfr.vnfr.id == vnf.id: + self._log.debug("Found vnfr for delete !") + delete_vnfr = vnfr + break + + if delete_vnfr: + self._log.debug("Removing VNFR : {}".format(delete_vnfr.vnfr.id)) + self._vnfrs.remove(vnfr) + yield from self._publisher.unpublish_vnfr(None, delete_vnfr.vnfr.vnfr_msg) + self._log.debug("Removed VNFR : {}".format(delete_vnfr.vnfr.id)) + yield from asyncio.sleep(1, loop=self._loop) @asyncio.coroutine @@ -527,13 +568,14 @@ class OpenmanoNsr(object): None, self._cli_api.ns_vim_network_delete, vlr.name, - vlr.om_datacenter_name) + vlr.datacenter_name) yield from self._publisher.unpublish_vlr(None, vlr.vlr_msg) yield from asyncio.sleep(1, loop=self._loop) @asyncio.coroutine def add_vnfr(self, vnfr): - vnfr = OpenmanoVnfr(self._log, self._loop, self._cli_api, vnfr, nsd=self.nsd) + vnfr = OpenmanoVnfr(self._log, self._loop, self._cli_api, self.http_api, + vnfr, nsd=self.nsd, ssh_key=self._ssh_key) yield from vnfr.create() self._vnfrs.append(vnfr) @@ -547,61 +589,100 @@ class OpenmanoNsr(object): return self._log.debug("Deleting openmano nsr") - self._cli_api.ns_delete(self._nsd_uuid) + # Here we need to check for existing instances using this scenario. + # This would exist when we use Scaling Descriptors. + # Deleting a scenario before deleting instances results in a orphaned state + # TODO: The RO should implement the check done here. - self._log.debug("Deleting openmano vnfrs") - deleted_vnf_id_list = [] - for vnfr in self._vnfrs: - if vnfr.vnfr.vnfd.id not in deleted_vnf_id_list: - vnfr.delete() - deleted_vnf_id_list.append(vnfr.vnfr.vnfd.id) + self._log.debug("Fetching Instance Scenario List before Deleting Scenario") + instances = self.http_api.instances() - @asyncio.coroutine - def create(self): - self._log.debug("Creating openmano scenario") - name_uuid_map = yield from self._loop.run_in_executor( - None, - self._cli_api.ns_list, - ) + scenarios_instances = False - if self._nsd_msg.name in name_uuid_map: - self._log.debug("Found existing openmano scenario") - self._nsd_uuid = name_uuid_map[self._nsd_msg.name] - return + self._log.debug("Fetched Instances List. Checking if scenario is used") + for instance in instances: + if instance["scenario_id"] == self._nsd_uuid: + scenarios_instances = True + break + self._log.debug("Scenario Instances Dependency Exists : %s", scenarios_instances) - # Use the nsd uuid as the scenario name to rebind to existing - # scenario on reload or to support muliple instances of the name - # nsd - self._nsd_uuid, _ = yield from self._loop.run_in_executor( - None, - self._cli_api.ns_create, - self.openmano_nsd_yaml, - self._nsd_msg.name - ) - fpath = dump_openmano_descriptor( - "{}_nsd".format(self._nsd_msg.name), - self.openmano_nsd_yaml, - ) + if not scenarios_instances: + try: + self._cli_api.ns_delete(self._nsd_uuid) + except Exception as e: + self._log.error(e) - self._log.debug("Dumped Openmano NS descriptor to: %s", fpath) + self._log.debug("Deleting openmano vnfrs(non scaling vnfs)") + deleted_vnf_id_list = [] + for vnfr in self._vnfrs: + if vnfr.vnfr.vnfd.id not in deleted_vnf_id_list: + try: + vnfr.delete() + except Exception as e: + self._log.error("Failed to delete the vnf at the RO") + if "Resource is not free" in str(e): + self._log.error("Resource is not free, hence forego the vnf-delete") + else: + raise e + deleted_vnf_id_list.append(vnfr.vnfr.vnfd.id) - self._created = True + @asyncio.coroutine + def create(self): + try: + self._log.debug("Created openmano scenario") + # The self.openmano_nsd_yaml internally creates the scenario if not found. + # Assigning the yaml to a variable so that the api is not fired unnecessarily. + nsd_yaml = self.openmano_nsd_yaml + + self._nsd_uuid = yaml.load(nsd_yaml)['uuid'] + fpath = dump_openmano_descriptor( + "{}_nsd".format(self._nsd_msg.name), + nsd_yaml, + ) + + self._log.debug("Dumped Openmano NS descriptor to: %s", fpath) + + self._created = True + except Exception as e: + self._log.error("Failed to create scenario on Openmano RO : %s", e) + raise e @asyncio.coroutine def scaling_scenario_create(self): self._log.debug("Creating scaling openmano scenario") - self._nsd_uuid, _ = yield from self._loop.run_in_executor( - None, - self._cli_api.ns_create, - self.openmano_scaling_yaml, - ) + # The self.openmano_nsd_yaml internally creates the scenario if not found. + # Assigning the yaml to a variable so that the api is not fired unnecessarily. + nsd_yaml = self.openmano_scaling_yaml + + self._nsd_uuid = yaml.load(nsd_yaml)['uuid'] + fpath = dump_openmano_descriptor( "{}_sgd".format(self._nsd_msg.name), self.scaling_instance_create_yaml, ) + self._created = True + + + @asyncio.coroutine + def get_nsr_opdata(self): + """ NSR opdata associated with this VNFR """ + xpath = self._project.add_project( + "D,/nsr:ns-instance-opdata/nsr:nsr" \ + "[nsr:ns-instance-config-ref={}]". \ + format(quoted_key(self.nsr_config_msg.id))) + + results = yield from self._dts.query_read(xpath, rwdts.XactFlag.MERGE) + + for result in results: + entry = yield from result + nsr_op = entry.result + return nsr_op + + return None + @asyncio.coroutine def instance_monitor_task(self): @@ -609,8 +690,11 @@ class OpenmanoNsr(object): start_time = time.time() active_vnfs = [] - + nsr = yield from self.get_nsr_opdata() while True: + active_vms = 0 + active_nets = 0 + yield from asyncio.sleep(1, loop=self._loop) try: @@ -624,10 +708,28 @@ class OpenmanoNsr(object): instance_resp_json, self._nsr_uuid) + for vnf in instance_resp_json['vnfs']: + for vm in vnf['vms']: + if vm['status'] == 'ACTIVE': + active_vms += 1 + for net in instance_resp_json['nets']: + if net['status'] == 'ACTIVE': + active_nets += 1 + + nsr.orchestration_progress.vms.active = active_vms + nsr.orchestration_progress.networks.active = active_nets + + # This is for accesibility of the status from nsm when the control goes back. + self._active_vms = active_vms + self._active_nets = active_nets + + yield from self._publisher.publish_nsr_opdata(None, nsr) + except openmano_client.InstanceStatusError as e: self._log.error("Could not get NS instance status: %s", str(e)) continue + def all_vms_active(vnf): for vm in vnf["vms"]: vm_status = vm["status"] @@ -653,15 +755,27 @@ class OpenmanoNsr(object): vm_status = vm["status"] vm_vim_info = vm["vim_info"] vm_uuid = vm["uuid"] - if vm_status == "ERROR": + if "ERROR" in vm_status: self._log.error("VM Error: %s (vim_info: %s)", vm_uuid, vm_vim_info) - return True + return True, vm['error_msg'] - return False + return False, '' def get_vnf_ip_address(vnf): if "ip_address" in vnf: return vnf["ip_address"].strip() + + else: + cp_info_list = get_ext_cp_info(vnf) + + for cp_name, ip, mac in cp_info_list: + for vld in self.nsd.vlds: + if not vld.mgmt_network: + continue + + for vld_cp in vld.vnfd_connection_point_ref: + if vld_cp.vnfd_connection_point_ref == cp_name: + return ip return None def get_vnf_mac_address(vnf): @@ -695,17 +809,17 @@ class OpenmanoNsr(object): return cp_info_list def get_vnf_status(vnfr): - # When we create an openmano descriptor we use __ + # When we create an openmano descriptor we use . # to come up with openmano constituent VNF name. Use this # knowledge to map the vnfr back. - openmano_vnfr_suffix = "__{}".format( + openmano_vnfr_suffix = ".{}".format( vnfr.vnfr.vnfr_msg.member_vnf_index_ref ) for vnf in instance_resp_json["vnfs"]: if vnf["vnf_name"].endswith(openmano_vnfr_suffix): return vnf - + self._log.warning("Could not find vnf status with name that ends with: %s", openmano_vnfr_suffix) return None @@ -729,10 +843,12 @@ class OpenmanoNsr(object): return # If there was a VNF that has a errored VM, then just fail the VNF and stop monitoring. - if any_vms_error(vnf_status): - self._log.debug("VM was found to be in error state. Marking as failed.") + vm_error, vm_err_msg = any_vms_error(vnf_status) + if vm_error: + self._log.error("VM was found to be in error state. Marking as failed.") self._state = OpenmanoNSRecordState.FAILED vnfr_msg.operational_status = "failed" + vnfr_msg.operational_status_details = vm_err_msg yield from self._publisher.publish_vnfr(None, vnfr_msg) return @@ -748,7 +864,7 @@ class OpenmanoNsr(object): vnf_mac_address = get_vnf_mac_address(vnf_status) if vnf_ip_address is None: - self._log.warning("No IP address obtained " + self._log.error("No IP address obtained " "for VNF: {}, will retry.".format( vnf_status['vnf_name'])) continue @@ -756,14 +872,19 @@ class OpenmanoNsr(object): self._log.debug("All VMs in VNF are active. Marking as running.") vnfr_msg.operational_status = "running" - self._log.debug("Got VNF ip address: %s, mac-address: %s", vnf_ip_address, vnf_mac_address) + self._log.debug("Got VNF ip address: %s, mac-address: %s", + vnf_ip_address, vnf_mac_address) vnfr_msg.mgmt_interface.ip_address = vnf_ip_address - vnfr_msg.vnf_configuration.config_access.mgmt_ip_address = vnf_ip_address - + + if vnfr._ssh_key: + vnfr_msg.mgmt_interface.ssh_key.public_key = \ + vnfr._ssh_key['public_key'] + vnfr_msg.mgmt_interface.ssh_key.private_key_file = \ + vnfr._ssh_key['private_key'] for vm in vnf_status["vms"]: if vm["uuid"] not in self._vdur_console_handler: - vdur_console_handler = VnfrConsoleOperdataDtsHandler(self._dts, self._log, self._loop, + vdur_console_handler = VnfrConsoleOperdataDtsHandler(self._project, self._dts, self._log, self._loop, self, vnfr_msg.id,vm["uuid"],vm["name"]) yield from vdur_console_handler.register() self._vdur_console_handler[vm["uuid"]] = vdur_console_handler @@ -793,7 +914,7 @@ class OpenmanoNsr(object): if len(active_vnfs) == len(self._vnfrs): self._state = OpenmanoNSRecordState.RUNNING - self._log.info("All VNF's are active. Exiting NSR monitoring task") + self._log.debug("All VNF's are active. Exiting NSR monitoring task") return @asyncio.coroutine @@ -872,14 +993,14 @@ class OpenmanoNsr(object): @asyncio.coroutine def create_vlr(self,vlr): - self._log.debug("Creating openmano vim network VLR name %s, VLR DC: %s",vlr.vld_msg.name, - vlr.om_datacenter_name) + self._log.error("Creating openmano vim network VLR name %s, VLR DC: %s",vlr.vld_msg.name, + vlr.datacenter_name) net_create = {} net = {} net['name'] = vlr.name net['shared'] = True net['type'] = 'bridge' - self._log.debug("Received ip profile is %s",vlr._ip_profile) + self._log.error("Received ip profile is %s",vlr._ip_profile) if vlr._ip_profile and vlr._ip_profile.has_field("ip_profile_params"): ip_profile_params = vlr._ip_profile.ip_profile_params ip_profile = {} @@ -904,31 +1025,31 @@ class OpenmanoNsr(object): fpath = dump_openmano_descriptor( "{}_vim_net_create_{}".format(self._nsr_config_msg.name,vlr.name), net_create_msg) - self._log.debug("Dumped Openmano VIM Net create to: %s", fpath) + self._log.error("Dumped Openmano VIM Net create to: %s", fpath) vim_network_uuid = yield from self._loop.run_in_executor( None, self._cli_api.ns_vim_network_create, net_create_msg, - vlr.om_datacenter_name) + vlr.datacenter_name) self._vlrs.append(vlr) -class OpenmanoNsPlugin(rwnsmplugin.NsmPluginBase): +class OpenmanoNsPlugin(nsmpluginbase.NsmPluginBase): """ RW Implentation of the NsmPluginBase """ - def __init__(self, dts, log, loop, publisher, ro_account): + def __init__(self, dts, log, loop, publisher, ro_account, project): self._dts = dts self._log = log self._loop = loop self._publisher = publisher - + self._project = project + self._cli_api = None self._http_api = None self._openmano_nsrs = {} - self._vnfr_uptime_tasks = {} self._openmano_nsr_by_vnfr_id = {} #self._nsr_uuid = None @@ -959,21 +1080,24 @@ class OpenmanoNsPlugin(rwnsmplugin.NsmPluginBase): OpenmanoNSRecordState.__members__.items() \ if member.value == state.value] - def create_nsr(self, nsr_config_msg, nsd_msg, key_pairs=None): + def create_nsr(self, nsr_config_msg, nsd_msg, key_pairs=None, ssh_key=None): """ Create Network service record """ openmano_nsr = OpenmanoNsr( - self._dts, - self._log, - self._loop, - self._publisher, - self._cli_api, - self._http_api, - nsd_msg, - nsr_config_msg, - key_pairs - ) + self._project, + self._dts, + self._log, + self._loop, + self._publisher, + self._cli_api, + self._http_api, + nsd_msg, + nsr_config_msg, + key_pairs, + ssh_key, + ) + self.log.debug("NSR created in openmano nsm %s", openmano_nsr) self._openmano_nsrs[nsr_config_msg.id] = openmano_nsr @asyncio.coroutine @@ -997,7 +1121,9 @@ class OpenmanoNsPlugin(rwnsmplugin.NsmPluginBase): """ openmano_nsr = self._openmano_nsrs[nsr.id] if scaleout: + self._log.debug("Scaleout set as True. Creating Scaling VNFR : {}".format(vnfr)) openmano_vnf_nsr = OpenmanoNsr( + self._project, self._dts, self._log, self._loop, @@ -1007,14 +1133,15 @@ class OpenmanoNsPlugin(rwnsmplugin.NsmPluginBase): openmano_nsr.nsd_msg, openmano_nsr.nsr_config_msg, openmano_nsr.key_pairs, - vnfr.vnfd.id + None, + rift_vnfd_id=vnfr.vnfd.id, ) self._openmano_nsr_by_vnfr_id[nsr.id] = openmano_nsr if vnfr.id in self._openmano_nsr_by_vnfr_id: raise VNFExistError("VNF %s already exist", vnfr.id) self._openmano_nsr_by_vnfr_id[vnfr.id] = openmano_vnf_nsr self._log.debug("VNFRID %s %s %s", type(self._openmano_nsr_by_vnfr_id), type(openmano_vnf_nsr), type(self._openmano_nsr_by_vnfr_id[vnfr.id])) - + self._log.debug("Inserting VNFR - {}, in NSR - {}".format(vnfr.id, self._openmano_nsr_by_vnfr_id)) for vlr in openmano_nsr.vlrs: yield from openmano_vnf_nsr.add_vlr(vlr) try: @@ -1034,6 +1161,7 @@ class OpenmanoNsPlugin(rwnsmplugin.NsmPluginBase): except Exception as e: self.log.exception(str(e)) else: + self._log.debug("Creating constituent VNFR - {}; for NSR - {}".format(vnfr, nsr)) yield from openmano_nsr.add_vnfr(vnfr) # Mark the VNFR as running @@ -1042,20 +1170,12 @@ class OpenmanoNsPlugin(rwnsmplugin.NsmPluginBase): vnfr_msg.operational_status = "init" self._log.debug("Attempting to publish openmano vnf: %s", vnfr_msg) - with self._dts.transaction() as xact: - yield from self._publisher.publish_vnfr(xact, vnfr_msg) - self._log.debug("Creating a task to update uptime for vnfr: %s", vnfr.id) - self._vnfr_uptime_tasks[vnfr.id] = self._loop.create_task(self.vnfr_uptime_update(vnfr)) + yield from self._publisher.publish_vnfr(None, vnfr_msg) - def vnfr_uptime_update(self, vnfr): - try: - vnfr_ = RwVnfrYang.YangData_Vnfr_VnfrCatalog_Vnfr.from_dict({'id': vnfr.id}) - while True: - vnfr_.uptime = int(time.time()) - vnfr._create_time - yield from self._publisher.publish_vnfr(None, vnfr_) - yield from asyncio.sleep(2, loop=self._loop) - except asyncio.CancelledError: - self._log.debug("Received cancellation request for vnfr_uptime_update task") + def update_vnfr(self, vnfr): + vnfr_msg = vnfr.vnfr_msg.deep_copy() + self._log.debug("Attempting to publish openmano vnf: %s", vnfr_msg) + yield from self._publisher.publish_vnfr(None, vnfr_msg) @asyncio.coroutine def instantiate_vl(self, nsr, vlr): @@ -1075,6 +1195,7 @@ class OpenmanoNsPlugin(rwnsmplugin.NsmPluginBase): """ Terminate the network service """ + self._log.debug("Terminate Received for Openamno NSR - {}".format(nsr)) nsr_id = nsr.id openmano_nsr = self._openmano_nsrs[nsr_id] @@ -1087,16 +1208,19 @@ class OpenmanoNsPlugin(rwnsmplugin.NsmPluginBase): openmano_nsr, ) - with self._dts.transaction() as xact: - for vnfr in openmano_nsr.vnfrs: - self._log.debug("Unpublishing VNFR: %s", vnfr.vnfr.vnfr_msg) - yield from self._publisher.unpublish_vnfr(xact, vnfr.vnfr.vnfr_msg) + for vnfr in openmano_nsr.vnfrs: + self._log.debug("Unpublishing Constituent VNFR: %s", vnfr.vnfr.vnfr_msg) + yield from self._publisher.unpublish_vnfr(None, vnfr.vnfr.vnfr_msg) del self._openmano_nsrs[nsr_id] def terminate(self, openmano_nsr): - openmano_nsr.terminate() - openmano_nsr.delete() + try: + openmano_nsr.terminate() + openmano_nsr.delete() + except Exception as e: + self._log.error("The NSR terminate failed for {}".format(openmano_nsr)) + raise e @asyncio.coroutine def terminate_vnf(self, nsr, vnfr, scalein=False): @@ -1104,12 +1228,16 @@ class OpenmanoNsPlugin(rwnsmplugin.NsmPluginBase): Terminate the network service """ if scalein: + self._log.debug("Terminating Scaling VNFR - {}".format(vnfr)) openmano_vnf_nsr = self._openmano_nsr_by_vnfr_id[vnfr.id] - openmano_vnf_nsr.terminate() - openmano_vnf_nsr.delete() + try: + openmano_vnf_nsr.terminate() + openmano_vnf_nsr.delete() + except Exception as e: + self._log.error("The NSR terminate failed for {}".format(openmano_nsr)) + raise e + yield from openmano_vnf_nsr.remove_vnf(vnfr) - if vnfr.id in self._vnfr_uptime_tasks: - self._vnfr_uptime_tasks[vnfr.id].cancel() @asyncio.coroutine def terminate_vl(self, vlr):