530e107964a78bb3f6ce340e840035f24cf4af61
[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 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 all of the sets within the data are empty (i.e., contain no PDPs).
80      *
81      * @return {@code true} if the data is completely empty, {@code false} otherwise
82      */
83     public boolean isEmpty() {
84         return (successPdps.isEmpty() && failPdps.isEmpty() && incompletePdps.isEmpty());
85     }
86
87     /**
88      * Puts the values from this data into the given status object.
89      *
90      * @param status object whose values are to be set
91      */
92     public void putValuesInto(PolicyStatus status) {
93         status.setFailureCount(failPdps.size());
94         status.setIncompleteCount(incompletePdps.size());
95         status.setSuccessCount(successPdps.size());
96     }
97
98     /**
99      * Adds PDPs to {@link #incompletePdps}, removing them from any other sets in which
100      * they appear.
101      *
102      * @param pdps PDPs to be added
103      */
104     public void addPdps(Collection<String> pdps) {
105         successPdps.removeAll(pdps);
106         failPdps.removeAll(pdps);
107
108         incompletePdps.addAll(pdps);
109     }
110
111     /**
112      * Removes PDPs from the sets.
113      *
114      * @param pdps PDPs to be removed
115      * @return {@code true} if the policy is now complete, {@code false} otherwise
116      */
117     public boolean removePdps(Collection<String> pdps) {
118         successPdps.removeAll(pdps);
119         failPdps.removeAll(pdps);
120
121         return (incompletePdps.removeAll(pdps) && incompletePdps.isEmpty());
122     }
123
124     /**
125      * Removes a PDP from all sets.
126      *
127      * @param pdp PDP to be removed
128      * @return {@code true} if the policy is now complete, {@code false} otherwise
129      */
130     public boolean removePdp(String pdp) {
131         successPdps.remove(pdp);
132         failPdps.remove(pdp);
133
134         return (incompletePdps.remove(pdp) && incompletePdps.isEmpty());
135     }
136
137     /**
138      * Indicates that a PDP has successfully processed this policy.
139      *
140      * @param pdp the PDP of interest
141      * @return {@code true} if the policy is now complete, {@code false} otherwise
142      */
143     public boolean success(String pdp) {
144         return complete(pdp, successPdps, failPdps);
145     }
146
147     /**
148      * Indicates that a PDP has processed this policy, but was unsuccessful.
149      *
150      * @param pdp the PDP of interest
151      * @return {@code true} if the policy is now complete, {@code false} otherwise
152      */
153     public boolean fail(String pdp) {
154         return complete(pdp, failPdps, successPdps);
155     }
156
157     /**
158      * Indicates that a PDP has processed this policy, either successfully or
159      * unsuccessfully.
160      *
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
165      */
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
169             addSet.add(pdp);
170             return incompletePdps.isEmpty();
171         }
172
173         /*
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
176          * notification.
177          */
178         return false;
179     }
180 }