Added error logger in exception scenario
[aai/spike.git] / src / main / java / org / onap / aai / spike / schema / RelationshipSchema.java
1 /**
2  * Gizmo
3  * ================================================================================
4  * Copyright © 2017 AT&T Intellectual Property.
5  * Copyright © 2017 Amdocs
6  * All rights reserved.
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
11  *
12  *    http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.spike.schema;
24
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;
30 import java.util.Map;
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;
40
41
42 public class RelationshipSchema {
43         
44
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";
49
50     private static org.onap.aai.cl.api.Logger logger =
51             LoggerFactory.getInstance().getLogger(RelationshipSchema.class.getName());
52
53     private Map<String, Map<String, Class<?>>> relations = new HashMap<>();
54     /**
55      * Hashmap of valid relationship types along with properties.
56      */
57     private Map<String, Map<String, Class<?>>> relationTypes = new HashMap<>();
58
59
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 -> {
64                     try {
65                         return resolveClass(p.getValue());
66                     } catch (SpikeException | ClassNotFoundException e) {
67                         logger.error(SpikeMsgs.OXM_LOAD_ERROR, "Error in RelationshipSchema: " + e);
68                     }
69                     return null;
70                 }));
71
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()),
75                     edgeProps);
76         });
77     }
78
79     public RelationshipSchema(List<String> jsonStrings) throws SpikeException, IOException {
80         String edgeRules = jsonStrings.get(0);
81         String props = jsonStrings.get(1);
82
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 -> {
88                     try {
89                         return resolveClass(p.getValue());
90                     } catch (SpikeException | ClassNotFoundException e) {
91                         logger.error(SpikeMsgs.OXM_LOAD_ERROR, "Error in RelationshipSchema: " + e);
92                     }
93                     return null;
94                 }));
95
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);
100         });
101     }
102
103
104
105     public Map<String, Class<?>> lookupRelation(String key) {
106         return this.relations.get(key);
107     }
108
109     public Map<String, Class<?>> lookupRelationType(String type) {
110         return this.relationTypes.get(type);
111     }
112
113     public boolean isValidType(String type) {
114         return relationTypes.containsKey(type);
115     }
116
117
118     private String buildRelation(String source, String target, String relation) {
119         return source + ":" + target + ":" + relation;
120     }
121
122     private Class<?> resolveClass(String type) throws SpikeException, ClassNotFoundException {
123         Class<?> clazz = Class.forName(type);
124         validateClassTypes(clazz);
125         return clazz;
126     }
127
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");
132         }
133     }
134 }
135
136