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=525ff55612146323284be99d4017a32a95c515ba;hpb=6e8cb34ea33cbcbb2990af3247fd04b6a0af6846;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 525ff55612..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; @@ -68,13 +70,21 @@ final class MinMaxElementsValidation extends SchemaAwareApplyOperation { LOG.debug("Could not validate {}, does not implement expected class {}", nodeMod, ModifiedNode.class); return; } - final ModifiedNode modification = (ModifiedNode) nodeMod; - final int childrenBefore = findChildrenBefore(current); + 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); - 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(), @@ -104,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: @@ -125,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 { @@ -153,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 @@ -174,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); } }