Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / wrapper / consulextend / CatalogClient.java
1 package org.onap.msb.apiroute.wrapper.consulextend;
2
3 import org.apache.http.HttpEntity;
4 import org.apache.http.HttpHost;
5 import org.onap.msb.apiroute.wrapper.consulextend.async.ConsulResponseCallback;
6 import org.onap.msb.apiroute.wrapper.consulextend.util.Http;
7 import org.onap.msb.apiroute.wrapper.util.ConfigUtil;
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10
11 import com.fasterxml.jackson.core.type.TypeReference;
12 import com.orbitz.consul.option.CatalogOptions;
13 import com.orbitz.consul.option.QueryOptions;
14
15 /**
16  * HTTP Client for /v1/catalog/ endpoints.
17  */
18 public class CatalogClient {
19
20         private static final Logger LOGGER = LoggerFactory
21                         .getLogger(CatalogClient.class);
22
23         private static final TypeReference<HttpEntity> TYPE_SERVICES_MAP = new TypeReference<HttpEntity>() {
24         };
25
26
27         private static final String CATALOG_URI_8500 = "/v1/catalog";
28         private static final String CATAlOG_URI_10081 = "/api/catalog/v1";
29
30         private static final String GET_SERVICES_URI = "/services";
31
32         private static final Http httpClient = Http.getInstance();
33
34         private HttpHost targetHost = null;
35         private String catalogUri = CATAlOG_URI_10081;
36
37         CatalogClient(final HttpHost targetHost) {
38                 this.targetHost = targetHost;
39                 if (targetHost.getPort() == 8500) {
40                         catalogUri = CATALOG_URI_8500;
41                 }
42         }
43
44         /**
45          * Retrieves all services for a given datacenter with
46          * {@link com.orbitz.consul.option.QueryOptions}.
47          * 
48          * GET /v1/catalog/services?dc={datacenter}
49          * 
50          * @param catalogOptions
51          *            Catalog specific options to use.
52          * @param queryOptions
53          *            The Query Options to use.
54          * @return A {@link com.orbitz.consul.model.ConsulResponse} containing a map
55          *         of service name to list of tags.
56          */
57         public void getServices(CatalogOptions catalogOptions,
58                         QueryOptions queryOptions,
59                         ConsulResponseCallback<HttpEntity> callback) {
60
61                 // prepare access path
62                 // path:10081 vs 8500
63                 String path = targetHost.toString() + catalogUri + GET_SERVICES_URI;
64
65                 // params:wait,index,dc......
66                 String params = Http.optionsFrom(catalogOptions, queryOptions);
67
68                 // node meta: ns,external,internal.....
69                 String node_meta = ConfigUtil.getInstance().getNodeMetaQueryParam();
70
71                 // add params
72                 path = (params != null && !params.isEmpty()) ? path += "?" + params
73                                 : path;
74
75                 // add node_meta
76                 if (node_meta != null && !node_meta.isEmpty()) {
77                         path = path.contains("?") ? path +"&"+ node_meta : path + "?"
78                                         + node_meta;
79                 }
80
81                 // async watch services
82                 if (LOGGER.isDebugEnabled()) {
83                         LOGGER.debug("get all services:" + path);
84                 }
85                 httpClient.asyncGetDelayHandle(path, TYPE_SERVICES_MAP, callback);
86         }       
87 }