2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
\r
6 * Copyright © 2017-2018 Amdocs
\r
7 * ================================================================================
\r
8 * Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * you may not use this file except in compliance with the License.
\r
10 * You may obtain a copy of the License at
\r
12 * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * Unless required by applicable law or agreed to in writing, software
\r
15 * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * See the License for the specific language governing permissions and
\r
18 * limitations under the License.
\r
19 * ============LICENSE_END=========================================================
\r
21 package org.onap.schema.validation;
\r
23 import java.util.List;
\r
24 import javax.ws.rs.core.Response.Status;
\r
25 import org.onap.crud.entity.Edge;
\r
26 import org.onap.crud.exception.CrudException;
\r
27 import org.onap.crud.parser.EdgePayload;
\r
28 import org.onap.crud.parser.util.EdgePayloadUtil;
\r
29 import org.onap.schema.EdgeRulesLoader;
\r
30 import org.onap.schema.RelationshipSchema;
\r
33 * Validator to enforce multiplicity rules on the creation of a new Edge
\r
36 public class MultiplicityValidator {
\r
38 public enum MultiplicityType {
\r
39 MANY2ONE("Many2One"), MANY2MANY("Many2Many"), ONE2MANY("One2Many"), ONE2ONE("One2One");
\r
41 private final String value;
\r
43 MultiplicityType(String value) {
\r
47 public String getValue() {
\r
53 * Validates the Edge payload's source and target vertices against multiplicity rule
\r
56 * @param edgesForSourceVertex
\r
57 * @param edgesForTargetVertex
\r
60 * @throws CrudException
\r
62 public static void validatePayloadMultiplicity(EdgePayload payload, List<Edge> edgesForSourceVertex,
\r
63 List<Edge> edgesForTargetVertex, String type, String version)
\r
64 throws CrudException {
\r
65 RelationshipSchema schema = EdgeRulesLoader.getSchemaForVersion(version);
\r
66 // find the validate the key from the schema
\r
67 String key = EdgePayloadUtil.generateEdgeKey(payload.getSource(), payload.getTarget(), type);
\r
69 // get the multiplicity rule for the relationships
\r
70 String multiplicityTypeValue = schema.lookupRelationMultiplicity(key);
\r
71 if (multiplicityTypeValue != null) {
\r
72 MultiplicityType multiplicityType = MultiplicityType.valueOf(multiplicityTypeValue.toUpperCase());
\r
74 boolean isVertexValidForMultiplicityType =
\r
75 isVertexValidForMultiplicityType(edgesForSourceVertex, edgesForTargetVertex, multiplicityType);
\r
77 if (!isVertexValidForMultiplicityType) {
\r
78 throw new CrudException(
\r
79 multiplicityType.toString() + " multiplicity rule broken for Edge:" + key,
\r
80 Status.BAD_REQUEST);
\r
86 * Compare vertex existing relationships to ensure its not in breach of multiplicity rules
\r
88 * @param edgesForVertex
\r
89 * @param multiplicityType
\r
92 public static Boolean isVertexValidForMultiplicityType(List<Edge> edgesForSourceVertex,
\r
93 List<Edge> edgesForTargetVertex,
\r
94 MultiplicityType multiplicityType) {
\r
96 switch (multiplicityType) {
\r
100 if (edgesForSourceVertex != null && !edgesForSourceVertex.isEmpty()) {
\r
105 if (edgesForTargetVertex != null && !edgesForTargetVertex.isEmpty()) {
\r
110 if ((edgesForSourceVertex != null && !edgesForSourceVertex.isEmpty())
\r
111 || (edgesForTargetVertex != null && !edgesForTargetVertex.isEmpty())) {
\r