Skip to content

Commit 999f165

Browse files
Refactor xcvrd common functions and improve logging (#654)
- Refactor common xcvrd functions into shared utilities - Move functions from xcvrd.py to xcvrd_utilities/xcvrd_shared_utilities.py - Add thread identifier to logging for better debugging - Fix test imports after refactoring - Add common utility functions for better code organization - Improve logging consistency across xcvrd components
1 parent 44666a0 commit 999f165

File tree

6 files changed

+469
-353
lines changed

6 files changed

+469
-353
lines changed

sonic-xcvrd/tests/test_xcvrd.py

Lines changed: 117 additions & 116 deletions
Large diffs are not rendered by default.

sonic-xcvrd/xcvrd/dom/dom_mgr.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from xcvrd.xcvrd_utilities import sfp_status_helper
2323
from xcvrd.xcvrd_utilities.xcvr_table_helper import *
2424
from xcvrd.xcvrd_utilities import port_event_helper
25+
from xcvrd.xcvrd_utilities import common
2526
from xcvrd.dom.utilities.dom_sensor.db_utils import DOMDBUtils
2627
from xcvrd.dom.utilities.vdm.utils import VDMUtils
2728
from xcvrd.dom.utilities.vdm.db_utils import VDMDBUtils
@@ -129,7 +130,7 @@ def is_port_in_cmis_initialization_process(self, logical_port_name):
129130
self.log_warning("Got invalid asic index for {} while checking cmis init status".format(logical_port_name))
130131
return False
131132

132-
cmis_state = xcvrd.get_cmis_state_from_state_db(logical_port_name, self.xcvr_table_helper.get_status_sw_tbl(asic_index))
133+
cmis_state = common.get_cmis_state_from_state_db(logical_port_name, self.xcvr_table_helper.get_status_sw_tbl(asic_index))
133134
if cmis_state not in xcvrd.CMIS_TERMINAL_STATES:
134135
return True
135136
else:
@@ -142,19 +143,19 @@ def is_port_dom_monitoring_disabled(self, logical_port_name):
142143
# Update port sfp firmware info in db
143144
def post_port_sfp_firmware_info_to_db(self, logical_port_name, port_mapping, table,
144145
stop_event=threading.Event(), firmware_info_cache=None):
145-
for physical_port, physical_port_name in xcvrd.get_physical_port_name_dict(logical_port_name, port_mapping).items():
146+
for physical_port, physical_port_name in common.get_physical_port_name_dict(logical_port_name, port_mapping).items():
146147
if stop_event.is_set():
147148
break
148149

149-
if not xcvrd._wrapper_get_presence(physical_port):
150+
if not common._wrapper_get_presence(physical_port):
150151
continue
151152

152153
try:
153154
if firmware_info_cache is not None and physical_port in firmware_info_cache:
154155
# If cache is enabled and firmware information is in cache, just read from cache, no need read from EEPROM
155156
transceiver_firmware_info_dict = firmware_info_cache[physical_port]
156157
else:
157-
transceiver_firmware_info_dict = xcvrd._wrapper_get_transceiver_firmware_info(physical_port)
158+
transceiver_firmware_info_dict = common._wrapper_get_transceiver_firmware_info(physical_port)
158159
if firmware_info_cache is not None:
159160
# If cache is enabled, put firmware information to cache
160161
firmware_info_cache[physical_port] = transceiver_firmware_info_dict
@@ -176,21 +177,21 @@ def post_port_sfp_firmware_info_to_db(self, logical_port_name, port_mapping, tab
176177

177178
# Update port pm info in db
178179
def post_port_pm_info_to_db(self, logical_port_name, port_mapping, table, stop_event=threading.Event(), pm_info_cache=None):
179-
for physical_port, physical_port_name in xcvrd.get_physical_port_name_dict(logical_port_name, port_mapping).items():
180+
for physical_port, physical_port_name in common.get_physical_port_name_dict(logical_port_name, port_mapping).items():
180181
if stop_event.is_set():
181182
break
182183

183-
if not xcvrd._wrapper_get_presence(physical_port):
184+
if not common._wrapper_get_presence(physical_port):
184185
continue
185186

186-
if xcvrd._wrapper_is_flat_memory(physical_port) == True:
187+
if common._wrapper_is_flat_memory(physical_port) == True:
187188
continue
188189

189190
if pm_info_cache is not None and physical_port in pm_info_cache:
190191
# If cache is enabled and pm info is in cache, just read from cache, no need read from EEPROM
191192
pm_info_dict = pm_info_cache[physical_port]
192193
else:
193-
pm_info_dict = xcvrd._wrapper_get_transceiver_pm(physical_port)
194+
pm_info_dict = common._wrapper_get_transceiver_pm(physical_port)
194195
if pm_info_cache is not None:
195196
# If cache is enabled, put dom information to cache
196197
pm_info_cache[physical_port] = pm_info_dict
@@ -268,7 +269,7 @@ def task_worker(self):
268269
continue
269270

270271
if not sfp_status_helper.detect_port_in_error_status(logical_port_name, self.xcvr_table_helper.get_status_sw_tbl(asic_index)):
271-
if not xcvrd._wrapper_get_presence(physical_port):
272+
if not common._wrapper_get_presence(physical_port):
272273
continue
273274

274275
try:
@@ -345,7 +346,7 @@ def run(self):
345346
self.task_worker()
346347
except Exception as e:
347348
self.log_error("Exception occured at {} thread due to {}".format(threading.current_thread().getName(), repr(e)))
348-
xcvrd.log_exception_traceback()
349+
common.log_exception_traceback()
349350
self.exc = e
350351
self.main_thread_stop_event.set()
351352

@@ -440,7 +441,7 @@ def on_remove_logical_port(self, port_change_event):
440441
# To avoid race condition, remove the entry TRANSCEIVER_FIRMWARE_INFO, TRANSCEIVER_DOM_SENSOR, TRANSCEIVER_PM and HW section of TRANSCEIVER_STATUS table.
441442
# This thread only updates TRANSCEIVER_FIRMWARE_INFO, TRANSCEIVER_DOM_SENSOR, TRANSCEIVER_PM and HW section of TRANSCEIVER_STATUS table,
442443
# so we don't have to remove entries from TRANSCEIVER_INFO, TRANSCEIVER_DOM_THRESHOLD and VDM threshold value tables.
443-
xcvrd.del_port_sfp_dom_info_from_db(port_change_event.port_name,
444+
common.del_port_sfp_dom_info_from_db(port_change_event.port_name,
444445
self.port_mapping,
445446
[self.xcvr_table_helper.get_dom_tbl(port_change_event.asic_id),
446447
self.xcvr_table_helper.get_dom_flag_tbl(port_change_event.asic_id),

0 commit comments

Comments
 (0)