Skip to content

Commit 6f1d51a

Browse files
stephenxskeboliu
authored andcommitted
Revert the logic to fetch presence status from error status for RJ45 port (#17)
* Revert the logic to fetch presence status from error status Signed-off-by: Stephen Sun <[email protected]> * Unit test Signed-off-by: Stephen Sun <[email protected]> * Fix error Signed-off-by: Stephen Sun <[email protected]>
1 parent a654a02 commit 6f1d51a

File tree

5 files changed

+24
-45
lines changed

5 files changed

+24
-45
lines changed

scripts/sfpshow

+10-18
Original file line numberDiff line numberDiff line change
@@ -431,34 +431,26 @@ class SFPShow(object):
431431
self.intf_eeprom[interface] = "SFP EEPROM Not detected\n"
432432

433433

434-
def convert_presence_info_to_cli_output_string(self, state_db, interface_name):
435-
sfp_info_dict = state_db.get_all(state_db.STATE_DB, 'TRANSCEIVER_INFO|{}'.format(interface_name))
436-
if not sfp_info_dict:
437-
status_string = 'Not present'
438-
elif sfp_info_dict.get('type') == RJ45_PORT_TYPE:
439-
status = state_db.get_all(state_db.STATE_DB, 'TRANSCEIVER_STATUS|{}'.format(interface_name))
440-
status_string = status.get('error')
441-
if status_string == 'N/A':
442-
status_string = 'Present'
443-
else:
444-
status_string = 'Present'
445-
446-
return status_string
447-
448434
@multi_asic_util.run_on_multi_asic
449435
def get_presence(self):
450436
port_table = []
451437

452438
if self.intf_name is not None:
453-
presence_str = self.convert_presence_info_to_cli_output_string(self.db, self.intf_name)
454-
port_table.append((self.intf_name, presence_str))
439+
presence = self.db.exists(self.db.STATE_DB, 'TRANSCEIVER_INFO|{}'.format(self.intf_name))
440+
if presence:
441+
port_table.append((self.intf_name, 'Present'))
442+
else:
443+
port_table.append((self.intf_name, 'Not present'))
455444
else:
456445
port_table_keys = self.db.keys(self.db.APPL_DB, "PORT_TABLE:*")
457446
for i in port_table_keys:
458447
key = re.split(':', i, maxsplit=1)[-1].strip()
459448
if key and key.startswith(front_panel_prefix()) and not key.startswith((backplane_prefix(), inband_prefix(), recirc_prefix())):
460-
presence_str = self.convert_presence_info_to_cli_output_string(self.db, key)
461-
port_table.append((key, presence_str))
449+
presence = self.db.exists(self.db.STATE_DB, 'TRANSCEIVER_INFO|{}'.format(key))
450+
if presence:
451+
port_table.append((key, 'Present'))
452+
else:
453+
port_table.append((key, 'Not present'))
462454

463455
self.table += port_table
464456

sfputil/main.py

+6-19
Original file line numberDiff line numberDiff line change
@@ -740,13 +740,6 @@ def presence(port):
740740

741741
logical_port_list = [port]
742742

743-
state_db = SonicV2Connector(host='127.0.0.1')
744-
if state_db is not None:
745-
state_db.connect(state_db.STATE_DB)
746-
else:
747-
click.echo("Failed to connect to STATE_DB")
748-
return
749-
750743
for logical_port_name in logical_port_list:
751744
ganged = False
752745
i = 1
@@ -762,19 +755,13 @@ def presence(port):
762755
for physical_port in physical_port_list:
763756
port_name = get_physical_port_name(logical_port_name, i, ganged)
764757

765-
if is_rj45_port_from_db(port_name, state_db):
766-
status = state_db.get_all(state_db.STATE_DB, 'TRANSCEIVER_STATUS|{}'.format(port_name))
767-
status_string = status.get('error')
768-
if status_string == 'N/A':
769-
status_string = 'Present'
770-
else:
771-
try:
772-
presence = platform_chassis.get_sfp(physical_port).get_presence()
773-
except NotImplementedError:
774-
click.echo("This functionality is currently not implemented for this platform")
775-
sys.exit(ERROR_NOT_IMPLEMENTED)
758+
try:
759+
presence = platform_chassis.get_sfp(physical_port).get_presence()
760+
except NotImplementedError:
761+
click.echo("This functionality is currently not implemented for this platform")
762+
sys.exit(ERROR_NOT_IMPLEMENTED)
776763

777-
status_string = "Present" if presence else "Not present"
764+
status_string = "Present" if presence else "Not present"
778765
output_table.append([port_name, status_string])
779766

780767
i += 1

tests/mock_tables/state_db.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@
222222
"error": "N/A"
223223
},
224224
"TRANSCEIVER_STATUS|Ethernet16": {
225-
"status": "255",
226-
"error": "Not present"
225+
"status": "0",
226+
"error": "N/A"
227227
},
228228
"TRANSCEIVER_STATUS|Ethernet28": {
229229
"status": "0",

tests/sfp_test.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,8 @@ def test_sfp_presence(self):
350350

351351
result = runner.invoke(show.cli.commands["interfaces"].commands["transceiver"].commands["presence"], ["Ethernet16"])
352352
expected = """Port Presence
353-
---------- -----------
354-
Ethernet16 Not present
353+
---------- ----------
354+
Ethernet16 Present
355355
"""
356356
assert result.exit_code == 0
357357
assert result.output == expected
@@ -367,7 +367,7 @@ def test_sfp_presence(self):
367367
result = runner.invoke(show.cli.commands["interfaces"].commands["transceiver"].commands["presence"], ["Ethernet36"])
368368
expected = """Port Presence
369369
---------- ----------
370-
Ethernet36 Unknown
370+
Ethernet36 Present
371371
"""
372372
assert result.exit_code == 0
373373
assert result.output == expected

tests/sfputil_test.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,8 @@ def test_show_presence(self, mock_chassis):
308308
result = runner.invoke(sfputil.cli.commands['show'].commands['presence'], ["-p", "Ethernet16"])
309309
assert result.exit_code == 0
310310
expected_output = """Port Presence
311-
---------- -----------
312-
Ethernet16 Not present
311+
---------- ----------
312+
Ethernet16 Present
313313
"""
314314
assert result.output == expected_output
315315

@@ -325,7 +325,7 @@ def test_show_presence(self, mock_chassis):
325325
assert result.exit_code == 0
326326
expected_output = """Port Presence
327327
---------- ----------
328-
Ethernet36 Unknown
328+
Ethernet36 Present
329329
"""
330330
assert result.output == expected_output
331331

0 commit comments

Comments
 (0)