Remove insecure dependency on PolicyEngineAPI
[policy/engine.git] / PolicyEngineAPI / src / main / java / org / onap / 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.onap.policy.std;
22
23 import java.io.IOException;
24 import java.net.URI;
25 import java.util.concurrent.CountDownLatch;
26
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;
33
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;
40
41 import org.onap.policy.xacml.api.XACMLErrorConstants;
42
43 import org.onap.policy.common.logging.flexlogger.*; 
44
45 @ClientEndpoint
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;
52         
53         public ManualClientEnd(URI serverUri) {
54                 super(serverUri);
55         }
56
57         @Override
58         public void onClose(int arg0, String arg1, boolean arg2) {
59                 // Not implemented
60         }
61
62         @Override
63         public void onError(Exception arg0) {
64                 // Not implemented
65         }
66
67         @Override
68         public void onMessage(String arg0) {
69                 // Not implemented
70         }
71
72         @Override
73         public void onOpen(ServerHandshake arg0) {
74                 // Not implemented
75         }
76
77         public static void start(String url) {
78                 latch = new CountDownLatch(1);
79
80                 if (url.contains("https")) {
81                         url = url.replaceAll("https", "wss");
82                 }
83                 else {
84                         url = url.replaceAll("http", "ws");
85                 }
86                 
87                 try {
88                         client = new ManualClientEnd(new URI(url+"notifications"));
89                         latch.await();
90                 } catch (Exception e) {
91                         logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
92                 }
93         }
94
95         public static PDPNotification result(NotificationScheme scheme) {
96                 if (resultJson == null || notification == null) {
97                         logger.debug("No Result" );
98                         return null;
99                 } else {
100                         if(scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)) {
101                                 boolean removed = false;
102                                 boolean updated = false; 
103                                 if(notification.getRemovedPolicies()!=null && !notification.getRemovedPolicies().isEmpty()){
104                                         removed = true;
105                                 }
106                                 if(notification.getLoadedPolicies()!=null && !notification.getLoadedPolicies().isEmpty()){
107                                         updated = true;
108                                 }
109                                 if(removed && updated) {
110                                         notification.setNotificationType(NotificationType.BOTH);
111                                 }else if(removed){
112                                         notification.setNotificationType(NotificationType.REMOVE);
113                                 }else if(updated){
114                                         notification.setNotificationType(NotificationType.UPDATE);
115                                 }
116                                 return notification;
117                         }else if(scheme.equals(NotificationScheme.MANUAL_NOTIFICATIONS)) {
118                                 return MatchStore.checkMatch(notification);
119                         }else {
120                                 return null;
121                         }
122                 }
123         }
124         
125         // WebSockets Code.. 
126         @OnOpen
127         public void onOpen(Session session) throws IOException {
128                 logger.info("Session Started with : " + session.getId());
129                 session.getBasicRemote().sendText("Manual");
130         }
131         
132         @OnError
133         public void onError(Session session, Throwable e) {
134                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error in: "+ session.getId());
135                 latch.countDown();
136         }
137         
138         @OnClose
139         public void onClose(Session session) {
140                 logger.info("Session ended with "+ session.getId());
141                 latch.countDown();
142                 client.close();
143         }
144         
145         @OnMessage
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;
149                 try {
150                         notification = NotificationUnMarshal.notificationJSON(message);
151                 } catch (Exception e) {
152                         logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
153                         latch.countDown();
154                 }
155                 try {
156                         session.close();
157                 } catch (IOException e) {
158                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
159                         latch.countDown();
160                 } // For Manual Client..
161                 latch.countDown();
162         }
163 }