proto_2_json.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include <google/protobuf/message.h>
  2. #include <google/protobuf/util/json_util.h>
  3. #include "proto/alarm/v1/config.pb.h"
  4. #include "proto/alarm/v1/stream.pb.h"
  5. #include "proto/aclk/v1/lib.pb.h"
  6. #include "proto/chart/v1/config.pb.h"
  7. #include "proto/chart/v1/stream.pb.h"
  8. #include "proto/agent/v1/connection.pb.h"
  9. #include "proto/agent/v1/disconnect.pb.h"
  10. #include "proto/nodeinstance/connection/v1/connection.pb.h"
  11. #include "proto/nodeinstance/create/v1/creation.pb.h"
  12. #include "proto/nodeinstance/info/v1/info.pb.h"
  13. #include "proto/context/v1/stream.pb.h"
  14. #include "proto/context/v1/context.pb.h"
  15. #include "libnetdata/libnetdata.h"
  16. #include "proto_2_json.h"
  17. using namespace google::protobuf::util;
  18. static google::protobuf::Message *msg_name_to_protomsg(const char *msgname)
  19. {
  20. //tx side
  21. if (!strcmp(msgname, "UpdateAgentConnection"))
  22. return new agent::v1::UpdateAgentConnection;
  23. if (!strcmp(msgname, "UpdateNodeInstanceConnection"))
  24. return new nodeinstance::v1::UpdateNodeInstanceConnection;
  25. if (!strcmp(msgname, "CreateNodeInstance"))
  26. return new nodeinstance::create::v1::CreateNodeInstance;
  27. if (!strcmp(msgname, "ChartsAndDimensionsUpdated"))
  28. return new chart::v1::ChartsAndDimensionsUpdated;
  29. if (!strcmp(msgname, "ChartConfigsUpdated"))
  30. return new chart::v1::ChartConfigsUpdated;
  31. if (!strcmp(msgname, "ResetChartMessages"))
  32. return new chart::v1::ResetChartMessages;
  33. if (!strcmp(msgname, "RetentionUpdated"))
  34. return new chart::v1::RetentionUpdated;
  35. if (!strcmp(msgname, "UpdateNodeInfo"))
  36. return new nodeinstance::info::v1::UpdateNodeInfo;
  37. if (!strcmp(msgname, "AlarmLogHealth"))
  38. return new alarms::v1::AlarmLogHealth;
  39. if (!strcmp(msgname, "ProvideAlarmConfiguration"))
  40. return new alarms::v1::ProvideAlarmConfiguration;
  41. if (!strcmp(msgname, "AlarmSnapshot"))
  42. return new alarms::v1::AlarmSnapshot;
  43. if (!strcmp(msgname, "AlarmLogEntry"))
  44. return new alarms::v1::AlarmLogEntry;
  45. if (!strcmp(msgname, "UpdateNodeCollectors"))
  46. return new nodeinstance::info::v1::UpdateNodeCollectors;
  47. if (!strcmp(msgname, "ContextsUpdated"))
  48. return new context::v1::ContextsUpdated;
  49. if (!strcmp(msgname, "ContextsSnapshot"))
  50. return new context::v1::ContextsSnapshot;
  51. //rx side
  52. if (!strcmp(msgname, "CreateNodeInstanceResult"))
  53. return new nodeinstance::create::v1::CreateNodeInstanceResult;
  54. if (!strcmp(msgname, "SendNodeInstances"))
  55. return new agent::v1::SendNodeInstances;
  56. if (!strcmp(msgname, "StreamChartsAndDimensions"))
  57. return new chart::v1::StreamChartsAndDimensions;
  58. if (!strcmp(msgname, "ChartsAndDimensionsAck"))
  59. return new chart::v1::ChartsAndDimensionsAck;
  60. if (!strcmp(msgname, "UpdateChartConfigs"))
  61. return new chart::v1::UpdateChartConfigs;
  62. if (!strcmp(msgname, "StartAlarmStreaming"))
  63. return new alarms::v1::StartAlarmStreaming;
  64. if (!strcmp(msgname, "SendAlarmLogHealth"))
  65. return new alarms::v1::SendAlarmLogHealth;
  66. if (!strcmp(msgname, "SendAlarmConfiguration"))
  67. return new alarms::v1::SendAlarmConfiguration;
  68. if (!strcmp(msgname, "SendAlarmSnapshot"))
  69. return new alarms::v1::SendAlarmSnapshot;
  70. if (!strcmp(msgname, "DisconnectReq"))
  71. return new agent::v1::DisconnectReq;
  72. if (!strcmp(msgname, "ContextsCheckpoint"))
  73. return new context::v1::ContextsCheckpoint;
  74. if (!strcmp(msgname, "StopStreamingContexts"))
  75. return new context::v1::StopStreamingContexts;
  76. return NULL;
  77. }
  78. char *protomsg_to_json(const void *protobin, size_t len, const char *msgname)
  79. {
  80. google::protobuf::Message *msg = msg_name_to_protomsg(msgname);
  81. if (msg == NULL)
  82. return strdupz("Don't know this message type by name.");
  83. if (!msg->ParseFromArray(protobin, len))
  84. return strdupz("Can't parse this message. Malformed or wrong parser used.");
  85. JsonPrintOptions options;
  86. std::string output;
  87. google::protobuf::util::MessageToJsonString(*msg, &output, options);
  88. delete msg;
  89. return strdupz(output.c_str());
  90. }