private final List<Set<QName>> fields;
private final boolean prettyPrint;
private final boolean tagged;
+ private final String withDefault;
private WriterParameters(final WriterParametersBuilder builder) {
this.content = builder.content;
this.fields = builder.fields;
this.prettyPrint = builder.prettyPrint;
this.tagged = builder.tagged;
+ this.withDefault = builder.withDefault;
}
public String getContent() {
return this.tagged;
}
+ public String getWithDefault() {
+ return withDefault;
+ }
+
public static class WriterParametersBuilder {
private String content;
private Integer depth;
private List<Set<QName>> fields;
private boolean prettyPrint;
private boolean tagged;
+ private String withDefault;
public WriterParametersBuilder() {}
return this;
}
+ public WriterParametersBuilder setWithDefault(final String withDefault) {
+ this.withDefault = withDefault;
+ return this;
+ }
+
public WriterParameters build() {
return new WriterParameters(this);
}
final SchemaContextRef schemaContextRef = new SchemaContextRef(this.schemaContextHandler.get());
final InstanceIdentifierContext<?> instanceIdentifier = ParserIdentifier.toInstanceIdentifier(
identifier, schemaContextRef.get(), Optional.of(this.mountPointServiceHandler.get()));
-
- boolean withDefaUsed = false;
- String withDefa = null;
-
- for (final Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
- switch (entry.getKey()) {
- case "with-defaults":
- if (!withDefaUsed) {
- withDefaUsed = true;
- withDefa = entry.getValue().iterator().next();
- } else {
- throw new RestconfDocumentedException("With-defaults parameter can be used only once.");
- }
- break;
- default:
- LOG.info("Unknown key : {}.", entry.getKey());
- break;
- }
- }
- boolean tagged = false;
- if (withDefaUsed) {
- if ("report-all-tagged".equals(withDefa)) {
- tagged = true;
- withDefa = null;
- }
- if ("report-all".equals(withDefa)) {
- withDefa = null;
- }
- }
-
- final WriterParameters parameters = ReadDataTransactionUtil.parseUriParameters(
- instanceIdentifier, uriInfo, tagged);
+ final WriterParameters parameters = ReadDataTransactionUtil.parseUriParameters(instanceIdentifier, uriInfo);
final DOMMountPoint mountPoint = instanceIdentifier.getMountPoint();
final TransactionChainHandler localTransactionChainHandler;
final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
instanceIdentifier, mountPoint, localTransactionChainHandler);
- final NormalizedNode<?, ?> node =
- ReadDataTransactionUtil.readData(identifier, parameters.getContent(), transactionNode, withDefa,
- schemaContextRef, uriInfo);
+ final NormalizedNode<?, ?> node = ReadDataTransactionUtil.readData(identifier, parameters.getContent(),
+ transactionNode, parameters.getWithDefault(), schemaContextRef, uriInfo);
if (identifier != null && identifier.contains(STREAM_PATH) && identifier.contains(STREAM_ACCESS_PATH_PART)
&& identifier.contains(STREAM_LOCATION_PATH_PART)) {
final String value = (String) node.getValue();
/**
* Parse parameters from URI request and check their types and values.
*
- *
* @param identifier
* {@link InstanceIdentifierContext}
* @param uriInfo
* URI info
- * @param tagged
- * set tagged for {@link WriterParameters}
* @return {@link WriterParameters}
*/
- public static @NonNull WriterParameters parseUriParameters(final @NonNull InstanceIdentifierContext<?> identifier,
- final @Nullable UriInfo uriInfo, final boolean tagged) {
- return parseParams(identifier, uriInfo, tagged);
- }
-
- /**
- * Parse parameters from URI request and check their types and values.
- *
- *
- * @param identifier
- * {@link InstanceIdentifierContext}
- * @param uriInfo
- * URI info
- * @return {@link WriterParameters}
- */
- public static @NonNull WriterParameters parseUriParameters(final @NonNull InstanceIdentifierContext<?> identifier,
- final @Nullable UriInfo uriInfo) {
- return parseParams(identifier, uriInfo, false);
- }
-
- private static WriterParameters parseParams(final InstanceIdentifierContext<?> identifier, final UriInfo uriInfo,
- final boolean tagged) {
+ public static WriterParameters parseUriParameters(final InstanceIdentifierContext<?> identifier,
+ final UriInfo uriInfo) {
final WriterParametersBuilder builder = new WriterParametersBuilder();
- builder.setTagged(tagged);
if (uriInfo == null) {
return builder.build();
final List<String> depth = uriInfo.getQueryParameters().getOrDefault(
RestconfDataServiceConstant.ReadData.DEPTH,
Collections.singletonList(RestconfDataServiceConstant.ReadData.UNBOUNDED));
+ final List<String> withDefaults = uriInfo.getQueryParameters().getOrDefault(
+ RestconfDataServiceConstant.ReadData.WITH_DEFAULTS,
+ Collections.emptyList());
// fields
final List<String> fields = uriInfo.getQueryParameters().getOrDefault(
RestconfDataServiceConstant.ReadData.FIELDS,
ParametersUtil.checkParameterCount(content, RestconfDataServiceConstant.ReadData.CONTENT);
ParametersUtil.checkParameterCount(depth, RestconfDataServiceConstant.ReadData.DEPTH);
ParametersUtil.checkParameterCount(fields, RestconfDataServiceConstant.ReadData.FIELDS);
+ ParametersUtil.checkParameterCount(fields, RestconfDataServiceConstant.ReadData.WITH_DEFAULTS);
// check and set content
final String contentValue = content.get(0);
builder.setFields(ParserFieldsParameter.parseFieldsParameter(identifier, fields.get(0)));
}
+ // check and set withDefaults parameter
+ if (!withDefaults.isEmpty()) {
+ switch (withDefaults.get(0)) {
+ case RestconfDataServiceConstant.ReadData.REPORT_ALL_TAGGED_DEFAULT_VALUE:
+ builder.setTagged(true);
+ break;
+ case RestconfDataServiceConstant.ReadData.REPORT_ALL_DEFAULT_VALUE:
+ break;
+ default:
+ builder.setWithDefault(withDefaults.get(0));
+ }
+ }
+
return builder.build();
}
public static final String READ_TYPE_TX = "READ";
public static final String WITH_DEFAULTS = "with-defaults";
+ public static final String REPORT_ALL_DEFAULT_VALUE = "report-all";
+ public static final String REPORT_ALL_TAGGED_DEFAULT_VALUE = "report-all-tagged";
private ReadData() {
throw new UnsupportedOperationException("Util class.");
package org.opendaylight.restconf.nb.rfc8040.rests.utils;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when;
import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
import org.opendaylight.restconf.nb.rfc8040.rests.transactions.TransactionVarsWrapper;
+import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfDataServiceConstant.ReadData;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
assertEquals("Error status code is not correct", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
}
}
+
+ /**
+ * Testing parsing of with-defaults parameter which value doesn't match report-all or report-all-tagged patterns
+ * - non-reporting setting.
+ */
+ @Test
+ public void parseUriParametersWithDefaultAndNonTaggedTest() {
+ // preparation of input data
+ final UriInfo uriInfo = Mockito.mock(UriInfo.class);
+ final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
+ final String preparedDefaultValue = "sample-default";
+ parameters.put(RestconfDataServiceConstant.ReadData.WITH_DEFAULTS,
+ Collections.singletonList(preparedDefaultValue));
+ when(uriInfo.getQueryParameters()).thenReturn(parameters);
+
+ final WriterParameters writerParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
+ assertEquals(preparedDefaultValue, writerParameters.getWithDefault());
+ assertFalse(writerParameters.isTagged());
+ }
+
+ /**
+ * Testing parsing of with-defaults parameter which value matches 'report-all-tagged' setting - default value should
+ * be set to {@code null} and tagged flag should be set to {@code true}.
+ */
+ @Test
+ public void parseUriParametersWithDefaultAndTaggedTest() {
+ // preparation of input data
+ final UriInfo uriInfo = Mockito.mock(UriInfo.class);
+ final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
+ parameters.put(RestconfDataServiceConstant.ReadData.WITH_DEFAULTS,
+ Collections.singletonList(ReadData.REPORT_ALL_TAGGED_DEFAULT_VALUE));
+ when(uriInfo.getQueryParameters()).thenReturn(parameters);
+
+ final WriterParameters writerParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
+ assertNull(writerParameters.getWithDefault());
+ assertTrue(writerParameters.isTagged());
+ }
+
+ /**
+ * Testing parsing of with-defaults parameter which value matches 'report-all' setting - default value should
+ * be set to {@code null} and tagged flag should be set to {@code false}.
+ */
+ @Test
+ public void parseUriParametersWithDefaultAndReportAllTest() {
+ // preparation of input data
+ final UriInfo uriInfo = Mockito.mock(UriInfo.class);
+ final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
+ parameters.put(RestconfDataServiceConstant.ReadData.WITH_DEFAULTS,
+ Collections.singletonList(ReadData.REPORT_ALL_DEFAULT_VALUE));
+ when(uriInfo.getQueryParameters()).thenReturn(parameters);
+
+ final WriterParameters writerParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
+ assertNull(writerParameters.getWithDefault());
+ assertFalse(writerParameters.isTagged());
+ }
}