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;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
26 import org.codehaus.jackson.map.ObjectMapper;
27 import org.onap.crud.exception.CrudException;
29 import java.io.IOException;
31 import java.util.stream.Collectors;
32 import javax.ws.rs.core.Response.Status;
35 public class RelationshipSchema {
36 private static final Gson gson = new GsonBuilder().create();
38 public static final String SCHEMA_SOURCE_NODE_TYPE = "from";
39 public static final String SCHEMA_TARGET_NODE_TYPE = "to";
40 public static final String SCHEMA_RELATIONSHIP_TYPE = "label";
41 public static final String SCHEMA_RULES_ARRAY = "rules";
44 private Map<String, Map<String, Class<?>>> relations = new HashMap<>();
46 * Hashmap of valid relationship types along with properties.
48 private Map<String, Map<String, Class<?>>> relationTypes = new HashMap<>();
51 public RelationshipSchema(List<String> jsonStrings) throws CrudException, IOException {
52 String edgeRules = jsonStrings.get(0);
53 String props = jsonStrings.get(1);
55 HashMap<String, ArrayList<LinkedHashMap<String, String>>> rules = new ObjectMapper().readValue(edgeRules, HashMap.class);
56 HashMap<String, String> properties = new ObjectMapper().readValue(props, HashMap.class);
57 Map<String, Class<?>> edgeProps = properties.entrySet().stream().collect(Collectors.toMap(p -> p.getKey(), p -> {
59 return resolveClass(p.getValue());
60 } catch (CrudException | ClassNotFoundException e) {
66 rules.get(SCHEMA_RULES_ARRAY).forEach(l -> {
67 relationTypes.put(l.get(SCHEMA_RELATIONSHIP_TYPE), edgeProps);
68 relations.put(buildRelation(l.get(SCHEMA_SOURCE_NODE_TYPE), l.get(SCHEMA_TARGET_NODE_TYPE), l.get(SCHEMA_RELATIONSHIP_TYPE)), edgeProps);
74 public Map<String, Class<?>> lookupRelation(String key) {
75 return this.relations.get(key);
78 public Map<String, Class<?>> lookupRelationType(String type) {
79 return this.relationTypes.get(type);
82 public boolean isValidType(String type) {
83 return relationTypes.containsKey(type);
87 private String buildRelation(String source, String target, String relation){
88 return source + ":" + target + ":" + relation;
91 private Class<?> resolveClass(String type) throws CrudException, ClassNotFoundException {
92 Class<?> clazz = Class.forName(type);
93 validateClassTypes(clazz);
97 private void validateClassTypes(Class<?> clazz) throws CrudException {
98 if (!clazz.isAssignableFrom(Integer.class) && !clazz.isAssignableFrom(Double.class)
99 && !clazz.isAssignableFrom(Boolean.class) && !clazz.isAssignableFrom(String.class)) {
100 throw new CrudException("", Status.BAD_REQUEST);