X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=yang%2Fyang-data-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fdata%2Fimpl%2Fschema%2Ftree%2FMinMaxElementsValidation.java;h=f5c75a6524eafc0d2b56ed2ccc19025f79aa9450;hb=refs%2Fchanges%2F90%2F93890%2F3;hp=ede5bcc067b227719a92655359c82df1a013ad75;hpb=f29f6b8445fae2505bce3feedfb3d005695adcdd;p=yangtools.git diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/MinMaxElementsValidation.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/MinMaxElementsValidation.java index ede5bcc067..f5c75a6524 100644 --- a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/MinMaxElementsValidation.java +++ b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/MinMaxElementsValidation.java @@ -5,101 +5,82 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.yangtools.yang.data.impl.schema.tree; - -import com.google.common.base.Optional; -import com.google.common.base.Preconditions; -import com.google.common.base.Verify; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument; +import com.google.common.base.MoreObjects.ToStringHelper; +import java.util.Optional; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer; import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode; -import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException; -import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode; -import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version; -import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition; +import org.opendaylight.yangtools.yang.data.api.schema.tree.RequiredElementCountException; import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.opendaylight.yangtools.yang.model.api.ElementCountConstraint; +import org.opendaylight.yangtools.yang.model.api.ElementCountConstraintAware; -final class MinMaxElementsValidation extends SchemaAwareApplyOperation { +final class MinMaxElementsValidation + extends AbstractValidation { + private final int minElements; + private final int maxElements; - private static final Logger LOG = LoggerFactory.getLogger(MinMaxElementsValidation.class); - private final SchemaAwareApplyOperation delegate; - private final Integer minElements; - private final Integer maxElements; - - private MinMaxElementsValidation(final SchemaAwareApplyOperation delegate, final Integer minElements, + private MinMaxElementsValidation(final SchemaAwareApplyOperation delegate, final Integer minElements, final Integer maxElements) { - this.delegate = Preconditions.checkNotNull(delegate); - this.minElements = minElements; - this.maxElements = maxElements; + super(delegate); + this.minElements = minElements != null ? minElements : 0; + this.maxElements = maxElements != null ? maxElements : Integer.MAX_VALUE; } - static SchemaAwareApplyOperation from(final SchemaAwareApplyOperation delegate, final DataSchemaNode schema) { - final ConstraintDefinition constraints = schema.getConstraints(); - if (constraints == null || (constraints.getMinElements() == null && constraints.getMaxElements() == null)) { + static ModificationApplyOperation from( + final SchemaAwareApplyOperation delegate) { + final Optional optConstraint = delegate.getSchema().getElementCountConstraint(); + if (!optConstraint.isPresent()) { return delegate; } - return new MinMaxElementsValidation(delegate, constraints.getMinElements(), constraints.getMaxElements()); + final ElementCountConstraint constraint = optConstraint.get(); + return new MinMaxElementsValidation<>(delegate, constraint.getMinElements(), constraint.getMaxElements()); } - private static int findChildrenBefore(final Optional current) { - if (current.isPresent()) { - return numOfChildrenFromValue(current.get().getData()); - } else { - return 0; - } + @Override + void enforceOnData(final NormalizedNode data) { + enforceOnData(data, (actual, message) -> new IllegalArgumentException(message)); } - private static int findChildrenAfter(final ModifiedNode modification) { - if (modification.getWrittenValue() != null) { - return numOfChildrenFromValue(modification.getWrittenValue()); - } else { - return 0; - } + @Override + void enforceOnData(final ModificationPath path, final NormalizedNode data) + throws RequiredElementCountException { + enforceOnData(data, (actual, message) -> new RequiredElementCountException(path.toInstanceIdentifier(), + minElements, maxElements, actual, message)); } - private void checkMinMaxElements(final YangInstanceIdentifier path, final NodeModification nodeMod, - final Optional current) throws DataValidationFailedException { - if (!(nodeMod instanceof ModifiedNode)) { - LOG.debug("Could not validate {}, does not implement expected class {}", nodeMod, ModifiedNode.class); - return; - } - - final ModifiedNode modification = (ModifiedNode) nodeMod; - final int childrenBefore = (modification.getOperation() == LogicalOperation.WRITE) ? 0 : findChildrenBefore - (current); - Verify.verify(childrenBefore >= 0, "Child count before is %s (from %s)", childrenBefore, current); - - final int childrenAfter = findChildrenAfter(modification); - Verify.verify(childrenAfter >= 0, "Child count after is %s (from %s)", childrenAfter, modification); - - final int childrenModified = numOfChildrenFromChildMods(modification, current); - LOG.debug("Modified child count is %s (from %s and %s)", childrenModified, modification, current); - - final int childrenTotal = childrenBefore + childrenAfter + childrenModified; - Verify.verify(childrenTotal >= 0, "Total child count is %s (from %s and %s)", childrenTotal, modification, current); + @FunctionalInterface + @NonNullByDefault + interface ExceptionSupplier { + T get(int actual, String message); + } - if (minElements != null && minElements > childrenTotal) { - throw new DataValidationFailedException(path, String.format( - "%s does not have enough elements (%s), needs at least %s", modification.getIdentifier(), - childrenTotal, minElements)); + private void enforceOnData(final NormalizedNode value, + final ExceptionSupplier exceptionSupplier) throws X { + final int children = numOfChildrenFromValue(value); + if (minElements > children) { + throw exceptionSupplier.get(children, value.getIdentifier() + + " does not have enough elements (" + children + "), needs at least " + minElements); } - if (maxElements != null && maxElements < childrenTotal) { - throw new DataValidationFailedException(path, String.format( - "%s has too many elements (%s), can have at most %s", modification.getIdentifier(), childrenTotal, - maxElements)); + if (maxElements < children) { + throw exceptionSupplier.get(children, value.getIdentifier() + + " has too many elements (" + children + "), can have at most " + maxElements); } } + @Override + ToStringHelper addToStringAttributes(final ToStringHelper helper) { + return super.addToStringAttributes(helper.add("min", minElements).add("max", maxElements)); + } + private static int numOfChildrenFromValue(final NormalizedNode value) { if (value instanceof NormalizedNodeContainer) { - return ((NormalizedNodeContainer) value).getValue().size(); + return ((NormalizedNodeContainer) value).size(); } else if (value instanceof UnkeyedListNode) { return ((UnkeyedListNode) value).getSize(); } @@ -108,101 +89,4 @@ final class MinMaxElementsValidation extends SchemaAwareApplyOperation { "Unexpected type '%s', expected types are NormalizedNodeContainer and UnkeyedListNode", value.getClass())); } - - private static int numOfChildrenFromChildMods(final ModifiedNode modification, final Optional current) { - int result = 0; - for (final ModifiedNode modChild : modification.getChildren()) { - switch (modChild.getOperation()) { - case WRITE: - if (!checkOriginalPresent(modChild)) { - result++; - } - break; - case MERGE: - if (!checkOriginalPresent(modChild)) { - result++; - } - break; - case DELETE: - if (checkOriginalPresent(modChild)) { - result--; - } - break; - case NONE: - case TOUCH: - // NOOP - break; - default: - throw new IllegalArgumentException("Unsupported operation type: " + modChild.getOperation()); - } - } - return result; - } - - private static boolean checkOriginalPresent(ModifiedNode child) { - return child.getOriginal().isPresent(); - } - - @Override - protected void checkTouchApplicable(final YangInstanceIdentifier path, final NodeModification modification, - final Optional current) throws DataValidationFailedException { - delegate.checkTouchApplicable(path, modification, current); - checkMinMaxElements(path, modification, current); - } - - @Override - protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification, - final Optional current) throws DataValidationFailedException { - delegate.checkMergeApplicable(path, modification, current); - checkMinMaxElements(path, modification, current); - } - - @Override - protected void checkWriteApplicable(final YangInstanceIdentifier path, final NodeModification modification, - final Optional current) throws DataValidationFailedException { - delegate.checkWriteApplicable(path, modification, current); - checkMinMaxElements(path, modification, current); - } - - - @Override - public Optional getChild(final PathArgument child) { - return delegate.getChild(child); - } - - @Override - protected void verifyStructure(final NormalizedNode modification, final boolean verifyChildren) { - delegate.verifyStructure(modification, verifyChildren); - } - - @Override - protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) { - return delegate.applyMerge(modification, currentMeta, version); - } - - @Override - protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) { - return delegate.applyTouch(modification, currentMeta, version); - } - - @Override - protected TreeNode applyWrite(final ModifiedNode modification, final Optional currentMeta, - final Version version) { - return delegate.applyWrite(modification, currentMeta, version); - } - - @Override - protected ChildTrackingPolicy getChildPolicy() { - return delegate.getChildPolicy(); - } - - @Override - void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode value, final Version version) { - delegate.mergeIntoModifiedNode(node, value, version); - } - - @Override - void recursivelyVerifyStructure(NormalizedNode value) { - delegate.recursivelyVerifyStructure(value); - } }