2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
3 * Copyright (c) 2021 PANTHEON.tech, s.r.o.
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
9 package org.opendaylight.restconf.nb.rfc8040;
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;
21 * Parser and holder of query paramteres from uriInfo for notifications.
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;
29 private NotificationQueryParams(final StartTimeParameter startTime, final StopTimeParameter stopTime,
30 final FilterParameter filter, final boolean skipNotificationData) {
31 this.startTime = startTime;
32 this.stopTime = stopTime;
34 this.skipNotificationData = skipNotificationData;
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;
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()) {
52 final String str = paramValues.get(0);
54 startTime = StartTimeParameter.forUriValue(str);
55 } catch (IllegalArgumentException e) {
56 throw new RestconfDocumentedException("Invalid start-time date: " + str, e);
60 throw new RestconfDocumentedException("Start-time parameter can be used only once.");
62 } else if (paramName.equals(StopTimeParameter.uriName())) {
63 switch (paramValues.size()) {
67 final String str = paramValues.get(0);
69 stopTime = StopTimeParameter.forUriValue(str);
70 } catch (IllegalArgumentException e) {
71 throw new RestconfDocumentedException("Invalid stop-time date: " + str, e);
75 throw new RestconfDocumentedException("Stop-time parameter can be used only once.");
77 } else if (paramName.equals(FilterParameter.uriName())) {
78 if (!paramValues.isEmpty()) {
79 filter = FilterParameter.forUriValue(paramValues.get(0));
81 } else if (paramName.equals("odl-skip-notification-data")) {
82 switch (paramValues.size()) {
86 skipNotificationData = Boolean.parseBoolean(paramValues.get(0));
89 throw new RestconfDocumentedException(
90 "Odl-skip-notification-data parameter can be used only once.");
93 throw new RestconfDocumentedException("Bad parameter used with notifications: " + paramName);
96 if (startTime == null && stopTime != null) {
97 throw new RestconfDocumentedException("Stop-time parameter has to be used with start-time parameter.");
100 return new NotificationQueryParams(startTime, stopTime, filter, skipNotificationData);
104 * Get start-time query parameter.
108 public @Nullable StartTimeParameter startTime() {
113 * Get stop-time query parameter.
117 public @Nullable StopTimeParameter stopTime() {
122 * Get filter query parameter.
126 public @Nullable FilterParameter filter() {
131 * Check whether this query should notify changes without data.
133 * @return true if this query should notify about changes with data
135 public boolean isSkipNotificationData() {
136 return skipNotificationData;
140 public String toString() {
141 final var helper = MoreObjects.toStringHelper(this);
142 if (startTime != null) {
143 helper.add("startTime", startTime.uriValue());
145 if (stopTime != null) {
146 helper.add("stopTime", stopTime.uriValue());
148 if (filter != null) {
149 helper.add("filter", filter.uriValue());
151 return helper.add("skipNotificationData", skipNotificationData).toString();