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;
23 import java.io.IOException;
25 import java.util.concurrent.CountDownLatch;
27 import javax.websocket.ClientEndpoint;
28 import javax.websocket.OnClose;
29 import javax.websocket.OnError;
30 import javax.websocket.OnMessage;
31 import javax.websocket.OnOpen;
32 import javax.websocket.Session;
34 import org.java_websocket.client.WebSocketClient;
35 import org.java_websocket.handshake.ServerHandshake;
36 import org.onap.policy.api.NotificationScheme;
37 import org.onap.policy.api.NotificationType;
38 import org.onap.policy.api.PDPNotification;
39 import org.onap.policy.std.StdPDPNotification;
41 import org.onap.policy.xacml.api.XACMLErrorConstants;
43 import org.onap.policy.common.logging.flexlogger.*;
46 public class ManualClientEnd extends WebSocketClient {
47 private static CountDownLatch latch;
48 private static StdPDPNotification notification = null;
49 private static String resultJson = null;
50 private static Logger logger = FlexLogger.getLogger(ManualClientEnd.class.getName());
51 private static ManualClientEnd client;
53 public ManualClientEnd(URI serverUri) {
58 public void onClose(int arg0, String arg1, boolean arg2) {
63 public void onError(Exception arg0) {
68 public void onMessage(String arg0) {
73 public void onOpen(ServerHandshake arg0) {
77 public static void start(String url) {
78 latch = new CountDownLatch(1);
80 if (url.contains("https")) {
81 url = url.replaceAll("https", "wss");
84 url = url.replaceAll("http", "ws");
88 client = new ManualClientEnd(new URI(url+"notifications"));
90 } catch (Exception e) {
91 logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
95 public static PDPNotification result(NotificationScheme scheme) {
96 if (resultJson == null || notification == null) {
97 logger.debug("No Result" );
100 if(scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)) {
101 boolean removed = false;
102 boolean updated = false;
103 if(notification.getRemovedPolicies()!=null && !notification.getRemovedPolicies().isEmpty()){
106 if(notification.getLoadedPolicies()!=null && !notification.getLoadedPolicies().isEmpty()){
109 if(removed && updated) {
110 notification.setNotificationType(NotificationType.BOTH);
112 notification.setNotificationType(NotificationType.REMOVE);
114 notification.setNotificationType(NotificationType.UPDATE);
117 }else if(scheme.equals(NotificationScheme.MANUAL_NOTIFICATIONS)) {
118 return MatchStore.checkMatch(notification);
127 public void onOpen(Session session) throws IOException {
128 logger.info("Session Started with : " + session.getId());
129 session.getBasicRemote().sendText("Manual");
133 public void onError(Session session, Throwable e) {
134 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error in: "+ session.getId());
139 public void onClose(Session session) {
140 logger.info("Session ended with "+ session.getId());
146 public static void onMessage(String message, Session session){
147 logger.debug(" Manual Notification Recieved Message : " + message +" Session info is : "+ session.getId());
148 resultJson = message;
150 notification = NotificationUnMarshal.notificationJSON(message);
151 } catch (Exception e) {
152 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
157 } catch (IOException e) {
158 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
160 } // For Manual Client..