* ============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;
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;
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.");
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;
}
}
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));
+ }
}
/*-
* ============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");
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;
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(),
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;
}
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);
if (apexEngineHandler.isApexEngineRunning()) {
pdpResponseDetails =
populateResponseForEngineInitiation(pdpUpdateMsg, pdpMessageHandler, apexEngineHandler);
+ runningPolicies = apexEngineHandler.getRunningPolicies();
} else {
pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
PdpResponseStatus.FAIL, "Apex engine failed to start.");
pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage());
}
- updateDeploymentCounts(pdpUpdateMsg, pdpResponseDetails);
+ updateDeploymentCounts(runningPolicies, pdpUpdateMsg);
return pdpResponseDetails;
}
/**
* 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));
}
}
}