-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathbgp_cli.py
128 lines (105 loc) · 2.8 KB
/
bgp_cli.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import click
import tabulate
import json
import utilities_common.cli as clicommon
from utilities_common.bgp import (
CFG_BGP_DEVICE_GLOBAL,
BGP_DEVICE_GLOBAL_KEY,
to_str,
)
#
# BGP helpers ---------------------------------------------------------------------------------------------------------
#
def format_attr_value(entry, attr):
""" Helper that formats attribute to be presented in the table output.
Args:
entry (Dict[str, str]): CONFIG DB entry configuration.
attr (Dict): Attribute metadata.
Returns:
str: formatted attribute value.
"""
if attr["is-leaf-list"]:
value = entry.get(attr["name"], [])
return "\n".join(value) if value else "N/A"
return entry.get(attr["name"], "N/A")
#
# BGP CLI -------------------------------------------------------------------------------------------------------------
#
@click.group(
name="bgp",
cls=clicommon.AliasedGroup
)
def BGP():
""" Show BGP configuration """
pass
#
# BGP device-global ---------------------------------------------------------------------------------------------------
#
@BGP.command(
name="device-global"
)
@click.option(
"-j", "--json", "json_format",
help="Display in JSON format",
is_flag=True,
default=False
)
@clicommon.pass_db
@click.pass_context
def DEVICE_GLOBAL(ctx, db, json_format):
""" Show BGP device global state """
header = [
"TSA",
"W-ECMP",
]
body = []
table = db.cfgdb.get_table(CFG_BGP_DEVICE_GLOBAL)
entry = table.get(BGP_DEVICE_GLOBAL_KEY, {})
if not entry:
click.echo("No configuration is present in CONFIG DB")
ctx.exit(0)
if json_format:
json_dict = {
"tsa": to_str(
format_attr_value(
entry,
{
'name': 'tsa_enabled',
'is-leaf-list': False
}
)
),
"w-ecmp": to_str(
format_attr_value(
entry,
{
'name': 'wcmp_enabled',
'is-leaf-list': False
}
)
)
}
click.echo(json.dumps(json_dict, indent=4))
ctx.exit(0)
row = [
to_str(
format_attr_value(
entry,
{
'name': 'tsa_enabled',
'is-leaf-list': False
}
)
),
to_str(
format_attr_value(
entry,
{
'name': 'wcmp_enabled',
'is-leaf-list': False
}
)
)
]
body.append(row)
click.echo(tabulate.tabulate(body, header))