2e849d252f5d1911b8163bd7a74456b7fcd050a1
[aai/aai-common.git] / aai-els-onap-logging / src / main / java / org / onap / aai / logging / ErrorObject.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aai.logging;
22
23 import javax.ws.rs.core.Response.Status;
24
25 /**
26  * 
27  * Contains the definition of all error message fields to be mapped from the Error
28  * properties file
29  * 
30  */
31 public class ErrorObject {
32
33     private String disposition;
34     private String category;
35     private String severity;
36     private Status httpResponseCode = Status.INTERNAL_SERVER_ERROR; // default
37     private String restErrorCode = "3002";
38     private String errorCode;
39     private String errorText;
40     private String details;
41     private String aaiElsErrorCode = AaiElsErrorCode.UNKNOWN_ERROR;
42
43     /**
44      * Instantiates a new error object.
45      */
46     public ErrorObject() {
47         super();
48     }
49
50     /**
51      * Creates an error object
52      *
53      * @param disposition the disposition
54      * @param category the category
55      * @param severity the severity
56      * @param httpResponseCode the http response code
57      * @param restErrorCode the rest error code
58      * @param errorCode the error code
59      * @param errorText the error text
60      */
61     public ErrorObject(String disposition, String category, String severity, Integer httpResponseCode,
62             String restErrorCode, String errorCode, String errorText) {
63         super();
64         this.setDisposition(disposition);
65         this.setCategory(category);
66         this.severity = severity;
67         this.setHTTPResponseCode(httpResponseCode);
68         this.setRESTErrorCode(restErrorCode);
69         this.setErrorCode(errorCode);
70         this.setErrorText(errorText);
71         this.setAaiElsErrorCode(AaiElsErrorCode.UNKNOWN_ERROR);
72     }
73
74     // OLD STARTS HERE
75
76     /**
77      * Instantiates a new error object.
78      * @param severity the severity
79      * @param errorCode the error code
80      * @param errorText the error text
81      * @param disposition the disposition
82      * @param category the category
83      */
84     public ErrorObject(String severity, String errorCode, String errorText, String disposition, String category) {
85         this(severity, Status.INTERNAL_SERVER_ERROR, errorCode, errorText, disposition, category);
86     }
87
88     /**
89      * Instantiates a new error object.
90      *
91      * @param severity the severity
92      * @param httpResponseCode the http response code
93      * @param errorCode the error code
94      * @param errorText the error text
95      * @param disposition the disposition
96      * @param category the category
97      */
98     public ErrorObject(String severity, Integer httpResponseCode, String errorCode, String errorText,
99             String disposition, String category) {
100         super();
101         this.severity = severity;
102         this.setHTTPResponseCode(httpResponseCode);
103         this.setErrorCode(errorCode);
104         this.setErrorText(errorText);
105         this.setDisposition(disposition);
106         this.setCategory(category);
107         this.setAaiElsErrorCode(AaiElsErrorCode.UNKNOWN_ERROR);
108     }
109
110     /**
111      * Instantiates a new error object.
112      *
113      * @param severity the severity
114      * @param httpResponseCode the http response code
115      * @param errorCode the error code
116      * @param errorText the error text
117      * @param disposition the disposition
118      * @param category the category
119      */
120     public ErrorObject(String severity, Status httpResponseCode, String errorCode, String errorText, String disposition,
121             String category) {
122         super();
123         this.severity = severity;
124         this.setHTTPResponseCode(httpResponseCode);
125         this.setErrorCode(errorCode);
126         this.setErrorText(errorText);
127         this.setDisposition(disposition);
128         this.setCategory(category);
129         this.setAaiElsErrorCode(AaiElsErrorCode.UNKNOWN_ERROR);
130     }
131
132     /**
133      * Gets the disposition.
134      *
135      * @return the disposition
136      */
137     public String getDisposition() {
138         return disposition;
139     }
140
141     /**
142      * Sets the disposition.
143      *
144      * @param disposition the new disposition
145      */
146     public void setDisposition(String disposition) {
147         this.disposition = disposition;
148     }
149
150     /**
151      * Gets the category.
152      *
153      * @return the category
154      */
155     public String getCategory() {
156         return category;
157     }
158
159     /**
160      * Sets the category.
161      *
162      * @param category the new category
163      */
164     public void setCategory(String category) {
165         this.category = category;
166     }
167
168     /**
169      * Gets the severity.
170      *
171      * @return the severity
172      */
173     public String getSeverity() {
174         return severity;
175     }
176
177     /**
178      * Sets the severity.
179      *
180      * @param severity the new severity
181      */
182     public void setSeverity(String severity) {
183         this.severity = severity;
184     }
185
186     /**
187      * Gets the error code.
188      *
189      * @return the error code
190      */
191     public String getErrorCode() {
192         return errorCode;
193     }
194
195     /**
196      * Sets the error code.
197      *
198      * @param errorCode the new error code
199      */
200     public void setErrorCode(String errorCode) {
201         this.errorCode = errorCode;
202     }
203
204     /**
205      * Gets the HTTP response code.
206      *
207      * @return the HTTP response code
208      */
209     public Status getHTTPResponseCode() {
210         return httpResponseCode;
211     }
212
213     /**
214      * Sets the HTTP response code.
215      *
216      * @param httpResponseCode the new HTTP response code
217      */
218     public void setHTTPResponseCode(Integer httpResponseCode) {
219         this.httpResponseCode = Status.fromStatusCode(httpResponseCode);
220         if (this.httpResponseCode == null) {
221             throw new IllegalArgumentException(
222                     "setHTTPResponseCode was passed an invalid Integer value, fix error.properties or your code "
223                             + httpResponseCode);
224         }
225     }
226
227     /**
228      * Sets the HTTP response code.
229      *
230      * @param httpResponseCode the new HTTP response code
231      */
232     public void setHTTPResponseCode(String httpResponseCode) {
233         this.httpResponseCode = Status.fromStatusCode(Integer.valueOf(httpResponseCode));
234         if (this.httpResponseCode == null) {
235             throw new IllegalArgumentException(
236                     "setHTTPResponseCode was passed an invalid String value, fix error.properties or your code "
237                             + httpResponseCode);
238         }
239     }
240
241     /**
242      * Sets the REST error code.
243      *
244      * @param restErrorCode the new REST error code
245      */
246     public void setRESTErrorCode(String restErrorCode) {
247         this.restErrorCode = restErrorCode;
248     }
249
250     /**
251      * Gets the REST error code.
252      *
253      * @return the REST error code
254      */
255     public String getRESTErrorCode() {
256         return this.restErrorCode;
257     }
258
259     /**
260      * Sets the HTTP response code.
261      *
262      * @param httpResponseCode the new HTTP response code
263      */
264     public void setHTTPResponseCode(Status httpResponseCode) {
265         this.httpResponseCode = httpResponseCode;
266         if (this.httpResponseCode == null) {
267             throw new IllegalArgumentException(
268                     "setHTTPResponseCode was passed an invalid String value, fix error.properties or your code "
269                             + httpResponseCode);
270         }
271     }
272
273     /**
274      * Gets the error text.
275      *
276      * @return the error text
277      */
278     public String getErrorText() {
279         return errorText;
280     }
281
282     /**
283      * Sets the error text.
284      *
285      * @param errorText the new error text
286      */
287     public void setErrorText(String errorText) {
288         this.errorText = errorText;
289     }
290
291     /**
292      * Gets the details.
293      *
294      * @return the details
295      */
296     public String getDetails() {
297         return details;
298     }
299
300     /**
301      * Sets the details.
302      *
303      * @param details the new details
304      */
305     public void setDetails(String details) {
306         this.details = details == null ? "" : details;
307     }
308     /**
309      * Sets the aai els error code.
310      *
311      * @param elsErrorCode the new code
312      */
313     public void setAaiElsErrorCode(String elsErrorCode) {
314         aaiElsErrorCode = elsErrorCode;
315     }
316     /**
317      * Gets the aai els error code.
318      *
319      * @return the code
320      */
321     public String getAaiElsErrorCode() {
322         return (aaiElsErrorCode);
323     }
324     /**
325      * Gets the error code string. This is also the string
326      * configured in Nagios to alert on
327      *
328      * @return the error code string
329      */
330     // Get the X.Y.Z representation of the error code
331     public String getErrorCodeString() {
332         String prefix = null;
333         switch (disposition) {
334             default:
335                 prefix = "";
336                 break;
337             case "5":
338                 prefix = "ERR.";
339                 break;
340         }
341         return prefix + disposition + "." + category + "." + errorCode;
342     }
343
344     /**
345      * Gets the severity Code. This is also the string
346      * configured in Nagios to alert on
347      *
348      * @return the severity
349      */
350     // Get the numerical value of severity
351     public String getSeverityCode(String severity) {
352         String severityCode = "";
353         switch (severity) {
354             case "WARN":
355                 severityCode = "1";
356                 break;
357             case "ERROR":
358                 severityCode = "2";
359                 break;
360             case "FATAL":
361                 severityCode = "3";
362                 break;
363         }
364         return severityCode;
365     }
366
367     /**
368      * {@inheritDoc}
369      */
370     @Override
371     public String toString() {
372         return "ErrorObject [errorCode=" + errorCode + ", errorText=" + errorText + ", restErrorCode=" + restErrorCode
373                 + ", httpResponseCode=" + httpResponseCode + ", severity=" + severity + ", disposition=" + disposition
374                 + ", category=" + category + "]";
375     }
376
377 }