Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / wrapper / consulextend / Consul.java
1 package org.onap.msb.apiroute.wrapper.consulextend;
2
3 import org.apache.http.HttpHost;
4 import org.slf4j.Logger;
5 import org.slf4j.LoggerFactory;
6
7 import com.google.common.annotations.VisibleForTesting;
8
9 public class Consul {
10         /**
11          * Default Consul HTTP API host.
12          */
13         public static final String DEFAULT_HTTP_HOST = "localhost";
14
15         /**
16          * Default Consul HTTP API port.
17          */
18         public static final int DEFAULT_HTTP_PORT = 8500;
19
20         private static final Logger LOGGER = LoggerFactory
21                         .getLogger(Consul.class);
22         
23         private final CatalogClient catalogClient;
24         private final HealthClient healthClient;
25
26         private Consul(CatalogClient catalogClient, HealthClient healthClient) {
27                 this.catalogClient = catalogClient;
28                 this.healthClient = healthClient;
29         }
30
31         /**
32          * Get the Catalog HTTP client.
33          * <p>
34          * /v1/catalog
35          * 
36          * @return The Catalog HTTP client.
37          */
38         public CatalogClient catalogClient() {
39                 return catalogClient;
40         }
41
42         /**
43          * Get the Health HTTP client.
44          * <p>
45          * /v1/health
46          * 
47          * @return The Health HTTP client.
48          */
49         public HealthClient healthClient() {
50                 return healthClient;
51         }
52         
53         /**
54          * Creates a new {@link Builder} object.
55          * 
56          * @return A new Consul builder.
57          */
58         public static Builder builder() {
59                 return new Builder();
60         }
61
62         /**
63          * Used to create a default Consul client.
64          * 
65          * @return A default {@link Consul} client.
66          */
67         @VisibleForTesting
68         public static Consul newClient() {
69                 return builder().build();
70         }
71
72         public static class Builder {
73
74                 private HttpHost targetHost;
75
76                 {
77                         targetHost = new HttpHost(DEFAULT_HTTP_HOST, DEFAULT_HTTP_PORT);
78                 }
79
80                 Builder() {
81
82                 }
83
84                 public Builder withHostAndPort(String hostname, int port) {
85                         this.targetHost = new HttpHost(hostname, port);
86                         return this;
87                 }
88
89                 public Consul build() {
90                         LOGGER.info("********build consul:"+targetHost.toString()+"****************");
91                         CatalogClient catalogClient = new CatalogClient(targetHost);
92                         HealthClient healthClient = new HealthClient(targetHost);
93                         return new Consul(catalogClient,healthClient);
94                 }
95
96         }
97 }