Update license date and text
[aai/gizmo.git] / src / main / java / org / onap / schema / RelationshipSchema.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.schema;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25
26 import org.codehaus.jackson.map.ObjectMapper;
27 import org.onap.crud.exception.CrudException;
28
29 import java.io.IOException;
30 import java.util.*;
31 import java.util.stream.Collectors;
32 import javax.ws.rs.core.Response.Status;
33
34
35 public class RelationshipSchema {
36   private static final Gson gson = new GsonBuilder().create();
37
38   public static final String SCHEMA_SOURCE_NODE_TYPE = "from";
39   public static final String SCHEMA_TARGET_NODE_TYPE = "to";
40   public static final String SCHEMA_RELATIONSHIP_TYPE = "label";
41   public static final String SCHEMA_RULES_ARRAY = "rules";
42
43
44   private Map<String, Map<String, Class<?>>> relations = new HashMap<>();
45   /**
46    * Hashmap of valid relationship types along with properties.
47    */
48   private Map<String, Map<String, Class<?>>> relationTypes  = new HashMap<>();
49
50
51   public RelationshipSchema(List<String> jsonStrings) throws CrudException, IOException {
52     String edgeRules = jsonStrings.get(0);
53     String props = jsonStrings.get(1);
54
55     HashMap<String, ArrayList<LinkedHashMap<String, String>>> rules = new ObjectMapper().readValue(edgeRules, HashMap.class);
56     HashMap<String, String> properties = new ObjectMapper().readValue(props, HashMap.class);
57     Map<String, Class<?>> edgeProps = properties.entrySet().stream().collect(Collectors.toMap(p -> p.getKey(), p -> {
58       try {
59         return resolveClass(p.getValue());
60       } catch (CrudException | ClassNotFoundException e) {
61         e.printStackTrace();
62       }
63       return null;
64     }));
65
66     rules.get(SCHEMA_RULES_ARRAY).forEach(l -> {
67       relationTypes.put(l.get(SCHEMA_RELATIONSHIP_TYPE), edgeProps);
68       relations.put(buildRelation(l.get(SCHEMA_SOURCE_NODE_TYPE), l.get(SCHEMA_TARGET_NODE_TYPE), l.get(SCHEMA_RELATIONSHIP_TYPE)), edgeProps);
69     });
70   }
71
72
73
74   public Map<String, Class<?>> lookupRelation(String key) {
75     return this.relations.get(key);
76   }
77
78   public Map<String, Class<?>> lookupRelationType(String type) {
79     return this.relationTypes.get(type);
80   }
81
82   public boolean isValidType(String type) {
83     return relationTypes.containsKey(type);
84   }
85
86
87   private String buildRelation(String source, String target, String relation){
88     return source + ":" + target + ":" + relation;
89   }
90
91   private Class<?> resolveClass(String type) throws CrudException, ClassNotFoundException {
92     Class<?> clazz = Class.forName(type);
93     validateClassTypes(clazz);
94     return clazz;
95   }
96
97   private void validateClassTypes(Class<?> clazz) throws CrudException {
98     if (!clazz.isAssignableFrom(Integer.class) && !clazz.isAssignableFrom(Double.class)
99             && !clazz.isAssignableFrom(Boolean.class) && !clazz.isAssignableFrom(String.class)) {
100       throw new CrudException("", Status.BAD_REQUEST);
101     }
102   }
103 }
104
105