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 <string>
24 #include <utility>
25
26 #include "absl/status/status.h"
27 #include "absl/status/statusor.h"
28
29 #include <grpc/slice.h>
30 #include <grpcpp/support/interceptor.h>
31 #include <grpcpp/support/slice.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(static_cast<StatusCode>(s.status().raw_code()),
70 s.status().ToString());
71 }
72 *response.add_config() = std::move(s.value());
73 stream->Write(response);
74 }
75 return Status::OK;
76 }
77
FetchClientStatus(ServerContext *,const ClientStatusRequest *,ClientStatusResponse * response)78 Status ClientStatusDiscoveryService::FetchClientStatus(
79 ServerContext* /*context*/, const ClientStatusRequest* /*request*/,
80 ClientStatusResponse* response) {
81 absl::StatusOr<ClientConfig> s = DumpClientConfig();
82 if (!s.ok()) {
83 if (s.status().code() == absl::StatusCode::kUnavailable) {
84 // If the xDS client is not initialized, return empty response
85 return Status::OK;
86 }
87 return Status(static_cast<StatusCode>(s.status().raw_code()),
88 s.status().ToString());
89 }
90 *response->add_config() = std::move(s.value());
91 return Status::OK;
92 }
93
94 } // namespace experimental
95 } // namespace xds
96 } // namespace grpc
97