--- /dev/null
+/**\r
+ * ============LICENSE_START=======================================================\r
+ * org.onap.aai\r
+ * ================================================================================\r
+ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.\r
+ * Copyright © 2017-2018 Amdocs\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ============LICENSE_END=========================================================\r
+ */\r
+package org.onap.schema.validation;\r
+\r
+import java.util.List;\r
+import javax.ws.rs.core.Response.Status;\r
+import org.onap.crud.entity.Edge;\r
+import org.onap.crud.exception.CrudException;\r
+import org.onap.crud.parser.EdgePayload;\r
+import org.onap.crud.parser.util.EdgePayloadUtil;\r
+import org.onap.schema.EdgeRulesLoader;\r
+import org.onap.schema.RelationshipSchema;\r
+\r
+/**\r
+ * Validator to enforce multiplicity rules on the creation of a new Edge\r
+ *\r
+ */\r
+public class MultiplicityValidator {\r
+\r
+ public enum MultiplicityType {\r
+ MANY2ONE("Many2One"), MANY2MANY("Many2Many"), ONE2MANY("One2Many"), ONE2ONE("One2One");\r
+\r
+ private final String value;\r
+\r
+ MultiplicityType(String value) {\r
+ this.value = value;\r
+ }\r
+\r
+ public String getValue() {\r
+ return value;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Validates the Edge payload's source and target vertices against multiplicity rule\r
+ *\r
+ * @param payload\r
+ * @param edgesForSourceVertex\r
+ * @param edgesForTargetVertex\r
+ * @param type\r
+ * @param version\r
+ * @throws CrudException\r
+ */\r
+ public static void validatePayloadMultiplicity(EdgePayload payload, List<Edge> edgesForSourceVertex,\r
+ List<Edge> edgesForTargetVertex, String type, String version)\r
+ throws CrudException {\r
+ RelationshipSchema schema = EdgeRulesLoader.getSchemaForVersion(version);\r
+ // find the validate the key from the schema\r
+ String key = EdgePayloadUtil.generateEdgeKey(payload.getSource(), payload.getTarget(), type);\r
+\r
+ // get the multiplicity rule for the relationships\r
+ String multiplicityTypeValue = schema.lookupRelationMultiplicity(key);\r
+ if (multiplicityTypeValue != null) {\r
+ MultiplicityType multiplicityType = MultiplicityType.valueOf(multiplicityTypeValue.toUpperCase());\r
+\r
+ boolean isVertexValidForMultiplicityType =\r
+ isVertexValidForMultiplicityType(edgesForSourceVertex, edgesForTargetVertex, multiplicityType);\r
+\r
+ if (!isVertexValidForMultiplicityType) {\r
+ throw new CrudException(\r
+ multiplicityType.toString() + " multiplicity rule broken for Edge:" + key,\r
+ Status.BAD_REQUEST);\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Compare vertex existing relationships to ensure its not in breach of multiplicity rules\r
+ *\r
+ * @param edgesForVertex\r
+ * @param multiplicityType\r
+ * @return\r
+ */\r
+ public static Boolean isVertexValidForMultiplicityType(List<Edge> edgesForSourceVertex,\r
+ List<Edge> edgesForTargetVertex,\r
+ MultiplicityType multiplicityType) {\r
+\r
+ switch (multiplicityType) {\r
+ case MANY2MANY:\r
+ return true;\r
+ case MANY2ONE:\r
+ if (edgesForSourceVertex != null && !edgesForSourceVertex.isEmpty()) {\r
+ return false;\r
+ }\r
+ break;\r
+ case ONE2MANY:\r
+ if (edgesForTargetVertex != null && !edgesForTargetVertex.isEmpty()) {\r
+ return false;\r
+ }\r
+ break;\r
+ case ONE2ONE:\r
+ if ((edgesForSourceVertex != null && !edgesForSourceVertex.isEmpty())\r
+ || (edgesForTargetVertex != null && !edgesForTargetVertex.isEmpty())) {\r
+ return false;\r
+ }\r
+ break;\r
+ }\r
+ return true;\r
+ }\r
+\r
+}
\ No newline at end of file