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.server.api;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.base.MoreObjects;
14 import java.util.function.Function;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.restconf.api.query.ChangedLeafNodesOnlyParam;
17 import org.opendaylight.restconf.api.query.ChildNodesOnlyParam;
18 import org.opendaylight.restconf.api.query.FilterParam;
19 import org.opendaylight.restconf.api.query.LeafNodesOnlyParam;
20 import org.opendaylight.restconf.api.query.SkipNotificationDataParam;
21 import org.opendaylight.restconf.api.query.StartTimeParam;
22 import org.opendaylight.restconf.api.query.StopTimeParam;
25 * Query parameters valid in the scope of a GET request on an event stream resource, as outline in
26 * <a href="https://www.rfc-editor.org/rfc/rfc8040#section-6.3">RFC8040 section 6.3</a>.
28 public record EventStreamGetParams(
29 StartTimeParam startTime,
30 StopTimeParam stopTime,
32 LeafNodesOnlyParam leafNodesOnly,
33 SkipNotificationDataParam skipNotificationData,
34 ChangedLeafNodesOnlyParam changedLeafNodesOnly,
35 ChildNodesOnlyParam childNodesOnly) {
36 public EventStreamGetParams {
37 if (stopTime != null && startTime == null) {
38 throw new IllegalArgumentException(StopTimeParam.uriName + " parameter has to be used with "
39 + StartTimeParam.uriName + " parameter");
41 if (changedLeafNodesOnly != null) {
42 if (leafNodesOnly != null) {
43 throw new IllegalArgumentException(ChangedLeafNodesOnlyParam.uriName + " parameter cannot be used with "
44 + LeafNodesOnlyParam.uriName + " parameter");
46 if (childNodesOnly != null) {
47 throw new IllegalArgumentException(ChangedLeafNodesOnlyParam.uriName + " parameter cannot be used with "
48 + ChildNodesOnlyParam.uriName + " parameter");
54 * Return {@link EventStreamGetParams} for specified query parameters.
56 * @param parameters Parameters and their values
57 * @return A {@link EventStreamGetParams}
58 * @throws NullPointerException if {@code queryParameters} is {@code null}
59 * @throws IllegalArgumentException if the parameters are invalid
61 public static @NonNull EventStreamGetParams of(final QueryParams parameters) {
62 StartTimeParam startTime = null;
63 StopTimeParam stopTime = null;
64 FilterParam filter = null;
65 LeafNodesOnlyParam leafNodesOnly = null;
66 SkipNotificationDataParam skipNotificationData = null;
67 ChangedLeafNodesOnlyParam changedLeafNodesOnly = null;
68 ChildNodesOnlyParam childNodesOnly = null;
70 for (var entry : parameters.asCollection()) {
71 final var paramName = entry.getKey();
72 final var paramValue = entry.getValue();
75 case FilterParam.uriName:
76 filter = mandatoryParam(FilterParam::forUriValue, paramName, paramValue);
78 case StartTimeParam.uriName:
79 startTime = mandatoryParam(StartTimeParam::forUriValue, paramName, paramValue);
81 case StopTimeParam.uriName:
82 stopTime = mandatoryParam(StopTimeParam::forUriValue, paramName, paramValue);
84 case LeafNodesOnlyParam.uriName:
85 leafNodesOnly = mandatoryParam(LeafNodesOnlyParam::forUriValue, paramName, paramValue);
87 case SkipNotificationDataParam.uriName:
88 skipNotificationData = mandatoryParam(SkipNotificationDataParam::forUriValue, paramName,
91 case ChangedLeafNodesOnlyParam.uriName:
92 changedLeafNodesOnly = mandatoryParam(ChangedLeafNodesOnlyParam::forUriValue, paramName,
95 case ChildNodesOnlyParam.uriName:
96 childNodesOnly = mandatoryParam(ChildNodesOnlyParam::forUriValue, paramName, paramValue);
99 throw new IllegalArgumentException("Invalid parameter: " + paramName);
103 return new EventStreamGetParams(startTime, stopTime, filter, leafNodesOnly, skipNotificationData,
104 changedLeafNodesOnly, childNodesOnly);
108 public String toString() {
109 final var helper = MoreObjects.toStringHelper(this);
110 if (startTime != null) {
111 helper.add("startTime", startTime.paramValue());
113 if (stopTime != null) {
114 helper.add("stopTime", stopTime.paramValue());
116 if (filter != null) {
117 helper.add("filter", filter.paramValue());
119 if (leafNodesOnly != null) {
120 helper.add("leafNodesOnly", leafNodesOnly.value());
122 if (skipNotificationData != null) {
123 helper.add("skipNotificationData", skipNotificationData.value());
125 if (changedLeafNodesOnly != null) {
126 helper.add("changedLeafNodesOnly", changedLeafNodesOnly.value());
128 if (childNodesOnly != null) {
129 helper.add("childNodesOnly", childNodesOnly.value());
131 return helper.toString();
134 // FIXME: find a better place for this method
135 public static <T> @NonNull T mandatoryParam(final Function<String, @NonNull T> factory, final String name,
136 final String value) {
138 return factory.apply(requireNonNull(value));
139 } catch (IllegalArgumentException e) {
140 throw new IllegalArgumentException("Invalid " + name + " value: " + value, e);