Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[show] vnet endpoint [ip/ipv6] command #2342

Merged
merged 11 commits into from
Oct 3, 2022
85 changes: 82 additions & 3 deletions show/vnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from natsort import natsorted
from swsscommon.swsscommon import SonicV2Connector, ConfigDBConnector
from tabulate import tabulate

import ipaddress

#
# 'vnet' command ("show vnet")
Expand Down Expand Up @@ -195,6 +195,81 @@ def neighbors():
if not bool(vnet_intfs):
click.echo(tabulate(table, header))

@vnet.command()
@click.argument('args', metavar='[IPADDRESS]', nargs=1, required=False)
def endpoint(args):
"""Show Vxlan tunnel endpoint status"""
"""Specify IPv4 or IPv6 address for detail"""

state_db = SonicV2Connector()
state_db.connect(state_db.STATE_DB)
appl_db = SonicV2Connector()
appl_db.connect(appl_db.APPL_DB)
filter_by_ip = ''
if args and len(args) > 0:
try:
filter_by_ip = ipaddress.ip_network(args)
except ValueError:
# Not ip address just ignore it
print ("wrong parameter",args)
return
# Fetching data from appl_db for VNET TUNNEL ROUTES
vnet_rt_keys = appl_db.keys(appl_db.APPL_DB, "VNET_ROUTE_TUNNEL_TABLE:*")
vnet_rt_keys = natsorted(vnet_rt_keys) if vnet_rt_keys else []
bfd_keys = state_db.keys(state_db.STATE_DB, "BFD_SESSION_TABLE|*")
if not filter_by_ip:
header = ['Endpoint', 'prefix count', 'status']
prefix_count ={}
monitor_dict = {}
table = []
for k in vnet_rt_keys:
val = appl_db.get_all(appl_db.APPL_DB, k)
endpoints = val.get('endpoint').split(',')
monitors = val.get('endpoint_monitor').split(',')
for idx, endpoint in enumerate(endpoints):
monitor_dict[endpoint] = monitors[idx]
if endpoint not in prefix_count:
prefix_count[endpoint] =0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after = for consistency

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

prefix_count[endpoint] += 1
for endpoint in prefix_count:
r = []
r.append(endpoint)
r.append(prefix_count[endpoint])
bfd_session_key = "BFD_SESSION_TABLE|default|default|" + monitor_dict[endpoint]
if bfd_session_key in bfd_keys:
val_state = state_db.get_all(state_db.STATE_DB, bfd_session_key)
r.append(val_state.get('state'))
else:
r.append('Unknown')
table.append(r)
else:
table = []
header = ['Endpoint', 'prefix', 'status']
state = 'Unknown'
prefix =[]
have_status = False
for k in vnet_rt_keys:
val = appl_db.get_all(appl_db.APPL_DB, k)
endpoints = val.get('endpoint').split(',')
monitors = val.get('endpoint_monitor').split(',')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space after =. Please fix spacing in all places

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

for idx, endpoint in enumerate(endpoints):
if args == endpoint:
prefix.append(k.split(":", 2)[2])
if not have_status:
bfd_session_key = "BFD_SESSION_TABLE|default|default|" + monitors[idx]
if bfd_session_key in bfd_keys:
val_state = state_db.get_all(state_db.STATE_DB, bfd_session_key)
state = val_state.get('state')
have_status = True
break
if prefix:
r = []
r.append(args)
r.append(prefix)
r.append(state)
table.append(r)
click.echo(tabulate(table, header))


@vnet.group()
def routes():
Expand All @@ -207,7 +282,8 @@ def all():
"""Show all vnet routes"""
appl_db = SonicV2Connector()
appl_db.connect(appl_db.APPL_DB)

state_db = SonicV2Connector()
state_db.connect(state_db.STATE_DB)
header = ['vnet name', 'prefix', 'nexthop', 'interface']

# Fetching data from appl_db for VNET ROUTES
Expand All @@ -227,7 +303,7 @@ def all():

click.echo()

header = ['vnet name', 'prefix', 'endpoint', 'mac address', 'vni']
header = ['vnet name', 'prefix', 'endpoint', 'mac address', 'vni', 'status']

# Fetching data from appl_db for VNET TUNNEL ROUTES
vnet_rt_keys = appl_db.keys(appl_db.APPL_DB, "VNET_ROUTE_TUNNEL_TABLE:*")
Expand All @@ -237,10 +313,13 @@ def all():
for k in vnet_rt_keys:
r = []
r.extend(k.split(":", 2)[1:])
state_db_key = '|'.join(k.split(":",2))
val = appl_db.get_all(appl_db.APPL_DB, k)
val_state = state_db.get_all(state_db.STATE_DB, state_db_key)
r.append(val.get('endpoint'))
r.append(val.get('mac_address'))
r.append(val.get('vni'))
r.append(val_state.get('state'))
table.append(r)

click.echo(tabulate(table, header))
Expand Down