diff --git a/src/sonic_ax_impl/mibs/ietf/rfc1213.py b/src/sonic_ax_impl/mibs/ietf/rfc1213.py index 1a13c6f94..4ee3bec63 100644 --- a/src/sonic_ax_impl/mibs/ietf/rfc1213.py +++ b/src/sonic_ax_impl/mibs/ietf/rfc1213.py @@ -162,10 +162,12 @@ def update_data(self): continue for nh in nexthops.split(','): # TODO: if ipn contains IP range, create more sub_id here - sub_id = ip2byte_tuple(ipn.network_address) - self.route_list.append(sub_id) - self.nexthop_map[sub_id] = ipaddress.ip_address(nh).packed - break # Just need the first nexthop + if type(ipaddress.ip_address(nh)) is ipaddress.IPv4Address: + sub_id = ip2byte_tuple(ipn.network_address) + self.route_list.append(sub_id) + self.nexthop_map[sub_id] = ipaddress.ip_address(nh).packed + break # Just need the first nexthop + mibs.logger.warning("Route {} has non-IPv4 nexthop: {}".format(routestr, nh)) self.route_list.sort() diff --git a/tests/mock_tables/appl_db.json b/tests/mock_tables/appl_db.json index cfbb79146..ed0915a3a 100644 --- a/tests/mock_tables/appl_db.json +++ b/tests/mock_tables/appl_db.json @@ -648,8 +648,8 @@ "speed": 100000 }, "ROUTE_TABLE:0.0.0.0/0": { - "ifname": "Ethernet0,Ethernet4,Ethernet8,Ethernet12,Ethernet16,Ethernet20,Ethernet24,Ethernet28,Ethernet32,Ethernet36,Ethernet40,Ethernet44,Ethernet48,Ethernet52,Ethernet78", - "nexthop": "10.0.0.1,10.0.0.3,10.0.0.5,10.0.0.7,10.0.0.9,10.0.0.11,10.0.0.13,10.0.0.15,10.0.0.17,10.0.0.19,10.0.0.21,10.0.0.23,10.0.0.25,10.0.0.27" + "ifname": "Ethernet74,Ethernet0,Ethernet4,Ethernet8,Ethernet12,Ethernet16,Ethernet20,Ethernet24,Ethernet28,Ethernet32,Ethernet36,Ethernet40,Ethernet44,Ethernet48,Ethernet52,Ethernet78", + "nexthop": "2001:0db8:85a3:0000:0000:8a2e:0370:7334,10.0.0.1,10.0.0.3,10.0.0.5,10.0.0.7,10.0.0.9,10.0.0.11,10.0.0.13,10.0.0.15,10.0.0.17,10.0.0.19,10.0.0.21,10.0.0.23,10.0.0.25,10.0.0.27" }, "ROUTE_TABLE:10.1.0.32": { "nexthop": "", diff --git a/tests/test_nexthop.py b/tests/test_nexthop.py index 6ad7ce0c4..44ba07cd2 100644 --- a/tests/test_nexthop.py +++ b/tests/test_nexthop.py @@ -60,6 +60,7 @@ def test_getnextpdu(self): n = len(response.values) value0 = response.values[0] self.assertEqual(value0.type_, ValueType.IP_ADDRESS) + self.assertEqual(type(ipaddress.ip_address(value0.data.string)), ipaddress.IPv4Address) self.assertEqual(str(value0.data), ipaddress.ip_address("10.0.0.1").packed.decode()) def test_getnextpdu_exactmatch(self): diff --git a/tests/test_rfc1213.py b/tests/test_rfc1213.py index 8a4b2cee6..13072cadb 100644 --- a/tests/test_rfc1213.py +++ b/tests/test_rfc1213.py @@ -47,6 +47,40 @@ def test_NextHopUpdater_route_no_next_hop(self): self.assertTrue(len(updater.route_list) == 0) + @mock.patch('sonic_ax_impl.mibs.Namespace.dbs_keys', mock.MagicMock(return_value=(["ROUTE_TABLE:0.0.0.0/0"]))) + @mock.patch('sonic_ax_impl.mibs.Namespace.dbs_get_all', mock.MagicMock(return_value=({"nexthop": "2001:db8:1111:2222:3333:4444:5555:6666,2001:db8:3333:4444:5555:6666:7777:8888", "ifname": "Ethernet0,Ethernet4"}))) + def test_NextHopUpdater_route_ipv6_next_hop(self): + updater = NextHopUpdater() + + with mock.patch('sonic_ax_impl.mibs.logger.warning') as mocked_warning: + updater.update_data() + + # check warning + expected = [ + mock.call("Route ROUTE_TABLE:0.0.0.0/0 has non-IPv4 nexthop: 2001:db8:1111:2222:3333:4444:5555:6666"), + mock.call("Route ROUTE_TABLE:0.0.0.0/0 has non-IPv4 nexthop: 2001:db8:3333:4444:5555:6666:7777:8888") + ] + mocked_warning.assert_has_calls(expected) + + self.assertTrue(len(updater.route_list) == 0) + self.assertTrue(len(updater.nexthop_map) == 0) + + @mock.patch('sonic_ax_impl.mibs.Namespace.dbs_keys', mock.MagicMock(return_value=(["ROUTE_TABLE:0.0.0.0/0"]))) + @mock.patch('sonic_ax_impl.mibs.Namespace.dbs_get_all', mock.MagicMock(return_value=({"nexthop": "2001:db8:1111:2222:3333:4444:5555:6666,127.1.1.1", "ifname": "Ethernet0,Ethernet4"}))) + def test_NextHopUpdater_route_ipv4_ipv6_next_hop(self): + updater = NextHopUpdater() + + with mock.patch('sonic_ax_impl.mibs.logger.warning') as mocked_warning: + updater.update_data() + + # check warning + expected = [ + mock.call("Route ROUTE_TABLE:0.0.0.0/0 has non-IPv4 nexthop: 2001:db8:1111:2222:3333:4444:5555:6666") + ] + mocked_warning.assert_has_calls(expected) + + self.assertTrue(len(updater.route_list) == 1) + self.assertTrue(len(updater.nexthop_map) == 1) class TestNextHopUpdaterRedisException(TestCase): def __init__(self, name): @@ -106,7 +140,6 @@ def mock_get_sync_d_from_all_namespace(per_namespace_func, db_conn): # check re-init connect_namespace_dbs.assert_called() - def test_InterfaceUpdater_get_counters(self): def mock_lag_entry_table(lag_name):