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