1 //
2 //
3 // Copyright 2018 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #include <grpc/support/port_platform.h>
20 
21 #include "src/cpp/server/channelz/channelz_service.h"
22 
23 #include <grpc/support/alloc.h>
24 #include <grpcpp/impl/codegen/config_protobuf.h>
25 
26 // IWYU pragma: no_include "google/protobuf/json/json.h"
27 // IWYU pragma: no_include "google/protobuf/util/json_util.h"
28 
29 // IWYU pragma: no_include <google/protobuf/util/json_util.h>
30 
31 namespace grpc {
32 
33 namespace {
34 
ParseJson(const char * json_str,grpc::protobuf::Message * message)35 grpc::protobuf::util::Status ParseJson(const char* json_str,
36                                        grpc::protobuf::Message* message) {
37   grpc::protobuf::json::JsonParseOptions options;
38   options.case_insensitive_enum_parsing = true;
39   return grpc::protobuf::json::JsonStringToMessage(json_str, message, options);
40 }
41 
42 }  // namespace
43 
GetTopChannels(ServerContext *,const channelz::v1::GetTopChannelsRequest * request,channelz::v1::GetTopChannelsResponse * response)44 Status ChannelzService::GetTopChannels(
45     ServerContext* /*unused*/,
46     const channelz::v1::GetTopChannelsRequest* request,
47     channelz::v1::GetTopChannelsResponse* response) {
48   char* json_str = grpc_channelz_get_top_channels(request->start_channel_id());
49   if (json_str == nullptr) {
50     return Status(StatusCode::INTERNAL,
51                   "grpc_channelz_get_top_channels returned null");
52   }
53   grpc::protobuf::util::Status s = ParseJson(json_str, response);
54   gpr_free(json_str);
55   if (!s.ok()) {
56     return Status(StatusCode::INTERNAL, s.ToString());
57   }
58   return Status::OK;
59 }
60 
GetServers(ServerContext *,const channelz::v1::GetServersRequest * request,channelz::v1::GetServersResponse * response)61 Status ChannelzService::GetServers(
62     ServerContext* /*unused*/, const channelz::v1::GetServersRequest* request,
63     channelz::v1::GetServersResponse* response) {
64   char* json_str = grpc_channelz_get_servers(request->start_server_id());
65   if (json_str == nullptr) {
66     return Status(StatusCode::INTERNAL,
67                   "grpc_channelz_get_servers returned null");
68   }
69   grpc::protobuf::util::Status s = ParseJson(json_str, response);
70   gpr_free(json_str);
71   if (!s.ok()) {
72     return Status(StatusCode::INTERNAL, s.ToString());
73   }
74   return Status::OK;
75 }
76 
GetServer(ServerContext *,const channelz::v1::GetServerRequest * request,channelz::v1::GetServerResponse * response)77 Status ChannelzService::GetServer(ServerContext* /*unused*/,
78                                   const channelz::v1::GetServerRequest* request,
79                                   channelz::v1::GetServerResponse* response) {
80   char* json_str = grpc_channelz_get_server(request->server_id());
81   if (json_str == nullptr) {
82     return Status(StatusCode::INTERNAL,
83                   "grpc_channelz_get_server returned null");
84   }
85   grpc::protobuf::util::Status s = ParseJson(json_str, response);
86   gpr_free(json_str);
87   if (!s.ok()) {
88     return Status(StatusCode::INTERNAL, s.ToString());
89   }
90   return Status::OK;
91 }
92 
GetServerSockets(ServerContext *,const channelz::v1::GetServerSocketsRequest * request,channelz::v1::GetServerSocketsResponse * response)93 Status ChannelzService::GetServerSockets(
94     ServerContext* /*unused*/,
95     const channelz::v1::GetServerSocketsRequest* request,
96     channelz::v1::GetServerSocketsResponse* response) {
97   char* json_str = grpc_channelz_get_server_sockets(
98       request->server_id(), request->start_socket_id(), request->max_results());
99   if (json_str == nullptr) {
100     return Status(StatusCode::INTERNAL,
101                   "grpc_channelz_get_server_sockets returned null");
102   }
103   grpc::protobuf::util::Status s = ParseJson(json_str, response);
104   gpr_free(json_str);
105   if (!s.ok()) {
106     return Status(StatusCode::INTERNAL, s.ToString());
107   }
108   return Status::OK;
109 }
110 
GetChannel(ServerContext *,const channelz::v1::GetChannelRequest * request,channelz::v1::GetChannelResponse * response)111 Status ChannelzService::GetChannel(
112     ServerContext* /*unused*/, const channelz::v1::GetChannelRequest* request,
113     channelz::v1::GetChannelResponse* response) {
114   char* json_str = grpc_channelz_get_channel(request->channel_id());
115   if (json_str == nullptr) {
116     return Status(StatusCode::NOT_FOUND, "No object found for that ChannelId");
117   }
118   grpc::protobuf::util::Status s = ParseJson(json_str, response);
119   gpr_free(json_str);
120   if (!s.ok()) {
121     return Status(StatusCode::INTERNAL, s.ToString());
122   }
123   return Status::OK;
124 }
125 
GetSubchannel(ServerContext *,const channelz::v1::GetSubchannelRequest * request,channelz::v1::GetSubchannelResponse * response)126 Status ChannelzService::GetSubchannel(
127     ServerContext* /*unused*/,
128     const channelz::v1::GetSubchannelRequest* request,
129     channelz::v1::GetSubchannelResponse* response) {
130   char* json_str = grpc_channelz_get_subchannel(request->subchannel_id());
131   if (json_str == nullptr) {
132     return Status(StatusCode::NOT_FOUND,
133                   "No object found for that SubchannelId");
134   }
135   grpc::protobuf::util::Status s = ParseJson(json_str, response);
136   gpr_free(json_str);
137   if (!s.ok()) {
138     return Status(StatusCode::INTERNAL, s.ToString());
139   }
140   return Status::OK;
141 }
142 
GetSocket(ServerContext *,const channelz::v1::GetSocketRequest * request,channelz::v1::GetSocketResponse * response)143 Status ChannelzService::GetSocket(ServerContext* /*unused*/,
144                                   const channelz::v1::GetSocketRequest* request,
145                                   channelz::v1::GetSocketResponse* response) {
146   char* json_str = grpc_channelz_get_socket(request->socket_id());
147   if (json_str == nullptr) {
148     return Status(StatusCode::NOT_FOUND, "No object found for that SocketId");
149   }
150   grpc::protobuf::util::Status s = ParseJson(json_str, response);
151   gpr_free(json_str);
152   if (!s.ok()) {
153     return Status(StatusCode::INTERNAL, s.ToString());
154   }
155   return Status::OK;
156 }
157 
158 }  // namespace grpc
159