From e0f36a5ff598c5172c65b501a11f1699739e77e8 Mon Sep 17 00:00:00 2001 From: SuvarnaMeenakshi <50386592+SuvarnaMeenakshi@users.noreply.github.com> Date: Wed, 30 Jun 2021 01:16:26 -0700 Subject: [PATCH] [multi-asic]: Udpate to use SonicDBConfig from swsscommon (#219) **- What I did** Update snmpagent to use SonicDBConfig from swsscommon. **- How I did it** - Update import of SonicDBConfig - init_namespace_dbs should return list of all namespace connector classes. Ensure that the list ordering is maintained such that the first element of the list is of the default namespace. **- How to verify it** Tested on single asic VS; SNMP service starts without any error log. Execute snmpwalk on one of the OIDs: admin@vlab-01:~$ docker exec -it snmp snmpwalk -v2c -c public 127.0.0.1 iso.3.6.1.2.1.2.2.1.2 iso.3.6.1.2.1.2.2.1.2.1 = STRING: "fortyGigE0/0" iso.3.6.1.2.1.2.2.1.2.5 = STRING: "fortyGigE0/4" iso.3.6.1.2.1.2.2.1.2.9 = STRING: "fortyGigE0/8" iso.3.6.1.2.1.2.2.1.2.13 = STRING: "fortyGigE0/12" iso.3.6.1.2.1.2.2.1.2.17 = STRING: "fortyGigE0/16" iso.3.6.1.2.1.2.2.1.2.21 = STRING: "fortyGigE0/20" iso.3.6.1.2.1.2.2.1.2.25 = STRING: "fortyGigE0/24" iso.3.6.1.2.1.2.2.1.2.29 = STRING: "fortyGigE0/28" .. iso.3.6.1.2.1.2.2.1.2.10000 = STRING: "eth0" Tested on multi asic VS; SNMP service starts without any error log. admin@vlab-08:~$ docker exec -it snmp snmpwalk -v2c -c public 127.0.0.1 iso.3.6.1.2.1.2.2.1.2 Execute snmpwalk on one of the OIDs: iso.3.6.1.2.1.2.2.1.2.1 = STRING: "Ethernet1/1" iso.3.6.1.2.1.2.2.1.2.5 = STRING: "Ethernet1/2" iso.3.6.1.2.1.2.2.1.2.9 = STRING: "Ethernet1/3" iso.3.6.1.2.1.2.2.1.2.13 = STRING: "Ethernet1/4" iso.3.6.1.2.1.2.2.1.2.17 = STRING: "Ethernet1/5" iso.3.6.1.2.1.2.2.1.2.21 = STRING: "Ethernet1/6" iso.3.6.1.2.1.2.2.1.2.25 = STRING: "Ethernet1/7" iso.3.6.1.2.1.2.2.1.2.29 = STRING: "Ethernet1/8" iso.3.6.1.2.1.2.2.1.2.1001 = STRING: "PortChannel0001" .. iso.3.6.1.2.1.2.2.1.2.9000 = STRING: "Eth4-ASIC0" iso.3.6.1.2.1.2.2.1.2.9004 = STRING: "Eth5-ASIC0" iso.3.6.1.2.1.2.2.1.2.9008 = STRING: "Eth6-ASIC0" iso.3.6.1.2.1.2.2.1.2.9012 = STRING: "Eth7-ASIC0" ... --- src/sonic_ax_impl/mibs/__init__.py | 26 +++++++++++++++++++++----- tests/mock_tables/dbconnector.py | 3 ++- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/sonic_ax_impl/mibs/__init__.py b/src/sonic_ax_impl/mibs/__init__.py index dde858bc26e9..e482169de035 100644 --- a/src/sonic_ax_impl/mibs/__init__.py +++ b/src/sonic_ax_impl/mibs/__init__.py @@ -3,12 +3,13 @@ import os from swsscommon.swsscommon import SonicV2Connector -from swsssdk import SonicDBConfig +from swsscommon.swsscommon import SonicDBConfig from swsssdk import port_util from swsssdk.port_util import get_index_from_str from ax_interface.mib import MIBUpdater from ax_interface.util import oid2tuple from sonic_ax_impl import logger +from sonic_py_common import multi_asic COUNTERS_PORT_NAME_MAP = 'COUNTERS_PORT_NAME_MAP' COUNTERS_QUEUE_NAME_MAP = 'COUNTERS_QUEUE_NAME_MAP' @@ -218,7 +219,12 @@ def init_db(): Connects to DB :return: db_conn """ - SonicDBConfig.load_sonic_global_db_config() + if not SonicDBConfig.isInit(): + if multi_asic.is_multi_asic(): + # Load the global config file database_global.json once. + SonicDBConfig.load_sonic_global_db_config() + else: + SonicDBConfig.load_sonic_db_config() # SyncD database connector. THIS MUST BE INITIALIZED ON A PER-THREAD BASIS. # Redis PubSub objects (such as those within swsssdk) are NOT thread-safe. db_conn = SonicV2Connector(**redis_kwargs) @@ -538,10 +544,20 @@ class Namespace: @staticmethod def init_namespace_dbs(): db_conn = [] - SonicDBConfig.load_sonic_global_db_config() - for namespace in SonicDBConfig.get_ns_list(): - db = SonicV2Connector(use_unix_socket_path=True, namespace=namespace, decode_responses=True) + if not SonicDBConfig.isInit(): + if multi_asic.is_multi_asic(): + SonicDBConfig.load_sonic_global_db_config() + else: + SonicDBConfig.load_sonic_db_config() + host_namespace_idx = 0 + for idx, namespace in enumerate(SonicDBConfig.get_ns_list()): + if namespace == multi_asic.DEFAULT_NAMESPACE: + host_namespace_idx = idx + db = SonicV2Connector(use_unix_socket_path=True, namespace=namespace) db_conn.append(db) + # Ensure that db connector of default namespace is the first element of + # db_conn list. + db_conn[0], db_conn[host_namespace_idx] = db_conn[host_namespace_idx], db_conn[0] Namespace.connect_namespace_dbs(db_conn) return db_conn diff --git a/tests/mock_tables/dbconnector.py b/tests/mock_tables/dbconnector.py index b88cf1fef5a6..84ce65e7b7fd 100644 --- a/tests/mock_tables/dbconnector.py +++ b/tests/mock_tables/dbconnector.py @@ -9,6 +9,7 @@ from swsssdk import SonicDBConfig from swsssdk.interface import DBInterface from swsscommon import swsscommon +from sonic_py_common import multi_asic if sys.version_info >= (3, 0): @@ -133,12 +134,12 @@ def keys(self, pattern='*'): # Find every key that matches the pattern return [key for key in self.redis.keys() if regex.match(key)] - DBInterface._subscribe_keyspace_notification = _subscribe_keyspace_notification mockredis.MockRedis.config_set = config_set redis.StrictRedis = SwssSyncClient SonicV2Connector.connect = connect_SonicV2Connector swsscommon.SonicV2Connector = SonicV2Connector +swsscommon.SonicDBConfig = SonicDBConfig # pytest case collecting will import some module before monkey patch, so reload from importlib import reload