Add event client dependency
[aai/gizmo.git] / src / main / java / org / onap / crud / service / CrudAsyncGraphDataService.java
index 840576e..d674d0e 100644 (file)
@@ -23,9 +23,8 @@
  */
 package org.onap.crud.service;
 
-import com.att.ecomp.event.api.EventConsumer;
-import com.att.ecomp.event.api.EventPublisher;
-import com.google.gson.JsonElement;
+import org.onap.aai.event.api.EventConsumer;
+import org.onap.aai.event.api.EventPublisher;
 
 import org.onap.aai.cl.api.LogFields;
 import org.onap.aai.cl.api.Logger;
@@ -49,10 +48,7 @@ import org.onap.schema.OxmModelValidator;
 import org.onap.schema.RelationshipSchemaValidator;
 
 import java.text.SimpleDateFormat;
-import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
 import java.util.Timer;
 import java.util.concurrent.Callable;
 import java.util.concurrent.CountDownLatch;
@@ -89,12 +85,21 @@ public class CrudAsyncGraphDataService extends AbstractGraphDataService {
   public static Integer getRequestTimeOut() {
     return requestTimeOut;
   }
+  
+  public CrudAsyncGraphDataService(GraphDao dao, 
+          EventPublisher asyncRequestPublisher,
+          EventConsumer asyncResponseConsumer) throws CrudException {
+      this(dao,dao,asyncRequestPublisher,asyncResponseConsumer);
+  }
 
   public CrudAsyncGraphDataService(GraphDao dao, 
+          GraphDao daoForGet, 
                  EventPublisher asyncRequestPublisher,
                  EventConsumer asyncResponseConsumer) throws CrudException {
 
-     super(dao);
+    super();
+    this.dao = dao;
+    this.daoForGet = daoForGet;
      
     requestTimeOut = DEFAULT_REQUEST_TIMEOUT;
     try {
@@ -266,7 +271,7 @@ public class CrudAsyncGraphDataService extends AbstractGraphDataService {
   public String patchVertex(String version, String id, String type, VertexPayload payload)
     throws CrudException {
     Vertex existingVertex
-      = dao.getVertex(id, OxmModelValidator.resolveCollectionType(version, type));
+      = dao.getVertex(id, OxmModelValidator.resolveCollectionType(version, type), version, new HashMap<String, String>());
     Vertex patchedVertex = OxmModelValidator.validateIncomingPatchPayload(id, version,
                                                                           type, payload.getProperties(),
                                                                           existingVertex);
@@ -323,7 +328,7 @@ public class CrudAsyncGraphDataService extends AbstractGraphDataService {
 
   public String updateEdge(String version, String id, String type, EdgePayload payload)
     throws CrudException {
-    Edge edge = dao.getEdge(id, type);
+    Edge edge = dao.getEdge(id, type, new HashMap<String, String>());
     Edge validatedEdge = RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version,
                                                                                    payload);
     GraphEvent event = GraphEvent.builder(GraphEventOperation.UPDATE)
@@ -345,7 +350,7 @@ public class CrudAsyncGraphDataService extends AbstractGraphDataService {
 
   public String patchEdge(String version, String id, String type, EdgePayload payload)
     throws CrudException {
-    Edge edge = dao.getEdge(id, type);
+    Edge edge = dao.getEdge(id, type, new HashMap<String, String>());
     Edge patchedEdge = RelationshipSchemaValidator.validateIncomingPatchPayload(edge, version,
                                                                                 payload);
     GraphEvent event = GraphEvent.builder(GraphEventOperation.UPDATE)
@@ -370,142 +375,54 @@ public class CrudAsyncGraphDataService extends AbstractGraphDataService {
     timer.cancel();
 
   }
-
+  
   @Override
-  public String addBulk(String version, BulkPayload payload) throws CrudException {
-    HashMap<String, Vertex> vertices = new HashMap<String, Vertex>();
-    HashMap<String, Edge> edges = new HashMap<String, Edge>();
-    String txId = dao.openTransaction();   
-     
-    try {
-      // Handle vertices
-      for (JsonElement v : payload.getObjects()) {
-        List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
-            v.getAsJsonObject().entrySet());
-
-        if (entries.size() != 2) {
-          throw new CrudException("", Status.BAD_REQUEST);
-        }
-        Map.Entry<String, JsonElement> opr = entries.get(0);
-        Map.Entry<String, JsonElement> item = entries.get(1);
-
-        VertexPayload vertexPayload = VertexPayload.fromJson(item.getValue().getAsJsonObject().toString());
-
-        if (opr.getValue().getAsString().equalsIgnoreCase("add")
-            || opr.getValue().getAsString().equalsIgnoreCase("modify")) {
-          Vertex validatedVertex;
-          GraphEvent event;
-          
-          if (opr.getValue().getAsString().equalsIgnoreCase("add")) {
-            // Publish add-vertex event
-            validatedVertex = OxmModelValidator.validateIncomingUpsertPayload(null, version, vertexPayload.getType(),
-                vertexPayload.getProperties());
-            event = GraphEvent.builder(GraphEventOperation.CREATE)
-                .vertex(GraphEventVertex.fromVertex(validatedVertex, version)).build();
-          } else {
-            // Publish update-vertex event
-            validatedVertex = OxmModelValidator.validateIncomingUpsertPayload(vertexPayload.getId(), version,
-                vertexPayload.getType(), vertexPayload.getProperties());
-            event = GraphEvent.builder(GraphEventOperation.UPDATE)
-                .vertex(GraphEventVertex.fromVertex(validatedVertex, version)).build();           
-          }
-          
-          event.setDbTransactionId(txId);
-          GraphEvent response = publishEvent(event);
-          Vertex persistedVertex = response.getVertex().toVertex();
-          Vertex outgoingVertex = OxmModelValidator.validateOutgoingPayload(version, persistedVertex);
-          vertices.put(item.getKey(), outgoingVertex);
-        } else if (opr.getValue().getAsString().equalsIgnoreCase("delete")) {
-          // Publish delete-vertex event
-          String type = OxmModelValidator.resolveCollectionType(version, vertexPayload.getType());
-          GraphEvent event = GraphEvent.builder(GraphEventOperation.DELETE)
-            .vertex(new GraphEventVertex(vertexPayload.getId(), version, type, null)).build();
-          event.setDbTransactionId(txId);
-          publishEvent(event); 
-        }
-      }
-      
-      // Handle Edges
-      for (JsonElement v : payload.getRelationships()) {
-        List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
-            v.getAsJsonObject().entrySet());
-
-        if (entries.size() != 2) {
-          throw new CrudException("", Status.BAD_REQUEST);
-        }
-        Map.Entry<String, JsonElement> opr = entries.get(0);
-        Map.Entry<String, JsonElement> item = entries.get(1);
-
-        EdgePayload edgePayload = EdgePayload.fromJson(item.getValue().getAsJsonObject().toString());
-
-        if (opr.getValue().getAsString().equalsIgnoreCase("add")
-            || opr.getValue().getAsString().equalsIgnoreCase("modify")) {
-          Edge validatedEdge;
-          Edge persistedEdge;
-          if (opr.getValue().getAsString().equalsIgnoreCase("add")) {
-            // Fix the source/destination
-            if (edgePayload.getSource().startsWith("$")) {
-              Vertex source = vertices.get(edgePayload.getSource().substring(1));
-              if (source == null) {
-                throw new CrudException("Not able to find vertex: " + edgePayload.getSource().substring(1),
-                    Status.INTERNAL_SERVER_ERROR);
-              }
-              edgePayload
-                  .setSource("services/inventory/" + version + "/" + source.getType() + "/" + source.getId().get());
-            }
-            if (edgePayload.getTarget().startsWith("$")) {
-              Vertex target = vertices.get(edgePayload.getTarget().substring(1));
-              if (target == null) {
-                throw new CrudException("Not able to find vertex: " + edgePayload.getTarget().substring(1),
-                    Status.INTERNAL_SERVER_ERROR);
-              }
-              edgePayload
-                  .setTarget("services/inventory/" + version + "/" + target.getType() + "/" + target.getId().get());
-            }
-            validatedEdge = RelationshipSchemaValidator.validateIncomingAddPayload(version, edgePayload.getType(),
-                edgePayload);
-            GraphEvent event = GraphEvent.builder(GraphEventOperation.CREATE)
-                .edge(GraphEventEdge.fromEdge(validatedEdge, version)).build();
-            event.setDbTransactionId(txId);
-            GraphEvent response = publishEvent(event);
-            persistedEdge =  response.getEdge().toEdge();
-          } else {
-            Edge edge = dao.getEdge(edgePayload.getId(), edgePayload.getType(), txId);
-            validatedEdge = RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, edgePayload);
-            GraphEvent event = GraphEvent.builder(GraphEventOperation.UPDATE)
-                .edge(GraphEventEdge.fromEdge(validatedEdge, version)).build();
-            event.setDbTransactionId(txId);
-            GraphEvent response = publishEvent(event);
-            persistedEdge = response.getEdge().toEdge();
-          }
-
-          Edge outgoingEdge = RelationshipSchemaValidator.validateOutgoingPayload(version, persistedEdge);
-          edges.put(item.getKey(), outgoingEdge);
-        } else if (opr.getValue().getAsString().equalsIgnoreCase("delete")) {
-          RelationshipSchemaValidator.validateType(version, edgePayload.getType());
-          // Publish delete-vertex event
-          GraphEvent event = GraphEvent.builder(GraphEventOperation.DELETE)
-            .edge(new GraphEventEdge(edgePayload.getId(), version, edgePayload.getType(), null, null, null)).build();
-          event.setDbTransactionId(txId);
-          publishEvent(event);
-        }
-      } 
-      
-      // commit transaction
-      dao.commitTransaction(txId);
-    } catch (CrudException ex) {
-      dao.rollbackTransaction(txId);
-      throw ex;
-    } catch (Exception ex) {
-      dao.rollbackTransaction(txId);
-      throw ex;
-    } finally {
-      if (dao.transactionExists(txId)) {
-        dao.rollbackTransaction(txId);
-      }
-    }
-    
-    return CrudResponseBuilder.buildUpsertBulkResponse(vertices, edges, version, payload);
+  protected Vertex addBulkVertex(Vertex vertex, String version, String dbTransId) throws CrudException {
+    GraphEvent event = GraphEvent.builder(GraphEventOperation.CREATE)
+        .vertex(GraphEventVertex.fromVertex(vertex, version)).build();
+    event.setDbTransactionId(dbTransId);
+    GraphEvent response = publishEvent(event); 
+    return response.getVertex().toVertex();
+  }
+  
+  @Override
+  protected Vertex updateBulkVertex(Vertex vertex, String id, String version, String dbTransId) throws CrudException {
+    GraphEvent event = GraphEvent.builder(GraphEventOperation.UPDATE)
+        .vertex(GraphEventVertex.fromVertex(vertex, version)).build();    
+    event.setDbTransactionId(dbTransId);
+    GraphEvent response = publishEvent(event);
+    return response.getVertex().toVertex();
+  }
+  
+  @Override
+  protected void deleteBulkVertex(String id, String version, String type, String dbTransId) throws CrudException {
+    GraphEvent event = GraphEvent.builder(GraphEventOperation.DELETE).vertex(new GraphEventVertex(id, version, type, null)).build();
+    event.setDbTransactionId(dbTransId);
+    publishEvent(event); 
+  }
+  
+  @Override
+  protected Edge addBulkEdge(Edge edge, String version, String dbTransId) throws CrudException {
+    GraphEvent event = GraphEvent.builder(GraphEventOperation.CREATE).edge(GraphEventEdge.fromEdge(edge, version)).build();
+    event.setDbTransactionId(dbTransId);
+    GraphEvent response = publishEvent(event);
+    return response.getEdge().toEdge();
+  }
+  
+  @Override
+  protected Edge updateBulkEdge(Edge edge, String version, String dbTransId) throws CrudException {
+    GraphEvent event = GraphEvent.builder(GraphEventOperation.UPDATE).edge(GraphEventEdge.fromEdge(edge, version)).build();
+    event.setDbTransactionId(dbTransId);
+    GraphEvent response = publishEvent(event);
+    return response.getEdge().toEdge();
+  }
+  
+  @Override
+  protected void deleteBulkEdge(String id, String version, String type, String dbTransId) throws CrudException {
+    GraphEvent event = GraphEvent.builder(GraphEventOperation.DELETE)
+        .edge(new GraphEventEdge(id, version, type, null, null, null)).build();
+    event.setDbTransactionId(dbTransId);
+    publishEvent(event);
   }
   
   private GraphEvent publishEvent(GraphEvent event) throws CrudException {
@@ -536,5 +453,4 @@ public class CrudAsyncGraphDataService extends AbstractGraphDataService {
         + " , operation: " + event.getOperation().toString() + " , result: "
         + response.getResult() + " , error: " + response.getErrorMessage());
   }
-  
 }
\ No newline at end of file