3 # Copyright (c) 2017 All rights reserved
4 # This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 """Utilities for setting up quagga peering"""
16 import functest.utils.functest_utils as ft_utils
17 import sdnvpn.lib.config as config
18 from sdnvpn.lib.utils import run_odl_cmd, exec_cmd
20 logger = logging.getLogger('sdnvpn-quagga')
22 COMMON_CONFIG = config.CommonConfig()
25 def odl_add_neighbor(neighbor_ip, controller_ip, controller):
26 # Explicitly pass controller_ip because controller.ip
27 # Might not be accessible from the Quagga instance
28 command = 'configure-bgp -op add-neighbor --as-num 200'
29 command += ' --ip %s --use-source-ip %s' % (neighbor_ip, controller_ip)
30 success = run_odl_cmd(controller, command)
31 # The run_cmd api is really whimsical
32 logger.info("Maybe stdout of %s: %s", command, success)
36 def bootstrap_quagga(fip_addr, controller_ip):
37 script = gen_quagga_setup_script(
40 cmd = "sshpass -popnfv ssh opnfv@%s << EOF %s EOF" % (fip_addr, script)
41 rc = ft_utils.execute_command(cmd)
45 def gen_quagga_setup_script(controller_ip,
48 ip_prefix, rd, irt, ert):
49 with open(COMMON_CONFIG.quagga_setup_script_path) as f:
51 script = template.format(controller_ip,
54 ip_prefix, rd, irt, ert)
58 def check_for_peering(controller):
59 cmd = 'show-bgp --cmd \\"ip bgp neighbors\\"'
62 bgp_state_regex = re.compile("(BGP state =.*)")
63 opens_regex = re.compile("Opens:(.*)")
65 if neighbors and 'Established' in neighbors:
67 neighbors = run_odl_cmd(controller, cmd)
68 logger.info("Output of %s: %s", cmd, neighbors)
70 opens = opens_regex.search(neighbors)
72 logger.info("Opens sent/received: %s", opens.group(1))
73 state = bgp_state_regex.search(neighbors)
75 logger.info("Peering state: %s", state.group(1))
79 if not neighbors or 'Established' not in neighbors:
80 logger.error("Quagga failed to peer with OpenDaylight")
81 logger.error("OpenDaylight status: %s", neighbors)
84 logger.info("Quagga peered with OpenDaylight")
88 def check_for_route_exchange(ip):
89 """Check that Quagga has learned the route to an IP"""
90 logger.debug("Checking that '%s' is in the Zebra routing table", ip)
91 routes, success = exec_cmd("vtysh -c 'show ip route'", verbose=True)
94 logger.debug("Zebra routing table: %s", routes)