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.net.URISyntaxException;
26 import java.util.concurrent.CountDownLatch;
28 import javax.websocket.ClientEndpoint;
29 import javax.websocket.DeploymentException;
30 import javax.websocket.OnClose;
31 import javax.websocket.OnError;
32 import javax.websocket.OnMessage;
33 import javax.websocket.OnOpen;
34 import javax.websocket.Session;
36 import org.glassfish.tyrus.client.ClientManager;
37 import org.onap.policy.api.NotificationScheme;
38 import org.onap.policy.api.NotificationType;
39 import org.onap.policy.api.PDPNotification;
40 import org.onap.policy.std.StdPDPNotification;
42 import org.onap.policy.xacml.api.XACMLErrorConstants;
44 import org.onap.policy.common.logging.flexlogger.*;
47 public class ManualClientEnd {
48 private static CountDownLatch latch;
49 private static StdPDPNotification notification = null;
50 private static String resultJson = null;
51 private static Logger logger = FlexLogger.getLogger(ManualClientEnd.class.getName());
53 public static void start(String url) {
54 latch = new CountDownLatch(1);
55 ClientManager client = ClientManager.createClient();
56 if(url.contains("https")){
57 url = url.replaceAll("https", "wss");
59 url = url.replaceAll("http", "ws");
62 client.connectToServer(ManualClientEnd.class, new URI(url+"notifications"));
64 } catch (DeploymentException | URISyntaxException | InterruptedException |IOException e) {
65 logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
69 public static PDPNotification result(NotificationScheme scheme) {
70 if (resultJson == null || notification == null) {
71 logger.debug("No Result" );
74 if(scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)) {
75 boolean removed = false;
76 boolean updated = false;
77 if(notification.getRemovedPolicies()!=null && !notification.getRemovedPolicies().isEmpty()){
80 if(notification.getLoadedPolicies()!=null && !notification.getLoadedPolicies().isEmpty()){
83 if(removed && updated) {
84 notification.setNotificationType(NotificationType.BOTH);
86 notification.setNotificationType(NotificationType.REMOVE);
88 notification.setNotificationType(NotificationType.UPDATE);
91 }else if(scheme.equals(NotificationScheme.MANUAL_NOTIFICATIONS)) {
92 return MatchStore.checkMatch(notification);
101 public void onOpen(Session session) throws IOException {
102 logger.info("Session Started with : " + session.getId());
103 session.getBasicRemote().sendText("Manual");
107 public void onError(Session session, Throwable e) {
108 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error in: "+ session.getId());
113 public void onClose(Session session) {
114 logger.info("Session ended with "+ session.getId());
119 public static void onMessage(String message, Session session){
120 logger.debug(" Manual Notification Recieved Message : " + message +" Session info is : "+ session.getId());
121 resultJson = message;
123 notification = NotificationUnMarshal.notificationJSON(message);
124 } catch (Exception e) {
125 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
130 } catch (IOException e) {
131 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
133 } // For Manual Client..