Skip to content

Commit

Permalink
Fix lldpMIB: use SubtreeMIBEntry (sonic-net#43)
Browse files Browse the repository at this point in the history
Signed-off-by: Qi Luo <qiluo-msft@users.noreply.github.com>
  • Loading branch information
qiluo-msft authored Oct 17, 2017
1 parent bc4becf commit d9d85a1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 16 deletions.
49 changes: 35 additions & 14 deletions src/sonic_ax_impl/mibs/ieee802_1ab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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.
Expand All @@ -57,23 +67,36 @@ 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
return None
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
Expand Down Expand Up @@ -103,15 +126,14 @@ 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'

# lldpLocPortNum = '1.1'

# 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'

Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
21 changes: 19 additions & 2 deletions tests/test_lldp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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' \
Expand Down

0 comments on commit d9d85a1

Please sign in to comment.