Fix quagga peering by working around the NAT
[sdnvpn.git] / sdnvpn / lib / quagga.py
1 """Utilities for setting up quagga peering"""
2
3 import re
4 import time
5
6 import functest.utils.functest_logger as ft_logger
7 import functest.utils.functest_utils as ft_utils
8 import sdnvpn.lib.config as config
9 from sdnvpn.lib.utils import run_odl_cmd, exec_cmd
10
11 logger = ft_logger.Logger("sdnvpn-quagga").getLogger()
12
13 COMMON_CONFIG = config.CommonConfig()
14
15
16 def odl_add_neighbor(neighbor_ip, controller):
17     command = 'configure-bgp -op add-neighbor --as-num 200'
18     command += ' --ip %s --use-source-ip %s' % (neighbor_ip, controller.ip)
19     success = run_odl_cmd(controller, command)
20     return success
21
22
23 def bootstrap_quagga(fip_addr, controller_ip):
24     script = gen_quagga_setup_script(
25         controller_ip,
26         fip_addr)
27     cmd = "sshpass -popnfv ssh opnfv@%s << EOF %s EOF" % (fip_addr, script)
28     rc = ft_utils.execute_command(cmd)
29     return rc == 0
30
31
32 def gen_quagga_setup_script(controller_ip,
33                             fake_floating_ip,
34                             ext_net_mask):
35     with open(COMMON_CONFIG.quagga_setup_script_path) as f:
36         template = f.read()
37     script = template % (controller_ip,
38                          fake_floating_ip,
39                          ext_net_mask)
40     return script
41
42
43 def check_for_peering(controller):
44     cmd = 'show-bgp --cmd "ip bgp neighbors"'
45     tries = 20
46     neighbors = None
47     bgp_state_regex = re.compile("(BGP state =.*)")
48     opens_regex = re.compile("Opens:(.*)")
49     while tries > 0:
50         if neighbors and 'Established' in neighbors:
51             break
52         neighbors = run_odl_cmd(controller, cmd)
53         logger.info("Output of %s: %s", cmd, neighbors)
54         if neighbors:
55             opens = opens_regex.search(neighbors)
56             if opens:
57                 logger.info("Opens sent/received: %s", opens.group(1))
58             state = bgp_state_regex.search(neighbors)
59             if state:
60                 logger.info("Peering state: %s", state.group(1))
61         tries -= 1
62         time.sleep(1)
63
64     if not neighbors or 'Established' not in neighbors:
65         logger.error("Quagga failed to peer with OpenDaylight")
66         logger.error("OpenDaylight status: %s", neighbors)
67         return False
68
69     logger.info("Quagga peered with OpenDaylight")
70     return True
71
72
73 def check_for_route_exchange(ip):
74     """Check that Quagga has learned the route to an IP"""
75     logger.debug("Checking that '%s' is in the Zebra routing table", ip)
76     routes, success = exec_cmd("vtysh -c 'show ip route'", verbose=True)
77     if not success:
78         return False
79     logger.debug("Zebra routing table: %s", routes)
80     return ip in routes