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.*;
36 import static org.openecomp.sdc.action.errors.ActionErrorConstants.*;
37 import static org.openecomp.sdc.action.types.ActionLogResponseCode.*;
39 public class ActionUtil {
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";
45 * Get Current Timestamp in UTC format.
47 * @return Current Timestamp in UTC format
49 public static Date getCurrentTimeStampUtc() {
50 return Date.from(java.time.ZonedDateTime.now(ZoneOffset.UTC).toInstant());
54 * Convert timestamp to UTC format date string.
56 * @param timeStamp UTC timestamp to be converted to the UTC Date format
57 * @return UTC formatted Date string from timestamp
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);
66 * Convert timestamp to UTC format date string.
68 * @param timeStamp UTC timestamp to be converted to the UTC Date format
69 * @return UTC formatted Date string from timestamp
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);
78 * Method to set up specific attributes MDC for the current logging operation.
80 * @param subOperation Request Name
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());
88 MDC.put(TARGET_ENTITY, targetEntity);
92 * Method to enhance the MDC after the logging operation for Metrics and Audit logs.
94 * @param statusCode Response code for the current operation
96 public static void actionLogPostProcessor(StatusCode statusCode) {
97 actionLogPostProcessor(statusCode, false);
100 public static void actionLogPostProcessor(StatusCode statusCode, boolean isServiceMetricLog) {
101 actionLogPostProcessor(statusCode, null, isServiceMetricLog);
104 public static void actionLogPostProcessor(StatusCode statusCode, String responseCode,
105 boolean isServiceMetricLog) {
106 actionLogPostProcessor(statusCode, responseCode, null, isServiceMetricLog);
110 * Action log post processor.
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
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));
125 MDC.put(RESPONSE_DESCRIPTION, responseDescription);
127 if (isServiceMetricLog) {
128 beginTimestamp = Long.valueOf(MDC.get(SERVICE_METRIC_BEGIN_TIMESTAMP));
130 beginTimestamp = Long.valueOf(MDC.get(BEGIN_TIMESTAMP));
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));
139 * Action Library Error logging Helper.
141 * @param errorCategory WARN or ERROR
142 * @param errorCode Action Library exception code
143 * @param errorDescription Description of the error
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) {
162 MDC.put(ERROR_CODE, getLogResponseCode(errorCode) + errorType);
164 MDC.put(ERROR_DESCRIPTION, errorDescription);
168 * Method to convert Action Library exception codes to OPENECOMP Audit codes in {@link
169 * ActionLogResponseCode} e.g: ACT1060 --> 201
171 * @param errorCode Action library exception code
172 * @return Audit log code corresponding to the Action Library exception
174 public static int getLogResponseCode(String errorCode) {
175 ActionLogResponseCode responseCode = INTERNAL_SERVER_ERROR;
177 case ACTION_REQUEST_INVALID_GENERIC_CODE:
178 responseCode = INVALID_REQUEST_PARAM;
180 case ACTION_AUTHENTICATION_ERR_CODE:
182 case ACTION_AUTHORIZATION_ERR_CODE:
183 responseCode = MISSING_AUTHORIZATION;
185 case ACTION_INVALID_INSTANCE_ID_CODE:
186 responseCode = MISSING_INSTANCE_ID_HEADER;
188 case ACTION_INVALID_REQUEST_ID_CODE:
189 responseCode = MISSING_REQUEST_ID_HEADER;
191 case ACTION_INVALID_PARAM_CODE:
192 responseCode = INVALID_REQUEST_PARAM;
194 case ACTION_INVALID_REQUEST_BODY_CODE:
195 responseCode = MISSING_REQUEST_BODY;
197 case ACTION_UPDATE_NOT_ALLOWED_CODE_NAME:
198 responseCode = ACTION_NAME_UPDATE_NOT_ALLOWED;
200 case ACTION_CHECKOUT_ON_LOCKED_ENTITY:
201 responseCode = CHECKOUT_ON_LOCKED_ENTITY;
203 case ACTION_ENTITY_UNIQUE_VALUE_ERROR:
204 responseCode = ACTION_NAME_ALREADY_EXISTS;
206 case ACTION_INVALID_SEARCH_CRITERIA:
207 responseCode = INVALID_SEARCH_FILTER_CRITERIA;
209 case ACTION_MULT_SEARCH_CRITERIA:
210 responseCode = MULTIPLE_FILTER_CRITERIA_NOT_SUPPORTED;
212 case ACTION_UPDATE_ON_UNLOCKED_ENTITY:
213 responseCode = UPDATE_ON_UNLOCKED_ENTITY;
215 case ACTION_UPDATE_INVALID_VERSION:
216 responseCode = INVALID_REQUESTED_VERSION;
218 case ACTION_UPDATE_NOT_ALLOWED_CODE:
219 responseCode = UPDATE_NOT_ALLOWED;
221 case ACTION_CHECKIN_ON_UNLOCKED_ENTITY:
222 responseCode = CHECKIN_ON_UNLOCKED_ENTITY;
224 case ACTION_SUBMIT_FINALIZED_ENTITY_NOT_ALLOWED:
225 responseCode = SUBMIT_ON_FINAL_ENTITY;
227 case ACTION_SUBMIT_LOCKED_ENTITY_NOT_ALLOWED:
228 responseCode = SUBMIT_ON_LOCKED_ENTITY_OTHER_USER;
230 case ACTION_UNDO_CHECKOUT_ON_UNLOCKED_ENTITY:
231 responseCode = UNDO_CHECKOUT_ON_UNLOCKED_ENTITY;
233 case ACTION_NOT_LOCKED_CODE:
234 responseCode = ACTION_NOT_LOCKED;
236 case ACTION_ARTIFACT_CHECKSUM_ERROR_CODE:
237 responseCode = CHECKSUM_ERROR;
239 case ACTION_ARTIFACT_TOO_BIG_ERROR_CODE:
240 responseCode = ARTIFACT_TOO_BIG;
242 case ACTION_ARTIFACT_ALREADY_EXISTS_CODE:
243 responseCode = ARTIFACT_ALREADY_EXISTS;
245 case ACTION_ARTIFACT_UPDATE_READ_ONLY:
246 responseCode = ARTIFACT_UPDATE_READ_ONLY;
248 case ACTION_ARTIFACT_DELETE_READ_ONLY:
249 responseCode = ARTIFACT_DELETE_READ_ONLY;
251 case ACTION_ARTIFACT_INVALID_PROTECTION_CODE:
252 responseCode = ARTIFACT_PROTECTION_INVALID;
254 case ACTION_ARTIFACT_INVALID_NAME_CODE:
255 responseCode = ARTIFACT_NAME_INVALID;
257 case ACTION_EDIT_ON_ENTITY_LOCKED_BY_OTHER_USER:
258 responseCode = UPDATE_ON_LOCKED_ENTITY;
260 case ACTION_CHECKIN_ON_ENTITY_LOCKED_BY_OTHER_USER:
261 responseCode = CHECKIN_ON_LOCKED_ENTITY_OTHER_USER;
263 case ACTION_CHECKOUT_ON_LOCKED_ENTITY_OTHER_USER:
264 responseCode = CHECKOUT_ON_LOCKED_ENTITY;
266 case ACTION_UNDO_CHECKOUT_ON_ENTITY_LOCKED_BY_OTHER_USER:
267 responseCode = UNDO_CHECKOUT_ON_LOCKED_ENTITY;
269 case ACTION_ENTITY_NOT_EXIST_CODE:
270 responseCode = ACTION_NOT_FOUND;
272 case ACTION_ARTIFACT_ENTITY_NOT_EXIST_CODE:
273 responseCode = ARTIFACT_NOT_FOUND;
275 case ACTION_ARTIFACT_DEL_LOCKED_OTHER_USER_CODE:
276 responseCode = DELETE_ARTIFACT_ON_LOCKED_ENTITY;
278 case ACTION_DELETE_ON_LOCKED_ENTITY_CODE:
279 responseCode = DELETE_ON_LOCKED_ENTITY_OTHER_USER;
281 case ACTION_INTERNAL_SERVER_ERR_CODE:
282 responseCode = INTERNAL_SERVER_ERROR;
284 case ACTION_QUERY_FAILURE_CODE:
285 responseCode = QUERY_FAILURE;
289 return responseCode.getValue();