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