Add ApiPath.empty()
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / server / api / EventStreamGetParams.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.server.api;
10
11 import static java.util.Objects.requireNonNull;
12
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;
23
24 /**
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>.
27  */
28 public record EventStreamGetParams(
29         StartTimeParam startTime,
30         StopTimeParam stopTime,
31         FilterParam filter,
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");
40         }
41         if (changedLeafNodesOnly != null) {
42             if (leafNodesOnly != null) {
43                 throw new IllegalArgumentException(ChangedLeafNodesOnlyParam.uriName + " parameter cannot be used with "
44                     + LeafNodesOnlyParam.uriName + " parameter");
45             }
46             if (childNodesOnly != null) {
47                 throw new IllegalArgumentException(ChangedLeafNodesOnlyParam.uriName + " parameter cannot be used with "
48                     + ChildNodesOnlyParam.uriName + " parameter");
49             }
50         }
51     }
52
53     /**
54      * Return {@link EventStreamGetParams} for specified query parameters.
55      *
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
60      */
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;
69
70         for (var entry : parameters.asCollection()) {
71             final var paramName = entry.getKey();
72             final var paramValue = entry.getValue();
73
74             switch (paramName) {
75                 case FilterParam.uriName:
76                     filter = mandatoryParam(FilterParam::forUriValue, paramName, paramValue);
77                     break;
78                 case StartTimeParam.uriName:
79                     startTime = mandatoryParam(StartTimeParam::forUriValue, paramName, paramValue);
80                     break;
81                 case StopTimeParam.uriName:
82                     stopTime = mandatoryParam(StopTimeParam::forUriValue, paramName, paramValue);
83                     break;
84                 case LeafNodesOnlyParam.uriName:
85                     leafNodesOnly = mandatoryParam(LeafNodesOnlyParam::forUriValue, paramName, paramValue);
86                     break;
87                 case SkipNotificationDataParam.uriName:
88                     skipNotificationData = mandatoryParam(SkipNotificationDataParam::forUriValue, paramName,
89                         paramValue);
90                     break;
91                 case ChangedLeafNodesOnlyParam.uriName:
92                     changedLeafNodesOnly = mandatoryParam(ChangedLeafNodesOnlyParam::forUriValue, paramName,
93                         paramValue);
94                     break;
95                 case ChildNodesOnlyParam.uriName:
96                     childNodesOnly = mandatoryParam(ChildNodesOnlyParam::forUriValue, paramName, paramValue);
97                     break;
98                 default:
99                     throw new IllegalArgumentException("Invalid parameter: " + paramName);
100             }
101         }
102
103         return new EventStreamGetParams(startTime, stopTime, filter, leafNodesOnly, skipNotificationData,
104             changedLeafNodesOnly, childNodesOnly);
105     }
106
107     @Override
108     public String toString() {
109         final var helper = MoreObjects.toStringHelper(this);
110         if (startTime != null) {
111             helper.add("startTime", startTime.paramValue());
112         }
113         if (stopTime != null) {
114             helper.add("stopTime", stopTime.paramValue());
115         }
116         if (filter != null) {
117             helper.add("filter", filter.paramValue());
118         }
119         if (leafNodesOnly != null) {
120             helper.add("leafNodesOnly", leafNodesOnly.value());
121         }
122         if (skipNotificationData != null) {
123             helper.add("skipNotificationData", skipNotificationData.value());
124         }
125         if (changedLeafNodesOnly != null) {
126             helper.add("changedLeafNodesOnly", changedLeafNodesOnly.value());
127         }
128         if (childNodesOnly != null) {
129             helper.add("childNodesOnly", childNodesOnly.value());
130         }
131         return helper.toString();
132     }
133
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) {
137         try {
138             return factory.apply(requireNonNull(value));
139         } catch (IllegalArgumentException e) {
140             throw new IllegalArgumentException("Invalid " + name + " value: " + value, e);
141         }
142     }
143 }