Support t_k queries from gizmo
[aai/gizmo.git] / src / main / java / org / onap / crud / service / AbstractGraphDataService.java
index 654ebfb..2a2df57 100644 (file)
@@ -25,6 +25,7 @@ package org.onap.crud.service;
 
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 
@@ -43,45 +44,44 @@ import org.onap.schema.RelationshipSchemaValidator;
 import com.google.gson.JsonElement;
 
 public abstract class AbstractGraphDataService {
+  protected GraphDao daoForGet;
   protected GraphDao dao;
   
-  public AbstractGraphDataService(GraphDao dao) throws CrudException {
-    this.dao = dao;
-
+  public AbstractGraphDataService() throws CrudException {
     CrudServiceUtil.loadModels();
   }
-  
-  public String getEdge(String version, String id, String type) throws CrudException {
+
+  public String getEdge(String version, String id, String type, Map<String, String> queryParams) throws CrudException {
     RelationshipSchemaValidator.validateType(version, type);
-    Edge edge = dao.getEdge(id, type);
+    Edge edge = daoForGet.getEdge(id, type, queryParams);
 
     return CrudResponseBuilder.buildGetEdgeResponse(RelationshipSchemaValidator.validateOutgoingPayload(version, edge), version);
   }
   
   public String getEdges(String version, String type, Map<String, String> filter) throws CrudException {
     RelationshipSchemaValidator.validateType(version, type);
-    List<Edge> items = dao.getEdges(type, RelationshipSchemaValidator.resolveCollectionfilter(version, type, filter));
+    List<Edge> items = daoForGet.getEdges(type, RelationshipSchemaValidator.resolveCollectionfilter(version, type, filter));
     return CrudResponseBuilder.buildGetEdgesResponse(items, version);
   }
   
-  public String getVertex(String version, String id, String type) throws CrudException {
+  public String getVertex(String version, String id, String type, Map<String, String> queryParams) throws CrudException {
     type = OxmModelValidator.resolveCollectionType(version, type);
-    Vertex vertex = dao.getVertex(id, type);
-    List<Edge> edges = dao.getVertexEdges(id);
+    Vertex vertex = daoForGet.getVertex(id, type, version, queryParams);
+    List<Edge> edges = daoForGet.getVertexEdges(id, null); //Not passing parameters as backend doesnt support these queries
     return CrudResponseBuilder.buildGetVertexResponse(OxmModelValidator.validateOutgoingPayload(version, vertex), edges,
         version);
   }
 
-  public String getVertices(String version, String type, Map<String, String> filter) throws CrudException {
+  public String getVertices(String version, String type, Map<String, String> filter, HashSet<String> properties) throws CrudException {
     type = OxmModelValidator.resolveCollectionType(version, type);
-    List<Vertex> items = dao.getVertices(type, OxmModelValidator.resolveCollectionfilter(version, type, filter));
+    List<Vertex> items = daoForGet.getVertices(type, OxmModelValidator.resolveCollectionfilter(version, type, filter), properties, version);
     return CrudResponseBuilder.buildGetVerticesResponse(items, version);
   }
   
   public String addBulk(String version, BulkPayload payload, HttpHeaders headers) throws CrudException {
     HashMap<String, Vertex> vertices = new HashMap<String, Vertex>();
     HashMap<String, Edge> edges = new HashMap<String, Edge>();
-
+    
     String txId = dao.openTransaction();   
      
     try {
@@ -155,6 +155,23 @@ public abstract class AbstractGraphDataService {
           Vertex outgoingVertex = OxmModelValidator.validateOutgoingPayload(version, persistedVertex);
           vertices.put(item.getKey(), outgoingVertex);
         }
+        
+        // Patch vertex 
+        else if (opr.getValue().getAsString().equalsIgnoreCase("patch")) {
+          if ( (vertexPayload.getId() == null) || (vertexPayload.getType() == null) ) {
+            throw new CrudException("id and type must be specified for patch request", Status.BAD_REQUEST);
+          }
+          
+          vertexPayload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(vertexPayload.getProperties(), 
+              headers, false));
+          
+          Vertex existingVertex = dao.getVertex(vertexPayload.getId(), OxmModelValidator.resolveCollectionType(version, vertexPayload.getType()), version, new HashMap<String, String>());
+          Vertex validatedVertex = OxmModelValidator.validateIncomingPatchPayload(vertexPayload.getId(), 
+              version, vertexPayload.getType(), vertexPayload.getProperties(), existingVertex);
+          Vertex persistedVertex = updateBulkVertex(validatedVertex, vertexPayload.getId(), version, txId);
+          Vertex outgoingVertex = OxmModelValidator.validateOutgoingPayload(version, persistedVertex);
+          vertices.put(item.getKey(), outgoingVertex);
+        }
       }
 
       // Step 4: Handle edge add/modify 
@@ -171,7 +188,8 @@ public abstract class AbstractGraphDataService {
 
         // Add/Update edge
         if (opr.getValue().getAsString().equalsIgnoreCase("add")
-            || opr.getValue().getAsString().equalsIgnoreCase("modify")) {
+            || opr.getValue().getAsString().equalsIgnoreCase("modify") 
+            || opr.getValue().getAsString().equalsIgnoreCase("patch")) {
           Edge validatedEdge;
           Edge persistedEdge;
           if (opr.getValue().getAsString().equalsIgnoreCase("add")) {
@@ -197,11 +215,19 @@ public abstract class AbstractGraphDataService {
             validatedEdge = RelationshipSchemaValidator.validateIncomingAddPayload(version, edgePayload.getType(),
                 edgePayload);
             persistedEdge = addBulkEdge(validatedEdge, version, txId);
-          } else {
+          } else if (opr.getValue().getAsString().equalsIgnoreCase("modify")) {
             Edge edge = dao.getEdge(edgePayload.getId(), edgePayload.getType(), txId);
             validatedEdge = RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, edgePayload);
             persistedEdge = updateBulkEdge(validatedEdge, version, txId);
+          } else {
+            if ( (edgePayload.getId() == null) || (edgePayload.getType() == null) ) {
+              throw new CrudException("id and type must be specified for patch request", Status.BAD_REQUEST);
+            }
+            Edge existingEdge = dao.getEdge(edgePayload.getId(), edgePayload.getType(), txId);
+            Edge patchedEdge = RelationshipSchemaValidator.validateIncomingPatchPayload(existingEdge, version, edgePayload);
+            persistedEdge = updateBulkEdge(patchedEdge, version, txId);
           }
+          
 
           Edge outgoingEdge = RelationshipSchemaValidator.validateOutgoingPayload(version, persistedEdge);
           edges.put(item.getKey(), outgoingEdge);
@@ -242,4 +268,5 @@ public abstract class AbstractGraphDataService {
   protected abstract Edge addBulkEdge(Edge edge, String version, String dbTransId) throws CrudException;
   protected abstract Edge updateBulkEdge(Edge edge, String version, String dbTransId) throws CrudException;
   protected abstract void deleteBulkEdge(String id, String version, String type, String dbTransId) throws CrudException;
+  
 }