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=c9a9cbea997499b3b8f24652b07c9d0c00620132;hb=970923b5f47f7507ec78021965fa5df1a878af48;hp=525ff55612146323284be99d4017a32a95c515ba;hpb=c30bea17ea3b476ba5748546df13249c41b41879;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..c9a9cbea99 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,185 +1,81 @@ /* - * 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 static com.google.common.base.Preconditions.checkArgument; -import com.google.common.base.Optional; -import com.google.common.base.Preconditions; -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()); - - } - - 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 = findChildrenBefore(current); - - final int childrenAfter = findChildrenAfter(modification); - - 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(), - childrenTotal, 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)); - } - } - - 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())); - } - - private static int numOfChildrenFromChildMods(final ModifiedNode modification, final Optional current) { - int result = 0; - for (final ModifiedNode modChild : modification.getChildren()) { - switch (modChild.getOperation()) { - case WRITE: - result++; - break; - case MERGE: - if (!current.isPresent()) { - result++; - } - break; - case DELETE: - result--; - break; - case NONE: - case TOUCH: - // NOOP - break; - default: - throw new IllegalArgumentException("Unsupported operation type: " + modChild.getOperation()); - } - } - return result; - } - @Override - protected void checkTouchApplicable(final YangInstanceIdentifier path, final NodeModification modification, - final Optional current) throws DataValidationFailedException { - delegate.checkTouchApplicable(path, modification, current); - checkMinMaxElements(path, modification, current); + final ElementCountConstraint constraint = optConstraint.get(); + return new MinMaxElementsValidation<>(delegate, constraint.getMinElements(), constraint.getMaxElements()); } @Override - protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification, - final Optional current) throws DataValidationFailedException { - delegate.checkMergeApplicable(path, modification, current); - checkMinMaxElements(path, modification, current); + void enforceOnData(final NormalizedNode data) { + enforceOnData(data, (actual, message) -> new MinMaxElementsValidationFailedException(message)); } @Override - protected void checkWriteApplicable(final YangInstanceIdentifier path, final NodeModification modification, - final Optional current) throws DataValidationFailedException { - delegate.checkWriteApplicable(path, modification, current); - checkMinMaxElements(path, modification, current); + void enforceOnData(final ModificationPath path, final NormalizedNode data) throws RequiredElementCountException { + enforceOnData(data, (actual, message) -> new RequiredElementCountException(path.toInstanceIdentifier(), + minElements, maxElements, actual, message)); } - - @Override - public Optional getChild(final PathArgument child) { - return delegate.getChild(child); + @FunctionalInterface + @NonNullByDefault + interface ExceptionSupplier { + T get(int actual, String message); } - @Override - void verifyStructure(final ModifiedNode modification) throws IllegalArgumentException { - delegate.verifyStructure(modification); - } - - @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 void verifyWrittenStructure(final NormalizedNode writtenValue) { - delegate.verifyWrittenStructure(writtenValue); + private void enforceOnData(final NormalizedNode value, + final ExceptionSupplier exceptionSupplier) throws X { + checkArgument(value instanceof NormalizedNodeContainer, "Value %s is not a NormalizedNodeContainer", value); + final int children = ((NormalizedNodeContainer) value).size(); + if (minElements > children) { + throw exceptionSupplier.get(children, value.getIdentifier() + + " does not have enough elements (" + children + "), needs at least " + minElements); + } + if (maxElements < children) { + throw exceptionSupplier.get(children, value.getIdentifier() + + " has too many elements (" + children + "), can have at most " + maxElements); + } } @Override - protected ChildTrackingPolicy getChildPolicy() { - return delegate.getChildPolicy(); + ToStringHelper addToStringAttributes(final ToStringHelper helper) { + return super.addToStringAttributes(helper.add("min", minElements).add("max", maxElements)); } }