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