2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.common.errors;
10 import static java.util.Objects.requireNonNull;
12 import java.io.Serializable;
13 import java.util.Locale;
14 import org.opendaylight.yangtools.yang.common.RpcError;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
20 * Encapsulates a restconf error as defined in the ietf restconf draft.
24 * <b>Note:</b> Enumerations defined within are provided by the ietf restconf draft.
27 * See also <a href="https://tools.ietf.org/html/draft-bierman-netconf-restconf-02">RESTCONF</a>.
29 public class RestconfError implements Serializable {
30 private static final Logger LOG = LoggerFactory.getLogger(RestconfError.class);
31 private static final long serialVersionUID = 1L;
33 public enum ErrorType {
35 * Errors relating to the transport layer.
39 * Errors relating to the RPC or notification layer.
43 * Errors relating to the protocol operation layer.
47 * Errors relating to the server application layer.
51 public String getErrorTypeTag() {
52 return name().toLowerCase(Locale.ROOT);
55 public static ErrorType valueOfCaseInsensitive(final String value) {
57 return ErrorType.valueOf(ErrorType.class, value.toUpperCase(Locale.ROOT));
58 } catch (IllegalArgumentException e) {
64 public enum ErrorTag {
65 IN_USE("in-use", 409 /* Conflict */),
66 INVALID_VALUE("invalid-value", 400 /* Bad Request */),
67 TOO_BIG("too-big", 413 /* Request Entity Too Large */),
68 MISSING_ATTRIBUTE("missing-attribute", 400 /* Bad Request */),
69 BAD_ATTRIBUTE("bad-attribute", 400 /* Bad Request */),
70 UNKNOWN_ATTRIBUTE("unknown-attribute", 400 /* Bad Request */),
71 MISSING_ELEMENT("missing-element", 400 /* Bad Request */),
72 BAD_ELEMENT("bad-element", 400 /* Bad Request */),
73 UNKNOWN_ELEMENT("unknown-element", 400 /* Bad Request */),
74 UNKNOWN_NAMESPACE("unknown-namespace", 400 /* Bad Request */),
75 ACCESS_DENIED("access-denied", 403 /* Forbidden */),
76 LOCK_DENIED("lock-denied", 409 /* Conflict */),
77 RESOURCE_DENIED("resource-denied", 409 /* Conflict */),
78 ROLLBACK_FAILED("rollback-failed", 500 /* INTERNAL_SERVER_ERROR */),
79 DATA_EXISTS("data-exists", 409 /* Conflict */),
80 DATA_MISSING("data-missing", dataMissingHttpStatus()),
81 OPERATION_NOT_SUPPORTED("operation-not-supported", 501 /* Not Implemented */),
82 OPERATION_FAILED("operation-failed", 500 /* INTERNAL_SERVER_ERROR */),
83 PARTIAL_OPERATION("partial-operation", 500 /* INTERNAL_SERVER_ERROR */),
84 MALFORMED_MESSAGE("malformed-message", 400 /* Bad Request */),
85 RESOURCE_DENIED_TRANSPORT("resource-denied-transport", 503 /* Service Unavailable */);
87 private final String tagValue;
88 private final int statusCode;
90 ErrorTag(final String tagValue, final int statusCode) {
91 this.tagValue = tagValue;
92 this.statusCode = statusCode;
95 public String getTagValue() {
96 return this.tagValue.toLowerCase(Locale.ROOT);
99 public static ErrorTag valueOfCaseInsensitive(final String value) {
101 return ErrorTag.valueOf(ErrorTag.class, value.toUpperCase(Locale.ROOT).replaceAll("-", "_"));
102 } catch (IllegalArgumentException e) {
103 return OPERATION_FAILED;
107 public int getStatusCode() {
111 private static int dataMissingHttpStatus() {
112 // Control over the HTTP status reported on "data-missing" conditions. This defaults to disabled,
113 // HTTP status 409 as specified by RFC8040 (and all previous drafts). See the discussion in:
114 // https://www.rfc-editor.org/errata/eid5565
115 // https://mailarchive.ietf.org/arch/msg/netconf/hkVDdHK4xA74NgvXzWP0zObMiyY/
116 final String propName = "org.opendaylight.restconf.eid5565";
117 final String propValue = System.getProperty(propName, "disabled");
120 // RFC7231 interpretation: 404 Not Found
121 LOG.info("RESTCONF data-missing condition is reported as HTTP status 404 (Errata 5565)");
126 LOG.warn("Unhandled {} value \"{}\", assuming disabled", propName, propValue);
129 // RFC8040 specification: 409 Conflict
134 private final ErrorType errorType;
135 private final ErrorTag errorTag;
136 private final String errorInfo;
137 private final String errorAppTag;
138 private final String errorMessage;
139 private final YangInstanceIdentifier errorPath;
142 * Constructs a RestConfError.
145 * The enumerated type indicating the layer where the error occurred.
147 * The enumerated tag representing a more specific error cause.
148 * @param errorMessage
149 * A string which provides a plain text string describing the error.
151 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage) {
152 this(errorType, errorTag, errorMessage, null, null, null);
156 * Constructs a RestConfError object.
159 * The enumerated type indicating the layer where the error occurred.
161 * The enumerated tag representing a more specific error cause.
162 * @param errorMessage
163 * A string which provides a plain text string describing the error.
165 * A string which represents an application-specific error tag that further specifies the error cause.
167 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
168 final String errorAppTag) {
169 this(errorType, errorTag, errorMessage, errorAppTag, null, null);
173 * Constructs a RestConfError object.
176 * The enumerated type indicating the layer where the error occurred.
178 * The enumerated tag representing a more specific error cause.
179 * @param errorMessage
180 * A string which provides a plain text string describing the error.
182 * An instance identifier which contains error path
184 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
185 final YangInstanceIdentifier errorPath) {
186 this(errorType, errorTag, errorMessage, null, null, errorPath);
190 * Constructs a RestConfError object.
193 * The enumerated type indicating the layer where the error occurred.
195 * The enumerated tag representing a more specific error cause.
196 * @param errorMessage
197 * A string which provides a plain text string describing the error.
199 * A string which represents an application-specific error tag that further specifies the error cause.
201 * A string, <b>formatted as XML</b>, which contains additional error information.
203 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
204 final String errorAppTag, final String errorInfo) {
205 this(errorType, errorTag, errorMessage, errorAppTag, errorInfo, null);
209 * Constructs a RestConfError object.
212 * The enumerated type indicating the layer where the error occurred.
214 * The enumerated tag representing a more specific error cause.
215 * @param errorMessage
216 * A string which provides a plain text string describing the error.
218 * A string which represents an application-specific error tag that further specifies the error cause.
220 * A string, <b>formatted as XML</b>, which contains additional error information.
222 * An instance identifier which contains error path
224 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
225 final String errorAppTag, final String errorInfo, final YangInstanceIdentifier errorPath) {
226 this.errorType = requireNonNull(errorType, "Error type is required for RestConfError");
227 this.errorTag = requireNonNull(errorTag, "Error tag is required for RestConfError");
228 this.errorMessage = errorMessage;
229 this.errorAppTag = errorAppTag;
230 this.errorInfo = errorInfo;
231 this.errorPath = errorPath;
235 * Constructs a RestConfError object from an RpcError.
237 public RestconfError(final RpcError rpcError) {
239 this.errorType = rpcError.getErrorType() == null ? ErrorType.APPLICATION : ErrorType
240 .valueOfCaseInsensitive(rpcError.getErrorType().name());
242 this.errorTag = rpcError.getTag() == null ? ErrorTag.OPERATION_FAILED : ErrorTag
243 .valueOfCaseInsensitive(rpcError.getTag());
245 this.errorMessage = rpcError.getMessage();
246 this.errorAppTag = rpcError.getApplicationTag();
248 String localErrorInfo = null;
249 if (rpcError.getInfo() == null) {
250 if (rpcError.getCause() != null) {
251 localErrorInfo = rpcError.getCause().getMessage();
252 } else if (rpcError.getSeverity() != null) {
253 localErrorInfo = "<severity>" + rpcError.getSeverity().toString().toLowerCase(Locale.ROOT)
257 localErrorInfo = rpcError.getInfo();
260 this.errorInfo = localErrorInfo;
261 this.errorPath = null;
264 public ErrorType getErrorType() {
268 public ErrorTag getErrorTag() {
272 public String getErrorInfo() {
276 public String getErrorAppTag() {
280 public String getErrorMessage() {
284 public YangInstanceIdentifier getErrorPath() {
289 public String toString() {
290 return "RestconfError ["
291 + "error-type: " + errorType.getErrorTypeTag() + ", error-tag: " + errorTag.getTagValue()
292 + (errorAppTag != null ? ", error-app-tag: " + errorAppTag : "")
293 + (errorMessage != null ? ", error-message: " + errorMessage : "")
294 + (errorInfo != null ? ", error-info: " + errorInfo : "")
295 + (errorPath != null ? ", error-path: " + errorPath.toString() : "")