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.openecomp.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.apache.log4j.Logger;
37 import org.glassfish.tyrus.client.ClientManager;
38 import org.openecomp.policy.api.NotificationScheme;
39 import org.openecomp.policy.api.NotificationType;
40 import org.openecomp.policy.api.PDPNotification;
41 import org.openecomp.policy.std.StdPDPNotification;
43 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
45 import org.openecomp.policy.common.logging.flexlogger.*;
48 public class ManualClientEnd {
49 private static CountDownLatch latch;
50 private static StdPDPNotification notification = null;
51 private static String resultJson = null;
52 private static Logger logger = FlexLogger.getLogger(ManualClientEnd.class.getName());
54 public static void start(String url) {
55 latch = new CountDownLatch(1);
56 ClientManager client = ClientManager.createClient();
57 if(url.contains("https")){
58 url = url.replaceAll("https", "wss");
60 url = url.replaceAll("http", "ws");
63 client.connectToServer(ManualClientEnd.class, new URI(url+"notifications"));
65 } catch (DeploymentException | URISyntaxException | InterruptedException e) {
66 logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
67 } catch (IOException e) {
68 logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
72 public static PDPNotification result(NotificationScheme scheme) {
73 if (resultJson == null || notification == null) {
74 logger.debug("No Result" );
77 if(scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)) {
78 boolean removed = false, updated = false;
79 if(notification.getRemovedPolicies()!=null && !notification.getRemovedPolicies().isEmpty()){
82 if(notification.getLoadedPolicies()!=null && !notification.getLoadedPolicies().isEmpty()){
85 if(removed && updated) {
86 notification.setNotificationType(NotificationType.BOTH);
88 notification.setNotificationType(NotificationType.REMOVE);
90 notification.setNotificationType(NotificationType.UPDATE);
93 }else if(scheme.equals(NotificationScheme.MANUAL_NOTIFICATIONS)) {
94 return MatchStore.checkMatch(notification);
103 public void onOpen(Session session) throws IOException {
104 logger.info("Session Started with : " + session.getId());
105 session.getBasicRemote().sendText("Manual");
109 public void onError(Session session, Throwable e) {
110 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error in: "+ session.getId());
115 public void onClose(Session session) {
116 logger.info("Session ended with "+ session.getId());
121 public void onMessage(String message, Session session){
122 logger.debug(" Manual Notification Recieved Message : " + message +" Session info is : "+ session.getId());
123 resultJson = message;
125 notification = NotificationUnMarshal.notificationJSON(message);
126 } catch (Exception e) {
127 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
132 } catch (IOException e) {
133 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
135 } // For Manual Client..