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 com.google.common.base.Preconditions;
11 import com.google.common.base.Verify;
12 import java.util.Optional;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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.spi.TreeNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraint;
23 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraintAware;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 final class MinMaxElementsValidation extends SchemaAwareApplyOperation {
28 private static final Logger LOG = LoggerFactory.getLogger(MinMaxElementsValidation.class);
30 private final SchemaAwareApplyOperation delegate;
31 private final int minElements;
32 private final int maxElements;
34 private MinMaxElementsValidation(final SchemaAwareApplyOperation delegate, final Integer minElements,
35 final Integer maxElements) {
36 this.delegate = Preconditions.checkNotNull(delegate);
37 this.minElements = minElements != null ? minElements : 0;
38 this.maxElements = maxElements != null ? maxElements : Integer.MAX_VALUE;
41 static SchemaAwareApplyOperation from(final SchemaAwareApplyOperation delegate, final DataSchemaNode schema) {
42 if (!(schema instanceof ElementCountConstraintAware)) {
45 final Optional<ElementCountConstraint> optConstraint = ((ElementCountConstraintAware) schema)
46 .getElementCountConstraint();
47 if (!optConstraint.isPresent()) {
51 final ElementCountConstraint constraint = optConstraint.get();
52 return new MinMaxElementsValidation(delegate, constraint.getMinElements(), constraint.getMaxElements());
55 private void validateMinMaxElements(final YangInstanceIdentifier path, final PathArgument id,
56 final NormalizedNode<?, ?> data) throws DataValidationFailedException {
57 final int children = numOfChildrenFromValue(data);
58 if (minElements > children) {
59 throw new DataValidationFailedException(path, String.format(
60 "%s does not have enough elements (%s), needs at least %s", id, children, minElements));
62 if (maxElements < children) {
63 throw new DataValidationFailedException(path, String.format(
64 "%s has too many elements (%s), can have at most %s", id, children, maxElements));
68 private void checkMinMaxElements(final YangInstanceIdentifier path, final NodeModification nodeMod,
69 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
70 if (!(nodeMod instanceof ModifiedNode)) {
71 LOG.debug("Could not validate {}, does not implement expected class {}", nodeMod, ModifiedNode.class);
75 final ModifiedNode modification = (ModifiedNode) nodeMod;
77 // We need to actually perform the operation to get deal with merge in a sane manner. We know the modification
78 // is immutable, so the result of validation will probably not change.
79 final Optional<TreeNode> maybeApplied = delegate.apply(modification, current, version);
80 Verify.verify(maybeApplied.isPresent());
82 final TreeNode applied = maybeApplied.get();
83 validateMinMaxElements(path, modification.getIdentifier(), applied.getData());
85 // Everything passed. We now have a snapshot of the result node, it would be too bad if we just threw it out.
86 // We know what the result of an apply operation is going to be *if* the following are kept unchanged:
87 // - the 'current' node
88 // - the schemacontext (therefore, the fact this object is associated with the modification)
90 // So let's stash the result. We will pick it up during apply operation.
91 modification.setValidatedNode(this, current, applied);
94 private static int numOfChildrenFromValue(final NormalizedNode<?, ?> value) {
95 if (value instanceof NormalizedNodeContainer) {
96 return ((NormalizedNodeContainer<?, ?, ?>) value).getValue().size();
97 } else if (value instanceof UnkeyedListNode) {
98 return ((UnkeyedListNode) value).getSize();
101 throw new IllegalArgumentException(String.format(
102 "Unexpected type '%s', expected types are NormalizedNodeContainer and UnkeyedListNode",
107 protected void checkTouchApplicable(final YangInstanceIdentifier path, final NodeModification modification,
108 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
109 delegate.checkTouchApplicable(path, modification, current, version);
110 checkMinMaxElements(path, modification, current, version);
114 protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification,
115 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
116 delegate.checkMergeApplicable(path, modification, current, version);
117 checkMinMaxElements(path, modification, current, version);
121 protected void checkWriteApplicable(final YangInstanceIdentifier path, final NodeModification modification,
122 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
123 delegate.checkWriteApplicable(path, modification, current, version);
124 checkMinMaxElements(path, modification, current, version);
128 public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
129 return delegate.getChild(child);
133 protected void verifyStructure(final NormalizedNode<?, ?> modification, final boolean verifyChildren) {
134 delegate.verifyStructure(modification, verifyChildren);
138 protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
139 final TreeNode validated = modification.getValidatedNode(this, Optional.of(currentMeta));
140 if (validated != null) {
144 // FIXME: the result moved, make sure we enforce again
145 return delegate.applyMerge(modification, currentMeta, version);
149 protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
150 final TreeNode validated = modification.getValidatedNode(this, Optional.of(currentMeta));
151 if (validated != null) {
155 // FIXME: the result moved, make sure we enforce again
156 return delegate.applyTouch(modification, currentMeta, version);
160 protected TreeNode applyWrite(final ModifiedNode modification, final Optional<TreeNode> currentMeta,
161 final Version version) {
162 final TreeNode validated = modification.getValidatedNode(this, currentMeta);
163 if (validated != null) {
167 // FIXME: the result moved, make sure we enforce again
168 return delegate.applyWrite(modification, currentMeta, version);
172 protected ChildTrackingPolicy getChildPolicy() {
173 return delegate.getChildPolicy();
177 void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode<?, ?> value, final Version version) {
178 delegate.mergeIntoModifiedNode(node, value, version);
182 void recursivelyVerifyStructure(final NormalizedNode<?, ?> value) {
183 delegate.recursivelyVerifyStructure(value);