Added oparent to sdc main
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / log / elements / LogFieldsMdcHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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.openecomp.sdc.common.log.elements;
22
23 import org.apache.commons.lang3.StringUtils;
24 import org.openecomp.sdc.common.log.api.ILogConfiguration;
25 import org.openecomp.sdc.common.log.api.ILogFieldsHandler;
26 import org.openecomp.sdc.common.log.enums.Severity;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.slf4j.MDC;
30
31 import java.net.InetAddress;
32 import java.time.Duration;
33 import java.time.Instant;
34 import java.time.LocalDateTime;
35 import java.time.ZoneOffset;
36 import java.time.format.DateTimeFormatter;
37
38 import static java.lang.Integer.valueOf;
39
40 public class LogFieldsMdcHandler implements ILogFieldsHandler {
41
42     private static LogFieldsMdcHandler instanceMdcWrapper = new LogFieldsMdcHandler();
43
44     public static LogFieldsMdcHandler getInstance() {
45         return instanceMdcWrapper;
46     }
47
48     private final static String dateFormatPattern = "yyyy-MM-dd HH:mm:ss.SSSz";
49     private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(dateFormatPattern);
50     protected static Logger log = LoggerFactory.getLogger(LogFieldsMdcHandler.class.getName());
51     protected static String hostAddress;
52     protected static String fqdn;
53
54     static {
55         try {
56             hostAddress = InetAddress.getLocalHost().getHostAddress();
57             fqdn = InetAddress.getByName(hostAddress).getCanonicalHostName();
58         } catch (Exception ex) {
59             log.error("failed to get machine parameters", ex);
60         }
61     }
62
63     @Override
64     public void startAuditTimer() {
65         if (StringUtils.isEmpty(MDC.get(ILogConfiguration.MDC_AUDIT_BEGIN_TIMESTAMP))) {
66             MDC.put(ILogConfiguration.MDC_AUDIT_BEGIN_TIMESTAMP, generatedTimeNow());
67         }
68     }
69
70     @Override
71     public void startMetricTimer() {
72         if (StringUtils.isEmpty(MDC.get(ILogConfiguration.MDC_METRIC_BEGIN_TIMESTAMP))) {
73             MDC.put(ILogConfiguration.MDC_METRIC_BEGIN_TIMESTAMP, generatedTimeNow());
74         }
75     }
76
77     @Override
78     public void stopAuditTimer() {
79         //set start time if it is not set yet
80         startAuditTimer();
81         MDC.put(ILogConfiguration.MDC_END_TIMESTAMP, generatedTimeNow());
82         setElapsedTime(MDC.get(ILogConfiguration.MDC_AUDIT_BEGIN_TIMESTAMP));
83     }
84
85     @Override
86     public void stopMetricTimer() {
87         //set start time if it is not set yet
88         startMetricTimer();
89         MDC.put(ILogConfiguration.MDC_END_TIMESTAMP, generatedTimeNow());
90         setElapsedTime(MDC.get(ILogConfiguration.MDC_METRIC_BEGIN_TIMESTAMP));
91     }
92
93     @Override
94     public void setClassName(String className) {
95         MDC.put(ILogConfiguration.MDC_CLASS_NAME, className);
96     }
97
98     @Override
99     public void setServerFQDN(String serverFQDN) {
100         MDC.put(ILogConfiguration.MDC_SERVER_FQDN, serverFQDN);
101     }
102
103     @Override
104     public void setServerIPAddress(String serverIPAddress) {
105         MDC.put(ILogConfiguration.MDC_SERVER_IP_ADDRESS, serverIPAddress);
106     }
107
108     @Override
109     public void setServerFQDNInternally() {
110         setServerFQDN(fqdn);
111     }
112
113     @Override
114     public void setServerIPAddressInternally() {
115         setServerIPAddress(hostAddress);
116     }
117
118     @Override
119     public void setInstanceUUID(String instanceUUID) {
120         MDC.put(ILogConfiguration.MDC_INSTANCE_UUID, instanceUUID);
121     }
122
123     @Override
124     public void setProcessKey(String processKey) {
125         MDC.put(ILogConfiguration.MDC_PROCESS_KEY, processKey);
126     }
127
128     @Override
129     public void setAlertSeverity(Severity alertSeverity) {
130         MDC.put(ILogConfiguration.MDC_ALERT_SEVERITY, String.valueOf(alertSeverity.getSeverityType()));
131     }
132
133     @Override
134     public void setOptCustomField1(String customField1) {
135         MDC.put(ILogConfiguration.MDC_OPT_FIELD1, customField1);
136     }
137
138     @Override
139     public void setOptCustomField2(String customField2) {
140         MDC.put(ILogConfiguration.MDC_OPT_FIELD2, customField2);
141     }
142
143     @Override
144     public void setOptCustomField3(String customField3) {
145         MDC.put(ILogConfiguration.MDC_OPT_FIELD3, customField3);
146     }
147
148     @Override
149     public void setOptCustomField4(String customField4) {
150         MDC.put(ILogConfiguration.MDC_OPT_FIELD4, customField4);
151     }
152
153     @Override
154     public void setKeyRequestId(String keyRequestId) {
155         MDC.put(ILogConfiguration.MDC_KEY_REQUEST_ID, keyRequestId); // eg. servletRequest.getSession().getId()
156     }
157
158     @Override
159     public void setRemoteHost(String remoteHost) {
160         MDC.put(ILogConfiguration.MDC_REMOTE_HOST, remoteHost);
161     }
162
163     @Override
164     public void setServiceName(String serviceName) {
165         MDC.put(ILogConfiguration.MDC_SERVICE_NAME, serviceName);
166     }
167
168     @Override
169     public void setStatusCode(String statusCode) {
170         MDC.put(ILogConfiguration.MDC_STATUS_CODE, statusCode);
171     }
172
173     @Override
174     public void setPartnerName(String partnerName) {
175         MDC.put(ILogConfiguration.MDC_PARTNER_NAME, partnerName);
176     }
177
178     @Override
179     public void setResponseCode(int responseCode) {
180         MDC.put(ILogConfiguration.MDC_RESPONSE_CODE, Integer.toString(responseCode));
181     }
182
183     @Override
184     public void setResponseDesc(String responseDesc) {
185         MDC.put(ILogConfiguration.MDC_RESPONSE_DESC, responseDesc);
186     }
187
188     @Override
189     public void setServiceInstanceId(String serviceInstanceId) {
190         MDC.put(ILogConfiguration.MDC_SERVICE_INSTANCE_ID, serviceInstanceId);
191     }
192
193     @Override
194     public void setTargetEntity(String targetEntity) {
195         MDC.put(ILogConfiguration.MDC_TARGET_ENTITY, targetEntity);
196     }
197
198     @Override
199     public void setTargetServiceName(String targetServiceName) {
200         MDC.put(ILogConfiguration.MDC_TARGET_SERVICE_NAME, targetServiceName);
201     }
202
203     @Override
204     public void setTargetVirtualEntity(String targetVirtualEntity) {
205         MDC.put(ILogConfiguration.MDC_TARGET_VIRTUAL_ENTITY, targetVirtualEntity);
206     }
207
208     @Override
209     public void setErrorCode(int errorCode) {
210         MDC.put(ILogConfiguration.MDC_ERROR_CODE, valueOf(errorCode).toString());
211     }
212
213     @Override
214     public void setErrorCategory(String errorCategory) {
215         MDC.put(ILogConfiguration.MDC_ERROR_CATEGORY, errorCategory);
216     }
217
218     @Override
219     public String getErrorCode() {
220         return MDC.get(ILogConfiguration.MDC_ERROR_CODE);
221     }
222
223     @Override
224     public String getServiceName() {
225         return MDC.get(ILogConfiguration.MDC_SERVICE_NAME);
226     }
227
228     @Override
229     public String getErrorCategory() {
230         return MDC.get(ILogConfiguration.MDC_ERROR_CATEGORY);
231     }
232
233     @Override
234     public void clear() {
235         MDC.clear();
236     }
237
238     @Override
239     public boolean isMDCParamEmpty(String mdcKeyName) {
240         return StringUtils.isEmpty(MDC.get(mdcKeyName));
241     }
242
243     @Override
244     public String getFqdn() {
245         return fqdn;
246     }
247
248     @Override
249     public String getHostAddress() {
250         return hostAddress;
251     }
252
253     @Override
254     public String getKeyRequestId() {
255         return MDC.get(ILogConfiguration.MDC_KEY_REQUEST_ID);
256     }
257
258     @Override
259     public void removeStatusCode() {
260         MDC.remove(ILogConfiguration.MDC_STATUS_CODE);
261     }
262
263     @Override
264     public void removePartnerName(){
265         MDC.remove(ILogConfiguration.MDC_PARTNER_NAME);
266     }
267
268     @Override
269     public void removeResponseCode(){
270         MDC.remove(ILogConfiguration.MDC_RESPONSE_CODE);
271     }
272
273     @Override
274     public void removeResponseDesc(){
275         MDC.remove(ILogConfiguration.MDC_RESPONSE_DESC);
276     }
277
278     @Override
279     public void removeServiceInstanceId(){
280         MDC.remove(ILogConfiguration.MDC_SERVICE_INSTANCE_ID);
281     }
282
283     @Override
284     public void removeTargetEntity(){
285         MDC.remove(ILogConfiguration.MDC_TARGET_ENTITY);
286     }
287
288     @Override
289     public void removeTargetServiceName(){
290         MDC.remove(ILogConfiguration.MDC_TARGET_SERVICE_NAME);
291     }
292
293     @Override
294     public void removeTargetVirtualEntity(){
295         MDC.remove(ILogConfiguration.MDC_TARGET_VIRTUAL_ENTITY);
296     }
297
298     @Override
299     public void removeErrorCode(){
300         MDC.remove(ILogConfiguration.MDC_ERROR_CODE);
301     }
302
303     @Override
304     public void removeErrorCategory(){
305         MDC.remove(ILogConfiguration.MDC_ERROR_CATEGORY);
306     }
307
308     @Override
309     public void removeErrorDescription(){
310         MDC.remove(ILogConfiguration.MDC_ERROR_DESC);
311     }
312
313     @Override
314     public void setAuditMessage(String message) {
315         MDC.put(ILogConfiguration.MDC_AUDIT_MESSAGE, message);
316     }
317
318     @Override
319     public String getAuditMessage() {
320         return MDC.get(ILogConfiguration.MDC_AUDIT_MESSAGE);
321     }
322
323     private void setElapsedTime(String beginTimestamp) {
324         try {
325             final LocalDateTime startTime = LocalDateTime.parse(beginTimestamp, dateTimeFormatter);
326             final LocalDateTime endTime = LocalDateTime.parse(MDC.get(ILogConfiguration.MDC_END_TIMESTAMP), dateTimeFormatter);
327             final Duration timeDifference = Duration.between(startTime, endTime);
328
329             MDC.put(ILogConfiguration.MDC_ELAPSED_TIME, String.valueOf(timeDifference.toMillis()));
330
331         } catch(Exception ex) {
332             log.error("failed to calculate elapsed time",ex);
333         }
334     }
335
336     private String generatedTimeNow() {
337         return dateTimeFormatter
338                 .withZone(ZoneOffset.UTC)
339                 .format(Instant.now());
340     }
341
342 }