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;
18 * Encapsulates a restconf error as defined in the ietf restconf draft.
22 * <b>Note:</b> Enumerations defined within are provided by the ietf restconf draft.
25 * See also <a href="https://tools.ietf.org/html/draft-bierman-netconf-restconf-02">RESTCONF</a>.
27 public class RestconfError implements Serializable {
28 private static final long serialVersionUID = 1L;
30 public enum ErrorType {
32 * Errors relating to the transport layer.
36 * Errors relating to the RPC or notification layer.
40 * Errors relating to the protocol operation layer.
44 * Errors relating to the server application layer.
48 public String getErrorTypeTag() {
49 return name().toLowerCase(Locale.ROOT);
52 public static ErrorType valueOfCaseInsensitive(final String value) {
54 return ErrorType.valueOf(ErrorType.class, value.toUpperCase(Locale.ROOT));
55 } catch (IllegalArgumentException e) {
61 public enum ErrorTag {
62 IN_USE("in-use", 409 /* Conflict */),
63 INVALID_VALUE("invalid-value", 400 /* Bad Request */),
64 TOO_BIG("too-big", 413 /* Request Entity Too Large */),
65 MISSING_ATTRIBUTE("missing-attribute", 400 /* Bad Request */),
66 BAD_ATTRIBUTE("bad-attribute", 400 /* Bad Request */),
67 UNKNOWN_ATTRIBUTE("unknown-attribute", 400 /* Bad Request */),
68 MISSING_ELEMENT("missing-element", 400 /* Bad Request */),
69 BAD_ELEMENT("bad-element", 400 /* Bad Request */),
70 UNKNOWN_ELEMENT("unknown-element", 400 /* Bad Request */),
71 UNKNOWN_NAMESPACE("unknown-namespace", 400 /* Bad Request */),
72 ACCESS_DENIED("access-denied", 403 /* Forbidden */),
73 LOCK_DENIED("lock-denied", 409 /* Conflict */),
74 RESOURCE_DENIED("resource-denied", 409 /* Conflict */),
75 ROLLBACK_FAILED("rollback-failed", 500 /* INTERNAL_SERVER_ERROR */),
76 DATA_EXISTS("data-exists", 409 /* Conflict */),
77 DATA_MISSING("data-missing", 404 /* Resource Not Found */),
78 OPERATION_NOT_SUPPORTED("operation-not-supported", 501 /* Not Implemented */),
79 OPERATION_FAILED("operation-failed", 500 /* INTERNAL_SERVER_ERROR */),
80 PARTIAL_OPERATION("partial-operation", 500 /* INTERNAL_SERVER_ERROR */),
81 MALFORMED_MESSAGE("malformed-message", 400 /* Bad Request */),
82 RESOURCE_DENIED_TRANSPORT("resource-denied-transport", 503 /* Service Unavailable */);
84 private final String tagValue;
85 private final int statusCode;
87 ErrorTag(final String tagValue, final int statusCode) {
88 this.tagValue = tagValue;
89 this.statusCode = statusCode;
92 public String getTagValue() {
93 return this.tagValue.toLowerCase(Locale.ROOT);
96 public static ErrorTag valueOfCaseInsensitive(final String value) {
98 return ErrorTag.valueOf(ErrorTag.class, value.toUpperCase(Locale.ROOT).replaceAll("-", "_"));
99 } catch (IllegalArgumentException e) {
100 return OPERATION_FAILED;
104 public int getStatusCode() {
109 private final ErrorType errorType;
110 private final ErrorTag errorTag;
111 private final String errorInfo;
112 private final String errorAppTag;
113 private final String errorMessage;
114 private final YangInstanceIdentifier errorPath;
117 * Constructs a RestConfError.
120 * The enumerated type indicating the layer where the error occurred.
122 * The enumerated tag representing a more specific error cause.
123 * @param errorMessage
124 * A string which provides a plain text string describing the error.
126 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage) {
127 this(errorType, errorTag, errorMessage, null, null, null);
131 * Constructs a RestConfError object.
134 * The enumerated type indicating the layer where the error occurred.
136 * The enumerated tag representing a more specific error cause.
137 * @param errorMessage
138 * A string which provides a plain text string describing the error.
140 * A string which represents an application-specific error tag that further specifies the error cause.
142 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
143 final String errorAppTag) {
144 this(errorType, errorTag, errorMessage, errorAppTag, null, null);
148 * Constructs a RestConfError object.
151 * The enumerated type indicating the layer where the error occurred.
153 * The enumerated tag representing a more specific error cause.
154 * @param errorMessage
155 * A string which provides a plain text string describing the error.
157 * An instance identifier which contains error path
159 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
160 final YangInstanceIdentifier errorPath) {
161 this(errorType, errorTag, errorMessage, null, null, errorPath);
165 * Constructs a RestConfError object.
168 * The enumerated type indicating the layer where the error occurred.
170 * The enumerated tag representing a more specific error cause.
171 * @param errorMessage
172 * A string which provides a plain text string describing the error.
174 * A string which represents an application-specific error tag that further specifies the error cause.
176 * A string, <b>formatted as XML</b>, which contains additional error information.
178 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
179 final String errorAppTag, final String errorInfo) {
180 this(errorType, errorTag, errorMessage, errorAppTag, errorInfo, null);
184 * Constructs a RestConfError object.
187 * The enumerated type indicating the layer where the error occurred.
189 * The enumerated tag representing a more specific error cause.
190 * @param errorMessage
191 * A string which provides a plain text string describing the error.
193 * A string which represents an application-specific error tag that further specifies the error cause.
195 * A string, <b>formatted as XML</b>, which contains additional error information.
197 * An instance identifier which contains error path
199 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
200 final String errorAppTag, final String errorInfo, final YangInstanceIdentifier errorPath) {
201 this.errorType = requireNonNull(errorType, "Error type is required for RestConfError");
202 this.errorTag = requireNonNull(errorTag, "Error tag is required for RestConfError");
203 this.errorMessage = errorMessage;
204 this.errorAppTag = errorAppTag;
205 this.errorInfo = errorInfo;
206 this.errorPath = errorPath;
210 * Constructs a RestConfError object from an RpcError.
212 public RestconfError(final RpcError rpcError) {
214 this.errorType = rpcError.getErrorType() == null ? ErrorType.APPLICATION : ErrorType
215 .valueOfCaseInsensitive(rpcError.getErrorType().name());
217 this.errorTag = rpcError.getTag() == null ? ErrorTag.OPERATION_FAILED : ErrorTag
218 .valueOfCaseInsensitive(rpcError.getTag());
220 this.errorMessage = rpcError.getMessage();
221 this.errorAppTag = rpcError.getApplicationTag();
223 String localErrorInfo = null;
224 if (rpcError.getInfo() == null) {
225 if (rpcError.getCause() != null) {
226 localErrorInfo = rpcError.getCause().getMessage();
227 } else if (rpcError.getSeverity() != null) {
228 localErrorInfo = "<severity>" + rpcError.getSeverity().toString().toLowerCase(Locale.ROOT)
232 localErrorInfo = rpcError.getInfo();
235 this.errorInfo = localErrorInfo;
236 this.errorPath = null;
239 public ErrorType getErrorType() {
243 public ErrorTag getErrorTag() {
247 public String getErrorInfo() {
251 public String getErrorAppTag() {
255 public String getErrorMessage() {
259 public YangInstanceIdentifier getErrorPath() {
264 public String toString() {
265 return "RestconfError ["
266 + "error-type: " + errorType.getErrorTypeTag() + ", error-tag: " + errorTag.getTagValue()
267 + (errorAppTag != null ? ", error-app-tag: " + errorAppTag : "")
268 + (errorMessage != null ? ", error-message: " + errorMessage : "")
269 + (errorInfo != null ? ", error-info: " + errorInfo : "")
270 + (errorPath != null ? ", error-path: " + errorPath.toString() : "")