2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
22 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.onap.schema;
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
29 import org.codehaus.jackson.map.ObjectMapper;
30 import org.onap.crud.exception.CrudException;
32 import java.io.IOException;
34 import java.util.stream.Collectors;
35 import javax.ws.rs.core.Response.Status;
38 public class RelationshipSchema {
39 private static final Gson gson = new GsonBuilder().create();
41 public static final String SCHEMA_SOURCE_NODE_TYPE = "from";
42 public static final String SCHEMA_TARGET_NODE_TYPE = "to";
43 public static final String SCHEMA_RELATIONSHIP_TYPE = "label";
44 public static final String SCHEMA_RULES_ARRAY = "rules";
47 private Map<String, Map<String, Class<?>>> relations = new HashMap<>();
49 * Hashmap of valid relationship types along with properties.
51 private Map<String, Map<String, Class<?>>> relationTypes = new HashMap<>();
54 public RelationshipSchema(List<String> jsonStrings) throws CrudException, IOException {
55 String edgeRules = jsonStrings.get(0);
56 String props = jsonStrings.get(1);
58 HashMap<String, ArrayList<LinkedHashMap<String, String>>> rules = new ObjectMapper().readValue(edgeRules, HashMap.class);
59 HashMap<String, String> properties = new ObjectMapper().readValue(props, HashMap.class);
60 Map<String, Class<?>> edgeProps = properties.entrySet().stream().collect(Collectors.toMap(p -> p.getKey(), p -> {
62 return resolveClass(p.getValue());
63 } catch (CrudException | ClassNotFoundException e) {
69 rules.get(SCHEMA_RULES_ARRAY).forEach(l -> {
70 relationTypes.put(l.get(SCHEMA_RELATIONSHIP_TYPE), edgeProps);
71 relations.put(buildRelation(l.get(SCHEMA_SOURCE_NODE_TYPE), l.get(SCHEMA_TARGET_NODE_TYPE), l.get(SCHEMA_RELATIONSHIP_TYPE)), edgeProps);
77 public Map<String, Class<?>> lookupRelation(String key) {
78 return this.relations.get(key);
81 public Map<String, Class<?>> lookupRelationType(String type) {
82 return this.relationTypes.get(type);
85 public boolean isValidType(String type) {
86 return relationTypes.containsKey(type);
90 private String buildRelation(String source, String target, String relation){
91 return source + ":" + target + ":" + relation;
94 private Class<?> resolveClass(String type) throws CrudException, ClassNotFoundException {
95 Class<?> clazz = Class.forName(type);
96 validateClassTypes(clazz);
100 private void validateClassTypes(Class<?> clazz) throws CrudException {
101 if (!clazz.isAssignableFrom(Integer.class) && !clazz.isAssignableFrom(Double.class)
102 && !clazz.isAssignableFrom(Boolean.class) && !clazz.isAssignableFrom(String.class)) {
103 throw new CrudException("", Status.BAD_REQUEST);