Skip to content

Commit 362ec9b

Browse files
authored
[show] vnet advertised-route command (sonic-net#2390)
What I did Added a new show command to see the VNET Tunnel routes being advertised by BGP. The output can be filtered based on IPv4/Ipv6 and community string. How I did it Iterate over the BGP_PROFILE_TABLE in Application DB and ADVERTISE_NETWORK_TABLE in state DB. Correlate the information and filter based on community string. How to verify it run **show vnet advertised-routes ** with optional community string. $ show vnet advertised-route Prefix Profile Community Id ------------------------ ------------------- -------------- 160.62.191.1/32 FROM_SDN_SLB_ROUTES 1234:1235 160.63.191.1/32 FROM_SDN_SLB_ROUTES 1234:1235 160.64.191.1/32 FROM_SDN_SLB_ROUTES 1234:1235 fccc:a250:a251::a6:1/128 fddd:a150:a251::a6:1/128 FROM_SDN_SLB_ROUTES 1234:1235 $ show vnet advertised-route 1234:1235 Prefix Profile Community Id ------------------------ ------------------- -------------- 160.62.191.1/32 FROM_SDN_SLB_ROUTES 1234:1235 160.63.191.1/32 FROM_SDN_SLB_ROUTES 1234:1235 160.64.191.1/32 FROM_SDN_SLB_ROUTES 1234:1235 fddd:a150:a251::a6:1/128 FROM_SDN_SLB_ROUTES 1234:1235
1 parent 2372e29 commit 362ec9b

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

show/vnet.py

+50
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,56 @@ def vnet():
1414
pass
1515

1616

17+
@vnet.command()
18+
@click.argument('args', metavar='[community:string]', required=False)
19+
def advertised_routes(args):
20+
"""Show vnet advertised-routes [community string XXXX:XXXX]"""
21+
state_db = SonicV2Connector()
22+
state_db.connect(state_db.STATE_DB)
23+
appl_db = SonicV2Connector()
24+
appl_db.connect(appl_db.APPL_DB)
25+
community_filter = ''
26+
profile_filter = 'NO_PROFILE'
27+
if args and len(args) > 0:
28+
community_filter = args
29+
30+
bgp_profile_keys = appl_db.keys(appl_db.APPL_DB, "BGP_PROFILE_TABLE:*")
31+
bgp_profile_keys = natsorted(bgp_profile_keys) if bgp_profile_keys else []
32+
profiles = {}
33+
for profilekey in bgp_profile_keys:
34+
val = appl_db.get_all(appl_db.APPL_DB, profilekey)
35+
if val:
36+
community_id = val.get('community_id')
37+
profiles[profilekey.split(':')[1]] = community_id
38+
if community_filter and community_filter == community_id:
39+
profile_filter = profilekey.split(':')[1]
40+
break;
41+
42+
adv_table_keys = state_db.keys(state_db.STATE_DB, "ADVERTISE_NETWORK_TABLE|*")
43+
adv_table_keys = natsorted(adv_table_keys) if adv_table_keys else []
44+
header = ['Prefix', 'Profile', 'Community Id']
45+
table = []
46+
for k in adv_table_keys:
47+
ip = k.split('|')[1]
48+
val = state_db.get_all(appl_db.STATE_DB, k)
49+
profile = val.get('profile') if val else 'NA'
50+
if community_filter:
51+
if profile == profile_filter:
52+
r = []
53+
r.append(ip)
54+
r.append(profile)
55+
r.append(community_filter)
56+
table.append(r)
57+
else:
58+
r = []
59+
r.append(ip)
60+
r.append(profile)
61+
if profile in profiles.keys():
62+
r.append(profiles[profile])
63+
table.append(r)
64+
click.echo(tabulate(table, header))
65+
66+
1767
@vnet.command()
1868
@click.argument('vnet_name', required=True)
1969
def name(vnet_name):

tests/mock_tables/appl_db.json

+3
Original file line numberDiff line numberDiff line change
@@ -329,5 +329,8 @@
329329
"VNET_ROUTE_TUNNEL_TABLE:test_v4_in_v4-0:160.164.191.1/32": {
330330
"endpoint":"100.251.7.1",
331331
"endpoint_monitor":"100.251.7.1"
332+
},
333+
"BGP_PROFILE_TABLE:FROM_SDN_SLB_ROUTES": {
334+
"community_id" : "1234:1235"
332335
}
333336
}

tests/mock_tables/state_db.json

+15
Original file line numberDiff line numberDiff line change
@@ -908,5 +908,20 @@
908908
"rx_interval" : "500",
909909
"multiplier" : "3",
910910
"multihop": "true"
911+
},
912+
"ADVERTISE_NETWORK_TABLE|160.63.191.1/32": {
913+
"profile": "FROM_SDN_SLB_ROUTES"
914+
},
915+
"ADVERTISE_NETWORK_TABLE|160.62.191.1/32": {
916+
"profile": "FROM_SDN_SLB_ROUTES"
917+
},
918+
"ADVERTISE_NETWORK_TABLE|160.64.191.1/32": {
919+
"profile": "FROM_SDN_SLB_ROUTES"
920+
},
921+
"ADVERTISE_NETWORK_TABLE|fddd:a150:a251::a6:1/128": {
922+
"profile": "FROM_SDN_SLB_ROUTES"
923+
},
924+
"ADVERTISE_NETWORK_TABLE|fccc:a250:a251::a6:1/128": {
925+
"profile": ""
911926
}
912927
}

tests/show_vnet_test.py

+48
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,51 @@ def test_show_vnet_routes_all_basic(self):
2727
test_v4_in_v4-0 160.164.191.1/32 100.251.7.1
2828
"""
2929
assert result.output == expected_output
30+
31+
class TestShowVnetAdvertisedRoutesIPX(object):
32+
@classmethod
33+
def setup_class(cls):
34+
print("SETUP")
35+
os.environ["UTILITIES_UNIT_TESTING"] = "1"
36+
37+
def test_show_vnet_adv_routes_ip_basic(self):
38+
runner = CliRunner()
39+
db = Db()
40+
result = runner.invoke(show.cli.commands['vnet'].commands['advertised-routes'], [], obj=db)
41+
assert result.exit_code == 0
42+
expected_output = """\
43+
Prefix Profile Community Id
44+
------------------------ ------------------- --------------
45+
160.62.191.1/32 FROM_SDN_SLB_ROUTES 1234:1235
46+
160.63.191.1/32 FROM_SDN_SLB_ROUTES 1234:1235
47+
160.64.191.1/32 FROM_SDN_SLB_ROUTES 1234:1235
48+
fccc:a250:a251::a6:1/128
49+
fddd:a150:a251::a6:1/128 FROM_SDN_SLB_ROUTES 1234:1235
50+
"""
51+
assert result.output == expected_output
52+
53+
def test_show_vnet_adv_routes_ip_string(self):
54+
runner = CliRunner()
55+
db = Db()
56+
result = runner.invoke(show.cli.commands['vnet'].commands['advertised-routes'], ['1234:1235'], obj=db)
57+
assert result.exit_code == 0
58+
expected_output = """\
59+
Prefix Profile Community Id
60+
------------------------ ------------------- --------------
61+
160.62.191.1/32 FROM_SDN_SLB_ROUTES 1234:1235
62+
160.63.191.1/32 FROM_SDN_SLB_ROUTES 1234:1235
63+
160.64.191.1/32 FROM_SDN_SLB_ROUTES 1234:1235
64+
fddd:a150:a251::a6:1/128 FROM_SDN_SLB_ROUTES 1234:1235
65+
"""
66+
assert result.output == expected_output
67+
68+
def test_show_vnet_adv_routes_ipv6_Error(self):
69+
runner = CliRunner()
70+
db = Db()
71+
result = runner.invoke(show.cli.commands['vnet'].commands['advertised-routes'], ['1230:1235'], obj=db)
72+
assert result.exit_code == 0
73+
expected_output = """\
74+
Prefix Profile Community Id
75+
-------- --------- --------------
76+
"""
77+
assert result.output == expected_output

0 commit comments

Comments
 (0)