Remove unused files with company keywords
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / logging / logic / EPLogUtil.java
1 /*-
2  * ================================================================================
3  * ECOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  * ================================================================================
19  */
20 package org.openecomp.portalapp.portal.logging.logic;
21
22 import static com.att.eelf.configuration.Configuration.MDC_ALERT_SEVERITY;
23
24 import java.text.MessageFormat;
25
26 import org.openecomp.portalapp.portal.logging.format.EPAppMessagesEnum;
27 import org.openecomp.portalsdk.core.logging.format.AlarmSeverityEnum;
28 import org.openecomp.portalsdk.core.logging.format.ErrorSeverityEnum;
29 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
30 import org.openecomp.portalsdk.core.web.support.UserUtils;
31 import org.slf4j.MDC;
32 import org.springframework.http.HttpStatus;
33
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
36
37 public class EPLogUtil {
38
39         // This class has no logger of its own; it uses loggers passed to it.
40         private static EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
41
42         /**
43          * Formats and writes a message to the error log with the class name and the
44          * specified parameters, using log level info, warn or error appropriate for
45          * the specified severity
46          * 
47          * @param classLogger
48          *            Logger for the class where the error occurred; the logger
49          *            carries the class name.
50          * @param epMessageEnum
51          *            Enum carrying alarm and error severity
52          * @param param
53          *            Values used to build the message.
54          */
55         public static void logEcompError(EELFLoggerDelegate classLogger, EPAppMessagesEnum epMessageEnum, String... param) {
56                 logEcompError(classLogger, epMessageEnum, null, param);
57         }
58
59         /**
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
63          * 
64          * @param classLogger
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
69          * @param param
70          *            Values used to build the message.
71          */
72         public static void logEcompError(EPAppMessagesEnum epMessageEnum, String... param) {
73                 try {
74                         AlarmSeverityEnum alarmSeverityEnum = epMessageEnum.getAlarmSeverity();
75                         ErrorSeverityEnum errorSeverityEnum = epMessageEnum.getErrorSeverity();
76
77                         MDC.put(MDC_ALERT_SEVERITY, alarmSeverityEnum.name());
78                         MDC.put("ErrorCode", epMessageEnum.getErrorCode());
79                         MDC.put("ErrorDescription", epMessageEnum.getErrorDescription());
80                         MDC.put("ClassName", EPLogUtil.class.getName());
81
82                         String resolution = EPLogUtil
83                                         .formatMessage(epMessageEnum.getDetails() + " " + epMessageEnum.getResolution(), (Object[]) param);
84                         if (errorSeverityEnum == ErrorSeverityEnum.WARN) {
85                                 errorLogger.warn(resolution);
86                         } else if (errorSeverityEnum == ErrorSeverityEnum.INFO) {
87                                 errorLogger.info(resolution);
88                         } else {
89                                 errorLogger.error(resolution);
90                         }
91                 } catch (Exception e) {
92                         errorLogger.error("Failed to log the error code. Details: " + UserUtils.getStackTrace(e));
93                 } finally {
94                         MDC.remove("ErrorCode");
95                         MDC.remove("ErrorDescription");
96                         MDC.remove("ClassName");
97                         MDC.remove(MDC_ALERT_SEVERITY);
98                 }
99         }
100
101         /**
102          * Formats and writes a message to the error log with the class name,
103          * throwable and the specified parameters, using log level info, warn or
104          * error appropriate for the specified severity
105          * 
106          * @param classLogger
107          *            Logger for the class where the error occurred; the logger
108          *            carries the class name.
109          * @param epMessageEnum
110          *            Enum carrying alarm and error severity
111          * @param th
112          *            Throwable; ignored if null
113          * @param param
114          *            Array of Strings used to build the message.
115          */
116         @SuppressWarnings("static-access")
117         public static void logEcompError(EELFLoggerDelegate classLogger, EPAppMessagesEnum epMessageEnum, Throwable th,
118                         String... param) {
119
120                 AlarmSeverityEnum alarmSeverityEnum = epMessageEnum.getAlarmSeverity();
121                 ErrorSeverityEnum errorSeverityEnum = epMessageEnum.getErrorSeverity();
122
123                 MDC.put(MDC_ALERT_SEVERITY, alarmSeverityEnum.name());
124                 MDC.put("ErrorCode", epMessageEnum.getErrorCode());
125                 MDC.put("ErrorDescription", epMessageEnum.getErrorDescription());
126
127                 final String message = EPLogUtil.formatMessage(epMessageEnum.getDetails() + " " + epMessageEnum.getResolution(),
128                                 (Object[]) param);
129                 if (errorSeverityEnum == ErrorSeverityEnum.INFO) {
130                         if (th == null)
131                                 classLogger.info(classLogger.errorLogger, message);
132                         else
133                                 classLogger.info(classLogger.errorLogger, message, th);
134                 } else if (errorSeverityEnum == ErrorSeverityEnum.WARN) {
135                         if (th == null)
136                                 classLogger.warn(classLogger.errorLogger, message);
137                         else
138                                 classLogger.warn(classLogger.errorLogger, message, th);
139                 } else {
140                         if (th == null)
141                                 classLogger.error(classLogger.errorLogger, message);
142                         else
143                                 classLogger.error(classLogger.errorLogger, message, th);
144                 }
145
146                 // Clean up
147                 MDC.remove(MDC_ALERT_SEVERITY);
148                 MDC.remove("ErrorCode");
149                 MDC.remove("ErrorDescription");
150         }
151
152         /**
153          * Builds a string using the format and parameters.
154          * 
155          * @param message
156          * @param args
157          * @return
158          */
159         private static String formatMessage(String message, Object... args) {
160                 StringBuilder sbFormattedMessage = new StringBuilder();
161                 if (args != null && args.length > 0 && message != null && message != "") {
162                         MessageFormat mf = new MessageFormat(message);
163                         sbFormattedMessage.append(mf.format(args));
164                 } else {
165                         sbFormattedMessage.append(message);
166                 }
167                 return sbFormattedMessage.toString();
168         }
169
170         /**
171          * Builds a comma-separated string of values to document a user action.
172          * 
173          * @param action
174          *            String
175          * @param activity
176          *            String
177          * @param userId
178          *            String
179          * @param affectedId
180          *            String
181          * @param comment
182          *            String
183          * @return Value suitable for writing to the audit log file.
184          */
185         public static String formatAuditLogMessage(String action, String activity, String userId, String affectedId,
186                         String comment) {
187                 StringBuilder auditLogMsg = new StringBuilder();
188                 auditLogMsg.append("Click_A:[");
189                 if (action != null && !action.equals("")) {
190                         auditLogMsg.append(" Action: ");
191                         auditLogMsg.append(action);
192                 }
193
194                 if (activity != null && !activity.equals("")) {
195                         auditLogMsg.append(",Activity CD: ");
196                         auditLogMsg.append(activity);
197                 }
198
199                 if (userId != null && !userId.equals("")) {
200                         auditLogMsg.append(",User ID: ");
201                         auditLogMsg.append(userId);
202                 }
203
204                 if (affectedId != null && !affectedId.equals("")) {
205                         auditLogMsg.append(",Affected ID: ");
206                         auditLogMsg.append(affectedId);
207                 }
208
209                 if (comment != null && !comment.equals("")) {
210                         auditLogMsg.append(",Comment: ");
211                         auditLogMsg.append(comment);
212                 }
213                 auditLogMsg.append("]");
214                 return auditLogMsg.toString();
215         }
216
217         /**
218          * Builds a comma-separated string of values to document a user browser
219          * action.
220          * 
221          * @param orgUserId
222          *            String
223          * @param appName
224          *            String
225          * @param action
226          *            String
227          * @param activity
228          *            String
229          * @param actionLink
230          *            String
231          * @param page
232          *            String
233          * @param function
234          *            String
235          * @param type
236          *            String
237          * @return String value suitable for writing to the audit log file.
238          */
239         public static String formatStoreAnalyticsAuditLogMessage(String orgUserId, String appName, String action,
240                         String activity, String actionLink, String page, String function, String type) {
241                 StringBuilder auditLogStoreAnalyticsMsg = new StringBuilder();
242                 auditLogStoreAnalyticsMsg.append("Click_Analytics:[");
243                 if (orgUserId != null && !orgUserId.equals("")) {
244                         auditLogStoreAnalyticsMsg.append(" Organization User ID: ");
245                         auditLogStoreAnalyticsMsg.append(orgUserId);
246                 }
247
248                 if (appName != null && !appName.equals("")) {
249                         auditLogStoreAnalyticsMsg.append(",AppName: ");
250                         auditLogStoreAnalyticsMsg.append(appName);
251                 }
252
253                 if (action != null && !action.equals("")) {
254                         auditLogStoreAnalyticsMsg.append(",Action: ");
255                         auditLogStoreAnalyticsMsg.append(action);
256                 }
257
258                 if (activity != null && !activity.equals("")) {
259                         auditLogStoreAnalyticsMsg.append(",Activity: ");
260                         auditLogStoreAnalyticsMsg.append(activity);
261                 }
262
263                 if (actionLink != null && !actionLink.equals("")) {
264                         auditLogStoreAnalyticsMsg.append(",ActionLink: ");
265                         auditLogStoreAnalyticsMsg.append(actionLink);
266                 }
267
268                 if (page != null && !page.equals("")) {
269                         auditLogStoreAnalyticsMsg.append(",Page: ");
270                         auditLogStoreAnalyticsMsg.append(page);
271                 }
272
273                 if (function != null && !function.equals("")) {
274                         auditLogStoreAnalyticsMsg.append(",Function: ");
275                         auditLogStoreAnalyticsMsg.append(function);
276                 }
277
278                 if (type != null && !type.equals("")) {
279                         auditLogStoreAnalyticsMsg.append(",Type: ");
280                         auditLogStoreAnalyticsMsg.append(type);
281                 }
282                 auditLogStoreAnalyticsMsg.append("]");
283                 return auditLogStoreAnalyticsMsg.toString();
284         }
285
286         public static void logExternalAuthAccessAlarm(EELFLoggerDelegate logger, HttpStatus res) {
287                 if (res.equals(HttpStatus.UNAUTHORIZED) || res.equals(HttpStatus.FORBIDDEN)) {
288                         EPLogUtil.logEcompError(logger, EPAppMessagesEnum.ExternalAuthAccessAuthenticationError);
289                 } else if (res.equals(HttpStatus.NOT_FOUND) || res.equals(HttpStatus.NOT_ACCEPTABLE)
290                                 || res.equals(HttpStatus.CONFLICT) || res.equals(HttpStatus.BAD_REQUEST)) {
291                         EPLogUtil.logEcompError(logger, EPAppMessagesEnum.ExternalAuthAccessConnectionError);
292                 } else if (!res.equals(HttpStatus.ACCEPTED) && !res.equals(HttpStatus.OK)) {
293                         EPLogUtil.logEcompError(logger, EPAppMessagesEnum.ExternalAuthAccessGeneralError);
294                 }
295         }
296
297 }