class Nsr(object): """Description of Nsr class""" def __init__(self, token): """Constructor of Nsr class""" self.__client = Client(verify_ssl_cert=False) self.basic_token = token def get_list(self): """Get the list of the NS records from the SO-ub container Returns: obj: a requests object Examples: >>> from soapi.nsr import Nsr >>> from soapi.identity import basic_token >>> from settings import OSM_ADMIN_CREDENTIALS >>> token = basic_token(OSM_ADMIN_CREDENTIALS.get('username'), OSM_ADMIN_CREDENTIALS.get('username')) >>> ns = Nsr(token) >>> ns_records = ns.get_list() >>> print(int(ns_records.status_code)) 200 """ endpoint = '{}/api/running/project/default/ns-instance-config'.format( OSM_COMPONENTS.get('SO-API')) headers = { "Authorization": "Basic {}".format(self.basic_token), "Accept": "application/json" } response = self.__client.get(endpoint, headers) return response def get(self, ns_uuid): """Get details for a NS record from the SO-ub container Args: ns_uuid (str): The ID of the network service Returns: obj: a requests object Examples: >>> from soapi.nsr import Nsr >>> from soapi.identity import basic_token >>> from settings import OSM_ADMIN_CREDENTIALS >>> token = basic_token(OSM_ADMIN_CREDENTIALS.get('username'), OSM_ADMIN_CREDENTIALS.get('username')) >>> ns = Nsr(token) >>> ns_record = ns.get('xxx') >>> print(int(ns_record.status_code)) 200 """ endpoint = '{}/api/operational/project/default/ns-instance-opdata/nsr/{}?deep'.format( OSM_COMPONENTS.get('SO-API'), ns_uuid) headers = { "Authorization": "Basic {}".format(self.basic_token), "Accept": "application/json" } response = self.__client.get(endpoint, headers) return response def instantiate(self, ns_descriptor, nsr_name, vim_account_name, admin_status="ENABLED"): """Instantiate a new NS based on NS descriptor and considering the given (by user) NS name and VIM name Args: ns_descriptor (dict): The NS descriptor nsr_name (str): The NS name vim_account_name (str): The VIM name Returns: obj: a requests object Examples: >>> from soapi.nsr import Nsr >>> from soapi.identity import basic_token >>> from settings import OSM_ADMIN_CREDENTIALS >>> token = basic_token(OSM_ADMIN_CREDENTIALS.get('username'), OSM_ADMIN_CREDENTIALS.get('username')) >>> ns = Nsr(token) >>> ns_descriptor = {"name": "cirros_2vnf_ns", "constituent-vnfd": [{"member-vnf-index": 1, "vnfd-id-ref": "cirros_vnfd", "start-by-default": "true"}, {"member-vnf-index": 2, "vnfd-id-ref": "cirros_vnfd", "start-by-default": "true"} ], "description": "Generated by OSM pacakage generator", "short-name": "cirros_2vnf_ns", "id": "cirros_2vnf_nsd", "version": "1.0", "vld": [{"name": "cirros_2vnf_nsd_vld1", "vnfd-connection-point-ref": [{"vnfd-connection-point-ref": "eth0", "vnfd-id-ref": "cirros_vnfd", "member-vnf-index-ref": 1 }, {"vnfd-connection-point-ref": "eth0", "vnfd-id-ref": "cirros_vnfd", "member-vnf-index-ref": 2 } ], "short-name": "cirros_2vnf_nsd_vld1", "mgmt-network": "true", "id": "cirros_2vnf_nsd_vld1", "type": "ELAN"} ], "logo": "osm_2x.png", "vendor": "OSM"} >>> ns_instance = ns.instantiate(ns_descriptor, "cirros_2vnf_ns_test", "devstack-ocata") >>> print(int(ns_instance.status_code)) >>> print(ns_instance.text) """ endpoint = '{}/api/config/project/default/ns-instance-config/nsr'.format( OSM_COMPONENTS.get('SO-API')) headers = { "Authorization": "Basic {}".format(self.basic_token), "Accept": "application/json", "Content-Type": "application/json" } nsr_id = str(uuid.uuid1()) payload = { "nsr": [{ "short-name": nsr_name, "datacenter": vim_account_name, "description": nsr_name, "resource-orchestrator": "osmopenmano", "nsd": ns_descriptor, "admin-status": admin_status, "id": nsr_id, "name": nsr_name }] } response = self.__client.post(endpoint, headers=headers, payload=json.dumps(payload)) return response def terminate(self, ns_uuid): """Terminate a running NS Args: ns_uuid (str, uuid): The ID of the network service Returns: obj: a requests object Examples: >>> from soapi.nsr import Nsr >>> from soapi.identity import basic_token >>> from settings import OSM_ADMIN_CREDENTIALS >>> token = basic_token(OSM_ADMIN_CREDENTIALS.get('username'), OSM_ADMIN_CREDENTIALS.get('username')) >>> ns = Nsr(token) >>> ns_instance = ns.terminate("32798fec-b042-11e8-a05b-fa163e682738") >>> print(ns_instance.status_code) 201 >>> print(ns_instance.text) {"success":""} """ endpoint = '{}/api/config/project/default/ns-instance-config/nsr/{}'.format( OSM_COMPONENTS.get('SO-API'), ns_uuid) headers = { "Authorization": "Basic {}".format(self.basic_token), "Accept": "application/json" } response = self.__client.delete(endpoint, headers=headers) return response def scale(self, ns_uuid, scaling_group_id, index): """Scale a NS record by given NS ID and scaling group ID Args: ns_uuid (str): The ID of the network service scaling_group_id (str): The ID of the scaling group as defined in the NS descriptor index (str): The ID of the triggered vnf Returns: obj: a requests object Examples: >>> from soapi.nsr import Nsr >>> from soapi.identity import basic_token >>> from settings import OSM_ADMIN_CREDENTIALS >>> token = basic_token(OSM_ADMIN_CREDENTIALS.get('username'), OSM_ADMIN_CREDENTIALS.get('username')) >>> ns = Nsr(token) >>> scaled_ns = ns.scale('xxx', 'scaling-group-1', '2134') >>> print(int(scaled_ns.status_code)) 200 """ endpoint = '{}/v1/api/config/project/default/ns-instance-config/nsr/{}/scaling-group/{}/instance'.format( OSM_COMPONENTS.get('SO-API'), ns_uuid, scaling_group_id) headers = { "Authorization": "Basic {}".format(self.basic_token), "Accept": "application/json", "Content-Type": "application/json" } payload = json.dumps({"instance": [{"id": str(index)}]}) response = self.__client.post(endpoint, headers, payload) return response
class Configuration: def __init__(self): """ Constructor """ self.__client = HttpClient(verify_ssl_cert=False) self.vdns_ip = VDNS_IP self.vdns_port = "9999" self.headers = {"X-Api-Key": "secret", "Content-Type": "application/json"} def add_vcache_entry(self, edge_vcache_ip_user_network, vcache_incremental_counter): """ Add an entry for the new vCache after its instantiation Args: edge_vcache_ip_user_network (str): the IPv4 of edge vCache in User network vcache_incremental_counter (int): incremental integer for each vCache edge added/scaled in the vCDN Returns: object: A requests object Raises: VdnsConfigurationFailed: The The vDNS configuration after the instantiation of a new vCache failed. Examples: >>> from actions.vnf_configuration import vdns >>> vcache_ip_user_network ="192.168.252.3" >>> vcache_incremental_counter = 2 >>> vdns_conf = vdns.Configuration() >>> vdns_conf.add_vcache_entry(vcache_ip_user_network, vcache_incremental_counter) """ endpoint = 'http://{}:{}/dns'.format(self.vdns_ip, self.vdns_port) payload = { "hostname": "cdn-uhd.cache{}.5gmedia.lab".format(vcache_incremental_counter), "ip": "{}".format(edge_vcache_ip_user_network) } request = self.__client.post(endpoint, headers=self.headers, payload=json.dumps(payload)) logger.debug("Request `POST {}` returns HTTP status `{}`, headers `{}` and body `{}`." .format(request.url, request.status_code, request.headers, request.text)) if request.status_code != 200: raise VdnsConfigurationFailed( "The vDNS configuration after the instantiation of the vCache with index={} " " failed".format(vcache_incremental_counter)) return request def delete_vcache_entry(self, vcache_incremental_counter): """ Remove the existing entry of vCache after its deletion Args: vcache_incremental_counter (int): incremental integer for each vCache edge added/scaled in the vCDN Returns: object: A requests object Raises: VdnsConfigurationFailed: The The vDNS configuration after the deletion of an existing vCache failed. Examples: >>> from actions.vnf_configuration import vdns >>> vcache_ip_user_network ="192.168.252.12" >>> vcache_incremental_counter = 2 >>> vdns_conf = vdns.Configuration(vcache_ip_user_network) >>> req = vdns_conf.delete_vcache_entry(vcache_incremental_counter) >>> req.status_code 200 """ endpoint = 'http://{}:{}/dns'.format(self.vdns_ip, self.vdns_port) payload = {"hostname": "cdn-uhd.cache{}.5gmedia.lab".format(vcache_incremental_counter)} request = self.__client.delete(endpoint, headers=self.headers, payload=json.dumps(payload)) logger.info("Request `DELETE {}` returns HTTP status `{}`, headers `{}` and body `{}`." .format(request.url, request.status_code, request.headers, request.text)) if request.status_code != 200: raise VdnsConfigurationFailed( "The vDNS configuration after the deletion of the vCache with index={} " "failed".format(vcache_incremental_counter)) return request def delete_faas_vcache_entry(self, vcache_incremental_counter): """ Remove the existing entry of vCache after its deletion Args: vcache_incremental_counter (int): incremental integer for each vCache edge added/scaled in the vCDN Returns: object: A requests object Raises: VdnsConfigurationFailed: The The vDNS configuration after the deletion of an existing faas vCache failed. Examples: >>> from actions.vnf_configuration import vdns >>> vcache_ip_user_network ="192.168.252.12" >>> vcache_incremental_counter = 2 >>> vdns_conf = vdns.Configuration(vcache_ip_user_network) >>> req = vdns_conf.delete_faas_vcache_entry(vcache_incremental_counter) >>> req.status_code 200 """ endpoint = 'http://{}:{}/dns'.format(self.vdns_ip, self.vdns_port) payload = { "hostname": "cdn-uhd.cache-faas-{}.5gmedia.lab".format(vcache_incremental_counter)} request = self.__client.delete(endpoint, headers=self.headers, payload=json.dumps(payload)) if request.status_code != 200: raise VdnsConfigurationFailed( "The vDNS configuration after the deletion of the vCache with index={} " "failed".format(vcache_incremental_counter)) return request