org.onap migration
[vid.git] / vid-app-common / src / main / java / org / onap / vid / logging / VidLoggerAspect.java
1 package org.onap.vid.logging;
2
3 import org.apache.commons.lang3.StringUtils;
4 import org.aspectj.lang.ProceedingJoinPoint;
5 import org.aspectj.lang.annotation.Around;
6 import org.aspectj.lang.annotation.Aspect;
7 import org.aspectj.lang.annotation.Pointcut;
8 import org.onap.portalsdk.core.logging.aspect.EELFLoggerAdvice;
9 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
10 import org.onap.portalsdk.core.util.SystemProperties;
11 import org.springframework.beans.factory.annotation.Autowired;
12
13 import java.net.InetAddress;
14 import java.net.UnknownHostException;
15
16 import static com.att.eelf.configuration.Configuration.MDC_SERVER_FQDN;
17
18
19 @Aspect
20 @org.springframework.context.annotation.Configuration
21 public class VidLoggerAspect {
22
23     private String canonicalHostName;
24     @Autowired
25     EELFLoggerAdvice advice;
26
27     public VidLoggerAspect() {
28         try {
29             final InetAddress localHost = InetAddress.getLocalHost();
30             canonicalHostName = localHost.getCanonicalHostName();
31         } catch (UnknownHostException e) {
32             // YOLO
33             canonicalHostName = null;
34         }
35     }
36
37     @Pointcut("execution(public * org.onap.vid.controller.*Controller.*(..))")
38     public void vidControllers() {}
39
40     @Around("vidControllers() && (" +
41             "  @within(org.onap.portalsdk.core.logging.aspect.AuditLog)" +
42             "  || @annotation(org.onap.portalsdk.core.logging.aspect.AuditLog)" +
43             "  || @annotation(org.springframework.web.bind.annotation.RequestMapping)" +
44             ")")
45     public Object logAuditMethodClassAround(ProceedingJoinPoint joinPoint) throws Throwable {
46         return logAroundMethod(joinPoint, SystemProperties.SecurityEventTypeEnum.INCOMING_REST_MESSAGE);
47     }
48
49     private Object logAroundMethod(ProceedingJoinPoint joinPoint, SystemProperties.SecurityEventTypeEnum securityEventType) throws Throwable {
50         //Before
51         Object[] passOnArgs = new Object[] {joinPoint.getSignature().getDeclaringType().getName(),joinPoint.getSignature().getName()};
52         Object[] returnArgs = advice.before(securityEventType, joinPoint.getArgs(), passOnArgs);
53
54         fixServerFqdnInMDC();
55
56         //Execute the actual method
57         Object result;
58         String restStatus = "COMPLETE";
59         try {
60             result = joinPoint.proceed();
61         } catch(Exception e) {
62             restStatus = "ERROR";
63             throw e;
64         } finally {
65             fixStatusCodeInMDC(restStatus);
66             advice.after(securityEventType, restStatus, joinPoint.getArgs(), returnArgs, passOnArgs);
67         }
68
69         return result;
70     }
71
72     // Set the status code into MDC *before* the metrics log is written by advice.after()
73     private void fixStatusCodeInMDC(String restStatus) {
74         EELFLoggerDelegate.mdcPut(SystemProperties.STATUS_CODE, restStatus);
75     }
76
77     // Override the non-canonical hostname set by EELFLoggerDelegate::setGlobalLoggingContext()
78     // that was invoked by advice.before() (and some other SDK cases)
79     private void fixServerFqdnInMDC() {
80         if (!StringUtils.isBlank(canonicalHostName)) {
81             EELFLoggerDelegate.mdcPut(MDC_SERVER_FQDN, canonicalHostName);
82         }
83     }
84
85 }