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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.