diff options
Diffstat (limited to 'sfc/lib/openstack_utils.py')
-rw-r--r-- | sfc/lib/openstack_utils.py | 182 |
1 files changed, 124 insertions, 58 deletions
diff --git a/sfc/lib/openstack_utils.py b/sfc/lib/openstack_utils.py index 7257b126..b7254bf1 100644 --- a/sfc/lib/openstack_utils.py +++ b/sfc/lib/openstack_utils.py @@ -4,7 +4,8 @@ import time import json import yaml from tackerclient.tacker import client as tackerclient -from functest.utils.constants import CONST +from functest.utils import constants +from functest.utils import env from snaps.openstack.tests import openstack_tests @@ -29,7 +30,7 @@ import snaps.openstack.create_instance as cr_inst from snaps.config.vm_inst import VmInstanceConfig, FloatingIpConfig from snaps.openstack.utils import ( - nova_utils, neutron_utils, glance_utils, heat_utils, keystone_utils) + nova_utils, neutron_utils, heat_utils, keystone_utils) logger = logging.getLogger(__name__) DEFAULT_TACKER_API_VERSION = '1.0' @@ -39,12 +40,12 @@ class OpenStackSFC: def __init__(self): self.os_creds = openstack_tests.get_credentials( - os_env_file=CONST.__getattribute__('openstack_creds')) + os_env_file=constants.ENV_FILE) self.creators = [] self.nova = nova_utils.nova_client(self.os_creds) self.neutron = neutron_utils.neutron_client(self.os_creds) - self.glance = glance_utils.glance_client(self.os_creds) self.heat = heat_utils.heat_client(self.os_creds) + self.keystone = keystone_utils.keystone_client(self.os_creds) def register_glance_image(self, name, url, img_format, public): image_settings = ImageConfig(name=name, img_format=img_format, url=url, @@ -80,7 +81,7 @@ class OpenStackSFC: self.creators.append(network_creator) # Router - ext_network_name = CONST.__getattribute__('EXTERNAL_NETWORK') + ext_network_name = env.get('EXTERNAL_NETWORK') router_settings = RouterConfig(name=router_name, external_gateway=ext_network_name, @@ -156,19 +157,30 @@ class OpenStackSFC: ''' Return the compute where the client sits ''' + return self.get_vm_compute('client') + + def get_compute_server(self): + ''' + Return the compute where the server sits + ''' + return self.get_vm_compute('server') + + def get_vm_compute(self, vm_name): + ''' + Return the compute where the vm sits + ''' for creator in self.creators: # We want to filter the vm creators - if hasattr(creator, 'get_vm_info'): - vm_info = creator.get_vm_info() - # We want to fetch only the client - if vm_info['name'] == 'client': - return vm_info['OS-EXT-SRV-ATTR:host'] + if hasattr(creator, 'get_vm_inst'): + # We want to fetch by vm_name + if creator.get_vm_inst().name == vm_name: + return creator.get_vm_inst().compute_host - raise Exception("There is no client VM!!") + raise Exception("There is no VM with name '{}'!!".format(vm_name)) def assign_floating_ip(self, router, vm, vm_creator): ''' - Assign a floating ips to all the VMs + Assign floating ips to all the VMs ''' name = vm.name + "-float" port_name = vm.ports[0].name @@ -180,22 +192,45 @@ class OpenStackSFC: return ip.ip # We need this function because tacker VMs cannot be created through SNAPs - def assign_floating_ip_vnfs(self, router): + def assign_floating_ip_vnfs(self, router, ips=None): ''' - Assign a floating ips to all the SFs + Assign floating ips to all the SFs. Optionally specify the + subnet IPs that a floating IP should be assigned to, assuming that the + SF is connected to a single subnet globally and per port. ''' stacks = self.heat.stacks.list() fips = [] + project_name = 'admin' for stack in stacks: servers = heat_utils.get_stack_servers(self.heat, self.nova, self.neutron, - stack) + self.keystone, + stack, + project_name) sf_creator = cr_inst.generate_creator(self.os_creds, servers[0], - self.image_settings) - port_name = servers[0].ports[0].name + self.image_settings, + project_name) + name = servers[0].name + "-float" + if ips is None: + port_name = servers[0].ports[0].name + else: + port_name = None + for port in servers[0].ports: + if port.ips[0]['ip_address'] in ips: + port_name = port.name + break + + if port_name is None: + err_msg = "The VNF {} does not have any suitable port {} " \ + "for floating IP assignment".format( + name, + 'with ip any of ' + str(ips) if ips else '') + logger.error(err_msg) + raise Exception(err_msg) + float_ip = FloatingIpConfig(name=name, port_name=port_name, router_name=router.name) @@ -205,13 +240,19 @@ class OpenStackSFC: return fips - def get_client_port_id(self, vm): + def get_client_port(self, vm, vm_creator): ''' Get the neutron port id of the client ''' - port_id = neutron_utils.get_port(self.neutron, - port_name=vm.name + "-port") - return port_id + port_name = vm.name + "-port" + port = vm_creator.get_port_by_name(port_name) + if port is not None: + return port + else: + logger.error("The VM {0} does not have any port" + " with name {1}".format(vm.name, port_name)) + raise Exception("Client VM does not have the desired port") + # TACKER SECTION # @@ -227,7 +268,7 @@ def get_tacker_client_version(): def get_tacker_client(other_creds={}): creds_override = None os_creds = openstack_tests.get_credentials( - os_env_file=CONST.__getattribute__('openstack_creds'), + os_env_file=constants.ENV_FILE, overrides=creds_override) sess = keystone_utils.keystone_session(os_creds) return tackerclient.Client(get_tacker_client_version(), session=sess) @@ -235,12 +276,12 @@ def get_tacker_client(other_creds={}): def get_id_from_name(tacker_client, resource_type, resource_name): try: - req_params = {'fields': 'id', 'name': resource_name} - endpoint = '/{0}s'.format(resource_type) - resp = tacker_client.get(endpoint, params=req_params) - endpoint = endpoint.replace('-', '_') - return resp[endpoint[1:]][0]['id'] - except Exception, e: + params = {'fields': 'id', 'name': resource_name} + collection = resource_type + 's' + path = '/' + collection + resp = tacker_client.list(collection, path, **params) + return resp[collection][0]['id'] + except Exception as e: logger.error("Error [get_id_from_name(tacker_client, " "resource_type, resource_name)]: %s" % e) return None @@ -288,7 +329,7 @@ def list_vnfds(tacker_client, verbose=False): if not verbose: vnfds = [vnfd['id'] for vnfd in vnfds['vnfds']] return vnfds - except Exception, e: + except Exception as e: logger.error("Error [list_vnfds(tacker_client)]: %s" % e) return None @@ -303,7 +344,7 @@ def create_vnfd(tacker_client, tosca_file=None, vnfd_name=None): return tacker_client.create_vnfd( body={"vnfd": {"attributes": {"vnfd": vnfd_body}, "name": vnfd_name}}) - except Exception, e: + except Exception as e: logger.error("Error [create_vnfd(tacker_client, '%s')]: %s" % (tosca_file, e)) return None @@ -317,7 +358,7 @@ def delete_vnfd(tacker_client, vnfd_id=None, vnfd_name=None): raise Exception('You need to provide VNFD id or VNFD name') vnfd = get_vnfd_id(tacker_client, vnfd_name) return tacker_client.delete_vnfd(vnfd) - except Exception, e: + except Exception as e: logger.error("Error [delete_vnfd(tacker_client, '%s', '%s')]: %s" % (vnfd_id, vnfd_name, e)) return None @@ -329,7 +370,7 @@ def list_vnfs(tacker_client, verbose=False): if not verbose: vnfs = [vnf['id'] for vnf in vnfs['vnfs']] return vnfs - except Exception, e: + except Exception as e: logger.error("Error [list_vnfs(tacker_client)]: %s" % e) return None @@ -364,7 +405,7 @@ def create_vnf(tacker_client, vnf_name, vnfd_id=None, vnf_body['vnf']['vim_id'] = get_vim_id(tacker_client, vim_name) return tacker_client.create_vnf(body=vnf_body) - except Exception, e: + except Exception as e: logger.error("error [create_vnf(tacker_client," " '%s', '%s', '%s')]: %s" % (vnf_name, vnfd_id, vnfd_name, e)) @@ -384,12 +425,28 @@ def get_vnf(tacker_client, vnf_id=None, vnf_name=None): else: raise Exception('Could not retrieve ID from name [%s]' % vnf_name) - except Exception, e: + except Exception as e: logger.error("Could not retrieve VNF [vnf_id=%s, vnf_name=%s] - %s" % (vnf_id, vnf_name, e)) return None +def get_vnf_ip(tacker_client, vnf_id=None, vnf_name=None): + """ + Get the management ip of the first VNF component as obtained from the + tacker REST API: + + { + "vnf": { + ... + "mgmt_url": "{\"VDU1\": \"192.168.120.3\"}", + ... + } + """ + vnf = get_vnf(tacker_client, vnf_id, vnf_name) + return json.loads(vnf['mgmt_url']).values()[0] + + def wait_for_vnf(tacker_client, vnf_id=None, vnf_name=None, timeout=100): try: vnf = get_vnf(tacker_client, vnf_id, vnf_name) @@ -409,7 +466,7 @@ def wait_for_vnf(tacker_client, vnf_id=None, vnf_name=None, timeout=100): raise Exception('Timeout when booting vnf %s' % vnf['id']) return vnf['id'] - except Exception, e: + except Exception as e: logger.error("error [wait_for_vnf(tacker_client, '%s', '%s')]: %s" % (vnf_id, vnf_name, e)) return None @@ -423,7 +480,7 @@ def delete_vnf(tacker_client, vnf_id=None, vnf_name=None): raise Exception('You need to provide a VNF id or name') vnf = get_vnf_id(tacker_client, vnf_name) return tacker_client.delete_vnf(vnf) - except Exception, e: + except Exception as e: logger.error("Error [delete_vnf(tacker_client, '%s', '%s')]: %s" % (vnf_id, vnf_name, e)) return None @@ -437,7 +494,7 @@ def create_vim(tacker_client, vim_file=None): vim_body = json.load(vim_fd) logger.info('VIM template:\n{0}'.format(vim_body)) return tacker_client.create_vim(body=vim_body) - except Exception, e: + except Exception as e: logger.error("Error [create_vim(tacker_client, '%s')]: %s" % (vim_file, e)) return None @@ -453,14 +510,14 @@ def create_vnffgd(tacker_client, tosca_file=None, vnffgd_name=None): return tacker_client.create_vnffgd( body={'vnffgd': {'name': vnffgd_name, 'template': {'vnffgd': vnffgd_body}}}) - except Exception, e: + except Exception as e: logger.error("Error [create_vnffgd(tacker_client, '%s')]: %s" % (tosca_file, e)) return None def create_vnffg(tacker_client, vnffg_name=None, vnffgd_id=None, - vnffgd_name=None, param_file=None): + vnffgd_name=None, param_file=None, symmetrical=False): ''' Creates the vnffg which will provide the RSP and the classifier ''' @@ -468,7 +525,8 @@ def create_vnffg(tacker_client, vnffg_name=None, vnffgd_id=None, vnffg_body = { 'vnffg': { 'attributes': {}, - 'name': vnffg_name + 'name': vnffg_name, + 'symmetrical': symmetrical } } if param_file is not None: @@ -485,7 +543,7 @@ def create_vnffg(tacker_client, vnffg_name=None, vnffgd_id=None, vnffg_body['vnffg']['vnffgd_id'] = get_vnffgd_id(tacker_client, vnffgd_name) return tacker_client.create_vnffg(body=vnffg_body) - except Exception, e: + except Exception as e: logger.error("error [create_vnffg(tacker_client," " '%s', '%s', '%s')]: %s" % (vnffg_name, vnffgd_id, vnffgd_name, e)) @@ -498,7 +556,7 @@ def list_vnffgds(tacker_client, verbose=False): if not verbose: vnffgds = [vnffgd['id'] for vnffgd in vnffgds['vnffgds']] return vnffgds - except Exception, e: + except Exception as e: logger.error("Error [list_vnffgds(tacker_client)]: %s" % e) return None @@ -509,7 +567,7 @@ def list_vnffgs(tacker_client, verbose=False): if not verbose: vnffgs = [vnffg['id'] for vnffg in vnffgs['vnffgs']] return vnffgs - except Exception, e: + except Exception as e: logger.error("Error [list_vnffgs(tacker_client)]: %s" % e) return None @@ -522,7 +580,7 @@ def delete_vnffg(tacker_client, vnffg_id=None, vnffg_name=None): raise Exception('You need to provide a VNFFG id or name') vnffg = get_vnffg_id(tacker_client, vnffg_name) return tacker_client.delete_vnffg(vnffg) - except Exception, e: + except Exception as e: logger.error("Error [delete_vnffg(tacker_client, '%s', '%s')]: %s" % (vnffg_id, vnffg_name, e)) return None @@ -536,7 +594,7 @@ def delete_vnffgd(tacker_client, vnffgd_id=None, vnffgd_name=None): raise Exception('You need to provide VNFFGD id or VNFFGD name') vnffgd = get_vnffgd_id(tacker_client, vnffgd_name) return tacker_client.delete_vnffgd(vnffgd) - except Exception, e: + except Exception as e: logger.error("Error [delete_vnffgd(tacker_client, '%s', '%s')]: %s" % (vnffgd_id, vnffgd_name, e)) return None @@ -548,7 +606,7 @@ def list_vims(tacker_client, verbose=False): if not verbose: vims = [vim['id'] for vim in vims['vims']] return vims - except Exception, e: + except Exception as e: logger.error("Error [list_vims(tacker_client)]: %s" % e) return None @@ -561,7 +619,7 @@ def delete_vim(tacker_client, vim_id=None, vim_name=None): raise Exception('You need to provide VIM id or VIM name') vim = get_vim_id(tacker_client, vim_name) return tacker_client.delete_vim(vim) - except Exception, e: + except Exception as e: logger.error("Error [delete_vim(tacker_client, '%s', '%s')]: %s" % (vim_id, vim_name, e)) return None @@ -582,9 +640,8 @@ def register_vim(tacker_client, vim_file=None): with open(vim_file) as f: json_dict = json.load(f) - json_dict['vim']['auth_url'] = CONST.__getattribute__('OS_AUTH_URL') - json_dict['vim']['auth_cred']['password'] = CONST.__getattribute__( - 'OS_PASSWORD') + json_dict['vim']['auth_url'] = os.environ['OS_AUTH_URL'] + json_dict['vim']['auth_cred']['password'] = os.environ['OS_PASSWORD'] json.dump(json_dict, open(tmp_file, 'w')) @@ -617,19 +674,28 @@ def create_vnf_in_av_zone( def create_vnffg_with_param_file(tacker_client, vnffgd_name, vnffg_name, - default_param_file, neutron_port): + default_param_file, client_port, + server_port=None, server_ip=None): param_file = default_param_file - - if neutron_port is not None: + data = {} + if client_port: + data['net_src_port_id'] = client_port + if server_port: + data['net_dst_port_id'] = server_port + if server_ip: + data['ip_dst_prefix'] = server_ip + + if client_port is not None or server_port is not None: param_file = os.path.join( '/tmp', - 'param_{0}.json'.format(neutron_port)) - data = { - 'net_src_port_id': neutron_port - } + 'param_{0}.json'.format(vnffg_name)) with open(param_file, 'w+') as f: json.dump(data, f) + + symmetrical = True if client_port and server_port else False + create_vnffg(tacker_client, vnffgd_name=vnffgd_name, vnffg_name=vnffg_name, - param_file=param_file) + param_file=param_file, + symmetrical=symmetrical) |