1 // Copyright 2022 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.google.api.generator.gapic.model;
16 
17 import com.google.auto.value.AutoValue;
18 import com.google.common.base.Splitter;
19 import com.google.common.collect.ImmutableList;
20 import java.util.List;
21 
22 /**
23  * This model represents routing rules configured in rpc services. It will be used for generating
24  * the logic to match-and-extract field values from request, the extracted values will be
25  * concatenated to a request header that is used for routing purposes.
26  */
27 @AutoValue
28 public abstract class RoutingHeaderRule {
29 
routingHeaderParams()30   public abstract ImmutableList<RoutingHeaderParam> routingHeaderParams();
31 
builder()32   public static Builder builder() {
33     return new AutoValue_RoutingHeaderRule.Builder().setRoutingHeaderParams(ImmutableList.of());
34   }
35 
36   @AutoValue
37   public abstract static class RoutingHeaderParam {
38 
fieldName()39     public abstract String fieldName();
40 
key()41     public abstract String key();
42 
pattern()43     public abstract String pattern();
44 
create(String field, String key, String pattern)45     public static RoutingHeaderParam create(String field, String key, String pattern) {
46       return new AutoValue_RoutingHeaderRule_RoutingHeaderParam(field, key, pattern);
47     }
48 
getDescendantFieldNames()49     public List<String> getDescendantFieldNames() {
50       return Splitter.on(".").splitToList(fieldName());
51     }
52   }
53 
54   @AutoValue.Builder
55   public abstract static class Builder {
routingHeaderParamsBuilder()56     abstract ImmutableList.Builder<RoutingHeaderParam> routingHeaderParamsBuilder();
57 
addParam(RoutingHeaderParam routingHeaderParam)58     public final Builder addParam(RoutingHeaderParam routingHeaderParam) {
59       routingHeaderParamsBuilder().add(routingHeaderParam);
60       return this;
61     }
62 
setRoutingHeaderParams( ImmutableList<RoutingHeaderParam> routingHeaderParams)63     public abstract Builder setRoutingHeaderParams(
64         ImmutableList<RoutingHeaderParam> routingHeaderParams);
65 
build()66     public abstract RoutingHeaderRule build();
67   }
68 }
69