diff --git a/pom.xml b/pom.xml index 85cee24..61a3a30 100644 --- a/pom.xml +++ b/pom.xml @@ -43,7 +43,7 @@ 4.5.13 2.13.3 0.8.4 - 11 + 1.8 2.4.1 2.5 2.17.1 diff --git a/src/main/java/com/accantosystems/stratoss/common/utils/LoggingUtils.java b/src/main/java/com/accantosystems/stratoss/common/utils/LoggingUtils.java index 30b62c4..9a8c8f4 100644 --- a/src/main/java/com/accantosystems/stratoss/common/utils/LoggingUtils.java +++ b/src/main/java/com/accantosystems/stratoss/common/utils/LoggingUtils.java @@ -6,15 +6,38 @@ import java.util.UUID; import javax.servlet.http.HttpServletRequest; +import com.accantosystems.stratoss.vnfmdriver.driver.SOL003ResponseException; +import com.accantosystems.stratoss.vnfmdriver.model.MessageDirection; +import com.accantosystems.stratoss.vnfmdriver.model.MessageType; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.slf4j.MDC; import org.springframework.http.HttpHeaders; public class LoggingUtils { + private final static Logger logger = LoggerFactory.getLogger(LoggingUtils.class); + private static final String LOGGING_CONTEXT_KEY_PREFIX = "traceCtx.".toLowerCase(); private static final String LOGGING_CONTEXT_HEADER_PREFIX = "X-TraceCtx-".toLowerCase(); private static final String TRANSACTION_ID_HEADER_KEY = "TransactionId".toLowerCase(); + private static final String LOG_MESSAGE_DIRECTION_KEY = "message_direction"; + + private static final String LOG_EXTERNAL_REQUEST_ID_KEY = "tracectx.externalrequestid"; + + private static final String LOG_CONTENT_TYPE_KEY = "content_type"; + + private static final String LOG_PROTOCOL_KEY = "protocol"; + + private static final String LOG_PROTOCOL_METADATA_KEY = "protocol_metadata"; + + private static final String LOG_MSG_TYP_KEY= "message_type"; + + private static final String LOG_DRIVER_REQUEST_ID ="tracectx.driverrequestid"; + public static Map getContextMapFromHttpHeaders(HttpServletRequest servletRequest) { final Map loggingContext = new HashMap<>(); final Enumeration headerNames = servletRequest.getHeaderNames(); @@ -38,6 +61,32 @@ public static Map getContextMapFromHttpHeaders(HttpServletReques return loggingContext; } + public static void logEnabledMDC(String message, MessageType messageType, MessageDirection messageDirection, String externalRequestId, String contentType, String protocol, Map protocolMetadata,String driverRequestId){ + try{ + MDC.put(LOG_MESSAGE_DIRECTION_KEY, messageDirection.toString()); + MDC.put(LOG_EXTERNAL_REQUEST_ID_KEY, externalRequestId); + MDC.put(LOG_CONTENT_TYPE_KEY, contentType); + MDC.put(LOG_PROTOCOL_KEY, protocol.toLowerCase()); + ObjectMapper jsonMapper = new ObjectMapper(); + try { + MDC.put(LOG_PROTOCOL_METADATA_KEY, jsonMapper.writeValueAsString(protocolMetadata)); + } catch (JsonProcessingException e) { + throw new SOL003ResponseException("Error in parsing protocol_metadata "+ protocolMetadata, e); + } + MDC.put(LOG_MSG_TYP_KEY,messageType.toString()); + MDC.put(LOG_DRIVER_REQUEST_ID,driverRequestId); + + logger.info(message); + }finally{ + MDC.remove(LOG_MESSAGE_DIRECTION_KEY); + MDC.remove(LOG_EXTERNAL_REQUEST_ID_KEY); + MDC.remove(LOG_CONTENT_TYPE_KEY); + MDC.remove(LOG_PROTOCOL_KEY); + MDC.remove(LOG_PROTOCOL_METADATA_KEY); + MDC.remove(LOG_MSG_TYP_KEY); + MDC.remove(LOG_DRIVER_REQUEST_ID); + } + } public static void setHttpHeadersFromMDC(final HttpHeaders httpHeaders) { Map mdcContext = MDC.getCopyOfContextMap(); diff --git a/src/main/java/com/accantosystems/stratoss/vnfmdriver/driver/VNFLifecycleManagementDriver.java b/src/main/java/com/accantosystems/stratoss/vnfmdriver/driver/VNFLifecycleManagementDriver.java index 698abdf..19bb8b6 100644 --- a/src/main/java/com/accantosystems/stratoss/vnfmdriver/driver/VNFLifecycleManagementDriver.java +++ b/src/main/java/com/accantosystems/stratoss/vnfmdriver/driver/VNFLifecycleManagementDriver.java @@ -6,7 +6,11 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.UUID; +import com.accantosystems.stratoss.common.utils.LoggingUtils; +import com.accantosystems.stratoss.vnfmdriver.model.MessageDirection; +import com.accantosystems.stratoss.vnfmdriver.model.MessageType; import org.etsi.sol003.lifecyclemanagement.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -98,9 +102,10 @@ public String createVnfInstance(final ResourceManagerDeploymentLocation deployme final HttpHeaders headers = getHttpHeaders(deploymentLocation); headers.setContentType(MediaType.APPLICATION_JSON); final HttpEntity requestEntity = new HttpEntity<>(createVnfRequest, headers); - + UUID uuid = UUID.randomUUID(); + LoggingUtils.logEnabledMDC(createVnfRequest, MessageType.REQUEST,MessageDirection.SENT, uuid.toString(),MediaType.APPLICATION_JSON.toString(), "http",null,uuid.toString()); final ResponseEntity responseEntity = authenticatedRestTemplateService.getRestTemplate(deploymentLocation).exchange(url, HttpMethod.POST, requestEntity, String.class); - + LoggingUtils.logEnabledMDC(responseEntity.getBody(), MessageType.RESPONSE,MessageDirection.RECEIVED,uuid.toString(),MediaType.APPLICATION_JSON.toString(), "http",getProtocolMetaData(url,responseEntity),uuid.toString()); // "Location" header also includes URI of the created instance checkResponseEntityMatches(responseEntity, HttpStatus.CREATED, true); return responseEntity.getBody(); @@ -127,9 +132,10 @@ public void deleteVnfInstance(final ResourceManagerDeploymentLocation deployment final HttpEntity requestEntity = new HttpEntity<>(headers); final Map uriVariables = new HashMap<>(); uriVariables.put("vnfInstanceId", vnfInstanceId); - + UUID uuid = UUID.randomUUID(); + LoggingUtils.logEnabledMDC(null, MessageType.REQUEST,MessageDirection.SENT, uuid.toString(),MediaType.APPLICATION_JSON.toString(), "http",null,uuid.toString()); final ResponseEntity responseEntity = authenticatedRestTemplateService.getRestTemplate(deploymentLocation).exchange(url, HttpMethod.DELETE, requestEntity, Void.class, uriVariables); - + LoggingUtils.logEnabledMDC(null, MessageType.RESPONSE,MessageDirection.RECEIVED,uuid.toString(),MediaType.APPLICATION_JSON.toString(), "http",getProtocolMetaData(url,responseEntity),uuid.toString()); checkResponseEntityMatches(responseEntity, HttpStatus.NO_CONTENT, false); } @@ -348,9 +354,10 @@ private String callVnfLcmOperation(final ResourceManagerDeploymentLocation deplo final String url = deploymentLocation.getProperties().get(VNFM_SERVER_URL) + API_CONTEXT_ROOT + API_PREFIX_VNF_INSTANCES + "/" + vnfInstanceId + "/" + operationName; final HttpHeaders headers = getHttpHeaders(deploymentLocation); final HttpEntity requestEntity = new HttpEntity<>(operationRequest, headers); - + UUID uuid = UUID.randomUUID(); + LoggingUtils.logEnabledMDC(operationRequest, MessageType.REQUEST,MessageDirection.SENT, uuid.toString(),MediaType.APPLICATION_JSON.toString(), "http",null,uuid.toString()); final ResponseEntity responseEntity = authenticatedRestTemplateService.getRestTemplate(deploymentLocation).exchange(url, HttpMethod.POST, requestEntity, String.class); - + LoggingUtils.logEnabledMDC(responseEntity.getBody(),MessageType.RESPONSE,MessageDirection.RECEIVED,uuid.toString(),MediaType.APPLICATION_JSON.toString(), "http",getProtocolMetaData(url,responseEntity),uuid.toString()); checkResponseEntityMatches(responseEntity, HttpStatus.ACCEPTED, false); // "Location" header contains URI of the created VnfLcmOpOcc record final URI location = responseEntity.getHeaders().getLocation(); @@ -422,10 +429,11 @@ public LccnSubscription createLifecycleSubscription(final ResourceManagerDeploym final String url = deploymentLocation.getProperties().get(VNFM_SERVER_URL) + API_CONTEXT_ROOT + API_PREFIX_SUBSCRIPTIONS; final HttpHeaders headers = getHttpHeaders(deploymentLocation); final HttpEntity requestEntity = new HttpEntity<>(lccnSubscriptionRequest, headers); - + UUID uuid = UUID.randomUUID(); + LoggingUtils.logEnabledMDC(lccnSubscriptionRequest.toString(),MessageType.REQUEST, MessageDirection.SENT, uuid.toString(),MediaType.APPLICATION_JSON.toString(), "http",null,uuid.toString()); final ResponseEntity responseEntity = authenticatedRestTemplateService.getRestTemplate(deploymentLocation) .exchange(url, HttpMethod.POST, requestEntity, LccnSubscription.class); - + LoggingUtils.logEnabledMDC(responseEntity.getBody().toString(),MessageType.RESPONSE, MessageDirection.RECEIVED,uuid.toString(),MediaType.APPLICATION_JSON.toString(), "http",getProtocolMetaData(url,responseEntity),uuid.toString()); // "Location" header also includes URI of the created instance checkResponseEntityMatches(responseEntity, HttpStatus.CREATED, true); return responseEntity.getBody(); @@ -482,9 +490,10 @@ public void deleteLifecycleSubscription(final ResourceManagerDeploymentLocation final HttpEntity requestEntity = new HttpEntity<>(headers); final Map uriVariables = new HashMap<>(); uriVariables.put("subscriptionId", subscriptionId); - + UUID uuid = UUID.randomUUID(); + LoggingUtils.logEnabledMDC(null, MessageType.REQUEST,MessageDirection.SENT, uuid.toString(),MediaType.APPLICATION_JSON.toString(), "http",null,uuid.toString()); final ResponseEntity responseEntity = authenticatedRestTemplateService.getRestTemplate(deploymentLocation).exchange(url, HttpMethod.DELETE, requestEntity, Void.class, uriVariables); - + LoggingUtils.logEnabledMDC(null, MessageType.RESPONSE,MessageDirection.RECEIVED,uuid.toString(),MediaType.APPLICATION_JSON.toString(), "http",getProtocolMetaData(url,responseEntity),uuid.toString()); checkResponseEntityMatches(responseEntity, HttpStatus.NO_CONTENT, false); } @@ -523,4 +532,16 @@ private void checkResponseEntityMatches(final ResponseEntity responseEntity, fin } } + Map getProtocolMetaData(String url,ResponseEntity responseEntity){ + + Map protocolMetadata=new HashMap<>(); + + protocolMetadata.put("status",responseEntity.getStatusCode()); + protocolMetadata.put("status_code",responseEntity.getStatusCodeValue()); + protocolMetadata.put("url",url); + + return protocolMetadata; + + } + } diff --git a/src/main/java/com/accantosystems/stratoss/vnfmdriver/model/MessageDirection.java b/src/main/java/com/accantosystems/stratoss/vnfmdriver/model/MessageDirection.java new file mode 100644 index 0000000..f8eacfe --- /dev/null +++ b/src/main/java/com/accantosystems/stratoss/vnfmdriver/model/MessageDirection.java @@ -0,0 +1,9 @@ +package com.accantosystems.stratoss.vnfmdriver.model; + +public enum MessageDirection { + + RECEIVED("received"), SENT("sent"); + + MessageDirection(String sent) { + } +} diff --git a/src/main/java/com/accantosystems/stratoss/vnfmdriver/model/MessageType.java b/src/main/java/com/accantosystems/stratoss/vnfmdriver/model/MessageType.java new file mode 100644 index 0000000..0d2fea2 --- /dev/null +++ b/src/main/java/com/accantosystems/stratoss/vnfmdriver/model/MessageType.java @@ -0,0 +1,9 @@ +package com.accantosystems.stratoss.vnfmdriver.model; + +public enum MessageType { + + REQUEST("request"), RESPONSE("response"), MESSAGE("message"); + + MessageType(String request) { + } +}