3138d21a22ba4c7929f5bbde73d82e40c1927191
[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 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.portalsdk.core.util.SystemProperties.SecurityEventTypeEnum;
27 import org.springframework.beans.factory.annotation.Autowired;
28
29
30 @Aspect
31 @org.springframework.context.annotation.Configuration
32 public class EELFLoggerAspect {
33         
34         @Autowired
35         EELFLoggerAdvice advice;
36                         
37         /*
38          * Point-cut expression to handle all INCOMING_REST_MESSAGES
39          */
40         @Pointcut("execution(public * org.openecomp.portalsdk.core.controller.*.*(..))")
41         public void incomingAuditMessages() {}
42         
43         @Around("incomingAuditMessages() && @annotation(auditLog)")
44         public Object logAuditMethodAround(ProceedingJoinPoint joinPoint, AuditLog auditLog) throws Throwable {
45                 return this.logAroundMethod(joinPoint, SecurityEventTypeEnum.INCOMING_REST_MESSAGE);
46         }
47
48         @Around("incomingAuditMessages() && @within(auditLog)")
49         public Object logAuditMethodClassAround(ProceedingJoinPoint joinPoint, AuditLog auditLog) throws Throwable {
50                 return this.logAroundMethod(joinPoint, SecurityEventTypeEnum.INCOMING_REST_MESSAGE);
51         }
52         
53         /*
54          * Point cut expression to capture metrics logging
55          */
56         @Pointcut("execution(public * *(..))")
57     public void publicMethod() {}
58         
59         @Around("publicMethod() && @within(metricsLog)")
60         public Object logMetricsClassAround(ProceedingJoinPoint joinPoint, MetricsLog metricsLog) throws Throwable {
61                 return this.logAroundMethod(joinPoint, null);
62         }
63         
64         @Around("publicMethod() && @annotation(metricsLog)")
65         public Object logMetricsMethodAround(ProceedingJoinPoint joinPoint, MetricsLog metricsLog) throws Throwable {
66                 return this.logAroundMethod(joinPoint, null);
67         }
68         
69         private Object logAroundMethod(ProceedingJoinPoint joinPoint, SecurityEventTypeEnum securityEventType) throws Throwable {
70                 //Before
71                 Object[] passOnArgs = new Object[] {joinPoint.getSignature().getDeclaringType().getName(),joinPoint.getSignature().getName()};
72                 Object[] returnArgs = advice.before(securityEventType, joinPoint.getArgs(), passOnArgs);
73                 
74                 //Execute the actual method
75                 Object result = null;
76                 String restStatus = "COMPLETE";
77                 try {
78                         result = joinPoint.proceed();
79                 } catch(Exception e) {
80                         restStatus = "ERROR";
81                 }
82                 
83                 //After
84                 advice.after(securityEventType, restStatus, joinPoint.getArgs(), returnArgs, passOnArgs);
85                 
86                 return result;
87         }
88 }