2 * Copyright 2016 2015-2016 ZTE, Inc. and others. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
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
23 * http://www.apache.org/licenses/LICENSE-2.0
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.
32 package org.openo.msb.wrapper.consul;
34 import static org.openo.msb.wrapper.consul.util.ClientUtil.response;
36 import java.util.List;
39 import javax.ws.rs.client.WebTarget;
40 import javax.ws.rs.core.GenericType;
41 import javax.ws.rs.core.MediaType;
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;
52 * HTTP Client for /v1/catalog/ endpoints or api/catalog/v1 by openresty
54 public class CatalogClient {
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>() {};
62 private final WebTarget webTarget;
65 * Constructs an instance of this class.
67 * @param webTarget The {@link javax.ws.rs.client.WebTarget} to base requests from.
69 CatalogClient(WebTarget webTarget) {
70 this.webTarget = webTarget;
74 * Retrieves all datacenters.
76 * GET /v1/catalog/datacenters
78 * @return A list of datacenter names.
80 public List<String> getDatacenters() {
81 return webTarget.path("datacenters").request()
82 .accept(MediaType.APPLICATION_JSON_TYPE).get(TYPE_STRING_LIST);
86 * Retrieves all nodes.
88 * GET /v1/catalog/nodes
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.
93 public ConsulResponse<List<Node>> getNodes() {
94 return getNodes(null, QueryOptions.BLANK);
98 * Retrieves all nodes for a given datacenter.
100 * GET /v1/catalog/nodes?dc={datacenter}
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.
106 public ConsulResponse<List<Node>> getNodes(CatalogOptions catalogOptions) {
107 return getNodes(catalogOptions, QueryOptions.BLANK);
111 * Retrieves all nodes with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
113 * GET /v1/catalog/nodes
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.
119 public ConsulResponse<List<Node>> getNodes(QueryOptions queryOptions) {
120 return getNodes(null, queryOptions);
124 * Retrieves all nodes for a given datacenter with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
126 * GET /v1/catalog/nodes?dc={datacenter}
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.
133 public ConsulResponse<List<Node>> getNodes(CatalogOptions catalogOptions, QueryOptions queryOptions) {
134 return response(webTarget.path("nodes"), catalogOptions, queryOptions, TYPE_NODE_LIST);
138 * Retrieves all services for a given datacenter.
140 * GET /v1/catalog/services?dc={datacenter}
142 * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a map of service name to list of tags.
144 public ConsulResponse<Map<String, List<String>>> getServices() {
145 return getServices(null, QueryOptions.BLANK);
149 * Retrieves all services for a given datacenter.
151 * GET /v1/catalog/services?dc={datacenter}
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.
156 public ConsulResponse<Map<String, List<String>>> getServices(CatalogOptions catalogOptions) {
157 return getServices(catalogOptions, QueryOptions.BLANK);
161 * Retrieves all services for a given datacenter with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
163 * GET /v1/catalog/services?dc={datacenter}
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.
168 public ConsulResponse<Map<String, List<String>>> getServices(QueryOptions queryOptions) {
169 return getServices(null, queryOptions);
173 * Retrieves all services for a given datacenter with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
175 * GET /v1/catalog/services?dc={datacenter}
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.
181 public ConsulResponse<Map<String, List<String>>> getServices(CatalogOptions catalogOptions, QueryOptions queryOptions) {
182 return response(webTarget.path("services"), catalogOptions, queryOptions, TYPE_SERVICES_MAP);
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);
191 * Retrieves a single service.
193 * GET /v1/catalog/service/{service}
195 * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing
196 * {@link org.openo.msb.wrapper.consul.model.catalog.CatalogService} objects.
198 public ConsulResponse<List<CatalogService>> getService(String service) {
199 return getService(service, null, QueryOptions.BLANK);
203 * Retrieves a single service for a given datacenter.
205 * GET /v1/catalog/service/{service}?dc={datacenter}
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.
211 public ConsulResponse<List<CatalogService>> getService(String service, CatalogOptions catalogOptions) {
212 return getService(service, catalogOptions, QueryOptions.BLANK);
216 * Retrieves a single service with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
218 * GET /v1/catalog/service/{service}
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.
224 public ConsulResponse<List<CatalogService>> getService(String service, QueryOptions queryOptions) {
225 return getService(service, null, queryOptions);
228 public void getService(String service, QueryOptions queryOptions, ConsulResponseCallback<List<CatalogService>> callback) {
230 response(webTarget.path("service").path(service), CatalogOptions.BLANK,
231 queryOptions, TYPE_CATALOG_SERVICE_LIST, callback);
237 * Retrieves a single service for a given datacenter with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
239 * GET /v1/catalog/service/{service}?dc={datacenter}
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.
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);
253 * Retrieves a single node.
255 * GET /v1/catalog/node/{node}
257 * @return A list of matching {@link org.openo.msb.wrapper.consul.model.catalog.CatalogService} objects.
259 public ConsulResponse<CatalogNode> getNode(String node) {
260 return getNode(node, null, QueryOptions.BLANK);
264 * Retrieves a single node for a given datacenter.
266 * GET /v1/catalog/node/{node}?dc={datacenter}
268 * @param catalogOptions Catalog specific options to use.
269 * @return A list of matching {@link org.openo.msb.wrapper.consul.model.catalog.CatalogService} objects.
271 public ConsulResponse<CatalogNode> getNode(String node, CatalogOptions catalogOptions) {
272 return getNode(node, catalogOptions, QueryOptions.BLANK);
276 * Retrieves a single node with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
278 * GET /v1/catalog/node/{node}
280 * @param queryOptions The Query Options to use.
281 * @return A list of matching {@link org.openo.msb.wrapper.consul.model.catalog.CatalogService} objects.
283 public ConsulResponse<CatalogNode> getNode(String node, QueryOptions queryOptions) {
284 return getNode(node, null, queryOptions);
288 * Retrieves a single node for a given datacenter with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
290 * GET /v1/catalog/node/{node}?dc={datacenter}
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.
296 public ConsulResponse<CatalogNode> getNode(String node, CatalogOptions catalogOptions, QueryOptions queryOptions) {
297 return response(webTarget.path("node").path(node), catalogOptions, queryOptions,