• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2019 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include "src/core/resolver/xds/xds_config.h"
18 
19 #include <memory>
20 #include <string>
21 #include <utility>
22 #include <vector>
23 
24 #include "absl/strings/str_cat.h"
25 #include "absl/strings/str_join.h"
26 #include "src/core/util/match.h"
27 
28 namespace grpc_core {
29 
30 //
31 // XdsConfig::ClusterConfig
32 //
33 
ClusterConfig(std::shared_ptr<const XdsClusterResource> cluster,std::shared_ptr<const XdsEndpointResource> endpoints,std::string resolution_note)34 XdsConfig::ClusterConfig::ClusterConfig(
35     std::shared_ptr<const XdsClusterResource> cluster,
36     std::shared_ptr<const XdsEndpointResource> endpoints,
37     std::string resolution_note)
38     : cluster(std::move(cluster)),
39       children(absl::in_place_type_t<EndpointConfig>(), std::move(endpoints),
40                std::move(resolution_note)) {}
41 
ClusterConfig(std::shared_ptr<const XdsClusterResource> cluster,std::vector<absl::string_view> leaf_clusters)42 XdsConfig::ClusterConfig::ClusterConfig(
43     std::shared_ptr<const XdsClusterResource> cluster,
44     std::vector<absl::string_view> leaf_clusters)
45     : cluster(std::move(cluster)),
46       children(absl::in_place_type_t<AggregateConfig>(),
47                std::move(leaf_clusters)) {}
48 
49 //
50 // XdsConfig
51 //
52 
ToString() const53 std::string XdsConfig::ToString() const {
54   std::vector<std::string> parts = {
55       "{\n  listener: {",     listener->ToString(),
56       "}\n  route_config: {", route_config->ToString(),
57       "}\n  virtual_host: {", virtual_host->ToString(),
58       "}\n  clusters: {\n"};
59   for (const auto& p : clusters) {
60     parts.push_back(absl::StrCat("    \"", p.first, "\": "));
61     if (!p.second.ok()) {
62       parts.push_back(p.second.status().ToString());
63       parts.push_back("\n");
64     } else {
65       parts.push_back(
66           absl::StrCat("      {\n"
67                        "        cluster: {",
68                        p.second->cluster->ToString(), "}\n"));
69       Match(
70           p.second->children,
71           [&](const ClusterConfig::EndpointConfig& endpoint_config) {
72             parts.push_back(
73                 absl::StrCat("        endpoints: {",
74                              endpoint_config.endpoints == nullptr
75                                  ? "<null>"
76                                  : endpoint_config.endpoints->ToString(),
77                              "}\n"
78                              "        resolution_note: \"",
79                              endpoint_config.resolution_note, "\"\n"));
80           },
81           [&](const ClusterConfig::AggregateConfig& aggregate_config) {
82             parts.push_back(absl::StrCat(
83                 "        leaf_clusters: [",
84                 absl::StrJoin(aggregate_config.leaf_clusters, ", "), "]\n"));
85           });
86       parts.push_back(
87           "      }\n"
88           "    ]\n");
89     }
90   }
91   parts.push_back("  }\n}");
92   return absl::StrJoin(parts, "");
93 }
94 
95 }  // namespace grpc_core
96