Merge "Resolve Bug:707 - ConfigPusher should wait for netconf-impl to register JMX...
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / impl / NotificationBrokerImpl.xtend
index 8b0400f51254af1a2948f7c72f94bca17e0b292f..6d675b4b5eb5f546b1a7fd7743d301fb62a56f0b 100644 (file)
-/*
- * 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
-
-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) {
-        notification.notificationTypes.forEach [
-            listeners.get(it as Class<? extends Notification>)?.notifyAll(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) {
-        throw new UnsupportedOperationException("TODO: auto-generated method stub")
-    }
-}
+/*\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.ImmutableSet\r
+import com.google.common.collect.Multimap\r
+import com.google.common.collect.Multimaps\r
+import java.util.Collections\r
+import java.util.concurrent.Callable\r
+import java.util.concurrent.ExecutorService\r
+import java.util.concurrent.Future\r
+import java.util.Set\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.api.NotificationProviderService.NotificationInterestListener\r
+import org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder\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.concepts.util.ListenerRegistry\r
+import org.opendaylight.yangtools.yang.binding.Notification\r
+import org.slf4j.LoggerFactory\r
+\r
+class NotificationBrokerImpl implements NotificationProviderService, AutoCloseable {\r
+    \r
+    val ListenerRegistry<NotificationInterestListener> interestListeners = ListenerRegistry.create;\r
+    \r
+    val Multimap<Class<? extends Notification>, NotificationListener<?>> listeners;\r
+\r
+    @Property\r
+    var ExecutorService executor;\r
+    \r
+    val logger = LoggerFactory.getLogger(NotificationBrokerImpl)\r
+\r
+    new() {\r
+        listeners = Multimaps.synchronizedSetMultimap(HashMultimap.create())\r
+    }\r
+\r
+    @Deprecated\r
+    new(ExecutorService executor) {\r
+        listeners = Multimaps.synchronizedSetMultimap(HashMultimap.create())\r
+        this.executor = executor;\r
+    }\r
+\r
+    def getNotificationTypes(Notification notification) {\r
+        notification.class.interfaces.filter[it != Notification && Notification.isAssignableFrom(it)]\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<? extends Object>> 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
+        submitAll(executor,tasks);\r
+    }\r
+    \r
+    def submitAll(ExecutorService service, Set<NotifyTask> tasks) {\r
+        val ret = ImmutableSet.<Future<Object>>builder();\r
+        for(task : tasks) {\r
+            ret.add(service.submit(task));\r
+        }\r
+        return ret.build();\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
+        announceNotificationSubscription(notificationType);\r
+        return reg;\r
+    }\r
+    \r
+    def announceNotificationSubscription(Class<? extends Notification> notification) {\r
+        for (listener : interestListeners) {\r
+            try {\r
+                listener.instance.onNotificationSubscribtion(notification);\r
+            } catch (Exception e) {\r
+                logger.error("", e.message)\r
+            }\r
+        }\r
+    }\r
+\r
+    override registerNotificationListener(\r
+        org.opendaylight.yangtools.yang.binding.NotificationListener listener) {\r
+        val invoker = SingletonHolder.INVOKER_FACTORY.invokerFor(listener);\r
+        for (notifyType : invoker.supportedNotifications) {\r
+            listeners.put(notifyType, invoker.invocationProxy)\r
+            announceNotificationSubscription(notifyType)\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
+    override registerInterestListener(NotificationInterestListener interestListener) {\r
+        val registration = interestListeners.register(interestListener);\r
+        \r
+        for(notification : listeners.keySet) {\r
+            interestListener.onNotificationSubscribtion(notification);\r
+        }\r
+        return registration\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
+    @SuppressWarnings("rawtypes")\r
+    val NotificationListener listener;\r
+    val Notification notification;\r
+\r
+    override call() {\r
+        //Only logging the complete notification in debug mode\r
+        try {\r
+            if(log.isDebugEnabled){\r
+                log.debug("Delivering notification {} to {}",notification,listener);\r
+            } else {\r
+                log.trace("Delivering notification {} to {}",notification.class.name,listener);\r
+            }\r
+            listener.onNotification(notification);\r
+            if(log.isDebugEnabled){\r
+                log.debug("Notification delivered {} to {}",notification,listener);\r
+            } else {\r
+                log.trace("Notification delivered {} to {}",notification.class.name,listener);\r
+            }\r
+        } catch (Exception e) {\r
+            log.error("Unhandled exception thrown by listener: {}", listener, e);\r
+        }\r
+        return null;\r
+    }\r
+\r
+}\r