BUG-4684: validate changes against effective state
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / MinMaxElementsValidation.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
10
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;
26
27 final class MinMaxElementsValidation extends SchemaAwareApplyOperation {
28
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;
33
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;
39     }
40
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)) {
44             return delegate;
45         }
46         return new MinMaxElementsValidation(delegate, constraints.getMinElements(), constraints.getMaxElements());
47
48     }
49
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));
57         }
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,
61                     maxElements));
62         }
63     }
64
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);
69             return;
70         }
71
72         final ModifiedNode modification = (ModifiedNode) nodeMod;
73
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());
78
79         final TreeNode applied = maybeApplied.get();
80         validateMinMaxElements(path, modification.getIdentifier(), applied.getData());
81
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)
86         //
87         // So let's stash the result. We will pick it up during apply operation.
88         modification.setValidatedNode(this, current, applied);
89     }
90
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();
96         }
97
98         throw new IllegalArgumentException(String.format(
99                 "Unexpected type '%s', expected types are NormalizedNodeContainer and UnkeyedListNode",
100                 value.getClass()));
101     }
102
103     private static boolean checkOriginalPresent(ModifiedNode child) {
104         return child.getOriginal().isPresent();
105     }
106
107     @Override
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);
112     }
113
114     @Override
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);
119     }
120
121     @Override
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);
126     }
127
128     @Override
129     public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
130         return delegate.getChild(child);
131     }
132
133     @Override
134     protected void verifyStructure(final NormalizedNode<?, ?> modification, final boolean verifyChildren) {
135         delegate.verifyStructure(modification, verifyChildren);
136     }
137
138     @Override
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) {
142             return validated;
143         }
144
145         // FIXME: the result moved, make sure we enforce again
146         return delegate.applyMerge(modification, currentMeta, version);
147     }
148
149     @Override
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) {
153             return validated;
154         }
155
156         // FIXME: the result moved, make sure we enforce again
157         return delegate.applyTouch(modification, currentMeta, version);
158     }
159
160     @Override
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) {
165             return validated;
166         }
167
168         // FIXME: the result moved, make sure we enforce again
169         return delegate.applyWrite(modification, currentMeta, version);
170     }
171
172     @Override
173     protected ChildTrackingPolicy getChildPolicy() {
174         return delegate.getChildPolicy();
175     }
176
177     @Override
178     void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode<?, ?> value, final Version version) {
179         delegate.mergeIntoModifiedNode(node, value, version);
180     }
181
182     @Override
183     void recursivelyVerifyStructure(NormalizedNode<?, ?> value) {
184         delegate.recursivelyVerifyStructure(value);
185     }
186 }