Account for batch deploy/undeploy in apex app metrics 20/128020/3
authorRashmi Pujar <rashmi.pujar1@bell.ca>
Wed, 23 Mar 2022 16:16:21 +0000 (12:16 -0400)
committerRashmi Pujar <rashmi.pujar1@bell.ca>
Thu, 24 Mar 2022 02:57:33 +0000 (22:57 -0400)
Batch deploy/undeploy operations will now increment
pdpa_policy_deployments_total counter by the total
number of policies as present in the request.

Issue-ID: POLICY-4043
Signed-off-by: Rashmi Pujar <rashmi.pujar1@bell.ca>
Change-Id: I9ea34d11f5952213b9e72d121ab85dcbd0b8dfc9

services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpStateChangeMessageHandler.java
services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java

index e6fd9e4..af3e93b 100644 (file)
@@ -2,6 +2,7 @@
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019-2021 Nordix Foundation.
  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
+ *  Modifications Copyright (C) 2022 Bell Canada. 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.
 
 package org.onap.policy.apex.services.onappf.handler;
 
+import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
+import java.util.stream.Collectors;
 import org.onap.policy.apex.service.engine.main.ApexPolicyStatisticsManager;
 import org.onap.policy.apex.services.onappf.ApexStarterConstants;
 import org.onap.policy.apex.services.onappf.comm.PdpStatusPublisher;
@@ -35,6 +38,7 @@ import org.onap.policy.models.pdp.enums.PdpResponseStatus;
 import org.onap.policy.models.pdp.enums.PdpState;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaWithTypeAndObjectProperties;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -139,6 +143,7 @@ public class PdpStateChangeMessageHandler {
                         pdpStateChangeMsg.getRequestId(), PdpResponseStatus.SUCCESS, message.toString());
                 }
                 pdpStatusContext.setState(PdpState.ACTIVE);
+                updateDeploymentCounts(runningPolicies, policies);
             } else {
                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
                     PdpResponseStatus.FAIL, "Apex engine failed to start. State cannot be changed to active.");
@@ -148,11 +153,6 @@ public class PdpStateChangeMessageHandler {
             pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
                     PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage());
         }
-        final var apexPolicyStatisticsManager = ApexPolicyStatisticsManager.getInstanceFromRegistry();
-        if (apexPolicyStatisticsManager != null) {
-            apexPolicyStatisticsManager
-                    .updatePolicyDeployCounter(pdpResponseDetails.getResponseStatus() == PdpResponseStatus.SUCCESS);
-        }
         return pdpResponseDetails;
     }
 
@@ -193,4 +193,27 @@ public class PdpStateChangeMessageHandler {
         }
         return pdpResponseDetails;
     }
+
+    /**
+     * Update count values for deployment on engine startup.
+     * @param runningPolicies the policies running in apex engine
+     * @param policies the list of policies to deploy as per PDP_STATE_CHANGE message from pap
+     */
+    private void updateDeploymentCounts(final List<ToscaConceptIdentifier> runningPolicies,
+        final List<ToscaPolicy> policies) {
+        final var statisticsManager = ApexPolicyStatisticsManager.getInstanceFromRegistry();
+        if (statisticsManager == null || policies == null || policies.isEmpty()) {
+            return;
+        }
+        var policiesToDeploy = policies.stream()
+            .map(ToscaWithTypeAndObjectProperties::getIdentifier).collect(Collectors.toList());
+
+        var policiesSuccessfullyDeployed = new ArrayList<>(policiesToDeploy);
+        policiesSuccessfullyDeployed.retainAll(runningPolicies);
+        policiesSuccessfullyDeployed.forEach(policy -> statisticsManager.updatePolicyDeployCounter(true));
+
+        var policiesFailedToDeploy =  new ArrayList<>(policiesToDeploy);
+        policiesFailedToDeploy.removeIf(runningPolicies::contains);
+        policiesFailedToDeploy.forEach(policy -> statisticsManager.updatePolicyDeployCounter(false));
+    }
 }
index 24ebc14..f10a0b0 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019-2021 Nordix Foundation.
- *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
+ *  Modifications Copyright (C) 2021-2022 Bell Canada. All rights reserved.
  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -41,6 +41,7 @@ import org.onap.policy.models.pdp.enums.PdpResponseStatus;
 import org.onap.policy.models.pdp.enums.PdpState;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaWithTypeAndObjectProperties;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -161,6 +162,7 @@ public class PdpUpdateMessageHandler {
         final PdpMessageHandler pdpMessageHandler, ApexEngineHandler apexEngineHandler) {
         PdpResponseDetails pdpResponseDetails = null;
         if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
+            final var runningPolicies = apexEngineHandler.getRunningPolicies();
             try {
                 apexEngineHandler.shutdown();
                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
@@ -170,7 +172,7 @@ public class PdpUpdateMessageHandler {
                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
                         PdpResponseStatus.FAIL, "Pdp update failed as the policies couldn't be undeployed.");
             }
-            updateDeploymentCounts(pdpUpdateMsg, pdpResponseDetails);
+            updateDeploymentCounts(runningPolicies, pdpUpdateMsg);
         }
         return pdpResponseDetails;
     }
@@ -178,11 +180,12 @@ public class PdpUpdateMessageHandler {
     private PdpResponseDetails startApexEngineBasedOnPolicies(final PdpUpdate pdpUpdateMsg,
         final PdpMessageHandler pdpMessageHandler, ApexEngineHandler apexEngineHandler) {
         PdpResponseDetails pdpResponseDetails = null;
-
+        List<ToscaConceptIdentifier> runningPolicies = null;
         try {
             if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
                 apexEngineHandler.updateApexEngine(pdpUpdateMsg.getPoliciesToBeDeployed(),
                     pdpUpdateMsg.getPoliciesToBeUndeployed());
+                runningPolicies = apexEngineHandler.getRunningPolicies();
             } else {
                 apexEngineHandler = new ApexEngineHandler(pdpUpdateMsg.getPoliciesToBeDeployed());
                 Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler);
@@ -190,6 +193,7 @@ public class PdpUpdateMessageHandler {
             if (apexEngineHandler.isApexEngineRunning()) {
                 pdpResponseDetails =
                     populateResponseForEngineInitiation(pdpUpdateMsg, pdpMessageHandler, apexEngineHandler);
+                runningPolicies = apexEngineHandler.getRunningPolicies();
             } else {
                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
                     PdpResponseStatus.FAIL, "Apex engine failed to start.");
@@ -199,7 +203,7 @@ public class PdpUpdateMessageHandler {
             pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
                     PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage());
         }
-        updateDeploymentCounts(pdpUpdateMsg, pdpResponseDetails);
+        updateDeploymentCounts(runningPolicies, pdpUpdateMsg);
         return pdpResponseDetails;
     }
 
@@ -286,22 +290,35 @@ public class PdpUpdateMessageHandler {
 
     /**
      * Update count values for deployment actions (deploy and undeploy) when applicable.
+     * @param runningPolicies the policies running in apex engine
      * @param pdpUpdateMsg the pdp update message from pap
-     * @param pdpResponseDetails the pdp response
      */
-    private void updateDeploymentCounts(final PdpUpdate pdpUpdateMsg, PdpResponseDetails pdpResponseDetails) {
+    private void updateDeploymentCounts(final List<ToscaConceptIdentifier> runningPolicies,
+        final PdpUpdate pdpUpdateMsg) {
         final var statisticsManager = ApexPolicyStatisticsManager.getInstanceFromRegistry();
-
         if (statisticsManager != null) {
             if (pdpUpdateMsg.getPoliciesToBeDeployed() != null && !pdpUpdateMsg.getPoliciesToBeDeployed().isEmpty()) {
-                statisticsManager.updatePolicyDeployCounter(
-                        pdpResponseDetails.getResponseStatus() == PdpResponseStatus.SUCCESS);
+                var policiesToDeploy = pdpUpdateMsg.getPoliciesToBeDeployed().stream()
+                    .map(ToscaWithTypeAndObjectProperties::getIdentifier).collect(Collectors.toList());
+
+                var policiesSuccessfullyDeployed = new ArrayList<>(policiesToDeploy);
+                policiesSuccessfullyDeployed.retainAll(runningPolicies);
+                policiesSuccessfullyDeployed.forEach(policy -> statisticsManager.updatePolicyDeployCounter(true));
+
+                var policiesFailedToDeploy =  new ArrayList<>(policiesToDeploy);
+                policiesFailedToDeploy.removeIf(runningPolicies::contains);
+                policiesFailedToDeploy.forEach(policy -> statisticsManager.updatePolicyDeployCounter(false));
             }
 
-            if (pdpUpdateMsg.getPoliciesToBeUndeployed() != null
-                    && !pdpUpdateMsg.getPoliciesToBeUndeployed().isEmpty()) {
-                statisticsManager.updatePolicyUndeployCounter(
-                        pdpResponseDetails.getResponseStatus() == PdpResponseStatus.SUCCESS);
+            var policiesToUndeploy = pdpUpdateMsg.getPoliciesToBeUndeployed();
+            if (policiesToUndeploy != null && !policiesToUndeploy.isEmpty()) {
+                var policiesSuccessfullyUndeployed = new ArrayList<>(policiesToUndeploy);
+                policiesSuccessfullyUndeployed.retainAll(runningPolicies);
+                policiesSuccessfullyUndeployed.forEach(policy -> statisticsManager.updatePolicyUndeployCounter(true));
+
+                var policiesFailedToUndeploy =  new ArrayList<>(policiesToUndeploy);
+                policiesFailedToUndeploy.removeIf(runningPolicies::contains);
+                policiesFailedToUndeploy.forEach(policy -> statisticsManager.updatePolicyUndeployCounter(false));
             }
         }
     }