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