Skip to content

Commit f0a3e5b

Browse files
stephenxsstephens
authored andcommitted
Improve the way to check port type of RJ45 port (sonic-net#2249)
* Update the presence state of RJ45 port Present/Not present => Link Up/Link Down Use the new platform API to test whether the port is an RJ45 port Signed-off-by: Stephen Sun <[email protected]> * Use new platform API to check whether a port is RJ45 and represent present status accordingly Signed-off-by: Stephen Sun <[email protected]> * Adjust sfputil and testcases Signed-off-by: Stephen Sun <[email protected]> * Adjust sfpshow Signed-off-by: Stephen Sun <[email protected]> * Exact is_rj45_port to a common module shared between sfpshow and intfutil Signed-off-by: Stephen Sun <[email protected]> * Fall back to old way for checking RJ45 port Signed-off-by: Stephen Sun <[email protected]> * Move RJ45 part to platform_sfputil_helper Signed-off-by: Stephen Sun <[email protected]> * Remove fallback mechanism in is_rj45_port Signed-off-by: Stephen Sun <[email protected]> * Remove get_child_ports which is not used Signed-off-by: Stephen Sun <[email protected]> * Temporarily commit Signed-off-by: Stephen Sun <[email protected]> * Update unit test Signed-off-by: stephens <[email protected]> * Adjust unit test Signed-off-by: Stephen Sun <[email protected]> * Commit missed files Signed-off-by: Stephen Sun <[email protected]> * Add missing files Signed-off-by: stephens <[email protected]> * Fix typo Signed-off-by: Stephen Sun <[email protected]> * Remove code that was committed by mistake. Signed-off-by: Stephen Sun <[email protected]> * Fix an issue: the ports should be in nature order in sfputil show presence Signed-off-by: Stephen Sun <[email protected]> * Fix present state for RJ45: Link Up/Down => Port Up/Down Signed-off-by: Stephen Sun <[email protected]> * LGTM warning supression Signed-off-by: Stephen Sun <[email protected]> * LGTM warning supression Signed-off-by: Stephen Sun <[email protected]> * Move present state part into another PR Signed-off-by: Stephen Sun <[email protected]> * Fix review comments Signed-off-by: Stephen Sun <[email protected]> Co-authored-by: stephens <[email protected]> Conflicts: scripts/intfutil scripts/sfpshow sfputil/main.py tests/mock_platform_sfputil/mock_platform_sfputil.py tests/sfputil_test.py
1 parent a3e8f5e commit f0a3e5b

File tree

6 files changed

+51
-52
lines changed

6 files changed

+51
-52
lines changed

scripts/intfutil

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ try:
1414
sys.path.insert(0, tests_path)
1515
import mock_tables.dbconnector
1616
from mock_platform_sfputil.mock_platform_sfputil import mock_platform_sfputil_helper
17-
import utilities_common.platform_sfputil_helper as platform_sfputil_helper
18-
mock_platform_sfputil_helper(platform_sfputil_helper)
17+
mock_platform_sfputil_helper()
1918
if os.environ["UTILITIES_UNIT_TESTING_TOPOLOGY"] == "multi_asic":
2019
import mock_tables.mock_multi_asic
2120
mock_tables.dbconnector.load_namespace_config()

scripts/sfpshow

+3-11
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ try:
2727
sys.path.insert(0, test_path)
2828
import mock_tables.dbconnector
2929
from mock_platform_sfputil.mock_platform_sfputil import mock_platform_sfputil_helper
30-
import utilities_common.platform_sfputil_helper as platform_sfputil_helper
31-
mock_platform_sfputil_helper(platform_sfputil_helper)
30+
mock_platform_sfputil_helper()
3231
if os.environ["UTILITIES_UNIT_TESTING_TOPOLOGY"] == "multi_asic":
3332
import mock_tables.mock_multi_asic
3433
mock_tables.dbconnector.load_namespace_config()
@@ -433,16 +432,9 @@ class SFPShow(object):
433432
def convert_interface_sfp_presence_state_to_cli_output_string(self, state_db, interface_name):
434433
sfp_info_dict = state_db.get_all(self.db.STATE_DB, 'TRANSCEIVER_INFO|{}'.format(interface_name))
435434
if sfp_info_dict:
436-
if sfp_info_dict['type'] == RJ45_PORT_TYPE:
437-
output = 'Link Up'
438-
else:
439-
output = 'Present'
435+
output = 'Present'
440436
else:
441-
# We have to check whether it is an RJ45 port via calling platform API in case it is absent
442-
if is_rj45_port(interface_name):
443-
output = 'Link Down'
444-
else:
445-
output = 'Not present'
437+
output = 'Not present'
446438
return output
447439

448440

sfputil/main.py

+25-20
Original file line numberDiff line numberDiff line change
@@ -292,22 +292,16 @@ def is_sfp_present(port_name):
292292
return bool(presence)
293293

294294

295-
def is_rj45_port_from_api(port_name):
295+
def is_port_type_rj45(port_name):
296296
physical_port = logical_port_to_physical_port_index(port_name)
297297

298298
try:
299299
port_types = platform_chassis.get_port_or_cage_type(physical_port)
300300
return SfpBase.SFP_PORT_TYPE_BIT_RJ45 == port_types
301301
except NotImplementedError:
302-
port_types = None
302+
pass
303303

304304
return False
305-
306-
307-
def skip_if_port_is_rj45(port_name):
308-
if is_rj45_port_from_api(port_name):
309-
click.echo("This functionality is not applicable for RJ45 port {}.".format(port_name))
310-
sys.exit(EXIT_FAIL)
311305
# ========================== Methods for formatting output ==========================
312306

313307
# Convert dict values to cli output string
@@ -649,7 +643,7 @@ def eeprom(port, dump_dom, namespace):
649643
for physical_port in physical_port_list:
650644
port_name = get_physical_port_name(logical_port_name, i, ganged)
651645

652-
if is_rj45_port_from_api(port_name):
646+
if is_port_type_rj45(port_name):
653647
output += "{}: SFP EEPROM is not applicable for RJ45 port\n".format(port_name)
654648
output += '\n'
655649
continue
@@ -715,6 +709,7 @@ def presence(port):
715709

716710
logical_port_list = [port]
717711

712+
logical_port_list = natsort.natsorted(logical_port_list)
718713
for logical_port_name in logical_port_list:
719714
ganged = False
720715
i = 1
@@ -810,7 +805,7 @@ def fetch_error_status_from_platform_api(port):
810805
physical_port_list = logical_port_name_to_physical_port_list(logical_port_name)
811806
port_name = get_physical_port_name(logical_port_name, 1, False)
812807

813-
if is_rj45_port_from_api(logical_port_name):
808+
if is_port_type_rj45(logical_port_name):
814809
output.append([port_name, "N/A"])
815810
else:
816811
output.append([port_name, output_dict.get(physical_port_list[0])])
@@ -836,7 +831,7 @@ def fetch_error_status_from_state_db(port, state_db):
836831
sorted_ports = natsort.natsorted(status)
837832
output = []
838833
for port in sorted_ports:
839-
if is_rj45_port_from_api(port):
834+
if is_port_type_rj45(port):
840835
description = "N/A"
841836
else:
842837
statestring = status[port].get('status')
@@ -912,7 +907,7 @@ def lpmode(port):
912907
click.echo("Error: No physical ports found for logical port '{}'".format(logical_port_name))
913908
return
914909

915-
if is_rj45_port_from_api(logical_port_name):
910+
if is_port_type_rj45(logical_port_name):
916911
output_table.append([logical_port_name, "N/A"])
917912
else:
918913
if len(physical_port_list) > 1:
@@ -955,7 +950,7 @@ def fwversion(port_name):
955950
physical_port = logical_port_to_physical_port_index(port_name)
956951
sfp = platform_chassis.get_sfp(physical_port)
957952

958-
if is_rj45_port_from_api(port_name):
953+
if is_port_type_rj45(port_name):
959954
click.echo("Show firmware version is not applicable for RJ45 port {}.".format(port_name))
960955
sys.exit(EXIT_FAIL)
961956

@@ -994,7 +989,7 @@ def set_lpmode(logical_port, enable):
994989
click.echo("Error: No physical ports found for logical port '{}'".format(logical_port))
995990
return
996991

997-
if is_rj45_port_from_api(logical_port):
992+
if is_port_type_rj45(logical_port):
998993
click.echo("{} low-power mode is not applicable for RJ45 port {}.".format("Enabling" if enable else "Disabling", logical_port))
999994
sys.exit(EXIT_FAIL)
1000995

@@ -1054,7 +1049,7 @@ def reset(port_name):
10541049
click.echo("Error: No physical ports found for logical port '{}'".format(port_name))
10551050
return
10561051

1057-
if is_rj45_port_from_api(port_name):
1052+
if is_port_type_rj45(port_name):
10581053
click.echo("Reset is not applicable for RJ45 port {}.".format(port_name))
10591054
sys.exit(EXIT_FAIL)
10601055

@@ -1219,7 +1214,9 @@ def download_firmware(port_name, filepath):
12191214
def run(port_name, mode):
12201215
"""Run the firmware with default mode=1"""
12211216

1222-
skip_if_port_is_rj45(port_name)
1217+
if is_port_type_rj45(port_name):
1218+
click.echo("This functionality is not applicable for RJ45 port {}.".format(port_name))
1219+
sys.exit(EXIT_FAIL)
12231220

12241221
if not is_sfp_present(port_name):
12251222
click.echo("{}: SFP EEPROM not detected\n".format(port_name))
@@ -1238,7 +1235,9 @@ def run(port_name, mode):
12381235
def commit(port_name):
12391236
"""Commit the running firmware"""
12401237

1241-
skip_if_port_is_rj45(port_name)
1238+
if is_port_type_rj45(port_name):
1239+
click.echo("This functionality is not applicable for RJ45 port {}.".format(port_name))
1240+
sys.exit(EXIT_FAIL)
12421241

12431242
if not is_sfp_present(port_name):
12441243
click.echo("{}: SFP EEPROM not detected\n".format(port_name))
@@ -1260,7 +1259,9 @@ def upgrade(port_name, filepath):
12601259

12611260
physical_port = logical_port_to_physical_port_index(port_name)
12621261

1263-
skip_if_port_is_rj45(port_name)
1262+
if is_port_type_rj45(port_name):
1263+
click.echo("This functionality is not applicable for RJ45 port {}.".format(port_name))
1264+
sys.exit(EXIT_FAIL)
12641265

12651266
if not is_sfp_present(port_name):
12661267
click.echo("{}: SFP EEPROM not detected\n".format(port_name))
@@ -1296,7 +1297,9 @@ def upgrade(port_name, filepath):
12961297
def download(port_name, filepath):
12971298
"""Download firmware on the transceiver"""
12981299

1299-
skip_if_port_is_rj45(port_name)
1300+
if is_port_type_rj45(port_name):
1301+
click.echo("This functionality is not applicable for RJ45 port {}.".format(port_name))
1302+
sys.exit(EXIT_FAIL)
13001303

13011304
if not is_sfp_present(port_name):
13021305
click.echo("{}: SFP EEPROM not detected\n".format(port_name))
@@ -1322,7 +1325,9 @@ def unlock(port_name, password):
13221325
physical_port = logical_port_to_physical_port_index(port_name)
13231326
sfp = platform_chassis.get_sfp(physical_port)
13241327

1325-
skip_if_port_is_rj45(port_name)
1328+
if is_port_type_rj45(port_name):
1329+
click.echo("This functionality is not applicable for RJ45 port {}.".format(port_name))
1330+
sys.exit(EXIT_FAIL)
13261331

13271332
if not is_sfp_present(port_name):
13281333
click.echo("{}: SFP EEPROM not detected\n".format(port_name))

tests/mock_platform_sfputil/mock_platform_sfputil.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from sonic_platform_base.platform_base import PlatformBase
44
from sonic_platform_base.chassis_base import ChassisBase
55
from sonic_platform_base.sfp_base import SfpBase
6+
import utilities_common.platform_sfputil_helper as platform_sfputil_helper
67

78
portMap = None
89
RJ45Ports = None
@@ -32,7 +33,7 @@ def mock_platform_sfputil_read_porttab_mappings():
3233
portMap = jsonobj['portMap']
3334
RJ45Ports = jsonobj['RJ45Ports']
3435

35-
def mock_platform_sfputil_helper(platform_sfputil_helper):
36+
def mock_platform_sfputil_helper():
3637
platform_sfputil_helper.platform_chassis = mock_Chassis()
3738
platform_sfputil_helper.platform_sfputil = True
3839
platform_sfputil_helper.platform_porttab_mapping_read = False

tests/sfp_test.py

+8
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,14 @@ def test_sfp_presence(self):
372372
assert result.exit_code == 0
373373
assert result.output == expected
374374

375+
result = runner.invoke(show.cli.commands["interfaces"].commands["transceiver"].commands["presence"], ["Ethernet29"])
376+
expected = """Port Presence
377+
---------- -----------
378+
Ethernet29 Not present
379+
"""
380+
assert result.exit_code == 0
381+
assert result.output == expected
382+
375383
result = runner.invoke(show.cli.commands["interfaces"].commands["transceiver"].commands["presence"], ["Ethernet36"])
376384
expected = """Port Presence
377385
---------- ----------

tests/sfputil_test.py

+12-18
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def test_version(self):
268268
result = runner.invoke(sfputil.cli.commands['version'], [])
269269
assert result.output.rstrip() == 'sfputil version {}'.format(sfputil.VERSION)
270270

271-
@patch('sfputil.main.is_rj45_port_from_api', MagicMock(return_value=False))
271+
@patch('sfputil.main.is_port_type_rj45', MagicMock(return_value=False))
272272
def test_error_status_from_db(self):
273273
db = Db()
274274
expected_output = [['Ethernet0', 'Blocking Error|High temperature'],
@@ -285,7 +285,7 @@ def test_error_status_from_db(self):
285285
output = sfputil.fetch_error_status_from_state_db('Ethernet0', db.db)
286286
assert output == expected_output_ethernet0
287287

288-
@patch('sfputil.main.is_rj45_port_from_api', MagicMock(return_value=True))
288+
@patch('sfputil.main.is_port_type_rj45', MagicMock(return_value=True))
289289
def test_error_status_from_db_RJ45(self):
290290
db = Db()
291291
expected_output = [['Ethernet0', 'N/A'],
@@ -304,7 +304,7 @@ def test_error_status_from_db_RJ45(self):
304304

305305
@patch('sfputil.main.logical_port_name_to_physical_port_list', MagicMock(return_value=[1]))
306306
@patch('sfputil.main.logical_port_to_physical_port_index', MagicMock(return_value=1))
307-
@patch('sfputil.main.is_rj45_port_from_api', MagicMock(return_value=False))
307+
@patch('sfputil.main.is_port_type_rj45', MagicMock(return_value=False))
308308
@patch('subprocess.check_output', MagicMock(return_value="['0:OK']"))
309309
def test_fetch_error_status_from_platform_api(self):
310310
output = sfputil.fetch_error_status_from_platform_api('Ethernet0')
@@ -313,7 +313,7 @@ def test_fetch_error_status_from_platform_api(self):
313313
@patch('sfputil.main.logical_port_name_to_physical_port_list', MagicMock(return_value=[1]))
314314
@patch('sfputil.main.logical_port_to_physical_port_index', MagicMock(return_value=1))
315315
@patch('subprocess.check_output', MagicMock(return_value="['0:OK']"))
316-
@patch('sfputil.main.is_rj45_port_from_api', MagicMock(return_value=True))
316+
@patch('sfputil.main.is_port_type_rj45', MagicMock(return_value=True))
317317
def test_fetch_error_status_from_platform_api_RJ45(self):
318318
output = sfputil.fetch_error_status_from_platform_api('Ethernet0')
319319
assert output == [['Ethernet0', 'N/A']]
@@ -407,7 +407,7 @@ def test_show_lpmode(self, mock_chassis):
407407
@patch('sfputil.main.logical_port_to_physical_port_index', MagicMock(return_value=1))
408408
@patch('sfputil.main.logical_port_name_to_physical_port_list', MagicMock(return_value=[1]))
409409
@patch('sfputil.main.platform_sfputil', MagicMock(is_logical_port=MagicMock(return_value=1)))
410-
@patch('sfputil.main.is_rj45_port_from_api', MagicMock(return_value=True))
410+
@patch('sfputil.main.is_port_type_rj45', MagicMock(return_value=True))
411411
def test_show_eeprom_RJ45(self, mock_chassis):
412412
mock_sfp = MagicMock()
413413
mock_api = MagicMock()
@@ -418,14 +418,8 @@ def test_show_eeprom_RJ45(self, mock_chassis):
418418
expected_output = "Ethernet16: SFP EEPROM is not applicable for RJ45 port\n\n\n"
419419
assert result.output == expected_output
420420

421-
@patch('sfputil.main.is_rj45_port_from_api', MagicMock(return_value=True))
422-
@patch('sys.exit', MagicMock(return_value=EXIT_FAIL))
423-
def test_skip_if_port_is_rj45(self):
424-
result = sfputil.skip_if_port_is_rj45('Ethernet0')
425-
assert result == None
426-
427421
@patch('sfputil.main.logical_port_name_to_physical_port_list', MagicMock(return_value=1))
428-
@patch('sfputil.main.is_rj45_port_from_api', MagicMock(return_value=True))
422+
@patch('sfputil.main.is_port_type_rj45', MagicMock(return_value=True))
429423
@patch('sfputil.main.platform_sfputil', MagicMock(is_logical_port=MagicMock(return_value=1)))
430424
def test_lpmode_set(self):
431425
runner = CliRunner()
@@ -434,7 +428,7 @@ def test_lpmode_set(self):
434428
assert result.exit_code == EXIT_FAIL
435429

436430
@patch('sfputil.main.logical_port_name_to_physical_port_list', MagicMock(return_value=1))
437-
@patch('sfputil.main.is_rj45_port_from_api', MagicMock(return_value=True))
431+
@patch('sfputil.main.is_port_type_rj45', MagicMock(return_value=True))
438432
@patch('sfputil.main.platform_sfputil', MagicMock(is_logical_port=MagicMock(return_value=1)))
439433
def test_reset_RJ45(self):
440434
runner = CliRunner()
@@ -457,7 +451,7 @@ def test_unlock_firmware(self, mock_chassis):
457451

458452
@patch('sfputil.main.platform_chassis')
459453
@patch('sfputil.main.logical_port_to_physical_port_index', MagicMock(return_value=1))
460-
@patch('sfputil.main.is_rj45_port_from_api', MagicMock(return_value=True))
454+
@patch('sfputil.main.is_port_type_rj45', MagicMock(return_value=True))
461455
def test_show_fwversion_Rj45(self, mock_chassis):
462456
mock_sfp = MagicMock()
463457
mock_api = MagicMock()
@@ -494,23 +488,23 @@ def test_commit_firmwre(self, mock_chassis):
494488
assert status == 1
495489

496490
@patch('sfputil.main.is_sfp_present', MagicMock(return_value=True))
497-
@patch('sfputil.main.is_rj45_port_from_api', MagicMock(return_value=True))
491+
@patch('sfputil.main.is_port_type_rj45', MagicMock(return_value=True))
498492
def test_firmware_run_RJ45(self):
499493
runner = CliRunner()
500494
result = runner.invoke(sfputil.cli.commands['firmware'].commands['run'], ["--mode", "0", "Ethernet0"])
501495
assert result.output == 'This functionality is not applicable for RJ45 port Ethernet0.\n'
502496
assert result.exit_code == EXIT_FAIL
503497

504498
@patch('sfputil.main.is_sfp_present', MagicMock(return_value=True))
505-
@patch('sfputil.main.is_rj45_port_from_api', MagicMock(return_value=True))
499+
@patch('sfputil.main.is_port_type_rj45', MagicMock(return_value=True))
506500
def test_firmware_commit_RJ45(self):
507501
runner = CliRunner()
508502
result = runner.invoke(sfputil.cli.commands['firmware'].commands['commit'], ["Ethernet0"])
509503
assert result.output == 'This functionality is not applicable for RJ45 port Ethernet0.\n'
510504
assert result.exit_code == EXIT_FAIL
511505

512506
@patch('sfputil.main.logical_port_to_physical_port_index', MagicMock(return_value=1))
513-
@patch('sfputil.main.is_rj45_port_from_api', MagicMock(return_value=True))
507+
@patch('sfputil.main.is_port_type_rj45', MagicMock(return_value=True))
514508
@patch('sfputil.main.is_sfp_present', MagicMock(return_value=1))
515509
def test_firmware_upgrade_RJ45(self):
516510
runner = CliRunner()
@@ -519,7 +513,7 @@ def test_firmware_upgrade_RJ45(self):
519513
assert result.exit_code == EXIT_FAIL
520514

521515
@patch('sfputil.main.logical_port_to_physical_port_index', MagicMock(return_value=1))
522-
@patch('sfputil.main.is_rj45_port_from_api', MagicMock(return_value=True))
516+
@patch('sfputil.main.is_port_type_rj45', MagicMock(return_value=True))
523517
@patch('sfputil.main.is_sfp_present', MagicMock(return_value=1))
524518
def test_firmware_download_RJ45(self):
525519
runner = CliRunner()

0 commit comments

Comments
 (0)