2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.policy.pdp.rest.notifications;
23 import java.io.IOException;
24 import java.net.MalformedURLException;
26 import java.security.GeneralSecurityException;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
30 import java.util.Queue;
31 import java.util.concurrent.ConcurrentLinkedQueue;
32 import java.util.concurrent.TimeUnit;
34 import javax.websocket.OnClose;
35 import javax.websocket.OnError;
36 import javax.websocket.OnMessage;
37 import javax.websocket.OnOpen;
38 import javax.websocket.Session;
39 import javax.websocket.server.ServerEndpoint;
41 import org.openecomp.policy.common.logging.eelf.MessageCodes;
42 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
43 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
44 import org.openecomp.policy.common.logging.flexlogger.Logger;
45 import org.openecomp.policy.rest.XACMLRestProperties;
46 import org.openecomp.policy.utils.BusPublisher;
47 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
49 import com.att.nsa.cambria.client.CambriaBatchingPublisher;
50 import com.att.nsa.cambria.client.CambriaClientBuilders;
51 import com.att.research.xacml.util.XACMLProperties;
55 * The NotificationServer sends the Server Notifications to the Clients once there is any Event.
56 * WebSockets is being used as a medium for sending Notifications.
57 * UEB is being used as a medium for sending Notifications.
58 * DMAAP is being used as a medium for sending Notifications.
63 @ServerEndpoint(value = "/notifications")
64 public class NotificationServer {
65 private static final Logger LOGGER = FlexLogger.getLogger(NotificationServer.class);
66 private static Queue<Session> queue = new ConcurrentLinkedQueue<Session>();
67 private static String update = null;
68 private static String hosts = null;
69 private static URL aURL = null;
72 public void openConnection(Session session) {
73 LOGGER.info("Session Connected: " + session.getId());
78 public void closeConnection(Session session) {
79 queue.remove(session);
83 public void error(Session session, Throwable t) {
84 queue.remove(session);
85 LOGGER.info(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Session Error for : " + session.getId() + " Error: " + t.getMessage());
90 public void Message(String message, Session session) {
92 if(message.equalsIgnoreCase("Manual")) {
94 session.getBasicRemote().sendText(update);
96 } catch (IOException e) {
97 LOGGER.info(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error in sending the Event Notification: "+ e.getMessage());
98 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error sending Message update");
103 public static void sendNotification(String notification, String propNotificationType, String pdpURL) throws Exception {
105 LOGGER.debug("Notification set to " + propNotificationType);
106 if (propNotificationType.equals("ueb")){
110 aURL = new URL(pdpURL);
111 topic = aURL.getHost() + aURL.getPort();
112 } catch (MalformedURLException e1) {
113 pdpURL = pdpURL.replace("/", "");
114 topic = pdpURL.replace(":", "");
115 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error in parsing out pdpURL for UEB notfication ");
116 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e1, "Error in parsing out pdpURL for UEB notfication ");
118 hosts = XACMLProperties.getProperty(XACMLRestProperties.PROP_NOTIFICATION_SERVERS);
119 String apiKey = XACMLProperties.getProperty(XACMLRestProperties.PROP_UEB_API_KEY);
120 String apiSecret = XACMLProperties.getProperty(XACMLRestProperties.PROP_UEB_API_SECRET);
122 LOGGER.debug("Creating Publisher for host: " + hosts + " with topic: " + topic);
123 CambriaBatchingPublisher pub = null;
125 if(hosts==null || topic==null || apiKey==null || apiSecret==null){
126 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "UEB properties are missing from the property file ");
127 throw new Exception(XACMLErrorConstants.ERROR_DATA_ISSUE + "UEB properties are missing from the property file ");
134 pub = new CambriaClientBuilders.PublisherBuilder ()
135 .usingHosts ( hosts )
137 .authenticatedBy ( apiKey, apiSecret )
141 } catch (MalformedURLException e1) {
142 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error creating the UEB publisher" + e1.getMessage());
143 e1.printStackTrace();
144 } catch (GeneralSecurityException e1) {
145 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error creating the UEB publisher" + e1.getMessage());
146 e1.printStackTrace();
150 pub.send( "MyPartitionKey", notification );
151 } catch (IOException e) {
152 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error sending notification update" + e.getMessage());
155 // close the publisher. The batching publisher does not send events
156 // immediately, so you MUST use close to send any remaining messages.
157 // You provide the amount of time you're willing to wait for the sends
158 // to succeed before giving up. If any messages are unsent after that time,
159 // they're returned to your app. You could, for example, persist to disk
160 // and try again later.
161 final List stuck = pub.close ( 20, TimeUnit.SECONDS );
163 if ( stuck.size () > 0 )
165 System.err.println ( stuck.size() + " messages unsent" );
169 System.out.println ( "Clean exit; all messages sent: " + notification );
172 } else if (propNotificationType.equals("dmaap")) {
174 // Setting up the Publisher for DMaaP MR
175 String dmaapServers = XACMLProperties.getProperty(XACMLRestProperties.PROP_NOTIFICATION_SERVERS);
176 String topic = XACMLProperties.getProperty(XACMLRestProperties.PROP_NOTIFICATION_TOPIC);
177 String aafLogin = XACMLProperties.getProperty("DMAAP_AAF_LOGIN");
178 String aafPassword = XACMLProperties.getProperty("DMAAP_AAF_PASSWORD");
181 if(dmaapServers==null || topic==null){
182 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "DMaaP properties are missing from the property file ");
183 throw new Exception(XACMLErrorConstants.ERROR_DATA_ISSUE + "DMaaP properties are missing from the property file ");
191 List<String> dmaapList = null;
192 if(dmaapServers.contains(",")) {
193 dmaapList = new ArrayList<String>(Arrays.asList(dmaapServers.split("\\s*,\\s*")));
195 dmaapList = new ArrayList<String>();
196 dmaapList.add(dmaapServers);
199 BusPublisher publisher =
200 new BusPublisher.DmaapPublisherWrapper(dmaapList,
205 // Sending notification through DMaaP Message Router
206 publisher.send( "MyPartitionKey", notification);
207 LOGGER.debug("Message Published on DMaaP :" + dmaapList.get(0) + "for Topic: " + topic);
210 } catch (Exception e) {
211 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error sending notification update" + e.getMessage());
215 for(Session session: queue) {
217 session.getBasicRemote().sendText(notification);
218 } catch (IOException e) {
219 LOGGER.info(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error in sending the Event Notification: "+ e.getMessage());
224 public static void setUpdate(String update) {
225 NotificationServer.update = update;