1 //
2 // Copyright 2016 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_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVICE_CONFIG_PARSER_H
18 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVICE_CONFIG_PARSER_H
19 
20 #include <grpc/support/port_platform.h>
21 
22 #include <memory>
23 
24 #include "absl/container/inlined_vector.h"
25 
26 #include <grpc/impl/codegen/grpc_types.h>
27 
28 #include "src/core/lib/iomgr/error.h"
29 #include "src/core/lib/json/json.h"
30 
31 namespace grpc_core {
32 
33 // Service config parser registry.
34 // See service_config.h for more information.
35 class ServiceConfigParser {
36  public:
37   /// This is the base class that all service config parsers MUST use to store
38   /// parsed service config data.
39   class ParsedConfig {
40    public:
41     virtual ~ParsedConfig() = default;
42   };
43 
44   /// This is the base class that all service config parsers should derive from.
45   class Parser {
46    public:
47     virtual ~Parser() = default;
48 
ParseGlobalParams(const grpc_channel_args *,const Json &,grpc_error ** error)49     virtual std::unique_ptr<ParsedConfig> ParseGlobalParams(
50         const grpc_channel_args*, const Json& /* json */, grpc_error** error) {
51       // Avoid unused parameter warning on debug-only parameter
52       (void)error;
53       GPR_DEBUG_ASSERT(error != nullptr);
54       return nullptr;
55     }
56 
ParsePerMethodParams(const grpc_channel_args *,const Json &,grpc_error ** error)57     virtual std::unique_ptr<ParsedConfig> ParsePerMethodParams(
58         const grpc_channel_args*, const Json& /* json */, grpc_error** error) {
59       // Avoid unused parameter warning on debug-only parameter
60       (void)error;
61       GPR_DEBUG_ASSERT(error != nullptr);
62       return nullptr;
63     }
64   };
65 
66   static constexpr int kNumPreallocatedParsers = 4;
67   typedef absl::InlinedVector<std::unique_ptr<ParsedConfig>,
68                               kNumPreallocatedParsers>
69       ParsedConfigVector;
70 
71   static void Init();
72   static void Shutdown();
73 
74   /// Globally register a service config parser. On successful registration, it
75   /// returns the index at which the parser was registered. On failure, -1 is
76   /// returned. Each new service config update will go through all the
77   /// registered parser. Each parser is responsible for reading the service
78   /// config json and returning a parsed config. This parsed config can later be
79   /// retrieved using the same index that was returned at registration time.
80   static size_t RegisterParser(std::unique_ptr<Parser> parser);
81 
82   static ParsedConfigVector ParseGlobalParameters(const grpc_channel_args* args,
83                                                   const Json& json,
84                                                   grpc_error** error);
85 
86   static ParsedConfigVector ParsePerMethodParameters(
87       const grpc_channel_args* args, const Json& json, grpc_error** error);
88 };
89 
90 }  // namespace grpc_core
91 
92 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVICE_CONFIG_PARSER_H */
93