Merge "Fixed a bug on view and editor screens"
[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 import org.onap.policy.api.LoadedPolicy;
28 import org.onap.policy.api.NotificationType;
29 import org.onap.policy.api.PDPNotification;
30 import org.onap.policy.api.RemovedPolicy;
31
32
33 /*
34  * This Should Compare and save the Notifications from the beginning of Time. 
35  * If there is something new (missed) We notify the client. 
36  * Works for PDP failures. 
37  * 
38  */
39 public class NotificationStore {
40
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                     String policyName = newRemovedPolicy.getPolicyName();
69                     String ver = newRemovedPolicy.getVersionNo();
70                     for (RemovedPolicy oldRemovedPolicy : notificationRecord.getRemovedPolicies()) {
71                         if (policyName.equals(oldRemovedPolicy.getPolicyName())
72                             && ver.equals(oldRemovedPolicy.getVersionNo())) {
73                             removed = false;
74                             // Don't want a duplicate.
75                             oldRemovedPolicies.remove(oldRemovedPolicy);
76                         }
77                     }
78                     //We need to change our record we have an Update record of this remove.
79                     for (LoadedPolicy oldUpdatedPolicy : notificationRecord.getLoadedPolicies()) {
80                         if (policyName.equals(oldUpdatedPolicy.getPolicyName())
81                             && ver.equals(oldUpdatedPolicy.getVersionNo())) {
82                             oldUpdatedPolicies.remove(oldUpdatedPolicy);
83                             oldUpdatedLostPolicies.remove(oldUpdatedPolicy);
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                     String policyName = newUpdatedPolicy.getPolicyName();
101                     String ver = newUpdatedPolicy.getVersionNo();
102                     for (LoadedPolicy oldUpdatedPolicy : notificationRecord.getLoadedPolicies()) {
103                         if (policyName.equals(oldUpdatedPolicy.getPolicyName())
104                             && ver.equals(oldUpdatedPolicy.getVersionNo())) {
105                             updated = false;
106                             // Remove the policy from copy.
107                             oldUpdatedLostPolicies.remove(oldUpdatedPolicy);
108                             // Eliminating Duplicate.
109                             oldUpdatedPolicies.remove(oldUpdatedPolicy);
110                         }
111                     }
112                     // Change the record if the policy has been Removed earlier.
113                     for (RemovedPolicy oldRemovedPolicy : notificationRecord.getRemovedPolicies()) {
114                         if (oldRemovedPolicy.getPolicyName().equals(policyName)
115                             && oldRemovedPolicy.getVersionNo().equals(ver)) {
116                             oldRemovedPolicies.remove(oldRemovedPolicy);
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                         sUPolicy.setUpdateType(uPolicy.getUpdateType());
185                         updatedPolicies.add(sUPolicy);
186                     }
187
188                     // Checking with the new updated policies.
189                     if (notification.getLoadedPolicies() != null && !notification.getLoadedPolicies().isEmpty()) {
190                         for (LoadedPolicy newUpdatedPolicy : notification.getLoadedPolicies()) {
191                             // If it was removed earlier then we need to remove from our record
192                             Iterator<StdRemovedPolicy> oldRemovedPolicy = removedPolicies.iterator();
193                             String policyName = newUpdatedPolicy.getPolicyName();
194                             String ver = newUpdatedPolicy.getVersionNo();
195                             while (oldRemovedPolicy.hasNext()) {
196                                 RemovedPolicy policy = oldRemovedPolicy.next();
197                                 if (policyName.equals(policy.getPolicyName())
198                                     && ver.equals(policy.getVersionNo())) {
199                                     oldRemovedPolicy.remove();
200                                 }
201                             }
202                             // If it was previously updated need to Overwrite it to the record.
203                             updatedPolicies.removeIf(policy -> policyName.equals(policy.getPolicyName())
204                                 && ver.equals(policy.getVersionNo()));
205
206                             StdLoadedPolicy sUPolicy = new StdLoadedPolicy();
207                             sUPolicy.setMatches(newUpdatedPolicy.getMatches());
208                             sUPolicy.setPolicyName(newUpdatedPolicy.getPolicyName());
209                             sUPolicy.setVersionNo(newUpdatedPolicy.getVersionNo());
210                             sUPolicy.setUpdateType(newUpdatedPolicy.getUpdateType());
211                             updatedPolicies.add(sUPolicy);
212                         }
213                     }
214                     // Checking with New Removed Policies.
215                     if (notification.getRemovedPolicies() != null && !notification.getRemovedPolicies().isEmpty()) {
216                         for (RemovedPolicy newRemovedPolicy : notification.getRemovedPolicies()) {
217                             // If it was previously removed Overwrite it to the record.
218                             Iterator<StdRemovedPolicy> oldRemovedPolicy = removedPolicies.iterator();
219                             String policyName = newRemovedPolicy.getPolicyName();
220                             String ver = newRemovedPolicy.getVersionNo();
221                             while (oldRemovedPolicy.hasNext()) {
222                                 RemovedPolicy policy = oldRemovedPolicy.next();
223                                 if (policyName.equals(policy.getPolicyName())
224                                     && ver.equals(policy.getVersionNo())) {
225                                     oldRemovedPolicy.remove();
226                                 }
227                             }
228                             // If it was added earlier then we need to remove from our record.
229                             updatedPolicies.removeIf(policy -> policyName.equals(policy.getPolicyName())
230                                 && ver.equals(policy.getVersionNo()));
231
232                             StdRemovedPolicy sRPolicy = new StdRemovedPolicy();
233                             sRPolicy.setPolicyName(policyName);
234                             sRPolicy.setVersionNo(ver);
235                             removedPolicies.add(sRPolicy);
236                         }
237                     }
238                     notificationRecord.setRemovedPolicies(removedPolicies);
239                     notificationRecord.setLoadedPolicies(updatedPolicies);
240                 }
241                 if (!notificationRecord.getLoadedPolicies().isEmpty() && !notificationRecord.getRemovedPolicies()
242                     .isEmpty()) {
243                     notificationRecord.setNotificationType(NotificationType.BOTH);
244                 } else if (!notificationRecord.getLoadedPolicies().isEmpty()) {
245                     notificationRecord.setNotificationType(NotificationType.UPDATE);
246                 } else if (!notificationRecord.getRemovedPolicies().isEmpty()) {
247                     notificationRecord.setNotificationType(NotificationType.REMOVE);
248                 }
249             }
250         }
251     }
252
253     // This should return the current Notification Record.
254     public static PDPNotification getNotificationRecord() {
255         return notificationRecord;
256     }
257 }