Merge "Fix minor sonar issues in SchemaGenerator4Hist"
[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         if(MDC.get(ONAPLogConstants.MDCs.SERVER_FQDN) == null) {
41                 setServerFQDN();
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         }
86         else {
87             statusCode = ONAPLogConstants.ResponseStatus.ERROR.toString();
88             MDC.put(ONAPLogConstants.MDCs.ERROR_CODE, aaiElsErrorCode);
89             MDC.put(ONAPLogConstants.MDCs.ERROR_DESC, errorDescription);
90         }
91         MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, statusCode);
92         MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, aaiElsErrorCode);
93
94     }
95 }