Incorporate Liam code review
[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.net.URI;
24 import java.util.concurrent.CountDownLatch;
25 import org.java_websocket.client.WebSocketClient;
26 import org.java_websocket.handshake.ServerHandshake;
27 import org.onap.policy.api.NotificationScheme;
28 import org.onap.policy.api.NotificationType;
29 import org.onap.policy.api.PDPNotification;
30 import org.onap.policy.common.logging.flexlogger.FlexLogger;
31 import org.onap.policy.common.logging.flexlogger.Logger;
32 import org.onap.policy.std.StdPDPNotification;
33 import org.onap.policy.xacml.api.XACMLErrorConstants;
34
35 public class ManualClientEnd extends WebSocketClient {
36     private static CountDownLatch latch;
37     private static StdPDPNotification notification = null;
38     private static String resultJson = null;
39     private static Logger logger = FlexLogger.getLogger(ManualClientEnd.class.getName());
40     private static ManualClientEnd client;
41
42     public ManualClientEnd(URI serverUri) {
43         super(serverUri);
44     }
45
46     @Override
47     public void onClose(int code, String reason, boolean remote) {
48         logger.info("ManualClientEnd disconnected from: " + getURI() + "; Code: " + code + ", reason :  " + reason);
49         latch.countDown();
50     }
51
52     @Override
53     public void onError(Exception ex) {
54         logger.error("XACMLErrorConstants.ERROR_PROCESS_FLOW + ManualClientEnd - Error connecting to: " + getURI()
55                 + ", Exception occured ...\n" + ex);
56         latch.countDown();
57     }
58
59     @Override
60     public void onMessage(String message) {
61         logger.info("Manual Notification Recieved Message from : " + getURI() + ", Notification: " + message);
62         ManualClientEnd.resultJson = message;
63         try {
64             ManualClientEnd.notification = NotificationUnMarshal.notificationJSON(message);     
65         } catch (Exception e) {
66             logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
67         }
68         latch.countDown();
69     }
70
71     @Override
72     public void onOpen(ServerHandshake arg0) {
73         logger.info("Manual Notification Session Started... " + getURI());
74         send("Manual");
75     }
76
77     /**
78      * Start.
79      *
80      * @param url the url
81      */
82     public static void start(String url) {
83         latch = new CountDownLatch(1);
84
85         if (url.contains("https")) {
86             url = url.replaceAll("https", "wss");
87         } else {
88             url = url.replaceAll("http", "ws");
89         }
90
91         try {
92             client = new ManualClientEnd(new URI(url + "notifications"));
93             client.connect();
94             latch.await();
95             client.closeBlocking();
96         } catch (Exception e) {
97             logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
98         }
99     }
100
101     /**
102      * Result.
103      *
104      * @param scheme the scheme
105      * @return the PDP notification
106      */
107     public static PDPNotification result(NotificationScheme scheme) {
108         if (resultJson == null || notification == null) {
109             logger.info("ManualClientENd - No Result available");
110             return null;
111         } else {
112             if (scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)) {
113                 boolean removed = false;
114                 boolean updated = false;
115                 if (notification.getRemovedPolicies() != null && !notification.getRemovedPolicies().isEmpty()) {
116                     removed = true;
117                     notification.setNotificationType(NotificationType.REMOVE);
118                 }
119                 if (notification.getLoadedPolicies() != null && !notification.getLoadedPolicies().isEmpty()) {
120                     updated = true;
121                     notification.setNotificationType(NotificationType.UPDATE);
122                 }
123                 if (removed && updated) {
124                     notification.setNotificationType(NotificationType.BOTH);
125                 }
126                 return notification;
127             } else if (scheme.equals(NotificationScheme.MANUAL_NOTIFICATIONS)) {
128                 return MatchStore.checkMatch(notification);
129             } else {
130                 return null;
131             }
132         }
133     }
134 }