2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
22 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.openecomp.schema;
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28 import com.google.gson.JsonArray;
29 import com.google.gson.JsonElement;
30 import com.google.gson.JsonObject;
31 import com.google.gson.JsonParser;
33 import org.openecomp.crud.exception.CrudException;
35 import java.util.HashMap;
38 import javax.ws.rs.core.Response.Status;
40 public class RelationshipSchema {
41 private static final Gson gson = new GsonBuilder().create();
43 public static final String SCHEMA_SOURCE_NODE_TYPE = "source-node-type";
44 public static final String SCHEMA_TARGET_NODE_TYPE = "target-node-type";
45 public static final String SCHEMA_RELATIONSHIP_TYPE = "relationship-type";
46 public static final String SCHEMA_RELATIONSHIP_TYPES_ARRAY = "relationship-types";
47 public static final String SCHEMA_RELATIONSHIP_PROPERTIES = "properties";
48 public static final String SCHEMA_RELATIONS_ARRAY = "relations";
51 * key = source-node-type:target-node-type:relationship-type value = map of properties with name
52 * and type . Like propertyName:PropertyType
54 private HashMap<String, HashMap<String, Class<?>>> relations
55 = new HashMap<String, HashMap<String, Class<?>>>();
57 * Hashmap of valid relationship types alongwith properrties.
59 private HashMap<String, HashMap<String, Class<?>>> relationTypes
60 = new HashMap<String, HashMap<String, Class<?>>>();
63 public RelationshipSchema(String json) throws CrudException {
65 JsonParser parser = new JsonParser();
67 JsonObject root = parser.parse(json).getAsJsonObject();
68 JsonArray relationshipTypesArray = root.getAsJsonArray(SCHEMA_RELATIONSHIP_TYPES_ARRAY);
69 JsonArray relationsArray = root.getAsJsonArray(SCHEMA_RELATIONS_ARRAY);
71 //First load all the relationship-types
72 for (JsonElement item : relationshipTypesArray) {
73 JsonObject obj = item.getAsJsonObject();
74 String type = obj.get(SCHEMA_RELATIONSHIP_TYPE).getAsString();
77 HashMap<String, Class<?>> props = new HashMap<String, Class<?>>();
78 Set<Map.Entry<String, JsonElement>> entries = obj.get(SCHEMA_RELATIONSHIP_PROPERTIES)
79 .getAsJsonObject().entrySet();
81 for (Map.Entry<String, JsonElement> entry : entries) {
82 props.put(entry.getKey(), resolveClass(entry.getValue().getAsString()));
85 relationTypes.put(type, props);
89 for (JsonElement item : relationsArray) {
90 JsonObject obj = item.getAsJsonObject();
91 // Parse the Source/Taget nodeTypes
93 String relationType = obj.get(SCHEMA_RELATIONSHIP_TYPE).getAsString();
94 String key = obj.get(SCHEMA_SOURCE_NODE_TYPE).getAsString() + ":"
95 + obj.get(SCHEMA_TARGET_NODE_TYPE).getAsString() + ":" + relationType;
98 if (!relationTypes.containsKey(relationType)) {
99 throw new CrudException(SCHEMA_RELATIONSHIP_TYPE + ": " + relationType + " not found",
103 relations.put(key, relationTypes.get(relationType));
105 } catch (Exception e) {
106 throw new CrudException(e.getMessage(), Status.BAD_REQUEST);
112 public HashMap<String, Class<?>> lookupRelation(String key) {
113 return this.relations.get(key);
116 public HashMap<String, Class<?>> lookupRelationType(String type) {
117 return this.relationTypes.get(type);
120 public boolean isValidType(String type) {
121 return relationTypes.containsKey(type);
124 private Class<?> resolveClass(String type) throws CrudException, ClassNotFoundException {
125 Class<?> clazz = Class.forName(type);
126 validateClassTypes(clazz);
130 private void validateClassTypes(Class<?> clazz) throws CrudException {
131 if (!clazz.isAssignableFrom(Integer.class) && !clazz.isAssignableFrom(Double.class)
132 && !clazz.isAssignableFrom(Boolean.class) && !clazz.isAssignableFrom(String.class)) {
133 throw new CrudException("", Status.BAD_REQUEST);