Performance Improvements for Gizmo bulk API
[aai/gizmo.git] / src / main / java / org / onap / crud / dao / champ / ChampDao.java
index 7bd4754..402d2cf 100644 (file)
@@ -27,16 +27,14 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import org.apache.http.NameValuePair;
 import org.apache.http.client.utils.URLEncodedUtils;
 import org.apache.http.message.BasicNameValuePair;
 import org.eclipse.jetty.util.security.Password;
-import org.onap.aai.cl.api.Logger;
-import org.onap.aai.cl.eelf.LoggerFactory;
 import org.onap.aai.cl.mdc.MdcContext;
-import org.onap.aai.logging.LoggingContext;
 import org.onap.aai.restclient.client.OperationResult;
 import org.onap.aai.restclient.client.RestClient;
 import org.onap.aai.restclient.enums.RestAuthenticationMode;
@@ -56,6 +54,7 @@ public class ChampDao implements GraphDao {
   protected String baseObjectUrl;
   protected String baseRelationshipUrl;
   protected String baseTransactionUrl;
+  protected String baseBulkUrl;
 
   protected static final String HEADER_FROM_APP = "X-FromAppId";
   protected static final String HEADER_TRANS_ID = "X-TransactionId";
@@ -63,28 +62,29 @@ public class ChampDao implements GraphDao {
   protected static final String OBJECT_SUB_URL = "objects";
   protected static final String RELATIONSHIP_SUB_URL = "relationships";
   protected static final String TRANSACTION_SUB_URL = "transaction";
-
-  private Logger logger = LoggerFactory.getInstance().getLogger(ChampDao.class.getName());
+  protected static final String BULK_SUB_URL = "bulk";
 
   // We use a custom vertex serializer for champ because it expects "key"
   // instead of "id"
   protected static final Gson champGson = new GsonBuilder()
-      .registerTypeAdapterFactory(new GsonJava8TypeAdapterFactory())
-      .registerTypeAdapter(Vertex.class, new ChampVertexSerializer())
-      .registerTypeAdapter(Edge.class, new ChampEdgeSerializer()).create();
+          .registerTypeAdapterFactory(new GsonJava8TypeAdapterFactory())
+          .registerTypeAdapter(Vertex.class, new ChampVertexSerializer())
+          .registerTypeAdapter(Edge.class, new ChampEdgeSerializer()).create();
 
   public ChampDao() {
   }
 
   public ChampDao(String champUrl, String certPassword) {
     try {
+      String deobfuscatedCertPassword = certPassword.startsWith("OBF:")?Password.deobfuscate(certPassword):certPassword;
       client = new RestClient().authenticationMode(RestAuthenticationMode.SSL_CERT).validateServerHostname(false)
-          .validateServerCertChain(false).clientCertFile(CrudServiceConstants.CRD_CHAMP_AUTH_FILE)
-          .clientCertPassword(Password.deobfuscate(certPassword));
+              .validateServerCertChain(false).clientCertFile(CrudServiceConstants.CRD_CHAMP_AUTH_FILE)
+              .clientCertPassword(deobfuscatedCertPassword);
 
       baseObjectUrl = champUrl + OBJECT_SUB_URL;
       baseRelationshipUrl = champUrl + RELATIONSHIP_SUB_URL;
       baseTransactionUrl = champUrl + TRANSACTION_SUB_URL;
+      baseBulkUrl = champUrl + BULK_SUB_URL;
     } catch (Exception e) {
       System.out.println("Error setting up Champ configuration");
       e.printStackTrace();
@@ -93,10 +93,10 @@ public class ChampDao implements GraphDao {
   }
 
   public ChampDao(RestClient client, String baseObjectUrl, String baseRelationshipUrl, String baseTransactionUrl) {
-      this.client = client;
-      this.baseObjectUrl = baseObjectUrl;
-      this.baseRelationshipUrl = baseRelationshipUrl;
-      this.baseTransactionUrl = baseTransactionUrl;
+    this.client = client;
+    this.baseObjectUrl = baseObjectUrl;
+    this.baseRelationshipUrl = baseRelationshipUrl;
+    this.baseTransactionUrl = baseTransactionUrl;
   }
 
   @Override
@@ -120,8 +120,8 @@ public class ChampDao implements GraphDao {
     strBuild.append(id);
     if(queryParams != null && !queryParams.isEmpty())
     {
-        strBuild.append("?");
-        strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParams), Charset.defaultCharset()));
+      strBuild.append("?");
+      strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParams), Charset.defaultCharset()));
     }
 
     OperationResult getResult = client.get(strBuild.toString(), createHeader(), MediaType.APPLICATION_JSON_TYPE);
@@ -133,25 +133,38 @@ public class ChampDao implements GraphDao {
         // We didn't find a vertex with the supplied type, so just throw an
         // exception.
         throw new CrudException("No vertex with id " + id + " and type " + type + " found in graph",
-            javax.ws.rs.core.Response.Status.NOT_FOUND);
+                javax.ws.rs.core.Response.Status.NOT_FOUND);
       }
       return getResult;
     } else {
       // We didn't find a vertex with the supplied id, so just throw an
       // exception.
-        throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertex with id " + id + " found in graph");
+      throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertex with id " + id + " found in graph");
     }
   }
 
   @Override
-  public List<Edge> getVertexEdges(String id, Map<String, String> queryParams) throws CrudException {
+  public List<Edge> getVertexEdges(String id, Map<String, String> queryParams, String txId) throws CrudException {
     StringBuilder strBuild = new StringBuilder(baseObjectUrl);
     strBuild.append("/relationships/");
     strBuild.append(id);
-    if(queryParams != null && !queryParams.isEmpty())
+
+    Map<String,String> queryParamsCopy = null;
+    if (queryParams != null) {
+      queryParamsCopy = new HashMap<String,String>(queryParams);
+    }
+    else {
+      queryParamsCopy = new HashMap<String,String>();
+    }
+
+    if (txId != null) {
+      queryParamsCopy.put("transactionId", txId);
+    }
+
+    if (!queryParamsCopy.isEmpty())
     {
-        strBuild.append("?");
-        strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParams), Charset.defaultCharset()));
+      strBuild.append("?");
+      strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParamsCopy), Charset.defaultCharset()));
     }
 
     OperationResult getResult = client.get(strBuild.toString(), createHeader(), MediaType.APPLICATION_JSON_TYPE);
@@ -172,13 +185,13 @@ public class ChampDao implements GraphDao {
   }
 
   @Override
-  public OperationResult getVertices(String type, Map<String, Object> filter, HashSet<String> properties, String version) throws CrudException {
-    filter.put(org.onap.schema.validation.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
+  public OperationResult getVertices(String type, Map<String, Object> filter, Set<String> properties, String version) throws CrudException {
+    filter.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
 
     List<NameValuePair> queryParams = convertToNameValuePair(filter);
     queryParams.addAll(convertToNameValuePair("properties", properties));
     String url = baseObjectUrl + "/filter" + "?"
-        + URLEncodedUtils.format(queryParams, Charset.defaultCharset());
+            + URLEncodedUtils.format(queryParams, Charset.defaultCharset());
 
     OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
 
@@ -198,8 +211,8 @@ public class ChampDao implements GraphDao {
     strBuild.append(id);
     if(queryParams != null && !queryParams.isEmpty())
     {
-        strBuild.append("?");
-        strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParams), Charset.defaultCharset()));
+      strBuild.append("?");
+      strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParams), Charset.defaultCharset()));
     }
     OperationResult getResult = client.get(strBuild.toString(), createHeader(), MediaType.APPLICATION_JSON_TYPE);
 
@@ -209,8 +222,8 @@ public class ChampDao implements GraphDao {
       if (!edge.getType().equalsIgnoreCase(type)) {
         // We didn't find an edge with the supplied type, so just throw an
         // exception.
-        throw new CrudException("No edge with id " + id + "and type " + type + " found in graph",
-            javax.ws.rs.core.Response.Status.NOT_FOUND);
+        throw new CrudException("No edge with id " + id + " and type " + type + " found in graph",
+                javax.ws.rs.core.Response.Status.NOT_FOUND);
       }
       return getResult;
     } else {
@@ -223,12 +236,12 @@ public class ChampDao implements GraphDao {
   @Override
   public OperationResult getEdges(String type, Map<String, Object> filter) throws CrudException {
     String url = baseRelationshipUrl + "/filter" + "?"
-        + URLEncodedUtils.format(convertToNameValuePair(filter), Charset.defaultCharset());
+            + URLEncodedUtils.format(convertToNameValuePair(filter), Charset.defaultCharset());
 
     OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
 
     if (getResult.getResultCode() == 200) {
-        return getResult;
+      return getResult;
     } else {
       // We didn't find a vertex with the supplied id, so just throw an
       // exception.
@@ -242,14 +255,14 @@ public class ChampDao implements GraphDao {
 
     // Add the aai_node_type so that AAI can read the data created by gizmo
     // TODO: This probably shouldn't be here
-    properties.put(org.onap.schema.validation.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
+    properties.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
 
     Vertex.Builder insertVertexBuilder = new Vertex.Builder(type);
     properties.forEach(insertVertexBuilder::property);
     Vertex insertVertex = insertVertexBuilder.build();
 
     OperationResult getResult = client.post(url, insertVertex.toJson(), createHeader(), MediaType.APPLICATION_JSON_TYPE,
-        MediaType.APPLICATION_JSON_TYPE);
+            MediaType.APPLICATION_JSON_TYPE);
 
     if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
       return getResult;
@@ -266,7 +279,7 @@ public class ChampDao implements GraphDao {
 
     // Add the aai_node_type so that AAI can read the data created by gizmo
     // TODO: This probably shouldn't be here
-    properties.put(org.onap.schema.validation.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
+    properties.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
 
     Vertex.Builder insertVertexBuilder = new Vertex.Builder(type);
     insertVertexBuilder.id(id);
@@ -275,7 +288,7 @@ public class ChampDao implements GraphDao {
 
     String payload = insertVertex.toJson(champGson);
     OperationResult getResult = client.put(url, payload, createHeader(), MediaType.APPLICATION_JSON_TYPE,
-        MediaType.APPLICATION_JSON_TYPE);
+            MediaType.APPLICATION_JSON_TYPE);
 
     if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
       return getResult;
@@ -314,7 +327,7 @@ public class ChampDao implements GraphDao {
 
     String edgeJson = insertEdge.toJson(champGson);
     OperationResult getResult = client.post(url, edgeJson, createHeader(), MediaType.APPLICATION_JSON_TYPE,
-        MediaType.APPLICATION_JSON_TYPE);
+            MediaType.APPLICATION_JSON_TYPE);
 
     if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
       return getResult;
@@ -334,7 +347,7 @@ public class ChampDao implements GraphDao {
 
     String edgeJson = edge.toJson(champGson);
     OperationResult getResult = client.put(url, edgeJson, createHeader(), MediaType.APPLICATION_JSON_TYPE,
-        MediaType.APPLICATION_JSON_TYPE);
+            MediaType.APPLICATION_JSON_TYPE);
 
     if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
       return getResult;
@@ -346,7 +359,7 @@ public class ChampDao implements GraphDao {
   }
 
   @Override
-  public void deleteEdge(String id, String type) throws CrudException {
+  public void deleteEdge(String id) throws CrudException {
     String url = baseRelationshipUrl + "/" + id;
     OperationResult getResult = client.delete(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
 
@@ -375,11 +388,11 @@ public class ChampDao implements GraphDao {
     String url = baseTransactionUrl + "/" + id;
 
     OperationResult getResult = client.put(url, "{\"method\": \"commit\"}", createHeader(), MediaType.APPLICATION_JSON_TYPE,
-        MediaType.TEXT_PLAIN_TYPE);
+            MediaType.TEXT_PLAIN_TYPE);
 
     if (getResult.getResultCode() != 200) {
       throw new CrudException("Unable to commit transaction",
-          Response.Status.fromStatusCode(getResult.getResultCode()));
+              Response.Status.fromStatusCode(getResult.getResultCode()));
     }
   }
 
@@ -388,11 +401,11 @@ public class ChampDao implements GraphDao {
     String url = baseTransactionUrl + "/" + id;
 
     OperationResult getResult = client.put(url, "{\"method\": \"rollback\"}", createHeader(), MediaType.APPLICATION_JSON_TYPE,
-        MediaType.TEXT_PLAIN_TYPE);
+            MediaType.TEXT_PLAIN_TYPE);
 
     if (getResult.getResultCode() != 200) {
       throw new CrudException("Unable to rollback transaction",
-          Response.Status.fromStatusCode(getResult.getResultCode()));
+              Response.Status.fromStatusCode(getResult.getResultCode()));
     }
   }
 
@@ -401,7 +414,7 @@ public class ChampDao implements GraphDao {
     String url = baseTransactionUrl + "/" + id;
     Map<String, List<String>> headers = new HashMap<>();
     headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo"));
-    headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
+    headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(MdcContext.MDC_REQUEST_ID)));
 
     OperationResult getResult = client.get(url, headers, MediaType.APPLICATION_JSON_TYPE);
 
@@ -414,14 +427,14 @@ public class ChampDao implements GraphDao {
 
     // Add the aai_node_type so that AAI can read the data created by gizmo
     // TODO: This probably shouldn't be here
-    properties.put(org.onap.schema.validation.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
+    properties.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
 
     Vertex.Builder insertVertexBuilder = new Vertex.Builder(type);
     properties.forEach(insertVertexBuilder::property);
     Vertex insertVertex = insertVertexBuilder.build();
 
     OperationResult getResult = client.post(url, insertVertex.toJson(), createHeader(), MediaType.APPLICATION_JSON_TYPE,
-        MediaType.APPLICATION_JSON_TYPE);
+            MediaType.APPLICATION_JSON_TYPE);
 
     if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
       return Vertex.fromJson(getResult.getResult(), version);
@@ -434,7 +447,7 @@ public class ChampDao implements GraphDao {
 
   @Override
   public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties, String version, String txId)
-      throws CrudException {
+          throws CrudException {
     String url = baseRelationshipUrl + "?transactionId=" + txId;
 
     // Try requests to ensure source and target exist in Champ
@@ -446,7 +459,7 @@ public class ChampDao implements GraphDao {
     Edge insertEdge = insertEdgeBuilder.build();
 
     OperationResult getResult = client.post(url, insertEdge.toJson(champGson), createHeader(),
-        MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
+            MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
 
     if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
       return Edge.fromJson(getResult.getResult());
@@ -463,7 +476,7 @@ public class ChampDao implements GraphDao {
 
     // Add the aai_node_type so that AAI can read the data created by gizmo
     // TODO: This probably shouldn't be here
-    properties.put(org.onap.schema.validation.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
+    properties.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
 
     Vertex.Builder insertVertexBuilder = new Vertex.Builder(type);
     insertVertexBuilder.id(id);
@@ -472,7 +485,7 @@ public class ChampDao implements GraphDao {
 
     String payload = insertVertex.toJson(champGson);
     OperationResult getResult = client.put(url, payload, createHeader(), MediaType.APPLICATION_JSON_TYPE,
-        MediaType.APPLICATION_JSON_TYPE);
+            MediaType.APPLICATION_JSON_TYPE);
 
     if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
       return Vertex.fromJson(getResult.getResult(), version);
@@ -502,7 +515,7 @@ public class ChampDao implements GraphDao {
     }
     String url = baseRelationshipUrl + "/" + edge.getId().get() + "?transactionId=" + txId;
     OperationResult getResult = client.put(url, edge.toJson(champGson), createHeader(), MediaType.APPLICATION_JSON_TYPE,
-        MediaType.APPLICATION_JSON_TYPE);
+            MediaType.APPLICATION_JSON_TYPE);
 
     if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
       return Edge.fromJson(getResult.getResult());
@@ -510,12 +523,12 @@ public class ChampDao implements GraphDao {
       // We didn't create an edge with the supplied type, so just throw an
       // exception.
       throw new CrudException("Failed to update edge: " + getResult.getFailureCause(),
-          Response.Status.fromStatusCode(getResult.getResultCode()));
+              Response.Status.fromStatusCode(getResult.getResultCode()));
     }
   }
 
   @Override
-  public void deleteEdge(String id, String type, String txId) throws CrudException {
+  public void deleteEdge(String id, String txId) throws CrudException {
     String url = baseRelationshipUrl + "/" + id + "?transactionId=" + txId;
     OperationResult getResult = client.delete(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
 
@@ -527,19 +540,12 @@ public class ChampDao implements GraphDao {
   }
 
   @Override
-  public Edge getEdge(String id, String type, String txId) throws CrudException {
+  public Edge getEdge(String id, String txId) throws CrudException {
     String url = baseRelationshipUrl + "/" + id + "?transactionId=" + txId;
     OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
 
     if (getResult.getResultCode() == 200) {
       Edge edge = Edge.fromJson(getResult.getResult());
-
-      if (!edge.getType().equalsIgnoreCase(type)) {
-        // We didn't find an edge with the supplied type, so just throw an
-        // exception.
-        throw new CrudException("No edge with id " + id + "and type " + type + " found in graph",
-            javax.ws.rs.core.Response.Status.NOT_FOUND);
-      }
       return edge;
     } else {
       // We didn't find an edge with the supplied id, so just throw an
@@ -548,6 +554,20 @@ public class ChampDao implements GraphDao {
     }
   }
 
+  @Override
+  public Edge getEdge(String id) throws CrudException {
+    String url = baseRelationshipUrl + "/" + id;
+    OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
+
+    if (getResult.getResultCode() == 200) {
+      Edge edge = Edge.fromJson(getResult.getResult());
+      return edge;
+    } else {
+      // We didn't find an edge with the supplied id, so just throw an exception.
+      throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph");
+    }
+  }
+
   public Vertex getVertex(String id, String type, String version, String txId) throws CrudException {
     String url = baseObjectUrl + "/" + id + "?transactionId=" + txId;
     OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
@@ -558,8 +578,8 @@ public class ChampDao implements GraphDao {
       if (!vert.getType().equalsIgnoreCase(type)) {
         // We didn't find a vertex with the supplied type, so just throw an
         // exception.
-        throw new CrudException("No vertex with id " + id + "and type " + type + " found in graph",
-            javax.ws.rs.core.Response.Status.NOT_FOUND);
+        throw new CrudException("No vertex with id " + id + " and type " + type + " found in graph",
+                javax.ws.rs.core.Response.Status.NOT_FOUND);
       }
       return vert;
     } else {
@@ -579,7 +599,7 @@ public class ChampDao implements GraphDao {
   }
 
   // https://stackoverflow.com/questions/26942330/convert-mapstring-string-to-listnamevaluepair-is-this-the-most-efficient
-  private List<NameValuePair> convertToNameValuePair(String k, HashSet<String> values) {
+  private List<NameValuePair> convertToNameValuePair(String k, Set<String> values) {
     List<NameValuePair> nvpList = new ArrayList<>(values.size());
 
     values.forEach((v) -> nvpList.add(new BasicNameValuePair(k, v)));
@@ -596,12 +616,26 @@ public class ChampDao implements GraphDao {
 
   private CrudException createErrorException(OperationResult result, javax.ws.rs.core.Response.Status defaultErrorCode , String defaultErrorMsg)
   {
-      CrudException ce = null;
-      if(result != null)
-          ce = new CrudException(result.getFailureCause(), Response.Status.fromStatusCode(result.getResultCode()));
-      else
-          ce = new CrudException(defaultErrorMsg, defaultErrorCode);
-      return ce;
+    CrudException ce = null;
+    if(result != null)
+      ce = new CrudException(result.getFailureCause(), Response.Status.fromStatusCode(result.getResultCode()));
+    else
+      ce = new CrudException(defaultErrorMsg, defaultErrorCode);
+    return ce;
+  }
+
+  @Override
+  public OperationResult bulkOperation(ChampBulkPayload champPayload) throws CrudException {
+    String url = baseBulkUrl;
+
+    OperationResult getResult = client.post(url, champPayload.toJson(), createHeader(), MediaType.APPLICATION_JSON_TYPE,
+            MediaType.APPLICATION_JSON_TYPE);
+
+    if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
+      return getResult;
+    } else {
+      throw new CrudException("Bulk request failed: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
+    }
   }
 
 }