- /**
- * Parse parameters from URI request and check their types and values.
- *
- * @param identifier {@link InstanceIdentifierContext}
- * @param uriInfo URI info
- * @return {@link WriterParameters}
- */
- public static WriterParameters parseUriParameters(final InstanceIdentifierContext<?> identifier,
- final UriInfo uriInfo) {
- final WriterParametersBuilder builder = new WriterParametersBuilder();
- 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(
- RestconfDataServiceConstant.ReadData.CONTENT, DEFAULT_CONTENT);
- final List<String> depth = queryParams.getOrDefault(
- RestconfDataServiceConstant.ReadData.DEPTH, DEFAULT_DEPTH);
- final List<String> withDefaults = queryParams.getOrDefault(
- RestconfDataServiceConstant.ReadData.WITH_DEFAULTS, List.of());
- // fields
- final List<String> fields = queryParams.getOrDefault(RestconfDataServiceConstant.ReadData.FIELDS, List.of());
-
- // parameter can be in URI at most once
- checkParameterCount(content, RestconfDataServiceConstant.ReadData.CONTENT);
- checkParameterCount(depth, RestconfDataServiceConstant.ReadData.DEPTH);
- checkParameterCount(fields, RestconfDataServiceConstant.ReadData.FIELDS);
- checkParameterCount(withDefaults, RestconfDataServiceConstant.ReadData.WITH_DEFAULTS);
-
- // check and set content
- final String contentValue = content.get(0);
- if (!contentValue.equals(RestconfDataServiceConstant.ReadData.ALL)) {
- if (!contentValue.equals(RestconfDataServiceConstant.ReadData.CONFIG)
- && !contentValue.equals(RestconfDataServiceConstant.ReadData.NONCONFIG)) {
- throw new RestconfDocumentedException(
- new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
- "Invalid content parameter: " + contentValue, null,
- "The content parameter value must be either config, nonconfig or all (default)"));
- }
- }
-
- builder.setContent(content.get(0));
-
- // check and set depth
- if (!depth.get(0).equals(RestconfDataServiceConstant.ReadData.UNBOUNDED)) {
- final Integer value = Ints.tryParse(depth.get(0));
-
- if (value == null || value < RestconfDataServiceConstant.ReadData.MIN_DEPTH
- || value > RestconfDataServiceConstant.ReadData.MAX_DEPTH) {
- throw new RestconfDocumentedException(
- new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.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 WithDefaults val = WithDefaults.forValue(str);
- if (val == null) {
- throw new RestconfDocumentedException(new RestconfError(RestconfError.ErrorType.PROTOCOL,
- RestconfError.ErrorTag.INVALID_VALUE, "Invalid with-defaults parameter: " + str, null,
- "The with-defaults parameter must be a string in " + WithDefaults.possibleValues()));
- }
-
- switch (val) {
- case REPORT_ALL:
- break;
- case REPORT_ALL_TAGGED:
- builder.setTagged(true);
- break;
- default:
- builder.setWithDefault(val.value());
- }
- }
- return builder.build();
- }
-