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.common.collect.Multimap;
24 import org.codehaus.jackson.map.ObjectMapper;
25 import org.onap.aai.edges.EdgeRule;
26 import org.onap.crud.exception.CrudException;
28 import java.io.IOException;
30 import java.util.stream.Collectors;
31 import javax.ws.rs.core.Response.Status;
34 public class RelationshipSchema {
36 public static final String SCHEMA_RELATIONSHIP_TYPE = "label";
39 private Map<String, Map<String, Class<?>>> relations = new HashMap<>();
41 * Hashmap of valid relationship types along with properties.
43 private Map<String, Map<String, Class<?>>> relationTypes = new HashMap<>();
45 public RelationshipSchema( Multimap<String, EdgeRule> rules, String props) throws CrudException, IOException {
46 HashMap<String, String> properties = new ObjectMapper().readValue(props, HashMap.class);
47 Map<String, Class<?>> edgeProps = properties.entrySet().stream().collect(Collectors.toMap(p -> p.getKey(), p -> {
49 return resolveClass(p.getValue());
50 } catch (CrudException | ClassNotFoundException e) {
56 rules.entries ().forEach ( (kv) -> {
57 relationTypes.put(kv.getValue ().getLabel (), edgeProps);
58 relations.put (buildRelation ( kv.getValue ().getFrom (), kv.getValue ().getTo (), kv.getValue ().getLabel ()), edgeProps);
62 public Map<String, Class<?>> lookupRelation(String key) {
63 return this.relations.get(key);
66 public Map<String, Class<?>> lookupRelationType(String type) {
67 return this.relationTypes.get(type);
70 public boolean isValidType(String type) {
71 return relationTypes.containsKey(type);
75 private String buildRelation(String source, String target, String relation){
76 return source + ":" + target + ":" + relation;
79 private Class<?> resolveClass(String type) throws CrudException, ClassNotFoundException {
80 Class<?> clazz = Class.forName(type);
81 validateClassTypes(clazz);
85 private void validateClassTypes(Class<?> clazz) throws CrudException {
86 if (!clazz.isAssignableFrom(Integer.class) && !clazz.isAssignableFrom(Double.class)
87 && !clazz.isAssignableFrom(Boolean.class) && !clazz.isAssignableFrom(String.class)) {
88 throw new CrudException("", Status.BAD_REQUEST);