X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fschema%2FRelationshipSchema.java;h=2aab7a96e13b33c8b1b56502e06eaa86aa49a75b;hb=d10a218c76633374f083f7a2802c198e93a6abae;hp=557c374862ca2def1aa24af5fd246edd237054fc;hpb=b6ec637f5ee03c573855431e65fcb6ab0f321851;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 557c374..2aab7a9 100644 --- a/src/main/java/org/onap/schema/RelationshipSchema.java +++ b/src/main/java/org/onap/schema/RelationshipSchema.java @@ -20,40 +20,44 @@ */ package org.onap.schema; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; - -import org.codehaus.jackson.map.ObjectMapper; -import org.onap.crud.exception.CrudException; import java.io.IOException; -import java.util.*; +import java.util.HashMap; +import java.util.Map; 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 { - private static final Gson gson = new GsonBuilder().create(); public static final String SCHEMA_SOURCE_NODE_TYPE = "from"; public static final String SCHEMA_TARGET_NODE_TYPE = "to"; public static final String SCHEMA_RELATIONSHIP_TYPE = "label"; + public static final String SCHEMA_MULTIPLICITY_TYPE = "multiplicity"; public static final String SCHEMA_RULES_ARRAY = "rules"; - private Map>> relations = new HashMap<>(); /** * Hashmap of valid relationship types along with properties. */ private Map>> relationTypes = new HashMap<>(); + private Map relationshipRules = new HashMap<>(); - public RelationshipSchema(List jsonStrings) throws CrudException, IOException { - String edgeRules = jsonStrings.get(0); - String props = jsonStrings.get(1); - - HashMap>> rules = new ObjectMapper().readValue(edgeRules, HashMap.class); + @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 key = buildRelation(rule.getFrom(), rule.getTo(), rule.getLabel()); + relationshipRules.put(key, rule); + } + Map> edgeProps = properties.entrySet().stream().collect(Collectors.toMap(p -> p.getKey(), p -> { try { return resolveClass(p.getValue()); @@ -63,18 +67,37 @@ public class RelationshipSchema { return null; })); - rules.get(SCHEMA_RULES_ARRAY).forEach(l -> { - relationTypes.put(l.get(SCHEMA_RELATIONSHIP_TYPE), edgeProps); - relations.put(buildRelation(l.get(SCHEMA_SOURCE_NODE_TYPE), l.get(SCHEMA_TARGET_NODE_TYPE), l.get(SCHEMA_RELATIONSHIP_TYPE)), edgeProps); + rules.entries ().forEach ( (kv) -> { + relationTypes.put(kv.getValue ().getLabel (), edgeProps); + relations.put (buildRelation ( kv.getValue ().getFrom (), kv.getValue ().getTo (), kv.getValue ().getLabel ()), edgeProps); }); } - - public Map> lookupRelation(String key) { return this.relations.get(key); } + /** + * Extract the multiplicity type from the Edge rules + * + * @param key + * @return + * @throws CrudException + */ + public String lookupRelationMultiplicity(String key) throws CrudException { + EdgeRule edgeRule = relationshipRules.get(key); + + if (edgeRule == null) { + throw new CrudException("Invalid source/target/relationship type: " + key, Status.BAD_REQUEST); + } + + if (edgeRule.getMultiplicityRule() != null) { + return edgeRule.getMultiplicityRule().toString(); + } + + return null; + } + public Map> lookupRelationType(String type) { return this.relationTypes.get(type); } @@ -84,7 +107,7 @@ public class RelationshipSchema { } - private String buildRelation(String source, String target, String relation){ + private String buildRelation(String source, String target, String relation) { return source + ":" + target + ":" + relation; } @@ -100,6 +123,4 @@ public class RelationshipSchema { throw new CrudException("", Status.BAD_REQUEST); } } -} - - +} \ No newline at end of file