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.QueryParameters;
17 import org.opendaylight.restconf.api.query.ChangedLeafNodesOnlyParam;
18 import org.opendaylight.restconf.api.query.ChildNodesOnlyParam;
19 import org.opendaylight.restconf.api.query.FilterParam;
20 import org.opendaylight.restconf.api.query.LeafNodesOnlyParam;
21 import org.opendaylight.restconf.api.query.SkipNotificationDataParam;
22 import org.opendaylight.restconf.api.query.StartTimeParam;
23 import org.opendaylight.restconf.api.query.StopTimeParam;
26 * Query parameters valid in the scope of a GET request on an event stream resource, as outline in
27 * <a href="https://www.rfc-editor.org/rfc/rfc8040#section-6.3">RFC8040 section 6.3</a>.
29 public record EventStreamGetParams(
30 StartTimeParam startTime,
31 StopTimeParam stopTime,
33 LeafNodesOnlyParam leafNodesOnly,
34 SkipNotificationDataParam skipNotificationData,
35 ChangedLeafNodesOnlyParam changedLeafNodesOnly,
36 ChildNodesOnlyParam childNodesOnly) {
37 public EventStreamGetParams {
38 if (stopTime != null && startTime == null) {
39 throw new IllegalArgumentException(StopTimeParam.uriName + " parameter has to be used with "
40 + StartTimeParam.uriName + " parameter");
42 if (changedLeafNodesOnly != null) {
43 if (leafNodesOnly != null) {
44 throw new IllegalArgumentException(ChangedLeafNodesOnlyParam.uriName + " parameter cannot be used with "
45 + LeafNodesOnlyParam.uriName + " parameter");
47 if (childNodesOnly != null) {
48 throw new IllegalArgumentException(ChangedLeafNodesOnlyParam.uriName + " parameter cannot be used with "
49 + ChildNodesOnlyParam.uriName + " parameter");
55 * Return {@link EventStreamGetParams} for specified query parameters.
57 * @param parames Parameters and their values
58 * @return A {@link EventStreamGetParams}
59 * @throws NullPointerException if {@code queryParameters} is {@code null}
60 * @throws IllegalArgumentException if the parameters are invalid
62 public static @NonNull EventStreamGetParams of(final QueryParameters parames) {
63 StartTimeParam startTime = null;
64 StopTimeParam stopTime = null;
65 FilterParam filter = null;
66 LeafNodesOnlyParam leafNodesOnly = null;
67 SkipNotificationDataParam skipNotificationData = null;
68 ChangedLeafNodesOnlyParam changedLeafNodesOnly = null;
69 ChildNodesOnlyParam childNodesOnly = null;
71 for (var entry : parames.asCollection()) {
72 final var paramName = entry.getKey();
73 final var paramValue = entry.getValue();
76 case FilterParam.uriName:
77 filter = mandatoryParam(FilterParam::forUriValue, paramName, paramValue);
79 case StartTimeParam.uriName:
80 startTime = mandatoryParam(StartTimeParam::forUriValue, paramName, paramValue);
82 case StopTimeParam.uriName:
83 stopTime = mandatoryParam(StopTimeParam::forUriValue, paramName, paramValue);
85 case LeafNodesOnlyParam.uriName:
86 leafNodesOnly = mandatoryParam(LeafNodesOnlyParam::forUriValue, paramName, paramValue);
88 case SkipNotificationDataParam.uriName:
89 skipNotificationData = mandatoryParam(SkipNotificationDataParam::forUriValue, paramName,
92 case ChangedLeafNodesOnlyParam.uriName:
93 changedLeafNodesOnly = mandatoryParam(ChangedLeafNodesOnlyParam::forUriValue, paramName,
96 case ChildNodesOnlyParam.uriName:
97 childNodesOnly = mandatoryParam(ChildNodesOnlyParam::forUriValue, paramName, paramValue);
100 throw new IllegalArgumentException("Invalid parameter: " + paramName);
104 return new EventStreamGetParams(startTime, stopTime, filter, leafNodesOnly, skipNotificationData,
105 changedLeafNodesOnly, childNodesOnly);
109 public String toString() {
110 final var helper = MoreObjects.toStringHelper(this);
111 if (startTime != null) {
112 helper.add("startTime", startTime.paramValue());
114 if (stopTime != null) {
115 helper.add("stopTime", stopTime.paramValue());
117 if (filter != null) {
118 helper.add("filter", filter.paramValue());
120 if (leafNodesOnly != null) {
121 helper.add("leafNodesOnly", leafNodesOnly.value());
123 if (skipNotificationData != null) {
124 helper.add("skipNotificationData", skipNotificationData.value());
126 if (changedLeafNodesOnly != null) {
127 helper.add("changedLeafNodesOnly", changedLeafNodesOnly.value());
129 if (childNodesOnly != null) {
130 helper.add("childNodesOnly", childNodesOnly.value());
132 return helper.toString();
135 // FIXME: find a better place for this method
136 public static <T> @NonNull T mandatoryParam(final Function<String, @NonNull T> factory, final String name,
137 final String value) {
139 return factory.apply(requireNonNull(value));
140 } catch (IllegalArgumentException e) {
141 throw new IllegalArgumentException("Invalid " + name + " value: " + value, e);