From 8450b69800c827ba221351eb0c374fcbd9146593 Mon Sep 17 00:00:00 2001 From: Jan Srnicek Date: Tue, 7 Feb 2017 09:33:38 +0100 Subject: Prevent submitting of structural reader for list node Static customizer must be provided instead till HONEYCOMB-344 is resolved Change-Id: Id71f69fe23b156e6de10eed06a3596de8e602bbe Signed-off-by: Jan Srnicek --- .../impl/read/registry/CompositeReaderRegistryBuilder.java | 12 +++++++++++- .../read/registry/CompositeReaderRegistryBuilderTest.java | 5 +++++ .../java/io/fd/honeycomb/translate/util/DataObjects.java | 3 +++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReaderRegistryBuilder.java b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReaderRegistryBuilder.java index 36ab90995..dd6a838c6 100644 --- a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReaderRegistryBuilder.java +++ b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReaderRegistryBuilder.java @@ -25,18 +25,23 @@ import io.fd.honeycomb.translate.read.registry.ModifiableReaderRegistryBuilder; import io.fd.honeycomb.translate.read.registry.ReaderRegistry; import io.fd.honeycomb.translate.read.registry.ReaderRegistryBuilder; import io.fd.honeycomb.translate.util.AbstractSubtreeManagerRegistryBuilderBuilder; + import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.stream.Collectors; import javax.annotation.Nonnull; import javax.annotation.concurrent.NotThreadSafe; + import org.opendaylight.yangtools.concepts.Builder; import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.Identifiable; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static com.google.common.base.Preconditions.checkArgument; + @NotThreadSafe public final class CompositeReaderRegistryBuilder extends AbstractSubtreeManagerRegistryBuilderBuilder>, ReaderRegistry> @@ -48,13 +53,18 @@ public final class CompositeReaderRegistryBuilder protected Reader> getSubtreeHandler(@Nonnull final Set> handledChildren, @Nonnull final Reader> reader) { return reader instanceof Initializer - ? InitSubtreeReader.createForReader(handledChildren, (InitReader)reader) + ? InitSubtreeReader.createForReader(handledChildren, (InitReader) reader) : SubtreeReader.createForReader(handledChildren, reader); } @Override public void addStructuralReader(@Nonnull InstanceIdentifier id, @Nonnull Class> builderType) { + // TODO https://jira.fd.io/browse/HONEYCOMB-344 provide variant of this method for list nodes + // prevent case to submit structural reader for list, which would cause fail because target setter consumes + // List, not Target type. If node is static, customizer that handles it should return static list + // of IDs. + checkArgument(!Identifiable.class.isAssignableFrom(id.getTargetType()), "Structural readers cannot be used for keyed nodes[node type %s]", id.getTargetType()); add(GenericReader.createReflexive(id, builderType)); } diff --git a/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReaderRegistryBuilderTest.java b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReaderRegistryBuilderTest.java index 4dee8e60f..0c7c71eac 100644 --- a/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReaderRegistryBuilderTest.java +++ b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReaderRegistryBuilderTest.java @@ -66,6 +66,11 @@ public class CompositeReaderRegistryBuilderTest { return mock; } + @Test(expected = IllegalArgumentException.class) + public void testPreventStructuralReaderForList() { + new CompositeReaderRegistryBuilder().addStructuralReader(InstanceIdentifier.create(DataObjects.DataObjectK.class), DataObjects.DataObjectKBuilder.class); + } + @Test public void testCompositeStructure() throws Exception { final CompositeReaderRegistryBuilder compositeReaderRegistryBuilder = new CompositeReaderRegistryBuilder(); diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/DataObjects.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/DataObjects.java index 8dd3d2065..77db65c5d 100644 --- a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/DataObjects.java +++ b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/DataObjects.java @@ -60,6 +60,9 @@ public class DataObjects { } } + public interface DataObjectKBuilder extends org.opendaylight.yangtools.concepts.Builder{ + } + public interface DataObject1ChildK extends DataObject, ChildOf, Identifiable { // needs to be defined like this to have paths totally equal after cutting path for internally keyed id inside infra InstanceIdentifier IID = -- cgit 1.2.3-korg