[POLICY-122] Policy GUI Fixes
[policy/engine.git] / PolicyEngineAPI / src / main / java / org / openecomp / policy / std / ManualClientEnd.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineAPI
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.policy.std;
22
23 import java.io.IOException;
24 import java.net.URI;
25 import java.net.URISyntaxException;
26 import java.util.concurrent.CountDownLatch;
27
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;
35
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;
42
43 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
44
45 import org.openecomp.policy.common.logging.flexlogger.*; 
46
47 @ClientEndpoint
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());
53         
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");
59                 }else {
60                         url = url.replaceAll("http", "ws");
61                 }
62                 try {
63                         client.connectToServer(ManualClientEnd.class, new URI(url+"notifications"));
64                         latch.await();
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);
69                 }
70         }
71
72         public static PDPNotification result(NotificationScheme scheme) {
73                 if (resultJson == null || notification == null) {
74                         logger.debug("No Result" );
75                         return null;
76                 } else {
77                         if(scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)) {
78                                 boolean removed = false, updated = false; 
79                                 if(notification.getRemovedPolicies()!=null && !notification.getRemovedPolicies().isEmpty()){
80                                         removed = true;
81                                 }
82                                 if(notification.getLoadedPolicies()!=null && !notification.getLoadedPolicies().isEmpty()){
83                                         updated = true;
84                                 }
85                                 if(removed && updated) {
86                                         notification.setNotificationType(NotificationType.BOTH);
87                                 }else if(removed){
88                                         notification.setNotificationType(NotificationType.REMOVE);
89                                 }else if(updated){
90                                         notification.setNotificationType(NotificationType.UPDATE);
91                                 }
92                                 return notification;
93                         }else if(scheme.equals(NotificationScheme.MANUAL_NOTIFICATIONS)) {
94                                 return MatchStore.checkMatch(notification);
95                         }else {
96                                 return null;
97                         }
98                 }
99         }
100         
101         // WebSockets Code.. 
102         @OnOpen
103         public void onOpen(Session session) throws IOException {
104                 logger.info("Session Started with : " + session.getId());
105                 session.getBasicRemote().sendText("Manual");
106         }
107         
108         @OnError
109         public void onError(Session session, Throwable e) {
110                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error in: "+ session.getId());
111                 latch.countDown();
112         }
113         
114         @OnClose
115         public void onClose(Session session) {
116                 logger.info("Session ended with "+ session.getId());
117                 latch.countDown();
118         }
119         
120         @OnMessage
121         public void onMessage(String message, Session session){
122                 logger.debug(" Manual Notification Recieved Message : " + message +" Session info is : "+ session.getId());
123                 resultJson = message;
124                 try {
125                         notification = NotificationUnMarshal.notificationJSON(message);
126                 } catch (Exception e) {
127                         logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
128                         latch.countDown();
129                 }
130                 try {
131                         session.close();
132                 } catch (IOException e) {
133                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
134                         latch.countDown();
135                 } // For Manual Client..
136                 latch.countDown();
137         }
138 }