bb0edbf85c0b48b8b79e35e704bf854fca743409
[msb/apigateway.git] /
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.apiroute.wrapper.consulextend;
17
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;
25
26 import com.fasterxml.jackson.core.type.TypeReference;
27 import com.orbitz.consul.option.CatalogOptions;
28 import com.orbitz.consul.option.QueryOptions;
29
30 /**
31  * HTTP Client for /v1/catalog/ endpoints.
32  */
33 public class CatalogClient {
34
35         private static final Logger LOGGER = LoggerFactory
36                         .getLogger(CatalogClient.class);
37
38         private static final TypeReference<HttpEntity> TYPE_SERVICES_MAP = new TypeReference<HttpEntity>() {
39         };
40
41
42         private static final String CATALOG_URI_8500 = "/v1/catalog";
43         private static final String CATAlOG_URI_10081 = "/api/catalog/v1";
44
45         private static final String GET_SERVICES_URI = "/services";
46
47         private static final Http httpClient = Http.getInstance();
48
49         private HttpHost targetHost = null;
50         private String catalogUri = CATAlOG_URI_10081;
51
52         CatalogClient(final HttpHost targetHost) {
53                 this.targetHost = targetHost;
54                 if (targetHost.getPort() == 8500) {
55                         catalogUri = CATALOG_URI_8500;
56                 }
57         }
58
59         /**
60          * Retrieves all services for a given datacenter with
61          * {@link com.orbitz.consul.option.QueryOptions}.
62          * 
63          * GET /v1/catalog/services?dc={datacenter}
64          * 
65          * @param catalogOptions
66          *            Catalog specific options to use.
67          * @param queryOptions
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.
71          */
72         public void getServices(CatalogOptions catalogOptions,
73                         QueryOptions queryOptions,
74                         ConsulResponseCallback<HttpEntity> callback) {
75
76                 // prepare access path
77                 // path:10081 vs 8500
78                 String path = targetHost.toString() + catalogUri + GET_SERVICES_URI;
79
80                 // params:wait,index,dc......
81                 String params = Http.optionsFrom(catalogOptions, queryOptions);
82
83                 // node meta: ns,external,internal.....
84                 String node_meta = ConfigUtil.getInstance().getNodeMetaQueryParam();
85
86                 // add params
87                 path = (params != null && !params.isEmpty()) ? path += "?" + params
88                                 : path;
89
90                 // add node_meta
91                 if (node_meta != null && !node_meta.isEmpty()) {
92                         path = path.contains("?") ? path +"&"+ node_meta : path + "?"
93                                         + node_meta;
94                 }
95
96                 // async watch services
97                 if (LOGGER.isDebugEnabled()) {
98                         LOGGER.debug("get all services:" + path);
99                 }
100                 httpClient.asyncGetDelayHandle(path, TYPE_SERVICES_MAP, callback);
101         }       
102 }