Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / wrapper / consulextend / HealthClient.java
1 package org.onap.msb.apiroute.wrapper.consulextend;
2
3 import java.util.List;
4
5 import org.apache.http.HttpHost;
6 import org.onap.msb.apiroute.wrapper.consulextend.async.ConsulResponseCallback;
7 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
8 import org.onap.msb.apiroute.wrapper.consulextend.util.Http;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import com.fasterxml.jackson.core.type.TypeReference;
13 import com.orbitz.consul.option.CatalogOptions;
14 import com.orbitz.consul.option.QueryOptions;
15
16 /**
17  * HTTP Client for /v1/health/ endpoints.
18  */
19 public class HealthClient {
20         private final static Logger LOGGER = LoggerFactory
21                         .getLogger(HealthClient.class);
22
23         private static final TypeReference<List<ServiceHealth>> TYPE_SERVICE_HEALTH_LIST = new TypeReference<List<ServiceHealth>>() {
24         };
25
26         private static final String HEALTH_URI_10081 = "/api/health/v1";
27         private static final String HEALTH_URI_8500 = "/v1/health";
28         private static final String GET_HEALTH_SERVICE_URI = "/service";
29         
30 //      private static final String GET_HEALTH_SERVICE_URI = "/v1/health/service";
31         
32 //      private static final String GET_HEALTH_SERVICE_URI = "/api/health/v1/service";
33
34         private final static Http httpClient = Http.getInstance();
35
36         private HttpHost targetHost = null;
37         private String healthUri = HEALTH_URI_10081;
38
39         HealthClient(final HttpHost targetHost) {
40                 this.targetHost = targetHost;
41                 
42                 if(targetHost.getPort() == 8500)
43                 {
44                         healthUri = HEALTH_URI_8500;
45                 }
46         }
47
48         /**
49          * Asynchronously retrieves the healthchecks for all healthy service
50          * instances in a given datacenter with
51          * {@link com.orbitz.consul.option.QueryOptions}.
52          * 
53          * GET /v1/health/service/{service}?dc={datacenter}&amp;passing
54          * 
55          * Experimental.
56          * 
57          * @param service
58          *            The service to query.
59          * @param catalogOptions
60          *            The catalog specific options to use.
61          * @param queryOptions
62          *            The Query Options to use.
63          * @param callback
64          *            Callback implemented by callee to handle results.
65          */
66         public void getHealthyServiceInstances(String service,
67                         CatalogOptions catalogOptions, QueryOptions queryOptions,
68                         ConsulResponseCallback<List<ServiceHealth>> callback) {
69                 // prepare access path
70                 String path = targetHost.toString() + healthUri + GET_HEALTH_SERVICE_URI + "/"+ service;
71                 
72                 String params = Http.optionsFrom(catalogOptions, queryOptions);
73                 path = (params != null && !params.isEmpty()) ? path += "?"
74                                 + params : path;  //query all nodes without filter for health
75
76                 // async watch
77 //              LOGGER.info("get health paasing service:" + path);
78                 httpClient.asyncGetDelayHandle(path, TYPE_SERVICE_HEALTH_LIST, callback);
79         }
80
81         /**
82          * Asynchronously retrieves the healthchecks for all nodes in a given
83          * datacenter with {@link com.orbitz.consul.option.QueryOptions}.
84          * 
85          * GET /v1/health/service/{service}?dc={datacenter}
86          * 
87          * Experimental.
88          * 
89          * @param service
90          *            The service to query.
91          * @param catalogOptions
92          *            The catalog specific options to use.
93          * @param queryOptions
94          *            The Query Options to use.
95          * @param callback
96          *            Callback implemented by callee to handle results.
97          */
98         public void getAllServiceInstances(String service,
99                         CatalogOptions catalogOptions, QueryOptions queryOptions,
100                         ConsulResponseCallback<List<ServiceHealth>> callback) {
101
102                 // prepare access path
103                 String path = targetHost.toString() + healthUri + GET_HEALTH_SERVICE_URI + "/"+ service;
104                 String params = Http.optionsFrom(catalogOptions, queryOptions);
105                 path = (params != null && !params.isEmpty()) ? path += "?" + params
106                                 : path;
107
108                 // async watch
109 //              LOGGER.debug("get service:" + path);
110                 httpClient.asyncGetDelayHandle(path, TYPE_SERVICE_HEALTH_LIST, callback);
111         }
112 }