-/*
- * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.controller.sal.binding.impl
-
-import org.opendaylight.controller.sal.binding.api.NotificationProviderService
-import org.opendaylight.yangtools.yang.binding.Notification
-import com.google.common.collect.Multimap
-import org.opendaylight.controller.sal.binding.api.NotificationListener
-import com.google.common.collect.HashMultimap
-import java.util.concurrent.ExecutorService
-import java.util.Collection
-import org.opendaylight.yangtools.concepts.Registration
-
-class NotificationBrokerImpl implements NotificationProviderService {
-
- val Multimap<Class<? extends Notification>, NotificationListener<?>> listeners;
- val ExecutorService executor;
-
- new(ExecutorService executor) {
- listeners = HashMultimap.create()
- this.executor = executor;
- }
-
- override <T extends Notification> addNotificationListener(Class<T> notificationType,
- NotificationListener<T> listener) {
- listeners.put(notificationType, listener)
- }
-
- override <T extends Notification> removeNotificationListener(Class<T> notificationType,
- NotificationListener<T> listener) {
- listeners.remove(notificationType, listener)
- }
-
- override notify(Notification notification) {
- publish(notification)
- }
-
- def getNotificationTypes(Notification notification) {
- notification.class.interfaces.filter[it != Notification && Notification.isAssignableFrom(it)]
- }
-
- @SuppressWarnings("unchecked")
- def notifyAll(Collection<NotificationListener<?>> listeners, Notification notification) {
- listeners.forEach[(it as NotificationListener).onNotification(notification)]
- }
-
- override addNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
- throw new UnsupportedOperationException("TODO: auto-generated method stub")
-
- }
-
- override removeNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
- throw new UnsupportedOperationException("TODO: auto-generated method stub")
- }
-
- override notify(Notification notification, ExecutorService service) {
- publish(notification)
- }
-
- override publish(Notification notification) {
- notification.notificationTypes.forEach [
- listeners.get(it as Class<? extends Notification>)?.notifyAll(notification)
- ]
- }
-
- override publish(Notification notification, ExecutorService service) {
- publish(notification)
- }
-
- override <T extends Notification> registerNotificationListener(Class<T> notificationType,
- NotificationListener<T> listener) {
- val reg = new GenericNotificationRegistration<T>(notificationType,listener,this);
- listeners.put(notificationType,listener);
- return reg;
- }
-
- override registerNotificationListener(
- org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
-
- }
-
-
- protected def unregisterListener(GenericNotificationRegistration<?> reg) {
- listeners.remove(reg.type,reg.instance);
- }
-}
-class GenericNotificationRegistration<T extends Notification> implements Registration<NotificationListener<T>> {
-
- @Property
- var NotificationListener<T> instance;
-
- @Property
- val Class<T> type;
-
-
- val NotificationBrokerImpl notificationBroker;
-
- public new(Class<T> type, NotificationListener<T> instance,NotificationBrokerImpl broker) {
- _instance = instance;
- _type = type;
- notificationBroker = broker;
- }
-
- override close() {
- notificationBroker.unregisterListener(this);
- }
-}
+/*\r
+ * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.\r
+ *\r
+ * This program and the accompanying materials are made available under the\r
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
+ * and is available at http://www.eclipse.org/legal/epl-v10.html\r
+ */\r
+package org.opendaylight.controller.sal.binding.impl\r
+\r
+import com.google.common.collect.HashMultimap\r
+import com.google.common.collect.Multimap\r
+import java.util.Collection\r
+import java.util.Collections\r
+import java.util.concurrent.Callable\r
+import java.util.concurrent.ExecutorService\r
+import org.opendaylight.controller.sal.binding.api.NotificationListener\r
+import org.opendaylight.controller.sal.binding.api.NotificationProviderService\r
+import org.opendaylight.controller.sal.binding.spi.NotificationInvokerFactory.NotificationInvoker\r
+import org.opendaylight.yangtools.concepts.AbstractObjectRegistration\r
+import org.opendaylight.yangtools.concepts.ListenerRegistration\r
+import org.opendaylight.yangtools.concepts.Registration\r
+import org.opendaylight.yangtools.yang.binding.Notification\r
+import org.slf4j.LoggerFactory\r
+\r
+class NotificationBrokerImpl implements NotificationProviderService, AutoCloseable {\r
+\r
+ val Multimap<Class<? extends Notification>, NotificationListener<?>> listeners;\r
+\r
+ @Property\r
+ var ExecutorService executor;\r
+\r
+ new(ExecutorService executor) {\r
+ listeners = HashMultimap.create()\r
+ this.executor = executor;\r
+ }\r
+\r
+ @Deprecated\r
+ override <T extends Notification> addNotificationListener(Class<T> notificationType,\r
+ NotificationListener<T> listener) {\r
+ listeners.put(notificationType, listener)\r
+ }\r
+\r
+ @Deprecated\r
+ override <T extends Notification> removeNotificationListener(Class<T> notificationType,\r
+ NotificationListener<T> listener) {\r
+ listeners.remove(notificationType, listener)\r
+ }\r
+\r
+ override notify(Notification notification) {\r
+ publish(notification)\r
+ }\r
+\r
+ def getNotificationTypes(Notification notification) {\r
+ notification.class.interfaces.filter[it != Notification && Notification.isAssignableFrom(it)]\r
+ }\r
+\r
+ @SuppressWarnings("unchecked")\r
+ private def notifyAll(Collection<NotificationListener<?>> listeners, Notification notification) {\r
+ listeners.forEach[(it as NotificationListener).onNotification(notification)]\r
+ }\r
+\r
+ @Deprecated\r
+ override addNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {\r
+ throw new UnsupportedOperationException("Deprecated method. Use registerNotificationListener instead.");\r
+\r
+ }\r
+\r
+ @Deprecated\r
+ override removeNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {\r
+ throw new UnsupportedOperationException(\r
+ "Deprecated method. Use RegisterNotificationListener returned value to close registration.")\r
+ }\r
+\r
+ @Deprecated\r
+ override notify(Notification notification, ExecutorService service) {\r
+ publish(notification, service)\r
+ }\r
+\r
+ override publish(Notification notification) {\r
+ publish(notification, executor)\r
+ }\r
+\r
+ override publish(Notification notification, ExecutorService service) {\r
+ val allTypes = notification.notificationTypes\r
+\r
+ var Iterable<NotificationListener<?>> listenerToNotify = Collections.emptySet();\r
+ for (type : allTypes) {\r
+ listenerToNotify = listenerToNotify + listeners.get(type as Class<? extends Notification>)\r
+ }\r
+ val tasks = listenerToNotify.map[new NotifyTask(it, notification)].toSet;\r
+ executor.invokeAll(tasks);\r
+ }\r
+\r
+ override <T extends Notification> registerNotificationListener(Class<T> notificationType,\r
+ NotificationListener<T> listener) {\r
+ val reg = new GenericNotificationRegistration<T>(notificationType, listener, this);\r
+ listeners.put(notificationType, listener);\r
+ return reg;\r
+ }\r
+\r
+ override registerNotificationListener(\r
+ org.opendaylight.yangtools.yang.binding.NotificationListener listener) {\r
+ val invoker = BindingAwareBrokerImpl.generator.invokerFactory.invokerFor(listener);\r
+ for (notifyType : invoker.supportedNotifications) {\r
+ listeners.put(notifyType, invoker.invocationProxy)\r
+ }\r
+ val registration = new GeneratedListenerRegistration(listener, invoker,this);\r
+ return registration as Registration<org.opendaylight.yangtools.yang.binding.NotificationListener>;\r
+ }\r
+\r
+ protected def unregisterListener(GenericNotificationRegistration<?> reg) {\r
+ listeners.remove(reg.type, reg.instance);\r
+ }\r
+\r
+ protected def unregisterListener(GeneratedListenerRegistration reg) {\r
+ for (notifyType : reg.invoker.supportedNotifications) {\r
+ listeners.remove(notifyType, reg.invoker.invocationProxy)\r
+ }\r
+ }\r
+ \r
+ override close() {\r
+ //FIXME: implement properly.\r
+ }\r
+ \r
+}\r
+\r
+class GenericNotificationRegistration<T extends Notification> extends AbstractObjectRegistration<NotificationListener<T>> implements ListenerRegistration<NotificationListener<T>> {\r
+\r
+ @Property\r
+ val Class<T> type;\r
+\r
+ var NotificationBrokerImpl notificationBroker;\r
+\r
+ public new(Class<T> type, NotificationListener<T> instance, NotificationBrokerImpl broker) {\r
+ super(instance);\r
+ _type = type;\r
+ notificationBroker = broker;\r
+ }\r
+\r
+ override protected removeRegistration() {\r
+ notificationBroker.unregisterListener(this);\r
+ notificationBroker = null;\r
+ }\r
+}\r
+\r
+class GeneratedListenerRegistration extends AbstractObjectRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> implements ListenerRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> {\r
+\r
+ @Property\r
+ val NotificationInvoker invoker;\r
+ \r
+ var NotificationBrokerImpl notificationBroker;\r
+ \r
+\r
+ new(org.opendaylight.yangtools.yang.binding.NotificationListener instance, NotificationInvoker invoker, NotificationBrokerImpl broker) {\r
+ super(instance);\r
+ _invoker = invoker;\r
+ notificationBroker = broker;\r
+ }\r
+\r
+ override protected removeRegistration() {\r
+ notificationBroker.unregisterListener(this);\r
+ notificationBroker = null;\r
+ invoker.close();\r
+ }\r
+}\r
+\r
+@Data\r
+class NotifyTask implements Callable<Object> {\r
+\r
+ private static val log = LoggerFactory.getLogger(NotifyTask);\r
+\r
+ val NotificationListener listener;\r
+ val Notification notification;\r
+\r
+ override call() {\r
+ try {\r
+ log.info("Delivering notification {} to {}",notification,listener);\r
+ listener.onNotification(notification);\r
+ log.info("Notification delivered {} to {}",notification,listener);\r
+ } catch (Exception e) {\r
+ log.error("Unhandled exception thrown by listener: {}", listener, e);\r
+ }\r
+ return null;\r
+ }\r
+\r
+}\r