Add suport for fuel installer
[sdnvpn.git] / sdnvpn / lib / quagga.py
1 #!/usr/bin/env python
2 #
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
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 """Utilities for setting up quagga peering"""
11
12 import logging
13 import re
14 import time
15
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
19
20 logger = logging.getLogger('sdnvpn-quagga')
21
22 COMMON_CONFIG = config.CommonConfig()
23
24
25 def odl_add_neighbor(neighbor_ip, odl_ip, odl_node):
26     # Explicitly pass odl_ip because odl_node.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, odl_ip)
30     success = run_odl_cmd(odl_node, command)
31     # The run_cmd api is really whimsical
32     logger.info("Maybe stdout of %s: %s", command, success)
33     return success
34
35
36 def bootstrap_quagga(fip_addr, controller_ip):
37     script = gen_quagga_setup_script(
38         controller_ip,
39         fip_addr)
40     cmd = "sshpass -popnfv ssh opnfv@%s << EOF %s EOF" % (fip_addr, script)
41     rc = ft_utils.execute_command(cmd)
42     return rc == 0
43
44
45 def gen_quagga_setup_script(odl_ip,
46                             fake_floating_ip,
47                             ext_net_mask,
48                             ip_prefix, rd, irt, ert):
49     with open(COMMON_CONFIG.quagga_setup_script_path) as f:
50         template = f.read()
51     script = template.format(odl_ip,
52                              fake_floating_ip,
53                              ext_net_mask,
54                              ip_prefix, rd, irt, ert)
55     return script
56
57
58 def check_for_peering(odl_node):
59     cmd = 'show-bgp --cmd \\"ip bgp neighbors\\"'
60     tries = 20
61     neighbors = None
62     bgp_state_regex = re.compile("(BGP state =.*)")
63     opens_regex = re.compile("Opens:(.*)")
64     while tries > 0:
65         if neighbors and 'Established' in neighbors:
66             break
67         neighbors = run_odl_cmd(odl_node, cmd)
68         logger.info("Output of %s: %s", cmd, neighbors)
69         if neighbors:
70             opens = opens_regex.search(neighbors)
71             if opens:
72                 logger.info("Opens sent/received: %s", opens.group(1))
73             state = bgp_state_regex.search(neighbors)
74             if state:
75                 logger.info("Peering state: %s", state.group(1))
76         tries -= 1
77         time.sleep(1)
78
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)
82         return False
83
84     logger.info("Quagga peered with OpenDaylight")
85     return True
86
87
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)
92     if not success:
93         return False
94     logger.debug("Zebra routing table: %s", routes)
95     return ip in routes