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