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;
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;
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.
39 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 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())) {
74 // Don't want a duplicate.
75 oldRemovedPolicies.remove(oldRemovedPolicy);
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);
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 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())) {
106 // Remove the policy from copy.
107 oldUpdatedLostPolicies.remove(oldUpdatedPolicy);
108 // Eliminating Duplicate.
109 oldUpdatedPolicies.remove(oldUpdatedPolicy);
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);
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);
153 if (remove && update) {
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 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();
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()));
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);
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();
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()));
232 StdRemovedPolicy sRPolicy = new StdRemovedPolicy();
233 sRPolicy.setPolicyName(policyName);
234 sRPolicy.setVersionNo(ver);
235 removedPolicies.add(sRPolicy);
238 notificationRecord.setRemovedPolicies(removedPolicies);
239 notificationRecord.setLoadedPolicies(updatedPolicies);
241 if (!notificationRecord.getLoadedPolicies().isEmpty() && !notificationRecord.getRemovedPolicies()
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);
253 // This should return the current Notification Record.
254 public static PDPNotification getNotificationRecord() {
255 return notificationRecord;