2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.schema;
24 import java.io.IOException;
25 import java.util.HashMap;
26 import java.util.HashSet;
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;
37 public class RelationshipSchema {
39 public static final String SCHEMA_RELATIONSHIP_TYPE = "label";
41 private Map<String, Map<String, Class<?>>> relations = new HashMap<>();
43 * Hashmap of valid relationship types along with properties.
45 private Map<String, Map<String, Class<?>>> relationTypes = new HashMap<>();
47 // A map storing the list of valid edge types for a source/target pair
48 private Map<String, Set<String>> edgeTypesForNodePair = new HashMap<>();
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);
55 // hold the true values of the edge rules by key - convert to java 8
56 for (EdgeRule rule : rules.values()) {
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);
65 edgeTypesForNodePair.get(nodePairKey).add(rule.getLabel());
69 Map<String, Class<?>> edgeProps = properties.entrySet().stream().collect(Collectors.toMap(p -> p.getKey(), p -> {
71 return resolveClass(p.getValue());
72 } catch (CrudException | ClassNotFoundException e) {
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);
84 public Map<String, Class<?>> lookupRelation(String key) {
85 return this.relations.get(key);
88 public Map<String, Class<?>> lookupRelationType(String type) {
89 return this.relationTypes.get(type);
92 public boolean isValidType(String type) {
93 return relationTypes.containsKey(type);
96 public Set<String> getValidRelationTypes(String source, String target) {
97 Set<String> typeList = edgeTypesForNodePair.get(buildNodePairKey(source, target));
99 if (typeList == null) {
100 return new HashSet<String>();
106 private String buildRelation(String source, String target, String relation) {
107 return source + ":" + target + ":" + relation;
110 private String buildNodePairKey(String source, String target) {
111 return source + ":" + target;
114 private Class<?> resolveClass(String type) throws CrudException, ClassNotFoundException {
115 Class<?> clazz = Class.forName(type);
116 validateClassTypes(clazz);
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);