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