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
9 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Verify;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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.spi.TreeNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
22 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 final class MinMaxElementsValidation extends SchemaAwareApplyOperation {
29 private static final Logger LOG = LoggerFactory.getLogger(MinMaxElementsValidation.class);
30 private final SchemaAwareApplyOperation delegate;
31 private final Integer minElements;
32 private final Integer maxElements;
34 private MinMaxElementsValidation(final SchemaAwareApplyOperation delegate, final Integer minElements,
35 final Integer maxElements) {
36 this.delegate = Preconditions.checkNotNull(delegate);
37 this.minElements = minElements;
38 this.maxElements = maxElements;
41 static SchemaAwareApplyOperation from(final SchemaAwareApplyOperation delegate, final DataSchemaNode schema) {
42 final ConstraintDefinition constraints = schema.getConstraints();
43 if (constraints == null || (constraints.getMinElements() == null && constraints.getMaxElements() == null)) {
46 return new MinMaxElementsValidation(delegate, constraints.getMinElements(), constraints.getMaxElements());
50 private void validateMinMaxElements(final YangInstanceIdentifier path, final PathArgument id,
51 final NormalizedNode<?, ?> data) throws DataValidationFailedException {
52 final int children = numOfChildrenFromValue(data);
53 if (minElements != null && minElements > children) {
54 throw new DataValidationFailedException(path, String.format(
55 "%s does not have enough elements (%s), needs at least %s", id,
56 children, minElements));
58 if (maxElements != null && maxElements < children) {
59 throw new DataValidationFailedException(path, String.format(
60 "%s has too many elements (%s), can have at most %s", id, children,
65 private void checkMinMaxElements(final YangInstanceIdentifier path, final NodeModification nodeMod,
66 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
67 if (!(nodeMod instanceof ModifiedNode)) {
68 LOG.debug("Could not validate {}, does not implement expected class {}", nodeMod, ModifiedNode.class);
72 final ModifiedNode modification = (ModifiedNode) nodeMod;
74 // We need to actually perform the operation to get deal with merge in a sane manner. We know the modification
75 // is immutable, so the result of validation will probably not change.
76 final Optional<TreeNode> maybeApplied = delegate.apply(modification, current, version);
77 Verify.verify(maybeApplied.isPresent());
79 final TreeNode applied = maybeApplied.get();
80 validateMinMaxElements(path, modification.getIdentifier(), applied.getData());
82 // Everything passed. We now have a snapshot of the result node, it would be too bad if we just threw it out.
83 // We know what the result of an apply operation is going to be *if* the following are kept unchanged:
84 // - the 'current' node
85 // - the schemacontext (therefore, the fact this object is associated with the modification)
87 // So let's stash the result. We will pick it up during apply operation.
88 modification.setValidatedNode(this, current, applied);
91 private static int numOfChildrenFromValue(final NormalizedNode<?, ?> value) {
92 if (value instanceof NormalizedNodeContainer) {
93 return ((NormalizedNodeContainer<?, ?, ?>) value).getValue().size();
94 } else if (value instanceof UnkeyedListNode) {
95 return ((UnkeyedListNode) value).getSize();
98 throw new IllegalArgumentException(String.format(
99 "Unexpected type '%s', expected types are NormalizedNodeContainer and UnkeyedListNode",
104 protected void checkTouchApplicable(final YangInstanceIdentifier path, final NodeModification modification,
105 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
106 delegate.checkTouchApplicable(path, modification, current, version);
107 checkMinMaxElements(path, modification, current, version);
111 protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification,
112 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
113 delegate.checkMergeApplicable(path, modification, current, version);
114 checkMinMaxElements(path, modification, current, version);
118 protected void checkWriteApplicable(final YangInstanceIdentifier path, final NodeModification modification,
119 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
120 delegate.checkWriteApplicable(path, modification, current, version);
121 checkMinMaxElements(path, modification, current, version);
125 public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
126 return delegate.getChild(child);
130 protected void verifyStructure(final NormalizedNode<?, ?> modification, final boolean verifyChildren) {
131 delegate.verifyStructure(modification, verifyChildren);
135 protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
136 final TreeNode validated = modification.getValidatedNode(this, Optional.of(currentMeta));
137 if (validated != null) {
141 // FIXME: the result moved, make sure we enforce again
142 return delegate.applyMerge(modification, currentMeta, version);
146 protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
147 final TreeNode validated = modification.getValidatedNode(this, Optional.of(currentMeta));
148 if (validated != null) {
152 // FIXME: the result moved, make sure we enforce again
153 return delegate.applyTouch(modification, currentMeta, version);
157 protected TreeNode applyWrite(final ModifiedNode modification, final Optional<TreeNode> currentMeta,
158 final Version version) {
159 final TreeNode validated = modification.getValidatedNode(this, currentMeta);
160 if (validated != null) {
164 // FIXME: the result moved, make sure we enforce again
165 return delegate.applyWrite(modification, currentMeta, version);
169 protected ChildTrackingPolicy getChildPolicy() {
170 return delegate.getChildPolicy();
174 void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode<?, ?> value, final Version version) {
175 delegate.mergeIntoModifiedNode(node, value, version);
179 void recursivelyVerifyStructure(NormalizedNode<?, ?> value) {
180 delegate.recursivelyVerifyStructure(value);