import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
+import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfDataServiceConstant.ReadData.WithDefaults;
import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserFieldsParameter;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.QNameModule;
// check and set withDefaults parameter
if (!withDefaults.isEmpty()) {
- switch (withDefaults.get(0)) {
- case RestconfDataServiceConstant.ReadData.REPORT_ALL_TAGGED_DEFAULT_VALUE:
- builder.setTagged(true);
+ 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 RestconfDataServiceConstant.ReadData.REPORT_ALL_DEFAULT_VALUE:
+ case REPORT_ALL_TAGGED:
+ builder.setTagged(true);
break;
default:
- builder.setWithDefault(withDefaults.get(0));
+ builder.setWithDefault(val.value());
}
}
return builder.build();
import static java.util.Objects.requireNonNull;
+import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import java.util.Arrays;
-import java.util.Map;
+import java.util.Set;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.opendaylight.yangtools.yang.common.QName;
public static final int MAX_DEPTH = 65535;
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";
+
+ /**
+ * With-default values, as per
+ * <a href="https://tools.ietf.org/html/rfc8040#section-4.8.9">RFC8040 section 4.8.9</a>.
+ */
+ enum WithDefaults {
+ /**
+ * All data nodes are reported.
+ */
+ REPORT_ALL("report-all"),
+ /**
+ * Data nodes set to the YANG default are not reported.
+ */
+ TRIM("trim"),
+ /**
+ * Data nodes set to the YANG default by the client are reported.
+ */
+ EXPLICIT("explicit"),
+ /**
+ * All data nodes are reported, and defaults are tagged.
+ */
+ REPORT_ALL_TAGGED("report-all-tagged");
+
+ private static final ImmutableMap<String, WithDefaults> VALUES =
+ Maps.uniqueIndex(Arrays.asList(values()), WithDefaults::value);
+
+ private @NonNull String value;
+
+ WithDefaults(final @NonNull String value) {
+ this.value = value;
+ }
+
+ public @NonNull String value() {
+ return value;
+ }
+
+ static @Nullable WithDefaults forValue(final String value) {
+ return VALUES.get(requireNonNull(value));
+ }
+
+ static @Nullable Set<String> possibleValues() {
+ return VALUES.keySet();
+ }
+ }
private ReadData() {
throw new UnsupportedOperationException("Util class.");
*/
AFTER("after");
- private static final Map<String, Insert> VALUES = Maps.uniqueIndex(Arrays.asList(values()), Insert::value);
+ private static final ImmutableMap<String, Insert> VALUES =
+ Maps.uniqueIndex(Arrays.asList(values()), Insert::value);
private @NonNull String value;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
import org.opendaylight.restconf.common.context.WriterParameters;
import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
+import org.opendaylight.restconf.common.errors.RestconfError;
import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
import org.opendaylight.restconf.nb.rfc8040.rests.transactions.NetconfRestconfStrategy;
import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfDataServiceConstant.ReadData;
+import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfDataServiceConstant.ReadData.WithDefaults;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
// 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));
+ parameters.put(RestconfDataServiceConstant.ReadData.WITH_DEFAULTS, Collections.singletonList("explicit"));
when(uriInfo.getQueryParameters()).thenReturn(parameters);
final WriterParameters writerParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
- assertEquals(preparedDefaultValue, writerParameters.getWithDefault());
+ assertSame(WithDefaults.EXPLICIT.value(), writerParameters.getWithDefault());
assertFalse(writerParameters.isTagged());
}
+ /**
+ * Testing parsing of with-defaults parameter which value which is not supported.
+ */
+ @Test
+ public void parseUriParametersWithDefaultInvalidTest() {
+ // 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("invalid"));
+ when(uriInfo.getQueryParameters()).thenReturn(parameters);
+
+ final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
+ () -> ReadDataTransactionUtil.parseUriParameters(context, uriInfo));
+ final List<RestconfError> errors = ex.getErrors();
+ assertEquals(1, errors.size());
+ assertEquals(ErrorTag.INVALID_VALUE, errors.get(0).getErrorTag());
+ }
+
/**
* 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}.
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));
+ Collections.singletonList(ReadData.WithDefaults.REPORT_ALL_TAGGED.value()));
when(uriInfo.getQueryParameters()).thenReturn(parameters);
final WriterParameters writerParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
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));
+ Collections.singletonList(ReadData.WithDefaults.REPORT_ALL.value()));
when(uriInfo.getQueryParameters()).thenReturn(parameters);
final WriterParameters writerParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);