* 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 static java.util.Objects.requireNonNull;
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+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 DelegatingModificationApplyOperation {
private static final Logger LOG = LoggerFactory.getLogger(MinMaxElementsValidation.class);
+
private final SchemaAwareApplyOperation delegate;
- private final Integer minElements;
- private final Integer maxElements;
+ 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 DataSchemaNode schema) {
+ if (!(schema instanceof ElementCountConstraintAware)) {
+ return delegate;
+ }
+ final Optional<ElementCountConstraint> optConstraint = ((ElementCountConstraintAware) schema)
+ .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<TreeNode> current) {
- if (current.isPresent()) {
- return numOfChildrenFromValue(current.get().getData());
- } else {
- return 0;
- }
+ @Override
+ ModificationApplyOperation delegate() {
+ return delegate;
}
- private static int findChildrenAfter(final ModifiedNode modification) {
- if (modification.getWrittenValue() != null) {
- return numOfChildrenFromValue(modification.getWrittenValue());
- } else {
- return 0;
+ @Override
+ Optional<TreeNode> apply(final ModifiedNode modification, final Optional<TreeNode> storeMeta,
+ final Version version) {
+ Optional<TreeNode> ret = modification.getValidatedNode(this, storeMeta);
+ if (ret == null) {
+ // Deal with the result moving on us
+ ret = delegate.apply(modification, storeMeta, version);
+ checkChildren(modification.getIdentifier(), numOfChildrenFromTreeNode(ret));
}
+ return ret;
}
- private void checkMinMaxElements(final YangInstanceIdentifier path, final NodeModification nodeMod,
- final Optional<TreeNode> current) 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<TreeNode> 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;
+ final ModifiedNode modified = (ModifiedNode) modification;
- final int childrenBefore = findChildrenBefore(current);
+ // 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.
+ final Optional<TreeNode> maybeApplied = delegate.apply(modified, current, version);
+ validateMinMaxElements(path, modified.getIdentifier(), numOfChildrenFromTreeNode(maybeApplied));
- final int childrenAfter = findChildrenAfter(modification);
+ // 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:
+ // - the 'current' node
+ // - 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.
+ modified.setValidatedNode(this, current, maybeApplied);
+ }
- 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));
+ @Override
+ void verifyStructure(final NormalizedNode<?, ?> modification, final boolean verifyChildren) {
+ delegate.verifyStructure(modification, verifyChildren);
+ if (verifyChildren) {
+ checkChildren(modification.getIdentifier(), numOfChildrenFromValue(modification));
}
- 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 void validateMinMaxElements(final ModificationPath path, final PathArgument id, final int children)
+ throws DataValidationFailedException {
+ 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);
+ }
+ }
+
+ private void checkChildren(final PathArgument id, final int children) {
+ 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);
+ }
+
+ private static int numOfChildrenFromTreeNode(final Optional<TreeNode> node) {
+ return node.isPresent() ? numOfChildrenFromValue(node.get().getData()) : 0;
}
private static int numOfChildrenFromValue(final NormalizedNode<?, ?> value) {
"Unexpected type '%s', expected types are NormalizedNodeContainer and UnkeyedListNode",
value.getClass()));
}
-
- private static int numOfChildrenFromChildMods(final ModifiedNode modification, final Optional<TreeNode> 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<TreeNode> current) throws DataValidationFailedException {
- delegate.checkTouchApplicable(path, modification, current);
- checkMinMaxElements(path, modification, current);
- }
-
- @Override
- protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification,
- final Optional<TreeNode> current) throws DataValidationFailedException {
- delegate.checkMergeApplicable(path, modification, current);
- checkMinMaxElements(path, modification, current);
- }
-
- @Override
- protected void checkWriteApplicable(final YangInstanceIdentifier path, final NodeModification modification,
- final Optional<TreeNode> current) throws DataValidationFailedException {
- delegate.checkWriteApplicable(path, modification, current);
- checkMinMaxElements(path, modification, current);
- }
-
-
- @Override
- public Optional<ModificationApplyOperation> 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<TreeNode> currentMeta,
- final Version version) {
- return delegate.applyWrite(modification, currentMeta, version);
- }
-
- @Override
- protected ChildTrackingPolicy getChildPolicy() {
- return delegate.getChildPolicy();
- }
}