Initial OpenECOMP policy/engine commit
[policy/engine.git] / PyPDPServer / src / main / java / org / openecomp / policy / pypdp / notifications / NotificationServer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP Policy Engine
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.openecomp.policy.pypdp.notifications;
22
23 import java.io.IOException;
24 import java.util.Queue;
25 import java.util.concurrent.ConcurrentLinkedQueue;
26
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;
33
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;
38
39 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
40
41
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;
47         
48         @OnOpen
49         public void openConnection(Session session) {
50                 logger.info("Session Connected: " + session.getId());
51                 queue.add(session);
52         }
53         
54         @OnClose
55         public void closeConnection(Session session) {
56                 queue.remove(session);
57         }
58         
59         @OnError
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());
63                 
64         }
65         
66         @OnMessage
67         public void Message(String message, Session session) {
68                 if(message.equalsIgnoreCase("Manual")) {
69                         try {
70                                 session.getBasicRemote().sendText(update);
71                         } catch (IOException e) {
72                                 logger.info(XACMLErrorConstants.ERROR_SYSTEM_ERROR+ "Error in sending the Event Notification: "+ e.getMessage());
73                         }
74                 }
75         }
76         
77         public static void sendNotification(String notification){
78                 for(Session session: queue) {
79                         try {
80                                 session.getBasicRemote().sendText(notification);
81                         } catch (IOException e) {
82                                 logger.info(XACMLErrorConstants.ERROR_SYSTEM_ERROR+ "Error in sending the Event Notification: "+ e.getMessage());
83                         }
84                 }
85         }
86         
87         public static void setUpdate(String update) {
88                 NotificationServer.update = update;
89         }
90 }