Add RestconfQueryParam
[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 java.time.Instant;
11 import java.time.format.DateTimeFormatter;
12 import java.time.format.DateTimeFormatterBuilder;
13 import java.time.format.DateTimeParseException;
14 import java.time.temporal.ChronoField;
15 import java.time.temporal.TemporalAccessor;
16 import javax.xml.xpath.XPathExpressionException;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
20 import org.opendaylight.restconf.nb.rfc8040.FilterParam;
21 import org.opendaylight.restconf.nb.rfc8040.StartTimeParam;
22 import org.opendaylight.restconf.nb.rfc8040.StopTimeParam;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
24
25 /**
26  * Features of query parameters part of both notifications.
27  */
28 abstract class AbstractQueryParams extends AbstractNotificationsData {
29     private static final DateTimeFormatter FORMATTER = new DateTimeFormatterBuilder()
30         .appendValue(ChronoField.YEAR, 4).appendLiteral('-')
31         .appendValue(ChronoField.MONTH_OF_YEAR, 2).appendLiteral('-')
32         .appendValue(ChronoField.DAY_OF_MONTH, 2).appendLiteral('T')
33         .appendValue(ChronoField.HOUR_OF_DAY, 2).appendLiteral(':')
34         .appendValue(ChronoField.MINUTE_OF_HOUR, 2).appendLiteral(':')
35         .appendValue(ChronoField.SECOND_OF_MINUTE, 2)
36         .appendFraction(ChronoField.NANO_OF_SECOND, 0, 9, true)
37         .appendOffset("+HH:MM", "Z").toFormatter();
38
39     // FIXME: these should be final
40     private Instant start = null;
41     private Instant stop = null;
42     private boolean leafNodesOnly = false;
43     private boolean skipNotificationData = false;
44
45     public final Instant getStart() {
46         return start;
47     }
48
49     /**
50      * Set query parameters for listener.
51      *
52      * @param startTime     Start-time of getting notification.
53      * @param stopTime      Stop-time of getting notification.
54      * @param filter        Indicates which subset of all possible events are of interest.
55      * @param leafNodesOnly If TRUE, notifications will contain changes of leaf nodes only.
56      */
57     @SuppressWarnings("checkstyle:hiddenField")
58     public final void setQueryParams(final StartTimeParam startTime, final StopTimeParam stopTime,
59             final FilterParam filter, final boolean leafNodesOnly, final boolean skipNotificationData) {
60         start = startTime == null ? Instant.now() : parseDateAndTime(startTime.value());
61         stop = stopTime == null ? null : parseDateAndTime(stopTime.value());
62         this.leafNodesOnly = leafNodesOnly;
63         this.skipNotificationData = skipNotificationData;
64
65         if (filter != null) {
66             try {
67                 setFilter(filter.paramValue());
68             } catch (XPathExpressionException e) {
69                 throw new IllegalArgumentException("Failed to get filter", e);
70             }
71         }
72     }
73
74     abstract void setFilter(@Nullable String xpathString) throws XPathExpressionException;
75
76     /**
77      * Parse input of query parameters - start-time or stop-time - from {@link DateAndTime} format
78      * to {@link Instant} format.
79      *
80      * @param uriValue Start-time or stop-time as string in {@link DateAndTime} format.
81      * @return Parsed {@link Instant} by entry.
82      */
83     private static @NonNull Instant parseDateAndTime(final DateAndTime dateAndTime) {
84         final TemporalAccessor accessor;
85         try {
86             accessor = FORMATTER.parse(dateAndTime.getValue());
87         } catch (final DateTimeParseException e) {
88             throw new RestconfDocumentedException("Cannot parse of value in date: " + dateAndTime, e);
89         }
90         return Instant.from(accessor);
91     }
92
93     /**
94      * Check whether this query should only notify about leaf node changes.
95      *
96      * @return true if this query should only notify about leaf node changes
97      */
98     boolean getLeafNodesOnly() {
99         return leafNodesOnly;
100     }
101
102     /**
103      * Check whether this query should notify changes without data.
104      *
105      * @return true if this query should notify about changes with  data
106      */
107     public boolean isSkipNotificationData() {
108         return skipNotificationData;
109     }
110
111     @SuppressWarnings("checkstyle:IllegalCatch")
112     <T extends BaseListenerInterface> boolean checkStartStop(final Instant now, final T listener) {
113         if (stop != null) {
114             if (start.compareTo(now) < 0 && stop.compareTo(now) > 0) {
115                 return true;
116             }
117             if (stop.compareTo(now) < 0) {
118                 try {
119                     listener.close();
120                 } catch (final Exception e) {
121                     throw new RestconfDocumentedException("Problem with unregister listener." + e);
122                 }
123             }
124         } else if (start != null) {
125             if (start.compareTo(now) < 0) {
126                 start = null;
127                 return true;
128             }
129         } else {
130             return true;
131         }
132         return false;
133     }
134 }