BUG-3095 Add EventTime attribute to DOMNotification from netconf
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / NotificationHandler.java
index 80451a1027713174d66a90ead14bd184ee4a3721..2d47c6ae1bbe11f5863d66c97db5301208fa29ae 100644 (file)
@@ -11,12 +11,12 @@ import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import java.util.LinkedList;
 import java.util.List;
+import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
 import org.opendaylight.controller.netconf.api.NetconfMessage;
 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
 import org.opendaylight.controller.sal.connect.api.MessageTransformer;
 import org.opendaylight.controller.sal.connect.api.RemoteDeviceHandler;
 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
-import org.opendaylight.yangtools.yang.data.api.CompositeNode;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -29,20 +29,20 @@ final class NotificationHandler {
 
     private final RemoteDeviceHandler<?> salFacade;
     private final List<NetconfMessage> queue = new LinkedList<>();
-    private final MessageTransformer<NetconfMessage> messageTransformer;
     private final RemoteDeviceId id;
     private boolean passNotifications = false;
+
     private NotificationFilter filter;
+    private MessageTransformer<NetconfMessage> messageTransformer;
 
-    NotificationHandler(final RemoteDeviceHandler<?> salFacade, final MessageTransformer<NetconfMessage> messageTransformer, final RemoteDeviceId id) {
+    NotificationHandler(final RemoteDeviceHandler<?> salFacade, final RemoteDeviceId id) {
         this.salFacade = Preconditions.checkNotNull(salFacade);
-        this.messageTransformer = Preconditions.checkNotNull(messageTransformer);
         this.id = Preconditions.checkNotNull(id);
     }
 
     synchronized void handleNotification(final NetconfMessage notification) {
         if(passNotifications) {
-            passNotification(messageTransformer.toNotification(notification));
+            passNotification(transformNotification(notification));
         } else {
             queueNotification(notification);
         }
@@ -50,20 +50,26 @@ final class NotificationHandler {
 
     /**
      * Forward all cached notifications and pass all notifications from this point directly to sal facade.
+     * @param messageTransformer
      */
-    synchronized void onRemoteSchemaUp() {
+    synchronized void onRemoteSchemaUp(final MessageTransformer<NetconfMessage> messageTransformer) {
+        this.messageTransformer = Preconditions.checkNotNull(messageTransformer);
+
         passNotifications = true;
 
         for (final NetconfMessage cachedNotification : queue) {
-            final CompositeNode parsedNotification = messageTransformer.toNotification(cachedNotification);
-            // TODO possible race condition here, because this exception is thrown occasionally
-            Preconditions.checkNotNull(parsedNotification, "Unable to parse received notification %s", cachedNotification);
-            passNotification(parsedNotification);
+            passNotification(transformNotification(cachedNotification));
         }
 
         queue.clear();
     }
 
+    private DOMNotification transformNotification(final NetconfMessage cachedNotification) {
+        final DOMNotification 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);
 
@@ -75,7 +81,7 @@ final class NotificationHandler {
         queue.add(notification);
     }
 
-    private synchronized void passNotification(final CompositeNode parsedNotification) {
+    private synchronized void passNotification(final DOMNotification parsedNotification) {
         logger.debug("{}: Forwarding notification {}", id, parsedNotification);
 
         if(filter == null || filter.filterNotification(parsedNotification).isPresent()) {
@@ -87,8 +93,14 @@ final class NotificationHandler {
         this.filter = filter;
     }
 
+    synchronized void onRemoteSchemaDown() {
+        queue.clear();
+        passNotifications = false;
+        messageTransformer = null;
+    }
+
     static interface NotificationFilter {
 
-        Optional<CompositeNode> filterNotification(CompositeNode notification);
+        Optional<DOMNotification> filterNotification(DOMNotification notification);
     }
 }