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