Rename binding-runtime-dynamic to binding-loader
[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://www.rfc-editor.org/rfc/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),
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),
35     /**
36      * A {@link NetconfLayer#OPERATIONS} layer error. These typically happen in a NETCONF protocol implementation.
37      */
38     PROTOCOL("protocol", NetconfLayer.OPERATIONS),
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);
44
45     private static final Map<String, ErrorType> BY_ELEMENT_BODY =
46         Maps.uniqueIndex(Arrays.asList(values()), ErrorType::elementBody);
47
48     private final String elementBody;
49     private final NetconfLayer layer;
50
51     ErrorType(final String elementName, final NetconfLayer layer) {
52         this.elementBody = requireNonNull(elementName);
53         this.layer = requireNonNull(layer);
54     }
55
56     /**
57      * Return the XML element body of this object.
58      *
59      * @return element body of this object
60      */
61     public String elementBody() {
62         return elementBody;
63     }
64
65     /**
66      * Return the {@link NetconfLayer} corresponding to this error type.
67      *
68      * @return A NETCONF layer
69      */
70     public final NetconfLayer layer() {
71         return layer;
72     }
73
74     public static @Nullable ErrorType forElementBody(final String elementBody) {
75         return BY_ELEMENT_BODY.get(requireNonNull(elementBody));
76     }
77 }