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
\r
28 class NotificationBrokerImpl implements NotificationProviderService, AutoCloseable {
\r
30 val ListenerRegistry<NotificationInterestListener> interestListeners = ListenerRegistry.create;
\r
32 val Multimap<Class<? extends Notification>, NotificationListener<?>> listeners;
\r
35 var ExecutorService executor;
\r
37 val logger = LoggerFactory.getLogger(NotificationBrokerImpl)
\r
40 listeners = Multimaps.synchronizedSetMultimap(HashMultimap.create())
\r
44 new(ExecutorService executor) {
\r
45 listeners = Multimaps.synchronizedSetMultimap(HashMultimap.create())
\r
46 this.executor = executor;
\r
50 override <T extends Notification> addNotificationListener(Class<T> notificationType,
\r
51 NotificationListener<T> listener) {
\r
52 listeners.put(notificationType, listener)
\r
56 override <T extends Notification> removeNotificationListener(Class<T> notificationType,
\r
57 NotificationListener<T> listener) {
\r
58 listeners.remove(notificationType, listener)
\r
61 override notify(Notification notification) {
\r
62 publish(notification)
\r
65 def getNotificationTypes(Notification notification) {
\r
66 notification.class.interfaces.filter[it != Notification && Notification.isAssignableFrom(it)]
\r
69 @SuppressWarnings("unchecked")
\r
70 private def notifyAll(Collection<NotificationListener<?>> listeners, Notification notification) {
\r
71 listeners.forEach[(it as NotificationListener).onNotification(notification)]
\r
75 override addNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
\r
76 throw new UnsupportedOperationException("Deprecated method. Use registerNotificationListener instead.");
\r
81 override removeNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
\r
82 throw new UnsupportedOperationException(
\r
83 "Deprecated method. Use RegisterNotificationListener returned value to close registration.")
\r
87 override notify(Notification notification, ExecutorService service) {
\r
88 publish(notification, service)
\r
91 override publish(Notification notification) {
\r
92 publish(notification, executor)
\r
95 override publish(Notification notification, ExecutorService service) {
\r
96 val allTypes = notification.notificationTypes
\r
98 var Iterable<NotificationListener<?>> listenerToNotify = Collections.emptySet();
\r
99 for (type : allTypes) {
\r
100 listenerToNotify = listenerToNotify + listeners.get(type as Class<? extends Notification>)
\r
102 val tasks = listenerToNotify.map[new NotifyTask(it, notification)].toSet;
\r
103 executor.invokeAll(tasks);
\r
106 override <T extends Notification> registerNotificationListener(Class<T> notificationType,
\r
107 NotificationListener<T> listener) {
\r
108 val reg = new GenericNotificationRegistration<T>(notificationType, listener, this);
\r
109 listeners.put(notificationType, listener);
\r
110 announceNotificationSubscription(notificationType);
\r
114 def announceNotificationSubscription(Class<? extends Notification> notification) {
\r
115 for (listener : interestListeners) {
\r
117 listener.instance.onNotificationSubscribtion(notification);
\r
118 } catch (Exception e) {
\r
119 logger.error("", e.message)
\r
124 override registerNotificationListener(
\r
125 org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
\r
126 val invoker = SingletonHolder.INVOKER_FACTORY.invokerFor(listener);
\r
127 for (notifyType : invoker.supportedNotifications) {
\r
128 listeners.put(notifyType, invoker.invocationProxy)
\r
129 announceNotificationSubscription(notifyType)
\r
131 val registration = new GeneratedListenerRegistration(listener, invoker,this);
\r
132 return registration as Registration<org.opendaylight.yangtools.yang.binding.NotificationListener>;
\r
135 protected def unregisterListener(GenericNotificationRegistration<?> reg) {
\r
136 listeners.remove(reg.type, reg.instance);
\r
139 protected def unregisterListener(GeneratedListenerRegistration reg) {
\r
140 for (notifyType : reg.invoker.supportedNotifications) {
\r
141 listeners.remove(notifyType, reg.invoker.invocationProxy)
\r
146 //FIXME: implement properly.
\r
149 override registerInterestListener(NotificationInterestListener interestListener) {
\r
150 val registration = interestListeners.register(interestListener);
\r
152 for(notification : listeners.keySet) {
\r
153 interestListener.onNotificationSubscribtion(notification);
\r
155 return registration
\r
159 class GenericNotificationRegistration<T extends Notification> extends AbstractObjectRegistration<NotificationListener<T>> implements ListenerRegistration<NotificationListener<T>> {
\r
164 var NotificationBrokerImpl notificationBroker;
\r
166 public new(Class<T> type, NotificationListener<T> instance, NotificationBrokerImpl broker) {
\r
169 notificationBroker = broker;
\r
172 override protected removeRegistration() {
\r
173 notificationBroker.unregisterListener(this);
\r
174 notificationBroker = null;
\r
178 class GeneratedListenerRegistration extends AbstractObjectRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> implements ListenerRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> {
\r
181 val NotificationInvoker invoker;
\r
183 var NotificationBrokerImpl notificationBroker;
\r
186 new(org.opendaylight.yangtools.yang.binding.NotificationListener instance, NotificationInvoker invoker, NotificationBrokerImpl broker) {
\r
188 _invoker = invoker;
\r
189 notificationBroker = broker;
\r
192 override protected removeRegistration() {
\r
193 notificationBroker.unregisterListener(this);
\r
194 notificationBroker = null;
\r
200 class NotifyTask implements Callable<Object> {
\r
202 private static val log = LoggerFactory.getLogger(NotifyTask);
\r
204 @SuppressWarnings("rawtypes")
\r
205 val NotificationListener listener;
\r
206 val Notification notification;
\r
209 //Only logging the complete notification in debug mode
\r
211 if(log.isDebugEnabled){
\r
212 log.debug("Delivering notification {} to {}",notification,listener);
\r
214 log.trace("Delivering notification {} to {}",notification.class.name,listener);
\r
216 listener.onNotification(notification);
\r
217 if(log.isDebugEnabled){
\r
218 log.debug("Notification delivered {} to {}",notification,listener);
\r
220 log.trace("Notification delivered {} to {}",notification.class.name,listener);
\r
222 } catch (Exception e) {
\r
223 log.error("Unhandled exception thrown by listener: {}", listener, e);
\r