Skip to content

Commit ac2f553

Browse files
stephenxsstephens
and
stephens
authored
Improve the way to check port type of RJ45 port (#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]>
1 parent feeac84 commit ac2f553

File tree

8 files changed

+210
-122
lines changed

8 files changed

+210
-122
lines changed

scripts/intfutil

+23-17
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@ import os
55
import re
66
import sys
77

8-
from natsort import natsorted
9-
from tabulate import tabulate
10-
from utilities_common import constants
11-
from utilities_common import multi_asic as multi_asic_util
12-
from utilities_common.intf_filter import parse_interface_in_filter
13-
from sonic_py_common.interface import get_intf_longname
14-
158
# mock the redis for unit test purposes #
169
try:
1710
if os.environ["UTILITIES_UNIT_TESTING"] == "2":
@@ -20,13 +13,23 @@ try:
2013
sys.path.insert(0, modules_path)
2114
sys.path.insert(0, tests_path)
2215
import mock_tables.dbconnector
16+
from mock_platform_sfputil.mock_platform_sfputil import mock_platform_sfputil_helper
17+
mock_platform_sfputil_helper()
2318
if os.environ["UTILITIES_UNIT_TESTING_TOPOLOGY"] == "multi_asic":
2419
import mock_tables.mock_multi_asic
2520
mock_tables.dbconnector.load_namespace_config()
2621

2722
except KeyError:
2823
pass
2924

25+
from natsort import natsorted
26+
from tabulate import tabulate
27+
from utilities_common import constants
28+
from utilities_common import multi_asic as multi_asic_util
29+
from utilities_common.intf_filter import parse_interface_in_filter
30+
from utilities_common.platform_sfputil_helper import is_rj45_port, RJ45_PORT_TYPE
31+
from sonic_py_common.interface import get_intf_longname
32+
3033
# ========================== Common interface-utils logic ==========================
3134

3235

@@ -49,7 +52,7 @@ PORT_RMT_ADV_SPEEDS = 'rmt_adv_speeds'
4952
PORT_INTERFACE_TYPE = 'interface_type'
5053
PORT_ADV_INTERFACE_TYPES = 'adv_interface_types'
5154
PORT_TPID = "tpid"
52-
OPTICS_TYPE_RJ45 = 'RJ45'
55+
OPTICS_TYPE_RJ45 = RJ45_PORT_TYPE
5356
PORT_LINK_TRAINING = 'link_training'
5457
PORT_LINK_TRAINING_STATUS = 'link_training_status'
5558

@@ -161,10 +164,10 @@ def appl_db_port_status_get(appl_db, intf_name, status_type):
161164
if status is None:
162165
return "N/A"
163166
if status_type == PORT_SPEED and status != "N/A":
164-
optics_type = state_db_port_optics_get(appl_db, intf_name, PORT_OPTICS_TYPE)
167+
optics_type = port_optics_get(appl_db, intf_name, PORT_OPTICS_TYPE)
165168
status = port_speed_parse(status, optics_type)
166169
elif status_type == PORT_ADV_SPEEDS and status != "N/A" and status != "all":
167-
optics_type = state_db_port_optics_get(appl_db, intf_name, PORT_OPTICS_TYPE)
170+
optics_type = port_optics_get(appl_db, intf_name, PORT_OPTICS_TYPE)
168171
speed_list = status.split(',')
169172
new_speed_list = []
170173
for s in natsorted(speed_list):
@@ -181,7 +184,7 @@ def state_db_port_status_get(db, intf_name, field):
181184
if not status:
182185
return "N/A"
183186
if field in [PORT_RMT_ADV_SPEEDS] and status not in ["N/A", "all"]:
184-
optics_type = state_db_port_optics_get(db, intf_name, PORT_OPTICS_TYPE)
187+
optics_type = port_optics_get(db, intf_name, PORT_OPTICS_TYPE)
185188
speed_list = status.split(',')
186189
new_speed_list = []
187190
for s in natsorted(speed_list):
@@ -198,7 +201,7 @@ def port_oper_speed_get(db, intf_name):
198201
if oper_speed is None or oper_speed == "N/A" or oper_status != "up":
199202
return appl_db_port_status_get(db, intf_name, PORT_SPEED)
200203
else:
201-
optics_type = state_db_port_optics_get(db, intf_name, PORT_OPTICS_TYPE)
204+
optics_type = port_optics_get(db, intf_name, PORT_OPTICS_TYPE)
202205
return port_speed_parse(oper_speed, optics_type)
203206

204207
def port_oper_speed_get_raw(db, intf_name):
@@ -211,14 +214,17 @@ def port_oper_speed_get_raw(db, intf_name):
211214
speed = db.get(db.APPL_DB, PORT_STATUS_TABLE_PREFIX + intf_name, PORT_SPEED)
212215
return speed
213216

214-
def state_db_port_optics_get(state_db, intf_name, type):
217+
def port_optics_get(state_db, intf_name, type):
215218
"""
216219
Get optic type info for port
217220
"""
218221
full_table_id = PORT_TRANSCEIVER_TABLE_PREFIX + intf_name
219222
optics_type = state_db.get(state_db.STATE_DB, full_table_id, type)
220223
if optics_type is None:
221-
return "N/A"
224+
if is_rj45_port(intf_name):
225+
return OPTICS_TYPE_RJ45
226+
else:
227+
return "N/A"
222228
return optics_type
223229

224230
def merge_dicts(x,y):
@@ -325,13 +331,13 @@ def po_speed_dict(po_int_dict, appl_db):
325331
# If no speed was returned, append None without format
326332
po_list.append(None)
327333
else:
328-
optics_type = state_db_port_optics_get(appl_db, value[0], PORT_OPTICS_TYPE)
334+
optics_type = port_optics_get(appl_db, value[0], PORT_OPTICS_TYPE)
329335
interface_speed = port_speed_parse(interface_speed, optics_type)
330336
po_list.append(interface_speed)
331337
elif len(value) > 1:
332338
for intf in value:
333339
temp_speed = port_oper_speed_get_raw(appl_db, intf)
334-
optics_type = state_db_port_optics_get(appl_db, intf, PORT_OPTICS_TYPE)
340+
optics_type = port_optics_get(appl_db, intf, PORT_OPTICS_TYPE)
335341
temp_speed = int(temp_speed) if temp_speed else 0
336342
agg_speed_list.append(temp_speed)
337343
interface_speed = sum(agg_speed_list)
@@ -477,7 +483,7 @@ class IntfStatus(object):
477483
config_db_vlan_port_keys_get(self.combined_int_to_vlan_po_dict, self.front_panel_ports_list, key),
478484
appl_db_port_status_get(self.db, key, PORT_OPER_STATUS),
479485
appl_db_port_status_get(self.db, key, PORT_ADMIN_STATUS),
480-
state_db_port_optics_get(self.db, key, PORT_OPTICS_TYPE),
486+
port_optics_get(self.db, key, PORT_OPTICS_TYPE),
481487
appl_db_port_status_get(self.db, key, PORT_PFC_ASYM_STATUS)))
482488

483489
for po, value in self.portchannel_speed_dict.items():

scripts/sfpshow

+38-37
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ from natsort import natsorted
1717
from sonic_py_common.interface import front_panel_prefix, backplane_prefix, inband_prefix, recirc_prefix
1818
from sonic_py_common import multi_asic
1919
from tabulate import tabulate
20-
from utilities_common import multi_asic as multi_asic_util
2120

2221
# Mock the redis DB for unit test purposes
2322
try:
@@ -27,12 +26,17 @@ try:
2726
sys.path.insert(0, modules_path)
2827
sys.path.insert(0, test_path)
2928
import mock_tables.dbconnector
29+
from mock_platform_sfputil.mock_platform_sfputil import mock_platform_sfputil_helper
30+
mock_platform_sfputil_helper()
3031
if os.environ["UTILITIES_UNIT_TESTING_TOPOLOGY"] == "multi_asic":
3132
import mock_tables.mock_multi_asic
3233
mock_tables.dbconnector.load_namespace_config()
3334
except KeyError:
3435
pass
3536

37+
from utilities_common import multi_asic as multi_asic_util
38+
from utilities_common.platform_sfputil_helper import is_rj45_port, RJ45_PORT_TYPE
39+
3640
# TODO: We should share these maps and the formatting functions between sfputil and sfpshow
3741
QSFP_DATA_MAP = {
3842
'model': 'Vendor PN',
@@ -215,8 +219,6 @@ QSFP_DD_DOM_VALUE_UNIT_MAP = {
215219
'voltage': 'Volts'
216220
}
217221

218-
RJ45_PORT_TYPE = 'RJ45'
219-
220222

221223
def display_invalid_intf_eeprom(intf_name):
222224
output = intf_name + ': SFP EEPROM Not detected\n'
@@ -231,7 +233,6 @@ def display_invalid_intf_presence(intf_name):
231233

232234

233235
class SFPShow(object):
234-
235236
def __init__(self, intf_name, namespace_option, dump_dom=False):
236237
super(SFPShow, self).__init__()
237238
self.db = None
@@ -394,63 +395,63 @@ class SFPShow(object):
394395
output = ''
395396

396397
sfp_info_dict = state_db.get_all(state_db.STATE_DB, 'TRANSCEIVER_INFO|{}'.format(interface_name))
397-
if sfp_info_dict['type'] == RJ45_PORT_TYPE:
398-
output = 'SFP EEPROM is not applicable for RJ45 port\n'
398+
if sfp_info_dict:
399+
if sfp_info_dict['type'] == RJ45_PORT_TYPE:
400+
output = 'SFP EEPROM is not applicable for RJ45 port\n'
401+
else:
402+
output = 'SFP EEPROM detected\n'
403+
sfp_info_output = self.convert_sfp_info_to_output_string(sfp_info_dict)
404+
output += sfp_info_output
405+
406+
if dump_dom:
407+
sfp_type = sfp_info_dict['type']
408+
dom_info_dict = state_db.get_all(state_db.STATE_DB, 'TRANSCEIVER_DOM_SENSOR|{}'.format(interface_name))
409+
dom_output = self.convert_dom_to_output_string(sfp_type, dom_info_dict)
410+
output += dom_output
399411
else:
400-
output = 'SFP EEPROM detected\n'
401-
sfp_info_output = self.convert_sfp_info_to_output_string(sfp_info_dict)
402-
output += sfp_info_output
403-
404-
if dump_dom:
405-
sfp_type = sfp_info_dict['type']
406-
dom_info_dict = state_db.get_all(state_db.STATE_DB, 'TRANSCEIVER_DOM_SENSOR|{}'.format(interface_name))
407-
dom_output = self.convert_dom_to_output_string(sfp_type, dom_info_dict)
408-
output += dom_output
412+
if is_rj45_port(interface_name):
413+
output = 'SFP EEPROM is not applicable for RJ45 port\n'
414+
else:
415+
output = "SFP EEPROM Not detected\n"
409416

410417
return output
411418

412419
@multi_asic_util.run_on_multi_asic
413420
def get_eeprom(self):
414421
if self.intf_name is not None:
415-
presence = self.db.exists(self.db.STATE_DB, 'TRANSCEIVER_INFO|{}'.format(self.intf_name))
416-
if presence:
417-
self.intf_eeprom[self.intf_name] = self.convert_interface_sfp_info_to_cli_output_string(
418-
self.db, self.intf_name, self.dump_dom)
419-
else:
420-
self.intf_eeprom[self.intf_name] = "SFP EEPROM Not detected\n"
422+
self.intf_eeprom[self.intf_name] = self.convert_interface_sfp_info_to_cli_output_string(
423+
self.db, self.intf_name, self.dump_dom)
421424
else:
422425
port_table_keys = self.db.keys(self.db.APPL_DB, "PORT_TABLE:*")
423426
for i in port_table_keys:
424427
interface = re.split(':', i, maxsplit=1)[-1].strip()
425428
if interface and interface.startswith(front_panel_prefix()) and not interface.startswith((backplane_prefix(), inband_prefix(), recirc_prefix())):
426-
presence = self.db.exists(self.db.STATE_DB, 'TRANSCEIVER_INFO|{}'.format(interface))
427-
if presence:
428-
self.intf_eeprom[interface] = self.convert_interface_sfp_info_to_cli_output_string(
429-
self.db, interface, self.dump_dom)
430-
else:
431-
self.intf_eeprom[interface] = "SFP EEPROM Not detected\n"
429+
self.intf_eeprom[interface] = self.convert_interface_sfp_info_to_cli_output_string(
430+
self.db, interface, self.dump_dom)
431+
432+
def convert_interface_sfp_presence_state_to_cli_output_string(self, state_db, interface_name):
433+
sfp_info_dict = state_db.get_all(self.db.STATE_DB, 'TRANSCEIVER_INFO|{}'.format(interface_name))
434+
if sfp_info_dict:
435+
output = 'Present'
436+
else:
437+
output = 'Not present'
438+
return output
432439

433440

434441
@multi_asic_util.run_on_multi_asic
435442
def get_presence(self):
436443
port_table = []
437444

438445
if self.intf_name is not None:
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'))
446+
presence_string = self.convert_interface_sfp_presence_state_to_cli_output_string(self.db, self.intf_name)
447+
port_table.append((self.intf_name, presence_string))
444448
else:
445449
port_table_keys = self.db.keys(self.db.APPL_DB, "PORT_TABLE:*")
446450
for i in port_table_keys:
447451
key = re.split(':', i, maxsplit=1)[-1].strip()
448452
if key and key.startswith(front_panel_prefix()) and not key.startswith((backplane_prefix(), inband_prefix(), recirc_prefix())):
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'))
453+
presence_string = self.convert_interface_sfp_presence_state_to_cli_output_string(self.db, key)
454+
port_table.append((key, presence_string))
454455

455456
self.table += port_table
456457

0 commit comments

Comments
 (0)