diff options
author | micdzied <michal.1.dziedzic@nokia.com> | 2018-08-30 10:25:19 +0200 |
---|---|---|
committer | micdzied <michal.1.dziedzic@nokia.com> | 2018-08-30 14:53:20 +0200 |
commit | 6fd8c7770fbea30ad2ec6e3f7645b8baab5a49a1 (patch) | |
tree | 78d572b5df8c88047d74edf3c75b28fcab3d0dc6 | |
parent | 82a6fc31d1905f380fbbae1d0b4a34b67d8258e3 (diff) |
add MDC's
Change-Id: Ie4fe86791f46b790ae42088a5d2b93f491e4549d
Issue-ID: DCAEGEN2-730
Signed-off-by: micdzied <michal.1.dziedzic@nokia.com>
38 files changed, 247 insertions, 130 deletions
diff --git a/prh-aai-client/pom.xml b/prh-aai-client/pom.xml index 87cdf389..cbd6c868 100644 --- a/prh-aai-client/pom.xml +++ b/prh-aai-client/pom.xml @@ -18,8 +18,8 @@ ~ limitations under the License. ~ ============LICENSE_END========================================================= --> -<project xmlns="http://maven.apache.org/POM/4.0.0" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" +<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/config/AaiClientConfiguration.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/config/AaiClientConfiguration.java index 50259bae..4727f4bd 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/config/AaiClientConfiguration.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/config/AaiClientConfiguration.java @@ -21,9 +21,7 @@ package org.onap.dcaegen2.services.prh.config; import java.io.Serializable; - import java.util.Map; - import org.immutables.gson.Gson; import org.immutables.value.Value; import org.springframework.stereotype.Component; diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AaiReactiveWebClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AaiReactiveWebClient.java index d79e245a..6daf54a1 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AaiReactiveWebClient.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AaiReactiveWebClient.java @@ -20,12 +20,15 @@ package org.onap.dcaegen2.services.prh.service; +import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.RESPONSE_CODE; +import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.SERVICE_NAME; import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication; import java.util.Map; import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.slf4j.MDC; import org.springframework.web.reactive.function.client.ExchangeFilterFunction; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Mono; @@ -68,6 +71,7 @@ public class AaiReactiveWebClient { private ExchangeFilterFunction logRequest() { return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> { + MDC.put(SERVICE_NAME, String.valueOf(clientRequest.url())); logger.info("Request: {} {}", clientRequest.method(), clientRequest.url()); clientRequest.headers() .forEach((name, values) -> values.forEach(value -> logger.info("{}={}", name, value))); @@ -77,6 +81,7 @@ public class AaiReactiveWebClient { private ExchangeFilterFunction logResponse() { return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> { + MDC.put(RESPONSE_CODE, String.valueOf(clientResponse.statusCode())); logger.info("Response Status {}", clientResponse.statusCode()); return Mono.just(clientResponse); }); diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClient.java index 0a6c9926..be6c63e0 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClient.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClient.java @@ -20,15 +20,20 @@ package org.onap.dcaegen2.services.prh.service.producer; +import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.REQUEST_ID; +import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.X_INVOCATION_ID; +import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.X_ONAP_REQUEST_ID; + import java.net.URI; import java.net.URISyntaxException; - +import java.util.UUID; import org.apache.http.client.utils.URIBuilder; import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration; import org.onap.dcaegen2.services.prh.exceptions.AaiRequestException; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.slf4j.MDC; import org.springframework.http.HttpStatus; import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.client.WebClient; @@ -37,12 +42,12 @@ import reactor.core.publisher.Mono; public class AaiProducerReactiveHttpClient { - private WebClient webClient; private final String aaiHost; private final String aaiProtocol; private final Integer aaiHostPortNumber; private final String aaiBasePath; private final Logger logger = LoggerFactory.getLogger(this.getClass()); + private WebClient webClient; /** @@ -78,6 +83,8 @@ public class AaiProducerReactiveHttpClient { try { return webClient.patch() .uri(getUri(dmaapModel.getSourceName())) + .header(X_ONAP_REQUEST_ID, MDC.get(REQUEST_ID)) + .header(X_INVOCATION_ID, UUID.randomUUID().toString()) .body(BodyInserters.fromObject(dmaapModel)) .retrieve() .onStatus( diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClientTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClientTest.java index c99deca5..9b0f4fe8 100644 --- a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClientTest.java +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClientTest.java @@ -45,10 +45,8 @@ import reactor.test.StepVerifier; class AaiProducerReactiveHttpClientTest { - private static AaiProducerReactiveHttpClient aaiProducerReactiveHttpClient; - private static final Integer SUCCESS_RESPONSE = 200; - + private static AaiProducerReactiveHttpClient aaiProducerReactiveHttpClient; private static AaiClientConfiguration aaiConfigurationMock = mock(AaiClientConfiguration.class); private static WebClient webClient = mock(WebClient.class); @@ -82,6 +80,14 @@ class AaiProducerReactiveHttpClientTest { responseSpec = mock(ResponseSpec.class); } + private static void setupHeaders() { + aaiHeaders = new HashMap<>(); + aaiHeaders.put("X-FromAppId", "PRH"); + aaiHeaders.put("X-TransactionId", "vv-temp"); + aaiHeaders.put("Accept", "application/json"); + aaiHeaders.put("Real-Time", "true"); + aaiHeaders.put("Content-Type", "application/merge-patch+json"); + } @Test void getAaiProducerResponse_shouldReturn200() { @@ -118,24 +124,15 @@ class AaiProducerReactiveHttpClientTest { )).expectSubscription().expectError(Exception.class).verify(); } - private void mockWebClientDependantObject() { WebClient.RequestHeadersSpec requestHeadersSpec = mock(WebClient.RequestHeadersSpec.class); when(webClient.patch()).thenReturn(requestBodyUriSpec); when(requestBodyUriSpec.uri((URI) any())).thenReturn(requestBodyUriSpec); + when(requestBodyUriSpec.header(any(), any())).thenReturn(requestBodyUriSpec); when(requestBodyUriSpec.body(any())).thenReturn(requestHeadersSpec); doReturn(responseSpec).when(requestHeadersSpec).retrieve(); doReturn(responseSpec).when(responseSpec).onStatus(any(), any()); } - private static void setupHeaders() { - aaiHeaders = new HashMap<>(); - aaiHeaders.put("X-FromAppId", "PRH"); - aaiHeaders.put("X-TransactionId", "vv-temp"); - aaiHeaders.put("Accept", "application/json"); - aaiHeaders.put("Real-Time", "true"); - aaiHeaders.put("Content-Type", "application/merge-patch+json"); - } - } diff --git a/prh-app-server/config/application.yaml b/prh-app-server/config/application.yaml index ff53a06c..390ea9d2 100644 --- a/prh-app-server/config/application.yaml +++ b/prh-app-server/config/application.yaml @@ -15,6 +15,5 @@ logging: org.springframework: ERROR org.springframework.data: ERROR org.onap.dcaegen2.services.prh: INFO - file: opt/log/application.log app: filepath: config/prh_endpoints.json
\ No newline at end of file diff --git a/prh-app-server/config/prh_endpoints.json b/prh-app-server/config/prh_endpoints.json index e2dd51a3..e5d1c7b8 100644 --- a/prh-app-server/config/prh_endpoints.json +++ b/prh-app-server/config/prh_endpoints.json @@ -35,11 +35,9 @@ "aaiBasePath": "/aai/v12", "aaiPnfPath": "/network/pnfs/pnf", "aaiHeaders": { - "X-FromAppId": "prh", - "X-TransactionId": "9999", "Accept": "application/json", "Real-Time": "true", - "Content-Type":"application/merge-patch+json" + "Content-Type": "application/merge-patch+json" } } } diff --git a/prh-app-server/pom.xml b/prh-app-server/pom.xml index ed24fdeb..daaa5acb 100644 --- a/prh-app-server/pom.xml +++ b/prh-app-server/pom.xml @@ -18,8 +18,8 @@ ~ limitations under the License. ~ ============LICENSE_END========================================================= --> -<project xmlns="http://maven.apache.org/POM/4.0.0" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" +<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>prh</artifactId> diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/CloudConfiguration.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/CloudConfiguration.java index b774f545..bc4bbf80 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/CloudConfiguration.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/CloudConfiguration.java @@ -36,10 +36,10 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import reactor.core.publisher.Flux; import reactor.core.scheduler.Schedulers; - /** * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 8/9/18 */ diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfig.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfig.java index ad040f6e..2fb61c06 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfig.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfig.java @@ -20,29 +20,30 @@ package org.onap.dcaegen2.services.prh.configuration; +import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.INVOCATION_ID; +import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.REQUEST_ID; + import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.JsonSyntaxException; import com.google.gson.TypeAdapterFactory; - import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; - import java.nio.charset.StandardCharsets; import java.util.ServiceLoader; import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotNull; - import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration; import org.onap.dcaegen2.services.prh.config.DmaapConsumerConfiguration; import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.slf4j.MDC; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Configuration; @@ -140,4 +141,11 @@ public abstract class PrhAppConfig implements Config { this.filepath = filepath; } + public void setXOnapRequestId(String xOnapRequestId) { + MDC.put(REQUEST_ID, xOnapRequestId); + } + + public void setXInvocationId(String xInvocationId) { + MDC.put(INVOCATION_ID, xInvocationId); + } }
\ No newline at end of file diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/SchedulerConfig.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/SchedulerConfig.java index a27feefb..6132a674 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/SchedulerConfig.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/SchedulerConfig.java @@ -28,6 +28,10 @@ import java.util.List; import java.util.concurrent.ScheduledFuture; import javax.annotation.PostConstruct; import org.onap.dcaegen2.services.prh.tasks.ScheduledTasks; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.Marker; +import org.slf4j.MarkerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Configuration; @@ -49,6 +53,8 @@ public class SchedulerConfig { private static final int SCHEDULING_DELAY_FOR_PRH_TASKS = 5; private static final int SCHEDULING_REQUEST_FOR_CONFIGURATION_DELAY = 5; private static volatile List<ScheduledFuture> scheduledPrhTaskFutureList = new ArrayList<>(); + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + private final Marker ENTRY = MarkerFactory.getMarker("ENRTY"); private final ConcurrentTaskScheduler taskScheduler; private final ScheduledTasks scheduledTask; @@ -88,6 +94,7 @@ public class SchedulerConfig { @PostConstruct @ApiOperation(value = "Start task if possible") public synchronized boolean tryToStartTask() { + logger.info(ENTRY,"Start scheduling PRH workflow"); if (scheduledPrhTaskFutureList.isEmpty()) { scheduledPrhTaskFutureList.add(cloudTaskScheduler .scheduleAtFixedRate(cloudConfiguration::runTask, Instant.now(), diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/SwaggerConfig.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/SwaggerConfig.java index 10bf2755..ecd2ef4c 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/SwaggerConfig.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/SwaggerConfig.java @@ -50,6 +50,7 @@ public class SwaggerConfig extends WebMvcConfigurationSupport { /** * Swagger configuration function for hosting it next to spring http website. + * * @return Docket */ @Bean diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/controllers/HeartbeatController.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/controllers/HeartbeatController.java index 0135ef76..573724d8 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/controllers/HeartbeatController.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/controllers/HeartbeatController.java @@ -54,7 +54,7 @@ public class HeartbeatController { @ApiResponse(code = 401, message = "You are not authorized to view the resource"), @ApiResponse(code = 403, message = "Accessing the resource you were trying to reach is forbidden"), @ApiResponse(code = 404, message = "The resource you were trying to reach is not found") - } + } ) public Mono<ResponseEntity<String>> heartbeat() { logger.trace("Receiving heartbeat request"); diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AaiProducerTaskImpl.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AaiProducerTaskImpl.java index 379d10c6..f5b8307b 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AaiProducerTaskImpl.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AaiProducerTaskImpl.java @@ -30,6 +30,8 @@ import org.onap.dcaegen2.services.prh.model.utils.HttpUtils; import org.onap.dcaegen2.services.prh.service.producer.AaiProducerReactiveHttpClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.slf4j.Marker; +import org.slf4j.MarkerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import reactor.core.publisher.Mono; @@ -42,6 +44,8 @@ public class AaiProducerTaskImpl extends AaiProducerTask { private final Logger logger = LoggerFactory.getLogger(this.getClass()); + private final Marker INVOKE = MarkerFactory.getMarker("INVOKE"); + private final Config config; private AaiProducerReactiveHttpClient aaiProducerReactiveHttpClient; @@ -80,7 +84,7 @@ public class AaiProducerTaskImpl extends throw new DmaapNotFoundException("Invoked null object to DMaaP task"); } aaiProducerReactiveHttpClient = resolveClient(); - logger.trace("Method called with arg {}", consumerDmaapModel); + logger.info(INVOKE, "Method called with arg {}", consumerDmaapModel); return publish(consumerDmaapModel); } diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImpl.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImpl.java index bf3acccc..9e1fadf1 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImpl.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImpl.java @@ -20,14 +20,19 @@ package org.onap.dcaegen2.services.prh.tasks; +import java.util.Map; import org.onap.dcaegen2.services.prh.config.DmaapConsumerConfiguration; import org.onap.dcaegen2.services.prh.configuration.AppConfig; import org.onap.dcaegen2.services.prh.configuration.Config; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; +import org.onap.dcaegen2.services.prh.model.logging.MDCVariables; import org.onap.dcaegen2.services.prh.service.DmaapConsumerJsonParser; import org.onap.dcaegen2.services.prh.service.consumer.DMaaPConsumerReactiveHttpClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.slf4j.MDC; +import org.slf4j.Marker; +import org.slf4j.MarkerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import reactor.core.publisher.Mono; @@ -39,6 +44,7 @@ import reactor.core.publisher.Mono; public class DmaapConsumerTaskImpl extends DmaapConsumerTask { private final Logger logger = LoggerFactory.getLogger(this.getClass()); + private final Marker INVOKE = MarkerFactory.getMarker("INVOKE"); private final Config config; private DmaapConsumerJsonParser dmaapConsumerJsonParser; private DMaaPConsumerReactiveHttpClient dmaaPConsumerReactiveHttpClient; @@ -62,8 +68,8 @@ public class DmaapConsumerTaskImpl extends DmaapConsumerTask { @Override public Mono<ConsumerDmaapModel> execute(String object) { dmaaPConsumerReactiveHttpClient = resolveClient(); - logger.trace("Method called with arg {}", object); - return consume((dmaaPConsumerReactiveHttpClient.getDMaaPConsumerResponse())); + logger.info(INVOKE, "Method called with arg {}", object); + return consume(dmaaPConsumerReactiveHttpClient.getDMaaPConsumerResponse()); } @Override diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImpl.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImpl.java index d4e1c1e3..73260381 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImpl.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImpl.java @@ -27,6 +27,8 @@ import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; import org.onap.dcaegen2.services.prh.service.producer.DMaaPProducerReactiveHttpClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.slf4j.Marker; +import org.slf4j.MarkerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import reactor.core.publisher.Mono; @@ -38,6 +40,7 @@ import reactor.core.publisher.Mono; public class DmaapPublisherTaskImpl extends DmaapPublisherTask { private final Logger logger = LoggerFactory.getLogger(this.getClass()); + private final Marker INVOKE = MarkerFactory.getMarker("INVOKE"); private final Config config; private DMaaPProducerReactiveHttpClient dmaapProducerReactiveHttpClient; @@ -61,7 +64,7 @@ public class DmaapPublisherTaskImpl extends DmaapPublisherTask { throw new DmaapNotFoundException("Invoked null object to DMaaP task"); } dmaapProducerReactiveHttpClient = resolveClient(); - logger.trace("Method called with arg {}", consumerDmaapModel); + logger.info(INVOKE, "Method called with arg {}", consumerDmaapModel); return publish(consumerDmaapModel); } diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/ScheduledTasks.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/ScheduledTasks.java index c021abe2..6432a338 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/ScheduledTasks.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/ScheduledTasks.java @@ -20,12 +20,21 @@ package org.onap.dcaegen2.services.prh.tasks; +import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.INSTANCE_UUID; +import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.RESPONSE_CODE; + +import java.util.Map; +import java.util.UUID; import java.util.concurrent.Callable; import org.onap.dcaegen2.services.prh.exceptions.DmaapEmptyResponseException; import org.onap.dcaegen2.services.prh.exceptions.PrhTaskException; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; +import org.onap.dcaegen2.services.prh.model.logging.MDCVariables; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.slf4j.MDC; +import org.slf4j.Marker; +import org.slf4j.MarkerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import reactor.core.publisher.Mono; @@ -38,10 +47,10 @@ import reactor.core.scheduler.Schedulers; public class ScheduledTasks { private final Logger logger = LoggerFactory.getLogger(this.getClass()); - private final DmaapConsumerTask dmaapConsumerTask; private final DmaapPublisherTask dmaapProducerTask; private final AaiProducerTask aaiProducerTask; + private Map<String, String> contextMap = MDC.getCopyOfContextMap(); /** * Constructor for tasks registration in PRHWorkflow. @@ -62,6 +71,7 @@ public class ScheduledTasks { * Main function for scheduling prhWorkflow. */ public void scheduleMainPrhEventTask() { + MDCVariables.setMdcContextMap(contextMap); logger.trace("Execution of tasks was registered"); Mono<String> dmaapProducerResponse = Mono.fromCallable(consumeFromDMaaPMessage()) @@ -78,6 +88,7 @@ public class ScheduledTasks { } private void onSuccess(String responseCode) { + MDC.put(RESPONSE_CODE, responseCode); logger.info("Prh consumed tasks. HTTP Response code {}", responseCode); } @@ -89,6 +100,8 @@ public class ScheduledTasks { private Callable<Mono<ConsumerDmaapModel>> consumeFromDMaaPMessage() { return () -> { + MDCVariables.setMdcContextMap(contextMap); + MDC.put(INSTANCE_UUID, UUID.randomUUID().toString()); dmaapConsumerTask.initConfigs(); return dmaapConsumerTask.execute(""); }; diff --git a/prh-app-server/src/main/resources/application.properties b/prh-app-server/src/main/resources/application.properties index fa38d188..ac0192ca 100644 --- a/prh-app-server/src/main/resources/application.properties +++ b/prh-app-server/src/main/resources/application.properties @@ -9,4 +9,6 @@ logging.level.root=ERROR logging.level.org.springframework=ERROR logging.level.org.springframework.data=ERROR logging.level.org.onap.dcaegen2.services.prh=INFO -app.filepath=config/prh_endpoints.json
\ No newline at end of file +app.filepath=config/prh_endpoints.json +app.xonaprequestid=requestID +app.xinvocationid=invocationID diff --git a/prh-app-server/src/main/resources/logback-spring.xml b/prh-app-server/src/main/resources/logback-spring.xml index 74c386b6..427373fd 100644 --- a/prh-app-server/src/main/resources/logback-spring.xml +++ b/prh-app-server/src/main/resources/logback-spring.xml @@ -1,7 +1,8 @@ <?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/defaults.xml"/> - <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/> + <property name="LOG_FILE" + value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/> <property name="FILE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN}"/> <Property name="outputFilename" value="prh-app-server_output"/> <Property name="log-path" value="/var/log/ONAP/prh/prh-app-server"/> @@ -11,7 +12,7 @@ <property name="totalSizeCap" value="10GB"/> <springProfile name="dev"> - <appender name="CONSOLE" target="SYSTEM_OUT" class="ch.qos.logback.core.ConsoleAppender"> + <appender class="ch.qos.logback.core.ConsoleAppender" name="CONSOLE" target="SYSTEM_OUT"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <Pattern>%nopexception%logger |%date{yyyy-MM-dd'T'HH:mm:ss.SSSXXX,UTC} @@ -21,10 +22,11 @@ |%replace(%replace(%rootException){'\t','\\\\t'}){'\n','\\\\n'} |%replace(%replace(%marker){'\t','\\\\t'}){'\n','\\\\n'} |%thread - |%n</Pattern> + |%n + </Pattern> </encoder> </appender> - <appender name="ROLLING-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> + <appender class="ch.qos.logback.core.rolling.RollingFileAppender" name="ROLLING-FILE"> <encoder> <pattern>%nopexception%logger |%date{yyyy-MM-dd'T'HH:mm:ss.SSSXXX,UTC} @@ -34,7 +36,8 @@ |%replace(%replace(%rootException){'\t','\\\\t'}){'\n','\\\\n'} |%replace(%replace(%marker){'\t','\\\\t'}){'\n','\\\\n'} |%thread - |%n</pattern> + |%n + </pattern> </encoder> <file>${LOG_FILE}</file> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> @@ -51,7 +54,7 @@ </springProfile> <springProfile name="prod"> - <appender name="ROLLING-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> + <appender class="ch.qos.logback.core.rolling.RollingFileAppender" name="ROLLING-FILE"> <encoder> <pattern>%nopexception%logger |%date{yyyy-MM-dd'T'HH:mm:ss.SSSXXX,UTC} @@ -61,7 +64,8 @@ |%replace(%replace(%rootException){'\t','\\\\t'}){'\n','\\\\n'} |%replace(%replace(%marker){'\t','\\\\t'}){'\n','\\\\n'} |%thread - |%n</pattern> + |%n + </pattern> </encoder> <File>${log-path}/${outputFilename}.log</File> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> diff --git a/prh-app-server/src/main/resources/scheduled-context.xml b/prh-app-server/src/main/resources/scheduled-context.xml index 91919aa2..82067c17 100644 --- a/prh-app-server/src/main/resources/scheduled-context.xml +++ b/prh-app-server/src/main/resources/scheduled-context.xml @@ -1,8 +1,8 @@ <?xml version="1.0" encoding="UTF-8"?> -<beans xmlns="http://www.springframework.org/schema/beans" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" +<beans xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" - xmlns:context="http://www.springframework.org/schema/context" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> @@ -10,7 +10,7 @@ <context:component-scan base-package="org.onap.dcaegen2.services.prh"/> <task:scheduled-tasks> - <task:scheduled ref="scheduleController" method="startTasks" - fixed-rate="1000"/> + <task:scheduled fixed-rate="1000" method="startTasks" + ref="scheduleController"/> </task:scheduled-tasks> </beans> diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/CloudConfigParserTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/CloudConfigParserTest.java index 6d74771d..dd0a3dbd 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/CloudConfigParserTest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/CloudConfigParserTest.java @@ -20,12 +20,17 @@ package org.onap.dcaegen2.services.prh.configuration; +import static org.assertj.core.api.Assertions.assertThat; + import com.google.gson.Gson; import com.google.gson.JsonObject; import org.junit.jupiter.api.Test; -import org.onap.dcaegen2.services.prh.config.*; - -import static org.assertj.core.api.Assertions.assertThat; +import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration; +import org.onap.dcaegen2.services.prh.config.DmaapConsumerConfiguration; +import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration; +import org.onap.dcaegen2.services.prh.config.ImmutableAaiClientConfiguration; +import org.onap.dcaegen2.services.prh.config.ImmutableDmaapConsumerConfiguration; +import org.onap.dcaegen2.services.prh.config.ImmutableDmaapPublisherConfiguration; public class CloudConfigParserTest { @@ -58,47 +63,48 @@ public class CloudConfigParserTest { private static final ImmutableAaiClientConfiguration correctAaiClientConfig = new ImmutableAaiClientConfiguration.Builder() - .aaiHost("aai.onap.svc.cluster.local") - .aaiPort(8443) - .aaiUserName("AAI") - .aaiPnfPath("/network/pnfs/pnf") - .aaiIgnoreSslCertificateErrors(true) - .aaiUserPassword("AAI") - .aaiProtocol("https") - .aaiBasePath("/aai/v12") - .build(); + .aaiHost("aai.onap.svc.cluster.local") + .aaiPort(8443) + .aaiUserName("AAI") + .aaiPnfPath("/network/pnfs/pnf") + .aaiIgnoreSslCertificateErrors(true) + .aaiUserPassword("AAI") + .aaiProtocol("https") + .aaiBasePath("/aai/v12") + .build(); private static final ImmutableDmaapConsumerConfiguration correctDmaapConsumerConfig = new ImmutableDmaapConsumerConfiguration.Builder() - .timeoutMs(-1) - .dmaapHostName("message-router.onap.svc.cluster.local") - .dmaapUserName("admin") - .dmaapUserPassword("admin") - .dmaapTopicName("/events/unauthenticated.SEC_OTHER_OUTPUT") - .dmaapPortNumber(3904) - .dmaapContentType("application/json") - .messageLimit(-1) - .dmaapProtocol("http") - .consumerId("c12") - .consumerGroup("OpenDCAE-c12") - .build(); + .timeoutMs(-1) + .dmaapHostName("message-router.onap.svc.cluster.local") + .dmaapUserName("admin") + .dmaapUserPassword("admin") + .dmaapTopicName("/events/unauthenticated.SEC_OTHER_OUTPUT") + .dmaapPortNumber(3904) + .dmaapContentType("application/json") + .messageLimit(-1) + .dmaapProtocol("http") + .consumerId("c12") + .consumerGroup("OpenDCAE-c12") + .build(); private static final ImmutableDmaapPublisherConfiguration correctDmaapPublisherConfig = new ImmutableDmaapPublisherConfiguration.Builder() - .dmaapTopicName("/events/unauthenticated.PNF_READY") - .dmaapUserPassword("admin") - .dmaapPortNumber(3904) - .dmaapProtocol("http") - .dmaapContentType("application/json") - .dmaapHostName("message-router.onap.svc.cluster.local") - .dmaapUserName("admin") - .build(); + .dmaapTopicName("/events/unauthenticated.PNF_READY") + .dmaapUserPassword("admin") + .dmaapPortNumber(3904) + .dmaapProtocol("http") + .dmaapContentType("application/json") + .dmaapHostName("message-router.onap.svc.cluster.local") + .dmaapUserName("admin") + .build(); - private CloudConfigParser cloudConfigParser = new CloudConfigParser(new Gson().fromJson(correctJson, JsonObject.class)); + private CloudConfigParser cloudConfigParser = new CloudConfigParser( + new Gson().fromJson(correctJson, JsonObject.class)); @Test - public void shouldCreateAaiConfigurationCorrectly(){ + public void shouldCreateAaiConfigurationCorrectly() { // when AaiClientConfiguration aaiClientConfig = cloudConfigParser.getAaiClientConfig(); @@ -109,7 +115,7 @@ public class CloudConfigParserTest { @Test - public void shouldCreateDmaapConsumerConfigurationCorrectly(){ + public void shouldCreateDmaapConsumerConfigurationCorrectly() { // when DmaapConsumerConfiguration dmaapConsumerConfig = cloudConfigParser.getDmaapConsumerConfig(); @@ -120,7 +126,7 @@ public class CloudConfigParserTest { @Test - public void shouldCreateDmaapPublisherConfigurationCorrectly(){ + public void shouldCreateDmaapPublisherConfigurationCorrectly() { // when DmaapPublisherConfiguration dmaapPublisherConfig = cloudConfigParser.getDmaapPublisherConfig(); diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/integration/junit5/mockito/MockitoExtension.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/integration/junit5/mockito/MockitoExtension.java index 5c19186b..9835e9ed 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/integration/junit5/mockito/MockitoExtension.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/integration/junit5/mockito/MockitoExtension.java @@ -23,7 +23,6 @@ package org.onap.dcaegen2.services.prh.integration.junit5.mockito; import static org.mockito.Mockito.mock; import java.lang.reflect.Parameter; - import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.ExtensionContext.Namespace; import org.junit.jupiter.api.extension.ExtensionContext.Store; @@ -36,9 +35,9 @@ import org.mockito.MockitoAnnotations; /** * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 3/27/18 * - * {@code MockitoExtension } showcases the {@link TestInstancePostProcessor} and {@link ParameterResolver} extension - * APIs of JUnit 5 by providing dependency injection support at the field level and at the method parameter level - * viaMockito 2.x's {@link Mock @Mock} annotation. + * {@code MockitoExtension } showcases the {@link TestInstancePostProcessor} and {@link ParameterResolver} extension + * APIs of JUnit 5 by providing dependency injection support at the field level and at the method parameter level + * viaMockito 2.x's {@link Mock @Mock} annotation. */ public class MockitoExtension implements TestInstancePostProcessor, ParameterResolver { diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AaiProducerTaskImplTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AaiProducerTaskImplTest.java index 4589341e..54259397 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AaiProducerTaskImplTest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AaiProducerTaskImplTest.java @@ -81,6 +81,17 @@ class AaiProducerTaskImplTest { } + private static void getAaiProducerTask_whenMockingResponseObject(Integer statusCode) { + //given + aaiProducerReactiveHttpClient = mock(AaiProducerReactiveHttpClient.class); + when(aaiProducerReactiveHttpClient.getAaiProducerResponse(any())) + .thenReturn(Mono.just(statusCode)); + when(appConfig.getAaiClientConfiguration()).thenReturn(aaiClientConfiguration); + aaiProducerTask = spy(new AaiProducerTaskImpl(appConfig)); + when(aaiProducerTask.resolveConfiguration()).thenReturn(aaiClientConfiguration); + doReturn(aaiProducerReactiveHttpClient).when(aaiProducerTask).resolveClient(); + } + @Test void whenPassedObjectDoesntFit_ThrowsPrhTaskException() { //given/when/ @@ -106,7 +117,6 @@ class AaiProducerTaskImplTest { } - @Test void whenPassedObjectFits_butIncorrectResponseReturns() throws PrhTaskException { //given/when @@ -117,15 +127,4 @@ class AaiProducerTaskImplTest { verify(aaiProducerReactiveHttpClient, times(1)).getAaiProducerResponse(any()); verifyNoMoreInteractions(aaiProducerReactiveHttpClient); } - - private static void getAaiProducerTask_whenMockingResponseObject(Integer statusCode) { - //given - aaiProducerReactiveHttpClient = mock(AaiProducerReactiveHttpClient.class); - when(aaiProducerReactiveHttpClient.getAaiProducerResponse(any())) - .thenReturn(Mono.just(statusCode)); - when(appConfig.getAaiClientConfiguration()).thenReturn(aaiClientConfiguration); - aaiProducerTask = spy(new AaiProducerTaskImpl(appConfig)); - when(aaiProducerTask.resolveConfiguration()).thenReturn(aaiClientConfiguration); - doReturn(aaiProducerReactiveHttpClient).when(aaiProducerTask).resolveClient(); - } }
\ No newline at end of file diff --git a/prh-app-server/src/test/resources/logback-test.xml b/prh-app-server/src/test/resources/logback-test.xml index 0a53315a..9c161fe3 100644 --- a/prh-app-server/src/test/resources/logback-test.xml +++ b/prh-app-server/src/test/resources/logback-test.xml @@ -18,8 +18,8 @@ --> <configuration debug="false"> <include resource="org/springframework/boot/logging/logback/base.xml"/> - <logger name="org.onap.dcaegen2.services.prh" level="DEBUG"/> - <appender name="Console" class="ch.qos.logback.core.ConsoleAppender"> + <logger level="DEBUG" name="org.onap.dcaegen2.services.prh"/> + <appender class="ch.qos.logback.core.ConsoleAppender" name="Console"> <encoder> <pattern>%d{ISO8601} - %-5p [%t:%C{1}@%L] - %m%n</pattern> </encoder> diff --git a/prh-app-server/src/test/resources/prh_endpoints.json b/prh-app-server/src/test/resources/prh_endpoints.json index e088e2ae..07e87c67 100644 --- a/prh-app-server/src/test/resources/prh_endpoints.json +++ b/prh-app-server/src/test/resources/prh_endpoints.json @@ -15,7 +15,7 @@ "X-TransactionId": "9999", "Accept": "application/json", "Real-Time": "true", - "Content-Type":"application/merge-patch+json" + "Content-Type": "application/merge-patch+json" } } }, diff --git a/prh-commons/pom.xml b/prh-commons/pom.xml index 8aa966cc..41b0a331 100644 --- a/prh-commons/pom.xml +++ b/prh-commons/pom.xml @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> -<project xmlns="http://maven.apache.org/POM/4.0.0" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" +<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> diff --git a/prh-commons/src/main/java/org/onap/dcaegen2/services/prh/model/logging/MDCVariables.java b/prh-commons/src/main/java/org/onap/dcaegen2/services/prh/model/logging/MDCVariables.java new file mode 100644 index 00000000..33d0c87d --- /dev/null +++ b/prh-commons/src/main/java/org/onap/dcaegen2/services/prh/model/logging/MDCVariables.java @@ -0,0 +1,43 @@ +/* + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.services.prh.model.logging; + +import java.util.Map; +import org.slf4j.MDC; + +public final class MDCVariables { + + public static final String X_ONAP_REQUEST_ID = "X-ONAP-RequestID"; + public static final String X_INVOCATION_ID = "X-InvocationID"; + public static final String REQUEST_ID = "RequestID"; + public static final String INVOCATION_ID = "InvocationID"; + public static final String INSTANCE_UUID = "InstanceUUID"; + public static final String RESPONSE_CODE = "ResponseCode"; + public static final String SERVICE_NAME = "ServiceName"; + + public static void setMdcContextMap(Map<String,String> mdcContextMap) { + if(mdcContextMap != null) + MDC.setContextMap(mdcContextMap); + } + + private MDCVariables() { + } +} diff --git a/prh-commons/src/main/java/org/onap/dcaegen2/services/prh/model/utils/HttpUtils.java b/prh-commons/src/main/java/org/onap/dcaegen2/services/prh/model/utils/HttpUtils.java index 11fca099..e0264eb3 100644 --- a/prh-commons/src/main/java/org/onap/dcaegen2/services/prh/model/utils/HttpUtils.java +++ b/prh-commons/src/main/java/org/onap/dcaegen2/services/prh/model/utils/HttpUtils.java @@ -24,7 +24,8 @@ import org.apache.http.HttpStatus; public final class HttpUtils implements HttpStatus { - private HttpUtils() {} + private HttpUtils() { + } public static boolean isSuccessfulResponseCode(Integer statusCode) { return statusCode >= 200 && statusCode < 300; diff --git a/prh-commons/src/test/java/org/onap/dcaegen2/services/prh/model/CommonFunctionsTest.java b/prh-commons/src/test/java/org/onap/dcaegen2/services/prh/model/CommonFunctionsTest.java index 470704a2..b3fc87a1 100644 --- a/prh-commons/src/test/java/org/onap/dcaegen2/services/prh/model/CommonFunctionsTest.java +++ b/prh-commons/src/test/java/org/onap/dcaegen2/services/prh/model/CommonFunctionsTest.java @@ -32,12 +32,11 @@ import org.junit.jupiter.api.Test; class CommonFunctionsTest { - // Given - private ConsumerDmaapModel model = new ConsumerDmaapModelForUnitTest(); - private static final HttpResponse httpResponseMock = mock(HttpResponse.class); private static final HttpEntity httpEntityMock = mock(HttpEntity.class); private static final StatusLine statusLineMock = mock(StatusLine.class); + // Given + private ConsumerDmaapModel model = new ConsumerDmaapModelForUnitTest(); @BeforeAll static void setup() { diff --git a/prh-dmaap-client/pom.xml b/prh-dmaap-client/pom.xml index 037bfe88..8e1a57e0 100644 --- a/prh-dmaap-client/pom.xml +++ b/prh-dmaap-client/pom.xml @@ -18,8 +18,8 @@ ~ limitations under the License. ~ ============LICENSE_END========================================================= --> -<project xmlns="http://maven.apache.org/POM/4.0.0" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" +<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>prh</artifactId> diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapConsumerConfiguration.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapConsumerConfiguration.java index 6663554e..287ebfd8 100644 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapConsumerConfiguration.java +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapConsumerConfiguration.java @@ -35,6 +35,10 @@ public abstract class DmaapConsumerConfiguration implements DmaapCustomConfig { private static final long serialVersionUID = 1L; + public static DmaapConsumerConfiguration.Builder builder() { + return ImmutableDmaapConsumerConfiguration.builder(); + } + @Value.Parameter public abstract String consumerId(); @@ -47,7 +51,6 @@ public abstract class DmaapConsumerConfiguration implements DmaapCustomConfig { @Value.Parameter public abstract Integer messageLimit(); - public interface Builder extends DmaapCustomConfig.Builder<DmaapConsumerConfiguration, DmaapConsumerConfiguration.Builder> { @@ -60,8 +63,4 @@ public abstract class DmaapConsumerConfiguration implements DmaapCustomConfig { Builder messageLimit(Integer messageLimit); } - public static DmaapConsumerConfiguration.Builder builder() { - return ImmutableDmaapConsumerConfiguration.builder(); - } - }
\ No newline at end of file diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapCustomConfig.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapCustomConfig.java index 6e7f538a..3c9bd99a 100644 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapCustomConfig.java +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapCustomConfig.java @@ -21,6 +21,7 @@ package org.onap.dcaegen2.services.prh.config; import java.io.Serializable; +import java.util.Map; import org.immutables.value.Value; /** @@ -49,7 +50,6 @@ public interface DmaapCustomConfig extends Serializable { @Value.Parameter String dmaapContentType(); - interface Builder<T extends DmaapCustomConfig, B extends Builder<T, B>> { B dmaapHostName(String dmaapHostName); diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapPublisherConfiguration.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapPublisherConfiguration.java index e353056f..28b1786a 100644 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapPublisherConfiguration.java +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapPublisherConfiguration.java @@ -33,12 +33,12 @@ public abstract class DmaapPublisherConfiguration implements DmaapCustomConfig { private static final long serialVersionUID = 1L; + public static DmaapPublisherConfiguration.Builder builder() { + return ImmutableDmaapPublisherConfiguration.builder(); + } + interface Builder extends DmaapCustomConfig.Builder<DmaapPublisherConfiguration, DmaapPublisherConfiguration.Builder> { } - - public static DmaapPublisherConfiguration.Builder builder() { - return ImmutableDmaapPublisherConfiguration.builder(); - } } diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/DMaaPReactiveWebClient.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/DMaaPReactiveWebClient.java index 65834b54..8ce81757 100644 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/DMaaPReactiveWebClient.java +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/DMaaPReactiveWebClient.java @@ -20,11 +20,12 @@ package org.onap.dcaegen2.services.prh.service; -import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication; - +import java.util.HashMap; +import java.util.Map; import org.onap.dcaegen2.services.prh.config.DmaapCustomConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.slf4j.MDC; import org.springframework.http.HttpHeaders; import org.springframework.web.reactive.function.client.ExchangeFilterFunction; import org.springframework.web.reactive.function.client.WebClient; @@ -37,9 +38,9 @@ public class DMaaPReactiveWebClient { private final Logger logger = LoggerFactory.getLogger(this.getClass()); - private String dmaaPContentType; private String dmaaPUserName; private String dmaaPUserPassword; + private String dmaaPContentType; /** * Creating DMaaPReactiveWebClient passing to them basic DMaaPConfig. @@ -51,6 +52,7 @@ public class DMaaPReactiveWebClient { this.dmaaPUserName = dmaapCustomConfig.dmaapUserName(); this.dmaaPUserPassword = dmaapCustomConfig.dmaapUserPassword(); this.dmaaPContentType = dmaapCustomConfig.dmaapContentType(); + return this; } @@ -69,6 +71,7 @@ public class DMaaPReactiveWebClient { private ExchangeFilterFunction logResponse() { return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> { + MDC.put("ResponseCode", String.valueOf(clientResponse.statusCode())); logger.info("Response Status {}", clientResponse.statusCode()); return Mono.just(clientResponse); }); @@ -76,6 +79,7 @@ public class DMaaPReactiveWebClient { private ExchangeFilterFunction logRequest() { return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> { + MDC.put("ServiceName", String.valueOf(clientRequest.url())); logger.info("Request: {} {}", clientRequest.method(), clientRequest.url()); clientRequest.headers() .forEach((name, values) -> values.forEach(value -> logger.info("{}={}", name, value))); diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPConsumerReactiveHttpClient.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPConsumerReactiveHttpClient.java index e04c07cf..ac13dd61 100644 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPConsumerReactiveHttpClient.java +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPConsumerReactiveHttpClient.java @@ -20,12 +20,18 @@ package org.onap.dcaegen2.services.prh.service.consumer; +import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.REQUEST_ID; +import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.X_INVOCATION_ID; +import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.X_ONAP_REQUEST_ID; + import java.net.URI; import java.net.URISyntaxException; +import java.util.UUID; import org.apache.http.client.utils.URIBuilder; import org.onap.dcaegen2.services.prh.config.DmaapConsumerConfiguration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.slf4j.MDC; import org.springframework.http.HttpStatus; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Mono; @@ -36,14 +42,13 @@ import reactor.core.publisher.Mono; public class DMaaPConsumerReactiveHttpClient { private final Logger logger = LoggerFactory.getLogger(this.getClass()); - - private WebClient webClient; private final String dmaapHostName; private final String dmaapProtocol; private final Integer dmaapPortNumber; private final String dmaapTopicName; private final String consumerGroup; private final String consumerId; + private WebClient webClient; /** * Constructor of DMaaPConsumerReactiveHttpClient. @@ -69,6 +74,8 @@ public class DMaaPConsumerReactiveHttpClient { return webClient .get() .uri(getUri()) + .header(X_ONAP_REQUEST_ID, MDC.get(REQUEST_ID)) + .header(X_INVOCATION_ID, UUID.randomUUID().toString()) .retrieve() .onStatus(HttpStatus::is4xxClientError, clientResponse -> Mono.error(new Exception("DmaaPConsumer HTTP " + clientResponse.statusCode())) diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/DMaaPProducerReactiveHttpClient.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/DMaaPProducerReactiveHttpClient.java index b06ebfdd..d049d380 100644 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/DMaaPProducerReactiveHttpClient.java +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/DMaaPProducerReactiveHttpClient.java @@ -20,13 +20,19 @@ package org.onap.dcaegen2.services.prh.service.producer; +import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.REQUEST_ID; +import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.X_INVOCATION_ID; +import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.X_ONAP_REQUEST_ID; + import java.net.URI; import java.net.URISyntaxException; +import java.util.UUID; import org.apache.http.client.utils.URIBuilder; import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.slf4j.MDC; import org.springframework.http.HttpStatus; import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.client.WebClient; @@ -38,12 +44,11 @@ import reactor.core.publisher.Mono; public class DMaaPProducerReactiveHttpClient { private final Logger logger = LoggerFactory.getLogger(this.getClass()); - - private WebClient webClient; private final String dmaapHostName; private final Integer dmaapPortNumber; private final String dmaapProtocol; private final String dmaapTopicName; + private WebClient webClient; /** * Constructor DMaaPProducerReactiveHttpClient. @@ -68,6 +73,8 @@ public class DMaaPProducerReactiveHttpClient { return webClient .post() .uri(getUri()) + .header(X_ONAP_REQUEST_ID, MDC.get(REQUEST_ID)) + .header(X_INVOCATION_ID, UUID.randomUUID().toString()) .body(BodyInserters.fromObject(consumerDmaapModelMono)) .retrieve() .onStatus(HttpStatus::is4xxClientError, clientResponse -> diff --git a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPConsumerReactiveHttpClientTest.java b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPConsumerReactiveHttpClientTest.java index b0d503af..1a237562 100644 --- a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPConsumerReactiveHttpClientTest.java +++ b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPConsumerReactiveHttpClientTest.java @@ -45,10 +45,9 @@ import reactor.test.StepVerifier; */ class DMaaPConsumerReactiveHttpClientTest { + private static final String JSON_MESSAGE = "{ \"responseFromDmaap\": \"Success\"}"; private DMaaPConsumerReactiveHttpClient dmaapConsumerReactiveHttpClient; - private DmaapConsumerConfiguration consumerConfigurationMock = mock(DmaapConsumerConfiguration.class); - private static final String JSON_MESSAGE = "{ \"responseFromDmaap\": \"Success\"}"; private Mono<String> expectedResult = Mono.empty(); private WebClient webClient; private RequestHeadersUriSpec requestHeadersSpec; @@ -114,6 +113,7 @@ class DMaaPConsumerReactiveHttpClientTest { private void mockDependantObjects() { when(webClient.get()).thenReturn(requestHeadersSpec); when(requestHeadersSpec.uri((URI) any())).thenReturn(requestHeadersSpec); + when(requestHeadersSpec.header(any(), any())).thenReturn(requestHeadersSpec); when(requestHeadersSpec.retrieve()).thenReturn(responseSpec); doReturn(responseSpec).when(responseSpec).onStatus(any(), any()); } diff --git a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/DMaaPProducerReactiveHttpClientTest.java b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/DMaaPProducerReactiveHttpClientTest.java index 538385ce..e8af8cd9 100644 --- a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/DMaaPProducerReactiveHttpClientTest.java +++ b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/DMaaPProducerReactiveHttpClientTest.java @@ -113,6 +113,7 @@ class DMaaPProducerReactiveHttpClientTest { RequestHeadersSpec requestHeadersSpec = mock(RequestHeadersSpec.class); when(webClient.post()).thenReturn(requestBodyUriSpec); when(requestBodyUriSpec.uri((URI) any())).thenReturn(requestBodyUriSpec); + when(requestBodyUriSpec.header(any(), any())).thenReturn(requestBodyUriSpec); when(requestBodyUriSpec.body(any())).thenReturn(requestHeadersSpec); doReturn(responseSpec).when(requestHeadersSpec).retrieve(); doReturn(responseSpec).when(responseSpec).onStatus(any(), any()); |