Skip to content

Commit 8af9aee

Browse files
authored
Show FG_NHG CLI Commands Added (#1056)
1. show fg-nhg-hash-view (shows the current hash bucket view of fg nhg) 2. show fg-nhg-active-hops (shows which set of next-hops are active)
1 parent 1753f22 commit 8af9aee

File tree

5 files changed

+500
-0
lines changed

5 files changed

+500
-0
lines changed

show/fgnhg.py

+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
import click
2+
import ipaddress
3+
from tabulate import tabulate
4+
from swsssdk import ConfigDBConnector
5+
from swsssdk import SonicV2Connector
6+
import utilities_common.cli as clicommon
7+
from collections import OrderedDict
8+
9+
@click.group(cls=clicommon.AliasedGroup)
10+
def fgnhg():
11+
"""Show FGNHG information"""
12+
pass
13+
14+
@fgnhg.command()
15+
@click.argument('nhg', required=False)
16+
def active_hops(nhg):
17+
config_db = ConfigDBConnector()
18+
config_db.connect()
19+
fg_nhg_prefix_table = {}
20+
fg_nhg_alias = {}
21+
fg_nhg_prefix_table = config_db.get_table('FG_NHG_PREFIX')
22+
23+
for key, value in fg_nhg_prefix_table.items():
24+
fg_nhg_alias[key] = value['FG_NHG']
25+
26+
state_db = SonicV2Connector(host='127.0.0.1')
27+
state_db.connect(state_db.STATE_DB, False) # Make one attempt only STATE_DB
28+
29+
TABLE_NAME_SEPARATOR = '|'
30+
prefix = 'FG_ROUTE_TABLE' + TABLE_NAME_SEPARATOR
31+
_hash = '{}{}'.format(prefix, '*')
32+
table_keys = []
33+
table_keys = state_db.keys(state_db.STATE_DB, _hash)
34+
t_dict = {}
35+
table = []
36+
output_dict = {}
37+
38+
if nhg is None:
39+
for nhg_prefix in table_keys :
40+
t_dict = state_db.get_all(state_db.STATE_DB, nhg_prefix)
41+
vals = sorted(set([val for val in t_dict.values()]))
42+
for nh_ip in vals:
43+
if nhg_prefix in output_dict:
44+
output_dict[nhg_prefix].append(nh_ip.split("@")[0])
45+
else:
46+
output_dict[nhg_prefix] = [nh_ip.split("@")[0]]
47+
48+
nhg_prefix_report = (nhg_prefix.split("|")[1])
49+
header = ["FG_NHG_PREFIX", "Active Next Hops"]
50+
formatted_nhps = ','.replace(',', '\n').join(output_dict[nhg_prefix])
51+
table.append([nhg_prefix_report, formatted_nhps])
52+
53+
click.echo(tabulate(table, header, tablefmt = "grid"))
54+
55+
else:
56+
for nhg_prefix, alias in fg_nhg_alias.items():
57+
if nhg == alias:
58+
if ":" in nhg_prefix:
59+
for key in table_keys:
60+
mod_key = key.split("|")[1].split("/")[0]
61+
mod_nhg_prefix = nhg_prefix.split("/")[0]
62+
if ipaddress.ip_address(unicode(mod_key)).exploded == ipaddress.ip_address(unicode(mod_nhg_prefix)).exploded:
63+
t_dict = state_db.get_all(state_db.STATE_DB, key)
64+
nhg_prefix = "FG_ROUTE_TABLE|" + nhg_prefix
65+
else:
66+
nhg_prefix = "FG_ROUTE_TABLE|" + nhg_prefix
67+
t_dict = state_db.get_all(state_db.STATE_DB, nhg_prefix)
68+
69+
vals = sorted(set([val for val in t_dict.values()]))
70+
71+
for nh_ip in vals:
72+
if nhg_prefix in output_dict:
73+
output_dict[nhg_prefix].append(nh_ip.split("@")[0])
74+
else:
75+
output_dict[nhg_prefix] = [nh_ip.split("@")[0]]
76+
77+
nhg_prefix_report = (nhg_prefix.split("|")[1])
78+
formatted_nhps = ','.replace(',', '\n').join(output_dict[nhg_prefix])
79+
table.append([nhg_prefix_report, formatted_nhps])
80+
header = ["FG_NHG_PREFIX", "Active Next Hops"]
81+
click.echo(tabulate(table, header, tablefmt = "grid"))
82+
83+
84+
@fgnhg.command()
85+
@click.argument('nhg', required=False)
86+
def hash_view(nhg):
87+
config_db = ConfigDBConnector()
88+
config_db.connect()
89+
fg_nhg_prefix_table = {}
90+
fg_nhg_alias = {}
91+
fg_nhg_prefix_table = config_db.get_table('FG_NHG_PREFIX')
92+
93+
for key, value in fg_nhg_prefix_table.items():
94+
fg_nhg_alias[key] = value['FG_NHG']
95+
96+
state_db = SonicV2Connector(host='127.0.0.1')
97+
state_db.connect(state_db.STATE_DB, False) # Make one attempt only STATE_DB
98+
99+
TABLE_NAME_SEPARATOR = '|'
100+
prefix = 'FG_ROUTE_TABLE' + TABLE_NAME_SEPARATOR
101+
_hash = '{}{}'.format(prefix, '*')
102+
table_keys = []
103+
table_keys = state_db.keys(state_db.STATE_DB, _hash)
104+
t_dict = {}
105+
table = []
106+
output_dict = {}
107+
bank_dict = {}
108+
109+
if nhg is None:
110+
for nhg_prefix in table_keys :
111+
bank_dict = {}
112+
t_dict = state_db.get_all(state_db.STATE_DB, nhg_prefix)
113+
vals = sorted(set([val for val in t_dict.values()]))
114+
115+
for nh_ip in vals:
116+
bank_ids = sorted([int(k) for k, v in t_dict.items() if v == nh_ip])
117+
118+
bank_ids = [str(x) for x in bank_ids]
119+
120+
if nhg_prefix in output_dict:
121+
output_dict[nhg_prefix].append(nh_ip.split("@")[0])
122+
else:
123+
output_dict[nhg_prefix] = [nh_ip.split("@")[0]]
124+
bank_dict[nh_ip.split("@")[0]] = bank_ids
125+
126+
bank_dict = OrderedDict(sorted(bank_dict.items()))
127+
nhg_prefix_report = (nhg_prefix.split("|")[1])
128+
header = ["FG_NHG_PREFIX", "Next Hop", "Hash buckets"]
129+
130+
for nhip,val in bank_dict.items():
131+
formatted_banks = ','.replace(',', '\n').join(bank_dict[nhip])
132+
table.append([nhg_prefix_report, nhip, formatted_banks])
133+
134+
click.echo(tabulate(table, header, tablefmt = "grid"))
135+
136+
else:
137+
for nhg_prefix, alias in fg_nhg_alias.items():
138+
if nhg == alias:
139+
if ":" in nhg_prefix:
140+
for key in table_keys:
141+
mod_key = key.split("|")[1].split("/")[0]
142+
mod_nhg_prefix = nhg_prefix.split("/")[0]
143+
if ipaddress.ip_address(unicode(mod_key)).exploded == ipaddress.ip_address(unicode(mod_nhg_prefix)).exploded:
144+
t_dict = state_db.get_all(state_db.STATE_DB, key)
145+
nhg_prefix = "FG_ROUTE_TABLE|" + nhg_prefix
146+
else:
147+
nhg_prefix = "FG_ROUTE_TABLE|" + nhg_prefix
148+
t_dict = state_db.get_all(state_db.STATE_DB, nhg_prefix)
149+
150+
vals = sorted(set([val for val in t_dict.values()]))
151+
152+
for nh_ip in vals:
153+
bank_ids = sorted([int(k) for k, v in t_dict.items() if v == nh_ip])
154+
bank_ids = [str(x) for x in bank_ids]
155+
if nhg_prefix in output_dict:
156+
output_dict[nhg_prefix].append(nh_ip.split("@")[0])
157+
else:
158+
output_dict[nhg_prefix] = [nh_ip.split("@")[0]]
159+
bank_dict[nh_ip.split("@")[0]] = bank_ids
160+
161+
nhg_prefix_report = (nhg_prefix.split("|")[1])
162+
bank_dict = OrderedDict(sorted(bank_dict.items()))
163+
header = ["FG_NHG_PREFIX", "Next Hop", "Hash buckets"]
164+
165+
for nhip,val in bank_dict.items():
166+
formatted_banks = ','.replace(',', '\n').join(bank_dict[nhip])
167+
table.append([nhg_prefix_report, nhip, formatted_banks])
168+
169+
click.echo(tabulate(table, header, tablefmt = "grid"))

show/main.py

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import utilities_common.cli as clicommon
1919
import vlan
2020
import system_health
21+
import fgnhg
2122

2223
from sonic_py_common import device_info, multi_asic
2324
from swsssdk import ConfigDBConnector, SonicV2Connector
@@ -130,6 +131,7 @@ def cli(ctx):
130131
cli.add_command(kube.kubernetes)
131132
cli.add_command(vlan.vlan)
132133
cli.add_command(system_health.system_health)
134+
cli.add_command(fgnhg.fgnhg)
133135

134136
#
135137
# 'vrf' command ("show vrf")

0 commit comments

Comments
 (0)