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