Remove StoreTreeNodes.getChild()
[yangtools.git] / data / yang-data-tree-api / src / main / java / org / opendaylight / yangtools / yang / data / tree / api / RequiredElementCountException.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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 package org.opendaylight.yangtools.yang.data.tree.api;
9
10 import com.google.common.annotations.Beta;
11 import java.util.List;
12 import java.util.OptionalInt;
13 import org.eclipse.jdt.annotation.NonNullByDefault;
14 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
15 import org.opendaylight.yangtools.yang.common.ErrorTag;
16 import org.opendaylight.yangtools.yang.common.ErrorType;
17 import org.opendaylight.yangtools.yang.data.api.ImmutableYangNetconfError;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangNetconfError;
20 import org.opendaylight.yangtools.yang.data.api.YangNetconfErrorAware;
21
22 /**
23  * Exception thrown when {@code min-elements} or {@code max-element} statement restrictions are violated.
24  *
25  * @author Robert Varga
26  */
27 @Beta
28 @NonNullByDefault
29 public final class RequiredElementCountException extends DataValidationFailedException
30         implements YangNetconfErrorAware {
31     private static final long serialVersionUID = 1L;
32
33     private final int actualCount;
34     private final int minimumCount;
35     private final int maximumCount;
36
37     public RequiredElementCountException(final YangInstanceIdentifier path, final int actualCount,
38             final int minimumCount, final int maximumCount, final String message) {
39         super(path, message);
40         this.minimumCount = minimumCount;
41         this.maximumCount = maximumCount;
42         this.actualCount = actualCount;
43     }
44
45     public RequiredElementCountException(final YangInstanceIdentifier path, final int actualCount,
46             final int minimumCount, final int maximumCount, final String format, final Object... args) {
47         this(path, actualCount, minimumCount, maximumCount, String.format(format, args));
48     }
49
50     public OptionalInt getMinimumCount() {
51         return minimumCount == 0 ? OptionalInt.empty() : OptionalInt.of(minimumCount);
52     }
53
54     public OptionalInt getMaximumCount() {
55         return maximumCount == Integer.MAX_VALUE ? OptionalInt.empty() : OptionalInt.of(maximumCount);
56     }
57
58     public int getActualCount() {
59         return actualCount;
60     }
61
62     @Override
63     public List<YangNetconfError> getNetconfErrors() {
64         final String appTag;
65         if (actualCount < minimumCount) {
66             appTag = "too-few-elements";
67         } else if (actualCount > maximumCount) {
68             appTag = "too-many-elements";
69         } else {
70             throw new IllegalStateException(
71                 "Invalid min " + minimumCount + " max " + maximumCount + " actual " + actualCount);
72         }
73
74         return List.of(ImmutableYangNetconfError.builder()
75             .severity(ErrorSeverity.ERROR)
76             .type(ErrorType.APPLICATION)
77             .tag(ErrorTag.OPERATION_FAILED)
78             .appTag(appTag)
79             .build());
80     }
81 }