Changed multimaps to synchronized multimaps
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / impl / NotificationBrokerImpl.xtend
index cf339ee4f474e032ff8aca805f58eb5fe1f97d3b..52aa8d029066b3093a90300f6bb078886b2b3d7b 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
-import org.opendaylight.yangtools.concepts.Registration
-import org.opendaylight.controller.sal.binding.codegen.RuntimeCodeGenerator
-import org.opendaylight.controller.sal.binding.spi.NotificationInvokerFactory
-import org.opendaylight.yangtools.concepts.ListenerRegistration
-import org.opendaylight.controller.sal.binding.spi.NotificationInvokerFactory.NotificationInvoker
-import org.opendaylight.yangtools.concepts.AbstractObjectRegistration
-import java.util.Collections
-import org.slf4j.LoggerFactory
-import java.util.concurrent.Callable
-
-class NotificationBrokerImpl implements NotificationProviderService, AutoCloseable {
-
-    val Multimap<Class<? extends Notification>, NotificationListener<?>> listeners;
-
-    @Property
-    var ExecutorService executor;
-
-    new(ExecutorService executor) {
-        listeners = HashMultimap.create()
-        this.executor = executor;
-    }
-
-    @Deprecated
-    override <T extends Notification> addNotificationListener(Class<T> notificationType,
-        NotificationListener<T> listener) {
-        listeners.put(notificationType, listener)
-    }
-
-    @Deprecated
-    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")
-    private def notifyAll(Collection<NotificationListener<?>> listeners, Notification notification) {
-        listeners.forEach[(it as NotificationListener).onNotification(notification)]
-    }
-
-    @Deprecated
-    override addNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
-        throw new UnsupportedOperationException("Deprecated method. Use registerNotificationListener instead.");
-
-    }
-
-    @Deprecated
-    override removeNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
-        throw new UnsupportedOperationException(
-            "Deprecated method. Use RegisterNotificationListener returned value to close registration.")
-    }
-
-    @Deprecated
-    override notify(Notification notification, ExecutorService service) {
-        publish(notification, service)
-    }
-
-    override publish(Notification notification) {
-        publish(notification, executor)
-    }
-
-    override publish(Notification notification, ExecutorService service) {
-        val allTypes = notification.notificationTypes
-
-        var Iterable<NotificationListener<?>> listenerToNotify = Collections.emptySet();
-        for (type : allTypes) {
-            listenerToNotify = listenerToNotify + listeners.get(type as Class<? extends Notification>)
-        }
-        val tasks = listenerToNotify.map[new NotifyTask(it, notification)].toSet;
-        executor.invokeAll(tasks);
-    }
-
-    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) {
-        val invoker = BindingAwareBrokerImpl.generator.invokerFactory.invokerFor(listener);
-        for (notifyType : invoker.supportedNotifications) {
-            listeners.put(notifyType, invoker.invocationProxy)
-        }
-        val registration = new GeneratedListenerRegistration(listener, invoker,this);
-        return registration as Registration<org.opendaylight.yangtools.yang.binding.NotificationListener>;
-    }
-
-    protected def unregisterListener(GenericNotificationRegistration<?> reg) {
-        listeners.remove(reg.type, reg.instance);
-    }
-
-    protected def unregisterListener(GeneratedListenerRegistration reg) {
-        for (notifyType : reg.invoker.supportedNotifications) {
-            listeners.remove(notifyType, reg.invoker.invocationProxy)
-        }
-    }
-    
-    override close()  {
-        //FIXME: implement properly.
-    }
-    
-}
-
-class GenericNotificationRegistration<T extends Notification> extends AbstractObjectRegistration<NotificationListener<T>> implements ListenerRegistration<NotificationListener<T>> {
-
-    @Property
-    val Class<T> type;
-
-    var NotificationBrokerImpl notificationBroker;
-
-    public new(Class<T> type, NotificationListener<T> instance, NotificationBrokerImpl broker) {
-        super(instance);
-        _type = type;
-        notificationBroker = broker;
-    }
-
-    override protected removeRegistration() {
-        notificationBroker.unregisterListener(this);
-        notificationBroker = null;
-    }
-}
-
-class GeneratedListenerRegistration extends AbstractObjectRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> implements ListenerRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> {
-
-    @Property
-    val NotificationInvoker invoker;
-    
-    var NotificationBrokerImpl notificationBroker;
-    
-
-    new(org.opendaylight.yangtools.yang.binding.NotificationListener instance, NotificationInvoker invoker, NotificationBrokerImpl broker) {
-        super(instance);
-        _invoker = invoker;
-        notificationBroker = broker;
-    }
-
-    override protected removeRegistration() {
-        notificationBroker.unregisterListener(this);
-        notificationBroker = null;
-        invoker.close();
-    }
-}
-
-@Data
-class NotifyTask implements Callable<Object> {
-
-    private static val log = LoggerFactory.getLogger(NotifyTask);
-
-    val NotificationListener listener;
-    val Notification notification;
-
-    override call() {
-        try {
-            log.info("Delivering notification {} to {}",notification,listener);
-            listener.onNotification(notification);
-            log.info("Notification delivered {} to {}",notification,listener);
-        } catch (Exception e) {
-            log.error("Unhandled exception thrown by listener: {}", listener, e);
-        }
-        return null;
-    }
-
-}
+/*\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
+import org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder\rimport com.google.common.collect.Multimaps
+
+class NotificationBrokerImpl implements NotificationProviderService, AutoCloseable {\r
+\r
+    val Multimap<Class<? extends Notification>, NotificationListener<?>> listeners;\r
+\r
+    @Property\r
+    var ExecutorService executor;\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
+    @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 = SingletonHolder.INVOKER_FACTORY.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
+    @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