2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.api.codec;
10 import static java.util.Objects.requireNonNull;
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;
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>:
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" <error-tag> in the <rpc-error>, 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.
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.
43 public class YangInvalidValueException extends IllegalArgumentException implements YangError, YangNetconfErrorAware {
44 private static final long serialVersionUID = 1L;
46 private final RpcError.@NonNull ErrorType errorType;
47 private final @Nullable String errorAppTag;
48 private final @Nullable String errorMessage;
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);
60 public final RpcError.ErrorType getErrorType() {
66 public final RpcError.ErrorSeverity getSeverity() {
67 return RpcError.ErrorSeverity.ERROR;
72 public final String getErrorTag() {
73 return "invalid-value";
78 public final Optional<String> getErrorAppTag() {
79 return Optional.ofNullable(errorAppTag);
84 public final Optional<String> getErrorMessage() {
85 return Optional.ofNullable(errorMessage);
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)