From 757222979bc02d0aaba1870eea36413383d15bde Mon Sep 17 00:00:00 2001 From: Maros Marsalek Date: Tue, 8 Nov 2016 10:13:36 +0100 Subject: HONEYCOMB-270 Add isPresent() to Readers/Customizers So that they can influence whether empty data is to be considered as present + Move registries implementations from util to impl + Introduce DelegatingReader trait + Extend GenericReader where possible to reduce duplication Change-Id: I5a416acd0c4eab1fbc30fcbe585719991dbe9215 Signed-off-by: Maros Marsalek --- .../fd/honeycomb/benchmark/util/StaticReader.java | 5 + .../data/impl/HoneycombReadInfraTest.java | 16 +- .../data/impl/HoneycombSubtreeReadInfraTest.java | 2 +- .../data/impl/HoneycombWriteInfraTest.java | 2 +- .../distro/data/config/WriterRegistryProvider.java | 2 +- .../data/oper/ReaderRegistryBuilderProvider.java | 2 +- .../io/fd/honeycomb/translate/read/Reader.java | 15 + infra/translate-impl/pom.xml | 7 + .../translate/impl/read/GenericListReader.java | 30 +- .../translate/impl/read/GenericReader.java | 11 + .../impl/read/registry/CompositeReader.java | 229 ++++++++++++++ .../read/registry/CompositeReaderRegistry.java | 131 ++++++++ .../registry/CompositeReaderRegistryBuilder.java | 112 +++++++ .../impl/read/registry/InitSubtreeReader.java | 72 +++++ .../impl/read/registry/SubtreeReader.java | 216 ++++++++++++++ .../impl/read/registry/TypeHierarchy.java | 101 +++++++ .../impl/write/registry/FlatWriterRegistry.java | 314 ++++++++++++++++++++ .../write/registry/FlatWriterRegistryBuilder.java | 71 +++++ .../impl/write/registry/SubtreeWriter.java | 85 ++++++ .../translate/impl/read/GenericListReaderTest.java | 3 +- .../CompositeReaderRegistryBuilderTest.java | 114 +++++++ .../read/registry/CompositeReaderRegistryTest.java | 124 ++++++++ .../impl/read/registry/CompositeReaderTest.java | 124 ++++++++ .../impl/read/registry/SubtreeReaderTest.java | 125 ++++++++ .../impl/read/registry/TypeHierarchyTest.java | 72 +++++ .../registry/FlatWriterRegistryBuilderTest.java | 193 ++++++++++++ .../write/registry/FlatWriterRegistryTest.java | 328 +++++++++++++++++++++ .../impl/write/registry/SubtreeWriterTest.java | 85 ++++++ .../translate/spi/read/ReaderCustomizer.java | 15 + infra/translate-utils/pom.xml | 16 + .../translate/util/read/AbstractGenericReader.java | 9 +- .../translate/util/read/BindingBrokerReader.java | 5 + .../translate/util/read/DelegatingReader.java | 103 +++++++ .../util/read/KeepaliveReaderWrapper.java | 40 +-- .../translate/util/read/ReflexiveReader.java | 57 ---- .../util/read/ReflexiveReaderCustomizer.java | 2 +- .../util/read/registry/CompositeReader.java | 241 --------------- .../read/registry/CompositeReaderRegistry.java | 131 -------- .../registry/CompositeReaderRegistryBuilder.java | 114 ------- .../util/read/registry/InitSubtreeReader.java | 72 ----- .../util/read/registry/SubtreeReader.java | 241 --------------- .../util/read/registry/TypeHierarchy.java | 101 ------- .../util/write/registry/FlatWriterRegistry.java | 314 -------------------- .../write/registry/FlatWriterRegistryBuilder.java | 71 ----- .../util/write/registry/SubtreeWriter.java | 85 ------ .../CompositeReaderRegistryBuilderTest.java | 114 ------- .../read/registry/CompositeReaderRegistryTest.java | 112 ------- .../util/read/registry/CompositeReaderTest.java | 124 -------- .../util/read/registry/SubtreeReaderTest.java | 124 -------- .../util/read/registry/TypeHierarchyTest.java | 72 ----- .../registry/FlatWriterRegistryBuilderTest.java | 193 ------------ .../write/registry/FlatWriterRegistryTest.java | 328 --------------------- .../util/write/registry/SubtreeWriterTest.java | 84 ------ .../honeycomb/vppioam/impl/VppIoamModuleTest.java | 9 +- .../java/io/fd/honeycomb/lisp/LispModuleTest.java | 4 +- .../java/io/fd/honeycomb/nat/NatModuleTest.java | 4 +- .../fd/honeycomb/vppnsh/impl/VppNshModuleTest.java | 4 +- .../honeycomb/translate/v3po/V3poModuleTest.java | 4 +- .../translate/v3po/vppstate/VppStateTest.java | 2 +- 59 files changed, 2716 insertions(+), 2670 deletions(-) create mode 100644 infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReader.java create mode 100644 infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReaderRegistry.java create mode 100644 infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReaderRegistryBuilder.java create mode 100644 infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/InitSubtreeReader.java create mode 100644 infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/SubtreeReader.java create mode 100644 infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/TypeHierarchy.java create mode 100644 infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistry.java create mode 100644 infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistryBuilder.java create mode 100644 infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/write/registry/SubtreeWriter.java create mode 100644 infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReaderRegistryBuilderTest.java create mode 100644 infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReaderRegistryTest.java create mode 100644 infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReaderTest.java create mode 100644 infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/registry/SubtreeReaderTest.java create mode 100644 infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/registry/TypeHierarchyTest.java create mode 100644 infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistryBuilderTest.java create mode 100644 infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistryTest.java create mode 100644 infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/registry/SubtreeWriterTest.java create mode 100644 infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/DelegatingReader.java delete mode 100644 infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveReader.java delete mode 100644 infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/CompositeReader.java delete mode 100644 infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistry.java delete mode 100644 infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryBuilder.java delete mode 100644 infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/InitSubtreeReader.java delete mode 100644 infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/SubtreeReader.java delete mode 100644 infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/TypeHierarchy.java delete mode 100644 infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/write/registry/FlatWriterRegistry.java delete mode 100644 infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/write/registry/FlatWriterRegistryBuilder.java delete mode 100644 infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/write/registry/SubtreeWriter.java delete mode 100644 infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryBuilderTest.java delete mode 100644 infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryTest.java delete mode 100644 infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderTest.java delete mode 100644 infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/SubtreeReaderTest.java delete mode 100644 infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/TypeHierarchyTest.java delete mode 100644 infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/write/registry/FlatWriterRegistryBuilderTest.java delete mode 100644 infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/write/registry/FlatWriterRegistryTest.java delete mode 100644 infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/write/registry/SubtreeWriterTest.java diff --git a/infra/it/benchmark/src/main/java/io/fd/honeycomb/benchmark/util/StaticReader.java b/infra/it/benchmark/src/main/java/io/fd/honeycomb/benchmark/util/StaticReader.java index 90e560152..76ad0e3d4 100644 --- a/infra/it/benchmark/src/main/java/io/fd/honeycomb/benchmark/util/StaticReader.java +++ b/infra/it/benchmark/src/main/java/io/fd/honeycomb/benchmark/util/StaticReader.java @@ -55,6 +55,11 @@ public final class StaticReader> impl '}'; } + @Override + public boolean isPresent(final InstanceIdentifier id, final T built, final ReadContext ctx) { + return true; + } + @Nonnull @Override public Optional read(@Nonnull final InstanceIdentifier id, diff --git a/infra/it/it-test/src/test/java/io/fd/honeycomb/data/impl/HoneycombReadInfraTest.java b/infra/it/it-test/src/test/java/io/fd/honeycomb/data/impl/HoneycombReadInfraTest.java index 5ec2b55ba..6d4c9e6f3 100644 --- a/infra/it/it-test/src/test/java/io/fd/honeycomb/data/impl/HoneycombReadInfraTest.java +++ b/infra/it/it-test/src/test/java/io/fd/honeycomb/data/impl/HoneycombReadInfraTest.java @@ -24,6 +24,7 @@ import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyListOf; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.atMost; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.inOrder; @@ -36,6 +37,8 @@ import com.google.common.collect.Lists; import com.google.common.collect.Multimap; import com.google.common.util.concurrent.CheckedFuture; import io.fd.honeycomb.translate.impl.read.GenericListReader; +import io.fd.honeycomb.translate.impl.read.GenericReader; +import io.fd.honeycomb.translate.impl.read.registry.CompositeReaderRegistryBuilder; import io.fd.honeycomb.translate.read.ListReader; import io.fd.honeycomb.translate.read.ReadContext; import io.fd.honeycomb.translate.read.ReadFailedException; @@ -43,8 +46,7 @@ import io.fd.honeycomb.translate.read.Reader; import io.fd.honeycomb.translate.read.registry.ReaderRegistry; import io.fd.honeycomb.translate.util.RWUtils; import io.fd.honeycomb.translate.util.read.ReflexiveListReaderCustomizer; -import io.fd.honeycomb.translate.util.read.ReflexiveReader; -import io.fd.honeycomb.translate.util.read.registry.CompositeReaderRegistryBuilder; +import io.fd.honeycomb.translate.util.read.ReflexiveReaderCustomizer; import java.lang.reflect.InvocationTargetException; import java.util.List; import java.util.stream.Collectors; @@ -185,8 +187,11 @@ public class HoneycombReadInfraTest extends AbstractInfraTest { } } - private > void verifyNoMoreReadInteractions(final Reader reader) { + private > void verifyNoMoreReadInteractions(final Reader reader) + throws ReadFailedException { verify(reader, atLeastOnce()).getManagedDataObjectType(); + // Just mark all the isPresent calls as verified + verify(reader, atMost(100)).isPresent(any(InstanceIdentifier.class), any(), any()); verifyNoMoreInteractions(reader); } @@ -254,7 +259,8 @@ public class HoneycombReadInfraTest extends AbstractInfraTest { static > Reader mockReader(InstanceIdentifier id, CurrentAttributesReader currentAttributesReader, Class builderClass) { - final ReflexiveReader reflex = new ReflexiveReader(id, builderClass) { + final Reader reflex = new GenericReader(id, + new ReflexiveReaderCustomizer<>(id.getTargetType(), builderClass)) { @Override public void readCurrentAttributes(@Nonnull final InstanceIdentifier id, @@ -270,6 +276,7 @@ public class HoneycombReadInfraTest extends AbstractInfraTest { final Reader mock = mock(Reader.class); try { doAnswer(i -> reflexiveAnswer(reflex, i)).when(mock).read(any(InstanceIdentifier.class), any(ReadContext.class)); + doAnswer(i -> reflexiveAnswer(reflex, i)).when(mock).isPresent(any(InstanceIdentifier.class), any(), any()); doAnswer(i -> reflexiveAnswer(reflex, i)).when(mock) .readCurrentAttributes(any(InstanceIdentifier.class), any(builderClass), any(ReadContext.class)); doAnswer(i -> reflexiveAnswer(reflex, i)).when(mock).merge(any(Builder.class), any(id.getTargetType())); @@ -311,6 +318,7 @@ public class HoneycombReadInfraTest extends AbstractInfraTest { // not using eq(id) instead using any(InstanceIdentifier.class) due to InstanceIdentifier.equals weird behavior // with wildcarded instance identifiers for lists doAnswer(i -> reflexiveAnswer(reflex, i)).when(mock).read(any(InstanceIdentifier.class), any(ReadContext.class)); + doAnswer(i -> reflexiveAnswer(reflex, i)).when(mock).isPresent(any(InstanceIdentifier.class), any(), any()); doAnswer(i -> reflexiveAnswer(reflex, i)).when(mock) .readCurrentAttributes(any(InstanceIdentifier.class), any(builderClass), any(ReadContext.class)); doAnswer(i -> reflexiveAnswer(reflex, i)).when(mock).merge(any(Builder.class), any(id.getTargetType())); diff --git a/infra/it/it-test/src/test/java/io/fd/honeycomb/data/impl/HoneycombSubtreeReadInfraTest.java b/infra/it/it-test/src/test/java/io/fd/honeycomb/data/impl/HoneycombSubtreeReadInfraTest.java index 5bb81b477..1894f4942 100644 --- a/infra/it/it-test/src/test/java/io/fd/honeycomb/data/impl/HoneycombSubtreeReadInfraTest.java +++ b/infra/it/it-test/src/test/java/io/fd/honeycomb/data/impl/HoneycombSubtreeReadInfraTest.java @@ -32,7 +32,7 @@ import io.fd.honeycomb.translate.read.ReadFailedException; import io.fd.honeycomb.translate.read.Reader; import io.fd.honeycomb.translate.read.registry.ReaderRegistry; import io.fd.honeycomb.translate.util.read.ReflexiveListReaderCustomizer; -import io.fd.honeycomb.translate.util.read.registry.CompositeReaderRegistryBuilder; +import io.fd.honeycomb.translate.impl.read.registry.CompositeReaderRegistryBuilder; import java.util.List; import java.util.stream.Collectors; import javax.annotation.Nonnull; diff --git a/infra/it/it-test/src/test/java/io/fd/honeycomb/data/impl/HoneycombWriteInfraTest.java b/infra/it/it-test/src/test/java/io/fd/honeycomb/data/impl/HoneycombWriteInfraTest.java index 12ae6568d..bd51e39de 100644 --- a/infra/it/it-test/src/test/java/io/fd/honeycomb/data/impl/HoneycombWriteInfraTest.java +++ b/infra/it/it-test/src/test/java/io/fd/honeycomb/data/impl/HoneycombWriteInfraTest.java @@ -29,7 +29,7 @@ import static org.mockito.Mockito.when; import com.google.common.collect.Lists; import com.google.common.collect.Sets; import io.fd.honeycomb.data.DataModification; -import io.fd.honeycomb.translate.util.write.registry.FlatWriterRegistryBuilder; +import io.fd.honeycomb.translate.impl.write.registry.FlatWriterRegistryBuilder; import io.fd.honeycomb.translate.write.WriteFailedException; import io.fd.honeycomb.translate.write.WriteContext; import io.fd.honeycomb.translate.write.Writer; diff --git a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/data/config/WriterRegistryProvider.java b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/data/config/WriterRegistryProvider.java index f3e8ce200..fa0c298fb 100644 --- a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/data/config/WriterRegistryProvider.java +++ b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/data/config/WriterRegistryProvider.java @@ -18,7 +18,7 @@ package io.fd.honeycomb.infra.distro.data.config; import com.google.inject.Inject; import io.fd.honeycomb.infra.distro.ProviderTrait; -import io.fd.honeycomb.translate.util.write.registry.FlatWriterRegistryBuilder; +import io.fd.honeycomb.translate.impl.write.registry.FlatWriterRegistryBuilder; import io.fd.honeycomb.translate.write.WriterFactory; import io.fd.honeycomb.translate.write.registry.ModifiableWriterRegistryBuilder; import java.util.HashSet; diff --git a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/data/oper/ReaderRegistryBuilderProvider.java b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/data/oper/ReaderRegistryBuilderProvider.java index 05177d54d..ab967e164 100644 --- a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/data/oper/ReaderRegistryBuilderProvider.java +++ b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/data/oper/ReaderRegistryBuilderProvider.java @@ -20,7 +20,7 @@ import com.google.inject.Inject; import io.fd.honeycomb.infra.distro.ProviderTrait; import io.fd.honeycomb.translate.read.ReaderFactory; import io.fd.honeycomb.translate.read.registry.ModifiableReaderRegistryBuilder; -import io.fd.honeycomb.translate.util.read.registry.CompositeReaderRegistryBuilder; +import io.fd.honeycomb.translate.impl.read.registry.CompositeReaderRegistryBuilder; import java.util.HashSet; import java.util.Set; diff --git a/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/Reader.java b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/Reader.java index dd9944624..7138006e0 100644 --- a/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/Reader.java +++ b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/Reader.java @@ -34,6 +34,21 @@ public interface Reader> extends Subt // TODO HONEYCOMB-169 make async + /** + * Check whether the data from current reader are present or not. + * Invoked after {@link #readCurrentAttributes(InstanceIdentifier, Builder, ReadContext)} + * + * @param id Keyed instance identifier of read data + * @param built Read data as returned from builder + * after {@link #readCurrentAttributes(InstanceIdentifier, Builder, ReadContext)} invocation + * @param ctx Read context + * + * + * @return true if the result value is present. + */ + boolean isPresent(@Nonnull InstanceIdentifier id, @Nonnull D built, @Nonnull ReadContext ctx) + throws ReadFailedException; + /** * Reads data identified by id * diff --git a/infra/translate-impl/pom.xml b/infra/translate-impl/pom.xml index 7892e90af..b431c07f9 100644 --- a/infra/translate-impl/pom.xml +++ b/infra/translate-impl/pom.xml @@ -46,6 +46,13 @@ translate-utils ${project.version} + + ${project.groupId} + translate-utils + ${project.version} + test-jar + test + junit diff --git a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericListReader.java b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericListReader.java index ad93e8fe9..b06ac060f 100644 --- a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericListReader.java +++ b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericListReader.java @@ -25,7 +25,6 @@ import io.fd.honeycomb.translate.read.ReadContext; import io.fd.honeycomb.translate.read.ReadFailedException; import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer; import io.fd.honeycomb.translate.util.RWUtils; -import io.fd.honeycomb.translate.util.read.AbstractGenericReader; import java.util.ArrayList; import java.util.List; import javax.annotation.Nonnull; @@ -48,12 +47,10 @@ import org.slf4j.LoggerFactory; @Beta @ThreadSafe public class GenericListReader, K extends Identifier, B extends Builder> - extends AbstractGenericReader implements ListReader { + extends GenericReader implements ListReader { private static final Logger LOG = LoggerFactory.getLogger(GenericListReader.class); - protected final ListReaderCustomizer customizer; - /** * Create new {@link GenericListReader} * @@ -62,8 +59,7 @@ public class GenericListReader, K extends */ public GenericListReader(@Nonnull final InstanceIdentifier managedDataObjectType, @Nonnull final ListReaderCustomizer customizer) { - super(managedDataObjectType); - this.customizer = customizer; + super(managedDataObjectType, customizer); } @Override @@ -92,31 +88,13 @@ public class GenericListReader, K extends public List getAllIds(@Nonnull final InstanceIdentifier id, @Nonnull final ReadContext ctx) throws ReadFailedException { LOG.trace("{}: Getting all list ids", this); - final List allIds = customizer.getAllIds(id, ctx); + final List allIds = ((ListReaderCustomizer) customizer).getAllIds(id, ctx); LOG.debug("{}: All list ids: {}", this, allIds); return allIds; } @Override public void merge(@Nonnull final Builder builder, @Nonnull final List readData) { - customizer.merge(builder, readData); - } - - @Override - public void readCurrentAttributes(@Nonnull final InstanceIdentifier id, @Nonnull final B builder, - @Nonnull final ReadContext ctx) - throws ReadFailedException { - try { - customizer.readCurrentAttributes(id, builder, ctx); - } catch (RuntimeException e) { - throw new ReadFailedException(id, e); - } + ((ListReaderCustomizer) customizer).merge(builder, readData); } - - @Nonnull - @Override - public B getBuilder(@Nonnull final InstanceIdentifier id) { - return customizer.getBuilder(id); - } - } diff --git a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericReader.java b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericReader.java index e76b6e9a3..2bf439f82 100644 --- a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericReader.java +++ b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericReader.java @@ -22,6 +22,7 @@ import io.fd.honeycomb.translate.read.ReadFailedException; import io.fd.honeycomb.translate.read.Reader; import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; import io.fd.honeycomb.translate.util.read.AbstractGenericReader; +import io.fd.honeycomb.translate.util.read.ReflexiveReaderCustomizer; import javax.annotation.Nonnull; import javax.annotation.concurrent.ThreadSafe; import org.opendaylight.yangtools.concepts.Builder; @@ -73,4 +74,14 @@ public class GenericReader> customizer.merge(parentBuilder, readValue); } + @Override + public boolean isPresent(final InstanceIdentifier id, final C built, final ReadContext ctx) + throws ReadFailedException { + return customizer.isPresent(id, built, ctx); + } + + public static > Reader createReflexive( + final InstanceIdentifier id, Class builderClass) { + return new GenericReader<>(id, new ReflexiveReaderCustomizer<>(id.getTargetType(), builderClass)); + } } diff --git a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReader.java b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReader.java new file mode 100644 index 000000000..ec82b3f7f --- /dev/null +++ b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReader.java @@ -0,0 +1,229 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.impl.read.registry; + +import static com.google.common.base.Preconditions.checkArgument; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Optional; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Iterables; +import io.fd.honeycomb.translate.read.InitFailedException; +import io.fd.honeycomb.translate.read.InitListReader; +import io.fd.honeycomb.translate.read.Initializer; +import io.fd.honeycomb.translate.read.ListReader; +import io.fd.honeycomb.translate.read.ReadContext; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.read.Reader; +import io.fd.honeycomb.translate.util.RWUtils; +import io.fd.honeycomb.translate.util.read.AbstractGenericReader; +import io.fd.honeycomb.translate.util.read.DelegatingReader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import javax.annotation.Nonnull; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +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.Identifier; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +class CompositeReader> + extends AbstractGenericReader + implements Initializer, DelegatingReader { + + private static final Logger LOG = LoggerFactory.getLogger(CompositeReader.class); + + private final Reader delegate; + private final ImmutableMap, Reader>> childReaders; + + private CompositeReader(final Reader reader, + final ImmutableMap, Reader>> childReaders) { + super(reader.getManagedDataObjectType()); + this.delegate = reader; + this.childReaders = childReaders; + } + + @VisibleForTesting + ImmutableMap, Reader>> getChildReaders() { + return childReaders; + } + + @SuppressWarnings("unchecked") + public static InstanceIdentifier appendTypeToId( + final InstanceIdentifier parentId, final InstanceIdentifier type) { + final InstanceIdentifier.PathArgument t = new InstanceIdentifier.Item<>(type.getTargetType()); + return (InstanceIdentifier) InstanceIdentifier.create(Iterables.concat( + parentId.getPathArguments(), Collections.singleton(t))); + } + + @Nonnull + @Override + public Optional read(@Nonnull final InstanceIdentifier id, + @Nonnull final ReadContext ctx) throws ReadFailedException { + if (shouldReadCurrent(id)) { + LOG.trace("{}: Reading current: {}", this, id); + return readCurrent((InstanceIdentifier) id, ctx); + } else if (shouldDelegateToChild(id)) { + LOG.trace("{}: Reading child: {}", this, id); + return readSubtree(id, ctx); + } else { + // Fallback + LOG.trace("{}: Delegating read: {}", this, id); + return delegate.read(id, ctx); + } + } + + private boolean shouldReadCurrent(@Nonnull final InstanceIdentifier id) { + return id.getTargetType().equals(getManagedDataObjectType().getTargetType()); + } + + private boolean shouldDelegateToChild(@Nonnull final InstanceIdentifier id) { + return childReaders.containsKey(RWUtils.getNextId(id, getManagedDataObjectType()).getType()); + } + + private Optional readSubtree(final InstanceIdentifier id, + final ReadContext ctx) throws ReadFailedException { + final InstanceIdentifier.PathArgument nextId = RWUtils.getNextId(id, getManagedDataObjectType()); + final Reader> nextReader = childReaders.get(nextId.getType()); + checkArgument(nextReader != null, "Unable to read: %s. No delegate present, available readers at next level: %s", + id, childReaders.keySet()); + return nextReader.read(id, ctx); + } + + @SuppressWarnings("unchecked") + private void readChildren(final InstanceIdentifier id, @Nonnull final ReadContext ctx, final B builder) + throws ReadFailedException { + LOG.debug("{}: Reading children: {}", this, childReaders.keySet()); + for (Reader child : childReaders.values()) { + final InstanceIdentifier childId = appendTypeToId(id, child.getManagedDataObjectType()); + + LOG.debug("{}: Reading child from: {}", this, child); + if (child instanceof ListReader) { + final List list = ((ListReader) child).readList(childId, ctx); + // Dont set empty lists + if (!list.isEmpty()) { + ((ListReader) child).merge(builder, list); + } + } else { + final Optional read = child.read(childId, ctx); + if (read.isPresent()) { + child.merge(builder, read.get()); + } + } + } + } + + @Override + public Reader getDelegate() { + return delegate; + } + + @Override + public void readCurrentAttributes(@Nonnull final InstanceIdentifier id, @Nonnull final B builder, + @Nonnull final ReadContext ctx) + throws ReadFailedException { + delegate.readCurrentAttributes(id, builder, ctx); + readChildren(id, ctx, builder); + } + + /** + * Wrap a Reader as a Composite Reader. + */ + static > Reader createForReader( + @Nonnull final Reader reader, + @Nonnull final ImmutableMap, Reader>> childReaders) { + + return (reader instanceof ListReader) + ? new CompositeListReader<>((ListReader) reader, childReaders) + : new CompositeReader<>(reader, childReaders); + } + + @SuppressWarnings("unchecked") + @Override + public void init(final DataBroker broker, final InstanceIdentifier id, final ReadContext ctx) throws InitFailedException { + if (delegate instanceof Initializer) { + LOG.trace("{}: Initializing current: {}", this, id); + ((Initializer) delegate).init(broker, id, ctx); + } + + for (Reader child : childReaders.values()) { + final InstanceIdentifier childId = appendTypeToId(id, child.getManagedDataObjectType()); + + if (child instanceof Initializer) { + LOG.trace("{}: Initializing child: {}", this, childId); + ((Initializer) child).init(broker, childId, ctx); + } + } + } + + private static class CompositeListReader, B extends Builder, K extends Identifier> + extends CompositeReader + implements DelegatingListReader, InitListReader { + + private final ListReader delegate; + + private CompositeListReader(final ListReader reader, + final ImmutableMap, Reader>> childReaders) { + super(reader, childReaders); + this.delegate = reader; + } + + public ListReader getDelegate() { + return delegate; + } + + @Nonnull + @Override + public List readList(@Nonnull final InstanceIdentifier id, @Nonnull final ReadContext ctx) + throws ReadFailedException { + LOG.trace("{}: Reading all list entries", this); + final List allIds = delegate.getAllIds(id, ctx); + LOG.debug("{}: Reading list entries for: {}", this, allIds); + + // Override read list in order to perform readCurrent + readChildren here + final ArrayList allEntries = new ArrayList<>(allIds.size()); + for (K key : allIds) { + final InstanceIdentifier.IdentifiableItem currentBdItem = RWUtils.getCurrentIdItem(id, key); + final InstanceIdentifier keyedId = RWUtils.replaceLastInId(id, currentBdItem); + final Optional read = readCurrent(keyedId, ctx); + if (read.isPresent()) { + final DataObject singleItem = read.get(); + checkArgument(getManagedDataObjectType().getTargetType().isAssignableFrom(singleItem.getClass())); + allEntries.add(getManagedDataObjectType().getTargetType().cast(singleItem)); + } + } + return allEntries; + } + + @Override + public void init(final DataBroker broker, final InstanceIdentifier id, final ReadContext ctx) + throws InitFailedException { + try { + final List allIds = delegate.getAllIds(id, ctx); + for (K key : allIds) { + super.init(broker, RWUtils.replaceLastInId(id, RWUtils.getCurrentIdItem(id, key)), ctx); + } + } catch (ReadFailedException e) { + throw new InitFailedException(id, e); + } + } + } +} diff --git a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReaderRegistry.java b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReaderRegistry.java new file mode 100644 index 000000000..d69e53765 --- /dev/null +++ b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReaderRegistry.java @@ -0,0 +1,131 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.impl.read.registry; + +import static com.google.common.base.Preconditions.checkNotNull; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Optional; +import com.google.common.collect.Iterables; +import com.google.common.collect.LinkedListMultimap; +import com.google.common.collect.Multimap; +import io.fd.honeycomb.translate.read.InitFailedException; +import io.fd.honeycomb.translate.read.Initializer; +import io.fd.honeycomb.translate.read.ListReader; +import io.fd.honeycomb.translate.read.ReadContext; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.read.Reader; +import io.fd.honeycomb.translate.read.registry.ReaderRegistry; +import io.fd.honeycomb.translate.util.RWUtils; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import javax.annotation.Nonnull; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.yangtools.concepts.Builder; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Simple reader registry able to perform and aggregated read (ROOT read) on top of all provided readers. Also able to + * delegate a specific read to one of the delegate readers. + *

+ * This could serve as a utility to hold & hide all available readers in upper layers. + */ +public final class CompositeReaderRegistry implements ReaderRegistry { + + private static final Logger LOG = LoggerFactory.getLogger(CompositeReaderRegistry.class); + + private final Map, Reader>> rootReaders; + + /** + * Create new {@link CompositeReaderRegistry}. + * + * @param rootReaders List of delegate readers + */ + public CompositeReaderRegistry(@Nonnull final List>> rootReaders) { + this.rootReaders = RWUtils.uniqueLinkedIndex(checkNotNull(rootReaders), RWUtils.MANAGER_CLASS_FUNCTION); + } + + @VisibleForTesting + Map, Reader>> getRootReaders() { + return rootReaders; + } + + @Override + @Nonnull + public Multimap, ? extends DataObject> readAll( + @Nonnull final ReadContext ctx) throws ReadFailedException { + + LOG.debug("Reading from all delegates: {}", this); + LOG.trace("Reading from all delegates: {}", rootReaders.values()); + + final Multimap, DataObject> objects = LinkedListMultimap.create(); + for (Reader> rootReader : rootReaders.values()) { + LOG.debug("Reading from delegate: {}", rootReader); + + if (rootReader instanceof ListReader) { + final List listEntries = + ((ListReader) rootReader).readList(rootReader.getManagedDataObjectType(), ctx); + if (!listEntries.isEmpty()) { + objects.putAll(rootReader.getManagedDataObjectType(), listEntries); + } + } else { + final Optional read = rootReader.read(rootReader.getManagedDataObjectType(), ctx); + if (read.isPresent()) { + objects.putAll(rootReader.getManagedDataObjectType(), Collections.singletonList(read.get())); + } + } + } + + return objects; + } + + + @Override + public void initAll(@Nonnull final DataBroker broker, @Nonnull final ReadContext ctx) throws InitFailedException { + for (Reader> rootReader : rootReaders.values()) { + if (rootReader instanceof Initializer) { + ((Initializer) rootReader).init(broker, rootReader.getManagedDataObjectType(), ctx); + } + } + } + + @Nonnull + @Override + public Optional read(@Nonnull final InstanceIdentifier id, + @Nonnull final ReadContext ctx) + throws ReadFailedException { + final InstanceIdentifier.PathArgument first = checkNotNull( + Iterables.getFirst(id.getPathArguments(), null), "Empty id"); + final Reader> reader = rootReaders.get(first.getType()); + checkNotNull(reader, + "Unable to read %s. Missing reader. Current readers for: %s", id, rootReaders.keySet()); + LOG.debug("Reading from delegate: {}", reader); + return reader.read(id, ctx); + } + + @Override + public String toString() { + return getClass().getSimpleName() + + rootReaders.keySet().stream().map(Class::getSimpleName).collect(Collectors.toList()); + } + +} 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 new file mode 100644 index 000000000..290c5d4aa --- /dev/null +++ b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReaderRegistryBuilder.java @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.impl.read.registry; + +import com.google.common.collect.ImmutableMap; +import io.fd.honeycomb.translate.impl.read.GenericReader; +import io.fd.honeycomb.translate.read.InitReader; +import io.fd.honeycomb.translate.read.Reader; +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.InstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@NotThreadSafe +public final class CompositeReaderRegistryBuilder + extends AbstractSubtreeManagerRegistryBuilderBuilder>, ReaderRegistry> + implements ModifiableReaderRegistryBuilder, ReaderRegistryBuilder { + + private static final Logger LOG = LoggerFactory.getLogger(CompositeReaderRegistryBuilder.class); + + @Override + protected Reader> getSubtreeHandler(@Nonnull final Set> handledChildren, + @Nonnull final Reader> reader) { + return reader instanceof InitReader + ? InitSubtreeReader.createForReader(handledChildren, reader) + : SubtreeReader.createForReader(handledChildren, reader); + } + + @Override + public void addStructuralReader(@Nonnull InstanceIdentifier id, + @Nonnull Class> builderType) { + add(GenericReader.createReflexive(id, builderType)); + } + + /** + * Create {@link CompositeReaderRegistry} with Readers ordered according to submitted relationships. + *

+ * Note: The ordering only applies between nodes on the same level, inter-level and inter-subtree relationships are + * ignored. + */ + @Override + public ReaderRegistry build() { + ImmutableMap, Reader>> mappedReaders = + getMappedHandlers(); + LOG.debug("Building Reader registry with Readers: {}", + mappedReaders.keySet().stream() + .map(InstanceIdentifier::getTargetType) + .map(Class::getSimpleName) + .collect(Collectors.joining(", "))); + + LOG.trace("Building Reader registry with Readers: {}", mappedReaders); + final List> readerOrder = new ArrayList<>(mappedReaders.keySet()); + + // Wrap readers into composite readers recursively, collect roots and create registry + final TypeHierarchy typeHierarchy = TypeHierarchy.create(mappedReaders.keySet()); + final List>> orderedRootReaders = + typeHierarchy.getRoots().stream() + .map(rootId -> toCompositeReader(rootId, mappedReaders, typeHierarchy)) + .collect(Collectors.toList()); + + // We are violating the ordering from mappedReaders, since we are forming a composite structure + // but at least order root writers + orderedRootReaders.sort((reader1, reader2) -> readerOrder.indexOf(reader1.getManagedDataObjectType()) + - readerOrder.indexOf(reader2.getManagedDataObjectType())); + + return new CompositeReaderRegistry(orderedRootReaders); + } + + private Reader> toCompositeReader( + final InstanceIdentifier instanceIdentifier, + final ImmutableMap, Reader>> mappedReaders, + final TypeHierarchy typeHierarchy) { + + // Order child readers according to the mappedReadersCollection + final ImmutableMap.Builder, Reader>> childReadersMapB = ImmutableMap.builder(); + for (InstanceIdentifier childId : mappedReaders.keySet()) { + if (typeHierarchy.getDirectChildren(instanceIdentifier).contains(childId)) { + childReadersMapB.put(childId.getTargetType(), toCompositeReader(childId, mappedReaders, typeHierarchy)); + } + } + + final ImmutableMap, Reader>> childReadersMap = childReadersMapB.build(); + return childReadersMap.isEmpty() + ? mappedReaders.get(instanceIdentifier) + : CompositeReader.createForReader(mappedReaders.get(instanceIdentifier), childReadersMap); + } +} diff --git a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/InitSubtreeReader.java b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/InitSubtreeReader.java new file mode 100644 index 000000000..809cdb214 --- /dev/null +++ b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/InitSubtreeReader.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.impl.read.registry; + +import io.fd.honeycomb.translate.read.InitFailedException; +import io.fd.honeycomb.translate.read.InitListReader; +import io.fd.honeycomb.translate.read.InitReader; +import io.fd.honeycomb.translate.read.ListReader; +import io.fd.honeycomb.translate.read.ReadContext; +import io.fd.honeycomb.translate.read.Reader; +import java.util.Set; +import javax.annotation.Nonnull; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +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.Identifier; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +class InitSubtreeReader> + extends SubtreeReader + implements InitReader { + + private InitSubtreeReader(final InitReader delegate, + final Set> handledTypes) { + super(delegate, handledTypes); + } + + @Override + public void init(final DataBroker broker, final InstanceIdentifier id, final ReadContext ctx) throws InitFailedException { + ((InitReader) delegate).init(broker, id, ctx); + } + + /** + * Wrap a Reader as an initializing subtree Reader. + */ + static > Reader createForReader(@Nonnull final Set> handledChildren, + @Nonnull final Reader reader) { + return (reader instanceof ListReader) + ? new InitSubtreeListReader<>((InitListReader) reader, handledChildren) + : new InitSubtreeReader<>(((InitReader) reader), handledChildren); + } + + private static class InitSubtreeListReader, B extends Builder, K extends Identifier> + extends SubtreeListReader + implements InitListReader { + + InitSubtreeListReader(final InitListReader delegate, + final Set> handledTypes) { + super(delegate, handledTypes); + } + + @Override + public void init(final DataBroker broker, final InstanceIdentifier id, final ReadContext ctx) throws InitFailedException { + ((InitListReader) delegate).init(broker, id, ctx); + } + } +} diff --git a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/SubtreeReader.java b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/SubtreeReader.java new file mode 100644 index 000000000..4db6dd70e --- /dev/null +++ b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/SubtreeReader.java @@ -0,0 +1,216 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.impl.read.registry; + +import static com.google.common.base.Preconditions.checkArgument; + +import com.google.common.base.Optional; +import com.google.common.collect.Iterables; +import io.fd.honeycomb.translate.read.ListReader; +import io.fd.honeycomb.translate.read.ReadContext; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.read.Reader; +import io.fd.honeycomb.translate.util.RWUtils; +import io.fd.honeycomb.translate.util.ReflectionUtils; +import io.fd.honeycomb.translate.util.read.DelegatingReader; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import javax.annotation.Nonnull; +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.Identifier; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Simple Reader delegate for subtree Readers (Readers handling also children nodes) providing a list of all the + * children nodes being handled. + */ +class SubtreeReader> implements DelegatingReader { + + private static final Logger LOG = LoggerFactory.getLogger(SubtreeReader.class); + + protected final Reader delegate; + private final Set> handledChildTypes = new HashSet<>(); + + SubtreeReader(final Reader delegate, Set> handledTypes) { + this.delegate = delegate; + for (InstanceIdentifier handledType : handledTypes) { + // Iid has to start with Reader's handled root type + checkArgument(delegate.getManagedDataObjectType().getTargetType().equals( + handledType.getPathArguments().iterator().next().getType()), + "Handled node from subtree has to be identified by an instance identifier starting from: %s." + + "Instance identifier was: %s", getManagedDataObjectType().getTargetType(), handledType); + checkArgument(Iterables.size(handledType.getPathArguments()) > 1, + "Handled node from subtree identifier too short: %s", handledType); + handledChildTypes.add(InstanceIdentifier.create(Iterables.concat( + getManagedDataObjectType().getPathArguments(), Iterables.skip(handledType.getPathArguments(), 1)))); + } + } + + /** + * Return set of types also handled by this Reader. All of the types are children of the type managed by this Reader + * excluding the type of this Reader. + */ + Set> getHandledChildTypes() { + return handledChildTypes; + } + + @Override + @Nonnull + public Optional read( + @Nonnull final InstanceIdentifier id, + @Nonnull final ReadContext ctx) throws ReadFailedException { + final InstanceIdentifier wildcarded = RWUtils.makeIidWildcarded(id); + + // Reading entire subtree and filtering if is current reader responsible + if (getHandledChildTypes().contains(wildcarded)) { + LOG.debug("{}: Subtree node managed by this writer requested: {}. Reading current and filtering", this, id); + // If there's no dedicated reader, use read current + final InstanceIdentifier currentId = RWUtils.cutId(id, getManagedDataObjectType()); + final Optional current = delegate.read(currentId, ctx); + // then perform post-reading filtering (return only requested sub-node) + final Optional readSubtree = current.isPresent() + ? filterSubtree(current.get(), id, getManagedDataObjectType().getTargetType()) + : current; + + LOG.debug("{}: Subtree: {} read successfully. Result: {}", this, id, readSubtree); + return readSubtree; + + // If child that's handled here is not requested, then delegate should be able to handle the read + } else { + return delegate.read(id, ctx); + } + } + + @Override + public Reader getDelegate() { + return delegate; + } + + @Nonnull + private static Optional filterSubtree(@Nonnull final DataObject parent, + @Nonnull final InstanceIdentifier absolutPath, + @Nonnull final Class managedType) { + final InstanceIdentifier.PathArgument nextId = + RWUtils.getNextId(absolutPath, InstanceIdentifier.create(parent.getClass())); + + final Optional nextParent = findNextParent(parent, nextId, managedType); + + if (Iterables.getLast(absolutPath.getPathArguments()).equals(nextId)) { + return nextParent; // we found the dataObject identified by absolutePath + } else if (nextParent.isPresent()) { + return filterSubtree(nextParent.get(), absolutPath, nextId.getType()); + } else { + return nextParent; // we can't go further, return Optional.absent() + } + } + + private static Optional findNextParent(@Nonnull final DataObject parent, + @Nonnull final InstanceIdentifier.PathArgument nextId, + @Nonnull final Class managedType) { + Optional method = ReflectionUtils.findMethodReflex(managedType, "get", + Collections.emptyList(), nextId.getType()); + + if (method.isPresent()) { + return Optional.fromNullable(filterSingle(parent, nextId, method.get())); + } else { + // List child nodes + method = ReflectionUtils.findMethodReflex(managedType, + "get" + nextId.getType().getSimpleName(), Collections.emptyList(), List.class); + + if (method.isPresent()) { + return filterList(parent, nextId, method.get()); + } else { + throw new IllegalStateException( + "Unable to filter " + nextId + " from " + parent + " getters not found using reflexion"); + } + } + } + + @SuppressWarnings("unchecked") + private static Optional filterList(final DataObject parent, + final InstanceIdentifier.PathArgument nextId, + final Method method) { + final List invoke = (List) invoke(method, nextId, parent); + + checkArgument(nextId instanceof InstanceIdentifier.IdentifiableItem, + "Unable to perform wildcarded read for %s", nextId); + final Identifier key = ((InstanceIdentifier.IdentifiableItem) nextId).getKey(); + + final Method keyGetter = ReflectionUtils.findMethodReflex(nextId.getType(), "get", + Collections.emptyList(), key.getClass()).get(); + + return Optional.fromNullable(invoke.stream() + .filter(item -> key.equals(invoke(keyGetter, nextId, item))) + .findFirst().orElse(null)); + } + + private static DataObject filterSingle(final DataObject parent, + final InstanceIdentifier.PathArgument nextId, final Method method) { + return nextId.getType().cast(invoke(method, nextId, parent)); + } + + private static Object invoke(final Method method, + final InstanceIdentifier.PathArgument nextId, final DataObject parent) { + try { + return method.invoke(parent); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new IllegalArgumentException("Unable to get " + nextId + " from " + parent, e); + } + } + + @Override + @Nonnull + public InstanceIdentifier getManagedDataObjectType() { + return delegate.getManagedDataObjectType(); + } + + /** + * Wrap a Reader as a subtree Reader. + */ + static > Reader createForReader(@Nonnull final Set> handledChildren, + @Nonnull final Reader reader) { + return (reader instanceof ListReader) + ? new SubtreeListReader<>((ListReader) reader, handledChildren) + : new SubtreeReader<>(reader, handledChildren); + } + + static class SubtreeListReader, B extends Builder, K extends Identifier> + extends SubtreeReader implements DelegatingListReader, ListReader { + + final ListReader delegate; + + SubtreeListReader(final ListReader delegate, + final Set> handledTypes) { + super(delegate, handledTypes); + this.delegate = delegate; + } + + @Override + public ListReader getDelegate() { + return delegate; + } + } + +} diff --git a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/TypeHierarchy.java b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/TypeHierarchy.java new file mode 100644 index 000000000..14fc57632 --- /dev/null +++ b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/TypeHierarchy.java @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.impl.read.registry; + +import static com.google.common.base.Preconditions.checkArgument; + +import com.google.common.collect.Iterables; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; +import javax.annotation.Nonnull; +import org.jgrapht.experimental.dag.DirectedAcyclicGraph; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +final class TypeHierarchy { + private final DirectedAcyclicGraph, Parent> hierarchy; + + private TypeHierarchy(@Nonnull final DirectedAcyclicGraph, Parent> hierarchy) { + this.hierarchy = hierarchy; + } + + Set> getAllChildren(InstanceIdentifier id) { + final HashSet> instanceIdentifiers = new HashSet<>(); + for (InstanceIdentifier childId : getDirectChildren(id)) { + instanceIdentifiers.add(childId); + instanceIdentifiers.addAll(getAllChildren(childId)); + } + return instanceIdentifiers; + } + + Set> getDirectChildren(InstanceIdentifier id) { + checkArgument(hierarchy.vertexSet().contains(id), + "Unknown reader: %s. Known readers: %s", id, hierarchy.vertexSet()); + + return hierarchy.outgoingEdgesOf(id).stream() + .map(hierarchy::getEdgeTarget) + .collect(Collectors.toSet()); + } + + Set> getRoots() { + return hierarchy.vertexSet().stream() + .filter(vertex -> hierarchy.incomingEdgesOf(vertex).size() == 0) + .collect(Collectors.toSet()); + } + + /** + * Create reader hierarchy from a flat set of instance identifiers. + * + * @param allIds Set of unkeyed instance identifiers + */ + static TypeHierarchy create(@Nonnull Set> allIds) { + final DirectedAcyclicGraph, Parent> + readersHierarchy = new DirectedAcyclicGraph<>((sourceVertex, targetVertex) -> new Parent()); + + for (InstanceIdentifier allId : allIds) { + checkArgument(!Iterables.isEmpty(allId.getPathArguments()), "Empty ID detected"); + + if (Iterables.size(allId.getPathArguments()) == 1) { + readersHierarchy.addVertex(allId); + } + + List pathArgs = new LinkedList<>(); + pathArgs.add(allId.getPathArguments().iterator().next()); + + for (InstanceIdentifier.PathArgument pathArgument : Iterables.skip(allId.getPathArguments(), 1)) { + final InstanceIdentifier previous = InstanceIdentifier.create(pathArgs); + pathArgs.add(pathArgument); + final InstanceIdentifier current = InstanceIdentifier.create(pathArgs); + + readersHierarchy.addVertex(previous); + readersHierarchy.addVertex(current); + + try { + readersHierarchy.addDagEdge(previous, current); + } catch (DirectedAcyclicGraph.CycleFoundException e) { + throw new IllegalArgumentException("Loop in hierarchy detected", e); + } + } + } + + return new TypeHierarchy(readersHierarchy); + } + + private static final class Parent{} +} diff --git a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistry.java b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistry.java new file mode 100644 index 000000000..990431a15 --- /dev/null +++ b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistry.java @@ -0,0 +1,314 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.impl.write.registry; + +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; +import static io.fd.honeycomb.translate.util.RWUtils.makeIidWildcarded; + +import com.google.common.base.Optional; +import com.google.common.collect.HashMultimap; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; +import com.google.common.collect.Multimap; +import com.google.common.collect.Sets; +import io.fd.honeycomb.translate.TranslationException; +import io.fd.honeycomb.translate.util.RWUtils; +import io.fd.honeycomb.translate.write.DataObjectUpdate; +import io.fd.honeycomb.translate.write.WriteContext; +import io.fd.honeycomb.translate.write.WriteFailedException; +import io.fd.honeycomb.translate.write.Writer; +import io.fd.honeycomb.translate.write.registry.WriterRegistry; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import javax.annotation.concurrent.ThreadSafe; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Flat writer registry, delegating updates to writers in the order writers were submitted. + */ +@ThreadSafe +final class FlatWriterRegistry implements WriterRegistry { + + private static final Logger LOG = LoggerFactory.getLogger(FlatWriterRegistry.class); + + // All types handled by writers directly or as children + private final ImmutableSet> handledTypes; + + private final Set> writersOrderReversed; + private final Set> writersOrder; + private final Map, Writer> writers; + + /** + * Create flat registry instance. + * + * @param writers immutable, ordered map of writers to use to process updates. Order of the writers has to be + * one in which create and update operations should be handled. Deletes will be handled in reversed + * order. All deletes are handled before handling all the updates. + */ + FlatWriterRegistry(@Nonnull final ImmutableMap, Writer> writers) { + this.writers = writers; + this.writersOrderReversed = Sets.newLinkedHashSet(Lists.reverse(Lists.newArrayList(writers.keySet()))); + this.writersOrder = writers.keySet(); + this.handledTypes = getAllHandledTypes(writers); + } + + private static ImmutableSet> getAllHandledTypes( + @Nonnull final ImmutableMap, Writer> writers) { + final ImmutableSet.Builder> handledTypesBuilder = ImmutableSet.builder(); + for (Map.Entry, Writer> writerEntry : writers.entrySet()) { + final InstanceIdentifier writerType = writerEntry.getKey(); + final Writer writer = writerEntry.getValue(); + handledTypesBuilder.add(writerType); + if (writer instanceof SubtreeWriter) { + handledTypesBuilder.addAll(((SubtreeWriter) writer).getHandledChildTypes()); + } + } + return handledTypesBuilder.build(); + } + + @Override + public void update(@Nonnull final DataObjectUpdates updates, + @Nonnull final WriteContext ctx) throws TranslationException { + if (updates.isEmpty()) { + return; + } + + // Optimization + if (updates.containsOnlySingleType()) { + // First process delete + singleUpdate(updates.getDeletes(), ctx); + // Next is update + singleUpdate(updates.getUpdates(), ctx); + } else { + // First process deletes + bulkUpdate(updates.getDeletes(), ctx, true, writersOrderReversed); + // Next are updates + bulkUpdate(updates.getUpdates(), ctx, true, writersOrder); + } + + LOG.debug("Update successful for types: {}", updates.getTypeIntersection()); + LOG.trace("Update successful for: {}", updates); + } + + private void singleUpdate(@Nonnull final Multimap, ? extends DataObjectUpdate> updates, + @Nonnull final WriteContext ctx) throws WriteFailedException { + if (updates.isEmpty()) { + return; + } + + final InstanceIdentifier singleType = updates.keySet().iterator().next(); + LOG.debug("Performing single type update for: {}", singleType); + Collection singleTypeUpdates = updates.get(singleType); + Writer writer = getWriter(singleType); + + if (writer == null) { + // This node must be handled by a subtree writer, find it and call it or else fail + checkArgument(handledTypes.contains(singleType), "Unable to process update. Missing writers for: %s", + singleType); + writer = getSubtreeWriterResponsible(singleType); + singleTypeUpdates = getParentDataObjectUpdate(ctx, updates, writer); + } + + LOG.trace("Performing single type update with writer: {}", writer); + for (DataObjectUpdate singleUpdate : singleTypeUpdates) { + writer.update(singleUpdate.getId(), singleUpdate.getDataBefore(), singleUpdate.getDataAfter(), ctx); + } + } + + private Writer getSubtreeWriterResponsible(final InstanceIdentifier singleType) { + return writers.values().stream() + .filter(w -> w instanceof SubtreeWriter) + .filter(w -> ((SubtreeWriter) w).getHandledChildTypes().contains(singleType)) + .findFirst() + .get(); + } + + private Collection getParentDataObjectUpdate(final WriteContext ctx, + final Multimap, ? extends DataObjectUpdate> updates, + final Writer writer) { + // Now read data for subtree reader root, but first keyed ID is needed and that ID can be cut from updates + InstanceIdentifier firstAffectedChildId = ((SubtreeWriter) writer).getHandledChildTypes().stream() + .filter(updates::containsKey) + .map(unkeyedId -> updates.get(unkeyedId)) + .flatMap(doUpdates -> doUpdates.stream()) + .map(DataObjectUpdate::getId) + .findFirst() + .get(); + + final InstanceIdentifier parentKeyedId = + RWUtils.cutId(firstAffectedChildId, writer.getManagedDataObjectType()); + + final Optional parentBefore = ctx.readBefore(parentKeyedId); + final Optional parentAfter = ctx.readAfter(parentKeyedId); + return Collections.singleton( + DataObjectUpdate.create(parentKeyedId, parentBefore.orNull(), parentAfter.orNull())); + } + + private void bulkUpdate(@Nonnull final Multimap, ? extends DataObjectUpdate> updates, + @Nonnull final WriteContext ctx, + final boolean attemptRevert, + @Nonnull final Set> writersOrder) throws BulkUpdateException { + if (updates.isEmpty()) { + return; + } + + LOG.debug("Performing bulk update with revert attempt: {} for: {}", attemptRevert, updates.keySet()); + + // Check that all updates can be handled + checkAllTypesCanBeHandled(updates); + + // Capture all changes successfully processed in case revert is needed + final Set> processedNodes = new HashSet<>(); + + // Iterate over all writers and call update if there are any related updates + for (InstanceIdentifier writerType : writersOrder) { + Collection writersData = updates.get(writerType); + final Writer writer = getWriter(writerType); + + if (writersData.isEmpty()) { + // If there are no data for current writer, but it is a SubtreeWriter and there are updates to + // its children, still invoke it with its root data + if (writer instanceof SubtreeWriter && isAffected(((SubtreeWriter) writer), updates)) { + // Provide parent data for SubtreeWriter for further processing + writersData = getParentDataObjectUpdate(ctx, updates, writer); + } else { + // Skipping unaffected writer + // Alternative to this would be modification sort according to the order of writers + continue; + } + } + + LOG.debug("Performing update for: {}", writerType); + LOG.trace("Performing update with writer: {}", writer); + + for (DataObjectUpdate singleUpdate : writersData) { + try { + writer.update(singleUpdate.getId(), singleUpdate.getDataBefore(), singleUpdate.getDataAfter(), ctx); + processedNodes.add(singleUpdate.getId()); + LOG.trace("Update successful for type: {}", writerType); + LOG.debug("Update successful for: {}", singleUpdate); + } catch (Exception e) { + // do not log this exception here,its logged in ModifiableDataTreeDelegator + + final Reverter reverter = attemptRevert + ? new ReverterImpl(processedNodes, updates, writersOrder) + : (final WriteContext writeContext) -> {};//NOOP reverter + + // Find out which changes left unprocessed + final Set> unprocessedChanges = updates.values().stream() + .map(DataObjectUpdate::getId) + .filter(id -> !processedNodes.contains(id)) + .collect(Collectors.toSet()); + throw new BulkUpdateException(unprocessedChanges, reverter, e); + } + } + } + } + + private void checkAllTypesCanBeHandled( + @Nonnull final Multimap, ? extends DataObjectUpdate> updates) { + if (!handledTypes.containsAll(updates.keySet())) { + final Sets.SetView> missingWriters = Sets.difference(updates.keySet(), handledTypes); + LOG.warn("Unable to process update. Missing writers for: {}", missingWriters); + throw new IllegalArgumentException("Unable to process update. Missing writers for: " + missingWriters); + } + } + + /** + * Check whether {@link SubtreeWriter} is affected by the updates. + * + * @return true if there are any updates to SubtreeWriter's child nodes (those marked by SubtreeWriter + * as being taken care of) + * */ + private static boolean isAffected(final SubtreeWriter writer, + final Multimap, ? extends DataObjectUpdate> updates) { + return !Sets.intersection(writer.getHandledChildTypes(), updates.keySet()).isEmpty(); + } + + @Nullable + private Writer getWriter(@Nonnull final InstanceIdentifier singleType) { + return writers.get(singleType); + } + + private final class ReverterImpl implements Reverter { + + private final Collection> processedNodes; + private final Multimap, ? extends DataObjectUpdate> updates; + private final Set> revertDeleteOrder; + + ReverterImpl(final Collection> processedNodes, + final Multimap, ? extends DataObjectUpdate> updates, + final Set> writersOrderOriginal) { + this.processedNodes = processedNodes; + this.updates = updates; + // Use opposite ordering when executing revert + this.revertDeleteOrder = writersOrderOriginal == FlatWriterRegistry.this.writersOrder + ? FlatWriterRegistry.this.writersOrderReversed + : FlatWriterRegistry.this.writersOrder; + } + + @Override + public void revert(@Nonnull final WriteContext writeContext) throws RevertFailedException { + checkNotNull(writeContext, "Cannot revert changes for null context"); + + Multimap, DataObjectUpdate> updatesToRevert = + filterAndRevertProcessed(updates, processedNodes); + + LOG.info("Attempting revert for changes: {}", updatesToRevert); + try { + // Perform reversed bulk update without revert attempt + bulkUpdate(updatesToRevert, writeContext, true, revertDeleteOrder); + LOG.info("Revert successful"); + } catch (BulkUpdateException e) { + LOG.error("Revert failed", e); + throw new RevertFailedException(e.getFailedIds(), e); + } + } + + /** + * Create new updates map, but only keep already processed changes. Switching before and after data for each + * update. + */ + private Multimap, DataObjectUpdate> filterAndRevertProcessed( + final Multimap, ? extends DataObjectUpdate> updates, + final Collection> processedNodes) { + final Multimap, DataObjectUpdate> filtered = HashMultimap.create(); + for (InstanceIdentifier processedNode : processedNodes) { + final InstanceIdentifier wildcardedIid = makeIidWildcarded(processedNode); + if (updates.containsKey(wildcardedIid)) { + updates.get(wildcardedIid).stream() + .filter(dataObjectUpdate -> processedNode.contains(dataObjectUpdate.getId())) + // putting under unkeyed identifier, to prevent failing of checkAllTypesCanBeHandled + .forEach(dataObjectUpdate -> filtered.put(wildcardedIid, dataObjectUpdate.reverse())); + } + } + return filtered; + } + } + +} diff --git a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistryBuilder.java b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistryBuilder.java new file mode 100644 index 000000000..936aa3c1e --- /dev/null +++ b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistryBuilder.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.impl.write.registry; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.ImmutableMap; +import io.fd.honeycomb.translate.write.registry.WriterRegistryBuilder; +import io.fd.honeycomb.translate.util.AbstractSubtreeManagerRegistryBuilderBuilder; +import io.fd.honeycomb.translate.write.Writer; +import io.fd.honeycomb.translate.write.registry.ModifiableWriterRegistryBuilder; +import io.fd.honeycomb.translate.write.registry.WriterRegistry; +import java.util.Set; +import java.util.stream.Collectors; +import javax.annotation.Nonnull; +import javax.annotation.concurrent.NotThreadSafe; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Builder for {@link FlatWriterRegistry} allowing users to specify inter-writer relationships. + */ +@NotThreadSafe +public final class FlatWriterRegistryBuilder + extends AbstractSubtreeManagerRegistryBuilderBuilder, WriterRegistry> + implements ModifiableWriterRegistryBuilder, WriterRegistryBuilder { + + private static final Logger LOG = LoggerFactory.getLogger(FlatWriterRegistryBuilder.class); + + @Override + protected Writer getSubtreeHandler(final @Nonnull Set> handledChildren, + final @Nonnull Writer writer) { + return SubtreeWriter.createForWriter(handledChildren, writer); + } + + /** + * Create FlatWriterRegistry with writers ordered according to submitted relationships. + */ + @Override + public WriterRegistry build() { + final ImmutableMap, Writer> mappedWriters = getMappedHandlers(); + LOG.debug("Building writer registry with writers: {}", + mappedWriters.keySet().stream() + .map(InstanceIdentifier::getTargetType) + .map(Class::getSimpleName) + .collect(Collectors.joining(", "))); + LOG.trace("Building writer registry with writers: {}", mappedWriters); + return new FlatWriterRegistry(mappedWriters); + } + + @VisibleForTesting + @Override + protected ImmutableMap, Writer> getMappedHandlers() { + return super.getMappedHandlers(); + } +} diff --git a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/write/registry/SubtreeWriter.java b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/write/registry/SubtreeWriter.java new file mode 100644 index 000000000..fc6ecc67e --- /dev/null +++ b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/write/registry/SubtreeWriter.java @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.impl.write.registry; + +import static com.google.common.base.Preconditions.checkArgument; + +import com.google.common.collect.Iterables; +import io.fd.honeycomb.translate.write.WriteContext; +import io.fd.honeycomb.translate.write.WriteFailedException; +import io.fd.honeycomb.translate.write.Writer; +import java.util.HashSet; +import java.util.Set; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +/** + * Simple writer delegate for subtree writers (writers handling also children nodes) providing a list of all the + * children nodes being handled. + */ +final class SubtreeWriter implements Writer { + + private final Writer delegate; + private final Set> handledChildTypes = new HashSet<>(); + + private SubtreeWriter(final Writer delegate, Set> handledTypes) { + this.delegate = delegate; + for (InstanceIdentifier handledType : handledTypes) { + // Iid has to start with writer's handled root type + checkArgument(delegate.getManagedDataObjectType().getTargetType().equals( + handledType.getPathArguments().iterator().next().getType()), + "Handled node from subtree has to be identified by an instance identifier starting from: %s." + + "Instance identifier was: %s", getManagedDataObjectType().getTargetType(), handledType); + checkArgument(Iterables.size(handledType.getPathArguments()) > 1, + "Handled node from subtree identifier too short: %s", handledType); + handledChildTypes.add(InstanceIdentifier.create(Iterables.concat( + getManagedDataObjectType().getPathArguments(), Iterables.skip(handledType.getPathArguments(), 1)))); + } + } + + /** + * Return set of types also handled by this writer. All of the types are children of the type managed by this + * writer excluding the type of this writer. + */ + Set> getHandledChildTypes() { + return handledChildTypes; + } + + @Override + public void update( + @Nonnull final InstanceIdentifier id, + @Nullable final DataObject dataBefore, + @Nullable final DataObject dataAfter, @Nonnull final WriteContext ctx) throws WriteFailedException { + delegate.update(id, dataBefore, dataAfter, ctx); + } + + @Override + @Nonnull + public InstanceIdentifier getManagedDataObjectType() { + return delegate.getManagedDataObjectType(); + } + + /** + * Wrap a writer as a subtree writer. + */ + static Writer createForWriter(@Nonnull final Set> handledChildren, + @Nonnull final Writer writer) { + return new SubtreeWriter<>(writer, handledChildren); + } +} diff --git a/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/GenericListReaderTest.java b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/GenericListReaderTest.java index 0dc916fb8..3ed400dba 100644 --- a/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/GenericListReaderTest.java +++ b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/GenericListReaderTest.java @@ -25,7 +25,6 @@ import static org.mockito.Mockito.when; import com.google.common.collect.Lists; import io.fd.honeycomb.translate.read.ReadContext; import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer; -import java.util.Collections; import java.util.List; import org.junit.Before; import org.junit.Test; @@ -78,7 +77,7 @@ public class GenericListReaderTest { @Test public void testMerge() throws Exception { reader.merge(builder, data); - verify(customizer).merge(builder, Collections.singletonList(data)); + verify(customizer).merge(builder, data); } @Test 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 new file mode 100644 index 000000000..4dee8e60f --- /dev/null +++ b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReaderRegistryBuilderTest.java @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.impl.read.registry; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; +import io.fd.honeycomb.translate.read.Reader; +import io.fd.honeycomb.translate.read.registry.ReaderRegistry; +import io.fd.honeycomb.translate.util.DataObjects; +import java.util.List; +import java.util.Map; +import org.junit.Test; +import org.mockito.Mockito; +import org.opendaylight.yangtools.concepts.Builder; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class CompositeReaderRegistryBuilderTest { + + private Reader> reader1 = + mock(DataObjects.DataObject1.class); + private Reader> reader2 = + mock(DataObjects.DataObject2.class); + private Reader> reader3 = + mock(DataObjects.DataObject3.class); + private Reader> reader31 = + mock(DataObjects.DataObject3.DataObject31.class); + + private Reader> reader4 = + mock(DataObjects.DataObject4.class); + private Reader> reader41 = + mock(DataObjects.DataObject4.DataObject41.class); + private Reader> reader411 = + mock(DataObjects.DataObject4.DataObject41.DataObject411.class); + private Reader> reader42 = + mock(DataObjects.DataObject4.DataObject42.class); + + @SuppressWarnings("unchecked") + private Reader> mock(final Class dataObjectType) { + final Reader> mock = Mockito.mock(Reader.class); + try { + when(mock.getManagedDataObjectType()) + .thenReturn(((InstanceIdentifier) dataObjectType.getDeclaredField("IID").get(null))); + } catch (IllegalAccessException | NoSuchFieldException e) { + throw new RuntimeException(e); + } + return mock; + } + + @Test + public void testCompositeStructure() throws Exception { + final CompositeReaderRegistryBuilder compositeReaderRegistryBuilder = new CompositeReaderRegistryBuilder(); + /* + Composite reader structure ordered left from right + + 1, 2, 3, 4 + 31 42, 41 + 411 + */ + compositeReaderRegistryBuilder.add(reader1); + compositeReaderRegistryBuilder.addAfter(reader2, reader1.getManagedDataObjectType()); + compositeReaderRegistryBuilder.addAfter(reader3, reader2.getManagedDataObjectType()); + compositeReaderRegistryBuilder.addAfter(reader31, reader1.getManagedDataObjectType()); + compositeReaderRegistryBuilder.addAfter(reader4, reader3.getManagedDataObjectType()); + compositeReaderRegistryBuilder.add(reader41); + compositeReaderRegistryBuilder.addBefore(reader42, reader41.getManagedDataObjectType()); + compositeReaderRegistryBuilder.add(reader411); + + final ReaderRegistry build = compositeReaderRegistryBuilder.build(); + + final Map, Reader>> rootReaders = + ((CompositeReaderRegistry) build).getRootReaders(); + final List> rootReaderOrder = Lists.newArrayList(rootReaders.keySet()); + + assertEquals(reader1.getManagedDataObjectType().getTargetType(), rootReaderOrder.get(0)); + assertEquals(reader2.getManagedDataObjectType().getTargetType(), rootReaderOrder.get(1)); + assertEquals(reader3.getManagedDataObjectType().getTargetType(), rootReaderOrder.get(2)); + assertEquals(reader4.getManagedDataObjectType().getTargetType(), rootReaderOrder.get(3)); + + assertFalse(rootReaders.get(DataObjects.DataObject1.class) instanceof CompositeReader); + assertFalse(rootReaders.get(DataObjects.DataObject2.class) instanceof CompositeReader); + assertTrue(rootReaders.get(DataObjects.DataObject3.class) instanceof CompositeReader); + assertTrue(rootReaders.get(DataObjects.DataObject4.class) instanceof CompositeReader); + + final ImmutableMap, Reader>> childReaders = + ((CompositeReader>) rootReaders + .get(DataObjects.DataObject4.class)).getChildReaders(); + final List> orderedChildReaders = Lists.newArrayList(childReaders.keySet()); + + assertEquals(reader42.getManagedDataObjectType().getTargetType(), orderedChildReaders.get(0)); + assertEquals(reader41.getManagedDataObjectType().getTargetType(), orderedChildReaders.get(1)); + assertTrue(childReaders.get(DataObjects.DataObject4.DataObject41.class) instanceof CompositeReader); + assertFalse(childReaders.get(DataObjects.DataObject4.DataObject42.class) instanceof CompositeReader); + } +} \ No newline at end of file diff --git a/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReaderRegistryTest.java b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReaderRegistryTest.java new file mode 100644 index 000000000..40068e02c --- /dev/null +++ b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReaderRegistryTest.java @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.impl.read.registry; + +import static io.fd.honeycomb.translate.util.DataObjects.DataObject3; +import static io.fd.honeycomb.translate.util.DataObjects.DataObject3.DataObject31; +import static io.fd.honeycomb.translate.util.DataObjects.DataObject4; +import static io.fd.honeycomb.translate.util.DataObjects.DataObject4.DataObject41; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.inOrder; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.google.common.base.Optional; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; +import io.fd.honeycomb.translate.read.ReadContext; +import io.fd.honeycomb.translate.read.Reader; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InOrder; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.opendaylight.yangtools.concepts.Builder; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class CompositeReaderRegistryTest { + + @Mock + private ReadContext ctx; + private CompositeReaderRegistry reg; + private Reader> reader31; + private Reader> rootReader3; + private Reader> reader41; + private Reader> rootReader4; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + reader31 = mockReader(DataObject31.class); + final Reader> mockedReader3 = mockReader(DataObject3.class); + rootReader3 = + Mockito.spy(CompositeReader.createForReader( + mockedReader3, + ImmutableMap.of(DataObject31.class, reader31))); + // This is a workaround. This functionality is already present in CompositeReader, however when wrapping as spy + // null is always returned. That's why we need to explicitly stub the method with its actual implementation. + // The problem is that the method is inherited as default method from an interface and mockito's spy seems to + // have a problem there + doReturn(mockedReader3.getBuilder(InstanceIdentifier.create(DataObject3.class))) + .when(rootReader3).getBuilder(any(InstanceIdentifier.class)); + + reader41 = mockReader(DataObject41.class); + final Reader> mockedReader4 = mockReader(DataObject4.class); + rootReader4 = + Mockito.spy(CompositeReader.createForReader( + mockedReader4, ImmutableMap.of( + DataObject41.class, reader41))); + // Workaround + doReturn(mockedReader4.getBuilder(InstanceIdentifier.create(DataObject4.class))) + .when(rootReader4).getBuilder(any(InstanceIdentifier.class)); + + reg = new CompositeReaderRegistry(Lists.newArrayList(rootReader3, rootReader4)); + } + + @Test + public void testReadAll() throws Exception { + reg.readAll(ctx); + + // Invoked according to composite ordering + final InOrder inOrder = inOrder(rootReader3, rootReader4, reader31, reader41); + inOrder.verify(rootReader3).read(any(InstanceIdentifier.class), any(ReadContext.class)); + inOrder.verify(reader31).read(any(InstanceIdentifier.class), any(ReadContext.class)); + inOrder.verify(rootReader4).read(any(InstanceIdentifier.class), any(ReadContext.class)); + inOrder.verify(reader41).read(any(InstanceIdentifier.class), any(ReadContext.class)); + } + + @Test + public void testReadSingleRoot() throws Exception { + reg.read(DataObject3.IID, ctx); + + // Invoked according to composite ordering + final InOrder inOrder = inOrder(rootReader3, rootReader4, reader31, reader41); + inOrder.verify(rootReader3).read(any(InstanceIdentifier.class), any(ReadContext.class)); + inOrder.verify(reader31).read(any(InstanceIdentifier.class), any(ReadContext.class)); + + // Only subtree under DataObject3 should be read + verify(rootReader4, times(0)).read(any(InstanceIdentifier.class), any(ReadContext.class)); + verify(reader41, times(0)).read(any(InstanceIdentifier.class), any(ReadContext.class)); + } + + @SuppressWarnings("unchecked") + static > Reader mockReader(final Class dataType) + throws Exception { + final Reader r = mock(Reader.class); + final Object iid = dataType.getDeclaredField("IID").get(null); + when(r.getManagedDataObjectType()).thenReturn((InstanceIdentifier) iid); + final Builder builder = mock(Builder.class); + when(builder.build()).thenReturn(mock(dataType)); + when(r.getBuilder(any(InstanceIdentifier.class))).thenReturn(builder); + when(r.read(any(InstanceIdentifier.class), any(ReadContext.class))).thenReturn(Optional.of(mock(dataType))); + return (Reader) r; + } +} \ No newline at end of file diff --git a/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReaderTest.java b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReaderTest.java new file mode 100644 index 000000000..8d4bdadda --- /dev/null +++ b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReaderTest.java @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.impl.read.registry; + +import static io.fd.honeycomb.translate.util.DataObjects.DataObject4; +import static io.fd.honeycomb.translate.util.DataObjects.DataObject4.DataObject41; +import static io.fd.honeycomb.translate.util.DataObjects.DataObjectK; +import static io.fd.honeycomb.translate.util.DataObjects.DataObjectKey; +import static io.fd.honeycomb.translate.impl.read.registry.CompositeReaderRegistryTest.mockReader; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.google.common.base.Optional; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; +import io.fd.honeycomb.translate.read.ListReader; +import io.fd.honeycomb.translate.read.ReadContext; +import io.fd.honeycomb.translate.read.Reader; +import io.fd.honeycomb.translate.util.DataObjects; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +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.Identifier; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class CompositeReaderTest { + + @Mock + private ReadContext ctx; + private Reader> reader41; + private Reader> reader4; + private Reader> compositeReader; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + reader41 = mockReader(DataObject41.class); + reader4 = mockReader(DataObject4.class); + compositeReader = CompositeReader + .createForReader(reader4, ImmutableMap.of(DataObject41.class, reader41)); + } + + @Test + public void testReadCurrent() throws Exception { + compositeReader.read(DataObject4.IID, ctx); + verify(reader4).readCurrentAttributes(eq(DataObject4.IID), any(Builder.class), eq(ctx)); + verify(reader41).read(DataObject41.IID, ctx); + } + + @Test + public void testReadJustChild() throws Exception { + // Delegating read to child + compositeReader.read(DataObject41.IID, ctx); + verify(reader4, times(0)) + .readCurrentAttributes(any(InstanceIdentifier.class), any(Builder.class), any(ReadContext.class)); + verify(reader41).read(DataObject41.IID, ctx); + } + + @Test + public void testReadFallback() throws Exception { + // Delegating read to delegate as a fallback since IID does not fit, could be handled by the delegate if its + // a subtree handler + compositeReader.read(DataObjects.DataObject4.DataObject42.IID, ctx); + verify(reader4).read(DataObjects.DataObject4.DataObject42.IID, ctx); + verify(reader41, times(0)).read(any(InstanceIdentifier.class), any(ReadContext.class)); + } + + @Test + public void testList() throws Exception { + final Reader> readerK1 = + mockReader(DataObjectK.DataObjectK1.class); + final ListReader> readerK = + mockListReader(DataObjectK.class, Lists.newArrayList(new DataObjectKey(), new DataObjectKey())); + final ListReader> + compositeReaderK = (ListReader>) + CompositeReader.createForReader(readerK, ImmutableMap.of(DataObject41.class, readerK1)); + + compositeReaderK.readList(DataObjectK.IID, ctx); + + verify(readerK).getAllIds(DataObjectK.IID, ctx); + verify(readerK, times(2)) + .readCurrentAttributes(any(InstanceIdentifier.class), any(Builder.class), any(ReadContext.class)); + } + + @SuppressWarnings("unchecked") + static , K extends Identifier, B extends Builder> ListReader mockListReader( + final Class dataType, List keys) + throws Exception { + final ListReader r = mock(ListReader.class); + final Object iid = dataType.getDeclaredField("IID").get(null); + when(r.getManagedDataObjectType()).thenReturn((InstanceIdentifier) iid); + final Builder builder = mock(Builder.class); + when(builder.build()).thenReturn(mock(dataType)); + when(r.getBuilder(any(InstanceIdentifier.class))).thenReturn(builder); + when(r.read(any(InstanceIdentifier.class), any(ReadContext.class))).thenReturn(Optional.of(mock(dataType))); + when(r.getAllIds(any(InstanceIdentifier.class), any(ReadContext.class))).thenReturn(keys); + return (ListReader) r; + } + +} \ No newline at end of file diff --git a/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/registry/SubtreeReaderTest.java b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/registry/SubtreeReaderTest.java new file mode 100644 index 000000000..d2375e6be --- /dev/null +++ b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/registry/SubtreeReaderTest.java @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.impl.read.registry; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import com.google.common.base.Optional; +import com.google.common.collect.Sets; +import io.fd.honeycomb.translate.read.ReadContext; +import io.fd.honeycomb.translate.read.Reader; +import io.fd.honeycomb.translate.util.DataObjects; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.opendaylight.yangtools.concepts.Builder; +import org.opendaylight.yangtools.yang.binding.ChildOf; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class SubtreeReaderTest { + + @Mock + private Reader> delegate; + @Mock + private Reader> delegateLocal; + @Mock + private ReadContext ctx; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + Mockito.doReturn(DataObjects.DataObject4.IID).when(delegate).getManagedDataObjectType(); + doReturn(DataObject1.IID).when(delegateLocal).getManagedDataObjectType(); + } + + @Test + public void testCreate() throws Exception { + final Reader> subtreeR = + SubtreeReader.createForReader(Sets.newHashSet(DataObjects.DataObject4.DataObject41.IID), delegate); + + subtreeR.getBuilder(DataObjects.DataObject4.IID); + verify(delegate).getBuilder(DataObjects.DataObject4.IID); + + subtreeR.getManagedDataObjectType(); + verify(delegate, atLeastOnce()).getManagedDataObjectType(); + } + + @Test(expected = IllegalArgumentException.class) + public void testCreateInvalid() throws Exception { + SubtreeReader.createForReader(Sets.newHashSet(DataObjects.DataObject1.IID), delegate); + } + + @Test(expected = IllegalStateException.class) + public void testReadOnlySubtreeCannotFilter() throws Exception { + final Reader> subtreeR = + SubtreeReader.createForReader(Sets.newHashSet(DataObjects.DataObject4.DataObject41.IID), delegate); + + doReturn(Optional.fromNullable(mock(DataObjects.DataObject4.class))).when(delegate).read(DataObjects.DataObject4.IID, ctx); + subtreeR.read(DataObjects.DataObject4.DataObject41.IID, ctx); + } + + @Test + public void testReadOnlySubtreeNotPresent() throws Exception { + final Reader> subtreeR = + SubtreeReader.createForReader(Sets.newHashSet(DataObjects.DataObject4.DataObject41.IID), delegate); + + doReturn(Optional.absent()).when(delegate).read(DataObjects.DataObject4.IID, ctx); + assertFalse(subtreeR.read(DataObjects.DataObject4.DataObject41.IID, ctx).isPresent()); + } + + @Test + public void testReadOnlySubtreeChild() throws Exception { + final Reader> subtreeR = + SubtreeReader.createForReader(Sets.newHashSet(DataObject1.DataObject11.IID), delegateLocal); + + final DataObject1 mock = mock(DataObject1.class); + final DataObject1.DataObject11 mock11 = mock(DataObject1.DataObject11.class); + doReturn(mock11).when(mock).getDataObject11(); + doReturn(Optional.fromNullable(mock)).when(delegateLocal).read(DataObject1.IID, ctx); + assertEquals(mock11, subtreeR.read(DataObject1.DataObject11.IID, ctx).get()); + } + + @Test + public void testReadEntireSubtree() throws Exception { + final Reader> subtreeR = + SubtreeReader.createForReader(Sets.newHashSet(DataObject1.DataObject11.IID), delegateLocal); + + final DataObject1 mock = mock(DataObject1.class); + final DataObject1.DataObject11 mock11 = mock(DataObject1.DataObject11.class); + doReturn(mock11).when(mock).getDataObject11(); + doReturn(Optional.fromNullable(mock)).when(delegateLocal).read(DataObject1.IID, ctx); + assertEquals(mock, subtreeR.read(DataObject1.IID, ctx).get()); + } + + public abstract static class DataObject1 implements DataObject { + public static InstanceIdentifier IID = InstanceIdentifier.create(DataObject1.class); + + public abstract DataObject11 getDataObject11(); + + public abstract static class DataObject11 implements DataObject, ChildOf { + public static InstanceIdentifier IID = DataObject1.IID.child(DataObject11.class); + } + } +} diff --git a/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/registry/TypeHierarchyTest.java b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/registry/TypeHierarchyTest.java new file mode 100644 index 000000000..bbccc488b --- /dev/null +++ b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/registry/TypeHierarchyTest.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.impl.read.registry; + +import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.CoreMatchers.hasItems; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import com.google.common.collect.Sets; +import io.fd.honeycomb.translate.util.DataObjects; +import org.hamcrest.CoreMatchers; +import org.junit.Test; + +public class TypeHierarchyTest { + + @Test + public void testHierarchy() throws Exception { + final TypeHierarchy typeHierarchy = TypeHierarchy.create(Sets.newHashSet( + DataObjects.DataObject4.DataObject41.DataObject411.IID, + DataObjects.DataObject4.DataObject41.IID,/* Included in previous already */ + DataObjects.DataObject1.IID, + DataObjects.DataObject3.DataObject31.IID)); + + // Roots + assertThat(typeHierarchy.getRoots().size(), is(3)); + assertThat(typeHierarchy.getRoots(), CoreMatchers + .hasItems(DataObjects.DataObject1.IID, DataObjects.DataObject3.IID, DataObjects.DataObject4.IID)); + + // Leaves + assertThat(typeHierarchy.getDirectChildren(DataObjects.DataObject1.IID).size(), is(0)); + assertThat(typeHierarchy.getDirectChildren(DataObjects.DataObject3.DataObject31.IID).size(), is(0)); + assertThat(typeHierarchy.getDirectChildren(DataObjects.DataObject4.DataObject41.DataObject411.IID).size(), is(0)); + + // Intermediate leaves + assertThat(typeHierarchy.getDirectChildren(DataObjects.DataObject3.IID).size(), is(1)); + assertThat(typeHierarchy.getDirectChildren(DataObjects.DataObject3.IID), CoreMatchers + .hasItem(DataObjects.DataObject3.DataObject31.IID)); + assertEquals(typeHierarchy.getDirectChildren(DataObjects.DataObject3.IID), typeHierarchy.getAllChildren( + DataObjects.DataObject3.IID)); + + assertThat(typeHierarchy.getDirectChildren(DataObjects.DataObject4.DataObject41.IID).size(), is(1)); + assertThat(typeHierarchy.getDirectChildren(DataObjects.DataObject4.DataObject41.IID), CoreMatchers.hasItem( + DataObjects.DataObject4.DataObject41.DataObject411.IID)); + assertEquals(typeHierarchy.getDirectChildren(DataObjects.DataObject4.DataObject41.IID), typeHierarchy.getAllChildren( + DataObjects.DataObject4.DataObject41.IID)); + + assertThat(typeHierarchy.getDirectChildren(DataObjects.DataObject4.IID).size(), is(1)); + assertThat(typeHierarchy.getDirectChildren(DataObjects.DataObject4.IID), CoreMatchers + .hasItem(DataObjects.DataObject4.DataObject41.IID)); + assertThat(typeHierarchy.getAllChildren(DataObjects.DataObject4.IID).size(), is(2)); + assertTrue(typeHierarchy.getAllChildren(DataObjects.DataObject4.IID).contains(DataObjects.DataObject4.DataObject41.IID)); + assertTrue(typeHierarchy.getAllChildren(DataObjects.DataObject4.IID).contains(DataObjects.DataObject4.DataObject41.DataObject411.IID)); + } +} + diff --git a/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistryBuilderTest.java b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistryBuilderTest.java new file mode 100644 index 000000000..3a6767265 --- /dev/null +++ b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistryBuilderTest.java @@ -0,0 +1,193 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.impl.write.registry; + +import static org.hamcrest.CoreMatchers.anyOf; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; +import com.google.common.collect.Multimaps; +import com.google.common.collect.Sets; +import io.fd.honeycomb.translate.util.DataObjects; +import io.fd.honeycomb.translate.write.DataObjectUpdate; +import io.fd.honeycomb.translate.write.WriteContext; +import io.fd.honeycomb.translate.write.Writer; +import io.fd.honeycomb.translate.write.registry.WriterRegistry; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import org.hamcrest.CoreMatchers; +import org.junit.Test; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class FlatWriterRegistryBuilderTest { + + @Test + public void testRelationsBefore() throws Exception { + final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); + /* + 1 -> 2 -> 3 + -> 4 + */ + flatWriterRegistryBuilder.add(mockWriter(DataObjects.DataObject3.class)); + flatWriterRegistryBuilder.add(mockWriter(DataObjects.DataObject4.class)); + flatWriterRegistryBuilder.addBefore(mockWriter(DataObjects.DataObject2.class), + Lists.newArrayList(DataObjects.DataObject3.IID, DataObjects.DataObject4.IID)); + flatWriterRegistryBuilder.addBefore(mockWriter(DataObjects.DataObject1.class), DataObjects.DataObject2.IID); + final ImmutableMap, Writer> mappedWriters = + flatWriterRegistryBuilder.getMappedHandlers(); + + final ArrayList> typesInList = Lists.newArrayList(mappedWriters.keySet()); + assertEquals(DataObjects.DataObject1.IID, typesInList.get(0)); + assertEquals(DataObjects.DataObject2.IID, typesInList.get(1)); + assertThat(typesInList.get(2), anyOf(equalTo(DataObjects.DataObject3.IID), equalTo(DataObjects.DataObject4.IID))); + assertThat(typesInList.get(3), anyOf(equalTo(DataObjects.DataObject3.IID), equalTo(DataObjects.DataObject4.IID))); + } + + @Test + public void testBuild() throws Exception { + final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); + final Writer writer = mockWriter(DataObjects.DataObject3.class); + flatWriterRegistryBuilder.add(writer); + final WriterRegistry build = flatWriterRegistryBuilder.build(); + + final InstanceIdentifier id = InstanceIdentifier.create(DataObjects.DataObject3.class); + final DataObjectUpdate update = mock(DataObjectUpdate.class); + doReturn(id).when(update).getId(); + final DataObjects.DataObject3 before = mock(DataObjects.DataObject3.class); + final DataObjects.DataObject3 after = mock(DataObjects.DataObject3.class); + when(update.getDataBefore()).thenReturn(before); + when(update.getDataAfter()).thenReturn(after); + + WriterRegistry.DataObjectUpdates updates = new WriterRegistry.DataObjectUpdates( + Multimaps.forMap(Collections.singletonMap(id, update)), + Multimaps.forMap(Collections.emptyMap())); + final WriteContext ctx = mock(WriteContext.class); + build.update(updates, ctx); + + verify(writer).update(id, before, after, ctx); + } + + @Test(expected = IllegalArgumentException.class) + public void testBuildUnknownWriter() throws Exception { + final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); + final Writer writer = mockWriter(DataObjects.DataObject3.class); + flatWriterRegistryBuilder.add(writer); + final WriterRegistry build = flatWriterRegistryBuilder.build(); + + final InstanceIdentifier id2 = InstanceIdentifier.create(DataObjects.DataObject1.class); + final DataObjectUpdate update2 = mock(DataObjectUpdate.class); + final WriterRegistry.DataObjectUpdates updates = new WriterRegistry.DataObjectUpdates( + Multimaps.forMap(Collections.singletonMap(id2, update2)), + Multimaps.forMap(Collections.emptyMap())); + build.update(updates, mock(WriteContext.class)); + } + + @Test + public void testRelationsAfter() throws Exception { + final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); + /* + 1 -> 2 -> 3 + -> 4 + */ + flatWriterRegistryBuilder.add(mockWriter(DataObjects.DataObject1.class)); + flatWriterRegistryBuilder.addAfter(mockWriter(DataObjects.DataObject2.class), DataObjects.DataObject1.IID); + flatWriterRegistryBuilder.addAfter(mockWriter(DataObjects.DataObject3.class), DataObjects.DataObject2.IID); + flatWriterRegistryBuilder.addAfter(mockWriter(DataObjects.DataObject4.class), + Lists.newArrayList(DataObjects.DataObject2.IID, DataObjects.DataObject3.IID)); + final ImmutableMap, Writer> mappedWriters = + flatWriterRegistryBuilder.getMappedHandlers(); + + final List> typesInList = Lists.newArrayList(mappedWriters.keySet()); + assertEquals(DataObjects.DataObject1.IID, typesInList.get(0)); + assertEquals(DataObjects.DataObject2.IID, typesInList.get(1)); + assertThat(typesInList.get(2), anyOf(equalTo(DataObjects.DataObject3.IID), equalTo(DataObjects.DataObject4.IID))); + assertThat(typesInList.get(3), anyOf(equalTo(DataObjects.DataObject3.IID), equalTo(DataObjects.DataObject4.IID))); + } + + @Test(expected = IllegalArgumentException.class) + public void testRelationsLoop() throws Exception { + final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); + /* + 1 -> 2 -> 1 + */ + flatWriterRegistryBuilder.add(mockWriter(DataObjects.DataObject1.class)); + flatWriterRegistryBuilder.addAfter(mockWriter(DataObjects.DataObject2.class), DataObjects.DataObject1.IID); + flatWriterRegistryBuilder.addAfter(mockWriter(DataObjects.DataObject1.class), DataObjects.DataObject2.IID); + } + + @Test(expected = IllegalArgumentException.class) + public void testAddWriterTwice() throws Exception { + final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); + flatWriterRegistryBuilder.add(mockWriter(DataObjects.DataObject1.class)); + flatWriterRegistryBuilder.add(mockWriter(DataObjects.DataObject1.class)); + } + + @Test + public void testAddSubtreeWriter() throws Exception { + final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); + flatWriterRegistryBuilder.subtreeAdd( + Sets.newHashSet(DataObjects.DataObject4.DataObject41.IID, + DataObjects.DataObject4.DataObject41.IID), + mockWriter(DataObjects.DataObject4.class)); + final ImmutableMap, Writer> mappedWriters = + flatWriterRegistryBuilder.getMappedHandlers(); + final ArrayList> typesInList = Lists.newArrayList(mappedWriters.keySet()); + + assertEquals(DataObjects.DataObject4.IID, typesInList.get(0)); + assertEquals(1, typesInList.size()); + } + + @Test + public void testCreateSubtreeWriter() throws Exception { + final Writer forWriter = SubtreeWriter.createForWriter(Sets.newHashSet( + DataObjects.DataObject4.DataObject41.IID, + DataObjects.DataObject4.DataObject41.DataObject411.IID, + DataObjects.DataObject4.DataObject42.IID), + mockWriter(DataObjects.DataObject4.class)); + assertThat(forWriter, instanceOf(SubtreeWriter.class)); + assertThat(((SubtreeWriter) forWriter).getHandledChildTypes().size(), is(3)); + assertThat(((SubtreeWriter) forWriter).getHandledChildTypes(), CoreMatchers.hasItems(DataObjects.DataObject4.DataObject41.IID, + DataObjects.DataObject4.DataObject42.IID, DataObjects.DataObject4.DataObject41.DataObject411.IID)); + } + + @Test(expected = IllegalArgumentException.class) + public void testCreateInvalidSubtreeWriter() throws Exception { + SubtreeWriter.createForWriter(Sets.newHashSet( + InstanceIdentifier.create(DataObjects.DataObject3.class).child(DataObjects.DataObject3.DataObject31.class)), + mockWriter(DataObjects.DataObject4.class)); + } + + @SuppressWarnings("unchecked") + private Writer mockWriter(final Class doClass) + throws NoSuchFieldException, IllegalAccessException { + final Writer mock = mock(Writer.class); + when(mock.getManagedDataObjectType()).thenReturn((InstanceIdentifier) doClass.getDeclaredField("IID").get(null)); + return mock; + } + +} \ No newline at end of file diff --git a/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistryTest.java b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistryTest.java new file mode 100644 index 000000000..65742df33 --- /dev/null +++ b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistryTest.java @@ -0,0 +1,328 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.impl.write.registry; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.inOrder; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.when; + +import com.google.common.collect.HashMultimap; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableMultimap; +import com.google.common.collect.Multimap; +import io.fd.honeycomb.translate.util.DataObjects; +import io.fd.honeycomb.translate.util.DataObjects.DataObject1; +import io.fd.honeycomb.translate.write.DataObjectUpdate; +import io.fd.honeycomb.translate.write.WriteContext; +import io.fd.honeycomb.translate.write.Writer; +import io.fd.honeycomb.translate.write.registry.WriterRegistry; +import org.hamcrest.CoreMatchers; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InOrder; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class FlatWriterRegistryTest { + + @Mock + private Writer writer1; + @Mock + private Writer writer2; + @Mock + private Writer writer3; + @Mock + private Writer writer4; + @Mock + private WriteContext ctx; + @Mock + private WriteContext revertWriteContext; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + when(writer1.getManagedDataObjectType()).thenReturn(DataObjects.DataObject1.IID); + when(writer2.getManagedDataObjectType()).thenReturn(DataObjects.DataObject2.IID); + when(writer3.getManagedDataObjectType()).thenReturn(DataObjects.DataObject3.IID); + } + + @Test + public void testMultipleUpdatesForSingleWriter() throws Exception { + final FlatWriterRegistry flatWriterRegistry = + new FlatWriterRegistry(ImmutableMap.of(DataObjects.DataObject1.IID, writer1, DataObjects.DataObject2.IID, writer2)); + + final Multimap, DataObjectUpdate> updates = HashMultimap.create(); + final InstanceIdentifier iid = InstanceIdentifier.create(DataObjects.DataObject1.class); + final InstanceIdentifier iid2 = InstanceIdentifier.create(DataObjects.DataObject1.class); + final DataObjects.DataObject1 dataObject = mock(DataObjects.DataObject1.class); + updates.put(DataObjects.DataObject1.IID, DataObjectUpdate.create(iid, dataObject, dataObject)); + updates.put(DataObjects.DataObject1.IID, DataObjectUpdate.create(iid2, dataObject, dataObject)); + flatWriterRegistry.update(new WriterRegistry.DataObjectUpdates(updates, ImmutableMultimap.of()), ctx); + + verify(writer1).update(iid, dataObject, dataObject, ctx); + verify(writer1).update(iid2, dataObject, dataObject, ctx); + // Invoked when registry is being created + verifyNoMoreInteractions(writer1); + verifyZeroInteractions(writer2); + } + + @Test + public void testMultipleUpdatesForMultipleWriters() throws Exception { + final FlatWriterRegistry flatWriterRegistry = + new FlatWriterRegistry(ImmutableMap.of(DataObjects.DataObject1.IID, writer1, DataObjects.DataObject2.IID, writer2)); + + final Multimap, DataObjectUpdate> updates = HashMultimap.create(); + final InstanceIdentifier iid = InstanceIdentifier.create(DataObjects.DataObject1.class); + final DataObjects.DataObject1 dataObject = mock(DataObjects.DataObject1.class); + updates.put(DataObjects.DataObject1.IID, DataObjectUpdate.create(iid, dataObject, dataObject)); + final InstanceIdentifier iid2 = InstanceIdentifier.create(DataObjects.DataObject2.class); + final DataObjects.DataObject2 dataObject2 = mock(DataObjects.DataObject2.class); + updates.put(DataObjects.DataObject2.IID, DataObjectUpdate.create(iid2, dataObject2, dataObject2)); + flatWriterRegistry.update(new WriterRegistry.DataObjectUpdates(updates, ImmutableMultimap.of()), ctx); + + final InOrder inOrder = inOrder(writer1, writer2); + inOrder.verify(writer1).update(iid, dataObject, dataObject, ctx); + inOrder.verify(writer2).update(iid2, dataObject2, dataObject2, ctx); + + verifyNoMoreInteractions(writer1); + verifyNoMoreInteractions(writer2); + } + + @Test + public void testMultipleDeletesForMultipleWriters() throws Exception { + final FlatWriterRegistry flatWriterRegistry = + new FlatWriterRegistry(ImmutableMap.of(DataObjects.DataObject1.IID, writer1, DataObjects.DataObject2.IID, writer2)); + + final Multimap, DataObjectUpdate.DataObjectDelete> deletes = HashMultimap.create(); + final InstanceIdentifier iid = InstanceIdentifier.create(DataObjects.DataObject1.class); + final DataObjects.DataObject1 dataObject = mock(DataObjects.DataObject1.class); + deletes.put(DataObjects.DataObject1.IID, ((DataObjectUpdate.DataObjectDelete) DataObjectUpdate.create(iid, dataObject, null))); + final InstanceIdentifier iid2 = InstanceIdentifier.create(DataObjects.DataObject2.class); + final DataObjects.DataObject2 dataObject2 = mock(DataObjects.DataObject2.class); + deletes.put(DataObjects.DataObject2.IID, ((DataObjectUpdate.DataObjectDelete) DataObjectUpdate.create(iid2, dataObject2, null))); + flatWriterRegistry.update(new WriterRegistry.DataObjectUpdates(ImmutableMultimap.of(), deletes), ctx); + + final InOrder inOrder = inOrder(writer1, writer2); + // Reversed order of invocation, first writer2 and then writer1 + inOrder.verify(writer2).update(iid2, dataObject2, null, ctx); + inOrder.verify(writer1).update(iid, dataObject, null, ctx); + + verifyNoMoreInteractions(writer1); + verifyNoMoreInteractions(writer2); + } + + @Test + public void testMultipleUpdatesAndDeletesForMultipleWriters() throws Exception { + final FlatWriterRegistry flatWriterRegistry = + new FlatWriterRegistry(ImmutableMap.of(DataObjects.DataObject1.IID, writer1, DataObjects.DataObject2.IID, writer2)); + + final Multimap, DataObjectUpdate.DataObjectDelete> deletes = HashMultimap.create(); + final Multimap, DataObjectUpdate> updates = HashMultimap.create(); + final InstanceIdentifier iid = InstanceIdentifier.create(DataObjects.DataObject1.class); + final DataObjects.DataObject1 dataObject = mock(DataObjects.DataObject1.class); + // Writer 1 delete + deletes.put(DataObjects.DataObject1.IID, ((DataObjectUpdate.DataObjectDelete) DataObjectUpdate.create(iid, dataObject, null))); + // Writer 1 update + updates.put(DataObjects.DataObject1.IID, DataObjectUpdate.create(iid, dataObject, dataObject)); + final InstanceIdentifier iid2 = InstanceIdentifier.create(DataObjects.DataObject2.class); + final DataObjects.DataObject2 dataObject2 = mock(DataObjects.DataObject2.class); + // Writer 2 delete + deletes.put(DataObjects.DataObject2.IID, ((DataObjectUpdate.DataObjectDelete) DataObjectUpdate.create(iid2, dataObject2, null))); + // Writer 2 update + updates.put(DataObjects.DataObject2.IID, DataObjectUpdate.create(iid2, dataObject2, dataObject2)); + flatWriterRegistry.update(new WriterRegistry.DataObjectUpdates(updates, deletes), ctx); + + final InOrder inOrder = inOrder(writer1, writer2); + // Reversed order of invocation, first writer2 and then writer1 for deletes + inOrder.verify(writer2).update(iid2, dataObject2, null, ctx); + inOrder.verify(writer1).update(iid, dataObject, null, ctx); + // Then also updates are processed + inOrder.verify(writer1).update(iid, dataObject, dataObject, ctx); + inOrder.verify(writer2).update(iid2, dataObject2, dataObject2, ctx); + + verifyNoMoreInteractions(writer1); + verifyNoMoreInteractions(writer2); + } + + @Test(expected = IllegalArgumentException.class) + public void testMultipleUpdatesOneMissing() throws Exception { + final FlatWriterRegistry flatWriterRegistry = + new FlatWriterRegistry(ImmutableMap.of(DataObjects.DataObject1.IID, writer1)); + + final Multimap, DataObjectUpdate> updates = HashMultimap.create(); + addUpdate(updates, DataObjects.DataObject1.class); + addUpdate(updates, DataObjects.DataObject2.class); + flatWriterRegistry.update(new WriterRegistry.DataObjectUpdates(updates, ImmutableMultimap.of()), ctx); + } + + @Test + public void testMultipleUpdatesOneFailing() throws Exception { + final FlatWriterRegistry flatWriterRegistry = + new FlatWriterRegistry(ImmutableMap.of(DataObjects.DataObject1.IID, writer1, DataObjects.DataObject2.IID, writer2)); + + // Writer1 always fails + doThrow(new RuntimeException()).when(writer1) + .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), any(WriteContext.class)); + + final Multimap, DataObjectUpdate> updates = HashMultimap.create(); + addUpdate(updates, DataObjects.DataObject1.class); + addUpdate(updates, DataObjects.DataObject2.class); + + try { + flatWriterRegistry.update(new WriterRegistry.DataObjectUpdates(updates, ImmutableMultimap.of()), ctx); + fail("Bulk update should have failed on writer1"); + } catch (WriterRegistry.BulkUpdateException e) { + assertThat(e.getFailedIds().size(), is(2)); + assertThat(e.getFailedIds(), CoreMatchers.hasItem(InstanceIdentifier.create(DataObjects.DataObject2.class))); + assertThat(e.getFailedIds(), CoreMatchers.hasItem(InstanceIdentifier.create(DataObjects.DataObject1.class))); + } + } + + @Test + public void testMultipleUpdatesOneFailingThenRevertWithSuccess() throws Exception { + final FlatWriterRegistry flatWriterRegistry = + new FlatWriterRegistry( + ImmutableMap.of(DataObjects.DataObject1.IID, writer1, DataObjects.DataObject2.IID, writer2, DataObjects.DataObject3.IID, writer3)); + + // Writer1 always fails + doThrow(new RuntimeException()).when(writer3) + .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), any(WriteContext.class)); + + final Multimap, DataObjectUpdate> updates = HashMultimap.create(); + addUpdate(updates, DataObjects.DataObject1.class); + addUpdate(updates, DataObjects.DataObject3.class); + final InstanceIdentifier iid2 = InstanceIdentifier.create(DataObjects.DataObject2.class); + final DataObjects.DataObject2 before2 = mock(DataObjects.DataObject2.class); + final DataObjects.DataObject2 after2 = mock(DataObjects.DataObject2.class); + updates.put(DataObjects.DataObject2.IID, DataObjectUpdate.create(iid2, before2, after2)); + + try { + flatWriterRegistry.update(new WriterRegistry.DataObjectUpdates(updates, ImmutableMultimap.of()), ctx); + fail("Bulk update should have failed on writer1"); + } catch (WriterRegistry.BulkUpdateException e) { + assertThat(e.getFailedIds().size(), is(1)); + + final InOrder inOrder = inOrder(writer1, writer2, writer3); + inOrder.verify(writer1) + .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), any(WriteContext.class)); + inOrder.verify(writer2) + .update(iid2, before2, after2, ctx); + inOrder.verify(writer3) + .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), any(WriteContext.class)); + + e.revertChanges(revertWriteContext); + // Revert changes. Successful updates are iterated in reverse + // also binding other write context,to verify if update context is not reused + inOrder.verify(writer2) + .update(iid2, after2, before2, revertWriteContext); + inOrder.verify(writer1) + .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), eq(revertWriteContext)); + verifyNoMoreInteractions(writer3); + } + } + + @Test + public void testMultipleUpdatesOneFailingThenRevertWithFail() throws Exception { + final FlatWriterRegistry flatWriterRegistry = + new FlatWriterRegistry( + ImmutableMap.of(DataObjects.DataObject1.IID, writer1, DataObjects.DataObject2.IID, writer2, DataObjects.DataObject3.IID, writer3)); + + // Writer1 always fails + doThrow(new RuntimeException()).when(writer3) + .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), any(WriteContext.class)); + + final Multimap, DataObjectUpdate> updates = HashMultimap.create(); + addUpdate(updates, DataObjects.DataObject1.class); + addUpdate(updates, DataObjects.DataObject2.class); + addUpdate(updates, DataObjects.DataObject3.class); + + try { + flatWriterRegistry.update(new WriterRegistry.DataObjectUpdates(updates, ImmutableMultimap.of()), ctx); + fail("Bulk update should have failed on writer1"); + } catch (WriterRegistry.BulkUpdateException e) { + // Writer1 always fails from now + doThrow(new RuntimeException()).when(writer1) + .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), any(WriteContext.class)); + try { + e.revertChanges(revertWriteContext); + } catch (WriterRegistry.Reverter.RevertFailedException e1) { + assertThat(e1.getNotRevertedChanges().size(), is(1)); + assertThat(e1.getNotRevertedChanges(), CoreMatchers + .hasItem(InstanceIdentifier.create(DataObjects.DataObject1.class))); + } + } + } + + @Test + public void testMutlipleUpdatesWithOneKeyedContainer() throws Exception { + final InstanceIdentifier internallyKeyedIdentifier = InstanceIdentifier.create(DataObject1.class) + .child(DataObjects.DataObject1ChildK.class, new DataObjects.DataObject1ChildKey()); + + final FlatWriterRegistry flatWriterRegistry = + new FlatWriterRegistry( + ImmutableMap.of(DataObjects.DataObject1.IID, writer1, DataObjects.DataObject1ChildK.IID,writer4)); + + // Writer1 always fails + doThrow(new RuntimeException()).when(writer1) + .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), + any(WriteContext.class)); + + final Multimap, DataObjectUpdate> updates = HashMultimap.create(); + addKeyedUpdate(updates,DataObjects.DataObject1ChildK.class); + addUpdate(updates, DataObjects.DataObject1.class); + try { + flatWriterRegistry.update(new WriterRegistry.DataObjectUpdates(updates, ImmutableMultimap.of()), ctx); + fail("Bulk update should have failed on writer1"); + } catch (WriterRegistry.BulkUpdateException e) { + // Writer1 always fails from now + doThrow(new RuntimeException()).when(writer1) + .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), + any(WriteContext.class)); + try { + e.revertChanges(revertWriteContext); + } catch (WriterRegistry.Reverter.RevertFailedException e1) { + assertThat(e1.getNotRevertedChanges().size(), is(1)); + assertThat(e1.getNotRevertedChanges(), CoreMatchers + .hasItem(InstanceIdentifier.create(DataObjects.DataObject1.class))); + } + } + } + + private void addKeyedUpdate(final Multimap, DataObjectUpdate> updates, + final Class type) throws Exception { + final InstanceIdentifier iid = (InstanceIdentifier) type.getDeclaredField("IID").get(null); + final InstanceIdentifier keyedIid = (InstanceIdentifier) type.getDeclaredField("INTERNALLY_KEYED_IID").get(null); + updates.put(iid, DataObjectUpdate.create(keyedIid, mock(type), mock(type))); + } + + private void addUpdate(final Multimap, DataObjectUpdate> updates, + final Class type) throws Exception { + final InstanceIdentifier iid = (InstanceIdentifier) type.getDeclaredField("IID").get(null); + updates.put(iid, DataObjectUpdate.create(iid, mock(type), mock(type))); + } +} \ No newline at end of file diff --git a/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/registry/SubtreeWriterTest.java b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/registry/SubtreeWriterTest.java new file mode 100644 index 000000000..ff2f83158 --- /dev/null +++ b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/registry/SubtreeWriterTest.java @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.impl.write.registry; + +import static org.hamcrest.CoreMatchers.hasItem; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.when; + +import com.google.common.collect.Sets; +import io.fd.honeycomb.translate.util.DataObjects; +import io.fd.honeycomb.translate.write.Writer; +import java.util.Collections; +import org.hamcrest.CoreMatchers; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class SubtreeWriterTest { + + @Mock + Writer writer; + @Mock + Writer writer11; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + when(writer.getManagedDataObjectType()).thenReturn(DataObjects.DataObject4.IID); + when(writer11.getManagedDataObjectType()).thenReturn(DataObjects.DataObject4.DataObject41.IID); + } + + @Test(expected = IllegalArgumentException.class) + public void testSubtreeWriterCreationFail() throws Exception { + // The subtree node identified by IID.c(DataObject.class) is not a child of writer.getManagedDataObjectType + SubtreeWriter.createForWriter(Collections.singleton(InstanceIdentifier.create(DataObject.class)), writer); + } + + @Test(expected = IllegalArgumentException.class) + public void testSubtreeWriterCreationFailInvalidIid() throws Exception { + // The subtree node identified by IID.c(DataObject.class) is not a child of writer.getManagedDataObjectType + SubtreeWriter.createForWriter(Collections.singleton(DataObjects.DataObject4.IID), writer); + } + + @Test + public void testSubtreeWriterCreation() throws Exception { + final SubtreeWriter forWriter = (SubtreeWriter) SubtreeWriter.createForWriter(Sets.newHashSet( + DataObjects.DataObject4.DataObject41.IID, + DataObjects.DataObject4.DataObject41.DataObject411.IID, + DataObjects.DataObject4.DataObject42.IID), + writer); + + assertEquals(writer.getManagedDataObjectType(), forWriter.getManagedDataObjectType()); + assertEquals(3, forWriter.getHandledChildTypes().size()); + } + + @Test + public void testSubtreeWriterHandledTypes() throws Exception { + final SubtreeWriter forWriter = (SubtreeWriter) SubtreeWriter.createForWriter(Sets.newHashSet( + DataObjects.DataObject4.DataObject41.DataObject411.IID), + writer); + + assertEquals(writer.getManagedDataObjectType(), forWriter.getManagedDataObjectType()); + assertEquals(1, forWriter.getHandledChildTypes().size()); + assertThat(forWriter.getHandledChildTypes(), CoreMatchers.hasItem(DataObjects.DataObject4.DataObject41.DataObject411.IID)); + } + +} \ No newline at end of file diff --git a/infra/translate-spi/src/main/java/io/fd/honeycomb/translate/spi/read/ReaderCustomizer.java b/infra/translate-spi/src/main/java/io/fd/honeycomb/translate/spi/read/ReaderCustomizer.java index acfabf5bc..738431eea 100644 --- a/infra/translate-spi/src/main/java/io/fd/honeycomb/translate/spi/read/ReaderCustomizer.java +++ b/infra/translate-spi/src/main/java/io/fd/honeycomb/translate/spi/read/ReaderCustomizer.java @@ -57,4 +57,19 @@ public interface ReaderCustomizer> { void merge(@Nonnull final Builder parentBuilder, @Nonnull final O readValue); + /** + * Check whether the resulting value should be added to the results or ignored. + * Invoked after {@link #readCurrentAttributes(InstanceIdentifier, Builder, ReadContext)} + * + * @param id Keyed instance identifier of read data + * @param built Read data as returned from builder + * after {@link #readCurrentAttributes(InstanceIdentifier, Builder, ReadContext)} invocation + * @param ctx Read context + * + * @return true if value is present (even if empty) + */ + default boolean isPresent(final InstanceIdentifier id, final O built, final ReadContext ctx) throws ReadFailedException { + // Default impl = check whether read value is empty + return !built.equals(getBuilder(id).build()); + } } diff --git a/infra/translate-utils/pom.xml b/infra/translate-utils/pom.xml index 1ac453d26..6f01a66fc 100644 --- a/infra/translate-utils/pom.xml +++ b/infra/translate-utils/pom.xml @@ -78,4 +78,20 @@ test + + + + + org.apache.maven.plugins + maven-jar-plugin + + + + test-jar + + + + + + diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/AbstractGenericReader.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/AbstractGenericReader.java index 40c78b3c9..b19b72ecf 100644 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/AbstractGenericReader.java +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/AbstractGenericReader.java @@ -56,18 +56,15 @@ public abstract class AbstractGenericReader read = built.equals(emptyValue) - ? Optional.absent() - : Optional.of(built); + final Optional read = isPresent(id, built, ctx) + ? Optional.of(built) + : Optional.absent(); LOG.debug("{}: Current node read successfully. Result: {}", this, read); return read; diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/BindingBrokerReader.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/BindingBrokerReader.java index b7a38e97f..e59e642c4 100644 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/BindingBrokerReader.java +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/BindingBrokerReader.java @@ -50,6 +50,11 @@ public final class BindingBrokerReader id, final D built, final ReadContext ctx) { + throw new UnsupportedOperationException("Not supported"); + } + @Nonnull @Override public Optional read(@Nonnull final InstanceIdentifier id, diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/DelegatingReader.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/DelegatingReader.java new file mode 100644 index 000000000..513639610 --- /dev/null +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/DelegatingReader.java @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.util.read; + +import com.google.common.base.Optional; +import io.fd.honeycomb.translate.read.ListReader; +import io.fd.honeycomb.translate.read.ReadContext; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.read.Reader; +import java.util.List; +import javax.annotation.Nonnull; +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.Identifier; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +/** + * A trait of a delegating reader. Delegates all the calls to its delegate. + */ +public interface DelegatingReader> extends Reader { + + Reader getDelegate(); + + @Nonnull + default Optional read(@Nonnull final InstanceIdentifier id, + @Nonnull final ReadContext ctx) throws ReadFailedException { + return getDelegate().read(id, ctx); + } + + default void readCurrentAttributes(@Nonnull final InstanceIdentifier id, + @Nonnull final B builder, + @Nonnull final ReadContext ctx) throws ReadFailedException { + getDelegate().readCurrentAttributes(id, builder, ctx); + } + + @Nonnull + default B getBuilder(final InstanceIdentifier id) { + return getDelegate().getBuilder(id); + } + + default void merge(@Nonnull final Builder parentBuilder, + @Nonnull final D readValue) { + getDelegate().merge(parentBuilder, readValue); + } + + @Override + default boolean isPresent(InstanceIdentifier id, D built, final ReadContext ctx) throws ReadFailedException { + return getDelegate().isPresent(id, built, ctx); + } + + @Nonnull + default InstanceIdentifier getManagedDataObjectType() { + return getDelegate().getManagedDataObjectType(); + } + + /** + * ListReader specific delegating trait. + */ + interface DelegatingListReader, K extends Identifier, B extends Builder> + extends DelegatingReader, ListReader { + + ListReader getDelegate(); + + @Override + default List getAllIds(@Nonnull InstanceIdentifier id, @Nonnull ReadContext ctx) + throws ReadFailedException { + return getDelegate().getAllIds(id, ctx); + } + + @Nonnull + @Override + default List readList(@Nonnull final InstanceIdentifier id, @Nonnull final ReadContext ctx) + throws ReadFailedException { + return getDelegate().readList(id, ctx); + } + + @Override + default void merge(@Nonnull final Builder builder, + @Nonnull final List readData) { + getDelegate().merge(builder, readData); + } + + @Override + default void merge(@Nonnull final Builder parentBuilder, @Nonnull final D readValue) { + getDelegate().merge(parentBuilder, readValue); + } + } +} diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/KeepaliveReaderWrapper.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/KeepaliveReaderWrapper.java index 5ab9bc59e..9a695a53c 100644 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/KeepaliveReaderWrapper.java +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/KeepaliveReaderWrapper.java @@ -18,11 +18,10 @@ package io.fd.honeycomb.translate.util.read; import com.google.common.base.Optional; import com.google.common.base.Preconditions; -import io.fd.honeycomb.translate.read.ReadContext; -import io.fd.honeycomb.translate.read.Reader; import io.fd.honeycomb.translate.MappingContext; import io.fd.honeycomb.translate.ModificationCache; -import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.read.ReadContext; +import io.fd.honeycomb.translate.read.Reader; import java.io.Closeable; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; @@ -40,7 +39,7 @@ import org.slf4j.LoggerFactory; * In case a specific error occurs, Keep-alive failure listener gets notified. */ public final class KeepaliveReaderWrapper> - implements Reader, Runnable, Closeable { + implements DelegatingReader,Runnable, Closeable { private static final Logger LOG = LoggerFactory.getLogger(KeepaliveReaderWrapper.class); @@ -73,34 +72,6 @@ public final class KeepaliveReaderWrapper read(@Nonnull final InstanceIdentifier id, - @Nonnull final ReadContext ctx) throws ReadFailedException { - return delegate.read(id, ctx); - } - - public void readCurrentAttributes(@Nonnull final InstanceIdentifier id, - @Nonnull final B builder, - @Nonnull final ReadContext ctx) throws ReadFailedException { - delegate.readCurrentAttributes(id, builder, ctx); - } - - @Nonnull - public B getBuilder(final InstanceIdentifier id) { - return delegate.getBuilder(id); - } - - public void merge(@Nonnull final Builder parentBuilder, - @Nonnull final D readValue) { - delegate.merge(parentBuilder, readValue); - } - - @Nonnull - @Override - public InstanceIdentifier getManagedDataObjectType() { - return delegate.getManagedDataObjectType(); - } - @Override public void run() { LOG.trace("Invoking keepalive"); @@ -123,6 +94,11 @@ public final class KeepaliveReaderWrapper getDelegate() { + return delegate; + } + /** * Listener that gets called whenever keepalive fails as expected. */ diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveReader.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveReader.java deleted file mode 100644 index 2aa35514e..000000000 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveReader.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (c) 2016 Cisco and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.fd.honeycomb.translate.util.read; - -import io.fd.honeycomb.translate.read.ReadContext; -import io.fd.honeycomb.translate.read.ReadFailedException; -import javax.annotation.Nonnull; -import org.opendaylight.yangtools.concepts.Builder; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; - -/** - * Reader that performs no read operation on its own, just fills in the hierarchy. - *

- * Might be slow due to reflection ! - */ -public class ReflexiveReader> extends AbstractGenericReader { - - private final ReflexiveReaderCustomizer customizer; - - public ReflexiveReader(final InstanceIdentifier identifier, final Class builderClass) { - super(identifier); - this.customizer = new ReflexiveReaderCustomizer<>(identifier.getTargetType(), builderClass); - } - - @Override - public void readCurrentAttributes(@Nonnull final InstanceIdentifier id, @Nonnull final B builder, - @Nonnull final ReadContext ctx) - throws ReadFailedException { - customizer.readCurrentAttributes(id, builder, ctx); - } - - @Nonnull - @Override - public B getBuilder(final InstanceIdentifier id) { - return customizer.getBuilder(id); - } - - @Override - public void merge(@Nonnull final Builder parentBuilder, @Nonnull final C readValue) { - customizer.merge(parentBuilder, readValue); - } -} diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveReaderCustomizer.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveReaderCustomizer.java index 18e6285f3..087873306 100644 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveReaderCustomizer.java +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveReaderCustomizer.java @@ -33,7 +33,7 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; /** * Might be slow. */ -class ReflexiveReaderCustomizer> extends NoopReaderCustomizer { +public class ReflexiveReaderCustomizer> extends NoopReaderCustomizer { private final Class typeClass; private final Class builderClass; diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/CompositeReader.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/CompositeReader.java deleted file mode 100644 index b52fd09ec..000000000 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/CompositeReader.java +++ /dev/null @@ -1,241 +0,0 @@ -/* - * Copyright (c) 2016 Cisco and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.fd.honeycomb.translate.util.read.registry; - -import static com.google.common.base.Preconditions.checkArgument; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Optional; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Iterables; -import io.fd.honeycomb.translate.read.InitFailedException; -import io.fd.honeycomb.translate.read.InitListReader; -import io.fd.honeycomb.translate.read.Initializer; -import io.fd.honeycomb.translate.read.ListReader; -import io.fd.honeycomb.translate.read.ReadContext; -import io.fd.honeycomb.translate.read.ReadFailedException; -import io.fd.honeycomb.translate.read.Reader; -import io.fd.honeycomb.translate.util.RWUtils; -import io.fd.honeycomb.translate.util.read.AbstractGenericReader; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import javax.annotation.Nonnull; -import org.opendaylight.controller.md.sal.binding.api.DataBroker; -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.Identifier; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -class CompositeReader> - extends AbstractGenericReader - implements Initializer { - - private static final Logger LOG = LoggerFactory.getLogger(CompositeReader.class); - - private final Reader delegate; - private final ImmutableMap, Reader>> childReaders; - - private CompositeReader(final Reader reader, - final ImmutableMap, Reader>> childReaders) { - super(reader.getManagedDataObjectType()); - this.delegate = reader; - this.childReaders = childReaders; - } - - @VisibleForTesting - ImmutableMap, Reader>> getChildReaders() { - return childReaders; - } - - @SuppressWarnings("unchecked") - public static InstanceIdentifier appendTypeToId( - final InstanceIdentifier parentId, final InstanceIdentifier type) { - final InstanceIdentifier.PathArgument t = new InstanceIdentifier.Item<>(type.getTargetType()); - return (InstanceIdentifier) InstanceIdentifier.create(Iterables.concat( - parentId.getPathArguments(), Collections.singleton(t))); - } - - @Nonnull - @Override - public Optional read(@Nonnull final InstanceIdentifier id, - @Nonnull final ReadContext ctx) throws ReadFailedException { - if (shouldReadCurrent(id)) { - LOG.trace("{}: Reading current: {}", this, id); - return readCurrent((InstanceIdentifier) id, ctx); - } else if (shouldDelegateToChild(id)) { - LOG.trace("{}: Reading child: {}", this, id); - return readSubtree(id, ctx); - } else { - // Fallback - LOG.trace("{}: Delegating read: {}", this, id); - return delegate.read(id, ctx); - } - } - - private boolean shouldReadCurrent(@Nonnull final InstanceIdentifier id) { - return id.getTargetType().equals(getManagedDataObjectType().getTargetType()); - } - - private boolean shouldDelegateToChild(@Nonnull final InstanceIdentifier id) { - return childReaders.containsKey(RWUtils.getNextId(id, getManagedDataObjectType()).getType()); - } - - private Optional readSubtree(final InstanceIdentifier id, - final ReadContext ctx) throws ReadFailedException { - final InstanceIdentifier.PathArgument nextId = RWUtils.getNextId(id, getManagedDataObjectType()); - final Reader> nextReader = childReaders.get(nextId.getType()); - checkArgument(nextReader != null, "Unable to read: %s. No delegate present, available readers at next level: %s", - id, childReaders.keySet()); - return nextReader.read(id, ctx); - } - - @SuppressWarnings("unchecked") - private void readChildren(final InstanceIdentifier id, @Nonnull final ReadContext ctx, final B builder) - throws ReadFailedException { - LOG.debug("{}: Reading children: {}", this, childReaders.keySet()); - for (Reader child : childReaders.values()) { - final InstanceIdentifier childId = appendTypeToId(id, child.getManagedDataObjectType()); - - LOG.debug("{}: Reading child from: {}", this, child); - if (child instanceof ListReader) { - final List list = ((ListReader) child).readList(childId, ctx); - // Dont set empty lists - if (!list.isEmpty()) { - ((ListReader) child).merge(builder, list); - } - } else { - final Optional read = child.read(childId, ctx); - if (read.isPresent()) { - child.merge(builder, read.get()); - } - } - } - } - - @Override - public void readCurrentAttributes(@Nonnull final InstanceIdentifier id, @Nonnull final B builder, - @Nonnull final ReadContext ctx) - throws ReadFailedException { - delegate.readCurrentAttributes(id, builder, ctx); - readChildren(id, ctx, builder); - } - - @Nonnull - @Override - public B getBuilder(final InstanceIdentifier id) { - return delegate.getBuilder(id); - } - - @Override - public void merge(@Nonnull final Builder parentBuilder, @Nonnull final D readValue) { - delegate.merge(parentBuilder, readValue); - } - - /** - * Wrap a Reader as a Composite Reader. - */ - static > Reader createForReader( - @Nonnull final Reader reader, - @Nonnull final ImmutableMap, Reader>> childReaders) { - - return (reader instanceof ListReader) - ? new CompositeListReader<>((ListReader) reader, childReaders) - : new CompositeReader<>(reader, childReaders); - } - - @SuppressWarnings("unchecked") - @Override - public void init(final DataBroker broker, final InstanceIdentifier id, final ReadContext ctx) throws InitFailedException { - if (delegate instanceof Initializer) { - LOG.trace("{}: Initializing current: {}", this, id); - ((Initializer) delegate).init(broker, id, ctx); - } - - for (Reader child : childReaders.values()) { - final InstanceIdentifier childId = appendTypeToId(id, child.getManagedDataObjectType()); - - if (child instanceof Initializer) { - LOG.trace("{}: Initializing child: {}", this, childId); - ((Initializer) child).init(broker, childId, ctx); - } - } - } - - private static class CompositeListReader, B extends Builder, K extends Identifier> - extends CompositeReader - implements InitListReader { - - private final ListReader delegate; - - private CompositeListReader(final ListReader reader, - final ImmutableMap, Reader>> childReaders) { - super(reader, childReaders); - this.delegate = reader; - } - - @Nonnull - @Override - public List readList(@Nonnull final InstanceIdentifier id, @Nonnull final ReadContext ctx) - throws ReadFailedException { - LOG.trace("{}: Reading all list entries", this); - final List allIds = delegate.getAllIds(id, ctx); - LOG.debug("{}: Reading list entries for: {}", this, allIds); - - // Override read list in order to perform readCurrent + readChildren here - final ArrayList allEntries = new ArrayList<>(allIds.size()); - for (K key : allIds) { - final InstanceIdentifier.IdentifiableItem currentBdItem = RWUtils.getCurrentIdItem(id, key); - final InstanceIdentifier keyedId = RWUtils.replaceLastInId(id, currentBdItem); - final Optional read = readCurrent(keyedId, ctx); - if (read.isPresent()) { - final DataObject singleItem = read.get(); - checkArgument(getManagedDataObjectType().getTargetType().isAssignableFrom(singleItem.getClass())); - allEntries.add(getManagedDataObjectType().getTargetType().cast(singleItem)); - } - } - return allEntries; - } - - @Override - public void init(final DataBroker broker, final InstanceIdentifier id, final ReadContext ctx) - throws InitFailedException { - try { - final List allIds = delegate.getAllIds(id, ctx); - for (K key : allIds) { - super.init(broker, RWUtils.replaceLastInId(id, RWUtils.getCurrentIdItem(id, key)), ctx); - } - } catch (ReadFailedException e) { - throw new InitFailedException(id, e); - } - } - - @Override - public void merge(@Nonnull final Builder builder, @Nonnull final List readData) { - delegate.merge(builder, readData); - } - - @Override - public List getAllIds(@Nonnull final InstanceIdentifier id, - @Nonnull final ReadContext ctx) throws ReadFailedException { - return delegate.getAllIds(id, ctx); - } - } -} diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistry.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistry.java deleted file mode 100644 index 9505a7730..000000000 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistry.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Copyright (c) 2016 Cisco and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.fd.honeycomb.translate.util.read.registry; - -import static com.google.common.base.Preconditions.checkNotNull; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Optional; -import com.google.common.collect.Iterables; -import com.google.common.collect.LinkedListMultimap; -import com.google.common.collect.Multimap; -import io.fd.honeycomb.translate.read.InitFailedException; -import io.fd.honeycomb.translate.read.Initializer; -import io.fd.honeycomb.translate.read.ListReader; -import io.fd.honeycomb.translate.read.ReadContext; -import io.fd.honeycomb.translate.read.ReadFailedException; -import io.fd.honeycomb.translate.read.Reader; -import io.fd.honeycomb.translate.read.registry.ReaderRegistry; -import io.fd.honeycomb.translate.util.RWUtils; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; -import javax.annotation.Nonnull; -import org.opendaylight.controller.md.sal.binding.api.DataBroker; -import org.opendaylight.yangtools.concepts.Builder; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Simple reader registry able to perform and aggregated read (ROOT read) on top of all provided readers. Also able to - * delegate a specific read to one of the delegate readers. - *

- * This could serve as a utility to hold & hide all available readers in upper layers. - */ -public final class CompositeReaderRegistry implements ReaderRegistry { - - private static final Logger LOG = LoggerFactory.getLogger(CompositeReaderRegistry.class); - - private final Map, Reader>> rootReaders; - - /** - * Create new {@link CompositeReaderRegistry}. - * - * @param rootReaders List of delegate readers - */ - public CompositeReaderRegistry(@Nonnull final List>> rootReaders) { - this.rootReaders = RWUtils.uniqueLinkedIndex(checkNotNull(rootReaders), RWUtils.MANAGER_CLASS_FUNCTION); - } - - @VisibleForTesting - Map, Reader>> getRootReaders() { - return rootReaders; - } - - @Override - @Nonnull - public Multimap, ? extends DataObject> readAll( - @Nonnull final ReadContext ctx) throws ReadFailedException { - - LOG.debug("Reading from all delegates: {}", this); - LOG.trace("Reading from all delegates: {}", rootReaders.values()); - - final Multimap, DataObject> objects = LinkedListMultimap.create(); - for (Reader> rootReader : rootReaders.values()) { - LOG.debug("Reading from delegate: {}", rootReader); - - if (rootReader instanceof ListReader) { - final List listEntries = - ((ListReader) rootReader).readList(rootReader.getManagedDataObjectType(), ctx); - if (!listEntries.isEmpty()) { - objects.putAll(rootReader.getManagedDataObjectType(), listEntries); - } - } else { - final Optional read = rootReader.read(rootReader.getManagedDataObjectType(), ctx); - if (read.isPresent()) { - objects.putAll(rootReader.getManagedDataObjectType(), Collections.singletonList(read.get())); - } - } - } - - return objects; - } - - - @Override - public void initAll(@Nonnull final DataBroker broker, @Nonnull final ReadContext ctx) throws InitFailedException { - for (Reader> rootReader : rootReaders.values()) { - if (rootReader instanceof Initializer) { - ((Initializer) rootReader).init(broker, rootReader.getManagedDataObjectType(), ctx); - } - } - } - - @Nonnull - @Override - public Optional read(@Nonnull final InstanceIdentifier id, - @Nonnull final ReadContext ctx) - throws ReadFailedException { - final InstanceIdentifier.PathArgument first = checkNotNull( - Iterables.getFirst(id.getPathArguments(), null), "Empty id"); - final Reader> reader = rootReaders.get(first.getType()); - checkNotNull(reader, - "Unable to read %s. Missing reader. Current readers for: %s", id, rootReaders.keySet()); - LOG.debug("Reading from delegate: {}", reader); - return reader.read(id, ctx); - } - - @Override - public String toString() { - return getClass().getSimpleName() - + rootReaders.keySet().stream().map(Class::getSimpleName).collect(Collectors.toList()); - } - -} diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryBuilder.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryBuilder.java deleted file mode 100644 index da9bbe934..000000000 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryBuilder.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (c) 2016 Cisco and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.fd.honeycomb.translate.util.read.registry; - -import com.google.common.collect.ImmutableMap; -import io.fd.honeycomb.translate.read.InitReader; -import io.fd.honeycomb.translate.read.Reader; -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 io.fd.honeycomb.translate.util.read.ReflexiveReader; -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.InstanceIdentifier; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -@NotThreadSafe -public final class CompositeReaderRegistryBuilder - extends AbstractSubtreeManagerRegistryBuilderBuilder>, ReaderRegistry> - implements ModifiableReaderRegistryBuilder, ReaderRegistryBuilder { - - private static final Logger LOG = LoggerFactory.getLogger(CompositeReaderRegistryBuilder.class); - - @Override - protected Reader> getSubtreeHandler(@Nonnull final Set> handledChildren, - @Nonnull final Reader> reader) { - return reader instanceof InitReader - ? InitSubtreeReader.createForReader(handledChildren, reader) - : SubtreeReader.createForReader(handledChildren, reader); - } - - @Override - public void addStructuralReader(@Nonnull InstanceIdentifier id, - @Nonnull Class> builderType) { - add(new ReflexiveReader<>(id, builderType)); - } - - - - /** - * Create {@link CompositeReaderRegistry} with Readers ordered according to submitted relationships. - *

- * Note: The ordering only applies between nodes on the same level, inter-level and inter-subtree relationships are - * ignored. - */ - @Override - public ReaderRegistry build() { - ImmutableMap, Reader>> mappedReaders = - getMappedHandlers(); - LOG.debug("Building Reader registry with Readers: {}", - mappedReaders.keySet().stream() - .map(InstanceIdentifier::getTargetType) - .map(Class::getSimpleName) - .collect(Collectors.joining(", "))); - - LOG.trace("Building Reader registry with Readers: {}", mappedReaders); - final List> readerOrder = new ArrayList<>(mappedReaders.keySet()); - - // Wrap readers into composite readers recursively, collect roots and create registry - final TypeHierarchy typeHierarchy = TypeHierarchy.create(mappedReaders.keySet()); - final List>> orderedRootReaders = - typeHierarchy.getRoots().stream() - .map(rootId -> toCompositeReader(rootId, mappedReaders, typeHierarchy)) - .collect(Collectors.toList()); - - // We are violating the ordering from mappedReaders, since we are forming a composite structure - // but at least order root writers - orderedRootReaders.sort((reader1, reader2) -> readerOrder.indexOf(reader1.getManagedDataObjectType()) - - readerOrder.indexOf(reader2.getManagedDataObjectType())); - - return new CompositeReaderRegistry(orderedRootReaders); - } - - private Reader> toCompositeReader( - final InstanceIdentifier instanceIdentifier, - final ImmutableMap, Reader>> mappedReaders, - final TypeHierarchy typeHierarchy) { - - // Order child readers according to the mappedReadersCollection - final ImmutableMap.Builder, Reader>> childReadersMapB = ImmutableMap.builder(); - for (InstanceIdentifier childId : mappedReaders.keySet()) { - if (typeHierarchy.getDirectChildren(instanceIdentifier).contains(childId)) { - childReadersMapB.put(childId.getTargetType(), toCompositeReader(childId, mappedReaders, typeHierarchy)); - } - } - - final ImmutableMap, Reader>> childReadersMap = childReadersMapB.build(); - return childReadersMap.isEmpty() - ? mappedReaders.get(instanceIdentifier) - : CompositeReader.createForReader(mappedReaders.get(instanceIdentifier), childReadersMap); - } -} diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/InitSubtreeReader.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/InitSubtreeReader.java deleted file mode 100644 index 4edc38f9d..000000000 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/InitSubtreeReader.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (c) 2016 Cisco and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.fd.honeycomb.translate.util.read.registry; - -import io.fd.honeycomb.translate.read.InitFailedException; -import io.fd.honeycomb.translate.read.InitListReader; -import io.fd.honeycomb.translate.read.InitReader; -import io.fd.honeycomb.translate.read.ListReader; -import io.fd.honeycomb.translate.read.ReadContext; -import io.fd.honeycomb.translate.read.Reader; -import java.util.Set; -import javax.annotation.Nonnull; -import org.opendaylight.controller.md.sal.binding.api.DataBroker; -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.Identifier; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; - -class InitSubtreeReader> - extends SubtreeReader - implements InitReader { - - private InitSubtreeReader(final InitReader delegate, - final Set> handledTypes) { - super(delegate, handledTypes); - } - - @Override - public void init(final DataBroker broker, final InstanceIdentifier id, final ReadContext ctx) throws InitFailedException { - ((InitReader) delegate).init(broker, id, ctx); - } - - /** - * Wrap a Reader as an initializing subtree Reader. - */ - static > Reader createForReader(@Nonnull final Set> handledChildren, - @Nonnull final Reader reader) { - return (reader instanceof ListReader) - ? new InitSubtreeListReader<>((InitListReader) reader, handledChildren) - : new InitSubtreeReader<>(((InitReader) reader), handledChildren); - } - - private static class InitSubtreeListReader, B extends Builder, K extends Identifier> - extends SubtreeListReader - implements InitListReader { - - InitSubtreeListReader(final InitListReader delegate, - final Set> handledTypes) { - super(delegate, handledTypes); - } - - @Override - public void init(final DataBroker broker, final InstanceIdentifier id, final ReadContext ctx) throws InitFailedException { - ((InitListReader) delegate).init(broker, id, ctx); - } - } -} diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/SubtreeReader.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/SubtreeReader.java deleted file mode 100644 index 3bc76b19a..000000000 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/SubtreeReader.java +++ /dev/null @@ -1,241 +0,0 @@ -/* - * Copyright (c) 2016 Cisco and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.fd.honeycomb.translate.util.read.registry; - -import static com.google.common.base.Preconditions.checkArgument; - -import com.google.common.base.Optional; -import com.google.common.collect.Iterables; -import io.fd.honeycomb.translate.read.ListReader; -import io.fd.honeycomb.translate.read.ReadContext; -import io.fd.honeycomb.translate.read.ReadFailedException; -import io.fd.honeycomb.translate.read.Reader; -import io.fd.honeycomb.translate.util.RWUtils; -import io.fd.honeycomb.translate.util.ReflectionUtils; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import javax.annotation.Nonnull; -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.Identifier; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Simple Reader delegate for subtree Readers (Readers handling also children nodes) providing a list of all the - * children nodes being handled. - */ -class SubtreeReader> implements Reader { - - private static final Logger LOG = LoggerFactory.getLogger(SubtreeReader.class); - - protected final Reader delegate; - private final Set> handledChildTypes = new HashSet<>(); - - SubtreeReader(final Reader delegate, Set> handledTypes) { - this.delegate = delegate; - for (InstanceIdentifier handledType : handledTypes) { - // Iid has to start with Reader's handled root type - checkArgument(delegate.getManagedDataObjectType().getTargetType().equals( - handledType.getPathArguments().iterator().next().getType()), - "Handled node from subtree has to be identified by an instance identifier starting from: %s." - + "Instance identifier was: %s", getManagedDataObjectType().getTargetType(), handledType); - checkArgument(Iterables.size(handledType.getPathArguments()) > 1, - "Handled node from subtree identifier too short: %s", handledType); - handledChildTypes.add(InstanceIdentifier.create(Iterables.concat( - getManagedDataObjectType().getPathArguments(), Iterables.skip(handledType.getPathArguments(), 1)))); - } - } - - /** - * Return set of types also handled by this Reader. All of the types are children of the type managed by this Reader - * excluding the type of this Reader. - */ - Set> getHandledChildTypes() { - return handledChildTypes; - } - - @Override - @Nonnull - public Optional read( - @Nonnull final InstanceIdentifier id, - @Nonnull final ReadContext ctx) throws ReadFailedException { - final InstanceIdentifier wildcarded = RWUtils.makeIidWildcarded(id); - - // Reading entire subtree and filtering if is current reader responsible - if (getHandledChildTypes().contains(wildcarded)) { - LOG.debug("{}: Subtree node managed by this writer requested: {}. Reading current and filtering", this, id); - // If there's no dedicated reader, use read current - final InstanceIdentifier currentId = RWUtils.cutId(id, getManagedDataObjectType()); - final Optional current = delegate.read(currentId, ctx); - // then perform post-reading filtering (return only requested sub-node) - final Optional readSubtree = current.isPresent() - ? filterSubtree(current.get(), id, getManagedDataObjectType().getTargetType()) - : current; - - LOG.debug("{}: Subtree: {} read successfully. Result: {}", this, id, readSubtree); - return readSubtree; - - // If child that's handled here is not requested, then delegate should be able to handle the read - } else { - return delegate.read(id, ctx); - } - } - - @Override - public void readCurrentAttributes(@Nonnull final InstanceIdentifier id, @Nonnull final B builder, - @Nonnull final ReadContext ctx) - throws ReadFailedException { - delegate.readCurrentAttributes(id, builder, ctx); - } - - @Nonnull - @Override - public B getBuilder(final InstanceIdentifier id) { - return delegate.getBuilder(id); - } - - @Override - public void merge(@Nonnull final Builder parentBuilder, @Nonnull final D readValue) { - delegate.merge(parentBuilder, readValue); - } - - @Nonnull - private static Optional filterSubtree(@Nonnull final DataObject parent, - @Nonnull final InstanceIdentifier absolutPath, - @Nonnull final Class managedType) { - final InstanceIdentifier.PathArgument nextId = - RWUtils.getNextId(absolutPath, InstanceIdentifier.create(parent.getClass())); - - final Optional nextParent = findNextParent(parent, nextId, managedType); - - if (Iterables.getLast(absolutPath.getPathArguments()).equals(nextId)) { - return nextParent; // we found the dataObject identified by absolutePath - } else if (nextParent.isPresent()) { - return filterSubtree(nextParent.get(), absolutPath, nextId.getType()); - } else { - return nextParent; // we can't go further, return Optional.absent() - } - } - - private static Optional findNextParent(@Nonnull final DataObject parent, - @Nonnull final InstanceIdentifier.PathArgument nextId, - @Nonnull final Class managedType) { - Optional method = ReflectionUtils.findMethodReflex(managedType, "get", - Collections.emptyList(), nextId.getType()); - - if (method.isPresent()) { - return Optional.fromNullable(filterSingle(parent, nextId, method.get())); - } else { - // List child nodes - method = ReflectionUtils.findMethodReflex(managedType, - "get" + nextId.getType().getSimpleName(), Collections.emptyList(), List.class); - - if (method.isPresent()) { - return filterList(parent, nextId, method.get()); - } else { - throw new IllegalStateException( - "Unable to filter " + nextId + " from " + parent + " getters not found using reflexion"); - } - } - } - - @SuppressWarnings("unchecked") - private static Optional filterList(final DataObject parent, - final InstanceIdentifier.PathArgument nextId, - final Method method) { - final List invoke = (List) invoke(method, nextId, parent); - - checkArgument(nextId instanceof InstanceIdentifier.IdentifiableItem, - "Unable to perform wildcarded read for %s", nextId); - final Identifier key = ((InstanceIdentifier.IdentifiableItem) nextId).getKey(); - - final Method keyGetter = ReflectionUtils.findMethodReflex(nextId.getType(), "get", - Collections.emptyList(), key.getClass()).get(); - - return Optional.fromNullable(invoke.stream() - .filter(item -> key.equals(invoke(keyGetter, nextId, item))) - .findFirst().orElse(null)); - } - - private static DataObject filterSingle(final DataObject parent, - final InstanceIdentifier.PathArgument nextId, final Method method) { - return nextId.getType().cast(invoke(method, nextId, parent)); - } - - private static Object invoke(final Method method, - final InstanceIdentifier.PathArgument nextId, final DataObject parent) { - try { - return method.invoke(parent); - } catch (IllegalAccessException | InvocationTargetException e) { - throw new IllegalArgumentException("Unable to get " + nextId + " from " + parent, e); - } - } - - @Override - @Nonnull - public InstanceIdentifier getManagedDataObjectType() { - return delegate.getManagedDataObjectType(); - } - - /** - * Wrap a Reader as a subtree Reader. - */ - static > Reader createForReader(@Nonnull final Set> handledChildren, - @Nonnull final Reader reader) { - return (reader instanceof ListReader) - ? new SubtreeListReader<>((ListReader) reader, handledChildren) - : new SubtreeReader<>(reader, handledChildren); - } - - static class SubtreeListReader, B extends Builder, K extends Identifier> - extends SubtreeReader implements ListReader { - - final ListReader delegate; - - SubtreeListReader(final ListReader delegate, - final Set> handledTypes) { - super(delegate, handledTypes); - this.delegate = delegate; - } - - @Nonnull - @Override - public List readList(@Nonnull final InstanceIdentifier id, @Nonnull final ReadContext ctx) - throws ReadFailedException { - return delegate.readList(id, ctx); - } - - @Override - public void merge(@Nonnull final Builder builder, @Nonnull final List readData) { - delegate.merge(builder, readData); - } - - @Override - public List getAllIds(@Nonnull final InstanceIdentifier id, - @Nonnull final ReadContext ctx) throws ReadFailedException { - return delegate.getAllIds(id, ctx); - } - } - -} diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/TypeHierarchy.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/TypeHierarchy.java deleted file mode 100644 index a30663221..000000000 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/TypeHierarchy.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright (c) 2016 Cisco and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.fd.honeycomb.translate.util.read.registry; - -import static com.google.common.base.Preconditions.checkArgument; - -import com.google.common.collect.Iterables; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; -import java.util.stream.Collectors; -import javax.annotation.Nonnull; -import org.jgrapht.experimental.dag.DirectedAcyclicGraph; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; - -final class TypeHierarchy { - private final DirectedAcyclicGraph, Parent> hierarchy; - - private TypeHierarchy(@Nonnull final DirectedAcyclicGraph, Parent> hierarchy) { - this.hierarchy = hierarchy; - } - - Set> getAllChildren(InstanceIdentifier id) { - final HashSet> instanceIdentifiers = new HashSet<>(); - for (InstanceIdentifier childId : getDirectChildren(id)) { - instanceIdentifiers.add(childId); - instanceIdentifiers.addAll(getAllChildren(childId)); - } - return instanceIdentifiers; - } - - Set> getDirectChildren(InstanceIdentifier id) { - checkArgument(hierarchy.vertexSet().contains(id), - "Unknown reader: %s. Known readers: %s", id, hierarchy.vertexSet()); - - return hierarchy.outgoingEdgesOf(id).stream() - .map(hierarchy::getEdgeTarget) - .collect(Collectors.toSet()); - } - - Set> getRoots() { - return hierarchy.vertexSet().stream() - .filter(vertex -> hierarchy.incomingEdgesOf(vertex).size() == 0) - .collect(Collectors.toSet()); - } - - /** - * Create reader hierarchy from a flat set of instance identifiers. - * - * @param allIds Set of unkeyed instance identifiers - */ - static TypeHierarchy create(@Nonnull Set> allIds) { - final DirectedAcyclicGraph, Parent> - readersHierarchy = new DirectedAcyclicGraph<>((sourceVertex, targetVertex) -> new Parent()); - - for (InstanceIdentifier allId : allIds) { - checkArgument(!Iterables.isEmpty(allId.getPathArguments()), "Empty ID detected"); - - if (Iterables.size(allId.getPathArguments()) == 1) { - readersHierarchy.addVertex(allId); - } - - List pathArgs = new LinkedList<>(); - pathArgs.add(allId.getPathArguments().iterator().next()); - - for (InstanceIdentifier.PathArgument pathArgument : Iterables.skip(allId.getPathArguments(), 1)) { - final InstanceIdentifier previous = InstanceIdentifier.create(pathArgs); - pathArgs.add(pathArgument); - final InstanceIdentifier current = InstanceIdentifier.create(pathArgs); - - readersHierarchy.addVertex(previous); - readersHierarchy.addVertex(current); - - try { - readersHierarchy.addDagEdge(previous, current); - } catch (DirectedAcyclicGraph.CycleFoundException e) { - throw new IllegalArgumentException("Loop in hierarchy detected", e); - } - } - } - - return new TypeHierarchy(readersHierarchy); - } - - private static final class Parent{} -} diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/write/registry/FlatWriterRegistry.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/write/registry/FlatWriterRegistry.java deleted file mode 100644 index e5b829d6a..000000000 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/write/registry/FlatWriterRegistry.java +++ /dev/null @@ -1,314 +0,0 @@ -/* - * Copyright (c) 2016 Cisco and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.fd.honeycomb.translate.util.write.registry; - -import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkNotNull; -import static io.fd.honeycomb.translate.util.RWUtils.makeIidWildcarded; - -import com.google.common.base.Optional; -import com.google.common.collect.HashMultimap; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Lists; -import com.google.common.collect.Multimap; -import com.google.common.collect.Sets; -import io.fd.honeycomb.translate.TranslationException; -import io.fd.honeycomb.translate.util.RWUtils; -import io.fd.honeycomb.translate.write.DataObjectUpdate; -import io.fd.honeycomb.translate.write.WriteContext; -import io.fd.honeycomb.translate.write.WriteFailedException; -import io.fd.honeycomb.translate.write.Writer; -import io.fd.honeycomb.translate.write.registry.WriterRegistry; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; -import java.util.stream.Collectors; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import javax.annotation.concurrent.ThreadSafe; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Flat writer registry, delegating updates to writers in the order writers were submitted. - */ -@ThreadSafe -final class FlatWriterRegistry implements WriterRegistry { - - private static final Logger LOG = LoggerFactory.getLogger(FlatWriterRegistry.class); - - // All types handled by writers directly or as children - private final ImmutableSet> handledTypes; - - private final Set> writersOrderReversed; - private final Set> writersOrder; - private final Map, Writer> writers; - - /** - * Create flat registry instance. - * - * @param writers immutable, ordered map of writers to use to process updates. Order of the writers has to be - * one in which create and update operations should be handled. Deletes will be handled in reversed - * order. All deletes are handled before handling all the updates. - */ - FlatWriterRegistry(@Nonnull final ImmutableMap, Writer> writers) { - this.writers = writers; - this.writersOrderReversed = Sets.newLinkedHashSet(Lists.reverse(Lists.newArrayList(writers.keySet()))); - this.writersOrder = writers.keySet(); - this.handledTypes = getAllHandledTypes(writers); - } - - private static ImmutableSet> getAllHandledTypes( - @Nonnull final ImmutableMap, Writer> writers) { - final ImmutableSet.Builder> handledTypesBuilder = ImmutableSet.builder(); - for (Map.Entry, Writer> writerEntry : writers.entrySet()) { - final InstanceIdentifier writerType = writerEntry.getKey(); - final Writer writer = writerEntry.getValue(); - handledTypesBuilder.add(writerType); - if (writer instanceof SubtreeWriter) { - handledTypesBuilder.addAll(((SubtreeWriter) writer).getHandledChildTypes()); - } - } - return handledTypesBuilder.build(); - } - - @Override - public void update(@Nonnull final DataObjectUpdates updates, - @Nonnull final WriteContext ctx) throws TranslationException { - if (updates.isEmpty()) { - return; - } - - // Optimization - if (updates.containsOnlySingleType()) { - // First process delete - singleUpdate(updates.getDeletes(), ctx); - // Next is update - singleUpdate(updates.getUpdates(), ctx); - } else { - // First process deletes - bulkUpdate(updates.getDeletes(), ctx, true, writersOrderReversed); - // Next are updates - bulkUpdate(updates.getUpdates(), ctx, true, writersOrder); - } - - LOG.debug("Update successful for types: {}", updates.getTypeIntersection()); - LOG.trace("Update successful for: {}", updates); - } - - private void singleUpdate(@Nonnull final Multimap, ? extends DataObjectUpdate> updates, - @Nonnull final WriteContext ctx) throws WriteFailedException { - if (updates.isEmpty()) { - return; - } - - final InstanceIdentifier singleType = updates.keySet().iterator().next(); - LOG.debug("Performing single type update for: {}", singleType); - Collection singleTypeUpdates = updates.get(singleType); - Writer writer = getWriter(singleType); - - if (writer == null) { - // This node must be handled by a subtree writer, find it and call it or else fail - checkArgument(handledTypes.contains(singleType), "Unable to process update. Missing writers for: %s", - singleType); - writer = getSubtreeWriterResponsible(singleType); - singleTypeUpdates = getParentDataObjectUpdate(ctx, updates, writer); - } - - LOG.trace("Performing single type update with writer: {}", writer); - for (DataObjectUpdate singleUpdate : singleTypeUpdates) { - writer.update(singleUpdate.getId(), singleUpdate.getDataBefore(), singleUpdate.getDataAfter(), ctx); - } - } - - private Writer getSubtreeWriterResponsible(final InstanceIdentifier singleType) { - return writers.values().stream() - .filter(w -> w instanceof SubtreeWriter) - .filter(w -> ((SubtreeWriter) w).getHandledChildTypes().contains(singleType)) - .findFirst() - .get(); - } - - private Collection getParentDataObjectUpdate(final WriteContext ctx, - final Multimap, ? extends DataObjectUpdate> updates, - final Writer writer) { - // Now read data for subtree reader root, but first keyed ID is needed and that ID can be cut from updates - InstanceIdentifier firstAffectedChildId = ((SubtreeWriter) writer).getHandledChildTypes().stream() - .filter(updates::containsKey) - .map(unkeyedId -> updates.get(unkeyedId)) - .flatMap(doUpdates -> doUpdates.stream()) - .map(DataObjectUpdate::getId) - .findFirst() - .get(); - - final InstanceIdentifier parentKeyedId = - RWUtils.cutId(firstAffectedChildId, writer.getManagedDataObjectType()); - - final Optional parentBefore = ctx.readBefore(parentKeyedId); - final Optional parentAfter = ctx.readAfter(parentKeyedId); - return Collections.singleton( - DataObjectUpdate.create(parentKeyedId, parentBefore.orNull(), parentAfter.orNull())); - } - - private void bulkUpdate(@Nonnull final Multimap, ? extends DataObjectUpdate> updates, - @Nonnull final WriteContext ctx, - final boolean attemptRevert, - @Nonnull final Set> writersOrder) throws BulkUpdateException { - if (updates.isEmpty()) { - return; - } - - LOG.debug("Performing bulk update with revert attempt: {} for: {}", attemptRevert, updates.keySet()); - - // Check that all updates can be handled - checkAllTypesCanBeHandled(updates); - - // Capture all changes successfully processed in case revert is needed - final Set> processedNodes = new HashSet<>(); - - // Iterate over all writers and call update if there are any related updates - for (InstanceIdentifier writerType : writersOrder) { - Collection writersData = updates.get(writerType); - final Writer writer = getWriter(writerType); - - if (writersData.isEmpty()) { - // If there are no data for current writer, but it is a SubtreeWriter and there are updates to - // its children, still invoke it with its root data - if (writer instanceof SubtreeWriter && isAffected(((SubtreeWriter) writer), updates)) { - // Provide parent data for SubtreeWriter for further processing - writersData = getParentDataObjectUpdate(ctx, updates, writer); - } else { - // Skipping unaffected writer - // Alternative to this would be modification sort according to the order of writers - continue; - } - } - - LOG.debug("Performing update for: {}", writerType); - LOG.trace("Performing update with writer: {}", writer); - - for (DataObjectUpdate singleUpdate : writersData) { - try { - writer.update(singleUpdate.getId(), singleUpdate.getDataBefore(), singleUpdate.getDataAfter(), ctx); - processedNodes.add(singleUpdate.getId()); - LOG.trace("Update successful for type: {}", writerType); - LOG.debug("Update successful for: {}", singleUpdate); - } catch (Exception e) { - // do not log this exception here,its logged in ModifiableDataTreeDelegator - - final Reverter reverter = attemptRevert - ? new ReverterImpl(processedNodes, updates, writersOrder) - : (final WriteContext writeContext) -> {};//NOOP reverter - - // Find out which changes left unprocessed - final Set> unprocessedChanges = updates.values().stream() - .map(DataObjectUpdate::getId) - .filter(id -> !processedNodes.contains(id)) - .collect(Collectors.toSet()); - throw new BulkUpdateException(unprocessedChanges, reverter, e); - } - } - } - } - - private void checkAllTypesCanBeHandled( - @Nonnull final Multimap, ? extends DataObjectUpdate> updates) { - if (!handledTypes.containsAll(updates.keySet())) { - final Sets.SetView> missingWriters = Sets.difference(updates.keySet(), handledTypes); - LOG.warn("Unable to process update. Missing writers for: {}", missingWriters); - throw new IllegalArgumentException("Unable to process update. Missing writers for: " + missingWriters); - } - } - - /** - * Check whether {@link SubtreeWriter} is affected by the updates. - * - * @return true if there are any updates to SubtreeWriter's child nodes (those marked by SubtreeWriter - * as being taken care of) - * */ - private static boolean isAffected(final SubtreeWriter writer, - final Multimap, ? extends DataObjectUpdate> updates) { - return !Sets.intersection(writer.getHandledChildTypes(), updates.keySet()).isEmpty(); - } - - @Nullable - private Writer getWriter(@Nonnull final InstanceIdentifier singleType) { - return writers.get(singleType); - } - - private final class ReverterImpl implements Reverter { - - private final Collection> processedNodes; - private final Multimap, ? extends DataObjectUpdate> updates; - private final Set> revertDeleteOrder; - - ReverterImpl(final Collection> processedNodes, - final Multimap, ? extends DataObjectUpdate> updates, - final Set> writersOrderOriginal) { - this.processedNodes = processedNodes; - this.updates = updates; - // Use opposite ordering when executing revert - this.revertDeleteOrder = writersOrderOriginal == FlatWriterRegistry.this.writersOrder - ? FlatWriterRegistry.this.writersOrderReversed - : FlatWriterRegistry.this.writersOrder; - } - - @Override - public void revert(@Nonnull final WriteContext writeContext) throws RevertFailedException { - checkNotNull(writeContext, "Cannot revert changes for null context"); - - Multimap, DataObjectUpdate> updatesToRevert = - filterAndRevertProcessed(updates, processedNodes); - - LOG.info("Attempting revert for changes: {}", updatesToRevert); - try { - // Perform reversed bulk update without revert attempt - bulkUpdate(updatesToRevert, writeContext, true, revertDeleteOrder); - LOG.info("Revert successful"); - } catch (BulkUpdateException e) { - LOG.error("Revert failed", e); - throw new RevertFailedException(e.getFailedIds(), e); - } - } - - /** - * Create new updates map, but only keep already processed changes. Switching before and after data for each - * update. - */ - private Multimap, DataObjectUpdate> filterAndRevertProcessed( - final Multimap, ? extends DataObjectUpdate> updates, - final Collection> processedNodes) { - final Multimap, DataObjectUpdate> filtered = HashMultimap.create(); - for (InstanceIdentifier processedNode : processedNodes) { - final InstanceIdentifier wildcardedIid = makeIidWildcarded(processedNode); - if (updates.containsKey(wildcardedIid)) { - updates.get(wildcardedIid).stream() - .filter(dataObjectUpdate -> processedNode.contains(dataObjectUpdate.getId())) - // putting under unkeyed identifier, to prevent failing of checkAllTypesCanBeHandled - .forEach(dataObjectUpdate -> filtered.put(wildcardedIid, dataObjectUpdate.reverse())); - } - } - return filtered; - } - } - -} diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/write/registry/FlatWriterRegistryBuilder.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/write/registry/FlatWriterRegistryBuilder.java deleted file mode 100644 index 0f75de7e7..000000000 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/write/registry/FlatWriterRegistryBuilder.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (c) 2016 Cisco and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.fd.honeycomb.translate.util.write.registry; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.collect.ImmutableMap; -import io.fd.honeycomb.translate.write.registry.WriterRegistryBuilder; -import io.fd.honeycomb.translate.util.AbstractSubtreeManagerRegistryBuilderBuilder; -import io.fd.honeycomb.translate.write.Writer; -import io.fd.honeycomb.translate.write.registry.ModifiableWriterRegistryBuilder; -import io.fd.honeycomb.translate.write.registry.WriterRegistry; -import java.util.Set; -import java.util.stream.Collectors; -import javax.annotation.Nonnull; -import javax.annotation.concurrent.NotThreadSafe; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Builder for {@link FlatWriterRegistry} allowing users to specify inter-writer relationships. - */ -@NotThreadSafe -public final class FlatWriterRegistryBuilder - extends AbstractSubtreeManagerRegistryBuilderBuilder, WriterRegistry> - implements ModifiableWriterRegistryBuilder, WriterRegistryBuilder { - - private static final Logger LOG = LoggerFactory.getLogger(FlatWriterRegistryBuilder.class); - - @Override - protected Writer getSubtreeHandler(final @Nonnull Set> handledChildren, - final @Nonnull Writer writer) { - return SubtreeWriter.createForWriter(handledChildren, writer); - } - - /** - * Create FlatWriterRegistry with writers ordered according to submitted relationships. - */ - @Override - public WriterRegistry build() { - final ImmutableMap, Writer> mappedWriters = getMappedHandlers(); - LOG.debug("Building writer registry with writers: {}", - mappedWriters.keySet().stream() - .map(InstanceIdentifier::getTargetType) - .map(Class::getSimpleName) - .collect(Collectors.joining(", "))); - LOG.trace("Building writer registry with writers: {}", mappedWriters); - return new FlatWriterRegistry(mappedWriters); - } - - @VisibleForTesting - @Override - protected ImmutableMap, Writer> getMappedHandlers() { - return super.getMappedHandlers(); - } -} diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/write/registry/SubtreeWriter.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/write/registry/SubtreeWriter.java deleted file mode 100644 index bab1da16f..000000000 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/write/registry/SubtreeWriter.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright (c) 2016 Cisco and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.fd.honeycomb.translate.util.write.registry; - -import static com.google.common.base.Preconditions.checkArgument; - -import com.google.common.collect.Iterables; -import io.fd.honeycomb.translate.write.WriteContext; -import io.fd.honeycomb.translate.write.WriteFailedException; -import io.fd.honeycomb.translate.write.Writer; -import java.util.HashSet; -import java.util.Set; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; - -/** - * Simple writer delegate for subtree writers (writers handling also children nodes) providing a list of all the - * children nodes being handled. - */ -final class SubtreeWriter implements Writer { - - private final Writer delegate; - private final Set> handledChildTypes = new HashSet<>(); - - private SubtreeWriter(final Writer delegate, Set> handledTypes) { - this.delegate = delegate; - for (InstanceIdentifier handledType : handledTypes) { - // Iid has to start with writer's handled root type - checkArgument(delegate.getManagedDataObjectType().getTargetType().equals( - handledType.getPathArguments().iterator().next().getType()), - "Handled node from subtree has to be identified by an instance identifier starting from: %s." - + "Instance identifier was: %s", getManagedDataObjectType().getTargetType(), handledType); - checkArgument(Iterables.size(handledType.getPathArguments()) > 1, - "Handled node from subtree identifier too short: %s", handledType); - handledChildTypes.add(InstanceIdentifier.create(Iterables.concat( - getManagedDataObjectType().getPathArguments(), Iterables.skip(handledType.getPathArguments(), 1)))); - } - } - - /** - * Return set of types also handled by this writer. All of the types are children of the type managed by this - * writer excluding the type of this writer. - */ - Set> getHandledChildTypes() { - return handledChildTypes; - } - - @Override - public void update( - @Nonnull final InstanceIdentifier id, - @Nullable final DataObject dataBefore, - @Nullable final DataObject dataAfter, @Nonnull final WriteContext ctx) throws WriteFailedException { - delegate.update(id, dataBefore, dataAfter, ctx); - } - - @Override - @Nonnull - public InstanceIdentifier getManagedDataObjectType() { - return delegate.getManagedDataObjectType(); - } - - /** - * Wrap a writer as a subtree writer. - */ - static Writer createForWriter(@Nonnull final Set> handledChildren, - @Nonnull final Writer writer) { - return new SubtreeWriter<>(writer, handledChildren); - } -} diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryBuilderTest.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryBuilderTest.java deleted file mode 100644 index d742575be..000000000 --- a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryBuilderTest.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (c) 2016 Cisco and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.fd.honeycomb.translate.util.read.registry; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.when; - -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Lists; -import io.fd.honeycomb.translate.read.Reader; -import io.fd.honeycomb.translate.read.registry.ReaderRegistry; -import io.fd.honeycomb.translate.util.DataObjects; -import java.util.List; -import java.util.Map; -import org.junit.Test; -import org.mockito.Mockito; -import org.opendaylight.yangtools.concepts.Builder; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; - -public class CompositeReaderRegistryBuilderTest { - - private Reader> reader1 = - mock(DataObjects.DataObject1.class); - private Reader> reader2 = - mock(DataObjects.DataObject2.class); - private Reader> reader3 = - mock(DataObjects.DataObject3.class); - private Reader> reader31 = - mock(DataObjects.DataObject3.DataObject31.class); - - private Reader> reader4 = - mock(DataObjects.DataObject4.class); - private Reader> reader41 = - mock(DataObjects.DataObject4.DataObject41.class); - private Reader> reader411 = - mock(DataObjects.DataObject4.DataObject41.DataObject411.class); - private Reader> reader42 = - mock(DataObjects.DataObject4.DataObject42.class); - - @SuppressWarnings("unchecked") - private Reader> mock(final Class dataObjectType) { - final Reader> mock = Mockito.mock(Reader.class); - try { - when(mock.getManagedDataObjectType()) - .thenReturn(((InstanceIdentifier) dataObjectType.getDeclaredField("IID").get(null))); - } catch (IllegalAccessException | NoSuchFieldException e) { - throw new RuntimeException(e); - } - return mock; - } - - @Test - public void testCompositeStructure() throws Exception { - final CompositeReaderRegistryBuilder compositeReaderRegistryBuilder = new CompositeReaderRegistryBuilder(); - /* - Composite reader structure ordered left from right - - 1, 2, 3, 4 - 31 42, 41 - 411 - */ - compositeReaderRegistryBuilder.add(reader1); - compositeReaderRegistryBuilder.addAfter(reader2, reader1.getManagedDataObjectType()); - compositeReaderRegistryBuilder.addAfter(reader3, reader2.getManagedDataObjectType()); - compositeReaderRegistryBuilder.addAfter(reader31, reader1.getManagedDataObjectType()); - compositeReaderRegistryBuilder.addAfter(reader4, reader3.getManagedDataObjectType()); - compositeReaderRegistryBuilder.add(reader41); - compositeReaderRegistryBuilder.addBefore(reader42, reader41.getManagedDataObjectType()); - compositeReaderRegistryBuilder.add(reader411); - - final ReaderRegistry build = compositeReaderRegistryBuilder.build(); - - final Map, Reader>> rootReaders = - ((CompositeReaderRegistry) build).getRootReaders(); - final List> rootReaderOrder = Lists.newArrayList(rootReaders.keySet()); - - assertEquals(reader1.getManagedDataObjectType().getTargetType(), rootReaderOrder.get(0)); - assertEquals(reader2.getManagedDataObjectType().getTargetType(), rootReaderOrder.get(1)); - assertEquals(reader3.getManagedDataObjectType().getTargetType(), rootReaderOrder.get(2)); - assertEquals(reader4.getManagedDataObjectType().getTargetType(), rootReaderOrder.get(3)); - - assertFalse(rootReaders.get(DataObjects.DataObject1.class) instanceof CompositeReader); - assertFalse(rootReaders.get(DataObjects.DataObject2.class) instanceof CompositeReader); - assertTrue(rootReaders.get(DataObjects.DataObject3.class) instanceof CompositeReader); - assertTrue(rootReaders.get(DataObjects.DataObject4.class) instanceof CompositeReader); - - final ImmutableMap, Reader>> childReaders = - ((CompositeReader>) rootReaders - .get(DataObjects.DataObject4.class)).getChildReaders(); - final List> orderedChildReaders = Lists.newArrayList(childReaders.keySet()); - - assertEquals(reader42.getManagedDataObjectType().getTargetType(), orderedChildReaders.get(0)); - assertEquals(reader41.getManagedDataObjectType().getTargetType(), orderedChildReaders.get(1)); - assertTrue(childReaders.get(DataObjects.DataObject4.DataObject41.class) instanceof CompositeReader); - assertFalse(childReaders.get(DataObjects.DataObject4.DataObject42.class) instanceof CompositeReader); - } -} \ No newline at end of file diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryTest.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryTest.java deleted file mode 100644 index 06cb8498f..000000000 --- a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryTest.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Copyright (c) 2016 Cisco and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.fd.honeycomb.translate.util.read.registry; - -import static io.fd.honeycomb.translate.util.DataObjects.DataObject3; -import static io.fd.honeycomb.translate.util.DataObjects.DataObject3.DataObject31; -import static io.fd.honeycomb.translate.util.DataObjects.DataObject4; -import static io.fd.honeycomb.translate.util.DataObjects.DataObject4.DataObject41; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.inOrder; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import com.google.common.base.Optional; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Lists; -import io.fd.honeycomb.translate.read.ReadContext; -import io.fd.honeycomb.translate.read.Reader; -import org.junit.Before; -import org.junit.Test; -import org.mockito.InOrder; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.opendaylight.yangtools.concepts.Builder; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; - -public class CompositeReaderRegistryTest { - - @Mock - private ReadContext ctx; - private CompositeReaderRegistry reg; - private Reader> reader31; - private Reader> rootReader3; - private Reader> reader41; - private Reader> rootReader4; - - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - - reader31 = mockReader(DataObject31.class); - rootReader3 = - spy(CompositeReader.createForReader( - mockReader(DataObject3.class), - ImmutableMap.of(DataObject31.class, reader31))); - - reader41 = mockReader(DataObject41.class); - rootReader4 = - spy(CompositeReader.createForReader( - mockReader(DataObject4.class), ImmutableMap.of( - DataObject41.class, reader41))); - - reg = new CompositeReaderRegistry(Lists.newArrayList(rootReader3, rootReader4)); - } - - @Test - public void testReadAll() throws Exception { - reg.readAll(ctx); - - // Invoked according to composite ordering - final InOrder inOrder = inOrder(rootReader3, rootReader4, reader31, reader41); - inOrder.verify(rootReader3).read(any(InstanceIdentifier.class), any(ReadContext.class)); - inOrder.verify(reader31).read(any(InstanceIdentifier.class), any(ReadContext.class)); - inOrder.verify(rootReader4).read(any(InstanceIdentifier.class), any(ReadContext.class)); - inOrder.verify(reader41).read(any(InstanceIdentifier.class), any(ReadContext.class)); - } - - @Test - public void testReadSingleRoot() throws Exception { - reg.read(DataObject3.IID, ctx); - - // Invoked according to composite ordering - final InOrder inOrder = inOrder(rootReader3, rootReader4, reader31, reader41); - inOrder.verify(rootReader3).read(any(InstanceIdentifier.class), any(ReadContext.class)); - inOrder.verify(reader31).read(any(InstanceIdentifier.class), any(ReadContext.class)); - - // Only subtree under DataObject3 should be read - verify(rootReader4, times(0)).read(any(InstanceIdentifier.class), any(ReadContext.class)); - verify(reader41, times(0)).read(any(InstanceIdentifier.class), any(ReadContext.class)); - } - - @SuppressWarnings("unchecked") - static > Reader mockReader(final Class dataType) - throws Exception { - final Reader r = mock(Reader.class); - final Object iid = dataType.getDeclaredField("IID").get(null); - when(r.getManagedDataObjectType()).thenReturn((InstanceIdentifier) iid); - final Builder builder = mock(Builder.class); - when(builder.build()).thenReturn(mock(dataType)); - when(r.getBuilder(any(InstanceIdentifier.class))).thenReturn(builder); - when(r.read(any(InstanceIdentifier.class), any(ReadContext.class))).thenReturn(Optional.of(mock(dataType))); - return (Reader) r; - } -} \ No newline at end of file diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderTest.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderTest.java deleted file mode 100644 index 9ae036013..000000000 --- a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderTest.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Copyright (c) 2016 Cisco and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.fd.honeycomb.translate.util.read.registry; - -import static io.fd.honeycomb.translate.util.DataObjects.DataObject4; -import static io.fd.honeycomb.translate.util.DataObjects.DataObject4.DataObject41; -import static io.fd.honeycomb.translate.util.DataObjects.DataObjectK; -import static io.fd.honeycomb.translate.util.DataObjects.DataObjectKey; -import static io.fd.honeycomb.translate.util.read.registry.CompositeReaderRegistryTest.mockReader; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import com.google.common.base.Optional; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Lists; -import io.fd.honeycomb.translate.read.ListReader; -import io.fd.honeycomb.translate.read.ReadContext; -import io.fd.honeycomb.translate.read.Reader; -import io.fd.honeycomb.translate.util.DataObjects; -import java.util.List; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -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.Identifier; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; - -public class CompositeReaderTest { - - @Mock - private ReadContext ctx; - private Reader> reader41; - private Reader> reader4; - private Reader> compositeReader; - - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - - reader41 = mockReader(DataObject41.class); - reader4 = mockReader(DataObject4.class); - compositeReader = CompositeReader - .createForReader(reader4, ImmutableMap.of(DataObject41.class, reader41)); - } - - @Test - public void testReadCurrent() throws Exception { - compositeReader.read(DataObject4.IID, ctx); - verify(reader4).readCurrentAttributes(eq(DataObject4.IID), any(Builder.class), eq(ctx)); - verify(reader41).read(DataObject41.IID, ctx); - } - - @Test - public void testReadJustChild() throws Exception { - // Delegating read to child - compositeReader.read(DataObject41.IID, ctx); - verify(reader4, times(0)) - .readCurrentAttributes(any(InstanceIdentifier.class), any(Builder.class), any(ReadContext.class)); - verify(reader41).read(DataObject41.IID, ctx); - } - - @Test - public void testReadFallback() throws Exception { - // Delegating read to delegate as a fallback since IID does not fit, could be handled by the delegate if its - // a subtree handler - compositeReader.read(DataObjects.DataObject4.DataObject42.IID, ctx); - verify(reader4).read(DataObjects.DataObject4.DataObject42.IID, ctx); - verify(reader41, times(0)).read(any(InstanceIdentifier.class), any(ReadContext.class)); - } - - @Test - public void testList() throws Exception { - final Reader> readerK1 = - mockReader(DataObjectK.DataObjectK1.class); - final ListReader> readerK = - mockListReader(DataObjectK.class, Lists.newArrayList(new DataObjectKey(), new DataObjectKey())); - final ListReader> - compositeReaderK = (ListReader>) - CompositeReader.createForReader(readerK, ImmutableMap.of(DataObject41.class, readerK1)); - - compositeReaderK.readList(DataObjectK.IID, ctx); - - verify(readerK).getAllIds(DataObjectK.IID, ctx); - verify(readerK, times(2)) - .readCurrentAttributes(any(InstanceIdentifier.class), any(Builder.class), any(ReadContext.class)); - } - - @SuppressWarnings("unchecked") - static , K extends Identifier, B extends Builder> ListReader mockListReader( - final Class dataType, List keys) - throws Exception { - final ListReader r = mock(ListReader.class); - final Object iid = dataType.getDeclaredField("IID").get(null); - when(r.getManagedDataObjectType()).thenReturn((InstanceIdentifier) iid); - final Builder builder = mock(Builder.class); - when(builder.build()).thenReturn(mock(dataType)); - when(r.getBuilder(any(InstanceIdentifier.class))).thenReturn(builder); - when(r.read(any(InstanceIdentifier.class), any(ReadContext.class))).thenReturn(Optional.of(mock(dataType))); - when(r.getAllIds(any(InstanceIdentifier.class), any(ReadContext.class))).thenReturn(keys); - return (ListReader) r; - } - -} \ No newline at end of file diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/SubtreeReaderTest.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/SubtreeReaderTest.java deleted file mode 100644 index 799b9553f..000000000 --- a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/SubtreeReaderTest.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Copyright (c) 2016 Cisco and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.fd.honeycomb.translate.util.read.registry; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.mockito.Mockito.atLeastOnce; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; - -import com.google.common.base.Optional; -import com.google.common.collect.Sets; -import io.fd.honeycomb.translate.read.ReadContext; -import io.fd.honeycomb.translate.read.Reader; -import io.fd.honeycomb.translate.util.DataObjects; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.opendaylight.yangtools.concepts.Builder; -import org.opendaylight.yangtools.yang.binding.ChildOf; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; - -public class SubtreeReaderTest { - - @Mock - private Reader> delegate; - @Mock - private Reader> delegateLocal; - @Mock - private ReadContext ctx; - - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - doReturn(DataObjects.DataObject4.IID).when(delegate).getManagedDataObjectType(); - doReturn(DataObject1.IID).when(delegateLocal).getManagedDataObjectType(); - } - - @Test - public void testCreate() throws Exception { - final Reader> subtreeR = - SubtreeReader.createForReader(Sets.newHashSet(DataObjects.DataObject4.DataObject41.IID), delegate); - - subtreeR.getBuilder(DataObjects.DataObject4.IID); - verify(delegate).getBuilder(DataObjects.DataObject4.IID); - - subtreeR.getManagedDataObjectType(); - verify(delegate, atLeastOnce()).getManagedDataObjectType(); - } - - @Test(expected = IllegalArgumentException.class) - public void testCreateInvalid() throws Exception { - SubtreeReader.createForReader(Sets.newHashSet(DataObjects.DataObject1.IID), delegate); - } - - @Test(expected = IllegalStateException.class) - public void testReadOnlySubtreeCannotFilter() throws Exception { - final Reader> subtreeR = - SubtreeReader.createForReader(Sets.newHashSet(DataObjects.DataObject4.DataObject41.IID), delegate); - - doReturn(Optional.fromNullable(mock(DataObjects.DataObject4.class))).when(delegate).read(DataObjects.DataObject4.IID, ctx); - subtreeR.read(DataObjects.DataObject4.DataObject41.IID, ctx); - } - - @Test - public void testReadOnlySubtreeNotPresent() throws Exception { - final Reader> subtreeR = - SubtreeReader.createForReader(Sets.newHashSet(DataObjects.DataObject4.DataObject41.IID), delegate); - - doReturn(Optional.absent()).when(delegate).read(DataObjects.DataObject4.IID, ctx); - assertFalse(subtreeR.read(DataObjects.DataObject4.DataObject41.IID, ctx).isPresent()); - } - - @Test - public void testReadOnlySubtreeChild() throws Exception { - final Reader> subtreeR = - SubtreeReader.createForReader(Sets.newHashSet(DataObject1.DataObject11.IID), delegateLocal); - - final DataObject1 mock = mock(DataObject1.class); - final DataObject1.DataObject11 mock11 = mock(DataObject1.DataObject11.class); - doReturn(mock11).when(mock).getDataObject11(); - doReturn(Optional.fromNullable(mock)).when(delegateLocal).read(DataObject1.IID, ctx); - assertEquals(mock11, subtreeR.read(DataObject1.DataObject11.IID, ctx).get()); - } - - @Test - public void testReadEntireSubtree() throws Exception { - final Reader> subtreeR = - SubtreeReader.createForReader(Sets.newHashSet(DataObject1.DataObject11.IID), delegateLocal); - - final DataObject1 mock = mock(DataObject1.class); - final DataObject1.DataObject11 mock11 = mock(DataObject1.DataObject11.class); - doReturn(mock11).when(mock).getDataObject11(); - doReturn(Optional.fromNullable(mock)).when(delegateLocal).read(DataObject1.IID, ctx); - assertEquals(mock, subtreeR.read(DataObject1.IID, ctx).get()); - } - - public abstract static class DataObject1 implements DataObject { - public static InstanceIdentifier IID = InstanceIdentifier.create(DataObject1.class); - - public abstract DataObject11 getDataObject11(); - - public abstract static class DataObject11 implements DataObject, ChildOf { - public static InstanceIdentifier IID = DataObject1.IID.child(DataObject11.class); - } - } -} diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/TypeHierarchyTest.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/TypeHierarchyTest.java deleted file mode 100644 index 3b3ea3a35..000000000 --- a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/TypeHierarchyTest.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (c) 2016 Cisco and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.fd.honeycomb.translate.util.read.registry; - -import static org.hamcrest.CoreMatchers.hasItem; -import static org.hamcrest.CoreMatchers.hasItems; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -import com.google.common.collect.Sets; -import io.fd.honeycomb.translate.util.DataObjects; -import org.hamcrest.CoreMatchers; -import org.junit.Test; - -public class TypeHierarchyTest { - - @Test - public void testHierarchy() throws Exception { - final TypeHierarchy typeHierarchy = TypeHierarchy.create(Sets.newHashSet( - DataObjects.DataObject4.DataObject41.DataObject411.IID, - DataObjects.DataObject4.DataObject41.IID,/* Included in previous already */ - DataObjects.DataObject1.IID, - DataObjects.DataObject3.DataObject31.IID)); - - // Roots - assertThat(typeHierarchy.getRoots().size(), is(3)); - assertThat(typeHierarchy.getRoots(), CoreMatchers - .hasItems(DataObjects.DataObject1.IID, DataObjects.DataObject3.IID, DataObjects.DataObject4.IID)); - - // Leaves - assertThat(typeHierarchy.getDirectChildren(DataObjects.DataObject1.IID).size(), is(0)); - assertThat(typeHierarchy.getDirectChildren(DataObjects.DataObject3.DataObject31.IID).size(), is(0)); - assertThat(typeHierarchy.getDirectChildren(DataObjects.DataObject4.DataObject41.DataObject411.IID).size(), is(0)); - - // Intermediate leaves - assertThat(typeHierarchy.getDirectChildren(DataObjects.DataObject3.IID).size(), is(1)); - assertThat(typeHierarchy.getDirectChildren(DataObjects.DataObject3.IID), CoreMatchers - .hasItem(DataObjects.DataObject3.DataObject31.IID)); - assertEquals(typeHierarchy.getDirectChildren(DataObjects.DataObject3.IID), typeHierarchy.getAllChildren( - DataObjects.DataObject3.IID)); - - assertThat(typeHierarchy.getDirectChildren(DataObjects.DataObject4.DataObject41.IID).size(), is(1)); - assertThat(typeHierarchy.getDirectChildren(DataObjects.DataObject4.DataObject41.IID), CoreMatchers.hasItem( - DataObjects.DataObject4.DataObject41.DataObject411.IID)); - assertEquals(typeHierarchy.getDirectChildren(DataObjects.DataObject4.DataObject41.IID), typeHierarchy.getAllChildren( - DataObjects.DataObject4.DataObject41.IID)); - - assertThat(typeHierarchy.getDirectChildren(DataObjects.DataObject4.IID).size(), is(1)); - assertThat(typeHierarchy.getDirectChildren(DataObjects.DataObject4.IID), CoreMatchers - .hasItem(DataObjects.DataObject4.DataObject41.IID)); - assertThat(typeHierarchy.getAllChildren(DataObjects.DataObject4.IID).size(), is(2)); - assertTrue(typeHierarchy.getAllChildren(DataObjects.DataObject4.IID).contains(DataObjects.DataObject4.DataObject41.IID)); - assertTrue(typeHierarchy.getAllChildren(DataObjects.DataObject4.IID).contains(DataObjects.DataObject4.DataObject41.DataObject411.IID)); - } -} - diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/write/registry/FlatWriterRegistryBuilderTest.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/write/registry/FlatWriterRegistryBuilderTest.java deleted file mode 100644 index 7822c8926..000000000 --- a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/write/registry/FlatWriterRegistryBuilderTest.java +++ /dev/null @@ -1,193 +0,0 @@ -/* - * Copyright (c) 2016 Cisco and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.fd.honeycomb.translate.util.write.registry; - -import static org.hamcrest.CoreMatchers.anyOf; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.hasItems; -import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Lists; -import com.google.common.collect.Multimaps; -import com.google.common.collect.Sets; -import io.fd.honeycomb.translate.util.DataObjects; -import io.fd.honeycomb.translate.write.DataObjectUpdate; -import io.fd.honeycomb.translate.write.WriteContext; -import io.fd.honeycomb.translate.write.Writer; -import io.fd.honeycomb.translate.write.registry.WriterRegistry; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import org.junit.Test; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; - -public class FlatWriterRegistryBuilderTest { - - @Test - public void testRelationsBefore() throws Exception { - final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); - /* - 1 -> 2 -> 3 - -> 4 - */ - flatWriterRegistryBuilder.add(mockWriter(DataObjects.DataObject3.class)); - flatWriterRegistryBuilder.add(mockWriter(DataObjects.DataObject4.class)); - flatWriterRegistryBuilder.addBefore(mockWriter(DataObjects.DataObject2.class), - Lists.newArrayList(DataObjects.DataObject3.IID, DataObjects.DataObject4.IID)); - flatWriterRegistryBuilder.addBefore(mockWriter(DataObjects.DataObject1.class), DataObjects.DataObject2.IID); - final ImmutableMap, Writer> mappedWriters = - flatWriterRegistryBuilder.getMappedHandlers(); - - final ArrayList> typesInList = Lists.newArrayList(mappedWriters.keySet()); - assertEquals(DataObjects.DataObject1.IID, typesInList.get(0)); - assertEquals(DataObjects.DataObject2.IID, typesInList.get(1)); - assertThat(typesInList.get(2), anyOf(equalTo(DataObjects.DataObject3.IID), equalTo(DataObjects.DataObject4.IID))); - assertThat(typesInList.get(3), anyOf(equalTo(DataObjects.DataObject3.IID), equalTo(DataObjects.DataObject4.IID))); - } - - @Test - public void testBuild() throws Exception { - final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); - final Writer writer = mockWriter(DataObjects.DataObject3.class); - flatWriterRegistryBuilder.add(writer); - final WriterRegistry build = flatWriterRegistryBuilder.build(); - - final InstanceIdentifier id = InstanceIdentifier.create(DataObjects.DataObject3.class); - final DataObjectUpdate update = mock(DataObjectUpdate.class); - doReturn(id).when(update).getId(); - final DataObjects.DataObject3 before = mock(DataObjects.DataObject3.class); - final DataObjects.DataObject3 after = mock(DataObjects.DataObject3.class); - when(update.getDataBefore()).thenReturn(before); - when(update.getDataAfter()).thenReturn(after); - - WriterRegistry.DataObjectUpdates updates = new WriterRegistry.DataObjectUpdates( - Multimaps.forMap(Collections.singletonMap(id, update)), - Multimaps.forMap(Collections.emptyMap())); - final WriteContext ctx = mock(WriteContext.class); - build.update(updates, ctx); - - verify(writer).update(id, before, after, ctx); - } - - @Test(expected = IllegalArgumentException.class) - public void testBuildUnknownWriter() throws Exception { - final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); - final Writer writer = mockWriter(DataObjects.DataObject3.class); - flatWriterRegistryBuilder.add(writer); - final WriterRegistry build = flatWriterRegistryBuilder.build(); - - final InstanceIdentifier id2 = InstanceIdentifier.create(DataObjects.DataObject1.class); - final DataObjectUpdate update2 = mock(DataObjectUpdate.class); - final WriterRegistry.DataObjectUpdates updates = new WriterRegistry.DataObjectUpdates( - Multimaps.forMap(Collections.singletonMap(id2, update2)), - Multimaps.forMap(Collections.emptyMap())); - build.update(updates, mock(WriteContext.class)); - } - - @Test - public void testRelationsAfter() throws Exception { - final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); - /* - 1 -> 2 -> 3 - -> 4 - */ - flatWriterRegistryBuilder.add(mockWriter(DataObjects.DataObject1.class)); - flatWriterRegistryBuilder.addAfter(mockWriter(DataObjects.DataObject2.class), DataObjects.DataObject1.IID); - flatWriterRegistryBuilder.addAfter(mockWriter(DataObjects.DataObject3.class), DataObjects.DataObject2.IID); - flatWriterRegistryBuilder.addAfter(mockWriter(DataObjects.DataObject4.class), - Lists.newArrayList(DataObjects.DataObject2.IID, DataObjects.DataObject3.IID)); - final ImmutableMap, Writer> mappedWriters = - flatWriterRegistryBuilder.getMappedHandlers(); - - final List> typesInList = Lists.newArrayList(mappedWriters.keySet()); - assertEquals(DataObjects.DataObject1.IID, typesInList.get(0)); - assertEquals(DataObjects.DataObject2.IID, typesInList.get(1)); - assertThat(typesInList.get(2), anyOf(equalTo(DataObjects.DataObject3.IID), equalTo(DataObjects.DataObject4.IID))); - assertThat(typesInList.get(3), anyOf(equalTo(DataObjects.DataObject3.IID), equalTo(DataObjects.DataObject4.IID))); - } - - @Test(expected = IllegalArgumentException.class) - public void testRelationsLoop() throws Exception { - final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); - /* - 1 -> 2 -> 1 - */ - flatWriterRegistryBuilder.add(mockWriter(DataObjects.DataObject1.class)); - flatWriterRegistryBuilder.addAfter(mockWriter(DataObjects.DataObject2.class), DataObjects.DataObject1.IID); - flatWriterRegistryBuilder.addAfter(mockWriter(DataObjects.DataObject1.class), DataObjects.DataObject2.IID); - } - - @Test(expected = IllegalArgumentException.class) - public void testAddWriterTwice() throws Exception { - final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); - flatWriterRegistryBuilder.add(mockWriter(DataObjects.DataObject1.class)); - flatWriterRegistryBuilder.add(mockWriter(DataObjects.DataObject1.class)); - } - - @Test - public void testAddSubtreeWriter() throws Exception { - final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); - flatWriterRegistryBuilder.subtreeAdd( - Sets.newHashSet(DataObjects.DataObject4.DataObject41.IID, - DataObjects.DataObject4.DataObject41.IID), - mockWriter(DataObjects.DataObject4.class)); - final ImmutableMap, Writer> mappedWriters = - flatWriterRegistryBuilder.getMappedHandlers(); - final ArrayList> typesInList = Lists.newArrayList(mappedWriters.keySet()); - - assertEquals(DataObjects.DataObject4.IID, typesInList.get(0)); - assertEquals(1, typesInList.size()); - } - - @Test - public void testCreateSubtreeWriter() throws Exception { - final Writer forWriter = SubtreeWriter.createForWriter(Sets.newHashSet( - DataObjects.DataObject4.DataObject41.IID, - DataObjects.DataObject4.DataObject41.DataObject411.IID, - DataObjects.DataObject4.DataObject42.IID), - mockWriter(DataObjects.DataObject4.class)); - assertThat(forWriter, instanceOf(SubtreeWriter.class)); - assertThat(((SubtreeWriter) forWriter).getHandledChildTypes().size(), is(3)); - assertThat(((SubtreeWriter) forWriter).getHandledChildTypes(), hasItems(DataObjects.DataObject4.DataObject41.IID, - DataObjects.DataObject4.DataObject42.IID, DataObjects.DataObject4.DataObject41.DataObject411.IID)); - } - - @Test(expected = IllegalArgumentException.class) - public void testCreateInvalidSubtreeWriter() throws Exception { - SubtreeWriter.createForWriter(Sets.newHashSet( - InstanceIdentifier.create(DataObjects.DataObject3.class).child(DataObjects.DataObject3.DataObject31.class)), - mockWriter(DataObjects.DataObject4.class)); - } - - @SuppressWarnings("unchecked") - private Writer mockWriter(final Class doClass) - throws NoSuchFieldException, IllegalAccessException { - final Writer mock = mock(Writer.class); - when(mock.getManagedDataObjectType()).thenReturn((InstanceIdentifier) doClass.getDeclaredField("IID").get(null)); - return mock; - } - -} \ No newline at end of file diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/write/registry/FlatWriterRegistryTest.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/write/registry/FlatWriterRegistryTest.java deleted file mode 100644 index 5566fca47..000000000 --- a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/write/registry/FlatWriterRegistryTest.java +++ /dev/null @@ -1,328 +0,0 @@ -/* - * Copyright (c) 2016 Cisco and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.fd.honeycomb.translate.util.write.registry; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.inOrder; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.verifyZeroInteractions; -import static org.mockito.Mockito.when; - -import com.google.common.collect.HashMultimap; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableMultimap; -import com.google.common.collect.Multimap; -import io.fd.honeycomb.translate.util.DataObjects; -import io.fd.honeycomb.translate.util.DataObjects.DataObject1; -import io.fd.honeycomb.translate.write.DataObjectUpdate; -import io.fd.honeycomb.translate.write.WriteContext; -import io.fd.honeycomb.translate.write.Writer; -import io.fd.honeycomb.translate.write.registry.WriterRegistry; -import org.hamcrest.CoreMatchers; -import org.junit.Before; -import org.junit.Test; -import org.mockito.InOrder; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; - -public class FlatWriterRegistryTest { - - @Mock - private Writer writer1; - @Mock - private Writer writer2; - @Mock - private Writer writer3; - @Mock - private Writer writer4; - @Mock - private WriteContext ctx; - @Mock - private WriteContext revertWriteContext; - - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - when(writer1.getManagedDataObjectType()).thenReturn(DataObjects.DataObject1.IID); - when(writer2.getManagedDataObjectType()).thenReturn(DataObjects.DataObject2.IID); - when(writer3.getManagedDataObjectType()).thenReturn(DataObjects.DataObject3.IID); - } - - @Test - public void testMultipleUpdatesForSingleWriter() throws Exception { - final FlatWriterRegistry flatWriterRegistry = - new FlatWriterRegistry(ImmutableMap.of(DataObjects.DataObject1.IID, writer1, DataObjects.DataObject2.IID, writer2)); - - final Multimap, DataObjectUpdate> updates = HashMultimap.create(); - final InstanceIdentifier iid = InstanceIdentifier.create(DataObjects.DataObject1.class); - final InstanceIdentifier iid2 = InstanceIdentifier.create(DataObjects.DataObject1.class); - final DataObjects.DataObject1 dataObject = mock(DataObjects.DataObject1.class); - updates.put(DataObjects.DataObject1.IID, DataObjectUpdate.create(iid, dataObject, dataObject)); - updates.put(DataObjects.DataObject1.IID, DataObjectUpdate.create(iid2, dataObject, dataObject)); - flatWriterRegistry.update(new WriterRegistry.DataObjectUpdates(updates, ImmutableMultimap.of()), ctx); - - verify(writer1).update(iid, dataObject, dataObject, ctx); - verify(writer1).update(iid2, dataObject, dataObject, ctx); - // Invoked when registry is being created - verifyNoMoreInteractions(writer1); - verifyZeroInteractions(writer2); - } - - @Test - public void testMultipleUpdatesForMultipleWriters() throws Exception { - final FlatWriterRegistry flatWriterRegistry = - new FlatWriterRegistry(ImmutableMap.of(DataObjects.DataObject1.IID, writer1, DataObjects.DataObject2.IID, writer2)); - - final Multimap, DataObjectUpdate> updates = HashMultimap.create(); - final InstanceIdentifier iid = InstanceIdentifier.create(DataObjects.DataObject1.class); - final DataObjects.DataObject1 dataObject = mock(DataObjects.DataObject1.class); - updates.put(DataObjects.DataObject1.IID, DataObjectUpdate.create(iid, dataObject, dataObject)); - final InstanceIdentifier iid2 = InstanceIdentifier.create(DataObjects.DataObject2.class); - final DataObjects.DataObject2 dataObject2 = mock(DataObjects.DataObject2.class); - updates.put(DataObjects.DataObject2.IID, DataObjectUpdate.create(iid2, dataObject2, dataObject2)); - flatWriterRegistry.update(new WriterRegistry.DataObjectUpdates(updates, ImmutableMultimap.of()), ctx); - - final InOrder inOrder = inOrder(writer1, writer2); - inOrder.verify(writer1).update(iid, dataObject, dataObject, ctx); - inOrder.verify(writer2).update(iid2, dataObject2, dataObject2, ctx); - - verifyNoMoreInteractions(writer1); - verifyNoMoreInteractions(writer2); - } - - @Test - public void testMultipleDeletesForMultipleWriters() throws Exception { - final FlatWriterRegistry flatWriterRegistry = - new FlatWriterRegistry(ImmutableMap.of(DataObjects.DataObject1.IID, writer1, DataObjects.DataObject2.IID, writer2)); - - final Multimap, DataObjectUpdate.DataObjectDelete> deletes = HashMultimap.create(); - final InstanceIdentifier iid = InstanceIdentifier.create(DataObjects.DataObject1.class); - final DataObjects.DataObject1 dataObject = mock(DataObjects.DataObject1.class); - deletes.put(DataObjects.DataObject1.IID, ((DataObjectUpdate.DataObjectDelete) DataObjectUpdate.create(iid, dataObject, null))); - final InstanceIdentifier iid2 = InstanceIdentifier.create(DataObjects.DataObject2.class); - final DataObjects.DataObject2 dataObject2 = mock(DataObjects.DataObject2.class); - deletes.put(DataObjects.DataObject2.IID, ((DataObjectUpdate.DataObjectDelete) DataObjectUpdate.create(iid2, dataObject2, null))); - flatWriterRegistry.update(new WriterRegistry.DataObjectUpdates(ImmutableMultimap.of(), deletes), ctx); - - final InOrder inOrder = inOrder(writer1, writer2); - // Reversed order of invocation, first writer2 and then writer1 - inOrder.verify(writer2).update(iid2, dataObject2, null, ctx); - inOrder.verify(writer1).update(iid, dataObject, null, ctx); - - verifyNoMoreInteractions(writer1); - verifyNoMoreInteractions(writer2); - } - - @Test - public void testMultipleUpdatesAndDeletesForMultipleWriters() throws Exception { - final FlatWriterRegistry flatWriterRegistry = - new FlatWriterRegistry(ImmutableMap.of(DataObjects.DataObject1.IID, writer1, DataObjects.DataObject2.IID, writer2)); - - final Multimap, DataObjectUpdate.DataObjectDelete> deletes = HashMultimap.create(); - final Multimap, DataObjectUpdate> updates = HashMultimap.create(); - final InstanceIdentifier iid = InstanceIdentifier.create(DataObjects.DataObject1.class); - final DataObjects.DataObject1 dataObject = mock(DataObjects.DataObject1.class); - // Writer 1 delete - deletes.put(DataObjects.DataObject1.IID, ((DataObjectUpdate.DataObjectDelete) DataObjectUpdate.create(iid, dataObject, null))); - // Writer 1 update - updates.put(DataObjects.DataObject1.IID, DataObjectUpdate.create(iid, dataObject, dataObject)); - final InstanceIdentifier iid2 = InstanceIdentifier.create(DataObjects.DataObject2.class); - final DataObjects.DataObject2 dataObject2 = mock(DataObjects.DataObject2.class); - // Writer 2 delete - deletes.put(DataObjects.DataObject2.IID, ((DataObjectUpdate.DataObjectDelete) DataObjectUpdate.create(iid2, dataObject2, null))); - // Writer 2 update - updates.put(DataObjects.DataObject2.IID, DataObjectUpdate.create(iid2, dataObject2, dataObject2)); - flatWriterRegistry.update(new WriterRegistry.DataObjectUpdates(updates, deletes), ctx); - - final InOrder inOrder = inOrder(writer1, writer2); - // Reversed order of invocation, first writer2 and then writer1 for deletes - inOrder.verify(writer2).update(iid2, dataObject2, null, ctx); - inOrder.verify(writer1).update(iid, dataObject, null, ctx); - // Then also updates are processed - inOrder.verify(writer1).update(iid, dataObject, dataObject, ctx); - inOrder.verify(writer2).update(iid2, dataObject2, dataObject2, ctx); - - verifyNoMoreInteractions(writer1); - verifyNoMoreInteractions(writer2); - } - - @Test(expected = IllegalArgumentException.class) - public void testMultipleUpdatesOneMissing() throws Exception { - final FlatWriterRegistry flatWriterRegistry = - new FlatWriterRegistry(ImmutableMap.of(DataObjects.DataObject1.IID, writer1)); - - final Multimap, DataObjectUpdate> updates = HashMultimap.create(); - addUpdate(updates, DataObjects.DataObject1.class); - addUpdate(updates, DataObjects.DataObject2.class); - flatWriterRegistry.update(new WriterRegistry.DataObjectUpdates(updates, ImmutableMultimap.of()), ctx); - } - - @Test - public void testMultipleUpdatesOneFailing() throws Exception { - final FlatWriterRegistry flatWriterRegistry = - new FlatWriterRegistry(ImmutableMap.of(DataObjects.DataObject1.IID, writer1, DataObjects.DataObject2.IID, writer2)); - - // Writer1 always fails - doThrow(new RuntimeException()).when(writer1) - .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), any(WriteContext.class)); - - final Multimap, DataObjectUpdate> updates = HashMultimap.create(); - addUpdate(updates, DataObjects.DataObject1.class); - addUpdate(updates, DataObjects.DataObject2.class); - - try { - flatWriterRegistry.update(new WriterRegistry.DataObjectUpdates(updates, ImmutableMultimap.of()), ctx); - fail("Bulk update should have failed on writer1"); - } catch (WriterRegistry.BulkUpdateException e) { - assertThat(e.getFailedIds().size(), is(2)); - assertThat(e.getFailedIds(), CoreMatchers.hasItem(InstanceIdentifier.create(DataObjects.DataObject2.class))); - assertThat(e.getFailedIds(), CoreMatchers.hasItem(InstanceIdentifier.create(DataObjects.DataObject1.class))); - } - } - - @Test - public void testMultipleUpdatesOneFailingThenRevertWithSuccess() throws Exception { - final FlatWriterRegistry flatWriterRegistry = - new FlatWriterRegistry( - ImmutableMap.of(DataObjects.DataObject1.IID, writer1, DataObjects.DataObject2.IID, writer2, DataObjects.DataObject3.IID, writer3)); - - // Writer1 always fails - doThrow(new RuntimeException()).when(writer3) - .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), any(WriteContext.class)); - - final Multimap, DataObjectUpdate> updates = HashMultimap.create(); - addUpdate(updates, DataObjects.DataObject1.class); - addUpdate(updates, DataObjects.DataObject3.class); - final InstanceIdentifier iid2 = InstanceIdentifier.create(DataObjects.DataObject2.class); - final DataObjects.DataObject2 before2 = mock(DataObjects.DataObject2.class); - final DataObjects.DataObject2 after2 = mock(DataObjects.DataObject2.class); - updates.put(DataObjects.DataObject2.IID, DataObjectUpdate.create(iid2, before2, after2)); - - try { - flatWriterRegistry.update(new WriterRegistry.DataObjectUpdates(updates, ImmutableMultimap.of()), ctx); - fail("Bulk update should have failed on writer1"); - } catch (WriterRegistry.BulkUpdateException e) { - assertThat(e.getFailedIds().size(), is(1)); - - final InOrder inOrder = inOrder(writer1, writer2, writer3); - inOrder.verify(writer1) - .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), any(WriteContext.class)); - inOrder.verify(writer2) - .update(iid2, before2, after2, ctx); - inOrder.verify(writer3) - .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), any(WriteContext.class)); - - e.revertChanges(revertWriteContext); - // Revert changes. Successful updates are iterated in reverse - // also binding other write context,to verify if update context is not reused - inOrder.verify(writer2) - .update(iid2, after2, before2, revertWriteContext); - inOrder.verify(writer1) - .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), eq(revertWriteContext)); - verifyNoMoreInteractions(writer3); - } - } - - @Test - public void testMultipleUpdatesOneFailingThenRevertWithFail() throws Exception { - final FlatWriterRegistry flatWriterRegistry = - new FlatWriterRegistry( - ImmutableMap.of(DataObjects.DataObject1.IID, writer1, DataObjects.DataObject2.IID, writer2, DataObjects.DataObject3.IID, writer3)); - - // Writer1 always fails - doThrow(new RuntimeException()).when(writer3) - .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), any(WriteContext.class)); - - final Multimap, DataObjectUpdate> updates = HashMultimap.create(); - addUpdate(updates, DataObjects.DataObject1.class); - addUpdate(updates, DataObjects.DataObject2.class); - addUpdate(updates, DataObjects.DataObject3.class); - - try { - flatWriterRegistry.update(new WriterRegistry.DataObjectUpdates(updates, ImmutableMultimap.of()), ctx); - fail("Bulk update should have failed on writer1"); - } catch (WriterRegistry.BulkUpdateException e) { - // Writer1 always fails from now - doThrow(new RuntimeException()).when(writer1) - .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), any(WriteContext.class)); - try { - e.revertChanges(revertWriteContext); - } catch (WriterRegistry.Reverter.RevertFailedException e1) { - assertThat(e1.getNotRevertedChanges().size(), is(1)); - assertThat(e1.getNotRevertedChanges(), CoreMatchers - .hasItem(InstanceIdentifier.create(DataObjects.DataObject1.class))); - } - } - } - - @Test - public void testMutlipleUpdatesWithOneKeyedContainer() throws Exception { - final InstanceIdentifier internallyKeyedIdentifier = InstanceIdentifier.create(DataObject1.class) - .child(DataObjects.DataObject1ChildK.class, new DataObjects.DataObject1ChildKey()); - - final FlatWriterRegistry flatWriterRegistry = - new FlatWriterRegistry( - ImmutableMap.of(DataObjects.DataObject1.IID, writer1, DataObjects.DataObject1ChildK.IID,writer4)); - - // Writer1 always fails - doThrow(new RuntimeException()).when(writer1) - .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), - any(WriteContext.class)); - - final Multimap, DataObjectUpdate> updates = HashMultimap.create(); - addKeyedUpdate(updates,DataObjects.DataObject1ChildK.class); - addUpdate(updates, DataObjects.DataObject1.class); - try { - flatWriterRegistry.update(new WriterRegistry.DataObjectUpdates(updates, ImmutableMultimap.of()), ctx); - fail("Bulk update should have failed on writer1"); - } catch (WriterRegistry.BulkUpdateException e) { - // Writer1 always fails from now - doThrow(new RuntimeException()).when(writer1) - .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), - any(WriteContext.class)); - try { - e.revertChanges(revertWriteContext); - } catch (WriterRegistry.Reverter.RevertFailedException e1) { - assertThat(e1.getNotRevertedChanges().size(), is(1)); - assertThat(e1.getNotRevertedChanges(), CoreMatchers - .hasItem(InstanceIdentifier.create(DataObjects.DataObject1.class))); - } - } - } - - private void addKeyedUpdate(final Multimap, DataObjectUpdate> updates, - final Class type) throws Exception { - final InstanceIdentifier iid = (InstanceIdentifier) type.getDeclaredField("IID").get(null); - final InstanceIdentifier keyedIid = (InstanceIdentifier) type.getDeclaredField("INTERNALLY_KEYED_IID").get(null); - updates.put(iid, DataObjectUpdate.create(keyedIid, mock(type), mock(type))); - } - - private void addUpdate(final Multimap, DataObjectUpdate> updates, - final Class type) throws Exception { - final InstanceIdentifier iid = (InstanceIdentifier) type.getDeclaredField("IID").get(null); - updates.put(iid, DataObjectUpdate.create(iid, mock(type), mock(type))); - } -} \ No newline at end of file diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/write/registry/SubtreeWriterTest.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/write/registry/SubtreeWriterTest.java deleted file mode 100644 index 313dd34a8..000000000 --- a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/write/registry/SubtreeWriterTest.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright (c) 2016 Cisco and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.fd.honeycomb.translate.util.write.registry; - -import static org.hamcrest.CoreMatchers.hasItem; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.when; - -import com.google.common.collect.Sets; -import io.fd.honeycomb.translate.util.DataObjects; -import io.fd.honeycomb.translate.write.Writer; -import java.util.Collections; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; - -public class SubtreeWriterTest { - - @Mock - Writer writer; - @Mock - Writer writer11; - - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - when(writer.getManagedDataObjectType()).thenReturn(DataObjects.DataObject4.IID); - when(writer11.getManagedDataObjectType()).thenReturn(DataObjects.DataObject4.DataObject41.IID); - } - - @Test(expected = IllegalArgumentException.class) - public void testSubtreeWriterCreationFail() throws Exception { - // The subtree node identified by IID.c(DataObject.class) is not a child of writer.getManagedDataObjectType - SubtreeWriter.createForWriter(Collections.singleton(InstanceIdentifier.create(DataObject.class)), writer); - } - - @Test(expected = IllegalArgumentException.class) - public void testSubtreeWriterCreationFailInvalidIid() throws Exception { - // The subtree node identified by IID.c(DataObject.class) is not a child of writer.getManagedDataObjectType - SubtreeWriter.createForWriter(Collections.singleton(DataObjects.DataObject4.IID), writer); - } - - @Test - public void testSubtreeWriterCreation() throws Exception { - final SubtreeWriter forWriter = (SubtreeWriter) SubtreeWriter.createForWriter(Sets.newHashSet( - DataObjects.DataObject4.DataObject41.IID, - DataObjects.DataObject4.DataObject41.DataObject411.IID, - DataObjects.DataObject4.DataObject42.IID), - writer); - - assertEquals(writer.getManagedDataObjectType(), forWriter.getManagedDataObjectType()); - assertEquals(3, forWriter.getHandledChildTypes().size()); - } - - @Test - public void testSubtreeWriterHandledTypes() throws Exception { - final SubtreeWriter forWriter = (SubtreeWriter) SubtreeWriter.createForWriter(Sets.newHashSet( - DataObjects.DataObject4.DataObject41.DataObject411.IID), - writer); - - assertEquals(writer.getManagedDataObjectType(), forWriter.getManagedDataObjectType()); - assertEquals(1, forWriter.getHandledChildTypes().size()); - assertThat(forWriter.getHandledChildTypes(), hasItem(DataObjects.DataObject4.DataObject41.DataObject411.IID)); - } - -} \ No newline at end of file diff --git a/ioam/impl/src/test/java/io/fd/honeycomb/vppioam/impl/VppIoamModuleTest.java b/ioam/impl/src/test/java/io/fd/honeycomb/vppioam/impl/VppIoamModuleTest.java index 288122fd0..d9176d39d 100644 --- a/ioam/impl/src/test/java/io/fd/honeycomb/vppioam/impl/VppIoamModuleTest.java +++ b/ioam/impl/src/test/java/io/fd/honeycomb/vppioam/impl/VppIoamModuleTest.java @@ -27,13 +27,10 @@ import static org.mockito.MockitoAnnotations.initMocks; import com.google.inject.Guice; import com.google.inject.Inject; import com.google.inject.Provider; -import com.google.inject.multibindings.Multibinder; import com.google.inject.name.Named; import com.google.inject.testing.fieldbinder.Bind; import com.google.inject.testing.fieldbinder.BoundFieldModule; -import io.fd.honeycomb.translate.util.read.registry.CompositeReaderRegistryBuilder; -import io.fd.honeycomb.translate.util.write.registry.FlatWriterRegistryBuilder; -import io.fd.honeycomb.translate.vpp.util.NamingContext; +import io.fd.honeycomb.translate.impl.write.registry.FlatWriterRegistryBuilder; import io.fd.honeycomb.translate.write.WriterFactory; import java.util.HashSet; import java.util.Set; @@ -41,11 +38,7 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import org.opendaylight.controller.md.sal.binding.api.DataBroker; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import io.fd.honeycomb.vppioam.impl.config.VppIoamWriterFactory; -import io.fd.honeycomb.vppioam.impl.util.JVppIoamProvider; -import io.fd.vpp.jvpp.ioamtrace.future.FutureJVppIoamtrace; import io.fd.vpp.jvpp.ioamtrace.future.FutureJVppIoamtraceFacade; import io.fd.vpp.jvpp.JVppRegistry; diff --git a/lisp/lisp2vpp/src/test/java/io/fd/honeycomb/lisp/LispModuleTest.java b/lisp/lisp2vpp/src/test/java/io/fd/honeycomb/lisp/LispModuleTest.java index 6d1e05ce8..20c6aa3f7 100644 --- a/lisp/lisp2vpp/src/test/java/io/fd/honeycomb/lisp/LispModuleTest.java +++ b/lisp/lisp2vpp/src/test/java/io/fd/honeycomb/lisp/LispModuleTest.java @@ -29,8 +29,8 @@ import com.google.inject.name.Named; import com.google.inject.testing.fieldbinder.Bind; import com.google.inject.testing.fieldbinder.BoundFieldModule; import io.fd.honeycomb.translate.read.ReaderFactory; -import io.fd.honeycomb.translate.util.read.registry.CompositeReaderRegistryBuilder; -import io.fd.honeycomb.translate.util.write.registry.FlatWriterRegistryBuilder; +import io.fd.honeycomb.translate.impl.read.registry.CompositeReaderRegistryBuilder; +import io.fd.honeycomb.translate.impl.write.registry.FlatWriterRegistryBuilder; import io.fd.honeycomb.translate.vpp.util.NamingContext; import io.fd.honeycomb.translate.write.WriterFactory; import java.util.HashSet; diff --git a/nat/nat2vpp/src/test/java/io/fd/honeycomb/nat/NatModuleTest.java b/nat/nat2vpp/src/test/java/io/fd/honeycomb/nat/NatModuleTest.java index 774dd6f2a..f69672e15 100644 --- a/nat/nat2vpp/src/test/java/io/fd/honeycomb/nat/NatModuleTest.java +++ b/nat/nat2vpp/src/test/java/io/fd/honeycomb/nat/NatModuleTest.java @@ -32,8 +32,8 @@ import com.google.inject.name.Named; import com.google.inject.testing.fieldbinder.Bind; import com.google.inject.testing.fieldbinder.BoundFieldModule; import io.fd.honeycomb.translate.read.ReaderFactory; -import io.fd.honeycomb.translate.util.read.registry.CompositeReaderRegistryBuilder; -import io.fd.honeycomb.translate.util.write.registry.FlatWriterRegistryBuilder; +import io.fd.honeycomb.translate.impl.read.registry.CompositeReaderRegistryBuilder; +import io.fd.honeycomb.translate.impl.write.registry.FlatWriterRegistryBuilder; import io.fd.honeycomb.translate.vpp.util.NamingContext; import io.fd.honeycomb.translate.write.WriterFactory; import io.fd.vpp.jvpp.snat.future.FutureJVppSnatFacade; diff --git a/nsh/impl/src/test/java/io/fd/honeycomb/vppnsh/impl/VppNshModuleTest.java b/nsh/impl/src/test/java/io/fd/honeycomb/vppnsh/impl/VppNshModuleTest.java index 211757474..0da2c1bc4 100644 --- a/nsh/impl/src/test/java/io/fd/honeycomb/vppnsh/impl/VppNshModuleTest.java +++ b/nsh/impl/src/test/java/io/fd/honeycomb/vppnsh/impl/VppNshModuleTest.java @@ -31,8 +31,8 @@ import com.google.inject.name.Named; import com.google.inject.testing.fieldbinder.Bind; import com.google.inject.testing.fieldbinder.BoundFieldModule; import io.fd.honeycomb.translate.read.ReaderFactory; -import io.fd.honeycomb.translate.util.read.registry.CompositeReaderRegistryBuilder; -import io.fd.honeycomb.translate.util.write.registry.FlatWriterRegistryBuilder; +import io.fd.honeycomb.translate.impl.read.registry.CompositeReaderRegistryBuilder; +import io.fd.honeycomb.translate.impl.write.registry.FlatWriterRegistryBuilder; import io.fd.honeycomb.translate.vpp.util.NamingContext; import io.fd.honeycomb.translate.write.WriterFactory; import io.fd.vpp.jvpp.JVppRegistry; diff --git a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/V3poModuleTest.java b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/V3poModuleTest.java index 93453dd22..19aa2c98b 100644 --- a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/V3poModuleTest.java +++ b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/V3poModuleTest.java @@ -30,8 +30,8 @@ import com.google.inject.testing.fieldbinder.Bind; import com.google.inject.testing.fieldbinder.BoundFieldModule; import io.fd.honeycomb.translate.MappingContext; import io.fd.honeycomb.translate.read.ReaderFactory; -import io.fd.honeycomb.translate.util.read.registry.CompositeReaderRegistryBuilder; -import io.fd.honeycomb.translate.util.write.registry.FlatWriterRegistryBuilder; +import io.fd.honeycomb.translate.impl.read.registry.CompositeReaderRegistryBuilder; +import io.fd.honeycomb.translate.impl.write.registry.FlatWriterRegistryBuilder; import io.fd.honeycomb.translate.write.WriterFactory; import java.util.HashSet; import java.util.Set; diff --git a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/vppstate/VppStateTest.java b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/vppstate/VppStateTest.java index cdb2b0376..cdc352ac5 100644 --- a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/vppstate/VppStateTest.java +++ b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/vppstate/VppStateTest.java @@ -35,7 +35,7 @@ import io.fd.honeycomb.translate.impl.read.GenericReader; import io.fd.honeycomb.translate.read.ReadContext; import io.fd.honeycomb.translate.read.ReadFailedException; import io.fd.honeycomb.translate.read.registry.ReaderRegistry; -import io.fd.honeycomb.translate.util.read.registry.CompositeReaderRegistryBuilder; +import io.fd.honeycomb.translate.impl.read.registry.CompositeReaderRegistryBuilder; import io.fd.honeycomb.vpp.test.util.NamingContextHelper; import io.fd.honeycomb.translate.vpp.util.NamingContext; import io.fd.honeycomb.vpp.test.util.FutureProducer; -- cgit 1.2.3-korg