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.Queue;
28 import java.util.concurrent.ConcurrentLinkedQueue;
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 import javax.websocket.server.ServerEndpoint;
37 import org.openecomp.policy.rest.XACMLRestProperties;
38 import org.openecomp.policy.common.logging.eelf.MessageCodes;
39 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
40 import com.att.nsa.cambria.client.CambriaClientFactory;
41 import com.att.nsa.cambria.client.CambriaPublisher;
42 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
43 import com.att.research.xacml.util.XACMLProperties;
45 import org.openecomp.policy.common.logging.flexlogger.*;
49 * The NotificationServer sends the Server Notifications to the Clients once there is any Event.
50 * WebSockets is being used as a medium for sending Notifications.
51 * UEB is being used as a medium for sending Notifications.
56 @ServerEndpoint(value = "/notifications")
57 public class NotificationServer {
58 private static final Logger logger = FlexLogger.getLogger(NotificationServer.class);
59 private static Queue<Session> queue = new ConcurrentLinkedQueue<Session>();
60 private static String update = null;
61 private static String hosts = null;
62 private static URL aURL = null;
65 public void openConnection(Session session) {
66 logger.info("Session Connected: " + session.getId());
71 public void closeConnection(Session session) {
72 queue.remove(session);
76 public void error(Session session, Throwable t) {
77 queue.remove(session);
78 logger.info(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Session Error for : " + session.getId() + " Error: " + t.getMessage());
83 public void Message(String message, Session session) {
85 if(message.equalsIgnoreCase("Manual")) {
87 session.getBasicRemote().sendText(update);
89 } catch (IOException e) {
90 logger.info(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error in sending the Event Notification: "+ e.getMessage());
91 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error sending Message update");
96 public static void sendNotification(String notification, String propNotificationType, String pdpURL){
98 logger.debug("Notification set to " + propNotificationType);
99 if (propNotificationType.equals("ueb")){
102 aURL = new URL(pdpURL);
103 topic = aURL.getHost() + aURL.getPort();
104 } catch (MalformedURLException e1) {
105 pdpURL = pdpURL.replace("/", "");
106 topic = pdpURL.replace(":", "");
107 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error in parsing out pdpURL for UEB notfication ");
108 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e1, "Error in parsing out pdpURL for UEB notfication ");
110 hosts = XACMLProperties.getProperty(XACMLRestProperties.PROP_NOTIFICATION_UEB_CLUSTER);
111 logger.debug("Creating Publisher for host: " + hosts + " with topic: " + topic);
112 CambriaPublisher pub = null;
114 pub = CambriaClientFactory.createSimplePublisher (null, hosts, topic );
115 } catch (MalformedURLException e1) {
116 // TODO Auto-generated catch block
117 e1.printStackTrace();
118 } catch (GeneralSecurityException e1) {
119 // TODO Auto-generated catch block
120 e1.printStackTrace();
123 pub.send( "MyPartitionKey", notification );
124 } catch (IOException e) {
125 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error sending notification update");
129 for(Session session: queue) {
131 session.getBasicRemote().sendText(notification);
132 } catch (IOException e) {
133 logger.info(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error in sending the Event Notification: "+ e.getMessage());
138 public static void setUpdate(String update) {
139 NotificationServer.update = update;