Skip to content

Commit 7497c3f

Browse files
committed
sonic-utilities: Create SONiC ISIS show commands
* Update show/main.py Include "isis" as a cli group option for show commands * Create show/isis_frr.py Add "isis" as a cli group option for show commands and add support for "show isis neighbors", "show isis database", "show isis hostname", and "show isis interface" Signed-off-by: [email protected]
1 parent 784a15c commit 7497c3f

File tree

6 files changed

+843
-0
lines changed

6 files changed

+843
-0
lines changed

show/isis_frr.py

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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'))

show/main.py

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
from . import flow_counters
4949
from . import gearbox
5050
from . import interfaces
51+
from . import isis_frr
5152
from . import kdump
5253
from . import kube
5354
from . import muxcable
@@ -276,6 +277,7 @@ def cli(ctx):
276277
cli.add_command(flow_counters.flowcnt_trap)
277278
cli.add_command(kdump.kdump)
278279
cli.add_command(interfaces.interfaces)
280+
cli.add_command(isis_frr.isis)
279281
cli.add_command(kdump.kdump)
280282
cli.add_command(kube.kubernetes)
281283
cli.add_command(muxcable.muxcable)

tests/conftest.py

+38
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
mock_show_bgp_network_single_asic,
1919
mock_show_bgp_network_multi_asic
2020
)
21+
from .isis_frr_input.isis_frr_test_vector import(
22+
mock_show_isis_neighbors,
23+
mock_show_isis_database,
24+
mock_show_isis_hostname,
25+
mock_show_isis_interface
26+
)
2127
from . import config_int_ip_common
2228
import utilities_common.constants as constants
2329
import config.main as config
@@ -367,3 +373,35 @@ def mock_restart_dhcp_relay_service():
367373
yield
368374

369375
config.vlan.dhcp_relay_util.handle_restart_dhcp_relay_service = origin_func
376+
377+
@pytest.fixture
378+
def setup_single_isis_instance(request):
379+
import utilities_common.bgp_util as bgp_util
380+
381+
_old_run_bgp_command = bgp_util.run_bgp_command
382+
383+
if request.param.startswith('isis_neighbor') or \
384+
request.param.startswith('isis_neighbor'):
385+
bgp_util.run_bgp_command = mock.MagicMock(
386+
return_value=mock_show_isis_neighbors(request))
387+
elif request.param.startswith('isis_database') or \
388+
request.param.startswith('isis_database'):
389+
bgp_util.run_bgp_command = mock.MagicMock(
390+
return_value=mock_show_isis_database(request))
391+
elif request.param.startswith('isis_hostname') or \
392+
request.param.startswith('isis_hostname'):
393+
bgp_util.run_bgp_command = mock.MagicMock(
394+
return_value=mock_show_isis_hostname(request))
395+
elif request.param.startswith('isis_interface') or \
396+
request.param.startswith('isis_interface'):
397+
bgp_util.run_bgp_command = mock.MagicMock(
398+
return_value=mock_show_isis_interface(request))
399+
yield
400+
401+
bgp_util.run_bgp_command = _old_run_bgp_command
402+
403+
404+
@pytest.fixture
405+
def setup_isis_commands():
406+
import show.main as show
407+
return show

tests/isis_frr_input/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)