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