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
\rimport org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder
25 class NotificationBrokerImpl implements NotificationProviderService, AutoCloseable {
\r
27 val Multimap<Class<? extends Notification>, NotificationListener<?>> listeners;
\r
30 var ExecutorService executor;
\r
33 listeners = HashMultimap.create()
\r
37 new(ExecutorService executor) {
\r
38 listeners = HashMultimap.create()
\r
39 this.executor = executor;
\r
43 override <T extends Notification> addNotificationListener(Class<T> notificationType,
\r
44 NotificationListener<T> listener) {
\r
45 listeners.put(notificationType, listener)
\r
49 override <T extends Notification> removeNotificationListener(Class<T> notificationType,
\r
50 NotificationListener<T> listener) {
\r
51 listeners.remove(notificationType, listener)
\r
54 override notify(Notification notification) {
\r
55 publish(notification)
\r
58 def getNotificationTypes(Notification notification) {
\r
59 notification.class.interfaces.filter[it != Notification && Notification.isAssignableFrom(it)]
\r
62 @SuppressWarnings("unchecked")
\r
63 private def notifyAll(Collection<NotificationListener<?>> listeners, Notification notification) {
\r
64 listeners.forEach[(it as NotificationListener).onNotification(notification)]
\r
68 override addNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
\r
69 throw new UnsupportedOperationException("Deprecated method. Use registerNotificationListener instead.");
\r
74 override removeNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
\r
75 throw new UnsupportedOperationException(
\r
76 "Deprecated method. Use RegisterNotificationListener returned value to close registration.")
\r
80 override notify(Notification notification, ExecutorService service) {
\r
81 publish(notification, service)
\r
84 override publish(Notification notification) {
\r
85 publish(notification, executor)
\r
88 override publish(Notification notification, ExecutorService service) {
\r
89 val allTypes = notification.notificationTypes
\r
91 var Iterable<NotificationListener<?>> listenerToNotify = Collections.emptySet();
\r
92 for (type : allTypes) {
\r
93 listenerToNotify = listenerToNotify + listeners.get(type as Class<? extends Notification>)
\r
95 val tasks = listenerToNotify.map[new NotifyTask(it, notification)].toSet;
\r
96 executor.invokeAll(tasks);
\r
99 override <T extends Notification> registerNotificationListener(Class<T> notificationType,
\r
100 NotificationListener<T> listener) {
\r
101 val reg = new GenericNotificationRegistration<T>(notificationType, listener, this);
\r
102 listeners.put(notificationType, listener);
\r
106 override registerNotificationListener(
\r
107 org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
\r
108 val invoker = SingletonHolder.INVOKER_FACTORY.invokerFor(listener);
\r
109 for (notifyType : invoker.supportedNotifications) {
\r
110 listeners.put(notifyType, invoker.invocationProxy)
\r
112 val registration = new GeneratedListenerRegistration(listener, invoker,this);
\r
113 return registration as Registration<org.opendaylight.yangtools.yang.binding.NotificationListener>;
\r
116 protected def unregisterListener(GenericNotificationRegistration<?> reg) {
\r
117 listeners.remove(reg.type, reg.instance);
\r
120 protected def unregisterListener(GeneratedListenerRegistration reg) {
\r
121 for (notifyType : reg.invoker.supportedNotifications) {
\r
122 listeners.remove(notifyType, reg.invoker.invocationProxy)
\r
127 //FIXME: implement properly.
\r
132 class GenericNotificationRegistration<T extends Notification> extends AbstractObjectRegistration<NotificationListener<T>> implements ListenerRegistration<NotificationListener<T>> {
\r
137 var NotificationBrokerImpl notificationBroker;
\r
139 public new(Class<T> type, NotificationListener<T> instance, NotificationBrokerImpl broker) {
\r
142 notificationBroker = broker;
\r
145 override protected removeRegistration() {
\r
146 notificationBroker.unregisterListener(this);
\r
147 notificationBroker = null;
\r
151 class GeneratedListenerRegistration extends AbstractObjectRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> implements ListenerRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> {
\r
154 val NotificationInvoker invoker;
\r
156 var NotificationBrokerImpl notificationBroker;
\r
159 new(org.opendaylight.yangtools.yang.binding.NotificationListener instance, NotificationInvoker invoker, NotificationBrokerImpl broker) {
\r
161 _invoker = invoker;
\r
162 notificationBroker = broker;
\r
165 override protected removeRegistration() {
\r
166 notificationBroker.unregisterListener(this);
\r
167 notificationBroker = null;
\r
173 class NotifyTask implements Callable<Object> {
\r
175 private static val log = LoggerFactory.getLogger(NotifyTask);
\r
177 val NotificationListener listener;
\r
178 val Notification notification;
\r
182 log.info("Delivering notification {} to {}",notification,listener);
\r
183 listener.onNotification(notification);
\r
184 log.info("Notification delivered {} to {}",notification,listener);
\r
185 } catch (Exception e) {
\r
186 log.error("Unhandled exception thrown by listener: {}", listener, e);
\r