1 package org.onap.vid.logging;
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;
13 import java.net.InetAddress;
14 import java.net.UnknownHostException;
16 import static com.att.eelf.configuration.Configuration.MDC_SERVER_FQDN;
20 @org.springframework.context.annotation.Configuration
21 public class VidLoggerAspect {
23 private String canonicalHostName;
25 EELFLoggerAdvice advice;
27 public VidLoggerAspect() {
29 final InetAddress localHost = InetAddress.getLocalHost();
30 canonicalHostName = localHost.getCanonicalHostName();
31 } catch (UnknownHostException e) {
33 canonicalHostName = null;
37 @Pointcut("execution(public * org.onap.vid.controller.*Controller.*(..))")
38 public void vidControllers() {}
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)" +
45 public Object logAuditMethodClassAround(ProceedingJoinPoint joinPoint) throws Throwable {
46 return logAroundMethod(joinPoint, SystemProperties.SecurityEventTypeEnum.INCOMING_REST_MESSAGE);
49 private Object logAroundMethod(ProceedingJoinPoint joinPoint, SystemProperties.SecurityEventTypeEnum securityEventType) throws Throwable {
51 Object[] passOnArgs = new Object[] {joinPoint.getSignature().getDeclaringType().getName(),joinPoint.getSignature().getName()};
52 Object[] returnArgs = advice.before(securityEventType, joinPoint.getArgs(), passOnArgs);
56 //Execute the actual method
58 String restStatus = "COMPLETE";
60 result = joinPoint.proceed();
61 } catch(Exception e) {
65 fixStatusCodeInMDC(restStatus);
66 advice.after(securityEventType, restStatus, joinPoint.getArgs(), returnArgs, passOnArgs);
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);
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);