1 // Copyright 2020 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.api.generator.engine.ast.TypeNode;
18 import com.google.auto.value.AutoValue;
19 import com.google.common.collect.ImmutableList;
20 import java.util.List;
21 import javax.annotation.Nullable;
22 
23 @AutoValue
24 public abstract class Method {
25   public enum Stream {
26     NONE,
27     CLIENT,
28     SERVER,
29     BIDI
30   };
31 
name()32   public abstract String name();
33 
stream()34   public abstract Stream stream();
35 
inputType()36   public abstract TypeNode inputType();
37 
outputType()38   public abstract TypeNode outputType();
39 
isBatching()40   public abstract boolean isBatching();
41 
isPaged()42   public boolean isPaged() {
43     return pageSizeFieldName() != null;
44   }
45 
46   @Nullable
pageSizeFieldName()47   public abstract String pageSizeFieldName();
48 
isDeprecated()49   public abstract boolean isDeprecated();
50 
51   @Nullable
lro()52   public abstract LongrunningOperation lro();
53 
54   @Nullable
description()55   public abstract String description();
56 
57   // Example: google.iam.v1.IAMPolicy.
58   @Nullable
mixedInApiName()59   public abstract String mixedInApiName();
60 
61   // HttpBinding pending dotted reference support.
62   @Nullable
httpBindings()63   public abstract HttpBindings httpBindings();
64 
65   @Nullable
routingHeaderRule()66   public abstract RoutingHeaderRule routingHeaderRule();
67 
68   // Example from Expand in echo.proto: Thet TypeNodes that map to
69   // [["content", "error"], ["content", "error", "info"]].
methodSignatures()70   public abstract ImmutableList<List<MethodArgument>> methodSignatures();
71 
operationPollingMethod()72   public abstract boolean operationPollingMethod();
73 
hasLro()74   public boolean hasLro() {
75     return lro() != null;
76   }
77 
hasDescription()78   public boolean hasDescription() {
79     return description() != null;
80   }
81 
hasHttpBindings()82   public boolean hasHttpBindings() {
83     return httpBindings() != null && !httpBindings().pathParameters().isEmpty();
84   }
85 
hasRoutingHeaderParams()86   public boolean hasRoutingHeaderParams() {
87     return routingHeaderRule() != null && !routingHeaderRule().routingHeaderParams().isEmpty();
88   }
89 
shouldSetParamsExtractor()90   public boolean shouldSetParamsExtractor() {
91     return (hasHttpBindings() && routingHeaderRule() == null) || hasRoutingHeaderParams();
92   }
93 
isMixin()94   public boolean isMixin() {
95     return mixedInApiName() != null;
96   }
97 
isOperationPollingMethod()98   public boolean isOperationPollingMethod() {
99     return operationPollingMethod();
100   }
101 
102   /**
103    * Determines if method is both eligible and enabled for the Transport. GRPC+REST Transport is not
104    * supported as each transport's sub composers will invoke this method the specific transport
105    * (GRPC or REST)
106    *
107    * @param transport Expects either GRPC or REST Transport
108    * @return boolean if method should be generated for the transport
109    */
isSupportedByTransport(Transport transport)110   public boolean isSupportedByTransport(Transport transport) {
111     if (transport == Transport.REST) {
112       return httpBindings() != null && stream() != Stream.BIDI && stream() != Stream.CLIENT;
113     } else if (transport == Transport.GRPC) {
114       return true;
115     } else {
116       throw new IllegalArgumentException(
117           String.format("Invalid Transport: %s. Expecting GRPC or REST", transport.name()));
118     }
119   }
120 
toBuilder()121   public abstract Builder toBuilder();
122 
builder()123   public static Builder builder() {
124     return new AutoValue_Method.Builder()
125         .setStream(Stream.NONE)
126         .setMethodSignatures(ImmutableList.of())
127         .setIsBatching(false)
128         .setIsDeprecated(false)
129         .setOperationPollingMethod(false);
130   }
131 
toStream(boolean isClientStreaming, boolean isServerStreaming)132   public static Stream toStream(boolean isClientStreaming, boolean isServerStreaming) {
133     if (!isClientStreaming && !isServerStreaming) {
134       return Stream.NONE;
135     }
136     if (isClientStreaming && isServerStreaming) {
137       return Stream.BIDI;
138     }
139     if (isClientStreaming) {
140       return Stream.CLIENT;
141     }
142     return Stream.SERVER;
143   }
144 
145   @AutoValue.Builder
146   public abstract static class Builder {
setName(String name)147     public abstract Builder setName(String name);
148 
setInputType(TypeNode inputType)149     public abstract Builder setInputType(TypeNode inputType);
150 
setOutputType(TypeNode outputType)151     public abstract Builder setOutputType(TypeNode outputType);
152 
setStream(Stream stream)153     public abstract Builder setStream(Stream stream);
154 
setLro(LongrunningOperation lro)155     public abstract Builder setLro(LongrunningOperation lro);
156 
setDescription(String description)157     public abstract Builder setDescription(String description);
158 
setMixedInApiName(String mixedInApiName)159     public abstract Builder setMixedInApiName(String mixedInApiName);
160 
setHttpBindings(HttpBindings httpBindings)161     public abstract Builder setHttpBindings(HttpBindings httpBindings);
162 
setMethodSignatures(List<List<MethodArgument>> methodSignature)163     public abstract Builder setMethodSignatures(List<List<MethodArgument>> methodSignature);
164 
setIsBatching(boolean isBatching)165     public abstract Builder setIsBatching(boolean isBatching);
166 
setPageSizeFieldName(String pagedFieldName)167     public abstract Builder setPageSizeFieldName(String pagedFieldName);
168 
setIsDeprecated(boolean isDeprecated)169     public abstract Builder setIsDeprecated(boolean isDeprecated);
170 
setOperationPollingMethod(boolean operationPollingMethod)171     public abstract Builder setOperationPollingMethod(boolean operationPollingMethod);
172 
setRoutingHeaderRule(RoutingHeaderRule routingHeaderRule)173     public abstract Builder setRoutingHeaderRule(RoutingHeaderRule routingHeaderRule);
174 
build()175     public abstract Method build();
176   }
177 }
178