5b28e2867abcd2a7cf120221b42e4e5c8c409b46
[aai/gizmo.git] / src / main / java / org / openecomp / schema / RelationshipSchema.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Gizmo
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  *
22  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23  */
24 package org.openecomp.schema;
25
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;
32
33 import org.openecomp.crud.exception.CrudException;
34
35 import java.util.HashMap;
36 import java.util.Map;
37 import java.util.Set;
38 import javax.ws.rs.core.Response.Status;
39
40 public class RelationshipSchema {
41   private static final Gson gson = new GsonBuilder().create();
42
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";
49
50   /**
51    * key = source-node-type:target-node-type:relationship-type value = map of properties with name
52    * and type . Like propertyName:PropertyType
53    */
54   private HashMap<String, HashMap<String, Class<?>>> relations
55       = new HashMap<String, HashMap<String, Class<?>>>();
56   /**
57    * Hashmap of valid relationship types alongwith properrties.
58    */
59   private HashMap<String, HashMap<String, Class<?>>> relationTypes
60       = new HashMap<String, HashMap<String, Class<?>>>();
61
62
63   public RelationshipSchema(String json) throws CrudException {
64
65     JsonParser parser = new JsonParser();
66     try {
67       JsonObject root = parser.parse(json).getAsJsonObject();
68       JsonArray relationshipTypesArray = root.getAsJsonArray(SCHEMA_RELATIONSHIP_TYPES_ARRAY);
69       JsonArray relationsArray = root.getAsJsonArray(SCHEMA_RELATIONS_ARRAY);
70
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();
75
76
77         HashMap<String, Class<?>> props = new HashMap<String, Class<?>>();
78         Set<Map.Entry<String, JsonElement>> entries = obj.get(SCHEMA_RELATIONSHIP_PROPERTIES)
79             .getAsJsonObject().entrySet();
80
81         for (Map.Entry<String, JsonElement> entry : entries) {
82           props.put(entry.getKey(), resolveClass(entry.getValue().getAsString()));
83
84         }
85         relationTypes.put(type, props);
86
87       }
88
89       for (JsonElement item : relationsArray) {
90         JsonObject obj = item.getAsJsonObject();
91         // Parse the Source/Taget nodeTypes
92
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;
96
97
98         if (!relationTypes.containsKey(relationType)) {
99           throw new CrudException(SCHEMA_RELATIONSHIP_TYPE + ": " + relationType + " not found",
100               Status.BAD_REQUEST);
101         }
102
103         relations.put(key, relationTypes.get(relationType));
104       }
105     } catch (Exception e) {
106       throw new CrudException(e.getMessage(), Status.BAD_REQUEST);
107     }
108
109   }
110
111
112   public HashMap<String, Class<?>> lookupRelation(String key) {
113     return this.relations.get(key);
114   }
115
116   public HashMap<String, Class<?>> lookupRelationType(String type) {
117     return this.relationTypes.get(type);
118   }
119
120   public boolean isValidType(String type) {
121     return relationTypes.containsKey(type);
122   }
123
124   private Class<?> resolveClass(String type) throws CrudException, ClassNotFoundException {
125     Class<?> clazz = Class.forName(type);
126     validateClassTypes(clazz);
127     return clazz;
128   }
129
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);
134     }
135   }
136
137
138 }