2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.main.notification;
23 import java.util.Collection;
24 import java.util.HashSet;
27 import org.onap.policy.models.pap.concepts.PolicyStatus;
28 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
31 * Data associated with a policy, used by PolicyTracker. PDPs start in
32 * {@link #incompletePdps} and are moved to either {@link #successPdps} or
33 * {@link #failPdps}, depending on their response. Objects of this type are not
36 public class PolicyTrackerData {
38 * The policy type associated with the policy.
41 private final ToscaPolicyTypeIdentifier policyType;
44 * PDPs that have successfully completed an update of the policy.
46 private final Set<String> successPdps = new HashSet<>();
49 * PDPs that have failed to complete an update of the policy.
51 private final Set<String> failPdps = new HashSet<>();
54 * PDPs for which we're still awaiting a response.
56 private final Set<String> incompletePdps = new HashSet<>();
60 * Constructs the object.
62 * @param policyType policy type
64 public PolicyTrackerData(ToscaPolicyTypeIdentifier policyType) {
65 this.policyType = policyType;
69 * Determines if this is complete (i.e., it is not waiting for responses from any
72 * @return {@code true} if this is complete, {@code false} otherwise
74 public boolean isComplete() {
75 return incompletePdps.isEmpty();
79 * Determines if everything has succeeded.
81 * @return {@code true} if this is complete <i>and</i> nothing has failed,
82 * {@code false} otherwise
84 public boolean allSucceeded() {
85 return (failPdps.isEmpty() && incompletePdps.isEmpty());
89 * Determines if all of the sets within the data are empty (i.e., contain no PDPs).
91 * @return {@code true} if the data is completely empty, {@code false} otherwise
93 public boolean isEmpty() {
94 return (successPdps.isEmpty() && failPdps.isEmpty() && incompletePdps.isEmpty());
98 * Puts the values from this data into the given status object.
100 * @param status object whose values are to be set
102 public void putValuesInto(PolicyStatus status) {
103 status.setFailureCount(failPdps.size());
104 status.setIncompleteCount(incompletePdps.size());
105 status.setSuccessCount(successPdps.size());
109 * Adds PDPs to {@link #incompletePdps}, removing them from any other sets in which
112 * @param pdps PDPs to be added
114 public void addPdps(Collection<String> pdps) {
115 successPdps.removeAll(pdps);
116 failPdps.removeAll(pdps);
118 incompletePdps.addAll(pdps);
122 * Removes PDPs from the sets.
124 * @param pdps PDPs to be removed
125 * @return {@code true} if anything changed and the policy is now complete, {@code false} otherwise
127 public boolean removePdps(Collection<String> pdps) {
128 boolean changed = successPdps.removeAll(pdps);
129 changed = failPdps.removeAll(pdps) || changed;
130 changed = incompletePdps.removeAll(pdps) || changed;
132 return (changed && incompletePdps.isEmpty());
136 * Removes a PDP from all sets.
138 * @param pdp PDP to be removed
139 * @return {@code true} if anything changed and the policy is now complete, {@code false} otherwise
141 public boolean removePdp(String pdp) {
142 boolean changed = successPdps.remove(pdp);
143 changed = failPdps.remove(pdp) || changed;
144 changed = incompletePdps.remove(pdp) || changed;
146 return (changed && incompletePdps.isEmpty());
150 * Indicates that a PDP has successfully processed this policy.
152 * @param pdp the PDP of interest
153 * @return {@code true} if the policy is now complete, {@code false} otherwise
155 public boolean success(String pdp) {
156 return complete(pdp, successPdps, failPdps);
160 * Indicates that a PDP has processed this policy, but was unsuccessful.
162 * @param pdp the PDP of interest
163 * @return {@code true} if the policy is now complete, {@code false} otherwise
165 public boolean fail(String pdp) {
166 return complete(pdp, failPdps, successPdps);
170 * Indicates that a PDP has processed this policy, either successfully or
173 * @param pdp the PDP of interest
174 * @param addSet set to which the PDP should be added
175 * @param removeSet set from which the PDP should be removed
176 * @return {@code true} if the policy is now complete, {@code false} otherwise
178 private boolean complete(String pdp, Set<String> addSet, Set<String> removeSet) {
179 if (incompletePdps.remove(pdp) || removeSet.remove(pdp)) {
180 // successfully removed from one of the sets
182 return incompletePdps.isEmpty();
186 * Else: wasn't in either set, thus it's already in the "addSet" or it isn't
187 * relevant to this policy. Either way, just discard it without triggering any new