From 04c868333214c0e5bfce3926c43a4302615f2ac5 Mon Sep 17 00:00:00 2001 From: Maros Marsalek Date: Wed, 26 Oct 2016 12:18:11 +0200 Subject: Honeycomb-73 Extensible initializers framework Change-Id: Ib23453d4040d59a512686315995a5cf9e532cefc Signed-off-by: Maros Marsalek --- .../java/io/fd/honeycomb/infra/distro/Main.java | 2 - .../data/ConfigAndOperationalPipelineModule.java | 6 +- .../distro/data/oper/ReadableDTDelegProvider.java | 7 +- .../data/oper/ReaderRegistryBuilderProvider.java | 40 +++++++++++ .../distro/data/oper/ReaderRegistryProvider.java | 19 ++--- .../initializer/InitializerPipelineModule.java | 2 +- .../initializer/InitializerRegistryAdapter.java | 55 ++++++++++++--- .../InitializerRegistryAdapterProvider.java | 54 ++++++++++++++ .../initializer/InitializerRegistryProvider.java | 44 ------------ infra/translate-api/pom.xml | 4 ++ .../translate/read/InitFailedException.java | 30 ++++++++ .../honeycomb/translate/read/InitListReader.java | 31 ++++++++ .../io/fd/honeycomb/translate/read/InitReader.java | 29 ++++++++ .../fd/honeycomb/translate/read/Initializer.java | 42 +++++++++++ .../translate/read/ReadFailedException.java | 8 ++- .../translate/read/registry/InitRegistry.java | 34 +++++++++ .../translate/read/registry/ReaderRegistry.java | 2 +- .../translate/impl/read/GenericInitListReader.java | 82 ++++++++++++++++++++++ .../translate/impl/read/GenericInitReader.java | 73 +++++++++++++++++++ .../translate/impl/read/GenericListReader.java | 7 +- .../translate/impl/read/GenericReader.java | 12 ++-- .../honeycomb/translate/spi/read/Initialized.java | 77 ++++++++++++++++++++ .../translate/spi/read/InitializingCustomizer.java | 45 ++++++++++++ .../spi/read/InitializingListReaderCustomizer.java | 29 ++++++++ .../spi/read/InitializingReaderCustomizer.java | 27 +++++++ .../translate/spi/read/ListReaderCustomizer.java | 12 ++-- .../translate/spi/read/ReaderCustomizer.java | 10 +-- ...stractSubtreeManagerRegistryBuilderBuilder.java | 6 +- .../io/fd/honeycomb/translate/util/RWUtils.java | 9 +++ .../util/read/registry/CompositeReader.java | 44 ++++++++++-- .../read/registry/CompositeReaderRegistry.java | 18 ++++- .../registry/CompositeReaderRegistryBuilder.java | 11 ++- .../util/read/registry/InitSubtreeReader.java | 72 +++++++++++++++++++ .../util/read/registry/SubtreeReader.java | 12 ++-- 34 files changed, 842 insertions(+), 113 deletions(-) create mode 100644 infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/data/oper/ReaderRegistryBuilderProvider.java create mode 100644 infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/initializer/InitializerRegistryAdapterProvider.java delete mode 100644 infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/initializer/InitializerRegistryProvider.java create mode 100644 infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/InitFailedException.java create mode 100644 infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/InitListReader.java create mode 100644 infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/InitReader.java create mode 100644 infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/Initializer.java create mode 100644 infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/registry/InitRegistry.java create mode 100644 infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericInitListReader.java create mode 100644 infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericInitReader.java create mode 100644 infra/translate-spi/src/main/java/io/fd/honeycomb/translate/spi/read/Initialized.java create mode 100644 infra/translate-spi/src/main/java/io/fd/honeycomb/translate/spi/read/InitializingCustomizer.java create mode 100644 infra/translate-spi/src/main/java/io/fd/honeycomb/translate/spi/read/InitializingListReaderCustomizer.java create mode 100644 infra/translate-spi/src/main/java/io/fd/honeycomb/translate/spi/read/InitializingReaderCustomizer.java create mode 100644 infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/InitSubtreeReader.java (limited to 'infra') diff --git a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/Main.java b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/Main.java index 3c62382c2..4769b570d 100644 --- a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/Main.java +++ b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/Main.java @@ -119,8 +119,6 @@ public final class Main { injector.getInstance(HoneycombNotification2NetconfProvider.HoneycombNotification2Netconf.class); } - LOG.info("Honeycomb started successfully!"); - try { LOG.info("Initializing configuration"); injector.getInstance(Key.get(InitializerRegistry.class, diff --git a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/data/ConfigAndOperationalPipelineModule.java b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/data/ConfigAndOperationalPipelineModule.java index bac1b3ce0..488f0b390 100644 --- a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/data/ConfigAndOperationalPipelineModule.java +++ b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/data/ConfigAndOperationalPipelineModule.java @@ -24,9 +24,11 @@ import io.fd.honeycomb.data.ReadableDataManager; import io.fd.honeycomb.data.init.DataTreeInitializer; import io.fd.honeycomb.infra.distro.data.config.WriterRegistryProvider; import io.fd.honeycomb.infra.distro.data.oper.ReadableDTDelegProvider; +import io.fd.honeycomb.infra.distro.data.oper.ReaderRegistryBuilderProvider; import io.fd.honeycomb.infra.distro.data.oper.ReaderRegistryProvider; import io.fd.honeycomb.infra.distro.initializer.PersistedFileInitializerProvider; import io.fd.honeycomb.translate.read.registry.ModifiableReaderRegistryBuilder; +import io.fd.honeycomb.translate.read.registry.ReaderRegistry; import io.fd.honeycomb.translate.write.registry.ModifiableWriterRegistryBuilder; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker; @@ -43,8 +45,10 @@ public class ConfigAndOperationalPipelineModule extends PrivateModule { // Expose registries for plugin reader/writer factories bind(ModifiableWriterRegistryBuilder.class).toProvider(WriterRegistryProvider.class).in(Singleton.class); expose(ModifiableWriterRegistryBuilder.class); - bind(ModifiableReaderRegistryBuilder.class).toProvider(ReaderRegistryProvider.class).in(Singleton.class); + bind(ModifiableReaderRegistryBuilder.class).toProvider(ReaderRegistryBuilderProvider.class).in(Singleton.class); expose(ModifiableReaderRegistryBuilder.class); + bind(ReaderRegistry.class).toProvider(ReaderRegistryProvider.class).in(Singleton.class); + expose(ReaderRegistry.class); // Non persisting data tree for config bind(DataTree.class).annotatedWith(Names.named(HONEYCOMB_CONFIG_NONPERSIST)) diff --git a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/data/oper/ReadableDTDelegProvider.java b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/data/oper/ReadableDTDelegProvider.java index 13e82e85f..caac736b5 100644 --- a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/data/oper/ReadableDTDelegProvider.java +++ b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/data/oper/ReadableDTDelegProvider.java @@ -22,8 +22,7 @@ import io.fd.honeycomb.data.ReadableDataManager; import io.fd.honeycomb.data.impl.ReadableDataTreeDelegator; import io.fd.honeycomb.infra.distro.ProviderTrait; import io.fd.honeycomb.infra.distro.data.context.ContextPipelineModule; -import io.fd.honeycomb.translate.read.registry.ModifiableReaderRegistryBuilder; -import io.fd.honeycomb.translate.read.registry.ReaderRegistryBuilder; +import io.fd.honeycomb.translate.read.registry.ReaderRegistry; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec; import org.opendaylight.controller.sal.core.api.model.SchemaService; @@ -35,7 +34,7 @@ public final class ReadableDTDelegProvider extends ProviderTrait { + + @Inject(optional = true) + private Set readerFactories = new HashSet<>(); + + @Override + protected CompositeReaderRegistryBuilder create() { + final CompositeReaderRegistryBuilder builder = new CompositeReaderRegistryBuilder(); + readerFactories.stream() + .forEach(it -> it.init(builder)); + return builder; + } + +} diff --git a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/data/oper/ReaderRegistryProvider.java b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/data/oper/ReaderRegistryProvider.java index ef09e0e97..8370da8bb 100644 --- a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/data/oper/ReaderRegistryProvider.java +++ b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/data/oper/ReaderRegistryProvider.java @@ -18,23 +18,18 @@ package io.fd.honeycomb.infra.distro.data.oper; 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 java.util.HashSet; -import java.util.Set; +import io.fd.honeycomb.translate.read.registry.ReaderRegistry; +import io.fd.honeycomb.translate.read.registry.ReaderRegistryBuilder; -public final class ReaderRegistryProvider extends ProviderTrait { +public final class ReaderRegistryProvider extends ProviderTrait { - @Inject(optional = true) - private Set readerFactories = new HashSet<>(); + @Inject + private ModifiableReaderRegistryBuilder readerRegistryBuilder; @Override - protected CompositeReaderRegistryBuilder create() { - final CompositeReaderRegistryBuilder builder = new CompositeReaderRegistryBuilder(); - readerFactories.stream() - .forEach(it -> it.init(builder)); - return builder; + protected ReaderRegistry create() { + return ((ReaderRegistryBuilder) readerRegistryBuilder).build(); } } diff --git a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/initializer/InitializerPipelineModule.java b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/initializer/InitializerPipelineModule.java index f5240093e..e0c38f38b 100644 --- a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/initializer/InitializerPipelineModule.java +++ b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/initializer/InitializerPipelineModule.java @@ -42,7 +42,7 @@ public final class InitializerPipelineModule extends PrivateModule { // Create initializer registry so that plugins can provide their initializers bind(InitializerRegistry.class).annotatedWith(Names.named(HONEYCOMB_INITIALIZER)) - .toProvider(InitializerRegistryProvider.class).in(Singleton.class); + .toProvider(InitializerRegistryAdapterProvider.class).in(Singleton.class); expose(InitializerRegistry.class).annotatedWith(Names.named(HONEYCOMB_INITIALIZER)); } } diff --git a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/initializer/InitializerRegistryAdapter.java b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/initializer/InitializerRegistryAdapter.java index 8add9fdf0..85fb7772c 100644 --- a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/initializer/InitializerRegistryAdapter.java +++ b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/initializer/InitializerRegistryAdapter.java @@ -16,12 +16,14 @@ package io.fd.honeycomb.infra.distro.initializer; -import com.google.common.collect.Lists; import io.fd.honeycomb.data.init.DataTreeInitializer; import io.fd.honeycomb.data.init.InitializerRegistry; -import io.fd.honeycomb.data.init.InitializerRegistryImpl; -import java.util.List; -import java.util.Set; +import io.fd.honeycomb.translate.MappingContext; +import io.fd.honeycomb.translate.ModificationCache; +import io.fd.honeycomb.translate.read.ReadContext; +import io.fd.honeycomb.translate.read.registry.InitRegistry; +import javax.annotation.Nonnull; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,27 +33,33 @@ final class InitializerRegistryAdapter implements InitializerRegistry { private final DataTreeInitializer configInitializer; private final DataTreeInitializer contextInitializer; - private final List pluginInitializers; + private final InitRegistry initRegistry; + private final DataBroker dataBroker; + private final MappingContext realtimeMappingContext; - InitializerRegistryAdapter(final DataTreeInitializer configInitializer, final DataTreeInitializer contextInitializer, - final Set pluginInitializers) { + InitializerRegistryAdapter(final DataTreeInitializer configInitializer, + final DataTreeInitializer contextInitializer, + final InitRegistry initRegistry, + final DataBroker noopConfigDataBroker, + final MappingContext realtimeMappingContext) { this.configInitializer = configInitializer; this.contextInitializer = contextInitializer; - this.pluginInitializers = Lists.newArrayList(pluginInitializers); + this.initRegistry = initRegistry; + this.dataBroker = noopConfigDataBroker; + this.realtimeMappingContext = realtimeMappingContext; } @Override public void initialize() throws DataTreeInitializer.InitializeException { LOG.info("Config initialization started"); - final InitializerRegistry initializer = new InitializerRegistryImpl(pluginInitializers); try { // Initialize contexts first so that other initializers can find any relevant mapping before initializing // configuration to what is already in VPP contextInitializer.initialize(); LOG.info("Persisted context restored successfully"); // Initialize all registered initializers - initializer.initialize(); + initRegistry.initAll(dataBroker, new InitReadContext(realtimeMappingContext)); LOG.info("Configuration initialized successfully"); // Initialize stored configuration on top configInitializer.initialize(); @@ -63,4 +71,31 @@ final class InitializerRegistryAdapter implements InitializerRegistry { LOG.info("Honeycomb initialized"); } + private static final class InitReadContext implements ReadContext { + + private final ModificationCache modificationCache; + private final MappingContext realtimeMappingContext; + + InitReadContext(final MappingContext realtimeMappingContext) { + modificationCache = new ModificationCache(); + this.realtimeMappingContext = realtimeMappingContext; + } + + @Nonnull + @Override + public ModificationCache getModificationCache() { + return modificationCache; + } + + @Nonnull + @Override + public MappingContext getMappingContext() { + return realtimeMappingContext; + } + + @Override + public void close() { + modificationCache.close(); + } + } } diff --git a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/initializer/InitializerRegistryAdapterProvider.java b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/initializer/InitializerRegistryAdapterProvider.java new file mode 100644 index 000000000..f66284b19 --- /dev/null +++ b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/initializer/InitializerRegistryAdapterProvider.java @@ -0,0 +1,54 @@ +/* + * 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.infra.distro.initializer; + +import static io.fd.honeycomb.infra.distro.data.context.ContextPipelineModule.HONEYCOMB_CONTEXT; +import static io.fd.honeycomb.infra.distro.initializer.InitializerPipelineModule.HONEYCOMB_INITIALIZER; + +import com.google.inject.Inject; +import com.google.inject.name.Named; +import io.fd.honeycomb.data.init.DataTreeInitializer; +import io.fd.honeycomb.data.init.InitializerRegistry; +import io.fd.honeycomb.infra.distro.ProviderTrait; +import io.fd.honeycomb.infra.distro.data.ConfigAndOperationalPipelineModule; +import io.fd.honeycomb.translate.MappingContext; +import io.fd.honeycomb.translate.read.registry.ReaderRegistry; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; + +public final class InitializerRegistryAdapterProvider extends ProviderTrait { + + @Inject + @Named(HONEYCOMB_CONTEXT) + private DataTreeInitializer contextInitializer; + @Inject + @Named(ConfigAndOperationalPipelineModule.HONEYCOMB_CONFIG) + private DataTreeInitializer configInitializer; + @Inject + private ReaderRegistry initRegistry; + @Inject + @Named(HONEYCOMB_INITIALIZER) + private DataBroker noopConfigDataBroker; + @Inject + @Named(HONEYCOMB_CONTEXT) + private MappingContext realtimeMappingContext; + + @Override + protected InitializerRegistryAdapter create() { + return new InitializerRegistryAdapter(configInitializer, contextInitializer, initRegistry, + noopConfigDataBroker, realtimeMappingContext); + } +} diff --git a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/initializer/InitializerRegistryProvider.java b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/initializer/InitializerRegistryProvider.java deleted file mode 100644 index a0ca925c1..000000000 --- a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/initializer/InitializerRegistryProvider.java +++ /dev/null @@ -1,44 +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.infra.distro.initializer; - -import com.google.inject.Inject; -import com.google.inject.name.Named; -import io.fd.honeycomb.data.init.DataTreeInitializer; -import io.fd.honeycomb.data.init.InitializerRegistry; -import io.fd.honeycomb.infra.distro.ProviderTrait; -import io.fd.honeycomb.infra.distro.data.ConfigAndOperationalPipelineModule; -import io.fd.honeycomb.infra.distro.data.context.ContextPipelineModule; -import java.util.HashSet; -import java.util.Set; - -public final class InitializerRegistryProvider extends ProviderTrait { - - @Inject - @Named(ContextPipelineModule.HONEYCOMB_CONTEXT) - private DataTreeInitializer contextInitializer; - @Inject - @Named(ConfigAndOperationalPipelineModule.HONEYCOMB_CONFIG) - private DataTreeInitializer configInitializer; - @Inject(optional = true) - private Set pluginInitializers = new HashSet<>(); - - @Override - protected InitializerRegistryAdapter create() { - return new InitializerRegistryAdapter(configInitializer, contextInitializer, pluginInitializers); - } -} diff --git a/infra/translate-api/pom.xml b/infra/translate-api/pom.xml index 374ed6c3e..40b37cc3f 100644 --- a/infra/translate-api/pom.xml +++ b/infra/translate-api/pom.xml @@ -38,6 +38,10 @@ org.opendaylight.mdsal mdsal-binding-api + + org.opendaylight.controller + sal-binding-api + junit diff --git a/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/InitFailedException.java b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/InitFailedException.java new file mode 100644 index 000000000..4e057c51e --- /dev/null +++ b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/InitFailedException.java @@ -0,0 +1,30 @@ +/* + * 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.read; + +import javax.annotation.Nonnull; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +/** + * Exception indicating a failure during the initialization process. + */ +public class InitFailedException extends ReadFailedException { + + public InitFailedException(@Nonnull final InstanceIdentifier failedId, final Throwable cause) { + super("Failed to initialize: ", failedId, cause); + } +} diff --git a/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/InitListReader.java b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/InitListReader.java new file mode 100644 index 000000000..a7d3ee2ac --- /dev/null +++ b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/InitListReader.java @@ -0,0 +1,31 @@ +/* + * 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.read; + +import com.google.common.annotations.Beta; +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; + +/** + * Special type of list reader capable of participating in the initialization process. + */ +@Beta +public interface InitListReader, K extends Identifier, B extends Builder> + extends ListReader, Initializer { +} diff --git a/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/InitReader.java b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/InitReader.java new file mode 100644 index 000000000..e5b81b947 --- /dev/null +++ b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/InitReader.java @@ -0,0 +1,29 @@ +/* + * 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.read; + +import com.google.common.annotations.Beta; +import org.opendaylight.yangtools.concepts.Builder; +import org.opendaylight.yangtools.yang.binding.DataObject; + +/** + * Special type of reader capable of participating in the initialization process. + */ +@Beta +public interface InitReader> + extends Reader, Initializer { +} diff --git a/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/Initializer.java b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/Initializer.java new file mode 100644 index 000000000..e26ab55bc --- /dev/null +++ b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/Initializer.java @@ -0,0 +1,42 @@ +/* + * 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.read; + +import com.google.common.annotations.Beta; +import javax.annotation.Nonnull; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +/** + * Generic initializer. + *

+ * Capable of invoking 0..n edits as a result of existing operational data. + */ +@Beta +public interface Initializer { + + /** + * Transform operational data located under provided (keyed) id. + * + * @param broker DataBroker that accepts the resulting config data + * @param id InstanceIdentifier of the operational data to initialize from + * @param ctx Standard read context to assist during initialization e.g. caching data between customizers + */ + void init(@Nonnull DataBroker broker, @Nonnull InstanceIdentifier id, @Nonnull ReadContext ctx) + throws InitFailedException; +} diff --git a/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/ReadFailedException.java b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/ReadFailedException.java index fc429b1d0..349e27451 100644 --- a/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/ReadFailedException.java +++ b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/ReadFailedException.java @@ -36,7 +36,13 @@ public class ReadFailedException extends TranslationException { * @param cause the cause of read failure */ public ReadFailedException(@Nonnull final InstanceIdentifier failedId, final Throwable cause) { - super("Failed to read " + failedId, cause); + this("Failed to read: ", failedId, cause); + } + + protected ReadFailedException(@Nonnull final String msgPrefix, + @Nonnull final InstanceIdentifier failedId, + @Nonnull final Throwable cause) { + super(msgPrefix + failedId, cause); this.failedId = checkNotNull(failedId, "failedId should not be null"); } diff --git a/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/registry/InitRegistry.java b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/registry/InitRegistry.java new file mode 100644 index 000000000..8a97a7141 --- /dev/null +++ b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/registry/InitRegistry.java @@ -0,0 +1,34 @@ +/* + * 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.read.registry; + + +import io.fd.honeycomb.translate.read.InitFailedException; +import io.fd.honeycomb.translate.read.ReadContext; +import javax.annotation.Nonnull; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; + +/** + * Root initializer. + */ +public interface InitRegistry { + + /** + * Perform initialization on top of the data root. + */ + void initAll(@Nonnull DataBroker broker, @Nonnull ReadContext ctx) throws InitFailedException; +} diff --git a/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/registry/ReaderRegistry.java b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/registry/ReaderRegistry.java index 9a1c99508..6436fd738 100644 --- a/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/registry/ReaderRegistry.java +++ b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/registry/ReaderRegistry.java @@ -29,7 +29,7 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; * Simple delegating reader suitable as a holder for all other root readers, providing readAll feature. */ @Beta -public interface ReaderRegistry { +public interface ReaderRegistry extends InitRegistry { /** * Performs read on all registered root readers and merges the results into a Multimap. Keys represent identifiers diff --git a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericInitListReader.java b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericInitListReader.java new file mode 100644 index 000000000..cabf31633 --- /dev/null +++ b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericInitListReader.java @@ -0,0 +1,82 @@ +/* + * 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; + +import static io.fd.honeycomb.translate.impl.read.GenericInitReader.writeInit; + +import com.google.common.base.Optional; +import io.fd.honeycomb.translate.read.InitFailedException; +import io.fd.honeycomb.translate.read.InitListReader; +import io.fd.honeycomb.translate.read.ReadContext; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.spi.read.Initialized; +import io.fd.honeycomb.translate.spi.read.InitializingListReaderCustomizer; +import io.fd.honeycomb.translate.util.RWUtils; +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; + +public final class GenericInitListReader, K extends Identifier, B extends Builder> + extends GenericListReader + implements InitListReader { + + private static final Logger LOG = LoggerFactory.getLogger(GenericInitListReader.class); + + public GenericInitListReader(@Nonnull final InstanceIdentifier id, + @Nonnull final InitializingListReaderCustomizer customizer) { + super(id, customizer); + } + + @Override + public void init(final DataBroker broker, final InstanceIdentifier id, final ReadContext ctx) + throws InitFailedException { + LOG.debug("{}: Initializing current: {}", this, id); + + try { + for (K k : getAllIds(id, ctx)) { + initSingle(broker, RWUtils.replaceLastInId(id, RWUtils.getCurrentIdItem(id, k)), ctx); + } + } catch (ReadFailedException e) { + LOG.warn("{}: Failed to initialize current, unable to read: {}", this, id, e); + throw new InitFailedException(e.getFailedId(), e); + } + } + + private void initSingle(final DataBroker broker, final InstanceIdentifier id, final ReadContext ctx) + throws InitFailedException { + LOG.debug("{}: Initializing current: {}", this, id); + + try { + final Optional operational = readCurrent(id, ctx); + if (operational.isPresent()) { + final Initialized init = + ((InitializingListReaderCustomizer) customizer).init(id, operational.get(), ctx); + LOG.debug("{}: Writing init config : {} at: {}", GenericInitListReader.this, init.getData(), init.getId()); + writeInit(broker, init); + } + } catch (ReadFailedException e) { + LOG.warn("{}: Failed to initialize current, unable to read: {}", this, id, e); + throw new InitFailedException(e.getFailedId(), e); + } + } +} diff --git a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericInitReader.java b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericInitReader.java new file mode 100644 index 000000000..024e0a815 --- /dev/null +++ b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericInitReader.java @@ -0,0 +1,73 @@ +/* + * 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; + +import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION; + +import com.google.common.base.Optional; +import io.fd.honeycomb.translate.read.InitFailedException; +import io.fd.honeycomb.translate.read.Initializer; +import io.fd.honeycomb.translate.read.ReadContext; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.spi.read.Initialized; +import io.fd.honeycomb.translate.spi.read.InitializingReaderCustomizer; +import javax.annotation.Nonnull; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; +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; + +public final class GenericInitReader> + extends GenericReader + implements Initializer { + + private static final Logger LOG = LoggerFactory.getLogger(GenericInitReader.class); + + public GenericInitReader(@Nonnull final InstanceIdentifier id, + @Nonnull final InitializingReaderCustomizer customizer) { + super(id, customizer); + } + + @Override + public void init(final DataBroker broker, final InstanceIdentifier id, final ReadContext ctx) throws + InitFailedException { + LOG.debug("{}: Initializing current: {}", this, id); + + try { + final Optional operational = readCurrent(id, ctx); + if (operational.isPresent()) { + final Initialized init = + ((InitializingReaderCustomizer) customizer).init(id, operational.get(), ctx); + LOG.debug("{}: Writing init config : {} at: {}", GenericInitReader.this, init.getData(), init.getId()); + writeInit(broker, init); + } + } catch (ReadFailedException e) { + LOG.warn("{}: Failed to initialize current, unable to read: {}", this, id, e); + throw new InitFailedException(e.getFailedId(), e); + } + } + + @SuppressWarnings("unchecked") + static void writeInit(final DataBroker broker, final Initialized init) { + final WriteTransaction writeTx = broker.newWriteOnlyTransaction(); + writeTx.merge(CONFIGURATION, (InstanceIdentifier) init.getId(), init.getData(), true); + writeTx.submit(); + } +} 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 d8accec91..be6c1b638 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 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 Cisco and/or its affiliates. + * Copyright (c) {today.year} 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. @@ -47,12 +47,12 @@ import org.slf4j.LoggerFactory; */ @Beta @ThreadSafe -public final class GenericListReader, K extends Identifier, B extends Builder> +public class GenericListReader, K extends Identifier, B extends Builder> extends AbstractGenericReader implements ListReader { private static final Logger LOG = LoggerFactory.getLogger(GenericListReader.class); - private final ListReaderCustomizer customizer; + protected final ListReaderCustomizer customizer; /** * Create new {@link GenericListReader} @@ -113,6 +113,7 @@ public final class GenericListReader, K e } } + @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 f60c8f137..e76b6e9a3 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 @@ -18,9 +18,9 @@ package io.fd.honeycomb.translate.impl.read; import com.google.common.annotations.Beta; 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.spi.read.ReaderCustomizer; -import io.fd.honeycomb.translate.read.ReadFailedException; import io.fd.honeycomb.translate.util.read.AbstractGenericReader; import javax.annotation.Nonnull; import javax.annotation.concurrent.ThreadSafe; @@ -33,15 +33,16 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; */ @Beta @ThreadSafe -public final class GenericReader> extends AbstractGenericReader - implements Reader { +public class GenericReader> + extends AbstractGenericReader + implements Reader { - private final ReaderCustomizer customizer; + protected final ReaderCustomizer customizer; /** * Create a new {@link GenericReader}. * - * @param id Instance identifier for managed data type + * @param id Instance identifier for managed data type * @param customizer Customizer instance to customize this generic reader */ public GenericReader(@Nonnull final InstanceIdentifier id, @@ -61,6 +62,7 @@ public final class GenericReader> ext } } + @Nonnull @Override public B getBuilder(@Nonnull final InstanceIdentifier id) { return customizer.getBuilder(id); diff --git a/infra/translate-spi/src/main/java/io/fd/honeycomb/translate/spi/read/Initialized.java b/infra/translate-spi/src/main/java/io/fd/honeycomb/translate/spi/read/Initialized.java new file mode 100644 index 000000000..94fb3cd98 --- /dev/null +++ b/infra/translate-spi/src/main/java/io/fd/honeycomb/translate/spi/read/Initialized.java @@ -0,0 +1,77 @@ +/* + * 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.spi.read; + +import com.google.common.base.Preconditions; +import java.util.Objects; +import javax.annotation.Nonnull; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +/** + * Just a DTO holding configuration data ID and the data itself. + */ +public final class Initialized { + + private final InstanceIdentifier id; + private final T data; + + private Initialized(final InstanceIdentifier id, final T data) { + this.id = id; + this.data = data; + } + + public InstanceIdentifier getId() { + return id; + } + + public T getData() { + return data; + } + + @Override + public boolean equals(final Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + final Initialized that = (Initialized) o; + return Objects.equals(id, that.id) && + Objects.equals(data, that.data); + } + + @Override + public int hashCode() { + return Objects.hash(id, data); + } + + @Override + public String toString() { + return "Initialized{" + + "id=" + id + + ", data=" + data + + '}'; + } + + public static Initialized create(@Nonnull final InstanceIdentifier id, + @Nonnull final C data) { + return new Initialized<>(Preconditions.checkNotNull(id, "Id cannot be null"), + Preconditions.checkNotNull(data, "Data cannot be null")); + } +} diff --git a/infra/translate-spi/src/main/java/io/fd/honeycomb/translate/spi/read/InitializingCustomizer.java b/infra/translate-spi/src/main/java/io/fd/honeycomb/translate/spi/read/InitializingCustomizer.java new file mode 100644 index 000000000..ce4b2a6e9 --- /dev/null +++ b/infra/translate-spi/src/main/java/io/fd/honeycomb/translate/spi/read/InitializingCustomizer.java @@ -0,0 +1,45 @@ +/* + * 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.spi.read; + +import io.fd.honeycomb.translate.read.ReadContext; +import javax.annotation.Nonnull; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +/** + * ReadCustomizers which want to participate in the initializing process need to implement this interface. + *

+ * It is triggered after Honeycomb initializes the plugins to give them a change + * reconcile(put data in HC in sync with underlying layer) with the underlying layer. + */ +public interface InitializingCustomizer { + + /** + * Transform Operational data into Config data. + * + * @param id InstanceIdentifier of operational data being initialized + * @param readValue Operational data being initialized(converted into config) + * @param ctx Standard read context to assist during initialization e.g. caching data between customizers + * + * @return Initialized, config data and its identifier + */ + @Nonnull + Initialized init(@Nonnull InstanceIdentifier id, + @Nonnull O readValue, + @Nonnull ReadContext ctx); +} diff --git a/infra/translate-spi/src/main/java/io/fd/honeycomb/translate/spi/read/InitializingListReaderCustomizer.java b/infra/translate-spi/src/main/java/io/fd/honeycomb/translate/spi/read/InitializingListReaderCustomizer.java new file mode 100644 index 000000000..20cc83364 --- /dev/null +++ b/infra/translate-spi/src/main/java/io/fd/honeycomb/translate/spi/read/InitializingListReaderCustomizer.java @@ -0,0 +1,29 @@ +/* + * 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.spi.read; + +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; + +/** + * Special type of list customizer capable of participating in the initialization process. + */ +public interface InitializingListReaderCustomizer, K extends Identifier, B extends Builder> + extends ListReaderCustomizer, InitializingCustomizer { +} diff --git a/infra/translate-spi/src/main/java/io/fd/honeycomb/translate/spi/read/InitializingReaderCustomizer.java b/infra/translate-spi/src/main/java/io/fd/honeycomb/translate/spi/read/InitializingReaderCustomizer.java new file mode 100644 index 000000000..dccc920ef --- /dev/null +++ b/infra/translate-spi/src/main/java/io/fd/honeycomb/translate/spi/read/InitializingReaderCustomizer.java @@ -0,0 +1,27 @@ +/* + * 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.spi.read; + +import org.opendaylight.yangtools.concepts.Builder; +import org.opendaylight.yangtools.yang.binding.DataObject; + +/** + * Special type of customizer capable of participating in the initialization process. + */ +public interface InitializingReaderCustomizer> + extends ReaderCustomizer, InitializingCustomizer { +} diff --git a/infra/translate-spi/src/main/java/io/fd/honeycomb/translate/spi/read/ListReaderCustomizer.java b/infra/translate-spi/src/main/java/io/fd/honeycomb/translate/spi/read/ListReaderCustomizer.java index 7c64e7f7b..fab91ffc7 100644 --- a/infra/translate-spi/src/main/java/io/fd/honeycomb/translate/spi/read/ListReaderCustomizer.java +++ b/infra/translate-spi/src/main/java/io/fd/honeycomb/translate/spi/read/ListReaderCustomizer.java @@ -31,13 +31,13 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; /** * CompositeListReader SPI to customize its behavior. * - * @param Specific DataObject derived type (Identifiable), that is handled by this customizer + * @param Specific DataObject derived type (Identifiable), that is handled by this customizer * @param Specific Identifier for handled type (C) * @param Specific Builder for handled type (C) */ @Beta -public interface ListReaderCustomizer, K extends Identifier, B extends Builder> - extends ReaderCustomizer { +public interface ListReaderCustomizer, K extends Identifier, B extends Builder> + extends ReaderCustomizer { /** * Return list with IDs of all list nodes to be read. @@ -47,16 +47,16 @@ public interface ListReaderCustomizer, K * @throws ReadFailedException if the list of IDs could not be read */ @Nonnull - List getAllIds(@Nonnull final InstanceIdentifier id, @Nonnull final ReadContext context) throws + List getAllIds(@Nonnull final InstanceIdentifier id, @Nonnull final ReadContext context) throws ReadFailedException; /** * Merge read data into provided parent builder. */ - void merge(@Nonnull final Builder builder, @Nonnull final List readData); + void merge(@Nonnull final Builder builder, @Nonnull final List readData); @Override - default void merge(@Nonnull final Builder parentBuilder, @Nonnull final C readValue) { + default void merge(@Nonnull final Builder parentBuilder, @Nonnull final O readValue) { merge(parentBuilder, Collections.singletonList(readValue)); } } 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 a13e518a6..acfabf5bc 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 @@ -27,17 +27,17 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; /** * CompositeChildReader SPI to customize its behavior. * - * @param Specific DataObject derived type (Identifiable), that is handled by this customizer + * @param Specific DataObject derived type (Identifiable), that is handled by this customizer * @param Specific Builder for handled type (C) */ @Beta -public interface ReaderCustomizer> { +public interface ReaderCustomizer> { /** * Creates new builder that will be used to build read value. */ @Nonnull - B getBuilder(@Nonnull final InstanceIdentifier id); + B getBuilder(@Nonnull final InstanceIdentifier id); /** * Adds current data (identified by id) to the provided builder. @@ -47,14 +47,14 @@ public interface ReaderCustomizer> { * @param ctx context for current read * @throws ReadFailedException if read was unsuccessful */ - void readCurrentAttributes(@Nonnull final InstanceIdentifier id, + void readCurrentAttributes(@Nonnull final InstanceIdentifier id, @Nonnull final B builder, @Nonnull final ReadContext ctx) throws ReadFailedException; /** * Merge read data into provided parent builder. */ - void merge(@Nonnull final Builder parentBuilder, @Nonnull final C readValue); + void merge(@Nonnull final Builder parentBuilder, @Nonnull final O readValue); } diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/AbstractSubtreeManagerRegistryBuilderBuilder.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/AbstractSubtreeManagerRegistryBuilderBuilder.java index 07f2bf1ee..6a19ed220 100644 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/AbstractSubtreeManagerRegistryBuilderBuilder.java +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/AbstractSubtreeManagerRegistryBuilderBuilder.java @@ -185,10 +185,10 @@ public abstract class AbstractSubtreeManagerRegistryBuilderBuilder, S> builder = ImmutableMap.builder(); // Iterate writer types according to their relationships from graph handlersRelations.iterator() - .forEachRemaining(writerType -> { + .forEachRemaining(handlerType -> { // There might be types stored just for relationship sake, no real writer, ignoring those - if (handlersMap.containsKey(writerType)) { - builder.put(writerType, handlersMap.get(writerType)); + if (handlersMap.containsKey(handlerType)) { + builder.put(handlerType, handlersMap.get(handlerType)); } }); diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/RWUtils.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/RWUtils.java index 4616f8347..5b0a4919e 100644 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/RWUtils.java +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/RWUtils.java @@ -117,6 +117,15 @@ public final class RWUtils { return (InstanceIdentifier) InstanceIdentifier.create(Iterables.limit(pathArguments, i + 1)); } + /** + * Trim InstanceIdentifier at indexOf(type). + */ + @Nonnull + public static InstanceIdentifier cutId(@Nonnull final InstanceIdentifier id, + @Nonnull final Class type) { + return cutId(id, InstanceIdentifier.create(type)); + } + /** * Create an ordered map from a collection, checking for duplicity in the process. */ 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 index e392228aa..b52fd09ec 100644 --- 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 @@ -22,16 +22,20 @@ 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.Reader; 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; @@ -40,7 +44,9 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -class CompositeReader> extends AbstractGenericReader { +class CompositeReader> + extends AbstractGenericReader + implements Initializer { private static final Logger LOG = LoggerFactory.getLogger(CompositeReader.class); @@ -155,9 +161,27 @@ class CompositeReader> extends Abstra : 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 ListReader { + implements InitListReader { private final ListReader delegate; @@ -190,6 +214,19 @@ class CompositeReader> extends Abstra 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); @@ -201,5 +238,4 @@ class CompositeReader> extends Abstra 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 index ec3ce0605..9505a7730 100644 --- 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 @@ -23,17 +23,20 @@ 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.Reader; -import io.fd.honeycomb.translate.util.RWUtils; 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; @@ -95,6 +98,16 @@ public final class CompositeReaderRegistry implements ReaderRegistry { 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, @@ -114,4 +127,5 @@ public final class CompositeReaderRegistry implements ReaderRegistry { 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 index 3e21bfca3..da9bbe934 100644 --- 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 @@ -17,12 +17,13 @@ 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.ReaderRegistryBuilder; -import io.fd.honeycomb.translate.util.read.ReflexiveReader; 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; @@ -45,7 +46,9 @@ public final class CompositeReaderRegistryBuilder @Override protected Reader> getSubtreeHandler(@Nonnull final Set> handledChildren, @Nonnull final Reader> reader) { - return SubtreeReader.createForReader(handledChildren, reader); + return reader instanceof InitReader + ? InitSubtreeReader.createForReader(handledChildren, reader) + : SubtreeReader.createForReader(handledChildren, reader); } @Override @@ -54,6 +57,8 @@ public final class CompositeReaderRegistryBuilder add(new ReflexiveReader<>(id, builderType)); } + + /** * Create {@link CompositeReaderRegistry} with Readers ordered according to submitted relationships. *

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 new file mode 100644 index 000000000..4edc38f9d --- /dev/null +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/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.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 index 260fb241b..3bc76b19a 100644 --- 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 @@ -49,10 +49,10 @@ class SubtreeReader> implements Reade private static final Logger LOG = LoggerFactory.getLogger(SubtreeReader.class); - private final Reader delegate; + protected final Reader delegate; private final Set> handledChildTypes = new HashSet<>(); - private SubtreeReader(final Reader delegate, Set> handledTypes) { + SubtreeReader(final Reader delegate, Set> handledTypes) { this.delegate = delegate; for (InstanceIdentifier handledType : handledTypes) { // Iid has to start with Reader's handled root type @@ -96,7 +96,7 @@ class SubtreeReader> implements Reade LOG.debug("{}: Subtree: {} read successfully. Result: {}", this, id, readSubtree); return readSubtree; - // Fallback solution, try delegate, maybe it can read the ID + // If child that's handled here is not requested, then delegate should be able to handle the read } else { return delegate.read(id, ctx); } @@ -208,12 +208,12 @@ class SubtreeReader> implements Reade : new SubtreeReader<>(reader, handledChildren); } - private static final class SubtreeListReader, B extends Builder, K extends Identifier> + static class SubtreeListReader, B extends Builder, K extends Identifier> extends SubtreeReader implements ListReader { - private final ListReader delegate; + final ListReader delegate; - private SubtreeListReader(final ListReader delegate, + SubtreeListReader(final ListReader delegate, final Set> handledTypes) { super(delegate, handledTypes); this.delegate = delegate; -- cgit 1.2.3-korg