1 // 2 // Copyright 2022 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_CLUSTER_SPECIFIER_PLUGIN_H 18 #define GRPC_SRC_CORE_XDS_GRPC_XDS_CLUSTER_SPECIFIER_PLUGIN_H 19 20 #include <grpc/support/port_platform.h> 21 22 #include <map> 23 #include <memory> 24 #include <utility> 25 26 #include "absl/strings/string_view.h" 27 #include "src/core/util/json/json.h" 28 #include "src/core/util/validation_errors.h" 29 #include "src/core/xds/grpc/xds_common_types.h" 30 #include "upb/mem/arena.h" 31 #include "upb/reflection/def.h" 32 33 namespace grpc_core { 34 35 class XdsClusterSpecifierPluginImpl { 36 public: 37 virtual ~XdsClusterSpecifierPluginImpl() = default; 38 39 // Returns the config proto message name. 40 virtual absl::string_view ConfigProtoName() const = 0; 41 42 // Loads the proto message into the upb symtab. 43 virtual void PopulateSymtab(upb_DefPool* symtab) const = 0; 44 45 // Returns the LB policy config in JSON form. 46 virtual Json GenerateLoadBalancingPolicyConfig( 47 XdsExtension extension, upb_Arena* arena, upb_DefPool* symtab, 48 ValidationErrors* errors) const = 0; 49 }; 50 51 class XdsRouteLookupClusterSpecifierPlugin final 52 : public XdsClusterSpecifierPluginImpl { 53 absl::string_view ConfigProtoName() const override; 54 55 void PopulateSymtab(upb_DefPool* symtab) const override; 56 57 Json GenerateLoadBalancingPolicyConfig( 58 XdsExtension extension, upb_Arena* arena, upb_DefPool* symtab, 59 ValidationErrors* errors) const override; 60 }; 61 62 class XdsClusterSpecifierPluginRegistry final { 63 public: 64 XdsClusterSpecifierPluginRegistry(); 65 66 // Not copyable. 67 XdsClusterSpecifierPluginRegistry(const XdsClusterSpecifierPluginRegistry&) = 68 delete; 69 XdsClusterSpecifierPluginRegistry& operator=( 70 const XdsClusterSpecifierPluginRegistry&) = delete; 71 72 // Movable. XdsClusterSpecifierPluginRegistry(XdsClusterSpecifierPluginRegistry && other)73 XdsClusterSpecifierPluginRegistry( 74 XdsClusterSpecifierPluginRegistry&& other) noexcept 75 : registry_(std::move(other.registry_)) {} 76 XdsClusterSpecifierPluginRegistry& operator=( 77 XdsClusterSpecifierPluginRegistry&& other) noexcept { 78 registry_ = std::move(other.registry_); 79 return *this; 80 } 81 82 void RegisterPlugin(std::unique_ptr<XdsClusterSpecifierPluginImpl> plugin); 83 84 void PopulateSymtab(upb_DefPool* symtab) const; 85 86 const XdsClusterSpecifierPluginImpl* GetPluginForType( 87 absl::string_view config_proto_type_name) const; 88 89 private: 90 std::map<absl::string_view, std::unique_ptr<XdsClusterSpecifierPluginImpl>> 91 registry_; 92 }; 93 94 } // namespace grpc_core 95 96 #endif // GRPC_SRC_CORE_XDS_GRPC_XDS_CLUSTER_SPECIFIER_PLUGIN_H 97