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