ErrorTag should be Serializable
[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 missing-attribute} {@link ErrorTag}.
80      */
81     public static final ErrorTag MISSING_ATTRIBUTE = new ErrorTag("missing-attribute");
82     /**
83      * {@code missing-element} {@link ErrorTag}. Covers mechanics specified in RFC6020 section 8.3.1, bullet 2
84      * <pre>
85      *   If all keys of a list entry are not present, the server MUST reply
86      *   with a "missing-element" error-tag in the rpc-error.
87      * </pre>
88      * as well as RFC6020 section 7.13.2, paragraph 2
89      * <pre>
90      *   If a leaf in the input tree has a "mandatory" statement with the
91      *   value "true", the leaf MUST be present in a NETCONF RPC invocation.
92      *   Otherwise, the server MUST return a "missing-element" error.
93      * </pre>
94      */
95     public static final ErrorTag MISSING_ELEMENT = new ErrorTag("missing-element");
96     /**
97      * {@code operation-failed} {@link ErrorTag}. Covers mechanics specified in RFC6020 section 13.1 through 13.4.
98      */
99     public static final ErrorTag OPERATION_FAILED = new ErrorTag("operation-failed");
100     /**
101      * {@code operation-not-supported} {@link ErrorTag}.
102      */
103     public static final ErrorTag OPERATION_NOT_SUPPORTED = new ErrorTag("operation-not-supported");
104     /**
105      * {@code partial-operation} {@link ErrorTag}.
106      * @deprecated This error-info is obsolete since RFC6241.
107      */
108     @Deprecated(since = "RFC6241")
109     public static final ErrorTag PARTIAL_OPERATION = new ErrorTag("partial-operation");
110     /**
111      * {@code resource-denied} {@link ErrorTag}.
112      */
113     public static final ErrorTag RESOURCE_DENIED = new ErrorTag("resource-denied");
114     /**
115      * {@code rollback-failed} {@link ErrorTag}.
116      */
117     public static final ErrorTag ROLLBACK_FAILED = new ErrorTag("rollback-failed");
118     /**
119      * {@code too-big} {@link ErrorTag}.
120      */
121     public static final ErrorTag TOO_BIG = new ErrorTag("too-big");
122     /**
123      * {@code unknown-element} {@link ErrorTag}. Covers mechanics specified in RFC6020 section 8.3.1, bullet 7
124      * <pre>
125      *   If the attributes "before" and "after" appears in any element that
126      *   is not a list whose "ordered-by" property is "user", the server
127      *   MUST reply with an "unknown-attribute" error-tag in the rpc-error.
128      * </pre>
129      */
130     public static final ErrorTag UNKNOWN_ATTRIBUTE = new ErrorTag("unknown-attribute");
131     /**
132      * {@code unknown-element} {@link ErrorTag}. Covers mechanics specified in RFC6020 section 8.3.1, bullet 4
133      * <pre>
134      *   If data for a node tagged with "if-feature" is present, and the
135      *   feature is not supported by the device, the server MUST reply with
136      *   an "unknown-element" error-tag in the rpc-error.
137      * </pre>
138      * as well as RFC6020 section 8.3.1, bullet 5
139      * <pre>
140      *   If data for a node tagged with "when" is present, and the "when"
141      *   condition evaluates to "false", the server MUST reply with an
142      *   "unknown-element" error-tag in the rpc-error.
143      * </pre>
144      */
145     public static final ErrorTag UNKNOWN_ELEMENT = new ErrorTag("unknown-element");
146     /**
147      * {@code unknown-namespace} {@link ErrorTag}.
148      */
149     public static final ErrorTag UNKNOWN_NAMESPACE = new ErrorTag("unknown-namespace");
150
151     private static final long serialVersionUID = 1L;
152
153     private final String elementBody;
154
155     public ErrorTag(final String elementBody) {
156         this.elementBody = requireNonNull(elementBody);
157     }
158
159     /**
160      * Return the XML element body of this object.
161      *
162      * @return element body of this object
163      */
164     public String elementBody() {
165         return elementBody;
166     }
167
168     @Override
169     public int hashCode() {
170         return elementBody.hashCode();
171     }
172
173     @Override
174     public boolean equals(final @Nullable Object obj) {
175         return obj == this || obj instanceof ErrorTag && elementBody.equals(((ErrorTag) obj).elementBody);
176     }
177
178     @Override
179     public String toString() {
180         return elementBody;
181     }
182
183     Object writeReplace() {
184         return new ETv1(this);
185     }
186 }