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 #ifndef GRPC_SRC_CORE_XDS_GRPC_XDS_BOOTSTRAP_GRPC_H 18 #define GRPC_SRC_CORE_XDS_GRPC_XDS_BOOTSTRAP_GRPC_H 19 20 #include <grpc/support/port_platform.h> 21 22 #include <map> 23 #include <memory> 24 #include <string> 25 #include <vector> 26 27 #include "absl/status/statusor.h" 28 #include "absl/strings/string_view.h" 29 #include "absl/types/optional.h" 30 #include "src/core/util/json/json.h" 31 #include "src/core/util/json/json_args.h" 32 #include "src/core/util/json/json_object_loader.h" 33 #include "src/core/util/ref_counted_ptr.h" 34 #include "src/core/util/validation_errors.h" 35 #include "src/core/xds/grpc/certificate_provider_store.h" 36 #include "src/core/xds/grpc/xds_audit_logger_registry.h" 37 #include "src/core/xds/grpc/xds_cluster_specifier_plugin.h" 38 #include "src/core/xds/grpc/xds_http_filter_registry.h" 39 #include "src/core/xds/grpc/xds_lb_policy_registry.h" 40 #include "src/core/xds/grpc/xds_server_grpc.h" 41 #include "src/core/xds/xds_client/xds_bootstrap.h" 42 43 namespace grpc_core { 44 45 class GrpcXdsBootstrap final : public XdsBootstrap { 46 public: 47 class GrpcNode final : public Node { 48 public: id()49 const std::string& id() const override { return id_; } cluster()50 const std::string& cluster() const override { return cluster_; } locality_region()51 const std::string& locality_region() const override { 52 return locality_.region; 53 } locality_zone()54 const std::string& locality_zone() const override { return locality_.zone; } locality_sub_zone()55 const std::string& locality_sub_zone() const override { 56 return locality_.sub_zone; 57 } metadata()58 const Json::Object& metadata() const override { return metadata_; } 59 60 static const JsonLoaderInterface* JsonLoader(const JsonArgs&); 61 62 private: 63 struct Locality { 64 std::string region; 65 std::string zone; 66 std::string sub_zone; 67 68 static const JsonLoaderInterface* JsonLoader(const JsonArgs&); 69 }; 70 71 std::string id_; 72 std::string cluster_; 73 Locality locality_; 74 Json::Object metadata_; 75 }; 76 77 class GrpcAuthority final : public Authority { 78 public: servers()79 std::vector<const XdsServer*> servers() const override { 80 std::vector<const XdsServer*> servers; 81 servers.reserve(servers_.size()); 82 for (const auto& server : servers_) { 83 servers.emplace_back(&server); 84 } 85 return servers; 86 } 87 client_listener_resource_name_template()88 const std::string& client_listener_resource_name_template() const { 89 return client_listener_resource_name_template_; 90 } 91 92 static const JsonLoaderInterface* JsonLoader(const JsonArgs&); 93 94 private: 95 std::vector<GrpcXdsServer> servers_; 96 std::string client_listener_resource_name_template_; 97 }; 98 99 // Creates bootstrap object from json_string. 100 static absl::StatusOr<std::unique_ptr<GrpcXdsBootstrap>> Create( 101 absl::string_view json_string); 102 103 static const JsonLoaderInterface* JsonLoader(const JsonArgs&); 104 void JsonPostLoad(const Json& json, const JsonArgs& args, 105 ValidationErrors* errors); 106 107 std::string ToString() const override; 108 servers()109 std::vector<const XdsServer*> servers() const override { 110 std::vector<const XdsServer*> servers; 111 servers.reserve(servers_.size()); 112 for (const auto& server : servers_) { 113 servers.emplace_back(&server); 114 } 115 return servers; 116 } 117 node()118 const Node* node() const override { 119 return node_.has_value() ? &*node_ : nullptr; 120 } 121 const Authority* LookupAuthority(const std::string& name) const override; 122 client_default_listener_resource_name_template()123 const std::string& client_default_listener_resource_name_template() const { 124 return client_default_listener_resource_name_template_; 125 } server_listener_resource_name_template()126 const std::string& server_listener_resource_name_template() const { 127 return server_listener_resource_name_template_; 128 } certificate_providers()129 const CertificateProviderStore::PluginDefinitionMap& certificate_providers() 130 const { 131 return certificate_providers_; 132 } http_filter_registry()133 const XdsHttpFilterRegistry& http_filter_registry() const { 134 return http_filter_registry_; 135 } cluster_specifier_plugin_registry()136 const XdsClusterSpecifierPluginRegistry& cluster_specifier_plugin_registry() 137 const { 138 return cluster_specifier_plugin_registry_; 139 } lb_policy_registry()140 const XdsLbPolicyRegistry& lb_policy_registry() const { 141 return lb_policy_registry_; 142 } audit_logger_registry()143 const XdsAuditLoggerRegistry& audit_logger_registry() const { 144 return audit_logger_registry_; 145 } 146 147 // Exposed for testing purposes only. authorities()148 const std::map<std::string, GrpcAuthority>& authorities() const { 149 return authorities_; 150 } 151 152 private: 153 std::vector<GrpcXdsServer> servers_; 154 absl::optional<GrpcNode> node_; 155 std::string client_default_listener_resource_name_template_; 156 std::string server_listener_resource_name_template_; 157 std::map<std::string, GrpcAuthority> authorities_; 158 CertificateProviderStore::PluginDefinitionMap certificate_providers_; 159 XdsHttpFilterRegistry http_filter_registry_; 160 XdsClusterSpecifierPluginRegistry cluster_specifier_plugin_registry_; 161 XdsLbPolicyRegistry lb_policy_registry_; 162 XdsAuditLoggerRegistry audit_logger_registry_; 163 }; 164 165 } // namespace grpc_core 166 167 #endif // GRPC_SRC_CORE_XDS_GRPC_XDS_BOOTSTRAP_GRPC_H 168