Check time expiry before attempting to format notification 62/79762/2
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 20 Jan 2019 16:34:20 +0000 (17:34 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 20 Jan 2019 19:08:58 +0000 (20:08 +0100)
If the listener is not started yet, or it has stopped, we should
not try to format the document. This splits the checking into two
phases, improving things and laying the foundation for a complete
factoring out the common bits.

Change-Id: Ibff73a4979a5d67385f10647ca7c9ab64a35f42a
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/streams/listeners/AbstractQueryParams.java
restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/streams/listeners/ListenerAdapter.java
restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/streams/listeners/NotificationListenerAdapter.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/listeners/AbstractQueryParams.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/listeners/ListenerAdapter.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/listeners/NotificationListenerAdapter.java

index 618bf4de91ac475bf676456655a54b39a23ae1d2..40770f2732f5a08467e3158b91bcd1c1eea88f40 100644 (file)
@@ -89,20 +89,11 @@ abstract class AbstractQueryParams extends AbstractNotificationsData {
         return leafNodesOnly;
     }
 
-    /**
-     * Checking query parameters on specific notification.
-     *
-     * @param xml       data of notification
-     * @param listener  listener of notification
-     * @return true if notification meets the requirements of query parameters,
-     *         false otherwise
-     */
     @SuppressWarnings("checkstyle:IllegalCatch")
-    protected <T extends BaseListenerInterface> boolean checkQueryParams(final String xml, final T listener) {
-        final Instant now = Instant.now();
+    <T extends BaseListenerInterface> boolean checkStartStop(final Instant now, final T listener) {
         if (this.stop != null) {
-            if ((this.start.compareTo(now) < 0) && (this.stop.compareTo(now) > 0)) {
-                return checkFilter(xml);
+            if (this.start.compareTo(now) < 0 && this.stop.compareTo(now) > 0) {
+                return true;
             }
             if (this.stop.compareTo(now) < 0) {
                 try {
@@ -114,10 +105,10 @@ abstract class AbstractQueryParams extends AbstractNotificationsData {
         } else if (this.start != null) {
             if (this.start.compareTo(now) < 0) {
                 this.start = null;
-                return checkFilter(xml);
+                return true;
             }
         } else {
-            return checkFilter(xml);
+            return true;
         }
         return false;
     }
@@ -128,7 +119,7 @@ abstract class AbstractQueryParams extends AbstractNotificationsData {
      * @param xml   data of notification
      */
     @SuppressWarnings("checkstyle:IllegalCatch")
-    private boolean checkFilter(final String xml) {
+    boolean checkFilter(final String xml) {
         if (this.filter == null) {
             return true;
         }
index b014859f927dca6f9ea66e2a449706153acfb892..5a13a0211c6ffe9a566a87cfccb91bd301e41302 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.netconf.sal.streams.listeners;
 
 import com.google.common.base.Preconditions;
 import java.io.IOException;
+import java.time.Instant;
 import java.util.Collection;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -76,8 +77,13 @@ public class ListenerAdapter extends AbstractCommonSubscriber implements Cluster
 
     @Override
     public void onDataTreeChanged(@Nonnull final Collection<DataTreeCandidate> dataTreeCandidates) {
+        final Instant now = Instant.now();
+        if (!checkStartStop(now, this)) {
+            return;
+        }
+
         final String xml = prepareXml(dataTreeCandidates);
-        if (checkQueryParams(xml, this)) {
+        if (checkFilter(xml)) {
             prepareAndPostData(xml);
         }
     }
index f3193e7993d9f81e4a69cc9bf9c6253ed93ca59a..c131237cb7e11d75ce3c988f89c110814f14fc5e 100644 (file)
@@ -80,9 +80,14 @@ public class NotificationListenerAdapter extends AbstractCommonSubscriber implem
 
     @Override
     public void onNotification(final DOMNotification notification) {
+        final Instant now = Instant.now();
+        if (!checkStartStop(now, this)) {
+            return;
+        }
+
         final SchemaContext schemaContext = controllerContext.getGlobalSchema();
         final String xml = prepareXml(schemaContext, notification);
-        if (checkQueryParams(xml, this)) {
+        if (checkFilter(xml)) {
             prepareAndPostData(outputType.equals("JSON") ? prepareJson(schemaContext, notification) : xml);
         }
     }
index 93dfc8030990af082e91f00a9204773165d41b85..1854a1dfc549a03a45c6a6f1890c647564f1177e 100644 (file)
@@ -89,20 +89,11 @@ abstract class AbstractQueryParams extends AbstractNotificationsData {
         return leafNodesOnly;
     }
 
-    /**
-     * Checking query parameters on specific notification.
-     *
-     * @param xml       data of notification
-     * @param listener  listener of notification
-     * @return true if notification meets the requirements of query parameters,
-     *         false otherwise
-     */
     @SuppressWarnings("checkstyle:IllegalCatch")
-    protected <T extends BaseListenerInterface> boolean checkQueryParams(final String xml, final T listener) {
-        final Instant now = Instant.now();
+    <T extends BaseListenerInterface> boolean checkStartStop(final Instant now, final T listener) {
         if (this.stop != null) {
-            if ((this.start.compareTo(now) < 0) && (this.stop.compareTo(now) > 0)) {
-                return checkFilter(xml);
+            if (this.start.compareTo(now) < 0 && this.stop.compareTo(now) > 0) {
+                return true;
             }
             if (this.stop.compareTo(now) < 0) {
                 try {
@@ -114,10 +105,10 @@ abstract class AbstractQueryParams extends AbstractNotificationsData {
         } else if (this.start != null) {
             if (this.start.compareTo(now) < 0) {
                 this.start = null;
-                return checkFilter(xml);
+                return true;
             }
         } else {
-            return checkFilter(xml);
+            return true;
         }
         return false;
     }
@@ -128,7 +119,7 @@ abstract class AbstractQueryParams extends AbstractNotificationsData {
      * @param xml   data of notification
      */
     @SuppressWarnings("checkstyle:IllegalCatch")
-    private boolean checkFilter(final String xml) {
+    boolean checkFilter(final String xml) {
         if (this.filter == null) {
             return true;
         }
index 69744bac1ddd1dcdb789396295238de53839bcfe..386f4d07af329ad94f3f03fdf4828a151a337fbf 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.restconf.nb.rfc8040.streams.listeners;
 
 import com.google.common.base.Preconditions;
 import java.io.IOException;
+import java.time.Instant;
 import java.util.Collection;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -75,8 +76,13 @@ public class ListenerAdapter extends AbstractCommonSubscriber implements Cluster
 
     @Override
     public void onDataTreeChanged(final Collection<DataTreeCandidate> dataTreeCandidates) {
+        final Instant now = Instant.now();
+        if (!checkStartStop(now, this)) {
+            return;
+        }
+
         final String xml = prepareXml(dataTreeCandidates);
-        if (checkQueryParams(xml, this)) {
+        if (checkFilter(xml)) {
             prepareAndPostData(xml);
         }
     }
index 14a345a209e36dbf6352303d5bc800a6ad66222d..1814f07113dca03db5ff617736684868bc29e2ed 100644 (file)
@@ -78,9 +78,14 @@ public class NotificationListenerAdapter extends AbstractCommonSubscriber implem
 
     @Override
     public void onNotification(final DOMNotification notification) {
+        final Instant now = Instant.now();
+        if (!checkStartStop(now, this)) {
+            return;
+        }
+
         final SchemaContext schemaContext = schemaHandler.get();
         final String xml = prepareXml(schemaContext, notification);
-        if (checkQueryParams(xml, this)) {
+        if (checkFilter(xml)) {
             prepareAndPostData(outputType.equals("JSON") ? prepareJson(schemaContext, notification) : xml);
         }
     }