3 * ================================================================================
4 * Copyright © 2017 AT&T Intellectual Property.
5 * Copyright © 2017 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.spike.schema;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.LinkedHashMap;
29 import java.util.List;
31 import java.util.stream.Collectors;
32 import com.google.common.collect.Multimap;
33 import com.google.gson.Gson;
34 import com.google.gson.GsonBuilder;
35 import org.onap.aai.cl.eelf.LoggerFactory;
36 import org.codehaus.jackson.map.ObjectMapper;
37 import org.onap.aai.edges.EdgeRule;
38 import org.onap.aai.spike.exception.SpikeException;
39 import org.onap.aai.spike.logging.SpikeMsgs;
42 public class RelationshipSchema {
45 public static final String SCHEMA_SOURCE_NODE_TYPE = "from";
46 public static final String SCHEMA_TARGET_NODE_TYPE = "to";
47 public static final String SCHEMA_RELATIONSHIP_TYPE = "label";
48 public static final String SCHEMA_RULES_ARRAY = "rules";
50 private static org.onap.aai.cl.api.Logger logger =
51 LoggerFactory.getInstance().getLogger(RelationshipSchema.class.getName());
53 private Map<String, Map<String, Class<?>>> relations = new HashMap<>();
55 * Hashmap of valid relationship types along with properties.
57 private Map<String, Map<String, Class<?>>> relationTypes = new HashMap<>();
60 public RelationshipSchema(Multimap<String, EdgeRule> rules, String props) throws SpikeException, IOException {
61 HashMap<String, String> properties = new ObjectMapper().readValue(props, HashMap.class);
62 Map<String, Class<?>> edgeProps =
63 properties.entrySet().stream().collect(Collectors.toMap(p -> p.getKey(), p -> {
65 return resolveClass(p.getValue());
66 } catch (SpikeException | ClassNotFoundException e) {
67 logger.error(SpikeMsgs.OXM_LOAD_ERROR, "Error in RelationshipSchema: " + e);
72 rules.entries().forEach((kv) -> {
73 relationTypes.put(kv.getValue().getLabel(), edgeProps);
74 relations.put(buildRelation(kv.getValue().getFrom(), kv.getValue().getTo(), kv.getValue().getLabel()),
79 public RelationshipSchema(List<String> jsonStrings) throws SpikeException, IOException {
80 String edgeRules = jsonStrings.get(0);
81 String props = jsonStrings.get(1);
83 HashMap<String, ArrayList<LinkedHashMap<String, String>>> rules =
84 new ObjectMapper().readValue(edgeRules, HashMap.class);
85 HashMap<String, String> properties = new ObjectMapper().readValue(props, HashMap.class);
86 Map<String, Class<?>> edgeProps =
87 properties.entrySet().stream().collect(Collectors.toMap(p -> p.getKey(), p -> {
89 return resolveClass(p.getValue());
90 } catch (SpikeException | ClassNotFoundException e) {
91 logger.error(SpikeMsgs.OXM_LOAD_ERROR, "Error in RelationshipSchema: " + e);
96 rules.get(SCHEMA_RULES_ARRAY).forEach(l -> {
97 relationTypes.put(l.get(SCHEMA_RELATIONSHIP_TYPE), edgeProps);
98 relations.put(buildRelation(l.get(SCHEMA_SOURCE_NODE_TYPE), l.get(SCHEMA_TARGET_NODE_TYPE),
99 l.get(SCHEMA_RELATIONSHIP_TYPE)), edgeProps);
105 public Map<String, Class<?>> lookupRelation(String key) {
106 return this.relations.get(key);
109 public Map<String, Class<?>> lookupRelationType(String type) {
110 return this.relationTypes.get(type);
113 public boolean isValidType(String type) {
114 return relationTypes.containsKey(type);
118 private String buildRelation(String source, String target, String relation) {
119 return source + ":" + target + ":" + relation;
122 private Class<?> resolveClass(String type) throws SpikeException, ClassNotFoundException {
123 Class<?> clazz = Class.forName(type);
124 validateClassTypes(clazz);
128 private void validateClassTypes(Class<?> clazz) throws SpikeException {
129 if (!clazz.isAssignableFrom(Integer.class) && !clazz.isAssignableFrom(Double.class)
130 && !clazz.isAssignableFrom(Boolean.class) && !clazz.isAssignableFrom(String.class)) {
131 throw new SpikeException("BAD_REQUEST");