c44a9ac0de6b91de1b0c4b1e4cf940ae6daa7a17
[platform/upstream/grpc.git] / src / cpp / server / channelz / channelz_service.cc
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 Status ChannelzService::GetTopChannels(
29     ServerContext* unused, const channelz::v1::GetTopChannelsRequest* request,
30     channelz::v1::GetTopChannelsResponse* response) {
31   char* json_str = grpc_channelz_get_top_channels(request->start_channel_id());
32   if (json_str == nullptr) {
33     return Status(StatusCode::INTERNAL,
34                   "grpc_channelz_get_top_channels returned null");
35   }
36   grpc::protobuf::util::Status s =
37       grpc::protobuf::json::JsonStringToMessage(json_str, response);
38   gpr_free(json_str);
39   if (!s.ok()) {
40     return Status(StatusCode::INTERNAL, s.ToString());
41   }
42   return Status::OK;
43 }
44
45 Status ChannelzService::GetServers(
46     ServerContext* unused, const channelz::v1::GetServersRequest* request,
47     channelz::v1::GetServersResponse* response) {
48   char* json_str = grpc_channelz_get_servers(request->start_server_id());
49   if (json_str == nullptr) {
50     return Status(StatusCode::INTERNAL,
51                   "grpc_channelz_get_servers returned null");
52   }
53   grpc::protobuf::util::Status s =
54       grpc::protobuf::json::JsonStringToMessage(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
62 Status ChannelzService::GetServer(ServerContext* unused,
63                                   const channelz::v1::GetServerRequest* request,
64                                   channelz::v1::GetServerResponse* response) {
65   char* json_str = grpc_channelz_get_server(request->server_id());
66   if (json_str == nullptr) {
67     return Status(StatusCode::INTERNAL,
68                   "grpc_channelz_get_server returned null");
69   }
70   grpc::protobuf::util::Status s =
71       grpc::protobuf::json::JsonStringToMessage(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
79 Status ChannelzService::GetServerSockets(
80     ServerContext* unused, const channelz::v1::GetServerSocketsRequest* request,
81     channelz::v1::GetServerSocketsResponse* response) {
82   char* json_str = grpc_channelz_get_server_sockets(
83       request->server_id(), request->start_socket_id(), request->max_results());
84   if (json_str == nullptr) {
85     return Status(StatusCode::INTERNAL,
86                   "grpc_channelz_get_server_sockets returned null");
87   }
88   grpc::protobuf::util::Status s =
89       grpc::protobuf::json::JsonStringToMessage(json_str, response);
90   gpr_free(json_str);
91   if (!s.ok()) {
92     return Status(StatusCode::INTERNAL, s.ToString());
93   }
94   return Status::OK;
95 }
96
97 Status ChannelzService::GetChannel(
98     ServerContext* unused, const channelz::v1::GetChannelRequest* request,
99     channelz::v1::GetChannelResponse* response) {
100   char* json_str = grpc_channelz_get_channel(request->channel_id());
101   if (json_str == nullptr) {
102     return Status(StatusCode::NOT_FOUND, "No object found for that ChannelId");
103   }
104   grpc::protobuf::util::Status s =
105       grpc::protobuf::json::JsonStringToMessage(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
113 Status ChannelzService::GetSubchannel(
114     ServerContext* unused, const channelz::v1::GetSubchannelRequest* request,
115     channelz::v1::GetSubchannelResponse* response) {
116   char* json_str = grpc_channelz_get_subchannel(request->subchannel_id());
117   if (json_str == nullptr) {
118     return Status(StatusCode::NOT_FOUND,
119                   "No object found for that SubchannelId");
120   }
121   grpc::protobuf::util::Status s =
122       grpc::protobuf::json::JsonStringToMessage(json_str, response);
123   gpr_free(json_str);
124   if (!s.ok()) {
125     return Status(StatusCode::INTERNAL, s.ToString());
126   }
127   return Status::OK;
128 }
129
130 Status ChannelzService::GetSocket(ServerContext* unused,
131                                   const channelz::v1::GetSocketRequest* request,
132                                   channelz::v1::GetSocketResponse* response) {
133   char* json_str = grpc_channelz_get_socket(request->socket_id());
134   if (json_str == nullptr) {
135     return Status(StatusCode::NOT_FOUND, "No object found for that SocketId");
136   }
137   grpc::protobuf::util::Status s =
138       grpc::protobuf::json::JsonStringToMessage(json_str, response);
139   gpr_free(json_str);
140   if (!s.ok()) {
141     return Status(StatusCode::INTERNAL, s.ToString());
142   }
143   return Status::OK;
144 }
145
146 }  // namespace grpc