1 /*******************************************************************************
2 * Copyright 2016-2017 ZTE, Inc. and others.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.apiroute.wrapper.consulextend;
18 import java.util.List;
20 import org.apache.http.HttpHost;
21 import org.onap.msb.apiroute.wrapper.consulextend.async.ConsulResponseCallback;
22 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
23 import org.onap.msb.apiroute.wrapper.consulextend.util.Http;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import com.fasterxml.jackson.core.type.TypeReference;
28 import com.orbitz.consul.option.CatalogOptions;
29 import com.orbitz.consul.option.QueryOptions;
32 * HTTP Client for /v1/health/ endpoints.
34 public class HealthClient {
35 private final static Logger LOGGER = LoggerFactory
36 .getLogger(HealthClient.class);
38 private static final TypeReference<List<ServiceHealth>> TYPE_SERVICE_HEALTH_LIST = new TypeReference<List<ServiceHealth>>() {
41 private static final String HEALTH_URI_10081 = "/api/health/v1";
42 private static final String HEALTH_URI_8500 = "/v1/health";
43 private static final String GET_HEALTH_SERVICE_URI = "/service";
45 // private static final String GET_HEALTH_SERVICE_URI = "/v1/health/service";
47 // private static final String GET_HEALTH_SERVICE_URI = "/api/health/v1/service";
49 private final static Http httpClient = Http.getInstance();
51 private HttpHost targetHost = null;
52 private String healthUri = HEALTH_URI_10081;
54 HealthClient(final HttpHost targetHost) {
55 this.targetHost = targetHost;
57 if(targetHost.getPort() == 8500)
59 healthUri = HEALTH_URI_8500;
64 * Asynchronously retrieves the healthchecks for all healthy service
65 * instances in a given datacenter with
66 * {@link com.orbitz.consul.option.QueryOptions}.
68 * GET /v1/health/service/{service}?dc={datacenter}&passing
73 * The service to query.
74 * @param catalogOptions
75 * The catalog specific options to use.
77 * The Query Options to use.
79 * Callback implemented by callee to handle results.
81 public void getHealthyServiceInstances(String service,
82 CatalogOptions catalogOptions, QueryOptions queryOptions,
83 ConsulResponseCallback<List<ServiceHealth>> callback) {
84 // prepare access path
85 String path = targetHost.toString() + healthUri + GET_HEALTH_SERVICE_URI + "/"+ service;
87 String params = Http.optionsFrom(catalogOptions, queryOptions);
88 path = (params != null && !params.isEmpty()) ? path += "?"
89 + params : path; //query all nodes without filter for health
92 // LOGGER.info("get health paasing service:" + path);
93 httpClient.asyncGetDelayHandle(path, TYPE_SERVICE_HEALTH_LIST, callback);
97 * Asynchronously retrieves the healthchecks for all nodes in a given
98 * datacenter with {@link com.orbitz.consul.option.QueryOptions}.
100 * GET /v1/health/service/{service}?dc={datacenter}
105 * The service to query.
106 * @param catalogOptions
107 * The catalog specific options to use.
108 * @param queryOptions
109 * The Query Options to use.
111 * Callback implemented by callee to handle results.
113 public void getAllServiceInstances(String service,
114 CatalogOptions catalogOptions, QueryOptions queryOptions,
115 ConsulResponseCallback<List<ServiceHealth>> callback) {
117 // prepare access path
118 String path = targetHost.toString() + healthUri + GET_HEALTH_SERVICE_URI + "/"+ service;
119 String params = Http.optionsFrom(catalogOptions, queryOptions);
120 path = (params != null && !params.isEmpty()) ? path += "?" + params
124 // LOGGER.debug("get service:" + path);
125 httpClient.asyncGetDelayHandle(path, TYPE_SERVICE_HEALTH_LIST, callback);