2 * Copyright (c) 2021 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.common;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.collect.Maps;
13 import java.util.Arrays;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
19 * Enumeration of {@code error-type} values. These provide glue between {@link NetconfLayer} and various sources of
20 * such errors. This enumeration is not extensible in YANG as it is modeled in
21 * <a href="https://datatracker.ietf.org/doc/html/rfc8040#section-3.9">RFC8040</a>.
24 public enum ErrorType {
26 * A {@link NetconfLayer#TRANSPORT} layer error. This typically happens on transport endpoints, where a protocol
27 * plugin needs to report a NETCONF-equivalent condition.
29 TRANSPORT("transport", NetconfLayer.TRANSPORT, RpcError.ErrorType.TRANSPORT),
31 * A {@link NetconfLayer#RPC} layer error. This typically happens on request routers, where a request may end up
32 * being resolved due to implementation-internal causes, such as timeouts and state loss.
34 RPC("rpc", NetconfLayer.RPC, RpcError.ErrorType.RPC),
36 * A {@link NetconfLayer#OPERATIONS} layer error. These typically happen in a NETCONF protocol implementation.
38 PROTOCOL("protocol", NetconfLayer.OPERATIONS, RpcError.ErrorType.PROTOCOL),
40 * A {@link NetconfLayer#CONTENT} layer error. These typically happen due to YANG data handling, such as
41 * type checking and structural consistency.
43 APPLICATION("application", NetconfLayer.CONTENT, RpcError.ErrorType.APPLICATION);
45 private static final Map<String, ErrorType> BY_ELEMENT_BODY =
46 Maps.uniqueIndex(Arrays.asList(values()), ErrorType::elementBody);
48 private final RpcError.ErrorType legacy;
49 private final String elementBody;
50 private final NetconfLayer layer;
52 ErrorType(final String elementName, final NetconfLayer layer, final RpcError.ErrorType legacy) {
53 this.elementBody = requireNonNull(elementName);
54 this.layer = requireNonNull(layer);
55 this.legacy = requireNonNull(legacy);
59 * Return the XML element body of this object.
61 * @return element body of this object
63 public String elementBody() {
68 * Return the {@link NetconfLayer} corresponding to this error type.
70 * @return A NETCONF layer
72 public final NetconfLayer layer() {
77 public final RpcError.ErrorType toLegacy() {
81 public static @Nullable ErrorType forElementBody(final String elementBody) {
82 return BY_ELEMENT_BODY.get(requireNonNull(elementBody));