remove not required docs and .readthedocs.yaml
[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
70
71         }).first().isPresent()) {
72             builder.register(JacksonJaxbJsonProvider.class);
73         }
74         final Client client = builder.register(new ObjectMapperContextResolver(mapper)).build();
75
76
77         this.healthClient = new HealthClient(client.target(url).path("v1").path("health"));
78
79
80
81         // agentClient.ping();
82     }
83
84     /**
85      * Creates a new client given a complete URL.
86      *
87      * @deprecated Use {@link Consul.Builder}
88      *
89      * @param url The Consul API URL.
90      * @param builder The JAX-RS client builder instance.
91      * @return A new client.
92      */
93     @Deprecated
94     public static Consul newClient(String url, ClientBuilder builder, ObjectMapper mapper) {
95         return new Consul(url, builder, mapper);
96     }
97
98     /**
99      * Creates a new client given a host and a port.
100      *
101      * @deprecated Use {@link Consul.Builder}
102      *
103      * @param host The Consul API hostname or IP.
104      * @param port The Consul port.
105      * @param builder The JAX-RS client builder instance.
106      * @return A new client.
107      */
108     @Deprecated
109     public static Consul newClient(String host, int port, ClientBuilder builder, ObjectMapper mapper) {
110         try {
111             return new Consul(new URL("http", host, port, "").toString(), builder, mapper);
112         } catch (MalformedURLException e) {
113             throw new ConsulException("Bad Consul URL", e);
114         }
115     }
116
117     /**
118      * Creates a new client given a host and a port.
119      *
120      * @deprecated Use {@link Consul.Builder}
121      *
122      * @param host The Consul API hostname or IP.
123      * @param port The Consul port.
124      * @return A new client.
125      */
126     @Deprecated
127     public static Consul newClient(String host, int port) {
128         return newClient(host, port, ClientBuilder.newBuilder(), Jackson.MAPPER);
129     }
130
131     /**
132      * Creates a new client given a host and a port.
133      *
134      * @deprecated Use {@link Consul.Builder}
135      *
136      * @return A new client.
137      */
138     @Deprecated
139     public static Consul newClient() {
140         return newClient(DEFAULT_HTTP_HOST, DEFAULT_HTTP_PORT);
141     }
142
143
144
145     public HealthClient healthClient() {
146         return healthClient;
147     }
148
149     /**
150      * Creates a new {@link Builder} object.
151      *
152      * @return A new Consul builder.
153      */
154     public static Builder builder() {
155         return new Builder();
156     }
157
158     /**
159      * Builder for {@link Consul} client objects.
160      */
161     public static class Builder {
162         private URL url;
163         private Optional<SSLContext> sslContext = Optional.absent();
164         private ObjectMapper objectMapper = Jackson.MAPPER;
165         private ClientBuilder clientBuilder = ClientBuilder.newBuilder();
166
167         {
168             try {
169                 url = new URL("http", "localhost", 8500, "");
170             } catch (MalformedURLException e) {
171                 throw new RuntimeException(e);
172             }
173         }
174
175         /**
176          * Constructs a new builder.
177          */
178         Builder() {
179
180         }
181
182         /**
183          * Sets the URL from a {@link URL} object.
184          *
185          * @param url The Consul agent URL.
186          * @return The builder.
187          */
188         public Builder withUrl(URL url) {
189             this.url = url;
190
191             return this;
192         }
193
194         /**
195          * Sets the URL from a {@link HostAndPort} object.
196          *
197          * @param hostAndPort The Consul agent host and port.
198          * @return The builder.
199          */
200         public Builder withHostAndPort(HostAndPort hostAndPort) {
201             try {
202                 this.url = new URL("http", hostAndPort.getHost(), hostAndPort.getPort(), "");
203             } catch (MalformedURLException e) {
204                 throw new RuntimeException(e);
205             }
206
207             return this;
208         }
209
210         /**
211          * Sets the URL from a string.
212          *
213          * @param url The Consul agent URL.
214          * @return The builder.
215          */
216         public Builder withUrl(String url) {
217             try {
218                 this.url = new URL(url);
219             } catch (MalformedURLException e) {
220                 throw new RuntimeException(e);
221             }
222
223             return this;
224         }
225
226         /**
227          * Sets the {@link SSLContext} for the client.
228          *
229          * @param sslContext The SSL context for HTTPS agents.
230          * @return The builder.
231          */
232         public Builder withSslContext(SSLContext sslContext) {
233             this.sslContext = Optional.of(sslContext);
234
235             return this;
236         }
237
238         /**
239          * Sets the {@link ObjectMapper} for the client.
240          *
241          * @param objectMapper The {@link ObjectMapper} to use.
242          * @return The builder.
243          */
244         public Builder withObjectMapper(ObjectMapper objectMapper) {
245             this.objectMapper = objectMapper;
246
247             objectMapper.registerModule(new GuavaModule());
248
249             return this;
250         }
251
252         /**
253          * Sets the JAX-RS {@link ClientBuilder} to use.
254          *
255          * @param clientBuilder The JAX-RS builder.
256          * @return This builder.
257          */
258         public Builder withClientBuilder(ClientBuilder clientBuilder) {
259             this.clientBuilder = clientBuilder;
260
261             return this;
262         }
263
264         /**
265          * Constructs a new {@link Consul} client.
266          *
267          * @return A new Consul client.
268          */
269         public Consul build() {
270             if (this.sslContext.isPresent()) {
271                 this.clientBuilder.sslContext(this.sslContext.get());
272             }
273
274             return new Consul(this.url.toExternalForm(), this.clientBuilder, this.objectMapper);
275         }
276     }
277 }