2 * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.databind.jaxrs;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.annotations.VisibleForTesting;
14 import java.text.ParseException;
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.Map.Entry;
19 import java.util.function.Function;
20 import java.util.stream.Collectors;
21 import javax.ws.rs.core.UriInfo;
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
25 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
26 import org.opendaylight.restconf.common.errors.RestconfError;
27 import org.opendaylight.restconf.nb.rfc8040.ContentParam;
28 import org.opendaylight.restconf.nb.rfc8040.DepthParam;
29 import org.opendaylight.restconf.nb.rfc8040.FieldsParam;
30 import org.opendaylight.restconf.nb.rfc8040.FilterParam;
31 import org.opendaylight.restconf.nb.rfc8040.InsertParam;
32 import org.opendaylight.restconf.nb.rfc8040.LeafNodesOnlyParam;
33 import org.opendaylight.restconf.nb.rfc8040.NotificationQueryParams;
34 import org.opendaylight.restconf.nb.rfc8040.PointParam;
35 import org.opendaylight.restconf.nb.rfc8040.ReadDataParams;
36 import org.opendaylight.restconf.nb.rfc8040.SkipNotificationDataParam;
37 import org.opendaylight.restconf.nb.rfc8040.StartTimeParam;
38 import org.opendaylight.restconf.nb.rfc8040.StopTimeParam;
39 import org.opendaylight.restconf.nb.rfc8040.WithDefaultsParam;
40 import org.opendaylight.restconf.nb.rfc8040.WriteDataParams;
41 import org.opendaylight.restconf.nb.rfc8040.legacy.QueryParameters;
42 import org.opendaylight.restconf.nb.rfc8040.utils.parser.NetconfFieldsTranslator;
43 import org.opendaylight.restconf.nb.rfc8040.utils.parser.WriterFieldsTranslator;
44 import org.opendaylight.yangtools.yang.common.ErrorTag;
45 import org.opendaylight.yangtools.yang.common.ErrorType;
48 public final class QueryParams {
49 private static final List<String> POSSIBLE_CONTENT = Arrays.stream(ContentParam.values())
50 .map(ContentParam::paramValue)
51 .collect(Collectors.toUnmodifiableList());
52 private static final List<String> POSSIBLE_WITH_DEFAULTS = Arrays.stream(WithDefaultsParam.values())
53 .map(WithDefaultsParam::paramValue)
54 .collect(Collectors.toUnmodifiableList());
55 private static final Set<String> KNOWN_PARAMS = Set.of(
57 ContentParam.uriName, DepthParam.uriName, FieldsParam.uriName, WithDefaultsParam.uriName,
59 InsertParam.uriName, PointParam.uriName,
61 FilterParam.uriName, StartTimeParam.uriName, StopTimeParam.uriName ,"odl-skip-notification-data");
64 private QueryParams() {
68 public static @NonNull NotificationQueryParams newNotificationQueryParams(final UriInfo uriInfo) {
69 StartTimeParam startTime = null;
70 StopTimeParam stopTime = null;
71 FilterParam filter = null;
72 LeafNodesOnlyParam leafNodesOnly = null;
73 SkipNotificationDataParam skipNotificationData = null;
75 for (Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
76 final String paramName = entry.getKey();
77 final List<String> paramValues = entry.getValue();
81 case FilterParam.uriName:
82 filter = optionalParam(FilterParam::forUriValue, paramName, paramValues);
84 case StartTimeParam.uriName:
85 startTime = optionalParam(StartTimeParam::forUriValue, paramName, paramValues);
87 case StopTimeParam.uriName:
88 stopTime = optionalParam(StopTimeParam::forUriValue, paramName, paramValues);
90 case LeafNodesOnlyParam.uriName:
91 leafNodesOnly = optionalParam(LeafNodesOnlyParam::forUriValue, paramName, paramValues);
93 case SkipNotificationDataParam.uriName:
94 skipNotificationData = optionalParam(SkipNotificationDataParam::forUriValue, paramName,
98 throw unhandledParam("notification", paramName);
100 } catch (IllegalArgumentException e) {
101 throw new RestconfDocumentedException("Invalid " + paramName + " value: " + e.getMessage(),
102 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, e);
107 return NotificationQueryParams.of(startTime, stopTime, filter, leafNodesOnly, skipNotificationData);
108 } catch (IllegalArgumentException e) {
109 throw new RestconfDocumentedException("Invalid query parameters: " + e.getMessage(), e);
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);
120 return identifier.getMountPoint() != null
121 ? QueryParameters.ofFieldPaths(params, NetconfFieldsTranslator.translate(identifier, fields))
122 : QueryParameters.ofFields(params, WriterFieldsTranslator.translate(identifier, fields));
126 * Parse parameters from URI request and check their types and values.
128 * @param uriInfo URI info
129 * @return {@link ReadDataParams}
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 boolean tagged = false;
138 for (Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
139 final String paramName = entry.getKey();
140 final List<String> paramValues = entry.getValue();
143 case ContentParam.uriName:
144 final String contentStr = optionalParam(paramName, paramValues);
145 if (contentStr != null) {
146 content = RestconfDocumentedException.throwIfNull(ContentParam.forUriValue(contentStr),
147 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
148 "Invalid content parameter: %s, allowed values are %s", contentStr, POSSIBLE_CONTENT);
151 case DepthParam.uriName:
152 final String depthStr = optionalParam(paramName, paramValues);
154 depth = DepthParam.forUriValue(depthStr);
155 } catch (IllegalArgumentException e) {
156 throw new RestconfDocumentedException(e, new RestconfError(ErrorType.PROTOCOL,
157 ErrorTag.INVALID_VALUE, "Invalid depth parameter: " + depthStr, null,
158 "The depth parameter must be an integer between 1 and 65535 or \"unbounded\""));
161 case FieldsParam.uriName:
162 final String fieldsStr = optionalParam(paramName, paramValues);
163 if (fieldsStr != null) {
165 fields = FieldsParam.parse(fieldsStr);
166 } catch (ParseException e) {
167 throw new RestconfDocumentedException(e, new RestconfError(ErrorType.PROTOCOL,
168 ErrorTag.INVALID_VALUE, "Invalid filds parameter: " + fieldsStr));
172 case WithDefaultsParam.uriName:
173 final String withDefaultsStr = optionalParam(paramName, paramValues);
174 if (withDefaultsStr != null) {
175 final WithDefaultsParam val = WithDefaultsParam.forUriValue(withDefaultsStr);
177 throw new RestconfDocumentedException(new RestconfError(ErrorType.PROTOCOL,
178 ErrorTag.INVALID_VALUE, "Invalid with-defaults parameter: " + withDefaultsStr, null,
179 "The with-defaults parameter must be a string in " + POSSIBLE_WITH_DEFAULTS));
187 case REPORT_ALL_TAGGED:
198 // FIXME: recognize pretty-print here
199 throw unhandledParam("read", paramName);
203 return ReadDataParams.of(content, depth, fields, withDefaults, tagged, false);
206 public static @NonNull WriteDataParams newWriteDataParams(final UriInfo uriInfo) {
207 InsertParam insert = null;
208 PointParam point = null;
210 for (final Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
211 final String uriName = entry.getKey();
212 final List<String> paramValues = entry.getValue();
214 case InsertParam.uriName:
215 final String instartStr = optionalParam(uriName, paramValues);
216 if (instartStr != null) {
217 insert = InsertParam.forUriValue(instartStr);
218 if (insert == null) {
219 throw new RestconfDocumentedException(
220 "Unrecognized insert parameter value '" + instartStr + "'", ErrorType.PROTOCOL,
221 ErrorTag.BAD_ELEMENT);
225 case PointParam.uriName:
226 final String pointStr = optionalParam(uriName, paramValues);
227 if (pointStr != null) {
228 point = PointParam.forUriValue(pointStr);
232 throw unhandledParam("write", uriName);
237 return WriteDataParams.of(insert, point);
238 } catch (IllegalArgumentException e) {
239 throw new RestconfDocumentedException("Invalid query parameters: " + e.getMessage(), e);
243 private static RestconfDocumentedException unhandledParam(final String operation, final String name) {
244 return KNOWN_PARAMS.contains(name)
245 ? new RestconfDocumentedException("Invalid parameter in " + operation + ": " + name,
246 ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE)
247 : new RestconfDocumentedException("Unknown parameter in " + operation + ": " + name,
248 ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ATTRIBUTE);
252 static @Nullable String optionalParam(final String name, final List<String> values) {
253 switch (values.size()) {
257 return requireNonNull(values.get(0));
259 throw new RestconfDocumentedException("Parameter " + name + " can appear at most once in request URI",
260 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
264 private static <T> @Nullable T optionalParam(final Function<String, @NonNull T> factory, final String name,
265 final List<String> values) {
266 final String str = optionalParam(name, values);
267 return str == null ? null : factory.apply(str);