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
26 class NotificationBrokerImpl implements NotificationProviderService, AutoCloseable {
\r
28 val Multimap<Class<? extends Notification>, NotificationListener<?>> listeners;
\r
31 var ExecutorService executor;
\r
34 listeners = Multimaps.synchronizedSetMultimap(HashMultimap.create())
\r
38 new(ExecutorService executor) {
\r
39 listeners = Multimaps.synchronizedSetMultimap(HashMultimap.create())
\r
40 this.executor = executor;
\r
44 override <T extends Notification> addNotificationListener(Class<T> notificationType,
\r
45 NotificationListener<T> listener) {
\r
46 listeners.put(notificationType, listener)
\r
50 override <T extends Notification> removeNotificationListener(Class<T> notificationType,
\r
51 NotificationListener<T> listener) {
\r
52 listeners.remove(notificationType, listener)
\r
55 override notify(Notification notification) {
\r
56 publish(notification)
\r
59 def getNotificationTypes(Notification notification) {
\r
60 notification.class.interfaces.filter[it != Notification && Notification.isAssignableFrom(it)]
\r
63 @SuppressWarnings("unchecked")
\r
64 private def notifyAll(Collection<NotificationListener<?>> listeners, Notification notification) {
\r
65 listeners.forEach[(it as NotificationListener).onNotification(notification)]
\r
69 override addNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
\r
70 throw new UnsupportedOperationException("Deprecated method. Use registerNotificationListener instead.");
\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
81 override notify(Notification notification, ExecutorService service) {
\r
82 publish(notification, service)
\r
85 override publish(Notification notification) {
\r
86 publish(notification, executor)
\r
89 override publish(Notification notification, ExecutorService service) {
\r
90 val allTypes = notification.notificationTypes
\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
96 val tasks = listenerToNotify.map[new NotifyTask(it, notification)].toSet;
\r
97 executor.invokeAll(tasks);
\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
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
113 val registration = new GeneratedListenerRegistration(listener, invoker,this);
\r
114 return registration as Registration<org.opendaylight.yangtools.yang.binding.NotificationListener>;
\r
117 protected def unregisterListener(GenericNotificationRegistration<?> reg) {
\r
118 listeners.remove(reg.type, reg.instance);
\r
121 protected def unregisterListener(GeneratedListenerRegistration reg) {
\r
122 for (notifyType : reg.invoker.supportedNotifications) {
\r
123 listeners.remove(notifyType, reg.invoker.invocationProxy)
\r
128 //FIXME: implement properly.
\r
133 class GenericNotificationRegistration<T extends Notification> extends AbstractObjectRegistration<NotificationListener<T>> implements ListenerRegistration<NotificationListener<T>> {
\r
138 var NotificationBrokerImpl notificationBroker;
\r
140 public new(Class<T> type, NotificationListener<T> instance, NotificationBrokerImpl broker) {
\r
143 notificationBroker = broker;
\r
146 override protected removeRegistration() {
\r
147 notificationBroker.unregisterListener(this);
\r
148 notificationBroker = null;
\r
152 class GeneratedListenerRegistration extends AbstractObjectRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> implements ListenerRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> {
\r
155 val NotificationInvoker invoker;
\r
157 var NotificationBrokerImpl notificationBroker;
\r
160 new(org.opendaylight.yangtools.yang.binding.NotificationListener instance, NotificationInvoker invoker, NotificationBrokerImpl broker) {
\r
162 _invoker = invoker;
\r
163 notificationBroker = broker;
\r
166 override protected removeRegistration() {
\r
167 notificationBroker.unregisterListener(this);
\r
168 notificationBroker = null;
\r
174 class NotifyTask implements Callable<Object> {
\r
176 private static val log = LoggerFactory.getLogger(NotifyTask);
\r
178 @SuppressWarnings("rawtypes")
\r
179 val NotificationListener listener;
\r
180 val Notification notification;
\r
183 //Only logging the complete notification in debug mode
\r
185 if(log.isDebugEnabled){
\r
186 log.debug("Delivering notification {} to {}",notification,listener);
\r
188 log.trace("Delivering notification {} to {}",notification.class.name,listener);
\r
190 listener.onNotification(notification);
\r
191 if(log.isDebugEnabled){
\r
192 log.debug("Notification delivered {} to {}",notification,listener);
\r
194 log.trace("Notification delivered {} to {}",notification.class.name,listener);
\r
196 } catch (Exception e) {
\r
197 log.error("Unhandled exception thrown by listener: {}", listener, e);
\r