Added new annotation for ScheduledLogging 88/102888/2
authorBrittany Plummer (bp896r) <bp896r@att.com>
Tue, 3 Mar 2020 16:50:53 +0000 (11:50 -0500)
committerBrittany Plummer (bp896r) <bp896r@att.com>
Tue, 3 Mar 2020 20:52:32 +0000 (15:52 -0500)
Issue-ID: LOG-1208
Change-Id: If6d8f8c18affcec8a11eab877f6f716ed34909ba
Signed-off-by: Brittany Plummer (bp896r) <bp896r@att.com>
reference/logging-filter/logging-filter-base/pom.xml
reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/AbstractMDCSetupAspect.java [new file with mode: 0644]
reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/ErrorCode.java [new file with mode: 0644]
reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/ScheduledLogging.java [new file with mode: 0644]
reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/ScheduledTaskException.java [new file with mode: 0644]
reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/ScheduledTasksMDCSetupAspect.java [new file with mode: 0644]
reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/ScheduledTasksMDCSetupAspectTest.java [new file with mode: 0644]
reference/logging-filter/logging-filter-spring/src/main/java/org/onap/logging/filter/spring/SpringScheduledTasksMDCSetupAspect.java [new file with mode: 0644]
reference/logging-filter/pom.xml

index 7bd9340..0ea49c3 100644 (file)
   <name>logging-analytics :: ${project.artifactId}</name>
 
   <dependencies>
+       <dependency>
+      <groupId>org.aspectj</groupId>
+      <artifactId>aspectjrt</artifactId>
+       </dependency>
     <dependency>
       <groupId>javax.annotation</groupId>
       <artifactId>javax.annotation-api</artifactId>
diff --git a/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/AbstractMDCSetupAspect.java b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/AbstractMDCSetupAspect.java
new file mode 100644 (file)
index 0000000..c9424c4
--- /dev/null
@@ -0,0 +1,78 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - Logging
+ * ================================================================================
+ * Copyright (C) 2020 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.logging.filter.base;
+
+import java.util.UUID;
+
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.onap.logging.ref.slf4j.ONAPLogConstants;
+import org.slf4j.MDC;
+
+public abstract class AbstractMDCSetupAspect extends MDCSetup {
+
+    public abstract void logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable;
+
+    protected void setupMDC(String methodName) {
+        try {
+            setEntryTimeStamp();
+            setServerFQDN();
+            String partnerName = getProperty(Constants.Property.PARTNER_NAME);
+            MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, UUID.randomUUID().toString());
+            MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.INPROGRESS.toString());
+            MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, UUID.randomUUID().toString());
+            MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, partnerName);
+            MDC.put(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME, Constants.DefaultValues.UNKNOWN);
+            MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, methodName);
+            setLogTimestamp();
+            setElapsedTime();
+            MDC.put(ONAPLogConstants.MDCs.PARTNER_NAME, partnerName);
+            logger.info(ONAPLogConstants.Markers.ENTRY, "Entering");
+        } catch (Exception e) {
+            logger.warn("Error in ScheduledTasksMDCSetup: {}", e.getMessage());
+        }
+    }
+
+    protected void exitAndClearMDC() {
+        try {
+            setStatusCode();
+            setLogTimestamp();
+            setElapsedTime();
+            logger.info(ONAPLogConstants.Markers.EXIT, "Exiting.");
+        } catch (Exception e) {
+            logger.warn("Error in ScheduledTasksMDCSetup clear MDC: {}", e.getMessage());
+        }
+        MDC.clear();
+    }
+
+    protected void setStatusCode() {
+        String currentStatusCode = MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE);
+        if (currentStatusCode == null || !currentStatusCode.equals(ONAPLogConstants.ResponseStatus.ERROR.toString())) {
+            MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.COMPLETE.toString());
+        }
+    }
+
+    public void errorMDCSetup(ErrorCode errorCode, String errorDescription) {
+        MDC.put(ONAPLogConstants.MDCs.ERROR_CODE, String.valueOf(errorCode.getValue()));
+        MDC.put(ONAPLogConstants.MDCs.ERROR_DESC, errorDescription);
+        MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.ERROR.toString());
+    }
+
+}
diff --git a/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/ErrorCode.java b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/ErrorCode.java
new file mode 100644 (file)
index 0000000..3935ccd
--- /dev/null
@@ -0,0 +1,40 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - Logging
+ * ================================================================================
+ * Copyright (C) 2020 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.logging.filter.base;
+
+public enum ErrorCode {
+    PermissionError(100),
+    AvailabilityError(200),
+    DataError(300),
+    SchemaError(400),
+    BusinessProcessError(500),
+    UnknownError(900);
+
+    private int value;
+
+    ErrorCode(int value) {
+        this.value = value;
+    }
+
+    public int getValue() {
+        return this.value;
+    }
+}
diff --git a/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/ScheduledLogging.java b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/ScheduledLogging.java
new file mode 100644 (file)
index 0000000..39e5653
--- /dev/null
@@ -0,0 +1,32 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - Logging
+ * ================================================================================
+ * Copyright (C) 2020 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.logging.filter.base;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.RetentionPolicy;
+
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface ScheduledLogging {
+
+}
diff --git a/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/ScheduledTaskException.java b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/ScheduledTaskException.java
new file mode 100644 (file)
index 0000000..499d452
--- /dev/null
@@ -0,0 +1,45 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - Logging
+ * ================================================================================
+ * Copyright (C) 2020 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.logging.filter.base;
+
+public class ScheduledTaskException extends Exception {
+
+    private static final long serialVersionUID = 1L;
+    private ErrorCode errorCode;
+
+    public ScheduledTaskException(ErrorCode errorCode, String errorMessage, Throwable cause) {
+        super(errorMessage, cause);
+        this.setErrorCode(errorCode);
+    }
+
+    public ScheduledTaskException(ErrorCode errorCode, String errorMessage) {
+        super(errorMessage);
+        this.setErrorCode(errorCode);
+    }
+
+    public ErrorCode getErrorCode() {
+        return errorCode;
+    }
+
+    public void setErrorCode(ErrorCode errorCode) {
+        this.errorCode = errorCode;
+    }
+}
diff --git a/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/ScheduledTasksMDCSetupAspect.java b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/ScheduledTasksMDCSetupAspect.java
new file mode 100644 (file)
index 0000000..74642b1
--- /dev/null
@@ -0,0 +1,47 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - Logging
+ * ================================================================================
+ * Copyright (C) 2020 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.logging.filter.base;
+
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.onap.logging.filter.base.AbstractMDCSetupAspect;
+import org.onap.logging.filter.base.ScheduledTaskException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Aspect
+public class ScheduledTasksMDCSetupAspect extends AbstractMDCSetupAspect {
+
+    protected static Logger logger = LoggerFactory.getLogger(ScheduledTasksMDCSetupAspect.class);
+
+    @Around("@annotation(ScheduledLogging)")
+    public void logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
+        setupMDC(joinPoint.getSignature().getName());
+        try {
+            joinPoint.proceed();
+        } catch (ScheduledTaskException e) {
+            errorMDCSetup(e.getErrorCode(), e.getMessage());
+            logger.error("ScheduledTaskException: ", e);
+        }
+        exitAndClearMDC();
+    }
+}
diff --git a/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/ScheduledTasksMDCSetupAspectTest.java b/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/ScheduledTasksMDCSetupAspectTest.java
new file mode 100644 (file)
index 0000000..bdd1033
--- /dev/null
@@ -0,0 +1,99 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - Logging
+ * ================================================================================
+ * Copyright (C) 2020 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.logging.filter.base;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.UUID;
+
+import org.junit.After;
+import org.junit.Test;
+import org.onap.logging.filter.base.Constants;
+import org.onap.logging.ref.slf4j.ONAPLogConstants;
+
+import org.slf4j.MDC;
+
+public class ScheduledTasksMDCSetupAspectTest {
+
+    private ScheduledTasksMDCSetupAspect tasksMDCSetup = new ScheduledTasksMDCSetupAspect();
+
+    @After
+    public void tearDown() {
+        MDC.clear();
+        System.clearProperty("partnerName");
+    }
+
+    @Test
+    public void mdcSetupTest() {
+        System.setProperty("partnerName", ONAPComponents.SO.toString());
+        tasksMDCSetup.setupMDC("mdcSetupTest");
+
+        assertTrue(isValidUUID(MDC.get(ONAPLogConstants.MDCs.REQUEST_ID)));
+        assertTrue(isValidUUID(MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID)));
+        assertEquals(ONAPComponents.SO.toString(), MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY));
+        assertEquals(ONAPComponents.SO.toString(), MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME));
+        assertEquals("mdcSetupTest", MDC.get(ONAPLogConstants.MDCs.SERVICE_NAME));
+        assertEquals(Constants.DefaultValues.UNKNOWN, MDC.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME));
+        assertNotNull(MDC.get(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP));
+        assertNotNull(MDC.get(ONAPLogConstants.MDCs.ELAPSED_TIME));
+        assertNotNull(MDC.get(ONAPLogConstants.MDCs.LOG_TIMESTAMP));
+        assertNotNull(MDC.get(ONAPLogConstants.MDCs.SERVER_FQDN));
+    }
+
+    @Test
+    public void errorMDCSetupTest() {
+        tasksMDCSetup.errorMDCSetup(ErrorCode.UnknownError, "Error");
+
+        assertEquals("900", MDC.get(ONAPLogConstants.MDCs.ERROR_CODE));
+        assertEquals("Error", MDC.get(ONAPLogConstants.MDCs.ERROR_DESC));
+    }
+
+    @Test
+    public void setStatusCodeTest() {
+        tasksMDCSetup.setStatusCode();
+
+        assertEquals(ONAPLogConstants.ResponseStatus.COMPLETE.toString(),
+                MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
+    }
+
+    @Test
+    public void setStatusCodeErrorTest() {
+        MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.ERROR.toString());
+        tasksMDCSetup.setStatusCode();
+
+        assertEquals(ONAPLogConstants.ResponseStatus.ERROR.toString(),
+                MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
+    }
+
+    public static boolean isValidUUID(String id) {
+        try {
+            if (null == id) {
+                return false;
+            }
+            UUID uuid = UUID.fromString(id);
+            return uuid.toString().equalsIgnoreCase(id);
+        } catch (IllegalArgumentException iae) {
+            return false;
+        }
+    }
+}
diff --git a/reference/logging-filter/logging-filter-spring/src/main/java/org/onap/logging/filter/spring/SpringScheduledTasksMDCSetupAspect.java b/reference/logging-filter/logging-filter-spring/src/main/java/org/onap/logging/filter/spring/SpringScheduledTasksMDCSetupAspect.java
new file mode 100644 (file)
index 0000000..24d9014
--- /dev/null
@@ -0,0 +1,49 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - Logging
+ * ================================================================================
+ * Copyright (C) 2020 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.logging.filter.spring;
+
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.onap.logging.filter.base.AbstractMDCSetupAspect;
+import org.onap.logging.filter.base.ScheduledTaskException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+
+@Aspect
+@Component
+public class SpringScheduledTasksMDCSetupAspect extends AbstractMDCSetupAspect {
+
+    protected static Logger logger = LoggerFactory.getLogger(SpringScheduledTasksMDCSetupAspect.class);
+
+    @Around("@annotation(org.onap.logging.filter.base.ScheduledLogging)")
+    public void logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
+        setupMDC(joinPoint.getSignature().getName());
+        try {
+            joinPoint.proceed();
+        } catch (ScheduledTaskException e) {
+            errorMDCSetup(e.getErrorCode(), e.getMessage());
+            logger.error("ScheduledTaskException: ", e);
+        }
+        exitAndClearMDC();
+    }
+}
index e518620..3e82591 100644 (file)
 
   <dependencyManagement>
     <dependencies>
+      <dependency>
+        <groupId>org.aspectj</groupId>
+        <artifactId>aspectjrt</artifactId>
+        <version>1.9.5</version>
+         </dependency>
       <dependency>
         <groupId>javax.annotation</groupId>
         <artifactId>javax.annotation-api</artifactId>