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