cd881b0421c77c1bfd1608818519b3ca7e78c4cb
[msb/apigateway.git] / msb-core / apiroute / apiroute-service / src / main / java / org / openo / msb / wrapper / consul / CatalogClient.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 import java.util.Map;
38
39 import javax.ws.rs.client.WebTarget;
40 import javax.ws.rs.core.GenericType;
41 import javax.ws.rs.core.MediaType;
42
43 import org.openo.msb.wrapper.consul.async.ConsulResponseCallback;
44 import org.openo.msb.wrapper.consul.model.ConsulResponse;
45 import org.openo.msb.wrapper.consul.model.catalog.CatalogNode;
46 import org.openo.msb.wrapper.consul.model.catalog.CatalogService;
47 import org.openo.msb.wrapper.consul.model.health.Node;
48 import org.openo.msb.wrapper.consul.option.CatalogOptions;
49 import org.openo.msb.wrapper.consul.option.QueryOptions;
50
51 /**
52  * HTTP Client for /v1/catalog/ endpoints or api/catalog/v1  by openresty
53  */
54 public class CatalogClient {
55
56     private static final GenericType<List<String>> TYPE_STRING_LIST = new GenericType<List<String>>() {};
57     private static final GenericType<List<Node>> TYPE_NODE_LIST = new GenericType<List<Node>>() {};
58     private static final GenericType<Map<String, List<String>>> TYPE_SERVICES_MAP = new GenericType<Map<String, List<String>>>() {};
59     private static final GenericType<List<CatalogService>> TYPE_CATALOG_SERVICE_LIST = new GenericType<List<CatalogService>>() {};
60     private static final GenericType<CatalogNode> TYPE_CATALOG_NODE = new GenericType<CatalogNode>() {};
61     
62     private final WebTarget webTarget;
63     
64     /**
65      * Constructs an instance of this class.
66      *
67      * @param webTarget The {@link javax.ws.rs.client.WebTarget} to base requests from.
68      */
69     CatalogClient(WebTarget webTarget) {
70         this.webTarget = webTarget;        
71     }
72
73     /**
74      * Retrieves all datacenters.
75      *
76      * GET /v1/catalog/datacenters
77      *
78      * @return A list of datacenter names.
79      */
80     public List<String> getDatacenters() {
81         return webTarget.path("datacenters").request()
82                 .accept(MediaType.APPLICATION_JSON_TYPE).get(TYPE_STRING_LIST);
83     }
84
85     /**
86      * Retrieves all nodes.
87      *
88      * GET /v1/catalog/nodes
89      *
90      * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a list of
91      * {@link org.openo.msb.wrapper.consul.model.health.Node} objects.
92      */
93     public ConsulResponse<List<Node>> getNodes() {
94         return getNodes(null, QueryOptions.BLANK);
95     }
96
97     /**
98      * Retrieves all nodes for a given datacenter.
99      *
100      * GET /v1/catalog/nodes?dc={datacenter}
101      *
102      * @param catalogOptions Catalog specific options to use.      
103      * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a list of
104      * {@link org.openo.msb.wrapper.consul.model.health.Node} objects.
105      */
106     public ConsulResponse<List<Node>> getNodes(CatalogOptions catalogOptions) {
107         return getNodes(catalogOptions, QueryOptions.BLANK);
108     }
109
110     /**
111      * Retrieves all nodes with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
112      *
113      * GET /v1/catalog/nodes
114      *
115      * @param queryOptions The Query Options to use.
116      * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a list of
117      * {@link org.openo.msb.wrapper.consul.model.health.Node} objects.
118      */
119     public ConsulResponse<List<Node>> getNodes(QueryOptions queryOptions) {
120         return getNodes(null, queryOptions);
121     }
122
123     /**
124      * Retrieves all nodes for a given datacenter with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
125      *
126      * GET /v1/catalog/nodes?dc={datacenter}
127      *
128      * @param catalogOptions Catalog specific options to use.      
129      * @param queryOptions The Query Options to use.
130      * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a list of
131      * {@link org.openo.msb.wrapper.consul.model.health.Node} objects.
132      */
133     public ConsulResponse<List<Node>> getNodes(CatalogOptions catalogOptions, QueryOptions queryOptions) {
134         return response(webTarget.path("nodes"), catalogOptions, queryOptions, TYPE_NODE_LIST);
135     }
136
137     /**
138      * Retrieves all services for a given datacenter.
139      *
140      * GET /v1/catalog/services?dc={datacenter}
141      *
142      * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a map of service name to list of tags.
143      */
144     public ConsulResponse<Map<String, List<String>>> getServices() {
145         return getServices(null, QueryOptions.BLANK);
146     }
147
148     /**
149      * Retrieves all services for a given datacenter.
150      *
151      * GET /v1/catalog/services?dc={datacenter}
152      *
153      * @param catalogOptions Catalog specific options to use.      
154      * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a map of service name to list of tags.
155      */
156     public ConsulResponse<Map<String, List<String>>> getServices(CatalogOptions catalogOptions) {
157         return getServices(catalogOptions, QueryOptions.BLANK);
158     }
159
160     /**
161      * Retrieves all services for a given datacenter with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
162      *
163      * GET /v1/catalog/services?dc={datacenter}
164      *
165      * @param queryOptions The Query Options to use.
166      * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a map of service name to list of tags.
167      */
168     public ConsulResponse<Map<String, List<String>>> getServices(QueryOptions queryOptions) {
169         return getServices(null, queryOptions);
170     }
171
172     /**
173      * Retrieves all services for a given datacenter with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
174      *
175      * GET /v1/catalog/services?dc={datacenter}
176      *
177      * @param catalogOptions Catalog specific options to use.      
178      * @param queryOptions The Query Options to use.
179      * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a map of service name to list of tags.
180      */
181     public ConsulResponse<Map<String, List<String>>> getServices(CatalogOptions catalogOptions, QueryOptions queryOptions) {
182         return response(webTarget.path("services"), catalogOptions, queryOptions, TYPE_SERVICES_MAP);
183     }
184     
185     public void  getService(QueryOptions queryOptions, ConsulResponseCallback<Map<String, List<String>>> callback) {
186         response(webTarget.path("services"), CatalogOptions.BLANK,
187             queryOptions, TYPE_SERVICES_MAP, callback);
188     }
189
190     /**
191      * Retrieves a single service.
192      *
193      * GET /v1/catalog/service/{service}
194      *
195      * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing
196      * {@link org.openo.msb.wrapper.consul.model.catalog.CatalogService} objects.
197      */
198     public ConsulResponse<List<CatalogService>> getService(String service) {
199         return getService(service, null, QueryOptions.BLANK);
200     }
201
202     /**
203      * Retrieves a single service for a given datacenter.
204      *
205      * GET /v1/catalog/service/{service}?dc={datacenter}
206      *
207      * @param catalogOptions Catalog specific options to use.      
208      * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing
209      * {@link org.openo.msb.wrapper.consul.model.catalog.CatalogService} objects.
210      */
211     public ConsulResponse<List<CatalogService>> getService(String service, CatalogOptions catalogOptions) {
212         return getService(service, catalogOptions, QueryOptions.BLANK);
213     }
214
215     /**
216      * Retrieves a single service with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
217      *
218      * GET /v1/catalog/service/{service}
219      *
220      * @param queryOptions The Query Options to use.
221      * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing
222      * {@link org.openo.msb.wrapper.consul.model.catalog.CatalogService} objects.
223      */
224     public ConsulResponse<List<CatalogService>> getService(String service, QueryOptions queryOptions) {
225         return getService(service, null, queryOptions);
226     }
227     
228     public void getService(String service, QueryOptions queryOptions, ConsulResponseCallback<List<CatalogService>> callback) {
229    
230         response(webTarget.path("service").path(service), CatalogOptions.BLANK,
231             queryOptions, TYPE_CATALOG_SERVICE_LIST, callback);
232     }
233     
234    
235
236     /**
237      * Retrieves a single service for a given datacenter with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
238      *
239      * GET /v1/catalog/service/{service}?dc={datacenter}
240      *
241      * @param catalogOptions Catalog specific options to use.      
242      * @param queryOptions The Query Options to use.
243      * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing
244      * {@link org.openo.msb.wrapper.consul.model.catalog.CatalogService} objects.
245      */
246     public ConsulResponse<List<CatalogService>> getService(String service, CatalogOptions catalogOptions,
247                                                            QueryOptions queryOptions) {
248         return response(webTarget.path("service").path(service), catalogOptions, queryOptions,
249                 TYPE_CATALOG_SERVICE_LIST);
250     }
251
252     /**
253      * Retrieves a single node.
254      *
255      * GET /v1/catalog/node/{node}
256      *
257      * @return A list of matching {@link org.openo.msb.wrapper.consul.model.catalog.CatalogService} objects.
258      */
259     public ConsulResponse<CatalogNode> getNode(String node) {
260         return getNode(node, null, QueryOptions.BLANK);
261     }
262
263     /**
264      * Retrieves a single node for a given datacenter.
265      *
266      * GET /v1/catalog/node/{node}?dc={datacenter}
267      *
268      * @param catalogOptions Catalog specific options to use.      
269      * @return A list of matching {@link org.openo.msb.wrapper.consul.model.catalog.CatalogService} objects.
270      */
271     public ConsulResponse<CatalogNode> getNode(String node, CatalogOptions catalogOptions) {
272         return getNode(node, catalogOptions, QueryOptions.BLANK);
273     }
274
275     /**
276      * Retrieves a single node with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
277      *
278      * GET /v1/catalog/node/{node}
279      *
280      * @param queryOptions The Query Options to use.
281      * @return A list of matching {@link org.openo.msb.wrapper.consul.model.catalog.CatalogService} objects.
282      */
283     public ConsulResponse<CatalogNode> getNode(String node, QueryOptions queryOptions) {
284         return getNode(node, null, queryOptions);
285     }
286
287     /**
288      * Retrieves a single node for a given datacenter with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
289      *
290      * GET /v1/catalog/node/{node}?dc={datacenter}
291      *
292      * @param catalogOptions Catalog specific options to use.      
293      * @param queryOptions The Query Options to use.
294      * @return A list of matching {@link org.openo.msb.wrapper.consul.model.catalog.CatalogService} objects.
295      */
296     public ConsulResponse<CatalogNode> getNode(String node, CatalogOptions catalogOptions, QueryOptions queryOptions) {
297         return response(webTarget.path("node").path(node), catalogOptions, queryOptions,
298                 TYPE_CATALOG_NODE);
299     }
300 }