Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / java / org / openecomp / policy / admin / PAPNotificationBroadcaster.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.admin;
22
23
24 import java.io.Serializable;
25 import java.util.LinkedList;
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.Executors;
28
29
30 import org.openecomp.policy.common.logging.flexlogger.FlexLogger; 
31 import org.openecomp.policy.common.logging.flexlogger.Logger;
32
33 /**
34  * Handle Notifications from the PAP that the PDP Groups have been changed.
35  * We need a Server Push Broadcaster because there may be multiple Vaadin instances (i.e. Users) that need to be told when a change occurs.
36  * 
37  * Initially we only update the entire set of PDPGroups in one shot.
38  * 
39  * (Code copied from Book of Vaadin chapter on Server Push
40  *
41  */
42 public class PAPNotificationBroadcaster implements Serializable {
43         /**
44          * 
45          */
46         private static final long serialVersionUID = -2539940306348821754L;
47
48
49         private static Logger logger    = FlexLogger.getLogger(PAPNotificationBroadcaster.class);
50
51         
52     static ExecutorService executorService = Executors.newSingleThreadExecutor();
53
54     /**
55      * Interface used by all classes that need to be notified when PAP sends an update message.
56      * 
57      *
58      */
59     public interface PAPNotificationBroadcastListener {
60         void updateAllGroups();
61     }
62     
63     
64     
65     /*
66      * list of registered listeners
67      */
68     private static LinkedList<PAPNotificationBroadcastListener> listeners =
69         new LinkedList<PAPNotificationBroadcastListener>();
70     
71     /**
72      * Listener registers to hear about updates.
73      * @param listener
74      */
75     public static synchronized void register(
76                 PAPNotificationBroadcastListener listener) {
77         listeners.add(listener);
78     }
79     
80     
81     /**
82      * Listener is going away.
83      * 
84      * @param listener
85      */
86     public static synchronized void unregister(
87                 PAPNotificationBroadcastListener listener) {
88         listeners.remove(listener);
89     }
90     
91     
92     
93     /**
94      * Tell all listeners about an update.
95      * 
96      * @param message
97      */
98     public static synchronized void updateAllGroups() {
99         for (final PAPNotificationBroadcastListener listener: listeners) {
100   // Original code copied from example:
101   //          executorService.execute(new Runnable() {
102   //              @Override
103   //              public void run() {
104   // The problem with this is that the execute starts a new Thread, but the thing we are calling (the listener.updateAllGroups)
105   // happens in this case to ALSO create a new thread, and it locks up because the shared threadpool queue is already locked by this method.
106   // On application shutdown that left us with a blocked thread, so the process never goes away.
107   // Since the listener.updateAllGroups does ALL of its work inside a new Runnable thread, there should be no need for this method to also create a thread.
108   
109                 /*
110                  * IMPORTANT:
111                  * All listeners MUST either execute with no possibility of blocking
112                  * OR must start their own threads to handle blocking and concurrent operations.
113                  */
114                 if (logger.isDebugEnabled()) {
115                         logger.debug("updateAllGroups");
116                 }
117             listener.updateAllGroups();
118         }
119     }
120 }