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