Added Google Guava as dependency. Replaced Map<K,List<V> for SAL listeners.
[controller.git] / opendaylight / sal / yang-prototype / sal / sal-broker-impl / src / main / java / org / opendaylight / controller / sal / core / impl / notify / NotificationModule.java
index fd19e7e6bc2ef50bd8c787bcdee94a04be810189..8f9632be9e3a843a1d15f5b38c78640cbfe883e2 100644 (file)
@@ -7,10 +7,9 @@
  */\r
 package org.opendaylight.controller.sal.core.impl.notify;\r
 \r
+import java.util.Collection;\r
 import java.util.Collections;\r
-import java.util.HashMap;\r
 import java.util.HashSet;\r
-import java.util.List;\r
 import java.util.Map;\r
 import java.util.Map.Entry;\r
 import java.util.Set;\r
@@ -24,28 +23,30 @@ import org.opendaylight.controller.sal.core.api.notify.NotificationListener;
 import org.opendaylight.controller.sal.core.api.notify.NotificationProviderService;\r
 import org.opendaylight.controller.sal.core.api.notify.NotificationService;\r
 import org.opendaylight.controller.sal.core.impl.BrokerServiceImpl;\r
-import org.opendaylight.controller.sal.core.impl.Utils;\r
 import org.opendaylight.controller.sal.core.spi.BrokerModule;\r
 import org.opendaylight.controller.yang.common.QName;\r
 import org.opendaylight.controller.yang.data.api.CompositeNode;\r
 import org.slf4j.Logger;\r
 import org.slf4j.LoggerFactory;\r
 \r
-\r
+import com.google.common.collect.HashMultimap;\r
+import com.google.common.collect.ImmutableSet;\r
+import com.google.common.collect.Multimap;\r
 \r
 public class NotificationModule implements BrokerModule {\r
     private static Logger log = LoggerFactory\r
             .getLogger(NotificationModule.class);\r
 \r
-    private Map<QName, List<NotificationListener>> listeners = new HashMap<QName, List<NotificationListener>>();\r
+    private Multimap<QName, NotificationListener> listeners = HashMultimap\r
+            .create();\r
+\r
+    private static final Set<Class<? extends BrokerService>> providedServices = ImmutableSet\r
+            .of((Class<? extends BrokerService>) NotificationService.class,\r
+                    NotificationProviderService.class);\r
 \r
     @Override\r
     public Set<Class<? extends BrokerService>> getProvidedServices() {\r
-        // FIXME Refactor\r
-        Set<Class<? extends BrokerService>> ret = new HashSet<Class<? extends BrokerService>>();\r
-        ret.add(NotificationService.class);\r
-        ret.add(NotificationProviderService.class);\r
-        return ret;\r
+        return providedServices;\r
     }\r
 \r
     @Override\r
@@ -77,7 +78,7 @@ public class NotificationModule implements BrokerModule {
 \r
     private void sendNotification(CompositeNode notification) {\r
         QName type = notification.getNodeType();\r
-        List<NotificationListener> toNotify = listeners.get(type);\r
+        Collection<NotificationListener> toNotify = listeners.get(type);\r
         log.info("Publishing notification " + type);\r
 \r
         if (toNotify == null) {\r
@@ -109,7 +110,8 @@ public class NotificationModule implements BrokerModule {
     private class NotificationConsumerSessionImpl extends BrokerServiceImpl\r
             implements NotificationService {\r
 \r
-        Map<QName, List<NotificationListener>> consumerListeners = new HashMap<QName, List<NotificationListener>>();\r
+        private Multimap<QName, NotificationListener> consumerListeners = HashMultimap\r
+                .create();\r
         private boolean closed = false;\r
 \r
         @Override\r
@@ -124,8 +126,8 @@ public class NotificationModule implements BrokerModule {
                 throw new IllegalArgumentException("Listener must not be null.");\r
             }\r
 \r
-            Utils.addToMap(consumerListeners, notification, listener);\r
-            Utils.addToMap(listeners, notification, listener);\r
+            consumerListeners.put(notification, listener);\r
+            listeners.put(notification, listener);\r
             log.info("Registered listener for notification: " + notification);\r
         }\r
 \r
@@ -140,17 +142,18 @@ public class NotificationModule implements BrokerModule {
             if (listener == null) {\r
                 throw new IllegalArgumentException("Listener must not be null.");\r
             }\r
-            Utils.removeFromMap(consumerListeners, notification, listener);\r
-            Utils.removeFromMap(listeners, notification, listener);\r
+            consumerListeners.remove(notification, listener);\r
+            listeners.remove(notification, listener);\r
         }\r
 \r
         @Override\r
         public void closeSession() {\r
             closed = true;\r
-            Set<Entry<QName, List<NotificationListener>>> toRemove = consumerListeners\r
-                    .entrySet();\r
-            for (Entry<QName, List<NotificationListener>> entry : toRemove) {\r
-                listeners.get(entry.getKey()).removeAll(entry.getValue());\r
+            Map<QName, Collection<NotificationListener>> toRemove = consumerListeners\r
+                    .asMap();\r
+            for (Entry<QName, Collection<NotificationListener>> entry : toRemove\r
+                    .entrySet()) {\r
+                listeners.remove(entry.getKey(), entry.getValue());\r
             }\r
         }\r
 \r
@@ -179,4 +182,3 @@ public class NotificationModule implements BrokerModule {
         return Collections.emptySet();\r
     }\r
 }\r
-