-
Notifications
You must be signed in to change notification settings - Fork 342
Add ProfileProvider class to support read profile config from PROFILE_DB. #683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
ed04b93
Install libyang in azure pipeline
liuh-80 3d76913
Add profile config provider.
liuh-80 364f353
Fix missing file
liuh-80 269b00e
Fix file mode
liuh-80 44c9a75
Fix build issue
liuh-80 61db251
Fix config issue
liuh-80 4c3c951
Remove add new DB from redis config
liuh-80 6dcbee5
Merge branch 'dev/liuh/install-yang-to-pipeline' into dev/liuh/profil…
liuh-80 4a12abf
Add new database for UT
liuh-80 92acd07
Fix empty file issue
liuh-80 9b60524
Merge remote-tracking branch 'origin' into dev/liuh/profile-provider
liuh-80 57b9f59
Improve UT config file
liuh-80 02af224
Fix build issue
liuh-80 c558e6b
Improve UT
liuh-80 9b0653d
Refactor getall method
liuh-80 508ed04
Merge branch 'dev/liuh/refact-getall' into dev/liuh/profile-provider
liuh-80 35b6774
Remove unecessary change
liuh-80 a2094ce
revert db config change.
liuh-80 eef426a
Fix DB seperator issue
liuh-80 4375c0a
Merge remote-tracking branch 'origin' into dev/liuh/profile-provider
liuh-80 c2a377e
Fix mode issue
liuh-80 448a0c4
Merge remote-tracking branch 'origin' into dev/liuh/profile-provider
liuh-80 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| #include <boost/algorithm/string.hpp> | ||
| #include <cassert> | ||
| #include <iostream> | ||
| #include <map> | ||
| #include <string> | ||
| #include <tuple> | ||
| #include <vector> | ||
| #include <dirent.h> | ||
| #include <stdio.h> | ||
|
|
||
| #include "profileprovider.h" | ||
| #include "logger.h" | ||
| #include "table.h" | ||
| #include "schema.h" | ||
|
|
||
| using namespace std; | ||
| using namespace swss; | ||
|
|
||
| ProfileProvider& ProfileProvider::instance() | ||
| { | ||
| static ProfileProvider instance; | ||
| return instance; | ||
| } | ||
|
|
||
| shared_ptr<string> ProfileProvider::getConfig(const string &table, const string &key, const string &field, DBConnector* cfgDbConnector) | ||
| { | ||
| assert(!table.empty()); | ||
| assert(!key.empty()); | ||
| assert(!field.empty()); | ||
|
|
||
| auto staticConfig = getConfigs(table, key, cfgDbConnector); | ||
| auto result = staticConfig.find(field); | ||
| if (result != staticConfig.end()) | ||
| { | ||
| return make_shared<string>(result->second); | ||
| } | ||
|
|
||
| // Config not found is different with 'empty' config | ||
| return nullptr; | ||
| } | ||
|
|
||
| bool ProfileProvider::appendConfigs(const string &table, const string &key, vector<pair<string, string> > &values, DBConnector* cfgDbConnector) | ||
| { | ||
| assert(!table.empty()); | ||
| assert(!key.empty()); | ||
|
|
||
| SWSS_LOG_DEBUG("DefaultValueProvider::AppendDefaultValues %s %s\n", table.c_str(), key.c_str()); | ||
|
|
||
| auto staticConfig = getConfigs(table, key, cfgDbConnector); | ||
|
|
||
| map<string, string> existedValues; | ||
| for (auto& fieldValuePair : values) | ||
| { | ||
| existedValues.emplace(fieldValuePair.first, fieldValuePair.second); | ||
| } | ||
|
|
||
| bool appendValues = false; | ||
| for (auto& fieldValuePair : staticConfig) | ||
| { | ||
| auto findresult = existedValues.find(fieldValuePair.first); | ||
| if (findresult == existedValues.end()) | ||
| { | ||
| appendValues = true; | ||
| values.emplace_back(fieldValuePair.first, fieldValuePair.second); | ||
| } | ||
| } | ||
|
|
||
| return appendValues; | ||
| } | ||
|
|
||
| map<string, string> ProfileProvider::getConfigs(const string &table, const string &key, DBConnector* cfgDbConnector) | ||
| { | ||
| if (itemDeleted(table, key, cfgDbConnector)) | ||
| { | ||
| SWSS_LOG_DEBUG("DefaultValueProvider::GetConfigs item %s %s deleted.\n", table.c_str(), key.c_str()); | ||
| map<string, string> map; | ||
| return map; | ||
| } | ||
|
|
||
| auto& staticCfgDbConnector = getStaticCfgDBConnector(cfgDbConnector); | ||
| auto itemkey = getKeyName(table, key, &staticCfgDbConnector); | ||
| return staticCfgDbConnector.hgetall<map<string, string>>(itemkey); | ||
| } | ||
|
|
||
| map<string, map<string, map<string, string>>> ProfileProvider::getConfigs(DBConnector* cfgDbConnector) | ||
| { | ||
| auto& staticCfgDbConnector = getStaticCfgDBConnector(cfgDbConnector); | ||
| auto configs = staticCfgDbConnector.getall(); | ||
|
|
||
| // If a profile item mark as 'deleted', it's shoud not exist in result. | ||
| list<pair<string, string>> deletedItems; | ||
| for(auto const& tableItem: configs) | ||
| { | ||
| auto table = tableItem.first; | ||
| for(auto const& item: tableItem.second) | ||
| { | ||
| auto key = item.first; | ||
| if (itemDeleted(table, key, cfgDbConnector)) | ||
| { | ||
| SWSS_LOG_DEBUG("DefaultValueProvider::GetConfigs item %s %s deleted.\n", table.c_str(), key.c_str()); | ||
| deletedItems.push_back(make_pair(table, key)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for(auto const& deletedItem: deletedItems) | ||
| { | ||
| auto table = deletedItem.first; | ||
| auto key = deletedItem.second; | ||
| SWSS_LOG_DEBUG("DefaultValueProvider::GetConfigs remove deleted item %s %s from result.\n", table.c_str(), key.c_str()); | ||
| configs[table].erase(key); | ||
| } | ||
|
|
||
| return configs; | ||
| } | ||
|
|
||
| vector<string> ProfileProvider::getKeys(const string &table, DBConnector* cfgDbConnector) | ||
| { | ||
| auto& staticCfgDbConnector = getStaticCfgDBConnector(cfgDbConnector); | ||
| auto pattern = getKeyName(table, "*", &staticCfgDbConnector); | ||
| auto keys = staticCfgDbConnector.keys(pattern); | ||
|
|
||
| const auto separator = SonicDBConfig::getSeparator(&staticCfgDbConnector); | ||
| vector<string> result; | ||
| for(auto const& itemKey: keys) | ||
| { | ||
| size_t pos = itemKey.find(separator); | ||
| if (pos == string::npos) | ||
| { | ||
| SWSS_LOG_DEBUG("DefaultValueProvider::GetConfigs can't find separator %s in %s.\n", separator.c_str(), itemKey.c_str()); | ||
| continue; | ||
| } | ||
|
|
||
| auto row = itemKey.substr(pos + 1); | ||
| if (!itemDeleted(table, row, cfgDbConnector)) | ||
| { | ||
| result.push_back(row); | ||
| } | ||
| else | ||
| { | ||
| SWSS_LOG_DEBUG("DefaultValueProvider::GetConfigs item %s %s deleted.\n", table.c_str(), row.c_str()); | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| ProfileProvider::ProfileProvider() | ||
| { | ||
| } | ||
|
|
||
| ProfileProvider::~ProfileProvider() | ||
| { | ||
| } | ||
|
|
||
| bool ProfileProvider::tryRevertItem(const string &table, const string &key, DBConnector* cfgDbConnector) | ||
| { | ||
| if (itemDeleted(table, key, cfgDbConnector)) | ||
| { | ||
| revertItem(table, key, cfgDbConnector); | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| bool ProfileProvider::tryDeleteItem(const string &table, const string &key, DBConnector* cfgDbConnector) | ||
| { | ||
| if (!itemDeleted(table, key, cfgDbConnector)) | ||
| { | ||
| deleteItem(table, key, cfgDbConnector); | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| bool ProfileProvider::itemDeleted(const string &table, const string &key, DBConnector* cfgDbConnector) | ||
| { | ||
| auto deletedkey = getDeletedKeyName(table, key, cfgDbConnector); | ||
| return cfgDbConnector->exists(deletedkey) == true; | ||
| } | ||
|
|
||
| void ProfileProvider::deleteItem(const string &table, const string &key, DBConnector* cfgDbConnector) | ||
| { | ||
| auto deletedkey = getDeletedKeyName(table, key, cfgDbConnector); | ||
| // Only need deletedkey to mark the item is deleted. | ||
| cfgDbConnector->hset(deletedkey, "", ""); | ||
| } | ||
|
|
||
| void ProfileProvider::revertItem(const string &table, const string &key, DBConnector* cfgDbConnector) | ||
| { | ||
| auto deletedkey = getDeletedKeyName(table, key,cfgDbConnector); | ||
| cfgDbConnector->del(deletedkey); | ||
| } | ||
|
|
||
| DBConnector& ProfileProvider::getStaticCfgDBConnector(DBConnector* cfgDbConnector) | ||
| { | ||
| auto ns = cfgDbConnector->getNamespace(); | ||
| auto result = m_staticCfgDBMap.find(ns); | ||
| if (result != m_staticCfgDBMap.end()) | ||
| { | ||
| return result->second; | ||
| } | ||
|
|
||
| // Create new DBConnector instance to PROFILE_DB | ||
| const string staticDbName = "PROFILE_DB"; | ||
| m_staticCfgDBMap.emplace(piecewise_construct, | ||
| forward_as_tuple(ns), | ||
| forward_as_tuple(staticDbName, SonicDBConfig::getDbId(staticDbName, ns), true, ns)); | ||
|
|
||
| result = m_staticCfgDBMap.find(ns); | ||
| return result->second; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| #pragma once | ||
|
|
||
| #include <map> | ||
| #include <string> | ||
| #include <vector> | ||
| #include "common/table.h" | ||
| #include "common/dbconnector.h" | ||
| #include "common/converter.h" | ||
|
|
||
| namespace swss { | ||
|
|
||
| const std::string DELETED_KEY_SEPARATOR = "_"; | ||
|
|
||
| class ProfileProvider | ||
| { | ||
| public: | ||
| static ProfileProvider& instance(); | ||
|
|
||
| bool appendConfigs(const std::string &table, const std::string &key, std::vector<std::pair<std::string, std::string> > &values, DBConnector* cfgDbConnector); | ||
|
|
||
| std::shared_ptr<std::string> getConfig(const std::string &table, const std::string &key, const std::string &field, DBConnector* cfgDbConnector); | ||
|
|
||
| std::map<std::string, std::string> getConfigs(const std::string &table, const std::string &key, DBConnector* cfgDbConnector); | ||
|
|
||
| std::map<std::string, std::map<std::string, std::map<std::string, std::string>>> getConfigs(DBConnector* cfgDbConnector); | ||
|
|
||
| std::vector<std::string> getKeys(const std::string &table, DBConnector* cfgDbConnector); | ||
|
|
||
| bool tryRevertItem(const std::string &table, const std::string &key, DBConnector* cfgDbConnector); | ||
|
|
||
| bool tryDeleteItem(const std::string &table, const std::string &key, DBConnector* cfgDbConnector); | ||
|
|
||
| std::string getDeletedKeyName(const std::string &table, const std::string &key, DBConnector* dbConnector) | ||
| { | ||
| auto itemKey = to_upper(table) + DELETED_KEY_SEPARATOR + key; | ||
| return getKeyName(PROFILE_DELETE_TABLE, itemKey, dbConnector); | ||
| } | ||
|
|
||
| private: | ||
| ProfileProvider(); | ||
| ~ProfileProvider(); | ||
|
|
||
| std::string getKeyName(const std::string &table, const std::string &key, DBConnector* dbConnector) | ||
| { | ||
| const auto separator = SonicDBConfig::getSeparator(dbConnector); | ||
| // Profile DB follow Config DB: table name is case insensetive. | ||
| return to_upper(table) + separator + key; | ||
| } | ||
|
|
||
| bool itemDeleted(const std::string &table, const std::string &key, DBConnector* cfgDbConnector); | ||
|
|
||
| void deleteItem(const std::string &table, const std::string &key, DBConnector* cfgDbConnector); | ||
|
|
||
| void revertItem(const std::string &table, const std::string &key, DBConnector* cfgDbConnector); | ||
|
|
||
| DBConnector& getStaticCfgDBConnector(DBConnector* cfgDbConnector); | ||
|
|
||
| std::map<std::string, DBConnector> m_staticCfgDBMap; | ||
| }; | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File change in configdb.cpp dbconnector.cpp and dbconnector.h are from another PR:
#692