Reduce the number of problems in aai-common by removing unused imports
[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      * 
79      * @param severity the severity
80      * @param errorCode the error code
81      * @param errorText the error text
82      * @param disposition the disposition
83      * @param category the category
84      */
85     public ErrorObject(String severity, String errorCode, String errorText, String disposition, String category) {
86         this(severity, Status.INTERNAL_SERVER_ERROR, errorCode, errorText, disposition, category);
87     }
88
89     /**
90      * Instantiates a new error object.
91      *
92      * @param severity the severity
93      * @param httpResponseCode the http response code
94      * @param errorCode the error code
95      * @param errorText the error text
96      * @param disposition the disposition
97      * @param category the category
98      */
99     public ErrorObject(String severity, Integer httpResponseCode, String errorCode, String errorText,
100             String disposition, String category) {
101         super();
102         this.severity = severity;
103         this.setHTTPResponseCode(httpResponseCode);
104         this.setErrorCode(errorCode);
105         this.setErrorText(errorText);
106         this.setDisposition(disposition);
107         this.setCategory(category);
108         this.setAaiElsErrorCode(AaiElsErrorCode.UNKNOWN_ERROR);
109     }
110
111     /**
112      * Instantiates a new error object.
113      *
114      * @param severity the severity
115      * @param httpResponseCode the http response code
116      * @param errorCode the error code
117      * @param errorText the error text
118      * @param disposition the disposition
119      * @param category the category
120      */
121     public ErrorObject(String severity, Status httpResponseCode, String errorCode, String errorText, String disposition,
122             String category) {
123         super();
124         this.severity = severity;
125         this.setHTTPResponseCode(httpResponseCode);
126         this.setErrorCode(errorCode);
127         this.setErrorText(errorText);
128         this.setDisposition(disposition);
129         this.setCategory(category);
130         this.setAaiElsErrorCode(AaiElsErrorCode.UNKNOWN_ERROR);
131     }
132
133     /**
134      * Gets the disposition.
135      *
136      * @return the disposition
137      */
138     public String getDisposition() {
139         return disposition;
140     }
141
142     /**
143      * Sets the disposition.
144      *
145      * @param disposition the new disposition
146      */
147     public void setDisposition(String disposition) {
148         this.disposition = disposition;
149     }
150
151     /**
152      * Gets the category.
153      *
154      * @return the category
155      */
156     public String getCategory() {
157         return category;
158     }
159
160     /**
161      * Sets the category.
162      *
163      * @param category the new category
164      */
165     public void setCategory(String category) {
166         this.category = category;
167     }
168
169     /**
170      * Gets the severity.
171      *
172      * @return the severity
173      */
174     public String getSeverity() {
175         return severity;
176     }
177
178     /**
179      * Sets the severity.
180      *
181      * @param severity the new severity
182      */
183     public void setSeverity(String severity) {
184         this.severity = severity;
185     }
186
187     /**
188      * Gets the error code.
189      *
190      * @return the error code
191      */
192     public String getErrorCode() {
193         return errorCode;
194     }
195
196     /**
197      * Sets the error code.
198      *
199      * @param errorCode the new error code
200      */
201     public void setErrorCode(String errorCode) {
202         this.errorCode = errorCode;
203     }
204
205     /**
206      * Gets the HTTP response code.
207      *
208      * @return the HTTP response code
209      */
210     public Status getHTTPResponseCode() {
211         return httpResponseCode;
212     }
213
214     /**
215      * Sets the HTTP response code.
216      *
217      * @param httpResponseCode the new HTTP response code
218      */
219     public void setHTTPResponseCode(Integer httpResponseCode) {
220         this.httpResponseCode = Status.fromStatusCode(httpResponseCode);
221         if (this.httpResponseCode == null) {
222             throw new IllegalArgumentException(
223                     "setHTTPResponseCode was passed an invalid Integer value, fix error.properties or your code "
224                             + httpResponseCode);
225         }
226     }
227
228     /**
229      * Sets the HTTP response code.
230      *
231      * @param httpResponseCode the new HTTP response code
232      */
233     public void setHTTPResponseCode(String httpResponseCode) {
234         this.httpResponseCode = Status.fromStatusCode(Integer.valueOf(httpResponseCode));
235         if (this.httpResponseCode == null) {
236             throw new IllegalArgumentException(
237                     "setHTTPResponseCode was passed an invalid String value, fix error.properties or your code "
238                             + httpResponseCode);
239         }
240     }
241
242     /**
243      * Sets the REST error code.
244      *
245      * @param restErrorCode the new REST error code
246      */
247     public void setRESTErrorCode(String restErrorCode) {
248         this.restErrorCode = restErrorCode;
249     }
250
251     /**
252      * Gets the REST error code.
253      *
254      * @return the REST error code
255      */
256     public String getRESTErrorCode() {
257         return this.restErrorCode;
258     }
259
260     /**
261      * Sets the HTTP response code.
262      *
263      * @param httpResponseCode the new HTTP response code
264      */
265     public void setHTTPResponseCode(Status httpResponseCode) {
266         this.httpResponseCode = httpResponseCode;
267         if (this.httpResponseCode == null) {
268             throw new IllegalArgumentException(
269                     "setHTTPResponseCode was passed an invalid String value, fix error.properties or your code "
270                             + httpResponseCode);
271         }
272     }
273
274     /**
275      * Gets the error text.
276      *
277      * @return the error text
278      */
279     public String getErrorText() {
280         return errorText;
281     }
282
283     /**
284      * Sets the error text.
285      *
286      * @param errorText the new error text
287      */
288     public void setErrorText(String errorText) {
289         this.errorText = errorText;
290     }
291
292     /**
293      * Gets the details.
294      *
295      * @return the details
296      */
297     public String getDetails() {
298         return details;
299     }
300
301     /**
302      * Sets the details.
303      *
304      * @param details the new details
305      */
306     public void setDetails(String details) {
307         this.details = details == null ? "" : details;
308     }
309
310     /**
311      * Sets the aai els error code.
312      *
313      * @param elsErrorCode the new code
314      */
315     public void setAaiElsErrorCode(String elsErrorCode) {
316         aaiElsErrorCode = elsErrorCode;
317     }
318
319     /**
320      * Gets the aai els error code.
321      *
322      * @return the code
323      */
324     public String getAaiElsErrorCode() {
325         return (aaiElsErrorCode);
326     }
327
328     /**
329      * Gets the error code string. This is also the string
330      * configured in Nagios to alert on
331      *
332      * @return the error code string
333      */
334     // Get the X.Y.Z representation of the error code
335     public String getErrorCodeString() {
336         String prefix = null;
337         switch (disposition) {
338             default:
339                 prefix = "";
340                 break;
341             case "5":
342                 prefix = "ERR.";
343                 break;
344         }
345         return prefix + disposition + "." + category + "." + errorCode;
346     }
347
348     /**
349      * Gets the severity Code. This is also the string
350      * configured in Nagios to alert on
351      *
352      * @return the severity
353      */
354     // Get the numerical value of severity
355     public String getSeverityCode(String severity) {
356         String severityCode = "";
357         switch (severity) {
358             case "WARN":
359                 severityCode = "1";
360                 break;
361             case "ERROR":
362                 severityCode = "2";
363                 break;
364             case "FATAL":
365                 severityCode = "3";
366                 break;
367         }
368         return severityCode;
369     }
370
371     /**
372      * {@inheritDoc}
373      */
374     @Override
375     public String toString() {
376         return "ErrorObject [errorCode=" + errorCode + ", errorText=" + errorText + ", restErrorCode=" + restErrorCode
377                 + ", httpResponseCode=" + httpResponseCode + ", severity=" + severity + ", disposition=" + disposition
378                 + ", category=" + category + "]";
379     }
380
381 }