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_SOURCE_NODE_TYPE = "from";
40 public static final String SCHEMA_TARGET_NODE_TYPE = "to";
41 public static final String SCHEMA_RELATIONSHIP_TYPE = "label";
42 public static final String SCHEMA_MULTIPLICITY_TYPE = "multiplicity";
43 public static final String SCHEMA_RULES_ARRAY = "rules";
45 private Map<String, Map<String, Class<?>>> relations = new HashMap<>();
47 * Hashmap of valid relationship types along with properties.
49 private Map<String, Map<String, Class<?>>> relationTypes = new HashMap<>();
51 private Map<String, EdgeRule> relationshipRules = new HashMap<>();
53 // A map storing the list of valid edge types for a source/target pair
54 private Map<String, Set<String>> edgeTypesForNodePair = new HashMap<>();
57 @SuppressWarnings("unchecked")
58 public RelationshipSchema(Multimap<String, EdgeRule> rules, String props) throws CrudException, IOException {
59 HashMap<String, String> properties = new ObjectMapper().readValue(props, HashMap.class);
61 // hold the true values of the edge rules by key - convert to java 8
62 for (EdgeRule rule : rules.values()) {
63 String key = buildRelation(rule.getFrom(), rule.getTo(), rule.getLabel());
64 relationshipRules.put(key, rule);
66 String nodePairKey = buildNodePairKey(rule.getFrom(), rule.getTo());
67 if (edgeTypesForNodePair.get(nodePairKey) == null) {
68 Set<String> typeSet = new HashSet<String>();
69 typeSet.add(rule.getLabel());
70 edgeTypesForNodePair.put(nodePairKey, typeSet);
73 edgeTypesForNodePair.get(nodePairKey).add(rule.getLabel());
77 Map<String, Class<?>> edgeProps = properties.entrySet().stream().collect(Collectors.toMap(p -> p.getKey(), p -> {
79 return resolveClass(p.getValue());
80 } catch (CrudException | ClassNotFoundException e) {
86 rules.entries ().forEach ( (kv) -> {
87 relationTypes.put(kv.getValue ().getLabel (), edgeProps);
88 relations.put (buildRelation ( kv.getValue ().getFrom (), kv.getValue ().getTo (), kv.getValue ().getLabel ()), edgeProps);
92 public Map<String, Class<?>> lookupRelation(String key) {
93 return this.relations.get(key);
97 * Extract the multiplicity type from the Edge rules
101 * @throws CrudException
103 public String lookupRelationMultiplicity(String key) throws CrudException {
104 EdgeRule edgeRule = relationshipRules.get(key);
106 if (edgeRule == null) {
107 throw new CrudException("Invalid source/target/relationship type: " + key, Status.BAD_REQUEST);
110 if (edgeRule.getMultiplicityRule() != null) {
111 return edgeRule.getMultiplicityRule().toString();
117 public Map<String, Class<?>> lookupRelationType(String type) {
118 return this.relationTypes.get(type);
121 public boolean isValidType(String type) {
122 return relationTypes.containsKey(type);
125 public Set<String> getValidRelationTypes(String source, String target) {
126 Set<String> typeList = edgeTypesForNodePair.get(buildNodePairKey(source, target));
128 if (typeList == null) {
129 return new HashSet<String>();
135 private String buildRelation(String source, String target, String relation) {
136 return source + ":" + target + ":" + relation;
139 private String buildNodePairKey(String source, String target) {
140 return source + ":" + target;
143 private Class<?> resolveClass(String type) throws CrudException, ClassNotFoundException {
144 Class<?> clazz = Class.forName(type);
145 validateClassTypes(clazz);
149 private void validateClassTypes(Class<?> clazz) throws CrudException {
150 if (!clazz.isAssignableFrom(Integer.class) && !clazz.isAssignableFrom(Double.class)
151 && !clazz.isAssignableFrom(Boolean.class) && !clazz.isAssignableFrom(String.class)) {
152 throw new CrudException("", Status.BAD_REQUEST);