Skip to content

Commit

Permalink
[port_util] Fix issue: port_util.get_vlan_interface_oid_map should no…
Browse files Browse the repository at this point in the history
…t raise exception when DB has not RIF data (#117)

**Why I did this?**

port_util.get_vlan_interface_oid_map would raise exception when there is no RIF configured, and it would causes snmpagent not work. This PR is to fix it.

```
#012Traceback (most recent call last):
#012File "/usr/local/lib/python3.7/dist-packages/ax_interface/mib.py", line 37, in start
#12    self.reinit_data()#12  File "/usr/local/lib/python3.7/dist-packages/sonic_ax_impl/mibs/ietf/rfc1213.py", line 233, in reinit_data
#12    self.vlan_oid_name_map = Namespace.get_sync_d_from_all_namespace(mibs.init_sync_d_vlan_tables, self.db_conn)
#12  File "/usr/local/lib/python3.7/dist-packages/sonic_ax_impl/mibs/__init__.py", line 651, in get_sync_d_from_all_namespace
#12    ns_tuple = per_namespace_func(db_conn)
#12  File "/usr/local/lib/python3.7/dist-packages/sonic_ax_impl/mibs/__init__.py", line 341, in init_sync_d_vlan_tables
#12    vlan_name_map = port_util.get_vlan_interface_oid_map(db_conn)
#12  File "/usr/local/lib/python3.7/dist-packages/swsssdk/port_util.py", line 167, in get_vlan_interface_oid_map
#12    rif_name_map = db.get_all('COUNTERS_DB', 'COUNTERS_RIF_NAME_MAP', blocking=True)
#12  File "/usr/lib/python3/dist-packages/swsscommon/swsscommon.py", line 1751, in get_all
#12    return dict(super(SonicV2Connector, self).get_all(db_name, _hash, blocking))
#12  File "/usr/lib/python3/dist-packages/swsscommon/swsscommon.py", line 1708, in get_all
#12    return _swsscommon.SonicV2Connector_Native_get_all(self, db_name, _hash, blocking)
#012RuntimeError: Key '{COUNTERS_RIF_NAME_MAP}' unavailable in database '{COUNTERS_DB}'
```

**How I did this:**
check redis key exists first before calling get_all.
  • Loading branch information
Junchao-Mellanox authored Mar 21, 2022
1 parent 96da8b9 commit 96c0590
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/swsssdk/port_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,14 @@ def get_rif_port_map(db):

return rif_port_oid_map

def get_vlan_interface_oid_map(db):
def get_vlan_interface_oid_map(db, blocking=True):
"""
Get Vlan Interface names and sai oids
"""
db.connect('COUNTERS_DB')
rif_name_map = db.get_all('COUNTERS_DB', 'COUNTERS_RIF_NAME_MAP', blocking=True)
rif_type_name_map = db.get_all('COUNTERS_DB', 'COUNTERS_RIF_TYPE_MAP', blocking=True)

rif_name_map = db.get_all('COUNTERS_DB', 'COUNTERS_RIF_NAME_MAP', blocking=blocking)
rif_type_name_map = db.get_all('COUNTERS_DB', 'COUNTERS_RIF_TYPE_MAP', blocking=blocking)

if not rif_name_map or not rif_type_name_map:
return {}
Expand Down
19 changes: 19 additions & 0 deletions test/test_port_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import os
import sys

if sys.version_info.major == 3:
from unittest import mock
else:
import mock

modules_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(modules_path, 'src'))

class TestPortUtil:
def test_get_vlan_interface_oid_map(self):
db = mock.MagicMock()
db.get_all = mock.MagicMock()
db.get_all.return_value = {}

from swsssdk.port_util import get_vlan_interface_oid_map
assert not get_vlan_interface_oid_map(db, True)

0 comments on commit 96c0590

Please sign in to comment.