8000 switchport status and config command improvement by 15 times by vikumarks · Pull Request #9 · vikumarks/sonic-utilities · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

switchport status and config command improvement by 15 times #9

New issue 8000

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 43 additions & 75 deletions show/interfaces/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1099,28 +1099,35 @@ def switchport():
def switchport_mode_config(db):
"""Show interface switchport config information"""

port_data = list(db.cfgdb.get_table('PORT').keys())
port_data = db.cfgdb.get_table('PORT')
port_keys = list(port_data.keys())
portchannel_data = list(db.cfgdb.get_table('PORTCHANNEL').keys())

portchannel_member_table = db.cfgdb.get_table('PORTCHANNEL_MEMBER')

for interface in port_data:
if clicommon.interface_is_in_portchannel(portchannel_member_table, interface):
port_data.remove(interface)

keys = port_data + portchannel_data

filter_port_data = [interface for interface in port_keys if not clicommon.interface_is_in_portchannel(portchannel_member_table, interface)]
keys = filter_port_data + portchannel_data
def tablelize(keys):
table = []

for key in natsorted(keys):
r = [clicommon.get_interface_name_for_display(db, key),
clicommon.get_interface_switchport_mode(db, key),
clicommon.get_interface_untagged_vlan_members(db, key),
clicommon.get_interface_tagged_vlan_members(db, key)]
table.append(r)

return table
interface_naming_mode = clicommon.get_interface_naming_mode()
natsorted_keys = natsorted(keys)
if interface_naming_mode != "alias":
interface =natsorted_keys
else:
interface = [clicommon.get_interface_name_for_display(db, key) for key in natsorted_keys]

vlan_member = db.cfgdb.get_table('VLAN_MEMBER')
vlan_member_keys = [key for _, key in vlan_member]
vlan_member = {
iface: {
'vlan_id': int(vlan.lstrip('Vlan')),
'tagging_mode': data.get('tagging_mode')
}
for (vlan, iface), data in vlan_member.items()
}
sw_mode = [clicommon.get_interface_switchport_mode(db, key, vlan_member_keys, port_data) for key in natsorted_keys]
untag_vlan_memb = [vlan_member[intf]['vlan_id'] if intf in vlan_member and vlan_member[intf].get('tagging_mode') == 'untagged' else '' for intf in interface]
tag_vlan_meb = [vlan_member[intf]['vlan_id'] if intf in vlan_member and vlan_member[intf].get('tagging_mode') == 'tagged' else '' for intf in interface]
tabel = list(zip(interface, sw_mode, untag_vlan_memb, tag_vlan_meb))
return tabel

header = ['Interface', 'Mode', 'Untagged', 'Tagged']
click.echo(tabulate(tablelize(keys), header, tablefmt="simple", stralign='left'))
Expand All @@ -1130,69 +1137,30 @@ def tablelize(keys):
@clicommon.pass_db
def switchport_mode_status(db):
"""Show interface switchport status information"""

port_data = list(db.cfgdb.get_table('PORT').keys())
port_data = db.cfgdb.get_table('PORT')
port_keys = list(port_data.keys())
portchannel_data = list(db.cfgdb.get_table('PORTCHANNEL').keys())

portchannel_member_table = db.cfgdb.get_table('PORTCHANNEL_MEMBER')

for interface in port_data:
if clicommon.interface_is_in_portchannel(portchannel_member_table, interface):
port_data.remove(interface)

keys = port_data + portchannel_data

def tablelize(keys):
table = []

for key in natsorted(keys):
r = [clicommon.get_interface_name_for_display(db, key),
clicommon.get_interface_switchport_mode(db, key)]
table.append(r)

return table

header = ['Interface', 'Mode']
click.echo(tabulate(tablelize(keys), header, tablefmt="simple", stralign='left'))

#
# dhcp-mitigation-rate group (show interfaces dhcp-mitigation-rate ...)
#


@interfaces.command(name='dhcp-mitigation-rate')
@click.argument('interfacename', required=False)
@clicommon.pass_db
def dhcp_mitigation_rate(db, interfacename):
"""Show interface dhcp-mitigation-rate information"""

ctx = click.get_current_context()

keys = []

if interfacename is None:
port_data = list(db.cfgdb.get_table('PORT').keys())
keys = port_data

vlan_member_table = db.cfgdb.get_table('VLAN_MEMBER')
vlan_member_keys = [key for _, key in vlan_member_table]
if portchannel_member_table:
filter_port_data = [interface for interface in port_keys if not clicommon.interface_is_in_portchannel(portchannel_member_table, interface)]
else:
if clicommon.is_valid_port(db.cfgdb, interfacename):
pass
elif clicommon.is_valid_portchannel(db.cfgdb, interfacename):
ctx.fail("{} is a PortChannel!".format(interfacename))
else:
ctx.fail("{} does not exist".format(interfacename))

keys.append(interfacename)
filter_port_data = port_keys[:]

keys = filter_port_data + portchannel_data

def tablelize(keys):
table = []
for key in natsorted(keys):
r = [
clicommon.get_interface_name_for_display(db, key),
clicommon.get_interface_dhcp_mitigation_rate(db.cfgdb, key)
]
table.append(r)
return table
interface_naming_mode = clicommon.get_interface_naming_mode()
natsorted_keys = natsorted(keys)
if interface_naming_mode != "alias":
interface = natsorted_keys
else:
interface = clicommon.get_interface_name_for_display(db, natsorted_keys)
modes = [clicommon.get_interface_switchport_mode(db, key, vlan_member_keys, port_data) for key in natsorted_keys]
tabel = list(zip(interface, modes))
return tabel

header = ['Interface', 'DHCP Mitigation Rate']
click.echo(tabulate(tablelize(keys), header, tablefmt="simple", stralign='left'))
32 changes: 15 additions & 17 deletions utilities_common/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,23 +506,21 @@ def get_interface_tagged_vlan_members(db, interface):
return "\n".join(formatted_tagged_vlans)


def get_interface_switchport_mode(db, interface):
port = db.cfgdb.get_entry('PORT', interface)
portchannel = db.cfgdb.get_entry('PORTCHANNEL', interface)
vlan_member_table = db.cfgdb.get_table('VLAN_MEMBER')

vlan_member_keys = []
for _, key in vlan_member_table:
vlan_member_keys.append(key)

switchport_mode = 'routed'
if "mode" in port:
switchport_mode = port['mode']
elif "mode" in portchannel:
switchport_mode = portchannel['mode']
elif interface in vlan_member_keys:
switchport_mode = 'trunk'
return switchport_mode
def get_interface_switchport_mode(db, interface, vlan_member_keys=None, port=None):
if port is None:
port = db.cfgdb.get_table('PORT')
if "mode" in port[interface]:
return port[interface]['mode']
elif vlan_member_keys is None:
vlan_member_table = db.cfgdb.get_table('VLAN_MEMBER')
vlan_member_keys = [key for _, key in vlan_member_table]
if interface in vlan_member_keys:
return 'trunk'
else:
portchannel = db.cfgdb.get_entry('PORTCHANNEL', interface)
if "mode" in portchannel:
return portchannel['mode']
return 'routed'


def is_port_mirror_dst_port(config_db, port):
Expand Down
0