X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=PolicyEngineAPI%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fstd%2FAutoClientEnd.java;h=e69f007d8304e93f4ddedd577c10b216467f49d0;hb=23c1e146852b43841c666aca584123dec896167e;hp=f0d99d09eb814e5a509f8cd81294be3dfc4ed106;hpb=80f072f60509ef3a35369a60857fe05f6c2a993a;p=policy%2Fengine.git diff --git a/PolicyEngineAPI/src/main/java/org/onap/policy/std/AutoClientEnd.java b/PolicyEngineAPI/src/main/java/org/onap/policy/std/AutoClientEnd.java index f0d99d09e..e69f007d8 100644 --- a/PolicyEngineAPI/src/main/java/org/onap/policy/std/AutoClientEnd.java +++ b/PolicyEngineAPI/src/main/java/org/onap/policy/std/AutoClientEnd.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * PolicyEngineAPI * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -56,6 +56,10 @@ public class AutoClientEnd { private static boolean error = false; private static Logger logger = FlexLogger.getLogger(AutoClientEnd.class.getName()); + private AutoClientEnd() { + // Empty constructor + } + public static void setAuto(NotificationScheme scheme, NotificationHandler handler) { AutoClientEnd.scheme = scheme; @@ -76,40 +80,42 @@ public class AutoClientEnd { public static void start(String url) { AutoClientEnd.url = url; + + if (scheme == null || handler == null || + ! (scheme.equals(NotificationScheme.AUTO_ALL_NOTIFICATIONS) || + scheme.equals(NotificationScheme.AUTO_NOTIFICATIONS) ) || + AutoClientEnd.client != null) { + return; + } + // Stop and Start needs to be done. - if (scheme != null && handler!=null) { - if (scheme.equals(NotificationScheme.AUTO_ALL_NOTIFICATIONS) || scheme.equals(NotificationScheme.AUTO_NOTIFICATIONS)) { - if (AutoClientEnd.client == null) { - client = ClientManager.createClient(); - if(url.contains("https")){ - url = url.replaceAll("https", "wss"); - }else { - url = url.replaceAll("http", "ws"); - } - try { - logger.info("Starting Auto Notification with the PDP server : " + url); - client.connectToServer(AutoClientEnd.class, new URI(url + "notifications")); - status = true; - if(error){ - // The URL's will be in Sync according to design Spec. - ManualClientEnd.start(AutoClientEnd.url); - StdPDPNotification notification = NotificationStore.getDeltaNotification((StdPDPNotification)ManualClientEnd.result(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)); - if(notification.getNotificationType()!=null&&oldNotification!=notification){ - oldNotification= notification; - AutoClientEnd.notification = notification; - callHandler(); - } - error = false; - } - // - } catch (DeploymentException | IOException | URISyntaxException e) { - logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e); - client = null; - status = false; - changeURL(); - } + client = ClientManager.createClient(); + if(url.contains("https")){ + url = url.replaceAll("https", "wss"); + }else { + url = url.replaceAll("http", "ws"); + } + try { + logger.info("Starting Auto Notification with the PDP server : " + url); + client.connectToServer(AutoClientEnd.class, new URI(url + "notifications")); + status = true; + if(error){ + // The URL's will be in Sync according to design Spec. + ManualClientEnd.start(AutoClientEnd.url); + StdPDPNotification notification = NotificationStore.getDeltaNotification((StdPDPNotification)ManualClientEnd.result(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)); + if(notification.getNotificationType()!=null&&oldNotification!=notification){ + oldNotification= notification; + AutoClientEnd.notification = notification; + callHandler(); } + error = false; } + // + } catch (DeploymentException | IOException | URISyntaxException e) { + logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e); + client = null; + status = false; + changeURL(); } } @@ -120,56 +126,58 @@ public class AutoClientEnd { } public static void stop() { - if (client != null) { - client.shutdown(); - if(session!=null){ - try { - stop = true; - logger.info("\n Closing Auto Notification WebSocket Connection.. "); - session.close(); - session = null; - } catch (IOException e) { - logger.error("Error closing websocket connection", e); - } + if (client == null) { + return; + } + client.shutdown(); + if(session!=null){ + try { + stop = true; + logger.info("\n Closing Auto Notification WebSocket Connection.. "); + session.close(); + session = null; + } catch (IOException e) { + logger.error("Error closing websocket connection", e); } - client = null; - status = false; - stop = false; } + client = null; + status = false; + stop = false; } private static void callHandler() { - if (handler != null && scheme != null) { - if (scheme.equals(NotificationScheme.AUTO_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); - } + if (handler == null || scheme == null) { + return; + } + if (scheme.equals(NotificationScheme.AUTO_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); + } + try{ + handler.notificationReceived(notification); + }catch (Exception e){ + logger.error("Error in Clients Handler Object : ", e); + } + } else if (scheme.equals(NotificationScheme.AUTO_NOTIFICATIONS)) { + PDPNotification newNotification = MatchStore.checkMatch(notification); + if (newNotification.getNotificationType() != null) { try{ - handler.notificationReceived(notification); + handler.notificationReceived(newNotification); }catch (Exception e){ logger.error("Error in Clients Handler Object : ", e); } - } else if (scheme.equals(NotificationScheme.AUTO_NOTIFICATIONS)) { - PDPNotification newNotification = MatchStore.checkMatch(notification); - if (newNotification.getNotificationType() != null) { - try{ - handler.notificationReceived(newNotification); - }catch (Exception e){ - logger.error("Error in Clients Handler Object : ", e); - } - } } } }