Expose streams with all supported encodings
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / ReceiveEventsParams.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.nb.rfc8040;
10
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.base.MoreObjects;
14 import java.util.Map;
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;
25
26 /**
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>.
29  */
30 public record ReceiveEventsParams(
31         StartTimeParam startTime,
32         StopTimeParam stopTime,
33         FilterParam filter,
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");
42         }
43         if (changedLeafNodesOnly != null) {
44             if (leafNodesOnly != null) {
45                 throw new IllegalArgumentException(ChangedLeafNodesOnlyParam.uriName + " parameter cannot be used with "
46                     + LeafNodesOnlyParam.uriName + " parameter");
47             }
48             if (childNodesOnly != null) {
49                 throw new IllegalArgumentException(ChangedLeafNodesOnlyParam.uriName + " parameter cannot be used with "
50                     + ChildNodesOnlyParam.uriName + " parameter");
51             }
52         }
53     }
54
55     /**
56      * Return {@link ReceiveEventsParams} for specified query parameters.
57      * @param queryParameters Parameters and their values
58      * @return A {@link ReceiveEventsParams}
59      */
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;
68
69         for (var entry : queryParameters.entrySet()) {
70             final var paramName = entry.getKey();
71             final var paramValue = entry.getValue();
72
73             switch (paramName) {
74                 case FilterParam.uriName:
75                     filter = optionalParam(FilterParam::forUriValue, paramName, paramValue);
76                     break;
77                 case StartTimeParam.uriName:
78                     startTime = optionalParam(StartTimeParam::forUriValue, paramName, paramValue);
79                     break;
80                 case StopTimeParam.uriName:
81                     stopTime = optionalParam(StopTimeParam::forUriValue, paramName, paramValue);
82                     break;
83                 case LeafNodesOnlyParam.uriName:
84                     leafNodesOnly = optionalParam(LeafNodesOnlyParam::forUriValue, paramName, paramValue);
85                     break;
86                 case SkipNotificationDataParam.uriName:
87                     skipNotificationData = optionalParam(SkipNotificationDataParam::forUriValue, paramName,
88                         paramValue);
89                     break;
90                 case ChangedLeafNodesOnlyParam.uriName:
91                     changedLeafNodesOnly = optionalParam(ChangedLeafNodesOnlyParam::forUriValue, paramName,
92                         paramValue);
93                     break;
94                 case ChildNodesOnlyParam.uriName:
95                     childNodesOnly = optionalParam(ChildNodesOnlyParam::forUriValue, paramName, paramValue);
96                     break;
97                 default:
98                     throw new IllegalArgumentException("Invalid parameter: " + paramName);
99             }
100         }
101
102         return new ReceiveEventsParams(startTime, stopTime, filter, leafNodesOnly, skipNotificationData,
103             changedLeafNodesOnly, childNodesOnly);
104     }
105
106     @Override
107     public String toString() {
108         final var helper = MoreObjects.toStringHelper(this);
109         if (startTime != null) {
110             helper.add("startTime", startTime.paramValue());
111         }
112         if (stopTime != null) {
113             helper.add("stopTime", stopTime.paramValue());
114         }
115         if (filter != null) {
116             helper.add("filter", filter.paramValue());
117         }
118         if (leafNodesOnly != null) {
119             helper.add("leafNodesOnly", leafNodesOnly.value());
120         }
121         if (skipNotificationData != null) {
122             helper.add("skipNotificationData", skipNotificationData.value());
123         }
124         if (changedLeafNodesOnly != null) {
125             helper.add("changedLeafNodesOnly", changedLeafNodesOnly.value());
126         }
127         if (childNodesOnly != null) {
128             helper.add("childNodesOnly", childNodesOnly.value());
129         }
130         return helper.toString();
131     }
132
133     private static <T> @Nullable T optionalParam(final Function<String, @NonNull T> factory, final String name,
134             final String value) {
135         try {
136             return factory.apply(requireNonNull(value));
137         } catch (IllegalArgumentException e) {
138             throw new IllegalArgumentException("Invalid " + name + " value: " + value, e);
139         }
140     }
141 }