Use pattern matching on instanceof in yang-common
[yangtools.git] / common / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / ErrorTag.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 java.io.Serializable;
13 import org.eclipse.jdt.annotation.NonNullByDefault;
14 import org.eclipse.jdt.annotation.Nullable;
15
16 /**
17  * Extensible enumeration of {@code error-tag} values, as defined in
18  * <a href="https://datatracker.ietf.org/doc/html/rfc6241#appendix-A">RFC6241</a>. These values are an extensible
19  * enumeration, since YANG does not place restriction on possible values in
20  * <a href="https://datatracker.ietf.org/doc/html/rfc8040#section-3.9">RFC8040</a>.
21  *
22  * <p>
23  * Error tag defines overall error semantics. Additional tag-specific information may be associated with a particular
24  * error tag.
25  */
26 @NonNullByDefault
27 public final class ErrorTag implements Serializable {
28     /**
29      * {@code access-denied} {@link ErrorTag}.
30      */
31     public static final ErrorTag ACCESS_DENIED = new ErrorTag("access-denied");
32     /**
33      * {@code bad-attribute} {@link ErrorTag}. Covers mechanics specified in RFC6020 section 8.3.1, bullet 6
34      * <pre>
35      *   For insert handling, if the value for the attributes "before" and
36      *   "after" are not valid for the type of the appropriate key leafs,
37      *   the server MUST reply with a "bad-attribute" error-tag in the rpc-
38      *   error.
39      * </pre>
40      */
41     public static final ErrorTag BAD_ATTRIBUTE = new ErrorTag("bad-attribute");
42     /**
43      * {@code bad-element} {@link ErrorTag}. Covers mechanics specified in RFC6020 section 8.3.1, bullet 3
44      * <pre>
45      *   If data for more than one case branch of a choice is present, the
46      *   server MUST reply with a "bad-element" in the rpc-error.
47      * </pre>
48      */
49     public static final ErrorTag BAD_ELEMENT = new ErrorTag("bad-element");
50     /**
51      * {@code data-exists} {@link ErrorTag}.
52      */
53     public static final ErrorTag DATA_EXISTS = new ErrorTag("data-exists");
54     /**
55      * {@code data-missing} {@link ErrorTag}. Covers mechanics specified in RFC6020 sections 13.5 through 13.7.
56      */
57     public static final ErrorTag DATA_MISSING = new ErrorTag("data-missing");
58     /**
59      * {@code in-use} {@link ErrorTag}.
60      */
61     public static final ErrorTag IN_USE = new ErrorTag("in-use");
62     /**
63      * {@code invalid-value} {@link ErrorTag}. Covers mechanics specified in RFC6020 section 8.3.1, bullet 1
64      * <pre>
65      *   If a leaf data value does not match the type constraints for the
66      *   leaf, including those defined in the type's "range", "length", and
67      *   "pattern" properties, the server MUST reply with an
68      *   "invalid-value" error-tag in the rpc-error, and with the error-
69      *   app-tag and error-message associated with the constraint, if any
70      *   exist.
71      * </pre>
72      */
73     public static final ErrorTag INVALID_VALUE = new ErrorTag("invalid-value");
74     /**
75      * {@code lock-denied} {@link ErrorTag}.
76      */
77     public static final ErrorTag LOCK_DENIED = new ErrorTag("lock-denied");
78     /**
79      * {@code unknown-element} {@link ErrorTag}. Covers mechanics specified in
80      * <a href="https://datatracker.ietf.org/doc/html/rfc6241#page-80">RFC6241, Appendix A, last item</a>. Note the
81      * specification expressly forbids reporting this tag for NETCONF clients older than {@code :base:1.1}.
82      */
83     public static final ErrorTag MALFORMED_MESSAGE = new ErrorTag("malformed-message");
84     /**
85      * {@code missing-attribute} {@link ErrorTag}.
86      */
87     public static final ErrorTag MISSING_ATTRIBUTE = new ErrorTag("missing-attribute");
88     /**
89      * {@code missing-element} {@link ErrorTag}. Covers mechanics specified in RFC6020 section 8.3.1, bullet 2
90      * <pre>
91      *   If all keys of a list entry are not present, the server MUST reply
92      *   with a "missing-element" error-tag in the rpc-error.
93      * </pre>
94      * as well as RFC6020 section 7.13.2, paragraph 2
95      * <pre>
96      *   If a leaf in the input tree has a "mandatory" statement with the
97      *   value "true", the leaf MUST be present in a NETCONF RPC invocation.
98      *   Otherwise, the server MUST return a "missing-element" error.
99      * </pre>
100      */
101     public static final ErrorTag MISSING_ELEMENT = new ErrorTag("missing-element");
102     /**
103      * {@code operation-failed} {@link ErrorTag}. Covers mechanics specified in RFC6020 section 13.1 through 13.4.
104      */
105     public static final ErrorTag OPERATION_FAILED = new ErrorTag("operation-failed");
106     /**
107      * {@code operation-not-supported} {@link ErrorTag}.
108      */
109     public static final ErrorTag OPERATION_NOT_SUPPORTED = new ErrorTag("operation-not-supported");
110     /**
111      * {@code partial-operation} {@link ErrorTag}.
112      * @deprecated This error-info is obsolete since RFC6241.
113      */
114     @Deprecated(since = "RFC6241")
115     public static final ErrorTag PARTIAL_OPERATION = new ErrorTag("partial-operation");
116     /**
117      * {@code resource-denied} {@link ErrorTag}.
118      */
119     public static final ErrorTag RESOURCE_DENIED = new ErrorTag("resource-denied");
120     /**
121      * {@code rollback-failed} {@link ErrorTag}.
122      */
123     public static final ErrorTag ROLLBACK_FAILED = new ErrorTag("rollback-failed");
124     /**
125      * {@code too-big} {@link ErrorTag}.
126      */
127     public static final ErrorTag TOO_BIG = new ErrorTag("too-big");
128     /**
129      * {@code unknown-element} {@link ErrorTag}. Covers mechanics specified in RFC6020 section 8.3.1, bullet 7
130      * <pre>
131      *   If the attributes "before" and "after" appears in any element that
132      *   is not a list whose "ordered-by" property is "user", the server
133      *   MUST reply with an "unknown-attribute" error-tag in the rpc-error.
134      * </pre>
135      */
136     public static final ErrorTag UNKNOWN_ATTRIBUTE = new ErrorTag("unknown-attribute");
137     /**
138      * {@code unknown-element} {@link ErrorTag}. Covers mechanics specified in RFC6020 section 8.3.1, bullet 4
139      * <pre>
140      *   If data for a node tagged with "if-feature" is present, and the
141      *   feature is not supported by the device, the server MUST reply with
142      *   an "unknown-element" error-tag in the rpc-error.
143      * </pre>
144      * as well as RFC6020 section 8.3.1, bullet 5
145      * <pre>
146      *   If data for a node tagged with "when" is present, and the "when"
147      *   condition evaluates to "false", the server MUST reply with an
148      *   "unknown-element" error-tag in the rpc-error.
149      * </pre>
150      */
151     public static final ErrorTag UNKNOWN_ELEMENT = new ErrorTag("unknown-element");
152     /**
153      * {@code unknown-namespace} {@link ErrorTag}.
154      */
155     public static final ErrorTag UNKNOWN_NAMESPACE = new ErrorTag("unknown-namespace");
156
157     private static final long serialVersionUID = 1L;
158
159     private final String elementBody;
160
161     public ErrorTag(final String elementBody) {
162         this.elementBody = requireNonNull(elementBody);
163     }
164
165     /**
166      * Return the XML element body of this object.
167      *
168      * @return element body of this object
169      */
170     public String elementBody() {
171         return elementBody;
172     }
173
174     @Override
175     public int hashCode() {
176         return elementBody.hashCode();
177     }
178
179     @Override
180     public boolean equals(final @Nullable Object obj) {
181         return obj == this || obj instanceof ErrorTag other && elementBody.equals(other.elementBody);
182     }
183
184     @Override
185     public String toString() {
186         return elementBody;
187     }
188
189     Object writeReplace() {
190         return new ETv1(this);
191     }
192 }