ES to SDS conversion
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / search / SearchServiceAdapter.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.sparky.search;
22
23 import java.util.Arrays;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import javax.ws.rs.core.MediaType;
29
30 import org.onap.aai.cl.mdc.MdcContext;
31 import org.onap.aai.restclient.client.Headers;
32 import org.onap.aai.restclient.client.OperationResult;
33 import org.onap.aai.restclient.client.RestClient;
34 import org.onap.aai.sparky.dal.rest.RestClientFactory;
35 import org.onap.aai.sparky.dal.rest.config.RestEndpointConfig;
36 import org.slf4j.MDC;
37
38
39 /**
40  * The Class SearchServiceAdapter.
41  */
42 public class SearchServiceAdapter {
43
44   private static final String VALUE_QUERY = "query";
45   private static final String SUGGEST_QUERY = "suggest";
46
47   private RestClient client;
48   private RestEndpointConfig endpointConfig;
49   private String serviceApiVersion;
50
51   private Map<String, List<String>> commonHeaders;
52
53   /**
54    * Instantiates a new search adapter.
55    * 
56    * @throws Exception
57    */
58   public SearchServiceAdapter(RestEndpointConfig endpointConfig, String serviceApiVersion)
59       throws Exception {
60
61     client = RestClientFactory.buildClient(endpointConfig);
62
63     commonHeaders = new HashMap<String, List<String>>();
64     commonHeaders.put("Accept", Arrays.asList("application/json"));
65     commonHeaders.put(Headers.FROM_APP_ID, Arrays.asList("AAI-UI"));
66
67     this.serviceApiVersion = serviceApiVersion;
68     this.endpointConfig = endpointConfig;
69   }
70
71   public String getServiceApiVersion() {
72     return serviceApiVersion;
73   }
74
75   public void setServiceApiVersion(String serviceApiVersion) {
76     this.serviceApiVersion = serviceApiVersion;
77   }
78
79   public RestEndpointConfig getEndpointConfig() {
80     return endpointConfig;
81   }
82
83   public void setEndpointConfig(RestEndpointConfig endpointConfig) {
84     this.endpointConfig = endpointConfig;
85   }
86
87   public OperationResult doPost(String url, String jsonPayload) {
88     OperationResult or = client.post(url, jsonPayload, getTxnHeader(),
89         MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
90     return new OperationResult(or.getResultCode(), or.getResult());
91   }
92   
93   @Deprecated
94   public OperationResult doPost(String url, String jsonPayload, String acceptContentType) {
95     OperationResult or = client.post(url, jsonPayload, getTxnHeader(),
96         MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
97     return new OperationResult(or.getResultCode(), or.getResult());
98   }
99
100   public OperationResult doGet(String url, String acceptContentType) {
101     OperationResult or = client.get(url, getTxnHeader(), MediaType.APPLICATION_JSON_TYPE);
102     return new OperationResult(or.getResultCode(), or.getResult());
103   }
104
105   public OperationResult doPut(String url, String payload, String acceptContentType) {
106     OperationResult or = client.put(url, payload, getTxnHeader(), MediaType.APPLICATION_JSON_TYPE,
107         MediaType.APPLICATION_JSON_TYPE);
108     return new OperationResult(or.getResultCode(), or.getResult());
109   }
110
111   public OperationResult doDelete(String url, String acceptContentType) {
112
113     OperationResult or = client.delete(url, getTxnHeader(), MediaType.APPLICATION_JSON_TYPE);
114     return new OperationResult(or.getResultCode(), or.getResult());
115   }
116
117   public Map<String, List<String>> getTxnHeader() {
118     HashMap<String, List<String>> headers = new HashMap<String, List<String>>();
119     headers.putAll(this.commonHeaders);
120     headers.put("X-TransactionId", Arrays.asList(MDC.get(MdcContext.MDC_REQUEST_ID)));
121     headers.put("X-FromAppId", Arrays.asList(MDC.get(MdcContext.MDC_PARTNER_NAME)));
122     return headers;
123   }
124
125   /**
126    * Get Full URL for search
127    *
128    * @param api the api
129    * @param indexName
130    * @return the full url
131    */
132   public String buildSearchServiceQueryUrl(String indexName) {
133     return buildSearchServiceUrlForApi(indexName, VALUE_QUERY);
134   }
135
136   /**
137    * Get Full URL for search
138    *
139    * @param api the api
140    * @param indexName
141    * @return the full url
142    */
143   public String buildSuggestServiceQueryUrl(String indexName) {
144     return buildSearchServiceUrlForApi(indexName, SUGGEST_QUERY);
145   }
146
147   public String buildSearchServiceUrlForApi(String indexName, String api) {
148     return String.format("https://%s:%s/services/search-data-service/%s/search/indexes/%s/%s",
149         endpointConfig.getEndpointIpAddress(), endpointConfig.getEndpointServerPort(),
150         serviceApiVersion, indexName, api);
151   }
152
153
154 }