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 private NotificationStore () {
44 // Sonar prefers that we have an empty public constructor
45 // as opposed to an implicit public constructor.
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;
64 return notificationDelta;
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())) {
79 // Don't want a duplicate.
80 oldRemovedPolicies.remove(oldRemovedPolicy);
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);
93 notificationRecord.getRemovedPolicies().add(newRemovedPolicy);
94 removedDelta.add((StdRemovedPolicy) newRemovedPolicy);
96 // This will be converted to New Later.
97 oldRemovedPolicies.add(newRemovedPolicy);
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())) {
111 // Remove the policy from copy.
112 oldUpdatedLostPolicies.remove(oldUpdatedPolicy);
113 // Eliminating Duplicate.
114 oldUpdatedPolicies.remove(oldUpdatedPolicy);
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);
126 updatedDelta.add((StdLoadedPolicy) newUpdatedPolicy);
128 // This will be converted to new Later
129 oldUpdatedPolicies.add(newUpdatedPolicy);
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);
142 // Update our Record.
143 if (!oldUpdatedPolicies.isEmpty()) {
144 for (LoadedPolicy updatedPolicy : oldUpdatedPolicies) {
145 newUpdatedPolicies.add((StdLoadedPolicy) updatedPolicy);
148 if (!oldRemovedPolicies.isEmpty()) {
149 for (RemovedPolicy removedPolicy : oldRemovedPolicies) {
150 newRemovedPolicies.add((StdRemovedPolicy) removedPolicy);
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);
161 notificationDelta.setNotificationType(NotificationType.REMOVE);
163 notificationDelta.setNotificationType(NotificationType.UPDATE);
166 return notificationDelta;
169 public static void recordNotification(StdPDPNotification notification) {
170 if (notification != null) {
171 if (notificationRecord.getRemovedPolicies() == null || notificationRecord.getLoadedPolicies() == null) {
172 notificationRecord = notification;
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);
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);
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();
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()));
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 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();
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()));
237 StdRemovedPolicy sRPolicy = new StdRemovedPolicy();
238 sRPolicy.setPolicyName(policyName);
239 sRPolicy.setVersionNo(ver);
240 removedPolicies.add(sRPolicy);
243 notificationRecord.setRemovedPolicies(removedPolicies);
244 notificationRecord.setLoadedPolicies(updatedPolicies);
246 if (!notificationRecord.getLoadedPolicies().isEmpty() && !notificationRecord.getRemovedPolicies()
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);
258 // This should return the current Notification Record.
259 public static PDPNotification getNotificationRecord() {
260 return notificationRecord;