Fix of issue 377 for Notifications resolving.
[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\r
27 \r
28 class NotificationBrokerImpl implements NotificationProviderService, AutoCloseable {\r
29     \r
30     val ListenerRegistry<NotificationInterestListener> interestListeners = ListenerRegistry.create;\r
31     \r
32     val Multimap<Class<? extends Notification>, NotificationListener<?>> listeners;\r
33 \r
34     @Property\r
35     var ExecutorService executor;\r
36     \r
37     val logger = LoggerFactory.getLogger(NotificationBrokerImpl)\r
38 \r
39     new() {\r
40         listeners = Multimaps.synchronizedSetMultimap(HashMultimap.create())\r
41     }\r
42 \r
43     @Deprecated\r
44     new(ExecutorService executor) {\r
45         listeners = Multimaps.synchronizedSetMultimap(HashMultimap.create())\r
46         this.executor = executor;\r
47     }\r
48 \r
49     @Deprecated\r
50     override <T extends Notification> addNotificationListener(Class<T> notificationType,\r
51         NotificationListener<T> listener) {\r
52         listeners.put(notificationType, listener)\r
53     }\r
54 \r
55     @Deprecated\r
56     override <T extends Notification> removeNotificationListener(Class<T> notificationType,\r
57         NotificationListener<T> listener) {\r
58         listeners.remove(notificationType, listener)\r
59     }\r
60 \r
61     override notify(Notification notification) {\r
62         publish(notification)\r
63     }\r
64 \r
65     def getNotificationTypes(Notification notification) {\r
66         notification.class.interfaces.filter[it != Notification && Notification.isAssignableFrom(it)]\r
67     }\r
68 \r
69     @SuppressWarnings("unchecked")\r
70     private def notifyAll(Collection<NotificationListener<?>> listeners, Notification notification) {\r
71         listeners.forEach[(it as NotificationListener).onNotification(notification)]\r
72     }\r
73 \r
74     @Deprecated\r
75     override addNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {\r
76         throw new UnsupportedOperationException("Deprecated method. Use registerNotificationListener instead.");\r
77 \r
78     }\r
79 \r
80     @Deprecated\r
81     override removeNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {\r
82         throw new UnsupportedOperationException(\r
83             "Deprecated method. Use RegisterNotificationListener returned value to close registration.")\r
84     }\r
85 \r
86     @Deprecated\r
87     override notify(Notification notification, ExecutorService service) {\r
88         publish(notification, service)\r
89     }\r
90 \r
91     override publish(Notification notification) {\r
92         publish(notification, executor)\r
93     }\r
94 \r
95     override publish(Notification notification, ExecutorService service) {\r
96         val allTypes = notification.notificationTypes\r
97 \r
98         var Iterable<NotificationListener<?>> listenerToNotify = Collections.emptySet();\r
99         for (type : allTypes) {\r
100             listenerToNotify = listenerToNotify + listeners.get(type as Class<? extends Notification>)\r
101         }\r
102         val tasks = listenerToNotify.map[new NotifyTask(it, notification)].toSet;\r
103         executor.invokeAll(tasks);\r
104     }\r
105 \r
106     override <T extends Notification> registerNotificationListener(Class<T> notificationType,\r
107         NotificationListener<T> listener) {\r
108         val reg = new GenericNotificationRegistration<T>(notificationType, listener, this);\r
109         listeners.put(notificationType, listener);\r
110         announceNotificationSubscription(notificationType);\r
111         return reg;\r
112     }\r
113     \r
114     def announceNotificationSubscription(Class<? extends Notification> notification) {\r
115         for (listener : interestListeners) {\r
116             try {\r
117                 listener.instance.onNotificationSubscribtion(notification);\r
118             } catch (Exception e) {\r
119                 logger.error("", e.message)\r
120             }\r
121         }\r
122     }\r
123 \r
124     override registerNotificationListener(\r
125         org.opendaylight.yangtools.yang.binding.NotificationListener listener) {\r
126         val invoker = SingletonHolder.INVOKER_FACTORY.invokerFor(listener);\r
127         for (notifyType : invoker.supportedNotifications) {\r
128             listeners.put(notifyType, invoker.invocationProxy)\r
129             announceNotificationSubscription(notifyType)\r
130         }\r
131         val registration = new GeneratedListenerRegistration(listener, invoker,this);\r
132         return registration as Registration<org.opendaylight.yangtools.yang.binding.NotificationListener>;\r
133     }\r
134 \r
135     protected def unregisterListener(GenericNotificationRegistration<?> reg) {\r
136         listeners.remove(reg.type, reg.instance);\r
137     }\r
138 \r
139     protected def unregisterListener(GeneratedListenerRegistration reg) {\r
140         for (notifyType : reg.invoker.supportedNotifications) {\r
141             listeners.remove(notifyType, reg.invoker.invocationProxy)\r
142         }\r
143     }\r
144     \r
145     override close()  {\r
146         //FIXME: implement properly.\r
147     }\r
148     \r
149     override registerInterestListener(NotificationInterestListener interestListener) {\r
150         val registration = interestListeners.register(interestListener);\r
151         \r
152         for(notification : listeners.keySet) {\r
153             interestListener.onNotificationSubscribtion(notification);\r
154         }\r
155         return registration\r
156     }\r
157 }\r
158 \r
159 class GenericNotificationRegistration<T extends Notification> extends AbstractObjectRegistration<NotificationListener<T>> implements ListenerRegistration<NotificationListener<T>> {\r
160 \r
161     @Property\r
162     val Class<T> type;\r
163 \r
164     var NotificationBrokerImpl notificationBroker;\r
165 \r
166     public new(Class<T> type, NotificationListener<T> instance, NotificationBrokerImpl broker) {\r
167         super(instance);\r
168         _type = type;\r
169         notificationBroker = broker;\r
170     }\r
171 \r
172     override protected removeRegistration() {\r
173         notificationBroker.unregisterListener(this);\r
174         notificationBroker = null;\r
175     }\r
176 }\r
177 \r
178 class GeneratedListenerRegistration extends AbstractObjectRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> implements ListenerRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> {\r
179 \r
180     @Property\r
181     val NotificationInvoker invoker;\r
182     \r
183     var NotificationBrokerImpl notificationBroker;\r
184     \r
185 \r
186     new(org.opendaylight.yangtools.yang.binding.NotificationListener instance, NotificationInvoker invoker, NotificationBrokerImpl broker) {\r
187         super(instance);\r
188         _invoker = invoker;\r
189         notificationBroker = broker;\r
190     }\r
191 \r
192     override protected removeRegistration() {\r
193         notificationBroker.unregisterListener(this);\r
194         notificationBroker = null;\r
195         invoker.close();\r
196     }\r
197 }\r
198 \r
199 @Data\r
200 class NotifyTask implements Callable<Object> {\r
201 \r
202     private static val log = LoggerFactory.getLogger(NotifyTask);\r
203 \r
204     @SuppressWarnings("rawtypes")\r
205     val NotificationListener listener;\r
206     val Notification notification;\r
207 \r
208     override call() {\r
209         //Only logging the complete notification in debug mode\r
210         try {\r
211             if(log.isDebugEnabled){\r
212                 log.debug("Delivering notification {} to {}",notification,listener);\r
213             } else {\r
214                 log.trace("Delivering notification {} to {}",notification.class.name,listener);\r
215             }\r
216             listener.onNotification(notification);\r
217             if(log.isDebugEnabled){\r
218                 log.debug("Notification delivered {} to {}",notification,listener);\r
219             } else {\r
220                 log.trace("Notification delivered {} to {}",notification.class.name,listener);\r
221             }\r
222         } catch (Exception e) {\r
223             log.error("Unhandled exception thrown by listener: {}", listener, e);\r
224         }\r
225         return null;\r
226     }\r
227 \r
228 }\r