[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / lib / openecomp-sdc-action-lib / openecomp-sdc-action-api / src / main / java / org / openecomp / sdc / action / util / ActionUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.sdc.action.util;
22
23 import static org.openecomp.sdc.action.ActionConstants.BEGIN_TIMESTAMP;
24 import static org.openecomp.sdc.action.ActionConstants.ELAPSED_TIME;
25 import static org.openecomp.sdc.action.ActionConstants.END_TIMESTAMP;
26 import static org.openecomp.sdc.action.ActionConstants.ERROR_CATEGORY;
27 import static org.openecomp.sdc.action.ActionConstants.ERROR_CODE;
28 import static org.openecomp.sdc.action.ActionConstants.ERROR_DESCRIPTION;
29 import static org.openecomp.sdc.action.ActionConstants.RESPONSE_CODE;
30 import static org.openecomp.sdc.action.ActionConstants.RESPONSE_DESCRIPTION;
31 import static org.openecomp.sdc.action.ActionConstants.SERVICE_METRIC_BEGIN_TIMESTAMP;
32 import static org.openecomp.sdc.action.ActionConstants.STATUS_CODE;
33 import static org.openecomp.sdc.action.ActionConstants.TARGET_ENTITY;
34 import static org.openecomp.sdc.action.ActionConstants.TARGET_SERVICE_NAME;
35 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_ALREADY_EXISTS_CODE;
36 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_CHECKSUM_ERROR_CODE;
37 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_DELETE_READ_ONLY;
38 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_DEL_LOCKED_OTHER_USER_CODE;
39 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_ENTITY_NOT_EXIST_CODE;
40 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_INVALID_NAME_CODE;
41 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_INVALID_PROTECTION_CODE;
42 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_TOO_BIG_ERROR_CODE;
43 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_UPDATE_READ_ONLY;
44 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_AUTHENTICATION_ERR_CODE;
45 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_AUTHORIZATION_ERR_CODE;
46 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_CHECKIN_ON_ENTITY_LOCKED_BY_OTHER_USER;
47 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_CHECKIN_ON_UNLOCKED_ENTITY;
48 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_CHECKOUT_ON_LOCKED_ENTITY;
49 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_CHECKOUT_ON_LOCKED_ENTITY_OTHER_USER;
50 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_DELETE_ON_LOCKED_ENTITY_CODE;
51 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_EDIT_ON_ENTITY_LOCKED_BY_OTHER_USER;
52 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ENTITY_NOT_EXIST_CODE;
53 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ENTITY_UNIQUE_VALUE_ERROR;
54 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INTERNAL_SERVER_ERR_CODE;
55 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INVALID_INSTANCE_ID_CODE;
56 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INVALID_PARAM_CODE;
57 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INVALID_REQUEST_BODY_CODE;
58 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INVALID_REQUEST_ID_CODE;
59 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INVALID_SEARCH_CRITERIA;
60 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_MULT_SEARCH_CRITERIA;
61 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_NOT_LOCKED_CODE;
62 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_QUERY_FAILURE_CODE;
63 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_REQUEST_INVALID_GENERIC_CODE;
64 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_SUBMIT_FINALIZED_ENTITY_NOT_ALLOWED;
65 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_SUBMIT_LOCKED_ENTITY_NOT_ALLOWED;
66 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UNDO_CHECKOUT_ON_ENTITY_LOCKED_BY_OTHER_USER;
67 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UNDO_CHECKOUT_ON_UNLOCKED_ENTITY;
68 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UPDATE_INVALID_VERSION;
69 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UPDATE_NOT_ALLOWED_CODE;
70 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UPDATE_NOT_ALLOWED_CODE_NAME;
71 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UPDATE_ON_UNLOCKED_ENTITY;
72 import static org.openecomp.sdc.action.types.ActionLogResponseCode.ACTION_NAME_ALREADY_EXISTS;
73 import static org.openecomp.sdc.action.types.ActionLogResponseCode.ACTION_NAME_UPDATE_NOT_ALLOWED;
74 import static org.openecomp.sdc.action.types.ActionLogResponseCode.ACTION_NOT_FOUND;
75 import static org.openecomp.sdc.action.types.ActionLogResponseCode.ACTION_NOT_LOCKED;
76 import static org.openecomp.sdc.action.types.ActionLogResponseCode.ARTIFACT_ALREADY_EXISTS;
77 import static org.openecomp.sdc.action.types.ActionLogResponseCode.ARTIFACT_DELETE_READ_ONLY;
78 import static org.openecomp.sdc.action.types.ActionLogResponseCode.ARTIFACT_NAME_INVALID;
79 import static org.openecomp.sdc.action.types.ActionLogResponseCode.ARTIFACT_NOT_FOUND;
80 import static org.openecomp.sdc.action.types.ActionLogResponseCode.ARTIFACT_PROTECTION_INVALID;
81 import static org.openecomp.sdc.action.types.ActionLogResponseCode.ARTIFACT_TOO_BIG;
82 import static org.openecomp.sdc.action.types.ActionLogResponseCode.ARTIFACT_UPDATE_READ_ONLY;
83 import static org.openecomp.sdc.action.types.ActionLogResponseCode.CHECKIN_ON_LOCKED_ENTITY_OTHER_USER;
84 import static org.openecomp.sdc.action.types.ActionLogResponseCode.CHECKIN_ON_UNLOCKED_ENTITY;
85 import static org.openecomp.sdc.action.types.ActionLogResponseCode.CHECKOUT_ON_LOCKED_ENTITY;
86 import static org.openecomp.sdc.action.types.ActionLogResponseCode.CHECKSUM_ERROR;
87 import static org.openecomp.sdc.action.types.ActionLogResponseCode.DELETE_ARTIFACT_ON_LOCKED_ENTITY;
88 import static org.openecomp.sdc.action.types.ActionLogResponseCode.DELETE_ON_LOCKED_ENTITY_OTHER_USER;
89 import static org.openecomp.sdc.action.types.ActionLogResponseCode.INTERNAL_SERVER_ERROR;
90 import static org.openecomp.sdc.action.types.ActionLogResponseCode.INVALID_REQUESTED_VERSION;
91 import static org.openecomp.sdc.action.types.ActionLogResponseCode.INVALID_REQUEST_PARAM;
92 import static org.openecomp.sdc.action.types.ActionLogResponseCode.INVALID_SEARCH_FILTER_CRITERIA;
93 import static org.openecomp.sdc.action.types.ActionLogResponseCode.MISSING_AUTHORIZATION;
94 import static org.openecomp.sdc.action.types.ActionLogResponseCode.MISSING_INSTANCE_ID_HEADER;
95 import static org.openecomp.sdc.action.types.ActionLogResponseCode.MISSING_REQUEST_BODY;
96 import static org.openecomp.sdc.action.types.ActionLogResponseCode.MISSING_REQUEST_ID_HEADER;
97 import static org.openecomp.sdc.action.types.ActionLogResponseCode.MULTIPLE_FILTER_CRITERIA_NOT_SUPPORTED;
98 import static org.openecomp.sdc.action.types.ActionLogResponseCode.QUERY_FAILURE;
99 import static org.openecomp.sdc.action.types.ActionLogResponseCode.SUBMIT_ON_FINAL_ENTITY;
100 import static org.openecomp.sdc.action.types.ActionLogResponseCode.SUBMIT_ON_LOCKED_ENTITY_OTHER_USER;
101 import static org.openecomp.sdc.action.types.ActionLogResponseCode.UNDO_CHECKOUT_ON_LOCKED_ENTITY;
102 import static org.openecomp.sdc.action.types.ActionLogResponseCode.UNDO_CHECKOUT_ON_UNLOCKED_ENTITY;
103 import static org.openecomp.sdc.action.types.ActionLogResponseCode.UPDATE_NOT_ALLOWED;
104 import static org.openecomp.sdc.action.types.ActionLogResponseCode.UPDATE_ON_LOCKED_ENTITY;
105 import static org.openecomp.sdc.action.types.ActionLogResponseCode.UPDATE_ON_UNLOCKED_ENTITY;
106
107 import org.openecomp.sdc.action.logging.CategoryLogLevel;
108 import org.openecomp.sdc.action.logging.StatusCode;
109 import org.openecomp.sdc.action.types.ActionLogResponseCode;
110 import org.openecomp.sdc.action.types.ActionSubOperation;
111 import org.slf4j.MDC;
112
113 import java.text.DateFormat;
114 import java.text.SimpleDateFormat;
115 import java.time.ZoneOffset;
116 import java.util.Date;
117 import java.util.TimeZone;
118
119 public class ActionUtil {
120
121   private static final String UTC_DATE_FORMAT = "dd MMM yyyy kk:mm:ss z";
122   private static final String LOG_UTC_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";
123
124   /**
125    * Get Current Timestamp in UTC format.
126    *
127    * @return Current Timestamp in UTC format
128    */
129   public static Date getCurrentTimeStampUtc() {
130     return Date.from(java.time.ZonedDateTime.now(ZoneOffset.UTC).toInstant());
131   }
132
133   /**
134    * Convert timestamp to UTC format date string.
135    *
136    * @param timeStamp UTC timestamp to be converted to the UTC Date format
137    * @return UTC formatted Date string from timestamp
138    */
139   public static String getUtcDateStringFromTimestamp(Date timeStamp) {
140     DateFormat df = new SimpleDateFormat(UTC_DATE_FORMAT);
141     df.setTimeZone(TimeZone.getTimeZone("GMT"));
142     return df.format(timeStamp);
143   }
144
145   /**
146    * Convert timestamp to UTC format date string.
147    *
148    * @param timeStamp UTC timestamp to be converted to the UTC Date format
149    * @return UTC formatted Date string from timestamp
150    */
151   public static String getLogUtcDateStringFromTimestamp(Date timeStamp) {
152     DateFormat df = new SimpleDateFormat(LOG_UTC_DATE_FORMAT);
153     df.setTimeZone(TimeZone.getTimeZone("GMT"));
154     return df.format(timeStamp);
155   }
156
157   /**
158    * Method to set up specific attributes MDC for the current logging operation.
159    *
160    * @param subOperation Request Name
161    */
162   public static void actionLogPreProcessor(ActionSubOperation subOperation, String targetEntity) {
163     MDC.put(BEGIN_TIMESTAMP, String.valueOf(System.currentTimeMillis()));
164     if (subOperation != null) {
165       MDC.put(TARGET_SERVICE_NAME, subOperation.name());
166     }
167
168     MDC.put(TARGET_ENTITY, targetEntity);
169   }
170
171   /**
172    * Method to enhance the MDC after the logging operation for Metrics and Audit logs.
173    *
174    * @param statusCode Response code for the current operation
175    */
176   public static void actionLogPostProcessor(StatusCode statusCode) {
177     actionLogPostProcessor(statusCode, false);
178   }
179
180   public static void actionLogPostProcessor(StatusCode statusCode, boolean isServiceMetricLog) {
181     actionLogPostProcessor(statusCode, null, isServiceMetricLog);
182   }
183
184   public static void actionLogPostProcessor(StatusCode statusCode, String responseCode,
185                                             boolean isServiceMetricLog) {
186     actionLogPostProcessor(statusCode, responseCode, null, isServiceMetricLog);
187   }
188
189   /**
190    * Action log post processor.
191    *
192    * @param statusCode          the status code
193    * @param responseCode        the response code
194    * @param responseDescription the response description
195    * @param isServiceMetricLog  the is service metric log
196    */
197   public static void actionLogPostProcessor(StatusCode statusCode, String responseCode,
198                                             String responseDescription,
199                                             boolean isServiceMetricLog) {
200     MDC.put(STATUS_CODE, statusCode.name());
201     if (responseCode != null) {
202       int logResponseCode = getLogResponseCode(responseCode);
203       MDC.put(RESPONSE_CODE, Integer.toString(logResponseCode));
204     }
205     MDC.put(RESPONSE_DESCRIPTION, responseDescription);
206     long beginTimestamp;
207     if (isServiceMetricLog) {
208       beginTimestamp = Long.valueOf(MDC.get(SERVICE_METRIC_BEGIN_TIMESTAMP));
209     } else {
210       beginTimestamp = Long.valueOf(MDC.get(BEGIN_TIMESTAMP));
211     }
212     long endTimestamp = System.currentTimeMillis();
213     MDC.put(BEGIN_TIMESTAMP, getLogUtcDateStringFromTimestamp(new Date(beginTimestamp)));
214     MDC.put(END_TIMESTAMP, getLogUtcDateStringFromTimestamp(new Date(endTimestamp)));
215     MDC.put(ELAPSED_TIME, String.valueOf(endTimestamp - beginTimestamp));
216   }
217
218   /**
219    * Action Library Error logging Helper.
220    *
221    * @param errorCategory    WARN or ERROR
222    * @param errorCode        Action Library exception code
223    * @param errorDescription Description of the error
224    */
225   public static void actionErrorLogProcessor(CategoryLogLevel errorCategory, String errorCode,
226                                              String errorDescription) {
227     MDC.put(ERROR_CATEGORY, errorCategory.name());
228     if (errorCode != null) {
229       String errorType = "";
230       switch (errorCategory) {
231         case WARN:
232           errorType = "W";
233           break;
234         case ERROR:
235           errorType = "E";
236           break;
237         case FATAL:
238           errorType = "F";
239           break;
240         default:
241       }
242       MDC.put(ERROR_CODE, getLogResponseCode(errorCode) + errorType);
243     }
244     MDC.put(ERROR_DESCRIPTION, errorDescription);
245   }
246
247   /**
248    * Method to convert Action Library exception codes to OPENECOMP Audit codes in {@link
249    * ActionLogResponseCode} e.g: ACT1060 --> 201
250    *
251    * @param errorCode Action library exception code
252    * @return Audit log code corresponding to the Action Library exception
253    */
254   public static int getLogResponseCode(String errorCode) {
255     ActionLogResponseCode responseCode = INTERNAL_SERVER_ERROR;
256     switch (errorCode) {
257       case ACTION_REQUEST_INVALID_GENERIC_CODE:
258         responseCode = INVALID_REQUEST_PARAM;
259         break;
260       case ACTION_AUTHENTICATION_ERR_CODE:
261         break;
262       case ACTION_AUTHORIZATION_ERR_CODE:
263         responseCode = MISSING_AUTHORIZATION;
264         break;
265       case ACTION_INVALID_INSTANCE_ID_CODE:
266         responseCode = MISSING_INSTANCE_ID_HEADER;
267         break;
268       case ACTION_INVALID_REQUEST_ID_CODE:
269         responseCode = MISSING_REQUEST_ID_HEADER;
270         break;
271       case ACTION_INVALID_PARAM_CODE:
272         responseCode = INVALID_REQUEST_PARAM;
273         break;
274       case ACTION_INVALID_REQUEST_BODY_CODE:
275         responseCode = MISSING_REQUEST_BODY;
276         break;
277       case ACTION_UPDATE_NOT_ALLOWED_CODE_NAME:
278         responseCode = ACTION_NAME_UPDATE_NOT_ALLOWED;
279         break;
280       case ACTION_CHECKOUT_ON_LOCKED_ENTITY:
281         responseCode = CHECKOUT_ON_LOCKED_ENTITY;
282         break;
283       case ACTION_ENTITY_UNIQUE_VALUE_ERROR:
284         responseCode = ACTION_NAME_ALREADY_EXISTS;
285         break;
286       case ACTION_INVALID_SEARCH_CRITERIA:
287         responseCode = INVALID_SEARCH_FILTER_CRITERIA;
288         break;
289       case ACTION_MULT_SEARCH_CRITERIA:
290         responseCode = MULTIPLE_FILTER_CRITERIA_NOT_SUPPORTED;
291         break;
292       case ACTION_UPDATE_ON_UNLOCKED_ENTITY:
293         responseCode = UPDATE_ON_UNLOCKED_ENTITY;
294         break;
295       case ACTION_UPDATE_INVALID_VERSION:
296         responseCode = INVALID_REQUESTED_VERSION;
297         break;
298       case ACTION_UPDATE_NOT_ALLOWED_CODE:
299         responseCode = UPDATE_NOT_ALLOWED;
300         break;
301       case ACTION_CHECKIN_ON_UNLOCKED_ENTITY:
302         responseCode = CHECKIN_ON_UNLOCKED_ENTITY;
303         break;
304       case ACTION_SUBMIT_FINALIZED_ENTITY_NOT_ALLOWED:
305         responseCode = SUBMIT_ON_FINAL_ENTITY;
306         break;
307       case ACTION_SUBMIT_LOCKED_ENTITY_NOT_ALLOWED:
308         responseCode = SUBMIT_ON_LOCKED_ENTITY_OTHER_USER;
309         break;
310       case ACTION_UNDO_CHECKOUT_ON_UNLOCKED_ENTITY:
311         responseCode = UNDO_CHECKOUT_ON_UNLOCKED_ENTITY;
312         break;
313       case ACTION_NOT_LOCKED_CODE:
314         responseCode = ACTION_NOT_LOCKED;
315         break;
316       case ACTION_ARTIFACT_CHECKSUM_ERROR_CODE:
317         responseCode = CHECKSUM_ERROR;
318         break;
319       case ACTION_ARTIFACT_TOO_BIG_ERROR_CODE:
320         responseCode = ARTIFACT_TOO_BIG;
321         break;
322       case ACTION_ARTIFACT_ALREADY_EXISTS_CODE:
323         responseCode = ARTIFACT_ALREADY_EXISTS;
324         break;
325       case ACTION_ARTIFACT_UPDATE_READ_ONLY:
326         responseCode = ARTIFACT_UPDATE_READ_ONLY;
327         break;
328       case ACTION_ARTIFACT_DELETE_READ_ONLY:
329         responseCode = ARTIFACT_DELETE_READ_ONLY;
330         break;
331       case ACTION_ARTIFACT_INVALID_PROTECTION_CODE:
332         responseCode = ARTIFACT_PROTECTION_INVALID;
333         break;
334       case ACTION_ARTIFACT_INVALID_NAME_CODE:
335         responseCode = ARTIFACT_NAME_INVALID;
336         break;
337       case ACTION_EDIT_ON_ENTITY_LOCKED_BY_OTHER_USER:
338         responseCode = UPDATE_ON_LOCKED_ENTITY;
339         break;
340       case ACTION_CHECKIN_ON_ENTITY_LOCKED_BY_OTHER_USER:
341         responseCode = CHECKIN_ON_LOCKED_ENTITY_OTHER_USER;
342         break;
343       case ACTION_CHECKOUT_ON_LOCKED_ENTITY_OTHER_USER:
344         responseCode = CHECKOUT_ON_LOCKED_ENTITY;
345         break;
346       case ACTION_UNDO_CHECKOUT_ON_ENTITY_LOCKED_BY_OTHER_USER:
347         responseCode = UNDO_CHECKOUT_ON_LOCKED_ENTITY;
348         break;
349       case ACTION_ENTITY_NOT_EXIST_CODE:
350         responseCode = ACTION_NOT_FOUND;
351         break;
352       case ACTION_ARTIFACT_ENTITY_NOT_EXIST_CODE:
353         responseCode = ARTIFACT_NOT_FOUND;
354         break;
355       case ACTION_ARTIFACT_DEL_LOCKED_OTHER_USER_CODE:
356         responseCode = DELETE_ARTIFACT_ON_LOCKED_ENTITY;
357         break;
358       case ACTION_DELETE_ON_LOCKED_ENTITY_CODE:
359         responseCode = DELETE_ON_LOCKED_ENTITY_OTHER_USER;
360         break;
361       case ACTION_INTERNAL_SERVER_ERR_CODE:
362         responseCode = INTERNAL_SERVER_ERROR;
363         break;
364       case ACTION_QUERY_FAILURE_CODE:
365         responseCode = QUERY_FAILURE;
366         break;
367       default:
368     }
369     return responseCode.getValue();
370   }
371 }