aboutsummaryrefslogtreecommitdiffstats
path: root/services/services-onappf/src/main/java
diff options
context:
space:
mode:
authorRam Krishna Verma <ram_krishna.verma@bell.ca>2021-04-28 16:51:38 +0000
committerGerrit Code Review <gerrit@onap.org>2021-04-28 16:51:38 +0000
commit2f5f1dbd3c38dd3915760158a003b04ca69b478b (patch)
tree084a3170c8af676099306605b9b315d20765f27c /services/services-onappf/src/main/java
parentcb09008c4d252dbc9a62f3a1d0b463a74380aa77 (diff)
parentc28ef7b282ab4378935387314f1a57d18e02fcf4 (diff)
Merge "Support delta policies in apex-pdp"
Diffstat (limited to 'services/services-onappf/src/main/java')
-rw-r--r--services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java16
-rw-r--r--services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java46
2 files changed, 45 insertions, 17 deletions
diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java
index 65c4b0373..f3b883141 100644
--- a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java
+++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java
@@ -89,16 +89,18 @@ public class ApexEngineHandler {
/**
* Updates the Apex Engine with the policy model created from new list of policies.
*
- * @param policies the list of policies
+
+ * @param polsToDeploy list of policies to deploy which will be modified to remove running policies
+ * @param polsToUndeploy list of policies to undeploy which will be modified to remove policies not running
* @throws ApexStarterException if the apex engine instantiation failed using the policies passed
*/
- public void updateApexEngine(List<ToscaPolicy> policies) throws ApexStarterException {
+ public void updateApexEngine(List<ToscaPolicy> polsToDeploy, List<ToscaConceptIdentifier> polsToUndeploy)
+ throws ApexStarterException {
List<ToscaConceptIdentifier> runningPolicies = getRunningPolicies();
- List<ToscaPolicy> policiesToDeploy = policies.stream()
- .filter(policy -> !runningPolicies.contains(policy.getIdentifier())).collect(Collectors.toList());
- List<ToscaConceptIdentifier> policiesToUnDeploy = runningPolicies.stream()
- .filter(polId -> policies.stream().noneMatch(policy -> policy.getIdentifier().equals(polId)))
- .collect(Collectors.toList());
+ List<ToscaPolicy> policiesToDeploy = polsToDeploy;
+ policiesToDeploy.removeIf(p -> runningPolicies.contains(p.getIdentifier()));
+ List<ToscaConceptIdentifier> policiesToUnDeploy = polsToUndeploy;
+ policiesToUnDeploy.removeIf(p -> !runningPolicies.contains(p));
Map<ToscaConceptIdentifier, ApexMain> undeployedPoliciesMainMap = new LinkedHashMap<>();
policiesToUnDeploy.forEach(policyId -> {
ApexMain apexMain = apexMainMap.get(policyId);
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 8dc49eefb..0e7465d19 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
@@ -21,9 +21,11 @@
package org.onap.policy.apex.services.onappf.handler;
+import java.util.ArrayList;
import java.util.Collections;
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;
@@ -36,6 +38,7 @@ import org.onap.policy.models.pdp.concepts.PdpUpdate;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -92,9 +95,14 @@ public class PdpUpdateMessageHandler {
}
pdpStatusContext.setPdpGroup(pdpUpdateMsg.getPdpGroup());
pdpStatusContext.setPdpSubgroup(pdpUpdateMsg.getPdpSubgroup());
- pdpStatusContext
- .setPolicies(new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()));
- Registry.registerOrReplace(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST, pdpUpdateMsg.getPolicies());
+ @SuppressWarnings("unchecked")
+ List<ToscaPolicy> policies = Registry.getOrDefault(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST,
+ List.class, new ArrayList<>());
+ policies.addAll(pdpUpdateMsg.getPoliciesToBeDeployed());
+ pdpStatusContext.setPolicies(policies.stream().map(ToscaPolicy::getIdentifier)
+ .collect(Collectors.toList()));
+ Registry.registerOrReplace(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST,
+ policies);
if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) {
pdpResponseDetails = startOrStopApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler);
@@ -135,7 +143,9 @@ public class PdpUpdateMessageHandler {
} catch (final IllegalArgumentException e) {
LOGGER.debug("ApenEngineHandler not in registry.", e);
}
- if (pdpUpdateMsg.getPolicies().isEmpty()) {
+ if (null != apexEngineHandler
+ && pdpUpdateMsg.getPoliciesToBeUndeployed().containsAll(apexEngineHandler.getRunningPolicies())
+ && pdpUpdateMsg.getPoliciesToBeDeployed().isEmpty()) {
pdpResponseDetails = stopApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler, apexEngineHandler);
} else {
pdpResponseDetails = startApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler, apexEngineHandler);
@@ -166,15 +176,17 @@ public class PdpUpdateMessageHandler {
try {
if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
- apexEngineHandler.updateApexEngine(pdpUpdateMsg.getPolicies());
+ apexEngineHandler.updateApexEngine(pdpUpdateMsg.getPoliciesToBeDeployed(),
+ pdpUpdateMsg.getPoliciesToBeUndeployed());
} else {
- apexEngineHandler = new ApexEngineHandler(pdpUpdateMsg.getPolicies());
+ apexEngineHandler = new ApexEngineHandler(pdpUpdateMsg.getPoliciesToBeDeployed());
Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler);
}
if (apexEngineHandler.isApexEngineRunning()) {
List<ToscaConceptIdentifier> runningPolicies = apexEngineHandler.getRunningPolicies();
- if (new HashSet<>(runningPolicies)
- .equals(new HashSet<>(pdpMessageHandler.getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies())))) {
+ if (new HashSet<>(runningPolicies).containsAll(new HashSet<>(pdpMessageHandler
+ .getToscaPolicyIdentifiers(pdpUpdateMsg.getPoliciesToBeDeployed())))
+ && !containsAny(runningPolicies, pdpUpdateMsg.getPoliciesToBeUndeployed())) {
pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
PdpResponseStatus.SUCCESS, "Apex engine started and policies are running.");
} else {
@@ -217,8 +229,10 @@ public class PdpUpdateMessageHandler {
&& pdpStatusContext.getPdpGroup().equals(pdpUpdateMsg.getPdpGroup())
&& null != pdpStatusContext.getPdpSubgroup()
&& pdpStatusContext.getPdpSubgroup().equals(pdpUpdateMsg.getPdpSubgroup())
- && null != pdpStatusContext.getPolicies() && new PdpMessageHandler()
- .getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()).equals(pdpStatusContext.getPolicies());
+ && null != pdpStatusContext.getPolicies()
+ && pdpStatusContext.getPolicies().containsAll(new PdpMessageHandler().getToscaPolicyIdentifiers(
+ pdpUpdateMsg.getPoliciesToBeDeployed()))
+ && !containsAny(pdpStatusContext.getPolicies(), pdpUpdateMsg.getPoliciesToBeUndeployed());
}
/**
@@ -233,4 +247,16 @@ public class PdpUpdateMessageHandler {
Registry.registerOrReplace(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER,
new PdpStatusPublisher(topicSinks, interval));
}
+
+ /**
+ * Checks if one list contains any element of another.
+ *
+ * @param listToCheckWith list to check contents of
+ * @param listToCheckAgainst list to check against other list for similarities
+ * @return boolean flag which tells if lists share same elements or not
+ */
+ private boolean containsAny(List<ToscaConceptIdentifier> listToCheckWith,
+ List<ToscaConceptIdentifier> listToCheckAgainst) {
+ return listToCheckAgainst.stream().anyMatch(listToCheckWith::contains);
+ }
}