2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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 all of the sets within the data are empty (i.e., contain no PDPs).
81 * @return {@code true} if the data is completely empty, {@code false} otherwise
83 public boolean isEmpty() {
84 return (successPdps.isEmpty() && failPdps.isEmpty() && incompletePdps.isEmpty());
88 * Puts the values from this data into the given status object.
90 * @param status object whose values are to be set
92 public void putValuesInto(PolicyStatus status) {
93 status.setFailureCount(failPdps.size());
94 status.setIncompleteCount(incompletePdps.size());
95 status.setSuccessCount(successPdps.size());
99 * Adds PDPs to {@link #incompletePdps}, removing them from any other sets in which
102 * @param pdps PDPs to be added
104 public void addPdps(Collection<String> pdps) {
105 successPdps.removeAll(pdps);
106 failPdps.removeAll(pdps);
108 incompletePdps.addAll(pdps);
112 * Removes PDPs from the sets.
114 * @param pdps PDPs to be removed
115 * @return {@code true} if the policy is now complete, {@code false} otherwise
117 public boolean removePdps(Collection<String> pdps) {
118 successPdps.removeAll(pdps);
119 failPdps.removeAll(pdps);
121 return (incompletePdps.removeAll(pdps) && incompletePdps.isEmpty());
125 * Removes a PDP from all sets.
127 * @param pdp PDP to be removed
128 * @return {@code true} if the policy is now complete, {@code false} otherwise
130 public boolean removePdp(String pdp) {
131 successPdps.remove(pdp);
132 failPdps.remove(pdp);
134 return (incompletePdps.remove(pdp) && incompletePdps.isEmpty());
138 * Indicates that a PDP has successfully processed this policy.
140 * @param pdp the PDP of interest
141 * @return {@code true} if the policy is now complete, {@code false} otherwise
143 public boolean success(String pdp) {
144 return complete(pdp, successPdps, failPdps);
148 * Indicates that a PDP has processed this policy, but was unsuccessful.
150 * @param pdp the PDP of interest
151 * @return {@code true} if the policy is now complete, {@code false} otherwise
153 public boolean fail(String pdp) {
154 return complete(pdp, failPdps, successPdps);
158 * Indicates that a PDP has processed this policy, either successfully or
161 * @param pdp the PDP of interest
162 * @param addSet set to which the PDP should be added
163 * @param removeSet set from which the PDP should be removed
164 * @return {@code true} if the policy is now complete, {@code false} otherwise
166 private boolean complete(String pdp, Set<String> addSet, Set<String> removeSet) {
167 if (incompletePdps.remove(pdp) || removeSet.remove(pdp)) {
168 // successfully removed from one of the sets
170 return incompletePdps.isEmpty();
174 * Else: wasn't in either set, thus it's already in the "addSet" or it isn't
175 * relevant to this policy. Either way, just discard it without triggering any new