summaryrefslogtreecommitdiffstats
path: root/portal-BE/src/main/java/org/onap/portal/logging/logic/EPLogUtil.java
blob: 9a3070c264d6035e1aab4de8fe00d182538d223d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
 * ============LICENSE_START==========================================
 * ONAP Portal
 * ===================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ===================================================================
 * Modifications Copyright (c) 2019 Samsung
 * ===================================================================
 *
 * Unless otherwise specified, all software contained herein is licensed
 * under the Apache License, Version 2.0 (the "License");
 * you may not use this software except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Unless otherwise specified, all documentation contained herein is licensed
 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
 * you may not use this documentation except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             https://creativecommons.org/licenses/by/4.0/
 *
 * Unless required by applicable law or agreed to in writing, documentation
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * ============LICENSE_END============================================
 *
 *
 */

package org.onap.portal.logging.logic;

import static com.att.eelf.configuration.Configuration.MDC_ALERT_SEVERITY;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import java.text.MessageFormat;
import org.onap.portal.logging.format.EPAppMessagesEnum;
import org.onap.portalsdk.core.logging.format.AlarmSeverityEnum;
import org.onap.portalsdk.core.logging.format.ErrorSeverityEnum;
import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.slf4j.MDC;
import org.springframework.http.HttpStatus;

public class EPLogUtil {

	// This class has no logger of its own; it uses loggers passed to it.
	private static EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();

	/**
	 * Formats and writes a message to the error log with the class name and the
	 * specified parameters, using log level info, warn or error appropriate for
	 * the specified severity
	 * 
	 * @param classLogger
	 *            Logger for the class where the error occurred; the logger
	 *            carries the class name.
	 * @param epMessageEnum
	 *            Enum carrying alarm and error severity
	 * @param param
	 *            Values used to build the message.
	 */
	public static void logEcompError(EELFLoggerDelegate classLogger, EPAppMessagesEnum epMessageEnum, String... param) {
		logEcompError(classLogger, epMessageEnum, null, param);
	}

	/**
	 * Formats and writes a message to the error log with the class name and the
	 * specified parameters, using log level info, warn or error appropriate for
	 * the specified severity
	 * 
	 * @param epMessageEnum
	 *            Enum carrying alarm and error severity
	 * @param param
	 *            Values used to build the message.
	 */
	public static void logEcompError(EPAppMessagesEnum epMessageEnum, String... param) {
		try {
			AlarmSeverityEnum alarmSeverityEnum = epMessageEnum.getAlarmSeverity();
			ErrorSeverityEnum errorSeverityEnum = epMessageEnum.getErrorSeverity();

			MDC.put(MDC_ALERT_SEVERITY, alarmSeverityEnum.severity());
			MDC.put("ErrorCode", epMessageEnum.getErrorCode());
			MDC.put("ErrorDescription", epMessageEnum.getErrorDescription());
			MDC.put("ClassName", EPLogUtil.class.getName());

			String resolution = EPLogUtil
					.formatMessage(epMessageEnum.getDetails() + " " + epMessageEnum.getResolution(), (Object[]) param);
			if (errorSeverityEnum == ErrorSeverityEnum.WARN) {
				errorLogger.warn(resolution);
			} else if (errorSeverityEnum == ErrorSeverityEnum.INFO) {
				errorLogger.info(resolution);
			} else {
				errorLogger.error(resolution);
			}
		} catch (Exception e) {
			errorLogger.error("logEcompError failed", e);
		} finally {
			MDC.remove("ErrorCode");
			MDC.remove("ErrorDescription");
			MDC.remove("ClassName");
			MDC.remove(MDC_ALERT_SEVERITY);
		}
	}

	/**
	 * Formats and writes a message to the error log with the class name,
	 * throwable and the specified parameters, using log level info, warn or
	 * error appropriate for the specified severity
	 * 
	 * @param classLogger
	 *            Logger for the class where the error occurred; the logger
	 *            carries the class name.
	 * @param epMessageEnum
	 *            Enum carrying alarm and error severity
	 * @param th
	 *            Throwable; ignored if null
	 * @param param
	 *            Array of Strings used to build the message.
	 */
	@SuppressWarnings("static-access")
	public static void logEcompError(EELFLoggerDelegate classLogger, EPAppMessagesEnum epMessageEnum, Throwable th,
			String... param) {

		AlarmSeverityEnum alarmSeverityEnum = epMessageEnum.getAlarmSeverity();
		ErrorSeverityEnum errorSeverityEnum = epMessageEnum.getErrorSeverity();

		MDC.put(MDC_ALERT_SEVERITY, alarmSeverityEnum.severity());
		MDC.put("ErrorCode", epMessageEnum.getErrorCode());
		MDC.put("ErrorDescription", epMessageEnum.getErrorDescription());

		final String message = EPLogUtil.formatMessage(epMessageEnum.getDetails() + " " + epMessageEnum.getResolution(),
				(Object[]) param);
		if (errorSeverityEnum == ErrorSeverityEnum.INFO) {
			if (th == null)
				classLogger.info(classLogger.errorLogger, message);
			else
				classLogger.info(classLogger.errorLogger, message, th);
		} else if (errorSeverityEnum == ErrorSeverityEnum.WARN) {
			if (th == null)
				classLogger.warn(classLogger.errorLogger, message);
			else
				classLogger.warn(classLogger.errorLogger, message, th);
		} else {
			if (th == null)
				classLogger.error(classLogger.errorLogger, message);
			else
				classLogger.error(classLogger.errorLogger, message, th);
		}

		// Clean up
		MDC.remove(MDC_ALERT_SEVERITY);
		MDC.remove("ErrorCode");
		MDC.remove("ErrorDescription");
	}

	/**
	 * Builds a string using the format and parameters.
	 * 
	 * @param message
	 * @param args
	 * @return
	 */
	private static String formatMessage(String message, Object... args) {
		StringBuilder sbFormattedMessage = new StringBuilder();
		if (args != null && args.length > 0 && message != null && message != "") {
			MessageFormat mf = new MessageFormat(message);
			sbFormattedMessage.append(mf.format(args));
		} else {
			sbFormattedMessage.append(message);
		}
		return sbFormattedMessage.toString();
	}

	/**
	 * Builds a comma-separated string of values to document a user action.
	 * 
	 * @param action
	 *            String
	 * @param activity
	 *            String
	 * @param userId
	 *            String
	 * @param affectedId
	 *            String
	 * @param comment
	 *            String
	 * @return Value suitable for writing to the audit log file.
	 */
	public static String formatAuditLogMessage(String action, String activity, String userId, String affectedId,
			String comment) {
		StringBuilder auditLogMsg = new StringBuilder();
		auditLogMsg.append("Click_A:[");
		if (action != null && !action.equals("")) {
			auditLogMsg.append(" Action: ");
			auditLogMsg.append(action);
		}

		if (activity != null && !activity.equals("")) {
			auditLogMsg.append(",Activity CD: ");
			auditLogMsg.append(activity);
		}

		if (userId != null && !userId.equals("")) {
			auditLogMsg.append(",User ID: ");
			auditLogMsg.append(userId);
		}

		if (affectedId != null && !affectedId.equals("")) {
			auditLogMsg.append(",Affected ID: ");
			auditLogMsg.append(affectedId);
		}

		if (comment != null && !comment.equals("")) {
			auditLogMsg.append(",Comment: ");
			auditLogMsg.append(comment);
		}
		auditLogMsg.append("]");
		return auditLogMsg.toString();
	}

	/**
	 * Builds a comma-separated string of values to document a user browser
	 * action.
	 * 
	 * @param orgUserId
	 *            String
	 * @param appName
	 *            String
	 * @param action
	 *            String
	 * @param activity
	 *            String
	 * @param actionLink
	 *            String
	 * @param page
	 *            String
	 * @param function
	 *            String
	 * @param type
	 *            String
	 * @return String value suitable for writing to the audit log file.
	 */
	public static String formatStoreAnalyticsAuditLogMessage(String orgUserId, String appName, String action,
			String activity, String actionLink, String page, String function, String type) {
		StringBuilder auditLogStoreAnalyticsMsg = new StringBuilder();
		auditLogStoreAnalyticsMsg.append("Click_Analytics:[");
		if (orgUserId != null && !orgUserId.equals("")) {
			auditLogStoreAnalyticsMsg.append(" Organization User ID: ");
			auditLogStoreAnalyticsMsg.append(orgUserId);
		}

		if (appName != null && !appName.equals("")) {
			auditLogStoreAnalyticsMsg.append(",AppName: ");
			auditLogStoreAnalyticsMsg.append(appName);
		}

		if (action != null && !action.equals("")) {
			auditLogStoreAnalyticsMsg.append(",Action: ");
			auditLogStoreAnalyticsMsg.append(action);
		}

		if (activity != null && !activity.equals("")) {
			auditLogStoreAnalyticsMsg.append(",Activity: ");
			auditLogStoreAnalyticsMsg.append(activity);
		}

		if (actionLink != null && !actionLink.equals("")) {
			auditLogStoreAnalyticsMsg.append(",ActionLink: ");
			auditLogStoreAnalyticsMsg.append(actionLink);
		}

		if (page != null && !page.equals("")) {
			auditLogStoreAnalyticsMsg.append(",Page: ");
			auditLogStoreAnalyticsMsg.append(page);
		}

		if (function != null && !function.equals("")) {
			auditLogStoreAnalyticsMsg.append(",Function: ");
			auditLogStoreAnalyticsMsg.append(function);
		}

		if (type != null && !type.equals("")) {
			auditLogStoreAnalyticsMsg.append(",Type: ");
			auditLogStoreAnalyticsMsg.append(type);
		}
		auditLogStoreAnalyticsMsg.append("]");
		return auditLogStoreAnalyticsMsg.toString();
	}

	public static void logExternalAuthAccessAlarm(EELFLoggerDelegate logger, HttpStatus res) {
		if (res.equals(HttpStatus.UNAUTHORIZED) || res.equals(HttpStatus.FORBIDDEN)) {
			EPLogUtil.logEcompError(logger, EPAppMessagesEnum.ExternalAuthAccessAuthenticationError);
		} else if (res.equals(HttpStatus.NOT_FOUND) || res.equals(HttpStatus.NOT_ACCEPTABLE)
				|| res.equals(HttpStatus.CONFLICT) || res.equals(HttpStatus.BAD_REQUEST)) {
			EPLogUtil.logEcompError(logger, EPAppMessagesEnum.ExternalAuthAccessConnectionError);
		} else if (!res.equals(HttpStatus.ACCEPTED) && !res.equals(HttpStatus.OK)) {
			EPLogUtil.logEcompError(logger, EPAppMessagesEnum.ExternalAuthAccessGeneralError);
		}
	}

	public static void schedulerAccessAlarm(EELFLoggerDelegate logger, int res) {
		if (res == HttpStatus.UNAUTHORIZED.value() || res == HttpStatus.FORBIDDEN.value()) {
			EPLogUtil.logEcompError(logger, EPAppMessagesEnum.SchedulerAuxAccessAuthenticationError);
		} else if (res == HttpStatus.NOT_FOUND.value() || res == HttpStatus.NOT_ACCEPTABLE.value()
				|| res == HttpStatus.CONFLICT.value() || res == HttpStatus.BAD_REQUEST.value()
				|| res == HttpStatus.REQUEST_TIMEOUT.value()||res==HttpStatus.INTERNAL_SERVER_ERROR.value()) {
			EPLogUtil.logEcompError(logger, EPAppMessagesEnum.SchedulerAccessConnectionError);
		} else if (res == HttpStatus.PRECONDITION_FAILED.value() || res == HttpStatus.EXPECTATION_FAILED.value()) {
			EPLogUtil.logEcompError(logger, EPAppMessagesEnum.SchedulerInvalidAttributeError);
		} else if (res != HttpStatus.ACCEPTED.value() && res != HttpStatus.OK.value()
				&& res != HttpStatus.NO_CONTENT.value()) {
			EPLogUtil.logEcompError(logger, EPAppMessagesEnum.SchedulerAccessGeneralError);
		} else {
			logger.error(EELFLoggerDelegate.errorLogger, "Other SchedulerErrors failed", res);
		}
	}

}