From ab663065de1cdc1c4161da5591f2b88865236f37 Mon Sep 17 00:00:00 2001 From: paavaanan Date: Sat, 11 Aug 2018 10:44:30 -0400 Subject: [PATCH 01/13] show commands with alias support for linux utilities --- show/main.py | 97 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 90 insertions(+), 7 deletions(-) diff --git a/show/main.py b/show/main.py index b779652efb..4417b6d28e 100755 --- a/show/main.py +++ b/show/main.py @@ -5,6 +5,7 @@ import getpass import json import os +import re import subprocess import sys from click_default_group import DefaultGroup @@ -28,6 +29,12 @@ class Config(object): def __init__(self): self.path = os.getcwd() self.aliases = {} + global port_dict + + cmd = 'sonic-cfggen -d --var-json "PORT"' + p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) + port_dict = json.loads(p.stdout.read()) + def read_config(self, filename): parser = configparser.RawConfigParser() @@ -129,6 +136,58 @@ def run_command(command, display_cmd=False): if rc != 0: sys.exit(rc) + +def get_interface_mode(): + mode = os.getenv('SONIC_CLI_IFACE_MODE') + if mode is None: + mode = "default" + return mode + + +def interface_name_to_alias(interface_name): + """Return alias interface name if default + name is given as argument + + """ + + if interface_name is not None: + for port_name in port_dict.keys(): + if interface_name == port_name: + return port_dict[port_name]['alias'] + print "Invalid interface {}".format(interface_name) + + raise click.Abort() + + +def run_command_in_alias_mode(command, linux_command=False): + """Replace command output results from default + interface name to vendor specific name + """ + + process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) + + while True: + output = process.stdout.readline() + if output == '' and process.poll() is not None: + break + if output: + if linux_command: + result = re.findall('Ethernet\w+', output) + interface_name = ''.join(result) + if interface_name != "": + output = output.replace(interface_name, + interface_name_to_alias(interface_name)) + print output.rstrip('\n') + else: + """Framework need to be designed for other tabulated + commands + """ + + rc = process.poll() + if rc != 0: + sys.exit(rc) + + CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help', '-?']) # @@ -161,7 +220,10 @@ def arp(ipaddress, iface, verbose): if iface is not None: cmd += " -if {}".format(iface) - run_command(cmd, display_cmd=verbose) + if get_interface_mode() == "alias": + run_command_in_alias_mode(cmd, linux_command=True) + else: + run_command(cmd, display_cmd=verbose) # # 'ndp' command ("show ndp") @@ -310,7 +372,10 @@ def eeprom(interfacename, dump_dom, verbose): if interfacename is not None: cmd += " -p {}".format(interfacename) - run_command(cmd, display_cmd=verbose) + if get_interface_mode() == "alias": + run_command_in_alias_mode(cmd, linux_command=True) + else: + run_command(cmd, display_cmd=verbose) @transceiver.command() @@ -495,7 +560,11 @@ def route(ipaddress, verbose): cmd += '"' - run_command(cmd, display_cmd=verbose) + if get_interface_mode() == "alias": + run_command_in_alias_mode(cmd, linux_command=True) + else: + run_command(cmd, display_cmd=verbose) + # 'protocol' command @ip.command() @@ -533,7 +602,10 @@ def route(ipaddress, verbose): cmd += '"' - run_command(cmd, display_cmd=verbose) + if get_interface_mode() == "alias": + run_command_in_alias_mode(cmd, linux_command=True) + else: + run_command(cmd, display_cmd=verbose) # 'protocol' command @@ -587,7 +659,10 @@ def neighbors(interfacename, verbose): if interfacename is not None: cmd += " {}".format(interfacename) - run_command(cmd, display_cmd=verbose) + if get_interface_mode() == "alias": + run_command_in_alias_mode(cmd, linux_command=True) + else: + run_command(cmd, display_cmd=verbose) # 'table' subcommand ("show lldp table") @lldp.command() @@ -926,7 +1001,11 @@ def vlan(): def brief(verbose): """Show all bridge information""" cmd = "sudo brctl show" - run_command(cmd, display_cmd=verbose) + if get_interface_mode() == "alias": + run_command_in_alias_mode(cmd, linux_command=True) + else: + run_command(cmd, display_cmd=verbose) + @vlan.command() @click.argument('bridge_name', required=True) @@ -955,7 +1034,11 @@ def tablelize(keys, data): r = [] r.append(k) r.append(data[k]['vlanid']) - r.append(m) + if get_interface_mode() == "alias": + alias = interface_name_to_alias(m) + r.append(alias) + else: + r.append(m) entry = config_db.get_entry('VLAN_MEMBER', (k, m)) mode = entry.get('tagging_mode') From e33af6f3bafdf58bbd2ffdcb0bd911db0eed4d8d Mon Sep 17 00:00:00 2001 From: paavaanan Date: Wed, 22 Aug 2018 08:43:04 -0400 Subject: [PATCH 02/13] show commands with alias support for sonic utilities --- show/main.py | 222 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 174 insertions(+), 48 deletions(-) diff --git a/show/main.py b/show/main.py index 4417b6d28e..35fb37b5fd 100755 --- a/show/main.py +++ b/show/main.py @@ -29,11 +29,6 @@ class Config(object): def __init__(self): self.path = os.getcwd() self.aliases = {} - global port_dict - - cmd = 'sonic-cfggen -d --var-json "PORT"' - p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) - port_dict = json.loads(p.stdout.read()) def read_config(self, filename): @@ -45,6 +40,45 @@ def read_config(self, filename): pass +class Alias(object): + """Object to Alias conversion""" + + def __init__(self): + self.vendor_name_length = 0 + cmd = 'sonic-cfggen -d --var-json "PORT"' + p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) + self.port_dict = json.loads(p.stdout.read()) + + for port_name in self.port_dict.keys(): + if self.vendor_name_length < len( + self.port_dict[port_name]['alias']): + self.vendor_name_length = len( + self.port_dict[port_name]['alias']) + + def interface_name_to_alias(self, interface_name): + """Return alias interface name if default + name is given as argument + """ + if interface_name is not None: + for port_name in self.port_dict.keys(): + if interface_name == port_name: + return self.port_dict[port_name]['alias'] + click.echo("Invalid interface {}".format(interface_name)) + + raise click.Abort() + + def interface_alias_to_name(self, interface_alias): + """Return default interface name if alias name is given as argument + """ + if interface_alias is not None: + for port_name in self.port_dict.keys(): + if interface_alias == self.port_dict[port_name]['alias']: + return port_name + click.echo("Invalid interface {}".format(interface_alias)) + + raise click.Abort() + + # Global Config object _config = None @@ -144,24 +178,13 @@ def get_interface_mode(): return mode -def interface_name_to_alias(interface_name): - """Return alias interface name if default - name is given as argument - - """ - - if interface_name is not None: - for port_name in port_dict.keys(): - if interface_name == port_name: - return port_dict[port_name]['alias'] - print "Invalid interface {}".format(interface_name) +# Global Alias object +_alias = Alias() - raise click.Abort() - -def run_command_in_alias_mode(command, linux_command=False): - """Replace command output results from default - interface name to vendor specific name +def run_command_in_alias_mode(command, command_type=0, linux_command=False): + """Run command and replace all instances of SONiC interface names + in output with vendor-sepecific interface aliases. """ process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) @@ -169,19 +192,84 @@ def run_command_in_alias_mode(command, linux_command=False): while True: output = process.stdout.readline() if output == '' and process.poll() is not None: - break + break if output: if linux_command: - result = re.findall('Ethernet\w+', output) - interface_name = ''.join(result) - if interface_name != "": - output = output.replace(interface_name, - interface_name_to_alias(interface_name)) - print output.rstrip('\n') + for port_name in _alias.port_dict.keys(): + regex = re.compile(r"\b{}\b".format(port_name)) + result = re.findall(regex, output) + if result: + interface_name = ''.join(result) + output = output.replace( + interface_name, _alias.interface_name_to_alias( + interface_name)) + click.echo(output.rstrip('\n')) + else: - """Framework need to be designed for other tabulated - commands - """ + output = output.lstrip() + index = 0 + + if command_type == 1: + """Show interface counters""" + if output.startswith("IFACE"): + output = output.replace("IFACE", "IFACE".rjust( + _alias.vendor_name_length)) + elif command_type == 2: + """Show pfc counters""" + if output.startswith("Port Tx"): + output = output.replace("Port Tx", "Port Tx".rjust( + _alias.vendor_name_length)) + elif output.startswith("Port Rx"): + output = output.replace("Port Rx", "Port Rx".rjust( + _alias.vendor_name_length)) + elif command_type == 3: + """show interface transceiver lpmode + show interface transceiver presence + show interface transceiver eeprom""" + if output.startswith("Port"): + output = output.replace("Port", "Port".rjust( + _alias.vendor_name_length)) + elif command_type == 4: + """show lldp table + This need to be updated""" + if output.startswith("LocalPort"): + output = output.replace("LocalPort", "LocalPort".rjust( + _alias.vendor_name_length)) + elif command_type == 5: + """show mac""" + index = 3 + if output.startswith("No."): + output = " " + output + output = re.sub( + 'Type', ' Type', output) + elif output[0].isdigit(): + output = " " + output + + else: + click.echo("Invalid command type") + + # Adjust tabulation width to length of alias name + if output.startswith("---"): + word = output.split() + dword = word[index] + underline = dword.rjust(_alias.vendor_name_length, '-') + word[index] = underline + output = ' ' .join(word) + + # Replace default interface name to vendor-name + for port_name in _alias.port_dict.keys(): + regex = re.compile(r"\b{}\b".format(port_name)) + result = re.findall(regex, output) + if result: + interface_name = ''.join(result) + alias_name = _alias.interface_name_to_alias( + interface_name) + if len(alias_name) < _alias.vendor_name_length: + alias_name = alias_name.rjust( + _alias.vendor_name_length) + output = output.replace(interface_name, alias_name) + + click.echo(output.rstrip('\n')) rc = process.poll() if rc != 0: @@ -221,7 +309,7 @@ def arp(ipaddress, iface, verbose): cmd += " -if {}".format(iface) if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, linux_command=True) + run_command_in_alias_mode(cmd, command_type=0, linux_command=True) else: run_command(cmd, display_cmd=verbose) @@ -348,7 +436,10 @@ def summary(interfacename, verbose): if interfacename is not None: cmd += " {}".format(interfacename) - run_command(cmd, display_cmd=verbose) + if get_interface_mode() == "alias": + run_command_in_alias_mode(cmd, command_type=0, linux_command=True) + else: + run_command(cmd, display_cmd=verbose) @interfaces.group(cls=AliasedGroup, default_if_no_args=False) @@ -373,7 +464,7 @@ def eeprom(interfacename, dump_dom, verbose): cmd += " -p {}".format(interfacename) if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, linux_command=True) + run_command_in_alias_mode(cmd, command_type=0, linux_command=True) else: run_command(cmd, display_cmd=verbose) @@ -413,6 +504,9 @@ def description(interfacename, verbose): cmd = "intfutil description" + if get_interface_mode() == "alias" and interfacename is not None: + interfacename = _alias.interface_alias_to_name(interfacename) + if interfacename is not None: cmd += " {}".format(interfacename) @@ -427,6 +521,9 @@ def status(interfacename, verbose): cmd = "intfutil status" + if get_interface_mode() == "alias" and interfacename is not None: + interfacename = _alias.interface_alias_to_name(interfacename) + if interfacename is not None: cmd += " {}".format(interfacename) @@ -452,7 +549,11 @@ def counters(period, printall, clear, verbose): if period is not None: cmd += " -p {}".format(period) - run_command(cmd, display_cmd=verbose) + if get_interface_mode() == "alias": + run_command_in_alias_mode(cmd, command_type=1, linux_command=False) + else: + run_command(cmd, display_cmd=verbose) + # 'portchannel' subcommand ("show interfaces portchannel") @interfaces.command() @@ -460,7 +561,11 @@ def counters(period, printall, clear, verbose): def portchannel(verbose): """Show PortChannel information""" cmd = "teamshow" - run_command(cmd, display_cmd=verbose) + + if get_interface_mode() == "alias": + run_command_in_alias_mode(cmd, command_type=1, linux_command=True) + else: + run_command(cmd, display_cmd=verbose) # # 'pfc' group ("show pfc ...") @@ -483,7 +588,11 @@ def counters(clear, verbose): if clear: cmd += " -c" - run_command(cmd, display_cmd=verbose) + if get_interface_mode() == "alias": + run_command_in_alias_mode(cmd, command_type=2, linux_command=False) + else: + run_command(cmd, display_cmd=verbose) + # # 'queue' group ("show queue ...") @@ -510,7 +619,10 @@ def counters(interfacename, clear, verbose): if interfacename is not None: cmd += " -p {}".format(interfacename) - run_command(cmd, display_cmd=verbose) + if get_interface_mode() == "alias": + run_command_in_alias_mode(cmd, command_type=3, linux_command=False) + else: + run_command(cmd, display_cmd=verbose) # # 'mac' command ("show mac ...") @@ -531,7 +643,11 @@ def mac(vlan, port, verbose): if port is not None: cmd += " -p {}".format(port) - run_command(cmd, display_cmd=verbose) + if get_interface_mode() == "alias": + run_command_in_alias_mode(cmd, command_type=5, linux_command=False) + else: + run_command(cmd, display_cmd=verbose) + # # 'ip' group ("show ip ...") @@ -561,7 +677,7 @@ def route(ipaddress, verbose): cmd += '"' if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, linux_command=True) + run_command_in_alias_mode(cmd, command_type=0, linux_command=True) else: run_command(cmd, display_cmd=verbose) @@ -603,7 +719,7 @@ def route(ipaddress, verbose): cmd += '"' if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, linux_command=True) + run_command_in_alias_mode(cmd, command_type=0, linux_command=True) else: run_command(cmd, display_cmd=verbose) @@ -660,7 +776,7 @@ def neighbors(interfacename, verbose): cmd += " {}".format(interfacename) if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, linux_command=True) + run_command_in_alias_mode(cmd, command_type=0, linux_command=True) else: run_command(cmd, display_cmd=verbose) @@ -670,7 +786,11 @@ def neighbors(interfacename, verbose): def table(verbose): """Show LLDP neighbors in tabular format""" cmd = "sudo lldpshow" - run_command(cmd, display_cmd=verbose) + if get_interface_mode() == "alias": + run_command_in_alias_mode(cmd, command_type=4, linux_command=False) + else: + run_command(cmd, display_cmd=verbose) + # # 'platform' group ("show platform ...") @@ -827,7 +947,8 @@ def cpu(verbose): # Run top in batch mode to prevent unexpected newline after each newline cmd = "top -bn 1 -o %CPU" run_command(cmd, display_cmd=verbose) - + + # 'memory' subcommand @processes.command() @click.option('--verbose', is_flag=True, help="Enable verbose output") @@ -1002,7 +1123,7 @@ def brief(verbose): """Show all bridge information""" cmd = "sudo brctl show" if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, linux_command=True) + run_command_in_alias_mode(cmd, command_type=0, linux_command=True) else: run_command(cmd, display_cmd=verbose) @@ -1035,7 +1156,7 @@ def tablelize(keys, data): r.append(k) r.append(data[k]['vlanid']) if get_interface_mode() == "alias": - alias = interface_name_to_alias(m) + alias = interface_name_to_alias(m) r.append(alias) else: r.append(m) @@ -1165,7 +1286,8 @@ def acl(): @click.option('--verbose', is_flag=True, help="Enable verbose output") def rule(table_name, rule_id, verbose): """Show existing ACL rules""" - cmd = "acl-loader show rule" + #cmd = "acl-loader show rule" + cmd = "cat /tmp/show_acl_table" if table_name is not None: cmd += " {}".format(table_name) @@ -1173,7 +1295,11 @@ def rule(table_name, rule_id, verbose): if rule_id is not None: cmd += " {}".format(rule_id) - run_command(cmd, display_cmd=verbose) + if get_interface_mode() == "alias": + run_command_in_alias_mode(cmd, command_type=0, linux_command=True) + else: + run_command(cmd, display_cmd=verbose) + # 'table' subcommand ("show acl table") From cd42d7b74e993e067ca02436e1c7c745df72257a Mon Sep 17 00:00:00 2001 From: paavaanan Date: Thu, 23 Aug 2018 06:18:36 -0400 Subject: [PATCH 03/13] added alias class support for vlan commands --- show/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/show/main.py b/show/main.py index 35fb37b5fd..acff614bca 100755 --- a/show/main.py +++ b/show/main.py @@ -1156,7 +1156,7 @@ def tablelize(keys, data): r.append(k) r.append(data[k]['vlanid']) if get_interface_mode() == "alias": - alias = interface_name_to_alias(m) + alias = _alias.interface_name_to_alias(m) r.append(alias) else: r.append(m) From c6d0c462b34c223c5266dec743de81098e1a102f Mon Sep 17 00:00:00 2001 From: paavaanan Date: Tue, 28 Aug 2018 09:23:47 -0400 Subject: [PATCH 04/13] Addressed review comments for show commands --- show/main.py | 86 +++++++++++++++++++++++++--------------------------- 1 file changed, 42 insertions(+), 44 deletions(-) diff --git a/show/main.py b/show/main.py index acff614bca..baac71133a 100755 --- a/show/main.py +++ b/show/main.py @@ -40,19 +40,19 @@ def read_config(self, filename): pass -class Alias(object): - """Object to Alias conversion""" +class InterfaceAliasConverter(object): + """Class which handles conversion between interface name and alias""" def __init__(self): - self.vendor_name_length = 0 + self.vendor_name_max_length = 0 cmd = 'sonic-cfggen -d --var-json "PORT"' p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) self.port_dict = json.loads(p.stdout.read()) for port_name in self.port_dict.keys(): - if self.vendor_name_length < len( + if self.vendor_name_max_length < len( self.port_dict[port_name]['alias']): - self.vendor_name_length = len( + self.vendor_name_max_length = len( self.port_dict[port_name]['alias']) def interface_name_to_alias(self, interface_name): @@ -63,8 +63,8 @@ def interface_name_to_alias(self, interface_name): for port_name in self.port_dict.keys(): if interface_name == port_name: return self.port_dict[port_name]['alias'] - click.echo("Invalid interface {}".format(interface_name)) + click.echo("Invalid interface {}".format(interface_name)) raise click.Abort() def interface_alias_to_name(self, interface_alias): @@ -74,8 +74,8 @@ def interface_alias_to_name(self, interface_alias): for port_name in self.port_dict.keys(): if interface_alias == self.port_dict[port_name]['alias']: return port_name - click.echo("Invalid interface {}".format(interface_alias)) + click.echo("Invalid interface {}".format(interface_alias)) raise click.Abort() @@ -179,10 +179,10 @@ def get_interface_mode(): # Global Alias object -_alias = Alias() +_alias = InterfaceAliasConverter() -def run_command_in_alias_mode(command, command_type=0, linux_command=False): +def run_command_in_alias_mode(command, linux_command=False): """Run command and replace all instances of SONiC interface names in output with vendor-sepecific interface aliases. """ @@ -209,33 +209,34 @@ def run_command_in_alias_mode(command, command_type=0, linux_command=False): output = output.lstrip() index = 0 - if command_type == 1: + if command == "portstat": """Show interface counters""" if output.startswith("IFACE"): output = output.replace("IFACE", "IFACE".rjust( - _alias.vendor_name_length)) - elif command_type == 2: + _alias.vendor_name_max_length)) + elif command == "pfcstat": """Show pfc counters""" if output.startswith("Port Tx"): output = output.replace("Port Tx", "Port Tx".rjust( - _alias.vendor_name_length)) + _alias.vendor_name_max_length)) elif output.startswith("Port Rx"): output = output.replace("Port Rx", "Port Rx".rjust( - _alias.vendor_name_length)) - elif command_type == 3: - """show interface transceiver lpmode - show interface transceiver presence - show interface transceiver eeprom""" + _alias.vendor_name_max_length)) + elif (command == "sudo sfputil show eeprom") or( + command == "sudo sfputil show presence") or( + command == "sudo sfputil show lpmode"): + """show interface transceiver lpmode, + presence, eeprom + """ if output.startswith("Port"): output = output.replace("Port", "Port".rjust( - _alias.vendor_name_length)) - elif command_type == 4: - """show lldp table - This need to be updated""" + _alias.vendor_name_max_length)) + elif command == "sudo lldpshow": + """show lldp table""" if output.startswith("LocalPort"): output = output.replace("LocalPort", "LocalPort".rjust( - _alias.vendor_name_length)) - elif command_type == 5: + _alias.vendor_name_max_length)) + elif command == "fdbshow": """show mac""" index = 3 if output.startswith("No."): @@ -245,14 +246,11 @@ def run_command_in_alias_mode(command, command_type=0, linux_command=False): elif output[0].isdigit(): output = " " + output - else: - click.echo("Invalid command type") - # Adjust tabulation width to length of alias name if output.startswith("---"): word = output.split() dword = word[index] - underline = dword.rjust(_alias.vendor_name_length, '-') + underline = dword.rjust(_alias.vendor_name_max_length, '-') word[index] = underline output = ' ' .join(word) @@ -264,9 +262,9 @@ def run_command_in_alias_mode(command, command_type=0, linux_command=False): interface_name = ''.join(result) alias_name = _alias.interface_name_to_alias( interface_name) - if len(alias_name) < _alias.vendor_name_length: + if len(alias_name) < _alias.vendor_name_max_length: alias_name = alias_name.rjust( - _alias.vendor_name_length) + _alias.vendor_name_max_length) output = output.replace(interface_name, alias_name) click.echo(output.rstrip('\n')) @@ -309,7 +307,7 @@ def arp(ipaddress, iface, verbose): cmd += " -if {}".format(iface) if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, command_type=0, linux_command=True) + run_command_in_alias_mode(cmd, linux_command=True) else: run_command(cmd, display_cmd=verbose) @@ -437,7 +435,7 @@ def summary(interfacename, verbose): cmd += " {}".format(interfacename) if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, command_type=0, linux_command=True) + run_command_in_alias_mode(cmd, linux_command=True) else: run_command(cmd, display_cmd=verbose) @@ -464,7 +462,7 @@ def eeprom(interfacename, dump_dom, verbose): cmd += " -p {}".format(interfacename) if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, command_type=0, linux_command=True) + run_command_in_alias_mode(cmd, linux_command=True) else: run_command(cmd, display_cmd=verbose) @@ -550,7 +548,7 @@ def counters(period, printall, clear, verbose): cmd += " -p {}".format(period) if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, command_type=1, linux_command=False) + run_command_in_alias_mode(cmd, linux_command=False) else: run_command(cmd, display_cmd=verbose) @@ -563,7 +561,7 @@ def portchannel(verbose): cmd = "teamshow" if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, command_type=1, linux_command=True) + run_command_in_alias_mode(cmd, linux_command=True) else: run_command(cmd, display_cmd=verbose) @@ -589,7 +587,7 @@ def counters(clear, verbose): cmd += " -c" if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, command_type=2, linux_command=False) + run_command_in_alias_mode(cmd, linux_command=False) else: run_command(cmd, display_cmd=verbose) @@ -620,7 +618,7 @@ def counters(interfacename, clear, verbose): cmd += " -p {}".format(interfacename) if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, command_type=3, linux_command=False) + run_command_in_alias_mode(cmd, linux_command=False) else: run_command(cmd, display_cmd=verbose) @@ -644,7 +642,7 @@ def mac(vlan, port, verbose): cmd += " -p {}".format(port) if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, command_type=5, linux_command=False) + run_command_in_alias_mode(cmd, linux_command=False) else: run_command(cmd, display_cmd=verbose) @@ -677,7 +675,7 @@ def route(ipaddress, verbose): cmd += '"' if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, command_type=0, linux_command=True) + run_command_in_alias_mode(cmd, linux_command=True) else: run_command(cmd, display_cmd=verbose) @@ -719,7 +717,7 @@ def route(ipaddress, verbose): cmd += '"' if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, command_type=0, linux_command=True) + run_command_in_alias_mode(cmd, linux_command=True) else: run_command(cmd, display_cmd=verbose) @@ -776,7 +774,7 @@ def neighbors(interfacename, verbose): cmd += " {}".format(interfacename) if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, command_type=0, linux_command=True) + run_command_in_alias_mode(cmd, linux_command=True) else: run_command(cmd, display_cmd=verbose) @@ -787,7 +785,7 @@ def table(verbose): """Show LLDP neighbors in tabular format""" cmd = "sudo lldpshow" if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, command_type=4, linux_command=False) + run_command_in_alias_mode(cmd, linux_command=False) else: run_command(cmd, display_cmd=verbose) @@ -1123,7 +1121,7 @@ def brief(verbose): """Show all bridge information""" cmd = "sudo brctl show" if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, command_type=0, linux_command=True) + run_command_in_alias_mode(cmd, linux_command=True) else: run_command(cmd, display_cmd=verbose) @@ -1296,7 +1294,7 @@ def rule(table_name, rule_id, verbose): cmd += " {}".format(rule_id) if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, command_type=0, linux_command=True) + run_command_in_alias_mode(cmd, linux_command=True) else: run_command(cmd, display_cmd=verbose) From 3161fc2560b17c587049df36d195dd45ac26525b Mon Sep 17 00:00:00 2001 From: paavaanan Date: Thu, 30 Aug 2018 13:01:18 -0400 Subject: [PATCH 05/13] show command changes with additional alias commands --- show/main.py | 96 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 56 insertions(+), 40 deletions(-) diff --git a/show/main.py b/show/main.py index baac71133a..0e2670c83f 100755 --- a/show/main.py +++ b/show/main.py @@ -55,7 +55,7 @@ def __init__(self): self.vendor_name_max_length = len( self.port_dict[port_name]['alias']) - def interface_name_to_alias(self, interface_name): + def name_to_alias(self, interface_name): """Return alias interface name if default name is given as argument """ @@ -67,7 +67,7 @@ def interface_name_to_alias(self, interface_name): click.echo("Invalid interface {}".format(interface_name)) raise click.Abort() - def interface_alias_to_name(self, interface_alias): + def alias_to_name(self, interface_alias): """Return default interface name if alias name is given as argument """ if interface_alias is not None: @@ -179,7 +179,7 @@ def get_interface_mode(): # Global Alias object -_alias = InterfaceAliasConverter() +_interface = InterfaceAliasConverter() def run_command_in_alias_mode(command, linux_command=False): @@ -195,47 +195,55 @@ def run_command_in_alias_mode(command, linux_command=False): break if output: if linux_command: - for port_name in _alias.port_dict.keys(): + for port_name in _interface.port_dict.keys(): regex = re.compile(r"\b{}\b".format(port_name)) result = re.findall(regex, output) if result: interface_name = ''.join(result) - output = output.replace( - interface_name, _alias.interface_name_to_alias( - interface_name)) + if not output.startswith(" PortID:"): + output = output.replace( + interface_name, _interface.name_to_alias( + interface_name)) click.echo(output.rstrip('\n')) else: - output = output.lstrip() index = 0 + alias_name = "" + output = output.lstrip() if command == "portstat": """Show interface counters""" if output.startswith("IFACE"): output = output.replace("IFACE", "IFACE".rjust( - _alias.vendor_name_max_length)) + _interface.vendor_name_max_length)) elif command == "pfcstat": """Show pfc counters""" if output.startswith("Port Tx"): output = output.replace("Port Tx", "Port Tx".rjust( - _alias.vendor_name_max_length)) + _interface.vendor_name_max_length)) elif output.startswith("Port Rx"): output = output.replace("Port Rx", "Port Rx".rjust( - _alias.vendor_name_max_length)) - elif (command == "sudo sfputil show eeprom") or( - command == "sudo sfputil show presence") or( - command == "sudo sfputil show lpmode"): + _interface.vendor_name_max_length)) + elif (command.startswith("sudo sfputil show")): """show interface transceiver lpmode, presence, eeprom """ if output.startswith("Port"): output = output.replace("Port", "Port".rjust( - _alias.vendor_name_max_length)) + _interface.vendor_name_max_length)) elif command == "sudo lldpshow": """show lldp table""" if output.startswith("LocalPort"): output = output.replace("LocalPort", "LocalPort".rjust( - _alias.vendor_name_max_length)) + _interface.vendor_name_max_length)) + elif command == "queuestat": + """show queue counters""" + if output.startswith("LocalPort"): + output = output.replace("LocalPort", "LocalPort".rjust( + _interface.vendor_name_max_length)) + if output.startswith("Port"): + output = output.replace("Port", "Port".rjust( + _interface.vendor_name_max_length)) elif command == "fdbshow": """show mac""" index = 3 @@ -250,22 +258,22 @@ def run_command_in_alias_mode(command, linux_command=False): if output.startswith("---"): word = output.split() dword = word[index] - underline = dword.rjust(_alias.vendor_name_max_length, '-') + underline = dword.rjust(_interface.vendor_name_max_length, '-') word[index] = underline output = ' ' .join(word) # Replace default interface name to vendor-name - for port_name in _alias.port_dict.keys(): - regex = re.compile(r"\b{}\b".format(port_name)) - result = re.findall(regex, output) - if result: - interface_name = ''.join(result) - alias_name = _alias.interface_name_to_alias( - interface_name) - if len(alias_name) < _alias.vendor_name_max_length: - alias_name = alias_name.rjust( - _alias.vendor_name_max_length) - output = output.replace(interface_name, alias_name) + word = output.split() + if word: + interface_name = word[index] + for port_name in natsorted(_interface.port_dict.keys()): + if interface_name == port_name: + alias_name = _interface.port_dict[port_name]['alias'] + if alias_name: + if len(alias_name) < _interface.vendor_name_max_length: + alias_name = alias_name.rjust( + _interface.vendor_name_max_length) + output = output.replace(interface_name, alias_name, 1) click.echo(output.rstrip('\n')) @@ -432,6 +440,9 @@ def summary(interfacename, verbose): cmd = "/sbin/ifconfig" if interfacename is not None: + if get_interface_mode() == "alias": + interfacename = _interface.alias_to_name(interfacename) + cmd += " {}".format(interfacename) if get_interface_mode() == "alias": @@ -502,8 +513,9 @@ def description(interfacename, verbose): cmd = "intfutil description" - if get_interface_mode() == "alias" and interfacename is not None: - interfacename = _alias.interface_alias_to_name(interfacename) + if interfacename is not None: + if get_interface_mode() == "alias": + interfacename = _interface.alias_to_name(interfacename) if interfacename is not None: cmd += " {}".format(interfacename) @@ -519,8 +531,9 @@ def status(interfacename, verbose): cmd = "intfutil status" - if get_interface_mode() == "alias" and interfacename is not None: - interfacename = _alias.interface_alias_to_name(interfacename) + if interfacename is not None: + if get_interface_mode() == "alias": + interfacename = _interface.alias_to_name(interfacename) if interfacename is not None: cmd += " {}".format(interfacename) @@ -611,6 +624,10 @@ def counters(interfacename, clear, verbose): cmd = "queuestat" + if interfacename is not None: + if get_interface_mode() == "alias": + interfacename = _interface.alias_to_name(interfacename) + if clear: cmd += " -c" else: @@ -1154,7 +1171,7 @@ def tablelize(keys, data): r.append(k) r.append(data[k]['vlanid']) if get_interface_mode() == "alias": - alias = _alias.interface_name_to_alias(m) + alias = _interface.name_to_alias(m) r.append(alias) else: r.append(m) @@ -1284,8 +1301,7 @@ def acl(): @click.option('--verbose', is_flag=True, help="Enable verbose output") def rule(table_name, rule_id, verbose): """Show existing ACL rules""" - #cmd = "acl-loader show rule" - cmd = "cat /tmp/show_acl_table" + cmd = "acl-loader show rule" if table_name is not None: cmd += " {}".format(table_name) @@ -1293,10 +1309,7 @@ def rule(table_name, rule_id, verbose): if rule_id is not None: cmd += " {}".format(rule_id) - if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, linux_command=True) - else: - run_command(cmd, display_cmd=verbose) + run_command(cmd, display_cmd=verbose) @@ -1311,7 +1324,10 @@ def table(table_name, verbose): if table_name is not None: cmd += " {}".format(table_name) - run_command(cmd, display_cmd=verbose) + if get_interface_mode() == "alias": + run_command_in_alias_mode(cmd, linux_command=True) + else: + run_command(cmd, display_cmd=verbose) # From a38bcdbee952ebc9cf4ee652901775e31dc48ff1 Mon Sep 17 00:00:00 2001 From: paavaanan Date: Thu, 30 Aug 2018 13:08:44 -0400 Subject: [PATCH 06/13] removal of unwated code --- show/main.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/show/main.py b/show/main.py index 0e2670c83f..6dddafafde 100755 --- a/show/main.py +++ b/show/main.py @@ -238,9 +238,6 @@ def run_command_in_alias_mode(command, linux_command=False): _interface.vendor_name_max_length)) elif command == "queuestat": """show queue counters""" - if output.startswith("LocalPort"): - output = output.replace("LocalPort", "LocalPort".rjust( - _interface.vendor_name_max_length)) if output.startswith("Port"): output = output.replace("Port", "Port".rjust( _interface.vendor_name_max_length)) From 867047cc7b5c364219d92b0762b7b5c6ab982e01 Mon Sep 17 00:00:00 2001 From: paavaanan Date: Thu, 6 Sep 2018 08:40:10 -0400 Subject: [PATCH 07/13] removal of linux_cmd variable and retaining run_cmd() --- show/main.py | 286 +++++++++++++++++++++++++-------------------------- 1 file changed, 141 insertions(+), 145 deletions(-) diff --git a/show/main.py b/show/main.py index 6dddafafde..315e97006c 100755 --- a/show/main.py +++ b/show/main.py @@ -30,7 +30,6 @@ def __init__(self): self.path = os.getcwd() self.aliases = {} - def read_config(self, filename): parser = configparser.RawConfigParser() parser.read([filename]) @@ -157,6 +156,10 @@ def run_command(command, display_cmd=False): if display_cmd: click.echo(click.style("Command: ", fg='cyan') + click.style(command, fg='green')) + if get_interface_mode() == "alias" and not command.startswith("intfutil"): + run_command_in_alias_mode(command) + raise sys.exit(0) + proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) while True: @@ -182,7 +185,39 @@ def get_interface_mode(): _interface = InterfaceAliasConverter() -def run_command_in_alias_mode(command, linux_command=False): +def print_output_in_alias_mode(output, index): + """Convert and print all instances of SONiC interface + name to vendor-sepecific interface aliases. + """ + + alias_name = "" + interface_name = "" + + # Adjust tabulation width to length of alias name + if output.startswith("---"): + word = output.split() + dword = word[index] + underline = dword.rjust(_interface.vendor_name_max_length, '-') + word[index] = underline + output = ' ' .join(word) + + # Replace default interface name to vendor-name + word = output.split() + if word: + interface_name = word[index] + for port_name in natsorted(_interface.port_dict.keys()): + if interface_name == port_name: + alias_name = _interface.port_dict[port_name]['alias'] + if alias_name: + if len(alias_name) < _interface.vendor_name_max_length: + alias_name = alias_name.rjust( + _interface.vendor_name_max_length) + output = output.replace(interface_name, alias_name, 1) + + click.echo(output.rstrip('\n')) + + +def run_command_in_alias_mode(command): """Run command and replace all instances of SONiC interface names in output with vendor-sepecific interface aliases. """ @@ -193,86 +228,87 @@ def run_command_in_alias_mode(command, linux_command=False): output = process.stdout.readline() if output == '' and process.poll() is not None: break + if output: - if linux_command: - for port_name in _interface.port_dict.keys(): - regex = re.compile(r"\b{}\b".format(port_name)) - result = re.findall(regex, output) - if result: - interface_name = ''.join(result) - if not output.startswith(" PortID:"): - output = output.replace( - interface_name, _interface.name_to_alias( - interface_name)) - click.echo(output.rstrip('\n')) + index = 1 + raw_output = output + output = output.lstrip() - else: + if command.startswith("portstat"): + """Show interface counters""" index = 0 - alias_name = "" - output = output.lstrip() - - if command == "portstat": - """Show interface counters""" - if output.startswith("IFACE"): - output = output.replace("IFACE", "IFACE".rjust( - _interface.vendor_name_max_length)) - elif command == "pfcstat": - """Show pfc counters""" - if output.startswith("Port Tx"): - output = output.replace("Port Tx", "Port Tx".rjust( - _interface.vendor_name_max_length)) - elif output.startswith("Port Rx"): - output = output.replace("Port Rx", "Port Rx".rjust( - _interface.vendor_name_max_length)) - elif (command.startswith("sudo sfputil show")): - """show interface transceiver lpmode, - presence, eeprom - """ - if output.startswith("Port"): - output = output.replace("Port", "Port".rjust( - _interface.vendor_name_max_length)) - elif command == "sudo lldpshow": - """show lldp table""" - if output.startswith("LocalPort"): - output = output.replace("LocalPort", "LocalPort".rjust( - _interface.vendor_name_max_length)) - elif command == "queuestat": - """show queue counters""" - if output.startswith("Port"): - output = output.replace("Port", "Port".rjust( - _interface.vendor_name_max_length)) - elif command == "fdbshow": - """show mac""" - index = 3 - if output.startswith("No."): - output = " " + output - output = re.sub( - 'Type', ' Type', output) - elif output[0].isdigit(): - output = " " + output - - # Adjust tabulation width to length of alias name - if output.startswith("---"): - word = output.split() - dword = word[index] - underline = dword.rjust(_interface.vendor_name_max_length, '-') - word[index] = underline - output = ' ' .join(word) - - # Replace default interface name to vendor-name - word = output.split() - if word: - interface_name = word[index] - for port_name in natsorted(_interface.port_dict.keys()): - if interface_name == port_name: - alias_name = _interface.port_dict[port_name]['alias'] - if alias_name: - if len(alias_name) < _interface.vendor_name_max_length: - alias_name = alias_name.rjust( - _interface.vendor_name_max_length) - output = output.replace(interface_name, alias_name, 1) - - click.echo(output.rstrip('\n')) + if output.startswith("IFACE"): + output = output.replace("IFACE", "IFACE".rjust( + _interface.vendor_name_max_length)) + print_output_in_alias_mode(output, index) + + elif command == "pfcstat": + """Show pfc counters""" + index = 0 + if output.startswith("Port Tx"): + output = output.replace("Port Tx", "Port Tx".rjust( + _interface.vendor_name_max_length)) + + elif output.startswith("Port Rx"): + output = output.replace("Port Rx", "Port Rx".rjust( + _interface.vendor_name_max_length)) + print_output_in_alias_mode(output, index) + + elif (command.startswith("sudo sfputil show")): + """show interface transceiver lpmode, + presence, eeprom + """ + index = 0 + if output.startswith("Port"): + output = output.replace("Port", "Port".rjust( + _interface.vendor_name_max_length)) + print_output_in_alias_mode(output, index) + + elif command == "sudo lldpshow": + """show lldp table""" + index = 0 + if output.startswith("LocalPort"): + output = output.replace("LocalPort", "LocalPort".rjust( + _interface.vendor_name_max_length)) + print_output_in_alias_mode(output, index) + + elif command.startswith("queuestat"): + """show queue counters""" + index = 0 + if output.startswith("Port"): + output = output.replace("Port", "Port".rjust( + _interface.vendor_name_max_length)) + print_output_in_alias_mode(output, index) + + elif command == "fdbshow": + """show mac""" + index = 3 + if output.startswith("No."): + output = " " + output + output = re.sub( + 'Type', ' Type', output) + elif output[0].isdigit(): + output = " " + output + print_output_in_alias_mode(output, index) + elif command.startswith("nbrshow"): + """show arp""" + index = 2 + if "Vlan" in output: + output = output.replace('Vlan', ' Vlan') + print_output_in_alias_mode(output, index) + + else: + if index: + for port_name in _interface.port_dict.keys(): + regex = re.compile(r"\b{}\b".format(port_name)) + result = re.findall(regex, raw_output) + if result: + interface_name = ''.join(result) + if not raw_output.startswith(" PortID:"): + raw_output = raw_output.replace( + interface_name, _interface.name_to_alias( + interface_name)) + click.echo(raw_output.rstrip('\n')) rc = process.poll() if rc != 0: @@ -309,12 +345,14 @@ def arp(ipaddress, iface, verbose): cmd += " -ip {}".format(ipaddress) if iface is not None: + if get_interface_mode() == "alias": + if not ((iface.startswith("PortChannel")) or + (iface.startswith("eth"))): + iface = _interface.alias_to_name(iface) + cmd += " -if {}".format(iface) - if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, linux_command=True) - else: - run_command(cmd, display_cmd=verbose) + run_command(cmd, display_cmd=verbose) # # 'ndp' command ("show ndp") @@ -360,6 +398,10 @@ def alias(interfacename): body = [] if interfacename is not None: + + if get_interface_mode() == "alias": + interfacename = _interface.alias_to_name(interfacename) + # If we're given an interface name, output name and alias for that interface only if interfacename in port_dict: if 'alias' in port_dict[interfacename]: @@ -442,10 +484,7 @@ def summary(interfacename, verbose): cmd += " {}".format(interfacename) - if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, linux_command=True) - else: - run_command(cmd, display_cmd=verbose) + run_command(cmd, display_cmd=verbose) @interfaces.group(cls=AliasedGroup, default_if_no_args=False) @@ -469,10 +508,7 @@ def eeprom(interfacename, dump_dom, verbose): if interfacename is not None: cmd += " -p {}".format(interfacename) - if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, linux_command=True) - else: - run_command(cmd, display_cmd=verbose) + run_command(cmd, display_cmd=verbose) @transceiver.command() @@ -514,7 +550,6 @@ def description(interfacename, verbose): if get_interface_mode() == "alias": interfacename = _interface.alias_to_name(interfacename) - if interfacename is not None: cmd += " {}".format(interfacename) run_command(cmd, display_cmd=verbose) @@ -532,7 +567,6 @@ def status(interfacename, verbose): if get_interface_mode() == "alias": interfacename = _interface.alias_to_name(interfacename) - if interfacename is not None: cmd += " {}".format(interfacename) run_command(cmd, display_cmd=verbose) @@ -557,11 +591,7 @@ def counters(period, printall, clear, verbose): if period is not None: cmd += " -p {}".format(period) - if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, linux_command=False) - else: - run_command(cmd, display_cmd=verbose) - + run_command(cmd, display_cmd=verbose) # 'portchannel' subcommand ("show interfaces portchannel") @interfaces.command() @@ -569,11 +599,7 @@ def counters(period, printall, clear, verbose): def portchannel(verbose): """Show PortChannel information""" cmd = "teamshow" - - if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, linux_command=True) - else: - run_command(cmd, display_cmd=verbose) + run_command(cmd, display_cmd=verbose) # # 'pfc' group ("show pfc ...") @@ -596,11 +622,7 @@ def counters(clear, verbose): if clear: cmd += " -c" - if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, linux_command=False) - else: - run_command(cmd, display_cmd=verbose) - + run_command(cmd, display_cmd=verbose) # # 'queue' group ("show queue ...") @@ -631,10 +653,7 @@ def counters(interfacename, clear, verbose): if interfacename is not None: cmd += " -p {}".format(interfacename) - if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, linux_command=False) - else: - run_command(cmd, display_cmd=verbose) + run_command(cmd, display_cmd=verbose) # # 'mac' command ("show mac ...") @@ -655,11 +674,7 @@ def mac(vlan, port, verbose): if port is not None: cmd += " -p {}".format(port) - if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, linux_command=False) - else: - run_command(cmd, display_cmd=verbose) - + run_command(cmd, display_cmd=verbose) # # 'ip' group ("show ip ...") @@ -688,11 +703,7 @@ def route(ipaddress, verbose): cmd += '"' - if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, linux_command=True) - else: - run_command(cmd, display_cmd=verbose) - + run_command(cmd, display_cmd=verbose) # 'protocol' command @ip.command() @@ -730,10 +741,7 @@ def route(ipaddress, verbose): cmd += '"' - if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, linux_command=True) - else: - run_command(cmd, display_cmd=verbose) + run_command(cmd, display_cmd=verbose) # 'protocol' command @@ -785,12 +793,12 @@ def neighbors(interfacename, verbose): cmd = "sudo lldpctl" if interfacename is not None: + if get_interface_mode() == "alias": + interfacename = _interface.alias_to_name(interfacename) + cmd += " {}".format(interfacename) - if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, linux_command=True) - else: - run_command(cmd, display_cmd=verbose) + run_command(cmd, display_cmd=verbose) # 'table' subcommand ("show lldp table") @lldp.command() @@ -798,11 +806,7 @@ def neighbors(interfacename, verbose): def table(verbose): """Show LLDP neighbors in tabular format""" cmd = "sudo lldpshow" - if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, linux_command=False) - else: - run_command(cmd, display_cmd=verbose) - + run_command(cmd, display_cmd=verbose) # # 'platform' group ("show platform ...") @@ -1134,11 +1138,7 @@ def vlan(): def brief(verbose): """Show all bridge information""" cmd = "sudo brctl show" - if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, linux_command=True) - else: - run_command(cmd, display_cmd=verbose) - + run_command(cmd, display_cmd=verbose) @vlan.command() @click.argument('bridge_name', required=True) @@ -1309,7 +1309,6 @@ def rule(table_name, rule_id, verbose): run_command(cmd, display_cmd=verbose) - # 'table' subcommand ("show acl table") @acl.command() @click.argument('table_name', required=False) @@ -1321,10 +1320,7 @@ def table(table_name, verbose): if table_name is not None: cmd += " {}".format(table_name) - if get_interface_mode() == "alias": - run_command_in_alias_mode(cmd, linux_command=True) - else: - run_command(cmd, display_cmd=verbose) + run_command(cmd, display_cmd=verbose) # From 04f747e3f7e56ff68a615eaf22d7582634c21c10 Mon Sep 17 00:00:00 2001 From: paavaanan Date: Fri, 7 Sep 2018 08:05:58 -0400 Subject: [PATCH 08/13] show command class instance name change --- show/main.py | 53 +++++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/show/main.py b/show/main.py index 315e97006c..4f2e16efa4 100755 --- a/show/main.py +++ b/show/main.py @@ -156,6 +156,8 @@ def run_command(command, display_cmd=False): if display_cmd: click.echo(click.style("Command: ", fg='cyan') + click.style(command, fg='green')) + # No conversion needed for intfutil commands as it already displays + # both SONiC interface name and alias name for all interfaces. if get_interface_mode() == "alias" and not command.startswith("intfutil"): run_command_in_alias_mode(command) raise sys.exit(0) @@ -181,8 +183,8 @@ def get_interface_mode(): return mode -# Global Alias object -_interface = InterfaceAliasConverter() +# Global class instance for SONiC interface name to alias conversion +convert_interface = InterfaceAliasConverter() def print_output_in_alias_mode(output, index): @@ -197,21 +199,21 @@ def print_output_in_alias_mode(output, index): if output.startswith("---"): word = output.split() dword = word[index] - underline = dword.rjust(_interface.vendor_name_max_length, '-') + underline = dword.rjust(convert_interface.vendor_name_max_length, '-') word[index] = underline output = ' ' .join(word) - # Replace default interface name to vendor-name + # Replace SONiC interface name with vendor alias word = output.split() if word: interface_name = word[index] - for port_name in natsorted(_interface.port_dict.keys()): + for port_name in natsorted(convert_interface.port_dict.keys()): if interface_name == port_name: - alias_name = _interface.port_dict[port_name]['alias'] + alias_name = convert_interface.port_dict[port_name]['alias'] if alias_name: - if len(alias_name) < _interface.vendor_name_max_length: + if len(alias_name) < convert_interface.vendor_name_max_length: alias_name = alias_name.rjust( - _interface.vendor_name_max_length) + convert_interface.vendor_name_max_length) output = output.replace(interface_name, alias_name, 1) click.echo(output.rstrip('\n')) @@ -239,7 +241,7 @@ def run_command_in_alias_mode(command): index = 0 if output.startswith("IFACE"): output = output.replace("IFACE", "IFACE".rjust( - _interface.vendor_name_max_length)) + convert_interface.vendor_name_max_length)) print_output_in_alias_mode(output, index) elif command == "pfcstat": @@ -247,11 +249,11 @@ def run_command_in_alias_mode(command): index = 0 if output.startswith("Port Tx"): output = output.replace("Port Tx", "Port Tx".rjust( - _interface.vendor_name_max_length)) + convert_interface.vendor_name_max_length)) elif output.startswith("Port Rx"): output = output.replace("Port Rx", "Port Rx".rjust( - _interface.vendor_name_max_length)) + convert_interface.vendor_name_max_length)) print_output_in_alias_mode(output, index) elif (command.startswith("sudo sfputil show")): @@ -261,7 +263,7 @@ def run_command_in_alias_mode(command): index = 0 if output.startswith("Port"): output = output.replace("Port", "Port".rjust( - _interface.vendor_name_max_length)) + convert_interface.vendor_name_max_length)) print_output_in_alias_mode(output, index) elif command == "sudo lldpshow": @@ -269,7 +271,7 @@ def run_command_in_alias_mode(command): index = 0 if output.startswith("LocalPort"): output = output.replace("LocalPort", "LocalPort".rjust( - _interface.vendor_name_max_length)) + convert_interface.vendor_name_max_length)) print_output_in_alias_mode(output, index) elif command.startswith("queuestat"): @@ -277,7 +279,7 @@ def run_command_in_alias_mode(command): index = 0 if output.startswith("Port"): output = output.replace("Port", "Port".rjust( - _interface.vendor_name_max_length)) + convert_interface.vendor_name_max_length)) print_output_in_alias_mode(output, index) elif command == "fdbshow": @@ -299,15 +301,16 @@ def run_command_in_alias_mode(command): else: if index: - for port_name in _interface.port_dict.keys(): + for port_name in convert_interface.port_dict.keys(): regex = re.compile(r"\b{}\b".format(port_name)) result = re.findall(regex, raw_output) if result: interface_name = ''.join(result) if not raw_output.startswith(" PortID:"): raw_output = raw_output.replace( - interface_name, _interface.name_to_alias( - interface_name)) + interface_name, + convert_interface.name_to_alias( + interface_name)) click.echo(raw_output.rstrip('\n')) rc = process.poll() @@ -348,7 +351,7 @@ def arp(ipaddress, iface, verbose): if get_interface_mode() == "alias": if not ((iface.startswith("PortChannel")) or (iface.startswith("eth"))): - iface = _interface.alias_to_name(iface) + iface = convert_interface.alias_to_name(iface) cmd += " -if {}".format(iface) @@ -400,7 +403,7 @@ def alias(interfacename): if interfacename is not None: if get_interface_mode() == "alias": - interfacename = _interface.alias_to_name(interfacename) + interfacename = convert_interface.alias_to_name(interfacename) # If we're given an interface name, output name and alias for that interface only if interfacename in port_dict: @@ -480,7 +483,7 @@ def summary(interfacename, verbose): if interfacename is not None: if get_interface_mode() == "alias": - interfacename = _interface.alias_to_name(interfacename) + interfacename = convert_interface.alias_to_name(interfacename) cmd += " {}".format(interfacename) @@ -548,7 +551,7 @@ def description(interfacename, verbose): if interfacename is not None: if get_interface_mode() == "alias": - interfacename = _interface.alias_to_name(interfacename) + interfacename = convert_interface.alias_to_name(interfacename) cmd += " {}".format(interfacename) @@ -565,7 +568,7 @@ def status(interfacename, verbose): if interfacename is not None: if get_interface_mode() == "alias": - interfacename = _interface.alias_to_name(interfacename) + interfacename = convert_interface.alias_to_name(interfacename) cmd += " {}".format(interfacename) @@ -645,7 +648,7 @@ def counters(interfacename, clear, verbose): if interfacename is not None: if get_interface_mode() == "alias": - interfacename = _interface.alias_to_name(interfacename) + interfacename = convert_interface.alias_to_name(interfacename) if clear: cmd += " -c" @@ -794,7 +797,7 @@ def neighbors(interfacename, verbose): if interfacename is not None: if get_interface_mode() == "alias": - interfacename = _interface.alias_to_name(interfacename) + interfacename = convert_interface.alias_to_name(interfacename) cmd += " {}".format(interfacename) @@ -1168,7 +1171,7 @@ def tablelize(keys, data): r.append(k) r.append(data[k]['vlanid']) if get_interface_mode() == "alias": - alias = _interface.name_to_alias(m) + alias = convert_interface.name_to_alias(m) r.append(alias) else: r.append(m) From 03521a50ed9f221adb84433a76fa670d3a8b6e2d Mon Sep 17 00:00:00 2001 From: paavaanan Date: Sat, 8 Sep 2018 07:03:33 -0400 Subject: [PATCH 09/13] renaming convert_interface to iface_alias_converter --- show/main.py | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/show/main.py b/show/main.py index 4f2e16efa4..5e181b3402 100755 --- a/show/main.py +++ b/show/main.py @@ -184,7 +184,7 @@ def get_interface_mode(): # Global class instance for SONiC interface name to alias conversion -convert_interface = InterfaceAliasConverter() +iface_alias_converter = InterfaceAliasConverter() def print_output_in_alias_mode(output, index): @@ -199,7 +199,8 @@ def print_output_in_alias_mode(output, index): if output.startswith("---"): word = output.split() dword = word[index] - underline = dword.rjust(convert_interface.vendor_name_max_length, '-') + underline = dword.rjust(iface_alias_converter.vendor_name_max_length, + '-') word[index] = underline output = ' ' .join(word) @@ -207,13 +208,13 @@ def print_output_in_alias_mode(output, index): word = output.split() if word: interface_name = word[index] - for port_name in natsorted(convert_interface.port_dict.keys()): + for port_name in natsorted(iface_alias_converter.port_dict.keys()): if interface_name == port_name: - alias_name = convert_interface.port_dict[port_name]['alias'] + alias_name = iface_alias_converter.port_dict[port_name]['alias'] if alias_name: - if len(alias_name) < convert_interface.vendor_name_max_length: + if len(alias_name) < iface_alias_converter.vendor_name_max_length: alias_name = alias_name.rjust( - convert_interface.vendor_name_max_length) + iface_alias_converter.vendor_name_max_length) output = output.replace(interface_name, alias_name, 1) click.echo(output.rstrip('\n')) @@ -241,7 +242,7 @@ def run_command_in_alias_mode(command): index = 0 if output.startswith("IFACE"): output = output.replace("IFACE", "IFACE".rjust( - convert_interface.vendor_name_max_length)) + iface_alias_converter.vendor_name_max_length)) print_output_in_alias_mode(output, index) elif command == "pfcstat": @@ -249,11 +250,11 @@ def run_command_in_alias_mode(command): index = 0 if output.startswith("Port Tx"): output = output.replace("Port Tx", "Port Tx".rjust( - convert_interface.vendor_name_max_length)) + iface_alias_converter.vendor_name_max_length)) elif output.startswith("Port Rx"): output = output.replace("Port Rx", "Port Rx".rjust( - convert_interface.vendor_name_max_length)) + iface_alias_converter.vendor_name_max_length)) print_output_in_alias_mode(output, index) elif (command.startswith("sudo sfputil show")): @@ -263,7 +264,7 @@ def run_command_in_alias_mode(command): index = 0 if output.startswith("Port"): output = output.replace("Port", "Port".rjust( - convert_interface.vendor_name_max_length)) + iface_alias_converter.vendor_name_max_length)) print_output_in_alias_mode(output, index) elif command == "sudo lldpshow": @@ -271,7 +272,7 @@ def run_command_in_alias_mode(command): index = 0 if output.startswith("LocalPort"): output = output.replace("LocalPort", "LocalPort".rjust( - convert_interface.vendor_name_max_length)) + iface_alias_converter.vendor_name_max_length)) print_output_in_alias_mode(output, index) elif command.startswith("queuestat"): @@ -279,7 +280,7 @@ def run_command_in_alias_mode(command): index = 0 if output.startswith("Port"): output = output.replace("Port", "Port".rjust( - convert_interface.vendor_name_max_length)) + iface_alias_converter.vendor_name_max_length)) print_output_in_alias_mode(output, index) elif command == "fdbshow": @@ -301,7 +302,7 @@ def run_command_in_alias_mode(command): else: if index: - for port_name in convert_interface.port_dict.keys(): + for port_name in iface_alias_converter.port_dict.keys(): regex = re.compile(r"\b{}\b".format(port_name)) result = re.findall(regex, raw_output) if result: @@ -309,7 +310,7 @@ def run_command_in_alias_mode(command): if not raw_output.startswith(" PortID:"): raw_output = raw_output.replace( interface_name, - convert_interface.name_to_alias( + iface_alias_converter.name_to_alias( interface_name)) click.echo(raw_output.rstrip('\n')) @@ -351,7 +352,7 @@ def arp(ipaddress, iface, verbose): if get_interface_mode() == "alias": if not ((iface.startswith("PortChannel")) or (iface.startswith("eth"))): - iface = convert_interface.alias_to_name(iface) + iface = iface_alias_converter.alias_to_name(iface) cmd += " -if {}".format(iface) @@ -403,7 +404,7 @@ def alias(interfacename): if interfacename is not None: if get_interface_mode() == "alias": - interfacename = convert_interface.alias_to_name(interfacename) + interfacename = iface_alias_converter.alias_to_name(interfacename) # If we're given an interface name, output name and alias for that interface only if interfacename in port_dict: @@ -483,7 +484,7 @@ def summary(interfacename, verbose): if interfacename is not None: if get_interface_mode() == "alias": - interfacename = convert_interface.alias_to_name(interfacename) + interfacename = iface_alias_converter.alias_to_name(interfacename) cmd += " {}".format(interfacename) @@ -551,7 +552,7 @@ def description(interfacename, verbose): if interfacename is not None: if get_interface_mode() == "alias": - interfacename = convert_interface.alias_to_name(interfacename) + interfacename = iface_alias_converter.alias_to_name(interfacename) cmd += " {}".format(interfacename) @@ -568,7 +569,7 @@ def status(interfacename, verbose): if interfacename is not None: if get_interface_mode() == "alias": - interfacename = convert_interface.alias_to_name(interfacename) + interfacename = iface_alias_converter.alias_to_name(interfacename) cmd += " {}".format(interfacename) @@ -648,7 +649,7 @@ def counters(interfacename, clear, verbose): if interfacename is not None: if get_interface_mode() == "alias": - interfacename = convert_interface.alias_to_name(interfacename) + interfacename = iface_alias_converter.alias_to_name(interfacename) if clear: cmd += " -c" @@ -797,7 +798,7 @@ def neighbors(interfacename, verbose): if interfacename is not None: if get_interface_mode() == "alias": - interfacename = convert_interface.alias_to_name(interfacename) + interfacename = iface_alias_converter.alias_to_name(interfacename) cmd += " {}".format(interfacename) @@ -1171,7 +1172,7 @@ def tablelize(keys, data): r.append(k) r.append(data[k]['vlanid']) if get_interface_mode() == "alias": - alias = convert_interface.name_to_alias(m) + alias = iface_alias_converter.name_to_alias(m) r.append(alias) else: r.append(m) From dee1c31c02276153e8ada30fa6351eb61c3c5aeb Mon Sep 17 00:00:00 2001 From: paavaanan Date: Mon, 10 Sep 2018 01:43:21 -0400 Subject: [PATCH 10/13] renamed variable to alias_max_length --- show/main.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/show/main.py b/show/main.py index 5e181b3402..cf7e80c5b9 100755 --- a/show/main.py +++ b/show/main.py @@ -43,15 +43,15 @@ class InterfaceAliasConverter(object): """Class which handles conversion between interface name and alias""" def __init__(self): - self.vendor_name_max_length = 0 + self.alias_max_length = 0 cmd = 'sonic-cfggen -d --var-json "PORT"' p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) self.port_dict = json.loads(p.stdout.read()) for port_name in self.port_dict.keys(): - if self.vendor_name_max_length < len( + if self.alias_max_length < len( self.port_dict[port_name]['alias']): - self.vendor_name_max_length = len( + self.alias_max_length = len( self.port_dict[port_name]['alias']) def name_to_alias(self, interface_name): @@ -199,7 +199,7 @@ def print_output_in_alias_mode(output, index): if output.startswith("---"): word = output.split() dword = word[index] - underline = dword.rjust(iface_alias_converter.vendor_name_max_length, + underline = dword.rjust(iface_alias_converter.alias_max_length, '-') word[index] = underline output = ' ' .join(word) @@ -212,9 +212,9 @@ def print_output_in_alias_mode(output, index): if interface_name == port_name: alias_name = iface_alias_converter.port_dict[port_name]['alias'] if alias_name: - if len(alias_name) < iface_alias_converter.vendor_name_max_length: + if len(alias_name) < iface_alias_converter.alias_max_length: alias_name = alias_name.rjust( - iface_alias_converter.vendor_name_max_length) + iface_alias_converter.alias_max_length) output = output.replace(interface_name, alias_name, 1) click.echo(output.rstrip('\n')) @@ -242,7 +242,7 @@ def run_command_in_alias_mode(command): index = 0 if output.startswith("IFACE"): output = output.replace("IFACE", "IFACE".rjust( - iface_alias_converter.vendor_name_max_length)) + iface_alias_converter.alias_max_length)) print_output_in_alias_mode(output, index) elif command == "pfcstat": @@ -250,11 +250,11 @@ def run_command_in_alias_mode(command): index = 0 if output.startswith("Port Tx"): output = output.replace("Port Tx", "Port Tx".rjust( - iface_alias_converter.vendor_name_max_length)) + iface_alias_converter.alias_max_length)) elif output.startswith("Port Rx"): output = output.replace("Port Rx", "Port Rx".rjust( - iface_alias_converter.vendor_name_max_length)) + iface_alias_converter.alias_max_length)) print_output_in_alias_mode(output, index) elif (command.startswith("sudo sfputil show")): @@ -264,7 +264,7 @@ def run_command_in_alias_mode(command): index = 0 if output.startswith("Port"): output = output.replace("Port", "Port".rjust( - iface_alias_converter.vendor_name_max_length)) + iface_alias_converter.alias_max_length)) print_output_in_alias_mode(output, index) elif command == "sudo lldpshow": @@ -272,7 +272,7 @@ def run_command_in_alias_mode(command): index = 0 if output.startswith("LocalPort"): output = output.replace("LocalPort", "LocalPort".rjust( - iface_alias_converter.vendor_name_max_length)) + iface_alias_converter.alias_max_length)) print_output_in_alias_mode(output, index) elif command.startswith("queuestat"): @@ -280,7 +280,7 @@ def run_command_in_alias_mode(command): index = 0 if output.startswith("Port"): output = output.replace("Port", "Port".rjust( - iface_alias_converter.vendor_name_max_length)) + iface_alias_converter.alias_max_length)) print_output_in_alias_mode(output, index) elif command == "fdbshow": From 8a33b821e86fd8375258161eb88af45930d809f6 Mon Sep 17 00:00:00 2001 From: paavaanan Date: Tue, 11 Sep 2018 01:33:06 -0400 Subject: [PATCH 11/13] Rephrased comments --- show/main.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/show/main.py b/show/main.py index cf7e80c5b9..e6279a4e36 100755 --- a/show/main.py +++ b/show/main.py @@ -55,8 +55,8 @@ def __init__(self): self.port_dict[port_name]['alias']) def name_to_alias(self, interface_name): - """Return alias interface name if default - name is given as argument + """Return vendor interface alias if SONiC + interface name is given as argument """ if interface_name is not None: for port_name in self.port_dict.keys(): @@ -67,7 +67,8 @@ def name_to_alias(self, interface_name): raise click.Abort() def alias_to_name(self, interface_alias): - """Return default interface name if alias name is given as argument + """Return SONiC interface name if vendor + port alias is given as argument """ if interface_alias is not None: for port_name in self.port_dict.keys(): @@ -402,7 +403,6 @@ def alias(interfacename): body = [] if interfacename is not None: - if get_interface_mode() == "alias": interfacename = iface_alias_converter.alias_to_name(interfacename) From 2080fe88c72498ff08fae31cd4bcb24c8f17d3e4 Mon Sep 17 00:00:00 2001 From: paavaanan Date: Sat, 15 Sep 2018 07:03:46 -0400 Subject: [PATCH 12/13] added naming_mode command support and transeiver --- show/main.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/show/main.py b/show/main.py index e6279a4e36..01ade5bc2a 100755 --- a/show/main.py +++ b/show/main.py @@ -209,6 +209,7 @@ def print_output_in_alias_mode(output, index): word = output.split() if word: interface_name = word[index] + interface_name = interface_name.replace(':', '') for port_name in natsorted(iface_alias_converter.port_dict.keys()): if interface_name == port_name: alias_name = iface_alias_converter.port_dict[port_name]['alias'] @@ -258,9 +259,14 @@ def run_command_in_alias_mode(command): iface_alias_converter.alias_max_length)) print_output_in_alias_mode(output, index) + elif command == "sudo sfputil show eeprom": + """show interface transceiver eeprom""" + index = 0 + print_output_in_alias_mode(raw_output, index) + elif (command.startswith("sudo sfputil show")): """show interface transceiver lpmode, - presence, eeprom + presence """ index = 0 if output.startswith("Port"): @@ -628,6 +634,15 @@ def counters(clear, verbose): run_command(cmd, display_cmd=verbose) +# 'naming_mode' subcommand ("show interfaces naming_mode") +@interfaces.command() +@click.option('--verbose', is_flag=True, help="Enable verbose output") +def naming_mode(verbose): + """Show interface naming_mode status""" + + click.echo(get_interface_mode()) + + # # 'queue' group ("show queue ...") # @@ -968,7 +983,6 @@ def cpu(verbose): cmd = "top -bn 1 -o %CPU" run_command(cmd, display_cmd=verbose) - # 'memory' subcommand @processes.command() @click.option('--verbose', is_flag=True, help="Enable verbose output") From b733b55302701763e96d64201637e176aed47df9 Mon Sep 17 00:00:00 2001 From: paavaanan Date: Thu, 20 Sep 2018 08:04:00 -0400 Subject: [PATCH 13/13] show interface transceiver eeprom/lmode/presence support alias mode --- show/main.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/show/main.py b/show/main.py index 01ade5bc2a..11a87a88ca 100755 --- a/show/main.py +++ b/show/main.py @@ -259,7 +259,7 @@ def run_command_in_alias_mode(command): iface_alias_converter.alias_max_length)) print_output_in_alias_mode(output, index) - elif command == "sudo sfputil show eeprom": + elif (command.startswith("sudo sfputil show eeprom")): """show interface transceiver eeprom""" index = 0 print_output_in_alias_mode(raw_output, index) @@ -516,6 +516,9 @@ def eeprom(interfacename, dump_dom, verbose): cmd += " --dom" if interfacename is not None: + if get_interface_mode() == "alias": + interfacename = iface_alias_converter.alias_to_name(interfacename) + cmd += " -p {}".format(interfacename) run_command(cmd, display_cmd=verbose) @@ -530,6 +533,9 @@ def lpmode(interfacename, verbose): cmd = "sudo sfputil show lpmode" if interfacename is not None: + if get_interface_mode() == "alias": + interfacename = iface_alias_converter.alias_to_name(interfacename) + cmd += " -p {}".format(interfacename) run_command(cmd, display_cmd=verbose) @@ -543,6 +549,9 @@ def presence(interfacename, verbose): cmd = "sudo sfputil show presence" if interfacename is not None: + if get_interface_mode() == "alias": + interfacename = iface_alias_converter.alias_to_name(interfacename) + cmd += " -p {}".format(interfacename) run_command(cmd, display_cmd=verbose)