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