Merge "fix - when retrieve topology we are using threadPool and the MDC values are...
authorIttay Stern <ittay.stern@att.com>
Mon, 4 Nov 2019 12:27:11 +0000 (12:27 +0000)
committerGerrit Code Review <gerrit@onap.org>
Mon, 4 Nov 2019 12:27:11 +0000 (12:27 +0000)
vid-app-common/src/main/java/org/onap/vid/services/AAITreeNodeBuilder.java
vid-automation/src/test/java/org/onap/vid/api/ServiceTreeApiTest.java
vid-automation/src/test/java/org/onap/vid/more/LoggerFormatTest.java

index d53eba8..9a5e748 100644 (file)
@@ -42,6 +42,7 @@ import org.onap.vid.properties.VidProperties;
 import org.onap.vid.utils.Streams;
 import org.onap.vid.utils.Tree;
 import org.onap.vid.utils.Unchecked;
+import org.slf4j.MDC;
 import org.springframework.http.HttpMethod;
 import org.springframework.stereotype.Component;
 
@@ -256,24 +257,33 @@ public class AAITreeNodeBuilder {
 
         if (!relationships.isEmpty()) {
             List<Callable<List<AAITreeNode>>> tasks = relationships.stream()
-                    .map(relationship ->
-                            (Callable<List<AAITreeNode>>) () ->
-                                    getChildNode(threadPool, nodesAccumulator, relationship.getRelatedTo(),
-                                            relationship.getRelatedLink(), pathsTree))
-                    .collect(Collectors.toList());
+                .map(relationship ->
+                    withCopyOfMDC(() -> getChildNode(threadPool, nodesAccumulator, relationship.getRelatedTo(),
+                            relationship.getRelatedLink(), pathsTree)))
+                .collect(Collectors.toList());
 
             try {
                 int depth = pathsTree.getChildrenDepth();
                 threadPool.invokeAll(tasks, timeout * depth, TimeUnit.SECONDS)
-                        .forEach(future ->
-                                addChildren(node, future)
-                        );
+                    .forEach(future ->
+                        addChildren(node, future)
+                    );
             } catch (Exception e) {
                 throw new GenericUncheckedException(e);
             }
         }
     }
 
+    private <V> Callable<V> withCopyOfMDC(Callable<V> callable) {
+        //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.
+        final Map<String, String> copyOfParentMDC = MDC.getCopyOfContextMap();
+        return () -> {
+            MDC.setContextMap(copyOfParentMDC);
+            return callable.call();
+        };
+    }
+
     private List<AAITreeNode> getChildNode(ExecutorService threadPool, ConcurrentSkipListSet<AAITreeNode> nodesAccumulator,
                                            String childNodeType, String childNodeUrl,
                                            Tree<AAIServiceTree.AaiRelationship> pathsTree) {
index e5a2a73..82cc720 100644 (file)
@@ -31,6 +31,7 @@ import org.onap.simulator.presetGenerator.presets.aai.PresetAAIStandardQueryGet;
 import org.onap.simulator.presetGenerator.presets.ecompportal_att.PresetGetSessionSlotCheckIntervalGet;
 import org.onap.simulator.presetGenerator.presets.sdc.PresetSDCGetServiceMetadataGet;
 import org.onap.simulator.presetGenerator.presets.sdc.PresetSDCGetServiceToscaModelGet;
+import org.onap.vid.more.LoggerFormatTest;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.testng.ITestResult;
@@ -150,13 +151,15 @@ public class ServiceTreeApiTest extends BaseApiTest {
                         new PresetAAIGetCloudRegionFromVnf(vnfPreset1.getInstanceId()),
                         new PresetAAIGetCloudRegionFromVnf(vnfPreset2.getInstanceId()),
                         new PresetAAIGetCloudRegionFromVnf(vnfPreset3.getInstanceId()),
-                        new PresetAAIGetCloudRegionFromVnf(vnfPreset4.getInstanceId())
+                        new PresetAAIGetCloudRegionFromVnf(vnfPreset4.getInstanceId()),
+                        new PresetAAIGetSubscribersGet()
                 ), CLEAR_THEN_SET);
 
         String api_url = "aai_search_group_members?subscriberId={subscriberId}&serviceType={serviceType}&serviceInvariantId={serviceInvariantId}" +
                 "&groupType={groupType}&groupRole={groupRole}";
 
-        final String response = restTemplate.getForObject(buildUri(api_url), String.class, "global-customer-id", "service-instance-type", "24632e6b-584b-4f45-80d4-fefd75fd9f14", "LOAD-GROUP", "SERVICE-ACCESS");
+        final ResponseEntity<String> responseEntity = restTemplate.getForEntity(buildUri(api_url), String.class, "global-customer-id", "service-instance-type", "24632e6b-584b-4f45-80d4-fefd75fd9f14", "LOAD-GROUP", "SERVICE-ACCESS");
+        String response = responseEntity.getBody();
 
         LOGGER.info(response);
 
@@ -176,6 +179,8 @@ public class ServiceTreeApiTest extends BaseApiTest {
                 .replace("VNF4_INSTANCE_TYPE", vnfPreset4.getInstanceType());
 
         assertJsonEquals(response, expected);
+        final String requestId = responseEntity.getHeaders().getFirst("X-ECOMP-RequestID-echo");
+        LoggerFormatTest.assertHeadersAndMetricLogs(restTemplate, uri, requestId,  "/network/generic-vnfs/generic-vnf/", 5);
     }
 
     @Test
index 6638a0e..a233f25 100644 (file)
@@ -167,7 +167,7 @@ public class LoggerFormatTest extends BaseApiTest {
     }
 
     public static void assertIdsInMetricsLog(List<String> logLines, String requestId, String invocationId) {
-        assertThat("request id and invocation id must be found in exactly two rows",
+        assertThat("request id and invocation id must be found in exactly two rows in: \n" + String.join("\n", logLines),
             logLines,
             containsInRelativeOrder(
                 allOf(