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