Auto-resolve edge type 59/56959/1
authorsblimkie <steven.blimkie@amdocs.com>
Thu, 19 Jul 2018 20:03:44 +0000 (16:03 -0400)
committersblimkie <steven.blimkie@amdocs.com>
Thu, 19 Jul 2018 20:08:20 +0000 (16:08 -0400)
Auto-resolve edge type based on EdgeRules file.

Change-Id: Ic6de47f5172bc410efcdd5f08c1ea5c4f822610e
Issue-ID: AAI-1396
Signed-off-by: sblimkie <steven.blimkie@amdocs.com>
14 files changed:
src/main/java/org/onap/crud/dao/GraphDao.java
src/main/java/org/onap/crud/dao/champ/ChampDao.java
src/main/java/org/onap/crud/service/AbstractGraphDataService.java
src/main/java/org/onap/crud/service/CrudAsyncGraphDataService.java
src/main/java/org/onap/crud/service/CrudGraphDataService.java
src/main/java/org/onap/crud/service/CrudRestService.java
src/main/java/org/onap/crud/util/CrudServiceUtil.java
src/main/java/org/onap/schema/RelationshipSchema.java
src/test/java/org/onap/crud/service/ChampDaoExceptionsTest.java
src/test/java/org/onap/crud/service/ChampDaoMockTest.java
src/test/java/org/onap/crud/service/CrudRestServiceTest.java
src/test/java/org/onap/crud/service/TestDao.java
src/test/java/org/onap/schema/EdgeRulesLoaderTest.java
src/test/resources/rules/DbEdgeRules_test_two_v11.json

index 6c1bca9..7cb3d4c 100644 (file)
@@ -42,10 +42,12 @@ public interface GraphDao {
    *          - The unique identifier of the vertex to retrieve the edges for.
    * @param queryParams
    *             - query parameters to be passed
    *          - The unique identifier of the vertex to retrieve the edges for.
    * @param queryParams
    *             - query parameters to be passed
+   * @param txid
+   *      - a DB transaction ID to use (if null, no transactionId is used)
    * @return - A collection of edges.
    * @throws CrudException
    */
    * @return - A collection of edges.
    * @throws CrudException
    */
-  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;
 
   /**
    * Retrieve a collection of {@link Vertex} objects which match the supplied
 
   /**
    * Retrieve a collection of {@link Vertex} objects which match the supplied
@@ -89,7 +91,7 @@ public interface GraphDao {
    * @throws CrudException
    */
   public OperationResult getEdge(String id, String type, Map<String, String> queryParams) throws CrudException;
    * @throws CrudException
    */
   public OperationResult getEdge(String id, String type, Map<String, String> queryParams) throws CrudException;
-
+  
   /**
    * Retrieve a collection of {@link Edge} objects with a given type and which
    * match a set of supplied filter parameters.
   /**
    * Retrieve a collection of {@link Edge} objects with a given type and which
    * match a set of supplied filter parameters.
@@ -172,7 +174,7 @@ public interface GraphDao {
    *          - The unique identifier of the edge to be deleted.
    * @throws CrudException
    */
    *          - The unique identifier of the edge to be deleted.
    * @throws CrudException
    */
-  public void deleteEdge(String id, String type) throws CrudException;
+  public void deleteEdge(String id) throws CrudException;
 
   public String openTransaction();
 
 
   public String openTransaction();
 
@@ -193,7 +195,9 @@ public interface GraphDao {
 
   public void deleteVertex(String id, String type, String txId) throws CrudException;
 
 
   public void deleteVertex(String id, String type, String txId) throws CrudException;
 
-  public void deleteEdge(String id, String type, String txId) throws CrudException;
+  public void deleteEdge(String id, String txId) throws CrudException;
 
 
-  public Edge getEdge(String id, String type, String txId) throws CrudException;
+  public Edge getEdge(String id, String txId) throws CrudException;
+  
+  public Edge getEdge(String id) throws CrudException;
 }
 }
index fd5de57..81980cc 100644 (file)
@@ -141,26 +141,39 @@ public class ChampDao implements GraphDao {
   }
 
   @Override
   }
 
   @Override
-  public List<Edge> getVertexEdges(String id, Map<String, String> queryParams) throws CrudException {
-    StringBuilder strBuild = new StringBuilder(baseObjectUrl);
-    strBuild.append("/relationships/");
-    strBuild.append(id);
-    if(queryParams != null && !queryParams.isEmpty())
-    {
-        strBuild.append("?");
-        strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParams), Charset.defaultCharset()));
-    }
+  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);
+
+      Map<String,String> queryParamsCopy = null;
+      if (queryParams != null) {
+          queryParamsCopy = new HashMap<String,String>(queryParams);
+      }
+      else {
+          queryParamsCopy = new HashMap<String,String>();
+      }
 
 
-    OperationResult getResult = client.get(strBuild.toString(), createHeader(), MediaType.APPLICATION_JSON_TYPE);
+      if (txId != null) {
+          queryParamsCopy.put("transactionId", txId);
+      }
 
 
-    if (getResult.getResultCode() == 200) {
-      return champGson.fromJson(getResult.getResult(), new TypeToken<List<Edge>>() {
-      }.getType());
-    } 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");
-    }
+      if (!queryParamsCopy.isEmpty())
+      {
+          strBuild.append("?");
+          strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParamsCopy), Charset.defaultCharset()));
+      }
+
+      OperationResult getResult = client.get(strBuild.toString(), createHeader(), MediaType.APPLICATION_JSON_TYPE);
+
+      if (getResult.getResultCode() == 200) {
+          return champGson.fromJson(getResult.getResult(), new TypeToken<List<Edge>>() {
+          }.getType());
+      } 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");
+      }
   }
 
   @Override
   }
 
   @Override
@@ -343,7 +356,7 @@ public class ChampDao implements GraphDao {
   }
 
   @Override
   }
 
   @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);
 
     String url = baseRelationshipUrl + "/" + id;
     OperationResult getResult = client.delete(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
 
@@ -512,7 +525,7 @@ public class ChampDao implements GraphDao {
   }
 
   @Override
   }
 
   @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);
 
     String url = baseRelationshipUrl + "/" + id + "?transactionId=" + txId;
     OperationResult getResult = client.delete(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
 
@@ -524,19 +537,12 @@ public class ChampDao implements GraphDao {
   }
 
   @Override
   }
 
   @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());
     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
       return edge;
     } else {
       // We didn't find an edge with the supplied id, so just throw an
@@ -544,6 +550,20 @@ public class ChampDao implements GraphDao {
       throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph");
     }
   }
       throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph");
     }
   }
+  
+  @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;
 
   public Vertex getVertex(String id, String type, String version, String txId) throws CrudException {
     String url = baseObjectUrl + "/" + id + "?transactionId=" + txId;
index a5b52bd..59046ab 100644 (file)
@@ -83,7 +83,7 @@ public abstract class AbstractGraphDataService {
     type = OxmModelValidator.resolveCollectionType(version, type);
     OperationResult vertexOpResult = daoForGet.getVertex(id, type, version, queryParams);
     Vertex vertex = Vertex.fromJson(vertexOpResult.getResult(), version);
     type = OxmModelValidator.resolveCollectionType(version, type);
     OperationResult vertexOpResult = daoForGet.getVertex(id, type, version, queryParams);
     Vertex vertex = Vertex.fromJson(vertexOpResult.getResult(), version);
-    List<Edge> edges = daoForGet.getVertexEdges(id, queryParams);
+    List<Edge> edges = daoForGet.getVertexEdges(id, queryParams, null);
     EntityTag entityTag = CrudServiceUtil.getETagFromHeader(vertexOpResult.getHeaders());
     return new ImmutablePair<>(entityTag, CrudResponseBuilder.buildGetVertexResponse(OxmModelValidator.validateOutgoingPayload(version, vertex), edges,
         version));
     EntityTag entityTag = CrudServiceUtil.getETagFromHeader(vertexOpResult.getHeaders());
     return new ImmutablePair<>(entityTag, CrudResponseBuilder.buildGetVertexResponse(OxmModelValidator.validateOutgoingPayload(version, vertex), edges,
         version));
@@ -117,8 +117,7 @@ public abstract class AbstractGraphDataService {
         EdgePayload edgePayload = EdgePayload.fromJson(item.getValue().getAsJsonObject().toString());
 
         if (opr.getValue().getAsString().equalsIgnoreCase("delete")) {
         EdgePayload edgePayload = EdgePayload.fromJson(item.getValue().getAsJsonObject().toString());
 
         if (opr.getValue().getAsString().equalsIgnoreCase("delete")) {
-          RelationshipSchemaValidator.validateType(version, edgePayload.getType());
-          deleteBulkEdge(edgePayload.getId(), version, edgePayload.getType(), txId);
+          deleteBulkEdge(edgePayload.getId(), version, txId);
         }
       }
 
         }
       }
 
@@ -233,36 +232,68 @@ public abstract class AbstractGraphDataService {
                   .setTarget("services/inventory/" + version + "/" + target.getType() + "/" + target.getId().get());
             }
 
                   .setTarget("services/inventory/" + version + "/" + target.getType() + "/" + target.getId().get());
             }
 
+            // If the type isn't set, resolve it based on on the sourece and target vertex types
+            if (edgePayload.getType() == null || edgePayload.getType().isEmpty()) {
+              edgePayload.setType(CrudServiceUtil.determineEdgeType(edgePayload, version));
+            }
+            
+            // TODO:  Champ needs to support getting an object's relationships within the context of an existing transaction.
+            //        Currently it doesn't.  Disabling multiplicity check until this happens.
+            
+            List<Edge> sourceVertexEdges = new ArrayList<Edge>();
+            List<Edge> targetVertexEdges = new ArrayList<Edge>();
+            
+            /*
             List<Edge> sourceVertexEdges =
                     EdgePayloadUtil.filterEdgesByRelatedVertexAndType(EdgePayloadUtil.getVertexNodeType(edgePayload.getSource()), edgePayload.getType(),
             List<Edge> sourceVertexEdges =
                     EdgePayloadUtil.filterEdgesByRelatedVertexAndType(EdgePayloadUtil.getVertexNodeType(edgePayload.getSource()), edgePayload.getType(),
-                                 daoForGet.getVertexEdges(EdgePayloadUtil.getVertexNodeId(edgePayload.getSource()), null));
-
+                                 dao.getVertexEdges(EdgePayloadUtil.getVertexNodeId(edgePayload.getSource()), null, txId));
+            
             List<Edge> targetVertexEdges =
                      EdgePayloadUtil.filterEdgesByRelatedVertexAndType(EdgePayloadUtil.getVertexNodeType(edgePayload.getTarget()), edgePayload.getType(),
             List<Edge> targetVertexEdges =
                      EdgePayloadUtil.filterEdgesByRelatedVertexAndType(EdgePayloadUtil.getVertexNodeType(edgePayload.getTarget()), edgePayload.getType(),
-                                 daoForGet.getVertexEdges(EdgePayloadUtil.getVertexNodeId(edgePayload.getTarget()), null));
-
+                                 dao.getVertexEdges(EdgePayloadUtil.getVertexNodeId(edgePayload.getTarget()), null, txId));
+            */
+            
             validatedEdge = RelationshipSchemaValidator.validateIncomingAddPayload(version, edgePayload.getType(), edgePayload, sourceVertexEdges,
                     targetVertexEdges);
             persistedEdge = addBulkEdge(validatedEdge, version, txId);
           } else if (opr.getValue().getAsString().equalsIgnoreCase("modify")) {
             validatedEdge = RelationshipSchemaValidator.validateIncomingAddPayload(version, edgePayload.getType(), edgePayload, sourceVertexEdges,
                     targetVertexEdges);
             persistedEdge = addBulkEdge(validatedEdge, version, txId);
           } else if (opr.getValue().getAsString().equalsIgnoreCase("modify")) {
-            Edge edge = dao.getEdge(edgePayload.getId(), edgePayload.getType(), txId);
+            Edge edge = dao.getEdge(edgePayload.getId(), txId);
+            
+            // If the type isn't set, resolve it based on on the sourece and target vertex types
+            if (edgePayload.getType() == null || edgePayload.getType().isEmpty()) {
+              edgePayload.setType(edge.getType());
+            }
 
 
+            // TODO:  Champ needs to support getting an object's relationships within the context of an existing transaction.
+            //        Currently it doesn't.  Disabling multiplicity check until this happens.
+            
+            List<Edge> sourceVertexEdges = new ArrayList<Edge>();
+            List<Edge> targetVertexEdges = new ArrayList<Edge>();
+            
+            /*
             // load source and target vertex relationships for validation
             List<Edge> sourceVertexEdges =
                    EdgePayloadUtil.filterEdgesByRelatedVertexAndType(EdgePayloadUtil.getVertexNodeType(edgePayload.getSource()), edgePayload.getType(),
             // load source and target vertex relationships for validation
             List<Edge> sourceVertexEdges =
                    EdgePayloadUtil.filterEdgesByRelatedVertexAndType(EdgePayloadUtil.getVertexNodeType(edgePayload.getSource()), edgePayload.getType(),
-                                daoForGet.getVertexEdges(EdgePayloadUtil.getVertexNodeId(edgePayload.getSource()), null));
+                                dao.getVertexEdges(EdgePayloadUtil.getVertexNodeId(edgePayload.getSource()), null, txId));
 
             List<Edge> targetVertexEdges =
                     EdgePayloadUtil.filterEdgesByRelatedVertexAndType(EdgePayloadUtil.getVertexNodeType(edgePayload.getTarget()), edgePayload.getType(),
 
             List<Edge> targetVertexEdges =
                     EdgePayloadUtil.filterEdgesByRelatedVertexAndType(EdgePayloadUtil.getVertexNodeType(edgePayload.getTarget()), edgePayload.getType(),
-                                daoForGet.getVertexEdges(EdgePayloadUtil.getVertexNodeId(edgePayload.getTarget()), null));
-
+                                dao.getVertexEdges(EdgePayloadUtil.getVertexNodeId(edgePayload.getTarget()), null, txId));
+            */
+            
             validatedEdge = RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, edgePayload, edgePayload.getType(), sourceVertexEdges, targetVertexEdges);
             persistedEdge = updateBulkEdge(validatedEdge, version, txId);
           } else {
             validatedEdge = RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, edgePayload, edgePayload.getType(), sourceVertexEdges, targetVertexEdges);
             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);
+            if (edgePayload.getId() == null) {
+              throw new CrudException("id must be specified for patch request", Status.BAD_REQUEST);
+            }
+            Edge existingEdge = dao.getEdge(edgePayload.getId(), txId);
+            
+            // If the type isn't set, resolve it based on on the sourece and target vertex types
+            if (edgePayload.getType() == null || edgePayload.getType().isEmpty()) {
+              edgePayload.setType(existingEdge.getType());
             }
             }
-            Edge existingEdge = dao.getEdge(edgePayload.getId(), edgePayload.getType(), txId);
+            
             Edge patchedEdge = RelationshipSchemaValidator.validateIncomingPatchPayload(existingEdge, version, edgePayload);
             persistedEdge = updateBulkEdge(patchedEdge, version, txId);
           }
             Edge patchedEdge = RelationshipSchemaValidator.validateIncomingPatchPayload(existingEdge, version, edgePayload);
             persistedEdge = updateBulkEdge(patchedEdge, version, txId);
           }
@@ -312,6 +343,6 @@ 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 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;
+  protected abstract void deleteBulkEdge(String id, String version, String dbTransId) throws CrudException;
 
 }
 
 }
index aa70a66..8d147eb 100644 (file)
@@ -414,7 +414,18 @@ public class CrudAsyncGraphDataService extends AbstractGraphDataService {
     }
 
     @Override
     }
 
     @Override
-    protected void deleteBulkEdge(String id, String version, String type, String dbTransId) throws CrudException {
+    protected void deleteBulkEdge(String id, String version, String dbTransId) throws CrudException {
+        // Get the edge type
+        String type = null;
+        try {
+            Edge edge = daoForGet.getEdge(id);
+            type = edge.getType();
+        }
+        catch (CrudException ex) {
+            // Likely the client is trying to delete an edge which isn't present.  Just swallow the exception
+            // and let the bulk request fail via the normal path.
+        }
+        
         GraphEvent event = GraphEvent.builder(GraphEventOperation.DELETE)
                 .edge(new GraphEventEdge(id, version, type, null, null, null)).build();
         event.setDbTransactionId(dbTransId);
         GraphEvent event = GraphEvent.builder(GraphEventOperation.DELETE)
                 .edge(new GraphEventEdge(id, version, type, null, null, null)).build();
         event.setDbTransactionId(dbTransId);
index 9ff48c7..ae2d294 100644 (file)
@@ -77,11 +77,11 @@ public class CrudGraphDataService extends AbstractGraphDataService {
     // load source and target vertex relationships for validation
     List<Edge> sourceVertexEdges =
              EdgePayloadUtil.filterEdgesByRelatedVertexAndType(EdgePayloadUtil.getVertexNodeType(payload.getSource()), type,
     // load source and target vertex relationships for validation
     List<Edge> sourceVertexEdges =
              EdgePayloadUtil.filterEdgesByRelatedVertexAndType(EdgePayloadUtil.getVertexNodeType(payload.getSource()), type,
-                          daoForGet.getVertexEdges(EdgePayloadUtil.getVertexNodeId(payload.getSource()), null));
+                          daoForGet.getVertexEdges(EdgePayloadUtil.getVertexNodeId(payload.getSource()), null, null));
 
     List<Edge> targetVertexEdges =
               EdgePayloadUtil.filterEdgesByRelatedVertexAndType(EdgePayloadUtil.getVertexNodeType(payload.getTarget()), type,
 
     List<Edge> targetVertexEdges =
               EdgePayloadUtil.filterEdgesByRelatedVertexAndType(EdgePayloadUtil.getVertexNodeType(payload.getTarget()), type,
-                          daoForGet.getVertexEdges(EdgePayloadUtil.getVertexNodeId(payload.getTarget()), null));
+                          daoForGet.getVertexEdges(EdgePayloadUtil.getVertexNodeId(payload.getTarget()), null, null));
 
     Edge edge = RelationshipSchemaValidator.validateIncomingAddPayload(version, type, payload, sourceVertexEdges, targetVertexEdges);
 
 
     Edge edge = RelationshipSchemaValidator.validateIncomingAddPayload(version, type, payload, sourceVertexEdges, targetVertexEdges);
 
@@ -140,7 +140,7 @@ public class CrudGraphDataService extends AbstractGraphDataService {
   @Override
   public String deleteEdge(String version, String id, String type) throws CrudException {
     RelationshipSchemaValidator.validateType(version, type);
   @Override
   public String deleteEdge(String version, String id, String type) throws CrudException {
     RelationshipSchemaValidator.validateType(version, type);
-    dao.deleteEdge(id, type);
+    dao.deleteEdge(id);
     return "";
   }
 
     return "";
   }
 
@@ -153,11 +153,11 @@ public class CrudGraphDataService extends AbstractGraphDataService {
     // load source and target vertex relationships for validation
     List<Edge> sourceVertexEdges =
              EdgePayloadUtil.filterEdgesByRelatedVertexAndType(EdgePayloadUtil.getVertexNodeType(payload.getSource()), type,
     // load source and target vertex relationships for validation
     List<Edge> sourceVertexEdges =
              EdgePayloadUtil.filterEdgesByRelatedVertexAndType(EdgePayloadUtil.getVertexNodeType(payload.getSource()), type,
-                          daoForGet.getVertexEdges(EdgePayloadUtil.getVertexNodeId(payload.getSource()), null));
+                          daoForGet.getVertexEdges(EdgePayloadUtil.getVertexNodeId(payload.getSource()), null, null));
 
     List<Edge> targetVertexEdges =
               EdgePayloadUtil.filterEdgesByRelatedVertexAndType(EdgePayloadUtil.getVertexNodeType(payload.getTarget()), type,
 
     List<Edge> targetVertexEdges =
               EdgePayloadUtil.filterEdgesByRelatedVertexAndType(EdgePayloadUtil.getVertexNodeType(payload.getTarget()), type,
-                          daoForGet.getVertexEdges(EdgePayloadUtil.getVertexNodeId(payload.getTarget()), null));
+                          daoForGet.getVertexEdges(EdgePayloadUtil.getVertexNodeId(payload.getTarget()), null, null));
 
     Edge validatedEdge = RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, payload, type, sourceVertexEdges, targetVertexEdges);
 
 
     Edge validatedEdge = RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, payload, type, sourceVertexEdges, targetVertexEdges);
 
@@ -213,7 +213,7 @@ public class CrudGraphDataService extends AbstractGraphDataService {
   }
 
   @Override
   }
 
   @Override
-  protected void deleteBulkEdge(String id, String version, String type, String dbTransId) throws CrudException {
-    dao.deleteEdge(id, type, dbTransId);
+  protected void deleteBulkEdge(String id, String version, String dbTransId) throws CrudException {
+    dao.deleteEdge(id, dbTransId);
   }
 }
   }
 }
index 25ab43a..3cfc657 100644 (file)
@@ -504,10 +504,6 @@ public class CrudRestService {
 
       EdgePayload edgePayload = EdgePayload.fromJson(item.getValue().getAsJsonObject().toString());
 
 
       EdgePayload edgePayload = EdgePayload.fromJson(item.getValue().getAsJsonObject().toString());
 
-      if (edgePayload.getType() == null) {
-        throw new CrudException("Edge Type cannot be null for: " + item.getKey(), Status.BAD_REQUEST);
-      }
-
       if (!opr.getKey().equalsIgnoreCase("operation")) {
         throw new CrudException("operation missing in item: " + item.getKey(), Status.BAD_REQUEST);
       }
       if (!opr.getKey().equalsIgnoreCase("operation")) {
         throw new CrudException("operation missing in item: " + item.getKey(), Status.BAD_REQUEST);
       }
@@ -697,8 +693,9 @@ public class CrudRestService {
         }
 
         if (payload.getType() == null || payload.getType().isEmpty()) {
         }
 
         if (payload.getType() == null || payload.getType().isEmpty()) {
-          throw new CrudException("Missing Edge Type ", Status.BAD_REQUEST);
+          payload.setType(CrudServiceUtil.determineEdgeType(payload, version));
         }
         }
+        
         ImmutablePair<EntityTag, String> result = graphDataService.addEdge(version, payload.getType(), payload);
         response = Response.status(Status.CREATED).entity(result.getValue()).tag(result.getKey()).type(mediaType).build();
       } else {
         ImmutablePair<EntityTag, String> result = graphDataService.addEdge(version, payload.getType(), payload);
         response = Response.status(Status.CREATED).entity(result.getValue()).tag(result.getKey()).type(mediaType).build();
       } else {
index ed5fe53..5eeb38e 100644 (file)
@@ -31,8 +31,12 @@ import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response.Status;
 import org.onap.aai.db.props.AAIProperties;
 import org.onap.crud.exception.CrudException;
 import javax.ws.rs.core.Response.Status;
 import org.onap.aai.db.props.AAIProperties;
 import org.onap.crud.exception.CrudException;
+import org.onap.crud.parser.EdgePayload;
+import org.onap.crud.parser.util.EdgePayloadUtil;
 import org.onap.schema.EdgeRulesLoader;
 import org.onap.schema.OxmModelLoader;
 import org.onap.schema.EdgeRulesLoader;
 import org.onap.schema.OxmModelLoader;
+import org.onap.schema.RelationshipSchema;
+
 import com.google.gson.Gson;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonPrimitive;
 import com.google.gson.Gson;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonPrimitive;
@@ -138,4 +142,25 @@ public class CrudServiceUtil {
     return entityTag;
   }
 
     return entityTag;
   }
 
+  public static String determineEdgeType(EdgePayload payload, String version) throws CrudException {
+    RelationshipSchema schema = EdgeRulesLoader.getSchemaForVersion(version);
+    
+    if (payload.getSource() == null || payload.getTarget() == null) {
+      throw new CrudException("Source/Target not specified", Status.BAD_REQUEST);
+    }
+    
+    Set<String> edgeTypes = schema.getValidRelationTypes(EdgePayloadUtil.getVertexNodeType(payload.getSource()), 
+        EdgePayloadUtil.getVertexNodeType(payload.getTarget()));
+    
+    if (edgeTypes.size() == 0) {
+      throw new CrudException("No valid relationship types from " + payload.getSource() + " to " + payload.getTarget(), Status.BAD_REQUEST);
+    }
+    
+    if (edgeTypes.size() > 1) {
+      throw new CrudException("Multiple possible relationship types from " + payload.getSource() + " to " + payload.getTarget(), Status.BAD_REQUEST);
+    }
+    
+    return edgeTypes.iterator().next();
+  }
+
 }
 }
index 2aab7a9..0cf419e 100644 (file)
@@ -23,7 +23,9 @@ package org.onap.schema;
 
 import java.io.IOException;
 import java.util.HashMap;
 
 import java.io.IOException;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
 import java.util.Map;
+import java.util.Set;
 import java.util.stream.Collectors;
 import javax.ws.rs.core.Response.Status;
 import org.codehaus.jackson.map.ObjectMapper;
 import java.util.stream.Collectors;
 import javax.ws.rs.core.Response.Status;
 import org.codehaus.jackson.map.ObjectMapper;
@@ -47,6 +49,10 @@ public class RelationshipSchema {
   private Map<String, Map<String, Class<?>>> relationTypes  = new HashMap<>();
 
   private Map<String, EdgeRule> relationshipRules = new HashMap<>();
   private Map<String, Map<String, Class<?>>> relationTypes  = new HashMap<>();
 
   private Map<String, EdgeRule> relationshipRules = new HashMap<>();
+  
+  // A map storing the list of valid edge types for a source/target pair
+  private Map<String, Set<String>> edgeTypesForNodePair = new HashMap<>();
+  
 
   @SuppressWarnings("unchecked")
   public RelationshipSchema(Multimap<String, EdgeRule> rules, String props) throws CrudException, IOException {
 
   @SuppressWarnings("unchecked")
   public RelationshipSchema(Multimap<String, EdgeRule> rules, String props) throws CrudException, IOException {
@@ -56,6 +62,16 @@ public class RelationshipSchema {
     for (EdgeRule rule : rules.values()) {
       String key = buildRelation(rule.getFrom(), rule.getTo(), rule.getLabel());
       relationshipRules.put(key, rule);
     for (EdgeRule rule : rules.values()) {
       String key = buildRelation(rule.getFrom(), rule.getTo(), rule.getLabel());
       relationshipRules.put(key, rule);
+      
+      String nodePairKey = buildNodePairKey(rule.getFrom(), rule.getTo());
+      if (edgeTypesForNodePair.get(nodePairKey) == null) {
+        Set<String> typeSet = new HashSet<String>();
+        typeSet.add(rule.getLabel());
+        edgeTypesForNodePair.put(nodePairKey, typeSet);
+      }
+      else {
+        edgeTypesForNodePair.get(nodePairKey).add(rule.getLabel());
+      }
     }
 
     Map<String, Class<?>> edgeProps = properties.entrySet().stream().collect(Collectors.toMap(p -> p.getKey(), p -> {
     }
 
     Map<String, Class<?>> edgeProps = properties.entrySet().stream().collect(Collectors.toMap(p -> p.getKey(), p -> {
@@ -106,10 +122,23 @@ public class RelationshipSchema {
     return relationTypes.containsKey(type);
   }
 
     return relationTypes.containsKey(type);
   }
 
+  public Set<String> getValidRelationTypes(String source, String target) {
+    Set<String> typeList = edgeTypesForNodePair.get(buildNodePairKey(source, target));
 
 
+    if (typeList == null) {
+      return new HashSet<String>();
+    }
+    
+    return typeList;
+  }
+  
   private String buildRelation(String source, String target, String relation) {
     return source + ":" + target + ":" + relation;
   }
   private String buildRelation(String source, String target, String relation) {
     return source + ":" + target + ":" + relation;
   }
+  
+  private String buildNodePairKey(String source, String target) {
+    return source + ":" + target;
+  }
 
   private Class<?> resolveClass(String type) throws CrudException, ClassNotFoundException {
     Class<?> clazz = Class.forName(type);
 
   private Class<?> resolveClass(String type) throws CrudException, ClassNotFoundException {
     Class<?> clazz = Class.forName(type);
index 1f0a20b..a7479b5 100644 (file)
@@ -229,7 +229,7 @@ public class ChampDaoExceptionsTest {
         buildChampDao();
 
         try {
         buildChampDao();
 
         try {
-            champDao.getEdge(idNotExists, type, txId);
+            champDao.getEdge(idNotExists, txId);
         } catch (CrudException e) {
             assertEquals(404, e.getHttpStatus().getStatusCode());
             assertThat(e.getMessage(), containsString(failureCauseForGetEdge));
         } catch (CrudException e) {
             assertEquals(404, e.getHttpStatus().getStatusCode());
             assertThat(e.getMessage(), containsString(failureCauseForGetEdge));
@@ -250,7 +250,7 @@ public class ChampDaoExceptionsTest {
 
         // Type not matches
         try {
 
         // Type not matches
         try {
-            champDao.getEdge(id, "", txId);
+            champDao.getEdge(id, txId);
         } catch (CrudException e) {
             assertEquals(404, e.getHttpStatus().getStatusCode());
             assertThat(e.getMessage(), containsString(failureCauseForGetEdgeTypeNotMatches));
         } catch (CrudException e) {
             assertEquals(404, e.getHttpStatus().getStatusCode());
             assertThat(e.getMessage(), containsString(failureCauseForGetEdgeTypeNotMatches));
@@ -332,7 +332,7 @@ public class ChampDaoExceptionsTest {
         buildChampDao();
 
         try {
         buildChampDao();
 
         try {
-            champDao.getVertexEdges(idNotExists, queryParams);
+            champDao.getVertexEdges(idNotExists, queryParams, null);
         } catch (CrudException e) {
             assertEquals(404, e.getHttpStatus().getStatusCode());
             assertThat(e.getMessage(), containsString(failureCauseForGetVertexEdges));
         } catch (CrudException e) {
             assertEquals(404, e.getHttpStatus().getStatusCode());
             assertThat(e.getMessage(), containsString(failureCauseForGetVertexEdges));
@@ -509,13 +509,13 @@ public class ChampDaoExceptionsTest {
         buildChampDao();
 
         try {
         buildChampDao();
 
         try {
-            champDao.deleteEdge(id, type);
+            champDao.deleteEdge(id);
         } catch (CrudException e) {
             assertEquals(400, e.getHttpStatus().getStatusCode());
             assertThat(e.getMessage(), containsString(failureCauseFordeleteEdge));
         }
         try {
         } catch (CrudException e) {
             assertEquals(400, e.getHttpStatus().getStatusCode());
             assertThat(e.getMessage(), containsString(failureCauseFordeleteEdge));
         }
         try {
-            champDao.deleteEdge(id, type, txId);
+            champDao.deleteEdge(id, txId);
         } catch (CrudException e) {
             assertEquals(400, e.getHttpStatus().getStatusCode());
             assertThat(e.getMessage(), containsString(failureCauseFordeleteEdge));
         } catch (CrudException e) {
             assertEquals(400, e.getHttpStatus().getStatusCode());
             assertThat(e.getMessage(), containsString(failureCauseFordeleteEdge));
index 75d5bfd..dadf81a 100644 (file)
@@ -269,11 +269,13 @@ public class ChampDaoMockTest {
         mockGetVertex("50bdab41-ad1c-4d00-952c-a0aa5d827811", "?transactionId=", "vserver");
         mockGetVertices(queryParamsVertices, "pserver");
         mockGetVertices(queryParamsVerticesV13, "pserver");
         mockGetVertex("50bdab41-ad1c-4d00-952c-a0aa5d827811", "?transactionId=", "vserver");
         mockGetVertices(queryParamsVertices, "pserver");
         mockGetVertices(queryParamsVerticesV13, "pserver");
-        mockGetVertexEdges("872dd5df-0be9-4167-95e9-2cf4b21165ed", queryParamsVertex, "tosca.relationships.HostedOn");
-        mockGetVertexEdges("872dd5df-0be9-4167-95e9-2cf4b21165ed", queryParamsVertexV13,
+        mockGetVertexEdges("872dd5df-0be9-4167-95e9-2cf4b21165ed", queryParamsVertex, null, "tosca.relationships.HostedOn");
+        mockGetVertexEdges("872dd5df-0be9-4167-95e9-2cf4b21165ed", queryParamsVertexV13, null,
                 "tosca.relationships.HostedOn");
                 "tosca.relationships.HostedOn");
-        mockGetVertexEdges("50bdab41-ad1c-4d00-952c-a0aa5d827811", emptyQueryParams, "tosca.relationships.HostedOn");
-        mockGetVertexEdges("1d326bc7-b985-492b-9604-0d5d1f06f908", emptyQueryParams, "tosca.relationships.HostedOn");
+        mockGetVertexEdges("50bdab41-ad1c-4d00-952c-a0aa5d827811", emptyQueryParams, null, "tosca.relationships.HostedOn");
+        mockGetVertexEdges("1d326bc7-b985-492b-9604-0d5d1f06f908", emptyQueryParams, null, "tosca.relationships.HostedOn");
+        mockGetVertexEdges("50bdab41-ad1c-4d00-952c-a0aa5d827811", emptyQueryParams, "?transactionId=", "tosca.relationships.HostedOn");
+        mockGetVertexEdges("1d326bc7-b985-492b-9604-0d5d1f06f908", emptyQueryParams, "?transactionId=", "tosca.relationships.HostedOn");
         mockGetEdges("?", "tosca.relationships.HostedOn");
         mockGetEdge("50bdab41-ad1c-4d00-952c-a0aa5d827811", "?transactionId=", "tosca.relationships.HostedOn");
         mockGetEdge("872dd5df-0be9-4167-95e9-2cf4b21165ed", emptyQueryParams, "tosca.relationships.HostedOn");
         mockGetEdges("?", "tosca.relationships.HostedOn");
         mockGetEdge("50bdab41-ad1c-4d00-952c-a0aa5d827811", "?transactionId=", "tosca.relationships.HostedOn");
         mockGetEdge("872dd5df-0be9-4167-95e9-2cf4b21165ed", emptyQueryParams, "tosca.relationships.HostedOn");
@@ -372,17 +374,24 @@ public class ChampDaoMockTest {
                 .thenReturn(operationResult);
     }
 
                 .thenReturn(operationResult);
     }
 
-    public void mockGetVertexEdges(String id, Map<String, String> queryParams, String type) {
+    public void mockGetVertexEdges(String id, Map<String, String> queryParams, String txId, String type) {
         String edgeResponse = champEdge.replace("edgeType", type);
         OperationResult operationResult = new OperationResult();
         List<String> edgeResponselist = new ArrayList<>();
         edgeResponselist.add(edgeResponse);
         operationResult.setResult(edgeResponselist.toString());
         operationResult.setResultCode(200);
         String edgeResponse = champEdge.replace("edgeType", type);
         OperationResult operationResult = new OperationResult();
         List<String> edgeResponselist = new ArrayList<>();
         edgeResponselist.add(edgeResponse);
         operationResult.setResult(edgeResponselist.toString());
         operationResult.setResultCode(200);
+        String baseUrl = BASE_OBJECT_URL + "/" + RELATIONSHIP_SUB_URL + "/" + id;
+        String url;
+        
+        if (txId != null) {
+            url = baseUrl + txId;
+        }
+        else {
+            url = appendQueryParams(baseUrl, queryParams).toString();
+        }
 
 
-        StringBuilder url = appendQueryParams(BASE_OBJECT_URL + "/" + RELATIONSHIP_SUB_URL + "/" + id, queryParams);
-
-        when(restClientMock.get(url.toString(), createHeaders(), MediaType.APPLICATION_JSON_TYPE))
+        when(restClientMock.get(url, createHeaders(), MediaType.APPLICATION_JSON_TYPE))
                 .thenReturn(operationResult);
     }
 
                 .thenReturn(operationResult);
     }
 
index d8be704..1098284 100644 (file)
@@ -335,7 +335,8 @@ public class CrudRestServiceTest {
 
     response = mockService.addBulk(payloadStr, "v9", "",
         "services/inventory/v9", new TestHeaders(), null, new TestRequest());
 
     response = mockService.addBulk(payloadStr, "v9", "",
         "services/inventory/v9", new TestHeaders(), null, new TestRequest());
-    System.out.println("Bulk Response: " + response.getStatus() + "\n" + response.getEntity().toString());
+    System.out.println("Bulk Response: " + response);
+    System.out.println("Response Entity: " + response.getEntity().toString());
     assertTrue(response.getStatus() == 200);
   }
 
     assertTrue(response.getStatus() == 200);
   }
 
index 20cd04a..4f1d34e 100644 (file)
@@ -84,7 +84,7 @@ public class TestDao implements GraphDao {
   }
 
   @Override
   }
 
   @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 {
     List<Edge> list = new ArrayList<Edge>();
     list.add(Edge.fromJson(champEdge));
     return list;
     List<Edge> list = new ArrayList<Edge>();
     list.add(Edge.fromJson(champEdge));
     return list;
@@ -159,7 +159,7 @@ public class TestDao implements GraphDao {
   }
 
   @Override
   }
 
   @Override
-  public void deleteEdge(String id, String type) throws CrudException {
+  public void deleteEdge(String id) throws CrudException {
     
   }
 
     
   }
 
@@ -217,13 +217,13 @@ public class TestDao implements GraphDao {
   }
 
   @Override
   }
 
   @Override
-  public void deleteEdge(String id, String type, String txId) throws CrudException {
+  public void deleteEdge(String id, String txId) throws CrudException {
     // TODO Auto-generated method stub
     
   }
 
   @Override
     // TODO Auto-generated method stub
     
   }
 
   @Override
-  public Edge getEdge(String id, String type, String txId) throws CrudException {
+  public Edge getEdge(String id, String txId) throws CrudException {
     return Edge.fromJson(champEdge);
   }
   
     return Edge.fromJson(champEdge);
   }
   
@@ -232,4 +232,10 @@ public class TestDao implements GraphDao {
     headers.add("etag", "test123");
     return headers;
   }
     headers.add("etag", "test123");
     return headers;
   }
+
+  @Override
+  public Edge getEdge(String id) throws CrudException {
+    // TODO Auto-generated method stub
+    return null;
+  }
 }
\ No newline at end of file
 }
\ No newline at end of file
index 05f998f..aad11fc 100644 (file)
@@ -24,8 +24,13 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
+
+import java.util.Set;
+
 import org.junit.Test;
 import org.onap.crud.exception.CrudException;
 import org.junit.Test;
 import org.onap.crud.exception.CrudException;
+import org.onap.crud.parser.EdgePayload;
+import org.onap.crud.util.CrudServiceUtil;
 
 public class EdgeRulesLoaderTest {
 
 
 public class EdgeRulesLoaderTest {
 
@@ -54,6 +59,63 @@ public class EdgeRulesLoaderTest {
         assertNull(g.lookupRelation("U:W:org.onap.relationships.inventory.groupsResourcesIn"));
     }
 
         assertNull(g.lookupRelation("U:W:org.onap.relationships.inventory.groupsResourcesIn"));
     }
 
+    @Test
+    public void getRelationshipTypeForNodePair() throws Exception {
+        EdgeRulesLoader.resetSchemaVersionContext();
+        EdgeRulesLoader.loadModels("v11");
+        RelationshipSchema schema = EdgeRulesLoader.getSchemaForVersion("v11");
+        
+        EdgePayload payload1 = new EdgePayload();
+        payload1.setSource("services/inventory/v11/availability-zone/xxx");
+        payload1.setTarget("services/inventory/v11/cloud-region/xxx");
+
+        EdgePayload payload2 = new EdgePayload();
+        payload2.setSource("services/inventory/v11/image/xxx");
+        payload2.setTarget("services/inventory/v11/pserver/xxx");
+        
+        EdgePayload payload3 = new EdgePayload();
+        payload3.setSource("services/inventory/v11/allotted-resource/xxx");
+        payload3.setTarget("services/inventory/v11/instance-group/xxx");
+        
+        // Get edge types for node pair with a single possible edge between them
+        Set<String> typeList = schema.getValidRelationTypes("availability-zone", "cloud-region");
+        assertEquals(1, typeList.size());
+        assertTrue(typeList.contains("org.onap.relationships.inventory.BelongsTo"));
+        assertEquals(CrudServiceUtil.determineEdgeType(payload1, "v11"), "org.onap.relationships.inventory.BelongsTo");
+        
+        // Get edge types for node pair with no possible edge between them
+        typeList = schema.getValidRelationTypes("image", "pserver");
+        assertEquals(0, typeList.size());
+        typeList = schema.getValidRelationTypes("cloud-region", "availability-zone");
+        assertEquals(0, typeList.size());
+        
+        try {
+          // Should throw an exception here
+          CrudServiceUtil.determineEdgeType(payload2, "v11");
+          assertTrue(false);
+        }
+        catch (CrudException ex) {
+          System.out.println(ex.getMessage());
+        }
+        
+        typeList = schema.getValidRelationTypes("allotted-resource", "instance-group");
+        assertEquals(2, typeList.size());
+        assertTrue(typeList.contains("org.onap.relationships.inventory.TestEdge"));
+        assertTrue(typeList.contains("org.onap.relationships.inventory.MemberOf"));
+        
+        for (String type : typeList) {
+          System.out.println(type);
+        }
+        
+        try {
+          // Should throw an exception here
+          CrudServiceUtil.determineEdgeType(payload3, "v11");
+          assertTrue(false);
+        }
+        catch (CrudException ex) {
+          System.out.println(ex.getMessage());
+        }
+    }
 
     @Test
     public void getSchemaForVersionFail() throws Exception {
 
     @Test
     public void getSchemaForVersionFail() throws Exception {
index f38bd2c..4ff4eb2 100644 (file)
                        "delete-other-v": "NONE",
                        "SVC-INFRA": "NONE",
                        "prevent-delete": "!${direction}"
                        "delete-other-v": "NONE",
                        "SVC-INFRA": "NONE",
                        "prevent-delete": "!${direction}"
+               },
+      {
+                       "from": "allotted-resource",
+                       "to": "instance-group",
+                       "label": "org.onap.relationships.inventory.TestEdge",
+                       "direction": "OUT",
+                       "multiplicity": "MANY2MANY",
+                       "contains-other-v": "NONE",
+                       "delete-other-v": "NONE",
+                       "SVC-INFRA": "NONE",
+                       "prevent-delete": "NONE",
+                       "default": "true"
                }
        ]
 }
                }
        ]
 }