modify copyright year
[msb/discovery.git] / sdclient / discovery-service / src / main / java / org / onap / msb / sdclient / wrapper / consul / option / QueryOptions.java
1 /**
2  * Copyright 2016-2017 ZTE, Inc. and others.
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 package org.onap.msb.sdclient.wrapper.consul.option;
17
18 import static com.google.common.base.Preconditions.checkArgument;
19 import static org.onap.msb.sdclient.wrapper.consul.option.Options.optionallyAdd;
20
21 import java.math.BigInteger;
22
23 import javax.ws.rs.client.WebTarget;
24
25 import com.google.common.base.Optional;
26
27 /**
28  * Container for common query options used by the Consul API.
29  */
30
31 public abstract class QueryOptions implements ParamAdder {
32
33     public static final QueryOptions BLANK = ImmutableQueryOptions.builder().build();
34
35     public abstract Optional<String> getWait();
36     public abstract Optional<String> getToken();
37     public abstract Optional<BigInteger> getIndex();
38     public abstract Optional<String> getNear();
39
40   
41     public ConsistencyMode getConsistencyMode() {
42         return ConsistencyMode.DEFAULT;
43     }
44
45
46     public boolean isBlocking() {
47         return getWait().isPresent();
48     }
49
50
51     public boolean hasToken() {
52         return getToken().isPresent();
53     }
54
55
56     void validate() {
57         if (isBlocking()) {
58             checkArgument(getIndex().isPresent(), "If wait is specified, index must also be specified");
59         }
60     }
61
62     public static ImmutableQueryOptions.Builder blockSeconds(int seconds, BigInteger index) {
63         return blockBuilder("s", seconds, index);
64     }
65
66     public static ImmutableQueryOptions.Builder blockMinutes(int minutes, BigInteger index) {
67         return blockBuilder("m", minutes, index);
68     }
69
70     private static ImmutableQueryOptions.Builder blockBuilder(String identifier, int qty, BigInteger index) {
71         return ImmutableQueryOptions.builder()
72                 .wait(String.format("%s%s", qty, identifier))
73                 .index(index);
74     }
75
76     @Override
77     public WebTarget apply(WebTarget input) {
78
79         WebTarget added = input;
80         switch (getConsistencyMode()) {
81             case CONSISTENT:
82                 added = added.queryParam("consistent", "");
83                 break;
84             case STALE:
85                 added = added.queryParam("stale", "");
86                 break;
87         }
88
89         if (isBlocking()) {
90             added = added.queryParam("wait", getWait().get())
91                     .queryParam("index", String.valueOf(getIndex().get()));
92         }
93
94         added = optionallyAdd(added, "token", getToken());
95         added = optionallyAdd(added, "near", getToken());
96
97         return added;
98     }
99 }