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