Merge "Fixed for bug 1197"
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / restconf / impl / RestconfError.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. 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.controller.sal.restconf.impl;
9
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;
14
15 /**
16  * Encapsulates a restconf error as defined in the ietf restconf draft.
17  *
18  * <br>
19  * <br>
20  * <b>Note:</b> Enumerations defined within are provided by the ietf restconf draft.
21  *
22  * @author Devin Avery
23  * @see {@link https://tools.ietf.org/html/draft-bierman-netconf-restconf-02}
24  */
25 public class RestconfError {
26
27     public static enum ErrorType {
28         /** Errors relating to the transport layer */
29         TRANSPORT,
30         /** Errors relating to the RPC or notification layer */
31         RPC,
32         /** Errors relating to the protocol operation layer. */
33         PROTOCOL,
34         /** Errors relating to the server application layer. */
35         APPLICATION;
36
37         public String getErrorTypeTag() {
38             return name().toLowerCase();
39         }
40
41         public static ErrorType valueOfCaseInsensitive(String value) {
42             try {
43                 return ErrorType.valueOf(ErrorType.class, value.toUpperCase());
44             } catch (IllegalArgumentException e) {
45                 return APPLICATION;
46             }
47         }
48     }
49
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 */);
70
71         private final String tagValue;
72         private final int statusCode;
73
74         ErrorTag(final String tagValue, final int statusCode) {
75             this.tagValue = tagValue;
76             this.statusCode = statusCode;
77         }
78
79         public String getTagValue() {
80             return this.tagValue.toLowerCase();
81         }
82
83         public static ErrorTag valueOfCaseInsensitive(String value) {
84             try {
85                 return ErrorTag.valueOf(ErrorTag.class, value.toUpperCase().replaceAll("-", "_"));
86             } catch (IllegalArgumentException e) {
87                 return OPERATION_FAILED;
88             }
89         }
90
91         public int getStatusCode() {
92             return statusCode;
93         }
94     }
95
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;
101
102     // TODO: Add in the error-path concept as defined in the ietf draft.
103
104     static String toErrorInfo(Throwable cause) {
105         StringWriter writer = new StringWriter();
106         cause.printStackTrace(new PrintWriter(writer));
107         return writer.toString();
108     }
109
110     /**
111      * Constructs a RestConfError
112      *
113      * @param errorType
114      *            The enumerated type indicating the layer where the error occurred.
115      * @param errorTag
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.
119      */
120     public RestconfError(ErrorType errorType, ErrorTag errorTag, String errorMessage) {
121         this(errorType, errorTag, errorMessage, null);
122     }
123
124     /**
125      * Constructs a RestConfError object.
126      *
127      * @param errorType
128      *            The enumerated type indicating the layer where the error occurred.
129      * @param errorTag
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.
133      * @param errorAppTag
134      *            A string which represents an application-specific error tag that further specifies the error cause.
135      */
136     public RestconfError(ErrorType errorType, ErrorTag errorTag, String errorMessage, String errorAppTag) {
137         this(errorType, errorTag, errorMessage, errorAppTag, null);
138     }
139
140     /**
141      * Constructs a RestConfError object.
142      *
143      * @param errorType
144      *            The enumerated type indicating the layer where the error occurred.
145      * @param errorTag
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.
149      * @param errorAppTag
150      *            A string which represents an application-specific error tag that further specifies the error cause.
151      * @param errorInfo
152      *            A string, <b>formatted as XML</b>, which contains additional error information.
153      */
154     public RestconfError(ErrorType errorType, ErrorTag errorTag, String errorMessage, String errorAppTag,
155             String errorInfo) {
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;
163     }
164
165     /**
166      * Constructs a RestConfError object from an RpcError.
167      */
168     public RestconfError(RpcError rpcError) {
169
170         this.errorType = rpcError.getErrorType() == null ? ErrorType.APPLICATION : ErrorType
171                 .valueOfCaseInsensitive(rpcError.getErrorType().name());
172
173         this.errorTag = rpcError.getTag() == null ? ErrorTag.OPERATION_FAILED : ErrorTag
174                 .valueOfCaseInsensitive(rpcError.getTag().toString());
175
176         this.errorMessage = rpcError.getMessage();
177         this.errorAppTag = rpcError.getApplicationTag();
178
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>";
185             }
186         } else {
187             errorInfo = rpcError.getInfo();
188         }
189
190         this.errorInfo = errorInfo;
191     }
192
193     public ErrorType getErrorType() {
194         return errorType;
195     }
196
197     public ErrorTag getErrorTag() {
198         return errorTag;
199     }
200
201     public String getErrorInfo() {
202         return errorInfo;
203     }
204
205     public String getErrorAppTag() {
206         return errorAppTag;
207     }
208
209     public String getErrorMessage() {
210         return errorMessage;
211     }
212
213     @Override
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 + ", " : "") + "]";
219     }
220
221 }