85c9b7884bd6c69be09e6bc27076a16073b7af2c
[msb/apigateway.git] /
1 /*******************************************************************************
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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;
17
18 import java.util.List;
19
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;
26
27 import com.fasterxml.jackson.core.type.TypeReference;
28 import com.orbitz.consul.option.CatalogOptions;
29 import com.orbitz.consul.option.QueryOptions;
30
31 /**
32  * HTTP Client for /v1/health/ endpoints.
33  */
34 public class HealthClient {
35         private final static Logger LOGGER = LoggerFactory
36                         .getLogger(HealthClient.class);
37
38         private static final TypeReference<List<ServiceHealth>> TYPE_SERVICE_HEALTH_LIST = new TypeReference<List<ServiceHealth>>() {
39         };
40
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";
44         
45 //      private static final String GET_HEALTH_SERVICE_URI = "/v1/health/service";
46         
47 //      private static final String GET_HEALTH_SERVICE_URI = "/api/health/v1/service";
48
49         private final static Http httpClient = Http.getInstance();
50
51         private HttpHost targetHost = null;
52         private String healthUri = HEALTH_URI_10081;
53
54         HealthClient(final HttpHost targetHost) {
55                 this.targetHost = targetHost;
56                 
57                 if(targetHost.getPort() == 8500)
58                 {
59                         healthUri = HEALTH_URI_8500;
60                 }
61         }
62
63         /**
64          * Asynchronously retrieves the healthchecks for all healthy service
65          * instances in a given datacenter with
66          * {@link com.orbitz.consul.option.QueryOptions}.
67          * 
68          * GET /v1/health/service/{service}?dc={datacenter}&amp;passing
69          * 
70          * Experimental.
71          * 
72          * @param service
73          *            The service to query.
74          * @param catalogOptions
75          *            The catalog specific options to use.
76          * @param queryOptions
77          *            The Query Options to use.
78          * @param callback
79          *            Callback implemented by callee to handle results.
80          */
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;
86                 
87                 String params = Http.optionsFrom(catalogOptions, queryOptions);
88                 path = (params != null && !params.isEmpty()) ? path += "?"
89                                 + params : path;  //query all nodes without filter for health
90
91                 // async watch
92 //              LOGGER.info("get health paasing service:" + path);
93                 httpClient.asyncGetDelayHandle(path, TYPE_SERVICE_HEALTH_LIST, callback);
94         }
95
96         /**
97          * Asynchronously retrieves the healthchecks for all nodes in a given
98          * datacenter with {@link com.orbitz.consul.option.QueryOptions}.
99          * 
100          * GET /v1/health/service/{service}?dc={datacenter}
101          * 
102          * Experimental.
103          * 
104          * @param service
105          *            The service to query.
106          * @param catalogOptions
107          *            The catalog specific options to use.
108          * @param queryOptions
109          *            The Query Options to use.
110          * @param callback
111          *            Callback implemented by callee to handle results.
112          */
113         public void getAllServiceInstances(String service,
114                         CatalogOptions catalogOptions, QueryOptions queryOptions,
115                         ConsulResponseCallback<List<ServiceHealth>> callback) {
116
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
121                                 : path;
122
123                 // async watch
124 //              LOGGER.debug("get service:" + path);
125                 httpClient.asyncGetDelayHandle(path, TYPE_SERVICE_HEALTH_LIST, callback);
126         }
127 }