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_LB_POLICY_REGISTRY_H
18 #define GRPC_SRC_CORE_EXT_XDS_XDS_LB_POLICY_REGISTRY_H
19 
20 #include <grpc/support/port_platform.h>
21 
22 #include <map>
23 #include <memory>
24 
25 #include "absl/strings/string_view.h"
26 #include "envoy/config/cluster/v3/cluster.upb.h"
27 
28 #include "src/core/ext/xds/xds_resource_type.h"
29 #include "src/core/lib/gprpp/validation_errors.h"
30 #include "src/core/lib/json/json.h"
31 
32 namespace grpc_core {
33 
34 // A registry that maintans a set of converters that are able to map xDS
35 // loadbalancing policy configurations to gRPC's JSON format.
36 class XdsLbPolicyRegistry {
37  public:
38   class ConfigFactory {
39    public:
40     virtual ~ConfigFactory() = default;
41     virtual Json::Object ConvertXdsLbPolicyConfig(
42         const XdsLbPolicyRegistry* registry,
43         const XdsResourceType::DecodeContext& context,
44         absl::string_view configuration, ValidationErrors* errors,
45         int recursion_depth) = 0;
46     virtual absl::string_view type() = 0;
47   };
48 
49   XdsLbPolicyRegistry();
50 
51   // Converts an xDS cluster load balancing policy message to gRPC's JSON
52   // format. An error is returned if none of the lb policies in the list are
53   // supported, or if a supported lb policy configuration conversion fails. \a
54   // recursion_depth indicates the current depth of the tree if lb_policy
55   // configuration recursively holds other lb policies.
56   Json::Array ConvertXdsLbPolicyConfig(
57       const XdsResourceType::DecodeContext& context,
58       const envoy_config_cluster_v3_LoadBalancingPolicy* lb_policy,
59       ValidationErrors* errors, int recursion_depth = 0) const;
60 
61  private:
62   // A map of config factories that goes from the type of the lb policy config
63   // to the config factory.
64   std::map<absl::string_view /* Owned by ConfigFactory */,
65            std::unique_ptr<ConfigFactory>>
66       policy_config_factories_;
67 };
68 
69 }  // namespace grpc_core
70 
71 #endif  // GRPC_SRC_CORE_EXT_XDS_XDS_LB_POLICY_REGISTRY_H
72