- /**
- * Parse parameters from URI request and check their types and values.
- *
- * @param identifier {@link InstanceIdentifierContext}
- * @param uriInfo URI info
- * @return {@link QueryParameters}
- */
- public static QueryParameters parseUriParameters(final InstanceIdentifierContext<?> identifier,
- final UriInfo uriInfo) {
- final QueryParameters.Builder builder = QueryParameters.builder();
- if (uriInfo == null) {
- return builder.build();
- }
-
- // check only allowed parameters
- 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 contentValueStr = content.get(0);
- builder.setContent(RestconfDocumentedException.throwIfNull(
- ContentParameter.forUriValue(contentValueStr), ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
- "Invalid content parameter: %s, allowed values are %s", contentValueStr, POSSIBLE_CONTENT));
-
- // check and set depth
- if (!depth.get(0).equals(UNBOUNDED)) {
- final Integer value = Ints.tryParse(depth.get(0));
-
- if (value == null || value < MIN_DEPTH || value > MAX_DEPTH) {
- throw new RestconfDocumentedException(
- new RestconfError(ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
- "Invalid depth parameter: " + depth, null,
- "The depth parameter must be an integer between 1 and 65535 or \"unbounded\""));
- } else {
- builder.setDepth(value);
- }
- }
-
- // check and set fields
- if (!fields.isEmpty()) {
- if (identifier.getMountPoint() != null) {
- builder.setFieldPaths(parseFieldsPaths(identifier, fields.get(0)));
- } else {
- builder.setFields(parseFieldsParameter(identifier, fields.get(0)));
- }
- }
-
- // check and set withDefaults parameter
- if (!withDefaults.isEmpty()) {
- final String str = withDefaults.get(0);
- final WithDefaultsParameter val = WithDefaultsParameter.forUriValue(str);
- if (val == null) {
- throw new RestconfDocumentedException(new RestconfError(ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
- "Invalid with-defaults parameter: " + str, null,
- "The with-defaults parameter must be a string in " + POSSIBLE_WITH_DEFAULTS));
- }
-
- switch (val) {
- case REPORT_ALL:
- break;
- case REPORT_ALL_TAGGED:
- builder.setTagged(true);
- break;
- default:
- builder.setWithDefault(val);
- }
- }
- return builder.build();
- }
-