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