da4e2aff3e45a40d7a4f057dedf98cc49a04531e
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * eCOMP Portal SDK
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.portalsdk.core.logging.aspect;
21
22 import java.text.SimpleDateFormat;
23 import java.util.Date;
24
25 import javax.servlet.http.HttpServletRequest;
26
27 import org.openecomp.portalsdk.core.logging.format.AuditLogFormatter;
28 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
29 import org.openecomp.portalsdk.core.service.AppService;
30 import org.openecomp.portalsdk.core.util.SystemProperties;
31 import org.openecomp.portalsdk.core.util.SystemProperties.SecurityEventTypeEnum;
32 import org.slf4j.MDC;
33 import org.springframework.beans.factory.annotation.Autowired;
34
35 import com.att.eelf.configuration.Configuration;
36
37 @org.springframework.context.annotation.Configuration
38 public class EELFLoggerAdvice {
39
40         @Autowired
41         AppService appService;
42
43         EELFLoggerDelegate adviceLogger = EELFLoggerDelegate.getLogger(EELFLoggerAdvice.class);
44
45         // DateTime Format according to the ECOMP Application Logging Guidelines.
46         private static final SimpleDateFormat ecompLogDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
47
48         /**
49          * Gets the current date and time in expected ECOMP log format.
50          * 
51          * @return Current date and time
52          */
53         public static String getCurrentDateTimeUTC() {
54                 String currentDateTime = ecompLogDateFormat.format(new Date());
55                 return currentDateTime;
56         }
57
58         /**
59          * 
60          * @param securityEventType
61          * @param args
62          * @param passOnArgs
63          * @return One-element array containing an empty String object.
64          */
65         public Object[] before(SecurityEventTypeEnum securityEventType, Object[] args, Object[] passOnArgs) {
66                 try {
67                         String className = "";
68                         if (passOnArgs[0] != null) {
69                                 className = passOnArgs[0].toString();
70                         }
71
72                         String methodName = "";
73                         if (passOnArgs[1] != null) {
74                                 methodName = passOnArgs[1].toString();
75                         }
76
77                         String appName = appService.getDefaultAppName();
78                         if (appName == null || appName == "") {
79                                 appName = SystemProperties.SDK_NAME;
80                         }
81
82                         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(className);
83
84                         // Initialize Request defaults only for controller methods.
85                         MDC.put(className + methodName + SystemProperties.METRICSLOG_BEGIN_TIMESTAMP, getCurrentDateTimeUTC());
86                         MDC.put(SystemProperties.TARGET_ENTITY, appName + "_BE");
87                         MDC.put(SystemProperties.TARGET_SERVICE_NAME, methodName);
88                         if (securityEventType != null) {
89                                 MDC.put(className + methodName + SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, getCurrentDateTimeUTC());
90                                 HttpServletRequest req = null;
91                                 if (args[0] != null && args[0] instanceof HttpServletRequest) {
92                                         req = (HttpServletRequest) args[0];
93                                         logger.setRequestBasedDefaultsIntoGlobalLoggingContext(req, appName);
94                                 }
95                         }
96                         logger.debug(EELFLoggerDelegate.debugLogger, (methodName + " was invoked."));
97                 } catch (Exception e) {
98                         adviceLogger.error(EELFLoggerDelegate.errorLogger,
99                                         "Exception occurred in EELFLoggerAdvice.before() method. Details: " + e.getMessage());
100                 }
101
102                 return new Object[] { "" };
103         }
104
105         /**
106          * 
107          * @param securityEventType
108          * @param result
109          * @param args
110          * @param returnArgs
111          * @param passOnArgs
112          */
113         public void after(SecurityEventTypeEnum securityEventType, String result, Object[] args, Object[] returnArgs,
114                         Object[] passOnArgs) {
115                 try {
116                         String className = "";
117                         if (passOnArgs[0] != null) {
118                                 className = passOnArgs[0].toString();
119                         }
120
121                         String methodName = "";
122                         if (passOnArgs[1] != null) {
123                                 methodName = passOnArgs[1].toString();
124                         }
125
126                         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(className);
127
128                         String appName = appService.getDefaultAppName();
129                         if (appName == null || appName == "") {
130                                 appName = SystemProperties.SDK_NAME;
131                         }
132
133                         if (MDC.get(SystemProperties.TARGET_SERVICE_NAME) == null
134                                         || MDC.get(SystemProperties.TARGET_SERVICE_NAME) == "") {
135                                 MDC.put(SystemProperties.TARGET_SERVICE_NAME, methodName);
136                         }
137
138                         if (MDC.get(SystemProperties.TARGET_ENTITY) == null || MDC.get(SystemProperties.TARGET_ENTITY) == "") {
139                                 MDC.put(SystemProperties.TARGET_ENTITY, appName + "_BE");
140                         }
141
142                         MDC.put(SystemProperties.METRICSLOG_BEGIN_TIMESTAMP,
143                                         MDC.get(className + methodName + SystemProperties.METRICSLOG_BEGIN_TIMESTAMP));
144                         MDC.put(SystemProperties.METRICSLOG_END_TIMESTAMP, getCurrentDateTimeUTC());
145                         this.calculateDateTimeDifference(MDC.get(SystemProperties.METRICSLOG_BEGIN_TIMESTAMP),
146                                         MDC.get(SystemProperties.METRICSLOG_END_TIMESTAMP));
147
148                         logger.info(EELFLoggerDelegate.metricsLogger, methodName + " operation is completed.");
149                         logger.debug(EELFLoggerDelegate.debugLogger, "Finished executing " + methodName + ".");
150
151                         if (securityEventType != null) {
152
153                                 MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP,
154                                                 MDC.get(className + methodName + SystemProperties.AUDITLOG_BEGIN_TIMESTAMP));
155                                 MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, getCurrentDateTimeUTC());
156                                 this.calculateDateTimeDifference(MDC.get(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP),
157                                                 MDC.get(SystemProperties.AUDITLOG_END_TIMESTAMP));
158
159                                 this.logSecurityMessage(logger, securityEventType, result, methodName);
160
161                                 // clear when finishes audit logging
162                                 MDC.remove(Configuration.MDC_KEY_REQUEST_ID);
163                                 MDC.remove(SystemProperties.PARTNER_NAME);
164                                 MDC.remove(SystemProperties.MDC_LOGIN_ID);
165                                 MDC.remove(SystemProperties.PROTOCOL);
166                                 MDC.remove(SystemProperties.FULL_URL);
167                                 MDC.remove(Configuration.MDC_SERVICE_NAME);
168                                 MDC.remove(SystemProperties.RESPONSE_CODE);
169                                 MDC.remove(SystemProperties.STATUS_CODE);
170                                 MDC.remove(className + methodName + SystemProperties.AUDITLOG_BEGIN_TIMESTAMP);
171                                 MDC.remove(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP);
172                                 MDC.remove(SystemProperties.AUDITLOG_END_TIMESTAMP);
173                         }
174
175                         MDC.remove(className + methodName + SystemProperties.METRICSLOG_BEGIN_TIMESTAMP);
176                         MDC.remove(SystemProperties.METRICSLOG_BEGIN_TIMESTAMP);
177                         MDC.remove(SystemProperties.METRICSLOG_END_TIMESTAMP);
178                         MDC.remove(SystemProperties.MDC_TIMER);
179                         MDC.remove(SystemProperties.TARGET_ENTITY);
180                         MDC.remove(SystemProperties.TARGET_SERVICE_NAME);
181                 } catch (Exception e) {
182                         adviceLogger.error(EELFLoggerDelegate.errorLogger,
183                                         "Exception occurred in EELFLoggerAdvice.after() method. Details: " + e.getMessage());
184                 }
185         }
186
187         /**
188          * 
189          * @param logger
190          * @param securityEventType
191          * @param result
192          * @param restMethod
193          */
194         private void logSecurityMessage(EELFLoggerDelegate logger, SecurityEventTypeEnum securityEventType, String result,
195                         String restMethod) {
196                 StringBuilder additionalInfoAppender = new StringBuilder();
197                 String auditMessage = "";
198
199                 additionalInfoAppender.append(String.format("%s request was received.", restMethod));
200
201                 // Status code
202                 MDC.put(SystemProperties.STATUS_CODE, result);
203
204                 String fullURL = MDC.get(SystemProperties.FULL_URL);
205                 if (fullURL != null && fullURL != "") {
206                         additionalInfoAppender.append(" Request-URL:" + MDC.get(SystemProperties.FULL_URL));
207                 }
208
209                 auditMessage = AuditLogFormatter.getInstance().createMessage(MDC.get(SystemProperties.PROTOCOL),
210                                 securityEventType.name(), MDC.get(SystemProperties.MDC_LOGIN_ID), additionalInfoAppender.toString());
211
212                 logger.info(EELFLoggerDelegate.auditLogger, auditMessage);
213         }
214
215         /**
216          * 
217          * @param beginDateTime
218          * @param endDateTime
219          */
220         private void calculateDateTimeDifference(String beginDateTime, String endDateTime) {
221                 if (beginDateTime != null && endDateTime != null) {
222                         try {
223                                 Date beginDate = ecompLogDateFormat.parse(beginDateTime);
224                                 Date endDate = ecompLogDateFormat.parse(endDateTime);
225                                 String timeDifference = String.format("%d ms", endDate.getTime() - beginDate.getTime());
226                                 MDC.put(SystemProperties.MDC_TIMER, timeDifference);
227                         } catch (Exception e) {
228                                 adviceLogger.error(EELFLoggerDelegate.errorLogger,
229                                                 "Exception occurred in EELFLoggerAdvice.calculateDateTimeDifference() method. Details: "
230                                                                 + e.getMessage());
231                         }
232                 }
233         }
234 }