Fix request handler class error
[appc.git] / appc-common / src / main / java / org / onap / appc / logging / LoggingUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.logging;
25
26 import org.onap.appc.i18n.Msg;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import com.att.eelf.i18n.EELFResolvableErrorEnum;
30 import com.att.eelf.i18n.EELFResourceManager;
31 import org.slf4j.MDC;
32 import static com.att.eelf.configuration.Configuration.MDC_KEY_REQUEST_ID;
33 import static com.att.eelf.configuration.Configuration.MDC_SERVICE_NAME;
34 import java.text.DateFormat;
35 import java.text.SimpleDateFormat;
36 import java.time.Instant;
37 import java.time.temporal.ChronoUnit;
38 import java.util.Date;
39 import java.util.TimeZone;
40 import java.util.UUID;
41
42 /**
43  * Logging utilities
44  */
45 public class LoggingUtils {
46
47     private static final EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
48     private static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
49     private static final EELFLogger metricLogger = EELFManager.getInstance().getMetricsLogger();
50
51     private LoggingUtils() {
52         throw new IllegalAccessError("LoggingUtils");
53     }
54
55     public static void logErrorMessage(String errorCode, String errorDescription,
56             String targetEntity, String targetServiceName, String additionalMessage,
57             String className) {
58         logError(errorCode, errorDescription, targetEntity, targetServiceName, additionalMessage,
59                 className);
60     }
61
62     public static void logErrorMessage(String targetEntity, String targetServiceName,
63             String additionalMessage, String className) {
64         logError("", "", targetEntity, targetServiceName, additionalMessage, className);
65     }
66
67     public static void logErrorMessage(String targetServiceName, String additionalMessage,
68             String className) {
69         logError("", "", LoggingConstants.TargetNames.APPC, targetServiceName, additionalMessage,
70                 className);
71     }
72
73     private static void logError(String errorCode, String errorDescription, String targetEntity,
74             String targetServiceName, String additionalMessage, String className) {
75         populateErrorLogContext(errorCode, errorDescription, targetEntity, targetServiceName,
76                 className);
77         errorLogger.error(additionalMessage == null ? "" : additionalMessage);
78         cleanErrorLogContext();
79     }
80
81     public static void logAuditMessage(Instant beginTimeStamp, Instant endTimeStamp, String code,
82             String responseDescription, String className) {
83         populateAuditLogContext(beginTimeStamp, endTimeStamp, code, responseDescription, className);
84         auditLogger.info(EELFResourceManager.format(Msg.APPC_AUDIT_MSG, MDC.get(MDC_SERVICE_NAME),
85                 MDC.get(LoggingConstants.MDCKeys.TARGET_VIRTUAL_ENTITY),
86                 MDC.get(LoggingConstants.MDCKeys.PARTNER_NAME), MDC.get(MDC_KEY_REQUEST_ID),
87                 MDC.get(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP),
88                 MDC.get(LoggingConstants.MDCKeys.END_TIMESTAMP),
89                 MDC.get(LoggingConstants.MDCKeys.RESPONSE_CODE)));
90         cleanAuditErrorContext();
91     }
92
93     public static void auditInfo(Instant beginTimeStamp, Instant endTimeStamp, String code,
94             String responseDescription, String className, EELFResolvableErrorEnum resourceId,
95             String... arguments) {
96         populateAuditLogContext(beginTimeStamp, endTimeStamp, code, responseDescription, className);
97         auditLogger.info(resourceId, arguments);
98         cleanAuditErrorContext();
99     }
100
101     public static void auditWarn(Instant beginTimeStamp, Instant endTimeStamp, String code,
102             String responseDescription, String className, EELFResolvableErrorEnum resourceId,
103             String... arguments) {
104         populateAuditLogContext(beginTimeStamp, endTimeStamp, code, responseDescription, className);
105         auditLogger.warn(resourceId, arguments);
106         cleanAuditErrorContext();
107     }
108
109     public static void logMetricsMessage(Instant beginTimeStamp, Instant endTimeStamp,
110             String targetEntity, String targetServiceName, String statusCode, String responseCode,
111             String responseDescription, String className) {
112         populateMetricLogContext(beginTimeStamp, endTimeStamp, targetEntity, targetServiceName,
113                 statusCode, responseCode, responseDescription, className);
114         metricLogger.info(EELFResourceManager.format(Msg.APPC_METRIC_MSG, MDC.get(MDC_SERVICE_NAME),
115                 MDC.get(LoggingConstants.MDCKeys.TARGET_VIRTUAL_ENTITY),
116                 MDC.get(LoggingConstants.MDCKeys.PARTNER_NAME), MDC.get(MDC_KEY_REQUEST_ID),
117                 MDC.get(LoggingConstants.MDCKeys.TARGET_ENTITY),
118                 MDC.get(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME),
119                 MDC.get(LoggingConstants.MDCKeys.ELAPSED_TIME),
120                 MDC.get(LoggingConstants.MDCKeys.STATUS_CODE)));
121         cleanMetricContext();
122     }
123
124     private static void populateAuditLogContext(Instant beginTimeStamp, Instant endTimeStamp,
125             String code, String responseDescription, String className) {
126         populateTimeContext(beginTimeStamp, endTimeStamp);
127         populateRequestContext();
128         String statusCode = ("100".equals(code) || "400".equals(code))
129                 ? LoggingConstants.StatusCodes.COMPLETE : LoggingConstants.StatusCodes.ERROR;
130         populateResponseContext(statusCode, code, responseDescription );
131         MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className != null ? className : "");
132     }
133
134     private static void cleanAuditErrorContext() {
135         cleanRequestContext();
136         cleanTimeContext();
137         cleanResponseContext();
138         MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
139     }
140
141     private static void populateErrorLogContext(String errorCode, String errorDescription,
142             String targetEntity, String targetServiceName, String className) {
143         populateErrorContext(errorCode, errorDescription);
144         populateTargetContext(targetEntity, targetServiceName != null ? targetServiceName : "");
145         MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className != null ? className : "");
146     }
147
148     private static void cleanErrorLogContext() {
149         cleanErrorContext();
150         cleanTargetContext();
151         MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
152     }
153
154     private static void populateMetricLogContext(Instant beginTimeStamp, Instant endTimeStamp,
155             String targetEntity, String targetServiceName, String statusCode, String responseCode,
156             String responseDescription, String className) {
157         populateRequestContext();
158         populateTimeContext(beginTimeStamp, endTimeStamp);
159         populateTargetContext(targetEntity, targetServiceName);
160         populateResponseContext(statusCode, responseCode, responseDescription);
161         MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className != null ? className : "");
162     }
163
164     private static void cleanMetricContext() {
165         cleanRequestContext();
166         cleanTimeContext();
167         cleanTargetContext();
168         cleanResponseContext();
169         MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
170     }
171
172     private static void populateTargetContext(String targetEntity, String targetServiceName) {
173         MDC.put(LoggingConstants.MDCKeys.TARGET_ENTITY, targetEntity != null ? targetEntity : "");
174         MDC.put(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME,
175                 targetServiceName != null ? targetServiceName : "");
176     }
177
178     private static void cleanTargetContext() {
179         MDC.remove(LoggingConstants.MDCKeys.TARGET_ENTITY);
180         MDC.remove(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME);
181     }
182
183     private static void populateRequestContext() {
184         try {
185             UUID.fromString(MDC.get(MDC_KEY_REQUEST_ID));
186             //reaching here without exception means existing RequestId is
187             //valid UUID as per ECOMP logging standards, no-op
188         } catch (Exception e) {
189             MDC.put(MDC_KEY_REQUEST_ID, UUID.randomUUID().toString());
190         }
191
192         try {
193             String partnerName = MDC.get(LoggingConstants.MDCKeys.PARTNER_NAME);
194
195             //ECOMP logging standards require some value for PartnerName.  Default to appc if empty
196             if (partnerName.isEmpty())
197                 MDC.put(LoggingConstants.MDCKeys.PARTNER_NAME, "appc");
198         } catch (Exception e) {
199             MDC.put(LoggingConstants.MDCKeys.PARTNER_NAME, "appc");
200         }
201
202         try {
203             String serviceName = MDC.get(MDC_SERVICE_NAME);
204
205             //ECOMP logging standards require some value for ServiceName.  Default to DEFAULT if empty
206             if (serviceName.isEmpty())
207                 MDC.put(MDC_SERVICE_NAME, "DEFAULT");
208         } catch (Exception e) {
209             MDC.put(MDC_SERVICE_NAME, "DEFAULT");
210         }
211     }
212
213     private static void cleanRequestContext() {
214         MDC.remove(MDC_KEY_REQUEST_ID);
215         MDC.remove(LoggingConstants.MDCKeys.PARTNER_NAME);
216         MDC.remove(MDC_SERVICE_NAME);
217     }
218     private static void populateTimeContext(Instant beginTimeStamp, Instant endTimeStamp) {
219         String beginTime = "";
220         String endTime = "";
221         String elapsedTime = "";
222
223         if (beginTimeStamp != null && endTimeStamp != null) {
224             elapsedTime = String.valueOf(ChronoUnit.MILLIS.between(beginTimeStamp, endTimeStamp));
225             beginTime = generateTimestampStr(beginTimeStamp);
226             endTime = generateTimestampStr(endTimeStamp);
227         }
228
229         MDC.put(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP, beginTime);
230         MDC.put(LoggingConstants.MDCKeys.END_TIMESTAMP, endTime);
231         MDC.put(LoggingConstants.MDCKeys.ELAPSED_TIME, elapsedTime);
232     }
233
234     public static String generateTimestampStr(Instant timeStamp) {
235         DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
236         TimeZone tz = TimeZone.getTimeZone("UTC");
237         df.setTimeZone(tz);
238         return df.format(Date.from(timeStamp));
239     }
240
241     private static void cleanTimeContext() {
242         MDC.remove(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP);
243         MDC.remove(LoggingConstants.MDCKeys.END_TIMESTAMP);
244         MDC.remove(LoggingConstants.MDCKeys.ELAPSED_TIME);
245     }
246
247     private static void populateResponseContext(String statusCode, String responseCode,
248             String responseDescription) {
249         MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, statusCode != null ? statusCode : "");
250         MDC.put(LoggingConstants.MDCKeys.RESPONSE_CODE, responseCode);
251         MDC.put(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION,
252                 responseDescription != null ? responseDescription : "");
253     }
254
255     private static void cleanResponseContext() {
256         MDC.remove(LoggingConstants.MDCKeys.STATUS_CODE);
257         MDC.remove(LoggingConstants.MDCKeys.RESPONSE_CODE);
258         MDC.remove(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION);
259     }
260
261     private static void populateErrorContext(String errorCode, String errorDescription) {
262         MDC.put(LoggingConstants.MDCKeys.ERROR_CODE, errorCode);
263         MDC.put(LoggingConstants.MDCKeys.ERROR_DESCRIPTION, errorDescription);
264     }
265
266     private static void cleanErrorContext() {
267         MDC.remove(LoggingConstants.MDCKeys.ERROR_CODE);
268         MDC.remove(LoggingConstants.MDCKeys.ERROR_DESCRIPTION);
269     }
270
271 }