1e7ad2b5dffb95f76bc80731376d0ca6be9fe9a2
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / streams / listeners / AbstractQueryParams.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.restconf.nb.rfc8040.streams.listeners;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.VisibleForTesting;
13 import java.time.Instant;
14 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
15
16 /**
17  * Features of query parameters part of both notifications.
18  */
19 abstract class AbstractQueryParams extends AbstractNotificationsData {
20     // FIXME: these should be final
21     private Instant startTime = null;
22     private Instant stopTime = null;
23     private boolean leafNodesOnly = false;
24     private boolean skipNotificationData = false;
25
26     @VisibleForTesting
27     public final Instant getStart() {
28         return startTime;
29     }
30
31     /**
32      * Set query parameters for listener.
33      *
34      * @param startTime     Start-time of getting notification.
35      * @param stopTime      Stop-time of getting notification.
36      * @param filter        Indicates which subset of all possible events are of interest.
37      * @param leafNodesOnly If TRUE, notifications will contain changes of leaf nodes only.
38      */
39     public abstract void setQueryParams(Instant startTime, Instant stopTime, String filter,
40             boolean leafNodesOnly, boolean skipNotificationData);
41
42     @SuppressWarnings("checkstyle:hiddenField")
43     final void setQueryParams(final Instant startTime, final Instant stopTime, final boolean leafNodesOnly,
44             final boolean skipNotificationData) {
45         this.startTime = requireNonNull(startTime);
46         this.stopTime = stopTime;
47         this.leafNodesOnly = leafNodesOnly;
48         this.skipNotificationData = skipNotificationData;
49     }
50
51     /**
52      * Check whether this query should only notify about leaf node changes.
53      *
54      * @return true if this query should only notify about leaf node changes
55      */
56     boolean getLeafNodesOnly() {
57         return leafNodesOnly;
58     }
59
60     /**
61      * Check whether this query should notify changes without data.
62      *
63      * @return true if this query should notify about changes with  data
64      */
65     public boolean isSkipNotificationData() {
66         return skipNotificationData;
67     }
68
69     @SuppressWarnings("checkstyle:IllegalCatch")
70     <T extends BaseListenerInterface> boolean checkStartStop(final Instant now, final T listener) {
71         if (stopTime != null) {
72             if (startTime.compareTo(now) < 0 && stopTime.compareTo(now) > 0) {
73                 return true;
74             }
75             if (stopTime.compareTo(now) < 0) {
76                 try {
77                     listener.close();
78                 } catch (final Exception e) {
79                     throw new RestconfDocumentedException("Problem with unregister listener." + e);
80                 }
81             }
82         } else if (startTime != null) {
83             if (startTime.compareTo(now) < 0) {
84                 startTime = null;
85                 return true;
86             }
87         } else {
88             return true;
89         }
90         return false;
91     }
92 }