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