Issue-id: OCS-9
[msb/apigateway.git] / msb-core / apiroute / apiroute-service / src / main / java / org / openo / msb / wrapper / consul / HealthClient.java
1 /**
2  * Copyright 2016 2015-2016 ZTE, Inc. and others. All rights reserved.
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 /**
17 * Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
18 *
19 * Licensed under the Apache License, Version 2.0 (the "License");
20 * you may not use this file except in compliance with the License.
21 * You may obtain a copy of the License at
22 *
23 * http://www.apache.org/licenses/LICENSE-2.0
24 *
25 * Unless required by applicable law or agreed to in writing, software
26 * distributed under the License is distributed on an "AS IS" BASIS,
27 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28 * See the License for the specific language governing permissions and
29 * limitations under the License.
30 */
31
32 package org.openo.msb.wrapper.consul;
33
34 import static org.openo.msb.wrapper.consul.util.ClientUtil.response;
35
36 import java.util.List;
37
38 import javax.ws.rs.client.WebTarget;
39 import javax.ws.rs.core.GenericType;
40
41 import org.openo.msb.wrapper.consul.async.ConsulResponseCallback;
42 import org.openo.msb.wrapper.consul.model.ConsulResponse;
43 import org.openo.msb.wrapper.consul.model.health.ServiceHealth;
44 import org.openo.msb.wrapper.consul.option.CatalogOptions;
45 import org.openo.msb.wrapper.consul.option.QueryOptions;
46
47 /**
48  * HTTP Client for /v1/health/ endpoints.
49  */
50 public class HealthClient {
51
52
53     private static final GenericType<List<ServiceHealth>> TYPE_SERVICE_HEALTH_LIST =
54             new GenericType<List<ServiceHealth>>() {};
55             
56     private final WebTarget webTarget;
57
58     /**
59      * Constructs an instance of this class.
60      *
61      * @param webTarget The {@link javax.ws.rs.client.WebTarget} to base requests from.
62      */
63     HealthClient(WebTarget webTarget) {
64         this.webTarget = webTarget;
65     }
66
67    
68
69     /**
70      * Retrieves the healthchecks for all healthy service instances.
71      * 
72      * GET /v1/health/service/{service}?passing
73      *
74      * @param service The service to query.
75      * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a list of
76      * {@link com.orbitz.consul.model.health.HealthCheck} objects.
77      */
78     public ConsulResponse<List<ServiceHealth>> getHealthyServiceInstances(String service) {
79         return getHealthyServiceInstances(service, null, QueryOptions.BLANK);
80     }
81
82     /**
83      * Retrieves the healthchecks for all healthy service instances in a given datacenter.
84      * 
85      * GET /v1/health/service/{service}?dc={datacenter}&amp;passing
86      *
87      * @param service        The service to query.
88      * @param catalogOptions The catalog specific options to use.
89      * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a list of
90      * {@link com.orbitz.consul.model.health.HealthCheck} objects.
91      */
92     public ConsulResponse<List<ServiceHealth>> getHealthyServiceInstances(String service, CatalogOptions catalogOptions) {
93         return getHealthyServiceInstances(service, catalogOptions, QueryOptions.BLANK);
94     }
95
96     /**
97      * Retrieves the healthchecks for all healthy service instances with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
98      * 
99      * GET /v1/health/service/{service}?passing
100      *
101      * @param service      The service to query.
102      * @param queryOptions The Query Options to use.
103      * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a list of
104      * {@link com.orbitz.consul.model.health.HealthCheck} objects.
105      */
106     public ConsulResponse<List<ServiceHealth>> getHealthyServiceInstances(String service, QueryOptions queryOptions) {
107         return getHealthyServiceInstances(service, null, queryOptions);
108     }
109
110     /**
111      * Retrieves the healthchecks for all healthy service instances in a given datacenter with
112      * {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
113      * 
114      * GET /v1/health/service/{service}?dc={datacenter}&amp;passing
115      *
116      * @param service        The service to query.
117      * @param catalogOptions The catalog specific options to use.
118      * @param queryOptions   The Query Options to use.
119      * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a list of
120      * {@link com.orbitz.consul.model.health.HealthCheck} objects.
121      */
122     public ConsulResponse<List<ServiceHealth>> getHealthyServiceInstances(String service, CatalogOptions catalogOptions,
123                                                                           QueryOptions queryOptions) {
124         return response(webTarget.path("service").path(service).queryParam("passing", "true"),
125                 catalogOptions, queryOptions, TYPE_SERVICE_HEALTH_LIST);
126     }
127
128     /**
129      * Asynchronously retrieves the healthchecks for all healthy service instances in a given
130      * datacenter with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
131      * 
132      * GET /v1/health/service/{service}?dc={datacenter}&amp;passing
133      * 
134      * Experimental.
135      *
136      * @param service        The service to query.
137      * @param catalogOptions The catalog specific options to use.
138      * @param queryOptions   The Query Options to use.
139      * @param callback       Callback implemented by callee to handle results.
140      */
141     public void getHealthyServiceInstances(String service, CatalogOptions catalogOptions,
142                                            QueryOptions queryOptions,
143                                            ConsulResponseCallback<List<ServiceHealth>> callback) {
144         response(webTarget.path("service").path(service).queryParam("passing", "true"),
145                 catalogOptions, queryOptions, TYPE_SERVICE_HEALTH_LIST, callback);
146     }
147
148     /**
149      * Asynchronously retrieves the healthchecks for all healthy service instances in a given
150      * datacenter with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
151      * 
152      * GET /v1/health/service/{service}?dc={datacenter}&amp;passing
153      * 
154      * Experimental.
155      *
156      * @param service      The service to query.
157      * @param queryOptions The Query Options to use.
158      * @param callback     Callback implemented by callee to handle results.
159      */
160     public void getHealthyServiceInstances(String service, QueryOptions queryOptions,
161                                            ConsulResponseCallback<List<ServiceHealth>> callback) {
162         response(webTarget.path("service").path(service).queryParam("passing", "true"),
163                 CatalogOptions.BLANK, queryOptions, TYPE_SERVICE_HEALTH_LIST, callback);
164     }
165
166     /**
167      * Retrieves the healthchecks for all nodes.
168      * 
169      * GET /v1/health/service/{service}
170      *
171      * @param service The service to query.
172      * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a list of
173      * {@link com.orbitz.consul.model.health.HealthCheck} objects.
174      */
175     public ConsulResponse<List<ServiceHealth>> getAllServiceInstances(String service) {
176         return getAllServiceInstances(service, null, QueryOptions.BLANK);
177     }
178
179     /**
180      * Retrieves the healthchecks for all nodes in a given datacenter.
181      * 
182      * GET /v1/health/service/{service}?dc={datacenter}
183      *
184      * @param service        The service to query.
185      * @param catalogOptions The catalog specific options to use.
186      * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a list of
187      * {@link com.orbitz.consul.model.health.HealthCheck} objects.
188      */
189     public ConsulResponse<List<ServiceHealth>> getAllServiceInstances(String service, CatalogOptions catalogOptions) {
190         return getAllServiceInstances(service, catalogOptions, QueryOptions.BLANK);
191     }
192
193     /**
194      * Retrieves the healthchecks for all nodes with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
195      * 
196      * GET /v1/health/service/{service}
197      *
198      * @param service      The service to query.
199      * @param queryOptions The Query Options to use.
200      * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a list of
201      * {@link com.orbitz.consul.model.health.HealthCheck} objects.
202      */
203     public ConsulResponse<List<ServiceHealth>> getAllServiceInstances(String service, QueryOptions queryOptions) {
204         return getAllServiceInstances(service, null, queryOptions);
205     }
206
207     /**
208      * Retrieves the healthchecks for all nodes in a given datacenter with
209      * {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
210      * 
211      * GET /v1/health/service/{service}?dc={datacenter}
212      *
213      * @param service        The service to query.
214      * @param catalogOptions The catalog specific options to use.
215      * @param queryOptions   The Query Options to use.
216      * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a list of
217      * {@link com.orbitz.consul.model.health.HealthCheck} objects.
218      */
219     public ConsulResponse<List<ServiceHealth>> getAllServiceInstances(String service, CatalogOptions catalogOptions,
220                                                                       QueryOptions queryOptions) {
221         return response(webTarget.path("service").path(service), catalogOptions, queryOptions,
222                 TYPE_SERVICE_HEALTH_LIST);
223     }
224
225     /**
226      * Asynchronously retrieves the healthchecks for all nodes in a given
227      * datacenter with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
228      * 
229      * GET /v1/health/service/{service}?dc={datacenter}
230      * 
231      * Experimental.
232      *
233      * @param service        The service to query.
234      * @param catalogOptions The catalog specific options to use.
235      * @param queryOptions   The Query Options to use.
236      * @param callback       Callback implemented by callee to handle results.
237      */
238     public void getAllServiceInstances(String service, CatalogOptions catalogOptions,
239                                        QueryOptions queryOptions,
240                                        ConsulResponseCallback<List<ServiceHealth>> callback) {
241         response(webTarget.path("service").path(service), catalogOptions, queryOptions,
242                 TYPE_SERVICE_HEALTH_LIST, callback);
243     }
244
245     /**
246      * Asynchronously retrieves the healthchecks for all nodes in a given
247      * datacenter with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
248      * 
249      * GET /v1/health/service/{service}?dc={datacenter}
250      * 
251      * Experimental.
252      *
253      * @param service      The service to query.
254      * @param queryOptions The Query Options to use.
255      * @param callback     Callback implemented by callee to handle results.
256      */
257     public void getAllServiceInstances(String service, QueryOptions queryOptions,
258                                        ConsulResponseCallback<List<ServiceHealth>> callback) {
259         response(webTarget.path("service").path(service), CatalogOptions.BLANK,
260                 queryOptions, TYPE_SERVICE_HEALTH_LIST, callback);
261     }
262 }