Update the license for 2017-2018 license
[aai/aai-common.git] / aai-core / 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 package org.onap.aai.logging;
21
22 import javax.ws.rs.core.Response.Status;
23
24 /**
25  * 
26  * Contains the definition of all error message fields to be mapped from the Error
27  * properties file
28  *  
29  */
30 public class ErrorObject {
31         
32         private String disposition;
33         private String category;
34         private String severity;
35         private Status httpResponseCode = Status.INTERNAL_SERVER_ERROR; // default
36         private String restErrorCode = "3002";
37         private String errorCode;
38         private String errorText;
39
40         /**
41          * Instantiates a new error object.
42          */
43         public ErrorObject() {
44                 super();
45         }
46
47         /**
48          * Creates an error object with the default HTTP Error Code (Status.INTERNAL_SERVER_ERROR)
49          *
50          * @param disposition the disposition
51          * @param category the category
52          * @param severity the severity
53          * @param httpResponseCode the http response code
54          * @param restErrorCode the rest error code
55          * @param errorCode the error code
56          * @param errorText the error text
57          */
58         public ErrorObject(String disposition, String category, String severity, Integer httpResponseCode, String restErrorCode, String errorCode, String errorText) {
59                 super();
60                 this.setDisposition(disposition);
61                 this.setCategory(category);
62                 this.severity = severity;
63                 this.setHTTPResponseCode(httpResponseCode);
64                 this.setRESTErrorCode(restErrorCode);
65                 this.setErrorCode(errorCode);
66                 this.setErrorText(errorText);
67         }
68
69         // OLD STARTS HERE
70
71         /**
72          * Instantiates a new error object.
73          *
74          * @param severity the severity
75          * @param errorCode the error code
76          * @param errorText the error text
77          * @param disposition the disposition
78          * @param category the category
79          */
80         public ErrorObject(String severity, String errorCode, String errorText, String disposition, String category) {
81                 this(severity, Status.INTERNAL_SERVER_ERROR, errorCode, errorText, disposition, category);
82         }
83         
84         /**
85          * Instantiates a new error object.
86          *
87          * @param severity the severity
88          * @param httpResponseCode the http response code
89          * @param errorCode the error code
90          * @param errorText the error text
91          * @param disposition the disposition
92          * @param category the category
93          */
94         public ErrorObject(String severity, Integer httpResponseCode, String errorCode, String errorText, String disposition, String category) {
95                 super();
96                 this.severity = severity;
97                 this.setHTTPResponseCode(httpResponseCode);
98                 this.setErrorCode(errorCode);
99                 this.setErrorText(errorText);
100                 this.setDisposition(disposition);
101                 this.setCategory(category);
102         }
103         
104         /**
105          * Instantiates a new error object.
106          *
107          * @param severity the severity
108          * @param httpResponseCode the http response code
109          * @param errorCode the error code
110          * @param errorText the error text
111          * @param disposition the disposition
112          * @param category the category
113          */
114         public ErrorObject(String severity, Status httpResponseCode, String errorCode, String errorText, String disposition, String category) {
115                 super();
116                 this.severity = severity;
117                 this.setHTTPResponseCode(httpResponseCode);
118                 this.setErrorCode(errorCode);
119                 this.setErrorText(errorText);
120                 this.setDisposition(disposition);
121                 this.setCategory(category);
122         }
123         
124         /**
125          * Gets the disposition.
126          *
127          * @return the disposition
128          */
129         public String getDisposition() {
130                 return disposition;
131         }
132
133         /**
134          * Sets the disposition.
135          *
136          * @param disposition the new disposition
137          */
138         public void setDisposition(String disposition) {
139                 this.disposition = disposition;
140         }
141
142         /**
143          * Gets the category.
144          *
145          * @return the category
146          */
147         public String getCategory() {
148                 return category;
149         }
150
151         /**
152          * Sets the category.
153          *
154          * @param category the new category
155          */
156         public void setCategory(String category) {
157                 this.category = category;
158         }
159         
160         /**
161          * Gets the severity.
162          *
163          * @return the severity
164          */
165         public String getSeverity() {
166                 return severity;
167         }
168
169         /**
170          * Sets the severity.
171          *
172          * @param severity the new severity
173          */
174         public void setSeverity(String severity) {
175                 this.severity = severity;
176         }
177
178         /**
179          * Gets the error code.
180          *
181          * @return the error code
182          */
183         public String getErrorCode() {
184                 return errorCode;
185         }
186
187         /**
188          * Sets the error code.
189          *
190          * @param errorCode the new error code
191          */
192         public void setErrorCode(String errorCode) {
193                 this.errorCode = errorCode;
194         }
195         
196         /**
197          * Gets the HTTP response code.
198          *
199          * @return the HTTP response code
200          */
201         public Status getHTTPResponseCode() {
202                 return httpResponseCode;
203         }
204
205         /**
206          * Sets the HTTP response code.
207          *
208          * @param httpResponseCode the new HTTP response code
209          */
210         public void setHTTPResponseCode(Integer httpResponseCode) {
211                 this.httpResponseCode = Status.fromStatusCode(httpResponseCode);
212                 if (this.httpResponseCode == null) {
213                         throw new IllegalArgumentException("setHTTPResponseCode was passed an invalid Integer value, fix error.properties or your code "+httpResponseCode);
214                 }
215         }
216         
217         /**
218          * Sets the HTTP response code.
219          *
220          * @param httpResponseCode the new HTTP response code
221          */
222         public void setHTTPResponseCode(String httpResponseCode) {
223                 this.httpResponseCode = Status.fromStatusCode(Integer.valueOf(httpResponseCode));
224                 if (this.httpResponseCode == null) {
225                         throw new IllegalArgumentException("setHTTPResponseCode was passed an invalid String value, fix error.properties or your code "+httpResponseCode);
226                 }
227         }
228         
229         /**
230          * Sets the REST error code.
231          *
232          * @param restErrorCode the new REST error code
233          */
234         public void setRESTErrorCode(String restErrorCode) {
235                 this.restErrorCode = restErrorCode;
236         }
237         
238         /**
239          * Gets the REST error code.
240          *
241          * @return the REST error code
242          */
243         public String getRESTErrorCode() {
244                 return this.restErrorCode;
245         }
246         
247         /**
248          * Sets the HTTP response code.
249          *
250          * @param httpResponseCode the new HTTP response code
251          */
252         public void setHTTPResponseCode(Status httpResponseCode) {
253                 this.httpResponseCode = httpResponseCode;
254                 if (this.httpResponseCode == null) {
255                         throw new IllegalArgumentException("setHTTPResponseCode was passed an invalid String value, fix error.properties or your code "+httpResponseCode);
256                 }
257         }
258
259         /**
260          * Gets the error text.
261          *
262          * @return the error text
263          */
264         public String getErrorText() {
265                 return errorText;
266         }
267
268         /**
269          * Sets the error text.
270          *
271          * @param errorText the new error text
272          */
273         public void setErrorText(String errorText) {
274                 this.errorText = errorText;
275         }
276
277         /**
278          * Gets the error code string.  This is also the string
279          * configured in Nagios to alert on
280          *
281          * @return the error code string
282          */
283         // Get the X.Y.Z representation of the error code
284         public String getErrorCodeString() {
285                 String prefix = null;
286                 switch (disposition) {
287                 default:
288                         prefix = "";
289                         break;
290                 case "5":
291                         prefix = "ERR.";
292                         break;
293                 }
294                 return prefix + disposition + "." + category + "." + errorCode;
295         }
296         
297         /**
298          * Gets the severity Code.  This is also the string
299          * configured in Nagios to alert on
300          *
301          * @return the severity
302          */
303         // Get the numerical value of severity
304         public String getSeverityCode(String severity) {
305                 String severityCode = "";
306                 switch (severity) {
307                 case "WARN":
308                         severityCode = "1";
309                         break;
310                 case "ERROR":
311                         severityCode = "2";
312                         break;
313                 case "FATAL":
314                         severityCode = "3";
315                         break;
316                 }
317                 return severityCode;
318         }
319
320         /**
321          * {@inheritDoc}
322          */
323         @Override
324         public String toString() {
325                 return "ErrorObject [errorCode="+ errorCode + ", errorText=" + errorText
326                                 + ", restErrorCode=" + restErrorCode + ", httpResponseCode="+ httpResponseCode
327                                 + ", severity=" + severity + ", disposition=" + disposition + ", category=" + category +"]";
328         }
329         
330 }