X-Git-Url: https://gerrit.onap.org/r/gitweb?p=policy%2Fengine.git;a=blobdiff_plain;f=PolicyEngineUtils%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fstd%2FNotificationStore.java;h=40c640f9a99f3d454bab1134ee7fd29f3ca5cfda;hp=6164418d91e481633fda9a75ae43244a405275f6;hb=e9b8aa0223e6f042c0533176ae8222fb061852de;hpb=3c95fa7249cccc3d0eb3c9e89ba708a3167d41ba diff --git a/PolicyEngineUtils/src/main/java/org/onap/policy/std/NotificationStore.java b/PolicyEngineUtils/src/main/java/org/onap/policy/std/NotificationStore.java index 6164418d9..40c640f9a 100644 --- a/PolicyEngineUtils/src/main/java/org/onap/policy/std/NotificationStore.java +++ b/PolicyEngineUtils/src/main/java/org/onap/policy/std/NotificationStore.java @@ -2,15 +2,15 @@ * ============LICENSE_START======================================================= * PolicyEngineUtils * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017, 2019 AT&T Intellectual Property. All rights reserved. * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -25,27 +25,33 @@ import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; + import org.onap.policy.api.LoadedPolicy; import org.onap.policy.api.NotificationType; import org.onap.policy.api.PDPNotification; import org.onap.policy.api.RemovedPolicy; - /* - * This Should Compare and save the Notifications from the beginning of Time. - * If there is something new (missed) We notify the client. - * Works for PDP failures. - * + * This Should Compare and save the Notifications from the beginning of Time. + * If there is something new (missed) We notify the client. + * Works for PDP failures. + * */ public class NotificationStore { private static StdPDPNotification notificationRecord = new StdPDPNotification(); - private NotificationStore () { + private NotificationStore() { // Sonar prefers that we have an empty public constructor // as opposed to an implicit public constructor. } - + + /** + * getDeltaNotification. + * + * @param newNotification StdPDPNotification + * @return StdPDPNotification + */ public static StdPDPNotification getDeltaNotification(StdPDPNotification newNotification) { StdPDPNotification notificationDelta = new StdPDPNotification(); ArrayList removedDelta = new ArrayList<>(); @@ -72,13 +78,15 @@ public class NotificationStore { // check for old removed policies. if (!newNotification.getRemovedPolicies().isEmpty()) { for (RemovedPolicy newRemovedPolicy : newNotification.getRemovedPolicies()) { - remove = updateRemovedPolicies(removedDelta, oldUpdatedLostPolicies, oldRemovedPolicies, oldUpdatedPolicies, remove, newRemovedPolicy); + remove = updateRemovedPolicies(removedDelta, oldUpdatedLostPolicies, oldRemovedPolicies, + oldUpdatedPolicies, remove, newRemovedPolicy); } } // Check for old Updated Policies. if (!newNotification.getLoadedPolicies().isEmpty()) { for (LoadedPolicy newUpdatedPolicy : newNotification.getLoadedPolicies()) { - update = modifyUpdatedPolicies(updatedDelta, oldUpdatedLostPolicies, oldRemovedPolicies, oldUpdatedPolicies, update, newUpdatedPolicy); + update = modifyUpdatedPolicies(updatedDelta, oldUpdatedLostPolicies, oldRemovedPolicies, + oldUpdatedPolicies, update, newUpdatedPolicy); } // Conversion of Update to Remove if that occurred. if (!oldUpdatedLostPolicies.isEmpty()) { @@ -118,14 +126,15 @@ public class NotificationStore { return notificationDelta; } - private static Boolean modifyUpdatedPolicies(ArrayList updatedDelta, Collection oldUpdatedLostPolicies, Collection oldRemovedPolicies, Collection oldUpdatedPolicies, Boolean update, LoadedPolicy newUpdatedPolicy) { + private static Boolean modifyUpdatedPolicies(ArrayList updatedDelta, + Collection oldUpdatedLostPolicies, Collection oldRemovedPolicies, + Collection oldUpdatedPolicies, Boolean update, LoadedPolicy newUpdatedPolicy) { // Look for policies which are not in Update Boolean updated = true; String policyName = newUpdatedPolicy.getPolicyName(); String ver = newUpdatedPolicy.getVersionNo(); for (LoadedPolicy oldUpdatedPolicy : notificationRecord.getLoadedPolicies()) { - if (policyName.equals(oldUpdatedPolicy.getPolicyName()) - && ver.equals(oldUpdatedPolicy.getVersionNo())) { + if (policyName.equals(oldUpdatedPolicy.getPolicyName()) && ver.equals(oldUpdatedPolicy.getVersionNo())) { updated = false; // Remove the policy from copy. oldUpdatedLostPolicies.remove(oldUpdatedPolicy); @@ -135,8 +144,7 @@ public class NotificationStore { } // Change the record if the policy has been Removed earlier. for (RemovedPolicy oldRemovedPolicy : notificationRecord.getRemovedPolicies()) { - if (oldRemovedPolicy.getPolicyName().equals(policyName) - && oldRemovedPolicy.getVersionNo().equals(ver)) { + if (oldRemovedPolicy.getPolicyName().equals(policyName) && oldRemovedPolicy.getVersionNo().equals(ver)) { oldRemovedPolicies.remove(oldRemovedPolicy); } } @@ -149,23 +157,23 @@ public class NotificationStore { return update; } - private static Boolean updateRemovedPolicies(ArrayList removedDelta, Collection oldUpdatedLostPolicies, Collection oldRemovedPolicies, Collection oldUpdatedPolicies, Boolean remove, RemovedPolicy newRemovedPolicy) { - //Look for policy Not in Remove + private static Boolean updateRemovedPolicies(ArrayList removedDelta, + Collection oldUpdatedLostPolicies, Collection oldRemovedPolicies, + Collection oldUpdatedPolicies, Boolean remove, RemovedPolicy newRemovedPolicy) { + // Look for policy Not in Remove Boolean removed = true; String policyName = newRemovedPolicy.getPolicyName(); String ver = newRemovedPolicy.getVersionNo(); for (RemovedPolicy oldRemovedPolicy : notificationRecord.getRemovedPolicies()) { - if (policyName.equals(oldRemovedPolicy.getPolicyName()) - && ver.equals(oldRemovedPolicy.getVersionNo())) { + if (policyName.equals(oldRemovedPolicy.getPolicyName()) && ver.equals(oldRemovedPolicy.getVersionNo())) { removed = false; // Don't want a duplicate. oldRemovedPolicies.remove(oldRemovedPolicy); } } - //We need to change our record we have an Update record of this remove. + // We need to change our record we have an Update record of this remove. for (LoadedPolicy oldUpdatedPolicy : notificationRecord.getLoadedPolicies()) { - if (policyName.equals(oldUpdatedPolicy.getPolicyName()) - && ver.equals(oldUpdatedPolicy.getVersionNo())) { + if (policyName.equals(oldUpdatedPolicy.getPolicyName()) && ver.equals(oldUpdatedPolicy.getVersionNo())) { oldUpdatedPolicies.remove(oldUpdatedPolicy); oldUpdatedLostPolicies.remove(oldUpdatedPolicy); } @@ -180,6 +188,11 @@ public class NotificationStore { return remove; } + /** + * recordNotification. + * + * @param notification StdPDPNotification + */ public static void recordNotification(StdPDPNotification notification) { if (notification == null) { return; @@ -187,49 +200,50 @@ public class NotificationStore { if (notificationRecord.getRemovedPolicies() == null || notificationRecord.getLoadedPolicies() == null) { notificationRecord = notification; - } else { - // Check if there is anything new and update the record. - if (notificationRecord.getLoadedPolicies() != null || notificationRecord.getRemovedPolicies() != null) { - HashSet removedPolicies = new HashSet<>(); - for (RemovedPolicy rPolicy : notificationRecord.getRemovedPolicies()) { - StdRemovedPolicy sRPolicy = new StdRemovedPolicy(); - sRPolicy.setPolicyName(rPolicy.getPolicyName()); - sRPolicy.setVersionNo(rPolicy.getVersionNo()); - removedPolicies.add(sRPolicy); - } - HashSet updatedPolicies = new HashSet<>(); - for (LoadedPolicy uPolicy : notificationRecord.getLoadedPolicies()) { - StdLoadedPolicy sUPolicy = new StdLoadedPolicy(); - sUPolicy.setMatches(uPolicy.getMatches()); - sUPolicy.setPolicyName(uPolicy.getPolicyName()); - sUPolicy.setVersionNo(uPolicy.getVersionNo()); - sUPolicy.setUpdateType(uPolicy.getUpdateType()); - updatedPolicies.add(sUPolicy); - } + return; + } + // Check if there is anything new and update the record. + if (notificationRecord.getLoadedPolicies() != null || notificationRecord.getRemovedPolicies() != null) { + HashSet removedPolicies = new HashSet<>(); + for (RemovedPolicy removedPolicy : notificationRecord.getRemovedPolicies()) { + StdRemovedPolicy stdRemovedPolicy = new StdRemovedPolicy(); + stdRemovedPolicy.setPolicyName(removedPolicy.getPolicyName()); + stdRemovedPolicy.setVersionNo(removedPolicy.getVersionNo()); + removedPolicies.add(stdRemovedPolicy); + } + HashSet updatedPolicies = new HashSet<>(); + for (LoadedPolicy loadedPolicy : notificationRecord.getLoadedPolicies()) { + StdLoadedPolicy stdLoadedPolicy = new StdLoadedPolicy(); + stdLoadedPolicy.setMatches(loadedPolicy.getMatches()); + stdLoadedPolicy.setPolicyName(loadedPolicy.getPolicyName()); + stdLoadedPolicy.setVersionNo(loadedPolicy.getVersionNo()); + stdLoadedPolicy.setUpdateType(loadedPolicy.getUpdateType()); + updatedPolicies.add(stdLoadedPolicy); + } - // Checking with the new updated policies. - if (notification.getLoadedPolicies() != null && !notification.getLoadedPolicies().isEmpty()) { - checkNewUpdatedPolicies(notification, removedPolicies, updatedPolicies); - } - // Checking with New Removed Policies. - if (notification.getRemovedPolicies() != null && !notification.getRemovedPolicies().isEmpty()) { - checkNewRemovedPolicies(notification, removedPolicies, updatedPolicies); - } - notificationRecord.setRemovedPolicies(removedPolicies); - notificationRecord.setLoadedPolicies(updatedPolicies); + // Checking with the new updated policies. + if (notification.getLoadedPolicies() != null && !notification.getLoadedPolicies().isEmpty()) { + checkNewUpdatedPolicies(notification, removedPolicies, updatedPolicies); } - if (!notificationRecord.getLoadedPolicies().isEmpty() && !notificationRecord.getRemovedPolicies() - .isEmpty()) { - notificationRecord.setNotificationType(NotificationType.BOTH); - } else if (!notificationRecord.getLoadedPolicies().isEmpty()) { - notificationRecord.setNotificationType(NotificationType.UPDATE); - } else if (!notificationRecord.getRemovedPolicies().isEmpty()) { - notificationRecord.setNotificationType(NotificationType.REMOVE); + // Checking with New Removed Policies. + if (notification.getRemovedPolicies() != null && !notification.getRemovedPolicies().isEmpty()) { + checkNewRemovedPolicies(notification, removedPolicies, updatedPolicies); } + notificationRecord.setRemovedPolicies(removedPolicies); + notificationRecord.setLoadedPolicies(updatedPolicies); + } + if (!notificationRecord.getLoadedPolicies().isEmpty() + && !notificationRecord.getRemovedPolicies().isEmpty()) { + notificationRecord.setNotificationType(NotificationType.BOTH); + } else if (!notificationRecord.getLoadedPolicies().isEmpty()) { + notificationRecord.setNotificationType(NotificationType.UPDATE); + } else if (!notificationRecord.getRemovedPolicies().isEmpty()) { + notificationRecord.setNotificationType(NotificationType.REMOVE); } } - private static void checkNewUpdatedPolicies(StdPDPNotification notification, HashSet removedPolicies, HashSet updatedPolicies) { + private static void checkNewUpdatedPolicies(StdPDPNotification notification, + HashSet removedPolicies, HashSet updatedPolicies) { for (LoadedPolicy newUpdatedPolicy : notification.getLoadedPolicies()) { // If it was removed earlier then we need to remove from our record Iterator oldRemovedPolicy = removedPolicies.iterator(); @@ -237,25 +251,25 @@ public class NotificationStore { String ver = newUpdatedPolicy.getVersionNo(); while (oldRemovedPolicy.hasNext()) { RemovedPolicy policy = oldRemovedPolicy.next(); - if (policyName.equals(policy.getPolicyName()) - && ver.equals(policy.getVersionNo())) { + if (policyName.equals(policy.getPolicyName()) && ver.equals(policy.getVersionNo())) { oldRemovedPolicy.remove(); } } // If it was previously updated need to Overwrite it to the record. - updatedPolicies.removeIf(policy -> policyName.equals(policy.getPolicyName()) - && ver.equals(policy.getVersionNo())); + updatedPolicies + .removeIf(policy -> policyName.equals(policy.getPolicyName()) && ver.equals(policy.getVersionNo())); - StdLoadedPolicy sUPolicy = new StdLoadedPolicy(); - sUPolicy.setMatches(newUpdatedPolicy.getMatches()); - sUPolicy.setPolicyName(newUpdatedPolicy.getPolicyName()); - sUPolicy.setVersionNo(newUpdatedPolicy.getVersionNo()); - sUPolicy.setUpdateType(newUpdatedPolicy.getUpdateType()); - updatedPolicies.add(sUPolicy); + StdLoadedPolicy stdLoadedPolicy = new StdLoadedPolicy(); + stdLoadedPolicy.setMatches(newUpdatedPolicy.getMatches()); + stdLoadedPolicy.setPolicyName(newUpdatedPolicy.getPolicyName()); + stdLoadedPolicy.setVersionNo(newUpdatedPolicy.getVersionNo()); + stdLoadedPolicy.setUpdateType(newUpdatedPolicy.getUpdateType()); + updatedPolicies.add(stdLoadedPolicy); } } - private static void checkNewRemovedPolicies(StdPDPNotification notification, HashSet removedPolicies, HashSet updatedPolicies) { + private static void checkNewRemovedPolicies(StdPDPNotification notification, + HashSet removedPolicies, HashSet updatedPolicies) { for (RemovedPolicy newRemovedPolicy : notification.getRemovedPolicies()) { // If it was previously removed Overwrite it to the record. Iterator oldRemovedPolicy = removedPolicies.iterator(); @@ -263,19 +277,18 @@ public class NotificationStore { String ver = newRemovedPolicy.getVersionNo(); while (oldRemovedPolicy.hasNext()) { RemovedPolicy policy = oldRemovedPolicy.next(); - if (policyName.equals(policy.getPolicyName()) - && ver.equals(policy.getVersionNo())) { + if (policyName.equals(policy.getPolicyName()) && ver.equals(policy.getVersionNo())) { oldRemovedPolicy.remove(); } } // If it was added earlier then we need to remove from our record. - updatedPolicies.removeIf(policy -> policyName.equals(policy.getPolicyName()) - && ver.equals(policy.getVersionNo())); + updatedPolicies + .removeIf(policy -> policyName.equals(policy.getPolicyName()) && ver.equals(policy.getVersionNo())); - StdRemovedPolicy sRPolicy = new StdRemovedPolicy(); - sRPolicy.setPolicyName(policyName); - sRPolicy.setVersionNo(ver); - removedPolicies.add(sRPolicy); + StdRemovedPolicy stdRemovedPolicy = new StdRemovedPolicy(); + stdRemovedPolicy.setPolicyName(policyName); + stdRemovedPolicy.setVersionNo(ver); + removedPolicies.add(stdRemovedPolicy); } }