Fixes for sonar critical issues
[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.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.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;
41
42 import org.onap.policy.xacml.api.XACMLErrorConstants;
43
44 import org.onap.policy.common.logging.flexlogger.*; 
45
46 @ClientEndpoint
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());
52         
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");
58                 }else {
59                         url = url.replaceAll("http", "ws");
60                 }
61                 try {
62                         client.connectToServer(ManualClientEnd.class, new URI(url+"notifications"));
63                         latch.await();
64                 } catch (DeploymentException | URISyntaxException | InterruptedException |IOException e) {
65                         logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
66                 }
67         }
68
69         public static PDPNotification result(NotificationScheme scheme) {
70                 if (resultJson == null || notification == null) {
71                         logger.debug("No Result" );
72                         return null;
73                 } else {
74                         if(scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)) {
75                                 boolean removed = false;
76                                 boolean updated = false; 
77                                 if(notification.getRemovedPolicies()!=null && !notification.getRemovedPolicies().isEmpty()){
78                                         removed = true;
79                                 }
80                                 if(notification.getLoadedPolicies()!=null && !notification.getLoadedPolicies().isEmpty()){
81                                         updated = true;
82                                 }
83                                 if(removed && updated) {
84                                         notification.setNotificationType(NotificationType.BOTH);
85                                 }else if(removed){
86                                         notification.setNotificationType(NotificationType.REMOVE);
87                                 }else if(updated){
88                                         notification.setNotificationType(NotificationType.UPDATE);
89                                 }
90                                 return notification;
91                         }else if(scheme.equals(NotificationScheme.MANUAL_NOTIFICATIONS)) {
92                                 return MatchStore.checkMatch(notification);
93                         }else {
94                                 return null;
95                         }
96                 }
97         }
98         
99         // WebSockets Code.. 
100         @OnOpen
101         public void onOpen(Session session) throws IOException {
102                 logger.info("Session Started with : " + session.getId());
103                 session.getBasicRemote().sendText("Manual");
104         }
105         
106         @OnError
107         public void onError(Session session, Throwable e) {
108                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error in: "+ session.getId());
109                 latch.countDown();
110         }
111         
112         @OnClose
113         public void onClose(Session session) {
114                 logger.info("Session ended with "+ session.getId());
115                 latch.countDown();
116         }
117         
118         @OnMessage
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;
122                 try {
123                         notification = NotificationUnMarshal.notificationJSON(message);
124                 } catch (Exception e) {
125                         logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
126                         latch.countDown();
127                 }
128                 try {
129                         session.close();
130                 } catch (IOException e) {
131                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
132                         latch.countDown();
133                 } // For Manual Client..
134                 latch.countDown();
135         }
136 }