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 org.java_websocket.client.WebSocketClient;
26 import org.java_websocket.handshake.ServerHandshake;
27 import org.onap.policy.api.NotificationScheme;
28 import org.onap.policy.api.NotificationType;
29 import org.onap.policy.api.PDPNotification;
30 import org.onap.policy.common.logging.flexlogger.FlexLogger;
31 import org.onap.policy.common.logging.flexlogger.Logger;
32 import org.onap.policy.std.StdPDPNotification;
33 import org.onap.policy.xacml.api.XACMLErrorConstants;
35 public class ManualClientEnd extends WebSocketClient {
36 private static CountDownLatch latch;
37 private static StdPDPNotification notification = null;
38 private static String resultJson = null;
39 private static Logger logger = FlexLogger.getLogger(ManualClientEnd.class.getName());
40 private static ManualClientEnd client;
42 public ManualClientEnd(URI serverUri) {
47 public void onClose(int code, String reason, boolean remote) {
48 logger.info("ManualClientEnd disconnected from: " + getURI() + "; Code: " + code + ", reason : " + reason);
53 public void onError(Exception ex) {
54 logger.error("XACMLErrorConstants.ERROR_PROCESS_FLOW + ManualClientEnd - Error connecting to: " + getURI()
55 + ", Exception occured ...\n" + ex);
60 public void onMessage(String message) {
61 logger.info("Manual Notification Recieved Message from : " + getURI() + ", Notification: " + message);
62 ManualClientEnd.resultJson = message;
64 ManualClientEnd.notification = NotificationUnMarshal.notificationJSON(message);
65 } catch (Exception e) {
66 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
72 public void onOpen(ServerHandshake arg0) {
73 logger.info("Manual Notification Session Started... " + getURI());
82 public static void start(String url) {
83 latch = new CountDownLatch(1);
85 if (url.contains("https")) {
86 url = url.replaceAll("https", "wss");
88 url = url.replaceAll("http", "ws");
92 client = new ManualClientEnd(new URI(url + "notifications"));
93 client.setConnectionLostTimeout(0);
96 client.closeBlocking();
97 } catch (Exception e) {
98 logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
105 * @param scheme the scheme
106 * @return the PDP notification
108 public static PDPNotification result(NotificationScheme scheme) {
109 if (resultJson == null || notification == null) {
110 logger.info("ManualClientENd - No Result available");
113 if (scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)) {
114 boolean removed = false;
115 boolean updated = false;
116 if (notification.getRemovedPolicies() != null && !notification.getRemovedPolicies().isEmpty()) {
118 notification.setNotificationType(NotificationType.REMOVE);
120 if (notification.getLoadedPolicies() != null && !notification.getLoadedPolicies().isEmpty()) {
122 notification.setNotificationType(NotificationType.UPDATE);
124 if (removed && updated) {
125 notification.setNotificationType(NotificationType.BOTH);
128 } else if (scheme.equals(NotificationScheme.MANUAL_NOTIFICATIONS)) {
129 return MatchStore.checkMatch(notification);