2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import java.util.Optional;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
17 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.RequiredElementCountException;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
22 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraint;
24 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraintAware;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 final class MinMaxElementsValidation<T extends DataSchemaNode & ElementCountConstraintAware>
29 extends DelegatingModificationApplyOperation {
30 private static final Logger LOG = LoggerFactory.getLogger(MinMaxElementsValidation.class);
32 private final SchemaAwareApplyOperation<T> delegate;
33 private final int minElements;
34 private final int maxElements;
36 private MinMaxElementsValidation(final SchemaAwareApplyOperation<T> delegate, final Integer minElements,
37 final Integer maxElements) {
38 this.delegate = requireNonNull(delegate);
39 this.minElements = minElements != null ? minElements : 0;
40 this.maxElements = maxElements != null ? maxElements : Integer.MAX_VALUE;
43 static <T extends DataSchemaNode & ElementCountConstraintAware> ModificationApplyOperation from(
44 final SchemaAwareApplyOperation<T> delegate) {
45 final Optional<ElementCountConstraint> optConstraint = delegate.getSchema().getElementCountConstraint();
46 if (!optConstraint.isPresent()) {
50 final ElementCountConstraint constraint = optConstraint.get();
51 return new MinMaxElementsValidation<>(delegate, constraint.getMinElements(), constraint.getMaxElements());
55 ModificationApplyOperation delegate() {
60 Optional<TreeNode> apply(final ModifiedNode modification, final Optional<TreeNode> storeMeta,
61 final Version version) {
62 Optional<TreeNode> ret = modification.getValidatedNode(this, storeMeta);
64 // Deal with the result moving on us
65 ret = delegate.apply(modification, storeMeta, version);
66 checkChildren(modification.getIdentifier(), numOfChildrenFromTreeNode(ret));
72 void checkApplicable(final ModificationPath path, final NodeModification modification,
73 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
74 delegate.checkApplicable(path, modification, current, version);
76 if (!(modification instanceof ModifiedNode)) {
77 LOG.debug("Could not validate {}, does not implement expected class {}", modification, ModifiedNode.class);
80 final ModifiedNode modified = (ModifiedNode) modification;
82 // We need to actually perform the operation to deal with merge in a sane manner. We know the modification
83 // is immutable, so the result of validation will probably not change.
84 final Optional<TreeNode> maybeApplied = delegate.apply(modified, current, version);
85 validateMinMaxElements(path, modified.getIdentifier(), numOfChildrenFromTreeNode(maybeApplied));
87 // Everything passed. We now have a snapshot of the result node, it would be too bad if we just threw it out.
88 // We know what the result of an apply operation is going to be *if* the following are kept unchanged:
89 // - the 'current' node
90 // - the schemacontext (therefore, the fact this object is associated with the modification)
92 // So let's stash the result. We will pick it up during apply operation.
93 modified.setValidatedNode(this, current, maybeApplied);
97 void fullVerifyStructure(final NormalizedNode<?, ?> modification) {
98 delegate.fullVerifyStructure(modification);
99 checkChildren(modification.getIdentifier(), numOfChildrenFromValue(modification));
102 private void validateMinMaxElements(final ModificationPath path, final PathArgument id, final int children)
103 throws DataValidationFailedException {
104 if (minElements > children) {
105 throw new RequiredElementCountException(path.toInstanceIdentifier(), minElements, maxElements,
106 children, "%s does not have enough elements (%s), needs at least %s", id, children, minElements);
108 if (maxElements < children) {
109 throw new RequiredElementCountException(path.toInstanceIdentifier(), minElements, maxElements,
110 children, "%s has too many elements (%s), can have at most %s", id, children, maxElements);
114 private void checkChildren(final PathArgument id, final int children) {
115 checkArgument(minElements <= children, "Node %s does not have enough elements (%s), needs at least %s", id,
116 children, minElements);
117 checkArgument(maxElements >= children, "Node %s has too many elements (%s), can have at most %s", id, children,
121 private static int numOfChildrenFromTreeNode(final Optional<TreeNode> node) {
122 return node.isPresent() ? numOfChildrenFromValue(node.get().getData()) : 0;
125 private static int numOfChildrenFromValue(final NormalizedNode<?, ?> value) {
126 if (value instanceof NormalizedNodeContainer) {
127 return ((NormalizedNodeContainer<?, ?, ?>) value).getValue().size();
128 } else if (value instanceof UnkeyedListNode) {
129 return ((UnkeyedListNode) value).getSize();
132 throw new IllegalArgumentException(String.format(
133 "Unexpected type '%s', expected types are NormalizedNodeContainer and UnkeyedListNode",