Use ErrorSeverity.elementBody() in RestconfError
[netconf.git] / restconf / restconf-common / src / main / java / org / opendaylight / restconf / common / errors / 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.restconf.common.errors;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.io.Serializable;
13 import org.opendaylight.yangtools.yang.common.ErrorTag;
14 import org.opendaylight.yangtools.yang.common.ErrorType;
15 import org.opendaylight.yangtools.yang.common.RpcError;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17
18 /**
19  * Encapsulates a restconf error as defined in the ietf restconf draft.
20  *
21  * <br>
22  * <br>
23  * <b>Note:</b> Enumerations defined within are provided by the ietf restconf draft.
24  *
25  * @author Devin Avery
26  *     See also <a href="https://tools.ietf.org/html/draft-bierman-netconf-restconf-02">RESTCONF</a>.
27  */
28 public class RestconfError implements Serializable {
29     private static final long serialVersionUID = 1L;
30
31     private final ErrorType errorType;
32     private final ErrorTag errorTag;
33     private final String errorInfo;
34     private final String errorAppTag;
35     private final String errorMessage;
36     private final YangInstanceIdentifier errorPath;
37
38     /**
39      * Constructs a RestConfError.
40      *
41      * @param errorType
42      *            The enumerated type indicating the layer where the error occurred.
43      * @param errorTag
44      *            The enumerated tag representing a more specific error cause.
45      * @param errorMessage
46      *            A string which provides a plain text string describing the error.
47      */
48     public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage) {
49         this(errorType, errorTag, errorMessage, null, null, null);
50     }
51
52     /**
53      * Constructs a RestConfError object.
54      *
55      * @param errorType
56      *            The enumerated type indicating the layer where the error occurred.
57      * @param errorTag
58      *            The enumerated tag representing a more specific error cause.
59      * @param errorMessage
60      *            A string which provides a plain text string describing the error.
61      * @param errorAppTag
62      *            A string which represents an application-specific error tag that further specifies the error cause.
63      */
64     public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
65                          final String errorAppTag) {
66         this(errorType, errorTag, errorMessage, errorAppTag, null, null);
67     }
68
69     /**
70      * Constructs a RestConfError object.
71      *
72      * @param errorType
73      *            The enumerated type indicating the layer where the error occurred.
74      * @param errorTag
75      *            The enumerated tag representing a more specific error cause.
76      * @param errorMessage
77      *            A string which provides a plain text string describing the error.
78      * @param errorPath
79      *            An instance identifier which contains error path
80      */
81     public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
82                          final YangInstanceIdentifier errorPath) {
83         this(errorType, errorTag, errorMessage, null, null, errorPath);
84     }
85
86     /**
87      * Constructs a RestConfError object.
88      *
89      * @param errorType
90      *            The enumerated type indicating the layer where the error occurred.
91      * @param errorTag
92      *            The enumerated tag representing a more specific error cause.
93      * @param errorMessage
94      *            A string which provides a plain text string describing the error.
95      * @param errorAppTag
96      *            A string which represents an application-specific error tag that further specifies the error cause.
97      * @param errorInfo
98      *            A string, <b>formatted as XML</b>, which contains additional error information.
99      */
100     public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
101                          final String errorAppTag, final String errorInfo) {
102         this(errorType, errorTag, errorMessage, errorAppTag, errorInfo, null);
103     }
104
105     /**
106      * Constructs a RestConfError object.
107      *
108      * @param errorType
109      *            The enumerated type indicating the layer where the error occurred.
110      * @param errorTag
111      *            The enumerated tag representing a more specific error cause.
112      * @param errorMessage
113      *            A string which provides a plain text string describing the error.
114      * @param errorAppTag
115      *            A string which represents an application-specific error tag that further specifies the error cause.
116      * @param errorInfo
117      *            A string, <b>formatted as XML</b>, which contains additional error information.
118      * @param errorPath
119      *            An instance identifier which contains error path
120      */
121     public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
122                          final String errorAppTag, final String errorInfo, final YangInstanceIdentifier errorPath) {
123         this.errorType = requireNonNull(errorType, "Error type is required for RestConfError");
124         this.errorTag = requireNonNull(errorTag, "Error tag is required for RestConfError");
125         this.errorMessage = errorMessage;
126         this.errorAppTag = errorAppTag;
127         this.errorInfo = errorInfo;
128         this.errorPath = errorPath;
129     }
130
131     /**
132      * Constructs a RestConfError object from an RpcError.
133      */
134     public RestconfError(final RpcError rpcError) {
135
136         errorType = rpcError.getErrorType();
137
138         final ErrorTag tag = rpcError.getTag();
139         errorTag = tag != null ? tag : ErrorTag.OPERATION_FAILED;
140
141         errorMessage = rpcError.getMessage();
142         errorAppTag = rpcError.getApplicationTag();
143
144         String localErrorInfo = null;
145         if (rpcError.getInfo() == null) {
146             if (rpcError.getCause() != null) {
147                 localErrorInfo = rpcError.getCause().getMessage();
148             } else if (rpcError.getSeverity() != null) {
149                 localErrorInfo = "<severity>" + rpcError.getSeverity().elementBody() + "</severity>";
150             }
151         } else {
152             localErrorInfo = rpcError.getInfo();
153         }
154
155         errorInfo = localErrorInfo;
156         errorPath = null;
157     }
158
159     public ErrorType getErrorType() {
160         return errorType;
161     }
162
163     public ErrorTag getErrorTag() {
164         return errorTag;
165     }
166
167     public String getErrorInfo() {
168         return errorInfo;
169     }
170
171     public String getErrorAppTag() {
172         return errorAppTag;
173     }
174
175     public String getErrorMessage() {
176         return errorMessage;
177     }
178
179     public YangInstanceIdentifier getErrorPath() {
180         return errorPath;
181     }
182
183     @Override
184     public String toString() {
185         return "RestconfError ["
186                 + "error-type: " + errorType.elementBody() + ", error-tag: " + errorTag.elementBody()
187                 + (errorAppTag != null ? ", error-app-tag: " + errorAppTag : "")
188                 + (errorMessage != null ? ", error-message: " + errorMessage : "")
189                 + (errorInfo != null ? ", error-info: " + errorInfo : "")
190                 + (errorPath != null ? ", error-path: " + errorPath.toString() : "")
191                 + "]";
192     }
193 }