fc91662ea23abce493fa45f512ed279945f51735
[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.common.collect.Multimap;
24 import org.codehaus.jackson.map.ObjectMapper;
25 import org.onap.aai.edges.EdgeRule;
26 import org.onap.crud.exception.CrudException;
27
28 import java.io.IOException;
29 import java.util.*;
30 import java.util.stream.Collectors;
31 import javax.ws.rs.core.Response.Status;
32
33
34 public class RelationshipSchema {
35
36   public static final String SCHEMA_RELATIONSHIP_TYPE = "label";
37
38
39   private Map<String, Map<String, Class<?>>> relations = new HashMap<>();
40   /**
41    * Hashmap of valid relationship types along with properties.
42    */
43   private Map<String, Map<String, Class<?>>> relationTypes  = new HashMap<>();
44
45   public RelationshipSchema( Multimap<String, EdgeRule> rules, String props) throws CrudException, IOException {
46     HashMap<String, String> properties = new ObjectMapper().readValue(props, HashMap.class);
47     Map<String, Class<?>> edgeProps = properties.entrySet().stream().collect(Collectors.toMap(p -> p.getKey(), p -> {
48       try {
49         return resolveClass(p.getValue());
50       } catch (CrudException | ClassNotFoundException e) {
51         e.printStackTrace();
52       }
53       return null;
54     }));
55
56     rules.entries ().forEach ( (kv) -> {
57       relationTypes.put(kv.getValue ().getLabel (), edgeProps);
58       relations.put (buildRelation ( kv.getValue ().getFrom (), kv.getValue ().getTo (), kv.getValue ().getLabel ()), edgeProps);
59     });
60   }
61
62   public Map<String, Class<?>> lookupRelation(String key) {
63     return this.relations.get(key);
64   }
65
66   public Map<String, Class<?>> lookupRelationType(String type) {
67     return this.relationTypes.get(type);
68   }
69
70   public boolean isValidType(String type) {
71     return relationTypes.containsKey(type);
72   }
73
74
75   private String buildRelation(String source, String target, String relation){
76     return source + ":" + target + ":" + relation;
77   }
78
79   private Class<?> resolveClass(String type) throws CrudException, ClassNotFoundException {
80     Class<?> clazz = Class.forName(type);
81     validateClassTypes(clazz);
82     return clazz;
83   }
84
85   private void validateClassTypes(Class<?> clazz) throws CrudException {
86     if (!clazz.isAssignableFrom(Integer.class) && !clazz.isAssignableFrom(Double.class)
87             && !clazz.isAssignableFrom(Boolean.class) && !clazz.isAssignableFrom(String.class)) {
88       throw new CrudException("", Status.BAD_REQUEST);
89     }
90   }
91 }
92
93