2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 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 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;
26 final class MinMaxElementsValidation extends SchemaAwareApplyOperation {
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;
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;
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)) {
45 return new MinMaxElementsValidation(delegate, constraints.getMinElements(), constraints.getMaxElements());
49 private void checkMinMaxElements(final YangInstanceIdentifier path, final NodeModification nodeMod,
50 final Optional<TreeNode> current) throws DataValidationFailedException {
51 if (!(nodeMod instanceof ModifiedNode)) {
52 LOG.debug("Could not validate {}, does not implement expected class {}", nodeMod, ModifiedNode.class);
55 final ModifiedNode modification = (ModifiedNode) nodeMod;
56 final int childrenBefore;
57 if (current.isPresent()) {
58 childrenBefore = numOfChildrenFromValue(current.get().getData());
63 final int childrenAfter;
64 if (modification.getWrittenValue() != null) {
65 childrenAfter = numOfChildrenFromValue(modification.getWrittenValue());
70 final int childrenTotal = childrenBefore + childrenAfter + numOfChildrenFromChildMods(modification, current);
71 if (minElements != null && minElements > childrenTotal) {
72 throw new DataValidationFailedException(path, String.format(
73 "%s does not have enough elements (%s), needs at least %s", modification.getIdentifier(),
74 childrenTotal, minElements));
76 if (maxElements != null && maxElements < childrenTotal) {
77 throw new DataValidationFailedException(path, String.format(
78 "%s has too many elements (%s), can have at most %s", modification.getIdentifier(), childrenTotal,
83 private static int numOfChildrenFromValue(final NormalizedNode<?, ?> value) {
84 if (value instanceof NormalizedNodeContainer) {
85 return ((NormalizedNodeContainer<?, ?, ?>) value).getValue().size();
86 } else if (value instanceof UnkeyedListNode) {
87 return ((UnkeyedListNode) value).getSize();
90 throw new IllegalArgumentException(String.format(
91 "Unexpected type '%s', expected types are NormalizedNodeContainer and UnkeyedListNode",
95 private static int numOfChildrenFromChildMods(final ModifiedNode modification, final Optional<TreeNode> current) {
97 for (final ModifiedNode modChild : modification.getChildren()) {
98 switch (modChild.getOperation()) {
103 if (!current.isPresent()) {
115 throw new IllegalArgumentException("Unsupported operation type: " + modChild.getOperation());
122 protected void checkTouchApplicable(final YangInstanceIdentifier path, final NodeModification modification,
123 final Optional<TreeNode> current) throws DataValidationFailedException {
124 delegate.checkTouchApplicable(path, modification, current);
125 checkMinMaxElements(path, modification, current);
129 protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification,
130 final Optional<TreeNode> current) throws DataValidationFailedException {
131 delegate.checkMergeApplicable(path, modification, current);
132 checkMinMaxElements(path, modification, current);
136 protected void checkWriteApplicable(final YangInstanceIdentifier path, final NodeModification modification,
137 final Optional<TreeNode> current) throws DataValidationFailedException {
138 delegate.checkWriteApplicable(path, modification, current);
139 checkMinMaxElements(path, modification, current);
144 public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
145 return delegate.getChild(child);
149 void verifyStructure(final ModifiedNode modification) throws IllegalArgumentException {
150 delegate.verifyStructure(modification);
154 protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
155 return delegate.applyMerge(modification, currentMeta, version);
159 protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
160 return delegate.applyTouch(modification, currentMeta, version);
164 protected TreeNode applyWrite(final ModifiedNode modification, final Optional<TreeNode> currentMeta,
165 final Version version) {
166 return delegate.applyWrite(modification, currentMeta, version);
170 protected void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
171 delegate.verifyWrittenStructure(writtenValue);
175 protected ChildTrackingPolicy getChildPolicy() {
176 return delegate.getChildPolicy();