From: Krishnajinka Date: Thu, 26 Jul 2018 07:33:11 +0000 (+0900) Subject: Refactor code for nested stmts in policy std X-Git-Tag: 1.3.0~55^2 X-Git-Url: https://gerrit.onap.org/r/gitweb?p=policy%2Fengine.git;a=commitdiff_plain;h=5a9d91a75f1a65074c71652e7f404f460390974a Refactor code for nested stmts in policy std Refactor code for nested control statements in PolicyEngineAPI policy.std package. These issues are flagged by Sonar as Major issues. Rework based on review comments Issue-ID: POLICY-1012 Change-Id: I6925dd5d56b653ef4cefe5ae64686a49f9741125 Signed-off-by: Krishnajinka --- diff --git a/PolicyEngineAPI/src/main/java/org/onap/policy/std/ManualClientEndDMAAP.java b/PolicyEngineAPI/src/main/java/org/onap/policy/std/ManualClientEndDMAAP.java index 0281f6010..065f084ba 100644 --- a/PolicyEngineAPI/src/main/java/org/onap/policy/std/ManualClientEndDMAAP.java +++ b/PolicyEngineAPI/src/main/java/org/onap/policy/std/ManualClientEndDMAAP.java @@ -3,6 +3,7 @@ * PolicyEngineAPI * ================================================================================ * Copyright (C) 2017 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. @@ -33,98 +34,103 @@ import org.onap.policy.utils.BusPublisher; import org.onap.policy.xacml.api.XACMLErrorConstants; public class ManualClientEndDMAAP { - private static StdPDPNotification notification = null; - private static String resultJson = null; - private static Logger logger = FlexLogger.getLogger(ManualClientEndDMAAP.class.getName()); - private static BusConsumer dmaapConsumer = null; - private static String uniquID = null; - private static String topic = null; - - private ManualClientEndDMAAP() { - // Empty constructor - } - + private static StdPDPNotification notification = null; + private static String resultJson = null; + private static Logger logger = FlexLogger.getLogger(ManualClientEndDMAAP.class.getName()); + private static BusConsumer dmaapConsumer = null; + private static String uniquID = null; + private static String topic = null; + private static int RETRY_LIMIT = 4; + private ManualClientEndDMAAP() { + // Empty constructor + } - public static PDPNotification result(NotificationScheme scheme) { - if (resultJson == null || notification == null) { - logger.debug("No Result" ); - return null; - } - if(scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)) { - boolean removed = false; - boolean updated = false; - if(notification.getRemovedPolicies()!=null && !notification.getRemovedPolicies().isEmpty()){ - removed = true; - } - if(notification.getLoadedPolicies()!=null && !notification.getLoadedPolicies().isEmpty()){ - updated = true; - } - if(removed && updated) { - notification.setNotificationType(NotificationType.BOTH); - }else if(removed){ - notification.setNotificationType(NotificationType.REMOVE); - }else if(updated){ - notification.setNotificationType(NotificationType.UPDATE); - } - return notification; - }else if(scheme.equals(NotificationScheme.MANUAL_NOTIFICATIONS)) { - return MatchStore.checkMatch(notification); - } - return null; - } - private static void publishMessage(String pubTopic, String uniqueID, List dmaapList, String aafLogin, String aafPassword) { + public static PDPNotification result(NotificationScheme scheme) { + if (resultJson == null || notification == null) { + logger.debug("No Result" ); + return null; + } + if(scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)) { + boolean removed = false; + boolean updated = false; + if(notification.getRemovedPolicies()!=null && !notification.getRemovedPolicies().isEmpty()){ + removed = true; + } + if(notification.getLoadedPolicies()!=null && !notification.getLoadedPolicies().isEmpty()){ + updated = true; + } + if(removed && updated) { + notification.setNotificationType(NotificationType.BOTH); + }else if(removed){ + notification.setNotificationType(NotificationType.REMOVE); + }else if(updated){ + notification.setNotificationType(NotificationType.UPDATE); + } + return notification; + }else if(scheme.equals(NotificationScheme.MANUAL_NOTIFICATIONS)) { + return MatchStore.checkMatch(notification); + } + return null; + } + + private static void publishMessage(String pubTopic, String uniqueID, List dmaapList, String aafLogin, String aafPassword) { BusPublisher pub = null; - try { - pub = new BusPublisher.DmaapPublisherWrapper(dmaapList, topic, aafLogin, aafPassword); - final JSONObject msg1 = new JSONObject (); - msg1.put ( "JSON", "DMaaP Update Request UID=" + uniqueID); - pub.send ( "MyPartitionKey", msg1.toString () ); - } catch (Exception e) { - logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Unable to create DMaaP Publisher: ", e); - } - if(pub != null){ - pub.close (); - } - } + try { + pub = new BusPublisher.DmaapPublisherWrapper(dmaapList, topic, aafLogin, aafPassword); + final JSONObject msg1 = new JSONObject (); + msg1.put ( "JSON", "DMaaP Update Request UID=" + uniqueID); + pub.send ( "MyPartitionKey", msg1.toString () ); + } catch (Exception e) { + logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Unable to create DMaaP Publisher: ", e); + } + if(pub != null){ + pub.close (); + } + } + + //NOTE: should be able to remove this for DMAAP since we will not be creating topics dynamically + public static void createTopic (String topic, String uniquID, List dmaapList, String aafLogin, String aafPassword){ + ManualClientEndDMAAP.topic = topic; + publishMessage(topic, uniquID, dmaapList, aafLogin, aafPassword); + } + + + public static void start(List dmaapList, String topic, String aafLogin, String aafPassword, String uniqueID) { + + ManualClientEndDMAAP.uniquID = uniqueID; + ManualClientEndDMAAP.topic = topic; + + String id = "0"; + + try { + dmaapConsumer = new BusConsumer.DmaapConsumerWrapper(dmaapList, topic, aafLogin, aafPassword, "clientGroup", id, 15*1000, 1000); + } catch (Exception e) { + logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Unable to create DMaaP Consumer: ", e); + } + + int retries = 1; + boolean isSuccess = false; + while (retries < RETRY_LIMIT && !isSuccess) { + isSuccess = publishMessageAndSetNotification(dmaapList, topic, aafLogin, aafPassword); + retries++; + } + } - //NOTE: should be able to remove this for DMAAP since we will not be creating topics dynamically - public static void createTopic (String topic, String uniquID, List dmaapList, String aafLogin, String aafPassword){ - ManualClientEndDMAAP.topic = topic; - publishMessage(topic, uniquID, dmaapList, aafLogin, aafPassword); - } - - - public static void start(List dmaapList, String topic, String aafLogin, String aafPassword, String uniqueID) { - - ManualClientEndDMAAP.uniquID = uniqueID; - ManualClientEndDMAAP.topic = topic; - - String id = "0"; - - try { - dmaapConsumer = new BusConsumer.DmaapConsumerWrapper(dmaapList, topic, aafLogin, aafPassword, "clientGroup", id, 15*1000, 1000); - } catch (Exception e) { - logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Unable to create DMaaP Consumer: ", e); - } - - int count = 1; - while (count < 4) { - publishMessage(topic, uniquID, dmaapList, aafLogin, aafPassword); - try { - for ( String msg : dmaapConsumer.fetch () ) - { - logger.debug("Manual Notification Recieved Message " + msg + " from DMaaP server : " + dmaapList.toString()); - resultJson = msg; - if (!msg.contains("DMaaP Update")){ - notification = NotificationUnMarshal.notificationJSON(msg); - count = 4; - } - } - }catch (Exception e) { - logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Unable to fetch messages from DMaaP servers: ", e); - } - count++; - } - } + private static boolean publishMessageAndSetNotification(List dmaapList, String topic, String aafLogin, String aafPassword) { + publishMessage(topic, uniquID, dmaapList, aafLogin, aafPassword); + try { + for ( String msg : dmaapConsumer.fetch () ) { + logger.debug("Manual Notification Recieved Message " + msg + " from DMaaP server : " + dmaapList.toString()); + resultJson = msg; + if (!msg.contains("DMaaP Update")){ + notification = NotificationUnMarshal.notificationJSON(msg); + return true; + } + } + }catch (Exception e) { + logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Unable to fetch messages from DMaaP servers: ", e); + } + return false; + } } diff --git a/PolicyEngineAPI/src/main/java/org/onap/policy/std/ManualClientEndUEB.java b/PolicyEngineAPI/src/main/java/org/onap/policy/std/ManualClientEndUEB.java index c04736e86..d67e136e3 100644 --- a/PolicyEngineAPI/src/main/java/org/onap/policy/std/ManualClientEndUEB.java +++ b/PolicyEngineAPI/src/main/java/org/onap/policy/std/ManualClientEndUEB.java @@ -3,6 +3,7 @@ * PolicyEngineAPI * ================================================================================ * Copyright (C) 2017 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. @@ -39,123 +40,127 @@ import com.att.nsa.cambria.client.CambriaPublisher; @SuppressWarnings("deprecation") public class ManualClientEndUEB { - private static StdPDPNotification notification = null; - private static String resultJson = null; - private static Logger logger = FlexLogger.getLogger(ManualClientEndUEB.class.getName()); - private static CambriaConsumer CConsumer = null; - @SuppressWarnings("unused") - private static List uebURLList = null; - @SuppressWarnings("unused") - private static boolean messageNotReceived = false; - @SuppressWarnings("unused") - private static String url = null; - private static String uniquID = null; - private static String topic = null; + private static StdPDPNotification notification = null; + private static String resultJson = null; + private static Logger logger = FlexLogger.getLogger(ManualClientEndUEB.class.getName()); + private static CambriaConsumer CConsumer = null; + @SuppressWarnings("unused") + private static List uebURLList = null; + @SuppressWarnings("unused") + private static boolean messageNotReceived = false; + @SuppressWarnings("unused") + private static String url = null; + private static String uniquID = null; + private static String topic = null; + private static int RETRY_LIMIT = 4; - private ManualClientEndUEB() { - // Empty constructor - } + private ManualClientEndUEB() { + // Empty constructor + } - public static PDPNotification result(NotificationScheme scheme) { - if (resultJson == null || notification == null) { - logger.debug("No Result" ); - return null; - } - if(scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)) { - boolean removed = false; - boolean updated = false; - if(notification.getRemovedPolicies()!=null && !notification.getRemovedPolicies().isEmpty()){ - removed = true; - } - if(notification.getLoadedPolicies()!=null && !notification.getLoadedPolicies().isEmpty()){ - updated = true; - } - if(removed && updated) { - notification.setNotificationType(NotificationType.BOTH); - }else if(removed){ - notification.setNotificationType(NotificationType.REMOVE); - }else if(updated){ - notification.setNotificationType(NotificationType.UPDATE); - } - return notification; - }else if(scheme.equals(NotificationScheme.MANUAL_NOTIFICATIONS)) { - return MatchStore.checkMatch(notification); - } - return null; - } + public static PDPNotification result(NotificationScheme scheme) { + if (resultJson == null || notification == null) { + logger.debug("No Result" ); + return null; + } + if(scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)) { + boolean removed = false; + boolean updated = false; + if(notification.getRemovedPolicies()!=null && !notification.getRemovedPolicies().isEmpty()){ + removed = true; + } + if(notification.getLoadedPolicies()!=null && !notification.getLoadedPolicies().isEmpty()){ + updated = true; + } + if(removed && updated) { + notification.setNotificationType(NotificationType.BOTH); + }else if(removed){ + notification.setNotificationType(NotificationType.REMOVE); + }else if(updated){ + notification.setNotificationType(NotificationType.UPDATE); + } + return notification; + }else if(scheme.equals(NotificationScheme.MANUAL_NOTIFICATIONS)) { + return MatchStore.checkMatch(notification); + } + return null; + } - private static void publishMessage(String pubTopic, String uniqueID , List uebURLList) { - - String UEBlist = uebURLList.toString(); - UEBlist = UEBlist.substring(1,UEBlist.length()-1); + private static void publishMessage(String pubTopic, String uniqueID , List uebURLList) { + + String UEBlist = uebURLList.toString(); + UEBlist = UEBlist.substring(1,UEBlist.length()-1); CambriaPublisher pub = null; - try { - pub = CambriaClientFactory.createSimplePublisher(null, UEBlist, pubTopic); - } catch (Exception e1) { - logger.error("Exception Occured"+e1); - } + try { + pub = CambriaClientFactory.createSimplePublisher(null, UEBlist, pubTopic); + } catch (Exception e1) { + logger.error("Exception Occured"+e1); + } final JSONObject msg1 = new JSONObject (); msg1.put ( "JSON", "UEB Update Ruest UID=" + uniqueID); if(pub != null){ - try { - pub.send ( "MyPartitionKey", msg1.toString () ); - pub.close (); - } catch (IOException e) { - logger.error("Exception Occured"+e); - } + try { + pub.send ( "MyPartitionKey", msg1.toString () ); + pub.close (); + } catch (IOException e) { + logger.error("Exception Occured"+e); + } } - } + } + + public static void createTopic (String url, String uniquID, List uebURLList){ + URL aURL; + try { + aURL = new URL(url); + topic = aURL.getHost() + aURL.getPort(); + } catch (MalformedURLException e) { + topic = url.replace("[:/]", ""); + } + + publishMessage(topic+ uniquID , uniquID, uebURLList); - public static void createTopic (String url, String uniquID, List uebURLList){ - URL aURL; - try { - aURL = new URL(url); - topic = aURL.getHost() + aURL.getPort(); - } catch (MalformedURLException e) { - topic = url.replace("[:/]", ""); - } + } + public static void start(String url, List uebURLList, + String uniqueID) { + ManualClientEndUEB.uebURLList = uebURLList; + ManualClientEndUEB.url = url; + ManualClientEndUEB.uniquID = uniqueID; + URL aURL; + try { + aURL = new URL(url); + ManualClientEndUEB.topic = aURL.getHost() + aURL.getPort(); + } catch (MalformedURLException e) { + ManualClientEndUEB.topic = url.replace("[:/]", ""); + } + String id = "0"; + try { + CConsumer = CambriaClientFactory.createConsumer ( null, uebURLList, topic + uniquID, "clientGroup", id, 15*1000, 1000 ); + } catch (Exception e1) { + logger.error("Exception Occured"+e1); + } + int retries = 1; + boolean isSuccess = false; + while (retries < RETRY_LIMIT && !isSuccess) { + isSuccess = publishMessageAndSetNotification(uebURLList); + retries++; + } + } - publishMessage(topic+ uniquID , uniquID, uebURLList); - - } - public static void start(String url, List uebURLList, - String uniqueID) { - ManualClientEndUEB.uebURLList = uebURLList; - ManualClientEndUEB.url = url; - ManualClientEndUEB.uniquID = uniqueID; - URL aURL; - try { - aURL = new URL(url); - ManualClientEndUEB.topic = aURL.getHost() + aURL.getPort(); - } catch (MalformedURLException e) { - ManualClientEndUEB.topic = url.replace("[:/]", ""); - } - String id = "0"; - try { - CConsumer = CambriaClientFactory.createConsumer ( null, uebURLList, topic + uniquID, "clientGroup", id, 15*1000, 1000 ); - } catch (Exception e1) { - logger.error("Exception Occured"+e1); - } - int count = 1; - while (count < 4) { - publishMessage(topic + "UpdateRequest", uniquID, uebURLList); - try { - for ( String msg : CConsumer.fetch () ) - { - - logger.debug("Manual Notification Recieved Message " + msg + " from UEB cluster : " + uebURLList.toString()); - resultJson = msg; - if (!msg.contains("UEB Update")){ - notification = NotificationUnMarshal.notificationJSON(msg); - count = 4; - } - } - }catch (Exception e) { - logger.error("Error in Manual CLient UEB notification ", e); - } - count++; - } - } - + private static boolean publishMessageAndSetNotification(List uebURLList) { + publishMessage(topic + "UpdateRequest", uniquID, uebURLList); + try { + for ( String msg : CConsumer.fetch () ) { + logger.debug("Manual Notification Recieved Message " + msg + " from UEB cluster : " + uebURLList.toString()); + resultJson = msg; + if (!msg.contains("UEB Update")){ + notification = NotificationUnMarshal.notificationJSON(msg); + return true; + } + } + }catch (Exception e) { + logger.error("Error in Manual CLient UEB notification ", e); + } + return false; + } } diff --git a/PolicyEngineAPI/src/main/java/org/onap/policy/std/StdPolicyEngine.java b/PolicyEngineAPI/src/main/java/org/onap/policy/std/StdPolicyEngine.java index f09b5779c..8f3cf043a 100644 --- a/PolicyEngineAPI/src/main/java/org/onap/policy/std/StdPolicyEngine.java +++ b/PolicyEngineAPI/src/main/java/org/onap/policy/std/StdPolicyEngine.java @@ -3,6 +3,7 @@ * PolicyEngineAPI * ================================================================================ * Copyright (C) 2017-2018 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. @@ -348,7 +349,7 @@ public class StdPolicyEngine { return deletePolicyImpl(parameters); } - public PolicyChangeResponse deletePolicyImpl(final DeletePolicyParameters parameters) throws PolicyException { + private PolicyChangeResponse deletePolicyImpl(final DeletePolicyParameters parameters) throws PolicyException { final StdPolicyChangeResponse response = new StdPolicyChangeResponse(); String body = null; // Create Request. @@ -379,7 +380,7 @@ public class StdPolicyEngine { return getDictionaryItemImpl(parameters); } - public DictionaryResponse getDictionaryItemImpl(final DictionaryParameters parameters) throws PolicyException { + private DictionaryResponse getDictionaryItemImpl(final DictionaryParameters parameters) throws PolicyException { final StdDictionaryResponse response = new StdDictionaryResponse(); String body = "{}"; // Create Request. @@ -450,7 +451,7 @@ public class StdPolicyEngine { return createUpdateDictionaryItemImpl(parameters, true); } - public PolicyChangeResponse createUpdateDictionaryItemImpl(final DictionaryParameters parameters, + private PolicyChangeResponse createUpdateDictionaryItemImpl(final DictionaryParameters parameters, final boolean updateFlag) throws PolicyException { final String resource = getDictionaryResouceName(updateFlag); @@ -487,7 +488,7 @@ public class StdPolicyEngine { return policyEngineImportImpl(importParameters); } - public PolicyChangeResponse policyEngineImportImpl(final ImportParameters importParameters) throws PolicyException { + private PolicyChangeResponse policyEngineImportImpl(final ImportParameters importParameters) throws PolicyException { final LinkedMultiValueMap parameters = new LinkedMultiValueMap<>(); // Create Request. try { @@ -530,7 +531,7 @@ public class StdPolicyEngine { return createUpdatePolicyImpl(policyParameters, true); } - public PolicyChangeResponse createUpdatePolicyImpl(final PolicyParameters policyParameters, + private PolicyChangeResponse createUpdatePolicyImpl(final PolicyParameters policyParameters, final boolean updateFlag) throws PolicyException { final String resource = getPolicyResourceName(updateFlag); String body = null; @@ -575,7 +576,7 @@ public class StdPolicyEngine { } } - public DecisionResponse getDecisionImpl(final String onapName, final Map decisionAttributes, + private DecisionResponse getDecisionImpl(final String onapName, final Map decisionAttributes, final UUID requestID) throws PolicyDecisionException { String body = null; // Create Request. @@ -604,7 +605,7 @@ public class StdPolicyEngine { } } - public Collection getConfigImpl(final ConfigRequestParameters configRequestParameters) + private Collection getConfigImpl(final ConfigRequestParameters configRequestParameters) throws PolicyConfigException { String body = null; // Create Request. @@ -632,51 +633,53 @@ public class StdPolicyEngine { private ArrayList configResult(final APIPolicyConfigResponse[] response) throws PolicyConfigException { final ArrayList result = new ArrayList<>(); - if (response != null) { - for (final APIPolicyConfigResponse policyConfigResponse : response) { - final StdPolicyConfig policyConfig = new StdPolicyConfig(); - policyConfig.setConfigStatus(policyConfigResponse.getPolicyConfigMessage()); - policyConfig.setMatchingConditions(policyConfigResponse.getMatchingConditions()); - policyConfig.setPolicyConfigStatus(policyConfigResponse.getPolicyConfigStatus()); - policyConfig.setPolicyName(policyConfigResponse.getPolicyName()); - policyConfig.setPolicyType(policyConfigResponse.getType()); - policyConfig.setPolicyVersion(policyConfigResponse.getPolicyVersion()); - policyConfig.setPolicyType(policyConfigResponse.getPolicyType()); - policyConfig.setResponseAttributes(policyConfigResponse.getResponseAttributes()); - setMatches(policyConfig.getMatchingConditions()); - if (policyConfigResponse.getType() != null) { - try { - switch (policyConfigResponse.getType()) { - case JSON: - final StringReader reader = new StringReader(policyConfigResponse.getConfig()); - try (final JsonReader jsonReader = Json.createReader(reader)) { - final JsonObject object = jsonReader.readObject(); - policyConfig.setJsonObject(object); - } - break; - case OTHER: - policyConfig.setOther(policyConfigResponse.getConfig()); - break; - case PROPERTIES: - final Properties props = new Properties(); - props.putAll(policyConfigResponse.getProperty()); - policyConfig.setProperties(props); - break; - case XML: - final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - final DocumentBuilder builder = factory.newDocumentBuilder(); - final StringReader stringReader = new StringReader(policyConfigResponse.getConfig()); - policyConfig.setDocument(builder.parse(new InputSource(stringReader))); - break; - } - } catch (final Exception exception) { - LOGGER.error(XACMLErrorConstants.ERROR_SCHEMA_INVALID + exception); - throw new PolicyConfigException( - XACMLErrorConstants.ERROR_SCHEMA_INVALID + "Unable to parse the config", exception); + if (response == null) { + return result; + } + + for (final APIPolicyConfigResponse policyConfigResponse : response) { + final StdPolicyConfig policyConfig = new StdPolicyConfig(); + policyConfig.setConfigStatus(policyConfigResponse.getPolicyConfigMessage()); + policyConfig.setMatchingConditions(policyConfigResponse.getMatchingConditions()); + policyConfig.setPolicyConfigStatus(policyConfigResponse.getPolicyConfigStatus()); + policyConfig.setPolicyName(policyConfigResponse.getPolicyName()); + policyConfig.setPolicyType(policyConfigResponse.getType()); + policyConfig.setPolicyVersion(policyConfigResponse.getPolicyVersion()); + policyConfig.setPolicyType(policyConfigResponse.getPolicyType()); + policyConfig.setResponseAttributes(policyConfigResponse.getResponseAttributes()); + setMatches(policyConfig.getMatchingConditions()); + if (policyConfigResponse.getType() != null) { + try { + switch (policyConfigResponse.getType()) { + case JSON: + final StringReader reader = new StringReader(policyConfigResponse.getConfig()); + try (final JsonReader jsonReader = Json.createReader(reader)) { + final JsonObject object = jsonReader.readObject(); + policyConfig.setJsonObject(object); + } + break; + case OTHER: + policyConfig.setOther(policyConfigResponse.getConfig()); + break; + case PROPERTIES: + final Properties props = new Properties(); + props.putAll(policyConfigResponse.getProperty()); + policyConfig.setProperties(props); + break; + case XML: + final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + final DocumentBuilder builder = factory.newDocumentBuilder(); + final StringReader stringReader = new StringReader(policyConfigResponse.getConfig()); + policyConfig.setDocument(builder.parse(new InputSource(stringReader))); + break; } + } catch (final Exception exception) { + LOGGER.error(XACMLErrorConstants.ERROR_SCHEMA_INVALID + exception); + throw new PolicyConfigException( + XACMLErrorConstants.ERROR_SCHEMA_INVALID + "Unable to parse the config", exception); } - result.add(policyConfig); } + result.add(policyConfig); } return result; } @@ -774,7 +777,7 @@ public class StdPolicyEngine { clientEncoding = encoder.encodeToString((userName + ":" + pass).getBytes(StandardCharsets.UTF_8)); } - public Collection listConfigImpl(final ConfigRequestParameters listRequestParameters) + private Collection listConfigImpl(final ConfigRequestParameters listRequestParameters) throws PolicyConfigException { final Collection policyList = new ArrayList<>(); if (junit) { @@ -792,7 +795,7 @@ public class StdPolicyEngine { return policyList; } - public Collection sendEventImpl(final Map eventAttributes, final UUID requestID) + private Collection sendEventImpl(final Map eventAttributes, final UUID requestID) throws PolicyEventException { String body = null; // Create Request. @@ -864,7 +867,7 @@ public class StdPolicyEngine { apiKey = properties.getProperty(UEB_API_KEY_PROP_NAME); apiSecret = properties.getProperty(UEB_API_SECRET_PROP_NAME); - setNotificationType(notificationTypeValue, DEFAULT_NOTIFICATION); + setNotificationType(notificationTypeValue); if (serverList == null) { notificationType.clear(); @@ -872,7 +875,7 @@ public class StdPolicyEngine { LOGGER.info( "Properties file doesn't have the NOTIFICATION_SERVERS parameter system will use defualt websockets"); } else { - notificationURLList = getPropertyValueAsList(serverList.trim(), COMMA); + notificationURLList = getPropertyValueAsList(serverList.trim()); } if (topic != null) { @@ -907,19 +910,7 @@ public class StdPolicyEngine { // Check the Keys for PDP_URLs for (final String propertyKey : prop.stringPropertyNames()) { if (propertyKey.startsWith(PDP_URL_PROP_NAME)) { - final String propertyValue = prop.getProperty(propertyKey); - if (propertyValue == null) { - throw new PolicyEngineException(XACMLErrorConstants.ERROR_DATA_ISSUE - + "Properties file doesn't have the PDP_URL parameter"); - } - if (propertyValue.contains(SEMICOLLON)) { - final List pdpDefault = Arrays.asList(propertyValue.split(REGEX)); - for (final String pdpVal : pdpDefault) { - readPDPParam(pdpVal); - } - } else { - readPDPParam(propertyValue); - } + readPDPPropertyURL(prop, propertyKey); } } if (pdps == null || pdps.isEmpty()) { @@ -928,13 +919,29 @@ public class StdPolicyEngine { } } - private void setNotificationType(final String propertyValue, final String defaultValue) { + private void readPDPPropertyURL(Properties prop, String propertyKey) throws PolicyEngineException { + final String propertyValue = prop.getProperty(propertyKey); + if (propertyValue == null) { + throw new PolicyEngineException(XACMLErrorConstants.ERROR_DATA_ISSUE + + "Properties file doesn't have the PDP_URL parameter"); + } + if (propertyValue.contains(SEMICOLLON)) { + final List pdpDefault = Arrays.asList(propertyValue.split(REGEX)); + for (final String pdpVal : pdpDefault) { + readPDPParam(pdpVal); + } + } else { + readPDPParam(propertyValue); + } + } + + private void setNotificationType(final String propertyValue) { if (propertyValue == null) { - notificationType.add(defaultValue); + notificationType.add(DEFAULT_NOTIFICATION); LOGGER.info( "Properties file doesn't have the NOTIFICATION_TYPE parameter system will use defualt websockets"); } else { - notificationType = getPropertyValueAsList(propertyValue.trim(), COMMA); + notificationType = getPropertyValueAsList(propertyValue.trim()); } } @@ -992,9 +999,9 @@ public class StdPolicyEngine { } } - private List getPropertyValueAsList(final String propertyValue, final String split) { - if (propertyValue.contains(split)) { - return Arrays.asList(propertyValue.split(split)); + private List getPropertyValueAsList(final String propertyValue) { + if (propertyValue.contains(COMMA)) { + return Arrays.asList(propertyValue.split(COMMA)); } final List valuesList = new ArrayList<>(); valuesList.add(propertyValue); @@ -1087,28 +1094,29 @@ public class StdPolicyEngine { if (junit) { return; } + if (pdps == null) { + return; + } - if (pdps != null) { - if (UEB.equals(notificationType.get(0)) && !this.uebThread) { - this.uebClientThread = new AutoClientUEB(pdps.get(0), notificationURLList, apiKey, apiSecret); - AutoClientUEB.setAuto(scheme, handler); - this.registerUEBThread = new Thread(this.uebClientThread); - this.registerUEBThread.start(); - this.uebThread = true; - } else if (notificationType.get(0).equals(DMAAP) && !this.dmaapThread) { - this.dmaapClientThread = new AutoClientDMAAP(notificationURLList, topic, userName, pass); - AutoClientDMAAP.setAuto(scheme, handler); - this.registerDMAAPThread = new Thread(this.dmaapClientThread); - this.registerDMAAPThread.start(); - this.dmaapThread = true; - } else { - if (pdps.get(0) != null) { - if (AutoClientEnd.getUrl() == null) { - AutoClientEnd.start(pdps.get(0)); - } else { - AutoClientEnd.stop(); - AutoClientEnd.start(pdps.get(0)); - } + if (UEB.equals(notificationType.get(0)) && !this.uebThread) { + this.uebClientThread = new AutoClientUEB(pdps.get(0), notificationURLList, apiKey, apiSecret); + AutoClientUEB.setAuto(scheme, handler); + this.registerUEBThread = new Thread(this.uebClientThread); + this.registerUEBThread.start(); + this.uebThread = true; + } else if (notificationType.get(0).equals(DMAAP) && !this.dmaapThread) { + this.dmaapClientThread = new AutoClientDMAAP(notificationURLList, topic, userName, pass); + AutoClientDMAAP.setAuto(scheme, handler); + this.registerDMAAPThread = new Thread(this.dmaapClientThread); + this.registerDMAAPThread.start(); + this.dmaapThread = true; + } else { + if (pdps.get(0) != null) { + if (AutoClientEnd.getUrl() == null) { + AutoClientEnd.start(pdps.get(0)); + } else { + AutoClientEnd.stop(); + AutoClientEnd.start(pdps.get(0)); } } } @@ -1227,7 +1235,7 @@ public class StdPolicyEngine { /* * Create Config Policy API Implementation */ - public String createUpdateConfigPolicyImpl(final String policyName, final String policyDescription, + private String createUpdateConfigPolicyImpl(final String policyName, final String policyDescription, final String onapName, final String configName, final Map configAttributes, final String configType, final String body, final String policyScope, final UUID requestID, final String riskLevel, final String riskType, final String guard, final String ttlDate, @@ -1276,7 +1284,7 @@ public class StdPolicyEngine { /* * Create Update Config Firewall Policy API implementation */ - public String createUpdateConfigFirewallPolicyImpl(final String policyName, final JsonObject firewallJson, + private String createUpdateConfigFirewallPolicyImpl(final String policyName, final JsonObject firewallJson, final String policyScope, final UUID requestID, final String riskLevel, final String riskType, final String guard, final String ttlDate, final boolean updateFlag) throws PolicyException { validateParameters(policyName, policyScope); 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 24e126386..6164418d9 100644 --- a/PolicyEngineUtils/src/main/java/org/onap/policy/std/NotificationStore.java +++ b/PolicyEngineUtils/src/main/java/org/onap/policy/std/NotificationStore.java @@ -3,6 +3,7 @@ * PolicyEngineUtils * ================================================================================ * Copyright (C) 2017 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. @@ -41,8 +42,8 @@ public class NotificationStore { private static StdPDPNotification notificationRecord = new StdPDPNotification(); private NotificationStore () { - // Sonar prefers that we have an empty public constructor - // as opposed to an implicit public constructor. + // Sonar prefers that we have an empty public constructor + // as opposed to an implicit public constructor. } public static StdPDPNotification getDeltaNotification(StdPDPNotification newNotification) { @@ -63,195 +64,218 @@ public class NotificationStore { } return notificationDelta; } + + if (newNotification == null) { + return notificationDelta; + } // do the Delta operation. - if (newNotification != null) { - // check for old removed policies. - if (!newNotification.getRemovedPolicies().isEmpty()) { - for (RemovedPolicy newRemovedPolicy : newNotification.getRemovedPolicies()) { - //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())) { - removed = false; - // Don't want a duplicate. - oldRemovedPolicies.remove(oldRemovedPolicy); - } - } - //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())) { - oldUpdatedPolicies.remove(oldUpdatedPolicy); - oldUpdatedLostPolicies.remove(oldUpdatedPolicy); - } - } - if (removed) { - remove = true; - notificationRecord.getRemovedPolicies().add(newRemovedPolicy); - removedDelta.add((StdRemovedPolicy) newRemovedPolicy); - } - // This will be converted to New Later. - oldRemovedPolicies.add(newRemovedPolicy); - } + // check for old removed policies. + if (!newNotification.getRemovedPolicies().isEmpty()) { + for (RemovedPolicy newRemovedPolicy : newNotification.getRemovedPolicies()) { + remove = updateRemovedPolicies(removedDelta, oldUpdatedLostPolicies, oldRemovedPolicies, oldUpdatedPolicies, remove, newRemovedPolicy); } - // Check for old Updated Policies. - if (!newNotification.getLoadedPolicies().isEmpty()) { - for (LoadedPolicy newUpdatedPolicy : newNotification.getLoadedPolicies()) { - // 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())) { - updated = false; - // Remove the policy from copy. - oldUpdatedLostPolicies.remove(oldUpdatedPolicy); - // Eliminating Duplicate. - oldUpdatedPolicies.remove(oldUpdatedPolicy); - } - } - // Change the record if the policy has been Removed earlier. - for (RemovedPolicy oldRemovedPolicy : notificationRecord.getRemovedPolicies()) { - if (oldRemovedPolicy.getPolicyName().equals(policyName) - && oldRemovedPolicy.getVersionNo().equals(ver)) { - oldRemovedPolicies.remove(oldRemovedPolicy); - } - } - if (updated) { - update = true; - updatedDelta.add((StdLoadedPolicy) newUpdatedPolicy); - } - // This will be converted to new Later - oldUpdatedPolicies.add(newUpdatedPolicy); - } - // Conversion of Update to Remove if that occurred. - if (!oldUpdatedLostPolicies.isEmpty()) { - for (LoadedPolicy updatedPolicy : oldUpdatedLostPolicies) { - StdRemovedPolicy removedPolicy = new StdRemovedPolicy(); - removedPolicy.setPolicyName(updatedPolicy.getPolicyName()); - removedPolicy.setVersionNo(updatedPolicy.getVersionNo()); - removedDelta.add(removedPolicy); - remove = true; - } - } + } + // Check for old Updated Policies. + if (!newNotification.getLoadedPolicies().isEmpty()) { + for (LoadedPolicy newUpdatedPolicy : newNotification.getLoadedPolicies()) { + update = modifyUpdatedPolicies(updatedDelta, oldUpdatedLostPolicies, oldRemovedPolicies, oldUpdatedPolicies, update, newUpdatedPolicy); } - // Update our Record. - if (!oldUpdatedPolicies.isEmpty()) { - for (LoadedPolicy updatedPolicy : oldUpdatedPolicies) { - newUpdatedPolicies.add((StdLoadedPolicy) updatedPolicy); + // Conversion of Update to Remove if that occurred. + if (!oldUpdatedLostPolicies.isEmpty()) { + for (LoadedPolicy updatedPolicy : oldUpdatedLostPolicies) { + StdRemovedPolicy removedPolicy = new StdRemovedPolicy(); + removedPolicy.setPolicyName(updatedPolicy.getPolicyName()); + removedPolicy.setVersionNo(updatedPolicy.getVersionNo()); + removedDelta.add(removedPolicy); + remove = true; } } - if (!oldRemovedPolicies.isEmpty()) { - for (RemovedPolicy removedPolicy : oldRemovedPolicies) { - newRemovedPolicies.add((StdRemovedPolicy) removedPolicy); - } + } + // Update our Record. + if (!oldUpdatedPolicies.isEmpty()) { + for (LoadedPolicy updatedPolicy : oldUpdatedPolicies) { + newUpdatedPolicies.add((StdLoadedPolicy) updatedPolicy); } - notificationRecord.setRemovedPolicies(newRemovedPolicies); - notificationRecord.setLoadedPolicies(newUpdatedPolicies); - // Update the notification Result. - notificationDelta.setRemovedPolicies(removedDelta); - notificationDelta.setLoadedPolicies(updatedDelta); - if (remove && update) { - notificationDelta.setNotificationType(NotificationType.BOTH); - } else if (remove) { - notificationDelta.setNotificationType(NotificationType.REMOVE); - } else if (update) { - notificationDelta.setNotificationType(NotificationType.UPDATE); + } + if (!oldRemovedPolicies.isEmpty()) { + for (RemovedPolicy removedPolicy : oldRemovedPolicies) { + newRemovedPolicies.add((StdRemovedPolicy) removedPolicy); } } + notificationRecord.setRemovedPolicies(newRemovedPolicies); + notificationRecord.setLoadedPolicies(newUpdatedPolicies); + // Update the notification Result. + notificationDelta.setRemovedPolicies(removedDelta); + notificationDelta.setLoadedPolicies(updatedDelta); + if (remove && update) { + notificationDelta.setNotificationType(NotificationType.BOTH); + } else if (remove) { + notificationDelta.setNotificationType(NotificationType.REMOVE); + } else if (update) { + notificationDelta.setNotificationType(NotificationType.UPDATE); + } + return notificationDelta; } + 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())) { + updated = false; + // Remove the policy from copy. + oldUpdatedLostPolicies.remove(oldUpdatedPolicy); + // Eliminating Duplicate. + oldUpdatedPolicies.remove(oldUpdatedPolicy); + } + } + // Change the record if the policy has been Removed earlier. + for (RemovedPolicy oldRemovedPolicy : notificationRecord.getRemovedPolicies()) { + if (oldRemovedPolicy.getPolicyName().equals(policyName) + && oldRemovedPolicy.getVersionNo().equals(ver)) { + oldRemovedPolicies.remove(oldRemovedPolicy); + } + } + if (updated) { + update = true; + updatedDelta.add((StdLoadedPolicy) newUpdatedPolicy); + } + // This will be converted to new Later + oldUpdatedPolicies.add(newUpdatedPolicy); + return update; + } + + 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())) { + removed = false; + // Don't want a duplicate. + oldRemovedPolicies.remove(oldRemovedPolicy); + } + } + //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())) { + oldUpdatedPolicies.remove(oldUpdatedPolicy); + oldUpdatedLostPolicies.remove(oldUpdatedPolicy); + } + } + if (removed) { + remove = true; + notificationRecord.getRemovedPolicies().add(newRemovedPolicy); + removedDelta.add((StdRemovedPolicy) newRemovedPolicy); + } + // This will be converted to New Later. + oldRemovedPolicies.add(newRemovedPolicy); + return remove; + } + public static void recordNotification(StdPDPNotification notification) { - if (notification != null) { - 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); - } + if (notification == null) { + return; + } - // Checking with the new updated policies. - if (notification.getLoadedPolicies() != null && !notification.getLoadedPolicies().isEmpty()) { - for (LoadedPolicy newUpdatedPolicy : notification.getLoadedPolicies()) { - // If it was removed earlier then we need to remove from our record - Iterator oldRemovedPolicy = removedPolicies.iterator(); - String policyName = newUpdatedPolicy.getPolicyName(); - String ver = newUpdatedPolicy.getVersionNo(); - while (oldRemovedPolicy.hasNext()) { - RemovedPolicy policy = oldRemovedPolicy.next(); - 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())); + 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); + } - StdLoadedPolicy sUPolicy = new StdLoadedPolicy(); - sUPolicy.setMatches(newUpdatedPolicy.getMatches()); - sUPolicy.setPolicyName(newUpdatedPolicy.getPolicyName()); - sUPolicy.setVersionNo(newUpdatedPolicy.getVersionNo()); - sUPolicy.setUpdateType(newUpdatedPolicy.getUpdateType()); - updatedPolicies.add(sUPolicy); - } - } - // Checking with New Removed Policies. - if (notification.getRemovedPolicies() != null && !notification.getRemovedPolicies().isEmpty()) { - for (RemovedPolicy newRemovedPolicy : notification.getRemovedPolicies()) { - // If it was previously removed Overwrite it to the record. - Iterator oldRemovedPolicy = removedPolicies.iterator(); - String policyName = newRemovedPolicy.getPolicyName(); - String ver = newRemovedPolicy.getVersionNo(); - while (oldRemovedPolicy.hasNext()) { - RemovedPolicy policy = oldRemovedPolicy.next(); - 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())); + // 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); + } + 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); + } + } + } - StdRemovedPolicy sRPolicy = new StdRemovedPolicy(); - sRPolicy.setPolicyName(policyName); - sRPolicy.setVersionNo(ver); - removedPolicies.add(sRPolicy); - } - } - notificationRecord.setRemovedPolicies(removedPolicies); - notificationRecord.setLoadedPolicies(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(); + String policyName = newUpdatedPolicy.getPolicyName(); + String ver = newUpdatedPolicy.getVersionNo(); + while (oldRemovedPolicy.hasNext()) { + RemovedPolicy policy = oldRemovedPolicy.next(); + if (policyName.equals(policy.getPolicyName()) + && ver.equals(policy.getVersionNo())) { + oldRemovedPolicy.remove(); } - 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); + } + // If it was previously updated need to Overwrite it to the record. + 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); + } + } + + 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(); + String policyName = newRemovedPolicy.getPolicyName(); + String ver = newRemovedPolicy.getVersionNo(); + while (oldRemovedPolicy.hasNext()) { + RemovedPolicy policy = oldRemovedPolicy.next(); + 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())); + + StdRemovedPolicy sRPolicy = new StdRemovedPolicy(); + sRPolicy.setPolicyName(policyName); + sRPolicy.setVersionNo(ver); + removedPolicies.add(sRPolicy); } }