Transport DTO up
[portal.git] / portal-BE / src / main / java / org / onap / portal / logging / logic / EPLogUtil.java
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ===================================================================
9  *
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
14  *
15  *             http://www.apache.org/licenses/LICENSE-2.0
16  *
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.
22  *
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
27  *
28  *             https://creativecommons.org/licenses/by/4.0/
29  *
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.
35  *
36  * ============LICENSE_END============================================
37  *
38  *
39  */
40
41 package org.onap.portal.logging.logic;
42
43 import static com.att.eelf.configuration.Configuration.MDC_ALERT_SEVERITY;
44
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;
52 import org.slf4j.MDC;
53 import org.springframework.http.HttpStatus;
54
55 public class EPLogUtil {
56
57         // This class has no logger of its own; it uses loggers passed to it.
58         private static EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
59
60         /**
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
64          * 
65          * @param classLogger
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
70          * @param param
71          *            Values used to build the message.
72          */
73         public static void logEcompError(EELFLoggerDelegate classLogger, EPAppMessagesEnum epMessageEnum, String... param) {
74                 logEcompError(classLogger, epMessageEnum, null, param);
75         }
76
77         /**
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
81          * 
82          * @param epMessageEnum
83          *            Enum carrying alarm and error severity
84          * @param param
85          *            Values used to build the message.
86          */
87         public static void logEcompError(EPAppMessagesEnum epMessageEnum, String... param) {
88                 try {
89                         AlarmSeverityEnum alarmSeverityEnum = epMessageEnum.getAlarmSeverity();
90                         ErrorSeverityEnum errorSeverityEnum = epMessageEnum.getErrorSeverity();
91
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());
96
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);
103                         } else {
104                                 errorLogger.error(resolution);
105                         }
106                 } catch (Exception e) {
107                         errorLogger.error("logEcompError failed", e);
108                 } finally {
109                         MDC.remove("ErrorCode");
110                         MDC.remove("ErrorDescription");
111                         MDC.remove("ClassName");
112                         MDC.remove(MDC_ALERT_SEVERITY);
113                 }
114         }
115
116         /**
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
120          * 
121          * @param classLogger
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
126          * @param th
127          *            Throwable; ignored if null
128          * @param param
129          *            Array of Strings used to build the message.
130          */
131         @SuppressWarnings("static-access")
132         public static void logEcompError(EELFLoggerDelegate classLogger, EPAppMessagesEnum epMessageEnum, Throwable th,
133                         String... param) {
134
135                 AlarmSeverityEnum alarmSeverityEnum = epMessageEnum.getAlarmSeverity();
136                 ErrorSeverityEnum errorSeverityEnum = epMessageEnum.getErrorSeverity();
137
138                 MDC.put(MDC_ALERT_SEVERITY, alarmSeverityEnum.severity());
139                 MDC.put("ErrorCode", epMessageEnum.getErrorCode());
140                 MDC.put("ErrorDescription", epMessageEnum.getErrorDescription());
141
142                 final String message = EPLogUtil.formatMessage(epMessageEnum.getDetails() + " " + epMessageEnum.getResolution(),
143                                 (Object[]) param);
144                 if (errorSeverityEnum == ErrorSeverityEnum.INFO) {
145                         if (th == null)
146                                 classLogger.info(classLogger.errorLogger, message);
147                         else
148                                 classLogger.info(classLogger.errorLogger, message, th);
149                 } else if (errorSeverityEnum == ErrorSeverityEnum.WARN) {
150                         if (th == null)
151                                 classLogger.warn(classLogger.errorLogger, message);
152                         else
153                                 classLogger.warn(classLogger.errorLogger, message, th);
154                 } else {
155                         if (th == null)
156                                 classLogger.error(classLogger.errorLogger, message);
157                         else
158                                 classLogger.error(classLogger.errorLogger, message, th);
159                 }
160
161                 // Clean up
162                 MDC.remove(MDC_ALERT_SEVERITY);
163                 MDC.remove("ErrorCode");
164                 MDC.remove("ErrorDescription");
165         }
166
167         /**
168          * Builds a string using the format and parameters.
169          * 
170          * @param message
171          * @param args
172          * @return
173          */
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));
179                 } else {
180                         sbFormattedMessage.append(message);
181                 }
182                 return sbFormattedMessage.toString();
183         }
184
185         /**
186          * Builds a comma-separated string of values to document a user action.
187          * 
188          * @param action
189          *            String
190          * @param activity
191          *            String
192          * @param userId
193          *            String
194          * @param affectedId
195          *            String
196          * @param comment
197          *            String
198          * @return Value suitable for writing to the audit log file.
199          */
200         public static String formatAuditLogMessage(String action, String activity, String userId, String affectedId,
201                         String comment) {
202                 StringBuilder auditLogMsg = new StringBuilder();
203                 auditLogMsg.append("Click_A:[");
204                 if (action != null && !action.equals("")) {
205                         auditLogMsg.append(" Action: ");
206                         auditLogMsg.append(action);
207                 }
208
209                 if (activity != null && !activity.equals("")) {
210                         auditLogMsg.append(",Activity CD: ");
211                         auditLogMsg.append(activity);
212                 }
213
214                 if (userId != null && !userId.equals("")) {
215                         auditLogMsg.append(",User ID: ");
216                         auditLogMsg.append(userId);
217                 }
218
219                 if (affectedId != null && !affectedId.equals("")) {
220                         auditLogMsg.append(",Affected ID: ");
221                         auditLogMsg.append(affectedId);
222                 }
223
224                 if (comment != null && !comment.equals("")) {
225                         auditLogMsg.append(",Comment: ");
226                         auditLogMsg.append(comment);
227                 }
228                 auditLogMsg.append("]");
229                 return auditLogMsg.toString();
230         }
231
232         /**
233          * Builds a comma-separated string of values to document a user browser
234          * action.
235          * 
236          * @param orgUserId
237          *            String
238          * @param appName
239          *            String
240          * @param action
241          *            String
242          * @param activity
243          *            String
244          * @param actionLink
245          *            String
246          * @param page
247          *            String
248          * @param function
249          *            String
250          * @param type
251          *            String
252          * @return String value suitable for writing to the audit log file.
253          */
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);
261                 }
262
263                 if (appName != null && !appName.equals("")) {
264                         auditLogStoreAnalyticsMsg.append(",AppName: ");
265                         auditLogStoreAnalyticsMsg.append(appName);
266                 }
267
268                 if (action != null && !action.equals("")) {
269                         auditLogStoreAnalyticsMsg.append(",Action: ");
270                         auditLogStoreAnalyticsMsg.append(action);
271                 }
272
273                 if (activity != null && !activity.equals("")) {
274                         auditLogStoreAnalyticsMsg.append(",Activity: ");
275                         auditLogStoreAnalyticsMsg.append(activity);
276                 }
277
278                 if (actionLink != null && !actionLink.equals("")) {
279                         auditLogStoreAnalyticsMsg.append(",ActionLink: ");
280                         auditLogStoreAnalyticsMsg.append(actionLink);
281                 }
282
283                 if (page != null && !page.equals("")) {
284                         auditLogStoreAnalyticsMsg.append(",Page: ");
285                         auditLogStoreAnalyticsMsg.append(page);
286                 }
287
288                 if (function != null && !function.equals("")) {
289                         auditLogStoreAnalyticsMsg.append(",Function: ");
290                         auditLogStoreAnalyticsMsg.append(function);
291                 }
292
293                 if (type != null && !type.equals("")) {
294                         auditLogStoreAnalyticsMsg.append(",Type: ");
295                         auditLogStoreAnalyticsMsg.append(type);
296                 }
297                 auditLogStoreAnalyticsMsg.append("]");
298                 return auditLogStoreAnalyticsMsg.toString();
299         }
300
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);
309                 }
310         }
311
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);
324                 } else {
325                         logger.error(EELFLoggerDelegate.errorLogger, "Other SchedulerErrors failed", res);
326                 }
327         }
328
329 }