Split up MessageTransformer
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / NotificationHandler.java
index 5e9b33efab4995754839085d27dc975bb7b2a2bf..917729e7cba70b5d2060cd173d8bd4329ffb88ff 100644 (file)
@@ -7,14 +7,17 @@
  */
 package org.opendaylight.netconf.sal.connect.netconf;
 
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Preconditions.checkState;
+import static java.util.Objects.requireNonNull;
+
 import java.util.LinkedList;
 import java.util.List;
-import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
+import java.util.function.Predicate;
+import org.opendaylight.mdsal.dom.api.DOMNotification;
 import org.opendaylight.netconf.api.NetconfMessage;
 import org.opendaylight.netconf.api.xml.XmlUtil;
-import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
+import org.opendaylight.netconf.sal.connect.api.NotificationTransformer;
 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
 import org.slf4j.Logger;
@@ -24,20 +27,20 @@ import org.slf4j.LoggerFactory;
  * Handles incoming notifications. Either caches them(until onRemoteSchemaUp is called) or passes to sal Facade.
  */
 final class NotificationHandler {
-
     private static final Logger LOG = LoggerFactory.getLogger(NotificationHandler.class);
 
-    private final RemoteDeviceHandler<?> salFacade;
+    private final RemoteDeviceHandler salFacade;
+    // FIXME: better implementation?
     private final List<NetconfMessage> queue = new LinkedList<>();
     private final RemoteDeviceId id;
-    private boolean passNotifications = false;
 
+    private boolean passNotifications = false;
     private NotificationFilter filter;
-    private MessageTransformer<NetconfMessage> messageTransformer;
+    private NotificationTransformer messageTransformer;
 
-    NotificationHandler(final RemoteDeviceHandler<?> salFacade, final RemoteDeviceId id) {
-        this.salFacade = Preconditions.checkNotNull(salFacade);
-        this.id = Preconditions.checkNotNull(id);
+    NotificationHandler(final RemoteDeviceHandler salFacade, final RemoteDeviceId id) {
+        this.salFacade = requireNonNull(salFacade);
+        this.id = requireNonNull(id);
     }
 
     synchronized void handleNotification(final NetconfMessage notification) {
@@ -52,8 +55,8 @@ final class NotificationHandler {
      * Forward all cached notifications and pass all notifications from this point directly to sal facade.
      * @param transformer Message transformer
      */
-    synchronized void onRemoteSchemaUp(final MessageTransformer<NetconfMessage> transformer) {
-        this.messageTransformer = Preconditions.checkNotNull(transformer);
+    synchronized void onRemoteSchemaUp(final NotificationTransformer transformer) {
+        messageTransformer = requireNonNull(transformer);
 
         passNotifications = true;
 
@@ -65,14 +68,12 @@ final class NotificationHandler {
     }
 
     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;
+        return checkNotNull(messageTransformer.toNotification(cachedNotification),
+            "%s: Unable to parse received notification: %s", id, cachedNotification);
     }
 
     private void queueNotification(final NetconfMessage notification) {
-        Preconditions.checkState(!passNotifications);
+        checkState(!passNotifications);
 
         LOG.debug("{}: Caching notification {}, remote schema not yet fully built", id, notification);
         if (LOG.isTraceEnabled()) {
@@ -85,13 +86,13 @@ final class NotificationHandler {
     private synchronized void passNotification(final DOMNotification parsedNotification) {
         LOG.debug("{}: Forwarding notification {}", id, parsedNotification);
 
-        if (filter == null || filter.filterNotification(parsedNotification).isPresent()) {
+        if (filter == null || filter.test(parsedNotification)) {
             salFacade.onNotification(parsedNotification);
         }
     }
 
     synchronized void addNotificationFilter(final NotificationFilter newFilter) {
-        this.filter = newFilter;
+        filter = newFilter;
     }
 
     synchronized void onRemoteSchemaDown() {
@@ -100,8 +101,8 @@ final class NotificationHandler {
         messageTransformer = null;
     }
 
-    interface NotificationFilter {
+    @FunctionalInterface
+    interface NotificationFilter extends Predicate<DOMNotification> {
 
-        Optional<DOMNotification> filterNotification(DOMNotification notification);
     }
 }