Skip to content

Commit 79ccd03

Browse files
authored
[show] cli support for show muxcable cableinfo (sonic-net#1448)
Summary: This PR provides the support for adding CLI commands for retrieving cable vendor name and part number information of the muxcable. In particular these Cli commands are supported: show muxcable cableinfo <port> Approach added the changes in sonic-utilities/show and sonic-utilities/config by changing the muxcable.py What is the motivation for this PR? To add the support for Cli for muxcable to be utilized for retrieving cable vendor name and part number information of the muxcable. Signed-off-by: vaibhav-dahiya <[email protected]>
1 parent 0430083 commit 79ccd03

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

show/muxcable.py

+31
Original file line numberDiff line numberDiff line change
@@ -381,3 +381,34 @@ def eyeinfo(port, target):
381381
lane_data.append(res)
382382
click.echo(tabulate(lane_data, headers=headers))
383383
sys.exit(EXIT_SUCCESS)
384+
385+
386+
@muxcable.command()
387+
@click.argument('port', required=True, default=None)
388+
def cableinfo(port):
389+
"""Show muxcable cable information"""
390+
391+
if platform_sfputil is not None:
392+
physical_port_list = platform_sfputil_helper.logical_port_name_to_physical_port_list(port)
393+
394+
if not isinstance(physical_port_list, list):
395+
click.echo("ERR: Unable to get a port on muxcable port")
396+
sys.exit(EXIT_FAIL)
397+
if len(physical_port_list) != 1:
398+
click.echo("ERR: Unable to get a single port on muxcable")
399+
sys.exit(EXIT_FAIL)
400+
401+
physical_port = physical_port_list[0]
402+
import sonic_y_cable.y_cable
403+
part_num = sonic_y_cable.y_cable.get_part_number(physical_port)
404+
if part_num == False or part_num == -1:
405+
click.echo("ERR: Unable to get cable info part number")
406+
sys.exit(EXIT_FAIL)
407+
vendor = sonic_y_cable.y_cable.get_vendor(physical_port)
408+
if vendor == False or vendor == -1:
409+
click.echo("ERR: Unable to get cable info vendor name")
410+
sys.exit(EXIT_FAIL)
411+
headers = ['Vendor', 'Model']
412+
413+
body = [[vendor, part_num]]
414+
click.echo(tabulate(body, headers=headers))

tests/muxcable_test.py

+59
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
sys.modules['sonic_y_cable'] = mock.Mock()
1414
sys.modules['y_cable'] = mock.Mock()
1515
sys.modules['sonic_y_cable.y_cable'] = mock.Mock()
16+
sys.modules['platform_sfputil'] = mock.Mock()
17+
sys.modules['platform_sfputil_helper'] = mock.Mock()
18+
sys.modules['utilities_common.platform_sfputil_helper'] = mock.Mock()
1619
#sys.modules['os'] = mock.Mock()
1720
#sys.modules['os.geteuid'] = mock.Mock()
1821
#sys.modules['platform_sfputil'] = mock.Mock()
@@ -153,6 +156,12 @@
153156
}
154157
"""
155158

159+
expected_muxcable_cableinfo_output = """\
160+
Vendor Model
161+
-------- ---------------
162+
Credo CACL1X321P2PA1M
163+
"""
164+
156165

157166
class TestMuxcable(object):
158167
@classmethod
@@ -487,6 +496,56 @@ def test_config_muxcable_disable_loopback(self):
487496

488497
assert result.exit_code == 100
489498

499+
@mock.patch('sonic_y_cable.y_cable.get_part_number', mock.MagicMock(return_value=("CACL1X321P2PA1M")))
500+
@mock.patch('sonic_y_cable.y_cable.get_vendor', mock.MagicMock(return_value=("Credo ")))
501+
@mock.patch('show.muxcable.platform_sfputil', mock.MagicMock(return_value=1))
502+
@mock.patch('utilities_common.platform_sfputil_helper.logical_port_name_to_physical_port_list', mock.MagicMock(return_value=[0]))
503+
def test_show_muxcable_cableinfo(self):
504+
runner = CliRunner()
505+
db = Db()
506+
507+
result = runner.invoke(show.cli.commands["muxcable"].commands["cableinfo"],
508+
["Ethernet0"], obj=db)
509+
510+
assert result.exit_code == 0
511+
assert result.output == expected_muxcable_cableinfo_output
512+
513+
@mock.patch('sonic_y_cable.y_cable.get_part_number', mock.MagicMock(return_value=(False)))
514+
@mock.patch('sonic_y_cable.y_cable.get_vendor', mock.MagicMock(return_value=(False)))
515+
@mock.patch('show.muxcable.platform_sfputil', mock.MagicMock(return_value=1))
516+
@mock.patch('utilities_common.platform_sfputil_helper.logical_port_name_to_physical_port_list', mock.MagicMock(return_value=[0]))
517+
def test_show_muxcable_cableinfo_incorrect_port(self):
518+
runner = CliRunner()
519+
db = Db()
520+
521+
result = runner.invoke(show.cli.commands["muxcable"].commands["cableinfo"],
522+
["Ethernet0"], obj=db)
523+
assert result.exit_code == 1
524+
525+
@mock.patch('sonic_y_cable.y_cable.get_part_number', mock.MagicMock(return_value=(False)))
526+
@mock.patch('sonic_y_cable.y_cable.get_vendor', mock.MagicMock(return_value=(False)))
527+
@mock.patch('show.muxcable.platform_sfputil', mock.MagicMock(return_value=1))
528+
@mock.patch('utilities_common.platform_sfputil_helper.logical_port_name_to_physical_port_list', mock.MagicMock(return_value=0))
529+
def test_show_muxcable_cableinfo_incorrect_port_return_value(self):
530+
runner = CliRunner()
531+
db = Db()
532+
533+
result = runner.invoke(show.cli.commands["muxcable"].commands["cableinfo"],
534+
["Ethernet0"], obj=db)
535+
assert result.exit_code == 1
536+
537+
@mock.patch('sonic_y_cable.y_cable.get_part_number', mock.MagicMock(return_value=(False)))
538+
@mock.patch('sonic_y_cable.y_cable.get_vendor', mock.MagicMock(return_value=(False)))
539+
@mock.patch('show.muxcable.platform_sfputil', mock.MagicMock(return_value=1))
540+
@mock.patch('utilities_common.platform_sfputil_helper.logical_port_name_to_physical_port_list', mock.MagicMock(return_value=[0, 1]))
541+
def test_show_muxcable_cableinfo_incorrect_logical_port_return_value(self):
542+
runner = CliRunner()
543+
db = Db()
544+
545+
result = runner.invoke(show.cli.commands["muxcable"].commands["cableinfo"],
546+
["Ethernet0"], obj=db)
547+
assert result.exit_code == 1
548+
490549
@classmethod
491550
def teardown_class(cls):
492551
os.environ['UTILITIES_UNIT_TESTING'] = "0"

utilities_common/platform_sfputil_helper.py

+10
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,13 @@ def platform_sfputil_read_porttab_mappings():
3737
sys.exit(1)
3838

3939
return 0
40+
41+
def logical_port_name_to_physical_port_list(port_name):
42+
if port_name.startswith("Ethernet"):
43+
if platform_sfputil.is_logical_port(port_name):
44+
return platform_sfputil.get_logical_to_physical(port_name)
45+
else:
46+
click.echo("Invalid port '{}'".format(port_name))
47+
return None
48+
else:
49+
return [int(port_name)]

0 commit comments

Comments
 (0)