[POLICY-73] replace openecomp for policy-engine
[policy/engine.git] / PolicyEngineUtils / src / main / java / org / onap / policy / std / NotificationStore.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineUtils
4  * ================================================================================
5  * Copyright (C) 2017 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.std;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.HashSet;
26 import java.util.Iterator;
27
28 import org.onap.policy.api.LoadedPolicy;
29 import org.onap.policy.api.NotificationType;
30 import org.onap.policy.api.PDPNotification;
31 import org.onap.policy.api.RemovedPolicy;
32
33
34 /*
35  * This Should Compare and save the Notifications from the beginning of Time. 
36  * If there is something new (missed) We notify the client. 
37  * Works for PDP failures. 
38  * 
39  */
40 public class NotificationStore {
41         private static StdPDPNotification notificationRecord = new StdPDPNotification();
42         
43         public static StdPDPNotification getDeltaNotification(StdPDPNotification newNotification){
44                 StdPDPNotification notificationDelta = new StdPDPNotification();
45                 ArrayList<StdRemovedPolicy> removedDelta = new ArrayList<>();
46                 ArrayList<StdLoadedPolicy> updatedDelta = new ArrayList<>();
47                 Collection<StdLoadedPolicy> newUpdatedPolicies = new ArrayList<>();
48                 Collection<StdRemovedPolicy> newRemovedPolicies = new ArrayList<>();
49                 Collection<LoadedPolicy> oldUpdatedLostPolicies = notificationRecord.getLoadedPolicies();
50                 Collection<RemovedPolicy> oldRemovedPolicies = notificationRecord.getRemovedPolicies();
51                 Collection<LoadedPolicy> oldUpdatedPolicies = notificationRecord.getLoadedPolicies();
52                 Boolean update = false;
53                 Boolean remove = false;
54                 // if the NotificationRecord is empty
55                 if(notificationRecord.getRemovedPolicies()==null || notificationRecord.getLoadedPolicies()==null){
56                         if(newNotification!=null){
57                                 notificationRecord = newNotification;
58                         }
59                         return notificationDelta;
60                 }
61                 // do the Delta operation. 
62                 if(newNotification!=null){
63                         // check for old removed policies.
64                         if(!newNotification.getRemovedPolicies().isEmpty()){
65                                 for(RemovedPolicy newRemovedPolicy: newNotification.getRemovedPolicies()){
66                                         //Look for policy Not in Remove
67                                         Boolean removed = true;
68                                         for(RemovedPolicy oldRemovedPolicy: notificationRecord.getRemovedPolicies()){
69                                                 if(newRemovedPolicy.getPolicyName().equals(oldRemovedPolicy.getPolicyName())){
70                                                         if(newRemovedPolicy.getVersionNo().equals(oldRemovedPolicy.getVersionNo())){
71                                                                 removed = false;
72                                                                 // Don't want a duplicate. 
73                                                                 oldRemovedPolicies.remove(oldRemovedPolicy);
74                                                         }
75                                                 }
76                                         }
77                                         //We need to change our record we have an Update record of this remove.  
78                                         for(LoadedPolicy oldUpdatedPolicy: notificationRecord.getLoadedPolicies()){
79                                                 if(newRemovedPolicy.getPolicyName().equals(oldUpdatedPolicy.getPolicyName())){
80                                                         if(newRemovedPolicy.getVersionNo().equals(oldUpdatedPolicy.getVersionNo())){
81                                                                 oldUpdatedPolicies.remove(oldUpdatedPolicy);
82                                                                 oldUpdatedLostPolicies.remove(oldUpdatedPolicy);
83                                                         }
84                                                 }
85                                         }
86                                         if(removed){
87                                                 remove = true;
88                                                 notificationRecord.getRemovedPolicies().add(newRemovedPolicy);
89                                                 removedDelta.add((StdRemovedPolicy)newRemovedPolicy);
90                                         }
91                                         // This will be converted to New Later. 
92                                         oldRemovedPolicies.add(newRemovedPolicy);
93                                 }
94                         }
95                         // Check for old Updated Policies. 
96                         if(!newNotification.getLoadedPolicies().isEmpty()){
97                                 for(LoadedPolicy newUpdatedPolicy: newNotification.getLoadedPolicies()){
98                                         // Look for policies which are not in Update
99                                         Boolean updated = true;
100                                         for(LoadedPolicy oldUpdatedPolicy: notificationRecord.getLoadedPolicies()){
101                                                 if(newUpdatedPolicy.getPolicyName().equals(oldUpdatedPolicy.getPolicyName())){
102                                                         if(newUpdatedPolicy.getVersionNo().equals(oldUpdatedPolicy.getVersionNo())){
103                                                                 updated = false;
104                                                                 // Remove the policy from copy. 
105                                                                 oldUpdatedLostPolicies.remove(oldUpdatedPolicy);
106                                                                 // Eliminating Duplicate. 
107                                                                 oldUpdatedPolicies.remove(oldUpdatedPolicy);
108                                                         }
109                                                 }
110                                         }
111                                         // Change the record if the policy has been Removed earlier. 
112                                         for(RemovedPolicy oldRemovedPolicy: notificationRecord.getRemovedPolicies()){
113                                                 if(oldRemovedPolicy.getPolicyName().equals(newUpdatedPolicy.getPolicyName())){
114                                                         if(oldRemovedPolicy.getVersionNo().equals(newUpdatedPolicy.getVersionNo())){
115                                                                 oldRemovedPolicies.remove(oldRemovedPolicy);
116                                                         }
117                                                 }
118                                         }
119                                         if(updated){
120                                                 update = true;
121                                                 updatedDelta.add((StdLoadedPolicy)newUpdatedPolicy);
122                                         }
123                                         // This will be converted to new Later
124                                         oldUpdatedPolicies.add(newUpdatedPolicy);
125                                 }
126                                 // Conversion of Update to Remove if that occurred.
127                                 if(!oldUpdatedLostPolicies.isEmpty()){
128                                         for(LoadedPolicy updatedPolicy: oldUpdatedLostPolicies){
129                                                 StdRemovedPolicy removedPolicy = new StdRemovedPolicy();
130                                                 removedPolicy.setPolicyName(updatedPolicy.getPolicyName());
131                                                 removedPolicy.setVersionNo(updatedPolicy.getVersionNo());
132                                                 removedDelta.add(removedPolicy);
133                                                 remove = true;
134                                         }
135                                 }
136                         }
137                         // Update our Record. 
138                         if(!oldUpdatedPolicies.isEmpty()){
139                                 for(LoadedPolicy updatedPolicy: oldUpdatedPolicies){
140                                         newUpdatedPolicies.add((StdLoadedPolicy)updatedPolicy);
141                                 }
142                         }
143                         if(!oldRemovedPolicies.isEmpty()){
144                                 for(RemovedPolicy removedPolicy: oldRemovedPolicies){
145                                         newRemovedPolicies.add((StdRemovedPolicy)removedPolicy);
146                                 }
147                         }
148                         notificationRecord.setRemovedPolicies(newRemovedPolicies);
149                         notificationRecord.setLoadedPolicies(newUpdatedPolicies);
150                         // Update the notification Result. 
151                         notificationDelta.setRemovedPolicies(removedDelta);
152                         notificationDelta.setLoadedPolicies(updatedDelta);
153                         if(remove&&update){
154                                 notificationDelta.setNotificationType(NotificationType.BOTH);
155                         }else if(remove){
156                                 notificationDelta.setNotificationType(NotificationType.REMOVE);
157                         }else if(update){
158                                 notificationDelta.setNotificationType(NotificationType.UPDATE);
159                         }
160                 }
161                 return notificationDelta;
162         }
163         
164         public static void recordNotification(StdPDPNotification notification){
165                 if(notification!=null){
166                         if(notificationRecord.getRemovedPolicies()==null || notificationRecord.getLoadedPolicies()==null){
167                                 notificationRecord = notification;
168                         }else{
169                                 // Check if there is anything new and update the record. 
170                                 if(notificationRecord.getLoadedPolicies()!=null || notificationRecord.getRemovedPolicies()!=null){
171                                         HashSet<StdRemovedPolicy> removedPolicies = new HashSet<>();
172                                         for(RemovedPolicy rPolicy: notificationRecord.getRemovedPolicies()){
173                                                 StdRemovedPolicy sRPolicy = new StdRemovedPolicy();
174                                                 sRPolicy.setPolicyName(rPolicy.getPolicyName());
175                                                 sRPolicy.setVersionNo(rPolicy.getVersionNo());
176                                                 removedPolicies.add(sRPolicy);
177                                         }
178                                         HashSet<StdLoadedPolicy> updatedPolicies = new HashSet<>();
179                                         for(LoadedPolicy uPolicy: notificationRecord.getLoadedPolicies()){
180                                                 StdLoadedPolicy sUPolicy = new StdLoadedPolicy();
181                                                 sUPolicy.setMatches(uPolicy.getMatches());
182                                                 sUPolicy.setPolicyName(uPolicy.getPolicyName());
183                                                 sUPolicy.setVersionNo(uPolicy.getVersionNo());
184                                                 updatedPolicies.add(sUPolicy);
185                                         }
186                                         
187                                         // Checking with the new updated policies.
188                                         if(notification.getLoadedPolicies()!=null && !notification.getLoadedPolicies().isEmpty()){
189                                                 for(LoadedPolicy newUpdatedPolicy: notification.getLoadedPolicies()){
190                                                         // If it was removed earlier then we need to remove from our record
191                                                         Iterator<StdRemovedPolicy> oldRemovedPolicy = removedPolicies.iterator();
192                                                         while(oldRemovedPolicy.hasNext()){
193                                                                 RemovedPolicy policy = oldRemovedPolicy.next(); 
194                                                                 if(newUpdatedPolicy.getPolicyName().equals(policy.getPolicyName())) {
195                                                                         if(newUpdatedPolicy.getVersionNo().equals(policy.getVersionNo())) {
196                                                                                 oldRemovedPolicy.remove();
197                                                                         }
198                                                                 }
199                                                         }
200                                                         // If it was previously updated need to Overwrite it to the record. 
201                                                         Iterator<StdLoadedPolicy> oldUpdatedPolicy = updatedPolicies.iterator();
202                                                         while(oldUpdatedPolicy.hasNext()){
203                                                                 LoadedPolicy policy = oldUpdatedPolicy.next();
204                                                                 if(newUpdatedPolicy.getPolicyName().equals(policy.getPolicyName())) {
205                                                                         if(newUpdatedPolicy.getVersionNo().equals(policy.getVersionNo())) {
206                                                                                 oldUpdatedPolicy.remove();
207                                                                         }
208                                                                 }
209                                                         }
210                                                         StdLoadedPolicy sUPolicy = new StdLoadedPolicy();
211                                                         sUPolicy.setMatches(newUpdatedPolicy.getMatches());
212                                                         sUPolicy.setPolicyName(newUpdatedPolicy.getPolicyName());
213                                                         sUPolicy.setVersionNo(newUpdatedPolicy.getVersionNo());
214                                                         updatedPolicies.add(sUPolicy);
215                                                 }
216                                         }
217                                         // Checking with New Removed Policies.
218                                         if(notification.getRemovedPolicies()!=null && !notification.getRemovedPolicies().isEmpty()){
219                                                 for(RemovedPolicy newRemovedPolicy : notification.getRemovedPolicies()){
220                                                         // If it was previously removed Overwrite it to the record. 
221                                                         Iterator<StdRemovedPolicy> oldRemovedPolicy = removedPolicies.iterator();
222                                                         while(oldRemovedPolicy.hasNext()){
223                                                                 RemovedPolicy policy = oldRemovedPolicy.next(); 
224                                                                 if(newRemovedPolicy.getPolicyName().equals(policy.getPolicyName())) {
225                                                                         if(newRemovedPolicy.getVersionNo().equals(policy.getVersionNo())) {
226                                                                                 oldRemovedPolicy.remove();
227                                                                         }
228                                                                 }
229                                                         }
230                                                         // If it was added earlier then we need to remove from our record. 
231                                                         Iterator<StdLoadedPolicy> oldUpdatedPolicy = updatedPolicies.iterator();
232                                                         while(oldUpdatedPolicy.hasNext()){
233                                                                 LoadedPolicy policy = oldUpdatedPolicy.next();
234                                                                 if(newRemovedPolicy.getPolicyName().equals(policy.getPolicyName())) {
235                                                                         if(newRemovedPolicy.getVersionNo().equals(policy.getVersionNo())) {
236                                                                                 oldUpdatedPolicy.remove();
237                                                                         }
238                                                                 }
239                                                         }
240                                                         StdRemovedPolicy sRPolicy = new StdRemovedPolicy();
241                                                         sRPolicy.setPolicyName(newRemovedPolicy.getPolicyName());
242                                                         sRPolicy.setVersionNo(newRemovedPolicy.getVersionNo());
243                                                         removedPolicies.add(sRPolicy);
244                                                 }
245                                         }
246                                         notificationRecord.setRemovedPolicies(removedPolicies);
247                                         notificationRecord.setLoadedPolicies(updatedPolicies);
248                                 }
249                                 if(!notificationRecord.getLoadedPolicies().isEmpty() && !notificationRecord.getRemovedPolicies().isEmpty()){
250                                         notificationRecord.setNotificationType(NotificationType.BOTH);
251                                 }else if(!notificationRecord.getLoadedPolicies().isEmpty()){
252                                         notificationRecord.setNotificationType(NotificationType.UPDATE);
253                                 }else if(!notificationRecord.getRemovedPolicies().isEmpty()){
254                                         notificationRecord.setNotificationType(NotificationType.REMOVE);
255                                 }
256                         }
257                 }
258         }
259         
260         // This should return the current Notification Record. 
261         public static PDPNotification getNotificationRecord(){
262                 return notificationRecord;
263         }
264 }