diff --git a/sonic-ycabled/tests/test_y_cable_helper.py b/sonic-ycabled/tests/test_y_cable_helper.py index db51d0d3d..3ddbceae3 100644 --- a/sonic-ycabled/tests/test_y_cable_helper.py +++ b/sonic-ycabled/tests/test_y_cable_helper.py @@ -244,7 +244,8 @@ def test_post_port_mux_info_to_db(self): def test_post_port_mux_static_info_to_db(self): logical_port_name = "Ethernet0" mux_tbl = Table("STATE_DB", "Y_CABLE_STATIC_INFO_TABLE") - rc = post_port_mux_static_info_to_db(logical_port_name, mux_tbl) + y_cable_tbl = Table("STATE_DB", "Y_CABLE_STATIC_INFO_TABLE") + rc = post_port_mux_static_info_to_db(logical_port_name, mux_tbl, y_cable_tbl) assert(rc != -1) def test_y_cable_helper_format_mapping_identifier1(self): @@ -1686,6 +1687,7 @@ def mock_get_asic_id(mock_logical_port_name): port_tbl = {} port_table_keys = {} loopback_keys = {} + grpc_config = {} hw_mux_cable_tbl, hw_mux_cable_tbl_peer = {}, {} y_cable_presence = [True] delete_change_event = [True] @@ -1706,13 +1708,20 @@ def mock_get_asic_id(mock_logical_port_name): port_tbl[asic_index] = swsscommon.Table( test_db[asic_index], "PORT_INFO_TABLE") port_tbl[asic_index].get.return_value = (status, fvs) + grpc_config[asic_index] = swsscommon.Table( + test_db[asic_index], "GRPC_CONFIG") + grpc_config[asic_index].get.return_value = (status, fvs) + fwd_state_response_tbl = {} - rc = init_ports_status_for_y_cable(platform_sfp, platform_chassis, y_cable_presence, state_db, port_tbl, y_cable_tbl, static_tbl, mux_tbl, port_table_keys, loopback_keys, hw_mux_cable_tbl, hw_mux_cable_tbl_peer, stop_event=threading.Event()) + rc = init_ports_status_for_y_cable(platform_sfp, platform_chassis, y_cable_presence, state_db, port_tbl, y_cable_tbl, static_tbl, mux_tbl, port_table_keys, loopback_keys, hw_mux_cable_tbl, hw_mux_cable_tbl_peer, grpc_config, fwd_state_response_tbl, stop_event=threading.Event()) assert(rc == None) @patch('ycable.ycable_utilities.y_cable_helper.logical_port_name_to_physical_port_list', MagicMock(return_value=[0])) @patch('ycable.ycable_utilities.y_cable_helper.y_cable_port_locks', MagicMock(return_value=[0])) + @patch('ycable.ycable_utilities.y_cable_helper.check_mux_cable_port_type', MagicMock(return_value=(True,"active-active"))) + @patch('ycable.ycable_utilities.y_cable_helper.check_identifier_presence_and_setup_channel', MagicMock(return_value=(None))) + @patch('ycable.ycable_utilities.y_cable_helper.process_loopback_interface_and_get_read_side',MagicMock(return_value=0)) @patch('swsscommon.swsscommon.Table') def test_change_ports_status_for_y_cable_change_event(self, mock_swsscommon_table): @@ -1729,25 +1738,29 @@ def mock_get_asic_id(mock_logical_port_name): mock_table.get = MagicMock( side_effect=[(True, (('index', 1), )), (True, (('index', 2), ))]) mock_swsscommon_table.return_value = mock_table - + port_tbl, port_table_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 = {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {} + port_table_keys[0] = ['Ethernet0'] with patch('ycable.ycable_utilities.y_cable_helper.y_cable_platform_sfputil') as patched_util: patched_util.get_asic_id_for_logical_port.return_value = 0 - change_ports_status_for_y_cable_change_event( - logical_port_dict, y_cable_presence, stop_event=threading.Event()) + rc = change_ports_status_for_y_cable_change_event( + logical_port_dict, y_cable_presence, port_tbl, port_table_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, stop_event=threading.Event()) - mock_swsscommon_table.assert_called() + assert(rc == None) - @patch('ycable.ycable_utilities.y_cable_helper.logical_port_name_to_physical_port_list', MagicMock(return_value=[0])) - @patch('ycable.ycable_utilities.y_cable_helper.y_cable_port_locks', MagicMock(return_value=[0])) - @patch('swsscommon.swsscommon.Table') - def test_change_ports_status_for_y_cable_change_event_sfp_removed(self, mock_swsscommon_table): + @patch('ycable.ycable_utilities.y_cable_helper.logical_port_name_to_physical_port_list', MagicMock(return_value=[0])) + @patch('ycable.ycable_utilities.y_cable_helper.y_cable_port_locks', MagicMock(return_value=[0])) + @patch('ycable.ycable_utilities.y_cable_helper.check_mux_cable_port_type', MagicMock(return_value=(True,"active-active"))) + @patch('ycable.ycable_utilities.y_cable_helper.check_identifier_presence_and_setup_channel', MagicMock(return_value=(None))) + @patch('ycable.ycable_utilities.y_cable_helper.process_loopback_interface_and_get_read_side',MagicMock(return_value=0)) + @patch('swsscommon.swsscommon.Table') + def test_change_ports_status_for_y_cable_change_event_sfp_removed(self, mock_swsscommon_table): - mock_logical_port_name = [""] + mock_logical_port_name = [""] - def mock_get_asic_id(mock_logical_port_name): - return 0 + def mock_get_asic_id(mock_logical_port_name): + return 0 y_cable_presence = [True] logical_port_dict = {'Ethernet0': '1'} @@ -1757,17 +1770,20 @@ def mock_get_asic_id(mock_logical_port_name): mock_table.get = MagicMock( side_effect=[(True, (('index', 1), )), (True, (('index', 2), ))]) mock_swsscommon_table.return_value = mock_table + port_tbl, port_table_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 = {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {} + port_table_keys[0] = ['Ethernet0'] with patch('ycable.ycable_utilities.y_cable_helper.y_cable_platform_sfputil') as patched_util: patched_util.get_asic_id_for_logical_port.return_value = 0 - change_ports_status_for_y_cable_change_event( - logical_port_dict, y_cable_presence, stop_event=threading.Event()) + rc = change_ports_status_for_y_cable_change_event( + logical_port_dict, y_cable_presence, port_tbl, port_table_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,stop_event=threading.Event()) - mock_swsscommon_table.assert_called() + assert(rc == None) @patch('ycable.ycable_utilities.y_cable_helper.logical_port_name_to_physical_port_list', MagicMock(return_value=[0])) @patch('ycable.ycable_utilities.y_cable_helper.y_cable_port_locks', MagicMock(return_value=[0])) + @patch('ycable.ycable_utilities.y_cable_helper.process_loopback_interface_and_get_read_side',MagicMock(return_value=0)) @patch('swsscommon.swsscommon.Table') def test_change_ports_status_for_y_cable_change_event_sfp_unknown(self, mock_swsscommon_table): @@ -1784,14 +1800,15 @@ def mock_get_asic_id(mock_logical_port_name): mock_table.get = MagicMock( side_effect=[(True, (('index', 1), )), (True, (('index', 2), ))]) mock_swsscommon_table.return_value = mock_table - + port_tbl, port_table_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 = {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {} + port_table_keys[0] = ['Ethernet0'] with patch('ycable.ycable_utilities.y_cable_helper.y_cable_platform_sfputil') as patched_util: patched_util.get_asic_id_for_logical_port.return_value = 0 - change_ports_status_for_y_cable_change_event( - logical_port_dict, y_cable_presence, stop_event=threading.Event()) + rc = change_ports_status_for_y_cable_change_event( + logical_port_dict, y_cable_presence,port_tbl, port_table_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, stop_event=threading.Event()) - mock_swsscommon_table.assert_called() + assert(rc == None) @patch('swsscommon.swsscommon.Table') @patch('ycable.ycable_utilities.y_cable_helper.y_cable_port_locks', MagicMock(return_value=[0])) @@ -2479,8 +2496,16 @@ def get_nic_temperature(): def test_get_muxcable_static_info(self, platform_sfputil): physical_port = 0 + asic_index = 0 logical_port_name = "Ethernet0" + test_db = "TEST_DB" + y_cable_tbl = {} + status = True + fvs = [('state', "auto"), ('read_side', 2)] + y_cable_tbl[asic_index] = swsscommon.Table( + test_db[asic_index], "Y_CABLE_TABLE") + y_cable_tbl[asic_index].get.return_value = (status, fvs) platform_sfputil.get_asic_id_for_logical_port = 0 swsscommon.Table.return_value.get.return_value = ( True, {"read_side": "1"}) @@ -2512,7 +2537,7 @@ def get_target_cursor_values(self, i, tgt): with patch('ycable.ycable_utilities.y_cable_helper.y_cable_platform_sfputil') as patched_util: patched_util.get_asic_id_for_logical_port.return_value = 0 - rc = get_muxcable_static_info(physical_port, logical_port_name) + rc = get_muxcable_static_info(physical_port, logical_port_name, y_cable_tbl) assert (rc['read_side'] == 'tor1') assert (rc['nic_lane1_precursor1'] == 1) @@ -2556,8 +2581,17 @@ def get_target_cursor_values(self, i, tgt): def test_get_muxcable_static_info_read_side_peer(self, platform_sfputil): physical_port = 0 + asic_index = 0 logical_port_name = "Ethernet0" + test_db = "TEST_DB" + status = True + y_cable_tbl = {} + fvs = [('state', "auto"), ('read_side', 2)] + + y_cable_tbl[asic_index] = swsscommon.Table( + test_db[asic_index], "Y_CABLE_TABLE") + y_cable_tbl[asic_index].get.return_value = (status, fvs) #swsscommon.Table = MagicMock() # this patch is already done as global instance platform_sfputil.get_asic_id_for_logical_port = 0 @@ -2591,7 +2625,7 @@ def get_target_cursor_values(self, i, tgt): with patch('ycable.ycable_utilities.y_cable_helper.y_cable_platform_sfputil') as patched_util: patched_util.get_asic_id_for_logical_port.return_value = 0 - rc = get_muxcable_static_info(physical_port, logical_port_name) + rc = get_muxcable_static_info(physical_port, logical_port_name, y_cable_tbl) assert (rc['read_side'] == 'tor2') assert (rc['nic_lane1_precursor1'] == 1) @@ -2635,8 +2669,17 @@ def get_target_cursor_values(self, i, tgt): def test_get_muxcable_static_info_read_side_peer_exceptions(self, platform_sfputil): physical_port = 0 + asic_index = 0 logical_port_name = "Ethernet0" + test_db = "TEST_DB" + y_cable_tbl = {} + + status = True + fvs = [('state', "auto"), ('read_side', 2)] + y_cable_tbl[asic_index] = swsscommon.Table( + test_db[asic_index], "Y_CABLE_TABLE") + y_cable_tbl[asic_index].get.return_value = (status, fvs) #swsscommon.Table = MagicMock() # this patch is already done as global instance platform_sfputil.get_asic_id_for_logical_port = 0 @@ -2665,7 +2708,7 @@ def get_target_cursor_values(self, i, tgt): with patch('ycable.ycable_utilities.y_cable_helper.y_cable_platform_sfputil') as patched_util: patched_util.get_asic_id_for_logical_port.return_value = 0 - rc = get_muxcable_static_info(physical_port, logical_port_name) + rc = get_muxcable_static_info(physical_port, logical_port_name, y_cable_tbl) assert (rc['read_side'] == 'tor2') assert (rc['nic_lane1_precursor1'] == "N/A") @@ -5520,6 +5563,7 @@ def test_handle_show_mux_switchmode_cmd_arg_tbl_notification_no_port(self, mock_ xcvrd_config_hwmode_swmode_rsp_tbl = mock_swsscommon_table xcvrd_show_hwmode_swmode_cmd_sts_tbl = mock_swsscommon_table xcvrd_show_hwmode_swmode_rsp_tbl = mock_swsscommon_table + hw_mux_cable_tbl = mock_swsscommon_table asic_index = 0 task_download_firmware_thread = {} @@ -5946,6 +5990,7 @@ def test_handle_show_mux_state_cmd_arg_tbl_notification_no_port(self, mock_swssc xcvrd_show_hwmode_dir_cmd_sts_tbl = mock_swsscommon_table xcvrd_show_hwmode_dir_rsp_tbl = mock_swsscommon_table xcvrd_show_hwmode_dir_res_tbl = mock_swsscommon_table + hw_mux_cable_tbl =mock_swsscommon_table port_tbl = mock_swsscommon_table asic_index = 0 @@ -5954,7 +5999,7 @@ def test_handle_show_mux_state_cmd_arg_tbl_notification_no_port(self, mock_swssc fvp = {"state": "active"} rc = handle_show_hwmode_state_cmd_arg_tbl_notification( - fvp, port_tbl, xcvrd_show_hwmode_dir_cmd_sts_tbl, xcvrd_show_hwmode_dir_rsp_tbl, xcvrd_show_hwmode_dir_res_tbl, asic_index, port) + fvp, port_tbl, xcvrd_show_hwmode_dir_cmd_sts_tbl, xcvrd_show_hwmode_dir_rsp_tbl, xcvrd_show_hwmode_dir_res_tbl, hw_mux_cable_tbl, asic_index, port) assert(rc == -1) @patch('swsscommon.swsscommon.Table') @@ -5974,6 +6019,7 @@ def test_handle_show_mux_state_cmd_arg_tbl_notification_else_condition(self, moc xcvrd_show_hwmode_dir_res_tbl = mock_swsscommon_table xcvrd_config_hwmode_state_cmd_sts_tbl = mock_swsscommon_table xcvrd_config_hwmode_state_rsp_tbl = mock_swsscommon_table + hw_mux_cable_tbl =mock_swsscommon_table port_tbl = mock_swsscommon_table asic_index = 0 @@ -5982,7 +6028,7 @@ def test_handle_show_mux_state_cmd_arg_tbl_notification_else_condition(self, moc fvp = {"down_firmware": "null"} rc = handle_show_hwmode_state_cmd_arg_tbl_notification( - fvp, port_tbl, xcvrd_show_hwmode_dir_cmd_sts_tbl, xcvrd_show_hwmode_dir_rsp_tbl, xcvrd_show_hwmode_dir_res_tbl, asic_index, port) + fvp, port_tbl, xcvrd_show_hwmode_dir_cmd_sts_tbl, xcvrd_show_hwmode_dir_rsp_tbl, xcvrd_show_hwmode_dir_res_tbl, hw_mux_cable_tbl, asic_index, port) assert(rc == None) @patch('swsscommon.swsscommon.Table') @@ -6008,6 +6054,7 @@ def test_handle_show_mux_state_cmd_arg_tbl_notification_with_instance_manual(sel xcvrd_config_hwmode_state_cmd_sts_tbl = mock_swsscommon_table xcvrd_config_hwmode_state_rsp_tbl = mock_swsscommon_table port_tbl = mock_swsscommon_table + hw_mux_cable_tbl =mock_swsscommon_table asic_index = 0 task_download_firmware_thread = {} port = "Ethernet0" @@ -6040,7 +6087,7 @@ def get_switching_mode(self): patched_util.get.return_value = PortInstanceHelper() rc = handle_show_hwmode_state_cmd_arg_tbl_notification( - fvp, port_tbl, xcvrd_show_hwmode_dir_cmd_sts_tbl, xcvrd_show_hwmode_dir_rsp_tbl, xcvrd_show_hwmode_dir_res_tbl, asic_index, port) + fvp, port_tbl, xcvrd_show_hwmode_dir_cmd_sts_tbl, xcvrd_show_hwmode_dir_rsp_tbl, xcvrd_show_hwmode_dir_res_tbl, hw_mux_cable_tbl, asic_index, port) assert(rc == None) @patch('swsscommon.swsscommon.Table') @@ -6061,6 +6108,7 @@ def test_handle_show_mux_state_cmd_arg_tbl_notification_no_instance(self, mock_s xcvrd_show_hwmode_dir_cmd_sts_tbl = mock_swsscommon_table xcvrd_show_hwmode_dir_rsp_tbl = mock_swsscommon_table xcvrd_show_hwmode_dir_res_tbl = mock_swsscommon_table + hw_mux_cable_tbl =mock_swsscommon_table port_tbl = mock_swsscommon_table asic_index = 0 @@ -6069,7 +6117,7 @@ def test_handle_show_mux_state_cmd_arg_tbl_notification_no_instance(self, mock_s fvp = {"state": "active"} rc = handle_show_hwmode_state_cmd_arg_tbl_notification( - fvp, port_tbl, xcvrd_show_hwmode_dir_cmd_sts_tbl, xcvrd_show_hwmode_dir_rsp_tbl, xcvrd_show_hwmode_dir_res_tbl, asic_index, port) + fvp, port_tbl, xcvrd_show_hwmode_dir_cmd_sts_tbl, xcvrd_show_hwmode_dir_rsp_tbl, xcvrd_show_hwmode_dir_res_tbl, hw_mux_cable_tbl, asic_index, port) assert(rc == -1) @patch('swsscommon.swsscommon.Table') @@ -6102,6 +6150,10 @@ def test_handle_show_mux_state_cmd_arg_tbl_notification_with_instance_auto_activ port = "Ethernet0" platform_sfputil.get_asic_id_for_logical_port = 0 fvp = {"state": "active"} + hw_mux_cable_tbl = {} + test_db = "TEST_DB" + hw_mux_cable_tbl[asic_index] = swsscommon.Table( + test_db[asic_index], "PORT_INFO_TABLE") with patch('ycable.ycable_utilities.y_cable_helper.y_cable_port_instances') as patched_util: class PortInstanceHelper(): @@ -6129,7 +6181,7 @@ def get_mux_direction(self): patched_util.get.return_value = PortInstanceHelper() rc = handle_show_hwmode_state_cmd_arg_tbl_notification( - fvp, port_tbl, xcvrd_show_hwmode_dir_cmd_sts_tbl, xcvrd_show_hwmode_dir_rsp_tbl, xcvrd_show_hwmode_dir_res_tbl, asic_index, port) + fvp, port_tbl, xcvrd_show_hwmode_dir_cmd_sts_tbl, xcvrd_show_hwmode_dir_rsp_tbl, xcvrd_show_hwmode_dir_res_tbl, hw_mux_cable_tbl, asic_index, port) assert(rc == None) @patch('swsscommon.swsscommon.Table') @@ -6157,9 +6209,13 @@ def test_handle_show_mux_state_cmd_arg_tbl_notification_with_instance_auto_activ fvp = {"state": "active"} swsscommon.Table.return_value.get.return_value = ( True, {"read_side": "2", "state": "active"}) + hw_mux_cable_tbl = {} + test_db = "TEST_DB" + hw_mux_cable_tbl[asic_index] = swsscommon.Table( + test_db[asic_index], "PORT_INFO_TABLE") rc = handle_show_hwmode_state_cmd_arg_tbl_notification( - fvp, port_tbl, xcvrd_show_hwmode_dir_cmd_sts_tbl, xcvrd_show_hwmode_dir_rsp_tbl, xcvrd_show_hwmode_dir_res_tbl, asic_index, port) + fvp, port_tbl, xcvrd_show_hwmode_dir_cmd_sts_tbl, xcvrd_show_hwmode_dir_rsp_tbl, xcvrd_show_hwmode_dir_res_tbl, hw_mux_cable_tbl, asic_index, port) assert(rc == None) @patch('swsscommon.swsscommon.Table') @@ -6187,20 +6243,67 @@ def test_handle_show_mux_state_cmd_arg_tbl_notification_with_instance_auto_activ fvp = {"state": "active"} swsscommon.Table.return_value.get.return_value = ( False, {"read_side": "2", "state": "active"}) + hw_mux_cable_tbl = {} + test_db = "TEST_DB" + hw_mux_cable_tbl[asic_index] = swsscommon.Table( + test_db[asic_index], "PORT_INFO_TABLE") rc = handle_show_hwmode_state_cmd_arg_tbl_notification( - fvp, port_tbl, xcvrd_show_hwmode_dir_cmd_sts_tbl, xcvrd_show_hwmode_dir_rsp_tbl, xcvrd_show_hwmode_dir_res_tbl, asic_index, port) + fvp, port_tbl, xcvrd_show_hwmode_dir_cmd_sts_tbl, xcvrd_show_hwmode_dir_rsp_tbl, xcvrd_show_hwmode_dir_res_tbl, hw_mux_cable_tbl, asic_index, port) assert(rc == -1) + @patch('ycable.ycable_utilities.y_cable_helper.setup_grpc_channel_for_port', MagicMock(return_value=(None,None))) def test_retry_setup_grpc_channel_for_port_incorrect(self): + status = False + fvs = [('state', "auto"), ('read_side', 1)] + Table = MagicMock() + Table.get.return_value = (status, fvs) + swsscommon.Table.return_value.get.return_value = ( + False, {"cable_type": "active-active", "soc_ipv4":"192.168.0.1/32", "state":"active"}) + port_tbl = {} + test_db = "TEST_DB" + asic_index = 0 + port_tbl[asic_index] = swsscommon.Table( + test_db[asic_index], "PORT_INFO_TABLE") + grpc_client , fwd_state_response_tbl = {}, {} + rc = retry_setup_grpc_channel_for_port("Ethernet0", 0, port_tbl, grpc_client, fwd_state_response_tbl) + assert(rc == False) + + @patch('ycable.ycable_utilities.y_cable_helper.setup_grpc_channel_for_port', MagicMock(return_value=(None,None))) + def test_retry_setup_grpc_channel_for_port_correct_none_val(self): + status = True fvs = [('state', "auto"), ('read_side', 1)] Table = MagicMock() Table.get.return_value = (status, fvs) swsscommon.Table.return_value.get.return_value = ( - False, {"read_side": "2"}) - rc = retry_setup_grpc_channel_for_port("Ethernet0", 0) + True, {"cable_type": "active-active", "soc_ipv4":"192.168.0.1/32", "state":"active"}) + port_tbl = {} + test_db = "TEST_DB" + asic_index = 0 + port_tbl[asic_index] = swsscommon.Table( + test_db[asic_index], "PORT_INFO_TABLE") + grpc_client , fwd_state_response_tbl = {}, {} + rc = retry_setup_grpc_channel_for_port("Ethernet0", 0, port_tbl, grpc_client, fwd_state_response_tbl) + assert(rc == False) + + def test_process_loopback_interface_and_get_read_side_rc(self): + + loopback_keys = [["Loopback3|10.212.64.2/3", "Loopback3|2603:1010:100:d::1/128"]] + rc = process_loopback_interface_and_get_read_side(loopback_keys) + assert(rc == 0) + Table = MagicMock() + Table.get.return_value = (status, fvs) + swsscommon.Table.return_value.get.return_value = ( + True, {"cable_type": "active-active", "soc_ipv4":"192.168.0.1/32", "state":"active"}) + port_tbl = {} + test_db = "TEST_DB" + asic_index = 0 + port_tbl[asic_index] = swsscommon.Table( + test_db[asic_index], "PORT_INFO_TABLE") + grpc_client , fwd_state_response_tbl = {}, {} + rc = retry_setup_grpc_channel_for_port("Ethernet0", 0 , port_tbl, grpc_client, fwd_state_response_tbl ) assert(rc == False) @patch('ycable.ycable_utilities.y_cable_helper.setup_grpc_channel_for_port', MagicMock(return_value=(True,True))) @@ -6212,7 +6315,13 @@ def test_retry_setup_grpc_channel_for_port_correct(self): Table.get.return_value = (status, fvs) swsscommon.Table.return_value.get.return_value = ( True, {"cable_type": "active-active", "soc_ipv4":"192.168.0.1/32", "state":"active"}) - rc = retry_setup_grpc_channel_for_port("Ethernet0", 0) + port_tbl = {} + test_db = "TEST_DB" + asic_index = 0 + port_tbl[asic_index] = swsscommon.Table( + test_db[asic_index], "PORT_INFO_TABLE") + grpc_client , fwd_state_response_tbl = {}, {} + rc = retry_setup_grpc_channel_for_port("Ethernet0", 0, port_tbl, grpc_client, fwd_state_response_tbl) assert(rc == True) @patch('ycable.ycable_utilities.y_cable_helper.setup_grpc_channel_for_port', MagicMock(return_value=(None,None))) @@ -6224,7 +6333,13 @@ def test_retry_setup_grpc_channel_for_port_correct_none_val(self): Table.get.return_value = (status, fvs) swsscommon.Table.return_value.get.return_value = ( True, {"cable_type": "active-active", "soc_ipv4":"192.168.0.1/32", "state":"active"}) - rc = retry_setup_grpc_channel_for_port("Ethernet0", 0) + port_tbl = {} + test_db = "TEST_DB" + asic_index = 0 + port_tbl[asic_index] = swsscommon.Table( + test_db[asic_index], "PORT_INFO_TABLE") + grpc_client , fwd_state_response_tbl = {}, {} + rc = retry_setup_grpc_channel_for_port("Ethernet0", 0, port_tbl, grpc_client, fwd_state_response_tbl) assert(rc == False) def test_process_loopback_interface_and_get_read_side_rc(self): @@ -6271,10 +6386,12 @@ def test_check_identifier_presence_and_setup_channel(self): test_db[asic_index], "HW_TABLE1") hw_mux_cable_tbl_peer[asic_index] = swsscommon.Table( test_db[asic_index], "HW_TABLE2") + grpc_client , fwd_state_response_tbl = {}, {} mux_tbl[asic_index] = swsscommon.Table( test_db[asic_index], "MUX_INFO_TABLE") - rc = check_identifier_presence_and_setup_channel("Ethernet0", port_tbl, hw_mux_cable_tbl, hw_mux_cable_tbl_peer, asic_index, read_side, mux_tbl, y_cable_presence) + rc = check_identifier_presence_and_setup_channel("Ethernet0", port_tbl, hw_mux_cable_tbl, hw_mux_cable_tbl_peer, asic_index, read_side, mux_tbl, y_cable_presence, grpc_client, fwd_state_response_tbl) + assert(rc == None) @patch('ycable.ycable_utilities.y_cable_helper.logical_port_name_to_physical_port_list', MagicMock(return_value=[0])) @@ -6304,10 +6421,12 @@ def test_check_identifier_presence_and_setup_channel_with_mock(self): test_db[asic_index], "HW_TABLE1") hw_mux_cable_tbl_peer[asic_index] = swsscommon.Table( test_db[asic_index], "HW_TABLE2") + grpc_client , fwd_state_response_tbl = {}, {} + mux_tbl[asic_index] = swsscommon.Table( test_db[asic_index], "MUX_INFO_TABLE") - rc = check_identifier_presence_and_setup_channel("Ethernet0", port_tbl, hw_mux_cable_tbl, hw_mux_cable_tbl_peer, asic_index, read_side, mux_tbl, y_cable_presence) + rc = check_identifier_presence_and_setup_channel("Ethernet0", port_tbl, hw_mux_cable_tbl, hw_mux_cable_tbl_peer, asic_index, read_side, mux_tbl, y_cable_presence, grpc_client, fwd_state_response_tbl) assert(rc == None) @@ -6341,19 +6460,35 @@ def test_check_identifier_presence_and_setup_channel_with_mock_not_none(self): test_db[asic_index], "HW_TABLE1") hw_mux_cable_tbl_peer[asic_index] = swsscommon.Table( test_db[asic_index], "HW_TABLE2") + grpc_client , fwd_state_response_tbl = {}, {} mux_tbl[asic_index] = swsscommon.Table( test_db[asic_index], "MUX_INFO_TABLE") - rc = check_identifier_presence_and_setup_channel("Ethernet0", port_tbl, hw_mux_cable_tbl, hw_mux_cable_tbl_peer, asic_index, read_side, mux_tbl, y_cable_presence) + rc = check_identifier_presence_and_setup_channel("Ethernet0", port_tbl, hw_mux_cable_tbl, hw_mux_cable_tbl_peer, asic_index, read_side, mux_tbl, y_cable_presence, grpc_client, fwd_state_response_tbl) + assert(rc == None) @patch('proto_out.linkmgr_grpc_driver_pb2_grpc.DualToRActiveStub', MagicMock(return_value=True)) def test_setup_grpc_channel_for_port(self): + status = True + fvs = [('state', "auto"), ('read_side', 1), ('cable_type','active-standby'), ('soc_ipv4','192.168.0.1')] + grpc_client , fwd_state_response_tbl = {}, {} + asic_index = 0 + Table = MagicMock() + Table.get.return_value = (status, fvs) + #swsscommon.Table.return_value.get.return_value = ( + # True, { 'config', {'type': 'secure'}}) + swsscommon.Table.return_value.get.return_value = ( + True, {"config": "1"}) + test_db = "TEST_DB" + asic_index = 0 + grpc_client[asic_index] = swsscommon.Table( + test_db[asic_index], "PORT_INFO_TABLE") with patch('ycable.ycable_utilities.y_cable_helper.y_cable_platform_sfputil') as patched_util: patched_util.get_asic_id_for_logical_port.return_value = 0 - (channel, stub) = setup_grpc_channel_for_port("Ethernet0", "192.168.0.1") + (channel, stub) = setup_grpc_channel_for_port("Ethernet0", "192.168.0.1", asic_index, grpc_client, fwd_state_response_tbl, False) assert(stub == True) assert(channel != None) @@ -6374,7 +6509,8 @@ def test_setup_grpc_channels(self): patched_util.logical.return_value = ['Ethernet0', 'Ethernet4'] patched_util.get_asic_id_for_logical_port.return_value = 0 - rc = setup_grpc_channels(stop_event) + loopback_keys, hw_mux_cable_tbl, hw_mux_cable_tbl_peer, port_tbl, loopback_tbl, port_table_keys, grpc_client, fwd_state_response_tbl = {}, {}, {}, {}, {}, {}, {}, {} + rc = setup_grpc_channels(stop_event, loopback_keys, hw_mux_cable_tbl, hw_mux_cable_tbl_peer, port_tbl, loopback_tbl, port_table_keys, grpc_client, fwd_state_response_tbl) assert(rc == None) @@ -6769,12 +6905,16 @@ def test_apply_grpc_secrets_configuration(self, open): parsed_data = {'GRPCCLIENT': {'config': {'type': 'secure', 'auth_level': 'server', 'log_level': 'info'}, 'certs': {'client_crt': 'one.crt', 'client_key': 'one.key', 'ca_crt': 'ss.crt', 'grpc_ssl_credential': 'jj.tsl'}}} - mock_file = MagicMock() - open.return_value = mock_file + asic_index = 0 + grpc_client = {} + test_db = {} + test_db[asic_index] = 'xyz' + grpc_client[asic_index] = swsscommon.Table( + test_db[asic_index], "PORT_INFO_TABLE") #json_load.return_value = parsed_data with patch('json.load') as patched_util: patched_util.return_value = parsed_data - rc = apply_grpc_secrets_configuration(None) + rc = apply_grpc_secrets_configuration(None, grpc_client) assert(rc == None) @@ -6977,3 +7117,6 @@ def test_get_muxcable_info_for_active_active(self): assert(rc['peer_mux_direction'] == 'unknown') assert(rc['mux_direction_probe_count'] == 'unknown') assert(rc['peer_mux_direction_probe_count'] == 'unknown') + + + diff --git a/sonic-ycabled/tests/test_ycable.py b/sonic-ycabled/tests/test_ycable.py index 864b40865..9ffac1066 100644 --- a/sonic-ycabled/tests/test_ycable.py +++ b/sonic-ycabled/tests/test_ycable.py @@ -163,7 +163,7 @@ def test_post_port_mux_info_to_db(self): mux_tbl = {} test_db = "TEST_DB" status = True - fvs = [('state', "auto"), ('read_side', 1)] + fvs = [('state', "auto"), ('read_side', 1), ('soc_ipv4', '192.168.0.1/32')] y_cable_tbl[asic_index] = swsscommon.Table( test_db[asic_index], "Y_CABLE_TABLE") y_cable_tbl[asic_index].get.return_value = (status, fvs) @@ -209,7 +209,8 @@ def test_post_port_mux_info_to_db(self): def test_post_port_mux_static_info_to_db(self): logical_port_name = "Ethernet0" mux_tbl = Table("STATE_DB", y_cable_helper.MUX_CABLE_STATIC_INFO_TABLE) - rc = post_port_mux_static_info_to_db(logical_port_name, mux_tbl) + y_cable_tbl = Table("STATE_DB", "Y_CABLE_STATIC_INFO_TABLE") + rc = post_port_mux_static_info_to_db(logical_port_name, mux_tbl, y_cable_tbl) assert(rc != -1) def test_y_cable_helper_format_mapping_identifier1(self): @@ -308,7 +309,8 @@ def test_handle_state_update_task(self): fvp_dict = {} y_cable_presence = False stopping_event = None - rc = handle_state_update_task(port, fvp_dict, y_cable_presence, stopping_event) + 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, stopping_event) assert(rc == None) @@ -365,3 +367,4 @@ def test_ycable_helper_class_run_loop_with_exception(self): assert("sonic-ycabled/ycable/ycable_utilities/y_cable_helper.py" in str(trace)) assert("swsscommon.Select" in str(trace)) + diff --git a/sonic-ycabled/ycable/ycable.py b/sonic-ycabled/ycable/ycable.py index fd3e0ecf5..f5ebbd020 100644 --- a/sonic-ycabled/ycable/ycable.py +++ b/sonic-ycabled/ycable/ycable.py @@ -81,13 +81,13 @@ def detect_port_in_error_status(logical_port_name, status_tbl): else: return False -def handle_state_update_task(port, fvp_dict, y_cable_presence, stopping_event): +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, stopping_event): port_dict = {} port_dict[port] = fvp_dict.get('status', None) y_cable_helper.change_ports_status_for_y_cable_change_event( - port_dict, y_cable_presence, stopping_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, stopping_event) # # Helper classes =============================================================== @@ -207,7 +207,8 @@ def task_worker(self, stopping_event, sfp_error_event, y_cable_presence): if not fvp_dict: continue - handle_state_update_task(port, fvp_dict, y_cable_presence, stopping_event) + # 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(), stopping_event) def run(self): if self.task_stopping_event.is_set(): @@ -338,7 +339,7 @@ def init(self): # Init port y_cable status table y_cable_helper.init_ports_status_for_y_cable( - platform_sfputil, platform_chassis, self.y_cable_presence, self.table_helper.get_state_db(), self.table_helper.get_port_tbl(), self.table_helper.get_y_cable_tbl(), self.table_helper.get_static_tbl(), self.table_helper.get_mux_tbl(), self.table_helper.port_table_keys, self.table_helper.loopback_keys , self.table_helper.get_hw_mux_cable_tbl(), self.table_helper.get_hw_mux_cable_tbl_peer(), self.stop_event, is_vs) + platform_sfputil, platform_chassis, self.y_cable_presence, self.table_helper.get_state_db(), self.table_helper.get_port_tbl(), self.table_helper.get_y_cable_tbl(), self.table_helper.get_static_tbl(), self.table_helper.get_mux_tbl(), self.table_helper.port_table_keys, 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_grpc_config_tbl(), self.table_helper.get_fwd_state_response_tbl(), self.stop_event, is_vs) # Deinitialize daemon def deinit(self): diff --git a/sonic-ycabled/ycable/ycable_utilities/y_cable_helper.py b/sonic-ycabled/ycable/ycable_utilities/y_cable_helper.py index 527f8e0f1..1eeae77a2 100644 --- a/sonic-ycabled/ycable/ycable_utilities/y_cable_helper.py +++ b/sonic-ycabled/ycable/ycable_utilities/y_cable_helper.py @@ -3,6 +3,7 @@ helper utlities configuring y_cable for xcvrd daemon """ +import asyncio import datetime import ipaddress import json @@ -345,18 +346,11 @@ def wrapper(*args, **kwargs): return wrapper -def retry_setup_grpc_channel_for_port(port, asic_index): +def retry_setup_grpc_channel_for_port(port, asic_index, port_tbl, grpc_client, fwd_state_response_tbl): global grpc_port_stubs global grpc_port_channels - config_db, port_tbl = {}, {} - namespaces = multi_asic.get_front_end_namespaces() - for namespace in namespaces: - asic_id = multi_asic.get_asic_index_from_namespace(namespace) - config_db[asic_id] = daemon_base.db_connect("CONFIG_DB", namespace) - port_tbl[asic_id] = swsscommon.Table(config_db[asic_id], "MUX_CABLE") - (status, fvs) = port_tbl[asic_index].get(port) if status is False: helper_logger.log_warning( @@ -372,7 +366,7 @@ def retry_setup_grpc_channel_for_port(port, asic_index): if soc_ipv4_full is not None: soc_ipv4 = soc_ipv4_full.split('/')[0] - channel, stub = setup_grpc_channel_for_port(port, soc_ipv4) + channel, stub = setup_grpc_channel_for_port(port, soc_ipv4, asic_index, grpc_client, fwd_state_response_tbl, False) if channel is None or stub is None: helper_logger.log_notice( "stub is None, while reattempt setting up channels did not work {}".format(port)) @@ -382,20 +376,12 @@ def retry_setup_grpc_channel_for_port(port, asic_index): grpc_port_stubs[port] = stub return True -def apply_grpc_secrets_configuration(SECRETS_PATH): +def apply_grpc_secrets_configuration(SECRETS_PATH, grpc_config): f = open(SECRETS_PATH, 'rb') parsed_data = json.load(f) - config_db, grpc_config = {}, {} - namespaces = multi_asic.get_front_end_namespaces() - for namespace in namespaces: - asic_id = multi_asic.get_asic_index_from_namespace(namespace) - config_db[asic_id] = daemon_base.db_connect("CONFIG_DB", namespace) - grpc_config[asic_id] = swsscommon.Table(config_db[asic_id], "GRPCCLIENT") - - asic_index = multi_asic.get_asic_index_from_namespace(DEFAULT_NAMESPACE) grpc_client_config = parsed_data.get("GRPCCLIENT", None) if grpc_client_config is not None: @@ -472,20 +458,7 @@ def connect_channel(channel, stub, port): else: break -def create_channel(type, level, kvp, soc_ip, port): - - - appl_db = {} - fwd_state_response_tbl = {} - namespaces = multi_asic.get_front_end_namespaces() - for namespace in namespaces: - # Open a handle to the Application database, in all namespaces - asic_id = multi_asic.get_asic_index_from_namespace(namespace) - appl_db[asic_id] = daemon_base.db_connect("APPL_DB", namespace) - fwd_state_response_tbl[asic_id] = swsscommon.Table( - appl_db[asic_id], "FORWARDING_STATE_RESPONSE") - - asic_index = y_cable_platform_sfputil.get_asic_id_for_logical_port(port) +def create_channel(type, level, kvp, soc_ip, port, asic_index, fwd_state_response_tbl, is_async): # Helper callback to get an channel connectivity state def wait_for_state_change(channel_connectivity): @@ -522,15 +495,23 @@ def wait_for_state_change(channel_connectivity): GRPC_CLIENT_OPTIONS.append(('grpc.ssl_target_name_override', '{}'.format(target_name))) - channel = grpc.secure_channel("{}:{}".format(soc_ip, GRPC_PORT), credential, options=GRPC_CLIENT_OPTIONS) + if is_async: + channel = grpc.aio.secure_channel("{}:{}".format(soc_ip, GRPC_PORT), credential, options=GRPC_CLIENT_OPTIONS) + else: + channel = grpc.secure_channel("{}:{}".format(soc_ip, GRPC_PORT), credential, options=GRPC_CLIENT_OPTIONS) + + else: - channel = grpc.insecure_channel("{}:{}".format(soc_ip, GRPC_PORT), options=GRPC_CLIENT_OPTIONS) + if is_async: + channel = grpc.aio.insecure_channel("{}:{}".format(soc_ip, GRPC_PORT), credential, options=GRPC_CLIENT_OPTIONS) + else: + channel = grpc.insecure_channel("{}:{}".format(soc_ip, GRPC_PORT), options=GRPC_CLIENT_OPTIONS) stub = linkmgr_grpc_driver_pb2_grpc.DualToRActiveStub(channel) - if channel is not None: + if not is_async and channel is not None: channel.subscribe(wait_for_state_change) #connect_channel(channel, stub, port) @@ -542,7 +523,7 @@ def wait_for_state_change(channel_connectivity): return channel, stub -def setup_grpc_channel_for_port(port, soc_ip): +def setup_grpc_channel_for_port(port, soc_ip, asic_index, grpc_config, fwd_state_response_tbl, is_async): """ Dummy values for lab for now @@ -558,14 +539,6 @@ def setup_grpc_channel_for_port(port, soc_ip): """ helper_logger.log_notice("Setting up gRPC channel for RPC's {} {}".format(port,soc_ip)) - config_db,grpc_config = {}, {} - namespaces = multi_asic.get_front_end_namespaces() - for namespace in namespaces: - asic_id = multi_asic.get_asic_index_from_namespace(namespace) - config_db[asic_id] = daemon_base.db_connect("CONFIG_DB", namespace) - grpc_config[asic_id] = swsscommon.Table(config_db[asic_id], "GRPCCLIENT") - - asic_index = y_cable_platform_sfputil.get_asic_id_for_logical_port(port) #if no config from config DB, treat channel to be as insecure type = "insecure" @@ -592,7 +565,7 @@ def setup_grpc_channel_for_port(port, soc_ip): kvp = dict(fvs) - channel, stub = create_channel(type, level, kvp, soc_ip, port) + channel, stub = create_channel(type, level, kvp, soc_ip, port, asic_index, fwd_state_response_tbl, is_async) if stub is None: helper_logger.log_warning("stub was not setup for gRPC soc ip {} port {}, no gRPC soc server running ?".format(soc_ip, port)) @@ -663,8 +636,7 @@ def process_loopback_interface_and_get_read_side(loopback_keys): return -1 -def check_identifier_presence_and_setup_channel(logical_port_name, port_tbl, hw_mux_cable_tbl, hw_mux_cable_tbl_peer, asic_index, read_side, mux_tbl, y_cable_presence): - +def check_identifier_presence_and_setup_channel(logical_port_name, port_tbl, hw_mux_cable_tbl, hw_mux_cable_tbl_peer, asic_index, read_side, mux_tbl, y_cable_presence, grpc_client, fwd_state_response_tbl): global grpc_port_stubs global grpc_port_channels @@ -701,7 +673,7 @@ def check_identifier_presence_and_setup_channel(logical_port_name, port_tbl, hw_ if prev_channel is not None and prev_stub is not None: return - channel, stub = setup_grpc_channel_for_port(logical_port_name, soc_ipv4) + channel, stub = setup_grpc_channel_for_port(logical_port_name, soc_ipv4, asic_index, grpc_client, fwd_state_response_tbl, False) post_port_mux_info_to_db(logical_port_name, mux_tbl, asic_index, hw_mux_cable_tbl, 'pseudo-cable') if channel is not None: grpc_port_channels[logical_port_name] = channel @@ -726,7 +698,7 @@ def check_identifier_presence_and_setup_channel(logical_port_name, port_tbl, hw_ "DAC cable logical to physical port mapping returned more than one physical ports while Channel setup Port {}".format(logical_port_name)) -def setup_grpc_channels(stop_event): +def setup_grpc_channels(stop_event, loopback_keys, hw_mux_cable_tbl, hw_mux_cable_tbl_peer, port_tbl, loopback_tbl, port_table_keys, grpc_client, fwd_state_response_tbl): global read_side helper_logger.log_debug("Y_CABLE_DEBUG:setting up channels for active-active") @@ -756,7 +728,7 @@ def setup_grpc_channels(stop_event): if read_side == -1: read_side = process_loopback_interface_and_get_read_side(loopback_keys) if os.path.isfile(SECRETS_PATH): - apply_grpc_secrets_configuration(SECRETS_PATH) + apply_grpc_secrets_configuration(SECRETS_PATH, grpc_client) helper_logger.log_debug("Y_CABLE_DEBUG:while setting up grpc channels read side = {}".format(read_side)) @@ -776,7 +748,7 @@ def setup_grpc_channels(stop_event): if logical_port_name in port_table_keys[asic_index]: check_identifier_presence_and_setup_channel( - logical_port_name, port_tbl, hw_mux_cable_tbl, hw_mux_cable_tbl_peer, asic_index, read_side, mux_tbl, y_cable_presence) + logical_port_name, port_tbl, hw_mux_cable_tbl, hw_mux_cable_tbl_peer, asic_index, read_side, mux_tbl, y_cable_presence, grpc_client, fwd_state_response_tbl) else: # This port does not exist in Port table of config but is present inside # logical_ports after loading the port_mappings from port_config_file @@ -1175,7 +1147,7 @@ def create_tables_and_insert_mux_unknown_entries(state_db, y_cable_tbl, static_t read_y_cable_and_update_statedb_port_tbl( logical_port_name, y_cable_tbl[asic_index]) post_port_mux_static_info_to_db( - logical_port_name, static_tbl[asic_index]) + logical_port_name, static_tbl[asic_index], y_cable_tbl) def check_identifier_presence_and_update_mux_table_entry(state_db, port_tbl, y_cable_tbl, static_tbl, mux_tbl, asic_index, logical_port_name, y_cable_presence): @@ -1281,7 +1253,7 @@ def check_identifier_presence_and_update_mux_table_entry(state_db, port_tbl, y_c read_y_cable_and_update_statedb_port_tbl( logical_port_name, y_cable_tbl[asic_index]) post_port_mux_static_info_to_db( - logical_port_name, static_tbl[asic_index]) + logical_port_name, static_tbl[asic_index], y_cable_tbl) else: # first create the state db y cable table and then fill in the entry @@ -1292,7 +1264,7 @@ def check_identifier_presence_and_update_mux_table_entry(state_db, port_tbl, y_c post_port_mux_info_to_db( logical_port_name, mux_tbl, asic_index, y_cable_tbl, 'active-standby') post_port_mux_static_info_to_db( - logical_port_name, static_tbl[asic_index]) + logical_port_name, static_tbl[asic_index], y_cable_tbl) else: helper_logger.log_warning( "Error: Could not get transceiver info dict Y cable port {} while inserting entries".format(logical_port_name)) @@ -1363,7 +1335,7 @@ def check_identifier_presence_and_delete_mux_table_entry(state_db, port_tbl, asi "Error: Retreived multiple ports for a Y cable port {} while delete entries".format(logical_port_name)) -def init_ports_status_for_y_cable(platform_sfp, platform_chassis, y_cable_presence, state_db ,port_tbl, y_cable_tbl, static_tbl, mux_tbl, port_table_keys, loopback_keys , hw_mux_cable_tbl, hw_mux_cable_tbl_peer, stop_event=threading.Event(), is_vs=False): +def init_ports_status_for_y_cable(platform_sfp, platform_chassis, y_cable_presence, state_db ,port_tbl, y_cable_tbl, static_tbl, mux_tbl, port_table_keys, loopback_keys , hw_mux_cable_tbl, hw_mux_cable_tbl_peer, grpc_client, fwd_state_response_tbl, stop_event=threading.Event(), is_vs=False): global y_cable_platform_sfputil global y_cable_platform_chassis global y_cable_port_instances @@ -1379,7 +1351,7 @@ def init_ports_status_for_y_cable(platform_sfp, platform_chassis, y_cable_presen if read_side == -1: read_side = process_loopback_interface_and_get_read_side(loopback_keys) if os.path.isfile(SECRETS_PATH): - apply_grpc_secrets_configuration(SECRETS_PATH) + apply_grpc_secrets_configuration(SECRETS_PATH, grpc_client) # Init PORT_STATUS table if ports are on Y cable logical_port_list = y_cable_platform_sfputil.logical @@ -1403,7 +1375,7 @@ def init_ports_status_for_y_cable(platform_sfp, platform_chassis, y_cable_presen if status and cable_type == "active-active": grpc_port_stats[logical_port_name] = {} check_identifier_presence_and_setup_channel( - logical_port_name, port_tbl, hw_mux_cable_tbl, hw_mux_cable_tbl_peer, asic_index, read_side, mux_tbl, y_cable_presence) + logical_port_name, port_tbl, hw_mux_cable_tbl, hw_mux_cable_tbl_peer, asic_index, read_side, mux_tbl, y_cable_presence, grpc_client, fwd_state_response_tbl) else: # This port does not exist in Port table of config but is present inside # logical_ports after loading the port_mappings from port_config_file @@ -1412,45 +1384,16 @@ def init_ports_status_for_y_cable(platform_sfp, platform_chassis, y_cable_presen "Could not retreive port inside config_db PORT table {} for Y-Cable initiation".format(logical_port_name)) -def change_ports_status_for_y_cable_change_event(port_dict, y_cable_presence, stop_event=threading.Event()): - # Connect to CONFIG_DB and create port status table inside state_db +def change_ports_status_for_y_cable_change_event(port_dict, y_cable_presence, port_tbl, port_table_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, stop_event=threading.Event()): + global read_side - config_db, state_db, port_tbl, y_cable_tbl = {}, {}, {}, {} - static_tbl, mux_tbl = {}, {} - port_table_keys = {} delete_change_event = [False] - loopback_tbl= {} - loopback_keys = {} - hw_mux_cable_tbl = {} - hw_mux_cable_tbl_peer = {} - # Get the namespaces in the platform - namespaces = multi_asic.get_front_end_namespaces() - # Get the keys from PORT table inside config db to prepare check for mux_cable identifier - for namespace in namespaces: - asic_id = multi_asic.get_asic_index_from_namespace(namespace) - config_db[asic_id] = daemon_base.db_connect("CONFIG_DB", namespace) - port_tbl[asic_id] = swsscommon.Table(config_db[asic_id], "MUX_CABLE") - port_table_keys[asic_id] = port_tbl[asic_id].getKeys() - loopback_tbl[asic_id] = swsscommon.Table( - config_db[asic_id], "LOOPBACK_INTERFACE") - loopback_keys[asic_id] = loopback_tbl[asic_id].getKeys() - state_db[asic_id] = daemon_base.db_connect("STATE_DB", namespace) - hw_mux_cable_tbl[asic_id] = swsscommon.Table( - state_db[asic_id], swsscommon.STATE_HW_MUX_CABLE_TABLE_NAME) - hw_mux_cable_tbl_peer[asic_id] = swsscommon.Table( - state_db[asic_id], "HW_MUX_CABLE_TABLE_PEER") - y_cable_tbl[asic_id] = swsscommon.Table( - state_db[asic_id], swsscommon.STATE_HW_MUX_CABLE_TABLE_NAME) - static_tbl[asic_id] = swsscommon.Table( - state_db[asic_id], MUX_CABLE_STATIC_INFO_TABLE) - mux_tbl[asic_id] = swsscommon.Table( - state_db[asic_id], MUX_CABLE_INFO_TABLE) if read_side == -1: read_side = process_loopback_interface_and_get_read_side(loopback_keys) if os.path.isfile(SECRETS_PATH): - apply_grpc_secrets_configuration(SECRETS_PATH) + apply_grpc_secrets_configuration(SECRETS_PATH, grpc_client) # Init PORT_STATUS table if ports are on Y cable and an event is received @@ -1473,7 +1416,7 @@ def change_ports_status_for_y_cable_change_event(port_dict, y_cable_presence, st state_db, port_tbl, y_cable_tbl, static_tbl, mux_tbl, asic_index, logical_port_name, y_cable_presence) if status and cable_type == "active-active": check_identifier_presence_and_setup_channel( - logical_port_name, port_tbl, hw_mux_cable_tbl, hw_mux_cable_tbl_peer, asic_index, read_side, mux_tbl, y_cable_presence) + logical_port_name, port_tbl, hw_mux_cable_tbl, hw_mux_cable_tbl_peer, asic_index, read_side, mux_tbl, y_cable_presence, grpc_client, fwd_state_response_tbl) elif value == SFP_STATUS_REMOVED: helper_logger.log_info("Got SFP deleted ycable event") check_identifier_presence_and_delete_mux_table_entry( @@ -1496,12 +1439,15 @@ def change_ports_status_for_y_cable_change_event(port_dict, y_cable_presence, st if y_cable_presence[0] is True and delete_change_event[0] is True: y_cable_presence[:] = [False] + state_db = {}, + yc_hw_mux_cable_table = {} for namespace in namespaces: asic_id = multi_asic.get_asic_index_from_namespace( namespace) - y_cable_tbl[asic_id] = swsscommon.Table( + state_db[asic_id] = daemon_base.db_connect("STATE_DB", namespace) + yc_hw_mux_cable_table[asic_id] = swsscommon.Table( state_db[asic_id], swsscommon.STATE_HW_MUX_CABLE_TABLE_NAME) - y_cable_table_size = len(y_cable_tbl[asic_id].getKeys()) + y_cable_table_size = len(yc_hw_mux_cable_table[asic_id].getKeys()) if y_cable_table_size > 0: y_cable_presence[:] = [True] break @@ -2109,23 +2055,15 @@ def get_muxcable_info(physical_port, logical_port_name, mux_tbl, asic_index, y_c return mux_info_dict -def get_muxcable_static_info(physical_port, logical_port_name): +def get_muxcable_static_info(physical_port, logical_port_name, y_cable_tbl): mux_static_info_dict = {} - y_cable_tbl, state_db = {}, {} port_instance = y_cable_port_instances.get(physical_port) if port_instance is None: helper_logger.log_error("Error: Could not get port instance for muxcable info for Y cable port {}".format(logical_port_name)) return -1 - namespaces = multi_asic.get_front_end_namespaces() - for namespace in namespaces: - asic_id = multi_asic.get_asic_index_from_namespace(namespace) - state_db[asic_id] = daemon_base.db_connect("STATE_DB", namespace) - y_cable_tbl[asic_id] = swsscommon.Table( - state_db[asic_id], swsscommon.STATE_HW_MUX_CABLE_TABLE_NAME) - asic_index = y_cable_platform_sfputil.get_asic_id_for_logical_port( logical_port_name) if asic_index is None: @@ -2138,6 +2076,7 @@ def get_muxcable_static_info(physical_port, logical_port_name): helper_logger.log_warning("Could not retreive fieldvalue pairs for {}, inside state_db table {}".format( logical_port_name, y_cable_tbl[asic_index].getTableName())) return -1 + mux_port_dict = dict(fvs) read_side = int(mux_port_dict.get("read_side")) @@ -2302,7 +2241,7 @@ def post_port_mux_info_to_db(logical_port_name, mux_tbl, asic_index, y_cable_tbl return -1 -def post_port_mux_static_info_to_db(logical_port_name, static_table): +def post_port_mux_static_info_to_db(logical_port_name, static_table, y_cable_tbl): physical_port_list = logical_port_name_to_physical_port_list( logical_port_name) @@ -2321,7 +2260,7 @@ def post_port_mux_static_info_to_db(logical_port_name, static_table): helper_logger.log_warning("Error: trying to post mux static info without presence of port {}".format(logical_port_name)) mux_static_info_dict = get_muxcable_static_info_without_presence() else: - mux_static_info_dict = get_muxcable_static_info(physical_port, logical_port_name) + mux_static_info_dict = get_muxcable_static_info(physical_port, logical_port_name, y_cable_tbl) if mux_static_info_dict is not None and mux_static_info_dict != -1: @@ -3235,9 +3174,8 @@ def handle_config_hwmode_state_cmd_arg_tbl_notification(fvp, xcvrd_config_hwmode helper_logger.log_error("Error: Wrong input param for cli command config mux hwmode state active/standby logical port {}".format(port)) set_result_and_delete_port('result', 'False', xcvrd_config_hwmode_state_cmd_sts_tbl[asic_index], xcvrd_config_hwmode_state_rsp_tbl[asic_index], port) -def handle_show_hwmode_state_cmd_arg_tbl_notification(fvp, port_tbl, xcvrd_show_hwmode_dir_cmd_sts_tbl, xcvrd_show_hwmode_dir_rsp_tbl, xcvrd_show_hwmode_dir_res_tbl, asic_index, port): +def handle_show_hwmode_state_cmd_arg_tbl_notification(fvp, port_tbl, xcvrd_show_hwmode_dir_cmd_sts_tbl, xcvrd_show_hwmode_dir_rsp_tbl, xcvrd_show_hwmode_dir_res_tbl, hw_mux_cable_tbl, asic_index, port): state_db = {} - hw_mux_cable_tbl = {} fvp_dict = dict(fvp) @@ -3318,15 +3256,9 @@ def handle_show_hwmode_state_cmd_arg_tbl_notification(fvp, port_tbl, xcvrd_show_ elif cable_status and cable_type == "active-active": - namespaces = multi_asic.get_front_end_namespaces() - # Get the keys from PORT table inside config db to prepare check for mux_cable identifier - for namespace in namespaces: - asic_id = multi_asic.get_asic_index_from_namespace(namespace) - state_db[asic_id] = daemon_base.db_connect("STATE_DB", namespace) - hw_mux_cable_tbl[asic_id] = swsscommon.Table( - state_db[asic_id], swsscommon.STATE_HW_MUX_CABLE_TABLE_NAME) (status, fv) = hw_mux_cable_tbl[asic_index].get(port) + if status is False: helper_logger.log_warning("Could not retreive fieldvalue pairs for {}, inside state_db table while responding to cli cmd show mux status {}".format( port, hw_mux_cable_tbl[asic_index].getTableName())) @@ -3447,7 +3379,7 @@ def parse_grpc_response_forwarding_state(ret, response, read_side, port): return (self_state, peer_state) -def handle_fwd_state_command_grpc_notification(fvp_m, hw_mux_cable_tbl, fwd_state_response_tbl, asic_index, port, appl_db): +def handle_fwd_state_command_grpc_notification(fvp_m, hw_mux_cable_tbl, fwd_state_response_tbl, asic_index, port, appl_db, port_tbl, grpc_client): helper_logger.log_debug("Y_CABLE_DEBUG:recevied the notification fwd state port {}".format(port)) fvp_dict = dict(fvp_m) @@ -3476,7 +3408,7 @@ def handle_fwd_state_command_grpc_notification(fvp_m, hw_mux_cable_tbl, fwd_stat stub = grpc_port_stubs.get(port, None) if stub is None: helper_logger.log_notice("stub is None for getting admin port forwarding state RPC port {}".format(port)) - retry_setup_grpc_channel_for_port(port, asic_index) + retry_setup_grpc_channel_for_port(port, asic_index, port_tbl, grpc_client, fwd_state_response_tbl) stub = grpc_port_stubs.get(port, None) if stub is None: helper_logger.log_warning( @@ -3511,7 +3443,7 @@ def handle_fwd_state_command_grpc_notification(fvp_m, hw_mux_cable_tbl, fwd_stat helper_logger.log_warning("command key not present in the notification fwd state handling port {}".format(port)) -def handle_hw_mux_cable_table_grpc_notification(fvp, hw_mux_cable_tbl, asic_index, grpc_metrics_tbl, peer, port): +def handle_hw_mux_cable_table_grpc_notification(fvp, hw_mux_cable_tbl, asic_index, grpc_metrics_tbl, peer, port, port_tbl, grpc_client, fwd_state_response_tbl): # entering this section signifies a gRPC start for state # change request from swss so initiate recording in mux_metrics table @@ -3558,7 +3490,7 @@ def handle_hw_mux_cable_table_grpc_notification(fvp, hw_mux_cable_tbl, asic_inde stub = grpc_port_stubs.get(port, None) if stub is None: helper_logger.log_debug("Y_CABLE_DEBUG:stub is None for performing hw mux RPC port {}".format(port)) - retry_setup_grpc_channel_for_port(port, asic_index) + retry_setup_grpc_channel_for_port(port, asic_index, port_tbl, grpc_client, fwd_state_response_tbl) stub = grpc_port_stubs.get(port, None) if stub is None: helper_logger.log_warning( @@ -3779,7 +3711,8 @@ def task_worker(self): if fvp: handle_hw_mux_cable_table_grpc_notification( - fvp, self.table_helper.get_hw_mux_cable_tbl(), asic_index, self.table_helper.get_mux_metrics_tbl(), False, port) + fvp, self.table_helper.get_hw_mux_cable_tbl(), asic_index, self.table_helper.get_mux_metrics_tbl(), False, port, self.table_helper.get_port_tbl(), self.table_helper.get_grpc_config_tbl(), self.table_helper.get_fwd_state_response_tbl()) + while True: (port_m, op_m, fvp_m) = self.table_helper.get_mux_cable_command_tbl()[asic_index].pop() @@ -3814,7 +3747,7 @@ def task_worker(self): if fvp_m: handle_fwd_state_command_grpc_notification( - fvp_m, self.table_helper.get_hw_mux_cable_tbl(), self.table_helper.get_fwd_state_response_tbl(), asic_index, port_m, self.table_helper.get_appl_db()) + fvp_m, self.table_helper.get_hw_mux_cable_tbl(), self.table_helper.get_fwd_state_response_tbl(), asic_index, port_m, self.table_helper.get_appl_db(), self.table_helper.get_port_tbl(), self.table_helper.get_grpc_config_tbl()) while True: (port_n, op_n, fvp_n) = self.table_helper.get_status_tbl_peer()[asic_index].pop() @@ -3828,7 +3761,7 @@ def task_worker(self): if fvp_n: handle_hw_mux_cable_table_grpc_notification( - fvp_n, self.table_helper.get_hw_mux_cable_tbl_peer(), asic_index, self.table_helper.get_mux_metrics_tbl(), True, port_n) + fvp_n, self.table_helper.get_hw_mux_cable_tbl_peer(), asic_index, self.table_helper.get_mux_metrics_tbl(), True, port_n, self.table_helper.get_port_tbl(), self.table_helper.get_grpc_config_tbl(), self.table_helper.get_fwd_state_response_tbl()) def run(self): if self.task_stopping_event.is_set(): @@ -4080,4 +4013,3 @@ def join(self): raise self.exc - 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 fa6a1cd10..7b51d08b5 100644 --- a/sonic-ycabled/ycable/ycable_utilities/y_cable_table_helper.py +++ b/sonic-ycabled/ycable/ycable_utilities/y_cable_table_helper.py @@ -58,27 +58,98 @@ class YcableStateUpdateTableHelper(object): def __init__(self): self.state_db = {} + self.appl_db = {} self.sub_status_tbl = {} + self.config_db = {} + self.port_tbl = {} + self.y_cable_tbl = {} + self.static_tbl = {} + self.mux_tbl = {} + self.port_table_keys = {} + self.loopback_tbl= {} + self.loopback_keys = {} + self.hw_mux_cable_tbl = {} + self.hw_mux_cable_tbl_peer = {} + self.grpc_config_tbl = {} + self.fwd_state_response_tbl = {} # Get the namespaces in the platform namespaces = multi_asic.get_front_end_namespaces() for namespace in namespaces: asic_id = multi_asic.get_asic_index_from_namespace(namespace) 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.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() + self.loopback_tbl[asic_id] = swsscommon.Table( + self.config_db[asic_id], "LOOPBACK_INTERFACE") + self.loopback_keys[asic_id] = self.loopback_tbl[asic_id].getKeys() + self.state_db[asic_id] = daemon_base.db_connect("STATE_DB", namespace) + self.hw_mux_cable_tbl[asic_id] = swsscommon.Table( + self.state_db[asic_id], swsscommon.STATE_HW_MUX_CABLE_TABLE_NAME) + self.hw_mux_cable_tbl_peer[asic_id] = swsscommon.Table( + self.state_db[asic_id], "HW_MUX_CABLE_TABLE_PEER") + self.y_cable_tbl[asic_id] = swsscommon.Table( + self.state_db[asic_id], swsscommon.STATE_HW_MUX_CABLE_TABLE_NAME) + self.static_tbl[asic_id] = swsscommon.Table( + self.state_db[asic_id], MUX_CABLE_STATIC_INFO_TABLE) + self.mux_tbl[asic_id] = swsscommon.Table( + self.state_db[asic_id], MUX_CABLE_INFO_TABLE) + self.grpc_config_tbl[asic_id] = swsscommon.Table(self.config_db[asic_id], "GRPCCLIENT") + self.fwd_state_response_tbl[asic_id] = swsscommon.Table( + self.appl_db[asic_id], "FORWARDING_STATE_RESPONSE") + self.static_tbl[asic_id] = swsscommon.Table( + self.state_db[asic_id], MUX_CABLE_STATIC_INFO_TABLE) def get_sub_status_tbl(self): return self.sub_status_tbl + def get_state_db(self): + return self.state_db + + def get_config_db(self): + return self.config_db + + def get_appl_db(self): + return self.appl_db + + def get_port_tbl(self): + return self.port_tbl + + def get_mux_tbl(self): + return self.mux_tbl + + def get_loopback_tbl(self): + return self.loopback_tbl + + def get_hw_mux_cable_tbl(self): + return self.hw_mux_cable_tbl + + def get_hw_mux_cable_tbl_peer(self): + return self.hw_mux_cable_tbl_peer + + def get_grpc_config_tbl(self): + return self.grpc_config_tbl + + def get_y_cable_tbl(self): + return self.y_cable_tbl + + def get_static_tbl(self): + return self.static_tbl + + def get_fwd_state_response_tbl(self): + return self.fwd_state_response_tbl + class DaemonYcableTableHelper(object): def __init__(self): self.state_db = {} + self.appl_db = {} self.config_db = {} self.port_tbl = {} self.y_cable_tbl = {} @@ -91,12 +162,14 @@ def __init__(self): self.hw_mux_cable_tbl = {} self.hw_mux_cable_tbl_peer = {} self.grpc_config_tbl = {} + self.fwd_state_response_tbl = {} # Get the namespaces in the platform fvs_updated = swsscommon.FieldValuePairs([('log_verbosity', 'notice')]) namespaces = multi_asic.get_front_end_namespaces() for namespace in namespaces: asic_id = multi_asic.get_asic_index_from_namespace(namespace) + self.appl_db[asic_id] = daemon_base.db_connect("APPL_DB", namespace) 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") @@ -119,6 +192,8 @@ def __init__(self): self.static_tbl[asic_id] = swsscommon.Table( self.state_db[asic_id], MUX_CABLE_STATIC_INFO_TABLE) self.grpc_config_tbl[asic_id] = swsscommon.Table(self.config_db[asic_id], "GRPCCLIENT") + self.fwd_state_response_tbl[asic_id] = swsscommon.Table( + self.appl_db[asic_id], "FORWARDING_STATE_RESPONSE") def get_state_db(self): @@ -157,6 +232,8 @@ def get_static_tbl(self): def get_grpc_config_tbl(self): return self.grpc_config_tbl + def get_fwd_state_response_tbl(self): + return self.fwd_state_response_tbl class YcableTableUpdateTableHelper(object): def __init__(self): @@ -167,6 +244,7 @@ def __init__(self): self.port_tbl, self.port_table_keys = {}, {} self.fwd_state_command_tbl, self.fwd_state_response_tbl, self.mux_cable_command_tbl = {}, {}, {} self.mux_metrics_tbl = {} + self.grpc_config_tbl = {} self.y_cable_response_tbl = {} @@ -202,6 +280,7 @@ def __init__(self): self.appl_db[asic_id], "MUX_CABLE_RESPONSE_TABLE") 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() + self.grpc_config_tbl[asic_id] = swsscommon.Table(self.config_db[asic_id], "GRPCCLIENT") def get_state_db(self): return self.state_db @@ -242,6 +321,9 @@ def get_y_cable_response_tbl(self): def get_port_tbl(self): return self.port_tbl + def get_grpc_config_tbl(self): + return self.grpc_config_tbl + class YcableCliUpdateTableHelper(object): def __init__(self):