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 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;
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;
35 import static org.openecomp.sdc.action.ActionConstants.BEGIN_TIMESTAMP;
36 import static org.openecomp.sdc.action.ActionConstants.ELAPSED_TIME;
37 import static org.openecomp.sdc.action.ActionConstants.END_TIMESTAMP;
38 import static org.openecomp.sdc.action.ActionConstants.ERROR_CATEGORY;
39 import static org.openecomp.sdc.action.ActionConstants.ERROR_CODE;
40 import static org.openecomp.sdc.action.ActionConstants.ERROR_DESCRIPTION;
41 import static org.openecomp.sdc.action.ActionConstants.RESPONSE_CODE;
42 import static org.openecomp.sdc.action.ActionConstants.RESPONSE_DESCRIPTION;
43 import static org.openecomp.sdc.action.ActionConstants.SERVICE_METRIC_BEGIN_TIMESTAMP;
44 import static org.openecomp.sdc.action.ActionConstants.STATUS_CODE;
45 import static org.openecomp.sdc.action.ActionConstants.TARGET_ENTITY;
46 import static org.openecomp.sdc.action.ActionConstants.TARGET_SERVICE_NAME;
47 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_ALREADY_EXISTS_CODE;
48 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_CHECKSUM_ERROR_CODE;
49 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_DELETE_READ_ONLY;
50 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_DEL_LOCKED_OTHER_USER_CODE;
51 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_ENTITY_NOT_EXIST_CODE;
52 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_INVALID_NAME_CODE;
53 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_INVALID_PROTECTION_CODE;
54 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_TOO_BIG_ERROR_CODE;
55 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_UPDATE_READ_ONLY;
56 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_AUTHENTICATION_ERR_CODE;
57 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_AUTHORIZATION_ERR_CODE;
58 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_CHECKIN_ON_ENTITY_LOCKED_BY_OTHER_USER;
59 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_CHECKIN_ON_UNLOCKED_ENTITY;
60 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_CHECKOUT_ON_LOCKED_ENTITY;
61 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_CHECKOUT_ON_LOCKED_ENTITY_OTHER_USER;
62 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_DELETE_ON_LOCKED_ENTITY_CODE;
63 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_EDIT_ON_ENTITY_LOCKED_BY_OTHER_USER;
64 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ENTITY_NOT_EXIST_CODE;
65 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ENTITY_UNIQUE_VALUE_ERROR;
66 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INTERNAL_SERVER_ERR_CODE;
67 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INVALID_INSTANCE_ID_CODE;
68 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INVALID_PARAM_CODE;
69 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INVALID_REQUEST_BODY_CODE;
70 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INVALID_REQUEST_ID_CODE;
71 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INVALID_SEARCH_CRITERIA;
72 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_MULT_SEARCH_CRITERIA;
73 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_NOT_LOCKED_CODE;
74 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_QUERY_FAILURE_CODE;
75 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_REQUEST_INVALID_GENERIC_CODE;
76 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_SUBMIT_FINALIZED_ENTITY_NOT_ALLOWED;
77 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_SUBMIT_LOCKED_ENTITY_NOT_ALLOWED;
78 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UNDO_CHECKOUT_ON_ENTITY_LOCKED_BY_OTHER_USER;
79 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UNDO_CHECKOUT_ON_UNLOCKED_ENTITY;
80 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UPDATE_INVALID_VERSION;
81 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UPDATE_NOT_ALLOWED_CODE;
82 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UPDATE_NOT_ALLOWED_CODE_NAME;
83 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UPDATE_ON_UNLOCKED_ENTITY;
84 import static org.openecomp.sdc.action.types.ActionLogResponseCode.ACTION_NAME_ALREADY_EXISTS;
85 import static org.openecomp.sdc.action.types.ActionLogResponseCode.ACTION_NAME_UPDATE_NOT_ALLOWED;
86 import static org.openecomp.sdc.action.types.ActionLogResponseCode.ACTION_NOT_FOUND;
87 import static org.openecomp.sdc.action.types.ActionLogResponseCode.ACTION_NOT_LOCKED;
88 import static org.openecomp.sdc.action.types.ActionLogResponseCode.ARTIFACT_ALREADY_EXISTS;
89 import static org.openecomp.sdc.action.types.ActionLogResponseCode.ARTIFACT_DELETE_READ_ONLY;
90 import static org.openecomp.sdc.action.types.ActionLogResponseCode.ARTIFACT_NAME_INVALID;
91 import static org.openecomp.sdc.action.types.ActionLogResponseCode.ARTIFACT_NOT_FOUND;
92 import static org.openecomp.sdc.action.types.ActionLogResponseCode.ARTIFACT_PROTECTION_INVALID;
93 import static org.openecomp.sdc.action.types.ActionLogResponseCode.ARTIFACT_TOO_BIG;
94 import static org.openecomp.sdc.action.types.ActionLogResponseCode.ARTIFACT_UPDATE_READ_ONLY;
95 import static org.openecomp.sdc.action.types.ActionLogResponseCode.CHECKIN_ON_LOCKED_ENTITY_OTHER_USER;
96 import static org.openecomp.sdc.action.types.ActionLogResponseCode.CHECKIN_ON_UNLOCKED_ENTITY;
97 import static org.openecomp.sdc.action.types.ActionLogResponseCode.CHECKOUT_ON_LOCKED_ENTITY;
98 import static org.openecomp.sdc.action.types.ActionLogResponseCode.CHECKSUM_ERROR;
99 import static org.openecomp.sdc.action.types.ActionLogResponseCode.DELETE_ARTIFACT_ON_LOCKED_ENTITY;
100 import static org.openecomp.sdc.action.types.ActionLogResponseCode.DELETE_ON_LOCKED_ENTITY_OTHER_USER;
101 import static org.openecomp.sdc.action.types.ActionLogResponseCode.INTERNAL_SERVER_ERROR;
102 import static org.openecomp.sdc.action.types.ActionLogResponseCode.INVALID_REQUESTED_VERSION;
103 import static org.openecomp.sdc.action.types.ActionLogResponseCode.INVALID_REQUEST_PARAM;
104 import static org.openecomp.sdc.action.types.ActionLogResponseCode.INVALID_SEARCH_FILTER_CRITERIA;
105 import static org.openecomp.sdc.action.types.ActionLogResponseCode.MISSING_AUTHORIZATION;
106 import static org.openecomp.sdc.action.types.ActionLogResponseCode.MISSING_INSTANCE_ID_HEADER;
107 import static org.openecomp.sdc.action.types.ActionLogResponseCode.MISSING_REQUEST_BODY;
108 import static org.openecomp.sdc.action.types.ActionLogResponseCode.MISSING_REQUEST_ID_HEADER;
109 import static org.openecomp.sdc.action.types.ActionLogResponseCode.MULTIPLE_FILTER_CRITERIA_NOT_SUPPORTED;
110 import static org.openecomp.sdc.action.types.ActionLogResponseCode.QUERY_FAILURE;
111 import static org.openecomp.sdc.action.types.ActionLogResponseCode.SUBMIT_ON_FINAL_ENTITY;
112 import static org.openecomp.sdc.action.types.ActionLogResponseCode.SUBMIT_ON_LOCKED_ENTITY_OTHER_USER;
113 import static org.openecomp.sdc.action.types.ActionLogResponseCode.UNDO_CHECKOUT_ON_LOCKED_ENTITY;
114 import static org.openecomp.sdc.action.types.ActionLogResponseCode.UNDO_CHECKOUT_ON_UNLOCKED_ENTITY;
115 import static org.openecomp.sdc.action.types.ActionLogResponseCode.UPDATE_NOT_ALLOWED;
116 import static org.openecomp.sdc.action.types.ActionLogResponseCode.UPDATE_ON_LOCKED_ENTITY;
117 import static org.openecomp.sdc.action.types.ActionLogResponseCode.UPDATE_ON_UNLOCKED_ENTITY;
119 public class ActionUtil {
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";
125 * Get Current Timestamp in UTC format.
127 * @return Current Timestamp in UTC format
129 public static Date getCurrentTimeStampUtc() {
130 return Date.from(java.time.ZonedDateTime.now(ZoneOffset.UTC).toInstant());
134 * Convert timestamp to UTC format date string.
136 * @param timeStamp UTC timestamp to be converted to the UTC Date format
137 * @return UTC formatted Date string from timestamp
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);
146 * Convert timestamp to UTC format date string.
148 * @param timeStamp UTC timestamp to be converted to the UTC Date format
149 * @return UTC formatted Date string from timestamp
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);
158 * Method to set up specific attributes MDC for the current logging operation.
160 * @param subOperation Request Name
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());
168 MDC.put(TARGET_ENTITY, targetEntity);
172 * Method to enhance the MDC after the logging operation for Metrics and Audit logs.
174 * @param statusCode Response code for the current operation
176 public static void actionLogPostProcessor(StatusCode statusCode) {
177 actionLogPostProcessor(statusCode, false);
180 public static void actionLogPostProcessor(StatusCode statusCode, boolean isServiceMetricLog) {
181 actionLogPostProcessor(statusCode, null, isServiceMetricLog);
184 public static void actionLogPostProcessor(StatusCode statusCode, String responseCode,
185 boolean isServiceMetricLog) {
186 actionLogPostProcessor(statusCode, responseCode, null, isServiceMetricLog);
190 * Action log post processor.
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
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));
205 MDC.put(RESPONSE_DESCRIPTION, responseDescription);
207 if (isServiceMetricLog) {
208 beginTimestamp = Long.valueOf(MDC.get(SERVICE_METRIC_BEGIN_TIMESTAMP));
210 beginTimestamp = Long.valueOf(MDC.get(BEGIN_TIMESTAMP));
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));
219 * Action Library Error logging Helper.
221 * @param errorCategory WARN or ERROR
222 * @param errorCode Action Library exception code
223 * @param errorDescription Description of the error
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) {
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;
369 return responseCode.getValue();