1 package org.openecomp.sdc.common.log.elements;
3 import org.apache.commons.lang3.StringUtils;
4 import org.onap.logging.ref.slf4j.ONAPLogConstants;
5 import org.openecomp.sdc.common.log.enums.ConstantsLogging;
6 import org.openecomp.sdc.common.log.api.ILogConfiguration;
7 import org.openecomp.sdc.common.log.api.ILogFieldsHandler;
8 import org.openecomp.sdc.common.log.enums.Severity;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
12 import javax.servlet.http.HttpServletRequest;
13 import java.net.InetAddress;
14 import java.time.Duration;
15 import java.time.Instant;
16 import java.time.LocalDateTime;
17 import java.time.ZoneOffset;
18 import java.time.format.DateTimeFormatter;
20 import static java.lang.Integer.valueOf;
22 public class LogFieldsMdcHandler implements ILogFieldsHandler {
24 private static LogFieldsMdcHandler instanceMdcWrapper = new LogFieldsMdcHandler();
26 public static LogFieldsMdcHandler getInstance() {
27 return instanceMdcWrapper;
30 private final static String dateFormatPattern = "yyyy-MM-dd'T'HH:mm:ss.SSSz";
31 private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter
32 .ofPattern(dateFormatPattern);
33 protected static Logger log = LoggerFactory.getLogger(LogFieldsMdcHandler.class.getName());
34 protected static String hostAddress;
35 protected static String fqdn;
39 hostAddress = InetAddress.getLocalHost().getHostAddress();
40 fqdn = InetAddress.getByName(hostAddress).getCanonicalHostName();
41 } catch (Exception ex) {
42 log.error("failed to get machine parameters", ex);
47 public void startAuditTimer() {
48 if (StringUtils.isEmpty(MDC.get(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP))) {
49 MDC.put(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP, generatedTimeNow());
54 public void startMetricTimer() {
55 if (StringUtils.isEmpty(MDC.get(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP))) {
56 MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP, generatedTimeNow());
61 public void stopAuditTimer() {
62 //set start time if it is not set yet
64 MDC.put(ILogConfiguration.MDC_END_TIMESTAMP, generatedTimeNow());
65 setElapsedTime(MDC.get(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP));
69 public void stopMetricTimer() {
70 //set start time if it is not set yet
72 MDC.put(ILogConfiguration.MDC_END_TIMESTAMP, generatedTimeNow());
73 setElapsedTime(MDC.get(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP));
77 public void setClassName(String className) {
78 MDC.put(ILogConfiguration.MDC_CLASS_NAME, className);
82 public void setServerFQDN(String serverFQDN) {
83 MDC.put(ONAPLogConstants.MDCs.SERVER_FQDN, serverFQDN);
87 public void setServerIPAddress(String serverIPAddress) {
88 MDC.put(ILogConfiguration.MDC_SERVER_IP_ADDRESS, serverIPAddress);
92 public void setServerFQDNInternally() {
97 public void setServerIPAddressInternally() {
98 setServerIPAddress(hostAddress);
102 public void setInstanceUUID(String instanceUUID) {
103 MDC.put(ONAPLogConstants.MDCs.INSTANCE_UUID, instanceUUID);
107 public void setProcessKey(String processKey) {
108 MDC.put(ILogConfiguration.MDC_PROCESS_KEY, processKey);
112 public void setAlertSeverity(Severity alertSeverity) {
113 MDC.put(ONAPLogConstants.MDCs.RESPONSE_SEVERITY, String.valueOf(alertSeverity.getSeverityType()));
117 public void setOptCustomField1(String customField1) {
118 MDC.put(ILogConfiguration.MDC_OPT_FIELD1, customField1);
122 public void setOutgoingInvocationId(String outgoingInvocationId) {
123 MDC.put(ILogConfiguration.MDC_OUTGOING_INVOCATION_ID, outgoingInvocationId);
127 public void setKeyRequestId(String keyRequestId) {
128 MDC.put(ONAPLogConstants.MDCs.REQUEST_ID,
129 keyRequestId); // eg. servletRequest.getSession().getId()
133 public void setKeyInvocationId(String invocationId ) {
134 MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID,
139 public void setRemoteHost(String remoteHost) {
140 MDC.put(ILogConfiguration.MDC_REMOTE_HOST, remoteHost);
144 public void setServiceName(String serviceName) {
145 MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, serviceName);
149 public void setStatusCode(String statusCode) {
150 MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, statusCode);
154 public void setPartnerName(String partnerName) {
155 MDC.put(ONAPLogConstants.MDCs.PARTNER_NAME, partnerName);
159 public void setResponseCode(int responseCode) {
160 MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, Integer.toString(responseCode));
164 public void setResponseDesc(String responseDesc) {
165 MDC.put(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION, responseDesc);
169 public void setServiceInstanceId(String serviceInstanceId) {
170 MDC.put(ILogConfiguration.MDC_SERVICE_INSTANCE_ID, serviceInstanceId);
174 public void setTargetEntity(String targetEntity) {
175 MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, targetEntity);
179 public void setTargetServiceName(String targetServiceName) {
180 MDC.put(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME, targetServiceName);
184 public void setTargetVirtualEntity(String targetVirtualEntity) {
185 MDC.put(ILogConfiguration.MDC_TARGET_VIRTUAL_ENTITY, targetVirtualEntity);
189 public void setErrorCode(int errorCode) {
190 MDC.put(ILogConfiguration.MDC_ERROR_CODE, valueOf(errorCode).toString());
194 public void setErrorCategory(String errorCategory) {
195 MDC.put(ILogConfiguration.MDC_ERROR_CATEGORY, errorCategory);
199 public String getErrorCode() {
200 return MDC.get(ILogConfiguration.MDC_ERROR_CODE);
204 public String getServiceName() {
205 return MDC.get(ONAPLogConstants.MDCs.SERVICE_NAME);
209 public String getErrorCategory() {
210 return MDC.get(ILogConfiguration.MDC_ERROR_CATEGORY);
214 public void clear() {
219 public boolean isMDCParamEmpty(String mdcKeyName) {
220 return StringUtils.isEmpty(MDC.get(mdcKeyName));
224 public String getFqdn() {
229 public String getHostAddress() {
234 public String getKeyRequestId() {
235 return MDC.get(ONAPLogConstants.MDCs.REQUEST_ID);
239 public String getTargetEntity() {
240 return MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY);
244 public String getTargetServiceName() {
245 return MDC.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME);
249 public void removeStatusCode() {
250 MDC.remove(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE);
254 public void removePartnerName() {
255 MDC.remove(ONAPLogConstants.MDCs.PARTNER_NAME);
259 public void removeResponseCode() {
260 MDC.remove(ONAPLogConstants.MDCs.RESPONSE_CODE);
264 public void removeResponseDesc() {
265 MDC.remove(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION);
269 public void removeServiceInstanceId() {
270 MDC.remove(ILogConfiguration.MDC_SERVICE_INSTANCE_ID);
274 public void removeTargetEntity() {
275 MDC.remove(ONAPLogConstants.MDCs.TARGET_ENTITY);
279 public void removeTargetServiceName() {
280 MDC.remove(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME);
284 public void removeTargetVirtualEntity() {
285 MDC.remove(ILogConfiguration.MDC_TARGET_VIRTUAL_ENTITY);
289 public void removeErrorCode() {
290 MDC.remove(ILogConfiguration.MDC_ERROR_CODE);
294 public void removeErrorCategory() {
295 MDC.remove(ILogConfiguration.MDC_ERROR_CATEGORY);
299 public void removeErrorDescription() {
300 MDC.remove(ILogConfiguration.MDC_ERROR_DESC);
304 public void setAuditMessage(String message) {
305 MDC.put(ILogConfiguration.MDC_AUDIT_MESSAGE, message);
309 public String getAuditMessage() {
310 return MDC.get(ILogConfiguration.MDC_AUDIT_MESSAGE);
315 public String getSupportablityStatusCode() {
316 return MDC.get(ILogConfiguration.MDC_SUPPORTABLITY_STATUS_CODE);
320 public String getSupportablityAction() {
321 return MDC.get(ILogConfiguration.MDC_SUPPORTABLITY_ACTION);
327 public String getRemoteHost() {
328 return MDC.get(ILogConfiguration.MDC_REMOTE_HOST);
332 public String getServerIpAddress() {
333 return MDC.get(ILogConfiguration.MDC_SERVER_IP_ADDRESS);
337 // public String getSupportablityCsarName() {
338 // return MDC.get(ILogConfiguration.MDC_SUPPORTABLITY_CSAR_NAME);
342 public String getSupportablityCsarUUID() {
343 return MDC.get(ILogConfiguration.MDC_SUPPORTABLITY_CSAR_UUID);
347 public String getSupportablityCsarVersion() {
348 return MDC.get(ILogConfiguration.MDC_SUPPORTABLITY_CSAR_VERSION);
353 public String getSupportablityComponentName() {
354 return MDC.get(ILogConfiguration.MDC_SUPPORTABLITY_COMPONENT_NAME);
358 public String getSupportablityComponentUUID() {
359 return MDC.get(ILogConfiguration.MDC_SUPPORTABLITY_COMPONENT_UUID);
364 public String getSupportablityComponentVersion() {
365 return MDC.get(ILogConfiguration.MDC_SUPPORTABLITY_COMPONENT_VERSION);
369 public String getKeyInvocationId() {
370 return MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID);
374 public void setSupportablityStatusCode(String statusCode) {
375 MDC.put(ILogConfiguration.MDC_SUPPORTABLITY_STATUS_CODE, statusCode);
379 public void setSupportablityAction(String action) {
380 MDC.put(ILogConfiguration.MDC_SUPPORTABLITY_ACTION, action);
384 public void setSupportablityCsarUUID(String uuid) {
385 MDC.put(ILogConfiguration.MDC_SUPPORTABLITY_CSAR_UUID, uuid);
389 public void setSupportablityCsarVersion(String version) {
390 MDC.put(ILogConfiguration.MDC_SUPPORTABLITY_CSAR_VERSION, version);
394 public void setSupportablityComponentName(String name) {
395 MDC.put(ILogConfiguration.MDC_SUPPORTABLITY_COMPONENT_NAME, name);
399 public void setSupportablityComponentUUID(String uuid) {
400 MDC.put(ILogConfiguration.MDC_SUPPORTABLITY_COMPONENT_UUID, uuid);
404 public void setSupportablityComponentVersion(String version) {
405 MDC.put(ILogConfiguration.MDC_SUPPORTABLITY_COMPONENT_VERSION, version);
409 public void removeSupportablityAction() {
410 MDC.remove(ILogConfiguration.MDC_SUPPORTABLITY_ACTION);
414 public void removeSupportablityComponentName() {
415 MDC.remove(ILogConfiguration.MDC_SUPPORTABLITY_COMPONENT_NAME);
419 public void removeSupportablityComponentUUID() {
420 MDC.remove(ILogConfiguration.MDC_SUPPORTABLITY_COMPONENT_UUID);
424 public void removeSupportablityComponentVersion() {
425 MDC.remove(ILogConfiguration.MDC_SUPPORTABLITY_COMPONENT_VERSION);
429 // public void removeSupportablityCsarName() {
430 // MDC.remove(ILogConfiguration.MDC_SUPPORTABLITY_CSAR_NAME);
434 public void removeSupportablityCsarUUID() {
435 MDC.remove(ILogConfiguration.MDC_SUPPORTABLITY_CSAR_UUID);
439 public void removeSupportablityCsarVersion() {
440 MDC.remove(ILogConfiguration.MDC_SUPPORTABLITY_CSAR_VERSION);
444 public void removeSupportablityStatusCode() {
445 MDC.remove(ILogConfiguration.MDC_SUPPORTABLITY_STATUS_CODE);
449 public String getPartnerName() {
450 return MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME);
453 private void setElapsedTime(String beginTimestamp) {
455 final LocalDateTime startTime = LocalDateTime.parse(beginTimestamp, dateTimeFormatter);
456 final LocalDateTime endTime = LocalDateTime
457 .parse(MDC.get(ILogConfiguration.MDC_END_TIMESTAMP), dateTimeFormatter);
458 final Duration timeDifference = Duration.between(startTime, endTime);
460 MDC.put(ILogConfiguration.MDC_ELAPSED_TIME, String.valueOf(timeDifference.toMillis()));
462 } catch (Exception ex) {
463 log.error("failed to calculate elapsed time", ex);
467 private String generatedTimeNow() {
468 return dateTimeFormatter
469 .withZone(ZoneOffset.UTC)
470 .format(Instant.now());
473 public void collectRequestInfoForErrorAndDebugLogging(HttpServletRequest httpRequest) {
474 LogFieldsMdcHandler.getInstance().clear();
475 String partnerName = LoggerBase.getPartnerName(httpRequest);
476 LogFieldsMdcHandler.getInstance().setPartnerName(partnerName);
478 String serviceInstanceID = httpRequest.getHeader(ConstantsLogging.X_ECOMP_SERVICE_ID_HEADER);
479 LogFieldsMdcHandler.getInstance().setServiceInstanceId(serviceInstanceID);
481 LogFieldsMdcHandler.getInstance().setRemoteHost(httpRequest.getRemoteHost());
482 LogFieldsMdcHandler.getInstance().setServerIPAddress(httpRequest.getLocalAddr());
484 String requestId = LoggerBase.getRequestId(httpRequest);
485 LogFieldsMdcHandler.getInstance().setKeyRequestId(requestId);
487 LogFieldsMdcHandler.getInstance().setServiceName(httpRequest.getRequestURI());
490 public void addInfoForErrorAndDebugLogging(String partnerName){
491 LogFieldsMdcHandler.getInstance().clear();
492 LogFieldsMdcHandler.getInstance().setPartnerName(partnerName);
494 String requestId = LoggerBase.generateKeyRequestId();
495 LogFieldsMdcHandler.getInstance().setKeyRequestId(requestId);