From be5cd25ed85bb42f4115b49aec13ddfec20c1b97 Mon Sep 17 00:00:00 2001 From: Romanos Skiadas Date: Thu, 8 Sep 2016 16:23:40 +0300 Subject: Implement quagga peering - Create an openstack instance - Assign it a floating IP - Execute a quagga bootstrap script using cloud init - Tell ODL to peer with the instance - Start an instance in a VPN - make sure the route was exchanged between the peered quagga and ODL Change-Id: I73bcaec5425df2b953c2bceaca7d4f09ff28f3d0 Signed-off-by: Romanos Skiadas --- sdnvpn/lib/config.py | 12 +++++++- sdnvpn/lib/quagga.py | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++ sdnvpn/lib/results.py | 6 ++++ sdnvpn/lib/utils.py | 62 ++++++++++++++++++++++++++++++++++++++++- 4 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 sdnvpn/lib/quagga.py (limited to 'sdnvpn/lib') diff --git a/sdnvpn/lib/config.py b/sdnvpn/lib/config.py index 7fc0cd4..4fd40ed 100644 --- a/sdnvpn/lib/config.py +++ b/sdnvpn/lib/config.py @@ -21,7 +21,6 @@ class CommonConfig(object): """ Common configuration parameters across testcases """ - def __init__(self): self.repo_path = CONST.dir_repo_sdnvpn self.config_file = os.path.join(self.repo_path, @@ -29,6 +28,9 @@ class CommonConfig(object): self.keyfile_path = os.path.join(self.repo_path, 'sdnvpn/artifacts/id_rsa') self.test_db = CONST.results_test_db_url + self.quagga_setup_script_path = os.path.join( + self.repo_path, + "sdnvpn/artifacts/quagga_setup.sh") self.line_length = 90 # length for the summary table self.vm_boot_timeout = 180 self.default_flavor = ft_utils.get_parameter_from_yaml( @@ -37,6 +39,14 @@ class CommonConfig(object): self.image_format = CONST.openstack_image_disk_format self.image_path = '{0}/{1}'.format(CONST.dir_functest_data, self.image_filename) + # This is the ubuntu image used by sfc + # Basically vanilla ubuntu + some scripts in there + # We can use it to setup a quagga instance + # TODO does functest have an ubuntu image somewhere? + self.ubuntu_image_name = "sdnvpn-ubuntu" + self.ubuntu_image_path = '{0}/{1}'.format( + CONST.dir_functest_data, + "ubuntu-16.04-server-cloudimg-amd64-disk1.img") class TestcaseConfig(object): diff --git a/sdnvpn/lib/quagga.py b/sdnvpn/lib/quagga.py new file mode 100644 index 0000000..e2885c2 --- /dev/null +++ b/sdnvpn/lib/quagga.py @@ -0,0 +1,76 @@ +"""Utilities for setting up quagga peering""" + +import re +import time + +import functest.utils.functest_logger as ft_logger +import functest.utils.functest_utils as ft_utils +import sdnvpn.lib.config as config +from sdnvpn.lib.utils import run_odl_cmd, exec_cmd + +logger = ft_logger.Logger("sdnvpn-quagga").getLogger() + +COMMON_CONFIG = config.CommonConfig() + + +def odl_add_neighbor(neighbor_ip, controller): + command = 'configure-bgp -op add-neighbor --as-num 200' + command += ' --ip %s --use-source-ip %s' % (neighbor_ip, controller.ip) + success = run_odl_cmd(controller, command) + return success + + +def bootstrap_quagga(fip_addr, controller_ip): + script = gen_quagga_setup_script( + controller_ip, + fip_addr) + cmd = "sshpass -popnfv ssh opnfv@%s << EOF %s EOF" % (fip_addr, script) + rc = ft_utils.execute_command(cmd) + return rc == 0 + + +def gen_quagga_setup_script(controller_ip, instance_floating_ip): + with open(COMMON_CONFIG.quagga_setup_script_path) as f: + template = f.read() + script = template % (controller_ip, instance_floating_ip) + return script + + +def check_for_peering(controller): + cmd = 'show-bgp --cmd "ip bgp neighbors"' + tries = 20 + neighbors = None + bgp_state_regex = re.compile("(BGP state =.*)") + opens_regex = re.compile("Opens:(.*)") + while tries > 0: + if neighbors and 'Established' in neighbors: + break + neighbors = run_odl_cmd(controller, cmd) + logger.info("Output of %s: %s", cmd, neighbors) + if neighbors: + opens = opens_regex.search(neighbors) + if opens: + logger.info("Opens sent/received: %s", opens.group(1)) + state = bgp_state_regex.search(neighbors) + if state: + logger.info("Peering state: %s", state.group(1)) + tries -= 1 + time.sleep(1) + + if not neighbors or 'Established' not in neighbors: + logger.error("Quagga failed to peer with OpenDaylight") + logger.error("OpenDaylight status: %s", neighbors) + return False + + logger.info("Quagga peered with OpenDaylight") + return True + + +def check_for_route_exchange(ip): + """Check that Quagga has learned the route to an IP""" + logger.debug("Checking that '%s' is in the Zebra routing table", ip) + routes, success = exec_cmd("vtysh -c 'show ip route'", verbose=True) + if not success: + return False + logger.debug("Zebra routing table: %s", routes) + return ip in routes diff --git a/sdnvpn/lib/results.py b/sdnvpn/lib/results.py index 5661d07..9f4fd19 100644 --- a/sdnvpn/lib/results.py +++ b/sdnvpn/lib/results.py @@ -114,6 +114,12 @@ class Results(object): def add_success(self, test): self.add_to_summary(2, "PASS", test) + def add_subtest(self, test, successful): + if successful: + self.add_success(test) + else: + self.add_failure(test) + def check_ssh_output(self, vm_source, vm_target, expected, timeout=30): console_log = vm_source.get_console_output() diff --git a/sdnvpn/lib/utils.py b/sdnvpn/lib/utils.py index 00d0fa7..149a37e 100644 --- a/sdnvpn/lib/utils.py +++ b/sdnvpn/lib/utils.py @@ -9,10 +9,12 @@ # import sys import time +import requests +import re +import subprocess import functest.utils.functest_logger as ft_logger import functest.utils.openstack_utils as os_utils -import re from sdnvpn.lib import config as sdnvpn_config @@ -20,6 +22,9 @@ logger = ft_logger.Logger("sndvpn_test_utils").getLogger() common_config = sdnvpn_config.CommonConfig() +ODL_USER = 'admin' +ODL_PASS = 'admin' + def create_net(neutron_client, name): logger.debug("Creating network %s", name) @@ -301,3 +306,58 @@ def open_icmp_ssh(neutron_client, security_group_id): security_group_id, 'tcp', 80, 80) + + +def open_bgp_port(neutron_client, security_group_id): + os_utils.create_secgroup_rule(neutron_client, + security_group_id, + 'tcp', + 179, 179) + + +def exec_cmd(cmd, verbose): + success = True + logger.debug("Executing '%s'" % cmd) + p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) + output = "" + for line in iter(p.stdout.readline, b''): + output += line + + if verbose: + logger.debug(output) + + p.stdout.close() + returncode = p.wait() + if returncode != 0: + logger.error("Command %s failed to execute." % cmd) + success = False + + return output, success + + +def check_odl_fib(ip, controller_ip): + """Check that there is an entry in the ODL Fib for `ip`""" + url = "http://" + controller_ip + \ + ":8181/restconf/config/odl-fib:fibEntries/" + logger.debug("Querring '%s' for FIB entries", url) + res = requests.get(url, auth=(ODL_USER, ODL_PASS)) + if res.status_code != 200: + logger.error("OpenDaylight response status code: %s", res.status_code) + return False + logger.debug("Checking whether '%s' is in the OpenDaylight FIB" + % controller_ip) + logger.debug("OpenDaylight FIB: \n%s" % res.text) + return ip in res.text + + +def run_odl_cmd(odl_node, cmd): + ''' + Run a command in the OpenDaylight Karaf shell + + This is a bit flimsy because of shell quote escaping, make sure that + the cmd passed does not have any top level double quotes or this + function will break. + ''' + karaf_cmd = '/opt/opendaylight/bin/client "%s" ' % cmd + return odl_node.run_cmd(karaf_cmd) -- cgit 1.2.3-korg