1 /*******************************************************************************
2 * Copyright 2016-2017 ZTE, Inc. and others.
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.
15 ******************************************************************************/
16 package org.onap.msb.apiroute.wrapper.consulextend;
18 import org.apache.http.HttpEntity;
19 import org.apache.http.HttpHost;
20 import org.onap.msb.apiroute.wrapper.consulextend.async.ConsulResponseCallback;
21 import org.onap.msb.apiroute.wrapper.consulextend.util.Http;
22 import org.onap.msb.apiroute.wrapper.util.ConfigUtil;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import com.fasterxml.jackson.core.type.TypeReference;
27 import com.orbitz.consul.option.CatalogOptions;
28 import com.orbitz.consul.option.QueryOptions;
31 * HTTP Client for /v1/catalog/ endpoints.
33 public class CatalogClient {
35 private static final Logger LOGGER = LoggerFactory
36 .getLogger(CatalogClient.class);
38 private static final TypeReference<HttpEntity> TYPE_SERVICES_MAP = new TypeReference<HttpEntity>() {
42 private static final String CATALOG_URI_8500 = "/v1/catalog";
43 private static final String CATAlOG_URI_10081 = "/api/catalog/v1";
45 private static final String GET_SERVICES_URI = "/services";
47 private static final Http httpClient = Http.getInstance();
49 private HttpHost targetHost = null;
50 private String catalogUri = CATAlOG_URI_10081;
52 CatalogClient(final HttpHost targetHost) {
53 this.targetHost = targetHost;
54 if (targetHost.getPort() == 8500) {
55 catalogUri = CATALOG_URI_8500;
60 * Retrieves all services for a given datacenter with
61 * {@link com.orbitz.consul.option.QueryOptions}.
63 * GET /v1/catalog/services?dc={datacenter}
65 * @param catalogOptions
66 * Catalog specific options to use.
68 * The Query Options to use.
69 * @return A {@link com.orbitz.consul.model.ConsulResponse} containing a map
70 * of service name to list of tags.
72 public void getServices(CatalogOptions catalogOptions,
73 QueryOptions queryOptions,
74 ConsulResponseCallback<HttpEntity> callback) {
76 // prepare access path
78 String path = targetHost.toString() + catalogUri + GET_SERVICES_URI;
80 // params:wait,index,dc......
81 String params = Http.optionsFrom(catalogOptions, queryOptions);
83 // node meta: ns,external,internal.....
84 String node_meta = ConfigUtil.getInstance().getNodeMetaQueryParam();
87 path = (params != null && !params.isEmpty()) ? path += "?" + params
91 if (node_meta != null && !node_meta.isEmpty()) {
92 path = path.contains("?") ? path +"&"+ node_meta : path + "?"
96 // async watch services
97 if (LOGGER.isDebugEnabled()) {
98 LOGGER.debug("get all services:" + path);
100 httpClient.asyncGetDelayHandle(path, TYPE_SERVICES_MAP, callback);