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=8e1e91c5633060d19c12e420622351199ea91dc2;hb=bf009ab944ceb87cbd348ee5b1e23eaefe2577da;hp=14c08f0be9e96dc84cb48ca020eeeeae4ddddf5a;hpb=19c31f13e5040750d30cf9f4211913b2be04b403;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 14c08f0be9..8e1e91c563 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,79 +5,86 @@ * 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 static com.google.common.base.Preconditions.checkArgument; +import static java.util.Objects.requireNonNull; + +import com.google.common.base.MoreObjects.ToStringHelper; +import java.util.Optional; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument; 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.RequiredElementCountException; 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.model.api.DataSchemaNode; +import org.opendaylight.yangtools.yang.model.api.ElementCountConstraint; +import org.opendaylight.yangtools.yang.model.api.ElementCountConstraintAware; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -final class MinMaxElementsValidation extends SchemaAwareApplyOperation { - +final class MinMaxElementsValidation + extends ModificationApplyOperation { 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 final SchemaAwareApplyOperation delegate; + private final int minElements; + private final int maxElements; + + private MinMaxElementsValidation(final SchemaAwareApplyOperation delegate, final Integer minElements, final Integer maxElements) { - this.delegate = Preconditions.checkNotNull(delegate); - this.minElements = minElements; - this.maxElements = maxElements; + this.delegate = requireNonNull(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 void validateMinMaxElements(final YangInstanceIdentifier path, final PathArgument id, - final NormalizedNode data) throws DataValidationFailedException { - final int children = numOfChildrenFromValue(data); - if (minElements != null && minElements > children) { - throw new DataValidationFailedException(path, String.format( - "%s does not have enough elements (%s), needs at least %s", id, - children, minElements)); - } - if (maxElements != null && maxElements < children) { - throw new DataValidationFailedException(path, String.format( - "%s has too many elements (%s), can have at most %s", id, children, - maxElements)); + @Override + Optional apply(final ModifiedNode modification, final Optional storeMeta, + final Version version) { + Optional ret = modification.getValidatedNode(this, storeMeta); + if (ret == null) { + // Deal with the result moving on us + ret = delegate.apply(modification, storeMeta, version); + if (ret.isPresent()) { + checkChildren(ret.get().getData()); + } } + + return ret; } - private void checkMinMaxElements(final YangInstanceIdentifier path, final NodeModification nodeMod, - final Optional current, final Version version) throws DataValidationFailedException { - if (!(nodeMod instanceof ModifiedNode)) { - LOG.debug("Could not validate {}, does not implement expected class {}", nodeMod, ModifiedNode.class); + @Override + void checkApplicable(final ModificationPath path, final NodeModification modification, + final Optional current, final Version version) throws DataValidationFailedException { + delegate.checkApplicable(path, modification, current, version); + + if (!(modification instanceof ModifiedNode)) { + LOG.debug("Could not validate {}, does not implement expected class {}", modification, ModifiedNode.class); return; } - - final ModifiedNode modification = (ModifiedNode) nodeMod; - - // We need to actually perform the operation to get deal with merge in a sane manner. We know the modification - // is immutable, so the result of validation will probably not change. - final Optional maybeApplied = delegate.apply(modification, current, version); - Verify.verify(maybeApplied.isPresent()); - - final TreeNode applied = maybeApplied.get(); - validateMinMaxElements(path, modification.getIdentifier(), applied.getData()); + final ModifiedNode modified = (ModifiedNode) modification; + + // We need to actually perform the operation to deal with merge in a sane manner. We know the modification + // is immutable, so the result of validation will probably not change. Note we should not be checking number + final Optional maybeApplied = delegate.apply(modified, current, version); + if (maybeApplied.isPresent()) { + // We only enforce min/max on present data and rely on MandatoryLeafEnforcer to take care of the empty case + validateMinMaxElements(path, maybeApplied.get().getData()); + } // Everything passed. We now have a snapshot of the result node, it would be too bad if we just threw it out. // We know what the result of an apply operation is going to be *if* the following are kept unchanged: @@ -85,98 +92,77 @@ final class MinMaxElementsValidation extends SchemaAwareApplyOperation { // - the schemacontext (therefore, the fact this object is associated with the modification) // // So let's stash the result. We will pick it up during apply operation. - modification.setValidatedNode(this, current, applied); - } - - private static int numOfChildrenFromValue(final NormalizedNode value) { - if (value instanceof NormalizedNodeContainer) { - return ((NormalizedNodeContainer) value).getValue().size(); - } else if (value instanceof UnkeyedListNode) { - return ((UnkeyedListNode) value).getSize(); - } - - throw new IllegalArgumentException(String.format( - "Unexpected type '%s', expected types are NormalizedNodeContainer and UnkeyedListNode", - value.getClass())); + modified.setValidatedNode(this, current, maybeApplied); } @Override - protected void checkTouchApplicable(final YangInstanceIdentifier path, final NodeModification modification, - final Optional current, final Version version) throws DataValidationFailedException { - delegate.checkTouchApplicable(path, modification, current, version); - checkMinMaxElements(path, modification, current, version); + void fullVerifyStructure(final NormalizedNode modification) { + delegate.fullVerifyStructure(modification); + checkChildren(modification); } @Override - protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification, - final Optional current, final Version version) throws DataValidationFailedException { - delegate.checkMergeApplicable(path, modification, current, version); - checkMinMaxElements(path, modification, current, version); + public Optional getChild(final PathArgument child) { + return delegate.getChild(child); } @Override - protected void checkWriteApplicable(final YangInstanceIdentifier path, final NodeModification modification, - final Optional current, final Version version) throws DataValidationFailedException { - delegate.checkWriteApplicable(path, modification, current, version); - checkMinMaxElements(path, modification, current, version); + ChildTrackingPolicy getChildPolicy() { + return delegate.getChildPolicy(); } @Override - public Optional getChild(final PathArgument child) { - return delegate.getChild(child); + void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode value, final Version version) { + delegate.mergeIntoModifiedNode(node, value, version); } @Override - protected void verifyStructure(final NormalizedNode modification, final boolean verifyChildren) { - delegate.verifyStructure(modification, verifyChildren); + void quickVerifyStructure(final NormalizedNode modification) { + delegate.quickVerifyStructure(modification); } @Override - protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) { - final TreeNode validated = modification.getValidatedNode(this, Optional.of(currentMeta)); - if (validated != null) { - return validated; - } - - // FIXME: the result moved, make sure we enforce again - return delegate.applyMerge(modification, currentMeta, version); + void recursivelyVerifyStructure(final NormalizedNode value) { + delegate.recursivelyVerifyStructure(value); } @Override - protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) { - final TreeNode validated = modification.getValidatedNode(this, Optional.of(currentMeta)); - if (validated != null) { - return validated; - } - - // FIXME: the result moved, make sure we enforce again - return delegate.applyTouch(modification, currentMeta, version); + ToStringHelper addToStringAttributes(final ToStringHelper helper) { + return helper.add("min", minElements).add("max", maxElements).add("delegate", delegate); } - @Override - protected TreeNode applyWrite(final ModifiedNode modification, final Optional currentMeta, - final Version version) { - final TreeNode validated = modification.getValidatedNode(this, currentMeta); - if (validated != null) { - return validated; + private void validateMinMaxElements(final ModificationPath path, final NormalizedNode value) + throws DataValidationFailedException { + final PathArgument id = value.getIdentifier(); + final int children = numOfChildrenFromValue(value); + if (minElements > children) { + throw new RequiredElementCountException(path.toInstanceIdentifier(), minElements, maxElements, children, + "%s does not have enough elements (%s), needs at least %s", id, children, minElements); + } + if (maxElements < children) { + throw new RequiredElementCountException(path.toInstanceIdentifier(), minElements, maxElements, children, + "%s has too many elements (%s), can have at most %s", id, children, maxElements); } - - // FIXME: the result moved, make sure we enforce again - return delegate.applyWrite(modification, currentMeta, version); } - @Override - protected ChildTrackingPolicy getChildPolicy() { - return delegate.getChildPolicy(); + private void checkChildren(final NormalizedNode value) { + final PathArgument id = value.getIdentifier(); + final int children = numOfChildrenFromValue(value); + checkArgument(minElements <= children, "Node %s does not have enough elements (%s), needs at least %s", id, + children, minElements); + checkArgument(maxElements >= children, "Node %s has too many elements (%s), can have at most %s", id, children, + maxElements); } - @Override - void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode value, final Version version) { - delegate.mergeIntoModifiedNode(node, value, version); - } + private static int numOfChildrenFromValue(final NormalizedNode value) { + if (value instanceof NormalizedNodeContainer) { + return ((NormalizedNodeContainer) value).size(); + } else if (value instanceof UnkeyedListNode) { + return ((UnkeyedListNode) value).getSize(); + } - @Override - void recursivelyVerifyStructure(NormalizedNode value) { - delegate.recursivelyVerifyStructure(value); + throw new IllegalArgumentException(String.format( + "Unexpected type '%s', expected types are NormalizedNodeContainer and UnkeyedListNode", + value.getClass())); } }