Skip to content

Commit 2c81943

Browse files
Added auto-generated CLIs from Fine-Grained Hashing Yang model (#3740)
What I did Generated config and show CLIs from Fine-Grained ECMP Yang model. PR for Yang Model changes: sonic-net/sonic-buildimage#21545 How I did it sonic-cli-gen generate config sonic-fine-grained-ecmp sonic-cli-gen generate show sonic-fine-grained-ecmp
1 parent 8ab3773 commit 2c81943

File tree

9 files changed

+1147
-2
lines changed

9 files changed

+1147
-2
lines changed

config/plugins/sonic-fine-grained-ecmp_yang.py

Lines changed: 514 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
"""
2+
Auto-generated show CLI plugin.
3+
"""
4+
5+
import click
6+
import tabulate
7+
import natsort
8+
import utilities_common.cli as clicommon
9+
10+
11+
def format_attr_value(entry, attr):
12+
""" Helper that formats attribute to be presented in the table output.
13+
14+
Args:
15+
entry (Dict[str, str]): CONFIG DB entry configuration.
16+
attr (Dict): Attribute metadata.
17+
18+
Returns:
19+
str: formatted attribute value.
20+
"""
21+
22+
if attr["is-leaf-list"]:
23+
return "\n".join(entry.get(attr["name"], []))
24+
return entry.get(attr["name"], "N/A")
25+
26+
27+
def format_group_value(entry, attrs):
28+
""" Helper that formats grouped attribute to be presented in the table output.
29+
30+
Args:
31+
entry (Dict[str, str]): CONFIG DB entry configuration.
32+
attrs (List[Dict]): Attributes metadata that belongs to the same group.
33+
34+
Returns:
35+
str: formatted group attributes.
36+
"""
37+
38+
data = []
39+
for attr in attrs:
40+
if entry.get(attr["name"]):
41+
data.append((attr["name"] + ":", format_attr_value(entry, attr)))
42+
return tabulate.tabulate(data, tablefmt="plain")
43+
44+
45+
@click.group(name="fg-nhg",
46+
cls=clicommon.AliasedGroup,
47+
invoke_without_command=True)
48+
@clicommon.pass_db
49+
def FG_NHG(db):
50+
""" [Callable command group] """
51+
52+
header = [
53+
"NAME",
54+
"BUCKET SIZE",
55+
"MATCH MODE",
56+
"MAX NEXT HOPS",
57+
]
58+
59+
body = []
60+
61+
table = db.cfgdb.get_table("FG_NHG")
62+
for key in natsort.natsorted(table):
63+
entry = table[key]
64+
if not isinstance(key, tuple):
65+
key = (key,)
66+
67+
row = [*key] + [
68+
format_attr_value(
69+
entry,
70+
{'name': 'bucket_size', 'description': 'total hash bucket size desired, recommended value of Lowest Common\
71+
Multiple of 1..max-next-hops', 'is-leaf-list': False, 'is-mandatory': True, 'group': ''}
72+
),
73+
format_attr_value(
74+
entry,
75+
{'name': 'match_mode', 'description': 'The filtering method used to identify when to use Fine Grained vs \
76+
regular route handling. \
77+
-- nexthop-based filters on nexthop IPs only. \
78+
-- route-based filters on both prefix and nexthop IPs. \
79+
-- prefix-based filters on prefix only.', 'is-leaf-list': False, 'is-mandatory': True, 'group': ''}
80+
),
81+
format_attr_value(
82+
entry,
83+
{'name': 'max_next_hops', 'description': 'Applicable only for match_mode = prefix-based. Maximum number \
84+
of nexthops that will be\nreceived in route updates for any of the prefixes that match FG_NHG_PREFIX \
85+
for this FG_NHG.', 'is-leaf-list': False, 'is-mandatory': True, 'group': ''}
86+
),
87+
]
88+
89+
body.append(row)
90+
91+
click.echo(tabulate.tabulate(body, header))
92+
93+
94+
@click.group(name="fg-nhg-prefix",
95+
cls=clicommon.AliasedGroup,
96+
invoke_without_command=True)
97+
@clicommon.pass_db
98+
def FG_NHG_PREFIX(db):
99+
""" [Callable command group] """
100+
101+
header = [
102+
"IP PREFIX",
103+
"FG NHG"
104+
]
105+
106+
body = []
107+
108+
table = db.cfgdb.get_table("FG_NHG_PREFIX")
109+
for key in natsort.natsorted(table):
110+
entry = table[key]
111+
if not isinstance(key, tuple):
112+
key = (key,)
113+
114+
row = [*key] + [
115+
format_attr_value(
116+
entry,
117+
{'name': 'FG_NHG', 'description': 'Fine Grained next-hop group name', 'is-leaf-list': False,
118+
'is-mandatory': True, 'group': ''}
119+
),
120+
]
121+
122+
body.append(row)
123+
124+
click.echo(tabulate.tabulate(body, header))
125+
126+
127+
@click.group(name="fg-nhg-member",
128+
cls=clicommon.AliasedGroup,
129+
invoke_without_command=True)
130+
@clicommon.pass_db
131+
def FG_NHG_MEMBER(db):
132+
""" [Callable command group] """
133+
134+
header = [
135+
"NEXT HOP IP",
136+
"FG NHG",
137+
"BANK",
138+
"LINK"
139+
]
140+
141+
body = []
142+
143+
table = db.cfgdb.get_table("FG_NHG_MEMBER")
144+
for key in natsort.natsorted(table):
145+
entry = table[key]
146+
if not isinstance(key, tuple):
147+
key = (key,)
148+
149+
row = [*key] + [
150+
format_attr_value(
151+
entry,
152+
{'name': 'FG_NHG', 'description': 'Fine Grained next-hop group name', 'is-leaf-list': False,
153+
'is-mandatory': True, 'group': ''}
154+
),
155+
format_attr_value(
156+
entry,
157+
{'name': 'bank', 'description': 'An index which specifies a bank/group in which the \
158+
redistribution is performed', 'is-leaf-list': False, 'is-mandatory': True, 'group': ''}
159+
),
160+
format_attr_value(
161+
entry,
162+
{'name': 'link', 'description': "Link associated with next-hop-ip, if configured, enables \
163+
next-hop withdrawal/addition per link's operational state changes",
164+
'is-leaf-list': False, 'is-mandatory': False, 'group': ''}
165+
),
166+
]
167+
168+
body.append(row)
169+
170+
click.echo(tabulate.tabulate(body, header))
171+
172+
173+
def register(cli):
174+
""" Register new CLI nodes in root CLI.
175+
176+
Args:
177+
cli (click.core.Command): Root CLI node.
178+
Raises:
179+
Exception: when root CLI already has a command
180+
we are trying to register.
181+
"""
182+
cli_nodes = [FG_NHG, FG_NHG_PREFIX, FG_NHG_MEMBER]
183+
for cli_node in cli_nodes:
184+
if cli_node.name in cli.commands:
185+
raise Exception(f"{cli_node.name} already exists in CLI")
186+
cli.add_command(cli_node)

sonic-utilities-data/templates/sonic-cli-gen/show.py.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def format_attr_value(entry, attr):
2828
attr (Dict): Attribute metadata.
2929

3030
Returns:
31-
str: fomatted attribute value.
31+
str: formatted attribute value.
3232
"""
3333

3434
if attr["is-leaf-list"]:
@@ -44,7 +44,7 @@ def format_group_value(entry, attrs):
4444
attrs (List[Dict]): Attributes metadata that belongs to the same group.
4545

4646
Returns:
47-
str: fomatted group attributes.
47+
str: formatted group attributes.
4848
"""
4949

5050
data = []
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""
2+
Module holding the correct values for show CLI command outputs for the fine_grained_ecmp_test.py
3+
"""
4+
5+
show_fg_nhg = """\
6+
NAME BUCKET SIZE MATCH MODE MAX NEXT HOPS
7+
-------- ------------- ------------- ---------------
8+
fg_grp_2 30 nexthop-based N/A
9+
"""
10+
11+
show_fg_nhg_empty = """\
12+
NAME BUCKET SIZE MATCH MODE MAX NEXT HOPS
13+
------ ------------- ------------ ---------------
14+
"""
15+
16+
show_fg_nhg_members = """\
17+
NEXT HOP IP FG NHG BANK LINK
18+
------------- -------- ------ ------
19+
10.10.20.1 fg_grp_1 0 N/A
20+
10.10.20.2 fg_grp_1 1 N/A
21+
"""
22+
23+
show_fg_nhg_members_empty = """\
24+
NEXT HOP IP FG NHG BANK LINK
25+
------------- -------- ------ ------
26+
"""
27+
28+
show_fg_nhg_prefix = """\
29+
IP PREFIX FG NHG
30+
--------------- --------
31+
192.168.11.0/24 fg_grp_1
32+
"""
33+
34+
show_fg_nhg_prefix_empty = """\
35+
IP PREFIX FG NHG
36+
----------- --------
37+
"""
38+
39+
show_fg_nhg_after_update = """\
40+
NAME BUCKET SIZE MATCH MODE MAX NEXT HOPS
41+
-------- ------------- ------------ ---------------
42+
fg_grp_2 120 route-based N/A
43+
"""
44+
45+
show_fg_nhg_members_after_update = """\
46+
NEXT HOP IP FG NHG BANK LINK
47+
------------- -------- ------ ------
48+
10.10.20.1 fg_grp_2 1 N/A
49+
10.10.20.2 fg_grp_2 0 N/A
50+
"""
51+
52+
show_fg_nhg_prefix_after_update = """\
53+
IP PREFIX FG NHG
54+
--------------- --------
55+
192.168.11.0/24 fg_grp_2
56+
"""
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
3+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"FG_NHG|fg_grp_1": {
3+
"bucket_size": "30",
4+
"match_mode": "prefix-based",
5+
"max_next_hops": "6"
6+
},
7+
"FG_NHG|fg_grp_2": {
8+
"bucket_size": "30",
9+
"match_mode": "nexthop-based"
10+
}
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"FG_NHG|fg_grp_1": {
3+
"bucket_size": "30",
4+
"match_mode": "nexthop-based"
5+
},
6+
"FG_NHG_MEMBER|10.10.20.1": {
7+
"FG_NHG": "fg_grp_1",
8+
"bank": "0"
9+
},
10+
"FG_NHG_MEMBER|10.10.20.2": {
11+
"FG_NHG": "fg_grp_1",
12+
"bank": "1"
13+
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"FG_NHG|fg_grp_1": {
3+
"bucket_size": "30",
4+
"match_mode": "prefix-based",
5+
"max_next_hops": "6"
6+
},
7+
"FG_NHG_PREFIX|192.168.11.1": {
8+
"FG_NHG": "fg_grp_1"
9+
}
10+
}

0 commit comments

Comments
 (0)