• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SRC_CORE_SERVICE_CONFIG_SERVICE_CONFIG_PARSER_H
18 #define GRPC_SRC_CORE_SERVICE_CONFIG_SERVICE_CONFIG_PARSER_H
19 
20 #include <grpc/support/port_platform.h>
21 #include <stddef.h>
22 
23 #include <algorithm>
24 #include <memory>
25 #include <utility>
26 #include <vector>
27 
28 #include "absl/strings/string_view.h"
29 #include "src/core/lib/channel/channel_args.h"
30 #include "src/core/util/json/json.h"
31 #include "src/core/util/validation_errors.h"
32 
33 namespace grpc_core {
34 
35 // Service config parser registry.
36 // See service_config.h for more information.
37 class ServiceConfigParser {
38  public:
39   /// This is the base class that all service config parsers MUST use to store
40   /// parsed service config data.
41   class ParsedConfig {
42    public:
43     virtual ~ParsedConfig() = default;
44   };
45 
46   /// This is the base class that all service config parsers should derive from.
47   class Parser {
48    public:
49     virtual ~Parser() = default;
50 
51     virtual absl::string_view name() const = 0;
52 
ParseGlobalParams(const ChannelArgs &,const Json &,ValidationErrors *)53     virtual std::unique_ptr<ParsedConfig> ParseGlobalParams(
54         const ChannelArgs& /*args*/, const Json& /*json*/,
55         ValidationErrors* /*errors*/) {
56       return nullptr;
57     }
58 
ParsePerMethodParams(const ChannelArgs &,const Json &,ValidationErrors *)59     virtual std::unique_ptr<ParsedConfig> ParsePerMethodParams(
60         const ChannelArgs& /*args*/, const Json& /*json*/,
61         ValidationErrors* /*errors*/) {
62       return nullptr;
63     }
64   };
65 
66   using ServiceConfigParserList = std::vector<std::unique_ptr<Parser>>;
67   using ParsedConfigVector = std::vector<std::unique_ptr<ParsedConfig>>;
68 
69   class Builder final {
70    public:
71     /// Globally register a service config parser. Each new service config
72     /// update will go through all the registered parser. Each parser is
73     /// responsible for reading the service config json and returning a parsed
74     /// config.
75     void RegisterParser(std::unique_ptr<Parser> parser);
76 
77     ServiceConfigParser Build();
78 
79    private:
80     ServiceConfigParserList registered_parsers_;
81   };
82 
83   ParsedConfigVector ParseGlobalParameters(const ChannelArgs& args,
84                                            const Json& json,
85                                            ValidationErrors* errors) const;
86 
87   ParsedConfigVector ParsePerMethodParameters(const ChannelArgs& args,
88                                               const Json& json,
89                                               ValidationErrors* errors) const;
90 
91   // Return the index for a given registered parser.
92   // If there is an error, return -1.
93   size_t GetParserIndex(absl::string_view name) const;
94 
95  private:
ServiceConfigParser(ServiceConfigParserList registered_parsers)96   explicit ServiceConfigParser(ServiceConfigParserList registered_parsers)
97       : registered_parsers_(std::move(registered_parsers)) {}
98   ServiceConfigParserList registered_parsers_;
99 };
100 
101 }  // namespace grpc_core
102 
103 #endif  // GRPC_SRC_CORE_SERVICE_CONFIG_SERVICE_CONFIG_PARSER_H
104