X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fschema%2FRelationshipSchema.java;h=47b5b99ebf027ebea117561a5697c8b8bca7cf5e;hb=0c40bcde9facc109ceb8dabb91156df2b4fb4129;hp=fc91662ea23abce493fa45f512ed279945f51735;hpb=00832f054dd0c21492af531548e321ea25cdb8b4;p=aai%2Fgizmo.git diff --git a/src/main/java/org/onap/schema/RelationshipSchema.java b/src/main/java/org/onap/schema/RelationshipSchema.java index fc91662..47b5b99 100644 --- a/src/main/java/org/onap/schema/RelationshipSchema.java +++ b/src/main/java/org/onap/schema/RelationshipSchema.java @@ -20,30 +20,52 @@ */ package org.onap.schema; -import com.google.common.collect.Multimap; -import org.codehaus.jackson.map.ObjectMapper; -import org.onap.aai.edges.EdgeRule; -import org.onap.crud.exception.CrudException; import java.io.IOException; -import java.util.*; +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; +import org.onap.aai.edges.EdgeRule; +import org.onap.crud.exception.CrudException; +import com.google.common.collect.Multimap; public class RelationshipSchema { public static final String SCHEMA_RELATIONSHIP_TYPE = "label"; - private Map>> relations = new HashMap<>(); /** * Hashmap of valid relationship types along with properties. */ private Map>> relationTypes = new HashMap<>(); - public RelationshipSchema( Multimap rules, String props) throws CrudException, IOException { + // A map storing the list of valid edge types for a source/target pair + private Map> edgeTypesForNodePair = new HashMap<>(); + + + @SuppressWarnings("unchecked") + public RelationshipSchema(Multimap rules, String props) throws CrudException, IOException { HashMap properties = new ObjectMapper().readValue(props, HashMap.class); + + // hold the true values of the edge rules by key - convert to java 8 + for (EdgeRule rule : rules.values()) { + + String nodePairKey = buildNodePairKey(rule.getFrom(), rule.getTo()); + if (edgeTypesForNodePair.get(nodePairKey) == null) { + Set typeSet = new HashSet(); + typeSet.add(rule.getLabel()); + edgeTypesForNodePair.put(nodePairKey, typeSet); + } + else { + edgeTypesForNodePair.get(nodePairKey).add(rule.getLabel()); + } + } + Map> edgeProps = properties.entrySet().stream().collect(Collectors.toMap(p -> p.getKey(), p -> { try { return resolveClass(p.getValue()); @@ -71,10 +93,23 @@ public class RelationshipSchema { return relationTypes.containsKey(type); } + public Set getValidRelationTypes(String source, String target) { + Set typeList = edgeTypesForNodePair.get(buildNodePairKey(source, target)); - private String buildRelation(String source, String target, String relation){ + if (typeList == null) { + return new HashSet(); + } + + 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); @@ -88,6 +123,4 @@ public class RelationshipSchema { throw new CrudException("", Status.BAD_REQUEST); } } -} - - +} \ No newline at end of file