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