Remove backport dependency
[aai/search-data-service.git] / src / main / java / org / onap / aai / sa / searchdbabstraction / elasticsearch / dao / ElasticSearchHttpController.java
index ef141ec..019d867 100644 (file)
  */
 package org.onap.aai.sa.searchdbabstraction.elasticsearch.dao;
 
-import com.att.aft.dme2.internal.google.common.base.Throwables;
+import com.google.common.base.Throwables;
 import com.fasterxml.jackson.annotation.JsonInclude.Include;
 import com.fasterxml.jackson.core.JsonParseException;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.JsonMappingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
-import edu.emory.mathcs.backport.java.util.Arrays;
 import org.json.simple.JSONArray;
 import org.json.simple.JSONObject;
 import org.json.simple.parser.JSONParser;
@@ -57,6 +56,8 @@ import org.onap.aai.cl.eelf.LoggerFactory;
 import org.onap.aai.cl.mdc.MdcContext;
 import org.onap.aai.cl.mdc.MdcOverride;
 import org.onap.aai.sa.rest.DocumentSchema;
+import org.onap.aai.sa.searchdbabstraction.entity.SuggestHit;
+import org.onap.aai.sa.searchdbabstraction.entity.SuggestHits;
 
 import java.io.BufferedReader;
 import java.io.File;
@@ -74,6 +75,7 @@ import java.net.ProtocolException;
 import java.net.URL;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 import java.util.Properties;
 import java.util.concurrent.atomic.AtomicBoolean;
@@ -187,10 +189,10 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
   public OperationResult createDynamicIndex(String index, String dynamicSchema) {
     OperationResult result = new OperationResult();
     result.setResultCode(500);
-    
+
     try {
       result = createTable(index, dynamicSchema);
-      
+
       // ElasticSearch will return us a 200 code on success when we
       // want to report a 201, so translate the result here.
       result.setResultCode((result.getResultCode() == 200) ? 201 : result.getResultCode());
@@ -200,10 +202,11 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
     } catch (DocumentStoreOperationException e) {
       result.setFailureCause("Document store operation failure.  Cause: " + e.getMessage());
     }
-    
+
     return result;
   }
 
+
   @Override
   public OperationResult deleteIndex(String indexName) throws DocumentStoreOperationException {
 
@@ -380,10 +383,10 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
     return opResult;
   }
 
-    /**
-   * Will send the passed in JSON payload to Elasticsearch using the 
+  /**
+   * Will send the passed in JSON payload to Elasticsearch using the
    * provided index name in an attempt to create the index.
-   * 
+   *
    * @param indexName - The name of the index to be created
    * @param settingsAndMappings - The actual JSON object that will define the index
    * @return - The operation result of writing into Elasticsearch
@@ -393,10 +396,10 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
     OperationResult result = new OperationResult();
     result.setResultCode(500);
     result.setResult(INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT);
-    
+
     // Grab the current time so we can use it to generate a metrics log.
     MdcOverride override = getStartTime(new MdcOverride());
-    
+
     String fullUrl = getFullUrl("/" + indexName + "/", false);
     HttpURLConnection conn = initializeConnection(fullUrl);
 
@@ -406,36 +409,36 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
       shutdownConnection(conn);
       throw new DocumentStoreOperationException("Failed to set HTTP request method to PUT.", e);
     }
-    
+
     attachContent(conn, settingsAndMappings);
     handleResponse(conn, result);
-    
+
     // Generate a metrics log so we can track how long the operation took.
     metricsLogger.info(SearchDbMsgs.CREATE_INDEX_TIME,
-        new LogFields()
-            .setField(LogLine.DefinedFields.RESPONSE_CODE, result.getResultCode())
-            .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, result.getResultCode()),
-        override,
-        indexName);
-    
+            new LogFields()
+                    .setField(LogLine.DefinedFields.RESPONSE_CODE, result.getResultCode())
+                    .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, result.getResultCode()),
+            override,
+            indexName);
+
     return result;
   }
 
   @Override
-  public DocumentOperationResult createDocument(String                  indexName, 
+  public DocumentOperationResult createDocument(String                  indexName,
                                                 DocumentStoreDataEntity document,
                                                 boolean                 allowImplicitIndexCreation)
       throws DocumentStoreOperationException {
-    
+
     if(!allowImplicitIndexCreation) {
-      
+
       // Before we do anything, make sure that the specified index actually exists in the
       // document store - we don't want to rely on ElasticSearch to fail the document
       // create because it could be configured to implicitly create a non-existent index,
       // which can lead to hard-to-debug behaviour with queries down the road.
       OperationResult indexExistsResult = checkIndexExistence(indexName);
       if ((indexExistsResult.getResultCode() < 200) || (indexExistsResult.getResultCode() >= 300)) {
-  
+
         DocumentOperationResult opResult = new DocumentOperationResult();
         opResult.setResultCode(Status.NOT_FOUND.getStatusCode());
         opResult.setResult("Document Index '" + indexName + "' does not exist.");
@@ -443,7 +446,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
         return opResult;
       }
     }
-    
+
     if (document.getId() == null || document.getId().isEmpty()) {
       return createDocumentWithoutId(indexName, document);
     } else {
@@ -608,20 +611,20 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
   }
 
   @Override
-  public DocumentOperationResult updateDocument(String                  indexName, 
+  public DocumentOperationResult updateDocument(String                  indexName,
                                                 DocumentStoreDataEntity document,
                                                 boolean                 allowImplicitIndexCreation)
       throws DocumentStoreOperationException {
-    
+
     if(!allowImplicitIndexCreation) {
-      
+
       // Before we do anything, make sure that the specified index actually exists in the
       // document store - we don't want to rely on ElasticSearch to fail the document
       // create because it could be configured to implicitly create a non-existent index,
       // which can lead to hard-to-debug behaviour with queries down the road.
       OperationResult indexExistsResult = checkIndexExistence(indexName);
       if ((indexExistsResult.getResultCode() < 200) || (indexExistsResult.getResultCode() >= 300)) {
-  
+
         DocumentOperationResult opResult = new DocumentOperationResult();
         opResult.setResultCode(Status.NOT_FOUND.getStatusCode());
         opResult.setResult("Document Index '" + indexName + "' does not exist.");
@@ -629,7 +632,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
         return opResult;
       }
     }
-    
+
     DocumentOperationResult opResult = new DocumentOperationResult();
 
     // Initialize operation result with a failure codes / fault string
@@ -844,6 +847,52 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
     return opResult;
   }
 
+
+  public SearchOperationResult suggestionQueryWithPayload(String indexName, String query)
+          throws DocumentStoreOperationException {
+
+    SearchOperationResult opResult = new SearchOperationResult();
+
+    if (logger.isDebugEnabled()) {
+      logger.debug("Querying Suggestion index: " + indexName + " with query string: " + query);
+    }
+
+    // Initialize operation result with a failure codes / fault string
+    opResult.setResultCode(500);
+    opResult.setResult(INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT);
+
+    String fullUrl = getFullUrl("/" + indexName + "/_suggest", false);
+
+    // Grab the current time so we can use it to generate a metrics log.
+    MdcOverride override = getStartTime(new MdcOverride());
+
+    HttpURLConnection conn = initializeConnection(fullUrl);
+
+    try {
+      conn.setRequestMethod("POST");
+    } catch (ProtocolException e) {
+      shutdownConnection(conn);
+      throw new DocumentStoreOperationException("Failed to set HTTP request method to POST.", e);
+    }
+
+    attachContent(conn, query);
+
+    logger.debug("\nsearch(), Sending 'POST' request to URL : " + conn.getURL());
+    logger.debug("Request body =  Elasticsearch query = " + query);
+
+    handleResponse(conn, opResult);
+    buildSuggestResult(opResult, indexName);
+
+    metricsLogger.info(SearchDbMsgs.QUERY_DOCUMENT_TIME,
+            new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode())
+                    .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResult()),
+            override, indexName, query);
+
+    shutdownConnection(conn);
+
+    return opResult;
+  }
+
   private void attachContent(HttpURLConnection conn, String content)
       throws DocumentStoreOperationException {
     OutputStream outputStream = null;
@@ -1690,4 +1739,64 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
 
   }
 
-}
+  private void buildSuggestResult(SearchOperationResult result, String index)
+          throws DocumentStoreOperationException {
+
+    JSONParser parser = new JSONParser ();
+    JSONObject root;
+    try {
+      root = (JSONObject) parser.parse ( result.getResult () );
+      if (result.getResultCode () >= 200 && result.getResultCode () <= 299) {
+        JSONArray hitArray = (JSONArray) root.get ( "suggest-vnf" );
+        JSONObject hitdata = (JSONObject) hitArray.get ( 0 );
+        JSONArray optionsArray = (JSONArray) hitdata.get ( "options" );
+        SuggestHits suggestHits = new SuggestHits ();
+        suggestHits.setTotalHits ( String.valueOf ( optionsArray.size () ) );
+
+        ArrayList<SuggestHit> suggestHitArray = new ArrayList<SuggestHit> ();
+
+        for (int i = 0; i < optionsArray.size (); i++) {
+          JSONObject hit = (JSONObject) optionsArray.get ( i );
+
+          SuggestHit suggestHit = new SuggestHit ();
+          suggestHit.setScore ( (hit.get ( "score" ) != null) ? hit.get ( "score" ).toString () : "" );
+          suggestHit.setText ( (hit.get ( "text" ) != null) ? hit.get ( "text" ).toString () : "" );
+          Document doc = new Document ();
+          if (hit.get ( "_version" ) != null) {
+            doc.setEtag ( (hit.get ( "_version" ) != null) ? hit.get ( "_version" ).toString () : "" );
+          }
+          doc.setUrl ( buildDocumentResponseUrl ( index,
+                  (hit.get ( "_id" ) != null) ? hit.get ( "_id" ).toString () : "" ) );
+
+          doc.setContent ( (JSONObject) hit.get ( "payload" ) );
+          suggestHit.setDocument ( doc );
+          suggestHitArray.add ( suggestHit );
+        }
+        suggestHits.setHits ( suggestHitArray.toArray ( new SuggestHit[suggestHitArray.size ()] ) );
+        result.setSuggestResult ( suggestHits );
+
+        JSONObject aggregations = (JSONObject) root.get ( "aggregations" );
+        if (aggregations != null) {
+          AggregationResult[] aggResults =
+                  AggregationParsingUtil.parseAggregationResults ( aggregations );
+          AggregationResults aggs = new AggregationResults ();
+          aggs.setAggregations ( aggResults );
+          result.setAggregationResult ( aggs );
+        }
+
+        // success
+      } else {
+        JSONObject error = (JSONObject) root.get ( "error" );
+        if (error != null) {
+          result.setError (
+                  new ErrorResult ( error.get ( "type" ).toString (), error.get ( "reason" ).toString () ) );
+        }
+      }
+    } catch (Exception e) {
+      throw new DocumentStoreOperationException (
+              "Failed to parse Elastic Search response." + result.getResult () );
+    }
+  }
+
+
+  }