diff options
author | sblimkie <steven.blimkie@amdocs.com> | 2018-07-19 16:03:44 -0400 |
---|---|---|
committer | sblimkie <steven.blimkie@amdocs.com> | 2018-07-19 16:08:20 -0400 |
commit | 3bc6a702f2d3d8710c7aaa94cdc8c0ccf3deb759 (patch) | |
tree | 9703ebe15777acc73ad118520f6b1a219f60e107 /src/main | |
parent | 194adee686ebb90488f739f2c637f6cb3def94d5 (diff) |
Auto-resolve edge type
Auto-resolve edge type based on EdgeRules file.
Change-Id: Ic6de47f5172bc410efcdd5f08c1ea5c4f822610e
Issue-ID: AAI-1396
Signed-off-by: sblimkie <steven.blimkie@amdocs.com>
Diffstat (limited to 'src/main')
8 files changed, 178 insertions, 61 deletions
diff --git a/src/main/java/org/onap/crud/dao/GraphDao.java b/src/main/java/org/onap/crud/dao/GraphDao.java index 6c1bca9..7cb3d4c 100644 --- a/src/main/java/org/onap/crud/dao/GraphDao.java +++ b/src/main/java/org/onap/crud/dao/GraphDao.java @@ -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 + * @param txid + * - a DB transaction ID to use (if null, no transactionId is used) * @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 @@ -89,7 +91,7 @@ public interface GraphDao { * @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. @@ -172,7 +174,7 @@ public interface GraphDao { * - 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(); @@ -193,7 +195,9 @@ public interface GraphDao { 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; } diff --git a/src/main/java/org/onap/crud/dao/champ/ChampDao.java b/src/main/java/org/onap/crud/dao/champ/ChampDao.java index fd5de57..81980cc 100644 --- a/src/main/java/org/onap/crud/dao/champ/ChampDao.java +++ b/src/main/java/org/onap/crud/dao/champ/ChampDao.java @@ -141,26 +141,39 @@ public class ChampDao implements GraphDao { } @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 @@ -343,7 +356,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); @@ -512,7 +525,7 @@ public class ChampDao implements GraphDao { } @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); @@ -524,19 +537,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 @@ -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"); } } + + @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; diff --git a/src/main/java/org/onap/crud/service/AbstractGraphDataService.java b/src/main/java/org/onap/crud/service/AbstractGraphDataService.java index a5b52bd..59046ab 100644 --- a/src/main/java/org/onap/crud/service/AbstractGraphDataService.java +++ b/src/main/java/org/onap/crud/service/AbstractGraphDataService.java @@ -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); - 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)); @@ -117,8 +117,7 @@ public abstract class AbstractGraphDataService { 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()); } + // 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(), - 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(), - 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")) { - 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(), - 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(), - 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 { - 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); } @@ -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 void deleteBulkEdge(String id, String version, String type, String dbTransId) throws CrudException; + protected abstract void deleteBulkEdge(String id, String version, String dbTransId) throws CrudException; } diff --git a/src/main/java/org/onap/crud/service/CrudAsyncGraphDataService.java b/src/main/java/org/onap/crud/service/CrudAsyncGraphDataService.java index aa70a66..8d147eb 100644 --- a/src/main/java/org/onap/crud/service/CrudAsyncGraphDataService.java +++ b/src/main/java/org/onap/crud/service/CrudAsyncGraphDataService.java @@ -414,7 +414,18 @@ public class CrudAsyncGraphDataService extends AbstractGraphDataService { } @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); diff --git a/src/main/java/org/onap/crud/service/CrudGraphDataService.java b/src/main/java/org/onap/crud/service/CrudGraphDataService.java index 9ff48c7..ae2d294 100644 --- a/src/main/java/org/onap/crud/service/CrudGraphDataService.java +++ b/src/main/java/org/onap/crud/service/CrudGraphDataService.java @@ -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, - 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, - daoForGet.getVertexEdges(EdgePayloadUtil.getVertexNodeId(payload.getTarget()), null)); + daoForGet.getVertexEdges(EdgePayloadUtil.getVertexNodeId(payload.getTarget()), null, null)); 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); - dao.deleteEdge(id, type); + dao.deleteEdge(id); 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, - 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, - 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); @@ -213,7 +213,7 @@ public class CrudGraphDataService extends AbstractGraphDataService { } @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); } } diff --git a/src/main/java/org/onap/crud/service/CrudRestService.java b/src/main/java/org/onap/crud/service/CrudRestService.java index 25ab43a..3cfc657 100644 --- a/src/main/java/org/onap/crud/service/CrudRestService.java +++ b/src/main/java/org/onap/crud/service/CrudRestService.java @@ -504,10 +504,6 @@ public class CrudRestService { 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); } @@ -697,8 +693,9 @@ public class CrudRestService { } 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 { diff --git a/src/main/java/org/onap/crud/util/CrudServiceUtil.java b/src/main/java/org/onap/crud/util/CrudServiceUtil.java index ed5fe53..5eeb38e 100644 --- a/src/main/java/org/onap/crud/util/CrudServiceUtil.java +++ b/src/main/java/org/onap/crud/util/CrudServiceUtil.java @@ -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 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.RelationshipSchema; + import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonPrimitive; @@ -138,4 +142,25 @@ public class CrudServiceUtil { 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(); + } + } diff --git a/src/main/java/org/onap/schema/RelationshipSchema.java b/src/main/java/org/onap/schema/RelationshipSchema.java index 2aab7a9..0cf419e 100644 --- a/src/main/java/org/onap/schema/RelationshipSchema.java +++ b/src/main/java/org/onap/schema/RelationshipSchema.java @@ -23,7 +23,9 @@ package org.onap.schema; import java.io.IOException; import java.util.HashMap; +import java.util.HashSet; 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; @@ -47,6 +49,10 @@ public class RelationshipSchema { 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 { @@ -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); + + 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 -> { @@ -106,10 +122,23 @@ public class RelationshipSchema { 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 buildNodePairKey(String source, String target) { + return source + ":" + target; + } private Class<?> resolveClass(String type) throws CrudException, ClassNotFoundException { Class<?> clazz = Class.forName(type); |