optionally disable client auth in gizmo
[aai/gizmo.git] / src / main / java / org / onap / schema / validation / MultiplicityValidator.java
1 /**\r
2  * ============LICENSE_START=======================================================\r
3  * org.onap.aai\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
11  *\r
12  *       http://www.apache.org/licenses/LICENSE-2.0\r
13  *\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
20  */\r
21 package org.onap.schema.validation;\r
22 \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
31 \r
32 /**\r
33  * Validator to enforce multiplicity rules on the creation of a new Edge\r
34  *\r
35  */\r
36 public class MultiplicityValidator {\r
37 \r
38     public enum MultiplicityType {\r
39         MANY2ONE("Many2One"), MANY2MANY("Many2Many"), ONE2MANY("One2Many"), ONE2ONE("One2One");\r
40 \r
41         private final String value;\r
42 \r
43         MultiplicityType(String value) {\r
44             this.value = value;\r
45         }\r
46 \r
47         public String getValue() {\r
48             return value;\r
49         }\r
50     }\r
51 \r
52     /**\r
53      * Validates the Edge payload's source and target vertices against multiplicity rule\r
54      *\r
55      * @param payload\r
56      * @param edgesForSourceVertex\r
57      * @param edgesForTargetVertex\r
58      * @param type\r
59      * @param version\r
60      * @throws CrudException\r
61      */\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
68 \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
73 \r
74             boolean isVertexValidForMultiplicityType =\r
75                     isVertexValidForMultiplicityType(edgesForSourceVertex, edgesForTargetVertex, multiplicityType);\r
76 \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
81             }\r
82         }\r
83     }\r
84 \r
85     /**\r
86      * Compare vertex existing relationships to ensure its not in breach of multiplicity rules\r
87      *\r
88      * @param edgesForVertex\r
89      * @param multiplicityType\r
90      * @return\r
91      */\r
92     public static Boolean isVertexValidForMultiplicityType(List<Edge> edgesForSourceVertex,\r
93             List<Edge> edgesForTargetVertex,\r
94             MultiplicityType multiplicityType) {\r
95 \r
96         switch (multiplicityType) {\r
97             case MANY2MANY:\r
98                 return true;\r
99             case MANY2ONE:\r
100                 if (edgesForSourceVertex != null && !edgesForSourceVertex.isEmpty()) {\r
101                     return false;\r
102                 }\r
103                 break;\r
104             case ONE2MANY:\r
105                 if (edgesForTargetVertex != null && !edgesForTargetVertex.isEmpty()) {\r
106                     return false;\r
107                 }\r
108                 break;\r
109             case ONE2ONE:\r
110                 if ((edgesForSourceVertex != null && !edgesForSourceVertex.isEmpty())\r
111                         || (edgesForTargetVertex != null && !edgesForTargetVertex.isEmpty())) {\r
112                     return false;\r
113                 }\r
114                 break;\r
115         }\r
116         return true;\r
117     }\r
118 \r
119 }