Update pdp statistics to count deploy/undeploy separately. 27/122527/2
authoradheli.tavares <adheli.tavares@est.tech>
Thu, 8 Jul 2021 14:36:25 +0000 (15:36 +0100)
committeradheli.tavares <adheli.tavares@est.tech>
Mon, 12 Jul 2021 13:10:10 +0000 (14:10 +0100)
Issue-ID: POLICY-3383
Change-Id: Ie9c5c7178c4c4302a0ba63ab42732f7157d302f8
Signed-off-by: adheli.tavares <adheli.tavares@est.tech>
services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexPolicyStatisticsManager.java
services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/ApexPolicyStatisticsManagerTest.java [new file with mode: 0644]
services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpMessageHandler.java
services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java
services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpUpdateListener.java

index 0200af3..f49e8cd 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2020 Nordix Foundation.
+ *  Copyright (C) 2020-2021 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -31,6 +31,9 @@ public class ApexPolicyStatisticsManager {
     private final AtomicLong policyDeployCount = new AtomicLong(0);
     private final AtomicLong policyDeploySuccessCount = new AtomicLong(0);
     private final AtomicLong policyDeployFailCount = new AtomicLong(0);
+    private final AtomicLong policyUndeployCount = new AtomicLong(0);
+    private final AtomicLong policyUndeploySuccessCount = new AtomicLong(0);
+    private final AtomicLong policyUndeployFailCount = new AtomicLong(0);
     private final AtomicLong policyExecutedCount = new AtomicLong(0);
     private final AtomicLong policyExecutedSuccessCount = new AtomicLong(0);
     private final AtomicLong policyExecutedFailCount = new AtomicLong(0);
@@ -82,6 +85,19 @@ public class ApexPolicyStatisticsManager {
         }
     }
 
+
+    /**
+     * Update the policy undeploy count.
+     */
+    public void updatePolicyUndeployCounter(final boolean isSuccessful) {
+        this.policyUndeployCount.incrementAndGet();
+        if (isSuccessful) {
+            this.policyUndeploySuccessCount.incrementAndGet();
+        } else {
+            this.policyUndeployFailCount.incrementAndGet();
+        }
+    }
+
     /**
      * Method to update the total policy deploy count.
      *
@@ -144,6 +160,9 @@ public class ApexPolicyStatisticsManager {
         policyDeployCount.set(0L);
         policyDeployFailCount.set(0L);
         policyDeploySuccessCount.set(0L);
+        policyUndeployCount.set(0L);
+        policyUndeployFailCount.set(0L);
+        policyUndeploySuccessCount.set(0L);
         policyExecutedCount.set(0L);
         policyExecutedSuccessCount.set(0L);
         policyExecutedFailCount.set(0L);
@@ -172,4 +191,16 @@ public class ApexPolicyStatisticsManager {
     public long getPolicyExecutedFailCount() {
         return policyExecutedFailCount.get();
     }
+
+    public long getPolicyUndeployCount() {
+        return policyUndeployCount.get();
+    }
+
+    public long getPolicyUndeploySuccessCount() {
+        return policyUndeploySuccessCount.get();
+    }
+
+    public long getPolicyUndeployFailCount() {
+        return policyUndeployFailCount.get();
+    }
 }
diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/ApexPolicyStatisticsManagerTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/ApexPolicyStatisticsManagerTest.java
new file mode 100644 (file)
index 0000000..4c541bd
--- /dev/null
@@ -0,0 +1,106 @@
+/*-
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2021 Nordix Foundation.
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.apex.service.engine.main;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class ApexPolicyStatisticsManagerTest {
+
+    private ApexPolicyStatisticsManager statisticsManager;
+
+    /**
+     * Starts the statisticsManager object for tests.
+     */
+    @Before
+    public void setup() {
+        statisticsManager = new ApexPolicyStatisticsManager();
+    }
+
+    @Test
+    public void testUpdatePolicyDeployCounter() {
+        statisticsManager.updatePolicyDeployCounter(false);
+        assertDeploys(1, 0, 1);
+
+        statisticsManager.updatePolicyDeployCounter(true);
+        statisticsManager.updatePolicyDeployCounter(true);
+        assertDeploys(3, 2, 1);
+    }
+
+    @Test
+    public void testUpdatePolicyExecutedCounter() {
+        statisticsManager.updatePolicyExecutedCounter(true);
+        assertExecuted(1, 1, 0);
+
+        statisticsManager.updatePolicyExecutedCounter(false);
+        assertExecuted(2, 1, 1);
+    }
+
+    @Test
+    public void testUpdatePolicyUndeployCounter() {
+        statisticsManager.updatePolicyUndeployCounter(false);
+        assertUndeploys(1, 0, 1);
+
+        statisticsManager.updatePolicyUndeployCounter(true);
+        assertUndeploys(2, 1, 1);
+    }
+
+    @Test
+    public void testResetAllStatistics() {
+        statisticsManager.updatePolicyDeployCounter(true);
+        statisticsManager.updatePolicyDeployCounter(true);
+        statisticsManager.updatePolicyDeployCounter(false);
+        statisticsManager.updatePolicyUndeployCounter(false);
+        statisticsManager.updatePolicyExecutedCounter(true);
+
+        assertDeploys(3, 2, 1);
+        assertUndeploys(1, 0, 1);
+        assertExecuted(1, 1, 0);
+
+        statisticsManager.resetAllStatistics();
+
+        assertDeploys(0, 0, 0);
+        assertUndeploys(0, 0, 0);
+        assertExecuted(0, 0, 0);
+
+    }
+
+    private void assertDeploys(long count, long success, long fail) {
+        assertEquals(count, statisticsManager.getPolicyDeployCount());
+        assertEquals(success, statisticsManager.getPolicyDeploySuccessCount());
+        assertEquals(fail, statisticsManager.getPolicyDeployFailCount());
+    }
+
+    private void assertUndeploys(long count, long success, long fail) {
+        assertEquals(count, statisticsManager.getPolicyUndeployCount());
+        assertEquals(success, statisticsManager.getPolicyUndeploySuccessCount());
+        assertEquals(fail, statisticsManager.getPolicyUndeployFailCount());
+    }
+
+    private void assertExecuted(long count, long success, long fail) {
+        assertEquals(count, statisticsManager.getPolicyExecutedCount());
+        assertEquals(success, statisticsManager.getPolicyExecutedSuccessCount());
+        assertEquals(fail, statisticsManager.getPolicyExecutedFailCount());
+    }
+
+}
index 4044a49..134b45c 100644 (file)
@@ -141,6 +141,11 @@ public class PdpMessageHandler {
             pdpStatistics.setPolicyDeploySuccessCount(apexPolicyCounter.getPolicyDeploySuccessCount());
             pdpStatistics.setPolicyDeployFailCount(apexPolicyCounter.getPolicyDeployFailCount());
             pdpStatistics.setPolicyDeployCount(apexPolicyCounter.getPolicyDeployCount());
+
+            pdpStatistics.setPolicyUndeploySuccessCount(apexPolicyCounter.getPolicyUndeploySuccessCount());
+            pdpStatistics.setPolicyUndeployFailCount(apexPolicyCounter.getPolicyUndeployFailCount());
+            pdpStatistics.setPolicyUndeployCount(apexPolicyCounter.getPolicyUndeployCount());
+
             pdpStatistics.setPolicyExecutedCount(apexPolicyCounter.getPolicyExecutedCount());
             pdpStatistics.setPolicyExecutedSuccessCount(apexPolicyCounter.getPolicyExecutedSuccessCount());
             pdpStatistics.setPolicyExecutedFailCount(apexPolicyCounter.getPolicyExecutedFailCount());
index e6c2c39..09fb92d 100644 (file)
@@ -168,6 +168,7 @@ public class PdpUpdateMessageHandler {
                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
                         PdpResponseStatus.FAIL, "Pdp update failed as the policies couldn't be undeployed.");
             }
+            updateDeploymentCounts(pdpUpdateMsg, pdpResponseDetails);
         }
         return pdpResponseDetails;
     }
@@ -196,12 +197,7 @@ public class PdpUpdateMessageHandler {
             pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
                     PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage());
         }
-        final ApexPolicyStatisticsManager apexPolicyStatisticsManager =
-                ApexPolicyStatisticsManager.getInstanceFromRegistry();
-        if (apexPolicyStatisticsManager != null) {
-            apexPolicyStatisticsManager
-                    .updatePolicyDeployCounter(pdpResponseDetails.getResponseStatus() == PdpResponseStatus.SUCCESS);
-        }
+        updateDeploymentCounts(pdpUpdateMsg, pdpResponseDetails);
         return pdpResponseDetails;
     }
 
@@ -284,4 +280,27 @@ public class PdpUpdateMessageHandler {
             List<ToscaConceptIdentifier> listToCheckAgainst) {
         return listToCheckAgainst.stream().anyMatch(listToCheckWith::contains);
     }
+
+    /**
+     * Update count values for deployment actions (deploy and undeploy) when applicable.
+     * @param pdpUpdateMsg the pdp update message from pap
+     * @param pdpResponseDetails the pdp response
+     */
+    private void updateDeploymentCounts(final PdpUpdate pdpUpdateMsg, PdpResponseDetails pdpResponseDetails) {
+        final ApexPolicyStatisticsManager statisticsManager =
+                ApexPolicyStatisticsManager.getInstanceFromRegistry();
+
+        if (statisticsManager != null) {
+            if (pdpUpdateMsg.getPoliciesToBeDeployed() != null && !pdpUpdateMsg.getPoliciesToBeDeployed().isEmpty()) {
+                statisticsManager.updatePolicyDeployCounter(
+                        pdpResponseDetails.getResponseStatus() == PdpResponseStatus.SUCCESS);
+            }
+
+            if (pdpUpdateMsg.getPoliciesToBeUndeployed() != null
+                    && !pdpUpdateMsg.getPoliciesToBeUndeployed().isEmpty()) {
+                statisticsManager.updatePolicyUndeployCounter(
+                        pdpResponseDetails.getResponseStatus() == PdpResponseStatus.SUCCESS);
+            }
+        }
+    }
 }
index 9d314b4..3716e04 100644 (file)
@@ -38,6 +38,7 @@ import java.util.stream.Collectors;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
+import org.onap.policy.apex.service.engine.main.ApexPolicyStatisticsManager;
 import org.onap.policy.apex.services.onappf.ApexStarterActivator;
 import org.onap.policy.apex.services.onappf.ApexStarterCommandLineArguments;
 import org.onap.policy.apex.services.onappf.ApexStarterConstants;
@@ -101,6 +102,8 @@ public class TestPdpUpdateListener {
         activator.initialize();
         pdpUpdateMessageListener = new PdpUpdateListener();
         pdpStateChangeListener = new PdpStateChangeListener();
+        Registry.register(ApexPolicyStatisticsManager.REG_APEX_PDP_POLICY_COUNTER,
+                new ApexPolicyStatisticsManager());
     }
 
     /**