X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=PolicyEngineAPI%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fstd%2FAutoClientEnd.java;h=3f97e19f7b379cff5e18f7a3468c6b755b091421;hb=cfd1160833ecb24c336fe6d0d197547c36ce2327;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..3f97e19f7 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. @@ -22,17 +22,16 @@ package org.onap.policy.std; import java.io.IOException; import java.net.URI; -import java.net.URISyntaxException; import javax.websocket.ClientEndpoint; -import javax.websocket.DeploymentException; import javax.websocket.OnClose; import javax.websocket.OnError; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; -import org.glassfish.tyrus.client.ClientManager; +import org.java_websocket.client.WebSocketClient; +import org.java_websocket.handshake.ServerHandshake; import org.onap.policy.api.NotificationHandler; import org.onap.policy.api.NotificationScheme; import org.onap.policy.api.NotificationType; @@ -42,10 +41,10 @@ import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.xacml.api.XACMLErrorConstants; @ClientEndpoint -public class AutoClientEnd { +public class AutoClientEnd extends WebSocketClient { private static StdPDPNotification notification = null; private static StdPDPNotification oldNotification = null; - private static ClientManager client = null; + private static AutoClientEnd client = null; private static NotificationScheme scheme = null; private static NotificationHandler handler = null; private static String url = null; @@ -56,6 +55,30 @@ public class AutoClientEnd { private static boolean error = false; private static Logger logger = FlexLogger.getLogger(AutoClientEnd.class.getName()); + private AutoClientEnd(URI serverUri) { + super(serverUri); + } + + @Override + public void onClose(int arg0, String arg1, boolean arg2) { + // Not implemented + } + + @Override + public void onError(Exception arg0) { + // Not implemented + } + + @Override + public void onMessage(String arg0) { + // Not implemented + } + + @Override + public void onOpen(ServerHandshake arg0) { + // Not implemented + } + public static void setAuto(NotificationScheme scheme, NotificationHandler handler) { AutoClientEnd.scheme = scheme; @@ -76,40 +99,44 @@ 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; + } + + if (url.contains("https")) { + url = url.replaceAll("https", "wss"); + } + else { + url = url.replaceAll("http", "ws"); + } + + // 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(); - } + try { + logger.info("Starting Auto Notification with the PDP server : " + url); + client = new AutoClientEnd(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 (Exception e) { + logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e); + client = null; + status = false; + changeURL(); } } @@ -120,56 +147,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.close(); + 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); - } - } } } }