Skip to content

Commit

Permalink
Fix/enhance lldp MIBs., add MockPubSub to mock_tables.dbconnector.py (#…
Browse files Browse the repository at this point in the history
…71)

* [lldp] fix updater, avoid warning log spam
* add MockPubSub to mock_tables.dbconnector.py
* pep8 cleanup
  • Loading branch information
mykolaf authored and qiluo-msft committed Jul 20, 2018
1 parent d49c345 commit 49dd351
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/sonic_ax_impl/mibs/ieee802_1ab.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from enum import Enum, unique
from bisect import bisect_right

from swsssdk import port_util
from sonic_ax_impl import mibs, logger
from ax_interface import MIBMeta, SubtreeMIBEntry, MIBEntry, MIBUpdater, ValueType

Expand All @@ -26,6 +27,7 @@ class LLDPRemoteTables(int, Enum):
lldp_rem_sys_cap_supported = 11
lldp_rem_sys_cap_enabled = 12


@unique
class LLDPLocalChassis(int, Enum):
"""
Expand All @@ -45,6 +47,7 @@ def __init__(self):
super().__init__()

self.db_conn = mibs.init_db()
self.db_conn.connect(mibs.APPL_DB)
self.if_name_map = {}
self.if_alias_map = {}
self.if_id_map = {}
Expand Down Expand Up @@ -83,7 +86,7 @@ def update_interface_data(self, if_name):
"""
Update data from the DB for a single interface
"""
loc_port_kvs = self.db_conn.get_all(mibs.APPL_DB, mibs.if_entry_table(bytes(if_name, 'utf-8')))
loc_port_kvs = self.db_conn.get_all(mibs.APPL_DB, mibs.if_entry_table(if_name))
if not loc_port_kvs:
return
self.loc_port_data.update({if_name: loc_port_kvs})
Expand Down Expand Up @@ -115,10 +118,14 @@ def update_data(self):
break

lldp_entry = msg["channel"].split(b":")[-1].decode()
data = msg['data'] # event data
data = msg['data'] # event data

# extract interface name
interface = lldp_entry.split('|')[-1]

# ignore management interface
if interface == "eth0":
continue
# get interface from interface name
if_index = port_util.get_index_from_str(interface)

Expand Down Expand Up @@ -185,6 +192,13 @@ def reinit_data(self):
self.db_conn.connect(mibs.APPL_DB)
self.loc_chassis_data = self.db_conn.get_all(mibs.APPL_DB, mibs.LOC_CHASSIS_TABLE)

def update_data(self):
"""
Avoid NotImplementedError
The data is mostly static, reinit it once a minute is enough.
"""
pass

def table_lookup(self, table_name):
try:
_table_name = bytes(getattr(table_name, 'name', table_name), 'utf-8')
Expand Down Expand Up @@ -476,4 +490,3 @@ class LLDPRemTable(metaclass=MIBMeta, prefix='.1.0.8802.1.1.2.1.4.1'):
lldpRemSysCapEnabled = \
SubtreeMIBEntry('1.12', lldp_updater, ValueType.OCTET_STRING, lldp_updater.lldp_table_lookup,
LLDPRemoteTables(12))

13 changes: 13 additions & 0 deletions tests/mock_tables/dbconnector.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,20 @@ def config_set(self, *args):
pass


class MockPubSub:
def get_message(self):
return None

def psubscribe(self, *args, **kwargs):
pass

def __call__(self, *args, **kwargs):
return self


INPUT_DIR = os.path.dirname(os.path.abspath(__file__))


class SwssSyncClient(mockredis.MockRedis):
def __init__(self, *args, **kwargs):
super(SwssSyncClient, self).__init__(strict=True, *args, **kwargs)
Expand All @@ -31,6 +43,7 @@ def __init__(self, *args, **kwargs):
fname = 'state_db.json'
else:
raise ValueError("Invalid db")
self.pubsub = MockPubSub()

fname = os.path.join(INPUT_DIR, fname)
with open(fname) as f:
Expand Down

0 comments on commit 49dd351

Please # to comment.