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",
103 private static boolean checkOriginalPresent(ModifiedNode child) {
104 return child.getOriginal().isPresent();
108 protected void checkTouchApplicable(final YangInstanceIdentifier path, final NodeModification modification,
109 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
110 delegate.checkTouchApplicable(path, modification, current, version);
111 checkMinMaxElements(path, modification, current, version);
115 protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification,
116 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
117 delegate.checkMergeApplicable(path, modification, current, version);
118 checkMinMaxElements(path, modification, current, version);
122 protected void checkWriteApplicable(final YangInstanceIdentifier path, final NodeModification modification,
123 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
124 delegate.checkWriteApplicable(path, modification, current, version);
125 checkMinMaxElements(path, modification, current, version);
129 public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
130 return delegate.getChild(child);
134 protected void verifyStructure(final NormalizedNode<?, ?> modification, final boolean verifyChildren) {
135 delegate.verifyStructure(modification, verifyChildren);
139 protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
140 final TreeNode validated = modification.getValidatedNode(this, Optional.of(currentMeta));
141 if (validated != null) {
145 // FIXME: the result moved, make sure we enforce again
146 return delegate.applyMerge(modification, currentMeta, version);
150 protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
151 final TreeNode validated = modification.getValidatedNode(this, Optional.of(currentMeta));
152 if (validated != null) {
156 // FIXME: the result moved, make sure we enforce again
157 return delegate.applyTouch(modification, currentMeta, version);
161 protected TreeNode applyWrite(final ModifiedNode modification, final Optional<TreeNode> currentMeta,
162 final Version version) {
163 final TreeNode validated = modification.getValidatedNode(this, currentMeta);
164 if (validated != null) {
168 // FIXME: the result moved, make sure we enforce again
169 return delegate.applyWrite(modification, currentMeta, version);
173 protected ChildTrackingPolicy getChildPolicy() {
174 return delegate.getChildPolicy();
178 void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode<?, ?> value, final Version version) {
179 delegate.mergeIntoModifiedNode(node, value, version);
183 void recursivelyVerifyStructure(NormalizedNode<?, ?> value) {
184 delegate.recursivelyVerifyStructure(value);