Merge "On openflow plugin restart, NPE in tx poller"
[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\rimport org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder
24
25 class NotificationBrokerImpl implements NotificationProviderService, AutoCloseable {\r
26 \r
27     val Multimap<Class<? extends Notification>, NotificationListener<?>> listeners;\r
28 \r
29     @Property\r
30     var ExecutorService executor;\r
31 \r
32     new() {\r
33         listeners = HashMultimap.create()\r
34     }\r
35 \r
36     @Deprecated\r
37     new(ExecutorService executor) {\r
38         listeners = HashMultimap.create()\r
39         this.executor = executor;\r
40     }\r
41 \r
42     @Deprecated\r
43     override <T extends Notification> addNotificationListener(Class<T> notificationType,\r
44         NotificationListener<T> listener) {\r
45         listeners.put(notificationType, listener)\r
46     }\r
47 \r
48     @Deprecated\r
49     override <T extends Notification> removeNotificationListener(Class<T> notificationType,\r
50         NotificationListener<T> listener) {\r
51         listeners.remove(notificationType, listener)\r
52     }\r
53 \r
54     override notify(Notification notification) {\r
55         publish(notification)\r
56     }\r
57 \r
58     def getNotificationTypes(Notification notification) {\r
59         notification.class.interfaces.filter[it != Notification && Notification.isAssignableFrom(it)]\r
60     }\r
61 \r
62     @SuppressWarnings("unchecked")\r
63     private def notifyAll(Collection<NotificationListener<?>> listeners, Notification notification) {\r
64         listeners.forEach[(it as NotificationListener).onNotification(notification)]\r
65     }\r
66 \r
67     @Deprecated\r
68     override addNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {\r
69         throw new UnsupportedOperationException("Deprecated method. Use registerNotificationListener instead.");\r
70 \r
71     }\r
72 \r
73     @Deprecated\r
74     override removeNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {\r
75         throw new UnsupportedOperationException(\r
76             "Deprecated method. Use RegisterNotificationListener returned value to close registration.")\r
77     }\r
78 \r
79     @Deprecated\r
80     override notify(Notification notification, ExecutorService service) {\r
81         publish(notification, service)\r
82     }\r
83 \r
84     override publish(Notification notification) {\r
85         publish(notification, executor)\r
86     }\r
87 \r
88     override publish(Notification notification, ExecutorService service) {\r
89         val allTypes = notification.notificationTypes\r
90 \r
91         var Iterable<NotificationListener<?>> listenerToNotify = Collections.emptySet();\r
92         for (type : allTypes) {\r
93             listenerToNotify = listenerToNotify + listeners.get(type as Class<? extends Notification>)\r
94         }\r
95         val tasks = listenerToNotify.map[new NotifyTask(it, notification)].toSet;\r
96         executor.invokeAll(tasks);\r
97     }\r
98 \r
99     override <T extends Notification> registerNotificationListener(Class<T> notificationType,\r
100         NotificationListener<T> listener) {\r
101         val reg = new GenericNotificationRegistration<T>(notificationType, listener, this);\r
102         listeners.put(notificationType, listener);\r
103         return reg;\r
104     }\r
105 \r
106     override registerNotificationListener(\r
107         org.opendaylight.yangtools.yang.binding.NotificationListener listener) {\r
108         val invoker = SingletonHolder.INVOKER_FACTORY.invokerFor(listener);\r
109         for (notifyType : invoker.supportedNotifications) {\r
110             listeners.put(notifyType, invoker.invocationProxy)\r
111         }\r
112         val registration = new GeneratedListenerRegistration(listener, invoker,this);\r
113         return registration as Registration<org.opendaylight.yangtools.yang.binding.NotificationListener>;\r
114     }\r
115 \r
116     protected def unregisterListener(GenericNotificationRegistration<?> reg) {\r
117         listeners.remove(reg.type, reg.instance);\r
118     }\r
119 \r
120     protected def unregisterListener(GeneratedListenerRegistration reg) {\r
121         for (notifyType : reg.invoker.supportedNotifications) {\r
122             listeners.remove(notifyType, reg.invoker.invocationProxy)\r
123         }\r
124     }\r
125     \r
126     override close()  {\r
127         //FIXME: implement properly.\r
128     }\r
129     \r
130 }\r
131 \r
132 class GenericNotificationRegistration<T extends Notification> extends AbstractObjectRegistration<NotificationListener<T>> implements ListenerRegistration<NotificationListener<T>> {\r
133 \r
134     @Property\r
135     val Class<T> type;\r
136 \r
137     var NotificationBrokerImpl notificationBroker;\r
138 \r
139     public new(Class<T> type, NotificationListener<T> instance, NotificationBrokerImpl broker) {\r
140         super(instance);\r
141         _type = type;\r
142         notificationBroker = broker;\r
143     }\r
144 \r
145     override protected removeRegistration() {\r
146         notificationBroker.unregisterListener(this);\r
147         notificationBroker = null;\r
148     }\r
149 }\r
150 \r
151 class GeneratedListenerRegistration extends AbstractObjectRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> implements ListenerRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> {\r
152 \r
153     @Property\r
154     val NotificationInvoker invoker;\r
155     \r
156     var NotificationBrokerImpl notificationBroker;\r
157     \r
158 \r
159     new(org.opendaylight.yangtools.yang.binding.NotificationListener instance, NotificationInvoker invoker, NotificationBrokerImpl broker) {\r
160         super(instance);\r
161         _invoker = invoker;\r
162         notificationBroker = broker;\r
163     }\r
164 \r
165     override protected removeRegistration() {\r
166         notificationBroker.unregisterListener(this);\r
167         notificationBroker = null;\r
168         invoker.close();\r
169     }\r
170 }\r
171 \r
172 @Data\r
173 class NotifyTask implements Callable<Object> {\r
174 \r
175     private static val log = LoggerFactory.getLogger(NotifyTask);\r
176 \r
177     val NotificationListener listener;\r
178     val Notification notification;\r
179 \r
180     override call() {\r
181         try {\r
182             log.info("Delivering notification {} to {}",notification,listener);\r
183             listener.onNotification(notification);\r
184             log.info("Notification delivered {} to {}",notification,listener);\r
185         } catch (Exception e) {\r
186             log.error("Unhandled exception thrown by listener: {}", listener, e);\r
187         }\r
188         return null;\r
189     }\r
190 \r
191 }\r