align router code with 1.2.1 version of rest
[aai/data-router.git] / src / main / java / org / onap / aai / datarouter / util / SearchServiceAgent.java
index 20d0981..4baa81a 100644 (file)
@@ -2,8 +2,8 @@
  * ============LICENSE_START=======================================================\r
  * org.onap.aai\r
  * ================================================================================\r
- * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
- * Copyright © 2017 Amdocs\r
+ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.\r
+ * Copyright © 2017-2018 Amdocs\r
  * ================================================================================\r
  * Licensed under the Apache License, Version 2.0 (the "License");\r
  * you may not use this file except in compliance with the License.\r
@@ -17,8 +17,6 @@
  * See the License for the specific language governing permissions and\r
  * limitations under the License.\r
  * ============LICENSE_END=========================================================\r
- *\r
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
  */\r
 package org.onap.aai.datarouter.util;\r
 \r
@@ -98,14 +96,14 @@ public class SearchServiceAgent {
                           String documentEndpoint, \r
                           Logger logger) {\r
     \r
+    String deobfuscatedCertPassword = keystorePwd.startsWith("OBF:")?Password.deobfuscate(keystorePwd):keystorePwd;\r
     // Create REST client for search service\r
     searchClient = new RestClient()\r
                     .authenticationMode(RestAuthenticationMode.SSL_CERT)\r
                     .validateServerHostname(false)\r
-                    .validateServerCertChain(true)\r
+                    .validateServerCertChain(false)\r
                     .clientCertFile(DataRouterConstants.DR_HOME_AUTH + certName)\r
-                    .clientCertPassword(Password.deobfuscate(keystorePwd))\r
-                    .trustStore(DataRouterConstants.DR_HOME_AUTH + keystore);\r
+                    .clientCertPassword(deobfuscatedCertPassword);                    \r
     \r
     this.searchUrl        = searchUrl;\r
     this.documentEndpoint = documentEndpoint;\r
@@ -129,6 +127,14 @@ public class SearchServiceAgent {
     createIndex(index, schemaLocation);\r
   }\r
   \r
+  public void createSearchIndex(String index, String schemaLocation, String endUrl) {\r
+    \r
+    // Create a mapping of the index name to schema location \r
+    indexSchemaMapping.put(index, schemaLocation);\r
+    \r
+    // Now, create the index.\r
+    createIndex(index, schemaLocation, endUrl);\r
+  }\r
   \r
   /**\r
    * This method performs the actual work of creating a search index.\r
@@ -162,6 +168,30 @@ public class SearchServiceAgent {
     }\r
   }\r
   \r
+  private void createIndex(String index, String schemaLocation, String endUrl) {\r
+    \r
+    logger.debug("Creating search index, index name: = " + index + ", schemaLocation = " + schemaLocation);\r
+    \r
+    MultivaluedMap<String, String> headers = new MultivaluedMapImpl();\r
+    headers.put("Accept", Arrays.asList("application/json"));\r
+    headers.put(Headers.FROM_APP_ID, Arrays.asList("DL"));\r
+    headers.put(Headers.TRANSACTION_ID, Arrays.asList(UUID.randomUUID().toString()));\r
+      \r
+    String url = concatSubUri(searchUrl, endUrl, index);\r
+    try {\r
+\r
+      OperationResult result = searchClient.put(url, loadFileData(schemaLocation), headers,\r
+                                                MediaType.APPLICATION_JSON_TYPE, null);\r
+      if (!HttpUtil.isHttpResponseClassSuccess(result.getResultCode())) {\r
+        logger.error(DataRouterMsgs.FAIL_TO_CREATE_SEARCH_INDEX, index, result.getFailureCause());\r
+      } else {\r
+        logger.info(DataRouterMsgs.SEARCH_INDEX_CREATE_SUCCESS, index);\r
+      }\r
+\r
+    } catch (Exception e) {\r
+      logger.error(DataRouterMsgs.FAIL_TO_CREATE_SEARCH_INDEX, index, e.getLocalizedMessage());\r
+    }\r
+  }\r
   \r
   /**\r
    * Retrieves a document from the search service.\r
@@ -290,8 +320,7 @@ public class SearchServiceAgent {
    * Removes a document from the Search Service.\r
    * \r
    * @param index   - The index to create the document in.\r
-   * @param id      - The identifier for the document.\r
-   * @param payload - The document contents.\r
+   * @param documentId      - The identifier for the document.\r
    * @param headers - HTTP headers.\r
    */\r
   public void deleteDocument(String index, String documentId, Map<String, List<String>> headers) {\r
@@ -310,12 +339,13 @@ public class SearchServiceAgent {
    */\r
   protected String loadFileData(String filename) throws Exception {\r
     StringBuilder data = new StringBuilder();\r
-    try {\r
-      BufferedReader in = new BufferedReader(new InputStreamReader(\r
-          EntityEventPolicy.class.getClassLoader().getResourceAsStream("/" + filename),\r
-          StandardCharsets.UTF_8));\r
-      String line;\r
 \r
+    try (InputStreamReader inputStreamReader = new InputStreamReader(EntityEventPolicy.class.getClassLoader()\r
+        .getResourceAsStream("/" + filename), StandardCharsets.UTF_8); BufferedReader in = new BufferedReader(\r
+        inputStreamReader)\r
+    ) {\r
+\r
+      String line;\r
       while ((line = in.readLine()) != null) {\r
         data.append(line);\r
       }\r