diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/org/onap/pomba/contextaggregator/rest/RestRequest.java | 59 | ||||
-rw-r--r-- | src/main/java/org/onap/pomba/contextaggregator/service/ContextAggregatorProcessor.java | 6 |
2 files changed, 54 insertions, 11 deletions
diff --git a/src/main/java/org/onap/pomba/contextaggregator/rest/RestRequest.java b/src/main/java/org/onap/pomba/contextaggregator/rest/RestRequest.java index c4f1eef..338af0a 100644 --- a/src/main/java/org/onap/pomba/contextaggregator/rest/RestRequest.java +++ b/src/main/java/org/onap/pomba/contextaggregator/rest/RestRequest.java @@ -17,9 +17,13 @@ */ package org.onap.pomba.contextaggregator.rest; +import java.net.InetAddress; +import java.text.SimpleDateFormat; import java.util.Base64; +import java.util.Date; import java.util.List; import java.util.Map; +import java.util.UUID; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.MultivaluedMap; @@ -33,6 +37,7 @@ import org.onap.pomba.contextaggregator.exception.ContextAggregatorError; import org.onap.pomba.contextaggregator.exception.ContextAggregatorException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.slf4j.MDC; import org.springframework.web.util.UriComponents; import org.springframework.web.util.UriComponentsBuilder; @@ -41,11 +46,22 @@ public class RestRequest { private static final String SERVICE_INSTANCE_ID = "serviceInstanceId"; private static final String MODEL_VERSION_ID = "modelVersionId"; private static final String MODEL_INVARIANT_ID = "modelInvariantId"; - private static final String APP_NAME = "context-aggregator"; - private static final String BASIC_AUTH = "Basic "; + private static final String MDC_REQUEST_ID = "RequestId"; + private static final String MDC_SERVER_FQDN = "ServerFQDN"; + private static final String MDC_SERVICE_NAME = "ServiceName"; + private static final String MDC_PARTNER_NAME = "PartnerName"; + private static final String MDC_START_TIME = "StartTime"; + private static final String MDC_SERVICE_INSTANCE_ID = "ServiceInstanceId"; + private static final String MDC_INVOCATION_ID = "InvocationID"; + private static final String MDC_CLIENT_ADDRESS = "ClientAddress"; + + private static final String MDC_STATUS_CODE = "StatusCode"; + private static final String MDC_RESPONSE_CODE = "ResponseCode"; + private static final String MDC_INSTANCE_UUID = "InstanceId"; + private static Logger log = LoggerFactory.getLogger(RestRequest.class); @@ -60,13 +76,15 @@ public class RestRequest { * @param event The audit event. * @return Returns the JSON response from the context builder */ - public static String getModelData(ContextBuilder builder, POAEvent event) throws ContextAggregatorException { + public static String getModelData(ContextBuilder builder, POAEvent event, UUID instanceId) throws ContextAggregatorException { + + initMdc(event, instanceId); RestClient restClient = createRestClient(builder); OperationResult result; - + try { - result = restClient.get(generateUri(builder, event), generateHeaders(event.getxTransactionId(), builder), + result = restClient.get(generateUri(builder, event), generateHeaders(event, builder), MediaType.APPLICATION_JSON_TYPE); } catch (Exception e) { log.error("Exception in Rest call", e); @@ -75,19 +93,44 @@ public class RestRequest { } if (result == null) { + MDC.put(MDC_STATUS_CODE, "ERROR"); throw new ContextAggregatorException(ContextAggregatorError.FAILED_TO_GET_MODEL_DATA, builder.getContextName(), "Null result"); } if (result.wasSuccessful()) { + MDC.put(MDC_RESPONSE_CODE, String.valueOf(result.getResultCode())); + MDC.put(MDC_STATUS_CODE, "COMPLETE"); log.info("Retrieved model data for '{}' context builder. Result: {}", builder.getContextName(), result.getResult()); return result.getResult(); } // failed! throw Exception: + MDC.put(MDC_STATUS_CODE, "ERROR"); throw new ContextAggregatorException(ContextAggregatorError.FAILED_TO_GET_MODEL_DATA, builder.getContextName(), result.getFailureCause()); } + private static void initMdc(POAEvent event, UUID instanceId) { + MDC.clear(); + MDC.put(MDC_REQUEST_ID, event.getxTransactionId()); + MDC.put(MDC_SERVICE_NAME, APP_NAME); + MDC.put(MDC_SERVICE_INSTANCE_ID, event.getServiceInstanceId()); + MDC.put(MDC_PARTNER_NAME, event.getxFromAppId()); + MDC.put(MDC_START_TIME, new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").format(new Date())); + MDC.put(MDC_INVOCATION_ID, UUID.randomUUID().toString()); + MDC.put(MDC_INSTANCE_UUID, instanceId.toString()); + + try { + MDC.put(MDC_CLIENT_ADDRESS, InetAddress.getLocalHost().getCanonicalHostName()); + } catch (Exception e) { + // If, for some reason we are unable to get the canonical host name, + // we + // just want to leave the field null. + log.info("Could not get canonical host name for " + MDC_SERVER_FQDN + ", leaving field null"); + } + } + + private static RestClient createRestClient(ContextBuilder builder) { return new RestClient() .connectTimeoutMs(builder.getConnectionTimeout()).readTimeoutMs(builder.getReadTimeout()); @@ -102,10 +145,10 @@ public class RestRequest { return uriComponents.toUriString(); } - private static Map<String, List<String>> generateHeaders(String transactionId, ContextBuilder builder) { + private static Map<String, List<String>> generateHeaders(POAEvent event, ContextBuilder builder) { MultivaluedMap<String, String> headers = new MultivaluedHashMap<>(); - headers.add(Headers.FROM_APP_ID, APP_NAME); - headers.add(Headers.TRANSACTION_ID, transactionId); + headers.add(Headers.FROM_APP_ID, event.getxFromAppId()); + headers.add(Headers.TRANSACTION_ID, event.getxTransactionId()); headers.add(Headers.AUTHORIZATION, getBasicAuthString(builder)); return headers; } diff --git a/src/main/java/org/onap/pomba/contextaggregator/service/ContextAggregatorProcessor.java b/src/main/java/org/onap/pomba/contextaggregator/service/ContextAggregatorProcessor.java index 21f3a3e..dca9796 100644 --- a/src/main/java/org/onap/pomba/contextaggregator/service/ContextAggregatorProcessor.java +++ b/src/main/java/org/onap/pomba/contextaggregator/service/ContextAggregatorProcessor.java @@ -27,7 +27,6 @@ import com.att.nsa.mr.client.MRConsumer; import com.att.nsa.mr.client.MRPublisher; import com.att.nsa.mr.client.MRTopicManager; import com.att.nsa.mr.client.impl.MRSimplerBatchPublisher; - import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; @@ -35,12 +34,12 @@ import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.UUID; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; - import org.onap.pomba.common.datatypes.DataQuality; import org.onap.pomba.common.datatypes.DataQuality.Status; import org.onap.pomba.common.datatypes.ModelContext; @@ -65,6 +64,7 @@ public class ContextAggregatorProcessor implements Callable<Void> { private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create(); private ExecutorService executor = Executors.newFixedThreadPool(10); private int retriesRemaining; + private static UUID instanceId = UUID.randomUUID(); @Autowired private MRConsumer consumer; @@ -108,7 +108,7 @@ public class ContextAggregatorProcessor implements Callable<Void> { for (ContextBuilder builder : contextBuilders) { try { log.info("Retrieving model data for: {}", builder.getContextName()); - String modelData = RestRequest.getModelData(builder, event); + String modelData = RestRequest.getModelData(builder, event, instanceId); retrievedModels.put(builder.getContextName(), modelData); } catch (ContextAggregatorException e) { DataQuality errorDataQuality = new DataQuality(); |