aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/main/java/org/onap/policy/pap/main/comm
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/main/java/org/onap/policy/pap/main/comm')
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/comm/PdpMessageGenerator.java21
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/comm/PdpStatusMessageHandler.java48
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/comm/msgdata/UpdateReq.java51
3 files changed, 110 insertions, 10 deletions
diff --git a/main/src/main/java/org/onap/policy/pap/main/comm/PdpMessageGenerator.java b/main/src/main/java/org/onap/policy/pap/main/comm/PdpMessageGenerator.java
index fb47e929..99408eb2 100644
--- a/main/src/main/java/org/onap/policy/pap/main/comm/PdpMessageGenerator.java
+++ b/main/src/main/java/org/onap/policy/pap/main/comm/PdpMessageGenerator.java
@@ -72,7 +72,6 @@ public class PdpMessageGenerator {
*/
private final Long heartBeatMs;
-
/**
* Constructs the object.
*
@@ -94,21 +93,33 @@ public class PdpMessageGenerator {
}
protected PdpUpdate createPdpUpdateMessage(final String pdpGroupName, final PdpSubGroup subGroup,
- final String pdpInstanceId, final PolicyModelsProvider databaseProvider)
- throws PfModelException {
+ final String pdpInstanceId, final PolicyModelsProvider databaseProvider,
+ final List<ToscaPolicy> policies, final List<ToscaPolicy> policiesToBeDeployed,
+ final List<ToscaConceptIdentifier> policiesToBeUndeployed) throws PfModelException {
final PdpUpdate update = new PdpUpdate();
+
update.setName(pdpInstanceId);
update.setPdpGroup(pdpGroupName);
update.setPdpSubgroup(subGroup.getPdpType());
- update.setPolicies(getToscaPolicies(subGroup, databaseProvider));
+ update.setPolicies(policies);
+ update.setPoliciesToBeDeployed(policiesToBeDeployed);
+ update.setPoliciesToBeUndeployed(policiesToBeUndeployed);
update.setPdpHeartbeatIntervalMs(heartBeatMs);
LOGGER.debug("Created PdpUpdate message - {}", update);
return update;
}
- private List<ToscaPolicy> getToscaPolicies(final PdpSubGroup subGroup, final PolicyModelsProvider databaseProvider)
+ /**
+ * Method to return a list of policies.
+ *
+ * @param subGroup PdpSubGroup to retrieve policies from
+ * @param databaseProvider PolicyModelsProvider used to retrieve list of policies
+ * @returns a list of ToscaPolicy
+ **/
+ public List<ToscaPolicy> getToscaPolicies(final PdpSubGroup subGroup,
+ final PolicyModelsProvider databaseProvider)
throws PfModelException {
final List<ToscaPolicy> policies = new LinkedList<>();
diff --git a/main/src/main/java/org/onap/policy/pap/main/comm/PdpStatusMessageHandler.java b/main/src/main/java/org/onap/policy/pap/main/comm/PdpStatusMessageHandler.java
index 2e9adda4..b692a2a5 100644
--- a/main/src/main/java/org/onap/policy/pap/main/comm/PdpStatusMessageHandler.java
+++ b/main/src/main/java/org/onap/policy/pap/main/comm/PdpStatusMessageHandler.java
@@ -23,9 +23,13 @@
package org.onap.policy.pap.main.comm;
import java.util.Arrays;
+import java.util.HashMap;
+import java.util.LinkedList;
import java.util.List;
+import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.onap.policy.common.utils.services.Registry;
import org.onap.policy.models.base.PfModelException;
@@ -39,6 +43,8 @@ import org.onap.policy.models.pdp.concepts.PdpSubGroup;
import org.onap.policy.models.pdp.concepts.PdpUpdate;
import org.onap.policy.models.pdp.enums.PdpState;
import org.onap.policy.models.provider.PolicyModelsProvider;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
import org.onap.policy.pap.main.PapConstants;
import org.onap.policy.pap.main.PolicyPapException;
import org.onap.policy.pap.main.parameters.PdpParameters;
@@ -57,6 +63,21 @@ public class PdpStatusMessageHandler extends PdpMessageGenerator {
private final PdpParameters params;
/**
+ * List to store policies present in db.
+ */
+ List<ToscaPolicy> policies = new LinkedList<>();
+
+ /**
+ * List to store policies to be deployed (heartbeat).
+ */
+ Map<ToscaConceptIdentifier, ToscaPolicy> policiesToBeDeployed = new HashMap<>();
+
+ /**
+ * List to store policies to be undeployed (heartbeat).
+ */
+ List<ToscaConceptIdentifier> policiesToBeUndeployed = new LinkedList<>();
+
+ /**
* Constructs the object.
*
* @param params PDP parameters
@@ -129,6 +150,12 @@ public class PdpStatusMessageHandler extends PdpMessageGenerator {
Optional<PdpSubGroup> subGroup;
boolean pdpGroupFound = false;
subGroup = findPdpSubGroup(message, finalizedPdpGroup);
+
+ policies = getToscaPolicies(subGroup.get(), databaseProvider);
+ policiesToBeDeployed = policies.stream().collect(Collectors
+ .toMap(ToscaPolicy::getIdentifier, policy -> policy));
+ policiesToBeUndeployed = null;
+
if (subGroup.isPresent()) {
LOGGER.debug("Found pdpGroup - {}, going for registration of PDP - {}", finalizedPdpGroup, message);
if (!findPdpInstance(message, subGroup.get()).isPresent()) {
@@ -201,7 +228,19 @@ public class PdpStatusMessageHandler extends PdpMessageGenerator {
}
private void processPdpDetails(final PdpStatus message, final PdpSubGroup pdpSubGroup, final Pdp pdpInstance,
- final PdpGroup pdpGroup, final PolicyModelsProvider databaseProvider) throws PfModelException {
+ final PdpGroup pdpGroup, final PolicyModelsProvider databaseProvider)
+ throws PfModelException {
+ // all policies
+ policies = getToscaPolicies(pdpSubGroup, databaseProvider);
+
+ // all (-) policies that the PDP already has
+ policiesToBeDeployed.keySet().removeAll(message.getPolicies());
+
+ // policies that the PDP already has (-) all
+ policiesToBeUndeployed = message.getPolicies();
+ policiesToBeUndeployed.removeAll(policies.stream().map(ToscaPolicy::getIdentifier)
+ .collect(Collectors.toList()));
+
if (PdpState.TERMINATED.equals(message.getState())) {
processPdpTermination(pdpSubGroup, pdpInstance, pdpGroup, databaseProvider);
} else if (validatePdpDetails(message, pdpGroup, pdpSubGroup, pdpInstance)) {
@@ -280,9 +319,12 @@ public class PdpStatusMessageHandler extends PdpMessageGenerator {
}
private void sendPdpMessage(final String pdpGroupName, final PdpSubGroup subGroup, final String pdpInstanceId,
- final PdpState pdpState, final PolicyModelsProvider databaseProvider) throws PfModelException {
+ final PdpState pdpState, final PolicyModelsProvider databaseProvider)
+ throws PfModelException {
+ final List<ToscaPolicy> polsToBeDeployed = new LinkedList<>(policiesToBeDeployed.values());
final PdpUpdate pdpUpdatemessage =
- createPdpUpdateMessage(pdpGroupName, subGroup, pdpInstanceId, databaseProvider);
+ createPdpUpdateMessage(pdpGroupName, subGroup, pdpInstanceId, databaseProvider, policies,
+ polsToBeDeployed, policiesToBeUndeployed);
final PdpStateChange pdpStateChangeMessage =
createPdpStateChangeMessage(pdpGroupName, subGroup, pdpInstanceId, pdpState);
updateDeploymentStatus(pdpGroupName, subGroup.getPdpType(), pdpInstanceId, pdpStateChangeMessage.getState(),
diff --git a/main/src/main/java/org/onap/policy/pap/main/comm/msgdata/UpdateReq.java b/main/src/main/java/org/onap/policy/pap/main/comm/msgdata/UpdateReq.java
index 4b5b7f04..18ae5af6 100644
--- a/main/src/main/java/org/onap/policy/pap/main/comm/msgdata/UpdateReq.java
+++ b/main/src/main/java/org/onap/policy/pap/main/comm/msgdata/UpdateReq.java
@@ -24,7 +24,9 @@ package org.onap.policy.pap.main.comm.msgdata;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
+import java.util.LinkedList;
import java.util.List;
+import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.Getter;
@@ -125,7 +127,36 @@ public class UpdateReq extends RequestImpl {
return true;
}
- reconfigure2(newMessage);
+ Map<ToscaConceptIdentifier, ToscaPolicy> newDeployMap = update.getPoliciesToBeDeployed().stream()
+ .collect(Collectors.toMap(ToscaPolicy::getIdentifier, policy -> policy));
+
+ // Merge full lists
+ final List<ToscaPolicy> fullPolicies = update.getPolicies();
+
+ // Merge undpeloy lists
+ Set<ToscaConceptIdentifier> policiesToBeUndeployedSet = new HashSet<>(getMessage().getPoliciesToBeUndeployed());
+ policiesToBeUndeployedSet.removeAll(newDeployMap.keySet());
+ policiesToBeUndeployedSet.addAll(update.getPoliciesToBeUndeployed());
+ final List<ToscaConceptIdentifier> policiestoBeUndeployed = new LinkedList<>(policiesToBeUndeployedSet);
+
+ // Merge deploy lists
+ final List<ToscaPolicy> policiesToBeDeployed;
+ if (update.getPoliciesToBeDeployed() == update.getPolicies()) {
+ policiesToBeDeployed = update.getPoliciesToBeDeployed();
+ } else {
+ Map<ToscaConceptIdentifier, ToscaPolicy> policiesToBeDeployedMap = getMessage().getPoliciesToBeDeployed()
+ .stream().collect(Collectors.toMap(ToscaPolicy::getIdentifier, policy -> policy));
+ policiesToBeDeployedMap.keySet().removeAll(update.getPoliciesToBeUndeployed());
+ policiesToBeDeployedMap.putAll(newDeployMap);
+ policiesToBeDeployed = new LinkedList<>(policiesToBeDeployedMap.values());
+ }
+
+ // Set lists in update
+ update.setPolicies(fullPolicies);
+ update.setPoliciesToBeDeployed(policiesToBeDeployed);
+ update.setPoliciesToBeUndeployed(policiestoBeUndeployed);
+
+ reconfigure2(update);
return true;
}
@@ -144,7 +175,23 @@ public class UpdateReq extends RequestImpl {
Set<ToscaPolicy> set1 = new HashSet<>(alwaysList(first.getPolicies()));
Set<ToscaPolicy> set2 = new HashSet<>(alwaysList(second.getPolicies()));
- return set1.equals(set2);
+ if (!(set1.equals(set2))) {
+ return false;
+ }
+
+ Map<ToscaConceptIdentifier, ToscaPolicy> dep1 = first.getPolicies().stream()
+ .collect(Collectors.toMap(ToscaPolicy::getIdentifier, p -> p));
+ Map<ToscaConceptIdentifier, ToscaPolicy> dep2 = second.getPoliciesToBeDeployed()
+ .stream().collect(Collectors.toMap(ToscaPolicy::getIdentifier, p -> p));
+
+ if (!(dep1.equals(dep2))) {
+ return false;
+ }
+
+ HashSet<ToscaConceptIdentifier> undep1 = new HashSet<>(alwaysList(first.getPoliciesToBeUndeployed()));
+ HashSet<ToscaConceptIdentifier> undep2 = new HashSet<>(alwaysList(second.getPoliciesToBeUndeployed()));
+
+ return undep1.equals(undep2);
}
/**