MDC fix

Change-Id: I4b12f9b9914d93ee2969b4b3cf48a47837330a6d
Issue-ID: DCAEGEN2-745
Signed-off-by: micdzied <michal.1.dziedzic@nokia.com>
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 b13f182..0dfe1f9 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
@@ -93,6 +93,7 @@
             LOGGER.info("Request: {} {}", clientRequest.method(), clientRequest.url());
             clientRequest.headers()
                 .forEach((name, values) -> values.forEach(value -> LOGGER.info("{}={}", name, value)));
+            MDC.remove(SERVICE_NAME);
             return Mono.just(clientRequest);
         });
     }
@@ -101,6 +102,7 @@
         return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
             MDC.put(RESPONSE_CODE, String.valueOf(clientResponse.statusCode()));
             LOGGER.info("Response Status {}", clientResponse.statusCode());
+            MDC.remove(RESPONSE_CODE);
             return Mono.just(clientResponse);
         });
     }
diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/MainApp.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/MainApp.java
index 96d47e3..39f2ea1 100644
--- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/MainApp.java
+++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/MainApp.java
@@ -20,6 +20,13 @@
 
 package org.onap.dcaegen2.services.prh;
 
+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 java.util.Map;
+import java.util.UUID;
+import org.slf4j.MDC;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
@@ -42,6 +49,13 @@
     }
 
     @Bean
+    Map<String, String> mdcContextMap(){
+        MDC.put(REQUEST_ID, "SampleRequestID");
+        MDC.put(INVOCATION_ID, UUID.randomUUID().toString());
+        return MDC.getCopyOfContextMap();
+    }
+
+    @Bean
     TaskScheduler concurrentTaskScheduler() {
         return new ConcurrentTaskScheduler();
     }
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 882e4d7..58f9693 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
@@ -137,12 +137,4 @@
     InputStream getInputStream(@NotNull String filepath) throws IOException {
         return new BufferedInputStream(new FileInputStream(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/tasks/AaiProducerTaskImpl.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AaiProducerTaskImpl.java
index 5bf3e40..328f389 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
@@ -45,7 +45,6 @@
 public class AaiProducerTaskImpl extends
     AaiProducerTask {
 
-    private static final Marker INVOKE = MarkerFactory.getMarker("INVOKE");
     private static final Logger LOGGER = LoggerFactory.getLogger(AaiProducerTaskImpl.class);
 
     private final Config config;
@@ -86,7 +85,7 @@
             throw new DmaapNotFoundException("Invoked null object to DMaaP task");
         }
         aaiProducerReactiveHttpClient = resolveClient();
-        LOGGER.debug(INVOKE, "Method called with arg {}", consumerDmaapModel);
+        LOGGER.debug("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 0d4be08..b05ec9f 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
@@ -40,8 +40,6 @@
 @Component
 public class DmaapConsumerTaskImpl extends DmaapConsumerTask {
 
-    private static final Marker INVOKE = MarkerFactory.getMarker("INVOKE");
-
     private static final Logger LOGGER = LoggerFactory.getLogger(DmaapConsumerTaskImpl.class);
     private final Config config;
     private DmaapConsumerJsonParser dmaapConsumerJsonParser;
@@ -65,7 +63,7 @@
     @Override
     public Mono<ConsumerDmaapModel> execute(String object) {
         DMaaPConsumerReactiveHttpClient dmaaPConsumerReactiveHttpClient = resolveClient();
-        LOGGER.debug(INVOKE, "Method called with arg {}", object);
+        LOGGER.debug("Method called with arg {}", object);
         return consume(dmaaPConsumerReactiveHttpClient.getDMaaPConsumerResponse());
     }
 
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 733b865..d7e6a32 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
@@ -42,7 +42,6 @@
 public class DmaapPublisherTaskImpl extends DmaapPublisherTask {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(DmaapPublisherTaskImpl.class);
-    private static final Marker INVOKE = MarkerFactory.getMarker("INVOKE");
     private final Config config;
     private DMaaPProducerReactiveHttpClient dmaapProducerReactiveHttpClient;
 
@@ -62,7 +61,7 @@
             throw new DmaapNotFoundException("Invoked null object to DMaaP task");
         }
         dmaapProducerReactiveHttpClient = resolveClient();
-        LOGGER.info(INVOKE, "Method called with arg {}", consumerDmaapModel);
+        LOGGER.info("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 92d810f..862c1f1 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
@@ -34,6 +34,8 @@
 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.http.ResponseEntity;
 import org.springframework.stereotype.Component;
@@ -46,11 +48,11 @@
 public class ScheduledTasks {
 
     private static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class);
-
+    private final Marker INVOKE = MarkerFactory.getMarker("INVOKE");
     private final DmaapConsumerTask dmaapConsumerTask;
     private final DmaapPublisherTask dmaapProducerTask;
     private final AaiProducerTask aaiProducerTask;
-    private Map<String, String> contextMap = MDC.getCopyOfContextMap();
+    private Map<String, String> mdcContextMap;
 
     /**
      * Constructor for tasks registration in PRHWorkflow.
@@ -61,17 +63,18 @@
      */
     @Autowired
     public ScheduledTasks(DmaapConsumerTask dmaapConsumerTask, DmaapPublisherTask dmaapPublisherTask,
-        AaiProducerTask aaiPublisherTask) {
+        AaiProducerTask aaiPublisherTask, Map<String, String> mdcContextMap) {
         this.dmaapConsumerTask = dmaapConsumerTask;
         this.dmaapProducerTask = dmaapPublisherTask;
         this.aaiProducerTask = aaiPublisherTask;
+        this.mdcContextMap = mdcContextMap;
     }
 
     /**
      * Main function for scheduling prhWorkflow.
      */
     public void scheduleMainPrhEventTask() {
-        MdcVariables.setMdcContextMap(contextMap);
+        MdcVariables.setMdcContextMap(mdcContextMap);
         try {
             logger.trace("Execution of tasks was registered");
             CountDownLatch mainCountDownLatch = new CountDownLatch(1);
@@ -100,6 +103,7 @@
         MDC.put(RESPONSE_CODE, responseCode.getStatusCode().toString());
         logger.info("Prh consumed tasks successfully. HTTP Response code from DMaaPProducer {}",
             responseCode.getStatusCode().value());
+        MDC.remove(RESPONSE_CODE);
     }
 
     private void onError(Throwable throwable) {
@@ -111,9 +115,9 @@
 
     private Mono<ConsumerDmaapModel> consumeFromDMaaPMessage() {
         return Mono.defer(() -> {
-            MdcVariables.setMdcContextMap(contextMap);
+            MdcVariables.setMdcContextMap(mdcContextMap);
             MDC.put(INSTANCE_UUID, UUID.randomUUID().toString());
-            logger.info("Init configs");
+            logger.info(INVOKE, "Init configs");
             dmaapConsumerTask.initConfigs();
             return dmaapConsumerTask.execute("");
         });
diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/ScheduleControllerSpy.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/ScheduleControllerSpy.java
index 28692af..5aa63e0 100644
--- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/ScheduleControllerSpy.java
+++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/ScheduleControllerSpy.java
@@ -22,6 +22,7 @@
 
 import static org.mockito.Mockito.spy;
 
+import java.util.Map;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
@@ -42,9 +43,12 @@
     @Autowired
     private AaiProducerTask aaiPublisherTaskImplSpy;
 
+    @Autowired
+    private Map<String, String> mdcContextMap;
+
     @Bean
     @Primary
     public ScheduledTasks registerSimpleScheduledTask() {
-        return spy(new ScheduledTasks(dmaapConsumerTaskImplSpy, dmaapPublisherTaskImplSpy, aaiPublisherTaskImplSpy));
+        return spy(new ScheduledTasks(dmaapConsumerTaskImplSpy, dmaapPublisherTaskImplSpy, aaiPublisherTaskImplSpy, mdcContextMap));
     }
 }
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 d34728c..242a425 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,6 +20,9 @@
 
 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 org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.MDC;
@@ -48,18 +51,20 @@
 
     private ExchangeFilterFunction logResponse() {
         return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
-            MDC.put("ResponseCode", String.valueOf(clientResponse.statusCode()));
+            MDC.put(RESPONSE_CODE, String.valueOf(clientResponse.statusCode()));
             logger.info("Response Status {}", clientResponse.statusCode());
+            MDC.remove(RESPONSE_CODE);
             return Mono.just(clientResponse);
         });
     }
 
     private ExchangeFilterFunction logRequest() {
         return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
-            MDC.put("ServiceName", String.valueOf(clientRequest.url()));
+            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)));
+            MDC.remove(SERVICE_NAME);
             return Mono.just(clientRequest);
         });
     }