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.JsonElement;
27 import com.google.gson.JsonNull;
29 import org.onap.crud.entity.Edge;
30 import org.onap.crud.entity.Vertex;
31 import org.onap.crud.exception.CrudException;
32 import org.onap.crud.service.EdgePayload;
33 import org.onap.crud.util.CrudServiceUtil;
34 import org.radeox.util.logging.Logger;
36 import java.util.HashMap;
39 import java.util.regex.Matcher;
40 import java.util.regex.Pattern;
41 import javax.ws.rs.core.Response.Status;
43 public class RelationshipSchemaValidator {
45 public static final String SOURCE_NODE = "source";
46 public static final String TARGET_NODE = "target";
48 final static Pattern urlPattern = Pattern.compile("services/inventory/(.*)/(.*)/(.*)");
50 public static Map<String, Object> resolveCollectionfilter(String version, String type,
51 Map<String, String> filter)
52 throws CrudException {
54 RelationshipSchema schema = RelationshipSchemaLoader.getSchemaForVersion(version);
56 throw new CrudException("", Status.NOT_FOUND);
59 Map<String, Class<?>> props = schema.lookupRelationType(type);
60 Map<String, Object> result = new HashMap<String, Object>();
62 for (String key : filter.keySet()) {
64 if (props.containsKey(key)) {
66 Object value = CrudServiceUtil.validateFieldType(filter.get(key), props.get(key));
67 result.put(key, value);
68 } catch (Exception ex) {
69 // Skip any exceptions thrown while validating the filter
80 public static void validateType(String version, String type) throws CrudException {
82 RelationshipSchema schema = RelationshipSchemaLoader.getSchemaForVersion(version);
83 if (!schema.isValidType(type)) {
84 throw new CrudException("Invalid " + RelationshipSchema.SCHEMA_RELATIONSHIP_TYPE
91 public static Edge validateIncomingAddPayload(String version, String type, Vertex sourceNode,
92 Vertex targetNode, JsonElement properties)
93 throws CrudException {
94 EdgePayload payload = new EdgePayload();
95 payload.setSource("services/inventory/" + version + "/" + sourceNode.getType()
96 + "/" + sourceNode.getId().get());
97 payload.setTarget("services/inventory/" + version + "/" + targetNode.getType()
98 + "/" + targetNode.getId().get());
99 payload.setType(type);
100 payload.setProperties(properties);
101 return validateIncomingAddPayload(version, type, payload);
104 public static Edge validateIncomingAddPayload(String version, String type, EdgePayload payload)
105 throws CrudException {
106 RelationshipSchema schema = RelationshipSchemaLoader.getSchemaForVersion(version);
110 if (payload.getSource() == null || payload.getTarget() == null) {
111 throw new CrudException("Source/Target not specified", Status.BAD_REQUEST);
114 Matcher sourceMatcher = urlPattern.matcher(payload.getSource());
115 Matcher targetMatcher = urlPattern.matcher(payload.getTarget());
117 if (!sourceMatcher.matches() || !targetMatcher.matches()) {
118 throw new CrudException("Invalid Source/Target Urls", Status.BAD_REQUEST);
121 // create key based on source:target:relationshipType
122 String sourceNodeType = sourceMatcher.group(2);
123 String targetNodeType = targetMatcher.group(2);
125 String sourceNodeId = sourceMatcher.group(3);
126 String targetNodeId = targetMatcher.group(3);
128 String key = sourceNodeType + ":" + targetNodeType + ":" + type;
130 // find the validate the key from the schema
131 Map<String, Class<?>> schemaObject = schema.lookupRelation(key);
133 if (schemaObject == null) {
134 throw new CrudException("Invalid source/target/relationship type: " + key,
138 Edge.Builder modelEdgeBuilder = new Edge.Builder(type);
140 modelEdgeBuilder.source(new Vertex.Builder(sourceNodeType).id(sourceNodeId).build());
141 modelEdgeBuilder.target(new Vertex.Builder(targetNodeType).id(targetNodeId).build());
143 // validate it properties
144 validateEdgeProps(modelEdgeBuilder, payload.getProperties(), schemaObject);
146 return modelEdgeBuilder.build();
147 } catch (Exception ex) {
149 throw new CrudException(ex.getMessage(), Status.BAD_REQUEST);
154 public static Edge validateIncomingPatchPayload(Edge edge, String version, EdgePayload payload)
155 throws CrudException {
156 RelationshipSchema schema = RelationshipSchemaLoader.getSchemaForVersion(version);
159 if (payload.getSource() != null) {
160 Matcher sourceMatcher = urlPattern.matcher(payload.getSource());
162 if (!sourceMatcher.matches()) {
163 throw new CrudException("Invalid Target Urls", Status.BAD_REQUEST);
165 String sourceNodeId = sourceMatcher.group(3);
166 if (!sourceNodeId.equals(edge.getSource().getId().get())) {
167 throw new CrudException("Source can't be updated", Status.BAD_REQUEST);
171 if (payload.getTarget() != null) {
172 Matcher targetMatcher = urlPattern.matcher(payload.getTarget());
174 if (!targetMatcher.matches()) {
175 throw new CrudException("Invalid Target Urls", Status.BAD_REQUEST);
177 String sourceNodeId = targetMatcher.group(3);
178 if (!sourceNodeId.equals(edge.getTarget().getId().get())) {
179 throw new CrudException("Target can't be updated", Status.BAD_REQUEST);
182 // create key based on source:target:relationshipType
184 String key = edge.getSource().getType() + ":" + edge.getTarget().getType()
185 + ":" + edge.getType();
187 // find the validate the key from the schema
188 Map<String, Class<?>> schemaObject = schema.lookupRelation(key);
190 if (schemaObject == null) {
191 Logger.warn("key :" + key
192 + " not found in relationship schema . Skipping the schema validation");
196 Set<Map.Entry<String, JsonElement>> entries = payload.getProperties()
197 .getAsJsonObject().entrySet();
199 for (Map.Entry<String, JsonElement> entry : entries) {
201 if (!schemaObject.containsKey(entry.getKey())) {
202 throw new CrudException("Invalid property: " + entry.getKey(), Status.BAD_REQUEST);
203 } else if (entry.getValue() instanceof JsonNull && edge.getProperties()
204 .containsKey(entry.getKey())) {
205 edge.getProperties().remove(entry.getKey());
206 } else if (!(entry.getValue() instanceof JsonNull)) {
207 Object value = CrudServiceUtil.validateFieldType(entry.getValue().getAsString(),
208 schemaObject.get(entry.getKey()));
209 edge.getProperties().put(entry.getKey(), value);
216 } catch (Exception ex) {
218 throw new CrudException(ex.getMessage(), Status.BAD_REQUEST);
222 public static Edge validateIncomingUpdatePayload(Edge edge, String version, Vertex sourceNode,
223 Vertex targetNode, JsonElement properties)
224 throws CrudException {
225 EdgePayload payload = new EdgePayload();
226 payload.setSource("services/inventory/" + version + "/" + sourceNode.getType()
227 + "/" + sourceNode.getId().get());
228 payload.setTarget("services/inventory/" + version + "/" + targetNode.getType()
229 + "/" + targetNode.getId().get());
230 payload.setType(edge.getType());
231 payload.setProperties(properties);
232 return validateIncomingUpdatePayload(edge, version, payload);
235 public static Edge validateIncomingUpdatePayload(Edge edge, String version, EdgePayload payload)
236 throws CrudException {
237 RelationshipSchema schema = RelationshipSchemaLoader.getSchemaForVersion(version);
241 if (payload.getSource() != null) {
242 Matcher sourceMatcher = urlPattern.matcher(payload.getSource());
244 if (!sourceMatcher.matches()) {
245 throw new CrudException("Invalid Target Urls", Status.BAD_REQUEST);
247 String sourceNodeId = sourceMatcher.group(3);
248 if (!sourceNodeId.equals(edge.getSource().getId().get())) {
249 throw new CrudException("Source can't be updated", Status.BAD_REQUEST);
253 if (payload.getTarget() != null) {
254 Matcher targetMatcher = urlPattern.matcher(payload.getTarget());
256 if (!targetMatcher.matches()) {
257 throw new CrudException("Invalid Target Urls", Status.BAD_REQUEST);
259 String sourceNodeId = targetMatcher.group(3);
260 if (!sourceNodeId.equals(edge.getTarget().getId().get())) {
261 throw new CrudException("Target can't be updated", Status.BAD_REQUEST);
264 // create key based on source:target:relationshipType
266 String key = edge.getSource().getType() + ":" + edge.getTarget().getType()
267 + ":" + edge.getType();
269 // find the validate the key from the schema
270 Map<String, Class<?>> schemaObject = schema.lookupRelation(key);
272 if (schemaObject == null) {
273 Logger.warn("key :" + key
274 + " not found in relationship schema . Skipping the schema validation");
278 Edge.Builder updatedEdgeBuilder = new Edge.Builder(edge.getType()).id(edge.getId().get());
281 .source(new Vertex.Builder(edge.getSource().getType()).id(edge.getSource().getId()
284 .target(new Vertex.Builder(edge.getTarget().getType()).id(edge.getTarget().getId()
287 validateEdgeProps(updatedEdgeBuilder, payload.getProperties(), schemaObject);
289 return updatedEdgeBuilder.build();
290 } catch (Exception ex) {
292 throw new CrudException(ex.getMessage(), Status.BAD_REQUEST);
297 private static void validateEdgeProps(Edge.Builder builder, JsonElement props,
298 Map<String, Class<?>> schemaObject)
299 throws CrudException {
300 Set<Map.Entry<String, JsonElement>> entries = props.getAsJsonObject().entrySet();
302 for (Map.Entry<String, JsonElement> entry : entries) {
304 if (!schemaObject.containsKey(entry.getKey())) {
305 throw new CrudException("Invalid property: " + entry.getKey(), Status.BAD_REQUEST);
307 Object value = CrudServiceUtil.validateFieldType(entry.getValue().getAsString(),
308 schemaObject.get(entry.getKey()));
309 builder.property(entry.getKey(), value);
316 public static Edge validateOutgoingPayload(String version, Edge edge) throws CrudException {
318 Edge.Builder modelEdgeBuilder = new Edge.Builder(edge.getType()).id(edge.getId()
319 .get()).source(edge.getSource())
320 .target(edge.getTarget());
322 RelationshipSchema schema = RelationshipSchemaLoader.getSchemaForVersion(version);
324 String key = edge.getSource().getType() + ":" + edge.getTarget().getType()
325 + ":" + edge.getType();
326 Map<String, Class<?>> schemaObject = schema.lookupRelation(key);
328 if (schemaObject == null || schemaObject.isEmpty()) {
332 for (String prop : edge.getProperties().keySet()) {
333 if (schemaObject.containsKey(prop)) {
334 modelEdgeBuilder.property(prop, edge.getProperties().get(prop));
338 return modelEdgeBuilder.build();
342 public static String vertexTypeFromUri(String uri) throws CrudException {
344 Matcher matcher = urlPattern.matcher(uri);
346 if (!matcher.matches()) {
347 throw new CrudException("Invalid Source/Target Urls", Status.BAD_REQUEST);
350 return matcher.group(2);