diff --git a/src/sonic_ax_impl/mibs/ieee802_1ab.py b/src/sonic_ax_impl/mibs/ieee802_1ab.py index ac1b3b642..da319ebd3 100644 --- a/src/sonic_ax_impl/mibs/ieee802_1ab.py +++ b/src/sonic_ax_impl/mibs/ieee802_1ab.py @@ -494,10 +494,9 @@ def __init__(self): # establish connection to application database. Namespace.connect_all_dbs(self.db_conn, mibs.APPL_DB) self.if_range = [] - self.mgmt_ips = {} + self.if_with_mgmt_ips = set() self.oid_name_map = {} self.mgmt_oid_name_map = {} - self.mgmt_ip_str = None self.pubsub = [None] * len(self.db_conn) def update_rem_if_mgmt(self, if_oid, if_name): @@ -511,28 +510,27 @@ def update_rem_if_mgmt(self, if_oid, if_name): if len(mgmt_ip_str) == 0: # the peer advertise an emtpy mgmt address return - time_mark = int(lldp_kvs['lldp_rem_time_mark']) - remote_index = int(lldp_kvs['lldp_rem_index']) - subtype = self.get_subtype(mgmt_ip_str) - ip_hex = self.get_ip_hex(mgmt_ip_str, subtype) - if subtype == ManAddrConst.man_addr_subtype_ipv4: - addr_subtype_sub_oid = 4 - mgmt_ip_sub_oid = (addr_subtype_sub_oid, *[int(i) for i in mgmt_ip_str.split('.')]) - elif subtype == ManAddrConst.man_addr_subtype_ipv6: - addr_subtype_sub_oid = 6 - mgmt_ip_sub_oid = (addr_subtype_sub_oid, *[int(i, 16) if i else 0 for i in mgmt_ip_str.split(':')]) - else: - logger.warning("Invalid management IP {}".format(mgmt_ip_str)) - return - self.if_range.append((time_mark, - if_oid, - remote_index, - subtype, - *mgmt_ip_sub_oid)) - - self.mgmt_ips.update({if_name: {"ip_str": mgmt_ip_str, - "addr_subtype": subtype, - "addr_hex": ip_hex}}) + for mgmt_ip in set(mgmt_ip_str.split(',')): + time_mark = int(lldp_kvs['lldp_rem_time_mark']) + remote_index = int(lldp_kvs['lldp_rem_index']) + subtype = self.get_subtype(mgmt_ip) + ip_hex = self.get_ip_hex(mgmt_ip, subtype) + if subtype == ManAddrConst.man_addr_subtype_ipv4: + addr_subtype_sub_oid = 4 + mgmt_ip_sub_oid = (addr_subtype_sub_oid, *[int(i) for i in mgmt_ip.split('.')]) + elif subtype == ManAddrConst.man_addr_subtype_ipv6: + addr_subtype_sub_oid = 6 + mgmt_ip_sub_oid = (addr_subtype_sub_oid, *[int(i, 16) if i else 0 for i in mgmt_ip.split(':')]) + else: + logger.warning("Invalid management IP {}".format(mgmt_ip_str)) + continue + self.if_range.append((time_mark, + if_oid, + remote_index, + subtype, + *mgmt_ip_sub_oid)) + self.if_with_mgmt_ips.add(if_name) + except (KeyError, AttributeError) as e: logger.warning("Error updating remote mgmt addr: {}".format(e)) return @@ -577,7 +575,7 @@ def reinit_data(self): Namespace.connect_all_dbs(self.db_conn, mibs.APPL_DB) self.if_range = [] - self.mgmt_ips = {} + self.if_with_mgmt_ips = set() for if_oid, if_name in self.oid_name_map.items(): self.update_rem_if_mgmt(if_oid, if_name) @@ -594,7 +592,7 @@ def lookup(self, sub_id, callable): if sub_id not in self.oid_name_map: return None if_name = self.oid_name_map[sub_id] - if if_name not in self.mgmt_ips: + if if_name not in self.if_with_mgmt_ips: # no data for this interface return None return callable(sub_id, if_name) @@ -623,15 +621,6 @@ def get_subtype(self, ip_str): logger.warning("Invalid mgmt IP {}".format(ip_str)) return None - def man_addr_subtype(self, sub_id, if_name): - return self.mgmt_ips[if_name]['addr_subtype'] - - def man_addr(self, sub_id, if_name): - """ - :param sub_id: - :return: MGMT IP in HEX - """ - return self.mgmt_ips[if_name]['addr_hex'] @staticmethod def man_addr_if_subtype(sub_id, _): return ManAddrConst.man_addr_if_subtype diff --git a/tests/mock_tables/appl_db.json b/tests/mock_tables/appl_db.json index 784a5212e..b1b079757 100644 --- a/tests/mock_tables/appl_db.json +++ b/tests/mock_tables/appl_db.json @@ -11,7 +11,7 @@ "lldp_rem_chassis_id_subtype": "4", "lldp_rem_sys_name": "switch13", "lldp_rem_port_id": "Ethernet1", - "lldp_rem_man_addr": "10.224.25.100" + "lldp_rem_man_addr": "10.224.25.100,2603:10e2:290:5016::" }, "LLDP_ENTRY_TABLE:Ethernet4": { "lldp_rem_port_id_subtype": "5", @@ -25,7 +25,7 @@ "lldp_rem_chassis_id_subtype": "4", "lldp_rem_sys_name": "switch13", "lldp_rem_port_id": "Ethernet2", - "lldp_rem_man_addr": "fe80::268a:7ff:fe3f:834c" + "lldp_rem_man_addr": "fe80::268a::7ff:fe3f:834c,10.224.25.101" }, "LLDP_ENTRY_TABLE:Ethernet8": { "lldp_rem_port_id_subtype": "5", @@ -53,7 +53,7 @@ "lldp_rem_chassis_id_subtype": "4", "lldp_rem_sys_name": "switch13", "lldp_rem_port_id": "Ethernet4", - "lldp_rem_man_addr": "10.224.25.103" + "lldp_rem_man_addr": "fe80:268a::7ff:fe3f:834c" }, "LLDP_ENTRY_TABLE:Ethernet16": { "lldp_rem_port_id_subtype": "5", @@ -67,7 +67,7 @@ "lldp_rem_chassis_id_subtype": "4", "lldp_rem_sys_name": "switch13", "lldp_rem_port_id": "Ethernet5", - "lldp_rem_man_addr": "10.224.25.104" + "lldp_rem_man_addr": "" }, "LLDP_ENTRY_TABLE:Ethernet20": { "lldp_rem_port_id_subtype": "5", diff --git a/tests/test_lldp.py b/tests/test_lldp.py index a9ee5f2fe..83808cc88 100644 --- a/tests/test_lldp.py +++ b/tests/test_lldp.py @@ -106,11 +106,36 @@ def test_subtype_lldp_loc_man_addr_table(self): def test_subtype_lldp_rem_man_addr_table(self): + # Verfiy both valid ipv4 and ipv6 address exit for entry in range(3, 6): mib_entry = self.lut[(1, 0, 8802, 1, 1, 2, 1, 4, 2, 1, entry)] ret = mib_entry(sub_id=(1, 1)) self.assertIsNotNone(ret) print(ret) + # Verfiy valid ipv4 and invalid ipv6 address exit + for entry in range(3, 6): + mib_entry = self.lut[(1, 0, 8802, 1, 1, 2, 1, 4, 2, 1, entry)] + ret = mib_entry(sub_id=(1, 5)) + self.assertIsNotNone(ret) + print(ret) + # Verfiy only valid ipv4 address exit + for entry in range(3, 6): + mib_entry = self.lut[(1, 0, 8802, 1, 1, 2, 1, 4, 2, 1, entry)] + ret = mib_entry(sub_id=(1, 9)) + self.assertIsNotNone(ret) + print(ret) + # Verfiy only valid ipv6 address exit + for entry in range(3, 6): + mib_entry = self.lut[(1, 0, 8802, 1, 1, 2, 1, 4, 2, 1, entry)] + ret = mib_entry(sub_id=(1, 13)) + self.assertIsNotNone(ret) + print(ret) + # Verfiy no mgmt address exit + for entry in range(3, 6): + mib_entry = self.lut[(1, 0, 8802, 1, 1, 2, 1, 4, 2, 1, entry)] + ret = mib_entry(sub_id=(1, 17)) + self.assertIsNone(ret) + print(ret) def test_local_port_identification(self): mib_entry = self.lut[(1, 0, 8802, 1, 1, 2, 1, 3, 7, 1, 3)]