Policy notifications appear to be reversed
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / notification / PolicyTrackerData.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.pap.main.notification;
22
23 import java.util.Collection;
24 import java.util.HashSet;
25 import java.util.Set;
26 import lombok.Getter;
27 import org.onap.policy.models.pap.concepts.PolicyStatus;
28 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
29
30 /**
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
34  * multi-thread safe.
35  */
36 public class PolicyTrackerData {
37     /**
38      * The policy type associated with the policy.
39      */
40     @Getter
41     private final ToscaPolicyTypeIdentifier policyType;
42
43     /**
44      * PDPs that have successfully completed an update of the policy.
45      */
46     private final Set<String> successPdps = new HashSet<>();
47
48     /**
49      * PDPs that have failed to complete an update of the policy.
50      */
51     private final Set<String> failPdps = new HashSet<>();
52
53     /**
54      * PDPs for which we're still awaiting a response.
55      */
56     private final Set<String> incompletePdps = new HashSet<>();
57
58
59     /**
60      * Constructs the object.
61      *
62      * @param policyType policy type
63      */
64     public PolicyTrackerData(ToscaPolicyTypeIdentifier policyType) {
65         this.policyType = policyType;
66     }
67
68     /**
69      * Determines if this is complete (i.e., it is not waiting for responses from any
70      * other PDPs).
71      *
72      * @return {@code true} if this is complete, {@code false} otherwise
73      */
74     public boolean isComplete() {
75         return incompletePdps.isEmpty();
76     }
77
78     /**
79      * Determines if everything has succeeded.
80      *
81      * @return {@code true} if this is complete <i>and</i> nothing has failed,
82      *         {@code false} otherwise
83      */
84     public boolean allSucceeded() {
85         return (failPdps.isEmpty() && incompletePdps.isEmpty());
86     }
87
88     /**
89      * Determines if all of the sets within the data are empty (i.e., contain no PDPs).
90      *
91      * @return {@code true} if the data is completely empty, {@code false} otherwise
92      */
93     public boolean isEmpty() {
94         return (successPdps.isEmpty() && failPdps.isEmpty() && incompletePdps.isEmpty());
95     }
96
97     /**
98      * Puts the values from this data into the given status object.
99      *
100      * @param status object whose values are to be set
101      */
102     public void putValuesInto(PolicyStatus status) {
103         status.setFailureCount(failPdps.size());
104         status.setIncompleteCount(incompletePdps.size());
105         status.setSuccessCount(successPdps.size());
106     }
107
108     /**
109      * Adds PDPs to {@link #incompletePdps}, removing them from any other sets in which
110      * they appear.
111      *
112      * @param pdps PDPs to be added
113      */
114     public void addPdps(Collection<String> pdps) {
115         successPdps.removeAll(pdps);
116         failPdps.removeAll(pdps);
117
118         incompletePdps.addAll(pdps);
119     }
120
121     /**
122      * Removes PDPs from the sets.
123      *
124      * @param pdps PDPs to be removed
125      * @return {@code true} if anything changed and the policy is now complete, {@code false} otherwise
126      */
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;
131
132         return (changed && incompletePdps.isEmpty());
133     }
134
135     /**
136      * Removes a PDP from all sets.
137      *
138      * @param pdp PDP to be removed
139      * @return {@code true} if anything changed and the policy is now complete, {@code false} otherwise
140      */
141     public boolean removePdp(String pdp) {
142         boolean changed = successPdps.remove(pdp);
143         changed = failPdps.remove(pdp) || changed;
144         changed = incompletePdps.remove(pdp) || changed;
145
146         return (changed && incompletePdps.isEmpty());
147     }
148
149     /**
150      * Indicates that a PDP has successfully processed this policy.
151      *
152      * @param pdp the PDP of interest
153      * @return {@code true} if the policy is now complete, {@code false} otherwise
154      */
155     public boolean success(String pdp) {
156         return complete(pdp, successPdps, failPdps);
157     }
158
159     /**
160      * Indicates that a PDP has processed this policy, but was unsuccessful.
161      *
162      * @param pdp the PDP of interest
163      * @return {@code true} if the policy is now complete, {@code false} otherwise
164      */
165     public boolean fail(String pdp) {
166         return complete(pdp, failPdps, successPdps);
167     }
168
169     /**
170      * Indicates that a PDP has processed this policy, either successfully or
171      * unsuccessfully.
172      *
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
177      */
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
181             addSet.add(pdp);
182             return incompletePdps.isEmpty();
183         }
184
185         /*
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
188          * notification.
189          */
190         return false;
191     }
192 }