Bug 3959 - support netconf notification
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / netconf / sal / streams / listeners / Notificator.java
index 9537732133e011180f0bf054cead560d06f9b6dc..0bd38652b766634f4b28088a4210d0c005beb2e7 100644 (file)
@@ -7,12 +7,17 @@
  */
 package org.opendaylight.netconf.sal.streams.listeners;
 
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * {@link Notificator} is responsible to create, remove and find
@@ -21,6 +26,9 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 public class Notificator {
 
     private static Map<String, ListenerAdapter> listenersByStreamName = new ConcurrentHashMap<>();
+    private static Map<String, List<NotificationListenerAdapter>> notificationListenersByStreamName = new ConcurrentHashMap<>();
+
+    private static final Logger LOG = LoggerFactory.getLogger(Notificator.class);
     private static final Lock lock = new ReentrantLock();
 
     private Notificator() {
@@ -40,7 +48,7 @@ public class Notificator {
      *            The name of the stream.
      * @return {@link ListenerAdapter} specified by stream name.
      */
-    public static ListenerAdapter getListenerFor(String streamName) {
+    public static ListenerAdapter getListenerFor(final String streamName) {
         return listenersByStreamName.get(streamName);
     }
 
@@ -50,7 +58,7 @@ public class Notificator {
      * @param streamName
      * @return True if the listener exist, false otherwise.
      */
-    public static boolean existListenerFor(String streamName) {
+    public static boolean existListenerFor(final String streamName) {
         return listenersByStreamName.containsKey(streamName);
     }
 
@@ -63,8 +71,8 @@ public class Notificator {
      *            The name of the stream.
      * @return New {@link ListenerAdapter} listener from {@link YangInstanceIdentifier} path and stream name.
      */
-    public static ListenerAdapter createListener(YangInstanceIdentifier path, String streamName) {
-        ListenerAdapter listener = new ListenerAdapter(path, streamName);
+    public static ListenerAdapter createListener(final YangInstanceIdentifier path, final String streamName) {
+        final ListenerAdapter listener = new ListenerAdapter(path, streamName);
         try {
             lock.lock();
             listenersByStreamName.put(streamName, listener);
@@ -82,7 +90,7 @@ public class Notificator {
      *            URI for creation stream name.
      * @return String representation of stream name.
      */
-    public static String createStreamNameFromUri(String uri) {
+    public static String createStreamNameFromUri(final String uri) {
         if (uri == null) {
             return null;
         }
@@ -100,10 +108,11 @@ public class Notificator {
      * Removes all listeners.
      */
     public static void removeAllListeners() {
-        for (ListenerAdapter listener : listenersByStreamName.values()) {
+        for (final ListenerAdapter listener : listenersByStreamName.values()) {
             try {
                 listener.close();
-            } catch (Exception e) {
+            } catch (final Exception e) {
+                LOG.error("Failed to close listener", e);
             }
         }
         try {
@@ -120,7 +129,7 @@ public class Notificator {
      * @param listener
      *            ListenerAdapter
      */
-    public static void removeListenerIfNoSubscriberExists(ListenerAdapter listener) {
+    public static void removeListenerIfNoSubscriberExists(final ListenerAdapter listener) {
         if (!listener.hasSubscribers()) {
             deleteListener(listener);
         }
@@ -132,11 +141,12 @@ public class Notificator {
      * @param listener
      *            ListenerAdapter
      */
-    private static void deleteListener(ListenerAdapter listener) {
+    private static void deleteListener(final ListenerAdapter listener) {
         if (listener != null) {
             try {
                 listener.close();
-            } catch (Exception e) {
+            } catch (final Exception e) {
+                LOG.error("Failed to close listener", e);
             }
             try {
                 lock.lock();
@@ -147,4 +157,57 @@ public class Notificator {
         }
     }
 
+    /**
+     * Check if the listener specified by qnames of request exist.
+     *
+     * @param streamName
+     *            - name of stream
+     * @return True if the listener exist, false otherwise.
+     */
+    public static boolean existNotificationListenerFor(final String streamName) {
+        return notificationListenersByStreamName.containsKey(streamName);
+
+    }
+
+    public static List<NotificationListenerAdapter> createNotificationListener(final List<SchemaPath> paths,
+            final String streamName, final String outputType) {
+        final List<NotificationListenerAdapter> listListeners = new ArrayList<>();
+        for (final SchemaPath path : paths) {
+            final NotificationListenerAdapter listener = new NotificationListenerAdapter(path, streamName, outputType);
+            listListeners.add(listener);
+        }
+        try {
+            lock.lock();
+            notificationListenersByStreamName.put(streamName, listListeners);
+        } finally {
+            lock.unlock();
+        }
+        return listListeners;
+    }
+
+    public static void removeNotificationListenerIfNoSubscriberExists(final NotificationListenerAdapter listener) {
+        if (!listener.hasSubscribers()) {
+            deleteNotificationListener(listener);
+        }
+    }
+
+    private static void deleteNotificationListener(final NotificationListenerAdapter listener) {
+        if (listener != null) {
+            try {
+                listener.close();
+            } catch (final Exception e) {
+                LOG.error("Failed to close listener", e);
+            }
+            try {
+                lock.lock();
+                notificationListenersByStreamName.remove(listener.getStreamName());
+            } finally {
+                lock.unlock();
+            }
+        }
+    }
+
+    public static List<NotificationListenerAdapter> getNotificationListenerFor(final String streamName) {
+        return notificationListenersByStreamName.get(streamName);
+    }
 }