Debian Codegraph @

src/cpp/server/channelz/channelz_service.cc

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