*/
package org.opendaylight.restconf.nb.rfc8040.rests.utils;
+import static com.google.common.base.Verify.verifyNotNull;
import static org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserFieldsParameter.parseFieldsParameter;
import static org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserFieldsParameter.parseFieldsPaths;
* </ul>
*/
public final class ReadDataTransactionUtil {
- private static final Set<String> ALLOWED_PARAMETERS = Set.of(
- ContentParameter.uriName(),
- DepthParameter.uriName(),
- FieldsParameter.uriName(),
- WithDefaultsParameter.uriName());
- private static final List<String> DEFAULT_CONTENT = List.of(ContentParameter.ALL.uriValue());
- private static final List<String> DEFAULT_DEPTH = List.of(DepthParameter.unboundedUriValue());
+ private static final Set<String> ALLOWED_PARAMETERS = Set.of(ContentParameter.uriName(), DepthParameter.uriName(),
+ FieldsParameter.uriName(), WithDefaultsParameter.uriName());
private static final List<String> POSSIBLE_CONTENT = Arrays.stream(ContentParameter.values())
.map(ContentParameter::uriValue)
.collect(Collectors.toUnmodifiableList());
final MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
checkParametersTypes(queryParams.keySet(), ALLOWED_PARAMETERS);
- // read parameters from URI or set default values
- final List<String> content = queryParams.getOrDefault(ContentParameter.uriName(), DEFAULT_CONTENT);
- final List<String> depth = queryParams.getOrDefault(DepthParameter.uriName(), DEFAULT_DEPTH);
- final List<String> withDefaults = queryParams.getOrDefault(WithDefaultsParameter.uriName(), List.of());
- // fields
- final List<String> fields = queryParams.getOrDefault(FieldsParameter.uriName(), List.of());
-
- // parameter can be in URI at most once
- checkParameterCount(content, ContentParameter.uriName());
- checkParameterCount(depth, DepthParameter.uriName());
- checkParameterCount(fields, FieldsParameter.uriName());
- checkParameterCount(withDefaults, WithDefaultsParameter.uriName());
-
// check and set content
- final String contentStr = content.get(0);
- builder.setContent(RestconfDocumentedException.throwIfNull(
- ContentParameter.forUriValue(contentStr), ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
- "Invalid content parameter: %s, allowed values are %s", contentStr, POSSIBLE_CONTENT));
-
- final String depthStr = depth.get(0);
- try {
- builder.setDepth(DepthParameter.forUriValue(depthStr));
- } catch (IllegalArgumentException e) {
- throw new RestconfDocumentedException(e, new RestconfError(ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
- "Invalid depth parameter: " + depthStr, null,
- "The depth parameter must be an integer between 1 and 65535 or \"unbounded\""));
+ final String contentStr = getSingleParameter(queryParams, ContentParameter.uriName());
+ if (contentStr != null) {
+ builder.setContent(RestconfDocumentedException.throwIfNull(
+ ContentParameter.forUriValue(contentStr), ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
+ "Invalid content parameter: %s, allowed values are %s", contentStr, POSSIBLE_CONTENT));
+ }
+
+ // check and set depth
+ final String depthStr = getSingleParameter(queryParams, DepthParameter.uriName());
+ if (depthStr != null) {
+ try {
+ builder.setDepth(DepthParameter.forUriValue(depthStr));
+ } catch (IllegalArgumentException e) {
+ throw new RestconfDocumentedException(e, new RestconfError(ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
+ "Invalid depth parameter: " + depthStr, null,
+ "The depth parameter must be an integer between 1 and 65535 or \"unbounded\""));
+ }
}
// check and set fields
- if (!fields.isEmpty()) {
+ final String fieldsStr = getSingleParameter(queryParams, FieldsParameter.uriName());
+ if (fieldsStr != null) {
+ // FIXME: parse a FieldsParameter instead
if (identifier.getMountPoint() != null) {
- builder.setFieldPaths(parseFieldsPaths(identifier, fields.get(0)));
+ builder.setFieldPaths(parseFieldsPaths(identifier, fieldsStr));
} else {
- builder.setFields(parseFieldsParameter(identifier, fields.get(0)));
+ builder.setFields(parseFieldsParameter(identifier, fieldsStr));
}
}
// check and set withDefaults parameter
- if (!withDefaults.isEmpty()) {
- final String str = withDefaults.get(0);
- final WithDefaultsParameter val = WithDefaultsParameter.forUriValue(str);
+ final String withDefaultsStr = getSingleParameter(queryParams, WithDefaultsParameter.uriName());
+ if (withDefaultsStr != null) {
+ final WithDefaultsParameter val = WithDefaultsParameter.forUriValue(withDefaultsStr);
if (val == null) {
throw new RestconfDocumentedException(new RestconfError(ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
- "Invalid with-defaults parameter: " + str, null,
+ "Invalid with-defaults parameter: " + withDefaultsStr, null,
"The with-defaults parameter must be a string in " + POSSIBLE_WITH_DEFAULTS));
}
builder.setWithDefault(val);
}
}
+
return builder.build();
}
}
}
- /**
- * Check if URI does not contain value for the same parameter more than once.
- *
- * @param parameterValues URI parameter values
- * @param parameterName URI parameter name
- */
@VisibleForTesting
- static void checkParameterCount(final @NonNull List<String> parameterValues, final @NonNull String parameterName) {
- if (parameterValues.size() > 1) {
- throw new RestconfDocumentedException(
- "Parameter " + parameterName + " can appear at most once in request URI",
+ static @Nullable String getSingleParameter(final MultivaluedMap<String, String> params, final String name) {
+ final var values = params.get(name);
+ if (values == null) {
+ return null;
+ }
+
+ switch (values.size()) {
+ case 0:
+ return null;
+ case 1:
+ return verifyNotNull(values.get(0));
+ default:
+ throw new RestconfDocumentedException("Parameter " + name + " can appear at most once in request URI",
ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
}
}
* @param allowedParameters allowed parameters for operation
*/
@VisibleForTesting
- static void checkParametersTypes(final @NonNull Set<String> usedParameters,
- final @NonNull Set<String> allowedParameters) {
+ static void checkParametersTypes(final Set<String> usedParameters, final Set<String> allowedParameters) {
if (!allowedParameters.containsAll(usedParameters)) {
final Set<String> notAllowedParameters = usedParameters.stream()
.filter(param -> !allowedParameters.contains(param))