def discover_vnfr_using_ip(ip_address=None): """Discover the VNF based on the assigned IP Args: ip_address (str): The assigned IP in VM Returns: dict: The VNF Raises: NotFoundVnfWithGivenIP: in case that there is no VNF/VDU having the given IP """ token = bearer_token(OSM_ADMIN_CREDENTIALS.get('username'), OSM_ADMIN_CREDENTIALS.get('username')) vnfr = Vnf(token) vnfrs = vnfr.get_list() vnf_records = vnfrs.json() for vnf_record in vnf_records: # todo: improve it in case of multiple VDUs per VNF vnf_ip_address = vnf_record.get('ip-address', None) if vnf_ip_address is not None and vnf_ip_address == ip_address: return vnf_record raise VduWithIpNotFound( "Not found VDU with given IP: {}".format(ip_address))
def allow_scale_action(self, scale_action="scale_out"): """ Allow or not the scale action. A scale-out action is allowed only if there is 1 edge vCache. A scale-in action is allowed only if there are 2 or more edge vCaches. Args: scale_action (str): the scale action Returns: bool: True to allow it. Otherwise, False. """ vnf = Vnf(self.__token) response = vnf.get_list_by_ns(ns_uuid=self.ns_uuid) vnfs_list = response.json() current_edge_vdus = 1 for vnf_instance in vnfs_list: if vnf_instance.get("member-vnf-index-ref", None) is not None and int( vnf_instance["member-vnf-index-ref"]) == 2: current_edge_vdus = len(vnf_instance.get("vdur", [])) if scale_action == "scale_out": return current_edge_vdus == 1 elif scale_action == "scale_in": return current_edge_vdus > 1 else: return False
def discover_vnf_uuid_by_vnfd_name_index(vnfd_name_index): """ Discover the VDU uuid by given the vnfd name and index Args: vnfd_name_index (str): The VNFd name & index Returns: str: the vnf uuid """ token = bearer_token(OSM_ADMIN_CREDENTIALS.get('username'), OSM_ADMIN_CREDENTIALS.get('username')) # Get the UUIDs of the running NSs ns = Ns(token) request = ns.get_list() nss_response = request.json() ns_uuids = [ns.get('id') for ns in nss_response] # TODO: what if more than one NSs are running # if len(ns_uuids): # raise Exception("More that one NSs are running...") vnf_uuid = None for ns_uuid in ns_uuids: vnf = Vnf(token) request = vnf.get_list_by_ns(ns_uuid=ns_uuid) vnfs = request.json() for i in vnfs: cur_vnfd_name_index = "{}.{}".format( i.get("vnfd-ref"), i.get("member-vnf-index-ref"), ) if vnfd_name_index == cur_vnfd_name_index: return i.get("id") return vnf_uuid
def discover_vdu_uuid_by_vnf_index(vnf_index): """ Discover the VDU uuid by given the vnf index Args: vnf_index (str): The VNF index Returns: str: the vdu uuid """ token = bearer_token(OSM_ADMIN_CREDENTIALS.get('username'), OSM_ADMIN_CREDENTIALS.get('username')) # Get the UUIDs of the running NSs ns = Ns(token) request = ns.get_list() nss_response = request.json() ns_uuids = [ns.get('id') for ns in nss_response] # TODO: what if more than one NSs are running # if len(ns_uuids): # raise Exception("More that one NSs are running...") vdu_uuid = None for ns_uuid in ns_uuids: vnf = Vnf(token) request = vnf.get_list_by_ns(ns_uuid=ns_uuid) vnfs = request.json() for i in vnfs: if vnf_index in i.get("member-vnf-index-ref"): for vdur in i.get("vdur"): vdu_uuid = vdur.get("vim-id") return vdu_uuid return vdu_uuid
def main(): """ Detect the failed scale out operations and check it is triggered from the limitation of the maximum allowed VNF instances. """ kafka_consumer = init_consumer() kafka_consumer.subscribe(pattern=OSM_KAFKA_NS_TOPIC) # Run any incoming message in the intra-OSM kafka bus, topic `NS` for message in kafka_consumer: action = message.key.decode('utf-8', 'ignore') payload = yaml.safe_load(message.value.decode('utf-8', 'ignore')) if action != "scaled": continue event_state = payload.get('operationState', None) if event_state != "FAILED": continue ns_uuid = payload.get('nsr_id', None) operation_uuid = payload.get('nslcmop_id', None) if operation_uuid is None: continue logger.info("A new event for NS_UUID `{}` in state `{}` was detected.".format( action, ns_uuid, event_state)) # Detect the event: SCALE_IN, SCALE_OUT or something else osm_token = bearer_token(OSM_ADMIN_CREDENTIALS['username'], OSM_ADMIN_CREDENTIALS['username']) ns_operation = NsLcmOperation(osm_token) request = ns_operation.get(operation_uuid=operation_uuid) response = request.json() event = response.get('operationParams', {}).get('scaleVnfData', {}).get( 'scaleVnfType', None) # Skip it if not a scale out operation if not event or event != "SCALE_OUT": continue # Get the VNF index that was scaled vnf_index = response.get('operationParams', {}).get('scaleVnfData', {}).get( 'scaleByStepData', {}).get('member-vnf-index', 0) if vnf_index == 0: continue # Get the list of involved VNFs in the NS vnf = Vnf(osm_token) vnfs_request = vnf.get_list_by_ns(ns_uuid=ns_uuid) vnfs_list = vnfs_request.json() # Detect the VNFr and VNFd that probably scaled_vnfr = None for vnfr in vnfs_list: if int(vnfr['member-vnf-index-ref']) == int(vnf_index): vnf_request = vnf.get(vnf_uuid=vnfr['_id']) scaled_vnfr = vnf_request.json() break if scaled_vnfr is None: continue # Get the Vim details that host this NS/VNF instances vim_acc = VimAccount(token=osm_token) vim_req = vim_acc.get(scaled_vnfr['vim-account-id']) vim_info = vim_req.json() # Get VNFD info vnfd = Vnfd(token=osm_token) vnfd_req = vnfd.get(scaled_vnfr['vnfd-id']) vnfd_info = vnfd_req.json() # Compose the recommendation message recommend_vnfd_scaling_group(vim_info, vnfd_info, scaled_vnfr)
def get_vnf_details(vnf_uuid, record, source="vtranscoder3d"): """ Append MANO (OSM) details (ns, vnf and vdu) by given VNF uuid Args: vnf_uuid (str): The uuid of the VNF record (dict): The original metric as it is sent from monitoring metrics generator source (str): The NFVI or application ref. It could be "vtranscoder3d" etc.. Returns: dict: osm info for the given vdu """ mano = {} token = bearer_token(OSM_ADMIN_CREDENTIALS.get('username'), OSM_ADMIN_CREDENTIALS.get('password')) vnfr = Vnf(token) vnf_response = vnfr.get(vnf_uuid=vnf_uuid) vnf_record = vnf_response.json() # Get NS details ns_id = vnf_record.get("nsr-id-ref", None) ns = Ns(token) ns_response = ns.get(ns_uuid=ns_id) nsr = ns_response.json() # VNFd info vnfd = vnf_record.get("vnfd-id", None) # VDUs info vdu_records = vnf_record.get('vdur', []) # Number of VDU per VNFd vdus_count = len(vdu_records) if vdus_count > 1: logger.critical("{} VDUs were found for the VNF with uuid: {}".format( vdus_count, vnf_uuid)) vdu_record = vdu_records[0] try: # Find the VDU info vdu_metadata = record.get("resource_metadata", {}) # If the data coming from VNFs, discover in different way the VDU info if source in [ "telegraf", "vtranscoder3d", "vce", "kubernetes", "opennebula", "vtranscoder3d_spectators" ]: vdu_metadata["instance_id"] = vdu_record.get('vim-id', None) vdu_metadata["flavor"] = {} if vdus_count: # TODO: what about the IPs if exists VNF with multiple VDUs? vdu_metadata["state"] = vdu_record.get('status', "") vdu_metadata['name'] = vdu_record.get('name', "") vdu_metadata["flavor"]["ram"] = None vdu_metadata["flavor"]["vcpus"] = None vdu_metadata["flavor"]["disk"] = None elif source == "openstack": """By default, OpenStack (ceilometer) provides us the following info: vcpus, ram, ephemeral, swap, disk, name, id """ pass # Get IP per VDU vdu_metadata['ip_address'] = vdu_record.get("ip-address", None) # Get the VIM account Info vim_account = VimAccount(token) vim_response = vim_account.get( vim_account_uuid=nsr.get('datacenter', None)) vimr = vim_response.json() # Get the NSd uuid nsd_id = nsr.get('nsdId', None) if nsd_id is None: nsd_id = nsr.get('instantiate_params', {}).get('nsdId', None) mano = { "ns": { "id": ns_id, "name": nsr.get('name-ref', None), "nsd_id": nsd_id, "nsd_name": nsr.get('nsd-name-ref', None) }, "vnf": { "id": vnf_record.get("id", None), "name": vnf_record.get("name", None), # not in osm r5: it could be <vnfd_name>_<index> "index": vnf_record.get("member-vnf-index-ref", 0), "short_name": vnf_record.get("short-name", None), # not in osm r5 "vnfd_id": vnf_record.get("vnfd-id", None), "vnfd_name": vnf_record.get('vnfd-ref', None) }, "vdu": { "id": vdu_metadata.get("instance_id", None), "name": vdu_metadata.get("name", None), "image_id": vdu_metadata.get("image", {}).get("id", None), "flavor": vdu_metadata.get("flavor", {}), "status": vdu_metadata.get("state", None), "ip_address": vdu_metadata['ip_address'], "mgmt-interface": None # future usage }, "vim": { "uuid": vimr.get('_id', None), "name": vimr.get('name', None), "type": vimr.get('vim_type', None), "url": vimr.get('vim_url', None), "tag": source } } logger.debug(mano) except Exception as ex: logger.exception(ex) finally: # TODO: Since we don't know the VDU uuid, the 1st VDU will be used since 1 VDU is used for the VNF (UC1). return mano
def get_vdus_info(ns_uuid=None): """Get information about NS, VNF(s) and VDU(s) by given NS uuid Args: ns_uuid (str): The NS uuid Returns: dict: ns, vnf and vdu info """ vdus_info = [] if ns_uuid is None: return vdus_info token = bearer_token(OSM_ADMIN_CREDENTIALS.get('username'), OSM_ADMIN_CREDENTIALS.get('username')) ns = Ns(token) ns_response = ns.get(ns_uuid=ns_uuid) nsr = ns_response.json() # Get Vim vim_uuid = nsr.get('datacenter', None) vim_info = get_vim_info(vim_uuid=vim_uuid) # Get the Vnf UUIDs, members of the NS vnf_uuids = nsr.get('constituent-vnfr-ref', []) vnfr = Vnf(token) for vnf_uuid in vnf_uuids: vnf_response = vnfr.get(vnf_uuid=vnf_uuid) vnf_record = vnf_response.json() # VDUs info vdu_records = vnf_record.get('vdur', []) for vdu_record in vdu_records: mano = { "vim": vim_info, "ns": { "id": ns_uuid, "name": nsr.get('name-ref', None), "nsd_id": nsr.get('nsdId', None), "nsd_name": nsr.get('nsd-name-ref', None) }, "vnf": { "id": vnf_record.get("id", None), "name": None, # not provided in osm r4 "short_name": None, # not provided in osm r4 "vnfd_id": vnf_record.get("vnfd-id", None), "vnfd_name": None # not provided in osm r4 }, "vdu": { "id": vdu_record.get("vim-id", None), # NFVI-based uuid "image_id": None, "flavor": {}, "status": vdu_record.get("status", None), "ip_address": vdu_record.get("ip-address", None), "mgmt-interface": None # future usage } } vdus_info.append(mano) return vdus_info
def get_vcdn_net_interfaces(ns_uuid, search_for_mid_cache="vCache-mid-vdu", search_for_edge_cache="vCache-edge-vdu"): """ Get the network interfaces of scaled VNF as well as the current count-index Args: ns_uuid (str): The NS uuid, in which the scaled VNF belongs to search_for_mid_cache (str): Search for the Mid vCache by given explicit name search_for_edge_cache (str): Search for scaled Edge vCache by given explicit name Returns: tuple(dict, int): The details of the VNF interfaces including the VDU index in the VNF ( { "edge": { "user": { "mac-address": "fa:16:3e:0c:94:7f", "ip-address": "192.168.252.12", "name": "ens6", "ns-vld-id": "user" }, "cache": { "mac-address": "fa:16:3e:4d:b9:64", "ip-address": "192.168.253.9", "name": "ens7", "ns-vld-id": "cache" }, "management": { "mgmt-vnf": "true", "mac-address": "fa:16:3e:99:33:43", "ip-address": "192.168.111.29", "name": "ens3", "ns-vld-id": "management" } }, "mid": { "management": { "ip-address": "192.168.111.13", "ns-vld-id": "management", "name": "ens3", "mac-address": "fa:16:3e:02:f5:1c", "mgmt-vnf": true }, "cache": { "ip-address": "192.168.253.12", "name": "ens6", "ns-vld-id": "cache", "mac-address": "fa:16:3e:60:5d:9d" }, "origin": { "ip-address": "192.168.254.5", "name": "ens7", "ns-vld-id": "origin", "mac-address": "fa:16:3e:0d:64:97" } } }, <int|1> ) """ vdus_list = [] interfaces = {"mid": None, "edge": None} edges_interfaces_all = {} count_index = None # Fetch the VNFs by given NS instance token = bearer_token(OSM_ADMIN_CREDENTIALS.get('username'), OSM_ADMIN_CREDENTIALS.get('password')) vnf = Vnf(token) response = vnf.get_list_by_ns(ns_uuid=ns_uuid) vnfs_list = response.json() # Keep the VDUs details for vnf_instance in vnfs_list: vdus_list += vnf_instance.get("vdur", []) # Discover the interfaces of the proper scaled Edge VNF and Mid vCache for vdu in vdus_list: # Get Mid vCache net details if vdu.get('vdu-id-ref', None) is not None and \ vdu['vdu-id-ref'] == search_for_mid_cache and \ vdu.get('count-index', None) == 0: interfaces['mid'] = format_vdu_interfaces(vdu.get( 'interfaces', [])) # Get Edge vCache net details (the new one) if vdu.get('vdu-id-ref', None) is not None and \ vdu['vdu-id-ref'] == search_for_edge_cache and \ vdu.get('count-index', None) >= 0: edges_interfaces_all[str( vdu['count-index'])] = format_vdu_interfaces( vdu.get('interfaces', [])) # Keep the VDU with the greatest count-index latest_vdu_index = max([int(k) for k in list(edges_interfaces_all.keys())]) count_index = latest_vdu_index interfaces['edge'] = edges_interfaces_all[str(latest_vdu_index)] return interfaces, count_index
def get_faas_vcdn_net_interfaces(ns_uuid, search_for_mid_cache="vCache_mid_vdu", search_for_edge_cache="vCache_edge_vdu"): """ Get the network interfaces of the VNF Args: ns_uuid (str): The NS uuid, in which the scaled VNF belongs to search_for_mid_cache (str): Search for the Mid vCache by given explicit name search_for_edge_cache (str): Search for scaled Edge vCache by given explicit name Returns: dict: The details of the VNF interfaces ( { "edge": None, "mid": { "management": { "ip-address": "192.168.111.13", "ns-vld-id": "management", "name": "ens3", "mac-address": "fa:16:3e:02:f5:1c", "mgmt-vnf": true }, "cache": { "ip-address": "192.168.253.12", "name": "ens6", "ns-vld-id": "cache", "mac-address": "fa:16:3e:60:5d:9d" }, "origin": { "ip-address": "192.168.254.5", "name": "ens7", "ns-vld-id": "origin", "mac-address": "fa:16:3e:0d:64:97" } } } ) """ vdus_list = [] interfaces = {"mid": None, "edge": None} edges_interfaces_all = {} count_index = None # Fetch the VNFs by given NS instance token = bearer_token(OSM_ADMIN_CREDENTIALS.get('username'), OSM_ADMIN_CREDENTIALS.get('password')) vnf = Vnf(token) response = vnf.get_list_by_ns(ns_uuid=ns_uuid) vnfs_list = response.json() # Keep the VDUs details for vnf_instance in vnfs_list: vdus_list += vnf_instance.get("vdur", []) # Discover the interfaces of the proper scaled Edge VNF and Mid vCache for vdu in vdus_list: # Get Mid vCache net details if vdu.get('vdu-id-ref', None) is not None and \ vdu['vdu-id-ref'] == search_for_mid_cache and \ vdu.get('count-index', None) == 0: interfaces['mid'] = format_vdu_interfaces(vdu.get( 'interfaces', [])) # Get Edge vCache net details (the new one) if vdu.get('vdu-id-ref', None) is not None and \ vdu['vdu-id-ref'] == search_for_edge_cache and \ vdu.get('count-index', None) > 0: edges_interfaces_all[str( vdu['count-index'])] = format_vdu_interfaces( vdu.get('interfaces', [])) return interfaces