X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fschema%2FRelationshipSchemaValidator.java;h=4f240712baad0e86579e08bd6c2c480274f549c1;hb=0c40bcde9facc109ceb8dabb91156df2b4fb4129;hp=15b17623e6e5887ef8d07cf0a739d43a832ae6f3;hpb=00832f054dd0c21492af531548e321ea25cdb8b4;p=aai%2Fgizmo.git diff --git a/src/main/java/org/onap/schema/RelationshipSchemaValidator.java b/src/main/java/org/onap/schema/RelationshipSchemaValidator.java index 15b1762..4f24071 100644 --- a/src/main/java/org/onap/schema/RelationshipSchemaValidator.java +++ b/src/main/java/org/onap/schema/RelationshipSchemaValidator.java @@ -20,8 +20,13 @@ */ package org.onap.schema; -import com.google.gson.JsonElement; -import com.google.gson.JsonNull; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.ws.rs.core.Response.Status; import org.onap.crud.entity.Edge; import org.onap.crud.entity.Vertex; @@ -31,24 +36,17 @@ import org.onap.crud.util.CrudServiceUtil; import org.onap.schema.OxmModelValidator.Metadata; import org.radeox.util.logging.Logger; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import javax.ws.rs.core.Response.Status; +import com.google.gson.JsonElement; +import com.google.gson.JsonNull; public class RelationshipSchemaValidator { - public static final String SOURCE_NODE = "source"; - public static final String TARGET_NODE = "target"; - - final static Pattern urlPattern = Pattern.compile("services/inventory/(.*)/(.*)/(.*)"); - - public static Map resolveCollectionfilter(String version, String type, - Map filter) - throws CrudException { - + public static final String SOURCE_NODE = "source"; + public static final String TARGET_NODE = "target"; + + final static Pattern urlPattern = Pattern.compile("services/inventory/(.*)/(.*)/(.*)"); + + public static Map resolveCollectionfilter(String version, String type,Map filter) throws CrudException { RelationshipSchema schema = EdgeRulesLoader.getSchemaForVersion(version); if (schema == null) { throw new CrudException("", Status.NOT_FOUND); @@ -64,19 +62,15 @@ public class RelationshipSchemaValidator { Object value = CrudServiceUtil.validateFieldType(filter.get(key), props.get(key)); result.put(key, value); } catch (Exception ex) { - // Skip any exceptions thrown while validating the filter - // key value + // Skip any exceptions thrown while validating the filter key value continue; } } } - return result; - } public static void validateType(String version, String type) throws CrudException { - RelationshipSchema schema = EdgeRulesLoader.getSchemaForVersion(version); if (!schema.isValidType(type)) { throw new CrudException("Invalid " + RelationshipSchema.SCHEMA_RELATIONSHIP_TYPE @@ -86,17 +80,14 @@ public class RelationshipSchemaValidator { } - public static Edge validateIncomingAddPayload(String version, String type, Vertex sourceNode, - Vertex targetNode, JsonElement properties) - throws CrudException { - EdgePayload payload = new EdgePayload(); - payload.setSource("services/inventory/" + version + "/" + sourceNode.getType() - + "/" + sourceNode.getId().get()); - payload.setTarget("services/inventory/" + version + "/" + targetNode.getType() - + "/" + targetNode.getId().get()); - payload.setType(type); - payload.setProperties(properties); - return validateIncomingAddPayload(version, type, payload); + public static Edge validateIncomingAddPayload(String version, String type, Vertex sourceNode, Vertex targetNode, JsonElement properties) throws CrudException { + EdgePayload payload = new EdgePayload(); + payload.setSource("services/inventory/" + version + "/" + sourceNode.getType() + "/" + sourceNode.getId().get()); + payload.setTarget("services/inventory/" + version + "/" + targetNode.getType() + "/" + targetNode.getId().get()); + payload.setType(type); + payload.setProperties(properties); + + return validateIncomingAddPayload(version, type, payload); } public static Edge validateIncomingAddPayload(String version, String type, EdgePayload payload) @@ -104,37 +95,35 @@ public class RelationshipSchemaValidator { RelationshipSchema schema = EdgeRulesLoader.getSchemaForVersion(version); try { - if (payload.getSource() == null || payload.getTarget() == null) { throw new CrudException("Source/Target not specified", Status.BAD_REQUEST); } Matcher sourceMatcher = urlPattern.matcher(payload.getSource()); Matcher targetMatcher = urlPattern.matcher(payload.getTarget()); - + if (!sourceMatcher.matches() || !targetMatcher.matches()) { - throw new CrudException("Invalid Source/Target Urls", Status.BAD_REQUEST); + throw new CrudException("Invalid Source/Target Urls", Status.BAD_REQUEST); } - + // create key based on source:target:relationshipType String sourceNodeType = sourceMatcher.group(2); String targetNodeType = targetMatcher.group(2); - + String sourceNodeId = sourceMatcher.group(3); String targetNodeId = targetMatcher.group(3); - + String key = sourceNodeType + ":" + targetNodeType + ":" + type; // find the validate the key from the schema Map> schemaObject = schema.lookupRelation(key); if (schemaObject == null) { - throw new CrudException("Invalid source/target/relationship type: " + key, - Status.BAD_REQUEST); + throw new CrudException("Invalid source/target/relationship type: " + key, Status.BAD_REQUEST); } Edge.Builder modelEdgeBuilder = new Edge.Builder(type); - + modelEdgeBuilder.source(new Vertex.Builder(sourceNodeType).id(sourceNodeId).build()); modelEdgeBuilder.target(new Vertex.Builder(targetNodeType).id(targetNodeId).build()); @@ -143,102 +132,90 @@ public class RelationshipSchemaValidator { return modelEdgeBuilder.build(); } catch (Exception ex) { - throw new CrudException(ex.getMessage(), Status.BAD_REQUEST); } - } public static Edge validateIncomingPatchPayload(Edge edge, String version, EdgePayload payload) - throws CrudException { - RelationshipSchema schema = EdgeRulesLoader.getSchemaForVersion(version); - - try { - if (payload.getSource() != null) { - Matcher sourceMatcher = urlPattern.matcher(payload.getSource()); - - if (!sourceMatcher.matches()) { - throw new CrudException("Invalid Target Urls", Status.BAD_REQUEST); - } - String sourceNodeId = sourceMatcher.group(3); - if (!sourceNodeId.equals(edge.getSource().getId().get())) { - throw new CrudException("Source can't be updated", Status.BAD_REQUEST); - } - } - - if (payload.getTarget() != null) { - Matcher targetMatcher = urlPattern.matcher(payload.getTarget()); - - if (!targetMatcher.matches()) { - throw new CrudException("Invalid Target Urls", Status.BAD_REQUEST); - } - String sourceNodeId = targetMatcher.group(3); - if (!sourceNodeId.equals(edge.getTarget().getId().get())) { - throw new CrudException("Target can't be updated", Status.BAD_REQUEST); - } - } - - // Remove the timestamp properties from the existing edge, as these should be managed by Champ. - Map existingProps = edge.getProperties(); - - if (existingProps.containsKey(Metadata.CREATED_TS.propertyName())) { - existingProps.remove(Metadata.CREATED_TS.propertyName()); + throws CrudException { + RelationshipSchema schema = EdgeRulesLoader.getSchemaForVersion(version); + + try { + if (payload.getSource() != null) { + Matcher sourceMatcher = urlPattern.matcher(payload.getSource()); + + if (!sourceMatcher.matches()) { + throw new CrudException("Invalid Source Urls", Status.BAD_REQUEST); + } + String sourceNodeId = sourceMatcher.group(3); + if (!sourceNodeId.equals(edge.getSource().getId().get())) { + throw new CrudException("Source can't be updated", Status.BAD_REQUEST); + } + } + + if (payload.getTarget() != null) { + Matcher targetMatcher = urlPattern.matcher(payload.getTarget()); + + if (!targetMatcher.matches()) { + throw new CrudException("Invalid Target Urls", Status.BAD_REQUEST); + } + String sourceNodeId = targetMatcher.group(3); + if (!sourceNodeId.equals(edge.getTarget().getId().get())) { + throw new CrudException("Target can't be updated", Status.BAD_REQUEST); + } + } + + // Remove the timestamp properties from the existing edge, as these should be managed by Champ. + Map existingProps = edge.getProperties(); + + if (existingProps.containsKey(Metadata.CREATED_TS.propertyName())) { + existingProps.remove(Metadata.CREATED_TS.propertyName()); + } + if (existingProps.containsKey(Metadata.UPDATED_TS.propertyName())) { + existingProps.remove(Metadata.UPDATED_TS.propertyName()); + } + + // create key based on source:target:relationshipType + String key = edge.getSource().getType() + ":" + edge.getTarget().getType() + + ":" + edge.getType(); + + // find the validate the key from the schema + Map> schemaObject = schema.lookupRelation(key); + + if (schemaObject == null) { + Logger.warn("key :" + key + + " not found in relationship schema . Skipping the schema validation"); + return edge; + } + + Set> entries = payload.getProperties().getAsJsonObject().entrySet(); + + for (Map.Entry entry : entries) { + if (!schemaObject.containsKey(entry.getKey())) { + throw new CrudException("Invalid property: " + entry.getKey(), Status.BAD_REQUEST); + } else if (entry.getValue() instanceof JsonNull && edge.getProperties().containsKey(entry.getKey())) { + edge.getProperties().remove(entry.getKey()); + } else if (!(entry.getValue() instanceof JsonNull)) { + Object value = CrudServiceUtil.validateFieldType(entry.getValue().getAsString(), schemaObject.get(entry.getKey())); + edge.getProperties().put(entry.getKey(), value); + } + } + + return edge; + } catch (Exception ex) { + throw new CrudException(ex.getMessage(), Status.BAD_REQUEST); } - if (existingProps.containsKey(Metadata.UPDATED_TS.propertyName())) { - existingProps.remove(Metadata.UPDATED_TS.propertyName()); - } - - // create key based on source:target:relationshipType - - String key = edge.getSource().getType() + ":" + edge.getTarget().getType() - + ":" + edge.getType(); - - // find the validate the key from the schema - Map> schemaObject = schema.lookupRelation(key); - - if (schemaObject == null) { - Logger.warn("key :" + key - + " not found in relationship schema . Skipping the schema validation"); - return edge; - } - - Set> entries = payload.getProperties() - .getAsJsonObject().entrySet(); - - for (Map.Entry entry : entries) { - - if (!schemaObject.containsKey(entry.getKey())) { - throw new CrudException("Invalid property: " + entry.getKey(), Status.BAD_REQUEST); - } else if (entry.getValue() instanceof JsonNull && edge.getProperties() - .containsKey(entry.getKey())) { - edge.getProperties().remove(entry.getKey()); - } else if (!(entry.getValue() instanceof JsonNull)) { - Object value = CrudServiceUtil.validateFieldType(entry.getValue().getAsString(), - schemaObject.get(entry.getKey())); - edge.getProperties().put(entry.getKey(), value); - } - - } - - return edge; - - } catch (Exception ex) { - - throw new CrudException(ex.getMessage(), Status.BAD_REQUEST); - } } - public static Edge validateIncomingUpdatePayload(Edge edge, String version, Vertex sourceNode, - Vertex targetNode, JsonElement properties) - throws CrudException { - EdgePayload payload = new EdgePayload(); - payload.setSource("services/inventory/" + version + "/" + sourceNode.getType() - + "/" + sourceNode.getId().get()); - payload.setTarget("services/inventory/" + version + "/" + targetNode.getType() - + "/" + targetNode.getId().get()); - payload.setType(edge.getType()); - payload.setProperties(properties); - return validateIncomingUpdatePayload(edge, version, payload); + + public static Edge validateIncomingUpdatePayload(Edge edge, String version, Vertex sourceNode, Vertex targetNode, JsonElement properties) + throws CrudException { + EdgePayload payload = new EdgePayload(); + payload.setSource("services/inventory/" + version + "/" + sourceNode.getType() + "/" + sourceNode.getId().get()); + payload.setTarget("services/inventory/" + version + "/" + targetNode.getType() + "/" + targetNode.getId().get()); + payload.setType(edge.getType()); + payload.setProperties(properties); + return validateIncomingUpdatePayload(edge, version, payload); } public static Edge validateIncomingUpdatePayload(Edge edge, String version, EdgePayload payload) @@ -246,32 +223,31 @@ public class RelationshipSchemaValidator { RelationshipSchema schema = EdgeRulesLoader.getSchemaForVersion(version); try { + if (payload.getSource() != null) { + Matcher sourceMatcher = urlPattern.matcher(payload.getSource()); + + if (!sourceMatcher.matches()) { + throw new CrudException("Invalid Source Urls", Status.BAD_REQUEST); + } + String sourceNodeId = sourceMatcher.group(3); + if (!sourceNodeId.equals(edge.getSource().getId().get())) { + throw new CrudException("Source can't be updated", Status.BAD_REQUEST); + } + } + + if (payload.getTarget() != null) { + Matcher targetMatcher = urlPattern.matcher(payload.getTarget()); + + if (!targetMatcher.matches()) { + throw new CrudException("Invalid Target Urls", Status.BAD_REQUEST); + } + String sourceNodeId = targetMatcher.group(3); + if (!sourceNodeId.equals(edge.getTarget().getId().get())) { + throw new CrudException("Target can't be updated", Status.BAD_REQUEST); + } + } - if (payload.getSource() != null) { - Matcher sourceMatcher = urlPattern.matcher(payload.getSource()); - - if (!sourceMatcher.matches()) { - throw new CrudException("Invalid Target Urls", Status.BAD_REQUEST); - } - String sourceNodeId = sourceMatcher.group(3); - if (!sourceNodeId.equals(edge.getSource().getId().get())) { - throw new CrudException("Source can't be updated", Status.BAD_REQUEST); - } - } - - if (payload.getTarget() != null) { - Matcher targetMatcher = urlPattern.matcher(payload.getTarget()); - - if (!targetMatcher.matches()) { - throw new CrudException("Invalid Target Urls", Status.BAD_REQUEST); - } - String sourceNodeId = targetMatcher.group(3); - if (!sourceNodeId.equals(edge.getTarget().getId().get())) { - throw new CrudException("Target can't be updated", Status.BAD_REQUEST); - } - } // create key based on source:target:relationshipType - String key = edge.getSource().getType() + ":" + edge.getTarget().getType() + ":" + edge.getType(); @@ -285,31 +261,23 @@ public class RelationshipSchemaValidator { } Edge.Builder updatedEdgeBuilder = new Edge.Builder(edge.getType()).id(edge.getId().get()); - - updatedEdgeBuilder - .source(new Vertex.Builder(edge.getSource().getType()).id(edge.getSource().getId() - .get()).build()); - updatedEdgeBuilder - .target(new Vertex.Builder(edge.getTarget().getType()).id(edge.getTarget().getId() - .get()).build()); + + updatedEdgeBuilder.source(new Vertex.Builder(edge.getSource().getType()).id(edge.getSource().getId().get()).build()); + updatedEdgeBuilder.target(new Vertex.Builder(edge.getTarget().getType()).id(edge.getTarget().getId().get()).build()); validateEdgeProps(updatedEdgeBuilder, payload.getProperties(), schemaObject); return updatedEdgeBuilder.build(); } catch (Exception ex) { - throw new CrudException(ex.getMessage(), Status.BAD_REQUEST); } } - private static void validateEdgeProps(Edge.Builder builder, JsonElement props, - Map> schemaObject) - throws CrudException { + private static void validateEdgeProps(Edge.Builder builder, JsonElement props, Map> schemaObject) throws CrudException { Set> entries = props.getAsJsonObject().entrySet(); for (Map.Entry entry : entries) { - if (!schemaObject.containsKey(entry.getKey())) { throw new CrudException("Invalid property: " + entry.getKey(), Status.BAD_REQUEST); } else { @@ -317,13 +285,10 @@ public class RelationshipSchemaValidator { schemaObject.get(entry.getKey())); builder.property(entry.getKey(), value); } - } - } public static Edge validateOutgoingPayload(String version, Edge edge) throws CrudException { - Edge.Builder modelEdgeBuilder = new Edge.Builder(edge.getType()).id(edge.getId() .get()).source(edge.getSource()) .target(edge.getTarget()); @@ -346,16 +311,15 @@ public class RelationshipSchemaValidator { } return modelEdgeBuilder.build(); } - - + public static String vertexTypeFromUri(String uri) throws CrudException { - - Matcher matcher = urlPattern.matcher(uri); - - if (!matcher.matches()) { - throw new CrudException("Invalid Source/Target Urls", Status.BAD_REQUEST); - } - return matcher.group(2); + Matcher matcher = urlPattern.matcher(uri); + + if (!matcher.matches()) { + throw new CrudException("Invalid Source/Target Urls", Status.BAD_REQUEST); + } + + return matcher.group(2); } }