Add yang.common.Error{Severity,Tag,Type} constructs
[yangtools.git] / common / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / ErrorType.java
1 /*
2  * Copyright (c) 2021 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.common;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.Maps;
13 import java.util.Arrays;
14 import java.util.Map;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17
18 /**
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>.
22  */
23 @NonNullByDefault
24 public enum ErrorType {
25     /**
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.
28      */
29     TRANSPORT("transport", NetconfLayer.TRANSPORT, RpcError.ErrorType.TRANSPORT),
30     /**
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.
33      */
34     RPC("rpc", NetconfLayer.RPC, RpcError.ErrorType.RPC),
35     /**
36      * A {@link NetconfLayer#OPERATIONS} layer error. These typically happen in a NETCONF protocol implementation.
37      */
38     PROTOCOL("protocol", NetconfLayer.OPERATIONS, RpcError.ErrorType.PROTOCOL),
39     /**
40      * A {@link NetconfLayer#CONTENT} layer error. These typically happen due to YANG data handling, such as
41      * type checking and structural consistency.
42      */
43     APPLICATION("application", NetconfLayer.CONTENT, RpcError.ErrorType.APPLICATION);
44
45     private static final Map<String, ErrorType> BY_ELEMENT_BODY =
46         Maps.uniqueIndex(Arrays.asList(values()), ErrorType::elementBody);
47
48     private final RpcError.ErrorType legacy;
49     private final String elementBody;
50     private final NetconfLayer layer;
51
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);
56     }
57
58     /**
59      * Return the XML element body of this object.
60      *
61      * @return element body of this object
62      */
63     public String elementBody() {
64         return elementBody;
65     }
66
67     /**
68      * Return the {@link NetconfLayer} corresponding to this error type.
69      *
70      * @return A NETCONF layer
71      */
72     public final NetconfLayer layer() {
73         return layer;
74     }
75
76     @Deprecated
77     public final RpcError.ErrorType toLegacy() {
78         return legacy;
79     }
80
81     public static @Nullable ErrorType forElementBody(final String elementBody) {
82         return BY_ELEMENT_BODY.get(requireNonNull(elementBody));
83     }
84 }