1 package org.onap.msb.apiroute.wrapper.consulextend;
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;
12 import com.fasterxml.jackson.core.type.TypeReference;
13 import com.orbitz.consul.option.CatalogOptions;
14 import com.orbitz.consul.option.QueryOptions;
17 * HTTP Client for /v1/health/ endpoints.
19 public class HealthClient {
20 private final static Logger LOGGER = LoggerFactory
21 .getLogger(HealthClient.class);
23 private static final TypeReference<List<ServiceHealth>> TYPE_SERVICE_HEALTH_LIST = new TypeReference<List<ServiceHealth>>() {
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";
30 // private static final String GET_HEALTH_SERVICE_URI = "/v1/health/service";
32 // private static final String GET_HEALTH_SERVICE_URI = "/api/health/v1/service";
34 private final static Http httpClient = Http.getInstance();
36 private HttpHost targetHost = null;
37 private String healthUri = HEALTH_URI_10081;
39 HealthClient(final HttpHost targetHost) {
40 this.targetHost = targetHost;
42 if(targetHost.getPort() == 8500)
44 healthUri = HEALTH_URI_8500;
49 * Asynchronously retrieves the healthchecks for all healthy service
50 * instances in a given datacenter with
51 * {@link com.orbitz.consul.option.QueryOptions}.
53 * GET /v1/health/service/{service}?dc={datacenter}&passing
58 * The service to query.
59 * @param catalogOptions
60 * The catalog specific options to use.
62 * The Query Options to use.
64 * Callback implemented by callee to handle results.
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;
72 String params = Http.optionsFrom(catalogOptions, queryOptions);
73 path = (params != null && !params.isEmpty()) ? path += "?"
74 + params : path; //query all nodes without filter for health
77 // LOGGER.info("get health paasing service:" + path);
78 httpClient.asyncGetDelayHandle(path, TYPE_SERVICE_HEALTH_LIST, callback);
82 * Asynchronously retrieves the healthchecks for all nodes in a given
83 * datacenter with {@link com.orbitz.consul.option.QueryOptions}.
85 * GET /v1/health/service/{service}?dc={datacenter}
90 * The service to query.
91 * @param catalogOptions
92 * The catalog specific options to use.
94 * The Query Options to use.
96 * Callback implemented by callee to handle results.
98 public void getAllServiceInstances(String service,
99 CatalogOptions catalogOptions, QueryOptions queryOptions,
100 ConsulResponseCallback<List<ServiceHealth>> callback) {
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
109 // LOGGER.debug("get service:" + path);
110 httpClient.asyncGetDelayHandle(path, TYPE_SERVICE_HEALTH_LIST, callback);