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