Merge "CM SUBSCRIPTION: add new subscription for non existing xpath"
[cps.git] / cps-service / src / main / java / org / onap / cps / aop / CpsLoggingAspectService.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.aop;
22
23 import java.util.Arrays;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import lombok.SneakyThrows;
27 import lombok.extern.slf4j.Slf4j;
28 import org.aspectj.lang.ProceedingJoinPoint;
29 import org.aspectj.lang.annotation.Around;
30 import org.aspectj.lang.annotation.Aspect;
31 import org.aspectj.lang.reflect.MethodSignature;
32 import org.springframework.stereotype.Component;
33 import org.springframework.util.StopWatch;
34
35 @Aspect
36 @Component
37 @Slf4j
38 public class CpsLoggingAspectService {
39
40     private static final String CPS_PACKAGE_NAME = "org.onap.cps";
41     private static final String ALL_CPS_METHODS = "execution(* " + CPS_PACKAGE_NAME + "..*(..)))";
42     private static final String METHOD_RETURNING_SENSITIVE_DATA = "AuthPassword";
43     private static final String SENSITIVE_DATA_MASK = "***********";
44
45     /**
46      * Intercept methods to measure and log execution details when debug level logging enabled.
47      *
48      * @param proceedingJoinPoint exposes the proceed(..) method in order to support around advice.
49      * @return empty in case of void otherwise an object of return type
50      */
51     @Around(ALL_CPS_METHODS)
52     @SneakyThrows
53     public Object interceptMethodCall(final ProceedingJoinPoint proceedingJoinPoint) {
54         if (isSlf4JDebugEnabled()) {
55             final StopWatch stopWatch = new StopWatch();
56             stopWatch.start();
57             final Object returnValue = proceedingJoinPoint.proceed();
58             stopWatch.stop();
59             final MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
60
61             final Object logValue;
62             if (methodSignature.getName().contains(METHOD_RETURNING_SENSITIVE_DATA)) {
63                 logValue = SENSITIVE_DATA_MASK;
64             } else {
65                 logValue = returnValue;
66             }
67             logMethodCall(methodSignature, proceedingJoinPoint, stopWatch, logValue);
68             return returnValue;
69         }
70         return proceedingJoinPoint.proceed();
71     }
72
73     void logMethodCall(final MethodSignature methodSignature,
74                        final ProceedingJoinPoint proceedingJoinPoint,
75                        final StopWatch stopWatch,
76                        final Object logValue) {
77         log.debug("Execution time of : {}.{}() with argument[s] = {} having result = {} :: {} ms",
78                 methodSignature.getDeclaringType().getSimpleName(),
79                 methodSignature.getName(), Arrays.toString(proceedingJoinPoint.getArgs()), logValue,
80                 stopWatch.getTotalTimeMillis());
81     }
82
83     private static boolean isSlf4JDebugEnabled() {
84         return Logger.getLogger(CPS_PACKAGE_NAME).isLoggable(Level.FINE);
85     }
86
87 }