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