2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.portal.logging.logic;
40 import static com.att.eelf.configuration.Configuration.MDC_ALERT_SEVERITY;
42 import java.text.MessageFormat;
44 import org.onap.portalapp.portal.logging.format.EPAppMessagesEnum;
45 import org.onap.portalsdk.core.logging.format.AlarmSeverityEnum;
46 import org.onap.portalsdk.core.logging.format.ErrorSeverityEnum;
47 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
49 import org.springframework.http.HttpStatus;
51 import com.att.eelf.configuration.EELFLogger;
52 import com.att.eelf.configuration.EELFManager;
54 public class EPLogUtil {
56 // This class has no logger of its own; it uses loggers passed to it.
57 private static EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
60 * Formats and writes a message to the error log with the class name and the
61 * specified parameters, using log level info, warn or error appropriate for
62 * the specified severity
65 * Logger for the class where the error occurred; the logger
66 * carries the class name.
67 * @param epMessageEnum
68 * Enum carrying alarm and error severity
70 * Values used to build the message.
72 public static void logEcompError(EELFLoggerDelegate classLogger, EPAppMessagesEnum epMessageEnum, String... param) {
73 logEcompError(classLogger, epMessageEnum, null, param);
77 * Formats and writes a message to the error log with the class name and the
78 * specified parameters, using log level info, warn or error appropriate for
79 * the specified severity
81 * @param epMessageEnum
82 * Enum carrying alarm and error severity
84 * Values used to build the message.
86 public static void logEcompError(EPAppMessagesEnum epMessageEnum, String... param) {
88 AlarmSeverityEnum alarmSeverityEnum = epMessageEnum.getAlarmSeverity();
89 ErrorSeverityEnum errorSeverityEnum = epMessageEnum.getErrorSeverity();
91 MDC.put(MDC_ALERT_SEVERITY, alarmSeverityEnum.severity());
92 MDC.put("ErrorCode", epMessageEnum.getErrorCode());
93 MDC.put("ErrorDescription", epMessageEnum.getErrorDescription());
94 MDC.put("ClassName", EPLogUtil.class.getName());
96 String resolution = EPLogUtil
97 .formatMessage(epMessageEnum.getDetails() + " " + epMessageEnum.getResolution(), (Object[]) param);
98 if (errorSeverityEnum == ErrorSeverityEnum.WARN) {
99 errorLogger.warn(resolution);
100 } else if (errorSeverityEnum == ErrorSeverityEnum.INFO) {
101 errorLogger.info(resolution);
103 errorLogger.error(resolution);
105 } catch (Exception e) {
106 errorLogger.error("logEcompError failed", e);
108 MDC.remove("ErrorCode");
109 MDC.remove("ErrorDescription");
110 MDC.remove("ClassName");
111 MDC.remove(MDC_ALERT_SEVERITY);
116 * Formats and writes a message to the error log with the class name,
117 * throwable and the specified parameters, using log level info, warn or
118 * error appropriate for the specified severity
121 * Logger for the class where the error occurred; the logger
122 * carries the class name.
123 * @param epMessageEnum
124 * Enum carrying alarm and error severity
126 * Throwable; ignored if null
128 * Array of Strings used to build the message.
130 @SuppressWarnings("static-access")
131 public static void logEcompError(EELFLoggerDelegate classLogger, EPAppMessagesEnum epMessageEnum, Throwable th,
134 AlarmSeverityEnum alarmSeverityEnum = epMessageEnum.getAlarmSeverity();
135 ErrorSeverityEnum errorSeverityEnum = epMessageEnum.getErrorSeverity();
137 MDC.put(MDC_ALERT_SEVERITY, alarmSeverityEnum.severity());
138 MDC.put("ErrorCode", epMessageEnum.getErrorCode());
139 MDC.put("ErrorDescription", epMessageEnum.getErrorDescription());
141 final String message = EPLogUtil.formatMessage(epMessageEnum.getDetails() + " " + epMessageEnum.getResolution(),
143 if (errorSeverityEnum == ErrorSeverityEnum.INFO) {
145 classLogger.info(classLogger.errorLogger, message);
147 classLogger.info(classLogger.errorLogger, message, th);
148 } else if (errorSeverityEnum == ErrorSeverityEnum.WARN) {
150 classLogger.warn(classLogger.errorLogger, message);
152 classLogger.warn(classLogger.errorLogger, message, th);
155 classLogger.error(classLogger.errorLogger, message);
157 classLogger.error(classLogger.errorLogger, message, th);
161 MDC.remove(MDC_ALERT_SEVERITY);
162 MDC.remove("ErrorCode");
163 MDC.remove("ErrorDescription");
167 * Builds a string using the format and parameters.
173 private static String formatMessage(String message, Object... args) {
174 StringBuilder sbFormattedMessage = new StringBuilder();
175 if (args != null && args.length > 0 && message != null && message != "") {
176 MessageFormat mf = new MessageFormat(message);
177 sbFormattedMessage.append(mf.format(args));
179 sbFormattedMessage.append(message);
181 return sbFormattedMessage.toString();
185 * Builds a comma-separated string of values to document a user action.
197 * @return Value suitable for writing to the audit log file.
199 public static String formatAuditLogMessage(String action, String activity, String userId, String affectedId,
201 StringBuilder auditLogMsg = new StringBuilder();
202 auditLogMsg.append("Click_A:[");
203 if (action != null && !action.equals("")) {
204 auditLogMsg.append(" Action: ");
205 auditLogMsg.append(action);
208 if (activity != null && !activity.equals("")) {
209 auditLogMsg.append(",Activity CD: ");
210 auditLogMsg.append(activity);
213 if (userId != null && !userId.equals("")) {
214 auditLogMsg.append(",User ID: ");
215 auditLogMsg.append(userId);
218 if (affectedId != null && !affectedId.equals("")) {
219 auditLogMsg.append(",Affected ID: ");
220 auditLogMsg.append(affectedId);
223 if (comment != null && !comment.equals("")) {
224 auditLogMsg.append(",Comment: ");
225 auditLogMsg.append(comment);
227 auditLogMsg.append("]");
228 return auditLogMsg.toString();
232 * Builds a comma-separated string of values to document a user browser
251 * @return String value suitable for writing to the audit log file.
253 public static String formatStoreAnalyticsAuditLogMessage(String orgUserId, String appName, String action,
254 String activity, String actionLink, String page, String function, String type) {
255 StringBuilder auditLogStoreAnalyticsMsg = new StringBuilder();
256 auditLogStoreAnalyticsMsg.append("Click_Analytics:[");
257 if (orgUserId != null && !orgUserId.equals("")) {
258 auditLogStoreAnalyticsMsg.append(" Organization User ID: ");
259 auditLogStoreAnalyticsMsg.append(orgUserId);
262 if (appName != null && !appName.equals("")) {
263 auditLogStoreAnalyticsMsg.append(",AppName: ");
264 auditLogStoreAnalyticsMsg.append(appName);
267 if (action != null && !action.equals("")) {
268 auditLogStoreAnalyticsMsg.append(",Action: ");
269 auditLogStoreAnalyticsMsg.append(action);
272 if (activity != null && !activity.equals("")) {
273 auditLogStoreAnalyticsMsg.append(",Activity: ");
274 auditLogStoreAnalyticsMsg.append(activity);
277 if (actionLink != null && !actionLink.equals("")) {
278 auditLogStoreAnalyticsMsg.append(",ActionLink: ");
279 auditLogStoreAnalyticsMsg.append(actionLink);
282 if (page != null && !page.equals("")) {
283 auditLogStoreAnalyticsMsg.append(",Page: ");
284 auditLogStoreAnalyticsMsg.append(page);
287 if (function != null && !function.equals("")) {
288 auditLogStoreAnalyticsMsg.append(",Function: ");
289 auditLogStoreAnalyticsMsg.append(function);
292 if (type != null && !type.equals("")) {
293 auditLogStoreAnalyticsMsg.append(",Type: ");
294 auditLogStoreAnalyticsMsg.append(type);
296 auditLogStoreAnalyticsMsg.append("]");
297 return auditLogStoreAnalyticsMsg.toString();
300 public static void logExternalAuthAccessAlarm(EELFLoggerDelegate logger, HttpStatus res) {
301 if (res.equals(HttpStatus.UNAUTHORIZED) || res.equals(HttpStatus.FORBIDDEN)) {
302 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.ExternalAuthAccessAuthenticationError);
303 } else if (res.equals(HttpStatus.NOT_FOUND) || res.equals(HttpStatus.NOT_ACCEPTABLE)
304 || res.equals(HttpStatus.CONFLICT) || res.equals(HttpStatus.BAD_REQUEST)) {
305 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.ExternalAuthAccessConnectionError);
306 } else if (!res.equals(HttpStatus.ACCEPTED) && !res.equals(HttpStatus.OK)) {
307 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.ExternalAuthAccessGeneralError);
311 public static void schedulerAccessAlarm(EELFLoggerDelegate logger, int res) {
312 if (res == HttpStatus.UNAUTHORIZED.value() || res == HttpStatus.FORBIDDEN.value()) {
313 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.SchedulerAuxAccessAuthenticationError);
314 } else if (res == HttpStatus.NOT_FOUND.value() || res == HttpStatus.NOT_ACCEPTABLE.value()
315 || res == HttpStatus.CONFLICT.value() || res == HttpStatus.BAD_REQUEST.value()
316 || res == HttpStatus.REQUEST_TIMEOUT.value()||res==HttpStatus.INTERNAL_SERVER_ERROR.value()) {
317 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.SchedulerAccessConnectionError);
318 } else if (res == HttpStatus.PRECONDITION_FAILED.value() || res == HttpStatus.EXPECTATION_FAILED.value()) {
319 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.SchedulerInvalidAttributeError);
320 } else if (res != HttpStatus.ACCEPTED.value() && res != HttpStatus.OK.value()
321 && res != HttpStatus.NO_CONTENT.value()) {
322 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.SchedulerAccessGeneralError);
324 logger.error(EELFLoggerDelegate.errorLogger, "Other SchedulerErrors failed", res);