Skip to content

Commit

Permalink
[show] Add 'show interface neighbor expected' command (sonic-net#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivlee1 authored and jleveque committed Jul 19, 2018
1 parent 4a36190 commit d1675c6
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,53 @@ def alias(interfacename):

click.echo(tabulate(body, header))

#
# 'neighbor' group ###
#
@interfaces.group(cls=AliasedGroup, default_if_no_args=False)
def neighbor():
"""Show neighbor related information"""
pass

# 'expected' subcommand ("show interface neighbor expected")
@neighbor.command()
@click.argument('interfacename', required=False)
def expected(interfacename):
"""Show expected neighbor information by interfaces"""
neighbor_cmd = 'sonic-cfggen -d --var-json "DEVICE_NEIGHBOR"'
p1 = subprocess.Popen(neighbor_cmd, shell=True, stdout=subprocess.PIPE)
neighbor_dict = json.loads(p1.stdout.read())

neighbor_metadata_cmd = 'sonic-cfggen -d --var-json "DEVICE_NEIGHBOR_METADATA"'
p2 = subprocess.Popen(neighbor_metadata_cmd, shell=True, stdout=subprocess.PIPE)
neighbor_metadata_dict = json.loads(p2.stdout.read())

#Swap Key and Value from interface: name to name: interface
device2interface_dict = {}
for port in natsorted(neighbor_dict.keys()):
device2interface_dict[neighbor_dict[port]['name']] = {'localPort': port, 'neighborPort': neighbor_dict[port]['port']}

header = ['LocalPort', 'Neighbor', 'NeighborPort', 'NeighborLoopback', 'NeighborMgmt', 'NeighborType']
body = []
if interfacename:
for device in natsorted(neighbor_metadata_dict.keys()):
if device2interface_dict[device]['localPort'] == interfacename:
body.append([device2interface_dict[device]['localPort'],
device,
device2interface_dict[device]['neighborPort'],
neighbor_metadata_dict[device]['lo_addr'],
neighbor_metadata_dict[device]['mgmt_addr'],
neighbor_metadata_dict[device]['type']])
else:
for device in natsorted(neighbor_metadata_dict.keys()):
body.append([device2interface_dict[device]['localPort'],
device,
device2interface_dict[device]['neighborPort'],
neighbor_metadata_dict[device]['lo_addr'],
neighbor_metadata_dict[device]['mgmt_addr'],
neighbor_metadata_dict[device]['type']])

click.echo(tabulate(body, header))

# 'summary' subcommand ("show interfaces summary")
@interfaces.command()
Expand Down

0 comments on commit d1675c6

Please sign in to comment.