45e2c82cad2d9d3b1e43f7d81a4547ddf1c864a9
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / NotificationQueryParams.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  * Copyright (c) 2021 PANTHEON.tech, s.r.o.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9 package org.opendaylight.restconf.nb.rfc8040;
10
11 import com.google.common.base.MoreObjects;
12 import java.util.List;
13 import java.util.Map.Entry;
14 import javax.ws.rs.core.UriInfo;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
18 import org.opendaylight.yangtools.concepts.Immutable;
19
20 /**
21  * Parser and holder of query paramteres from uriInfo for notifications.
22  */
23 public final class NotificationQueryParams implements Immutable {
24     private final StartTimeParameter startTime;
25     private final StopTimeParameter stopTime;
26     private final FilterParameter filter;
27     private final boolean skipNotificationData;
28
29     private NotificationQueryParams(final StartTimeParameter startTime, final StopTimeParameter stopTime,
30             final FilterParameter filter, final boolean skipNotificationData) {
31         this.startTime = startTime;
32         this.stopTime = stopTime;
33         this.filter = filter;
34         this.skipNotificationData = skipNotificationData;
35     }
36
37     // FIXME: this is JAX-RS specific
38     public static @NonNull NotificationQueryParams fromUriInfo(final UriInfo uriInfo) {
39         StartTimeParameter startTime = null;
40         StopTimeParameter stopTime = null;
41         FilterParameter filter = null;
42         boolean skipNotificationData = false;
43
44         for (final Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
45             final String paramName = entry.getKey();
46             final List<String> paramValues = entry.getValue();
47             if (paramName.equals(StartTimeParameter.uriName())) {
48                 switch (paramValues.size()) {
49                     case 0:
50                         break;
51                     case 1:
52                         final String str = paramValues.get(0);
53                         try {
54                             startTime = StartTimeParameter.forUriValue(str);
55                         } catch (IllegalArgumentException e) {
56                             throw new RestconfDocumentedException("Invalid start-time date: " + str, e);
57                         }
58                         break;
59                     default:
60                         throw new RestconfDocumentedException("Start-time parameter can be used only once.");
61                 }
62             } else if (paramName.equals(StopTimeParameter.uriName())) {
63                 switch (paramValues.size()) {
64                     case 0:
65                         break;
66                     case 1:
67                         final String str = paramValues.get(0);
68                         try {
69                             stopTime = StopTimeParameter.forUriValue(str);
70                         } catch (IllegalArgumentException e) {
71                             throw new RestconfDocumentedException("Invalid stop-time date: " + str, e);
72                         }
73                         break;
74                     default:
75                         throw new RestconfDocumentedException("Stop-time parameter can be used only once.");
76                 }
77             } else if (paramName.equals(FilterParameter.uriName())) {
78                 if (!paramValues.isEmpty()) {
79                     filter = FilterParameter.forUriValue(paramValues.get(0));
80                 }
81             } else if (paramName.equals("odl-skip-notification-data")) {
82                 switch (paramValues.size()) {
83                     case 0:
84                         break;
85                     case 1:
86                         skipNotificationData = Boolean.parseBoolean(paramValues.get(0));
87                         break;
88                     default:
89                         throw new RestconfDocumentedException(
90                             "Odl-skip-notification-data parameter can be used only once.");
91                 }
92             } else {
93                 throw new RestconfDocumentedException("Bad parameter used with notifications: " + paramName);
94             }
95         }
96         if (startTime == null && stopTime != null) {
97             throw new RestconfDocumentedException("Stop-time parameter has to be used with start-time parameter.");
98         }
99
100         return new NotificationQueryParams(startTime, stopTime, filter, skipNotificationData);
101     }
102
103     /**
104      * Get start-time query parameter.
105      *
106      * @return start-time
107      */
108     public @Nullable StartTimeParameter startTime() {
109         return startTime;
110     }
111
112     /**
113      * Get stop-time query parameter.
114      *
115      * @return stop-time
116      */
117     public @Nullable StopTimeParameter stopTime() {
118         return stopTime;
119     }
120
121     /**
122      * Get filter query parameter.
123      *
124      * @return filter
125      */
126     public @Nullable FilterParameter filter() {
127         return filter;
128     }
129
130     /**
131      * Check whether this query should notify changes without data.
132      *
133      * @return true if this query should notify about changes with  data
134      */
135     public boolean isSkipNotificationData() {
136         return skipNotificationData;
137     }
138
139     @Override
140     public String toString() {
141         final var helper = MoreObjects.toStringHelper(this);
142         if (startTime != null) {
143             helper.add("startTime", startTime.uriValue());
144         }
145         if (stopTime != null) {
146             helper.add("stopTime", stopTime.uriValue());
147         }
148         if (filter != null) {
149             helper.add("filter", filter.uriValue());
150         }
151         return helper.add("skipNotificationData", skipNotificationData).toString();
152     }
153 }