a7dcf80e8dbd5366ca366b0d2c50fce573a598bf
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / impl / NotificationBrokerImpl.xtend
1 /*\r
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 package org.opendaylight.controller.sal.binding.impl\r
9 \r
10 import com.google.common.collect.HashMultimap\r
11 import com.google.common.collect.Multimap\r
12 import java.util.Collection\r
13 import java.util.Collections\r
14 import java.util.concurrent.Callable\r
15 import java.util.concurrent.ExecutorService\r
16 import org.opendaylight.controller.sal.binding.api.NotificationListener\r
17 import org.opendaylight.controller.sal.binding.api.NotificationProviderService\r
18 import org.opendaylight.controller.sal.binding.spi.NotificationInvokerFactory.NotificationInvoker\r
19 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration\r
20 import org.opendaylight.yangtools.concepts.ListenerRegistration\r
21 import org.opendaylight.yangtools.concepts.Registration\r
22 import org.opendaylight.yangtools.yang.binding.Notification\r
23 import org.slf4j.LoggerFactory\r
24 import org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder\rimport com.google.common.collect.Multimaps\r
25 import org.opendaylight.yangtools.concepts.util.ListenerRegistry\r
26 import org.opendaylight.controller.sal.binding.api.NotificationProviderService.NotificationInterestListener\rimport java.util.Set
27 import com.google.common.collect.ImmutableSet
28 import java.util.concurrent.Future
29
30 class NotificationBrokerImpl implements NotificationProviderService, AutoCloseable {\r
31     \r
32     val ListenerRegistry<NotificationInterestListener> interestListeners = ListenerRegistry.create;\r
33     \r
34     val Multimap<Class<? extends Notification>, NotificationListener<?>> listeners;\r
35 \r
36     @Property\r
37     var ExecutorService executor;\r
38     \r
39     val logger = LoggerFactory.getLogger(NotificationBrokerImpl)\r
40 \r
41     new() {\r
42         listeners = Multimaps.synchronizedSetMultimap(HashMultimap.create())\r
43     }\r
44 \r
45     @Deprecated\r
46     new(ExecutorService executor) {\r
47         listeners = Multimaps.synchronizedSetMultimap(HashMultimap.create())\r
48         this.executor = executor;\r
49     }\r
50 \r
51     @Deprecated\r
52     override <T extends Notification> addNotificationListener(Class<T> notificationType,\r
53         NotificationListener<T> listener) {\r
54         listeners.put(notificationType, listener)\r
55     }\r
56 \r
57     @Deprecated\r
58     override <T extends Notification> removeNotificationListener(Class<T> notificationType,\r
59         NotificationListener<T> listener) {\r
60         listeners.remove(notificationType, listener)\r
61     }\r
62 \r
63     override notify(Notification notification) {\r
64         publish(notification)\r
65     }\r
66 \r
67     def getNotificationTypes(Notification notification) {\r
68         notification.class.interfaces.filter[it != Notification && Notification.isAssignableFrom(it)]\r
69     }\r
70 \r
71     @SuppressWarnings("unchecked")\r
72     private def notifyAll(Collection<NotificationListener<?>> listeners, Notification notification) {\r
73         listeners.forEach[(it as NotificationListener).onNotification(notification)]\r
74     }\r
75 \r
76     @Deprecated\r
77     override addNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {\r
78         throw new UnsupportedOperationException("Deprecated method. Use registerNotificationListener instead.");\r
79 \r
80     }\r
81 \r
82     @Deprecated\r
83     override removeNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {\r
84         throw new UnsupportedOperationException(\r
85             "Deprecated method. Use RegisterNotificationListener returned value to close registration.")\r
86     }\r
87 \r
88     @Deprecated\r
89     override notify(Notification notification, ExecutorService service) {\r
90         publish(notification, service)\r
91     }\r
92 \r
93     override publish(Notification notification) {\r
94         publish(notification, executor)\r
95     }\r
96 \r
97     override publish(Notification notification, ExecutorService service) {\r
98         val allTypes = notification.notificationTypes\r
99 \r
100         var Iterable<NotificationListener<?>> listenerToNotify = Collections.emptySet();\r
101         for (type : allTypes) {\r
102             listenerToNotify = listenerToNotify + listeners.get(type as Class<? extends Notification>)\r
103         }\r
104         val tasks = listenerToNotify.map[new NotifyTask(it, notification)].toSet;\r
105         submitAll(executor,tasks);\r
106     }\r
107     \r
108     def submitAll(ExecutorService service, Set<NotifyTask> tasks) {
109         val ret = ImmutableSet.<Future<Object>>builder();\r
110         for(task : tasks) {\r
111             ret.add(service.submit(task));\r
112         }\r
113         return ret.build();
114     }\r
115 \r
116     override <T extends Notification> registerNotificationListener(Class<T> notificationType,\r
117         NotificationListener<T> listener) {\r
118         val reg = new GenericNotificationRegistration<T>(notificationType, listener, this);\r
119         listeners.put(notificationType, listener);\r
120         announceNotificationSubscription(notificationType);\r
121         return reg;\r
122     }\r
123     \r
124     def announceNotificationSubscription(Class<? extends Notification> notification) {\r
125         for (listener : interestListeners) {\r
126             try {\r
127                 listener.instance.onNotificationSubscribtion(notification);\r
128             } catch (Exception e) {\r
129                 logger.error("", e.message)\r
130             }\r
131         }\r
132     }\r
133 \r
134     override registerNotificationListener(\r
135         org.opendaylight.yangtools.yang.binding.NotificationListener listener) {\r
136         val invoker = SingletonHolder.INVOKER_FACTORY.invokerFor(listener);\r
137         for (notifyType : invoker.supportedNotifications) {\r
138             listeners.put(notifyType, invoker.invocationProxy)\r
139             announceNotificationSubscription(notifyType)\r
140         }\r
141         val registration = new GeneratedListenerRegistration(listener, invoker,this);\r
142         return registration as Registration<org.opendaylight.yangtools.yang.binding.NotificationListener>;\r
143     }\r
144 \r
145     protected def unregisterListener(GenericNotificationRegistration<?> reg) {\r
146         listeners.remove(reg.type, reg.instance);\r
147     }\r
148 \r
149     protected def unregisterListener(GeneratedListenerRegistration reg) {\r
150         for (notifyType : reg.invoker.supportedNotifications) {\r
151             listeners.remove(notifyType, reg.invoker.invocationProxy)\r
152         }\r
153     }\r
154     \r
155     override close()  {\r
156         //FIXME: implement properly.\r
157     }\r
158     \r
159     override registerInterestListener(NotificationInterestListener interestListener) {\r
160         val registration = interestListeners.register(interestListener);\r
161         \r
162         for(notification : listeners.keySet) {\r
163             interestListener.onNotificationSubscribtion(notification);\r
164         }\r
165         return registration\r
166     }\r
167 }\r
168 \r
169 class GenericNotificationRegistration<T extends Notification> extends AbstractObjectRegistration<NotificationListener<T>> implements ListenerRegistration<NotificationListener<T>> {\r
170 \r
171     @Property\r
172     val Class<T> type;\r
173 \r
174     var NotificationBrokerImpl notificationBroker;\r
175 \r
176     public new(Class<T> type, NotificationListener<T> instance, NotificationBrokerImpl broker) {\r
177         super(instance);\r
178         _type = type;\r
179         notificationBroker = broker;\r
180     }\r
181 \r
182     override protected removeRegistration() {\r
183         notificationBroker.unregisterListener(this);\r
184         notificationBroker = null;\r
185     }\r
186 }\r
187 \r
188 class GeneratedListenerRegistration extends AbstractObjectRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> implements ListenerRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> {\r
189 \r
190     @Property\r
191     val NotificationInvoker invoker;\r
192     \r
193     var NotificationBrokerImpl notificationBroker;\r
194     \r
195 \r
196     new(org.opendaylight.yangtools.yang.binding.NotificationListener instance, NotificationInvoker invoker, NotificationBrokerImpl broker) {\r
197         super(instance);\r
198         _invoker = invoker;\r
199         notificationBroker = broker;\r
200     }\r
201 \r
202     override protected removeRegistration() {\r
203         notificationBroker.unregisterListener(this);\r
204         notificationBroker = null;\r
205         invoker.close();\r
206     }\r
207 }\r
208 \r
209 @Data\r
210 class NotifyTask implements Callable<Object> {\r
211 \r
212     private static val log = LoggerFactory.getLogger(NotifyTask);\r
213 \r
214     @SuppressWarnings("rawtypes")\r
215     val NotificationListener listener;\r
216     val Notification notification;\r
217 \r
218     override call() {\r
219         //Only logging the complete notification in debug mode\r
220         try {\r
221             if(log.isDebugEnabled){\r
222                 log.debug("Delivering notification {} to {}",notification,listener);\r
223             } else {\r
224                 log.trace("Delivering notification {} to {}",notification.class.name,listener);\r
225             }\r
226             listener.onNotification(notification);\r
227             if(log.isDebugEnabled){\r
228                 log.debug("Notification delivered {} to {}",notification,listener);\r
229             } else {\r
230                 log.trace("Notification delivered {} to {}",notification.class.name,listener);\r
231             }\r
232         } catch (Exception e) {\r
233             log.error("Unhandled exception thrown by listener: {}", listener, e);\r
234         }\r
235         return null;\r
236     }\r
237 \r
238 }\r