Generate notifications when policies change
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / depundep / SessionData.java
index 437d7a1..5b2a8ee 100644 (file)
@@ -23,8 +23,10 @@ package org.onap.policy.pap.main.rest.depundep;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 import org.apache.commons.lang3.tuple.Pair;
@@ -38,9 +40,11 @@ import org.onap.policy.models.provider.PolicyModelsProvider;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter.ToscaPolicyFilterBuilder;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOptVersion;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
+import org.onap.policy.pap.main.notification.PolicyPdpNotificationData;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -88,6 +92,18 @@ public class SessionData {
      */
     private final Map<ToscaPolicyTypeIdentifier, ToscaPolicyType> typeCache = new HashMap<>();
 
+    /**
+     * Policies to be deployed. This is just used to build up the data, which is then
+     * passed to the notifier once the update is "committed".
+     */
+    private final Map<ToscaPolicyIdentifier, PolicyPdpNotificationData> deploy = new HashMap<>();
+
+    /**
+     * Policies to be undeployed. This is just used to build up the data, which is then
+     * passed to the notifier once the update is "committed".
+     */
+    private final Map<ToscaPolicyIdentifier, PolicyPdpNotificationData> undeploy = new HashMap<>();
+
 
     /**
      * Constructs the object.
@@ -402,4 +418,90 @@ public class SessionData {
         logger.info("deleting DB group {}", group.getName());
         dao.deletePdpGroup(group.getName());
     }
+
+    /**
+     * Adds policy deployment data.
+     *
+     * @param policyId ID of the policy being deployed
+     * @param pdps PDPs to which the policy is being deployed
+     * @throws PfModelException if an error occurred
+     */
+    protected void trackDeploy(ToscaPolicyIdentifier policyId, Collection<String> pdps) throws PfModelException {
+        trackDeploy(policyId, new HashSet<>(pdps));
+    }
+
+    /**
+     * Adds policy deployment data.
+     *
+     * @param policyId ID of the policy being deployed
+     * @param pdps PDPs to which the policy is being deployed
+     * @throws PfModelException if an error occurred
+     */
+    protected void trackDeploy(ToscaPolicyIdentifier policyId, Set<String> pdps) throws PfModelException {
+        addData(policyId, pdps, deploy, undeploy);
+    }
+
+    /**
+     * Adds policy undeployment data.
+     *
+     * @param policyId ID of the policy being undeployed
+     * @param pdps PDPs to which the policy is being undeployed
+     * @throws PfModelException if an error occurred
+     */
+    protected void trackUndeploy(ToscaPolicyIdentifier policyId, Collection<String> pdps) throws PfModelException {
+        trackUndeploy(policyId, new HashSet<>(pdps));
+    }
+
+    /**
+     * Adds policy undeployment data.
+     *
+     * @param policyId ID of the policy being undeployed
+     * @param pdps PDPs to which the policy is being undeployed
+     * @throws PfModelException if an error occurred
+     */
+    protected void trackUndeploy(ToscaPolicyIdentifier policyId, Set<String> pdps) throws PfModelException {
+        addData(policyId, pdps, undeploy, deploy);
+    }
+
+    /**
+     * Adds policy deployment/undeployment data.
+     *
+     * @param policyId ID of the policy being deployed/undeployed
+     * @param pdps PDPs to which the policy is being deployed/undeployed
+     * @param addMap map to which it should be added
+     * @param removeMap map from which it should be removed
+     * @throws PfModelException if an error occurred
+     */
+    private void addData(ToscaPolicyIdentifier policyId, Set<String> pdps,
+                    Map<ToscaPolicyIdentifier, PolicyPdpNotificationData> addMap,
+                    Map<ToscaPolicyIdentifier, PolicyPdpNotificationData> removeMap) throws PfModelException {
+
+        PolicyPdpNotificationData removeData = removeMap.get(policyId);
+        if (removeData != null) {
+            removeData.removeAll(pdps);
+        }
+
+        ToscaPolicyIdentifierOptVersion optid = new ToscaPolicyIdentifierOptVersion(policyId);
+        ToscaPolicyTypeIdentifier policyType = getPolicy(optid).getTypeIdentifier();
+
+        addMap.computeIfAbsent(policyId, key -> new PolicyPdpNotificationData(policyId, policyType)).addAll(pdps);
+    }
+
+    /**
+     * Gets the policies to be deployed.
+     *
+     * @return the policies to be deployed
+     */
+    public Collection<PolicyPdpNotificationData> getDeployData() {
+        return deploy.values();
+    }
+
+    /**
+     * Gets the policies to be undeployed.
+     *
+     * @return the policies to be undeployed
+     */
+    public Collection<PolicyPdpNotificationData> getUndeployData() {
+        return undeploy.values();
+    }
 }