diff --git a/sonic-ycabled/tests/test_ycable.py b/sonic-ycabled/tests/test_ycable.py index 1847ffd8d..4e25b5b01 100644 --- a/sonic-ycabled/tests/test_ycable.py +++ b/sonic-ycabled/tests/test_ycable.py @@ -311,11 +311,52 @@ def test_handle_state_update_task(self): state_db = {} y_cable_presence = False stopping_event = None + op = swsscommon.SET_COMMAND port_tbl, port_tbl_keys, loopback_tbl, loopback_keys, hw_mux_cable_tbl, hw_mux_cable_tbl_peer, y_cable_tbl, static_tbl, mux_tbl, grpc_client, fwd_state_response_tbl = {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {} - rc = handle_state_update_task(port, fvp_dict, y_cable_presence, port_tbl, port_tbl_keys, loopback_tbl, loopback_keys, hw_mux_cable_tbl, hw_mux_cable_tbl_peer, y_cable_tbl, static_tbl, mux_tbl, grpc_client, fwd_state_response_tbl, state_db, stopping_event) + rc = handle_state_update_task(op, port, fvp_dict, y_cable_presence, port_tbl, port_tbl_keys, loopback_tbl, loopback_keys, hw_mux_cable_tbl, hw_mux_cable_tbl_peer, y_cable_tbl, static_tbl, mux_tbl, grpc_client, fwd_state_response_tbl, state_db, stopping_event) assert(rc == None) + @patch('ycable.ycable_utilities.y_cable_helper.change_ports_status_for_y_cable_change_event', MagicMock(return_value=0)) + def test_handle_state_update_task_with_delete(self): + + port = "Ethernet0" + fvp_dict = {} + state_db = {} + y_cable_presence = False + stopping_event = None + op = swsscommon.DEL_COMMAND + port_tbl, port_tbl_keys, loopback_tbl, loopback_keys, hw_mux_cable_tbl, hw_mux_cable_tbl_peer, y_cable_tbl, static_tbl, mux_tbl, grpc_client, fwd_state_response_tbl = {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {} + rc = handle_state_update_task(op, port, fvp_dict, y_cable_presence, port_tbl, port_tbl_keys, loopback_tbl, loopback_keys, hw_mux_cable_tbl, hw_mux_cable_tbl_peer, y_cable_tbl, static_tbl, mux_tbl, grpc_client, fwd_state_response_tbl, state_db, stopping_event) + assert(rc == None) + + @patch('swsscommon.swsscommon.Select.addSelectable', MagicMock()) + @patch('swsscommon.swsscommon.Select.TIMEOUT', MagicMock(return_value=None)) + @patch('swsscommon.swsscommon.CastSelectableToRedisSelectObj', MagicMock()) + @patch('swsscommon.swsscommon.SubscriberStateTable') + @patch('swsscommon.swsscommon.Select.select') + def test_ycable_helper_state_update_task(self, mock_select, mock_sub_table): + + mock_selectable = MagicMock() + mock_selectable.pop = MagicMock( + side_effect=[('Ethernet0', swsscommon.SET_COMMAND, (('state', 'active'), )), (False, False, False), (False, False, False), (False, False, False), (False, False, False), (False, False, False), (False, False, False), (False, False, False), (False, False, False), (False, False, False), (False, False, False), (False, False, False), (False, False, False), (False, False, False)]) + mock_select.return_value = (swsscommon.Select.OBJECT, mock_selectable) + mock_sub_table.return_value = mock_selectable + + y_cable_presence = [True] + stopping_event = MagicMock() + sfp_error_event = MagicMock() + Y_cable_task = YcableStateUpdateTask(sfp_error_event, y_cable_presence) + Y_cable_task.task_stopping_event.is_set = MagicMock(side_effect=[False, True]) + mock_table = MagicMock() + """mock_table.getKeys = MagicMock(return_value=['Ethernet0', 'Ethernet4']) + mock_table.get = MagicMock( + side_effect=[(True, (('index', 1), )), (True, (('index', 2), ))]) + mock_swsscommon_table.return_value = mock_table + """ + Y_cable_task.task_worker(stopping_event, sfp_error_event, y_cable_presence) + assert swsscommon.Select.select.call_count == 1 + def wait_until(total_wait_time, interval, call_back, *args, **kwargs): wait_time = 0 diff --git a/sonic-ycabled/ycable/ycable.py b/sonic-ycabled/ycable/ycable.py index bc85b04d9..0b6ad0406 100644 --- a/sonic-ycabled/ycable/ycable.py +++ b/sonic-ycabled/ycable/ycable.py @@ -56,6 +56,9 @@ NOT_IMPLEMENTED_ERROR = 3 SFP_SYSTEM_ERROR = 4 +SFP_STATUS_REMOVED = "0" +SFP_STATUS_INSERTED = "1" + # Global platform specific sfputil class instance platform_sfputil = None # Global chassis object based on new platform api @@ -94,17 +97,20 @@ def detect_port_in_error_status(logical_port_name, status_tbl): rec, fvp = status_tbl.get(logical_port_name) if rec: status_dict = dict(fvp) - if status_dict['status'] in errors_block_eeprom_reading: + if status_dict.get('status', None) in errors_block_eeprom_reading: return True else: return False else: return False -def handle_state_update_task(port, fvp_dict, y_cable_presence, port_tbl, port_tbl_keys, loopback_tbl, loopback_keys, hw_mux_cable_tbl, hw_mux_cable_tbl_peer, y_cable_tbl, static_tbl, mux_tbl, grpc_client, fwd_state_response_tbl, state_db, stopping_event): +def handle_state_update_task(op, port, fvp_dict, y_cable_presence, port_tbl, port_tbl_keys, loopback_tbl, loopback_keys, hw_mux_cable_tbl, hw_mux_cable_tbl_peer, y_cable_tbl, static_tbl, mux_tbl, grpc_client, fwd_state_response_tbl, state_db, stopping_event): port_dict = {} - port_dict[port] = fvp_dict.get('status', None) + if op == swsscommon.SET_COMMAND: + port_dict[port] = SFP_STATUS_INSERTED + elif op == swsscommon.DEL_COMMAND: + port_dict[port] = SFP_STATUS_REMOVED y_cable_helper.change_ports_status_for_y_cable_change_event( port_dict, y_cable_presence, port_tbl, port_tbl_keys, loopback_tbl, loopback_keys, hw_mux_cable_tbl, hw_mux_cable_tbl_peer, y_cable_tbl, static_tbl, mux_tbl, grpc_client, fwd_state_response_tbl, state_db, stopping_event) @@ -143,9 +149,8 @@ def task_worker(self, y_cable_presence): logger.log_warning("Got invalid asic index for {}, ignored".format(logical_port_name)) continue - if not detect_port_in_error_status(logical_port_name, self.table_helper.get_status_tbl()[asic_index]): - if y_cable_presence[0] is True: - y_cable_helper.check_identifier_presence_and_update_mux_info_entry(self.table_helper.get_state_db(), self.table_helper.get_mux_tbl(), asic_index, logical_port_name, self.table_helper.get_y_cable_tbl(), self.table_helper.get_port_tbl()) + if y_cable_presence[0] is True: + y_cable_helper.check_identifier_presence_and_update_mux_info_entry(self.table_helper.get_state_db(), self.table_helper.get_mux_tbl(), asic_index, logical_port_name, self.table_helper.get_y_cable_tbl(), self.table_helper.get_port_tbl()) helper_logger.log_info("Stop DOM monitoring loop") @@ -230,7 +235,7 @@ def task_worker(self, stopping_event, sfp_error_event, y_cable_presence): continue # Check if all tables are created in table_helper - handle_state_update_task(port, fvp_dict, y_cable_presence, self.table_helper.get_port_tbl(), self.table_helper.port_table_keys, self.table_helper.get_loopback_tbl(), self.table_helper.loopback_keys, self.table_helper.get_hw_mux_cable_tbl(), self.table_helper.get_hw_mux_cable_tbl_peer(), self.table_helper.get_y_cable_tbl(), self.table_helper.get_static_tbl(), self.table_helper.get_mux_tbl(), self.table_helper.get_grpc_config_tbl(), self.table_helper.get_fwd_state_response_tbl(), self.table_helper.state_db, stopping_event) + handle_state_update_task(op, port, fvp_dict, y_cable_presence, self.table_helper.get_port_tbl(), self.table_helper.port_table_keys, self.table_helper.get_loopback_tbl(), self.table_helper.loopback_keys, self.table_helper.get_hw_mux_cable_tbl(), self.table_helper.get_hw_mux_cable_tbl_peer(), self.table_helper.get_y_cable_tbl(), self.table_helper.get_static_tbl(), self.table_helper.get_mux_tbl(), self.table_helper.get_grpc_config_tbl(), self.table_helper.get_fwd_state_response_tbl(), self.table_helper.state_db, stopping_event) def run(self): if self.task_stopping_event.is_set(): diff --git a/sonic-ycabled/ycable/ycable_utilities/y_cable_table_helper.py b/sonic-ycabled/ycable/ycable_utilities/y_cable_table_helper.py index 428b2feeb..285b10019 100644 --- a/sonic-ycabled/ycable/ycable_utilities/y_cable_table_helper.py +++ b/sonic-ycabled/ycable/ycable_utilities/y_cable_table_helper.py @@ -10,7 +10,7 @@ MUX_CABLE_STATIC_INFO_TABLE = "MUX_CABLE_STATIC_INFO" MUX_CABLE_INFO_TABLE = "MUX_CABLE_INFO" -TRANSCEIVER_STATUS_TABLE = 'TRANSCEIVER_STATUS' +TRANSCEIVER_INFO_TABLE = 'TRANSCEIVER_INFO' class YcableInfoUpdateTableHelper(object): def __init__(self): @@ -29,7 +29,7 @@ def __init__(self): self.state_db[asic_id] = daemon_base.db_connect("STATE_DB", namespace) self.config_db[asic_id] = daemon_base.db_connect("CONFIG_DB", namespace) self.port_tbl[asic_id] = swsscommon.Table(self.config_db[asic_id], "MUX_CABLE") - self.status_tbl[asic_id] = swsscommon.Table(self.state_db[asic_id], TRANSCEIVER_STATUS_TABLE) + self.status_tbl[asic_id] = swsscommon.Table(self.state_db[asic_id], TRANSCEIVER_INFO_TABLE) self.y_cable_tbl[asic_id] = swsscommon.Table( self.state_db[asic_id], swsscommon.STATE_HW_MUX_CABLE_TABLE_NAME) self.mux_tbl[asic_id] = swsscommon.Table( @@ -80,7 +80,7 @@ def __init__(self): self.state_db[asic_id] = daemon_base.db_connect("STATE_DB", namespace) self.appl_db[asic_id] = daemon_base.db_connect("APPL_DB", namespace) self.sub_status_tbl[asic_id] = swsscommon.SubscriberStateTable( - self.state_db[asic_id], TRANSCEIVER_STATUS_TABLE) + self.state_db[asic_id], TRANSCEIVER_INFO_TABLE) self.config_db[asic_id] = daemon_base.db_connect("CONFIG_DB", namespace) self.port_tbl[asic_id] = swsscommon.Table(self.config_db[asic_id], "MUX_CABLE") self.port_table_keys[asic_id] = self.port_tbl[asic_id].getKeys() @@ -497,7 +497,7 @@ def __init__(self): self.appl_db[asic_id] = daemon_base.db_connect("APPL_DB", namespace) self.config_db[asic_id] = daemon_base.db_connect("CONFIG_DB", namespace) self.port_tbl[asic_id] = swsscommon.Table(self.config_db[asic_id], "MUX_CABLE") - self.status_tbl[asic_id] = swsscommon.Table(self.state_db[asic_id], TRANSCEIVER_STATUS_TABLE) + self.status_tbl[asic_id] = swsscommon.Table(self.state_db[asic_id], TRANSCEIVER_INFO_TABLE) self.y_cable_tbl[asic_id] = swsscommon.Table( self.state_db[asic_id], swsscommon.STATE_HW_MUX_CABLE_TABLE_NAME) self.mux_tbl[asic_id] = swsscommon.Table(