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.pypdp.notifications;
23 import java.io.IOException;
24 import java.util.Queue;
25 import java.util.concurrent.ConcurrentLinkedQueue;
27 import javax.websocket.OnClose;
28 import javax.websocket.OnError;
29 import javax.websocket.OnMessage;
30 import javax.websocket.OnOpen;
31 import javax.websocket.Session;
32 import javax.websocket.server.ServerEndpoint;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
37 import org.openecomp.policy.common.logging.flexlogger.Logger;
39 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
42 @ServerEndpoint(value = "/org.openecomp.policy.pypdp.notifications")
43 public class NotificationServer {
44 private static final Logger logger = FlexLogger.getLogger(NotificationServer.class);
45 private static Queue<Session> queue = new ConcurrentLinkedQueue<Session>();
46 private static String update = null;
49 public void openConnection(Session session) {
50 logger.info("Session Connected: " + session.getId());
55 public void closeConnection(Session session) {
56 queue.remove(session);
60 public void error(Session session, Throwable t) {
61 queue.remove(session);
62 logger.info(XACMLErrorConstants.ERROR_SYSTEM_ERROR+ "Session Error for : " + session.getId() + " Error: " + t.getMessage());
67 public void Message(String message, Session session) {
68 if(message.equalsIgnoreCase("Manual")) {
70 session.getBasicRemote().sendText(update);
71 } catch (IOException e) {
72 logger.info(XACMLErrorConstants.ERROR_SYSTEM_ERROR+ "Error in sending the Event Notification: "+ e.getMessage());
77 public static void sendNotification(String notification){
78 for(Session session: queue) {
80 session.getBasicRemote().sendText(notification);
81 } catch (IOException e) {
82 logger.info(XACMLErrorConstants.ERROR_SYSTEM_ERROR+ "Error in sending the Event Notification: "+ e.getMessage());
87 public static void setUpdate(String update) {
88 NotificationServer.update = update;