From ad302d4062e12a7447cac7b478442485ef012b9a Mon Sep 17 00:00:00 2001 From: bingwang-ms <66248323+bingwang-ms@users.noreply.github.com> Date: Thu, 28 Jan 2021 14:21:05 +0800 Subject: [PATCH] Fix: 'key not found' exception in bgp4.py (#192) **- What I did** A 'key not found' exception will be raised in bgp4.py if the state for a given neighbor is not found in STATE_DB. ``` ERR snmp#snmp-subagent [ax_interface] ERROR: MIBUpdater.start() caught an unexpected exception during update_data() #012Traceback (most recent call last): #012 File "/usr/local/lib/python3.7/dist-packages/ax_interface/mib.py", line 43, in start #012 self.update_data()#012 File "/usr/local/lib/python3.7/dist-packages/sonic_ax_impl/mibs/vendor/cisco/bgp4.py", line 42, in update_data #012 state = neigh_info['state'] #012 File "/usr/lib/python3/dist-packages/swsscommon/swsscommon.py", line 345, in __getitem__ #012 return _swsscommon.FieldValueMap___getitem__(self, key) #012IndexError: key not found ``` It is becaues an empty ```dict``` is returned by ```get_all``` when nothing is found for the given key. So check for ```None``` can't detect the error. **- How I did it** This commit addressed the issue by checking the key ```state```. **- How to verify it** Verified on A7260. No exception is observed after the update. **- Description for the changelog** This PR fix exception caused by non existing key. --- src/sonic_ax_impl/mibs/vendor/cisco/bgp4.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/sonic_ax_impl/mibs/vendor/cisco/bgp4.py b/src/sonic_ax_impl/mibs/vendor/cisco/bgp4.py index c8475d56044f..f5f3e3724cb9 100644 --- a/src/sonic_ax_impl/mibs/vendor/cisco/bgp4.py +++ b/src/sonic_ax_impl/mibs/vendor/cisco/bgp4.py @@ -1,8 +1,6 @@ -import socket from bisect import bisect_right from sonic_ax_impl import mibs from ax_interface import MIBMeta, ValueType, MIBUpdater, SubtreeMIBEntry -from ax_interface.mib import MIBEntry from sonic_ax_impl.mibs import Namespace import ipaddress @@ -22,7 +20,7 @@ def __init__(self): super().__init__() self.db_conn = Namespace.init_namespace_dbs() - self.neigh_state_map = {} + self.neigh_state_map = {} self.session_status_map = {} self.session_status_list = [] @@ -38,7 +36,7 @@ def update_data(self): neigh_str = neigh_key neigh_str = neigh_str.split('|')[1] neigh_info = self.db_conn[db_index].get_all(mibs.STATE_DB, neigh_key, blocking=False) - if neigh_info is not None: + if neigh_info: state = neigh_info['state'] ip = ipaddress.ip_address(neigh_str) if type(ip) is ipaddress.IPv4Address: