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 #include "src/core/ext/filters/stateful_session/stateful_session_service_config_parser.h"
18
19 #include <grpc/support/port_platform.h>
20
21 #include <vector>
22
23 #include "absl/types/optional.h"
24 #include "src/core/lib/channel/channel_args.h"
25
26 namespace grpc_core {
27
28 const JsonLoaderInterface*
JsonLoader(const JsonArgs &)29 StatefulSessionMethodParsedConfig::CookieConfig::JsonLoader(const JsonArgs&) {
30 static const auto* loader = JsonObjectLoader<CookieConfig>()
31 .OptionalField("name", &CookieConfig::name)
32 .OptionalField("path", &CookieConfig::path)
33 .OptionalField("ttl", &CookieConfig::ttl)
34 .Finish();
35 return loader;
36 }
37
JsonPostLoad(const Json &,const JsonArgs &,ValidationErrors * errors)38 void StatefulSessionMethodParsedConfig::CookieConfig::JsonPostLoad(
39 const Json&, const JsonArgs&, ValidationErrors* errors) {
40 // Validate that cookie_name is non-empty.
41 if (name.has_value() && name->empty()) {
42 ValidationErrors::ScopedField field(errors, ".name");
43 errors->AddError("must be non-empty");
44 }
45 }
46
JsonLoader(const JsonArgs &)47 const JsonLoaderInterface* StatefulSessionMethodParsedConfig::JsonLoader(
48 const JsonArgs&) {
49 static const auto* loader =
50 JsonObjectLoader<StatefulSessionMethodParsedConfig>()
51 .OptionalField("stateful_session",
52 &StatefulSessionMethodParsedConfig::configs_)
53 .Finish();
54 return loader;
55 }
56
57 std::unique_ptr<ServiceConfigParser::ParsedConfig>
ParsePerMethodParams(const ChannelArgs & args,const Json & json,ValidationErrors * errors)58 StatefulSessionServiceConfigParser::ParsePerMethodParams(
59 const ChannelArgs& args, const Json& json, ValidationErrors* errors) {
60 // Only parse config if the following channel arg is present.
61 if (!args.GetBool(GRPC_ARG_PARSE_STATEFUL_SESSION_METHOD_CONFIG)
62 .value_or(false)) {
63 return nullptr;
64 }
65 // Parse config from json.
66 return LoadFromJson<std::unique_ptr<StatefulSessionMethodParsedConfig>>(
67 json, JsonArgs(), errors);
68 }
69
Register(CoreConfiguration::Builder * builder)70 void StatefulSessionServiceConfigParser::Register(
71 CoreConfiguration::Builder* builder) {
72 builder->service_config_parser()->RegisterParser(
73 std::make_unique<StatefulSessionServiceConfigParser>());
74 }
75
ParserIndex()76 size_t StatefulSessionServiceConfigParser::ParserIndex() {
77 return CoreConfiguration::Get().service_config_parser().GetParserIndex(
78 parser_name());
79 }
80
81 } // namespace grpc_core
82