diff --git a/common/Makefile.am b/common/Makefile.am index 5ea63f82..d7855ccb 100644 --- a/common/Makefile.am +++ b/common/Makefile.am @@ -75,7 +75,7 @@ common_libswsscommon_la_SOURCES = \ common_libswsscommon_la_CXXFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(LIBNL_CFLAGS) $(CODE_COVERAGE_CXXFLAGS) common_libswsscommon_la_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(LIBNL_CPPFLAGS) $(CODE_COVERAGE_CPPFLAGS) -common_libswsscommon_la_LIBADD = -lpthread $(LIBNL_LIBS) $(CODE_COVERAGE_LIBS) -lzmq -lboost_serialization -luuid -lyang +common_libswsscommon_la_LIBADD = -lpthread $(LIBNL_LIBS) $(CODE_COVERAGE_LIBS) -lzmq -lboost_serialization -luuid -lyang -ldashapi common_libswsscommon_la_LDFLAGS = -Wl,-z,now $(LDFLAGS) common_swssloglevel_SOURCES = \ diff --git a/common/redisreply.cpp b/common/redisreply.cpp index 8e3e7360..0d304851 100644 --- a/common/redisreply.cpp +++ b/common/redisreply.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include "common/logger.h" @@ -14,6 +15,8 @@ #include "common/rediscommand.h" #include "common/stringutility.h" +#include + using namespace std; using namespace boost; @@ -276,7 +279,7 @@ string RedisReply::to_string() return RedisReply::to_string(this->getContext()); } -string RedisReply::to_string(redisReply *reply, string command) +string RedisReply::to_string(redisReply *reply, const string &command) { /* Response format need keep as same as redis-py, redis-py using a command to result type mapping to convert result: @@ -308,7 +311,31 @@ string RedisReply::to_string(redisReply *reply, string command) } } -string RedisReply::formatReply(string command, long long integer) +string RedisReply::to_string(redisReply *reply,const vector &commands) +{ + if (commands.empty()) + { + return to_string(reply); + } + + auto command = boost::to_upper_copy(commands[0]); + + if (commands.size() > 1) + { + if (reply->type == REDIS_REPLY_ARRAY + && command == "HGETALL" + && reply->elements > 1 + && to_string(reply->element[0]) == PROTOBUF_TYPE_TAG + && reply->element[1]->type != REDIS_REPLY_STRING) + { + return formatPbReply(reply->element, reply->elements, commands[1]); + } + } + + return to_string(reply, command); +} + +string RedisReply::formatReply(const string &command, long long integer) { if (g_intToBoolCommands.find(command) != g_intToBoolCommands.end()) { @@ -332,7 +359,7 @@ string RedisReply::formatReply(string command, long long integer) return std::to_string(integer); } -string RedisReply::formatReply(string command, const char* str, size_t len) +string RedisReply::formatReply(const string &command, const char* str, size_t len) { string result = string(str, len); if (g_strToBoolCommands.find(command) != g_strToBoolCommands.end() @@ -357,7 +384,7 @@ string RedisReply::formatReply(string command, const char* str, size_t len) return result; } -string RedisReply::formatReply(string command, struct redisReply **element, size_t elements) +string RedisReply::formatReply(const string &command, struct redisReply **element, size_t elements) { if (command == "HGETALL") { @@ -475,7 +502,31 @@ string RedisReply::formatTupleReply(struct redisReply **element, size_t elements return swss::join(", ", '(', ')', elementvector.begin(), elementvector.end()); } -string RedisReply::formatStringWithQuot(string str) +string RedisReply::formatPbReply(struct redisReply **element, size_t elements, const string &key) +{ + if ( + elements != 2 + || to_string(element[0]) != PROTOBUF_TYPE_TAG + || element[1]->type != REDIS_REPLY_STRING) + { + throw system_error(make_error_code(errc::io_error), + "Invalid result"); + } + + std::string pb_buffer(element[1]->str, element[1]->len); + + // Extract table name + std::string table_name( + key.begin(), + find_if( + key.begin(), + key.end(), + [](char c){ return c == ':' || c == '|';})); + + return dash::PbBinaryToJsonString(table_name, pb_buffer); +} + +string RedisReply::formatStringWithQuot(const string &str) { if (str.find('\'') != std::string::npos) { diff --git a/common/redisreply.h b/common/redisreply.h index e9d126ad..e4575e3b 100644 --- a/common/redisreply.h +++ b/common/redisreply.h @@ -94,22 +94,24 @@ class RedisReply std::string to_string(); - static std::string to_string(redisReply *reply, std::string command = std::string()); + static std::string to_string(redisReply *reply,const std::string &command = std::string()); + static std::string to_string(redisReply *reply,const std::vector &commands); private: void checkStatus(const char *status); void checkReply(); - static std::string formatReply(std::string command, struct redisReply **element, size_t elements); - static std::string formatReply(std::string command, long long integer); - static std::string formatReply(std::string command, const char* str, size_t len); + static std::string formatReply(const std::string &command, struct redisReply **element, size_t elements); + static std::string formatReply(const std::string &command, long long integer); + static std::string formatReply(const std::string &command, const char* str, size_t len); static std::string formatSscanReply(struct redisReply **element, size_t elements); static std::string formatHscanReply(struct redisReply **element, size_t elements); static std::string formatDictReply(struct redisReply **element, size_t elements); static std::string formatArrayReply(struct redisReply **element, size_t elements); static std::string formatListReply(struct redisReply **element, size_t elements); static std::string formatTupleReply(struct redisReply **element, size_t elements); - static std::string formatStringWithQuot(std::string str); + static std::string formatPbReply(struct redisReply **element, size_t elements, const std::string &key); + static std::string formatStringWithQuot(const std::string &str); redisReply *m_reply; }; diff --git a/common/schema.h b/common/schema.h index 9622ea49..25f3ded8 100644 --- a/common/schema.h +++ b/common/schema.h @@ -532,6 +532,8 @@ namespace swss { #define DEL_COMMAND "DEL" #define EMPTY_PREFIX "" +#define PROTOBUF_TYPE_TAG "pb" + #define CFG_DTEL_TABLE_NAME "DTEL" #define CFG_DTEL_REPORT_SESSION_TABLE_NAME "DTEL_REPORT_SESSION" #define CFG_DTEL_INT_SESSION_TABLE_NAME "DTEL_INT_SESSION" diff --git a/sonic-db-cli/sonic-db-cli.cpp b/sonic-db-cli/sonic-db-cli.cpp index f253d522..48f3ea0c 100755 --- a/sonic-db-cli/sonic-db-cli.cpp +++ b/sonic-db-cli/sonic-db-cli.cpp @@ -168,8 +168,7 @@ int executeCommands( based on our usage in SONiC, None and list type output from python API needs to be modified with these changes, it is enough for us to mimic redis-cli in SONiC so far since no application uses tty mode redis-cli output */ - auto commandName = getCommandName(commands); - cout << RedisReply::to_string(reply.getContext(), commandName) << endl; + cout << RedisReply::to_string(reply.getContext(), commands) << endl; } catch (const std::system_error& e) { @@ -371,13 +370,3 @@ int cli_exception_wrapper( return 1; } } - -string getCommandName(vector& commands) -{ - if (commands.size() == 0) - { - return string(); - } - - return boost::to_upper_copy(commands[0]); -} \ No newline at end of file