Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<httpclient.version>4.5.13</httpclient.version>
<jackson.version>2.13.3</jackson.version>
<jacoco-maven-plugin.version>0.8.4</jacoco-maven-plugin.version>
<java.version>11</java.version>
<java.version>1.8</java.version>
<kafka.version>2.4.1</kafka.version>
<kiwigrid.version>2.5</kiwigrid.version>
<log4j.version>2.17.1</log4j.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> getContextMapFromHttpHeaders(HttpServletRequest servletRequest) {
final Map<String, String> loggingContext = new HashMap<>();
final Enumeration<String> headerNames = servletRequest.getHeaderNames();
Expand All @@ -38,6 +61,32 @@ public static Map<String, String> getContextMapFromHttpHeaders(HttpServletReques

return loggingContext;
}
public static void logEnabledMDC(String message, MessageType messageType, MessageDirection messageDirection, String externalRequestId, String contentType, String protocol, Map<String,Object> 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<String, String> mdcContext = MDC.getCopyOfContextMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -98,9 +102,10 @@ public String createVnfInstance(final ResourceManagerDeploymentLocation deployme
final HttpHeaders headers = getHttpHeaders(deploymentLocation);
headers.setContentType(MediaType.APPLICATION_JSON);
final HttpEntity<String> 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<String> 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();
Expand All @@ -127,9 +132,10 @@ public void deleteVnfInstance(final ResourceManagerDeploymentLocation deployment
final HttpEntity<Void> requestEntity = new HttpEntity<>(headers);
final Map<String, String> 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<Void> 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);
}

Expand Down Expand Up @@ -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<String> 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<String> 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();
Expand Down Expand Up @@ -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<LccnSubscriptionRequest> 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<LccnSubscription> 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();
Expand Down Expand Up @@ -482,9 +490,10 @@ public void deleteLifecycleSubscription(final ResourceManagerDeploymentLocation
final HttpEntity<Void> requestEntity = new HttpEntity<>(headers);
final Map<String, String> 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<Void> 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);
}

Expand Down Expand Up @@ -523,4 +532,16 @@ private void checkResponseEntityMatches(final ResponseEntity responseEntity, fin
}
}

Map<String,Object> getProtocolMetaData(String url,ResponseEntity responseEntity){

Map<String,Object> protocolMetadata=new HashMap<>();

protocolMetadata.put("status",responseEntity.getStatusCode());
protocolMetadata.put("status_code",responseEntity.getStatusCodeValue());
protocolMetadata.put("url",url);

return protocolMetadata;

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.accantosystems.stratoss.vnfmdriver.model;

public enum MessageDirection {

RECEIVED("received"), SENT("sent");

MessageDirection(String sent) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.accantosystems.stratoss.vnfmdriver.model;

public enum MessageType {

REQUEST("request"), RESPONSE("response"), MESSAGE("message");

MessageType(String request) {
}
}