4c6e77603a11f3de3700381de4989a3ea985b66a
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / databind / jaxrs / QueryParams.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.restconf.nb.rfc8040.databind.jaxrs;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.annotations.VisibleForTesting;
14 import java.util.List;
15 import java.util.Map.Entry;
16 import java.util.Set;
17 import java.util.function.Function;
18 import javax.ws.rs.core.UriInfo;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.restconf.api.query.ChangedLeafNodesOnlyParam;
22 import org.opendaylight.restconf.api.query.ContentParam;
23 import org.opendaylight.restconf.api.query.DepthParam;
24 import org.opendaylight.restconf.api.query.FieldsParam;
25 import org.opendaylight.restconf.api.query.FilterParam;
26 import org.opendaylight.restconf.api.query.InsertParam;
27 import org.opendaylight.restconf.api.query.LeafNodesOnlyParam;
28 import org.opendaylight.restconf.api.query.PointParam;
29 import org.opendaylight.restconf.api.query.PrettyPrintParam;
30 import org.opendaylight.restconf.api.query.SkipNotificationDataParam;
31 import org.opendaylight.restconf.api.query.StartTimeParam;
32 import org.opendaylight.restconf.api.query.StopTimeParam;
33 import org.opendaylight.restconf.api.query.WithDefaultsParam;
34 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
35 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
36 import org.opendaylight.restconf.common.errors.RestconfError;
37 import org.opendaylight.restconf.nb.rfc8040.NotificationQueryParams;
38 import org.opendaylight.restconf.nb.rfc8040.ReadDataParams;
39 import org.opendaylight.restconf.nb.rfc8040.WriteDataParams;
40 import org.opendaylight.restconf.nb.rfc8040.legacy.QueryParameters;
41 import org.opendaylight.restconf.nb.rfc8040.utils.parser.NetconfFieldsTranslator;
42 import org.opendaylight.restconf.nb.rfc8040.utils.parser.WriterFieldsTranslator;
43 import org.opendaylight.yangtools.yang.common.ErrorTag;
44 import org.opendaylight.yangtools.yang.common.ErrorType;
45
46 @Beta
47 public final class QueryParams {
48     private static final Set<String> KNOWN_PARAMS = Set.of(
49         // Read data
50         ContentParam.uriName, DepthParam.uriName, FieldsParam.uriName, WithDefaultsParam.uriName,
51         PrettyPrintParam.uriName,
52         // Modify data
53         InsertParam.uriName, PointParam.uriName,
54         // Notifications
55         FilterParam.uriName, StartTimeParam.uriName, StopTimeParam.uriName,
56         LeafNodesOnlyParam.uriName, SkipNotificationDataParam.uriName, ChangedLeafNodesOnlyParam.uriName);
57
58     private QueryParams() {
59         // Utility class
60     }
61
62     public static @NonNull NotificationQueryParams newNotificationQueryParams(final UriInfo uriInfo) {
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
70         for (Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
71             final String paramName = entry.getKey();
72             final List<String> paramValues = entry.getValue();
73
74             try {
75                 switch (paramName) {
76                     case FilterParam.uriName:
77                         filter = optionalParam(FilterParam::forUriValue, paramName, paramValues);
78                         break;
79                     case StartTimeParam.uriName:
80                         startTime = optionalParam(StartTimeParam::forUriValue, paramName, paramValues);
81                         break;
82                     case StopTimeParam.uriName:
83                         stopTime = optionalParam(StopTimeParam::forUriValue, paramName, paramValues);
84                         break;
85                     case LeafNodesOnlyParam.uriName:
86                         leafNodesOnly = optionalParam(LeafNodesOnlyParam::forUriValue, paramName, paramValues);
87                         break;
88                     case SkipNotificationDataParam.uriName:
89                         skipNotificationData = optionalParam(SkipNotificationDataParam::forUriValue, paramName,
90                             paramValues);
91                         break;
92                     case ChangedLeafNodesOnlyParam.uriName:
93                         changedLeafNodesOnly = optionalParam(ChangedLeafNodesOnlyParam::forUriValue, paramName,
94                             paramValues);
95                         break;
96                     default:
97                         throw unhandledParam("notification", paramName);
98                 }
99             } catch (IllegalArgumentException e) {
100                 throw new RestconfDocumentedException("Invalid " + paramName + " value: " + e.getMessage(),
101                     ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, e);
102             }
103         }
104
105         try {
106             return NotificationQueryParams.of(startTime, stopTime, filter, leafNodesOnly, skipNotificationData,
107                     changedLeafNodesOnly);
108         } catch (IllegalArgumentException e) {
109             throw new RestconfDocumentedException("Invalid query parameters: " + e.getMessage(), e);
110         }
111     }
112
113     public static QueryParameters newQueryParameters(final ReadDataParams params,
114             final InstanceIdentifierContext identifier) {
115         final var fields = params.fields();
116         if (fields == null) {
117             return QueryParameters.of(params);
118         }
119
120         return identifier.getMountPoint() != null
121             ? QueryParameters.ofFieldPaths(params, NetconfFieldsTranslator.translate(identifier, fields))
122                 : QueryParameters.ofFields(params, WriterFieldsTranslator.translate(identifier, fields));
123     }
124
125     /**
126      * Parse parameters from URI request and check their types and values.
127      *
128      * @param uriInfo    URI info
129      * @return {@link ReadDataParams}
130      */
131     public static @NonNull ReadDataParams newReadDataParams(final UriInfo uriInfo) {
132         ContentParam content = ContentParam.ALL;
133         DepthParam depth = null;
134         FieldsParam fields = null;
135         WithDefaultsParam withDefaults = null;
136         PrettyPrintParam prettyPrint = null;
137
138         for (Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
139             final String paramName = entry.getKey();
140             final List<String> paramValues = entry.getValue();
141
142             try {
143                 switch (paramName) {
144                     case ContentParam.uriName:
145                         content = optionalParam(ContentParam::forUriValue, paramName, paramValues);
146                         break;
147                     case DepthParam.uriName:
148                         final String depthStr = optionalParam(paramName, paramValues);
149                         try {
150                             depth = DepthParam.forUriValue(depthStr);
151                         } catch (IllegalArgumentException e) {
152                             throw new RestconfDocumentedException(e, new RestconfError(ErrorType.PROTOCOL,
153                                 ErrorTag.INVALID_VALUE, "Invalid depth parameter: " + depthStr, null,
154                                 "The depth parameter must be an integer between 1 and 65535 or \"unbounded\""));
155                         }
156                         break;
157                     case FieldsParam.uriName:
158                         fields = optionalParam(FieldsParam::forUriValue, paramName, paramValues);
159                         break;
160                     case WithDefaultsParam.uriName:
161                         withDefaults = optionalParam(WithDefaultsParam::forUriValue, paramName, paramValues);
162                         break;
163                     case PrettyPrintParam.uriName:
164                         prettyPrint = optionalParam(PrettyPrintParam::forUriValue, paramName, paramValues);
165                         break;
166                     default:
167                         throw unhandledParam("read", paramName);
168                 }
169             } catch (IllegalArgumentException e) {
170                 throw new RestconfDocumentedException("Invalid " + paramName + " value: " + e.getMessage(),
171                     ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, e);
172             }
173         }
174
175         return new ReadDataParams(content, depth, fields, withDefaults, prettyPrint);
176     }
177
178     public static @NonNull WriteDataParams newWriteDataParams(final UriInfo uriInfo) {
179         InsertParam insert = null;
180         PointParam point = null;
181
182         for (final Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
183             final String paramName = entry.getKey();
184             final List<String> paramValues = entry.getValue();
185
186             try {
187                 switch (paramName) {
188                     case InsertParam.uriName:
189                         insert = optionalParam(InsertParam::forUriValue, paramName, paramValues);
190                         break;
191                     case PointParam.uriName:
192                         point = optionalParam(PointParam::forUriValue, paramName, paramValues);
193                         break;
194                     default:
195                         throw unhandledParam("write", paramName);
196                 }
197             } catch (IllegalArgumentException e) {
198                 throw new RestconfDocumentedException("Invalid " + paramName + " value: " + e.getMessage(),
199                     ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, e);
200             }
201         }
202
203         try {
204             return WriteDataParams.of(insert, point);
205         } catch (IllegalArgumentException e) {
206             throw new RestconfDocumentedException("Invalid query parameters: " + e.getMessage(), e);
207         }
208     }
209
210     private static RestconfDocumentedException unhandledParam(final String operation, final String name) {
211         return KNOWN_PARAMS.contains(name)
212             ? new RestconfDocumentedException("Invalid parameter in " + operation + ": " + name,
213                 ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE)
214             : new RestconfDocumentedException("Unknown parameter in " + operation + ": " + name,
215                 ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ATTRIBUTE);
216     }
217
218     @VisibleForTesting
219     static @Nullable String optionalParam(final String name, final List<String> values) {
220         return switch (values.size()) {
221             case 0 -> null;
222             case 1 -> requireNonNull(values.get(0));
223             default -> throw new RestconfDocumentedException(
224                 "Parameter " + name + " can appear at most once in request URI",
225                 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
226         };
227     }
228
229     private static <T> @Nullable T optionalParam(final Function<String, @NonNull T> factory, final String name,
230             final List<String> values) {
231         final String str = optionalParam(name, values);
232         return str == null ? null : factory.apply(str);
233     }
234 }