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