7f6831a9d0fdb612ea2cfde51a1820caa28edba9
[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 terms of the Eclipse
5  * Public License v1.0 which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
9
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
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.ConstraintDefinition;
22 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 final class MinMaxElementsValidation extends SchemaAwareApplyOperation {
27
28     private static final Logger LOG = LoggerFactory.getLogger(MinMaxElementsValidation.class);
29     private final SchemaAwareApplyOperation delegate;
30     private final Integer minElements;
31     private final Integer maxElements;
32
33     private MinMaxElementsValidation(final SchemaAwareApplyOperation delegate, final Integer minElements,
34             final Integer maxElements) {
35         this.delegate = Preconditions.checkNotNull(delegate);
36         this.minElements = minElements;
37         this.maxElements = maxElements;
38     }
39
40     static final SchemaAwareApplyOperation from(final SchemaAwareApplyOperation delegate, final DataSchemaNode schema) {
41         final ConstraintDefinition constraints = schema.getConstraints();
42         if (constraints == null || (constraints.getMinElements() == null && constraints.getMaxElements() == null)) {
43             return delegate;
44         }
45         return new MinMaxElementsValidation(delegate, constraints.getMinElements(), constraints.getMaxElements());
46
47     }
48
49     private final int findChildrenBefore(final Optional<TreeNode> current) {
50         final int children;
51         if (current.isPresent()) {
52           children = numOfChildrenFromValue(current.get().getData());
53         } else {
54           children = 0;
55         }
56         return children;
57     }
58
59     private final int findChildrenAfter(final ModifiedNode modification) {
60       final int children;
61       if (modification.getWrittenValue() != null) {
62         children = numOfChildrenFromValue(modification.getWrittenValue());
63       } else {
64         children = 0;
65       }
66       return children;
67   }
68
69     private void checkMinMaxElements(final YangInstanceIdentifier path, final NodeModification nodeMod,
70             final Optional<TreeNode> current) throws DataValidationFailedException {
71         if (!(nodeMod instanceof ModifiedNode)) {
72             LOG.debug("Could not validate {}, does not implement expected class {}", nodeMod, ModifiedNode.class);
73             return;
74         }
75         final ModifiedNode modification = (ModifiedNode) nodeMod;
76
77         final int childrenBefore = findChildrenBefore(current);
78
79         final int childrenAfter = findChildrenAfter(modification);
80
81         final int childrenTotal = childrenBefore + childrenAfter + numOfChildrenFromChildMods(modification, current);
82         if (minElements != null && minElements > childrenTotal) {
83             throw new DataValidationFailedException(path, String.format(
84                     "%s does not have enough elements (%s), needs at least %s", modification.getIdentifier(),
85                     childrenTotal, minElements));
86         }
87         if (maxElements != null && maxElements < childrenTotal) {
88             throw new DataValidationFailedException(path, String.format(
89                     "%s has too many elements (%s), can have at most %s", modification.getIdentifier(), childrenTotal,
90                     maxElements));
91         }
92     }
93
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();
99         }
100
101         throw new IllegalArgumentException(String.format(
102                 "Unexpected type '%s', expected types are NormalizedNodeContainer and UnkeyedListNode",
103                 value.getClass()));
104     }
105
106     private static int numOfChildrenFromChildMods(final ModifiedNode modification, final Optional<TreeNode> current) {
107         int result = 0;
108         for (final ModifiedNode modChild : modification.getChildren()) {
109             switch (modChild.getOperation()) {
110                 case WRITE:
111                     result++;
112                     break;
113                 case MERGE:
114                     if (!current.isPresent()) {
115                         result++;
116                     }
117                     break;
118                 case DELETE:
119                     result--;
120                     break;
121                 case NONE:
122                 case TOUCH:
123                     // NOOP
124                     break;
125                 default:
126                     throw new IllegalArgumentException("Unsupported operation type: " + modChild.getOperation());
127             }
128         }
129         return result;
130     }
131
132     @Override
133     protected void checkTouchApplicable(final YangInstanceIdentifier path, final NodeModification modification,
134             final Optional<TreeNode> current) throws DataValidationFailedException {
135         delegate.checkTouchApplicable(path, modification, current);
136         checkMinMaxElements(path, modification, current);
137     }
138
139     @Override
140     protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification,
141             final Optional<TreeNode> current) throws DataValidationFailedException {
142         delegate.checkMergeApplicable(path, modification, current);
143         checkMinMaxElements(path, modification, current);
144     }
145
146     @Override
147     protected void checkWriteApplicable(final YangInstanceIdentifier path, final NodeModification modification,
148             final Optional<TreeNode> current) throws DataValidationFailedException {
149         delegate.checkWriteApplicable(path, modification, current);
150         checkMinMaxElements(path, modification, current);
151     }
152
153
154     @Override
155     public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
156         return delegate.getChild(child);
157     }
158
159     @Override
160     void verifyStructure(final ModifiedNode modification) throws IllegalArgumentException {
161         delegate.verifyStructure(modification);
162     }
163
164     @Override
165     protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
166         return delegate.applyMerge(modification, currentMeta, version);
167     }
168
169     @Override
170     protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
171         return delegate.applyTouch(modification, currentMeta, version);
172     }
173
174     @Override
175     protected TreeNode applyWrite(final ModifiedNode modification, final Optional<TreeNode> currentMeta,
176             final Version version) {
177         return delegate.applyWrite(modification, currentMeta, version);
178     }
179
180     @Override
181     protected void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
182         delegate.verifyWrittenStructure(writtenValue);
183     }
184
185     @Override
186     protected ChildTrackingPolicy getChildPolicy() {
187         return delegate.getChildPolicy();
188     }
189 }