summaryrefslogtreecommitdiffstats
path: root/tests/topotests/lib/bgpcheck.py
blob: 5ca35a50a46b671296ddda4ce245fd6e48501289 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
# SPDX-License-Identifier: GPL-2.0-or-later

# Copyright 2023, 6wind
import json

from lib import topotest


def check_show_bgp_vpn_prefix_not_found(router, ipversion, prefix, rd, label=None):
    """
    Check if a given vpn prefix is not present in the BGP RIB
    * 'router': the router to check BGP VPN RIB
    * 'ipversion': The ip version to check: ipv4 or ipv6
    * 'prefix': the IP prefix to check
    * 'rd': the route distinguisher to check
    * 'label: the label to check
    """
    output = json.loads(
        router.vtysh_cmd("show bgp {} vpn {} json".format(ipversion, prefix))
    )
    if label:
        expected = {rd: {"prefix": prefix, "paths": [{"remoteLabel": label}]}}
    else:
        expected = {rd: {"prefix": prefix}}
    ret = topotest.json_cmp(output, expected)
    if ret is None:
        return "not good"
    return None


def check_show_bgp_vpn_prefix_found(
    router, ipversion, prefix, rd, label=None, nexthop=None
):
    """
    Check if a given vpn prefix is present in the BGP RIB
    * 'router': the router to check BGP VPN RIB
    * 'ipversion': The ip version to check: ipv4 or ipv6
    * 'prefix': the IP prefix to check
    * 'rd': the route distinguisher to check
    * 'label: the label to check
    """
    output = json.loads(
        router.vtysh_cmd("show bgp {} vpn {} json".format(ipversion, prefix))
    )
    if label:
        if nexthop:
            expected = {
                rd: {
                    "prefix": prefix,
                    "paths": [{"remoteLabel": label, "nexthops": [{"ip": nexthop}]}],
                }
            }
        else:
            expected = {rd: {"prefix": prefix, "paths": [{"remoteLabel": label}]}}
    else:
        if nexthop:
            expected = {
                rd: {"prefix": prefix, "paths": [{"nexthops": [{"ip": nexthop}]}]}
            }
        else:
            expected = {rd: {"prefix": prefix}}
    return topotest.json_cmp(output, expected)