1 //
2 //
3 // Copyright 2021 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/csds/csds.h"
22
23 #include "absl/status/statusor.h"
24
25 #include <grpc/grpc.h>
26 #include <grpc/support/alloc.h>
27 #include <grpcpp/impl/codegen/slice.h>
28
29 #include <string>
30
31 #include "src/proto/grpc/testing/xds/v3/csds.grpc.pb.h"
32
33 namespace grpc {
34 namespace xds {
35 namespace experimental {
36
37 using envoy::service::status::v3::ClientConfig;
38 using envoy::service::status::v3::ClientStatusRequest;
39 using envoy::service::status::v3::ClientStatusResponse;
40
41 namespace {
42
DumpClientConfig()43 absl::StatusOr<ClientConfig> DumpClientConfig() {
44 ClientConfig client_config;
45 grpc_slice serialized_client_config = grpc_dump_xds_configs();
46 std::string bytes = StringFromCopiedSlice(serialized_client_config);
47 grpc_slice_unref(serialized_client_config);
48 if (!client_config.ParseFromString(bytes)) {
49 return absl::InternalError("Failed to parse ClientConfig.");
50 }
51 return client_config;
52 }
53
54 } // namespace
55
StreamClientStatus(ServerContext *,ServerReaderWriter<ClientStatusResponse,ClientStatusRequest> * stream)56 Status ClientStatusDiscoveryService::StreamClientStatus(
57 ServerContext* /*context*/,
58 ServerReaderWriter<ClientStatusResponse, ClientStatusRequest>* stream) {
59 ClientStatusRequest request;
60 while (stream->Read(&request)) {
61 ClientStatusResponse response;
62 absl::StatusOr<ClientConfig> s = DumpClientConfig();
63 if (!s.ok()) {
64 if (s.status().code() == absl::StatusCode::kUnavailable) {
65 // If the xDS client is not initialized, return empty response
66 stream->Write(response);
67 continue;
68 }
69 return Status(StatusCode(s.status().raw_code()), s.status().ToString());
70 }
71 *response.add_config() = std::move(s.value());
72 stream->Write(response);
73 }
74 return Status::OK;
75 }
76
FetchClientStatus(ServerContext *,const ClientStatusRequest *,ClientStatusResponse * response)77 Status ClientStatusDiscoveryService::FetchClientStatus(
78 ServerContext* /*context*/, const ClientStatusRequest* /*request*/,
79 ClientStatusResponse* response) {
80 absl::StatusOr<ClientConfig> s = DumpClientConfig();
81 if (!s.ok()) {
82 if (s.status().code() == absl::StatusCode::kUnavailable) {
83 // If the xDS client is not initialized, return empty response
84 return Status::OK;
85 }
86 return Status(StatusCode(s.status().raw_code()), s.status().ToString());
87 }
88 *response->add_config() = std::move(s.value());
89 return Status::OK;
90 }
91
92 } // namespace experimental
93 } // namespace xds
94 } // namespace grpc
95