• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/cpp/server/channelz/channelz_service.h"
20 
21 #include <grpc/support/alloc.h>
22 #include <grpc/support/port_platform.h>
23 #include <grpcpp/impl/codegen/config_protobuf.h>
24 
25 #include <memory>
26 
27 // IWYU pragma: no_include "google/protobuf/json/json.h"
28 // IWYU pragma: no_include "google/protobuf/util/json_util.h"
29 
30 // IWYU pragma: no_include <google/protobuf/util/json_util.h>
31 
32 namespace grpc {
33 
34 namespace {
35 
ParseJson(const char * json_str,grpc::protobuf::Message * message)36 grpc::protobuf::util::Status ParseJson(const char* json_str,
37                                        grpc::protobuf::Message* message) {
38   grpc::protobuf::json::JsonParseOptions options;
39   options.case_insensitive_enum_parsing = true;
40   return grpc::protobuf::json::JsonStringToMessage(json_str, message, options);
41 }
42 
43 }  // namespace
44 
GetTopChannels(ServerContext *,const channelz::v1::GetTopChannelsRequest * request,channelz::v1::GetTopChannelsResponse * response)45 Status ChannelzService::GetTopChannels(
46     ServerContext* /*unused*/,
47     const channelz::v1::GetTopChannelsRequest* request,
48     channelz::v1::GetTopChannelsResponse* response) {
49   char* json_str = grpc_channelz_get_top_channels(request->start_channel_id());
50   if (json_str == nullptr) {
51     return Status(StatusCode::INTERNAL,
52                   "grpc_channelz_get_top_channels returned null");
53   }
54   grpc::protobuf::util::Status s = ParseJson(json_str, response);
55   gpr_free(json_str);
56   if (!s.ok()) {
57     return Status(StatusCode::INTERNAL, s.ToString());
58   }
59   return Status::OK;
60 }
61 
GetServers(ServerContext *,const channelz::v1::GetServersRequest * request,channelz::v1::GetServersResponse * response)62 Status ChannelzService::GetServers(
63     ServerContext* /*unused*/, const channelz::v1::GetServersRequest* request,
64     channelz::v1::GetServersResponse* response) {
65   char* json_str = grpc_channelz_get_servers(request->start_server_id());
66   if (json_str == nullptr) {
67     return Status(StatusCode::INTERNAL,
68                   "grpc_channelz_get_servers returned null");
69   }
70   grpc::protobuf::util::Status s = ParseJson(json_str, response);
71   gpr_free(json_str);
72   if (!s.ok()) {
73     return Status(StatusCode::INTERNAL, s.ToString());
74   }
75   return Status::OK;
76 }
77 
GetServer(ServerContext *,const channelz::v1::GetServerRequest * request,channelz::v1::GetServerResponse * response)78 Status ChannelzService::GetServer(ServerContext* /*unused*/,
79                                   const channelz::v1::GetServerRequest* request,
80                                   channelz::v1::GetServerResponse* response) {
81   char* json_str = grpc_channelz_get_server(request->server_id());
82   if (json_str == nullptr) {
83     return Status(StatusCode::INTERNAL,
84                   "grpc_channelz_get_server returned null");
85   }
86   grpc::protobuf::util::Status s = ParseJson(json_str, response);
87   gpr_free(json_str);
88   if (!s.ok()) {
89     return Status(StatusCode::INTERNAL, s.ToString());
90   }
91   return Status::OK;
92 }
93 
GetServerSockets(ServerContext *,const channelz::v1::GetServerSocketsRequest * request,channelz::v1::GetServerSocketsResponse * response)94 Status ChannelzService::GetServerSockets(
95     ServerContext* /*unused*/,
96     const channelz::v1::GetServerSocketsRequest* request,
97     channelz::v1::GetServerSocketsResponse* response) {
98   char* json_str = grpc_channelz_get_server_sockets(
99       request->server_id(), request->start_socket_id(), request->max_results());
100   if (json_str == nullptr) {
101     return Status(StatusCode::INTERNAL,
102                   "grpc_channelz_get_server_sockets returned null");
103   }
104   grpc::protobuf::util::Status s = ParseJson(json_str, response);
105   gpr_free(json_str);
106   if (!s.ok()) {
107     return Status(StatusCode::INTERNAL, s.ToString());
108   }
109   return Status::OK;
110 }
111 
GetChannel(ServerContext *,const channelz::v1::GetChannelRequest * request,channelz::v1::GetChannelResponse * response)112 Status ChannelzService::GetChannel(
113     ServerContext* /*unused*/, const channelz::v1::GetChannelRequest* request,
114     channelz::v1::GetChannelResponse* response) {
115   char* json_str = grpc_channelz_get_channel(request->channel_id());
116   if (json_str == nullptr) {
117     return Status(StatusCode::NOT_FOUND, "No object found for that ChannelId");
118   }
119   grpc::protobuf::util::Status s = ParseJson(json_str, response);
120   gpr_free(json_str);
121   if (!s.ok()) {
122     return Status(StatusCode::INTERNAL, s.ToString());
123   }
124   return Status::OK;
125 }
126 
GetSubchannel(ServerContext *,const channelz::v1::GetSubchannelRequest * request,channelz::v1::GetSubchannelResponse * response)127 Status ChannelzService::GetSubchannel(
128     ServerContext* /*unused*/,
129     const channelz::v1::GetSubchannelRequest* request,
130     channelz::v1::GetSubchannelResponse* response) {
131   char* json_str = grpc_channelz_get_subchannel(request->subchannel_id());
132   if (json_str == nullptr) {
133     return Status(StatusCode::NOT_FOUND,
134                   "No object found for that SubchannelId");
135   }
136   grpc::protobuf::util::Status s = ParseJson(json_str, response);
137   gpr_free(json_str);
138   if (!s.ok()) {
139     return Status(StatusCode::INTERNAL, s.ToString());
140   }
141   return Status::OK;
142 }
143 
GetSocket(ServerContext *,const channelz::v1::GetSocketRequest * request,channelz::v1::GetSocketResponse * response)144 Status ChannelzService::GetSocket(ServerContext* /*unused*/,
145                                   const channelz::v1::GetSocketRequest* request,
146                                   channelz::v1::GetSocketResponse* response) {
147   char* json_str = grpc_channelz_get_socket(request->socket_id());
148   if (json_str == nullptr) {
149     return Status(StatusCode::NOT_FOUND, "No object found for that SocketId");
150   }
151   grpc::protobuf::util::Status s = ParseJson(json_str, response);
152   gpr_free(json_str);
153   if (!s.ok()) {
154     return Status(StatusCode::INTERNAL, s.ToString());
155   }
156   return Status::OK;
157 }
158 
159 }  // namespace grpc
160