diff options
Diffstat (limited to 'main/src/main/java/org/onap')
7 files changed, 159 insertions, 19 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); } /** diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupDeployProvider.java b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupDeployProvider.java index 3c8ce4c8..56d2ad3f 100644 --- a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupDeployProvider.java +++ b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupDeployProvider.java @@ -262,11 +262,11 @@ public class PdpGroupDeployProvider extends ProviderBase { return false; } - Set<String> pdps = dbsub.getPdpInstances().stream().map(Pdp::getInstanceId).collect(Collectors.toSet()); for (ToscaConceptIdentifier policyId : deployed) { - data.trackDeploy(policyId, pdps, pdpGroup, dbsub.getPdpType()); + ToscaPolicy policyToBeDeployed = data.getPolicy(new ToscaConceptIdentifierOptVersion(policyId)); + data.trackDeploy(policyToBeDeployed, pdps, pdpGroup, dbsub.getPdpType()); } for (ToscaConceptIdentifier policyId : undeployed) { @@ -443,7 +443,8 @@ public class PdpGroupDeployProvider extends ProviderBase { subgroup.getPdpType(), subgroup.getPolicies().size()); Set<String> pdps = subgroup.getPdpInstances().stream().map(Pdp::getInstanceId).collect(Collectors.toSet()); - data.trackDeploy(desiredIdent, pdps, group.getName(), subgroup.getPdpType()); + ToscaPolicy policyToBeDeployed = data.getPolicy(new ToscaConceptIdentifierOptVersion(desiredIdent)); + data.trackDeploy(policyToBeDeployed, pdps, group.getName(), subgroup.getPdpType()); return true; }; diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeProvider.java b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeProvider.java index 374b5282..abea6966 100644 --- a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeProvider.java +++ b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeProvider.java @@ -34,6 +34,7 @@ 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.ToscaPolicy; import org.onap.policy.pap.main.comm.PdpMessageGenerator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -117,10 +118,12 @@ public class PdpGroupStateChangeProvider extends PdpMessageGenerator { final PolicyModelsProvider databaseProvider) throws PfModelException { String pdpGroupName = pdpGroup.getName(); for (final PdpSubGroup subGroup : pdpGroup.getPdpSubgroups()) { + List<ToscaPolicy> policies = getToscaPolicies(subGroup, databaseProvider); for (final Pdp pdp : subGroup.getPdpInstances()) { String pdpInstanceId = pdp.getInstanceId(); final PdpUpdate pdpUpdatemessage = - createPdpUpdateMessage(pdpGroupName, subGroup, pdpInstanceId, databaseProvider); + createPdpUpdateMessage(pdpGroup.getName(), subGroup, pdp.getInstanceId(), databaseProvider, + policies, policies, null); final PdpStateChange pdpStateChangeMessage = createPdpStateChangeMessage(pdpGroupName, subGroup, pdpInstanceId, pdpState); updateDeploymentStatus(pdpGroupName, subGroup.getPdpType(), pdpInstanceId, diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/ProviderBase.java b/main/src/main/java/org/onap/policy/pap/main/rest/ProviderBase.java index 7f013ac7..d64d4e36 100644 --- a/main/src/main/java/org/onap/policy/pap/main/rest/ProviderBase.java +++ b/main/src/main/java/org/onap/policy/pap/main/rest/ProviderBase.java @@ -74,7 +74,6 @@ public abstract class ProviderBase { */ private final PolicyModelsProviderFactoryWrapper daoFactory; - /** * Constructs the object. */ @@ -182,7 +181,8 @@ public abstract class ProviderBase { * @param updater function to update a group * @throws PfModelException if an error occurred */ - private void upgradeGroup(SessionData data, PdpGroup group, Updater updater) throws PfModelException { + private void upgradeGroup(SessionData data, PdpGroup group, Updater updater) + throws PfModelException { boolean updated = false; @@ -197,7 +197,6 @@ public abstract class ProviderBase { makeUpdates(data, group, subgroup); } - if (updated) { // something changed data.update(group); @@ -236,6 +235,8 @@ public abstract class ProviderBase { update.setPdpSubgroup(subgroup.getPdpType()); update.setPolicies(subgroup.getPolicies().stream().map(ToscaConceptIdentifierOptVersion::new) .map(ident -> getPolicy(data, ident)).collect(Collectors.toList())); + update.setPoliciesToBeDeployed(data.getPoliciesToBeDeployed()); + update.setPoliciesToBeUndeployed(data.getPoliciesToBeUndeployed()); return update; } diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/SessionData.java b/main/src/main/java/org/onap/policy/pap/main/rest/SessionData.java index b9807c79..c8cddbb7 100644 --- a/main/src/main/java/org/onap/policy/pap/main/rest/SessionData.java +++ b/main/src/main/java/org/onap/policy/pap/main/rest/SessionData.java @@ -25,8 +25,11 @@ import com.google.re2j.Pattern; import java.util.Collection; import java.util.Collections; import java.util.HashMap; +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 org.apache.commons.lang3.tuple.Pair; import org.onap.policy.models.base.PfModelException; @@ -91,6 +94,16 @@ public class SessionData { private final Map<ToscaConceptIdentifier, ToscaPolicyType> typeCache = new HashMap<>(); /** + * Map's a policy's identifier to the policies for deployment. + */ + private Map<ToscaConceptIdentifier, ToscaPolicy> policiesToBeDeployed = new HashMap<>(); + + /** + * Set of policies to be undeployed. + */ + private Set<ToscaConceptIdentifier> policiesToBeUndeployed = new HashSet<>(); + + /** * Tracks policy deployment status so notifications can be generated. */ private final DeploymentStatus deployStatus; @@ -358,6 +371,24 @@ public class SessionData { } /** + * Gets the list of policies to be deployed to the PDPs. + * + * @returns a list of policies to be deployed + */ + public List<ToscaPolicy> getPoliciesToBeDeployed() { + return new LinkedList<>(this.policiesToBeDeployed.values()); + } + + /** + * Gets the list of policies to be undeployed from the PDPs. + * + * @returns a list of policies to be undeployed + */ + public List<ToscaConceptIdentifier> getPoliciesToBeUndeployed() { + return new LinkedList<>(this.policiesToBeUndeployed); + } + + /** * Adds a group to the group cache, if it isn't already in the cache. * * @param group the group to be added @@ -422,14 +453,17 @@ public class SessionData { /** * Adds policy deployment data. * - * @param policyId ID of the policy being deployed + * @param policy policy being deployed * @param pdps PDPs to which the policy is being deployed * @param pdpGroup PdpGroup containing the PDP of interest * @param pdpType PDP type (i.e., PdpSubGroup) containing the PDP of interest * @throws PfModelException if an error occurred */ - protected void trackDeploy(ToscaConceptIdentifier policyId, Collection<String> pdps, String pdpGroup, + protected void trackDeploy(ToscaPolicy policy, Collection<String> pdps, String pdpGroup, String pdpType) throws PfModelException { + ToscaConceptIdentifier policyId = policy.getIdentifier(); + policiesToBeDeployed.put(policyId, policy); + addData(policyId, pdps, pdpGroup, pdpType, true); } @@ -444,6 +478,7 @@ public class SessionData { */ protected void trackUndeploy(ToscaConceptIdentifier policyId, Collection<String> pdps, String pdpGroup, String pdpType) throws PfModelException { + policiesToBeUndeployed.add(policyId); addData(policyId, pdps, pdpGroup, pdpType, false); } |