summaryrefslogtreecommitdiffstats
path: root/main/src
diff options
context:
space:
mode:
Diffstat (limited to 'main/src')
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/comm/PdpModifyRequestMap.java16
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/comm/msgdata/UpdateReq.java12
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/notification/DeploymentStatus.java10
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/notification/DeploymentTracker.java8
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/notification/PolicyCommonTracker.java245
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/notification/PolicyDeployTracker.java54
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/notification/PolicyNotifier.java201
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/notification/PolicyPdpNotificationData.java86
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/notification/PolicyTrackerData.java193
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/notification/PolicyUndeployTracker.java52
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupCreateOrUpdateProvider.java9
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupDeleteProvider.java4
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupDeployProvider.java24
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/rest/PolicyStatusControllerV1.java72
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/rest/PolicyStatusProvider.java95
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/rest/PolicyUndeployerImpl.java4
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/rest/ProviderBase.java13
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/rest/SessionData.java104
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/startstop/Main.java10
-rw-r--r--main/src/main/resources/META-INF/persistence.xml2
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/comm/PdpModifyRequestMapTest.java41
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/comm/msgdata/UpdateReqTest.java12
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/notification/PolicyCommonSupport.java94
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/notification/PolicyCommonTrackerTest.java347
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/notification/PolicyDeployTrackerTest.java250
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/notification/PolicyNotifierTest.java245
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/notification/PolicyPdpNotificationDataTest.java121
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/notification/PolicyTrackerDataTest.java350
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/notification/PolicyUndeployTrackerTest.java264
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/rest/ProviderSuper.java13
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupCreateOrUpdateProvider.java6
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeleteProvider.java4
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployProvider.java50
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/rest/TestPolicyStatusProvider.java152
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/rest/TestProviderBase.java36
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/rest/TestSessionData.java89
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/rest/e2e/End2EndBase.java30
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/rest/e2e/PolicyStatusTest.java12
-rw-r--r--main/src/test/resources/META-INF/persistence.xml2
-rw-r--r--main/src/test/resources/e2e/policyStatus.json65
40 files changed, 623 insertions, 2774 deletions
diff --git a/main/src/main/java/org/onap/policy/pap/main/comm/PdpModifyRequestMap.java b/main/src/main/java/org/onap/policy/pap/main/comm/PdpModifyRequestMap.java
index 85038dee..fb19b01a 100644
--- a/main/src/main/java/org/onap/policy/pap/main/comm/PdpModifyRequestMap.java
+++ b/main/src/main/java/org/onap/policy/pap/main/comm/PdpModifyRequestMap.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP PAP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2021 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -30,6 +30,7 @@ import java.util.List;
import java.util.Map;
import lombok.Setter;
import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.pap.concepts.PolicyNotification;
import org.onap.policy.models.pdp.concepts.Pdp;
import org.onap.policy.models.pdp.concepts.PdpGroup;
import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
@@ -45,6 +46,7 @@ import org.onap.policy.pap.main.comm.msgdata.Request;
import org.onap.policy.pap.main.comm.msgdata.RequestListener;
import org.onap.policy.pap.main.comm.msgdata.StateChangeReq;
import org.onap.policy.pap.main.comm.msgdata.UpdateReq;
+import org.onap.policy.pap.main.notification.DeploymentStatus;
import org.onap.policy.pap.main.notification.PolicyNotifier;
import org.onap.policy.pap.main.parameters.PdpModifyRequestMapParams;
import org.onap.policy.pap.main.parameters.RequestParams;
@@ -264,9 +266,13 @@ public class PdpModifyRequestMap {
List<PdpGroup> groups = dao.getFilteredPdpGroups(filter);
List<PdpGroup> updates = new ArrayList<>(1);
+ DeploymentStatus status = new DeploymentStatus(dao);
+
for (PdpGroup group : groups) {
if (removeFromGroup(pdpName, group)) {
updates.add(group);
+ status.loadByGroup(group.getName());
+ status.deleteDeployment(pdpName);
}
}
@@ -275,6 +281,12 @@ public class PdpModifyRequestMap {
} else {
dao.updatePdpGroups(updates);
+
+ PolicyNotification notification = new PolicyNotification();
+ status.flush(notification);
+
+ policyNotifier.publish(notification);
+
return true;
}
}
@@ -448,8 +460,6 @@ public class PdpModifyRequestMap {
logger.warn("removing {}", pdpName);
- policyNotifier.removePdp(pdpName);
-
// remove the PDP from all groups
try {
removeFromGroups(pdpName);
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 ada9d193..4b5b7f04 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
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP PAP
* ================================================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2021 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -77,9 +77,6 @@ public class UpdateReq extends RequestImpl {
return reason;
}
- Set<ToscaConceptIdentifier> actualSet = new HashSet<>(alwaysList(response.getPolicies()));
- getNotifier().processResponse(response.getName(), actualSet);
-
PdpUpdate message = getMessage();
if (!StringUtils.equals(message.getPdpGroup(), response.getPdpGroup())) {
@@ -94,11 +91,14 @@ public class UpdateReq extends RequestImpl {
return null;
}
- // see if the policies match
-
+ Set<ToscaConceptIdentifier> actualSet = new HashSet<>(alwaysList(response.getPolicies()));
Set<ToscaConceptIdentifier> expectedSet = new HashSet<>(alwaysList(message.getPolicies()).stream()
.map(ToscaPolicy::getIdentifier).collect(Collectors.toSet()));
+ getNotifier().processResponse(response.getName(), message.getPdpGroup(), expectedSet, actualSet);
+
+ // see if the policies match
+
if (!actualSet.equals(expectedSet)) {
// need to undeploy the policies that are expected, but missing from the
// response
diff --git a/main/src/main/java/org/onap/policy/pap/main/notification/DeploymentStatus.java b/main/src/main/java/org/onap/policy/pap/main/notification/DeploymentStatus.java
index ef97fae9..7b627803 100644
--- a/main/src/main/java/org/onap/policy/pap/main/notification/DeploymentStatus.java
+++ b/main/src/main/java/org/onap/policy/pap/main/notification/DeploymentStatus.java
@@ -41,7 +41,14 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.onap.policy.pap.main.notification.StatusAction.Action;
/**
- * Collection of Policy Deployment Status records.
+ * Collection of Policy Deployment Status records. The sequence of method invocations
+ * should be as follows:
+ * <ol>
+ * <li>{@link #loadByGroup(String)}</li>
+ * <li>various other methods</li>
+ * <li>repeat the previous steps as appropriate</li>
+ * <li>{@link #flush(PolicyNotification)}</li>
+ * </ol>
*/
public class DeploymentStatus {
/**
@@ -115,6 +122,7 @@ public class DeploymentStatus {
* @param notif notification to which to add policy status
*/
public void flush(PolicyNotification notif) {
+ // must add notifications BEFORE deleting undeployments
addNotifications(notif);
deleteUndeployments();
flush();
diff --git a/main/src/main/java/org/onap/policy/pap/main/notification/DeploymentTracker.java b/main/src/main/java/org/onap/policy/pap/main/notification/DeploymentTracker.java
index cef70a39..4522debb 100644
--- a/main/src/main/java/org/onap/policy/pap/main/notification/DeploymentTracker.java
+++ b/main/src/main/java/org/onap/policy/pap/main/notification/DeploymentTracker.java
@@ -146,6 +146,14 @@ public class DeploymentTracker {
newStatus.setPolicyTypeId(status.getPolicyTypeId());
newStatus.setPolicyTypeVersion(status.getPolicyTypeVersion());
+ /*
+ * Adding the status to the "added" set may be a bit unexpected, but when all
+ * status records are deleted from the group, we don't actually undeploy the
+ * policy from the subgroup. Instead, we leave it in the subgroup so that as
+ * soon as a PDP registers, we immediately deploy the policy to the PDP and
+ * continue on; the client can always undeploy the policy when it receives the
+ * notification, if so desired.
+ */
notif.getAdded().add(newStatus);
}
}
diff --git a/main/src/main/java/org/onap/policy/pap/main/notification/PolicyCommonTracker.java b/main/src/main/java/org/onap/policy/pap/main/notification/PolicyCommonTracker.java
deleted file mode 100644
index 3529ccb7..00000000
--- a/main/src/main/java/org/onap/policy/pap/main/notification/PolicyCommonTracker.java
+++ /dev/null
@@ -1,245 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * ONAP PAP
- * ================================================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
- * Modifications Copyright (C) 2021 Nordix Foundation.
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.pap.main.notification;
-
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Optional;
-import java.util.Set;
-import java.util.function.BiPredicate;
-import java.util.stream.Collectors;
-import org.onap.policy.models.pap.concepts.PolicyStatus;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
-
-/**
- * Common super class for deploy and undeploy trackers.
- */
-public abstract class PolicyCommonTracker {
-
- /**
- * Maps a policy id to its deployment data. The subclass determines when an entry is
- * removed.
- *
- * <p/>
- * Use a LinkedHashMap, because we'll be doing lots of iteration over the map, and
- * iteration over a LinkedHashMap is faster than over a plain HashMap.
- */
- private final Map<ToscaConceptIdentifier, PolicyTrackerData> policy2data = new LinkedHashMap<>();
-
-
- /**
- * Constructs the object.
- */
- protected PolicyCommonTracker() {
- super();
- }
-
- /**
- * Gets the status of all policies being tracked.
- *
- * @return the status of all policies
- */
- public List<PolicyStatus> getStatus() {
- return policy2data.entrySet().stream().map(this::makeStatus).collect(Collectors.toList());
- }
-
- /**
- * Gets the status of all versions of a policy.
- *
- * @param policyId ID of the policy of interest, without the version
- * @return the status of all versions of the policy having the given ID
- */
- public List<PolicyStatus> getStatus(String policyId) {
- // version is not specified - have to scan the whole list
- return policy2data.entrySet().stream().filter(ent -> ent.getKey().getName().equals(policyId))
- .map(this::makeStatus).collect(Collectors.toList());
- }
-
- /**
- * Gets the status of a particular policy.
- *
- * @param ident identifier of the policy of interest
- * @return the status of the given policy, or empty if the policy is not found
- */
- public Optional<PolicyStatus> getStatus(ToscaConceptIdentifier ident) {
- ToscaConceptIdentifier ident2 = new ToscaConceptIdentifier(ident.getName(), ident.getVersion());
- PolicyTrackerData data = policy2data.get(ident2);
- return Optional.ofNullable(data == null ? null : makeStatus(ident2, data));
- }
-
- /**
- * Adds data to the tracker.
- *
- * @param data data to be added to the tracker
- */
- public void addData(PolicyPdpNotificationData data) {
- policy2data.computeIfAbsent(data.getPolicyId(), policyId -> new PolicyTrackerData(data.getPolicyType()))
- .addPdps(data.getPdps());
- }
-
- /**
- * Removes a set of PDPs from all policies within the tracker.
- *
- * @param notifyData data identifying the policy and the PDPs to be removed from it
- */
- public void removeData(PolicyPdpNotificationData notifyData) {
-
- policy2data.computeIfPresent(notifyData.getPolicyId(), (policyId, data) -> {
-
- if (!data.removePdps(notifyData.getPdps())) {
- // not complete yet
- return data;
- }
-
- // this policy is complete
- return (shouldRemove(data) ? null : data);
- });
- }
-
- /**
- * Removes a PDP from all policies within the tracker.
- *
- * @param pdp PDP to be removed
- * @param statusList status messages are added here if policies become complete as a
- * result of this operation
- */
- public void removePdp(String pdp, List<PolicyStatus> statusList) {
- updateMap(statusList, (policyId, data) -> data.removePdp(pdp));
- }
-
- /**
- * Processes a response from a PDP.
- *
- * @param pdp PDP of interest
- * @param activePolicies policies that are still active on the PDP, as specified in
- * the response
- * @param statusList status messages are added here if policies become complete as a
- * result of this operation
- */
- public void processResponse(String pdp, Collection<ToscaConceptIdentifier> activePolicies,
- List<PolicyStatus> statusList) {
- processResponse(pdp, new HashSet<>(activePolicies), statusList);
- }
-
- /**
- * Processes a response from a PDP.
- *
- * @param pdp PDP of interest
- * @param activePolicies policies that are still active on the PDP, as specified in
- * the response
- * @param statusList status messages are added here if policies become complete as a
- * result of this operation
- */
- public void processResponse(String pdp, Set<ToscaConceptIdentifier> activePolicies, List<PolicyStatus> statusList) {
- updateMap(statusList, (policyId, data) -> updateData(pdp, data, activePolicies.contains(policyId)));
- }
-
- /**
- * Updates the map.
- *
- * <p/>
- * Note: this iterates through the whole map. While it may be more efficient to
- * iterate through just the policies relevant to the PDP, that would complicate the
- * code and complicate the testing. In addition, this should still perform well
- * enough, but if not, it can always be enhanced.
- *
- * @param statusList status messages are added here if policies become complete as a
- * result of this operation
- * @param updater function to update a policy's data. Returns {@code true} if the
- * policy is complete (i.e., no longer awaiting any responses)
- */
- private void updateMap(List<PolicyStatus> statusList,
- BiPredicate<ToscaConceptIdentifier, PolicyTrackerData> updater) {
-
- Iterator<Entry<ToscaConceptIdentifier, PolicyTrackerData>> iter = policy2data.entrySet().iterator();
- while (iter.hasNext()) {
- Entry<ToscaConceptIdentifier, PolicyTrackerData> ent = iter.next();
-
- ToscaConceptIdentifier policyId = ent.getKey();
- PolicyTrackerData data = ent.getValue();
-
- if (!updater.test(policyId, data)) {
- // not complete yet
- continue;
- }
-
- // this policy is complete - notify
- statusList.add(makeStatus(ent));
-
- if (shouldRemove(data)) {
- iter.remove();
- }
- }
- }
-
- /**
- * Updates the policy data, based on a response from a PDP.
- *
- * @param pdp PDP whose response was just received
- * @param data data associated with the policy of interest
- * @param stillActive {@code true} if the policy is still active for the PDP,
- * {@code false} otherwise
- * @return {@code true} if the policy is complete (i.e., no longer awaiting any
- * responses), {@code false} otherwise
- */
- protected abstract boolean updateData(String pdp, PolicyTrackerData data, boolean stillActive);
-
- /**
- * Determines if a policy should be removed from the tracker, based on the state of
- * its data.
- *
- * @param data data associated with the policy of interest
- * @return {@code true} if the policy should be removed from the tracker,
- * {@code false} otherwise
- */
- protected abstract boolean shouldRemove(PolicyTrackerData data);
-
- /**
- * Makes a status notification for the given policy entry.
- *
- * @param entry policy entry
- * @return a new status notification
- */
- private PolicyStatus makeStatus(Map.Entry<ToscaConceptIdentifier, PolicyTrackerData> entry) {
- return makeStatus(entry.getKey(), entry.getValue());
- }
-
- /**
- * Makes a status notification for the given policy.
- *
- * @param policyId policy ID
- * @param data data to be used to set the status fields
- * @return a new status notification
- */
- private PolicyStatus makeStatus(ToscaConceptIdentifier policyId, PolicyTrackerData data) {
-
- PolicyStatus status = new PolicyStatus(data.getPolicyType(), policyId);
- data.putValuesInto(status);
- return status;
- }
-}
diff --git a/main/src/main/java/org/onap/policy/pap/main/notification/PolicyDeployTracker.java b/main/src/main/java/org/onap/policy/pap/main/notification/PolicyDeployTracker.java
deleted file mode 100644
index 17a5e211..00000000
--- a/main/src/main/java/org/onap/policy/pap/main/notification/PolicyDeployTracker.java
+++ /dev/null
@@ -1,54 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * ONAP PAP
- * ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.pap.main.notification;
-
-/**
- * Tracker for policy deployments to PDPs.
- *
- * <p/>
- * Policies are not removed from the internal map until all of the sets contained within
- * the data are empty. This may be the result of a PDP being removed from the system
- * because it is no longer responsive, or because the PDP Subgroup that contained it is
- * deleted.
- */
-public class PolicyDeployTracker extends PolicyCommonTracker {
-
- /**
- * Constructs the object.
- */
- public PolicyDeployTracker() {
- super();
- }
-
- @Override
- protected boolean updateData(String pdp, PolicyTrackerData data, boolean stillActive) {
- return (stillActive ? data.success(pdp) : data.fail(pdp));
- }
-
- /**
- * Returns {@code true} only when the data is <i>completely empty</i> (i.e., it has no
- * more PDPs)
- */
- @Override
- protected boolean shouldRemove(PolicyTrackerData data) {
- return data.isEmpty();
- }
-}
diff --git a/main/src/main/java/org/onap/policy/pap/main/notification/PolicyNotifier.java b/main/src/main/java/org/onap/policy/pap/main/notification/PolicyNotifier.java
index c13e9b78..66b83ccc 100644
--- a/main/src/main/java/org/onap/policy/pap/main/notification/PolicyNotifier.java
+++ b/main/src/main/java/org/onap/policy/pap/main/notification/PolicyNotifier.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP PAP
* ================================================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2020-2021 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -21,24 +21,11 @@
package org.onap.policy.pap.main.notification;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
import java.util.Set;
-import java.util.stream.Collectors;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.pap.concepts.PolicyNotification;
-import org.onap.policy.models.pap.concepts.PolicyStatus;
-import org.onap.policy.models.pdp.concepts.Pdp;
-import org.onap.policy.models.pdp.concepts.PdpGroup;
-import org.onap.policy.models.pdp.concepts.PdpSubGroup;
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.models.tosca.authorative.concepts.ToscaPolicyFilter;
import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
import org.onap.policy.pap.main.comm.Publisher;
import org.onap.policy.pap.main.comm.QueueToken;
@@ -56,15 +43,7 @@ public class PolicyNotifier {
*/
private final Publisher<PolicyNotification> publisher;
- /**
- * Deployment tracker.
- */
- private final PolicyDeployTracker deployTracker = makeDeploymentTracker();
-
- /**
- * Undeployment tracker.
- */
- private final PolicyUndeployTracker undeployTracker = makeUndeploymentTracker();
+ private final PolicyModelsProviderFactoryWrapper daoFactory;
/**
@@ -72,170 +51,38 @@ public class PolicyNotifier {
*
* @param publisher notification publisher
* @param daoFactory factory used to load policy deployment data from the DB
- * @throws PfModelException if a DB error occurs
*/
- public PolicyNotifier(Publisher<PolicyNotification> publisher, PolicyModelsProviderFactoryWrapper daoFactory)
- throws PfModelException {
+ public PolicyNotifier(Publisher<PolicyNotification> publisher, PolicyModelsProviderFactoryWrapper daoFactory) {
this.publisher = publisher;
-
- try (PolicyModelsProvider dao = daoFactory.create()) {
- Map<ToscaConceptIdentifier, ToscaConceptIdentifier> id2type = loadPolicyTypes(dao);
- loadPolicies(dao, id2type);
- }
- }
-
- /**
- * Loads policy types from the DB.
- *
- * @param dao provider used to retrieve policies from the DB
- * @return a mapping from policy id to policy type
- * @throws PfModelException if a DB error occurs
- */
- private Map<ToscaConceptIdentifier, ToscaConceptIdentifier> loadPolicyTypes(PolicyModelsProvider dao)
- throws PfModelException {
-
- Map<ToscaConceptIdentifier, ToscaConceptIdentifier> id2type = new HashMap<>();
-
- for (ToscaPolicy policy : dao.getFilteredPolicyList(ToscaPolicyFilter.builder().build())) {
- id2type.put(policy.getIdentifier(), policy.getTypeIdentifier());
- }
-
- return id2type;
- }
-
- /**
- * Loads deployed policies.
- *
- * @param id2type mapping from policy id to policy type
- * @param dao provider used to retrieve policies from the DB
- * @throws PfModelException if a DB error occurs
- */
- private void loadPolicies(PolicyModelsProvider dao, Map<ToscaConceptIdentifier, ToscaConceptIdentifier> id2type)
- throws PfModelException {
- for (PdpGroup group : dao.getPdpGroups(null)) {
- for (PdpSubGroup subgrp : group.getPdpSubgroups()) {
- loadPolicies(id2type, group, subgrp);
- }
- }
- }
-
- /**
- * Loads a subgroup's deployed policies.
- *
- * @param id2type maps a policy id to its type
- * @param group group containing the subgroup
- * @param subgrp subgroup whose policies are to be loaded
- */
- private void loadPolicies(Map<ToscaConceptIdentifier, ToscaConceptIdentifier> id2type, PdpGroup group,
- PdpSubGroup subgrp) {
-
- for (ToscaConceptIdentifier policyId : subgrp.getPolicies()) {
-
- ToscaConceptIdentifier type = id2type.get(policyId);
- if (type == null) {
- logger.error("group {}:{} refers to non-existent policy {}", group.getName(), subgrp.getPdpType(),
- policyId);
- continue;
- }
-
- PolicyPdpNotificationData data = new PolicyPdpNotificationData(policyId, type);
- data.addAll(subgrp.getPdpInstances().stream().map(Pdp::getInstanceId).collect(Collectors.toList()));
- deployTracker.addData(data);
- }
- }
-
- /**
- * Gets the status of all deployed policies.
- *
- * @return the status of all deployed policies
- */
- public synchronized List<PolicyStatus> getStatus() {
- return deployTracker.getStatus();
- }
-
- /**
- * Gets the status of a particular deployed policy.
- *
- * @param policyId ID of the policy of interest, without the version
- * @return the status of all deployed policies matching the given identifier
- */
- public synchronized List<PolicyStatus> getStatus(String policyId) {
- return deployTracker.getStatus(policyId);
- }
-
- /**
- * Gets the status of a particular deployed policy.
- *
- * @param ident identifier of the policy of interest
- * @return the status of the given policy, or empty if the policy is not found
- */
- public synchronized Optional<PolicyStatus> getStatus(ToscaConceptIdentifier ident) {
- return deployTracker.getStatus(ident);
- }
-
- /**
- * Adds data to the deployment tracker. If a PDP appears within the undeployment
- * tracker, then it's removed from there.
- *
- * @param data data to be added
- */
- public synchronized void addDeploymentData(PolicyPdpNotificationData data) {
- undeployTracker.removeData(data);
- deployTracker.addData(data);
- }
-
- /**
- * Adds data to the undeployment tracker. If a PDP appears within the deployment
- * tracker, then it's removed from there.
- *
- * @param data data to be added
- */
- public synchronized void addUndeploymentData(PolicyPdpNotificationData data) {
- deployTracker.removeData(data);
- undeployTracker.addData(data);
- }
-
- /**
- * Processes a response from a PDP.
- *
- * @param pdp PDP of interest
- * @param activePolicies policies that are still active on the PDP, as specified in
- * the response
- */
- public synchronized void processResponse(String pdp, Collection<ToscaConceptIdentifier> activePolicies) {
- processResponse(pdp, new HashSet<>(activePolicies));
+ this.daoFactory = daoFactory;
}
/**
* Processes a response from a PDP.
*
* @param pdp PDP of interest
- * @param activePolicies policies that are still active on the PDP, as specified in
+ * @param pdpGroup name of the PdpGroup containing the PDP
+ * @param expectedPolicies policies that expected to be deployed on the PDP
+ * @param actualPolicies policies that are still active on the PDP, as specified in
* the response
*/
- public synchronized void processResponse(String pdp, Set<ToscaConceptIdentifier> activePolicies) {
- PolicyNotification notification = new PolicyNotification();
+ public synchronized void processResponse(String pdp, String pdpGroup, Set<ToscaConceptIdentifier> expectedPolicies,
+ Set<ToscaConceptIdentifier> actualPolicies) {
- undeployTracker.processResponse(pdp, activePolicies, notification.getDeleted());
- deployTracker.processResponse(pdp, activePolicies, notification.getAdded());
+ try (PolicyModelsProvider dao = daoFactory.create()) {
+ DeploymentStatus status = makeDeploymentTracker(dao);
+ status.loadByGroup(pdpGroup);
+ status.completeDeploy(pdp, expectedPolicies, actualPolicies);
- publish(notification);
- }
+ PolicyNotification notification = new PolicyNotification();
+ status.flush(notification);
- /**
- * Removes a PDP from any policies still awaiting responses from it, generating
- * notifications for any of those policies that become complete as a result.
- *
- * @param pdp PDP to be removed
- */
- public synchronized void removePdp(String pdp) {
- PolicyNotification notification = new PolicyNotification();
+ publish(notification);
- undeployTracker.removePdp(pdp, notification.getDeleted());
- deployTracker.removePdp(pdp, notification.getAdded());
-
- publish(notification);
+ } catch (PfModelException e) {
+ logger.warn("cannot update deployment status", e);
+ }
}
/**
@@ -243,7 +90,7 @@ public class PolicyNotifier {
*
* @param notification notification to be published
*/
- private void publish(PolicyNotification notification) {
+ public synchronized void publish(PolicyNotification notification) {
if (!notification.isEmpty()) {
publisher.enqueue(new QueueToken<>(notification));
}
@@ -252,11 +99,7 @@ public class PolicyNotifier {
// the following methods may be overridden by junit tests
- protected PolicyDeployTracker makeDeploymentTracker() {
- return new PolicyDeployTracker();
- }
-
- protected PolicyUndeployTracker makeUndeploymentTracker() {
- return new PolicyUndeployTracker();
+ protected DeploymentStatus makeDeploymentTracker(PolicyModelsProvider dao) {
+ return new DeploymentStatus(dao);
}
}
diff --git a/main/src/main/java/org/onap/policy/pap/main/notification/PolicyPdpNotificationData.java b/main/src/main/java/org/onap/policy/pap/main/notification/PolicyPdpNotificationData.java
deleted file mode 100644
index b93f8930..00000000
--- a/main/src/main/java/org/onap/policy/pap/main/notification/PolicyPdpNotificationData.java
+++ /dev/null
@@ -1,86 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * ONAP PAP
- * ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2021 Nordix Foundation.
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.pap.main.notification;
-
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Set;
-import lombok.Getter;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
-
-/**
- * Data used to track deploy/undeploy of a policy to PDPs.
- */
-@Getter
-public class PolicyPdpNotificationData {
- private final ToscaConceptIdentifier policyId;
- private final ToscaConceptIdentifier policyType;
- private final Set<String> pdps = new HashSet<>();
-
-
- /**
- * Constructs the object.
- *
- * @param policyId ID of the policy being deployed/undeployed
- * @param policyType type of the associated policy
- */
- public PolicyPdpNotificationData(ToscaConceptIdentifier policyId, ToscaConceptIdentifier policyType) {
- this.policyId = policyId;
- this.policyType = policyType;
- }
-
- /**
- * Determines if there are any PDPs in the data.
- *
- * @return {@code true} if the data contains at least one PDP, {@code false} otherwise
- */
- public boolean isEmpty() {
- return pdps.isEmpty();
- }
-
- /**
- * Adds a PDP to the set of PDPs.
- *
- * @param pdp PDP to be added
- */
- public void add(String pdp) {
- pdps.add(pdp);
- }
-
- /**
- * Adds PDPs to the set of PDPs.
- *
- * @param pdps PDPs to be added
- */
- public void addAll(Collection<String> pdps) {
- this.pdps.addAll(pdps);
- }
-
- /**
- * Removes PDPs from the set of PDPs.
- *
- * @param pdps PDPs to be removed
- */
- public void removeAll(Collection<String> pdps) {
- this.pdps.removeAll(pdps);
- }
-}
diff --git a/main/src/main/java/org/onap/policy/pap/main/notification/PolicyTrackerData.java b/main/src/main/java/org/onap/policy/pap/main/notification/PolicyTrackerData.java
deleted file mode 100644
index 7dfb378f..00000000
--- a/main/src/main/java/org/onap/policy/pap/main/notification/PolicyTrackerData.java
+++ /dev/null
@@ -1,193 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * ONAP PAP
- * ================================================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2021 Nordix Foundation.
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.pap.main.notification;
-
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Set;
-import lombok.Getter;
-import org.onap.policy.models.pap.concepts.PolicyStatus;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
-
-/**
- * Data associated with a policy, used by PolicyTracker. PDPs start in
- * {@link #incompletePdps} and are moved to either {@link #successPdps} or
- * {@link #failPdps}, depending on their response. Objects of this type are not
- * multi-thread safe.
- */
-public class PolicyTrackerData {
- /**
- * The policy type associated with the policy.
- */
- @Getter
- private final ToscaConceptIdentifier policyType;
-
- /**
- * PDPs that have successfully completed an update of the policy.
- */
- private final Set<String> successPdps = new HashSet<>();
-
- /**
- * PDPs that have failed to complete an update of the policy.
- */
- private final Set<String> failPdps = new HashSet<>();
-
- /**
- * PDPs for which we're still awaiting a response.
- */
- private final Set<String> incompletePdps = new HashSet<>();
-
-
- /**
- * Constructs the object.
- *
- * @param policyType policy type
- */
- public PolicyTrackerData(ToscaConceptIdentifier policyType) {
- this.policyType = policyType;
- }
-
- /**
- * Determines if this is complete (i.e., it is not waiting for responses from any
- * other PDPs).
- *
- * @return {@code true} if this is complete, {@code false} otherwise
- */
- public boolean isComplete() {
- return incompletePdps.isEmpty();
- }
-
- /**
- * Determines if everything has succeeded.
- *
- * @return {@code true} if this is complete <i>and</i> nothing has failed,
- * {@code false} otherwise
- */
- public boolean allSucceeded() {
- return (failPdps.isEmpty() && incompletePdps.isEmpty());
- }
-
- /**
- * Determines if all of the sets within the data are empty (i.e., contain no PDPs).
- *
- * @return {@code true} if the data is completely empty, {@code false} otherwise
- */
- public boolean isEmpty() {
- return (successPdps.isEmpty() && failPdps.isEmpty() && incompletePdps.isEmpty());
- }
-
- /**
- * Puts the values from this data into the given status object.
- *
- * @param status object whose values are to be set
- */
- public void putValuesInto(PolicyStatus status) {
- status.setFailureCount(failPdps.size());
- status.setIncompleteCount(incompletePdps.size());
- status.setSuccessCount(successPdps.size());
- }
-
- /**
- * Adds PDPs to {@link #incompletePdps}, removing them from any other sets in which
- * they appear.
- *
- * @param pdps PDPs to be added
- */
- public void addPdps(Collection<String> pdps) {
- successPdps.removeAll(pdps);
- failPdps.removeAll(pdps);
-
- incompletePdps.addAll(pdps);
- }
-
- /**
- * Removes PDPs from the sets.
- *
- * @param pdps PDPs to be removed
- * @return {@code true} if anything changed and the policy is now complete, {@code false} otherwise
- */
- public boolean removePdps(Collection<String> pdps) {
- boolean changed = successPdps.removeAll(pdps);
- changed = failPdps.removeAll(pdps) || changed;
- changed = incompletePdps.removeAll(pdps) || changed;
-
- return (changed && incompletePdps.isEmpty());
- }
-
- /**
- * Removes a PDP from all sets.
- *
- * @param pdp PDP to be removed
- * @return {@code true} if anything changed and the policy is now complete, {@code false} otherwise
- */
- public boolean removePdp(String pdp) {
- boolean changed = successPdps.remove(pdp);
- changed = failPdps.remove(pdp) || changed;
- changed = incompletePdps.remove(pdp) || changed;
-
- return (changed && incompletePdps.isEmpty());
- }
-
- /**
- * Indicates that a PDP has successfully processed this policy.
- *
- * @param pdp the PDP of interest
- * @return {@code true} if the policy is now complete, {@code false} otherwise
- */
- public boolean success(String pdp) {
- return complete(pdp, successPdps, failPdps);
- }
-
- /**
- * Indicates that a PDP has processed this policy, but was unsuccessful.
- *
- * @param pdp the PDP of interest
- * @return {@code true} if the policy is now complete, {@code false} otherwise
- */
- public boolean fail(String pdp) {
- return complete(pdp, failPdps, successPdps);
- }
-
- /**
- * Indicates that a PDP has processed this policy, either successfully or
- * unsuccessfully.
- *
- * @param pdp the PDP of interest
- * @param addSet set to which the PDP should be added
- * @param removeSet set from which the PDP should be removed
- * @return {@code true} if the policy is now complete, {@code false} otherwise
- */
- private boolean complete(String pdp, Set<String> addSet, Set<String> removeSet) {
- if (incompletePdps.remove(pdp) || removeSet.remove(pdp)) {
- // successfully removed from one of the sets
- addSet.add(pdp);
- return incompletePdps.isEmpty();
- }
-
- /*
- * Else: wasn't in either set, thus it's already in the "addSet" or it isn't
- * relevant to this policy. Either way, just discard it without triggering any new
- * notification.
- */
- return false;
- }
-}
diff --git a/main/src/main/java/org/onap/policy/pap/main/notification/PolicyUndeployTracker.java b/main/src/main/java/org/onap/policy/pap/main/notification/PolicyUndeployTracker.java
deleted file mode 100644
index af2f0b6a..00000000
--- a/main/src/main/java/org/onap/policy/pap/main/notification/PolicyUndeployTracker.java
+++ /dev/null
@@ -1,52 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * ONAP PAP
- * ================================================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.pap.main.notification;
-
-/**
- * Tracker for policy undeployments from PDPs.
- *
- * <p/>
- * Policies are removed from the internal map when they are no longer waiting for
- * responses from any PDPs.
- */
-public class PolicyUndeployTracker extends PolicyCommonTracker {
-
- /**
- * Constructs the object.
- */
- public PolicyUndeployTracker() {
- super();
- }
-
- @Override
- protected boolean updateData(String pdp, PolicyTrackerData data, boolean stillActive) {
- // note: still active means the policy wasn't undeployed, thus it's a failure
- return (stillActive ? data.fail(pdp) : data.success(pdp));
- }
-
- /**
- * Returns {@code true} only when the data is all successful.
- */
- @Override
- protected boolean shouldRemove(PolicyTrackerData data) {
- return data.allSucceeded();
- }
-}
diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupCreateOrUpdateProvider.java b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupCreateOrUpdateProvider.java
index 00db7cc0..39608cdd 100644
--- a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupCreateOrUpdateProvider.java
+++ b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupCreateOrUpdateProvider.java
@@ -3,7 +3,7 @@
* ONAP PAP
* ================================================================================
* Copyright (C) 2019-2021 Nordix Foundation.
- * Modifications Copyright (C) 2019 AT&T Intellectual Property.
+ * Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -303,7 +303,7 @@ public class PdpGroupCreateOrUpdateProvider extends ProviderBase {
// this subgroup no longer appears - notify its PDPs
updated = true;
notifyPdpsDelSubGroup(data, subgrp);
- trackPdpsDelSubGroup(data, subgrp);
+ trackPdpsDelSubGroup(data, dbgroup.getName(), subgrp);
}
}
@@ -337,14 +337,15 @@ public class PdpGroupCreateOrUpdateProvider extends ProviderBase {
* Tracks PDP responses when their subgroup is removed.
*
* @param data session data
+ * @param pdpGroup PdpGroup name
* @param subgrp subgroup that is being removed
* @throws PfModelException if an error occurred
*/
- private void trackPdpsDelSubGroup(SessionData data, PdpSubGroup subgrp) throws PfModelException {
+ private void trackPdpsDelSubGroup(SessionData data, String pdpGroup, PdpSubGroup subgrp) throws PfModelException {
Set<String> pdps = subgrp.getPdpInstances().stream().map(Pdp::getInstanceId).collect(Collectors.toSet());
for (ToscaConceptIdentifier policyId : subgrp.getPolicies()) {
- data.trackUndeploy(policyId, pdps);
+ data.trackUndeploy(policyId, pdps, pdpGroup, subgrp.getPdpType());
}
}
diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupDeleteProvider.java b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupDeleteProvider.java
index 38851cd3..0170197c 100644
--- a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupDeleteProvider.java
+++ b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupDeleteProvider.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP PAP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2020-2021 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -158,7 +158,7 @@ public class PdpGroupDeleteProvider extends ProviderBase {
logger.info("remove policy {} from subgroup {} {} count={}", ident,
group.getName(), subgroup.getPdpType(), subgroup.getPolicies().size());
- data.trackUndeploy(ident, pdps);
+ data.trackUndeploy(ident, pdps, group.getName(), subgroup.getPdpType());
}
}
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 c30ee8f9..3c8ce4c8 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
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP PAP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2020-2021 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -202,14 +202,14 @@ public class PdpGroupDeployProvider extends ProviderBase {
switch (subgrp.getAction()) {
case POST:
- updated = addPolicies(data, dbsub, subgrp);
+ updated = addPolicies(data, dbgroup.getName(), dbsub, subgrp);
break;
case DELETE:
- updated = deletePolicies(data, dbsub, subgrp);
+ updated = deletePolicies(data, dbgroup.getName(), dbsub, subgrp);
break;
case PATCH:
default:
- updated = updatePolicies(data, dbsub, subgrp);
+ updated = updatePolicies(data, dbgroup.getName(), dbsub, subgrp);
break;
}
@@ -222,7 +222,7 @@ public class PdpGroupDeployProvider extends ProviderBase {
return false;
}
- private boolean addPolicies(SessionData data, PdpSubGroup dbsub, DeploymentSubGroup subgrp)
+ private boolean addPolicies(SessionData data, String pdpGroup, PdpSubGroup dbsub, DeploymentSubGroup subgrp)
throws PfModelException {
Set<ToscaConceptIdentifier> policies = new LinkedHashSet<>(dbsub.getPolicies());
@@ -232,10 +232,10 @@ public class PdpGroupDeployProvider extends ProviderBase {
subgrp2.getPolicies().clear();
subgrp2.getPolicies().addAll(policies);
- return updatePolicies(data, dbsub, subgrp2);
+ return updatePolicies(data, pdpGroup, dbsub, subgrp2);
}
- private boolean deletePolicies(SessionData data, PdpSubGroup dbsub, DeploymentSubGroup subgrp)
+ private boolean deletePolicies(SessionData data, String pdpGroup, PdpSubGroup dbsub, DeploymentSubGroup subgrp)
throws PfModelException {
Set<ToscaConceptIdentifier> policies = new LinkedHashSet<>(dbsub.getPolicies());
@@ -245,10 +245,10 @@ public class PdpGroupDeployProvider extends ProviderBase {
subgrp2.getPolicies().clear();
subgrp2.getPolicies().addAll(policies);
- return updatePolicies(data, dbsub, subgrp2);
+ return updatePolicies(data, pdpGroup, dbsub, subgrp2);
}
- private boolean updatePolicies(SessionData data, PdpSubGroup dbsub, DeploymentSubGroup subgrp)
+ private boolean updatePolicies(SessionData data, String pdpGroup, PdpSubGroup dbsub, DeploymentSubGroup subgrp)
throws PfModelException {
Set<ToscaConceptIdentifier> undeployed = new HashSet<>(dbsub.getPolicies());
@@ -266,11 +266,11 @@ public class PdpGroupDeployProvider extends ProviderBase {
Set<String> pdps = dbsub.getPdpInstances().stream().map(Pdp::getInstanceId).collect(Collectors.toSet());
for (ToscaConceptIdentifier policyId : deployed) {
- data.trackDeploy(policyId, pdps);
+ data.trackDeploy(policyId, pdps, pdpGroup, dbsub.getPdpType());
}
for (ToscaConceptIdentifier policyId : undeployed) {
- data.trackUndeploy(policyId, pdps);
+ data.trackUndeploy(policyId, pdps, pdpGroup, dbsub.getPdpType());
}
dbsub.setPolicies(new ArrayList<>(subgrp.getPolicies()));
@@ -443,7 +443,7 @@ 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);
+ data.trackDeploy(desiredIdent, pdps, group.getName(), subgroup.getPdpType());
return true;
};
diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PolicyStatusControllerV1.java b/main/src/main/java/org/onap/policy/pap/main/rest/PolicyStatusControllerV1.java
index bc838621..ec7c00a9 100644
--- a/main/src/main/java/org/onap/policy/pap/main/rest/PolicyStatusControllerV1.java
+++ b/main/src/main/java/org/onap/policy/pap/main/rest/PolicyStatusControllerV1.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP PAP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2021 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -29,30 +29,30 @@ import io.swagger.annotations.Authorization;
import io.swagger.annotations.Extension;
import io.swagger.annotations.ExtensionProperty;
import io.swagger.annotations.ResponseHeader;
-import java.util.List;
-import java.util.Optional;
+import java.util.Collection;
import java.util.UUID;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
-import org.onap.policy.common.utils.services.Registry;
+import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.base.PfModelRuntimeException;
import org.onap.policy.models.pap.concepts.PolicyStatus;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
-import org.onap.policy.pap.main.PapConstants;
-import org.onap.policy.pap.main.notification.PolicyNotifier;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* Class to provide REST end points for PAP component to retrieve the status of deployed
* policies.
*/
public class PolicyStatusControllerV1 extends PapRestControllerV1 {
- private final PolicyNotifier notifier;
+ private static final String GET_DEPLOYMENTS_FAILED = "get deployments failed";
- public PolicyStatusControllerV1() {
- this.notifier = Registry.get(PapConstants.REG_POLICY_NOTIFIER, PolicyNotifier.class);
- }
+ private static final Logger logger = LoggerFactory.getLogger(PolicyStatusControllerV1.class);
+
+ private final PolicyStatusProvider provider = new PolicyStatusProvider();
/**
* Queries status of all deployed policies.
@@ -88,8 +88,15 @@ public class PolicyStatusControllerV1 extends PapRestControllerV1 {
public Response queryAllDeployedPolicies(
@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
- return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
- .entity(notifier.getStatus()).build();
+ try {
+ return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
+ .entity(provider.getStatus()).build();
+
+ } catch (PfModelException | PfModelRuntimeException e) {
+ logger.warn(GET_DEPLOYMENTS_FAILED, e);
+ return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
+ requestId).entity(e.getErrorResponse().getErrorMessage()).build();
+ }
}
@@ -128,13 +135,20 @@ public class PolicyStatusControllerV1 extends PapRestControllerV1 {
@ApiParam(value = "Policy Id", required = true) @PathParam("name") String name,
@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
- List<PolicyStatus> result = notifier.getStatus(name);
- if (result.isEmpty()) {
- return makeNotFoundResponse(requestId);
+ try {
+ Collection<PolicyStatus> result = provider.getStatus(new ToscaConceptIdentifierOptVersion(name, null));
+ if (result.isEmpty()) {
+ return makeNotFoundResponse(requestId);
- } else {
- return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
- .entity(result).build();
+ } else {
+ return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
+ .entity(result).build();
+ }
+
+ } catch (PfModelException | PfModelRuntimeException e) {
+ logger.warn(GET_DEPLOYMENTS_FAILED, e);
+ return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
+ requestId).entity(e.getErrorResponse().getErrorMessage()).build();
}
}
@@ -174,14 +188,20 @@ public class PolicyStatusControllerV1 extends PapRestControllerV1 {
@ApiParam(value = "Policy Version", required = true) @PathParam("version") String version,
@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
- ToscaConceptIdentifier ident = new ToscaConceptIdentifier(name, version);
- Optional<PolicyStatus> result = notifier.getStatus(ident);
- if (result.isPresent()) {
- return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
- .entity(result.get()).build();
+ try {
+ Collection<PolicyStatus> result = provider.getStatus(new ToscaConceptIdentifierOptVersion(name, version));
+ if (result.isEmpty()) {
+ return makeNotFoundResponse(requestId);
+
+ } else {
+ return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
+ .entity(result.iterator().next()).build();
+ }
- } else {
- return makeNotFoundResponse(requestId);
+ } catch (PfModelException | PfModelRuntimeException e) {
+ logger.warn(GET_DEPLOYMENTS_FAILED, e);
+ return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
+ requestId).entity(e.getErrorResponse().getErrorMessage()).build();
}
}
diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PolicyStatusProvider.java b/main/src/main/java/org/onap/policy/pap/main/rest/PolicyStatusProvider.java
new file mode 100644
index 00000000..98cf47b9
--- /dev/null
+++ b/main/src/main/java/org/onap/policy/pap/main/rest/PolicyStatusProvider.java
@@ -0,0 +1,95 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2021 AT&T Intellectual Property. 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.pap.main.rest;
+
+import java.util.Collection;
+import org.onap.policy.common.utils.services.Registry;
+import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.pap.concepts.PolicyStatus;
+import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
+import org.onap.policy.models.provider.PolicyModelsProvider;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
+import org.onap.policy.pap.main.PapConstants;
+import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
+import org.onap.policy.pap.main.notification.DeploymentTracker;
+
+/**
+ * Provider for PAP component to query policy deployment status.
+ */
+public class PolicyStatusProvider {
+
+ /**
+ * Factory for PAP DAO.
+ */
+ private final PolicyModelsProviderFactoryWrapper daoFactory;
+
+
+ /**
+ * Constructs the object. Loads all deployed policies into the internal cache.
+ */
+ public PolicyStatusProvider() {
+ this.daoFactory = Registry.get(PapConstants.REG_PAP_DAO_FACTORY, PolicyModelsProviderFactoryWrapper.class);
+ }
+
+ /**
+ * Gets the deployment status of all policies.
+ *
+ * @return the deployment status of all policies
+ * @throws PfModelException if a DB error occurs
+ */
+ public Collection<PolicyStatus> getStatus() throws PfModelException {
+ try (PolicyModelsProvider dao = daoFactory.create()) {
+ return accumulate(dao.getAllPolicyStatus());
+ }
+ }
+
+ /**
+ * Gets the deployment status of a policy.
+ *
+ * @param policy policy of interest
+ * @return the deployment status of all policies
+ * @throws PfModelException if a DB error occurs
+ */
+ public Collection<PolicyStatus> getStatus(ToscaConceptIdentifierOptVersion policy) throws PfModelException {
+ try (PolicyModelsProvider dao = daoFactory.create()) {
+ return accumulate(dao.getAllPolicyStatus(policy));
+ }
+ }
+
+ /**
+ * Accumulates the deployment status of individual PDP/policy pairs into a status for
+ * a policy.
+ *
+ * @param source PDP/policy pairs
+ * @return the deployment status of the policies
+ */
+ private Collection<PolicyStatus> accumulate(Collection<PdpPolicyStatus> source) {
+ DeploymentTracker tracker = new DeploymentTracker();
+
+ for (PdpPolicyStatus status : source) {
+ if (status.isDeploy()) {
+ tracker.add(status);
+ }
+ }
+
+ return tracker.getDeploymentStatus();
+ }
+}
diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PolicyUndeployerImpl.java b/main/src/main/java/org/onap/policy/pap/main/rest/PolicyUndeployerImpl.java
index 1e7c788b..c3dd505c 100644
--- a/main/src/main/java/org/onap/policy/pap/main/rest/PolicyUndeployerImpl.java
+++ b/main/src/main/java/org/onap/policy/pap/main/rest/PolicyUndeployerImpl.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP PAP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2020-2021 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -93,7 +93,7 @@ public class PolicyUndeployerImpl extends ProviderBase implements PolicyUndeploy
subgroup.getPdpType(), subgroup.getPolicies().size());
updated = true;
- data.trackUndeploy(ident, pdps);
+ data.trackUndeploy(ident, pdps, policyInfo.group, policyInfo.subgroup);
}
// push the updates
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 0de7a6c4..7f013ac7 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
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP PAP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2020-2021 Nordix Foundation.
* Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
* ================================================================================
@@ -28,6 +28,7 @@ import javax.ws.rs.core.Response.Status;
import org.onap.policy.common.utils.services.Registry;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.base.PfModelRuntimeException;
+import org.onap.policy.models.pap.concepts.PolicyNotification;
import org.onap.policy.models.pdp.concepts.Pdp;
import org.onap.policy.models.pdp.concepts.PdpGroup;
import org.onap.policy.models.pdp.concepts.PdpSubGroup;
@@ -95,6 +96,7 @@ public abstract class ProviderBase {
synchronized (updateLock) {
SessionData data;
+ PolicyNotification notif = new PolicyNotification();
try (PolicyModelsProvider dao = daoFactory.create()) {
@@ -102,7 +104,7 @@ public abstract class ProviderBase {
processor.accept(data, request);
// make all of the DB updates
- data.updateDb();
+ data.updateDb(notif);
} catch (PfModelException | PfModelRuntimeException e) {
throw e;
@@ -111,12 +113,11 @@ public abstract class ProviderBase {
throw new PfModelException(Status.INTERNAL_SERVER_ERROR, "request failed", e);
}
- // track responses for notification purposes
- data.getDeployData().forEach(notifier::addDeploymentData);
- data.getUndeployData().forEach(notifier::addUndeploymentData);
-
// publish the requests
data.getPdpRequests().forEach(pair -> requestMap.addRequest(pair.getLeft(), pair.getRight()));
+
+ // publish the notifications
+ notifier.publish(notif);
}
}
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 cdcc51c6..930302a2 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
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP PAP
* ================================================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2021 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -25,13 +25,12 @@ import com.google.re2j.Pattern;
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.stream.Collectors;
import org.apache.commons.lang3.tuple.Pair;
import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.pap.concepts.PolicyNotification;
import org.onap.policy.models.pdp.concepts.PdpGroup;
import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
import org.onap.policy.models.pdp.concepts.PdpStateChange;
@@ -44,7 +43,7 @@ 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.ToscaPolicyType;
-import org.onap.policy.pap.main.notification.PolicyPdpNotificationData;
+import org.onap.policy.pap.main.notification.DeploymentStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -93,16 +92,9 @@ public class SessionData {
private final Map<ToscaConceptIdentifier, 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".
+ * Tracks policy deployment status so notifications can be generated.
*/
- private final Map<ToscaConceptIdentifier, 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<ToscaConceptIdentifier, PolicyPdpNotificationData> undeploy = new HashMap<>();
+ private final DeploymentStatus deployStatus;
/**
@@ -112,6 +104,7 @@ public class SessionData {
*/
public SessionData(PolicyModelsProvider dao) {
this.dao = dao;
+ this.deployStatus = makeDeploymentStatus(dao);
}
/**
@@ -332,6 +325,8 @@ public class SessionData {
data = new GroupData(lst.get(0));
groupCache.put(name, data);
+ deployStatus.loadByGroup(name);
+
} else {
logger.info("use cached group {}", name);
}
@@ -352,7 +347,7 @@ public class SessionData {
* exception and handling that would obfuscate the code too much, thus disabling
* the sonar.
*/
- List<GroupData> data = type2groups.get(type); // NOSONAR
+ List<GroupData> data = type2groups.get(type); // NOSONAR
if (data == null) {
PdpGroupFilter filter = PdpGroupFilter.builder().policyTypeList(Collections.singletonList(type))
.groupState(PdpState.ACTIVE).build();
@@ -388,10 +383,11 @@ public class SessionData {
/**
* Update the DB with the changes.
+ * @param notification notification to which to add policy status
*
* @throws PfModelException if an error occurred
*/
- public void updateDb() throws PfModelException {
+ public void updateDb(PolicyNotification notification) throws PfModelException {
// create new groups
List<GroupData> created = groupCache.values().stream().filter(GroupData::isNew).collect(Collectors.toList());
if (!created.isEmpty()) {
@@ -410,6 +406,9 @@ public class SessionData {
}
dao.updatePdpGroups(updated.stream().map(GroupData::getGroup).collect(Collectors.toList()));
}
+
+ // flush deployment status records to the DB
+ deployStatus.flush(notification);
}
/**
@@ -429,32 +428,13 @@ public class SessionData {
*
* @param policyId ID of the 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) 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(ToscaConceptIdentifier 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(ToscaConceptIdentifier policyId, Collection<String> pdps) throws PfModelException {
- trackUndeploy(policyId, new HashSet<>(pdps));
+ protected void trackDeploy(ToscaConceptIdentifier policyId, Collection<String> pdps, String pdpGroup,
+ String pdpType) throws PfModelException {
+ addData(policyId, pdps, pdpGroup, pdpType, true);
}
/**
@@ -462,10 +442,13 @@ public class SessionData {
*
* @param policyId ID of the policy being undeployed
* @param pdps PDPs to which the policy is being undeployed
+ * @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 trackUndeploy(ToscaConceptIdentifier policyId, Set<String> pdps) throws PfModelException {
- addData(policyId, pdps, undeploy, deploy);
+ protected void trackUndeploy(ToscaConceptIdentifier policyId, Collection<String> pdps, String pdpGroup,
+ String pdpType) throws PfModelException {
+ addData(policyId, pdps, pdpGroup, pdpType, false);
}
/**
@@ -473,40 +456,29 @@ public class SessionData {
*
* @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
+ * @param deploy {@code true} if the policy is being deployed, {@code false} if
+ * undeployed
+ * @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
*/
- private void addData(ToscaConceptIdentifier policyId, Set<String> pdps,
- Map<ToscaConceptIdentifier, PolicyPdpNotificationData> addMap,
- Map<ToscaConceptIdentifier, PolicyPdpNotificationData> removeMap) throws PfModelException {
+ private void addData(ToscaConceptIdentifier policyId, Collection<String> pdps, String pdpGroup, String pdpType,
+ boolean deploy) throws PfModelException {
- PolicyPdpNotificationData removeData = removeMap.get(policyId);
- if (removeData != null) {
- removeData.removeAll(pdps);
- }
+ // delete all records whose "deploy" flag is the opposite of what we want
+ deployStatus.deleteDeployment(policyId, !deploy);
ToscaConceptIdentifierOptVersion optid = new ToscaConceptIdentifierOptVersion(policyId);
ToscaConceptIdentifier policyType = getPolicy(optid).getTypeIdentifier();
- addMap.computeIfAbsent(policyId, key -> new PolicyPdpNotificationData(policyId, policyType)).addAll(pdps);
+ for (String pdp : pdps) {
+ deployStatus.deploy(pdp, policyId, policyType, pdpGroup, pdpType, deploy);
+ }
}
- /**
- * Gets the policies to be deployed.
- *
- * @return the policies to be deployed
- */
- public Collection<PolicyPdpNotificationData> getDeployData() {
- return deploy.values();
- }
+ // these may be overridden by junit tests
- /**
- * Gets the policies to be undeployed.
- *
- * @return the policies to be undeployed
- */
- public Collection<PolicyPdpNotificationData> getUndeployData() {
- return undeploy.values();
+ protected DeploymentStatus makeDeploymentStatus(PolicyModelsProvider dao) {
+ return new DeploymentStatus(dao);
}
}
diff --git a/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java b/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java
index 4ccfb116..a351e6f7 100644
--- a/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java
+++ b/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java
@@ -1,7 +1,7 @@
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2019 Nordix Foundation.
- * Modifications Copyright (C) 2019 AT&T Intellectual Property.
+ * Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property.
* Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -78,7 +78,13 @@ public class Main {
// Start the activator
activator.start();
- } catch (Exception exp) {
+ } catch (Exception exp) { // NOSONAR
+ /*
+ * Disabled sonar on the above line, because we want to capture the stack
+ * trace via the logger while still reporting the exception message on stdout
+ * when the JVM exits.
+ */
+ LOGGER.error("failed to start Main", exp);
if (null != activator) {
Registry.unregister(PapConstants.REG_PAP_ACTIVATOR);
}
diff --git a/main/src/main/resources/META-INF/persistence.xml b/main/src/main/resources/META-INF/persistence.xml
index 8c385f8a..a9624f35 100644
--- a/main/src/main/resources/META-INF/persistence.xml
+++ b/main/src/main/resources/META-INF/persistence.xml
@@ -2,6 +2,7 @@
<!--
============LICENSE_START=======================================================
Copyright (C) 2019-2020 Nordix Foundation.
+ Modifications Copyright (C) 2021 AT&T Intellectual Property.
================================================================================
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -28,6 +29,7 @@
<class>org.onap.policy.models.dao.converters.Uuid2String</class>
<class>org.onap.policy.models.pdp.persistence.concepts.JpaPdp</class>
<class>org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup</class>
+ <class>org.onap.policy.models.pdp.persistence.concepts.JpaPdpPolicyStatus</class>
<class>org.onap.policy.models.pdp.persistence.concepts.JpaPdpStatistics</class>
<class>org.onap.policy.models.pdp.persistence.concepts.JpaPdpSubGroup</class>
<class>org.onap.policy.models.tosca.simple.concepts.JpaToscaCapabilityAssignment</class>
diff --git a/main/src/test/java/org/onap/policy/pap/main/comm/PdpModifyRequestMapTest.java b/main/src/test/java/org/onap/policy/pap/main/comm/PdpModifyRequestMapTest.java
index 114d50e6..65708aa6 100644
--- a/main/src/test/java/org/onap/policy/pap/main/comm/PdpModifyRequestMapTest.java
+++ b/main/src/test/java/org/onap/policy/pap/main/comm/PdpModifyRequestMapTest.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP PAP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2020-2021 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -21,6 +21,7 @@
package org.onap.policy.pap.main.comm;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -51,9 +52,13 @@ import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.pap.concepts.PolicyNotification;
+import org.onap.policy.models.pap.concepts.PolicyStatus;
import org.onap.policy.models.pdp.concepts.Pdp;
import org.onap.policy.models.pdp.concepts.PdpGroup;
import org.onap.policy.models.pdp.concepts.PdpMessage;
+import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
+import org.onap.policy.models.pdp.concepts.PdpPolicyStatus.State;
import org.onap.policy.models.pdp.concepts.PdpStateChange;
import org.onap.policy.models.pdp.concepts.PdpStatus;
import org.onap.policy.models.pdp.concepts.PdpSubGroup;
@@ -82,6 +87,9 @@ public class PdpModifyRequestMapTest extends CommonRequestBase {
@Captor
private ArgumentCaptor<List<PdpGroup>> updateCaptor;
+ @Captor
+ private ArgumentCaptor<PolicyNotification> notificationCaptor;
+
/**
* Used to capture input to undeployer.undeploy().
*/
@@ -360,7 +368,22 @@ public class PdpModifyRequestMapTest extends CommonRequestBase {
PdpGroup group = makeGroup(MY_GROUP);
group.setPdpSubgroups(Arrays.asList(makeSubGroup(MY_SUBGROUP, PDP1)));
+ final ToscaConceptIdentifier policy1 = new ToscaConceptIdentifier("MyPolicy", "10.20.30");
+ final ToscaConceptIdentifier policyType = new ToscaConceptIdentifier("MyPolicyType", "10.20.30");
+
+ // @formatter:off
when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group));
+ when(dao.getGroupPolicyStatus(any())).thenReturn(Arrays.asList(
+ PdpPolicyStatus.builder()
+ .pdpGroup(MY_GROUP)
+ .pdpType(MY_SUBGROUP)
+ .pdpId(PDP1)
+ .policy(policy1)
+ .policyType(policyType)
+ .deploy(true)
+ .state(State.SUCCESS)
+ .build()));
+ // @formatter:on
// indicate retries exhausted
invokeLastRetryHandler(1);
@@ -368,8 +391,17 @@ public class PdpModifyRequestMapTest extends CommonRequestBase {
// should have stopped publishing
verify(requests).stopPublishing();
- // should have generated a notification
- verify(notifier).removePdp(PDP1);
+ // should have generated a notification; yes, it should go into the "added" set
+ verify(notifier).publish(notificationCaptor.capture());
+ assertThat(notificationCaptor.getValue().getDeleted()).isEmpty();
+ assertThat(notificationCaptor.getValue().getAdded()).hasSize(1);
+
+ PolicyStatus status = notificationCaptor.getValue().getAdded().get(0);
+ assertThat(status.getPolicy()).isEqualTo(policy1);
+ assertThat(status.getPolicyType()).isEqualTo(policyType);
+ assertThat(status.getFailureCount()).isZero();
+ assertThat(status.getIncompleteCount()).isZero();
+ assertThat(status.getSuccessCount()).isZero();
// should have removed from the group
List<PdpGroup> groups = getGroupUpdates();
@@ -390,6 +422,9 @@ public class PdpModifyRequestMapTest extends CommonRequestBase {
// should not have done any updates
verify(dao, never()).updatePdpGroups(any());
+
+ // and no publishes
+ verify(notifier, never()).publish(any());
}
@Test
diff --git a/main/src/test/java/org/onap/policy/pap/main/comm/msgdata/UpdateReqTest.java b/main/src/test/java/org/onap/policy/pap/main/comm/msgdata/UpdateReqTest.java
index db955155..ca6a62ca 100644
--- a/main/src/test/java/org/onap/policy/pap/main/comm/msgdata/UpdateReqTest.java
+++ b/main/src/test/java/org/onap/policy/pap/main/comm/msgdata/UpdateReqTest.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP PAP
* ================================================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2021 AT&T Intellectual Property. 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.
@@ -115,7 +115,7 @@ public class UpdateReqTest extends CommonRequestBase {
assertEquals("group does not match", data.checkResponse(response));
assertTrue(data.getUndeployPolicies().isEmpty());
- verifyResponse();
+ verifyNoResponse();
}
@Test
@@ -124,7 +124,7 @@ public class UpdateReqTest extends CommonRequestBase {
assertEquals("subgroup does not match", data.checkResponse(response));
assertTrue(data.getUndeployPolicies().isEmpty());
- verifyResponse();
+ verifyNoResponse();
}
@Test
@@ -137,7 +137,7 @@ public class UpdateReqTest extends CommonRequestBase {
assertEquals(null, data.checkResponse(response));
assertTrue(data.getUndeployPolicies().isEmpty());
- verifyResponse();
+ verifyNoResponse();
}
@Test
@@ -277,12 +277,12 @@ public class UpdateReqTest extends CommonRequestBase {
@SuppressWarnings("unchecked")
private void verifyResponse() {
- verify(notifier).processResponse(any(), any(Set.class));
+ verify(notifier).processResponse(any(), any(), any(Set.class), any(Set.class));
}
@SuppressWarnings("unchecked")
private void verifyNoResponse() {
- verify(notifier, never()).processResponse(any(), any(Set.class));
+ verify(notifier, never()).processResponse(any(), any(), any(Set.class), any(Set.class));
}
/**
diff --git a/main/src/test/java/org/onap/policy/pap/main/notification/PolicyCommonSupport.java b/main/src/test/java/org/onap/policy/pap/main/notification/PolicyCommonSupport.java
deleted file mode 100644
index de1848d4..00000000
--- a/main/src/test/java/org/onap/policy/pap/main/notification/PolicyCommonSupport.java
+++ /dev/null
@@ -1,94 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * ONAP PAP
- * ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2021 Nordix Foundation.
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.pap.main.notification;
-
-import java.util.Arrays;
-import java.util.List;
-import org.junit.Before;
-import org.onap.policy.models.pap.concepts.PolicyStatus;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
-
-/**
- * Super class for policy notification test classes.
- */
-public class PolicyCommonSupport {
- protected static final String MAP_FIELD = "policy2data";
- protected static final String PDP1 = "pdp-1";
- protected static final String PDP2 = "pdp-2";
- protected static final String PDP3 = "pdp-3";
- protected static final String PDP4 = "pdp-4";
-
- protected ToscaConceptIdentifier type;
- protected ToscaConceptIdentifier policy1;
- protected ToscaConceptIdentifier policy2;
- protected ToscaConceptIdentifier policy3;
- protected ToscaConceptIdentifier policy4;
-
- /**
- * Creates various objects.
- */
- @Before
- public void setUp() {
- type = new ToscaConceptIdentifier("my-type", "3.2.1");
- policy1 = new ToscaConceptIdentifier("my-id-a", "1.2.0");
- policy2 = new ToscaConceptIdentifier("my-id-b", "1.2.1");
- policy3 = new ToscaConceptIdentifier("my-id-c", "1.2.2");
- policy4 = new ToscaConceptIdentifier("my-id-d", "1.2.3");
- }
-
- /**
- * Makes notification data.
- *
- * @param policyId ID of the policy with which the data should be associated
- * @param pdps PDPs to be included within the data
- * @return a new notification data structure
- */
- protected PolicyPdpNotificationData makeData(ToscaConceptIdentifier policyId, String... pdps) {
- PolicyPdpNotificationData data = new PolicyPdpNotificationData(policyId, type);
- data.addAll(Arrays.asList(pdps));
- return data;
- }
-
- /**
- * Extracts the counts from the sets contained within tracker data.
- *
- * @param data data from which to extract the sets
- * @return a list containing the number of successes, failures, and incomplete PDPs,
- * in that order
- */
- protected List<Integer> getCounts(PolicyTrackerData data) {
- PolicyStatus status = new PolicyStatus();
- data.putValuesInto(status);
- return getCounts(status);
- }
-
- /**
- * Extracts the counts from within a status notification.
- *
- * @param status status from which to extract the counts
- * @return a list containing the number of successes, failures, and incomplete PDPs,
- * in that order
- */
- protected List<Integer> getCounts(PolicyStatus status) {
- return Arrays.asList(status.getSuccessCount(), status.getFailureCount(), status.getIncompleteCount());
- }
-}
diff --git a/main/src/test/java/org/onap/policy/pap/main/notification/PolicyCommonTrackerTest.java b/main/src/test/java/org/onap/policy/pap/main/notification/PolicyCommonTrackerTest.java
deleted file mode 100644
index b80b80d7..00000000
--- a/main/src/test/java/org/onap/policy/pap/main/notification/PolicyCommonTrackerTest.java
+++ /dev/null
@@ -1,347 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * ONAP PAP
- * ================================================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2021 Nordix Foundation.
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.pap.main.notification;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNotSame;
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertTrue;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
-import java.util.Set;
-import java.util.stream.Collectors;
-import org.junit.Before;
-import org.junit.Test;
-import org.onap.policy.models.pap.concepts.PolicyStatus;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
-import org.powermock.reflect.Whitebox;
-
-public class PolicyCommonTrackerTest extends PolicyCommonSupport {
-
- private MyTracker tracker;
- private Map<ToscaConceptIdentifier, PolicyTrackerData> map;
-
- /**
- * Creates various objects, including {@link #tracker}.
- */
- @Override
- @Before
- public void setUp() {
- super.setUp();
-
- tracker = new MyTracker();
- }
-
- @Test
- public void testGetStatus() {
- tracker.addData(makeData(policy1, PDP1, PDP2));
- tracker.addData(makeData(policy2, PDP2));
-
- List<PolicyStatus> statusList = tracker.getStatus();
- assertEquals(2, statusList.size());
-
- Set<String> names = statusList.stream().map(PolicyStatus::getPolicyId).collect(Collectors.toSet());
- assertTrue(names.contains(policy1.getName()));
- assertTrue(names.contains(policy2.getName()));
- }
-
- @Test
- public void testGetStatusString() {
- tracker.addData(makeData(policy1, PDP1, PDP2));
- tracker.addData(makeData(policy2, PDP2));
-
- policy3 = new ToscaConceptIdentifier(policy1.getName(), policy1.getVersion() + "0");
- tracker.addData(makeData(policy3, PDP3));
-
- List<PolicyStatus> statusList = tracker.getStatus(policy1.getName());
- assertEquals(2, statusList.size());
-
- Set<ToscaConceptIdentifier> idents =
- statusList.stream().map(PolicyStatus::getPolicy).collect(Collectors.toSet());
- assertTrue(idents.contains(policy1));
- assertTrue(idents.contains(policy3));
- }
-
- @Test
- public void testGetStatusToscaConceptIdentifier() {
- tracker.addData(makeData(policy1, PDP1, PDP2));
- tracker.addData(makeData(policy2, PDP2));
-
- policy3 = new ToscaConceptIdentifier(policy1.getName(), policy1.getVersion() + "0");
- tracker.addData(makeData(policy3, PDP3));
-
- Optional<PolicyStatus> status = tracker.getStatus(policy1);
- assertTrue(status.isPresent());
-
- assertEquals(policy1, status.get().getPolicy());
-
- // check not-found case
- status = tracker.getStatus(policy4);
- assertFalse(status.isPresent());
- }
-
- @Test
- public void testAddData() {
- tracker.addData(makeData(policy1, PDP1, PDP2));
- assertEquals(1, map.size());
-
- PolicyTrackerData data = map.get(policy1);
- assertNotNull(data);
- assertFalse(data.isComplete());
-
- // add same policy - should still have the same data object
- tracker.addData(makeData(policy1, PDP1, PDP3));
- assertEquals(1, map.size());
- assertSame(data, map.get(policy1));
- assertFalse(data.isComplete());
-
- // add another policy
- tracker.addData(makeData(policy2, PDP2));
- assertEquals(2, map.size());
-
- // data for policy 1 is unchanged
- assertSame(data, map.get(policy1));
- assertFalse(data.isComplete());
-
- // policy 2 should have its own data
- assertNotSame(data, map.get(policy2));
- data = map.get(policy2);
- assertNotNull(data);
- assertFalse(data.isComplete());
- }
-
- /**
- * Tests removeData() when the policy isn't in the map.
- */
- @Test
- public void testRemoveDataUnknownPolicy() {
- tracker.addData(makeData(policy1, PDP1, PDP2));
- tracker.addData(makeData(policy2, PDP1, PDP3));
-
- // remove a policy that isn't in the map
- tracker.removeData(makeData(policy3, PDP1));
- assertEquals(2, map.size());
- }
-
- /**
- * Tests removeData() when only some PDPs are removed from the policy.
- */
- @Test
- public void testRemoveDataRemoveSomePdps() {
- tracker.addData(makeData(policy1, PDP1, PDP2));
- tracker.addData(makeData(policy2, PDP1, PDP3));
-
- // remove some PDPs from a policy - no changes to the map
- tracker.removeData(makeData(policy2, PDP1));
- assertTrue(map.containsKey(policy1));
- assertTrue(map.containsKey(policy2));
- }
-
- /**
- * Tests removeData() when the subclass indicates that the policy should NOT be
- * removed.
- */
- @Test
- public void testRemoveDataDoNotRemovePolicy() {
- tracker = new MyTracker() {
- @Override
- protected boolean shouldRemove(PolicyTrackerData data) {
- return false;
- }
- };
-
- tracker.addData(makeData(policy1, PDP1, PDP2));
- tracker.addData(makeData(policy2, PDP1, PDP3));
-
- // remove all the PDPs from one policy, but do NOT remove the policy
- tracker.removeData(makeData(policy2, PDP1, PDP3));
- assertTrue(map.containsKey(policy1));
- assertTrue(map.containsKey(policy2));
- }
-
- /**
- * Tests removeData() when the subclass indicates that the policy should be removed.
- */
- @Test
- public void testRemoveDataRemovePolicy() {
- tracker.addData(makeData(policy1, PDP1, PDP2));
- tracker.addData(makeData(policy2, PDP1, PDP3));
-
- // remove all the PDPs from one policy, and remove the policy
- tracker.removeData(makeData(policy1, PDP1, PDP2, PDP3));
- assertFalse(map.containsKey(policy1));
- assertTrue(map.containsKey(policy2));
- }
-
- @Test
- public void testRemovePdp() {
- tracker.addData(makeData(policy1, PDP1));
- tracker.addData(makeData(policy2, PDP1, PDP3));
- tracker.addData(makeData(policy3, PDP1, PDP2, PDP3));
- tracker.addData(makeData(policy4, PDP4, PDP2, PDP3));
-
- List<PolicyStatus> statusList = new ArrayList<>();
- tracker.removePdp(PDP1, statusList);
-
- assertEquals(1, statusList.size());
- assertEquals(policy1, statusList.get(0).getPolicy());
- assertEquals(type, statusList.get(0).getPolicyType());
-
- assertEquals(3, map.size());
- assertFalse(map.containsKey(policy1));
- assertEquals("[0, 0, 1]", getCounts(map.get(policy2)).toString());
- assertEquals("[0, 0, 2]", getCounts(map.get(policy3)).toString());
- assertEquals("[0, 0, 3]", getCounts(map.get(policy4)).toString());
- }
-
- @Test
- public void testProcessResponse() {
- tracker.addData(makeData(policy1, PDP1));
- tracker.addData(makeData(policy2, PDP1, PDP3));
- tracker.addData(makeData(policy3, PDP1, PDP2, PDP3));
- tracker.addData(makeData(policy4, PDP4, PDP2, PDP3));
-
- List<PolicyStatus> statusList = new ArrayList<>();
- tracker.processResponse(PDP1, Arrays.asList(policy3), statusList);
-
- assertEquals(1, statusList.size());
- assertEquals(policy1, statusList.get(0).getPolicy());
- assertEquals(type, statusList.get(0).getPolicyType());
-
- assertEquals(3, map.size());
- assertFalse(map.containsKey(policy1));
- assertEquals("[0, 1, 1]", getCounts(map.get(policy2)).toString());
- assertEquals("[1, 0, 2]", getCounts(map.get(policy3)).toString());
- assertEquals("[0, 0, 3]", getCounts(map.get(policy4)).toString());
- }
-
- @Test
- public void testUpdateMap() {
- tracker.addData(makeData(policy1, PDP1));
- tracker.addData(makeData(policy2, PDP1, PDP3));
- tracker.addData(makeData(policy3, PDP1, PDP2, PDP3));
- tracker.addData(makeData(policy4, PDP4, PDP2, PDP3));
-
- List<PolicyStatus> statusList = new ArrayList<>();
- tracker.processResponse(PDP1, Arrays.asList(policy3), statusList);
-
- assertEquals(1, statusList.size());
- assertEquals(policy1, statusList.get(0).getPolicy());
- assertEquals(type, statusList.get(0).getPolicyType());
-
- assertEquals(3, map.size());
- assertFalse(map.containsKey(policy1));
- assertEquals("[0, 1, 1]", getCounts(map.get(policy2)).toString());
- assertEquals("[1, 0, 2]", getCounts(map.get(policy3)).toString());
- assertEquals("[0, 0, 3]", getCounts(map.get(policy4)).toString());
- }
-
- /**
- * Tests updateMap() when the policy should NOT be removed.
- */
- @Test
- public void testUpdateMapDoNotRemove() {
- tracker = new MyTracker() {
- @Override
- protected boolean shouldRemove(PolicyTrackerData data) {
- return false;
- }
- };
-
- tracker.addData(makeData(policy1, PDP1, PDP2));
-
- // indicate that PDP2 has succeeded
- tracker.processResponse(PDP2, Arrays.asList(policy1), new ArrayList<>(0));
-
- // indicate that PDP1 has succeeded
- List<PolicyStatus> statusList = new ArrayList<>();
- tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
-
- assertEquals(1, statusList.size());
- assertEquals(policy1, statusList.get(0).getPolicy());
- assertEquals(type, statusList.get(0).getPolicyType());
-
- assertEquals(1, map.size());
- assertTrue(map.containsKey(policy1));
- assertEquals("[2, 0, 0]", getCounts(map.get(policy1)).toString());
- }
-
- /**
- * Tests updateMap() when the policy SHOULD be removed.
- */
- @Test
- public void testUpdateMapRemovePolicy() {
- tracker.addData(makeData(policy1, PDP1, PDP2));
-
- // indicate that PDP2 has succeeded
- tracker.processResponse(PDP2, Arrays.asList(policy1), new ArrayList<>(0));
-
- // indicate that PDP1 has succeeded
- List<PolicyStatus> statusList = new ArrayList<>();
- tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
-
- assertEquals(1, statusList.size());
- assertEquals(policy1, statusList.get(0).getPolicy());
- assertEquals(type, statusList.get(0).getPolicyType());
- assertEquals("[2, 0, 0]", getCounts(statusList.get(0)).toString());
-
- assertTrue(map.isEmpty());
- }
-
- @Test
- public void testMakeStatus() {
- tracker.addData(makeData(policy1, PDP1, PDP2));
-
- // indicate that PDP2 has succeeded
- List<PolicyStatus> statusList = new ArrayList<>();
- tracker.processResponse(PDP2, Arrays.asList(policy1), statusList);
- assertTrue(statusList.isEmpty());
-
- // indicate that PDP1 has failed
- tracker.processResponse(PDP1, Arrays.asList(policy2), statusList);
- assertEquals(1, statusList.size());
- assertEquals("[1, 1, 0]", getCounts(statusList.get(0)).toString());
- }
-
- private class MyTracker extends PolicyCommonTracker {
-
- public MyTracker() {
- map = Whitebox.getInternalState(this, MAP_FIELD);
- }
-
- @Override
- protected boolean updateData(String pdp, PolicyTrackerData data, boolean stillActive) {
- return (stillActive ? data.success(pdp) : data.fail(pdp));
- }
-
- @Override
- protected boolean shouldRemove(PolicyTrackerData data) {
- return data.isComplete();
- }
- }
-}
diff --git a/main/src/test/java/org/onap/policy/pap/main/notification/PolicyDeployTrackerTest.java b/main/src/test/java/org/onap/policy/pap/main/notification/PolicyDeployTrackerTest.java
deleted file mode 100644
index 742d87d5..00000000
--- a/main/src/test/java/org/onap/policy/pap/main/notification/PolicyDeployTrackerTest.java
+++ /dev/null
@@ -1,250 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * ONAP PAP
- * ================================================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.pap.main.notification;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-import static org.mockito.Matchers.any;
-import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-import org.junit.Before;
-import org.junit.Test;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.onap.policy.models.pap.concepts.PolicyStatus;
-
-public class PolicyDeployTrackerTest extends PolicyCommonSupport {
-
- @Mock
- private PolicyTrackerData data;
-
- private PolicyDeployTracker tracker;
-
- /**
- * Creates various objects, including {@link #tracker}.
- */
- @Before
- public void setUp() {
- MockitoAnnotations.initMocks(this);
-
- super.setUp();
-
- tracker = new PolicyDeployTracker();
- }
-
- /**
- * Simple test with one PDP that immediately responds with success.
- */
- @Test
- public void testSimpleImmediateSuccess() {
- tracker.addData(makeData(policy1, PDP1));
-
- // indicate that PDP1 has succeeded
- List<PolicyStatus> statusList = new ArrayList<>();
- tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
-
- assertEquals(1, statusList.size());
- assertEquals(policy1, statusList.get(0).getPolicy());
- assertEquals(type, statusList.get(0).getPolicyType());
- assertEquals("[1, 0, 0]", getCounts(statusList.get(0)).toString());
-
- // indicate that PDP1 has succeeded again - should be no output
- statusList.clear();
- tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
- assertEquals(0, statusList.size());
-
- // indicate failure
- statusList.clear();
- tracker.processResponse(PDP1, Arrays.asList(), statusList);
- assertEquals(1, statusList.size());
- assertEquals("[0, 1, 0]", getCounts(statusList.get(0)).toString());
-
- // indicate that PDP1 has failed again - should be no output
- statusList.clear();
- tracker.processResponse(PDP1, Arrays.asList(), statusList);
- assertEquals(0, statusList.size());
-
- // indicate that PDP1 has succeeded again
- statusList.clear();
- tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
- assertEquals("[1, 0, 0]", getCounts(statusList.get(0)).toString());
- }
-
- /**
- * Simple test with one PDP that immediately responds with success.
- */
- @Test
- public void testSimpleImmediateFail() {
- tracker.addData(makeData(policy1, PDP1));
-
- // indicate that PDP1 has failed
- List<PolicyStatus> statusList = new ArrayList<>();
- tracker.processResponse(PDP1, Arrays.asList(), statusList);
- assertEquals(1, statusList.size());
- assertEquals("[0, 1, 0]", getCounts(statusList.get(0)).toString());
-
- // indicate that PDP1 has failed again - should be no output
- statusList.clear();
- tracker.processResponse(PDP1, Arrays.asList(), statusList);
- assertEquals(0, statusList.size());
-
- // indicate success
- tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
-
- assertEquals(1, statusList.size());
- assertEquals(policy1, statusList.get(0).getPolicy());
- assertEquals(type, statusList.get(0).getPolicyType());
- assertEquals("[1, 0, 0]", getCounts(statusList.get(0)).toString());
-
- // indicate that PDP1 has succeeded again - should be no output
- statusList.clear();
- tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
- assertEquals(0, statusList.size());
- }
-
- /**
- * Simple test where PDP is removed and then it responds.
- */
- @Test
- public void testSimpleRemove() {
- tracker.addData(makeData(policy1, PDP1));
-
- // remove the PDP
- List<PolicyStatus> statusList = new ArrayList<>();
- tracker.removePdp(PDP1, statusList);
- assertEquals(1, statusList.size());
- assertEquals(policy1, statusList.get(0).getPolicy());
- assertEquals(type, statusList.get(0).getPolicyType());
- assertEquals("[0, 0, 0]", getCounts(statusList.get(0)).toString());
-
- /*
- * indicate that PDP1 has succeeded - should be no message since PDP was removed
- * from the policy
- */
- statusList.clear();
- tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
- assertEquals(0, statusList.size());
-
- /*
- * indicate that PDP1 has failed - should be no message since PDP was removed
- * from the policy
- */
- statusList.clear();
- tracker.processResponse(PDP1, Arrays.asList(), statusList);
- assertEquals(0, statusList.size());
- }
-
- /**
- * Test with multiple PDPs.
- */
- @Test
- public void testMulti() {
- tracker.addData(makeData(policy1, PDP1, PDP2));
-
- // indicate that PDP2 has succeeded
- List<PolicyStatus> statusList = new ArrayList<>();
- tracker.processResponse(PDP2, Arrays.asList(policy1), statusList);
- assertEquals(0, statusList.size());
-
- // indicate that PDP1 has succeeded
- statusList.clear();
- tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
-
- assertEquals(1, statusList.size());
- assertEquals(policy1, statusList.get(0).getPolicy());
- assertEquals(type, statusList.get(0).getPolicyType());
- assertEquals("[2, 0, 0]", getCounts(statusList.get(0)).toString());
-
- // indicate that PDP1 has failed - should get a notification
- statusList.clear();
- tracker.processResponse(PDP1, Collections.emptyList(), statusList);
- assertEquals(1, statusList.size());
- assertEquals(policy1, statusList.get(0).getPolicy());
- assertEquals(type, statusList.get(0).getPolicyType());
- assertEquals("[1, 1, 0]", getCounts(statusList.get(0)).toString());
-
- // indicate that PDP1 has succeeded
- statusList.clear();
- tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
- assertEquals(1, statusList.size());
- assertEquals("[2, 0, 0]", getCounts(statusList.get(0)).toString());
-
- // remove PDP2 - expect message
- statusList.clear();
- tracker.removePdp(PDP2, statusList);
- assertEquals(1, statusList.size());
- assertEquals("[1, 0, 0]", getCounts(statusList.get(0)).toString());
-
- // re-add PDP2
- tracker.addData(makeData(policy1, PDP2));
-
- // indicate that PDP2 has succeeded; PDP1 should still be ok
- statusList.clear();
- tracker.processResponse(PDP2, Arrays.asList(policy1), statusList);
- assertEquals(1, statusList.size());
- assertEquals("[2, 0, 0]", getCounts(statusList.get(0)).toString());
- }
-
- @Test
- public void testUpdateData() {
- // when success returns false
- assertFalse(tracker.updateData(PDP1, data, true));
- verify(data).success(PDP1);
- verify(data, never()).fail(any());
-
- // when inactive
- assertFalse(tracker.updateData(PDP1, data, false));
- verify(data).success(PDP1);
- verify(data).fail(any());
-
- // when success & fail return true
- when(data.success(PDP1)).thenReturn(true);
- when(data.fail(PDP1)).thenReturn(true);
- assertTrue(tracker.updateData(PDP1, data, true));
- verify(data, times(2)).success(PDP1);
- verify(data, times(1)).fail(PDP1);
-
- // when inactive
- assertTrue(tracker.updateData(PDP1, data, false));
- verify(data, times(2)).success(PDP1);
- verify(data, times(2)).fail(PDP1);
- }
-
- @Test
- public void testShouldRemove() {
- // when data is complete, but not empty
- when(data.isComplete()).thenReturn(true);
- when(data.isEmpty()).thenReturn(false);
- assertFalse(tracker.shouldRemove(data));
-
- // when data is empty
- when(data.isEmpty()).thenReturn(true);
- assertTrue(tracker.shouldRemove(data));
- }
-}
diff --git a/main/src/test/java/org/onap/policy/pap/main/notification/PolicyNotifierTest.java b/main/src/test/java/org/onap/policy/pap/main/notification/PolicyNotifierTest.java
index 5e64c939..a3665958 100644
--- a/main/src/test/java/org/onap/policy/pap/main/notification/PolicyNotifierTest.java
+++ b/main/src/test/java/org/onap/policy/pap/main/notification/PolicyNotifierTest.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP PAP
* ================================================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2021 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -21,44 +21,39 @@
package org.onap.policy.pap.main.notification;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertSame;
+import static org.assertj.core.api.Assertions.assertThatCode;
import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
-import java.util.ArrayList;
-import java.util.Arrays;
import java.util.Collections;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Optional;
+import java.util.Set;
+import javax.ws.rs.core.Response.Status;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
-import org.mockito.stubbing.Answer;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.pap.concepts.PolicyNotification;
import org.onap.policy.models.pap.concepts.PolicyStatus;
-import org.onap.policy.models.pdp.concepts.Pdp;
-import org.onap.policy.models.pdp.concepts.PdpGroup;
-import org.onap.policy.models.pdp.concepts.PdpSubGroup;
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.PolicyModelsProviderFactoryWrapper;
import org.onap.policy.pap.main.PolicyPapRuntimeException;
import org.onap.policy.pap.main.comm.Publisher;
import org.onap.policy.pap.main.comm.QueueToken;
-public class PolicyNotifierTest extends PolicyCommonSupport {
+public class PolicyNotifierTest {
+ private static final String GROUP_A = "groupA";
+ private static final String PDP1 = "pdp-1";
+ private static final ToscaConceptIdentifier policy1 = new ToscaConceptIdentifier("policy1", "1.2.3");
+ private static final ToscaConceptIdentifier policy2 = new ToscaConceptIdentifier("policy2", "1.2.3");
@Mock
private Publisher<PolicyNotification> publisher;
@@ -70,10 +65,7 @@ public class PolicyNotifierTest extends PolicyCommonSupport {
private PolicyModelsProvider dao;
@Mock
- private PolicyDeployTracker deploy;
-
- @Mock
- private PolicyUndeployTracker undeploy;
+ private DeploymentStatus tracker;
@Mock
private PolicyStatus status1;
@@ -95,16 +87,13 @@ public class PolicyNotifierTest extends PolicyCommonSupport {
/**
* Creates various objects, including {@link #notifier}.
*/
- @Override
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
- super.setUp();
-
try {
when(daoFactory.create()).thenReturn(dao);
- when(dao.getPdpGroups(null)).thenReturn(Collections.emptyList());
+ when(dao.getGroupPolicyStatus(anyString())).thenReturn(Collections.emptyList());
notifier = new MyNotifier(publisher);
@@ -114,154 +103,31 @@ public class PolicyNotifierTest extends PolicyCommonSupport {
}
@Test
- public void testLoadPoliciesPolicyModelsProviderFactoryWrapper() throws PfModelException {
- final PdpGroup group1 = makeGroup("my group #1", makeSubGroup("sub #1 A", 2, policy1, policy4),
- makeSubGroup("sub #1 B", 1, policy2));
-
- // one policy is a duplicate
- final PdpGroup group2 = makeGroup("my group #2", makeSubGroup("sub #2 A", 1, policy1, policy3));
-
- when(dao.getPdpGroups(null)).thenReturn(Arrays.asList(group1, group2));
-
- ToscaConceptIdentifier type2 = new ToscaConceptIdentifier("my other type", "8.8.8");
-
- // note: no mapping for policy4
- when(dao.getFilteredPolicyList(any())).thenReturn(Arrays.asList(makePolicy(policy1, type),
- makePolicy(policy2, type2), makePolicy(policy3, type)));
-
- // load it
- notifier = new MyNotifier(publisher);
-
- ArgumentCaptor<PolicyPdpNotificationData> captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class);
-
- // should have added policy1, policy2, policy1 (duplicate), policy3, but not
- // policy4
- verify(deploy, times(4)).addData(captor.capture());
-
- Iterator<PolicyPdpNotificationData> iter = captor.getAllValues().iterator();
- PolicyPdpNotificationData data = iter.next();
- assertEquals(policy1, data.getPolicyId());
- assertEquals(type, data.getPolicyType());
- assertEquals("[sub #1 A 0, sub #1 A 1]", data.getPdps().toString());
-
- data = iter.next();
- assertEquals(policy2, data.getPolicyId());
- assertEquals(type2, data.getPolicyType());
- assertEquals("[sub #1 B 0]", data.getPdps().toString());
-
- data = iter.next();
- assertEquals(policy1, data.getPolicyId());
- assertEquals(type, data.getPolicyType());
- assertEquals("[sub #2 A 0]", data.getPdps().toString());
-
- data = iter.next();
- assertEquals(policy3, data.getPolicyId());
- assertEquals(type, data.getPolicyType());
- assertEquals("[sub #2 A 0]", data.getPdps().toString());
- }
-
- private ToscaPolicy makePolicy(ToscaConceptIdentifier policyId, ToscaConceptIdentifier type) {
- ToscaPolicy policy = new ToscaPolicy();
-
- policy.setName(policyId.getName());
- policy.setVersion(policyId.getVersion());
- policy.setType(type.getName());
- policy.setTypeVersion(type.getVersion());
-
- return policy;
- }
-
- private PdpGroup makeGroup(String name, PdpSubGroup... subgrps) {
- final PdpGroup group = new PdpGroup();
- group.setName(name);
-
- group.setPdpSubgroups(Arrays.asList(subgrps));
-
- return group;
- }
-
- private PdpSubGroup makeSubGroup(String name, int numPdps, ToscaConceptIdentifier... policies) {
- final PdpSubGroup subgrp = new PdpSubGroup();
- subgrp.setPdpType(name);
- subgrp.setPdpInstances(new ArrayList<>(numPdps));
-
- for (int x = 0; x < numPdps; ++x) {
- Pdp pdp = new Pdp();
- pdp.setInstanceId(name + " " + x);
-
- subgrp.getPdpInstances().add(pdp);
- }
-
- subgrp.setPolicies(Arrays.asList(policies));
-
- return subgrp;
- }
-
- @Test
- public void testGetStatus() {
- List<PolicyStatus> statusList = Arrays.asList(status1);
- when(deploy.getStatus()).thenReturn(statusList);
-
- assertSame(statusList, notifier.getStatus());
- }
-
- @Test
- public void testGetStatusString() {
- List<PolicyStatus> statusList = Arrays.asList(status1);
- when(deploy.getStatus("a policy")).thenReturn(statusList);
-
- assertSame(statusList, notifier.getStatus("a policy"));
- }
-
- @Test
- public void testGetStatusToscaConceptIdentifier() {
- Optional<PolicyStatus> status = Optional.of(status1);
- when(deploy.getStatus(policy1)).thenReturn(status);
-
- assertSame(status, notifier.getStatus(policy1));
- }
-
- @Test
- public void testAddDeploymentData() {
- PolicyPdpNotificationData data = makeData(policy1, PDP1, PDP2);
- notifier.addDeploymentData(data);
-
- verify(deploy).addData(data);
- verify(undeploy).removeData(eq(data));
- }
-
- @Test
- public void testAddUndeploymentData() {
- PolicyPdpNotificationData data = makeData(policy1, PDP1, PDP2);
- notifier.addUndeploymentData(data);
-
- verify(undeploy).addData(data);
- verify(deploy).removeData(eq(data));
- }
+ public void testProcessResponseString() throws PfModelException {
+ Set<ToscaConceptIdentifier> expected = Set.of(policy1);
+ Set<ToscaConceptIdentifier> actual = Set.of(policy2);
+
+ // add a status to the notification when tracker.flush(notif) is called
+ doAnswer(invocation -> {
+ PolicyNotification notif = invocation.getArgument(0);
+ notif.getAdded().add(new PolicyStatus());
+ return null;
+ }).when(tracker).flush(any());
- @Test
- public void testProcessResponseString() {
- doAnswer(addStatus(2, status1, status2)).when(deploy).processResponse(eq(PDP1), any(), any());
- doAnswer(addStatus(2, status3, status4)).when(undeploy).processResponse(eq(PDP1), any(), any());
+ notifier.processResponse(PDP1, GROUP_A, expected, actual);
- List<ToscaConceptIdentifier> activePolicies = Arrays.asList(policy1, policy2);
- notifier.processResponse(PDP1, activePolicies);
+ verify(tracker).loadByGroup(GROUP_A);
+ verify(tracker).completeDeploy(PDP1, expected, actual);
+ verify(tracker).flush(any());
- PolicyNotification notification = getNotification();
- assertEquals(Arrays.asList(status1, status2), notification.getAdded());
- assertEquals(Arrays.asList(status3, status4), notification.getDeleted());
+ verify(publisher).enqueue(any());
}
@Test
- public void testRemovePdp() {
- doAnswer(addStatus(1, status1, status2)).when(deploy).removePdp(eq(PDP1), any());
- doAnswer(addStatus(1, status3, status4)).when(undeploy).removePdp(eq(PDP1), any());
-
- notifier.removePdp(PDP1);
+ public void testProcessResponseString_Ex() throws PfModelException {
+ doThrow(new PfModelException(Status.BAD_REQUEST, "expected exception")).when(tracker).loadByGroup(anyString());
- PolicyNotification notification = getNotification();
- assertEquals(Arrays.asList(status1, status2), notification.getAdded());
- assertEquals(Arrays.asList(status3, status4), notification.getDeleted());
+ assertThatCode(() -> notifier.processResponse(PDP1, GROUP_A, Set.of(), Set.of())).doesNotThrowAnyException();
}
/**
@@ -269,8 +135,7 @@ public class PolicyNotifierTest extends PolicyCommonSupport {
*/
@Test
public void testPublishEmpty() {
- notifier.removePdp(PDP1);
-
+ notifier.publish(new PolicyNotification());
verify(publisher, never()).enqueue(any());
}
@@ -279,45 +144,20 @@ public class PolicyNotifierTest extends PolicyCommonSupport {
*/
@Test
public void testPublishNotEmpty() {
- doAnswer(addStatus(1, status1, status2)).when(deploy).removePdp(eq(PDP1), any());
+ PolicyNotification notif = new PolicyNotification();
+ notif.getAdded().add(new PolicyStatus());
- notifier.removePdp(PDP1);
+ notifier.publish(notif);
verify(publisher).enqueue(any());
}
@Test
- public void testMakeDeploymentTracker_testMakeUndeploymentTracker() throws PfModelException {
+ public void testMakeDeploymentTracker() throws PfModelException {
// make real object, which will invoke the real makeXxx() methods
- new PolicyNotifier(publisher, daoFactory).removePdp(PDP1);
+ new PolicyNotifier(publisher, daoFactory).processResponse(PDP1, GROUP_A, Set.of(), Set.of());
- verify(publisher, never()).enqueue(any());
- }
-
- /**
- * Creates an answer that adds status updates to a status list.
- *
- * @param listIndex index of the status list within the argument list
- * @param status status updates to be added
- * @return an answer that adds the given status updates
- */
- private Answer<Void> addStatus(int listIndex, PolicyStatus... status) {
- return invocation -> {
- @SuppressWarnings("unchecked")
- List<PolicyStatus> statusList = invocation.getArgument(listIndex, List.class);
- statusList.addAll(Arrays.asList(status));
- return null;
- };
- }
-
- /**
- * Gets the notification that was published.
- *
- * @return the notification that was published
- */
- private PolicyNotification getNotification() {
- verify(publisher).enqueue(notifyCaptor.capture());
- return notifyCaptor.getValue().get();
+ verify(dao).getGroupPolicyStatus(GROUP_A);
}
@@ -328,13 +168,8 @@ public class PolicyNotifierTest extends PolicyCommonSupport {
}
@Override
- protected PolicyDeployTracker makeDeploymentTracker() {
- return deploy;
- }
-
- @Override
- protected PolicyUndeployTracker makeUndeploymentTracker() {
- return undeploy;
+ protected DeploymentStatus makeDeploymentTracker(PolicyModelsProvider dao) {
+ return tracker;
}
}
}
diff --git a/main/src/test/java/org/onap/policy/pap/main/notification/PolicyPdpNotificationDataTest.java b/main/src/test/java/org/onap/policy/pap/main/notification/PolicyPdpNotificationDataTest.java
deleted file mode 100644
index 67a3cb1f..00000000
--- a/main/src/test/java/org/onap/policy/pap/main/notification/PolicyPdpNotificationDataTest.java
+++ /dev/null
@@ -1,121 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * ONAP PAP
- * ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2021 Nordix Foundation.
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.pap.main.notification;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertTrue;
-
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.TreeSet;
-import org.junit.Before;
-import org.junit.Test;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
-
-/**
- * Note: this wraps the PDPs in a TreeSet so that the content can be verified without
- * worrying about order.
- */
-public class PolicyPdpNotificationDataTest {
- private static final String PDP1 = "pdp-1";
- private static final String PDP2 = "pdp-2";
- private static final String PDP3 = "pdp-3";
- private static final String PDP4 = "pdp-4";
-
- private ToscaConceptIdentifier policyId;
- private ToscaConceptIdentifier policyType;
- private PolicyPdpNotificationData data;
-
- /**
- * Creates various objects, including {@link #data}.
- */
- @Before
- public void setUp() {
- policyId = new ToscaConceptIdentifier("my-id", "1.2.3");
- policyType = new ToscaConceptIdentifier("my-type", "3.2.1");
-
- data = new PolicyPdpNotificationData(policyId, policyType);
- }
-
- @Test
- public void testPolicyPdpNotificationData() {
- assertSame(policyId, data.getPolicyId());
- assertSame(policyType, data.getPolicyType());
- assertTrue(data.getPdps().isEmpty());
- }
-
- @Test
- public void testIsEmpty() {
- assertTrue(data.isEmpty());
-
- data.add(PDP1);
- assertFalse(data.isEmpty());
-
- data.add(PDP2);
- data.add(PDP3);
- assertFalse(data.isEmpty());
-
- data.removeAll(Arrays.asList(PDP1, PDP3));
- assertFalse(data.isEmpty());
-
- data.removeAll(Arrays.asList(PDP2));
- assertTrue(data.isEmpty());
- }
-
- @Test
- public void testAdd() {
- data.add(PDP1);
- assertEquals("[pdp-1]", new TreeSet<>(data.getPdps()).toString());
-
- data.add(PDP3);
- assertEquals("[pdp-1, pdp-3]", new TreeSet<>(data.getPdps()).toString());
- }
-
- @Test
- public void testAddAll() {
- // verify we can add an empty list
- data.addAll(Collections.emptyList());
- assertTrue(data.getPdps().isEmpty());
-
- // try a non-empty list
- data.addAll(Arrays.asList(PDP1, PDP3));
- assertEquals("[pdp-1, pdp-3]", new TreeSet<>(data.getPdps()).toString());
- }
-
- @Test
- public void testRemoveAll() {
- // verify we can remove an empty list
- data.removeAll(Collections.emptyList());
- assertTrue(data.getPdps().isEmpty());
-
- // now test with non-empty lists
- data.addAll(Arrays.asList(PDP1, PDP2, PDP3, PDP4));
-
- data.removeAll(Arrays.asList(PDP1, PDP3));
- assertEquals("[pdp-2, pdp-4]", new TreeSet<>(data.getPdps()).toString());
-
- data.removeAll(Arrays.asList(PDP2, PDP4));
- assertEquals("[]", new TreeSet<>(data.getPdps()).toString());
- }
-}
diff --git a/main/src/test/java/org/onap/policy/pap/main/notification/PolicyTrackerDataTest.java b/main/src/test/java/org/onap/policy/pap/main/notification/PolicyTrackerDataTest.java
deleted file mode 100644
index 2d27b75e..00000000
--- a/main/src/test/java/org/onap/policy/pap/main/notification/PolicyTrackerDataTest.java
+++ /dev/null
@@ -1,350 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * ONAP PAP
- * ================================================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2021 Nordix Foundation.
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.pap.main.notification;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertTrue;
-
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.List;
-import org.junit.Before;
-import org.junit.Test;
-import org.onap.policy.models.pap.concepts.PolicyStatus;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
-
-public class PolicyTrackerDataTest {
-
- private static final ToscaConceptIdentifier TYPE = new ToscaConceptIdentifier("my-type", "1.2.3");
- private static final String PDP1 = "pdp-1";
- private static final String PDP2 = "pdp-2";
- private static final String PDP3 = "pdp-3";
- private static final String PDP4 = "pdp-4";
- private static final String PDP5 = "pdp-5";
- private static final String PDP6 = "pdp-6";
-
- private Collection<String> fullSet;
- private PolicyTrackerData data;
-
- @Before
- public void setUp() {
- fullSet = Arrays.asList(PDP1, PDP2, PDP3, PDP4, PDP5, PDP6);
- data = new PolicyTrackerData(TYPE);
- }
-
- @Test
- public void testPolicyTrackerData_testGetPolicyType() {
- assertSame(TYPE, data.getPolicyType());
- }
-
- @Test
- public void testIsComplete() {
- assertTrue(data.isComplete());
-
- data.addPdps(Arrays.asList(PDP1, PDP2));
- assertFalse(data.isComplete());
-
- data.success(PDP1);
- assertFalse(data.isComplete());
-
- data.fail(PDP2);
- assertTrue(data.isComplete());
- }
-
- @Test
- public void testAllSucceeded() {
- assertTrue(data.allSucceeded());
-
- data.addPdps(Arrays.asList(PDP1, PDP2));
- assertFalse(data.allSucceeded());
-
- data.success(PDP1);
- assertFalse(data.allSucceeded());
-
- data.fail(PDP2);
- assertFalse(data.allSucceeded());
-
- data.success(PDP2);
- assertTrue(data.allSucceeded());
-
- data.fail(PDP2);
- assertFalse(data.allSucceeded());
-
- data.success(PDP2);
- assertTrue(data.allSucceeded());
- }
-
- @Test
- public void testIsEmpty() {
- assertTrue(data.isEmpty());
-
- data.addPdps(Arrays.asList(PDP1, PDP2));
- assertFalse(data.isEmpty());
-
- data.success(PDP1);
- assertFalse(data.isEmpty());
-
- data.fail(PDP2);
- assertFalse(data.isEmpty());
-
- data.removePdp(PDP1);
- assertFalse(data.isEmpty());
-
- data.removePdp(PDP2);
- assertTrue(data.isEmpty());
- }
-
- @Test
- public void testPutValuesInto() {
- data.addPdps(fullSet);
- data.success(PDP1);
- data.fail(PDP2);
- data.fail(PDP3);
-
- PolicyStatus status = new PolicyStatus();
- data.putValuesInto(status);
-
- assertEquals(1, status.getSuccessCount());
- assertEquals(2, status.getFailureCount());
- assertEquals(3, status.getIncompleteCount());
- }
-
- @Test
- public void testAddPdps_testSuccess_testFail() {
- data.addPdps(Arrays.asList(PDP1, PDP2, PDP3, PDP4));
- assertEquals("[0, 0, 4]", getCounts().toString());
-
- data.success(PDP1);
- assertEquals("[1, 0, 3]", getCounts().toString());
-
- data.success(PDP2);
- assertEquals("[2, 0, 2]", getCounts().toString());
-
- // repeat
- data.success(PDP2);
- assertEquals("[2, 0, 2]", getCounts().toString());
-
- data.fail(PDP3);
- assertEquals("[2, 1, 1]", getCounts().toString());
-
- // repeat
- data.fail(PDP3);
- assertEquals("[2, 1, 1]", getCounts().toString());
-
- data.addPdps(Arrays.asList(PDP2, PDP3, PDP4, PDP5));
-
- // PDP1 is still success
- assertEquals("[1, 0, 4]", getCounts().toString());
- }
-
- @Test
- public void testRemovePdps() {
- data.addPdps(Arrays.asList(PDP1, PDP2, PDP3, PDP4, PDP5, PDP6));
- data.success(PDP1);
- data.success(PDP2);
- data.fail(PDP3);
- data.fail(PDP4);
- assertFalse(data.removePdps(Arrays.asList(PDP1, PDP3, PDP5)));
- assertEquals("[1, 1, 1]", getCounts().toString());
-
- assertTrue(data.removePdps(Arrays.asList(PDP6)));
- assertEquals("[1, 1, 0]", getCounts().toString());
- }
-
- /**
- * Tests removePdps(), where nothing is removed from the "incomplete" set.
- */
- @Test
- public void testRemovePdpsNoIncompleteRemove() {
- assertFalse(data.removePdps(Arrays.asList(PDP1, PDP2)));
- assertEquals("[0, 0, 0]", getCounts().toString());
- }
-
- /**
- * Tests removePdps(), where remaining incomplete items are removed.
- */
- @Test
- public void testRemovePdpsAllComplete() {
- data.addPdps(Arrays.asList(PDP1));
- assertTrue(data.removePdps(Arrays.asList(PDP1)));
-
- data.addPdps(Arrays.asList(PDP1, PDP2, PDP3));
- assertFalse(data.removePdps(Arrays.asList(PDP1)));
- assertTrue(data.removePdps(Arrays.asList(PDP2, PDP3)));
- }
-
- /**
- * Tests removePdps() with more variations.
- */
- @Test
- public void testRemovePdpsVariations() {
- data.addPdps(Arrays.asList(PDP1, PDP2, PDP3));
- data.success(PDP1);
- data.fail(PDP2);
- assertEquals("[1, 1, 1]", getCounts().toString());
-
- // remove PDP1, which checks removal from "success" set, while incomplete
- assertFalse(data.removePdps(Arrays.asList(PDP1)));
- assertEquals("[0, 1, 1]", getCounts().toString());
-
- // remove PDP2, which checks removal from "failure" set, while incomplete
- assertFalse(data.removePdps(Arrays.asList(PDP2)));
- assertEquals("[0, 0, 1]", getCounts().toString());
-
- // re-add 1 & 2
- data.addPdps(Arrays.asList(PDP1, PDP2));
- data.success(PDP1);
- data.fail(PDP2);
- assertEquals("[1, 1, 1]", getCounts().toString());
-
- // remove PDP3, which checks removal from "incomplete" set
- assertTrue(data.removePdps(Arrays.asList(PDP3)));
- assertEquals("[1, 1, 0]", getCounts().toString());
-
- // remove PDP1, which checks removal from "success" set, while complete
- assertTrue(data.removePdps(Arrays.asList(PDP1)));
- assertEquals("[0, 1, 0]", getCounts().toString());
-
- // remove PDP2, which checks removal from "failure" set, while complete
- assertTrue(data.removePdps(Arrays.asList(PDP2)));
- assertEquals("[0, 0, 0]", getCounts().toString());
-
- // re-add 1 and then remove it again
- data.addPdps(Arrays.asList(PDP1));
- assertTrue(data.removePdps(Arrays.asList(PDP1)));
- assertEquals("[0, 0, 0]", getCounts().toString());
- }
-
- @Test
- public void testRemovePdp() {
- data.addPdps(Arrays.asList(PDP1, PDP2, PDP3, PDP4, PDP5, PDP6));
- data.success(PDP1);
- data.success(PDP2);
- data.fail(PDP3);
- data.fail(PDP4);
-
- assertFalse(data.removePdp(PDP1));
- assertEquals("[1, 2, 2]", getCounts().toString());
-
- assertFalse(data.removePdp(PDP2));
- assertEquals("[0, 2, 2]", getCounts().toString());
-
- assertFalse(data.removePdp(PDP3));
- assertEquals("[0, 1, 2]", getCounts().toString());
-
- assertFalse(data.removePdp(PDP4));
- assertEquals("[0, 0, 2]", getCounts().toString());
-
- assertFalse(data.removePdp(PDP5));
- assertEquals("[0, 0, 1]", getCounts().toString());
-
- assertTrue(data.removePdp(PDP6));
- assertEquals("[0, 0, 0]", getCounts().toString());
- }
-
- /**
- * Tests removePdps(), where nothing is removed from the "incomplete" set.
- */
- @Test
- public void testRemovePdpNoIncompleteRemove() {
- assertFalse(data.removePdp(PDP1));
- assertEquals("[0, 0, 0]", getCounts().toString());
- }
-
- /**
- * Tests removePdps(), where remaining incomplete items are removed.
- */
- @Test
- public void testRemovePdpAllComplete() {
- data.addPdps(Arrays.asList(PDP1, PDP2));
- assertFalse(data.removePdp(PDP1));
-
- assertTrue(data.removePdp(PDP2));
- }
-
- /**
- * Tests removePdp() with more variations.
- */
- @Test
- public void testRemovePdpVariations() {
- data.addPdps(Arrays.asList(PDP1, PDP2, PDP3));
- data.success(PDP1);
- data.fail(PDP2);
- assertEquals("[1, 1, 1]", getCounts().toString());
-
- // remove PDP1, which checks removal from "success" set, while incomplete
- assertFalse(data.removePdp(PDP1));
- assertEquals("[0, 1, 1]", getCounts().toString());
-
- // remove PDP2, which checks removal from "failure" set, while incomplete
- assertFalse(data.removePdp(PDP2));
- assertEquals("[0, 0, 1]", getCounts().toString());
-
- // re-add 1 & 2
- data.addPdps(Arrays.asList(PDP1, PDP2));
- data.success(PDP1);
- data.fail(PDP2);
- assertEquals("[1, 1, 1]", getCounts().toString());
-
- // remove PDP3, which checks removal from "incomplete" set
- assertTrue(data.removePdp(PDP3));
- assertEquals("[1, 1, 0]", getCounts().toString());
-
- // remove PDP1, which checks removal from "success" set, while complete
- assertTrue(data.removePdp(PDP1));
- assertEquals("[0, 1, 0]", getCounts().toString());
-
- // remove PDP2, which checks removal from "failure" set, while complete
- assertTrue(data.removePdp(PDP2));
- assertEquals("[0, 0, 0]", getCounts().toString());
-
- // re-add 1 and then remove it again
- data.addPdps(Arrays.asList(PDP1));
- assertTrue(data.removePdp(PDP1));
- assertEquals("[0, 0, 0]", getCounts().toString());
- }
-
- @Test
- public void testComplete() {
- // attempt to remove a PDP that isn't in the data
- assertFalse(data.success(PDP1));
-
- // remove one that was incomplete
- data.addPdps(Arrays.asList(PDP1));
- assertTrue(data.success(PDP1));
-
- // move from one set to the other
- assertTrue(data.fail(PDP1));
-
- // already in the correct set
- assertFalse(data.fail(PDP1));
- }
-
- private List<Integer> getCounts() {
- PolicyStatus status = new PolicyStatus();
- data.putValuesInto(status);
-
- return Arrays.asList(status.getSuccessCount(), status.getFailureCount(), status.getIncompleteCount());
- }
-}
diff --git a/main/src/test/java/org/onap/policy/pap/main/notification/PolicyUndeployTrackerTest.java b/main/src/test/java/org/onap/policy/pap/main/notification/PolicyUndeployTrackerTest.java
deleted file mode 100644
index 7d3f54fb..00000000
--- a/main/src/test/java/org/onap/policy/pap/main/notification/PolicyUndeployTrackerTest.java
+++ /dev/null
@@ -1,264 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * ONAP PAP
- * ================================================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.pap.main.notification;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-import static org.mockito.Matchers.any;
-import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-import org.junit.Before;
-import org.junit.Test;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.onap.policy.models.pap.concepts.PolicyStatus;
-
-public class PolicyUndeployTrackerTest extends PolicyCommonSupport {
-
- @Mock
- private PolicyTrackerData data;
-
- private PolicyUndeployTracker tracker;
-
- /**
- * Creates various objects, including {@link #tracker}.
- */
- @Before
- public void setUp() {
- MockitoAnnotations.initMocks(this);
-
- super.setUp();
-
- tracker = new PolicyUndeployTracker();
- }
-
- /**
- * Simple test with one PDP that immediately responds with success.
- */
- @Test
- public void testSimpleImmediateSuccess() {
- tracker.addData(makeData(policy1, PDP1));
-
- // indicate that PDP1 has succeeded (i.e., undeployed)
- List<PolicyStatus> statusList = new ArrayList<>();
- tracker.processResponse(PDP1, Arrays.asList(), statusList);
-
- assertEquals(1, statusList.size());
- assertEquals(policy1, statusList.get(0).getPolicy());
- assertEquals(type, statusList.get(0).getPolicyType());
- assertEquals("[1, 0, 0]", getCounts(statusList.get(0)).toString());
-
- // indicate that PDP1 has succeeded again - should be no output
- statusList.clear();
- tracker.processResponse(PDP1, Arrays.asList(), statusList);
- assertEquals(0, statusList.size());
-
- // indicate failure (i.e., still deployed) - no output, because no longer tracked
- statusList.clear();
- tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
- assertEquals(0, statusList.size());
-
- // indicate that PDP1 has failed again - still no output
- statusList.clear();
- tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
- assertEquals(0, statusList.size());
-
- // indicate that PDP1 has succeeded again - still no output
- statusList.clear();
- tracker.processResponse(PDP1, Arrays.asList(), statusList);
- assertEquals(0, statusList.size());
- }
-
- /**
- * Simple test with one PDP that immediately responds with success.
- */
- @Test
- public void testSimpleImmediateFail() {
- tracker.addData(makeData(policy1, PDP1));
-
- // indicate that PDP1 has failed (i.e., still deployed)
- List<PolicyStatus> statusList = new ArrayList<>();
- tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
- assertEquals(1, statusList.size());
- assertEquals("[0, 1, 0]", getCounts(statusList.get(0)).toString());
-
- // indicate that PDP1 has failed again - should be no output
- statusList.clear();
- tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
- assertEquals(0, statusList.size());
-
- // indicate success (i.e., undeployed)
- tracker.processResponse(PDP1, Arrays.asList(), statusList);
-
- assertEquals(1, statusList.size());
- assertEquals(policy1, statusList.get(0).getPolicy());
- assertEquals(type, statusList.get(0).getPolicyType());
- assertEquals("[1, 0, 0]", getCounts(statusList.get(0)).toString());
-
- // indicate that PDP1 has succeeded again - should be no output
- statusList.clear();
- tracker.processResponse(PDP1, Arrays.asList(), statusList);
- assertEquals(0, statusList.size());
-
- // indicate that PDP1 has failed again - still no output
- statusList.clear();
- tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
- assertEquals(0, statusList.size());
- }
-
- /**
- * Simple test where PDP is removed and then it responds.
- */
- @Test
- public void testSimpleRemove() {
- tracker.addData(makeData(policy1, PDP1));
-
- // remove the PDP
- List<PolicyStatus> statusList = new ArrayList<>();
- tracker.removePdp(PDP1, statusList);
- assertEquals(1, statusList.size());
- assertEquals(policy1, statusList.get(0).getPolicy());
- assertEquals(type, statusList.get(0).getPolicyType());
- assertEquals("[0, 0, 0]", getCounts(statusList.get(0)).toString());
-
- /*
- * indicate that PDP1 has succeeded (i.e., undeployed) - should be no message
- * since PDP was removed from the policy
- */
- statusList.clear();
- tracker.processResponse(PDP1, Arrays.asList(), statusList);
- assertEquals(0, statusList.size());
-
- /*
- * indicate that PDP1 has failed (i.e., still deployed) - should be no message
- * since PDP was removed from the policy
- */
- statusList.clear();
- tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
- assertEquals(0, statusList.size());
- }
-
- /**
- * Test with multiple PDPs.
- */
- @Test
- public void testMulti() {
- tracker.addData(makeData(policy1, PDP1, PDP2));
-
- // indicate that PDP2 has been undeployed
- tracker.processResponse(PDP2, Collections.emptyList(), new ArrayList<>(0));
-
- // indicate that PDP1 has been undeployed
- List<PolicyStatus> statusList = new ArrayList<>();
- tracker.processResponse(PDP1, Collections.emptyList(), statusList);
-
- assertEquals(1, statusList.size());
- assertEquals(policy1, statusList.get(0).getPolicy());
- assertEquals(type, statusList.get(0).getPolicyType());
- assertEquals("[2, 0, 0]", getCounts(statusList.get(0)).toString());
-
- /*
- * indicate that PDP1 has been re-deployed - should not get a notification,
- * because policy is gone
- */
- statusList.clear();
- tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
- assertTrue(statusList.isEmpty());
- }
-
- /**
- * Test with multiple PDPs, and one is removed while it is in a failure state.
- */
- @Test
- public void testMultiRemove() {
- tracker.addData(makeData(policy1, PDP1, PDP2));
-
- // indicate that PDP2 has been undeployed
- tracker.processResponse(PDP2, Collections.emptyList(), new ArrayList<>(0));
-
- // indicate that PDP1 has failed (i.e., still deployed)
- List<PolicyStatus> statusList = new ArrayList<>();
- tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
-
- assertEquals(1, statusList.size());
- assertEquals(policy1, statusList.get(0).getPolicy());
- assertEquals(type, statusList.get(0).getPolicyType());
- assertEquals("[1, 1, 0]", getCounts(statusList.get(0)).toString());
-
- // remove PDP1 - expect message AND policy should be removed
- statusList.clear();
- tracker.removePdp(PDP1, statusList);
- assertEquals(1, statusList.size());
- assertEquals("[1, 0, 0]", getCounts(statusList.get(0)).toString());
-
- // re-add PDP1
- tracker.addData(makeData(policy1, PDP1));
-
- // indicate that PDP1 has succeeded; policy is now new, so doesn't include PDP2
- statusList.clear();
- tracker.processResponse(PDP1, Arrays.asList(), statusList);
- assertEquals(1, statusList.size());
- assertEquals("[1, 0, 0]", getCounts(statusList.get(0)).toString());
- }
-
- @Test
- public void testUpdateData() {
- // when success returns false
- assertFalse(tracker.updateData(PDP1, data, false));
- verify(data).success(PDP1);
- verify(data, never()).fail(any());
-
- // when inactive
- assertFalse(tracker.updateData(PDP1, data, true));
- verify(data).success(PDP1);
- verify(data).fail(any());
-
- // when success & fail return true
- when(data.success(PDP1)).thenReturn(true);
- when(data.fail(PDP1)).thenReturn(true);
- assertTrue(tracker.updateData(PDP1, data, false));
- verify(data, times(2)).success(PDP1);
- verify(data, times(1)).fail(PDP1);
-
- // when inactive
- assertTrue(tracker.updateData(PDP1, data, true));
- verify(data, times(2)).success(PDP1);
- verify(data, times(2)).fail(PDP1);
- }
-
- @Test
- public void testShouldRemove() {
- // when data is not complete
- assertFalse(tracker.shouldRemove(data));
-
- // when data has succeeded
- when(data.allSucceeded()).thenReturn(true);
- assertTrue(tracker.shouldRemove(data));
- }
-}
diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/ProviderSuper.java b/main/src/test/java/org/onap/policy/pap/main/rest/ProviderSuper.java
index a0fa196e..4241b41f 100644
--- a/main/src/test/java/org/onap/policy/pap/main/rest/ProviderSuper.java
+++ b/main/src/test/java/org/onap/policy/pap/main/rest/ProviderSuper.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP PAP
* ================================================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2021 AT&T Intellectual Property. 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.
@@ -20,6 +20,7 @@
package org.onap.policy.pap.main.rest;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
@@ -42,6 +43,7 @@ import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.common.utils.services.Registry;
+import org.onap.policy.models.pap.concepts.PolicyNotification;
import org.onap.policy.models.pdp.concepts.PdpGroup;
import org.onap.policy.models.pdp.concepts.PdpGroups;
import org.onap.policy.models.pdp.concepts.PdpStateChange;
@@ -267,6 +269,15 @@ public class ProviderSuper {
}
/**
+ * Verifies that an empty notification was published.
+ */
+ protected void checkEmptyNotification() {
+ ArgumentCaptor<PolicyNotification> captor = ArgumentCaptor.forClass(PolicyNotification.class);
+ verify(notifier).publish(captor.capture());
+ assertThat(captor.getValue().isEmpty()).isTrue();
+ }
+
+ /**
* Wraps a list of policies. The decoder doesn't work with generic lists, so we wrap
* the list and decode it into the wrapper before extracting the list contents.
*/
diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupCreateOrUpdateProvider.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupCreateOrUpdateProvider.java
index a753db1e..64534530 100644
--- a/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupCreateOrUpdateProvider.java
+++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupCreateOrUpdateProvider.java
@@ -3,6 +3,7 @@
* ONAP PAP
* ================================================================================
* Copyright (C) 2019-2021 Nordix Foundation.
+ * Modifications Copyright (C) 2021 AT&T Intellectual Property.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -284,7 +285,7 @@ public class TestPdpGroupCreateOrUpdateProvider extends ProviderSuper {
assertEquals(newgrp.toString(), dbgroup.toString());
// no deployment notifications
- verify(notifier, never()).addDeploymentData(any());
+ checkEmptyNotification();
// this requires a PDP UPDATE message
List<PdpUpdate> pdpUpdates = getUpdateRequests(2);
@@ -519,8 +520,7 @@ public class TestPdpGroupCreateOrUpdateProvider extends ProviderSuper {
assertEquals(newgrp.toString(), group.toString());
// no notifications
- verify(notifier, never()).addDeploymentData(any());
- verify(notifier, never()).addUndeploymentData(any());
+ checkEmptyNotification();
// no group updates
assertNoGroupAction();
diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeleteProvider.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeleteProvider.java
index 99aa3542..fc71c62c 100644
--- a/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeleteProvider.java
+++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeleteProvider.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP PAP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2020-2021 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -242,7 +242,7 @@ public class TestPdpGroupDeleteProvider extends ProviderSuper {
assertEquals(origSize - 1, subgroup.getPolicies().size());
assertFalse(subgroup.getPolicies().contains(ident));
- verify(session).trackUndeploy(eq(ident), pdpCaptor.capture());
+ verify(session).trackUndeploy(eq(ident), pdpCaptor.capture(), eq(group.getName()), eq(subgroup.getPdpType()));
assertEquals("[pdpA]", pdpCaptor.getValue().toString());
}
diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployProvider.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployProvider.java
index b6763008..a7f48ff3 100644
--- a/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployProvider.java
+++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployProvider.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP PAP
* ================================================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2021 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -27,7 +27,6 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -35,13 +34,11 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
-import java.util.TreeSet;
import java.util.stream.Collectors;
import javax.ws.rs.core.Response.Status;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
-import org.mockito.ArgumentCaptor;
import org.onap.policy.common.utils.services.Registry;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.base.PfModelRuntimeException;
@@ -56,7 +53,6 @@ import org.onap.policy.models.pdp.concepts.PdpSubGroup;
import org.onap.policy.models.pdp.concepts.PdpUpdate;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
-import org.onap.policy.pap.main.notification.PolicyPdpNotificationData;
public class TestPdpGroupDeployProvider extends ProviderSuper {
private static final String EXPECTED_EXCEPTION = "expected exception";
@@ -164,7 +160,8 @@ public class TestPdpGroupDeployProvider extends ProviderSuper {
}
/**
- * Tests updateGroupPolicies when policies are being added and deleted in the same subgroup.
+ * Tests updateGroupPolicies when policies are being added and deleted in the same
+ * subgroup.
*/
@Test
public void testUpdateGroupPoliciesAddAndDelete() throws Exception {
@@ -353,14 +350,8 @@ public class TestPdpGroupDeployProvider extends ProviderSuper {
assertEquals(newgrp.toString(), group.toString());
- // should have notified of added policy/PDPs
- ArgumentCaptor<PolicyPdpNotificationData> captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class);
- verify(notifier, times(2)).addDeploymentData(captor.capture());
- assertDeploymentData(captor.getAllValues().get(0), policyId2, "[pdpA]");
- assertDeploymentData(captor.getAllValues().get(1), policyId3, "[pdpA]");
-
- // should NOT have notified of any deleted policy/PDPs
- verify(notifier, never()).addUndeploymentData(any());
+ // nothing is complete - notification should be empty
+ checkEmptyNotification();
// this requires a PDP UPDATE message
assertGroupUpdate(newgrp, subgrp);
@@ -426,8 +417,7 @@ public class TestPdpGroupDeployProvider extends ProviderSuper {
assertEquals(newgrp.toString(), group.toString());
// no notifications
- verify(notifier, never()).addDeploymentData(any());
- verify(notifier, never()).addUndeploymentData(any());
+ checkEmptyNotification();
// no group updates
assertNoGroupAction();
@@ -472,8 +462,7 @@ public class TestPdpGroupDeployProvider extends ProviderSuper {
when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json"));
- assertThatThrownBy(() -> prov.updateGroupPolicies(groups))
- .isInstanceOf(PfModelException.class)
+ assertThatThrownBy(() -> prov.updateGroupPolicies(groups)).isInstanceOf(PfModelException.class)
.hasMessageContaining(newgrp.getPdpSubgroups().get(0).getPolicies().get(0).getName())
.hasMessageContaining("not a supported policy for the subgroup");
@@ -511,13 +500,8 @@ public class TestPdpGroupDeployProvider extends ProviderSuper {
List<PdpUpdate> requests = getUpdateRequests(1);
assertUpdate(requests, GROUP1_NAME, PDP2_TYPE, PDP2);
- // should have notified of added policy/PDPs
- ArgumentCaptor<PolicyPdpNotificationData> captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class);
- verify(notifier).addDeploymentData(captor.capture());
- assertDeploymentData(captor.getValue(), policy1.getIdentifier(), "[pdpB]");
-
- // no undeployment notifications
- verify(notifier, never()).addUndeploymentData(any());
+ // nothing is complete - notification should be empty
+ checkEmptyNotification();
}
@Test
@@ -581,13 +565,8 @@ public class TestPdpGroupDeployProvider extends ProviderSuper {
assertUpdate(requests, GROUP1_NAME, PDP2_TYPE, PDP2);
assertUpdate(requests, GROUP1_NAME, PDP4_TYPE, PDP4);
- // should have notified of added policy/PDPs
- ArgumentCaptor<PolicyPdpNotificationData> captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class);
- verify(notifier).addDeploymentData(captor.capture());
- assertDeploymentData(captor.getValue(), policy1.getIdentifier(), "[pdpB, pdpD]");
-
- // no undeployment notifications
- verify(notifier, never()).addUndeploymentData(any());
+ // nothing is complete - notification should be empty
+ checkEmptyNotification();
}
@Test
@@ -661,13 +640,6 @@ public class TestPdpGroupDeployProvider extends ProviderSuper {
assertEquals(Arrays.asList(group), updates);
}
- private void assertDeploymentData(PolicyPdpNotificationData data, ToscaConceptIdentifier policyId,
- String expectedPdps) {
- assertEquals(policyId, data.getPolicyId());
- assertEquals(policy1.getTypeIdentifier(), data.getPolicyType());
- assertEquals(expectedPdps, new TreeSet<>(data.getPdps()).toString());
- }
-
/**
* Loads a standard request.
*
diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestPolicyStatusProvider.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestPolicyStatusProvider.java
new file mode 100644
index 00000000..44c8d828
--- /dev/null
+++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestPolicyStatusProvider.java
@@ -0,0 +1,152 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2021 AT&T Intellectual Property. 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.pap.main.rest;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import lombok.NonNull;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.policy.common.utils.services.Registry;
+import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.pap.concepts.PolicyStatus;
+import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
+import org.onap.policy.models.pdp.concepts.PdpPolicyStatus.PdpPolicyStatusBuilder;
+import org.onap.policy.models.pdp.concepts.PdpPolicyStatus.State;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
+
+public class TestPolicyStatusProvider extends ProviderSuper {
+ private static final String MY_GROUP = "MyGroup";
+ private static final String MY_PDP_TYPE = "MySubGroup";
+ private static final @NonNull String VERSION = "1.2.3";
+ private static final String PDP_A = "pdpA";
+ private static final String PDP_B = "pdpB";
+ private static final String PDP_C = "pdpC";
+ private static final ToscaConceptIdentifier POLICY_TYPE = new ToscaConceptIdentifier("MyPolicyType", VERSION);
+ private static final ToscaConceptIdentifier POLICY_A = new ToscaConceptIdentifier("MyPolicyA", VERSION);
+ private static final ToscaConceptIdentifier POLICY_B = new ToscaConceptIdentifier("MyPolicyB", VERSION);
+ private static final ToscaConceptIdentifier POLICY_C = new ToscaConceptIdentifier("MyPolicyC", VERSION);
+
+ private PolicyStatusProvider prov;
+
+
+ @AfterClass
+ public static void tearDownAfterClass() {
+ Registry.newRegistry();
+ }
+
+ /**
+ * Configures mocks and objects.
+ *
+ * @throws Exception if an error occurs
+ */
+ @Override
+ @Before
+ public void setUp() throws Exception {
+
+ super.setUp();
+
+ prov = new PolicyStatusProvider();
+ }
+
+ @Test
+ public void testGetStatus_testAccumulate() throws PfModelException {
+
+ PdpPolicyStatusBuilder builder = PdpPolicyStatus.builder().pdpGroup(MY_GROUP).pdpType(MY_PDP_TYPE)
+ .policyType(POLICY_TYPE).state(State.WAITING);
+
+ PdpPolicyStatus notDeployed = builder.deploy(false).policy(POLICY_B).pdpId(PDP_A).build();
+
+ // remaining policies are deployed
+ builder.deploy(true);
+
+ // @formatter:off
+ when(dao.getAllPolicyStatus()).thenReturn(List.of(
+ builder.policy(POLICY_A).pdpId(PDP_A).build(),
+ builder.policy(POLICY_A).pdpId(PDP_B).build(),
+ notDeployed,
+ builder.policy(POLICY_C).pdpId(PDP_A).build()
+ ));
+ // @formatter:on
+
+ List<PolicyStatus> result = new ArrayList<>(prov.getStatus());
+ Collections.sort(result, (rec1, rec2) -> rec1.getPolicy().compareTo(rec2.getPolicy()));
+
+ assertThat(result).hasSize(2);
+
+ Iterator<PolicyStatus> iter = result.iterator();
+
+ PolicyStatus status = iter.next();
+ assertThat(status.getPolicy()).isEqualTo(POLICY_A);
+ assertThat(status.getPolicyType()).isEqualTo(POLICY_TYPE);
+ assertThat(status.getIncompleteCount()).isEqualTo(2);
+ assertThat(status.getFailureCount()).isZero();
+ assertThat(status.getSuccessCount()).isZero();
+
+ status = iter.next();
+ assertThat(status.getPolicy()).isEqualTo(POLICY_C);
+ assertThat(status.getPolicyType()).isEqualTo(POLICY_TYPE);
+ assertThat(status.getIncompleteCount()).isEqualTo(1);
+ assertThat(status.getFailureCount()).isZero();
+ assertThat(status.getSuccessCount()).isZero();
+ }
+
+ @Test
+ public void testGetStatusToscaConceptIdentifierOptVersion() throws PfModelException {
+
+ PdpPolicyStatusBuilder builder = PdpPolicyStatus.builder().pdpGroup(MY_GROUP).pdpType(MY_PDP_TYPE)
+ .policy(POLICY_A).policyType(POLICY_TYPE);
+
+ PdpPolicyStatus notDeployed = builder.deploy(false).pdpId(PDP_B).state(State.FAILURE).build();
+
+ // remaining policies are deployed
+ builder.deploy(true).state(State.WAITING);
+
+ ToscaConceptIdentifierOptVersion optIdent = new ToscaConceptIdentifierOptVersion(POLICY_A);
+
+ // @formatter:off
+ when(dao.getAllPolicyStatus(optIdent)).thenReturn(List.of(
+ builder.policy(POLICY_A).pdpId(PDP_A).build(),
+ notDeployed,
+ builder.policy(POLICY_A).pdpId(PDP_C).build()
+ ));
+ // @formatter:on
+
+ List<PolicyStatus> result = new ArrayList<>(prov.getStatus(optIdent));
+ assertThat(result).hasSize(1);
+
+ Iterator<PolicyStatus> iter = result.iterator();
+
+ PolicyStatus status = iter.next();
+ assertThat(status.getPolicy()).isEqualTo(POLICY_A);
+ assertThat(status.getPolicyType()).isEqualTo(POLICY_TYPE);
+ assertThat(status.getIncompleteCount()).isEqualTo(2);
+ assertThat(status.getFailureCount()).isZero();
+ assertThat(status.getSuccessCount()).isZero();
+ }
+}
diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestProviderBase.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestProviderBase.java
index 060173bb..b2c67c93 100644
--- a/main/src/test/java/org/onap/policy/pap/main/rest/TestProviderBase.java
+++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestProviderBase.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP PAP
* ================================================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2021 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -27,7 +27,6 @@ import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -36,12 +35,10 @@ import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
-import java.util.TreeSet;
import javax.ws.rs.core.Response.Status;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
-import org.mockito.ArgumentCaptor;
import org.onap.policy.common.utils.services.Registry;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.base.PfModelRuntimeException;
@@ -52,7 +49,6 @@ import org.onap.policy.models.pdp.concepts.PdpUpdate;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
-import org.onap.policy.pap.main.notification.PolicyPdpNotificationData;
import org.powermock.reflect.Whitebox;
public class TestProviderBase extends ProviderSuper {
@@ -110,23 +106,7 @@ public class TestProviderBase extends ProviderSuper {
assertUpdate(getUpdateRequests(1), GROUP1_NAME, PDP1_TYPE, PDP1);
- ArgumentCaptor<PolicyPdpNotificationData> captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class);
- verify(notifier, times(2)).addDeploymentData(captor.capture());
- assertNotifier(captor, PDP1, PDP3);
-
- captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class);
- verify(notifier, times(2)).addUndeploymentData(captor.capture());
- assertNotifier(captor, PDP2, PDP4);
- }
-
- private void assertNotifier(ArgumentCaptor<PolicyPdpNotificationData> captor, String firstPdp, String secondPdp) {
- assertEquals(1, captor.getAllValues().get(0).getPdps().size());
- assertEquals(1, captor.getAllValues().get(1).getPdps().size());
-
- // ensure the order by using a TreeSet
- TreeSet<String> pdps = new TreeSet<>(captor.getAllValues().get(0).getPdps());
- pdps.addAll(captor.getAllValues().get(1).getPdps());
- assertEquals("[" + firstPdp + ", " + secondPdp + "]", pdps.toString());
+ checkEmptyNotification();
}
@Test
@@ -376,15 +356,17 @@ public class TestProviderBase extends ProviderSuper {
return (group, subgroup) -> {
if (shouldUpdate.remove()) {
+ ToscaConceptIdentifier ident1 = policy.getIdentifier();
+
// queue indicated that the update should succeed
- subgroup.getPolicies().add(policy.getIdentifier());
+ subgroup.getPolicies().add(ident1);
- data.trackDeploy(policy.getIdentifier(), Collections.singleton(PDP1));
- data.trackUndeploy(policy.getIdentifier(), Collections.singleton(PDP2));
+ data.trackDeploy(ident1, Collections.singleton(PDP1), GROUP1_NAME, PDP1_TYPE);
+ data.trackUndeploy(ident1, Collections.singleton(PDP2), GROUP1_NAME, PDP2_TYPE);
ToscaConceptIdentifier ident2 = new ToscaConceptIdentifier(POLICY1_NAME, "9.9.9");
- data.trackDeploy(ident2, Collections.singleton(PDP3));
- data.trackUndeploy(ident2, Collections.singleton(PDP4));
+ data.trackDeploy(ident2, Collections.singleton(PDP3), GROUP1_NAME, PDP3_TYPE);
+ data.trackUndeploy(ident2, Collections.singleton(PDP4), GROUP1_NAME, PDP4_TYPE);
return true;
} else {
diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestSessionData.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestSessionData.java
index a2914cfa..3fbe4785 100644
--- a/main/src/test/java/org/onap/policy/pap/main/rest/TestSessionData.java
+++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestSessionData.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP PAP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2021 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -21,6 +21,7 @@
package org.onap.policy.pap.main.rest;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@@ -30,6 +31,8 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@@ -42,26 +45,27 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
-import java.util.TreeSet;
-import java.util.function.Supplier;
import javax.ws.rs.core.Response.Status;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.pap.concepts.PolicyNotification;
import org.onap.policy.models.pdp.concepts.PdpGroup;
import org.onap.policy.models.pdp.concepts.PdpStateChange;
import org.onap.policy.models.pdp.concepts.PdpUpdate;
+import org.onap.policy.models.provider.PolicyModelsProvider;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
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.ToscaPolicyType;
-import org.onap.policy.pap.main.notification.PolicyPdpNotificationData;
+import org.onap.policy.pap.main.notification.DeploymentStatus;
public class TestSessionData extends ProviderSuper {
private static final String GROUP_NAME = "groupA";
+ private static final String PDP_TYPE = "MySubGroup";
private static final String PDP1 = "pdp_1";
private static final String PDP2 = "pdp_2";
private static final String PDP3 = "pdp_3";
@@ -505,7 +509,9 @@ public class TestSessionData extends ProviderSuper {
session.update(newgrp5);
// push the changes to the DB
- session.updateDb();
+ PolicyNotification notif = new PolicyNotification();
+ session.updateDb(notif);
+ assertThat(notif.getAdded()).isEmpty();
// expect one create for groups 4 & 5 (group5 replaced by newgrp5)
List<PdpGroup> creates = getGroupCreates();
@@ -526,7 +532,10 @@ public class TestSessionData extends ProviderSuper {
when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1, group2));
session.getActivePdpGroupsByPolicyType(type);
- session.updateDb();
+ PolicyNotification notif = new PolicyNotification();
+ session.updateDb(notif);
+ assertThat(notif.getAdded()).isEmpty();
+
verify(dao, never()).createPdpGroups(any());
verify(dao, never()).updatePdpGroups(any());
}
@@ -540,38 +549,26 @@ public class TestSessionData extends ProviderSuper {
@Test
public void testTrackDeploy() throws PfModelException {
- testTrack(session::getDeployData, session::getUndeployData, session::trackDeploy);
- }
-
- /**
- * Tests trackDeploy() when there is something in the undeployed list.
- *
- * @throws PfModelException if an error occurs
- */
- @Test
- public void testTrackDeployRemoveUndeploy() throws PfModelException {
- testTrack(session::getDeployData, session::getUndeployData, session::trackUndeploy, session::trackDeploy);
+ testTrack(true);
}
@Test
public void testTrackUndeploy() throws PfModelException {
- testTrack(session::getUndeployData, session::getDeployData, session::trackUndeploy);
+ testTrack(false);
}
- /**
- * Tests trackUndeploy() when there is something in the deployed list.
- *
- * @throws PfModelException if an error occurs
- */
- @Test
- public void testTrackUndeployRemoveUndeploy() throws PfModelException {
- testTrack(session::getUndeployData, session::getDeployData, session::trackDeploy, session::trackUndeploy);
- }
-
- protected void testTrack(Supplier<Collection<PolicyPdpNotificationData>> expected,
- Supplier<Collection<PolicyPdpNotificationData>> unexpected, TrackEx... trackFuncs)
+ protected void testTrack(boolean deploy)
throws PfModelException {
+ DeploymentStatus status = mock(DeploymentStatus.class);
+
+ session = new SessionData(dao) {
+ @Override
+ protected DeploymentStatus makeDeploymentStatus(PolicyModelsProvider dao) {
+ return status;
+ }
+ };
+
ToscaPolicy policy = makePolicy(POLICY_NAME, POLICY_VERSION);
policy.setType(POLICY_TYPE);
policy.setTypeVersion(POLICY_TYPE_VERSION);
@@ -581,25 +578,20 @@ public class TestSessionData extends ProviderSuper {
ToscaConceptIdentifier policyId = new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION);
List<String> pdps = Arrays.asList(PDP1, PDP2);
- for (TrackEx trackFunc : trackFuncs) {
- trackFunc.accept(policyId, pdps);
+ if (deploy) {
+ session.trackDeploy(policyId, pdps, GROUP_NAME, PDP_TYPE);
+ } else {
+ session.trackUndeploy(policyId, pdps, GROUP_NAME, PDP_TYPE);
}
- // "unexpected" list should be empty of any PDPs
- Collection<PolicyPdpNotificationData> dataList = unexpected.get();
- assertTrue(dataList.size() <= 1);
- if (!dataList.isEmpty()) {
- PolicyPdpNotificationData data = dataList.iterator().next();
- assertTrue(data.getPdps().isEmpty());
- }
-
- dataList = expected.get();
- assertEquals(1, dataList.size());
+ // should be called just once
+ verify(status).deleteDeployment(any(), anyBoolean());
+ verify(status).deleteDeployment(policyId, !deploy);
- PolicyPdpNotificationData data = dataList.iterator().next();
- assertEquals(policyId, data.getPolicyId());
- assertEquals(type, data.getPolicyType());
- assertEquals("[pdp_1, pdp_2]", new TreeSet<>(data.getPdps()).toString());
+ // should be called for each PDP
+ verify(status, times(2)).deploy(any(), any(), any(), any(), any(), anyBoolean());
+ verify(status).deploy(PDP1, policyId, policy.getTypeIdentifier(), GROUP_NAME, PDP_TYPE, deploy);
+ verify(status).deploy(PDP2, policyId, policy.getTypeIdentifier(), GROUP_NAME, PDP_TYPE, deploy);
}
private PdpUpdate makeUpdate(String pdpName) {
@@ -655,9 +647,4 @@ public class TestSessionData extends ProviderSuper {
private String getName(Pair<PdpUpdate, PdpStateChange> pair) {
return (pair.getKey() != null ? pair.getKey().getName() : pair.getValue().getName());
}
-
- @FunctionalInterface
- private static interface TrackEx {
- public void accept(ToscaConceptIdentifier policyId, Collection<String> pdps) throws PfModelException;
- }
}
diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/e2e/End2EndBase.java b/main/src/test/java/org/onap/policy/pap/main/rest/e2e/End2EndBase.java
index 3f4015b1..1ecea074 100644
--- a/main/src/test/java/org/onap/policy/pap/main/rest/e2e/End2EndBase.java
+++ b/main/src/test/java/org/onap/policy/pap/main/rest/e2e/End2EndBase.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP PAP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2019-2020 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -37,6 +37,7 @@ import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.pdp.concepts.PdpGroup;
import org.onap.policy.models.pdp.concepts.PdpGroups;
+import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
import org.onap.policy.models.pdp.concepts.PdpStatistics;
import org.onap.policy.models.provider.PolicyModelsProvider;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
@@ -77,17 +78,7 @@ public class End2EndBase extends CommonPapRestServer {
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
- setUpBeforeClass(true);
- }
-
- /**
- * Starts Main, if specified, and connects to the DB.
- *
- * @param shouldStart {@code true} if Main should be started, {@code false} otherwise
- * @throws Exception if an error occurs
- */
- public static void setUpBeforeClass(final boolean shouldStart) throws Exception {
- CommonPapRestServer.setUpBeforeClass(shouldStart);
+ CommonPapRestServer.setUpBeforeClass(true);
final PapParameterGroup params = new StandardCoder().decode(new File(CONFIG_FILE), PapParameterGroup.class);
daoFactory = new PolicyModelsProviderFactoryWrapper(params.getDatabaseProviderParameters());
@@ -195,6 +186,17 @@ public class End2EndBase extends CommonPapRestServer {
}
/**
+ * Adds PdpPolicyStatus records to the DB.
+ *
+ * @param jsonFile name of the JSON file specifying the data to be loaded
+ * @throws PfModelException if a DAO error occurs
+ */
+ public static void addPdpPolicyStatus(final String jsonFile) throws PfModelException {
+ final PolicyStatusRecords data = loadJsonFile(jsonFile, PolicyStatusRecords.class);
+ dbConn.cudPolicyStatus(data.records, null, null);
+ }
+
+ /**
* Loads an object from a YAML file.
*
* @param fileName name of the file from which to load
@@ -246,4 +248,8 @@ public class End2EndBase extends CommonPapRestServer {
throw new RuntimeException(e);
}
}
+
+ public class PolicyStatusRecords {
+ private List<PdpPolicyStatus> records;
+ }
}
diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PolicyStatusTest.java b/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PolicyStatusTest.java
index afabb892..19a5f990 100644
--- a/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PolicyStatusTest.java
+++ b/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PolicyStatusTest.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP PAP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2021 AT&T Intellectual Property. 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.
@@ -40,14 +40,8 @@ public class PolicyStatusTest extends End2EndBase {
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
- // don't start Main until AFTER we add the policies to the DB
- End2EndBase.setUpBeforeClass(false);
-
- addToscaPolicyTypes("monitoring.policy-type.yaml");
- addToscaPolicies("monitoring.policy.yaml");
- addGroups("policyStatus.json");
-
- startMain();
+ End2EndBase.setUpBeforeClass();
+ addPdpPolicyStatus("policyStatus.json");
}
@Test
diff --git a/main/src/test/resources/META-INF/persistence.xml b/main/src/test/resources/META-INF/persistence.xml
index 47ee9fd8..e49dafa4 100644
--- a/main/src/test/resources/META-INF/persistence.xml
+++ b/main/src/test/resources/META-INF/persistence.xml
@@ -2,6 +2,7 @@
<!--
============LICENSE_START=======================================================
Copyright (C) 2019-2020 Nordix Foundation.
+ Modifications Copyright (C) 2021 AT&T Intellectual Property.
================================================================================
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -28,6 +29,7 @@
<class>org.onap.policy.models.dao.converters.Uuid2String</class>
<class>org.onap.policy.models.pdp.persistence.concepts.JpaPdp</class>
<class>org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup</class>
+ <class>org.onap.policy.models.pdp.persistence.concepts.JpaPdpPolicyStatus</class>
<class>org.onap.policy.models.pdp.persistence.concepts.JpaPdpStatistics</class>
<class>org.onap.policy.models.pdp.persistence.concepts.JpaPdpSubGroup</class>
<class>org.onap.policy.models.tosca.simple.concepts.JpaToscaCapabilityAssignment</class>
diff --git a/main/src/test/resources/e2e/policyStatus.json b/main/src/test/resources/e2e/policyStatus.json
index 973e2c0e..185f332c 100644
--- a/main/src/test/resources/e2e/policyStatus.json
+++ b/main/src/test/resources/e2e/policyStatus.json
@@ -1,56 +1,19 @@
{
- "groups": [
+ "records": [
{
- "name": "policyStatus",
- "pdpGroupState": "ACTIVE",
- "pdpSubgroups": [
- {
- "pdpType": "pdpTypeA",
- "desiredInstanceCount": 2,
- "pdpInstances": [
- {
- "instanceId": "pdpA_1",
- "pdpState": "ACTIVE",
- "healthy": "HEALTHY"
- },
- {
- "instanceId": "pdpA_2",
- "pdpState": "ACTIVE",
- "healthy": "HEALTHY"
- }
- ],
- "supportedPolicyTypes": [
- {
- "name": "onap.policies.monitoring.cdap.tca.hi.lo.app",
- "version": "1.0.0"
- }
- ],
- "policies": []
- },
- {
- "pdpType": "pdpTypeB",
- "desiredInstanceCount": 1,
- "pdpInstances": [
- {
- "instanceId": "pdpB_1",
- "pdpState": "ACTIVE",
- "healthy": "HEALTHY"
- }
- ],
- "supportedPolicyTypes": [
- {
- "name": "onap.policies.monitoring.cdap.tca.hi.lo.app",
- "version": "1.0.0"
- }
- ],
- "policies": [
- {
- "name": "onap.restart.tca",
- "version": "1.0.0"
- }
- ]
- }
- ]
+ "pdpGroup": "policyStatus",
+ "pdpType": "pdpTypeB",
+ "pdpId": "pdpB_1",
+ "policy": {
+ "name": "onap.restart.tca",
+ "version": "1.0.0"
+ },
+ "policyType": {
+ "name": "onap.policies.monitoring.cdap.tca.hi.lo.app",
+ "version": "1.0.0"
+ },
+ "deploy": true,
+ "state": "WAITING"
}
]
}