X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-data-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fdata%2Fimpl%2Fschema%2Ftree%2FMinMaxElementsValidation.java;h=ede5bcc067b227719a92655359c82df1a013ad75;hb=bf405586fc69c3781311cfb8ac19ba93b670ec8d;hp=306d9c8581233362e07ec61f27500a5680548e85;hpb=11d15938d7d4a0a6558a17fac0dea962ee709d68;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 306d9c8581..ede5bcc067 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 @@ -1,15 +1,17 @@ /* - * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. * - * This program and the accompanying materials are made available under the terms of the Eclipse - * Public License v1.0 which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html + * This program and the accompanying materials are made available under the + * 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 org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; @@ -37,7 +39,7 @@ final class MinMaxElementsValidation extends SchemaAwareApplyOperation { this.maxElements = maxElements; } - static final SchemaAwareApplyOperation from(final SchemaAwareApplyOperation delegate, final DataSchemaNode schema) { + static SchemaAwareApplyOperation from(final SchemaAwareApplyOperation delegate, final DataSchemaNode schema) { final ConstraintDefinition constraints = schema.getConstraints(); if (constraints == null || (constraints.getMinElements() == null && constraints.getMaxElements() == null)) { return delegate; @@ -46,28 +48,43 @@ final class MinMaxElementsValidation extends SchemaAwareApplyOperation { } + private static int findChildrenBefore(final Optional current) { + if (current.isPresent()) { + return numOfChildrenFromValue(current.get().getData()); + } else { + return 0; + } + } + + private static int findChildrenAfter(final ModifiedNode modification) { + if (modification.getWrittenValue() != null) { + return numOfChildrenFromValue(modification.getWrittenValue()); + } else { + return 0; + } + } + 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; - if (current.isPresent()) { - childrenBefore = numOfChildrenFromValue(current.get().getData()); - } else { - childrenBefore = 0; - } + 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; - if (modification.getWrittenValue() != null) { - childrenAfter = numOfChildrenFromValue(modification.getWrittenValue()); - } else { - childrenAfter = 0; - } + 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); - final int childrenTotal = childrenBefore + childrenAfter + numOfChildrenFromChildMods(modification, current); if (minElements != null && minElements > childrenTotal) { throw new DataValidationFailedException(path, String.format( "%s does not have enough elements (%s), needs at least %s", modification.getIdentifier(), @@ -97,15 +114,19 @@ final class MinMaxElementsValidation extends SchemaAwareApplyOperation { for (final ModifiedNode modChild : modification.getChildren()) { switch (modChild.getOperation()) { case WRITE: - result++; + if (!checkOriginalPresent(modChild)) { + result++; + } break; case MERGE: - if (!current.isPresent()) { + if (!checkOriginalPresent(modChild)) { result++; } break; case DELETE: - result--; + if (checkOriginalPresent(modChild)) { + result--; + } break; case NONE: case TOUCH: @@ -118,6 +139,10 @@ final class MinMaxElementsValidation extends SchemaAwareApplyOperation { 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 { @@ -146,8 +171,8 @@ final class MinMaxElementsValidation extends SchemaAwareApplyOperation { } @Override - void verifyStructure(final ModifiedNode modification) throws IllegalArgumentException { - delegate.verifyStructure(modification); + protected void verifyStructure(final NormalizedNode modification, final boolean verifyChildren) { + delegate.verifyStructure(modification, verifyChildren); } @Override @@ -167,12 +192,17 @@ final class MinMaxElementsValidation extends SchemaAwareApplyOperation { } @Override - protected void verifyWrittenStructure(final NormalizedNode writtenValue) { - delegate.verifyWrittenStructure(writtenValue); + protected ChildTrackingPolicy getChildPolicy() { + return delegate.getChildPolicy(); } @Override - protected ChildTrackingPolicy getChildPolicy() { - return delegate.getChildPolicy(); + void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode value, final Version version) { + delegate.mergeIntoModifiedNode(node, value, version); + } + + @Override + void recursivelyVerifyStructure(NormalizedNode value) { + delegate.recursivelyVerifyStructure(value); } }