Merge "Bug 2697: Improvement wrong response handling, missing message"
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / NotificationHandler.java
index cc8960fb4f7233b297426dac875679d7cb509706..bc3326e1ae5b58dc23dc7c14927a83d5d34d1fcf 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.controller.sal.connect.netconf;
 
+import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import java.util.LinkedList;
 import java.util.List;
@@ -31,6 +32,7 @@ final class NotificationHandler {
     private final MessageTransformer<NetconfMessage> messageTransformer;
     private final RemoteDeviceId id;
     private boolean passNotifications = false;
+    private NotificationFilter filter;
 
     NotificationHandler(final RemoteDeviceHandler<?> salFacade, final MessageTransformer<NetconfMessage> messageTransformer, final RemoteDeviceId id) {
         this.salFacade = Preconditions.checkNotNull(salFacade);
@@ -40,7 +42,7 @@ final class NotificationHandler {
 
     synchronized void handleNotification(final NetconfMessage notification) {
         if(passNotifications) {
-            passNotification(messageTransformer.toNotification(notification));
+            passNotification(transformNotification(notification));
         } else {
             queueNotification(notification);
         }
@@ -53,12 +55,18 @@ final class NotificationHandler {
         passNotifications = true;
 
         for (final NetconfMessage cachedNotification : queue) {
-            passNotification(messageTransformer.toNotification(cachedNotification));
+            passNotification(transformNotification(cachedNotification));
         }
 
         queue.clear();
     }
 
+    private CompositeNode transformNotification(final NetconfMessage cachedNotification) {
+        final CompositeNode parsedNotification = messageTransformer.toNotification(cachedNotification);
+        Preconditions.checkNotNull(parsedNotification, "%s: Unable to parse received notification: %s", id, cachedNotification);
+        return parsedNotification;
+    }
+
     private void queueNotification(final NetconfMessage notification) {
         Preconditions.checkState(passNotifications == false);
 
@@ -70,9 +78,25 @@ final class NotificationHandler {
         queue.add(notification);
     }
 
-    private void passNotification(final CompositeNode parsedNotification) {
+    private synchronized void passNotification(final CompositeNode parsedNotification) {
         logger.debug("{}: Forwarding notification {}", id, parsedNotification);
-        Preconditions.checkNotNull(parsedNotification);
-        salFacade.onNotification(parsedNotification);
+
+        if(filter == null || filter.filterNotification(parsedNotification).isPresent()) {
+            salFacade.onNotification(parsedNotification);
+        }
+    }
+
+    synchronized void addNotificationFilter(final NotificationFilter filter) {
+        this.filter = filter;
+    }
+
+    synchronized void onRemoteSchemaDown() {
+        queue.clear();
+        passNotifications = false;
+    }
+
+    static interface NotificationFilter {
+
+        Optional<CompositeNode> filterNotification(CompositeNode notification);
     }
 }