2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.action.util;
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;
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;
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;
120 public class ActionUtil {
122 private static final String UTC_DATE_FORMAT = "dd MMM yyyy kk:mm:ss z";
123 private static final String LOG_UTC_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";
126 * Get Current Timestamp in UTC format.
128 * @return Current Timestamp in UTC format.
130 public static Date getCurrentTimeStampUtc() {
131 return Date.from(java.time.ZonedDateTime.now(ZoneOffset.UTC).toInstant());
135 * Convert timestamp to UTC format date string.
137 * @param timeStamp UTC timestamp to be converted to the UTC Date format.
138 * @return UTC formatted Date string from timestamp.
140 public static String getUtcDateStringFromTimestamp(Date timeStamp) {
141 DateFormat df = new SimpleDateFormat(UTC_DATE_FORMAT);
142 df.setTimeZone(TimeZone.getTimeZone("GMT"));
143 return df.format(timeStamp);
147 * Convert timestamp to UTC format date string.
149 * @param timeStamp UTC timestamp to be converted to the UTC Date format.
150 * @return UTC formatted Date string from timestamp.
152 public static String getLogUtcDateStringFromTimestamp(Date timeStamp) {
153 DateFormat df = new SimpleDateFormat(LOG_UTC_DATE_FORMAT);
154 df.setTimeZone(TimeZone.getTimeZone("GMT"));
155 return df.format(timeStamp);
159 * Method to set up specific attributes MDC for the current logging operation.
161 * @param subOperation Request Name.
163 public static void actionLogPreProcessor(ActionSubOperation subOperation, String targetEntity) {
164 MDC.put(BEGIN_TIMESTAMP, String.valueOf(System.currentTimeMillis()));
165 if (subOperation != null) {
166 MDC.put(TARGET_SERVICE_NAME, subOperation.name());
169 MDC.put(TARGET_ENTITY, targetEntity);
173 * Method to enhance the MDC after the logging operation for Metrics and Audit logs.
175 * @param statusCode Response code for the current operation.
177 public static void actionLogPostProcessor(StatusCode statusCode) {
178 actionLogPostProcessor(statusCode, false);
181 public static void actionLogPostProcessor(StatusCode statusCode, boolean isServiceMetricLog) {
182 actionLogPostProcessor(statusCode, null, isServiceMetricLog);
185 public static void actionLogPostProcessor(StatusCode statusCode, String responseCode,
186 boolean isServiceMetricLog) {
187 actionLogPostProcessor(statusCode, responseCode, null, isServiceMetricLog);
191 * Method to enhance the MDC after the logging operation for Metrics and Audit logs.
193 * @param statusCode Response code for the current operation.
196 public static void actionLogPostProcessor(StatusCode statusCode, String responseCode,
197 String responseDescription,
198 boolean isServiceMetricLog) {
199 MDC.put(STATUS_CODE, statusCode.name());
200 if (responseCode != null) {
201 int logResponseCode = getLogResponseCode(responseCode);
202 MDC.put(RESPONSE_CODE, Integer.toString(logResponseCode));
204 MDC.put(RESPONSE_DESCRIPTION, responseDescription);
206 if (isServiceMetricLog) {
207 beginTimestamp = Long.valueOf(MDC.get(SERVICE_METRIC_BEGIN_TIMESTAMP));
209 beginTimestamp = Long.valueOf(MDC.get(BEGIN_TIMESTAMP));
211 long endTimestamp = System.currentTimeMillis();
212 MDC.put(BEGIN_TIMESTAMP, getLogUtcDateStringFromTimestamp(new Date(beginTimestamp)));
213 MDC.put(END_TIMESTAMP, getLogUtcDateStringFromTimestamp(new Date(endTimestamp)));
214 MDC.put(ELAPSED_TIME, String.valueOf(endTimestamp - beginTimestamp));
218 * Action Library Error logging Helper.
220 * @param errorCategory WARN or ERROR.
221 * @param errorCode Action Library exception code.
222 * @param errorDescription Description of the error.
224 public static void actionErrorLogProcessor(CategoryLogLevel errorCategory, String errorCode,
225 String errorDescription) {
226 MDC.put(ERROR_CATEGORY, errorCategory.name());
227 if (errorCode != null) {
228 String errorType = "";
229 switch (errorCategory) {
242 MDC.put(ERROR_CODE, getLogResponseCode(errorCode) + errorType);
244 MDC.put(ERROR_DESCRIPTION, errorDescription);
248 * Method to convert Action Library exception codes to OPENECOMP Audit codes in {@link.
249 * ActionLogResponseCode} e.g: ACT1060 --> 201.
251 * @param errorCode Action library exception code.
252 * @return Audit log code corresponding to the Action Library exception.
254 public static int getLogResponseCode(String errorCode) {
255 ActionLogResponseCode responseCode = INTERNAL_SERVER_ERROR;
257 case ACTION_REQUEST_INVALID_GENERIC_CODE:
258 responseCode = INVALID_REQUEST_PARAM;
260 case ACTION_AUTHENTICATION_ERR_CODE:
262 case ACTION_AUTHORIZATION_ERR_CODE:
263 responseCode = MISSING_AUTHORIZATION;
265 case ACTION_INVALID_INSTANCE_ID_CODE:
266 responseCode = MISSING_INSTANCE_ID_HEADER;
268 case ACTION_INVALID_REQUEST_ID_CODE:
269 responseCode = MISSING_REQUEST_ID_HEADER;
271 case ACTION_INVALID_PARAM_CODE:
272 responseCode = INVALID_REQUEST_PARAM;
274 case ACTION_INVALID_REQUEST_BODY_CODE:
275 responseCode = MISSING_REQUEST_BODY;
277 case ACTION_UPDATE_NOT_ALLOWED_CODE_NAME:
278 responseCode = ACTION_NAME_UPDATE_NOT_ALLOWED;
280 case ACTION_CHECKOUT_ON_LOCKED_ENTITY:
281 responseCode = CHECKOUT_ON_LOCKED_ENTITY;
283 case ACTION_ENTITY_UNIQUE_VALUE_ERROR:
284 responseCode = ACTION_NAME_ALREADY_EXISTS;
286 case ACTION_INVALID_SEARCH_CRITERIA:
287 responseCode = INVALID_SEARCH_FILTER_CRITERIA;
289 case ACTION_MULT_SEARCH_CRITERIA:
290 responseCode = MULTIPLE_FILTER_CRITERIA_NOT_SUPPORTED;
292 case ACTION_UPDATE_ON_UNLOCKED_ENTITY:
293 responseCode = UPDATE_ON_UNLOCKED_ENTITY;
295 case ACTION_UPDATE_INVALID_VERSION:
296 responseCode = INVALID_REQUESTED_VERSION;
298 case ACTION_UPDATE_NOT_ALLOWED_CODE:
299 responseCode = UPDATE_NOT_ALLOWED;
301 case ACTION_CHECKIN_ON_UNLOCKED_ENTITY:
302 responseCode = CHECKIN_ON_UNLOCKED_ENTITY;
304 case ACTION_SUBMIT_FINALIZED_ENTITY_NOT_ALLOWED:
305 responseCode = SUBMIT_ON_FINAL_ENTITY;
307 case ACTION_SUBMIT_LOCKED_ENTITY_NOT_ALLOWED:
308 responseCode = SUBMIT_ON_LOCKED_ENTITY_OTHER_USER;
310 case ACTION_UNDO_CHECKOUT_ON_UNLOCKED_ENTITY:
311 responseCode = UNDO_CHECKOUT_ON_UNLOCKED_ENTITY;
313 case ACTION_NOT_LOCKED_CODE:
314 responseCode = ACTION_NOT_LOCKED;
316 case ACTION_ARTIFACT_CHECKSUM_ERROR_CODE:
317 responseCode = CHECKSUM_ERROR;
319 case ACTION_ARTIFACT_TOO_BIG_ERROR_CODE:
320 responseCode = ARTIFACT_TOO_BIG;
322 case ACTION_ARTIFACT_ALREADY_EXISTS_CODE:
323 responseCode = ARTIFACT_ALREADY_EXISTS;
325 case ACTION_ARTIFACT_UPDATE_READ_ONLY:
326 responseCode = ARTIFACT_UPDATE_READ_ONLY;
328 case ACTION_ARTIFACT_DELETE_READ_ONLY:
329 responseCode = ARTIFACT_DELETE_READ_ONLY;
331 case ACTION_ARTIFACT_INVALID_PROTECTION_CODE:
332 responseCode = ARTIFACT_PROTECTION_INVALID;
334 case ACTION_ARTIFACT_INVALID_NAME_CODE:
335 responseCode = ARTIFACT_NAME_INVALID;
337 case ACTION_EDIT_ON_ENTITY_LOCKED_BY_OTHER_USER:
338 responseCode = UPDATE_ON_LOCKED_ENTITY;
340 case ACTION_CHECKIN_ON_ENTITY_LOCKED_BY_OTHER_USER:
341 responseCode = CHECKIN_ON_LOCKED_ENTITY_OTHER_USER;
343 case ACTION_CHECKOUT_ON_LOCKED_ENTITY_OTHER_USER:
344 responseCode = CHECKOUT_ON_LOCKED_ENTITY;
346 case ACTION_UNDO_CHECKOUT_ON_ENTITY_LOCKED_BY_OTHER_USER:
347 responseCode = UNDO_CHECKOUT_ON_LOCKED_ENTITY;
349 case ACTION_ENTITY_NOT_EXIST_CODE:
350 responseCode = ACTION_NOT_FOUND;
352 case ACTION_ARTIFACT_ENTITY_NOT_EXIST_CODE:
353 responseCode = ARTIFACT_NOT_FOUND;
355 case ACTION_ARTIFACT_DEL_LOCKED_OTHER_USER_CODE:
356 responseCode = DELETE_ARTIFACT_ON_LOCKED_ENTITY;
358 case ACTION_DELETE_ON_LOCKED_ENTITY_CODE:
359 responseCode = DELETE_ON_LOCKED_ENTITY_OTHER_USER;
361 case ACTION_INTERNAL_SERVER_ERR_CODE:
362 responseCode = INTERNAL_SERVER_ERROR;
364 case ACTION_QUERY_FAILURE_CODE:
365 responseCode = QUERY_FAILURE;
370 return responseCode.getValue();