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