1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
|
package org.openecomp.sdc.common.log.elements;
import static java.lang.Integer.valueOf;
import java.net.InetAddress;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.onap.logging.ref.slf4j.ONAPLogConstants;
import org.openecomp.sdc.common.log.api.ILogConfiguration;
import org.openecomp.sdc.common.log.api.ILogFieldsHandler;
import org.openecomp.sdc.common.log.enums.ConstantsLogging;
import org.openecomp.sdc.common.log.enums.Severity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
public class LogFieldsMdcHandler implements ILogFieldsHandler {
private final static String dateFormatPattern = "yyyy-MM-dd'T'HH:mm:ss.SSSz";
protected static Logger log = LoggerFactory.getLogger(LogFieldsMdcHandler.class.getName());
protected static String hostAddress;
protected static String fqdn;
private static LogFieldsMdcHandler instanceMdcWrapper = new LogFieldsMdcHandler();
static {
try {
hostAddress = InetAddress.getLocalHost().getHostAddress();
fqdn = InetAddress.getByName(hostAddress).getCanonicalHostName();
} catch (Exception ex) {
log.error("failed to get machine parameters", ex);
}
}
private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(dateFormatPattern);
public static LogFieldsMdcHandler getInstance() {
return instanceMdcWrapper;
}
@Override
public void startAuditTimer() {
if (StringUtils.isEmpty(MDC.get(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP))) {
MDC.put(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP, generatedTimeNow());
}
}
@Override
public void startMetricTimer() {
if (StringUtils.isEmpty(MDC.get(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP))) {
MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP, generatedTimeNow());
}
}
@Override
public void stopAuditTimer() {
//set start time if it is not set yet
startAuditTimer();
MDC.put(ILogConfiguration.MDC_END_TIMESTAMP, generatedTimeNow());
setElapsedTime(MDC.get(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP));
}
@Override
public void stopMetricTimer() {
//set start time if it is not set yet
startMetricTimer();
MDC.put(ILogConfiguration.MDC_END_TIMESTAMP, generatedTimeNow());
setElapsedTime(MDC.get(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP));
}
@Override
public void setClassName(String className) {
MDC.put(ILogConfiguration.MDC_CLASS_NAME, className);
}
@Override
public void setServerFQDN(String serverFQDN) {
MDC.put(ONAPLogConstants.MDCs.SERVER_FQDN, serverFQDN);
}
@Override
public void setServerIPAddress(String serverIPAddress) {
MDC.put(ILogConfiguration.MDC_SERVER_IP_ADDRESS, serverIPAddress);
}
@Override
public void setServerFQDNInternally() {
setServerFQDN(fqdn);
}
@Override
public void setServerIPAddressInternally() {
setServerIPAddress(hostAddress);
}
@Override
public void setInstanceUUID(String instanceUUID) {
MDC.put(ONAPLogConstants.MDCs.INSTANCE_UUID, instanceUUID);
}
@Override
public void setProcessKey(String processKey) {
MDC.put(ILogConfiguration.MDC_PROCESS_KEY, processKey);
}
@Override
public void setAlertSeverity(Severity alertSeverity) {
MDC.put(ONAPLogConstants.MDCs.RESPONSE_SEVERITY, String.valueOf(alertSeverity.getSeverityType()));
}
@Override
public void setOptCustomField1(String customField1) {
MDC.put(ILogConfiguration.MDC_OPT_FIELD1, customField1);
}
@Override
public void setOutgoingInvocationId(String outgoingInvocationId) {
MDC.put(ILogConfiguration.MDC_OUTGOING_INVOCATION_ID, outgoingInvocationId);
}
@Override
public void setStatusCode(String statusCode) {
MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, statusCode);
}
@Override
public void setResponseCode(int responseCode) {
MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, Integer.toString(responseCode));
}
@Override
public void setResponseDesc(String responseDesc) {
MDC.put(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION, responseDesc);
}
@Override
public void setServiceInstanceId(String serviceInstanceId) {
MDC.put(ILogConfiguration.MDC_SERVICE_INSTANCE_ID, serviceInstanceId);
}
@Override
public void setTargetVirtualEntity(String targetVirtualEntity) {
MDC.put(ILogConfiguration.MDC_TARGET_VIRTUAL_ENTITY, targetVirtualEntity);
}
@Override
public String getErrorCode() {
return MDC.get(ILogConfiguration.MDC_ERROR_CODE);
}
@Override
public void setErrorCode(int errorCode) {
MDC.put(ILogConfiguration.MDC_ERROR_CODE, valueOf(errorCode).toString());
}
@Override
public String getServiceName() {
return MDC.get(ONAPLogConstants.MDCs.SERVICE_NAME);
}
@Override
public void setServiceName(String serviceName) {
MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, serviceName);
}
@Override
public String getErrorCategory() {
return MDC.get(ILogConfiguration.MDC_ERROR_CATEGORY);
}
@Override
public void setErrorCategory(String errorCategory) {
MDC.put(ILogConfiguration.MDC_ERROR_CATEGORY, errorCategory);
}
@Override
public void clear() {
MDC.clear();
}
@Override
public boolean isMDCParamEmpty(String mdcKeyName) {
return StringUtils.isEmpty(MDC.get(mdcKeyName));
}
@Override
public String getFqdn() {
return fqdn;
}
@Override
public String getHostAddress() {
return hostAddress;
}
@Override
public String getKeyRequestId() {
return MDC.get(ONAPLogConstants.MDCs.REQUEST_ID);
}
@Override
public void setKeyRequestId(String keyRequestId) {
MDC.put(ONAPLogConstants.MDCs.REQUEST_ID,
keyRequestId); // eg. servletRequest.getSession().getId()
}
@Override
public String getTargetEntity() {
return MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY);
}
@Override
public void setTargetEntity(String targetEntity) {
MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, targetEntity);
}
@Override
public String getTargetServiceName() {
return MDC.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME);
}
@Override
public void setTargetServiceName(String targetServiceName) {
MDC.put(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME, targetServiceName);
}
@Override
public void removeStatusCode() {
MDC.remove(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE);
}
@Override
public void removePartnerName() {
MDC.remove(ONAPLogConstants.MDCs.PARTNER_NAME);
}
@Override
public void removeResponseCode() {
MDC.remove(ONAPLogConstants.MDCs.RESPONSE_CODE);
}
@Override
public void removeResponseDesc() {
MDC.remove(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION);
}
@Override
public void removeServiceInstanceId() {
MDC.remove(ILogConfiguration.MDC_SERVICE_INSTANCE_ID);
}
@Override
public void removeTargetEntity() {
MDC.remove(ONAPLogConstants.MDCs.TARGET_ENTITY);
}
@Override
public void removeTargetServiceName() {
MDC.remove(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME);
}
@Override
public void removeTargetVirtualEntity() {
MDC.remove(ILogConfiguration.MDC_TARGET_VIRTUAL_ENTITY);
}
@Override
public void removeErrorCode() {
MDC.remove(ILogConfiguration.MDC_ERROR_CODE);
}
@Override
public void removeErrorCategory() {
MDC.remove(ILogConfiguration.MDC_ERROR_CATEGORY);
}
@Override
public void removeErrorDescription() {
MDC.remove(ILogConfiguration.MDC_ERROR_DESC);
}
@Override
public String getAuditMessage() {
return MDC.get(ILogConfiguration.MDC_AUDIT_MESSAGE);
}
@Override
public void setAuditMessage(String message) {
MDC.put(ILogConfiguration.MDC_AUDIT_MESSAGE, message);
}
@Override
public String getSupportablityStatusCode() {
return MDC.get(ILogConfiguration.MDC_SUPPORTABLITY_STATUS_CODE);
}
@Override
public void setSupportablityStatusCode(String statusCode) {
MDC.put(ILogConfiguration.MDC_SUPPORTABLITY_STATUS_CODE, statusCode);
}
@Override
public String getSupportablityAction() {
return MDC.get(ILogConfiguration.MDC_SUPPORTABLITY_ACTION);
}
@Override
public void setSupportablityAction(String action) {
MDC.put(ILogConfiguration.MDC_SUPPORTABLITY_ACTION, action);
}
@Override
public String getRemoteHost() {
return MDC.get(ILogConfiguration.MDC_REMOTE_HOST);
}
@Override
public void setRemoteHost(String remoteHost) {
MDC.put(ILogConfiguration.MDC_REMOTE_HOST, remoteHost);
}
@Override
public String getServerIpAddress() {
return MDC.get(ILogConfiguration.MDC_SERVER_IP_ADDRESS);
}
// @Override
// public String getSupportablityCsarName() {
// return MDC.get(ILogConfiguration.MDC_SUPPORTABLITY_CSAR_NAME);
// }
@Override
public String getSupportablityCsarUUID() {
return MDC.get(ILogConfiguration.MDC_SUPPORTABLITY_CSAR_UUID);
}
@Override
public void setSupportablityCsarUUID(String uuid) {
MDC.put(ILogConfiguration.MDC_SUPPORTABLITY_CSAR_UUID, uuid);
}
@Override
public String getSupportablityCsarVersion() {
return MDC.get(ILogConfiguration.MDC_SUPPORTABLITY_CSAR_VERSION);
}
@Override
public void setSupportablityCsarVersion(String version) {
MDC.put(ILogConfiguration.MDC_SUPPORTABLITY_CSAR_VERSION, version);
}
@Override
public String getSupportablityComponentName() {
return MDC.get(ILogConfiguration.MDC_SUPPORTABLITY_COMPONENT_NAME);
}
@Override
public void setSupportablityComponentName(String name) {
MDC.put(ILogConfiguration.MDC_SUPPORTABLITY_COMPONENT_NAME, name);
}
@Override
public String getSupportablityComponentUUID() {
return MDC.get(ILogConfiguration.MDC_SUPPORTABLITY_COMPONENT_UUID);
}
@Override
public void setSupportablityComponentUUID(String uuid) {
MDC.put(ILogConfiguration.MDC_SUPPORTABLITY_COMPONENT_UUID, uuid);
}
@Override
public String getSupportablityComponentVersion() {
return MDC.get(ILogConfiguration.MDC_SUPPORTABLITY_COMPONENT_VERSION);
}
@Override
public void setSupportablityComponentVersion(String version) {
MDC.put(ILogConfiguration.MDC_SUPPORTABLITY_COMPONENT_VERSION, version);
}
@Override
public String getKeyInvocationId() {
return MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID);
}
@Override
public void setKeyInvocationId(String invocationId) {
MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId);
}
@Override
public void removeSupportablityAction() {
MDC.remove(ILogConfiguration.MDC_SUPPORTABLITY_ACTION);
}
@Override
public void removeSupportablityComponentName() {
MDC.remove(ILogConfiguration.MDC_SUPPORTABLITY_COMPONENT_NAME);
}
@Override
public void removeSupportablityComponentUUID() {
MDC.remove(ILogConfiguration.MDC_SUPPORTABLITY_COMPONENT_UUID);
}
@Override
public void removeSupportablityComponentVersion() {
MDC.remove(ILogConfiguration.MDC_SUPPORTABLITY_COMPONENT_VERSION);
}
// }
@Override
public void removeSupportablityCsarUUID() {
MDC.remove(ILogConfiguration.MDC_SUPPORTABLITY_CSAR_UUID);
}
// @Override
// public void removeSupportablityCsarName() {
// MDC.remove(ILogConfiguration.MDC_SUPPORTABLITY_CSAR_NAME);
@Override
public void removeSupportablityCsarVersion() {
MDC.remove(ILogConfiguration.MDC_SUPPORTABLITY_CSAR_VERSION);
}
@Override
public void removeSupportablityStatusCode() {
MDC.remove(ILogConfiguration.MDC_SUPPORTABLITY_STATUS_CODE);
}
@Override
public String getPartnerName() {
return MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME);
}
@Override
public void setPartnerName(String partnerName) {
MDC.put(ONAPLogConstants.MDCs.PARTNER_NAME, partnerName);
}
private void setElapsedTime(String beginTimestamp) {
try {
final LocalDateTime startTime = LocalDateTime.parse(beginTimestamp, dateTimeFormatter);
final LocalDateTime endTime = LocalDateTime.parse(MDC.get(ILogConfiguration.MDC_END_TIMESTAMP), dateTimeFormatter);
final Duration timeDifference = Duration.between(startTime, endTime);
MDC.put(ILogConfiguration.MDC_ELAPSED_TIME, String.valueOf(timeDifference.toMillis()));
} catch (Exception ex) {
log.error("failed to calculate elapsed time", ex);
}
}
private String generatedTimeNow() {
return dateTimeFormatter.withZone(ZoneOffset.UTC).format(Instant.now());
}
public void collectRequestInfoForErrorAndDebugLogging(HttpServletRequest httpRequest) {
LogFieldsMdcHandler.getInstance().clear();
String partnerName = LoggerBase.getPartnerName(httpRequest);
LogFieldsMdcHandler.getInstance().setPartnerName(partnerName);
String serviceInstanceID = httpRequest.getHeader(ConstantsLogging.X_ECOMP_SERVICE_ID_HEADER);
LogFieldsMdcHandler.getInstance().setServiceInstanceId(serviceInstanceID);
LogFieldsMdcHandler.getInstance().setRemoteHost(httpRequest.getRemoteHost());
LogFieldsMdcHandler.getInstance().setServerIPAddress(httpRequest.getLocalAddr());
String requestId = LoggerBase.getRequestId(httpRequest);
LogFieldsMdcHandler.getInstance().setKeyRequestId(requestId);
LogFieldsMdcHandler.getInstance().setServiceName(httpRequest.getRequestURI());
}
public void addInfoForErrorAndDebugLogging(String partnerName) {
LogFieldsMdcHandler.getInstance().clear();
LogFieldsMdcHandler.getInstance().setPartnerName(partnerName);
String requestId = LoggerBase.generateKeyRequestId();
LogFieldsMdcHandler.getInstance().setKeyRequestId(requestId);
}
}
|