proto_2_json.cc 3.3 KB

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