2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 java.util.concurrent.CountDownLatch;
25 import javax.websocket.ClientEndpoint;
26 import org.java_websocket.client.WebSocketClient;
27 import org.java_websocket.handshake.ServerHandshake;
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.std.StdPDPNotification;
34 import org.onap.policy.xacml.api.XACMLErrorConstants;
37 public class ManualClientEnd extends WebSocketClient {
38 private static CountDownLatch latch;
39 private static StdPDPNotification notification = null;
40 private static String resultJson = null;
41 private static Logger logger = FlexLogger.getLogger(ManualClientEnd.class.getName());
42 private static ManualClientEnd client;
44 public ManualClientEnd(URI serverUri) {
49 public void onClose(int code, String reason, boolean remote) {
50 logger.info("ManualClientEnd disconnected from: " + getURI() + "; Code: " + code + ", reason : " + reason);
55 public void onError(Exception ex) {
56 logger.error("XACMLErrorConstants.ERROR_PROCESS_FLOW + ManualClientEnd - Error connecting to: " + getURI()
57 + ", Exception occured ...\n" + ex);
62 public void onMessage(String message) {
63 logger.info("Manual Notification Recieved Message from : " + getURI() + ", Notification: " + message);
64 ManualClientEnd.resultJson = message;
66 ManualClientEnd.notification = NotificationUnMarshal.notificationJSON(message);
68 } catch (Exception e) {
69 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
75 public void onOpen(ServerHandshake arg0) {
76 logger.info("Manual Notification Session Started... " + getURI());
85 public static void start(String url) {
86 latch = new CountDownLatch(1);
88 if (url.contains("https")) {
89 url = url.replaceAll("https", "wss");
91 url = url.replaceAll("http", "ws");
95 client = new ManualClientEnd(new URI(url + "notifications"));
99 } catch (Exception e) {
100 logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
107 * @param scheme the scheme
108 * @return the PDP notification
110 public static PDPNotification result(NotificationScheme scheme) {
111 if (resultJson == null || notification == null) {
112 logger.info("ManualClientENd - No Result available");
115 if (scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)) {
116 boolean removed = false;
117 boolean updated = false;
118 if (notification.getRemovedPolicies() != null && !notification.getRemovedPolicies().isEmpty()) {
120 notification.setNotificationType(NotificationType.REMOVE);
122 if (notification.getLoadedPolicies() != null && !notification.getLoadedPolicies().isEmpty()) {
124 notification.setNotificationType(NotificationType.UPDATE);
126 if (removed && updated) {
127 notification.setNotificationType(NotificationType.BOTH);
130 } else if (scheme.equals(NotificationScheme.MANUAL_NOTIFICATIONS)) {
131 return MatchStore.checkMatch(notification);