Skip to content

Commit

Permalink
[show] vnet advertised-route command (#2390) (#2478)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
siqbal1986 authored Nov 4, 2022
1 parent 9d921a7 commit fff5f67
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
50 changes: 50 additions & 0 deletions show/vnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,56 @@ def vnet():
pass


@vnet.command()
@click.argument('args', metavar='[community:string]', required=False)
def advertised_routes(args):
"""Show vnet advertised-routes [community string XXXX:XXXX]"""
state_db = SonicV2Connector()
state_db.connect(state_db.STATE_DB)
appl_db = SonicV2Connector()
appl_db.connect(appl_db.APPL_DB)
community_filter = ''
profile_filter = 'NO_PROFILE'
if args and len(args) > 0:
community_filter = args

bgp_profile_keys = appl_db.keys(appl_db.APPL_DB, "BGP_PROFILE_TABLE:*")
bgp_profile_keys = natsorted(bgp_profile_keys) if bgp_profile_keys else []
profiles = {}
for profilekey in bgp_profile_keys:
val = appl_db.get_all(appl_db.APPL_DB, profilekey)
if val:
community_id = val.get('community_id')
profiles[profilekey.split(':')[1]] = community_id
if community_filter and community_filter == community_id:
profile_filter = profilekey.split(':')[1]
break;

adv_table_keys = state_db.keys(state_db.STATE_DB, "ADVERTISE_NETWORK_TABLE|*")
adv_table_keys = natsorted(adv_table_keys) if adv_table_keys else []
header = ['Prefix', 'Profile', 'Community Id']
table = []
for k in adv_table_keys:
ip = k.split('|')[1]
val = state_db.get_all(appl_db.STATE_DB, k)
profile = val.get('profile') if val else 'NA'
if community_filter:
if profile == profile_filter:
r = []
r.append(ip)
r.append(profile)
r.append(community_filter)
table.append(r)
else:
r = []
r.append(ip)
r.append(profile)
if profile in profiles.keys():
r.append(profiles[profile])
table.append(r)
click.echo(tabulate(table, header))


@vnet.command()
@click.argument('vnet_name', required=True)
def name(vnet_name):
Expand Down
3 changes: 3 additions & 0 deletions tests/mock_tables/appl_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -329,5 +329,8 @@
"VNET_ROUTE_TUNNEL_TABLE:test_v4_in_v4-0:160.164.191.1/32": {
"endpoint":"100.251.7.1",
"endpoint_monitor":"100.251.7.1"
},
"BGP_PROFILE_TABLE:FROM_SDN_SLB_ROUTES": {
"community_id" : "1234:1235"
}
}
15 changes: 15 additions & 0 deletions tests/mock_tables/state_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -905,5 +905,20 @@
"VNET_ROUTE_TUNNEL_TABLE|Vnet_v6_in_v6-0|fddd:a156:a251::a6:1/128": {
"active_endpoints":"fddd:a100:a251::a10:1,fddd:a101:a251::a10:1",
"state":"active"
},
"ADVERTISE_NETWORK_TABLE|160.63.191.1/32": {
"profile": "FROM_SDN_SLB_ROUTES"
},
"ADVERTISE_NETWORK_TABLE|160.62.191.1/32": {
"profile": "FROM_SDN_SLB_ROUTES"
},
"ADVERTISE_NETWORK_TABLE|160.64.191.1/32": {
"profile": "FROM_SDN_SLB_ROUTES"
},
"ADVERTISE_NETWORK_TABLE|fddd:a150:a251::a6:1/128": {
"profile": "FROM_SDN_SLB_ROUTES"
},
"ADVERTISE_NETWORK_TABLE|fccc:a250:a251::a6:1/128": {
"profile": ""
}
}
48 changes: 48 additions & 0 deletions tests/show_vnet_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,51 @@ def test_show_vnet_routes_all_basic(self):
test_v4_in_v4-0 160.164.191.1/32 100.251.7.1
"""
assert result.output == expected_output

class TestShowVnetAdvertisedRoutesIPX(object):
@classmethod
def setup_class(cls):
print("SETUP")
os.environ["UTILITIES_UNIT_TESTING"] = "1"

def test_show_vnet_adv_routes_ip_basic(self):
runner = CliRunner()
db = Db()
result = runner.invoke(show.cli.commands['vnet'].commands['advertised-routes'], [], obj=db)
assert result.exit_code == 0
expected_output = """\
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
"""
assert result.output == expected_output

def test_show_vnet_adv_routes_ip_string(self):
runner = CliRunner()
db = Db()
result = runner.invoke(show.cli.commands['vnet'].commands['advertised-routes'], ['1234:1235'], obj=db)
assert result.exit_code == 0
expected_output = """\
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
"""
assert result.output == expected_output

def test_show_vnet_adv_routes_ipv6_Error(self):
runner = CliRunner()
db = Db()
result = runner.invoke(show.cli.commands['vnet'].commands['advertised-routes'], ['1230:1235'], obj=db)
assert result.exit_code == 0
expected_output = """\
Prefix Profile Community Id
-------- --------- --------------
"""
assert result.output == expected_output

0 comments on commit fff5f67

Please sign in to comment.