nexus site path corrected
[portal.git] / ecomp-portal-BE / src / main / java / org / openecomp / portalapp / portal / logging / aop / EPEELFLoggerAspect.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.aop;
21
22 import org.aspectj.lang.ProceedingJoinPoint;
23 import org.aspectj.lang.annotation.Around;
24 import org.aspectj.lang.annotation.Aspect;
25 import org.aspectj.lang.annotation.Pointcut;
26 import org.openecomp.portalapp.portal.transport.FieldsValidator;
27 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
28 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
29 import org.openecomp.portalsdk.core.util.SystemProperties.SecurityEventTypeEnum;
30 import org.springframework.beans.factory.annotation.Autowired;
31
32 @Aspect
33 @org.springframework.context.annotation.Configuration
34 public class EPEELFLoggerAspect {
35         
36         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPEELFLoggerAspect.class);
37
38         @Autowired
39         EPEELFLoggerAdvice epAdvice;
40         
41         /*
42          * Point-cut expression to handle all INCOMING_REST_MESSAGES
43          */
44         @Pointcut("execution(public * org.openecomp.portalapp.portal.controller.*.*(..))")
45         public void incomingAuditMessages() {}
46         
47         /*
48          * Handles all INCOMING_REST_MESSAGES from kpiDashboard
49          */
50         @Pointcut("execution(public * org.openecomp.portalapp.kpidash.controller.*.*(..))")
51         public void kpiDashboardAuditMessages() {}
52
53         /*
54          * Point-cut expression to handle all session management INCOMING_REST_MESSAGES
55          */
56         @Pointcut("execution(public * org.openecomp.portalapp.controller.sessionmgt.*.*(..))")
57         public void sessionMgtIncomingAuditMessages() {}
58         
59         /*
60          * Point-cut expression to handle UserProfileController INCOMING_REST_MESSAGES
61          */
62         @Pointcut("execution(public * org.openecomp.portalapp.controller.UserProfileController.*(..))")
63         public void userProfileIncomingAuditMessages() {}
64         
65         /*
66          * Point-cut expression to handle UserProfileController INCOMING_REST_MESSAGES
67          */
68         @Pointcut("execution(public * org.openecomp.portalapp.controller.WelcomeController.*(..))")
69         public void welcomeIncomingAuditMessages() {}
70         
71         /*
72          * Point-cut expression to handle INCOING Logout Rest Messages
73          */
74         @Pointcut("execution(public * org.openecomp.portalapp.controller.ECOMPLogoutController.*(..))")
75         public void logoutAuditMessages() {}
76
77         
78         /*
79          * Point-cut expression which handles all the OUTGOING_REST_MESSAGES.
80          */
81         @Pointcut("execution(public * org.openecomp.portalapp.portal.service.ApplicationsRestClientServiceImpl.*(..))")
82         public void outgoingAuditMessages() {}
83         
84         /*
85          * Point-cut expression to handle all the session management OUTGOING_REST_MESSAGES.
86          */
87         @Pointcut("execution(public * org.openecomp.portalapp.service.sessionmgt.SessionCommunication.*(..))")
88         public void sessionMgtOutgoingAuditMessages() {}
89         
90         /*
91          * Point-cut expression which handles all the LDAP_PHONEBOOK_USER_SEARCH calls.
92          */
93         @Pointcut("execution(public * org.openecomp.portalapp.portal.service.EPLdapServiceImpl.*(..))")
94         public void phoneBookSearchAuditMessages() {}
95         
96         /*
97          * Handles Audit, Metrics & Debug logging for the point-cut
98          * expression defined at class-level
99          */
100         @Around("(incomingAuditMessages() || kpiDashboardAuditMessages() || sessionMgtIncomingAuditMessages() || "
101                         + "userProfileIncomingAuditMessages() || welcomeIncomingAuditMessages()) && @within(epAuditLog)")
102         public Object incomingAuditMessagesAroundClass(ProceedingJoinPoint joinPoint, EPAuditLog epAuditLog) throws Throwable {
103                 return this.logAroundMethod(joinPoint, SecurityEventTypeEnum.INCOMING_REST_MESSAGE);
104         }
105         
106         /*
107          * Handles Audit, Metrics & Debug logging for the point-cut
108          * expression defined at class-level
109          */
110         @Around("(outgoingAuditMessages() || sessionMgtOutgoingAuditMessages()) && @within(epAuditLog)")
111         public Object outgoingAuditMessagesAroundClass(ProceedingJoinPoint joinPoint, EPAuditLog epAuditLog) throws Throwable {
112                 return this.logAroundMethod(joinPoint, SecurityEventTypeEnum.OUTGOING_REST_MESSAGE);
113         }
114         
115         
116         /*
117          * Handles Audit, Metrics & Debug logging for the point-cut
118          * expression defined at method-level
119          */
120         @Around("(outgoingAuditMessages() || sessionMgtOutgoingAuditMessages()) && @annotation(epAuditLog)")
121         public Object outgoingAuditMessagesAroundMethod(ProceedingJoinPoint joinPoint, EPAuditLog epAuditLog) throws Throwable {
122                 return this.logAroundMethod(joinPoint, SecurityEventTypeEnum.OUTGOING_REST_MESSAGE);
123         }
124         
125         /*
126          * Handles Audit, Metrics & Debug logging for the point-cut
127          * expression defined at method-level
128          */
129         @Around("(incomingAuditMessages() || kpiDashboardAuditMessages() || sessionMgtIncomingAuditMessages() ||"
130                         + "userProfileIncomingAuditMessages() || welcomeIncomingAuditMessages()) && @annotation(epAuditLog)")
131         public Object incomingAuditMessagesAroundMethod(ProceedingJoinPoint joinPoint, EPAuditLog epAuditLog) throws Throwable {
132                 return this.logAroundMethod(joinPoint, SecurityEventTypeEnum.INCOMING_REST_MESSAGE);
133         }
134         
135         @Around("@annotation(epAuditLog)")
136         public Object loginAuditMessagesAroundMethod(ProceedingJoinPoint joinPoint, EPAuditLog epAuditLog) throws Throwable {
137                 return this.logAroundMethod(joinPoint, SecurityEventTypeEnum.FE_LOGIN_ATTEMPT);
138         }
139         
140         @Around("logoutAuditMessages() && @annotation(epAuditLog)")
141         public Object logoutAuditMessagesAroundMethod(ProceedingJoinPoint joinPoint, EPAuditLog epAuditLog) throws Throwable {
142                 return this.logAroundMethod(joinPoint, SecurityEventTypeEnum.FE_LOGOUT);
143         }
144         
145         @Around("phoneBookSearchAuditMessages() && @annotation(epAuditLog)")
146         public Object phonebookSearchAuditMessagesAroundMethod(ProceedingJoinPoint joinPoint, EPAuditLog epAuditLog) throws Throwable {
147                 return this.logAroundMethod(joinPoint, SecurityEventTypeEnum.LDAP_PHONEBOOK_USER_SEARCH);
148         }
149         
150         private Object logAroundMethod(ProceedingJoinPoint joinPoint, SecurityEventTypeEnum securityEventType) throws Throwable {
151                 //Before
152                 Object[] passOnArgs = new Object[] {joinPoint.getSignature().getDeclaringType().getName(), joinPoint.getSignature().getName()};
153                 Object[] returnArgs = epAdvice.before(securityEventType, joinPoint.getArgs(), passOnArgs);
154                 
155                 //Call the actual method
156                 Object result = null;
157                 String statusCode = "COMPLETE";
158                 String responseCode = "200";
159                 try {
160                         result = joinPoint.proceed();
161                 } catch(Exception e) {
162                         logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
163                         statusCode = "ERROR";
164                         responseCode = "500"; //Internal server error
165                 }
166                 
167                 //Check the result
168                 if (securityEventType!=null) {
169                         if (result==null) {
170                                 statusCode = "ERROR";
171                                 //Check if there is an internal response code
172                                 //and use it if the caller function has configured it.
173                                 responseCode = epAdvice.getInternalResponseCode();
174                                 if (responseCode==null||responseCode=="") {
175                                         responseCode = "500"; //Internal server error
176                                 }
177                         } else if (result instanceof FieldsValidator) {
178                                 FieldsValidator fieldsValidator = (FieldsValidator) result;
179                                 if (fieldsValidator!=null && fieldsValidator.httpStatusCode!=null) {
180                                         responseCode = fieldsValidator.httpStatusCode.toString();
181                                 }
182                         }
183                 }
184                 
185                 //After
186                 epAdvice.after(securityEventType, statusCode, responseCode, joinPoint.getArgs(), returnArgs, passOnArgs);
187                 
188                 return result;
189         }
190         
191         //Metrics Logging
192         @Pointcut("execution(* *(..))")
193     public void performMetricsLogging() {}
194         
195         @Around("performMetricsLogging() && @within(epMetricsLog)")
196         public Object metricsLoggingAroundClass(ProceedingJoinPoint joinPoint, EPMetricsLog epMetricsLog) throws Throwable {
197                 return this.logAroundMethod(joinPoint, null);
198         }
199         
200         @Around("performMetricsLogging() && @annotation(epMetricsLog)")
201         public Object metricsLoggingAroundMethod(ProceedingJoinPoint joinPoint, EPMetricsLog epMetricsLog) throws Throwable {
202                 return this.logAroundMethod(joinPoint, null);
203         }
204 }