Remove Multiplicity feature
[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
24 import java.io.IOException;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.Map;
28 import java.util.Set;
29 import java.util.stream.Collectors;
30 import javax.ws.rs.core.Response.Status;
31 import org.codehaus.jackson.map.ObjectMapper;
32 import org.onap.aai.edges.EdgeRule;
33 import org.onap.crud.exception.CrudException;
34 import com.google.common.collect.Multimap;
35
36
37 public class RelationshipSchema {
38
39   public static final String SCHEMA_RELATIONSHIP_TYPE = "label";
40
41   private Map<String, Map<String, Class<?>>> relations = new HashMap<>();
42   /**
43    * Hashmap of valid relationship types along with properties.
44    */
45   private Map<String, Map<String, Class<?>>> relationTypes  = new HashMap<>();
46
47   // A map storing the list of valid edge types for a source/target pair
48   private Map<String, Set<String>> edgeTypesForNodePair = new HashMap<>();
49   
50
51   @SuppressWarnings("unchecked")
52   public RelationshipSchema(Multimap<String, EdgeRule> rules, String props) throws CrudException, IOException {
53     HashMap<String, String> properties = new ObjectMapper().readValue(props, HashMap.class);
54
55     // hold the true values of the edge rules by key - convert to java 8
56     for (EdgeRule rule : rules.values()) {
57       
58       String nodePairKey = buildNodePairKey(rule.getFrom(), rule.getTo());
59       if (edgeTypesForNodePair.get(nodePairKey) == null) {
60         Set<String> typeSet = new HashSet<String>();
61         typeSet.add(rule.getLabel());
62         edgeTypesForNodePair.put(nodePairKey, typeSet);
63       }
64       else {
65         edgeTypesForNodePair.get(nodePairKey).add(rule.getLabel());
66       }
67     }
68
69     Map<String, Class<?>> edgeProps = properties.entrySet().stream().collect(Collectors.toMap(p -> p.getKey(), p -> {
70       try {
71         return resolveClass(p.getValue());
72       } catch (CrudException | ClassNotFoundException e) {
73         e.printStackTrace();
74       }
75       return null;
76     }));
77
78     rules.entries ().forEach ( (kv) -> {
79       relationTypes.put(kv.getValue ().getLabel (), edgeProps);
80       relations.put (buildRelation ( kv.getValue ().getFrom (), kv.getValue ().getTo (), kv.getValue ().getLabel ()), edgeProps);
81     });
82   }
83
84   public Map<String, Class<?>> lookupRelation(String key) {
85     return this.relations.get(key);
86   }
87
88   public Map<String, Class<?>> lookupRelationType(String type) {
89     return this.relationTypes.get(type);
90   }
91
92   public boolean isValidType(String type) {
93     return relationTypes.containsKey(type);
94   }
95
96   public Set<String> getValidRelationTypes(String source, String target) {
97     Set<String> typeList = edgeTypesForNodePair.get(buildNodePairKey(source, target));
98
99     if (typeList == null) {
100       return new HashSet<String>();
101     }
102     
103     return typeList;
104   }
105   
106   private String buildRelation(String source, String target, String relation) {
107     return source + ":" + target + ":" + relation;
108   }
109   
110   private String buildNodePairKey(String source, String target) {
111     return source + ":" + target;
112   }
113
114   private Class<?> resolveClass(String type) throws CrudException, ClassNotFoundException {
115     Class<?> clazz = Class.forName(type);
116     validateClassTypes(clazz);
117     return clazz;
118   }
119
120   private void validateClassTypes(Class<?> clazz) throws CrudException {
121     if (!clazz.isAssignableFrom(Integer.class) && !clazz.isAssignableFrom(Double.class)
122             && !clazz.isAssignableFrom(Boolean.class) && !clazz.isAssignableFrom(String.class)) {
123       throw new CrudException("", Status.BAD_REQUEST);
124     }
125   }
126 }