From 6c3a6abb7e18be81284866f61a54557474e643ed Mon Sep 17 00:00:00 2001 From: Rashmi Pujar Date: Wed, 23 Mar 2022 12:16:21 -0400 Subject: Account for batch deploy/undeploy in apex app metrics 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 Change-Id: I9ea34d11f5952213b9e72d121ab85dcbd0b8dfc9 --- .../handler/PdpStateChangeMessageHandler.java | 33 ++++++++++++++--- .../onappf/handler/PdpUpdateMessageHandler.java | 43 +++++++++++++++------- 2 files changed, 58 insertions(+), 18 deletions(-) diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpStateChangeMessageHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpStateChangeMessageHandler.java index e6fd9e477..af3e93b6a 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpStateChangeMessageHandler.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpStateChangeMessageHandler.java @@ -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. @@ -21,8 +22,10 @@ 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 runningPolicies, + final List 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)); + } } diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java index 24ebc14de..f10a0b064 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java @@ -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 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 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)); } } } -- cgit 1.2.3-korg