2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright © 2017 AT&T Intellectual Property.
\r
6 * Copyright © 2017 Amdocs
\r
7 * All rights reserved.
\r
8 * ================================================================================
\r
9 * Licensed under the Apache License, Version 2.0 (the "License");
\r
10 * you may not use this file except in compliance with the License.
\r
11 * You may obtain a copy of the License at
\r
13 * http://www.apache.org/licenses/LICENSE-2.0
\r
15 * Unless required by applicable law or agreed to in writing, software
\r
16 * distributed under the License is distributed on an "AS IS" BASIS,
\r
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
18 * See the License for the specific language governing permissions and
\r
19 * limitations under the License.
\r
20 * ============LICENSE_END=========================================================
\r
22 * ECOMP and OpenECOMP are trademarks
\r
23 * and service marks of AT&T Intellectual Property.
\r
25 package org.openecomp.datarouter.util;
\r
27 import java.io.BufferedReader;
\r
28 import java.io.InputStreamReader;
\r
29 import java.nio.charset.StandardCharsets;
\r
30 import java.util.Arrays;
\r
31 import java.util.HashMap;
\r
32 import java.util.List;
\r
33 import java.util.Map;
\r
34 import java.util.UUID;
\r
36 import javax.ws.rs.core.MediaType;
\r
37 import javax.ws.rs.core.MultivaluedMap;
\r
38 import javax.ws.rs.core.Response.Status;
\r
40 import org.eclipse.jetty.util.security.Password;
\r
41 import org.openecomp.cl.api.Logger;
\r
42 import org.openecomp.cl.mdc.MdcContext;
\r
43 import org.openecomp.datarouter.logging.DataRouterMsgs;
\r
44 import org.openecomp.datarouter.policy.EntityEventPolicy;
\r
45 import org.openecomp.restclient.client.Headers;
\r
46 import org.openecomp.restclient.client.OperationResult;
\r
47 import org.openecomp.restclient.client.RestClient;
\r
48 import org.openecomp.restclient.enums.RestAuthenticationMode;
\r
49 import org.openecomp.restclient.rest.HttpUtil;
\r
50 import org.slf4j.MDC;
\r
52 import com.sun.jersey.core.util.MultivaluedMapImpl;
\r
54 public class SearchServiceAgent {
\r
56 private Logger logger;
\r
58 private RestClient searchClient = null;
\r
59 private Map<String, String> indexSchemaMapping = new HashMap<String, String>();
\r
61 private String searchUrl = null;
\r
62 private String documentEndpoint = null;
\r
66 * Creates a new instance of the search service agent.
\r
68 * @param certName - Certificate to use for talking to the Search Service.
\r
69 * @param keystore - Keystore to use for talking to the Search Service.
\r
70 * @param keystorePwd - Keystore password for talking to the Search Service.
\r
71 * @param searchUrl - URL at which the Search Service can be reached.
\r
72 * @param documentEndpoint - Endpoint for accessing document resources on the Search Service.
\r
73 * @param logger - Logger to use for system logs.
\r
75 public SearchServiceAgent(String certName,
\r
79 String documentEndpoint,
\r
82 initialize(certName, keystore, keystorePwd, searchUrl, documentEndpoint, logger);
\r
87 * Performs all one-time initialization required for the search agent.
\r
89 * @param certName - Certificate to use for talking to the Search Service.
\r
90 * @param keystore - Keystore to use for talking to the Search Service.
\r
91 * @param keystorePwd - Keystore password for talking to the Search Service.
\r
92 * @param searchUrl - URL at which the Search Service can be reached.
\r
93 * @param documentEndpoint - Endpoint for accessing document resources on the Search Service.
\r
94 * @param logger - Logger to use for system logs.
\r
96 private void initialize(String certName,
\r
98 String keystorePwd,
\r
100 String documentEndpoint,
\r
103 // Create REST client for search service
\r
104 searchClient = new RestClient()
\r
105 .authenticationMode(RestAuthenticationMode.SSL_CERT)
\r
106 .validateServerHostname(false)
\r
107 .validateServerCertChain(true)
\r
108 .clientCertFile(DataRouterConstants.DR_HOME_AUTH + certName)
\r
109 .clientCertPassword(Password.deobfuscate(keystorePwd))
\r
110 .trustStore(DataRouterConstants.DR_HOME_AUTH + keystore);
\r
112 this.searchUrl = searchUrl;
\r
113 this.documentEndpoint = documentEndpoint;
\r
115 this.logger = logger;
\r
120 * Creates an index through the search db abstraction
\r
122 * @param index - The name of the index to be created.
\r
123 * @param schemaLocation - The name of the schema file for the index.
\r
125 public void createSearchIndex(String index, String schemaLocation) {
\r
127 // Create a mapping of the index name to schema location
\r
128 indexSchemaMapping.put(index, schemaLocation);
\r
130 // Now, create the index.
\r
131 createIndex(index, schemaLocation);
\r
136 * This method performs the actual work of creating a search index.
\r
138 * @param index - The name of the index to be created.
\r
139 * @param schemaLocation - The name of the schema file for the index.
\r
141 private void createIndex(String index, String schemaLocation) {
\r
143 logger.debug("Creating search index, index name: = " + index + ", schemaLocation = " + schemaLocation);
\r
145 MultivaluedMap<String, String> headers = new MultivaluedMapImpl();
\r
146 headers.put("Accept", Arrays.asList("application/json"));
\r
147 headers.put(Headers.FROM_APP_ID, Arrays.asList("DL"));
\r
148 headers.put(Headers.TRANSACTION_ID, Arrays.asList(UUID.randomUUID().toString()));
\r
150 String url = concatSubUri(searchUrl, index);
\r
153 OperationResult result = searchClient.put(url, loadFileData(schemaLocation), headers,
\r
154 MediaType.APPLICATION_JSON_TYPE, null);
\r
156 if (!HttpUtil.isHttpResponseClassSuccess(result.getResultCode())) {
\r
157 logger.error(DataRouterMsgs.FAIL_TO_CREATE_SEARCH_INDEX, index, result.getFailureCause());
\r
159 logger.info(DataRouterMsgs.SEARCH_INDEX_CREATE_SUCCESS, index);
\r
162 } catch (Exception e) {
\r
163 logger.error(DataRouterMsgs.FAIL_TO_CREATE_SEARCH_INDEX, index, e.getLocalizedMessage());
\r
169 * Retrieves a document from the search service.
\r
171 * @param index - The index to retrieve the document from.
\r
172 * @param id - The unique identifier for the document.
\r
174 * @return - The REST response returned from the Search Service.
\r
176 public OperationResult getDocument(String index, String id) {
\r
178 Map<String, List<String>> headers = new HashMap<>();
\r
179 headers.put(Headers.FROM_APP_ID, Arrays.asList("Data Router"));
\r
180 headers.put(Headers.TRANSACTION_ID, Arrays.asList(MDC.get(MdcContext.MDC_REQUEST_ID)));
\r
182 String url = concatSubUri(searchUrl, index, documentEndpoint, id);
\r
183 return searchClient.get(url, headers, MediaType.APPLICATION_JSON_TYPE);
\r
188 * Creates or updates a document in the Search Service.
\r
190 * @param index - The index to create or update the document in.
\r
191 * @param id - The identifier for the document.
\r
192 * @param payload - The document contents.
\r
193 * @param headers - HTTP headers.
\r
195 public void putDocument(String index, String id, String payload, Map<String, List<String>> headers) {
\r
197 // Try to post the document to the search service.
\r
198 OperationResult result = doDocumentPut(index, id, payload, headers);
\r
200 // A 404 response from the Search Service may indicate that the index we are writing
\r
201 // to does not actually exist. We will try creating it now.
\r
202 if(result.getResultCode() == Status.NOT_FOUND.getStatusCode()) {
\r
204 // Lookup the location of the schema that we want to create.
\r
205 String indexSchemaLocation = indexSchemaMapping.get(index);
\r
206 if(indexSchemaLocation != null) {
\r
208 // Try creating the index now...
\r
209 logger.info(DataRouterMsgs.CREATE_MISSING_INDEX, index);
\r
210 createIndex(index, indexSchemaLocation);
\r
212 // ...and retry the document post.
\r
213 result = doDocumentPut(index, id, payload, headers);
\r
217 if(!resultSuccessful(result)) {
\r
218 logger.error(DataRouterMsgs.FAIL_TO_CREATE_UPDATE_DOC, index, result.getFailureCause());
\r
224 * This method does the actual work of submitting a document PUT request to the Search Service.
\r
226 * @param index - The index to create or update the document in.
\r
227 * @param id - The identifier for the document.
\r
228 * @param payload - The document contents.
\r
229 * @param headers - HTTP headers.
\r
231 * @return - The HTTP response returned by the Search Service.
\r
233 private OperationResult doDocumentPut(String index, String id, String payload, Map<String, List<String>> headers) {
\r
235 String url = concatSubUri(searchUrl, index, documentEndpoint, id);
\r
236 return searchClient.put(url, payload, headers, MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
\r
241 * Creates a document in the Search Service.
\r
243 * @param index - The index to create the document in.
\r
244 * @param payload - The document contents.
\r
245 * @param headers - HTTP headers.
\r
247 public void postDocument(String index, String payload, Map<String, List<String>> headers) {
\r
249 // Try to post the document to the search service.
\r
250 OperationResult result = doDocumentPost(index, payload, headers);
\r
252 // A 404 response from the Search Service may indicate that the index we are writing
\r
253 // to does not actually exist. We will try creating it now.
\r
254 if(result.getResultCode() == Status.NOT_FOUND.getStatusCode()) {
\r
256 // Lookup the location of the schema that we want to create.
\r
257 String indexSchemaLocation = indexSchemaMapping.get(index);
\r
258 if(indexSchemaLocation != null) {
\r
260 // Try creating the index now...
\r
261 logger.info(DataRouterMsgs.CREATE_MISSING_INDEX, index);
\r
262 createIndex(index, indexSchemaLocation);
\r
264 // ...and retry the document post.
\r
265 result = doDocumentPost(index, payload, headers);
\r
269 if(!resultSuccessful(result)) {
\r
270 logger.error(DataRouterMsgs.FAIL_TO_CREATE_UPDATE_DOC, index, result.getFailureCause());
\r
276 * This method does the actual work of submitting a document PUT request to the Search Service.
\r
278 * @param index - The index to create or update the document in.
\r
279 * @param payload - The document contents.
\r
280 * @param headers - HTTP headers.
\r
282 * @return - The HTTP response returned by the Search Service.
\r
284 private OperationResult doDocumentPost(String index, String payload, Map<String, List<String>> headers) {
\r
286 String url = concatSubUri(searchUrl, index, documentEndpoint);
\r
287 return searchClient.post(url, payload, headers, MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
\r
292 * Removes a document from the Search Service.
\r
294 * @param index - The index to create the document in.
\r
295 * @param id - The identifier for the document.
\r
296 * @param payload - The document contents.
\r
297 * @param headers - HTTP headers.
\r
299 public void deleteDocument(String index, String documentId, Map<String, List<String>> headers) {
\r
301 String url = concatSubUri(searchUrl, index, documentEndpoint, documentId);
\r
302 searchClient.delete(url, headers, null);
\r
307 * Convenience method to load up all the data from a file into a string
\r
309 * @param filename the filename to read from disk
\r
310 * @return the data contained within the file
\r
311 * @throws Exception
\r
313 protected String loadFileData(String filename) throws Exception {
\r
314 StringBuilder data = new StringBuilder();
\r
316 BufferedReader in = new BufferedReader(new InputStreamReader(
\r
317 EntityEventPolicy.class.getClassLoader().getResourceAsStream("/" + filename),
\r
318 StandardCharsets.UTF_8));
\r
321 while ((line = in.readLine()) != null) {
\r
324 } catch (Exception e) {
\r
325 throw new Exception("Failed to read from file = " + filename + ".", e);
\r
328 return data.toString();
\r
333 * Helper utility to concatenate substrings of a URI together to form a proper URI.
\r
335 * @param suburis the list of substrings to concatenate together
\r
336 * @return the concatenated list of substrings
\r
338 public static String concatSubUri(String... suburis) {
\r
339 String finalUri = "";
\r
341 for (String suburi : suburis) {
\r
343 if (suburi != null) {
\r
344 // Remove any leading / since we only want to append /
\r
345 suburi = suburi.replaceFirst("^/*", "");
\r
347 // Add a trailing / if one isn't already there
\r
348 finalUri += suburi.endsWith("/") ? suburi : suburi + "/";
\r
357 * Helper utility to check the response code of an HTTP response.
\r
359 * @param aResult - The response that we want to check.
\r
361 * @return - true if the response contains a success code,
\r
364 private boolean resultSuccessful(OperationResult aResult) {
\r
366 return (aResult.getResultCode() >= 200) && (aResult.getResultCode() < 300);
\r