From d9d85a1383d752220054a714487899c06b697863 Mon Sep 17 00:00:00 2001 From: Qi Luo Date: Mon, 16 Oct 2017 22:06:17 -0700 Subject: [PATCH] Fix lldpMIB: use SubtreeMIBEntry (#43) Signed-off-by: Qi Luo --- src/sonic_ax_impl/mibs/ieee802_1ab.py | 49 +++++++++++++++++++-------- tests/test_lldp.py | 21 ++++++++++-- 2 files changed, 54 insertions(+), 16 deletions(-) diff --git a/src/sonic_ax_impl/mibs/ieee802_1ab.py b/src/sonic_ax_impl/mibs/ieee802_1ab.py index dbd9124449a4..86f2d131fbe3 100644 --- a/src/sonic_ax_impl/mibs/ieee802_1ab.py +++ b/src/sonic_ax_impl/mibs/ieee802_1ab.py @@ -2,10 +2,10 @@ http://www.ieee802.org/1/files/public/MIBs/LLDP-MIB-200505060000Z.txt """ from enum import Enum, unique +from bisect import bisect_right from sonic_ax_impl import mibs, logger -from ax_interface import MIBMeta, ContextualMIBEntry, MIBUpdater, ValueType - +from ax_interface import MIBMeta, SubtreeMIBEntry, MIBUpdater, ValueType @unique class LLDPRemoteTables(int, Enum): @@ -49,6 +49,16 @@ def reinit_data(self): self.oid_sai_map, \ self.oid_name_map = mibs.init_sync_d_interface_tables(self.db_conn) + def get_next(self, sub_id): + """ + :param sub_id: The 1-based sub-identifier query. + :return: the next sub id. + """ + right = bisect_right(self.if_range, sub_id) + if right == len(self.if_range): + return None + return self.if_range[right] + def update_data(self): """ Subclass update data routine. Updates available LLDP counters. @@ -57,16 +67,24 @@ def update_data(self): # establish connection to application database. self.db_conn.connect(mibs.APPL_DB) + self.if_range = [] self.lldp_counters = {} - for if_name in self.if_name_map: + for if_oid, if_name in self.oid_name_map.items(): lldp_kvs = self.db_conn.get_all(mibs.APPL_DB, mibs.lldp_entry_table(if_name)) if not lldp_kvs: continue + self.if_range.append((if_oid, )) self.lldp_counters.update({if_name: lldp_kvs}) if not self.lldp_counters: logger.warning("0 - b'LLDP_ENTRY_TABLE' is empty. No LLDP information could be retrieved.") + self.if_range.sort() def local_port_id(self, sub_id): + if len(sub_id) <= 0: + return None + sub_id = sub_id[0] + 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.lldp_counters: # no LLDP data for this interface--we won't report the local interface @@ -74,6 +92,11 @@ def local_port_id(self, sub_id): return self.if_alias_map[if_name] def lldp_table_lookup(self, sub_id, table_name): + if len(sub_id) <= 0: + return None + sub_id = sub_id[0] + 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.lldp_counters: # no LLDP data for this interface @@ -103,7 +126,6 @@ class LLDPLocPortTable(metaclass=MIBMeta, prefix='.1.0.8802.1.1.2.1.3.7'): 'lldpLocPortTable' """ lldp_updater = _lldp_updater - if_range = lldp_updater.oid_name_map.keys() # lldpLocPortEntry = '1' @@ -111,7 +133,7 @@ class LLDPLocPortTable(metaclass=MIBMeta, prefix='.1.0.8802.1.1.2.1.3.7'): # lldpLocPortIdSubtype = '1.2' - lldpLocPortId = ContextualMIBEntry('1.3', if_range, ValueType.OCTET_STRING, lldp_updater.local_port_id) + lldpLocPortId = SubtreeMIBEntry('1.3', lldp_updater, ValueType.OCTET_STRING, lldp_updater.local_port_id) # lldpLocPortDesc = '1.4' @@ -205,10 +227,9 @@ class LLDPRemTable(metaclass=MIBMeta, prefix='.1.0.8802.1.1.2.1.4.1'): } """ lldp_updater = _lldp_updater - if_range = lldp_updater.oid_name_map.keys() lldpRemTimeMark = \ - ContextualMIBEntry('1.1', if_range, ValueType.TIME_TICKS, lldp_updater.lldp_table_lookup_integer, + SubtreeMIBEntry('1.1', lldp_updater, ValueType.TIME_TICKS, lldp_updater.lldp_table_lookup_integer, LLDPRemoteTables(1)) # TODO: Impl. @@ -221,31 +242,31 @@ class LLDPRemTable(metaclass=MIBMeta, prefix='.1.0.8802.1.1.2.1.4.1'): # LLDPRemoteTables(3)) lldpRemChassisIdSubtype = \ - ContextualMIBEntry('1.4', if_range, ValueType.INTEGER, lldp_updater.lldp_table_lookup_integer, + SubtreeMIBEntry('1.4', lldp_updater, ValueType.INTEGER, lldp_updater.lldp_table_lookup_integer, LLDPRemoteTables(4)) lldpRemChassisId = \ - ContextualMIBEntry('1.5', if_range, ValueType.OCTET_STRING, lldp_updater.lldp_table_lookup, + SubtreeMIBEntry('1.5', lldp_updater, ValueType.OCTET_STRING, lldp_updater.lldp_table_lookup, LLDPRemoteTables(5)) lldpRemPortIdSubtype = \ - ContextualMIBEntry('1.6', if_range, ValueType.INTEGER, lldp_updater.lldp_table_lookup_integer, + SubtreeMIBEntry('1.6', lldp_updater, ValueType.INTEGER, lldp_updater.lldp_table_lookup_integer, LLDPRemoteTables(6)) lldpRemPortId = \ - ContextualMIBEntry('1.7', if_range, ValueType.OCTET_STRING, lldp_updater.lldp_table_lookup, + SubtreeMIBEntry('1.7', lldp_updater, ValueType.OCTET_STRING, lldp_updater.lldp_table_lookup, LLDPRemoteTables(7)) lldpRemPortDesc = \ - ContextualMIBEntry('1.8', if_range, ValueType.OCTET_STRING, lldp_updater.lldp_table_lookup, + SubtreeMIBEntry('1.8', lldp_updater, ValueType.OCTET_STRING, lldp_updater.lldp_table_lookup, LLDPRemoteTables(8)) lldpRemSysName = \ - ContextualMIBEntry('1.9', if_range, ValueType.OCTET_STRING, lldp_updater.lldp_table_lookup, + SubtreeMIBEntry('1.9', lldp_updater, ValueType.OCTET_STRING, lldp_updater.lldp_table_lookup, LLDPRemoteTables(9)) lldpRemSysDesc = \ - ContextualMIBEntry('1.10', if_range, ValueType.OCTET_STRING, lldp_updater.lldp_table_lookup, + SubtreeMIBEntry('1.10', lldp_updater, ValueType.OCTET_STRING, lldp_updater.lldp_table_lookup, LLDPRemoteTables(10)) # TODO: Impl. diff --git a/tests/test_lldp.py b/tests/test_lldp.py index 0866a79730c4..b0d40ca046ba 100644 --- a/tests/test_lldp.py +++ b/tests/test_lldp.py @@ -67,17 +67,34 @@ def test_getnextpdu_eth2(self): def test_subtype(self): for entry in range(4, 11): - mib_entry = self.lut[(1, 0, 8802, 1, 1, 2, 1, 4, 1, 1, entry, 1)] + mib_entry = self.lut[(1, 0, 8802, 1, 1, 2, 1, 4, 1, 1, entry)] ret = mib_entry(sub_id=(1,)) self.assertIsNotNone(ret) print(ret) def test_local_port_identification(self): - mib_entry = self.lut[(1, 0, 8802, 1, 1, 2, 1, 3, 7, 1, 3, 1)] + mib_entry = self.lut[(1, 0, 8802, 1, 1, 2, 1, 3, 7, 1, 3)] ret = mib_entry(sub_id=(1,)) self.assertEquals(ret, b'Ethernet0') print(ret) + def test_getnextpdu_local_port_identification(self): + # oid.include = 1 + oid = ObjectIdentifier(11, 0, 1, 0, (1, 0, 8802, 1, 1, 2, 1, 3, 7, 1, 3)) + get_pdu = GetNextPDU( + header=PDUHeader(1, PduTypes.GET, 16, 0, 42, 0, 0, 0), + oids=[oid] + ) + + encoded = get_pdu.encode() + response = get_pdu.make_response(self.lut) + + n = len(response.values) + # self.assertEqual(n, 7) + value0 = response.values[0] + self.assertEqual(value0.type_, ValueType.OCTET_STRING) + self.assertEqual(str(value0.data), "Ethernet0") + def test_lab_breaks(self): break1 = b'\x01\x06\x10\x00\x00\x00\x00q\x00\x01\xd1\x02\x00\x01\xd1\x03\x00\x00\x00P\t\x00\x01\x00\x00' \ b'\x00\x00\x01\x00\x00\x00\x00\x00\x00"b\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x02\x00' \