Skip to content

Commit d5903e1

Browse files
committed
Addressed review comments and added _validate_and_get_physical_port function
1 parent 3b4dd06 commit d5903e1

File tree

7 files changed

+97
-95
lines changed

7 files changed

+97
-95
lines changed

sonic-xcvrd/tests/test_xcvrd.py

+16-11
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ def mock_get_transceiver_dom_flags(physical_port):
586586
dom_tbl = Table("STATE_DB", TRANSCEIVER_DOM_FLAG_TABLE)
587587
dom_db_utils.xcvr_table_helper.get_dom_flag_tbl = MagicMock(return_value=dom_tbl)
588588
dom_db_utils.dom_utils.get_transceiver_dom_flags = MagicMock(return_value=None)
589-
dom_db_utils.update_flag_metadata_tables = MagicMock()
589+
dom_db_utils._update_flag_metadata_tables = MagicMock()
590590
assert dom_tbl.get_size() == 0
591591

592592
# Ensure table is empty asic_index is None
@@ -618,17 +618,17 @@ def mock_get_transceiver_dom_flags(physical_port):
618618
dom_db_utils.dom_utils.get_transceiver_dom_flags = MagicMock(side_effect=mock_get_transceiver_dom_flags)
619619
dom_db_utils.post_port_dom_flags_to_db(logical_port_name, db_cache=db_cache)
620620
assert dom_tbl.get_size_for_key(logical_port_name) == 21
621-
assert dom_db_utils.update_flag_metadata_tables.call_count == 1
621+
assert dom_db_utils._update_flag_metadata_tables.call_count == 1
622622

623623
# Reset the mock to clear the call count
624-
dom_db_utils.update_flag_metadata_tables.reset_mock()
624+
dom_db_utils._update_flag_metadata_tables.reset_mock()
625625

626626
# Ensure db_cache is populated correctly
627627
assert db_cache.get(0) is not None
628628
dom_db_utils.dom_utils.get_transceiver_dom_flags = MagicMock(return_value=None)
629629
dom_db_utils.post_port_dom_flags_to_db(logical_port_name, db_cache=db_cache)
630630
assert dom_tbl.get_size_for_key(logical_port_name) == 21
631-
assert dom_db_utils.update_flag_metadata_tables.call_count == 0
631+
assert dom_db_utils._update_flag_metadata_tables.call_count == 0
632632

633633
@pytest.mark.parametrize("flag_value_table, flag_value_table_found, current_value, expected_change_count, expected_set_time, expected_clear_time", [
634634
(None, False, 'N/A', None, None, None),
@@ -665,7 +665,7 @@ def field_value_pairs_to_dict(fvp):
665665
stop_event = threading.Event()
666666
db_utils = DBUtils(mock_sfp_obj_dict, port_mapping, stop_event, mock_logger)
667667
# Call the function
668-
db_utils.update_flag_metadata_tables(logical_port_name, mock_curr_flag_dict,
668+
db_utils._update_flag_metadata_tables(logical_port_name, mock_curr_flag_dict,
669669
flag_values_dict_update_time, flag_value_table,
670670
flag_change_count_table, flag_last_set_time_table,
671671
flag_last_clear_time_table, table_name_for_logging)
@@ -966,7 +966,7 @@ def mock_get_transceiver_status(physical_port):
966966
status_db_utils.post_port_transceiver_hw_status_to_db(logical_port_name, db_cache=db_cache)
967967
assert status_tbl.get_size_for_key(logical_port_name) == 12
968968

969-
def testpost_port_transceiver_hw_status_flags_to_db(self):
969+
def test_post_port_transceiver_hw_status_flags_to_db(self):
970970
def mock_get_transceiver_status_flags(physical_port):
971971
return {
972972
"datapath_firmware_fault": "False",
@@ -996,7 +996,7 @@ def mock_get_transceiver_status_flags(physical_port):
996996
status_flag_tbl = Table("STATE_DB", TRANSCEIVER_STATUS_FLAG_TABLE)
997997
status_db_utils.xcvr_table_helper.get_status_flag_tbl = MagicMock(return_value=status_flag_tbl)
998998
status_db_utils.status_utils.get_transceiver_status_flags = MagicMock(return_value=None)
999-
status_db_utils.update_flag_metadata_tables = MagicMock()
999+
status_db_utils._update_flag_metadata_tables = MagicMock()
10001000
assert status_flag_tbl.get_size() == 0
10011001

10021002
# Ensure table is empty asic_index is None
@@ -1028,17 +1028,17 @@ def mock_get_transceiver_status_flags(physical_port):
10281028
status_db_utils.status_utils.get_transceiver_status_flags = MagicMock(side_effect=mock_get_transceiver_status_flags)
10291029
status_db_utils.post_port_transceiver_hw_status_flags_to_db(logical_port_name, db_cache=db_cache)
10301030
assert status_flag_tbl.get_size_for_key(logical_port_name) == 12
1031-
assert status_db_utils.update_flag_metadata_tables.call_count == 1
1031+
assert status_db_utils._update_flag_metadata_tables.call_count == 1
10321032

10331033
# Reset the mock to clear the call count
1034-
status_db_utils.update_flag_metadata_tables.reset_mock()
1034+
status_db_utils._update_flag_metadata_tables.reset_mock()
10351035

10361036
# Ensure db_cache is populated correctly
10371037
assert db_cache.get(0) is not None
10381038
status_db_utils.status_utils.get_transceiver_status_flags = MagicMock(return_value=None)
10391039
status_db_utils.post_port_transceiver_hw_status_flags_to_db(logical_port_name, db_cache=db_cache)
10401040
assert status_flag_tbl.get_size_for_key(logical_port_name) == 12
1041-
assert status_db_utils.update_flag_metadata_tables.call_count == 0
1041+
assert status_db_utils._update_flag_metadata_tables.call_count == 0
10421042

10431043
@patch('xcvrd.xcvrd_utilities.port_event_helper.PortMapping.logical_port_name_to_physical_port_list', MagicMock(return_value=[0]))
10441044
@patch('xcvrd.xcvrd._wrapper_get_presence', MagicMock(return_value=True))
@@ -3301,11 +3301,16 @@ def test_beautify_dom_info_dict(self):
33013301
port_mapping = PortMapping()
33023302
xcvr_table_helper = XcvrTableHelper(DEFAULT_NAMESPACE)
33033303
stop_event = threading.Event()
3304-
dom_db_utils = DOMDBUtils(mock_sfp_obj_dict, port_mapping, xcvr_table_helper, stop_event, helper_logger)
3304+
mock_logger = MagicMock()
3305+
dom_db_utils = DOMDBUtils(mock_sfp_obj_dict, port_mapping, xcvr_table_helper, stop_event, mock_logger)
33053306

33063307
dom_db_utils._beautify_dom_info_dict(dom_info_dict)
33073308
assert dom_info_dict == expected_dom_info_dict
33083309

3310+
# Ensure that the method handles None input gracefully and logs a warning
3311+
dom_db_utils._beautify_dom_info_dict(None)
3312+
mock_logger.log_warning.assert_called_once_with("DOM info dict is None while beautifying")
3313+
33093314
def test_beautify_info_dict(self):
33103315
dom_info_dict = {
33113316
'eSNR' : 1.1,

sonic-xcvrd/xcvrd/dom/utilities/db/utils.py

+48-20
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,8 @@ def post_diagnostic_values_to_db(self, logical_port_name, table, get_values_func
2929
beautify_func (function, optional): Function to beautify the diagnostic values. Defaults to self.beautify_info_dict.
3030
enable_flat_memory_check (bool, optional): Flag to check for flat memory support. Defaults to False.
3131
"""
32-
if self.task_stopping_event.is_set():
33-
return
34-
35-
pport_list = self.port_mapping.get_logical_to_physical(logical_port_name)
36-
if not pport_list:
37-
self.logger.log_error(f"Post port diagnostic values to db failed for {logical_port_name} "
38-
"as no physical port found")
39-
return
40-
physical_port = pport_list[0]
41-
42-
if physical_port not in self.sfp_obj_dict:
43-
self.logger.log_error(f"Post port diagnostic values to db failed for {logical_port_name} "
44-
"as no sfp object found")
45-
return
46-
47-
if not self.xcvrd_utils.get_transceiver_presence(physical_port):
48-
return
49-
50-
if enable_flat_memory_check and self.xcvrd_utils.is_transceiver_flat_memory(physical_port):
32+
physical_port = self._validate_and_get_physical_port(logical_port_name, enable_flat_memory_check)
33+
if physical_port is None:
5134
return
5235

5336
try:
@@ -78,7 +61,52 @@ def post_diagnostic_values_to_db(self, logical_port_name, table, get_values_func
7861
"as functionality is not implemented")
7962
return
8063

81-
def update_flag_metadata_tables(self, logical_port_name, curr_flag_dict,
64+
def _validate_and_get_physical_port(self, logical_port_name, enable_flat_memory_check=False):
65+
"""
66+
Validates the logical port and retrieves the corresponding physical port.
67+
68+
Validation Steps:
69+
1. Ensures `task_stopping_event` is not set.
70+
2. Checks if the logical port maps to a physical port.
71+
3. Checks if the physical port has an associated SFP object.
72+
4. Checks if the transceiver is present.
73+
5. (Optional) Ensures the transceiver is not flat memory if `enable_flat_memory_check` is True.
74+
75+
If any of these checks fail, an error message is logged and `None` is returned.
76+
If all checks pass, the physical port number is returned.
77+
78+
Args:
79+
logical_port_name (str): Logical port name.
80+
enable_flat_memory_check (bool): Flag to check for flat memory support.
81+
82+
Returns:
83+
int: The physical port number if validation succeeds, or None if validation fails.
84+
"""
85+
if self.task_stopping_event.is_set():
86+
return None
87+
88+
pport_list = self.port_mapping.get_logical_to_physical(logical_port_name)
89+
if not pport_list:
90+
self.logger.log_error(f"Validate and get physical port failed for {logical_port_name} "
91+
"as no physical port found")
92+
return None
93+
94+
physical_port = pport_list[0]
95+
96+
if physical_port not in self.sfp_obj_dict:
97+
self.logger.log_error(f"Validate and get physical port failed for {logical_port_name} "
98+
"as no sfp object found")
99+
return None
100+
101+
if not self.xcvrd_utils.get_transceiver_presence(physical_port):
102+
return None
103+
104+
if enable_flat_memory_check and self.xcvrd_utils.is_transceiver_flat_memory(physical_port):
105+
return None
106+
107+
return physical_port
108+
109+
def _update_flag_metadata_tables(self, logical_port_name, curr_flag_dict,
82110
flag_values_dict_update_time,
83111
flag_value_table,
84112
flag_change_count_table, flag_last_set_time_table, flag_last_clear_time_table,

sonic-xcvrd/xcvrd/dom/utilities/dom_sensor/db_utils.py

+11-20
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ class DOMDBUtils(DBUtils):
88
"""
99
This class provides utility functions for managing DB operations
1010
related to DOM on transceivers.
11+
Handles data related to the following tables:
12+
- TRANSCEIVER_DOM_SENSOR
13+
- TRANSCEIVER_DOM_FLAG and its corresponding metadata tables (change count, set time, clear time)
14+
- TRANSCEIVER_DOM_THRESHOLD
1115
"""
1216
TEMP_UNIT = 'C'
1317
VOLT_UNIT = 'Volts'
@@ -35,31 +39,14 @@ def post_port_dom_sensor_info_to_db(self, logical_port_name, db_cache=None):
3539
enable_flat_memory_check=True)
3640

3741
def post_port_dom_flags_to_db(self, logical_port_name, db_cache=None):
38-
if self.task_stopping_event.is_set():
39-
return
40-
4142
asic_index = self.port_mapping.get_asic_id_for_logical_port(logical_port_name)
4243
if asic_index is None:
4344
self.logger.log_error(f"Post port dom flags to db failed for {logical_port_name} "
4445
"as no asic index found")
4546
return
4647

47-
pport_list = self.port_mapping.get_logical_to_physical(logical_port_name)
48-
if not pport_list:
49-
self.logger.log_error(f"Post port dom flags to db failed for {logical_port_name} "
50-
"as no physical port found")
51-
return
52-
physical_port = pport_list[0]
53-
54-
if physical_port not in self.sfp_obj_dict:
55-
self.logger.log_error(f"Post port dom flags to db failed for {logical_port_name} "
56-
"as no sfp object found")
57-
return
58-
59-
if not self.xcvrd_utils.get_transceiver_presence(physical_port):
60-
return
61-
62-
if self.xcvrd_utils.is_transceiver_flat_memory(physical_port):
48+
physical_port = self._validate_and_get_physical_port(logical_port_name, enable_flat_memory_check=True)
49+
if physical_port is None:
6350
return
6451

6552
try:
@@ -75,7 +62,7 @@ def post_port_dom_flags_to_db(self, logical_port_name, db_cache=None):
7562
return
7663
if dom_flags_dict:
7764
dom_flags_dict_update_time = self.get_current_time()
78-
self.update_flag_metadata_tables(logical_port_name, dom_flags_dict,
65+
self._update_flag_metadata_tables(logical_port_name, dom_flags_dict,
7966
dom_flags_dict_update_time,
8067
self.xcvr_table_helper.get_dom_flag_tbl(asic_index),
8168
self.xcvr_table_helper.get_dom_flag_change_count_tbl(asic_index),
@@ -126,6 +113,10 @@ def _strip_unit(self, value, unit):
126113

127114
# Remove unnecessary unit from the raw data
128115
def _beautify_dom_info_dict(self, dom_info_dict):
116+
if dom_info_dict is None:
117+
self.logger.log_warning("DOM info dict is None while beautifying")
118+
return
119+
129120
for k, v in dom_info_dict.items():
130121
if k == 'temperature':
131122
dom_info_dict[k] = self._strip_unit(v, self.TEMP_UNIT)

sonic-xcvrd/xcvrd/dom/utilities/status/db_utils.py

+8-19
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
class StatusDBUtils(DBUtils):
77
"""
88
This class provides utility functions for managing DB operations
9-
related to transceiver status.
9+
related to transceiver status (specifically, all the hardware related fields of transceiver status).
10+
Handles data related to the following tables:
11+
- TRANSCEIVER_STATUS
12+
- TRANSCEIVER_STATUS_FLAG and its corresponding metadata tables (change count, set time, clear time)
1013
"""
1114

1215
def __init__(self, sfp_obj_dict, port_mapping, xcvr_table_helper, task_stopping_event, logger):
@@ -36,28 +39,14 @@ def post_port_transceiver_hw_status_to_db(self, logical_port_name, db_cache=None
3639
db_cache=db_cache)
3740

3841
def post_port_transceiver_hw_status_flags_to_db(self, logical_port_name, db_cache=None):
39-
if self.task_stopping_event.is_set():
40-
return
41-
4242
asic_index = self.port_mapping.get_asic_id_for_logical_port(logical_port_name)
4343
if asic_index is None:
4444
self.logger.log_error(f"Post port transceiver hw status flags to db failed for {logical_port_name} "
4545
"as no asic index found")
4646
return
4747

48-
pport_list = self.port_mapping.get_logical_to_physical(logical_port_name)
49-
if not pport_list:
50-
self.logger.log_error(f"Post port transceiver hw status flags to db failed for {logical_port_name} "
51-
"as no physical port found")
52-
return
53-
physical_port = pport_list[0]
54-
55-
if physical_port not in self.sfp_obj_dict:
56-
self.logger.log_error(f"Post port transceiver hw status flags to db failed for {logical_port_name} "
57-
"as no sfp object found")
58-
return
59-
60-
if not self.xcvrd_utils.get_transceiver_presence(physical_port):
48+
physical_port = self._validate_and_get_physical_port(logical_port_name)
49+
if physical_port is None:
6150
return
6251

6352
try:
@@ -72,7 +61,7 @@ def post_port_transceiver_hw_status_flags_to_db(self, logical_port_name, db_cach
7261
"as no status flags found")
7362
return
7463
if status_flags_dict:
75-
self.update_flag_metadata_tables(logical_port_name, status_flags_dict,
64+
self._update_flag_metadata_tables(logical_port_name, status_flags_dict,
7665
self.get_current_time(),
7766
self.xcvr_table_helper.get_status_flag_tbl(asic_index),
7867
self.xcvr_table_helper.get_status_flag_change_count_tbl(asic_index),
@@ -97,6 +86,6 @@ def post_port_transceiver_hw_status_flags_to_db(self, logical_port_name, db_cach
9786
return
9887

9988
except NotImplementedError:
100-
self.logger.log_error(f"Post port transceiver hw status flags to db failed for {logical_port_name} "
89+
self.logger.log_notice(f"Post port transceiver hw status flags to db failed for {logical_port_name} "
10190
"as functionality is not implemented")
10291
return

sonic-xcvrd/xcvrd/dom/utilities/vdm/db_utils.py

+9-20
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ class VDMDBUtils(DBUtils):
99
"""
1010
This class provides utility functions for managing
1111
DB operations related to VDM on transceivers.
12+
Handles data related to the following tables:
13+
- TRANSCEIVER_VDM_REAL_VALUE
14+
- TRANSCEIVER_VDM_XXXX_FLAG and its corresponding metadata tables (change count, set time, clear time)
15+
- XXXX refers to HALARM, LALARM, HWARN or LWARN
16+
- TRANSCEIVER_VDM_XXXX_THRESHOLD
17+
- XXXX refers to HALARM, LALARM, HWARN or LWARN
1218
"""
1319
def __init__(self, sfp_obj_dict, port_mapping, xcvr_table_helper, task_stopping_event, logger):
1420
super().__init__(sfp_obj_dict, port_mapping, task_stopping_event, logger)
@@ -40,25 +46,8 @@ def post_port_vdm_thresholds_to_db(self, logical_port_name, db_cache=None):
4046
# Update transceiver VDM threshold or flag info to db
4147
def _post_port_vdm_thresholds_or_flags_to_db(self, logical_port_name, get_vdm_table_func,
4248
get_vdm_values_func, flag_data=False, db_cache=None):
43-
if self.task_stopping_event.is_set():
44-
return
45-
46-
pport_list = self.port_mapping.get_logical_to_physical(logical_port_name)
47-
if not pport_list:
48-
self.logger.log_error(f"Post port vdm thresholds or flags to db failed for {logical_port_name} "
49-
"as no physical port found with flag_data {flag_data}")
50-
return
51-
physical_port = pport_list[0]
52-
53-
if physical_port not in self.sfp_obj_dict:
54-
self.logger.log_error(f"Post port vdm thresholds or flags to db failed for {logical_port_name} "
55-
"as no sfp object found with flag_data {flag_data}")
56-
return
57-
58-
if not self.xcvrd_utils.get_transceiver_presence(physical_port):
59-
return
60-
61-
if self.xcvrd_utils.is_transceiver_flat_memory(physical_port):
49+
physical_port = self._validate_and_get_physical_port(logical_port_name, enable_flat_memory_check=True)
50+
if physical_port is None:
6251
return
6352

6453
try:
@@ -89,7 +78,7 @@ def _post_port_vdm_thresholds_or_flags_to_db(self, logical_port_name, get_vdm_ta
8978
# for the flags
9079
if flag_data and threshold_value_dict:
9180
asic_id = self.port_mapping.get_asic_id_for_logical_port(logical_port_name)
92-
self.update_flag_metadata_tables(logical_port_name, threshold_value_dict,
81+
self._update_flag_metadata_tables(logical_port_name, threshold_value_dict,
9382
vdm_values_dict_update_time,
9483
self.xcvr_table_helper.get_vdm_flag_tbl(asic_id, threshold_type),
9584
self.xcvr_table_helper.get_vdm_flag_change_count_tbl(asic_id, threshold_type),

0 commit comments

Comments
 (0)