Merge "Removed unused dependency."
[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 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 static int findChildrenBefore(final Optional<TreeNode> current) {
50         if (current.isPresent()) {
51             return numOfChildrenFromValue(current.get().getData());
52         } else {
53             return 0;
54         }
55     }
56
57     private static int findChildrenAfter(final ModifiedNode modification) {
58         if (modification.getWrittenValue() != null) {
59             return numOfChildrenFromValue(modification.getWrittenValue());
60         } else {
61             return 0;
62         }
63     }
64
65     private void checkMinMaxElements(final YangInstanceIdentifier path, final NodeModification nodeMod,
66             final Optional<TreeNode> current) 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         final ModifiedNode modification = (ModifiedNode) nodeMod;
72
73         final int childrenBefore = findChildrenBefore(current);
74
75         final int childrenAfter = findChildrenAfter(modification);
76
77         final int childrenTotal = childrenBefore + childrenAfter + numOfChildrenFromChildMods(modification, current);
78         if (minElements != null && minElements > childrenTotal) {
79             throw new DataValidationFailedException(path, String.format(
80                     "%s does not have enough elements (%s), needs at least %s", modification.getIdentifier(),
81                     childrenTotal, minElements));
82         }
83         if (maxElements != null && maxElements < childrenTotal) {
84             throw new DataValidationFailedException(path, String.format(
85                     "%s has too many elements (%s), can have at most %s", modification.getIdentifier(), childrenTotal,
86                     maxElements));
87         }
88     }
89
90     private static int numOfChildrenFromValue(final NormalizedNode<?, ?> value) {
91         if (value instanceof NormalizedNodeContainer) {
92             return ((NormalizedNodeContainer<?, ?, ?>) value).getValue().size();
93         } else if (value instanceof UnkeyedListNode) {
94             return ((UnkeyedListNode) value).getSize();
95         }
96
97         throw new IllegalArgumentException(String.format(
98                 "Unexpected type '%s', expected types are NormalizedNodeContainer and UnkeyedListNode",
99                 value.getClass()));
100     }
101
102     private static int numOfChildrenFromChildMods(final ModifiedNode modification, final Optional<TreeNode> current) {
103         int result = 0;
104         for (final ModifiedNode modChild : modification.getChildren()) {
105             switch (modChild.getOperation()) {
106                 case WRITE:
107                     result++;
108                     break;
109                 case MERGE:
110                     if (!current.isPresent()) {
111                         result++;
112                     }
113                     break;
114                 case DELETE:
115                     result--;
116                     break;
117                 case NONE:
118                 case TOUCH:
119                     // NOOP
120                     break;
121                 default:
122                     throw new IllegalArgumentException("Unsupported operation type: " + modChild.getOperation());
123             }
124         }
125         return result;
126     }
127
128     @Override
129     protected void checkTouchApplicable(final YangInstanceIdentifier path, final NodeModification modification,
130             final Optional<TreeNode> current) throws DataValidationFailedException {
131         delegate.checkTouchApplicable(path, modification, current);
132         checkMinMaxElements(path, modification, current);
133     }
134
135     @Override
136     protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification,
137             final Optional<TreeNode> current) throws DataValidationFailedException {
138         delegate.checkMergeApplicable(path, modification, current);
139         checkMinMaxElements(path, modification, current);
140     }
141
142     @Override
143     protected void checkWriteApplicable(final YangInstanceIdentifier path, final NodeModification modification,
144             final Optional<TreeNode> current) throws DataValidationFailedException {
145         delegate.checkWriteApplicable(path, modification, current);
146         checkMinMaxElements(path, modification, current);
147     }
148
149
150     @Override
151     public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
152         return delegate.getChild(child);
153     }
154
155     @Override
156     void verifyStructure(final ModifiedNode modification) throws IllegalArgumentException {
157         delegate.verifyStructure(modification);
158     }
159
160     @Override
161     protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
162         return delegate.applyMerge(modification, currentMeta, version);
163     }
164
165     @Override
166     protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
167         return delegate.applyTouch(modification, currentMeta, version);
168     }
169
170     @Override
171     protected TreeNode applyWrite(final ModifiedNode modification, final Optional<TreeNode> currentMeta,
172             final Version version) {
173         return delegate.applyWrite(modification, currentMeta, version);
174     }
175
176     @Override
177     protected void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
178         delegate.verifyWrittenStructure(writtenValue);
179     }
180
181     @Override
182     protected ChildTrackingPolicy getChildPolicy() {
183         return delegate.getChildPolicy();
184     }
185 }