2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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;
24 import javax.websocket.ClientEndpoint;
25 import org.java_websocket.client.WebSocketClient;
26 import org.java_websocket.handshake.ServerHandshake;
27 import org.onap.policy.api.NotificationHandler;
28 import org.onap.policy.api.NotificationScheme;
29 import org.onap.policy.api.NotificationType;
30 import org.onap.policy.api.PDPNotification;
31 import org.onap.policy.common.logging.flexlogger.FlexLogger;
32 import org.onap.policy.common.logging.flexlogger.Logger;
33 import org.onap.policy.xacml.api.XACMLErrorConstants;
36 public class AutoClientEnd extends WebSocketClient {
37 private static StdPDPNotification notification = null;
38 private static StdPDPNotification oldNotification = null;
39 private static AutoClientEnd client = null;
40 private static NotificationScheme scheme = null;
41 private static NotificationHandler handler = null;
42 private static String url = null;
43 private static boolean status = false;
44 private static boolean stop = false;
45 private static boolean message = false;
46 private static boolean error = false;
47 private static Logger logger = FlexLogger.getLogger(AutoClientEnd.class.getName());
49 private AutoClientEnd(URI serverUri) {
54 public void onMessage(String msg) {
55 logger.info("Received Auto Notification from : " + getURI() + ", Notification: " + msg);
56 AutoClientEnd.message = true;
58 AutoClientEnd.notification = NotificationUnMarshal.notificationJSON(msg);
59 } catch (Exception e) {
60 logger.error("PE500 " + e);
63 NotificationStore.recordNotification(notification);
64 } catch (Exception e) {
67 if (AutoClientEnd.oldNotification != AutoClientEnd.notification) {
68 AutoClientEnd.oldNotification = AutoClientEnd.notification;
72 AutoClientEnd.message = false;
76 public void onClose(int code, String reason, boolean remote) {
77 logger.info("AutoClientEnd disconnected from: " + getURI() + "; Code: " + code + ", reason : " + reason);
78 if (!AutoClientEnd.stop && !AutoClientEnd.message) {
79 // This Block of code is executed if there is any Network Failure or
80 // if the Notification is Down.
81 logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Disconnected from Notification Server");
82 AutoClientEnd.client = null;
83 AutoClientEnd.status = false;
84 // Try to connect Back to available PDP.
85 AutoClientEnd.error = true;
88 AutoClientEnd.message = false;
92 public void onError(Exception ex) {
93 logger.error("XACMLErrorConstants.ERROR_PROCESS_FLOW + Error connecting to: " + getURI()
94 + ", Exception occured ...\n" + ex);
95 // trying to Restart by self.
97 if (AutoClientEnd.url != null) {
98 AutoClientEnd.client = null;
99 AutoClientEnd.status = false;
100 AutoClientEnd.error = true;
101 AutoClientEnd.start(AutoClientEnd.url);
106 public void onOpen(ServerHandshake arg0) {
107 logger.info("Auto Notification Session Started... " + getURI());
113 * @param scheme the scheme
114 * @param handler the handler
116 public static void setAuto(NotificationScheme scheme, NotificationHandler handler) {
117 logger.info("Auto Notification setAuto, scheme: " + scheme);
118 AutoClientEnd.scheme = scheme;
119 AutoClientEnd.handler = handler;
122 public static void setScheme(NotificationScheme scheme) {
123 AutoClientEnd.scheme = scheme;
126 public static boolean getStatus() {
127 return AutoClientEnd.status;
130 public static String getUrl() {
131 return AutoClientEnd.url;
139 public static void start(String url) {
140 AutoClientEnd.url = url;
142 if (scheme == null || handler == null || !(scheme.equals(NotificationScheme.AUTO_ALL_NOTIFICATIONS)
143 || scheme.equals(NotificationScheme.AUTO_NOTIFICATIONS)) || AutoClientEnd.client != null) {
147 if (url.contains("https")) {
148 url = url.replaceAll("https", "wss");
150 url = url.replaceAll("http", "ws");
153 // Stop and Start needs to be done.
155 logger.info("Starting Auto Notification with the PDP server : " + url);
156 client = new AutoClientEnd(new URI(url + "notifications"));
160 // will not trigger. leave it in to later add checks
161 // The URL's will be in Sync according to design Spec.
162 ManualClientEnd.start(AutoClientEnd.url);
163 StdPDPNotification notification = NotificationStore.getDeltaNotification(
164 (StdPDPNotification) ManualClientEnd.result(NotificationScheme.MANUAL_ALL_NOTIFICATIONS));
165 if (notification.getNotificationType() != null && oldNotification != notification) {
166 oldNotification = notification;
167 AutoClientEnd.notification = notification;
172 } catch (Exception e) {
173 logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
180 private static void changeUrl() {
181 // Change the PDP if it is not Up.
182 StdPolicyEngine.rotatePDPList();
183 start(StdPolicyEngine.getPDPURL());
187 * Stop the websocket connection.
189 public static void stop() {
190 if (client == null) {
193 logger.info("\n Closing Auto Notification WebSocket Connection.. ");
196 client.closeBlocking();
197 } catch (InterruptedException e) {
198 logger.info("\n Error Closing Auto Notification WebSocket Connection.. InterruptedException");
200 logger.info("\n Closed the Auto Notification WebSocket Connection.. ");
206 private static void callHandler() {
207 if (handler == null || scheme == null) {
210 if (scheme.equals(NotificationScheme.AUTO_ALL_NOTIFICATIONS)) {
211 boolean removed = false;
212 boolean updated = false;
213 if (notification.getRemovedPolicies() != null && !notification.getRemovedPolicies().isEmpty()) {
215 notification.setNotificationType(NotificationType.REMOVE);
217 if (notification.getLoadedPolicies() != null && !notification.getLoadedPolicies().isEmpty()) {
219 notification.setNotificationType(NotificationType.UPDATE);
221 if (removed && updated) {
222 notification.setNotificationType(NotificationType.BOTH);
225 handler.notificationReceived(notification);
226 } catch (Exception e) {
227 logger.error("Error in Clients Handler Object : ", e);
229 } else if (scheme.equals(NotificationScheme.AUTO_NOTIFICATIONS)) {
230 PDPNotification newNotification = MatchStore.checkMatch(notification);
231 if (newNotification.getNotificationType() != null) {
233 handler.notificationReceived(newNotification);
234 } catch (Exception e) {
235 logger.error("Error in Clients Handler Object : ", e);