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.openecomp.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.openecomp.policy.api.LoadedPolicy;
29 import org.openecomp.policy.api.NotificationType;
30 import org.openecomp.policy.api.PDPNotification;
31 import org.openecomp.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<StdRemovedPolicy>();
46 ArrayList<StdLoadedPolicy> updatedDelta = new ArrayList<StdLoadedPolicy>();
47 Collection<StdLoadedPolicy> newUpdatedPolicies = new ArrayList<StdLoadedPolicy>();
48 Collection<StdRemovedPolicy> newRemovedPolicies = new ArrayList<StdRemovedPolicy>();
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<StdRemovedPolicy>();
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<StdLoadedPolicy>();
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);
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();
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();
210 StdLoadedPolicy sUPolicy = new StdLoadedPolicy();
211 sUPolicy.setMatches(newUpdatedPolicy.getMatches());
212 sUPolicy.setPolicyName(newUpdatedPolicy.getPolicyName());
213 sUPolicy.setVersionNo(newUpdatedPolicy.getVersionNo());
214 updatedPolicies.add(sUPolicy);
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();
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();
240 StdRemovedPolicy sRPolicy = new StdRemovedPolicy();
241 sRPolicy.setPolicyName(newRemovedPolicy.getPolicyName());
242 sRPolicy.setVersionNo(newRemovedPolicy.getVersionNo());
243 removedPolicies.add(sRPolicy);
246 notificationRecord.setRemovedPolicies(removedPolicies);
247 notificationRecord.setLoadedPolicies(updatedPolicies);
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);
260 // This should return the current Notification Record.
261 public static PDPNotification getNotificationRecord(){
262 return notificationRecord;