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 static java.util.Objects.requireNonNull;
13 import com.google.common.base.MoreObjects;
15 import java.util.function.Function;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.restconf.api.query.ChangedLeafNodesOnlyParam;
19 import org.opendaylight.restconf.api.query.ChildNodesOnlyParam;
20 import org.opendaylight.restconf.api.query.FilterParam;
21 import org.opendaylight.restconf.api.query.LeafNodesOnlyParam;
22 import org.opendaylight.restconf.api.query.SkipNotificationDataParam;
23 import org.opendaylight.restconf.api.query.StartTimeParam;
24 import org.opendaylight.restconf.api.query.StopTimeParam;
27 * Query parameters valid in the scope of a GET request on an event stream resource, as outline in
28 * <a href="https://www.rfc-editor.org/rfc/rfc8040#section-6.3">RFC8040 section 6.3</a>.
30 public record ReceiveEventsParams(
31 StartTimeParam startTime,
32 StopTimeParam stopTime,
34 LeafNodesOnlyParam leafNodesOnly,
35 SkipNotificationDataParam skipNotificationData,
36 ChangedLeafNodesOnlyParam changedLeafNodesOnly,
37 ChildNodesOnlyParam childNodesOnly) {
38 public ReceiveEventsParams {
39 if (stopTime != null && startTime == null) {
40 throw new IllegalArgumentException(StopTimeParam.uriName + " parameter has to be used with "
41 + StartTimeParam.uriName + " parameter");
43 if (changedLeafNodesOnly != null) {
44 if (leafNodesOnly != null) {
45 throw new IllegalArgumentException(ChangedLeafNodesOnlyParam.uriName + " parameter cannot be used with "
46 + LeafNodesOnlyParam.uriName + " parameter");
48 if (childNodesOnly != null) {
49 throw new IllegalArgumentException(ChangedLeafNodesOnlyParam.uriName + " parameter cannot be used with "
50 + ChildNodesOnlyParam.uriName + " parameter");
56 * Return {@link ReceiveEventsParams} for specified query parameters.
57 * @param queryParameters Parameters and their values
58 * @return A {@link ReceiveEventsParams}
60 public static @NonNull ReceiveEventsParams ofQueryParameters(final Map<String, String> queryParameters) {
61 StartTimeParam startTime = null;
62 StopTimeParam stopTime = null;
63 FilterParam filter = null;
64 LeafNodesOnlyParam leafNodesOnly = null;
65 SkipNotificationDataParam skipNotificationData = null;
66 ChangedLeafNodesOnlyParam changedLeafNodesOnly = null;
67 ChildNodesOnlyParam childNodesOnly = null;
69 for (var entry : queryParameters.entrySet()) {
70 final var paramName = entry.getKey();
71 final var paramValue = entry.getValue();
74 case FilterParam.uriName:
75 filter = optionalParam(FilterParam::forUriValue, paramName, paramValue);
77 case StartTimeParam.uriName:
78 startTime = optionalParam(StartTimeParam::forUriValue, paramName, paramValue);
80 case StopTimeParam.uriName:
81 stopTime = optionalParam(StopTimeParam::forUriValue, paramName, paramValue);
83 case LeafNodesOnlyParam.uriName:
84 leafNodesOnly = optionalParam(LeafNodesOnlyParam::forUriValue, paramName, paramValue);
86 case SkipNotificationDataParam.uriName:
87 skipNotificationData = optionalParam(SkipNotificationDataParam::forUriValue, paramName,
90 case ChangedLeafNodesOnlyParam.uriName:
91 changedLeafNodesOnly = optionalParam(ChangedLeafNodesOnlyParam::forUriValue, paramName,
94 case ChildNodesOnlyParam.uriName:
95 childNodesOnly = optionalParam(ChildNodesOnlyParam::forUriValue, paramName, paramValue);
98 throw new IllegalArgumentException("Invalid parameter: " + paramName);
102 return new ReceiveEventsParams(startTime, stopTime, filter, leafNodesOnly, skipNotificationData,
103 changedLeafNodesOnly, childNodesOnly);
107 public String toString() {
108 final var helper = MoreObjects.toStringHelper(this);
109 if (startTime != null) {
110 helper.add("startTime", startTime.paramValue());
112 if (stopTime != null) {
113 helper.add("stopTime", stopTime.paramValue());
115 if (filter != null) {
116 helper.add("filter", filter.paramValue());
118 if (leafNodesOnly != null) {
119 helper.add("leafNodesOnly", leafNodesOnly.value());
121 if (skipNotificationData != null) {
122 helper.add("skipNotificationData", skipNotificationData.value());
124 if (changedLeafNodesOnly != null) {
125 helper.add("changedLeafNodesOnly", changedLeafNodesOnly.value());
127 if (childNodesOnly != null) {
128 helper.add("childNodesOnly", childNodesOnly.value());
130 return helper.toString();
133 private static <T> @Nullable T optionalParam(final Function<String, @NonNull T> factory, final String name,
134 final String value) {
136 return factory.apply(requireNonNull(value));
137 } catch (IllegalArgumentException e) {
138 throw new IllegalArgumentException("Invalid " + name + " value: " + value, e);