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.controller.sal.restconf.impl;
10 import com.google.common.base.Preconditions;
11 import java.io.PrintWriter;
12 import java.io.StringWriter;
13 import org.opendaylight.yangtools.yang.common.RpcError;
16 * Encapsulates a restconf error as defined in the ietf restconf draft.
20 * <b>Note:</b> Enumerations defined within are provided by the ietf restconf draft.
23 * @see {@link https://tools.ietf.org/html/draft-bierman-netconf-restconf-02}
25 public class RestconfError {
27 public static enum ErrorType {
28 /** Errors relating to the transport layer */
30 /** Errors relating to the RPC or notification layer */
32 /** Errors relating to the protocol operation layer. */
34 /** Errors relating to the server application layer. */
37 public String getErrorTypeTag() {
38 return name().toLowerCase();
41 public static ErrorType valueOfCaseInsensitive(String value) {
43 return ErrorType.valueOf(ErrorType.class, value.toUpperCase());
44 } catch (IllegalArgumentException e) {
50 public static enum ErrorTag {
51 IN_USE("in-use", 409 /* Conflict */),
52 INVALID_VALUE("invalid-value", 400 /* Bad Request */),
53 TOO_BIG("too-big", 413 /* Request Entity Too Large */),
54 MISSING_ATTRIBUTE("missing-attribute", 400 /* Bad Request */),
55 BAD_ATTRIBUTE("bad-attribute", 400 /* Bad Request */),
56 UNKNOWN_ATTRIBUTE("unknown-attribute", 400 /* Bad Request */),
57 BAD_ELEMENT("bad-element", 400 /* Bad Request */),
58 UNKNOWN_ELEMENT("unknown-element", 400 /* Bad Request */),
59 UNKNOWN_NAMESPACE("unknown-namespace", 400 /* Bad Request */),
60 ACCESS_DENIED("access-denied", 403 /* Forbidden */),
61 LOCK_DENIED("lock-denied", 409 /* Conflict */),
62 RESOURCE_DENIED("resource-denied", 409 /* Conflict */),
63 ROLLBACK_FAILED("rollback-failed", 500 /* INTERNAL_SERVER_ERROR */),
64 DATA_EXISTS("data-exists", 409 /* Conflict */),
65 DATA_MISSING("data-missing", 409 /* Conflict */),
66 OPERATION_NOT_SUPPORTED("operation-not-supported", 501 /* Not Implemented */),
67 OPERATION_FAILED("operation-failed", 500 /* INTERNAL_SERVER_ERROR */),
68 PARTIAL_OPERATION("partial-operation", 500 /* INTERNAL_SERVER_ERROR */),
69 MALFORMED_MESSAGE("malformed-message", 400 /* Bad Request */);
71 private final String tagValue;
72 private final int statusCode;
74 ErrorTag(final String tagValue, final int statusCode) {
75 this.tagValue = tagValue;
76 this.statusCode = statusCode;
79 public String getTagValue() {
80 return this.tagValue.toLowerCase();
83 public static ErrorTag valueOfCaseInsensitive(String value) {
85 return ErrorTag.valueOf(ErrorTag.class, value.toUpperCase().replaceAll("-", "_"));
86 } catch (IllegalArgumentException e) {
87 return OPERATION_FAILED;
91 public int getStatusCode() {
96 private final ErrorType errorType;
97 private final ErrorTag errorTag;
98 private final String errorInfo;
99 private final String errorAppTag;
100 private final String errorMessage;
102 // TODO: Add in the error-path concept as defined in the ietf draft.
104 static String toErrorInfo(Throwable cause) {
105 StringWriter writer = new StringWriter();
106 cause.printStackTrace(new PrintWriter(writer));
107 return writer.toString();
111 * Constructs a RestConfError
114 * The enumerated type indicating the layer where the error occurred.
116 * The enumerated tag representing a more specific error cause.
117 * @param errorMessage
118 * A string which provides a plain text string describing the error.
120 public RestconfError(ErrorType errorType, ErrorTag errorTag, String errorMessage) {
121 this(errorType, errorTag, errorMessage, null);
125 * Constructs a RestConfError object.
128 * The enumerated type indicating the layer where the error occurred.
130 * The enumerated tag representing a more specific error cause.
131 * @param errorMessage
132 * A string which provides a plain text string describing the error.
134 * A string which represents an application-specific error tag that further specifies the error cause.
136 public RestconfError(ErrorType errorType, ErrorTag errorTag, String errorMessage, String errorAppTag) {
137 this(errorType, errorTag, errorMessage, errorAppTag, null);
141 * Constructs a RestConfError object.
144 * The enumerated type indicating the layer where the error occurred.
146 * The enumerated tag representing a more specific error cause.
147 * @param errorMessage
148 * A string which provides a plain text string describing the error.
150 * A string which represents an application-specific error tag that further specifies the error cause.
152 * A string, <b>formatted as XML</b>, which contains additional error information.
154 public RestconfError(ErrorType errorType, ErrorTag errorTag, String errorMessage, String errorAppTag,
156 Preconditions.checkNotNull(errorType, "Error type is required for RestConfError");
157 Preconditions.checkNotNull(errorTag, "Error tag is required for RestConfError");
158 this.errorType = errorType;
159 this.errorTag = errorTag;
160 this.errorMessage = errorMessage;
161 this.errorAppTag = errorAppTag;
162 this.errorInfo = errorInfo;
166 * Constructs a RestConfError object from an RpcError.
168 public RestconfError(RpcError rpcError) {
170 this.errorType = rpcError.getErrorType() == null ? ErrorType.APPLICATION : ErrorType
171 .valueOfCaseInsensitive(rpcError.getErrorType().name());
173 this.errorTag = rpcError.getTag() == null ? ErrorTag.OPERATION_FAILED : ErrorTag
174 .valueOfCaseInsensitive(rpcError.getTag().toString());
176 this.errorMessage = rpcError.getMessage();
177 this.errorAppTag = rpcError.getApplicationTag();
179 String errorInfo = null;
180 if (rpcError.getInfo() == null) {
181 if (rpcError.getCause() != null) {
182 errorInfo = toErrorInfo(rpcError.getCause());
183 } else if (rpcError.getSeverity() != null) {
184 errorInfo = "<severity>" + rpcError.getSeverity().toString().toLowerCase() + "</severity>";
187 errorInfo = rpcError.getInfo();
190 this.errorInfo = errorInfo;
193 public ErrorType getErrorType() {
197 public ErrorTag getErrorTag() {
201 public String getErrorInfo() {
205 public String getErrorAppTag() {
209 public String getErrorMessage() {
214 public String toString() {
215 return "error-type: " + errorType.getErrorTypeTag() + ", error-tag: " + errorTag.getTagValue() + ", "
216 + (errorAppTag != null ? "error-app-tag: " + errorAppTag + ", " : "")
217 + (errorMessage != null ? "error-message: " + errorMessage : "")
218 + (errorInfo != null ? "error-info: " + errorInfo + ", " : "") + "]";