e6a40b5ca965b9bdbc70f466d7449636c13e4ace
[aai/gizmo.git] / src / main / java / org / onap / 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.onap.schema;
25
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28
29 import org.codehaus.jackson.map.ObjectMapper;
30 import org.onap.crud.exception.CrudException;
31
32 import java.io.IOException;
33 import java.util.*;
34 import java.util.stream.Collectors;
35 import javax.ws.rs.core.Response.Status;
36
37
38 public class RelationshipSchema {
39   private static final Gson gson = new GsonBuilder().create();
40
41   public static final String SCHEMA_SOURCE_NODE_TYPE = "from";
42   public static final String SCHEMA_TARGET_NODE_TYPE = "to";
43   public static final String SCHEMA_RELATIONSHIP_TYPE = "label";
44   public static final String SCHEMA_RULES_ARRAY = "rules";
45
46
47   private Map<String, Map<String, Class<?>>> relations = new HashMap<>();
48   /**
49    * Hashmap of valid relationship types along with properties.
50    */
51   private Map<String, Map<String, Class<?>>> relationTypes  = new HashMap<>();
52
53
54   public RelationshipSchema(List<String> jsonStrings) throws CrudException, IOException {
55     String edgeRules = jsonStrings.get(0);
56     String props = jsonStrings.get(1);
57
58     HashMap<String, ArrayList<LinkedHashMap<String, String>>> rules = new ObjectMapper().readValue(edgeRules, HashMap.class);
59     HashMap<String, String> properties = new ObjectMapper().readValue(props, HashMap.class);
60     Map<String, Class<?>> edgeProps = properties.entrySet().stream().collect(Collectors.toMap(p -> p.getKey(), p -> {
61       try {
62         return resolveClass(p.getValue());
63       } catch (CrudException | ClassNotFoundException e) {
64         e.printStackTrace();
65       }
66       return null;
67     }));
68
69     rules.get(SCHEMA_RULES_ARRAY).forEach(l -> {
70       relationTypes.put(l.get(SCHEMA_RELATIONSHIP_TYPE), edgeProps);
71       relations.put(buildRelation(l.get(SCHEMA_SOURCE_NODE_TYPE), l.get(SCHEMA_TARGET_NODE_TYPE), l.get(SCHEMA_RELATIONSHIP_TYPE)), edgeProps);
72     });
73   }
74
75
76
77   public Map<String, Class<?>> lookupRelation(String key) {
78     return this.relations.get(key);
79   }
80
81   public Map<String, Class<?>> lookupRelationType(String type) {
82     return this.relationTypes.get(type);
83   }
84
85   public boolean isValidType(String type) {
86     return relationTypes.containsKey(type);
87   }
88
89
90   private String buildRelation(String source, String target, String relation){
91     return source + ":" + target + ":" + relation;
92   }
93
94   private Class<?> resolveClass(String type) throws CrudException, ClassNotFoundException {
95     Class<?> clazz = Class.forName(type);
96     validateClassTypes(clazz);
97     return clazz;
98   }
99
100   private void validateClassTypes(Class<?> clazz) throws CrudException {
101     if (!clazz.isAssignableFrom(Integer.class) && !clazz.isAssignableFrom(Double.class)
102             && !clazz.isAssignableFrom(Boolean.class) && !clazz.isAssignableFrom(String.class)) {
103       throw new CrudException("", Status.BAD_REQUEST);
104     }
105   }
106 }
107
108