From 7e69be504213aa92893fd3354a767128b3a583f1 Mon Sep 17 00:00:00 2001 From: Shwetank Dave Date: Tue, 22 Aug 2017 10:36:30 -0400 Subject: Using DbEdgeRules.json files from aai-core jar. Before we were aai_relationship_vxx.json files. Now using the edge rules directly from aai-core jar. Issue-ID: AAI-21 Change-Id: Id62494caabc75bc29e4f3558268ec78897946937 Signed-off-by: Shwetank Dave --- .../org/openecomp/schema/RelationshipSchema.java | 108 ++++++++------------- 1 file changed, 39 insertions(+), 69 deletions(-) (limited to 'src/main/java/org/openecomp/schema/RelationshipSchema.java') diff --git a/src/main/java/org/openecomp/schema/RelationshipSchema.java b/src/main/java/org/openecomp/schema/RelationshipSchema.java index 5b28e28..a8d101d 100644 --- a/src/main/java/org/openecomp/schema/RelationshipSchema.java +++ b/src/main/java/org/openecomp/schema/RelationshipSchema.java @@ -25,95 +25,60 @@ package org.openecomp.schema; import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; +import org.codehaus.jackson.map.ObjectMapper; import org.openecomp.crud.exception.CrudException; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; +import java.io.IOException; +import java.util.*; +import java.util.stream.Collectors; import javax.ws.rs.core.Response.Status; + public class RelationshipSchema { private static final Gson gson = new GsonBuilder().create(); - public static final String SCHEMA_SOURCE_NODE_TYPE = "source-node-type"; - public static final String SCHEMA_TARGET_NODE_TYPE = "target-node-type"; - public static final String SCHEMA_RELATIONSHIP_TYPE = "relationship-type"; - public static final String SCHEMA_RELATIONSHIP_TYPES_ARRAY = "relationship-types"; - public static final String SCHEMA_RELATIONSHIP_PROPERTIES = "properties"; - public static final String SCHEMA_RELATIONS_ARRAY = "relations"; + 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_RULES_ARRAY = "rules"; + + private Map>> relations = new HashMap<>(); /** - * key = source-node-type:target-node-type:relationship-type value = map of properties with name - * and type . Like propertyName:PropertyType - */ - private HashMap>> relations - = new HashMap>>(); - /** - * Hashmap of valid relationship types alongwith properrties. + * Hashmap of valid relationship types along with properties. */ - private HashMap>> relationTypes - = new HashMap>>(); - - - public RelationshipSchema(String json) throws CrudException { + private Map>> relationTypes = new HashMap<>(); - JsonParser parser = new JsonParser(); - try { - JsonObject root = parser.parse(json).getAsJsonObject(); - JsonArray relationshipTypesArray = root.getAsJsonArray(SCHEMA_RELATIONSHIP_TYPES_ARRAY); - JsonArray relationsArray = root.getAsJsonArray(SCHEMA_RELATIONS_ARRAY); - //First load all the relationship-types - for (JsonElement item : relationshipTypesArray) { - JsonObject obj = item.getAsJsonObject(); - String type = obj.get(SCHEMA_RELATIONSHIP_TYPE).getAsString(); - - - HashMap> props = new HashMap>(); - Set> entries = obj.get(SCHEMA_RELATIONSHIP_PROPERTIES) - .getAsJsonObject().entrySet(); - - for (Map.Entry entry : entries) { - props.put(entry.getKey(), resolveClass(entry.getValue().getAsString())); - - } - relationTypes.put(type, props); + 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); + HashMap properties = new ObjectMapper().readValue(props, HashMap.class); + Map> edgeProps = properties.entrySet().stream().collect(Collectors.toMap(p -> p.getKey(), p -> { + try { + return resolveClass(p.getValue()); + } catch (CrudException | ClassNotFoundException e) { + e.printStackTrace(); } + return null; + })); - for (JsonElement item : relationsArray) { - JsonObject obj = item.getAsJsonObject(); - // Parse the Source/Taget nodeTypes - - String relationType = obj.get(SCHEMA_RELATIONSHIP_TYPE).getAsString(); - String key = obj.get(SCHEMA_SOURCE_NODE_TYPE).getAsString() + ":" - + obj.get(SCHEMA_TARGET_NODE_TYPE).getAsString() + ":" + relationType; - - - if (!relationTypes.containsKey(relationType)) { - throw new CrudException(SCHEMA_RELATIONSHIP_TYPE + ": " + relationType + " not found", - Status.BAD_REQUEST); - } - - relations.put(key, relationTypes.get(relationType)); - } - } catch (Exception e) { - throw new CrudException(e.getMessage(), Status.BAD_REQUEST); - } - + 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); + }); } - public HashMap> lookupRelation(String key) { + + public Map> lookupRelation(String key) { return this.relations.get(key); } - public HashMap> lookupRelationType(String type) { + public Map> lookupRelationType(String type) { return this.relationTypes.get(type); } @@ -121,6 +86,11 @@ public class RelationshipSchema { return relationTypes.containsKey(type); } + + private String buildRelation(String source, String target, String relation){ + return source + ":" + target + ":" + relation; + } + private Class resolveClass(String type) throws CrudException, ClassNotFoundException { Class clazz = Class.forName(type); validateClassTypes(clazz); @@ -129,10 +99,10 @@ public class RelationshipSchema { private void validateClassTypes(Class clazz) throws CrudException { if (!clazz.isAssignableFrom(Integer.class) && !clazz.isAssignableFrom(Double.class) - && !clazz.isAssignableFrom(Boolean.class) && !clazz.isAssignableFrom(String.class)) { + && !clazz.isAssignableFrom(Boolean.class) && !clazz.isAssignableFrom(String.class)) { throw new CrudException("", Status.BAD_REQUEST); } } +} -} -- cgit 1.2.3-korg