2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.std;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.HashSet;
26 import java.util.Iterator;
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;
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.
40 public class NotificationStore {
41 private static StdPDPNotification notificationRecord = new StdPDPNotification();
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;
59 return notificationDelta;
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())){
72 // Don't want a duplicate.
73 oldRemovedPolicies.remove(oldRemovedPolicy);
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);
88 notificationRecord.getRemovedPolicies().add(newRemovedPolicy);
89 removedDelta.add((StdRemovedPolicy)newRemovedPolicy);
91 // This will be converted to New Later.
92 oldRemovedPolicies.add(newRemovedPolicy);
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())){
104 // Remove the policy from copy.
105 oldUpdatedLostPolicies.remove(oldUpdatedPolicy);
106 // Eliminating Duplicate.
107 oldUpdatedPolicies.remove(oldUpdatedPolicy);
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);
121 updatedDelta.add((StdLoadedPolicy)newUpdatedPolicy);
123 // This will be converted to new Later
124 oldUpdatedPolicies.add(newUpdatedPolicy);
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);
137 // Update our Record.
138 if(!oldUpdatedPolicies.isEmpty()){
139 for(LoadedPolicy updatedPolicy: oldUpdatedPolicies){
140 newUpdatedPolicies.add((StdLoadedPolicy)updatedPolicy);
143 if(!oldRemovedPolicies.isEmpty()){
144 for(RemovedPolicy removedPolicy: oldRemovedPolicies){
145 newRemovedPolicies.add((StdRemovedPolicy)removedPolicy);
148 notificationRecord.setRemovedPolicies(newRemovedPolicies);
149 notificationRecord.setLoadedPolicies(newUpdatedPolicies);
150 // Update the notification Result.
151 notificationDelta.setRemovedPolicies(removedDelta);
152 notificationDelta.setLoadedPolicies(updatedDelta);
154 notificationDelta.setNotificationType(NotificationType.BOTH);
156 notificationDelta.setNotificationType(NotificationType.REMOVE);
158 notificationDelta.setNotificationType(NotificationType.UPDATE);
161 return notificationDelta;
164 public static void recordNotification(StdPDPNotification notification){
165 if(notification!=null){
166 if(notificationRecord.getRemovedPolicies()==null || notificationRecord.getLoadedPolicies()==null){
167 notificationRecord = notification;
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);
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);
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 while(oldRemovedPolicy.hasNext()){
194 RemovedPolicy policy = oldRemovedPolicy.next();
195 if(newUpdatedPolicy.getPolicyName().equals(policy.getPolicyName())) {
196 if(newUpdatedPolicy.getVersionNo().equals(policy.getVersionNo())) {
197 oldRemovedPolicy.remove();
201 // If it was previously updated need to Overwrite it to the record.
202 Iterator<StdLoadedPolicy> oldUpdatedPolicy = updatedPolicies.iterator();
203 while(oldUpdatedPolicy.hasNext()){
204 LoadedPolicy policy = oldUpdatedPolicy.next();
205 if(newUpdatedPolicy.getPolicyName().equals(policy.getPolicyName())) {
206 if(newUpdatedPolicy.getVersionNo().equals(policy.getVersionNo())) {
207 oldUpdatedPolicy.remove();
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);
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 while(oldRemovedPolicy.hasNext()){
225 RemovedPolicy policy = oldRemovedPolicy.next();
226 if(newRemovedPolicy.getPolicyName().equals(policy.getPolicyName())) {
227 if(newRemovedPolicy.getVersionNo().equals(policy.getVersionNo())) {
228 oldRemovedPolicy.remove();
232 // If it was added earlier then we need to remove from our record.
233 Iterator<StdLoadedPolicy> oldUpdatedPolicy = updatedPolicies.iterator();
234 while(oldUpdatedPolicy.hasNext()){
235 LoadedPolicy policy = oldUpdatedPolicy.next();
236 if(newRemovedPolicy.getPolicyName().equals(policy.getPolicyName())) {
237 if(newRemovedPolicy.getVersionNo().equals(policy.getVersionNo())) {
238 oldUpdatedPolicy.remove();
242 StdRemovedPolicy sRPolicy = new StdRemovedPolicy();
243 sRPolicy.setPolicyName(newRemovedPolicy.getPolicyName());
244 sRPolicy.setVersionNo(newRemovedPolicy.getVersionNo());
245 removedPolicies.add(sRPolicy);
248 notificationRecord.setRemovedPolicies(removedPolicies);
249 notificationRecord.setLoadedPolicies(updatedPolicies);
251 if(!notificationRecord.getLoadedPolicies().isEmpty() && !notificationRecord.getRemovedPolicies().isEmpty()){
252 notificationRecord.setNotificationType(NotificationType.BOTH);
253 }else if(!notificationRecord.getLoadedPolicies().isEmpty()){
254 notificationRecord.setNotificationType(NotificationType.UPDATE);
255 }else if(!notificationRecord.getRemovedPolicies().isEmpty()){
256 notificationRecord.setNotificationType(NotificationType.REMOVE);
262 // This should return the current Notification Record.
263 public static PDPNotification getNotificationRecord(){
264 return notificationRecord;