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