2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ===================================================================
10 * Unless otherwise specified, all software contained herein is licensed
11 * under the Apache License, Version 2.0 (the "License");
12 * you may not use this software except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * Unless otherwise specified, all documentation contained herein is licensed
24 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25 * you may not use this documentation except in compliance with the License.
26 * You may obtain a copy of the License at
28 * https://creativecommons.org/licenses/by/4.0/
30 * Unless required by applicable law or agreed to in writing, documentation
31 * distributed under the License is distributed on an "AS IS" BASIS,
32 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33 * See the License for the specific language governing permissions and
34 * limitations under the License.
36 * ============LICENSE_END============================================
41 package org.onap.portal.logging.logic;
43 import static com.att.eelf.configuration.Configuration.MDC_ALERT_SEVERITY;
45 import com.att.eelf.configuration.EELFLogger;
46 import com.att.eelf.configuration.EELFManager;
47 import java.text.MessageFormat;
48 import org.onap.portal.logging.format.EPAppMessagesEnum;
49 import org.onap.portalsdk.core.logging.format.AlarmSeverityEnum;
50 import org.onap.portalsdk.core.logging.format.ErrorSeverityEnum;
51 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
53 import org.springframework.http.HttpStatus;
55 public class EPLogUtil {
57 // This class has no logger of its own; it uses loggers passed to it.
58 private static EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
61 * Formats and writes a message to the error log with the class name and the
62 * specified parameters, using log level info, warn or error appropriate for
63 * the specified severity
66 * Logger for the class where the error occurred; the logger
67 * carries the class name.
68 * @param epMessageEnum
69 * Enum carrying alarm and error severity
71 * Values used to build the message.
73 public static void logEcompError(EELFLoggerDelegate classLogger, EPAppMessagesEnum epMessageEnum, String... param) {
74 logEcompError(classLogger, epMessageEnum, null, param);
78 * Formats and writes a message to the error log with the class name and the
79 * specified parameters, using log level info, warn or error appropriate for
80 * the specified severity
82 * @param epMessageEnum
83 * Enum carrying alarm and error severity
85 * Values used to build the message.
87 public static void logEcompError(EPAppMessagesEnum epMessageEnum, String... param) {
89 AlarmSeverityEnum alarmSeverityEnum = epMessageEnum.getAlarmSeverity();
90 ErrorSeverityEnum errorSeverityEnum = epMessageEnum.getErrorSeverity();
92 MDC.put(MDC_ALERT_SEVERITY, alarmSeverityEnum.severity());
93 MDC.put("ErrorCode", epMessageEnum.getErrorCode());
94 MDC.put("ErrorDescription", epMessageEnum.getErrorDescription());
95 MDC.put("ClassName", EPLogUtil.class.getName());
97 String resolution = EPLogUtil
98 .formatMessage(epMessageEnum.getDetails() + " " + epMessageEnum.getResolution(), (Object[]) param);
99 if (errorSeverityEnum == ErrorSeverityEnum.WARN) {
100 errorLogger.warn(resolution);
101 } else if (errorSeverityEnum == ErrorSeverityEnum.INFO) {
102 errorLogger.info(resolution);
104 errorLogger.error(resolution);
106 } catch (Exception e) {
107 errorLogger.error("logEcompError failed", e);
109 MDC.remove("ErrorCode");
110 MDC.remove("ErrorDescription");
111 MDC.remove("ClassName");
112 MDC.remove(MDC_ALERT_SEVERITY);
117 * Formats and writes a message to the error log with the class name,
118 * throwable and the specified parameters, using log level info, warn or
119 * error appropriate for the specified severity
122 * Logger for the class where the error occurred; the logger
123 * carries the class name.
124 * @param epMessageEnum
125 * Enum carrying alarm and error severity
127 * Throwable; ignored if null
129 * Array of Strings used to build the message.
131 @SuppressWarnings("static-access")
132 public static void logEcompError(EELFLoggerDelegate classLogger, EPAppMessagesEnum epMessageEnum, Throwable th,
135 AlarmSeverityEnum alarmSeverityEnum = epMessageEnum.getAlarmSeverity();
136 ErrorSeverityEnum errorSeverityEnum = epMessageEnum.getErrorSeverity();
138 MDC.put(MDC_ALERT_SEVERITY, alarmSeverityEnum.severity());
139 MDC.put("ErrorCode", epMessageEnum.getErrorCode());
140 MDC.put("ErrorDescription", epMessageEnum.getErrorDescription());
142 final String message = EPLogUtil.formatMessage(epMessageEnum.getDetails() + " " + epMessageEnum.getResolution(),
144 if (errorSeverityEnum == ErrorSeverityEnum.INFO) {
146 classLogger.info(classLogger.errorLogger, message);
148 classLogger.info(classLogger.errorLogger, message, th);
149 } else if (errorSeverityEnum == ErrorSeverityEnum.WARN) {
151 classLogger.warn(classLogger.errorLogger, message);
153 classLogger.warn(classLogger.errorLogger, message, th);
156 classLogger.error(classLogger.errorLogger, message);
158 classLogger.error(classLogger.errorLogger, message, th);
162 MDC.remove(MDC_ALERT_SEVERITY);
163 MDC.remove("ErrorCode");
164 MDC.remove("ErrorDescription");
168 * Builds a string using the format and parameters.
174 private static String formatMessage(String message, Object... args) {
175 StringBuilder sbFormattedMessage = new StringBuilder();
176 if (args != null && args.length > 0 && message != null && !message.equals("")) {
177 MessageFormat mf = new MessageFormat(message);
178 sbFormattedMessage.append(mf.format(args));
180 sbFormattedMessage.append(message);
182 return sbFormattedMessage.toString();
186 * Builds a comma-separated string of values to document a user action.
198 * @return Value suitable for writing to the audit log file.
200 public static String formatAuditLogMessage(String action, String activity, String userId, String affectedId,
202 StringBuilder auditLogMsg = new StringBuilder();
203 auditLogMsg.append("Click_A:[");
204 if (action != null && !action.equals("")) {
205 auditLogMsg.append(" Action: ");
206 auditLogMsg.append(action);
209 if (activity != null && !activity.equals("")) {
210 auditLogMsg.append(",Activity CD: ");
211 auditLogMsg.append(activity);
214 if (userId != null && !userId.equals("")) {
215 auditLogMsg.append(",User ID: ");
216 auditLogMsg.append(userId);
219 if (affectedId != null && !affectedId.equals("")) {
220 auditLogMsg.append(",Affected ID: ");
221 auditLogMsg.append(affectedId);
224 if (comment != null && !comment.equals("")) {
225 auditLogMsg.append(",Comment: ");
226 auditLogMsg.append(comment);
228 auditLogMsg.append("]");
229 return auditLogMsg.toString();
233 * Builds a comma-separated string of values to document a user browser
252 * @return String value suitable for writing to the audit log file.
254 public static String formatStoreAnalyticsAuditLogMessage(String orgUserId, String appName, String action,
255 String activity, String actionLink, String page, String function, String type) {
256 StringBuilder auditLogStoreAnalyticsMsg = new StringBuilder();
257 auditLogStoreAnalyticsMsg.append("Click_Analytics:[");
258 if (orgUserId != null && !orgUserId.equals("")) {
259 auditLogStoreAnalyticsMsg.append(" Organization User ID: ");
260 auditLogStoreAnalyticsMsg.append(orgUserId);
263 if (appName != null && !appName.equals("")) {
264 auditLogStoreAnalyticsMsg.append(",AppName: ");
265 auditLogStoreAnalyticsMsg.append(appName);
268 if (action != null && !action.equals("")) {
269 auditLogStoreAnalyticsMsg.append(",Action: ");
270 auditLogStoreAnalyticsMsg.append(action);
273 if (activity != null && !activity.equals("")) {
274 auditLogStoreAnalyticsMsg.append(",Activity: ");
275 auditLogStoreAnalyticsMsg.append(activity);
278 if (actionLink != null && !actionLink.equals("")) {
279 auditLogStoreAnalyticsMsg.append(",ActionLink: ");
280 auditLogStoreAnalyticsMsg.append(actionLink);
283 if (page != null && !page.equals("")) {
284 auditLogStoreAnalyticsMsg.append(",Page: ");
285 auditLogStoreAnalyticsMsg.append(page);
288 if (function != null && !function.equals("")) {
289 auditLogStoreAnalyticsMsg.append(",Function: ");
290 auditLogStoreAnalyticsMsg.append(function);
293 if (type != null && !type.equals("")) {
294 auditLogStoreAnalyticsMsg.append(",Type: ");
295 auditLogStoreAnalyticsMsg.append(type);
297 auditLogStoreAnalyticsMsg.append("]");
298 return auditLogStoreAnalyticsMsg.toString();
301 public static void logExternalAuthAccessAlarm(EELFLoggerDelegate logger, HttpStatus res) {
302 if (res.equals(HttpStatus.UNAUTHORIZED) || res.equals(HttpStatus.FORBIDDEN)) {
303 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.ExternalAuthAccessAuthenticationError);
304 } else if (res.equals(HttpStatus.NOT_FOUND) || res.equals(HttpStatus.NOT_ACCEPTABLE)
305 || res.equals(HttpStatus.CONFLICT) || res.equals(HttpStatus.BAD_REQUEST)) {
306 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.ExternalAuthAccessConnectionError);
307 } else if (!res.equals(HttpStatus.ACCEPTED) && !res.equals(HttpStatus.OK)) {
308 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.ExternalAuthAccessGeneralError);
312 public static void schedulerAccessAlarm(EELFLoggerDelegate logger, int res) {
313 if (res == HttpStatus.UNAUTHORIZED.value() || res == HttpStatus.FORBIDDEN.value()) {
314 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.SchedulerAuxAccessAuthenticationError);
315 } else if (res == HttpStatus.NOT_FOUND.value() || res == HttpStatus.NOT_ACCEPTABLE.value()
316 || res == HttpStatus.CONFLICT.value() || res == HttpStatus.BAD_REQUEST.value()
317 || res == HttpStatus.REQUEST_TIMEOUT.value()||res==HttpStatus.INTERNAL_SERVER_ERROR.value()) {
318 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.SchedulerAccessConnectionError);
319 } else if (res == HttpStatus.PRECONDITION_FAILED.value() || res == HttpStatus.EXPECTATION_FAILED.value()) {
320 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.SchedulerInvalidAttributeError);
321 } else if (res != HttpStatus.ACCEPTED.value() && res != HttpStatus.OK.value()
322 && res != HttpStatus.NO_CONTENT.value()) {
323 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.SchedulerAccessGeneralError);
325 logger.error(EELFLoggerDelegate.errorLogger, "Other SchedulerErrors failed", res);