update sparky with configurable features
[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   private static final String BULK_API = "bulk";
47   private static final String DOCUMENT_EDNPOINT = "documents";
48   private static final String SEARH_SERVICE_BULK_TEMPLATE =
49               "{\"create\":{\"metaData\":{\"url\":\"%s\"},\"document\":\"%s\"}}\n";
50   
51   private static final String SEARH_SERVICE_SINGLE_ENTITY_TEMPLATE =
52               "{\"queries\":[{\"must\":{\"match\":{\"field\":\"_id\",\"value\":\"%s\"}}}]}\n";
53
54   private RestClient client;
55   private RestEndpointConfig endpointConfig;
56   private String serviceApiVersion;
57   private String appPartnerName = "";
58
59   private Map<String, List<String>> commonHeaders;
60
61   /**
62    * Instantiates a new search adapter.
63    * 
64    * @throws Exception
65    */
66   public SearchServiceAdapter(RestEndpointConfig endpointConfig, String serviceApiVersion)
67       throws Exception {
68
69     client = RestClientFactory.buildClient(endpointConfig);
70
71     commonHeaders = new HashMap<String, List<String>>();
72     commonHeaders.put("Accept", Arrays.asList("application/json"));
73     commonHeaders.put(Headers.FROM_APP_ID, Arrays.asList(appPartnerName));
74
75     this.serviceApiVersion = serviceApiVersion;
76     this.endpointConfig = endpointConfig;
77   }
78   
79   public String getAppPartnerName() {
80     return appPartnerName;
81   }
82
83   public void setAppPartnerName(String appPartnerName) {
84     this.appPartnerName = appPartnerName;
85   }
86
87   public String getServiceApiVersion() {
88     return serviceApiVersion;
89   }
90
91   public void setServiceApiVersion(String serviceApiVersion) {
92     this.serviceApiVersion = serviceApiVersion;
93   }
94
95   public RestEndpointConfig getEndpointConfig() {
96     return endpointConfig;
97   }
98
99   public void setEndpointConfig(RestEndpointConfig endpointConfig) {
100     this.endpointConfig = endpointConfig;
101   }
102
103   public OperationResult doPost(String url, String jsonPayload) {
104     OperationResult or = client.post(url, jsonPayload, getTxnHeader(),
105         MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
106     return new OperationResult(or.getResultCode(), or.getResult());
107   }
108   
109   @Deprecated
110   public OperationResult doPost(String url, String jsonPayload, String acceptContentType) {
111     OperationResult or = client.post(url, jsonPayload, getTxnHeader(),
112         MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
113     return new OperationResult(or.getResultCode(), or.getResult());
114   }
115
116   public OperationResult doGet(String url, String acceptContentType) {
117     OperationResult or = client.get(url, getTxnHeader(), MediaType.APPLICATION_JSON_TYPE);
118     return new OperationResult(or.getResultCode(), or.getResult());
119   }
120
121   public OperationResult doPut(String url, String payload, String acceptContentType) {
122     OperationResult or = client.put(url, payload, getTxnHeader(), MediaType.APPLICATION_JSON_TYPE,
123         MediaType.APPLICATION_JSON_TYPE);
124     return new OperationResult(or.getResultCode(), or.getResult());
125   }
126
127   public OperationResult doDelete(String url, String acceptContentType) {
128
129     OperationResult or = client.delete(url, getTxnHeader(), MediaType.APPLICATION_JSON_TYPE);
130     return new OperationResult(or.getResultCode(), or.getResult());
131   }
132   
133   public OperationResult doBulkOperation(String url, String jsonPayload) {
134           
135           OperationResult or = client.post(url, jsonPayload, getTxnHeader(),
136                           MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
137           return new OperationResult(or.getResultCode(), or.getResult());
138   }
139
140   public Map<String, List<String>> getTxnHeader() {
141     HashMap<String, List<String>> headers = new HashMap<String, List<String>>();
142     headers.putAll(this.commonHeaders);
143     headers.put("X-TransactionId", Arrays.asList(MDC.get(MdcContext.MDC_REQUEST_ID)));
144     headers.put("X-FromAppId", Arrays.asList(MDC.get(MdcContext.MDC_PARTNER_NAME)));
145     return headers;
146   }
147   
148 public String buildBulkImportOperationRequest(String indexName, String id, String payload){
149           
150           StringBuilder requestPayload = new StringBuilder(128);
151           String SearchTarget = buildSearchServiceDocUrl(indexName,id);
152           
153           requestPayload.append(String.format(SEARH_SERVICE_BULK_TEMPLATE,SearchTarget,payload));
154           return requestPayload.toString();   
155   }
156
157   /**
158    * Get Full URL for search
159    *
160    * @param api the api
161    * @param indexName
162    * @return the full url
163    */
164   public String buildSearchServiceQueryUrl(String indexName) {
165     return buildSearchServiceUrlForApi(indexName, VALUE_QUERY);
166   }
167
168   /**
169    * Get Full URL for search
170    *
171    * @param api the api
172    * @param indexName
173    * @return the full url
174    */
175   public String buildSuggestServiceQueryUrl(String indexName) {
176     return buildSearchServiceUrlForApi(indexName, SUGGEST_QUERY);
177   }
178   
179   public String buildSearchServiceDocUrl(String indexName,String api) {
180           
181           return String.format("https://%s:%s/services/search-data-service/%s/search/indexes/%s/%s/%s",
182                       endpointConfig.getEndpointIpAddress(), endpointConfig.getEndpointServerPort(),
183                         serviceApiVersion, indexName,DOCUMENT_EDNPOINT, api);
184   }
185   
186   
187   public String buildSearchServiceCreateDocApi(String indexName){
188           
189           return String.format("https://%s:%s/services/search-data-service/%s/search/indexes/%s/%s",
190                       endpointConfig.getEndpointIpAddress(), endpointConfig.getEndpointServerPort(),
191                       serviceApiVersion, indexName,DOCUMENT_EDNPOINT );
192   }
193
194   public String buildSearchServiceUrlForApi(String indexName, String api) {
195           
196     return String.format("https://%s:%s/services/search-data-service/%s/search/indexes/%s/%s",
197         endpointConfig.getEndpointIpAddress(), endpointConfig.getEndpointServerPort(),
198         serviceApiVersion, indexName, api);
199   }
200   
201   public String buildSearchServiceBulkUrl() {
202           
203             return String.format("https://%s:%s/services/search-data-service/%s/search/%s", endpointConfig.getEndpointIpAddress(),
204                 endpointConfig.getEndpointServerPort(),serviceApiVersion,BULK_API);
205   }
206
207   public OperationResult retrieveEntityById(String entityId,String indexName) {
208           
209           StringBuilder requestPayload = new StringBuilder(128);
210           requestPayload.append(String.format(SEARH_SERVICE_SINGLE_ENTITY_TEMPLATE,entityId));
211           String payload = requestPayload.toString();
212           String searchServiceUrl = buildSearchServiceQueryUrl(indexName);
213            
214           return this.doPost(searchServiceUrl,payload);
215   }
216
217 public String buildSearchServiceCreateIndexUrl(String indexName) {
218         
219             return String.format("https://%s:%s/services/search-data-service/%s/search/indexes/dynamic/%s", endpointConfig.getEndpointIpAddress(),
220                 endpointConfig.getEndpointServerPort(),serviceApiVersion,indexName);
221         }
222
223
224 }