diff --git a/common/dbconnector.cpp b/common/dbconnector.cpp index 9595ea389..f2c162387 100755 --- a/common/dbconnector.cpp +++ b/common/dbconnector.cpp @@ -447,6 +447,22 @@ vector SonicDBConfig::getNamespaces() return vector(list.begin(), list.end()); } +vector SonicDBConfig::getDbKeys() +{ + vector keys; + std::lock_guard guard(m_db_info_mutex); + + if (!m_init) + initialize(DEFAULT_SONIC_DB_CONFIG_FILE); + + // This API returns back all db keys. + for (auto it = m_inst_info.cbegin(); it != m_inst_info.cend(); ++it) { + keys.push_back(it->first); + } + + return keys; +} + std::vector SonicDBConfig::getDbList(const std::string &netns, const std::string &containerName) { SonicDBKey key; diff --git a/common/dbconnector.h b/common/dbconnector.h index 6450b73b6..c5bd48ad6 100644 --- a/common/dbconnector.h +++ b/common/dbconnector.h @@ -127,6 +127,7 @@ class SonicDBConfig static int getDbPort(const std::string &dbName, const std::string &netns = EMPTY_NAMESPACE, const std::string &containerName=EMPTY_CONTAINERNAME); static int getDbPort(const std::string &dbName, const SonicDBKey &key); static std::vector getNamespaces(); + static std::vector getDbKeys(); #if defined(SWIG) && defined(SWIGPYTHON) %pythoncode %{ ## TODO: the python function and C++ one is not on-par diff --git a/pyext/swsscommon.i b/pyext/swsscommon.i index 6218de6ee..81b9cdb19 100644 --- a/pyext/swsscommon.i +++ b/pyext/swsscommon.i @@ -80,6 +80,7 @@ %template(GetConfigResult) std::map>>; %template(GetInstanceListResult) std::map; %template(KeyOpFieldsValuesQueue) std::deque>>>; +%template(VectorSonicDbKey) std::vector; #ifdef SWIGPYTHON %exception { diff --git a/tests/redis_multi_ns_ut.cpp b/tests/redis_multi_ns_ut.cpp index ca379ecf1..910e1cd9b 100644 --- a/tests/redis_multi_ns_ut.cpp +++ b/tests/redis_multi_ns_ut.cpp @@ -19,6 +19,8 @@ TEST(DBConnector, multi_ns_test) std::string local_file, dir_name; set namespaces; vector ns_names; + vector dbkeys; + vector target_dbkeys; // load global config file again, should throw exception with init already done try @@ -67,7 +69,12 @@ TEST(DBConnector, multi_ns_test) { key.netns = element["namespace"]; } + if (!element["container_name"].empty()) + { + key.containerName = element["container_name"]; + } namespaces.insert(key.netns); + target_dbkeys.push_back(key); if (!element["container_name"].empty()) { @@ -136,6 +143,18 @@ TEST(DBConnector, multi_ns_test) sort (ns_names.begin(), ns_names.end()); EXPECT_EQ(ns_names.size(), namespaces.size()); EXPECT_TRUE(std::equal(namespaces.begin(), namespaces.end(), ns_names.begin())); + + dbkeys = SonicDBConfig::getDbKeys(); + EXPECT_EQ(dbkeys.size(), target_dbkeys.size()); + for (auto &key : target_dbkeys) + { + cout<< "target key " << key.toString() <