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 java.io.PrintWriter;
11 import java.io.StringWriter;
13 import org.opendaylight.yangtools.yang.common.RpcError;
15 import com.google.common.base.Preconditions;
18 * Encapsulates a restconf error as defined in the ietf restconf draft.
20 * <br><br><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 )
44 return ErrorType.valueOf( ErrorType.class, value.toUpperCase() );
46 catch( IllegalArgumentException e ) {
52 public static enum ErrorTag {
53 IN_USE( "in-use", 409 /*Conflict*/ ),
54 INVALID_VALUE( "invalid-value", 400 /*Bad Request*/ ),
55 TOO_BIG( "too-big", 413 /*Request Entity Too Large*/ ),
56 MISSING_ATTRIBUTE( "missing-attribute", 400 /*Bad Request*/ ),
57 BAD_ATTRIBUTE( "bad-attribute", 400 /*Bad Request*/ ),
58 UNKNOWN_ATTRIBUTE( "unknown-attribute", 400 /*Bad Request*/ ),
59 BAD_ELEMENT( "bad-element", 400 /*Bad Request*/ ),
60 UNKNOWN_ELEMENT( "unknown-element", 400 /*Bad Request*/ ),
61 UNKNOWN_NAMESPACE( "unknown-namespace", 400 /*Bad Request*/ ),
62 ACCESS_DENIED( "access-denied", 403 /*Forbidden*/ ),
63 LOCK_DENIED( "lock-denied", 409 /*Conflict*/ ),
64 RESOURCE_DENIED( "resource-denied", 409 /*Conflict*/ ),
65 ROLLBACK_FAILED( "rollback-failed", 500 /*INTERNAL_SERVER_ERROR*/ ),
66 DATA_EXISTS( "data-exists", 409 /*Conflict*/ ),
67 DATA_MISSING( "data-missing", 409 /*Conflict*/ ),
68 OPERATION_NOT_SUPPORTED( "operation-not-supported", 501 /*Not Implemented*/ ),
69 OPERATION_FAILED( "operation-failed", 500 /*INTERNAL_SERVER_ERROR*/ ),
70 PARTIAL_OPERATION( "partial-operation", 500 /*INTERNAL_SERVER_ERROR*/ ),
71 MALFORMED_MESSAGE( "malformed-message", 400 /*Bad Request*/ );
73 private final String tagValue;
74 private final int statusCode;
76 ErrorTag(final String tagValue, final int statusCode) {
77 this.tagValue = tagValue;
78 this.statusCode = statusCode;
81 public String getTagValue() {
82 return this.tagValue.toLowerCase();
85 public static ErrorTag valueOfCaseInsensitive( String value )
88 return ErrorTag.valueOf( ErrorTag.class, value.toUpperCase().replaceAll( "-","_" ) );
90 catch( IllegalArgumentException e ) {
91 return OPERATION_FAILED;
95 public int getStatusCode() {
100 private final ErrorType errorType;
101 private final ErrorTag errorTag;
102 private final String errorInfo;
103 private final String errorAppTag;
104 private final String errorMessage;
105 //TODO: Add in the error-path concept as defined in the ietf draft.
107 static String toErrorInfo( Throwable cause ) {
108 StringWriter writer = new StringWriter();
109 cause.printStackTrace( new PrintWriter( writer ) );
110 return writer.toString();
114 * Constructs a RestConfError
116 * @param errorType The enumerated type indicating the layer where the error occurred.
117 * @param errorTag The enumerated tag representing a more specific error cause.
118 * @param errorMessage 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.
127 * @param errorType The enumerated type indicating the layer where the error occurred.
128 * @param errorTag The enumerated tag representing a more specific error cause.
129 * @param errorMessage A string which provides a plain text string describing the error.
130 * @param errorAppTag A string which represents an application-specific error tag that further
131 * specifies the error cause.
133 public RestconfError(ErrorType errorType, ErrorTag errorTag, String errorMessage,
134 String errorAppTag) {
135 this( errorType, errorTag, errorMessage, errorAppTag, null );
139 * Constructs a RestConfError object.
141 * @param errorType The enumerated type indicating the layer where the error occurred.
142 * @param errorTag The enumerated tag representing a more specific error cause.
143 * @param errorMessage A string which provides a plain text string describing the error.
144 * @param errorAppTag A string which represents an application-specific error tag that further
145 * specifies the error cause.
146 * @param errorInfo A string, <b>formatted as XML</b>, which contains additional error information.
148 public RestconfError(ErrorType errorType, ErrorTag errorTag, String errorMessage,
149 String errorAppTag, String errorInfo) {
150 Preconditions.checkNotNull( errorType, "Error type is required for RestConfError" );
151 Preconditions.checkNotNull( errorTag, "Error tag is required for RestConfError");
152 this.errorType = errorType;
153 this.errorTag = errorTag;
154 this.errorMessage = errorMessage;
155 this.errorAppTag = errorAppTag;
156 this.errorInfo = errorInfo;
160 * Constructs a RestConfError object from an RpcError.
162 public RestconfError( RpcError rpcError ) {
164 this.errorType = rpcError.getErrorType() == null ? ErrorType.APPLICATION :
165 ErrorType.valueOfCaseInsensitive( rpcError.getErrorType().name() );
167 this.errorTag = rpcError.getTag() == null ? ErrorTag.OPERATION_FAILED :
168 ErrorTag.valueOfCaseInsensitive( rpcError.getTag().toString() );
170 this.errorMessage = rpcError.getMessage();
171 this.errorAppTag = rpcError.getApplicationTag();
173 String errorInfo = null;
174 if( rpcError.getInfo() == null ) {
175 if( rpcError.getCause() != null ) {
176 errorInfo = toErrorInfo( rpcError.getCause() );
178 else if( rpcError.getSeverity() != null ) {
179 errorInfo = "<severity>" + rpcError.getSeverity().toString().toLowerCase() +
184 errorInfo = rpcError.getInfo();
187 this.errorInfo = errorInfo;
190 public ErrorType getErrorType() {
194 public ErrorTag getErrorTag() {
198 public String getErrorInfo() {
202 public String getErrorAppTag() {
206 public String getErrorMessage() {
211 public String toString() {
212 return "error-type: " + errorType.getErrorTypeTag()
213 + ", error-tag: " + errorTag.getTagValue() + ", "
214 + (errorAppTag != null ? "error-app-tag: " + errorAppTag + ", " : "")
215 + (errorMessage != null ? "error-message: " + errorMessage : "")
216 + (errorInfo != null ? "error-info: " + errorInfo + ", " : "") + "]";