Fix java check style issue
[msb/discovery.git] / sdclient / discovery-service / src / main / java / org / onap / msb / sdclient / wrapper / consul / Consul.java
1 /**
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. 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 distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package org.onap.msb.sdclient.wrapper.consul;
15
16 import java.net.MalformedURLException;
17 import java.net.URL;
18
19 import javax.net.ssl.SSLContext;
20 import javax.ws.rs.client.Client;
21 import javax.ws.rs.client.ClientBuilder;
22
23 import org.onap.msb.sdclient.wrapper.consul.util.Jackson;
24 import org.onap.msb.sdclient.wrapper.consul.util.ObjectMapperContextResolver;
25
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import com.fasterxml.jackson.datatype.guava.GuavaModule;
28 import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
29 import com.google.common.base.Optional;
30 import com.google.common.base.Predicate;
31 import com.google.common.collect.FluentIterable;
32 import com.google.common.net.HostAndPort;
33
34 /**
35  * Client for interacting with the Consul HTTP API.
36  *
37  * @author rfast
38  */
39 public class Consul {
40
41     /**
42      * Default Consul HTTP API host.
43      */
44     public static final String DEFAULT_HTTP_HOST = "localhost";
45
46     /**
47      * Default Consul HTTP API port.
48      */
49     public static final int DEFAULT_HTTP_PORT = 8500;
50
51
52
53     private final HealthClient healthClient;
54
55
56     /**
57      * Private constructor.
58      *
59      * @param url The full URL of a running Consul instance.
60      * @param builder JAX-RS client builder instance.
61      */
62     private Consul(String url, ClientBuilder builder, ObjectMapper mapper) {
63
64         if (!FluentIterable.from(builder.getConfiguration().getClasses()).filter(new Predicate<Class<?>>() {
65             @Override
66             public boolean apply(final Class<?> clazz) {
67                 return JacksonJaxbJsonProvider.class.isAssignableFrom(clazz);
68             }
69         }).first().isPresent()) {
70             builder.register(JacksonJaxbJsonProvider.class);
71         }
72         final Client client = builder.register(new ObjectMapperContextResolver(mapper)).build();
73
74
75         this.healthClient = new HealthClient(client.target(url).path("v1").path("health"));
76
77
78
79         // agentClient.ping();
80     }
81
82     /**
83      * Creates a new client given a complete URL.
84      *
85      * @deprecated Use {@link Consul.Builder}
86      *
87      * @param url The Consul API URL.
88      * @param builder The JAX-RS client builder instance.
89      * @return A new client.
90      */
91     @Deprecated
92     public static Consul newClient(String url, ClientBuilder builder, ObjectMapper mapper) {
93         return new Consul(url, builder, mapper);
94     }
95
96     /**
97      * Creates a new client given a host and a port.
98      *
99      * @deprecated Use {@link Consul.Builder}
100      *
101      * @param host The Consul API hostname or IP.
102      * @param port The Consul port.
103      * @param builder The JAX-RS client builder instance.
104      * @return A new client.
105      */
106     @Deprecated
107     public static Consul newClient(String host, int port, ClientBuilder builder, ObjectMapper mapper) {
108         try {
109             return new Consul(new URL("http", host, port, "").toString(), builder, mapper);
110         } catch (MalformedURLException e) {
111             throw new ConsulException("Bad Consul URL", e);
112         }
113     }
114
115     /**
116      * Creates a new client given a host and a port.
117      *
118      * @deprecated Use {@link Consul.Builder}
119      *
120      * @param host The Consul API hostname or IP.
121      * @param port The Consul port.
122      * @return A new client.
123      */
124     @Deprecated
125     public static Consul newClient(String host, int port) {
126         return newClient(host, port, ClientBuilder.newBuilder(), Jackson.MAPPER);
127     }
128
129     /**
130      * Creates a new client given a host and a port.
131      *
132      * @deprecated Use {@link Consul.Builder}
133      *
134      * @return A new client.
135      */
136     @Deprecated
137     public static Consul newClient() {
138         return newClient(DEFAULT_HTTP_HOST, DEFAULT_HTTP_PORT);
139     }
140
141
142
143     public HealthClient healthClient() {
144         return healthClient;
145     }
146
147     /**
148      * Creates a new {@link Builder} object.
149      *
150      * @return A new Consul builder.
151      */
152     public static Builder builder() {
153         return new Builder();
154     }
155
156     /**
157      * Builder for {@link Consul} client objects.
158      */
159     public static class Builder {
160         private URL url;
161         private Optional<SSLContext> sslContext = Optional.absent();
162         private ObjectMapper objectMapper = Jackson.MAPPER;
163         private ClientBuilder clientBuilder = ClientBuilder.newBuilder();
164
165         {
166             try {
167                 url = new URL("http", "localhost", 8500, "");
168             } catch (MalformedURLException e) {
169                 throw new RuntimeException(e);
170             }
171         }
172
173         /**
174          * Constructs a new builder.
175          */
176         Builder() {
177
178         }
179
180         /**
181          * Sets the URL from a {@link URL} object.
182          *
183          * @param url The Consul agent URL.
184          * @return The builder.
185          */
186         public Builder withUrl(URL url) {
187             this.url = url;
188
189             return this;
190         }
191
192         /**
193          * Sets the URL from a {@link HostAndPort} object.
194          *
195          * @param hostAndPort The Consul agent host and port.
196          * @return The builder.
197          */
198         public Builder withHostAndPort(HostAndPort hostAndPort) {
199             try {
200                 this.url = new URL("http", hostAndPort.getHostText(), hostAndPort.getPort(), "");
201             } catch (MalformedURLException e) {
202                 throw new RuntimeException(e);
203             }
204
205             return this;
206         }
207
208         /**
209          * Sets the URL from a string.
210          *
211          * @param url The Consul agent URL.
212          * @return The builder.
213          */
214         public Builder withUrl(String url) {
215             try {
216                 this.url = new URL(url);
217             } catch (MalformedURLException e) {
218                 throw new RuntimeException(e);
219             }
220
221             return this;
222         }
223
224         /**
225          * Sets the {@link SSLContext} for the client.
226          *
227          * @param sslContext The SSL context for HTTPS agents.
228          * @return The builder.
229          */
230         public Builder withSslContext(SSLContext sslContext) {
231             this.sslContext = Optional.of(sslContext);
232
233             return this;
234         }
235
236         /**
237          * Sets the {@link ObjectMapper} for the client.
238          *
239          * @param objectMapper The {@link ObjectMapper} to use.
240          * @return The builder.
241          */
242         public Builder withObjectMapper(ObjectMapper objectMapper) {
243             this.objectMapper = objectMapper;
244
245             objectMapper.registerModule(new GuavaModule());
246
247             return this;
248         }
249
250         /**
251          * Sets the JAX-RS {@link ClientBuilder} to use.
252          *
253          * @param clientBuilder The JAX-RS builder.
254          * @return This builder.
255          */
256         public Builder withClientBuilder(ClientBuilder clientBuilder) {
257             this.clientBuilder = clientBuilder;
258
259             return this;
260         }
261
262         /**
263          * Constructs a new {@link Consul} client.
264          *
265          * @return A new Consul client.
266          */
267         public Consul build() {
268             if (this.sslContext.isPresent()) {
269                 this.clientBuilder.sslContext(this.sslContext.get());
270             }
271
272             return new Consul(this.url.toExternalForm(), this.clientBuilder, this.objectMapper);
273         }
274     }
275 }