Added new annotation for ScheduledLogging
[logging-analytics.git] / reference / logging-filter / logging-filter-base / src / main / java / org / onap / logging / filter / base / AbstractMDCSetupAspect.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - Logging
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
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  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.logging.filter.base;
22
23 import java.util.UUID;
24
25 import org.aspectj.lang.ProceedingJoinPoint;
26 import org.onap.logging.ref.slf4j.ONAPLogConstants;
27 import org.slf4j.MDC;
28
29 public abstract class AbstractMDCSetupAspect extends MDCSetup {
30
31     public abstract void logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable;
32
33     protected void setupMDC(String methodName) {
34         try {
35             setEntryTimeStamp();
36             setServerFQDN();
37             String partnerName = getProperty(Constants.Property.PARTNER_NAME);
38             MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, UUID.randomUUID().toString());
39             MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.INPROGRESS.toString());
40             MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, UUID.randomUUID().toString());
41             MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, partnerName);
42             MDC.put(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME, Constants.DefaultValues.UNKNOWN);
43             MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, methodName);
44             setLogTimestamp();
45             setElapsedTime();
46             MDC.put(ONAPLogConstants.MDCs.PARTNER_NAME, partnerName);
47             logger.info(ONAPLogConstants.Markers.ENTRY, "Entering");
48         } catch (Exception e) {
49             logger.warn("Error in ScheduledTasksMDCSetup: {}", e.getMessage());
50         }
51     }
52
53     protected void exitAndClearMDC() {
54         try {
55             setStatusCode();
56             setLogTimestamp();
57             setElapsedTime();
58             logger.info(ONAPLogConstants.Markers.EXIT, "Exiting.");
59         } catch (Exception e) {
60             logger.warn("Error in ScheduledTasksMDCSetup clear MDC: {}", e.getMessage());
61         }
62         MDC.clear();
63     }
64
65     protected void setStatusCode() {
66         String currentStatusCode = MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE);
67         if (currentStatusCode == null || !currentStatusCode.equals(ONAPLogConstants.ResponseStatus.ERROR.toString())) {
68             MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.COMPLETE.toString());
69         }
70     }
71
72     public void errorMDCSetup(ErrorCode errorCode, String errorDescription) {
73         MDC.put(ONAPLogConstants.MDCs.ERROR_CODE, String.valueOf(errorCode.getValue()));
74         MDC.put(ONAPLogConstants.MDCs.ERROR_DESC, errorDescription);
75         MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.ERROR.toString());
76     }
77
78 }