missing logs
[ccsdk/apps.git] / services / src / main / java / org / onap / ccsdk / apps / filters / AuditLogFilter.java
diff --git a/services/src/main/java/org/onap/ccsdk/apps/filters/AuditLogFilter.java b/services/src/main/java/org/onap/ccsdk/apps/filters/AuditLogFilter.java
new file mode 100644 (file)
index 0000000..b6d52c5
--- /dev/null
@@ -0,0 +1,43 @@
+package org.onap.ccsdk.apps.filters
+;
+
+import javax.servlet.http.HttpServletRequest;
+import org.onap.logging.filter.base.AuditLogServletFilter;
+import org.onap.logging.ref.slf4j.ONAPLogConstants;
+import org.slf4j.MDC;
+import org.springframework.stereotype.Component;
+
+@Component
+public class AuditLogFilter extends AuditLogServletFilter {
+    private static final String MDC_HTTP_METHOD_KEY = "HttpMethod";
+
+    @Override
+    protected void additionalPreHandling(HttpServletRequest httpServletRequest) {
+        // Don't overwrite service instance id if it was set outside of this automated method
+        if (MDC.get(ONAPLogConstants.MDCs.SERVICE_INSTANCE_ID) == null) {
+            String serviceInstanceId = getServiceInstanceId(httpServletRequest.getRequestURI());
+            if (serviceInstanceId != null) {
+                MDC.put(ONAPLogConstants.MDCs.SERVICE_INSTANCE_ID, serviceInstanceId);
+            }
+        }
+        MDC.put(MDC_HTTP_METHOD_KEY, httpServletRequest.getMethod());
+    }
+
+    // restconf URLs follow a pattern, this method attempts to extract the service instance id according to that pattern
+    protected String getServiceInstanceId(String path) {
+        int idx = path.indexOf("service-list");
+        if (idx != -1) {
+            // chomp off service-list/
+            String str = path.substring(idx + 13);
+            idx = str.indexOf("/");
+            //if there is another forward slash with more information chomp it off
+            if (idx != -1) {
+                return str.substring(0, idx);
+            } else {
+                return str;
+            }
+        }
+        return null;
+    }
+
+}
\ No newline at end of file