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
25 class NotificationBrokerImpl implements NotificationProviderService, AutoCloseable {
\r
27 val Multimap<Class<? extends Notification>, NotificationListener<?>> listeners;
\r
30 var ExecutorService executor;
\r
32 new(ExecutorService executor) {
\r
33 listeners = HashMultimap.create()
\r
34 this.executor = executor;
\r
38 override <T extends Notification> addNotificationListener(Class<T> notificationType,
\r
39 NotificationListener<T> listener) {
\r
40 listeners.put(notificationType, listener)
\r
44 override <T extends Notification> removeNotificationListener(Class<T> notificationType,
\r
45 NotificationListener<T> listener) {
\r
46 listeners.remove(notificationType, listener)
\r
49 override notify(Notification notification) {
\r
50 publish(notification)
\r
53 def getNotificationTypes(Notification notification) {
\r
54 notification.class.interfaces.filter[it != Notification && Notification.isAssignableFrom(it)]
\r
57 @SuppressWarnings("unchecked")
\r
58 private def notifyAll(Collection<NotificationListener<?>> listeners, Notification notification) {
\r
59 listeners.forEach[(it as NotificationListener).onNotification(notification)]
\r
63 override addNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
\r
64 throw new UnsupportedOperationException("Deprecated method. Use registerNotificationListener instead.");
\r
69 override removeNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
\r
70 throw new UnsupportedOperationException(
\r
71 "Deprecated method. Use RegisterNotificationListener returned value to close registration.")
\r
75 override notify(Notification notification, ExecutorService service) {
\r
76 publish(notification, service)
\r
79 override publish(Notification notification) {
\r
80 publish(notification, executor)
\r
83 override publish(Notification notification, ExecutorService service) {
\r
84 val allTypes = notification.notificationTypes
\r
86 var Iterable<NotificationListener<?>> listenerToNotify = Collections.emptySet();
\r
87 for (type : allTypes) {
\r
88 listenerToNotify = listenerToNotify + listeners.get(type as Class<? extends Notification>)
\r
90 val tasks = listenerToNotify.map[new NotifyTask(it, notification)].toSet;
\r
91 executor.invokeAll(tasks);
\r
94 override <T extends Notification> registerNotificationListener(Class<T> notificationType,
\r
95 NotificationListener<T> listener) {
\r
96 val reg = new GenericNotificationRegistration<T>(notificationType, listener, this);
\r
97 listeners.put(notificationType, listener);
\r
101 override registerNotificationListener(
\r
102 org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
\r
103 val invoker = BindingAwareBrokerImpl.generator.invokerFactory.invokerFor(listener);
\r
104 for (notifyType : invoker.supportedNotifications) {
\r
105 listeners.put(notifyType, invoker.invocationProxy)
\r
107 val registration = new GeneratedListenerRegistration(listener, invoker,this);
\r
108 return registration as Registration<org.opendaylight.yangtools.yang.binding.NotificationListener>;
\r
111 protected def unregisterListener(GenericNotificationRegistration<?> reg) {
\r
112 listeners.remove(reg.type, reg.instance);
\r
115 protected def unregisterListener(GeneratedListenerRegistration reg) {
\r
116 for (notifyType : reg.invoker.supportedNotifications) {
\r
117 listeners.remove(notifyType, reg.invoker.invocationProxy)
\r
122 //FIXME: implement properly.
\r
127 class GenericNotificationRegistration<T extends Notification> extends AbstractObjectRegistration<NotificationListener<T>> implements ListenerRegistration<NotificationListener<T>> {
\r
132 var NotificationBrokerImpl notificationBroker;
\r
134 public new(Class<T> type, NotificationListener<T> instance, NotificationBrokerImpl broker) {
\r
137 notificationBroker = broker;
\r
140 override protected removeRegistration() {
\r
141 notificationBroker.unregisterListener(this);
\r
142 notificationBroker = null;
\r
146 class GeneratedListenerRegistration extends AbstractObjectRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> implements ListenerRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> {
\r
149 val NotificationInvoker invoker;
\r
151 var NotificationBrokerImpl notificationBroker;
\r
154 new(org.opendaylight.yangtools.yang.binding.NotificationListener instance, NotificationInvoker invoker, NotificationBrokerImpl broker) {
\r
156 _invoker = invoker;
\r
157 notificationBroker = broker;
\r
160 override protected removeRegistration() {
\r
161 notificationBroker.unregisterListener(this);
\r
162 notificationBroker = null;
\r
168 class NotifyTask implements Callable<Object> {
\r
170 private static val log = LoggerFactory.getLogger(NotifyTask);
\r
172 val NotificationListener listener;
\r
173 val Notification notification;
\r
177 log.info("Delivering notification {} to {}",notification,listener);
\r
178 listener.onNotification(notification);
\r
179 log.info("Notification delivered {} to {}",notification,listener);
\r
180 } catch (Exception e) {
\r
181 log.error("Unhandled exception thrown by listener: {}", listener, e);
\r