[AAI] Fix doc config files
[aai/aai-common.git] / aai-schema-abstraction / src / main / java / org / onap / aai / schemaif / oxm / RelationshipSchema.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2019 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.schemaif.oxm;
24
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.LinkedHashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.stream.Collectors;
34
35 import org.codehaus.jackson.map.ObjectMapper;
36 import org.onap.aai.cl.eelf.LoggerFactory;
37 import org.onap.aai.edges.EdgeRule;
38 import org.onap.aai.schemaif.SchemaProviderException;
39 import org.onap.aai.schemaif.SchemaProviderMsgs;
40
41 import com.google.common.collect.Multimap;
42
43
44 public class RelationshipSchema {
45
46
47     public static final String SCHEMA_SOURCE_NODE_TYPE = "from";
48     public static final String SCHEMA_TARGET_NODE_TYPE = "to";
49     public static final String SCHEMA_RELATIONSHIP_TYPE = "label";
50     public static final String SCHEMA_RULES_ARRAY = "rules";
51
52     private static org.onap.aai.cl.api.Logger logger =
53             LoggerFactory.getInstance().getLogger(RelationshipSchema.class.getName());
54
55     private Map<String, Map<String, Class<?>>> relations = new HashMap<>();
56     /**
57      * Hashmap of valid relationship types along with properties.
58      */
59     private Map<String, Map<String, Class<?>>> relationTypes = new HashMap<>();
60     private Map<String, EdgeRule> relationshipRules = new HashMap<>();
61
62     // A map storing the list of valid edge types for a source/target pair
63     private Map<String, Set<String>> edgeTypesForNodePair = new HashMap<>();
64
65
66     public RelationshipSchema(Multimap<String, EdgeRule> rules, String props) throws SchemaProviderException, IOException {
67         HashMap<String, String> properties = new ObjectMapper().readValue(props, HashMap.class);
68
69         // hold the true values of the edge rules by key 
70         for (EdgeRule rule : rules.values()) {
71             String nodePairKey = buildNodePairKey(rule.getFrom(), rule.getTo());
72             if (edgeTypesForNodePair.get(nodePairKey) == null) {
73                 Set<String> typeSet = new HashSet<String>();
74                 typeSet.add(rule.getLabel());
75                 edgeTypesForNodePair.put(nodePairKey, typeSet);
76             }
77             else {
78                 edgeTypesForNodePair.get(nodePairKey).add(rule.getLabel());
79             }
80
81             String key = buildRelation(rule.getFrom(), rule.getTo(), rule.getLabel());
82             relationshipRules.put(key, rule);
83         }
84
85         Map<String, Class<?>> edgeProps =
86                 properties.entrySet().stream().collect(Collectors.toMap(p -> p.getKey(), p -> {
87                     try {
88                         return resolveClass(p.getValue());
89                     } catch (SchemaProviderException | ClassNotFoundException e) {
90                         logger.error(SchemaProviderMsgs.SCHEMA_LOAD_ERROR, "Error in RelationshipSchema: " + e);
91                     }
92                     return null;
93                 }));
94
95         rules.entries().forEach((kv) -> {
96             relationTypes.put(kv.getValue().getLabel(), edgeProps);
97             relations.put(buildRelation(kv.getValue().getFrom(), kv.getValue().getTo(), kv.getValue().getLabel()),
98                     edgeProps);
99         });
100     }
101
102     public EdgeRule lookupEdgeRule(String key) throws SchemaProviderException {
103         return relationshipRules.get(key);
104     }
105
106     public List<EdgeRule> lookupAdjacentEdges(String vertex) throws SchemaProviderException {
107         List<EdgeRule> edges = new ArrayList<EdgeRule>();
108         for (EdgeRule rule : relationshipRules.values()) {
109             if (rule.getFrom().equals(vertex) || rule.getTo().equals(vertex)) {
110                 edges.add(rule);
111             }
112         }
113
114         return edges;
115     }
116
117     public RelationshipSchema(List<String> jsonStrings) throws SchemaProviderException, IOException {
118         String edgeRules = jsonStrings.get(0);
119         String props = jsonStrings.get(1);
120
121         HashMap<String, ArrayList<LinkedHashMap<String, String>>> rules =
122                 new ObjectMapper().readValue(edgeRules, HashMap.class);
123         HashMap<String, String> properties = new ObjectMapper().readValue(props, HashMap.class);
124         Map<String, Class<?>> edgeProps =
125                 properties.entrySet().stream().collect(Collectors.toMap(p -> p.getKey(), p -> {
126                     try {
127                         return resolveClass(p.getValue());
128                     } catch (SchemaProviderException | ClassNotFoundException e) {
129                         logger.error(SchemaProviderMsgs.SCHEMA_LOAD_ERROR, "Error in RelationshipSchema: " + e);
130                     }
131                     return null;
132                 }));
133
134         rules.get(SCHEMA_RULES_ARRAY).forEach(l -> {
135             relationTypes.put(l.get(SCHEMA_RELATIONSHIP_TYPE), edgeProps);
136             relations.put(buildRelation(l.get(SCHEMA_SOURCE_NODE_TYPE), l.get(SCHEMA_TARGET_NODE_TYPE),
137                     l.get(SCHEMA_RELATIONSHIP_TYPE)), edgeProps);
138         });
139     }
140
141
142
143     public Map<String, Class<?>> lookupRelation(String key) {
144         return this.relations.get(key);
145     }
146
147     public Map<String, Class<?>> lookupRelationType(String type) {
148         return this.relationTypes.get(type);
149     }
150
151     public boolean isValidType(String type) {
152         return relationTypes.containsKey(type);
153     }
154
155
156     private String buildRelation(String source, String target, String relation) {
157         return source + ":" + target + ":" + relation;
158     }
159
160     public Set<String> getValidRelationTypes(String source, String target) {
161         Set<String> typeList = edgeTypesForNodePair.get(buildNodePairKey(source, target));
162
163         if (typeList == null) {
164             return new HashSet<String>();
165         }
166
167         return typeList;
168     }
169
170     private String buildNodePairKey(String source, String target) {
171         return source + ":" + target;
172     }
173
174
175     private Class<?> resolveClass(String type) throws SchemaProviderException, ClassNotFoundException {
176         Class<?> clazz = Class.forName(type);
177         validateClassTypes(clazz);
178         return clazz;
179     }
180
181     private void validateClassTypes(Class<?> clazz) throws SchemaProviderException {
182         if (!clazz.isAssignableFrom(Integer.class) && !clazz.isAssignableFrom(Double.class)
183                 && !clazz.isAssignableFrom(Boolean.class) && !clazz.isAssignableFrom(String.class)) {
184             throw new SchemaProviderException("BAD_REQUEST");
185         }
186     }
187 }
188
189