2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.schemaif.oxm;
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;
33 import java.util.stream.Collectors;
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;
41 import com.google.common.collect.Multimap;
44 public class RelationshipSchema {
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";
52 private static org.onap.aai.cl.api.Logger logger =
53 LoggerFactory.getInstance().getLogger(RelationshipSchema.class.getName());
55 private Map<String, Map<String, Class<?>>> relations = new HashMap<>();
57 * Hashmap of valid relationship types along with properties.
59 private Map<String, Map<String, Class<?>>> relationTypes = new HashMap<>();
60 private Map<String, EdgeRule> relationshipRules = new HashMap<>();
62 // A map storing the list of valid edge types for a source/target pair
63 private Map<String, Set<String>> edgeTypesForNodePair = new HashMap<>();
66 public RelationshipSchema(Multimap<String, EdgeRule> rules, String props) throws SchemaProviderException, IOException {
67 HashMap<String, String> properties = new ObjectMapper().readValue(props, HashMap.class);
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);
78 edgeTypesForNodePair.get(nodePairKey).add(rule.getLabel());
81 String key = buildRelation(rule.getFrom(), rule.getTo(), rule.getLabel());
82 relationshipRules.put(key, rule);
85 Map<String, Class<?>> edgeProps =
86 properties.entrySet().stream().collect(Collectors.toMap(p -> p.getKey(), p -> {
88 return resolveClass(p.getValue());
89 } catch (SchemaProviderException | ClassNotFoundException e) {
90 logger.error(SchemaProviderMsgs.SCHEMA_LOAD_ERROR, "Error in RelationshipSchema: " + e);
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()),
102 public EdgeRule lookupEdgeRule(String key) throws SchemaProviderException {
103 return relationshipRules.get(key);
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)) {
117 public RelationshipSchema(List<String> jsonStrings) throws SchemaProviderException, IOException {
118 String edgeRules = jsonStrings.get(0);
119 String props = jsonStrings.get(1);
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 -> {
127 return resolveClass(p.getValue());
128 } catch (SchemaProviderException | ClassNotFoundException e) {
129 logger.error(SchemaProviderMsgs.SCHEMA_LOAD_ERROR, "Error in RelationshipSchema: " + e);
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);
143 public Map<String, Class<?>> lookupRelation(String key) {
144 return this.relations.get(key);
147 public Map<String, Class<?>> lookupRelationType(String type) {
148 return this.relationTypes.get(type);
151 public boolean isValidType(String type) {
152 return relationTypes.containsKey(type);
156 private String buildRelation(String source, String target, String relation) {
157 return source + ":" + target + ":" + relation;
160 public Set<String> getValidRelationTypes(String source, String target) {
161 Set<String> typeList = edgeTypesForNodePair.get(buildNodePairKey(source, target));
163 if (typeList == null) {
164 return new HashSet<String>();
170 private String buildNodePairKey(String source, String target) {
171 return source + ":" + target;
175 private Class<?> resolveClass(String type) throws SchemaProviderException, ClassNotFoundException {
176 Class<?> clazz = Class.forName(type);
177 validateClassTypes(clazz);
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");