summaryrefslogtreecommitdiff
path: root/grpc/src/cpp/server/csds/csds.cc
blob: fc8e7e0e3c2b1523519a0e2c1f1d047f7b7c304b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
//
// Copyright 2021 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//

#include <grpc/support/port_platform.h>

#include "src/cpp/server/csds/csds.h"

#include "absl/status/statusor.h"

#include <grpc/grpc.h>
#include <grpc/support/alloc.h>
#include <grpcpp/impl/codegen/slice.h>

#include <string>

#include "src/proto/grpc/testing/xds/v3/csds.grpc.pb.h"

namespace grpc {
namespace xds {
namespace experimental {

using envoy::service::status::v3::ClientConfig;
using envoy::service::status::v3::ClientStatusRequest;
using envoy::service::status::v3::ClientStatusResponse;

namespace {

absl::StatusOr<ClientConfig> DumpClientConfig() {
  ClientConfig client_config;
  grpc_slice serialized_client_config = grpc_dump_xds_configs();
  std::string bytes = StringFromCopiedSlice(serialized_client_config);
  grpc_slice_unref(serialized_client_config);
  if (!client_config.ParseFromString(bytes)) {
    return absl::InternalError("Failed to parse ClientConfig.");
  }
  return client_config;
}

}  // namespace

Status ClientStatusDiscoveryService::StreamClientStatus(
    ServerContext* /*context*/,
    ServerReaderWriter<ClientStatusResponse, ClientStatusRequest>* stream) {
  ClientStatusRequest request;
  while (stream->Read(&request)) {
    ClientStatusResponse response;
    absl::StatusOr<ClientConfig> s = DumpClientConfig();
    if (!s.ok()) {
      if (s.status().code() == absl::StatusCode::kUnavailable) {
        // If the xDS client is not initialized, return empty response
        stream->Write(response);
        continue;
      }
      return Status(StatusCode(s.status().raw_code()), s.status().ToString());
    }
    *response.add_config() = std::move(s.value());
    stream->Write(response);
  }
  return Status::OK;
}

Status ClientStatusDiscoveryService::FetchClientStatus(
    ServerContext* /*context*/, const ClientStatusRequest* /*request*/,
    ClientStatusResponse* response) {
  absl::StatusOr<ClientConfig> s = DumpClientConfig();
  if (!s.ok()) {
    if (s.status().code() == absl::StatusCode::kUnavailable) {
      // If the xDS client is not initialized, return empty response
      return Status::OK;
    }
    return Status(StatusCode(s.status().raw_code()), s.status().ToString());
  }
  *response->add_config() = std::move(s.value());
  return Status::OK;
}

}  // namespace experimental
}  // namespace xds
}  // namespace grpc