Skip to content
New issue

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

[show] Add subcommand to show midplane status for modular chassis #1267

Merged
merged 4 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
47 changes: 44 additions & 3 deletions show/chassis_modules.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import click
from natsort import natsorted
from tabulate import tabulate
from swsssdk import SonicV2Connector
from swsscommon.swsscommon import SonicV2Connector

import utilities_common.cli as clicommon

Expand All @@ -12,6 +12,10 @@
CHASSIS_MODULE_INFO_OPERSTATUS_FIELD = 'oper_status'
CHASSIS_MODULE_INFO_ADMINSTATUS_FIELD = 'admin_status'

CHASSIS_MIDPLANE_INFO_TABLE = 'CHASSIS_MIDPLANE_TABLE'
CHASSIS_MIDPLANE_INFO_IP_FIELD = 'ip_address'
CHASSIS_MIDPLANE_INFO_ACCESS_FIELD = 'access'

@click.group(cls=clicommon.AliasedGroup)
def chassis_modules():
"""Show chassis-modules information"""
Expand All @@ -31,7 +35,7 @@ def status(db, chassis_module_name):

key_pattern = '*'
if chassis_module_name:
key_pattern = '|'+chassis_module_name
key_pattern = '|' + chassis_module_name

keys = state_db.keys(state_db.STATE_DB, CHASSIS_MODULE_INFO_TABLE + key_pattern)
if not keys:
Expand All @@ -41,7 +45,7 @@ def status(db, chassis_module_name):
table = []
for key in natsorted(keys):
key_list = key.split('|')
if len(key_list) != 2: # error data in DB, log it and ignore
if len(key_list) != 2: # error data in DB, log it and ignore
print('Warn: Invalid Key {} in {} table'.format(key, CHASSIS_MODULE_INFO_TABLE))
continue

Expand All @@ -61,3 +65,40 @@ def status(db, chassis_module_name):
click.echo(tabulate(table, header, tablefmt='simple', stralign='right'))
else:
click.echo('No data available in CHASSIS_MODULE_TABLE\n')

@chassis_modules.command()
@click.argument('chassis_module_name', metavar='<module_name>', required=False)
def midplane_status(chassis_module_name):
"""Show chassis-modules midplane-status"""

header = ['Name', 'IP-Address', 'Reachability']

state_db = SonicV2Connector(host="127.0.0.1")
state_db.connect(state_db.STATE_DB)

key_pattern = '*'
if chassis_module_name:
key_pattern = '|' + chassis_module_name

keys = state_db.keys(state_db.STATE_DB, CHASSIS_MIDPLANE_INFO_TABLE + key_pattern)
if not keys:
print('Key {} not found in {} table'.format(key_pattern, CHASSIS_MIDPLANE_INFO_TABLE))
return

table = []
for key in natsorted(keys):
key_list = key.split('|')
if len(key_list) != 2: # error data in DB, log it and ignore
print('Warn: Invalid Key {} in {} table'.format(key, CHASSIS_MIDPLANE_INFO_TABLE))
continue

data_dict = state_db.get_all(state_db.STATE_DB, key)
ip = data_dict[CHASSIS_MIDPLANE_INFO_IP_FIELD]
access = data_dict[CHASSIS_MIDPLANE_INFO_ACCESS_FIELD]

table.append((key_list[1], ip, access))

if table:
click.echo(tabulate(table, header, tablefmt='simple', stralign='right'))
else:
click.echo('No data available in CHASSIS_MIDPLANE_TABLE\n')
37 changes: 37 additions & 0 deletions tests/chassis_modules_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,43 @@ def test_config_incorrect_module(self):
print(result.output)
assert result.exit_code != 0

def test_midplane_show_all_count_lines(self):
runner = CliRunner()
result = runner.invoke(show.cli.commands["chassis-modules"].commands["midplane-status"], [])
print(result.output)
mprabhu-nokia marked this conversation as resolved.
Show resolved Hide resolved
result_lines = result.output.strip('\n').split('\n')
modules = ["LINE-CARD0", "LINE-CARD1", "SUPERVISOR0"]
for i, module in enumerate(modules):
assert module in result_lines[i + warning_lines + header_lines]
assert len(result_lines) == warning_lines + header_lines + len(modules)

def test_midplane_show_single_count_lines(self):
runner = CliRunner()
result = runner.invoke(show.cli.commands["chassis-modules"].commands["midplane-status"], ["LINE-CARD0"])
print(result.output)
result_lines = result.output.strip('\n').split('\n')
modules = ["LINE-CARD0"]
for i, module in enumerate(modules):
assert module in result_lines[i+header_lines]
assert len(result_lines) == header_lines + len(modules)

def test_midplane_show_module_down(self):
runner = CliRunner()
result = runner.invoke(show.cli.commands["chassis-modules"].commands["midplane-status"], ["LINE-CARD1"])
print(result.output)
result_lines = result.output.strip('\n').split('\n')
assert result.exit_code == 0
result_out = (result_lines[header_lines]).split()
print(result_out)
assert result_out[2] == 'False'

def test_midplane_show_incorrect_module(self):
runner = CliRunner()
result = runner.invoke(show.cli.commands["chassis-modules"].commands["midplane-status"], ["TEST-CARD1"])
print(result.output)
print(result.exit_code)
assert result.exit_code == 0

@classmethod
def teardown_class(cls):
print("TEARDOWN")
Expand Down
12 changes: 12 additions & 0 deletions tests/mock_tables/state_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -377,5 +377,17 @@
"xon": "18432",
"xoff": "32768",
"size": "51200"
},
"CHASSIS_MIDPLANE_TABLE|SUPERVISOR0": {
"ip_address": "192.168.1.100",
"access": "True"
},
"CHASSIS_MIDPLANE_TABLE|LINE-CARD0": {
"ip_address": "192.168.1.1",
"access": "True"
},
"CHASSIS_MIDPLANE_TABLE|LINE-CARD1": {
"ip_address": "192.168.1.2",
"access": "False"
}
}