aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/main/java/org/onap/vid/utils
diff options
context:
space:
mode:
authorIttay Stern <ittay.stern@att.com>2019-11-06 14:27:53 +0200
committerIttay Stern <ittay.stern@att.com>2019-11-07 14:19:47 +0000
commit97d42f7035ade2c171c349d01cfcd9b61b71dd52 (patch)
tree1a1b6d7c52545fcf78a0eb94eb12ec6d225c5d58 /vid-app-common/src/main/java/org/onap/vid/utils
parentebef5249cb9d282ace976feac8517a7b44add450 (diff)
fix - when retrieve topology we are using threadPool and the MDC values are not updated
This time for homing-by-vnf-id parallel requests. Issue-ID: VID-253 Change-Id: Ic0be1470445c6fef0e18f9b9d8f1604df508265b Signed-off-by: Ittay Stern <ittay.stern@att.com>
Diffstat (limited to 'vid-app-common/src/main/java/org/onap/vid/utils')
-rw-r--r--vid-app-common/src/main/java/org/onap/vid/utils/Logging.java29
-rw-r--r--vid-app-common/src/main/java/org/onap/vid/utils/Unchecked.java19
2 files changed, 46 insertions, 2 deletions
diff --git a/vid-app-common/src/main/java/org/onap/vid/utils/Logging.java b/vid-app-common/src/main/java/org/onap/vid/utils/Logging.java
index 0d8e58878..43f059d54 100644
--- a/vid-app-common/src/main/java/org/onap/vid/utils/Logging.java
+++ b/vid-app-common/src/main/java/org/onap/vid/utils/Logging.java
@@ -33,8 +33,11 @@ import com.google.common.collect.ImmutableList;
import io.joshworks.restclient.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
+import java.util.Map;
import java.util.Optional;
import java.util.UUID;
+import java.util.concurrent.Callable;
+import java.util.function.Function;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Response;
import org.apache.commons.io.IOUtils;
@@ -42,6 +45,8 @@ import org.apache.commons.lang3.StringUtils;
import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.onap.portalsdk.core.util.SystemProperties;
import org.onap.vid.exceptions.GenericUncheckedException;
+import org.onap.vid.utils.Unchecked.UncheckedThrowingSupplier;
+import org.slf4j.MDC;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
@@ -197,5 +202,29 @@ public class Logging {
}
}
+ /**
+ * in order to be able to write the correct data while creating the node on a new thread save a copy of the current
+ * thread's context map, with keys and values of type String.
+ */
+ public <T> Callable<T> withMDC(Map<String, String> copyOfParentMDC, Callable<T> callable) {
+ return () -> withMDCInternal(copyOfParentMDC, callable::call);
+ }
+
+ /**
+ * in order to be able to write the correct data while creating the node on a new thread save a copy of the current
+ * thread's context map, with keys and values of type String.
+ */
+ public <T, U> Function<T, U> withMDC(Map<String, String> copyOfParentMDC, Function<T, U> function) {
+ return t -> withMDCInternal(copyOfParentMDC, () -> function.apply(t));
+ }
+
+ <T> T withMDCInternal(Map<String, String> copyOfParentMDC, UncheckedThrowingSupplier<T> supplier) {
+ try {
+ MDC.setContextMap(copyOfParentMDC);
+ return supplier.get();
+ } finally {
+ MDC.clear();
+ }
+ }
}
diff --git a/vid-app-common/src/main/java/org/onap/vid/utils/Unchecked.java b/vid-app-common/src/main/java/org/onap/vid/utils/Unchecked.java
index 23127b61a..9fb15f690 100644
--- a/vid-app-common/src/main/java/org/onap/vid/utils/Unchecked.java
+++ b/vid-app-common/src/main/java/org/onap/vid/utils/Unchecked.java
@@ -20,10 +20,11 @@
package org.onap.vid.utils;
-import org.onap.vid.exceptions.GenericUncheckedException;
-
import java.net.URI;
import java.net.URISyntaxException;
+import java.util.function.Supplier;
+import org.apache.commons.lang3.exception.ExceptionUtils;
+import org.onap.vid.exceptions.GenericUncheckedException;
public class Unchecked {
private Unchecked() {
@@ -39,5 +40,19 @@ public class Unchecked {
}
}
+ @FunctionalInterface
+ public interface UncheckedThrowingSupplier<T> extends Supplier<T> {
+
+ @Override
+ default T get() {
+ try {
+ return getThrows();
+ } catch (Exception e) {
+ return ExceptionUtils.rethrow(e);
+ }
+ }
+
+ T getThrows() throws Exception;
+ }
}