Merge "Resolve Bug:707 - ConfigPusher should wait for netconf-impl to register JMX...
[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     def getNotificationTypes(Notification notification) {\r
53         notification.class.interfaces.filter[it != Notification && Notification.isAssignableFrom(it)]\r
54     }\r
55 \r
56     override publish(Notification notification) {\r
57         publish(notification, executor)\r
58     }\r
59 \r
60     override publish(Notification notification, ExecutorService service) {\r
61         val allTypes = notification.notificationTypes\r
62 \r
63         var Iterable<NotificationListener<? extends Object>> listenerToNotify = Collections.emptySet();\r
64         for (type : allTypes) {\r
65             listenerToNotify = listenerToNotify + listeners.get(type as Class<? extends Notification>)\r
66         }\r
67         val tasks = listenerToNotify.map[new NotifyTask(it, notification)].toSet;\r
68         submitAll(executor,tasks);\r
69     }\r
70     \r
71     def submitAll(ExecutorService service, Set<NotifyTask> tasks) {\r
72         val ret = ImmutableSet.<Future<Object>>builder();\r
73         for(task : tasks) {\r
74             ret.add(service.submit(task));\r
75         }\r
76         return ret.build();\r
77     }\r
78     \r
79     override <T extends Notification> registerNotificationListener(Class<T> notificationType,\r
80         NotificationListener<T> listener) {\r
81         val reg = new GenericNotificationRegistration<T>(notificationType, listener, this);\r
82         listeners.put(notificationType, listener);\r
83         announceNotificationSubscription(notificationType);\r
84         return reg;\r
85     }\r
86     \r
87     def announceNotificationSubscription(Class<? extends Notification> notification) {\r
88         for (listener : interestListeners) {\r
89             try {\r
90                 listener.instance.onNotificationSubscribtion(notification);\r
91             } catch (Exception e) {\r
92                 logger.error("", e.message)\r
93             }\r
94         }\r
95     }\r
96 \r
97     override registerNotificationListener(\r
98         org.opendaylight.yangtools.yang.binding.NotificationListener listener) {\r
99         val invoker = SingletonHolder.INVOKER_FACTORY.invokerFor(listener);\r
100         for (notifyType : invoker.supportedNotifications) {\r
101             listeners.put(notifyType, invoker.invocationProxy)\r
102             announceNotificationSubscription(notifyType)\r
103         }\r
104         val registration = new GeneratedListenerRegistration(listener, invoker,this);\r
105         return registration as Registration<org.opendaylight.yangtools.yang.binding.NotificationListener>;\r
106     }\r
107 \r
108     protected def unregisterListener(GenericNotificationRegistration<?> reg) {\r
109         listeners.remove(reg.type, reg.instance);\r
110     }\r
111 \r
112     protected def unregisterListener(GeneratedListenerRegistration reg) {\r
113         for (notifyType : reg.invoker.supportedNotifications) {\r
114             listeners.remove(notifyType, reg.invoker.invocationProxy)\r
115         }\r
116     }\r
117     \r
118     override close()  {\r
119         //FIXME: implement properly.\r
120     }\r
121     \r
122     override registerInterestListener(NotificationInterestListener interestListener) {\r
123         val registration = interestListeners.register(interestListener);\r
124         \r
125         for(notification : listeners.keySet) {\r
126             interestListener.onNotificationSubscribtion(notification);\r
127         }\r
128         return registration\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     @SuppressWarnings("rawtypes")\r
178     val NotificationListener listener;\r
179     val Notification notification;\r
180 \r
181     override call() {\r
182         //Only logging the complete notification in debug mode\r
183         try {\r
184             if(log.isDebugEnabled){\r
185                 log.debug("Delivering notification {} to {}",notification,listener);\r
186             } else {\r
187                 log.trace("Delivering notification {} to {}",notification.class.name,listener);\r
188             }\r
189             listener.onNotification(notification);\r
190             if(log.isDebugEnabled){\r
191                 log.debug("Notification delivered {} to {}",notification,listener);\r
192             } else {\r
193                 log.trace("Notification delivered {} to {}",notification.class.name,listener);\r
194             }\r
195         } catch (Exception e) {\r
196             log.error("Unhandled exception thrown by listener: {}", listener, e);\r
197         }\r
198         return null;\r
199     }\r
200 \r
201 }\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.