|
| 1 | +import click |
| 2 | + |
| 3 | +import utilities_common.bgp_util as bgp_util |
| 4 | +import utilities_common.cli as clicommon |
| 5 | +from natsort import natsorted |
| 6 | +from swsscommon.swsscommon import ConfigDBConnector |
| 7 | + |
| 8 | + |
| 9 | +############################################################################### |
| 10 | +# |
| 11 | +# 'show isis' cli stanza |
| 12 | +# |
| 13 | +############################################################################### |
| 14 | + |
| 15 | +def get_interfaces(): |
| 16 | + """Get list of interfaces |
| 17 | + """ |
| 18 | + tables = ['LOOPBACK_INTERFACE', 'PORT', 'PORTCHANNEL'] |
| 19 | + data = [] |
| 20 | + |
| 21 | + config_db = ConfigDBConnector() |
| 22 | + config_db.connect() |
| 23 | + |
| 24 | + for table_name in tables: |
| 25 | + interface_list = list(config_db.get_table(table_name).keys()) |
| 26 | + interface_list = [x for x in interface_list if isinstance(x, str)] |
| 27 | + data += natsorted(interface_list) |
| 28 | + return data |
| 29 | + |
| 30 | +INTERFACE_LIST = get_interfaces() |
| 31 | + |
| 32 | +def invalid_arg(input:str): |
| 33 | + if input == '?': |
| 34 | + ctx = click.get_current_context() |
| 35 | + ctx.fail('The argument: "{}" is invalid. Try "-?".'.format(input)) |
| 36 | + |
| 37 | +@click.group(cls=clicommon.AliasedGroup, name="isis") |
| 38 | +def isis(): |
| 39 | + """Show ISIS (Intermediate System to Intermediate System) information""" |
| 40 | + pass |
| 41 | + |
| 42 | + |
| 43 | +# 'neighbors' subcommand ("show isis neighbors") |
| 44 | +@isis.command() |
| 45 | +@click.argument('system_id', required=False) |
| 46 | +@click.option('--verbose', is_flag=True, help="Enable verbose output") |
| 47 | +def neighbors(system_id, verbose): |
| 48 | + """Show ISIS neighbors""" |
| 49 | + |
| 50 | + command = 'show isis neighbor' |
| 51 | + if system_id is not None: |
| 52 | + invalid_arg(system_id) |
| 53 | + command += ' {}'.format(system_id) |
| 54 | + elif verbose: |
| 55 | + command += ' detail' |
| 56 | + |
| 57 | + output = "" |
| 58 | + output += bgp_util.run_bgp_show_command(command) |
| 59 | + |
| 60 | + click.echo(output.rstrip('\n')) |
| 61 | + |
| 62 | +# 'database' subcommand ("show isis database") |
| 63 | +@isis.command() |
| 64 | +@click.argument('lsp_id', required=False) |
| 65 | +@click.option('--verbose', is_flag=True, help="Enable verbose output") |
| 66 | +def database(lsp_id, verbose): |
| 67 | + """Show ISIS database""" |
| 68 | + |
| 69 | + command = 'show isis database' |
| 70 | + if verbose: |
| 71 | + command += ' detail' |
| 72 | + if lsp_id is not None: |
| 73 | + invalid_arg(lsp_id) |
| 74 | + command += ' {0}'.format(lsp_id) |
| 75 | + |
| 76 | + output = "" |
| 77 | + output += bgp_util.run_bgp_show_command(command) |
| 78 | + |
| 79 | + click.echo(output.rstrip('\n')) |
| 80 | + |
| 81 | +# 'hostname' subcommand ("show isis hostname") |
| 82 | +@isis.command() |
| 83 | +def hostname(): |
| 84 | + """Show ISIS hostname""" |
| 85 | + |
| 86 | + command = 'show isis hostname' |
| 87 | + |
| 88 | + output = "" |
| 89 | + output += bgp_util.run_bgp_show_command(command) |
| 90 | + |
| 91 | + click.echo(output.rstrip('\n')) |
| 92 | + |
| 93 | +# 'interface' subcommand ("show isis interface") |
| 94 | +@isis.command() |
| 95 | +@click.argument('interface', |
| 96 | + metavar='[INTERFACE]', |
| 97 | + type=click.Choice( |
| 98 | + INTERFACE_LIST), |
| 99 | + required=False) |
| 100 | +@click.option('--verbose', is_flag=True, help="Enable verbose output") |
| 101 | +@click.option('--display', is_flag=True, help=f"Display available [INTERFACE] options:\n{INTERFACE_LIST}") |
| 102 | +def interface(interface, verbose, display): |
| 103 | + """Show ISIS interface""" |
| 104 | + |
| 105 | + if display: |
| 106 | + d = f"[INTERFACE] options: {INTERFACE_LIST}" |
| 107 | + click.echo(d) |
| 108 | + |
| 109 | + command = 'show isis interface' |
| 110 | + |
| 111 | + if interface is not None: |
| 112 | + command += ' {0}'.format(interface) |
| 113 | + elif verbose: |
| 114 | + command += ' detail' |
| 115 | + |
| 116 | + output = "" |
| 117 | + output += bgp_util.run_bgp_show_command(command) |
| 118 | + |
| 119 | + click.echo(output.rstrip('\n')) |
0 commit comments