Enhancements for the aai-common library
[aai/aai-common.git] / aai-els-onap-logging / src / main / java / org / onap / aai / aailog / logs / AaiDmaapMetricLog.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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.aai.aailog.logs;
22
23 import org.onap.aai.logging.AaiElsErrorCode;
24 import org.onap.logging.filter.base.MDCSetup;
25 import org.onap.logging.ref.slf4j.ONAPLogConstants;
26 import org.slf4j.*;
27
28 import java.time.ZoneOffset;
29 import java.time.ZonedDateTime;
30 import java.time.format.DateTimeFormatter;
31 import java.util.regex.PatternSyntaxException;
32
33 public class AaiDmaapMetricLog extends MDCSetup {
34
35     protected static final Logger logger = LoggerFactory.getLogger(AaiDmaapMetricLog.class);
36     private static final Marker INVOKE_RETURN = MarkerFactory.getMarker("INVOKE-RETURN");
37     private static final String TARGET_ENTITY = "DMaaP";
38
39     public AaiDmaapMetricLog() {
40     }
41     public void pre(String targetServiceName, String event, String transactionId, String serviceName) {
42
43         try {
44             MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP,
45                 ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
46             setLogTimestamp();
47             setElapsedTimeInvokeTimestamp();
48             MDC.put(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME, targetServiceName);
49             MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.INPROGRESS.toString());
50             MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, TARGET_ENTITY);
51             if (transactionId != null && !(transactionId.isEmpty())) {
52                 MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, transactionId);
53             }
54             if (serviceName != null && !(serviceName.isEmpty())) {
55                 MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, serviceName);
56             }
57             setInvocationIdFromMDC();
58             logger.info(ONAPLogConstants.Markers.INVOKE, event );
59
60         } catch (Exception e) {
61             logger.warn("Error in AaiDmaapMetricLog pre", e.getMessage());
62         }
63     }
64
65     public void post(String responseCode, String errorDescription) {
66
67         try {
68             setLogTimestamp();
69             setElapsedTimeInvokeTimestamp();
70             setResponseStatusCode(responseCode, errorDescription);
71             logger.info(INVOKE_RETURN, "InvokeReturn");
72             clearClientMDCs();
73         } catch (Exception e) {
74             logger.warn("Error in AaiDmaapMetricLog post", e.getMessage());
75         }
76     }
77
78     public void setResponseStatusCode(String aaiElsErrorCode, String errorDescription) {
79         String statusCode;
80         if (AaiElsErrorCode.SUCCESS.equals(aaiElsErrorCode)) {
81             statusCode = ONAPLogConstants.ResponseStatus.COMPLETE.toString();
82         }
83         else {
84             statusCode = ONAPLogConstants.ResponseStatus.ERROR.toString();
85             MDC.put(ONAPLogConstants.MDCs.ERROR_CODE, aaiElsErrorCode);
86             MDC.put(ONAPLogConstants.MDCs.ERROR_DESC, errorDescription);
87         }
88         MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, statusCode);
89         MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, aaiElsErrorCode);
90
91     }
92 }