2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
\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
8 package org.opendaylight.controller.sal.binding.impl
\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
\rimport java.util.Set
27 import com.google.common.collect.ImmutableSet
28 import java.util.concurrent.Future
30 class NotificationBrokerImpl implements NotificationProviderService, AutoCloseable {
\r
32 val ListenerRegistry<NotificationInterestListener> interestListeners = ListenerRegistry.create;
\r
34 val Multimap<Class<? extends Notification>, NotificationListener<?>> listeners;
\r
37 var ExecutorService executor;
\r
39 val logger = LoggerFactory.getLogger(NotificationBrokerImpl)
\r
42 listeners = Multimaps.synchronizedSetMultimap(HashMultimap.create())
\r
46 new(ExecutorService executor) {
\r
47 listeners = Multimaps.synchronizedSetMultimap(HashMultimap.create())
\r
48 this.executor = executor;
\r
52 override <T extends Notification> addNotificationListener(Class<T> notificationType,
\r
53 NotificationListener<T> listener) {
\r
54 listeners.put(notificationType, listener)
\r
58 override <T extends Notification> removeNotificationListener(Class<T> notificationType,
\r
59 NotificationListener<T> listener) {
\r
60 listeners.remove(notificationType, listener)
\r
63 override notify(Notification notification) {
\r
64 publish(notification)
\r
67 def getNotificationTypes(Notification notification) {
\r
68 notification.class.interfaces.filter[it != Notification && Notification.isAssignableFrom(it)]
\r
71 @SuppressWarnings("unchecked")
\r
72 private def notifyAll(Collection<NotificationListener<?>> listeners, Notification notification) {
\r
73 listeners.forEach[(it as NotificationListener).onNotification(notification)]
\r
77 override addNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
\r
78 throw new UnsupportedOperationException("Deprecated method. Use registerNotificationListener instead.");
\r
83 override removeNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
\r
84 throw new UnsupportedOperationException(
\r
85 "Deprecated method. Use RegisterNotificationListener returned value to close registration.")
\r
89 override notify(Notification notification, ExecutorService service) {
\r
90 publish(notification, service)
\r
93 override publish(Notification notification) {
\r
94 publish(notification, executor)
\r
97 override publish(Notification notification, ExecutorService service) {
\r
98 val allTypes = notification.notificationTypes
\r
100 var Iterable<NotificationListener<?>> listenerToNotify = Collections.emptySet();
\r
101 for (type : allTypes) {
\r
102 listenerToNotify = listenerToNotify + listeners.get(type as Class<? extends Notification>)
\r
104 val tasks = listenerToNotify.map[new NotifyTask(it, notification)].toSet;
\r
105 submitAll(executor,tasks);
\r
108 def submitAll(ExecutorService service, Set<NotifyTask> tasks) {
109 val ret = ImmutableSet.<Future<Object>>builder();
\r
110 for(task : tasks) {
\r
111 ret.add(service.submit(task));
\r
116 override <T extends Notification> registerNotificationListener(Class<T> notificationType,
\r
117 NotificationListener<T> listener) {
\r
118 val reg = new GenericNotificationRegistration<T>(notificationType, listener, this);
\r
119 listeners.put(notificationType, listener);
\r
120 announceNotificationSubscription(notificationType);
\r
124 def announceNotificationSubscription(Class<? extends Notification> notification) {
\r
125 for (listener : interestListeners) {
\r
127 listener.instance.onNotificationSubscribtion(notification);
\r
128 } catch (Exception e) {
\r
129 logger.error("", e.message)
\r
134 override registerNotificationListener(
\r
135 org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
\r
136 val invoker = SingletonHolder.INVOKER_FACTORY.invokerFor(listener);
\r
137 for (notifyType : invoker.supportedNotifications) {
\r
138 listeners.put(notifyType, invoker.invocationProxy)
\r
139 announceNotificationSubscription(notifyType)
\r
141 val registration = new GeneratedListenerRegistration(listener, invoker,this);
\r
142 return registration as Registration<org.opendaylight.yangtools.yang.binding.NotificationListener>;
\r
145 protected def unregisterListener(GenericNotificationRegistration<?> reg) {
\r
146 listeners.remove(reg.type, reg.instance);
\r
149 protected def unregisterListener(GeneratedListenerRegistration reg) {
\r
150 for (notifyType : reg.invoker.supportedNotifications) {
\r
151 listeners.remove(notifyType, reg.invoker.invocationProxy)
\r
156 //FIXME: implement properly.
\r
159 override registerInterestListener(NotificationInterestListener interestListener) {
\r
160 val registration = interestListeners.register(interestListener);
\r
162 for(notification : listeners.keySet) {
\r
163 interestListener.onNotificationSubscribtion(notification);
\r
165 return registration
\r
169 class GenericNotificationRegistration<T extends Notification> extends AbstractObjectRegistration<NotificationListener<T>> implements ListenerRegistration<NotificationListener<T>> {
\r
174 var NotificationBrokerImpl notificationBroker;
\r
176 public new(Class<T> type, NotificationListener<T> instance, NotificationBrokerImpl broker) {
\r
179 notificationBroker = broker;
\r
182 override protected removeRegistration() {
\r
183 notificationBroker.unregisterListener(this);
\r
184 notificationBroker = null;
\r
188 class GeneratedListenerRegistration extends AbstractObjectRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> implements ListenerRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> {
\r
191 val NotificationInvoker invoker;
\r
193 var NotificationBrokerImpl notificationBroker;
\r
196 new(org.opendaylight.yangtools.yang.binding.NotificationListener instance, NotificationInvoker invoker, NotificationBrokerImpl broker) {
\r
198 _invoker = invoker;
\r
199 notificationBroker = broker;
\r
202 override protected removeRegistration() {
\r
203 notificationBroker.unregisterListener(this);
\r
204 notificationBroker = null;
\r
210 class NotifyTask implements Callable<Object> {
\r
212 private static val log = LoggerFactory.getLogger(NotifyTask);
\r
214 @SuppressWarnings("rawtypes")
\r
215 val NotificationListener listener;
\r
216 val Notification notification;
\r
219 //Only logging the complete notification in debug mode
\r
221 if(log.isDebugEnabled){
\r
222 log.debug("Delivering notification {} to {}",notification,listener);
\r
224 log.trace("Delivering notification {} to {}",notification.class.name,listener);
\r
226 listener.onNotification(notification);
\r
227 if(log.isDebugEnabled){
\r
228 log.debug("Notification delivered {} to {}",notification,listener);
\r
230 log.trace("Notification delivered {} to {}",notification.class.name,listener);
\r
232 } catch (Exception e) {
\r
233 log.error("Unhandled exception thrown by listener: {}", listener, e);
\r