yang.data.impl.schema.tree clean-up
[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     @Override
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);
108     }
109
110     @Override
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);
115     }
116
117     @Override
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);
122     }
123
124     @Override
125     public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
126         return delegate.getChild(child);
127     }
128
129     @Override
130     protected void verifyStructure(final NormalizedNode<?, ?> modification, final boolean verifyChildren) {
131         delegate.verifyStructure(modification, verifyChildren);
132     }
133
134     @Override
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) {
138             return validated;
139         }
140
141         // FIXME: the result moved, make sure we enforce again
142         return delegate.applyMerge(modification, currentMeta, version);
143     }
144
145     @Override
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) {
149             return validated;
150         }
151
152         // FIXME: the result moved, make sure we enforce again
153         return delegate.applyTouch(modification, currentMeta, version);
154     }
155
156     @Override
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) {
161             return validated;
162         }
163
164         // FIXME: the result moved, make sure we enforce again
165         return delegate.applyWrite(modification, currentMeta, version);
166     }
167
168     @Override
169     protected ChildTrackingPolicy getChildPolicy() {
170         return delegate.getChildPolicy();
171     }
172
173     @Override
174     void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode<?, ?> value, final Version version) {
175         delegate.mergeIntoModifiedNode(node, value, version);
176     }
177
178     @Override
179     void recursivelyVerifyStructure(NormalizedNode<?, ?> value) {
180         delegate.recursivelyVerifyStructure(value);
181     }
182 }