e2885c2d348d2de9216ecfa22ea16b55cd51dbd1
[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, instance_floating_ip):
33     with open(COMMON_CONFIG.quagga_setup_script_path) as f:
34         template = f.read()
35     script = template % (controller_ip, instance_floating_ip)
36     return script
37
38
39 def check_for_peering(controller):
40     cmd = 'show-bgp --cmd "ip bgp neighbors"'
41     tries = 20
42     neighbors = None
43     bgp_state_regex = re.compile("(BGP state =.*)")
44     opens_regex = re.compile("Opens:(.*)")
45     while tries > 0:
46         if neighbors and 'Established' in neighbors:
47             break
48         neighbors = run_odl_cmd(controller, cmd)
49         logger.info("Output of %s: %s", cmd, neighbors)
50         if neighbors:
51             opens = opens_regex.search(neighbors)
52             if opens:
53                 logger.info("Opens sent/received: %s", opens.group(1))
54             state = bgp_state_regex.search(neighbors)
55             if state:
56                 logger.info("Peering state: %s", state.group(1))
57         tries -= 1
58         time.sleep(1)
59
60     if not neighbors or 'Established' not in neighbors:
61         logger.error("Quagga failed to peer with OpenDaylight")
62         logger.error("OpenDaylight status: %s", neighbors)
63         return False
64
65     logger.info("Quagga peered with OpenDaylight")
66     return True
67
68
69 def check_for_route_exchange(ip):
70     """Check that Quagga has learned the route to an IP"""
71     logger.debug("Checking that '%s' is in the Zebra routing table", ip)
72     routes, success = exec_cmd("vtysh -c 'show ip route'", verbose=True)
73     if not success:
74         return False
75     logger.debug("Zebra routing table: %s", routes)
76     return ip in routes