use onap logging 1.6.1 with needed workarounds 74/97674/7
authorEylon Malin <eylon.malin@intl.att.com>
Tue, 29 Oct 2019 13:01:17 +0000 (15:01 +0200)
committerEylon Malin <eylon.malin@intl.att.com>
Wed, 30 Oct 2019 05:47:32 +0000 (07:47 +0200)
Issue-ID: VID-253

Onap logging 1.6.1 has some mismatches regards invocationId.
This commit fix these mismatches by fixing the MDC when needed.

Change-Id: Ic8d35318ae511667dfa6f9c4297d562fd976a717
Signed-off-by: Eylon Malin <eylon.malin@intl.att.com>
13 files changed:
epsdk-app-onap/src/main/java/org/onap/portalapp/conf/ExternalAppInitializer.java
vid-app-common/pom.xml
vid-app-common/src/main/java/org/onap/vid/controller/WebConfig.java
vid-app-common/src/main/java/org/onap/vid/logging/ApacheClientMetricInterceptor.java
vid-app-common/src/main/java/org/onap/vid/logging/LoggingFilterHelper.java [new file with mode: 0644]
vid-app-common/src/main/java/org/onap/vid/logging/VidLoggingInterceptor.java [new file with mode: 0644]
vid-app-common/src/main/java/org/onap/vid/logging/VidMetricLogClientFilter.java [new file with mode: 0644]
vid-app-common/src/main/java/org/onap/vid/mso/RestMsoImplementation.java
vid-app-common/src/test/java/org/onap/vid/logging/ApacheClientMetricInterceptorTest.java
vid-app-common/src/test/java/org/onap/vid/logging/VidLoggingInterceptorTest.java [new file with mode: 0644]
vid-app-common/src/test/java/org/onap/vid/logging/VidMetricLogClientFilterTest.java [new file with mode: 0644]
vid-automation/src/main/java/org/onap/simulator/presetGenerator/presets/BasePresets/BaseSDCPreset.java
vid-automation/src/test/java/org/onap/vid/more/LoggerFormatTest.java

index e4386c1..0a4e87e 100644 (file)
@@ -44,6 +44,7 @@ import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 import org.onap.logging.filter.base.AbstractAuditLogFilter;
 import org.onap.logging.filter.base.AbstractMetricLogFilter;
+import org.onap.logging.ref.slf4j.ONAPLogConstants;
 import org.onap.portalsdk.core.conf.AppInitializer;
 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
 import org.slf4j.LoggerFactory;
@@ -70,8 +71,8 @@ public class ExternalAppInitializer extends AppInitializer {
 
        //mark in logs that vid is started.
        private void logVidIsStarted() {
-               LoggerFactory.getLogger(AbstractMetricLogFilter.class).info(VID_IS_STARTED);
-               LoggerFactory.getLogger(AbstractAuditLogFilter.class).info(VID_IS_STARTED);
+               LoggerFactory.getLogger(AbstractMetricLogFilter.class).info(ONAPLogConstants.Markers.INVOKE, VID_IS_STARTED);
+               LoggerFactory.getLogger(AbstractAuditLogFilter.class).info(ONAPLogConstants.Markers.ENTRY, VID_IS_STARTED);
        }
 
        private void setPartnerName() {
index fc267a6..9e5f58d 100755 (executable)
@@ -34,7 +34,7 @@
         <hibernate.version>4.3.11.Final</hibernate.version>
         <jackson.version>2.9.9</jackson.version>
         <jackson.databind.version>2.9.9.3</jackson.databind.version>
-        <onap.logging.version>1.5.1</onap.logging.version>
+        <onap.logging.version>1.6.1</onap.logging.version>
         <jersey.version>2.29</jersey.version>
         <surefire.version>2.22.1</surefire.version>
         <selenium.version>3.141.59</selenium.version>
index 6c5595c..91fb942 100644 (file)
@@ -29,7 +29,6 @@ import java.io.File;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import javax.servlet.ServletContext;
-import org.onap.logging.filter.spring.LoggingInterceptor;
 import org.onap.portalsdk.core.util.SystemProperties;
 import org.onap.vid.aai.AaiClient;
 import org.onap.vid.aai.AaiClientInterface;
@@ -53,6 +52,7 @@ import org.onap.vid.asdc.parser.ToscaParserImpl2;
 import org.onap.vid.asdc.parser.VidNotionsBuilder;
 import org.onap.vid.asdc.rest.SdcRestClient;
 import org.onap.vid.client.SyncRestClient;
+import org.onap.vid.logging.VidLoggingInterceptor;
 import org.onap.vid.properties.AsdcClientConfiguration;
 import org.onap.vid.properties.VidProperties;
 import org.onap.vid.scheduler.SchedulerService;
@@ -222,6 +222,6 @@ public class WebConfig implements WebMvcConfigurer {
 
     @Override
     public void addInterceptors(InterceptorRegistry registry) {
-        registry.addInterceptor(new LoggingInterceptor());
+        registry.addInterceptor(new VidLoggingInterceptor());
     }
 }
index 51e6844..6e125b4 100644 (file)
@@ -25,6 +25,7 @@ import org.apache.http.HttpMessage;
 import org.apache.http.HttpRequest;
 import org.apache.http.HttpResponse;
 import org.onap.logging.filter.base.AbstractMetricLogFilter;
+import org.onap.logging.ref.slf4j.ONAPLogConstants;
 
 
 public abstract class ApacheClientMetricInterceptor extends AbstractMetricLogFilter<HttpRequest, HttpResponse, HttpMessage> {
@@ -59,4 +60,11 @@ public abstract class ApacheClientMetricInterceptor extends AbstractMetricLogFil
         //fallback to default value that provided by AbstractMetricLogFilter
         return null;
     }
+
+    @Override
+    protected void additionalPre(HttpRequest request, HttpMessage message) {
+        LoggingFilterHelper.updateInvocationIDInMdcWithHeaderValue(
+            ()->message.getFirstHeader(ONAPLogConstants.Headers.INVOCATION_ID).getValue());
+    }
+
 }
diff --git a/vid-app-common/src/main/java/org/onap/vid/logging/LoggingFilterHelper.java b/vid-app-common/src/main/java/org/onap/vid/logging/LoggingFilterHelper.java
new file mode 100644 (file)
index 0000000..1447e82
--- /dev/null
@@ -0,0 +1,49 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * VID
+ * ================================================================================
+ * Copyright (C) 2017 - 2019 AT&T 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.vid.logging;
+
+import java.util.function.Supplier;
+import org.onap.logging.filter.base.AbstractMetricLogFilter;
+import org.onap.logging.ref.slf4j.ONAPLogConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.slf4j.MDC;
+
+public class LoggingFilterHelper {
+
+    protected static final Logger logger = LoggerFactory.getLogger(AbstractMetricLogFilter.class);
+
+    /**
+     * set invocationId in MDC with actual header value,
+     * due to bug in AbstractMetricLogFilter
+     *
+     * @param headerSupplier - return the InvocationId header value
+     */
+    static void updateInvocationIDInMdcWithHeaderValue(Supplier<String> headerSupplier) {
+        try {
+            String invocationId = headerSupplier.get();
+            MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId);
+        }
+        catch (Exception e) {
+            logger.debug("Failed to retrieve "+ONAPLogConstants.Headers.INVOCATION_ID+" header", e);
+        }
+    }
+}
diff --git a/vid-app-common/src/main/java/org/onap/vid/logging/VidLoggingInterceptor.java b/vid-app-common/src/main/java/org/onap/vid/logging/VidLoggingInterceptor.java
new file mode 100644 (file)
index 0000000..cdeb207
--- /dev/null
@@ -0,0 +1,46 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * VID
+ * ================================================================================
+ * Copyright (C) 2017 - 2019 AT&T 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.vid.logging;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import org.onap.logging.filter.spring.LoggingInterceptor;
+import org.onap.logging.ref.slf4j.ONAPLogConstants;
+import org.slf4j.MDC;
+import org.springframework.web.servlet.ModelAndView;
+
+public class VidLoggingInterceptor extends LoggingInterceptor {
+
+    static final String INBOUND_INVO_ID = "InboundInvoId";
+
+    @Override
+    protected void additionalPreHandling(HttpServletRequest request)  {
+        super.additionalPreHandling(request);
+        MDC.put(INBOUND_INVO_ID, MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID));
+    }
+
+    @Override
+    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
+        MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, MDC.get(INBOUND_INVO_ID));
+        super.postHandle(request, response, handler, modelAndView);
+    }
+
+}
diff --git a/vid-app-common/src/main/java/org/onap/vid/logging/VidMetricLogClientFilter.java b/vid-app-common/src/main/java/org/onap/vid/logging/VidMetricLogClientFilter.java
new file mode 100644 (file)
index 0000000..6b9222e
--- /dev/null
@@ -0,0 +1,38 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * VID
+ * ================================================================================
+ * Copyright (C) 2017 - 2019 AT&T 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.vid.logging;
+
+import static org.onap.vid.logging.LoggingFilterHelper.updateInvocationIDInMdcWithHeaderValue;
+
+import javax.ws.rs.client.ClientRequestContext;
+import javax.ws.rs.core.MultivaluedMap;
+import org.onap.logging.filter.base.MetricLogClientFilter;
+import org.onap.logging.ref.slf4j.ONAPLogConstants;
+
+public class VidMetricLogClientFilter extends MetricLogClientFilter {
+
+    @Override
+    protected void additionalPre(ClientRequestContext clientRequestContext, MultivaluedMap<String, Object> stringObjectMultivaluedMap) {
+        updateInvocationIDInMdcWithHeaderValue(
+            ()->(String)stringObjectMultivaluedMap.getFirst(ONAPLogConstants.Headers.INVOCATION_ID)
+        );
+    }
+}
index 0585a47..a2d71d9 100644 (file)
@@ -36,11 +36,11 @@ import javax.ws.rs.core.Response;
 import org.apache.commons.codec.binary.Base64;
 import org.eclipse.jetty.util.security.Password;
 import org.glassfish.jersey.client.ClientProperties;
-import org.onap.logging.filter.base.MetricLogClientFilter;
 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
 import org.onap.vid.aai.util.HttpClientMode;
 import org.onap.vid.aai.util.HttpsAuthClient;
 import org.onap.vid.client.HttpBasicClient;
+import org.onap.vid.logging.VidMetricLogClientFilter;
 import org.onap.vid.utils.Logging;
 import org.onap.vid.utils.SystemPropertiesWrapper;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -131,7 +131,7 @@ public class RestMsoImplementation {
     }
 
     private void registerClientToMetricLogClientFilter(Client client) {
-        MetricLogClientFilter metricLogClientFilter = new MetricLogClientFilter();
+        VidMetricLogClientFilter metricLogClientFilter = new VidMetricLogClientFilter();
         client.register(metricLogClientFilter);
     }
 
index 43f9865..0463581 100644 (file)
@@ -27,6 +27,8 @@ import org.apache.http.HttpResponse;
 import org.apache.http.ProtocolVersion;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.message.BasicHttpResponse;
+import org.onap.logging.ref.slf4j.ONAPLogConstants;
+import org.slf4j.MDC;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
@@ -42,6 +44,7 @@ public class ApacheClientMetricInterceptorTest {
         interceptor = new ApacheClientMetricInterceptor() {};
         request = new HttpGet(path);
         response = new BasicHttpResponse(new ProtocolVersion("a",1,2), 200, "ok");
+        MDC.clear();
     }
 
     @Test
@@ -74,4 +77,17 @@ public class ApacheClientMetricInterceptorTest {
     public void testGetTargetEntity() {
         assertNull(interceptor.getTargetEntity(request));
     }
+
+    @Test
+    protected void testAdditionalPre() {
+        request.addHeader(ONAPLogConstants.Headers.INVOCATION_ID, "123");
+        interceptor.additionalPre(request, request);
+        assertEquals(MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID), "123");
+    }
+
+    @Test
+    protected void whenThereIsNoInvocationIdHeader_thenMdcValueIsNull() {
+        interceptor.additionalPre(request, request);
+        assertNull(MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID));
+    }
 }
diff --git a/vid-app-common/src/test/java/org/onap/vid/logging/VidLoggingInterceptorTest.java b/vid-app-common/src/test/java/org/onap/vid/logging/VidLoggingInterceptorTest.java
new file mode 100644 (file)
index 0000000..fc78ed7
--- /dev/null
@@ -0,0 +1,48 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * VID
+ * ================================================================================
+ * Copyright (C) 2017 - 2019 AT&T 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.vid.logging;
+
+import static org.onap.vid.logging.VidLoggingInterceptor.INBOUND_INVO_ID;
+import static org.testng.Assert.assertEquals;
+
+import org.onap.logging.ref.slf4j.ONAPLogConstants;
+import org.slf4j.MDC;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+public class VidLoggingInterceptorTest {
+
+    private VidLoggingInterceptor interceptor;
+
+    @BeforeMethod
+    public void setup() {
+        interceptor = new VidLoggingInterceptor();
+        MDC.clear();
+    }
+
+    @Test
+    public void testAdditionalPreHandling() {
+        MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, "987");
+        interceptor.additionalPreHandling(null);
+        assertEquals(MDC.get(INBOUND_INVO_ID), "987");
+    }
+
+}
diff --git a/vid-app-common/src/test/java/org/onap/vid/logging/VidMetricLogClientFilterTest.java b/vid-app-common/src/test/java/org/onap/vid/logging/VidMetricLogClientFilterTest.java
new file mode 100644 (file)
index 0000000..aaf8eaf
--- /dev/null
@@ -0,0 +1,49 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * VID
+ * ================================================================================
+ * Copyright (C) 2017 - 2019 AT&T 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.vid.logging;
+
+import static org.testng.Assert.assertEquals;
+
+import javax.ws.rs.core.MultivaluedHashMap;
+import javax.ws.rs.core.MultivaluedMap;
+import org.onap.logging.ref.slf4j.ONAPLogConstants;
+import org.slf4j.MDC;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+public class VidMetricLogClientFilterTest {
+
+    VidMetricLogClientFilter metricLogClientFilter;
+
+    @BeforeMethod
+    public void setup() {
+        this.metricLogClientFilter = new VidMetricLogClientFilter();
+        MDC.clear();
+    }
+
+    @Test
+    public void testAdditionalPre() {
+        MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
+        headers.add(ONAPLogConstants.Headers.INVOCATION_ID, "xyz");
+        metricLogClientFilter.additionalPre(null, headers);
+        assertEquals(MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID), "xyz");
+    }
+}
index ba35299..4a1da9e 100644 (file)
@@ -15,7 +15,7 @@ public abstract class BaseSDCPreset extends BasePreset {
     public Map<String, String> getRequestHeaders() {
         Map<String, String> map = super.getRequestHeaders();
         map.put("X-ONAP-PartnerName", "VID.VID");
-        map.put("X-ONAP-InvocationID", "[0-9a-fA-F]{8}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{12}");
+        map.put("X-InvocationID", "[0-9a-fA-F]{8}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{12}");
         map.put("X-ONAP-RequestID", "[0-9a-fA-F]{8}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{12}");
         return map;
     }
index 50c3db8..11a6932 100644 (file)
@@ -145,7 +145,7 @@ public class LoggerFormatTest extends BaseApiTest {
 
         underTestRequests.forEach(request->{
 
-            List<String> invocationIds = request.headers.get("X-ONAP-InvocationID");
+            List<String> invocationIds = request.headers.get("X-InvocationID");
             assertThat(invocationIds, hasSize(1));
 
             String invocationId = invocationIds.get(0);