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 com.google.common.base.MoreObjects.ToStringHelper;
14 import java.util.Optional;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
18 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.RequiredElementCountException;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraint;
25 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraintAware;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 final class MinMaxElementsValidation<T extends DataSchemaNode & ElementCountConstraintAware>
30 extends ModificationApplyOperation {
31 private static final Logger LOG = LoggerFactory.getLogger(MinMaxElementsValidation.class);
33 private final SchemaAwareApplyOperation<T> delegate;
34 private final int minElements;
35 private final int maxElements;
37 private MinMaxElementsValidation(final SchemaAwareApplyOperation<T> delegate, final Integer minElements,
38 final Integer maxElements) {
39 this.delegate = requireNonNull(delegate);
40 this.minElements = minElements != null ? minElements : 0;
41 this.maxElements = maxElements != null ? maxElements : Integer.MAX_VALUE;
44 static <T extends DataSchemaNode & ElementCountConstraintAware> ModificationApplyOperation from(
45 final SchemaAwareApplyOperation<T> delegate) {
46 final Optional<ElementCountConstraint> optConstraint = delegate.getSchema().getElementCountConstraint();
47 if (!optConstraint.isPresent()) {
51 final ElementCountConstraint constraint = optConstraint.get();
52 return new MinMaxElementsValidation<>(delegate, constraint.getMinElements(), constraint.getMaxElements());
56 Optional<? extends TreeNode> apply(final ModifiedNode modification, final Optional<? extends TreeNode> storeMeta,
57 final Version version) {
58 Optional<? extends TreeNode> ret = modification.getValidatedNode(this, storeMeta);
60 // Deal with the result moving on us
61 ret = delegate.apply(modification, storeMeta, version);
62 if (ret.isPresent()) {
63 checkChildren(ret.get().getData());
71 void checkApplicable(final ModificationPath path, final NodeModification modification,
72 final Optional<? extends TreeNode> current, final Version version) throws DataValidationFailedException {
73 delegate.checkApplicable(path, modification, current, version);
75 if (!(modification instanceof ModifiedNode)) {
76 LOG.debug("Could not validate {}, does not implement expected class {}", modification, ModifiedNode.class);
79 final ModifiedNode modified = (ModifiedNode) modification;
81 // We need to actually perform the operation to deal with merge in a sane manner. We know the modification
82 // is immutable, so the result of validation will probably not change. Note we should not be checking number
83 final Optional<? extends TreeNode> maybeApplied = delegate.apply(modified, current, version);
84 if (maybeApplied.isPresent()) {
85 // We only enforce min/max on present data and rely on MandatoryLeafEnforcer to take care of the empty case
86 validateMinMaxElements(path, maybeApplied.get().getData());
89 // Everything passed. We now have a snapshot of the result node, it would be too bad if we just threw it out.
90 // We know what the result of an apply operation is going to be *if* the following are kept unchanged:
91 // - the 'current' node
92 // - the schemacontext (therefore, the fact this object is associated with the modification)
94 // So let's stash the result. We will pick it up during apply operation.
95 modified.setValidatedNode(this, current, maybeApplied);
99 void fullVerifyStructure(final NormalizedNode<?, ?> modification) {
100 delegate.fullVerifyStructure(modification);
101 checkChildren(modification);
105 public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
106 return delegate.getChild(child);
110 ChildTrackingPolicy getChildPolicy() {
111 return delegate.getChildPolicy();
115 void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode<?, ?> value, final Version version) {
116 delegate.mergeIntoModifiedNode(node, value, version);
120 void quickVerifyStructure(final NormalizedNode<?, ?> modification) {
121 delegate.quickVerifyStructure(modification);
125 void recursivelyVerifyStructure(final NormalizedNode<?, ?> value) {
126 delegate.recursivelyVerifyStructure(value);
130 ToStringHelper addToStringAttributes(final ToStringHelper helper) {
131 return helper.add("min", minElements).add("max", maxElements).add("delegate", delegate);
134 private void validateMinMaxElements(final ModificationPath path, final NormalizedNode<?, ?> value)
135 throws DataValidationFailedException {
136 final PathArgument id = value.getIdentifier();
137 final int children = numOfChildrenFromValue(value);
138 if (minElements > children) {
139 throw new RequiredElementCountException(path.toInstanceIdentifier(), minElements, maxElements, children,
140 "%s does not have enough elements (%s), needs at least %s", id, children, minElements);
142 if (maxElements < children) {
143 throw new RequiredElementCountException(path.toInstanceIdentifier(), minElements, maxElements, children,
144 "%s has too many elements (%s), can have at most %s", id, children, maxElements);
148 private void checkChildren(final NormalizedNode<?, ?> value) {
149 final PathArgument id = value.getIdentifier();
150 final int children = numOfChildrenFromValue(value);
151 checkArgument(minElements <= children, "Node %s does not have enough elements (%s), needs at least %s", id,
152 children, minElements);
153 checkArgument(maxElements >= children, "Node %s has too many elements (%s), can have at most %s", id, children,
157 private static int numOfChildrenFromValue(final NormalizedNode<?, ?> value) {
158 if (value instanceof NormalizedNodeContainer) {
159 return ((NormalizedNodeContainer<?, ?, ?>) value).size();
160 } else if (value instanceof UnkeyedListNode) {
161 return ((UnkeyedListNode) value).getSize();
164 throw new IllegalArgumentException(String.format(
165 "Unexpected type '%s', expected types are NormalizedNodeContainer and UnkeyedListNode",