a75cfc2649395f18bae3614f9cb8a156020c0ed4
[yangtools.git] /
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, 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.api.codec;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import java.util.List;
14 import java.util.Optional;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
18 import org.opendaylight.yangtools.yang.common.ErrorTag;
19 import org.opendaylight.yangtools.yang.common.RpcError;
20 import org.opendaylight.yangtools.yang.common.YangError;
21 import org.opendaylight.yangtools.yang.data.api.ImmutableYangNetconfError;
22 import org.opendaylight.yangtools.yang.data.api.YangNetconfError;
23 import org.opendaylight.yangtools.yang.data.api.YangNetconfErrorAware;
24 import org.opendaylight.yangtools.yang.model.api.ConstraintMetaDefinition;
25
26 /**
27  * Dedicated exception for reporting conditions where {@code error-tag} should be reported as {@code invalid-value}.
28  * This is covered by <a href="https://tools.ietf.org/html/rfc7950#section-8.3.1">RFC7950 section 8.3.1</a>:
29  * <pre>
30  *   If a leaf data value does not match the type constraints for the
31  *   leaf, including those defined in the type's "range", "length", and
32  *   "pattern" properties, the server MUST reply with an
33  *   "invalid-value" &lt;error-tag&gt; in the &lt;rpc-error&gt;, and with the
34  *   error-app-tag (Section 7.5.4.2) and error-message
35  *   (Section 7.5.4.1) associated with the constraint, if any exist.
36  * </pre>
37  *
38  * <p>
39  * This error tag also references <a href="https://tools.ietf.org/html/rfc6241#appendix-A">RFC6241 Appendix A</a>,
40  * which defines the appropriate severity and adds more semantics.
41  */
42 @Beta
43 public class YangInvalidValueException extends IllegalArgumentException implements YangError, YangNetconfErrorAware {
44     private static final long serialVersionUID = 1L;
45
46     private final RpcError.@NonNull ErrorType errorType;
47     private final @Nullable String errorAppTag;
48     private final @Nullable String errorMessage;
49
50     public YangInvalidValueException(final RpcError.ErrorType errorType, final ConstraintMetaDefinition constraint,
51             final String message) {
52         super(requireNonNull(message));
53         this.errorType = requireNonNull(errorType);
54         this.errorAppTag = constraint.getErrorAppTag().orElse(null);
55         this.errorMessage = constraint.getErrorMessage().orElse(null);
56     }
57
58     @Deprecated
59     @Override
60     public final RpcError.ErrorType getErrorType() {
61         return errorType;
62     }
63
64     @Deprecated
65     @Override
66     public final RpcError.ErrorSeverity getSeverity() {
67         return RpcError.ErrorSeverity.ERROR;
68     }
69
70     @Deprecated
71     @Override
72     public final String getErrorTag() {
73         return "invalid-value";
74     }
75
76     @Deprecated
77     @Override
78     public final Optional<String> getErrorAppTag() {
79         return Optional.ofNullable(errorAppTag);
80     }
81
82     @Deprecated
83     @Override
84     public final Optional<String> getErrorMessage() {
85         return Optional.ofNullable(errorMessage);
86     }
87
88     @Override
89     public List<YangNetconfError> getNetconfErrors() {
90         return List.of(ImmutableYangNetconfError.builder()
91             .severity(ErrorSeverity.ERROR)
92             .type(errorType.toNetconf())
93             .tag(ErrorTag.INVALID_VALUE)
94             .message(errorMessage)
95             .appTag(errorAppTag)
96             .build());
97     }
98 }