diff options
33 files changed, 806 insertions, 77 deletions
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<ReadableDataMan @Inject private SchemaService schemaService; @Inject - private ModifiableReaderRegistryBuilder registry; + private ReaderRegistry registry; @Inject @Named(ContextPipelineModule.HONEYCOMB_CONTEXT) private DataBroker contextBroker; @@ -43,6 +42,6 @@ public final class ReadableDTDelegProvider extends ProviderTrait<ReadableDataMan @Override protected ReadableDataTreeDelegator create() { return new ReadableDataTreeDelegator(serializer, schemaService.getGlobalContext(), - ((ReaderRegistryBuilder) registry).build(), contextBroker); + registry, contextBroker); } } 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 new file mode 100644 index 000000000..05177d54d --- /dev/null +++ b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/data/oper/ReaderRegistryBuilderProvider.java @@ -0,0 +1,40 @@ +/* + * 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.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; + +public final class ReaderRegistryBuilderProvider extends ProviderTrait<ModifiableReaderRegistryBuilder> { + + @Inject(optional = true) + private Set<ReaderFactory> 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<ModifiableReaderRegistryBuilder> { +public final class ReaderRegistryProvider extends ProviderTrait<ReaderRegistry> { - @Inject(optional = true) - private Set<ReaderFactory> 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<DataTreeInitializer> pluginInitializers; + private final InitRegistry initRegistry; + private final DataBroker dataBroker; + private final MappingContext realtimeMappingContext; - InitializerRegistryAdapter(final DataTreeInitializer configInitializer, final DataTreeInitializer contextInitializer, - final Set<DataTreeInitializer> 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/InitializerRegistryProvider.java b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/initializer/InitializerRegistryAdapterProvider.java index a0ca925c1..f66284b19 100644 --- 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/InitializerRegistryAdapterProvider.java @@ -16,29 +16,39 @@ 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.infra.distro.data.context.ContextPipelineModule; -import java.util.HashSet; -import java.util.Set; +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 InitializerRegistryProvider extends ProviderTrait<InitializerRegistry> { +public final class InitializerRegistryAdapterProvider extends ProviderTrait<InitializerRegistry> { @Inject - @Named(ContextPipelineModule.HONEYCOMB_CONTEXT) + @Named(HONEYCOMB_CONTEXT) private DataTreeInitializer contextInitializer; @Inject @Named(ConfigAndOperationalPipelineModule.HONEYCOMB_CONFIG) private DataTreeInitializer configInitializer; - @Inject(optional = true) - private Set<DataTreeInitializer> pluginInitializers = new HashSet<>(); + @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, pluginInitializers); + return new InitializerRegistryAdapter(configInitializer, contextInitializer, initRegistry, + noopConfigDataBroker, realtimeMappingContext); } } 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 @@ <groupId>org.opendaylight.mdsal</groupId> <artifactId>mdsal-binding-api</artifactId> </dependency> + <dependency> + <groupId>org.opendaylight.controller</groupId> + <artifactId>sal-binding-api</artifactId> + </dependency> <dependency> <groupId>junit</groupId> 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<O extends DataObject & Identifiable<K>, K extends Identifier<O>, B extends Builder<O>> + extends ListReader<O, K, B>, Initializer<O> { +} 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<O extends DataObject, B extends Builder<O>> + extends Reader<O, B>, Initializer<O> { +} 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. + * <p/> + * Capable of invoking 0..n edits as a result of existing operational data. + */ +@Beta +public interface Initializer<O extends DataObject> { + + /** + * 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<O> 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<O extends DataObject & Identifiable<K>, K extends Identifier<O>, B extends Builder<O>> + extends GenericListReader<O, K, B> + implements InitListReader<O, K, B> { + + private static final Logger LOG = LoggerFactory.getLogger(GenericInitListReader.class); + + public GenericInitListReader(@Nonnull final InstanceIdentifier<O> id, + @Nonnull final InitializingListReaderCustomizer<O, K, B> customizer) { + super(id, customizer); + } + + @Override + public void init(final DataBroker broker, final InstanceIdentifier<O> 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<O> id, final ReadContext ctx) + throws InitFailedException { + LOG.debug("{}: Initializing current: {}", this, id); + + try { + final Optional<O> operational = readCurrent(id, ctx); + if (operational.isPresent()) { + final Initialized<? extends DataObject> init = + ((InitializingListReaderCustomizer<O, K, B>) 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<O extends DataObject, B extends Builder<O>> + extends GenericReader<O, B> + implements Initializer<O> { + + private static final Logger LOG = LoggerFactory.getLogger(GenericInitReader.class); + + public GenericInitReader(@Nonnull final InstanceIdentifier<O> id, + @Nonnull final InitializingReaderCustomizer<O, B> customizer) { + super(id, customizer); + } + + @Override + public void init(final DataBroker broker, final InstanceIdentifier<O> id, final ReadContext ctx) throws + InitFailedException { + LOG.debug("{}: Initializing current: {}", this, id); + + try { + final Optional<O> operational = readCurrent(id, ctx); + if (operational.isPresent()) { + final Initialized<? extends DataObject> init = + ((InitializingReaderCustomizer<O, B>) 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<? extends DataObject> 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<C extends DataObject & Identifiable<K>, K extends Identifier<C>, B extends Builder<C>> +public class GenericListReader<C extends DataObject & Identifiable<K>, K extends Identifier<C>, B extends Builder<C>> extends AbstractGenericReader<C, B> implements ListReader<C, K, B> { private static final Logger LOG = LoggerFactory.getLogger(GenericListReader.class); - private final ListReaderCustomizer<C, K, B> customizer; + protected final ListReaderCustomizer<C, K, B> customizer; /** * Create new {@link GenericListReader} @@ -113,6 +113,7 @@ public final class GenericListReader<C extends DataObject & Identifiable<K>, K e } } + @Nonnull @Override public B getBuilder(@Nonnull final InstanceIdentifier<C> 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<C extends DataObject, B extends Builder<C>> extends AbstractGenericReader<C, B> - implements Reader<C, B> { +public class GenericReader<C extends DataObject, B extends Builder<C>> + extends AbstractGenericReader<C, B> + implements Reader<C, B> { - private final ReaderCustomizer<C, B> customizer; + protected final ReaderCustomizer<C, B> 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<C> id, @@ -61,6 +62,7 @@ public final class GenericReader<C extends DataObject, B extends Builder<C>> ext } } + @Nonnull @Override public B getBuilder(@Nonnull final InstanceIdentifier<C> 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<T extends DataObject> { + + private final InstanceIdentifier<T> id; + private final T data; + + private Initialized(final InstanceIdentifier<T> id, final T data) { + this.id = id; + this.data = data; + } + + public InstanceIdentifier<T> 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 <C extends DataObject> Initialized<C> create(@Nonnull final InstanceIdentifier<C> 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. + * <p/> + * 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<O extends DataObject> { + + /** + * 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<? extends DataObject> init(@Nonnull InstanceIdentifier<O> 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<O extends DataObject & Identifiable<K>, K extends Identifier<O>, B extends Builder<O>> + extends ListReaderCustomizer<O, K, B>, InitializingCustomizer<O> { +} 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<O extends DataObject, B extends Builder<O>> + extends ReaderCustomizer<O, B>, InitializingCustomizer<O> { +} 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 <C> Specific DataObject derived type (Identifiable), that is handled by this customizer + * @param <O> Specific DataObject derived type (Identifiable), that is handled by this customizer * @param <K> Specific Identifier for handled type (C) * @param <B> Specific Builder for handled type (C) */ @Beta -public interface ListReaderCustomizer<C extends DataObject & Identifiable<K>, K extends Identifier<C>, B extends Builder<C>> - extends ReaderCustomizer<C, B> { +public interface ListReaderCustomizer<O extends DataObject & Identifiable<K>, K extends Identifier<O>, B extends Builder<O>> + extends ReaderCustomizer<O, B> { /** * Return list with IDs of all list nodes to be read. @@ -47,16 +47,16 @@ public interface ListReaderCustomizer<C extends DataObject & Identifiable<K>, K * @throws ReadFailedException if the list of IDs could not be read */ @Nonnull - List<K> getAllIds(@Nonnull final InstanceIdentifier<C> id, @Nonnull final ReadContext context) throws + List<K> getAllIds(@Nonnull final InstanceIdentifier<O> id, @Nonnull final ReadContext context) throws ReadFailedException; /** * Merge read data into provided parent builder. */ - void merge(@Nonnull final Builder<? extends DataObject> builder, @Nonnull final List<C> readData); + void merge(@Nonnull final Builder<? extends DataObject> builder, @Nonnull final List<O> readData); @Override - default void merge(@Nonnull final Builder<? extends DataObject> parentBuilder, @Nonnull final C readValue) { + default void merge(@Nonnull final Builder<? extends DataObject> 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 <C> Specific DataObject derived type (Identifiable), that is handled by this customizer + * @param <O> Specific DataObject derived type (Identifiable), that is handled by this customizer * @param <B> Specific Builder for handled type (C) */ @Beta -public interface ReaderCustomizer<C extends DataObject, B extends Builder<C>> { +public interface ReaderCustomizer<O extends DataObject, B extends Builder<O>> { /** * Creates new builder that will be used to build read value. */ @Nonnull - B getBuilder(@Nonnull final InstanceIdentifier<C> id); + B getBuilder(@Nonnull final InstanceIdentifier<O> id); /** * Adds current data (identified by id) to the provided builder. @@ -47,14 +47,14 @@ public interface ReaderCustomizer<C extends DataObject, B extends Builder<C>> { * @param ctx context for current read * @throws ReadFailedException if read was unsuccessful */ - void readCurrentAttributes(@Nonnull final InstanceIdentifier<C> id, + void readCurrentAttributes(@Nonnull final InstanceIdentifier<O> id, @Nonnull final B builder, @Nonnull final ReadContext ctx) throws ReadFailedException; /** * Merge read data into provided parent builder. */ - void merge(@Nonnull final Builder<? extends DataObject> parentBuilder, @Nonnull final C readValue); + void merge(@Nonnull final Builder<? extends DataObject> 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 extends Sub final ImmutableMap.Builder<InstanceIdentifier<?>, 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 @@ -118,6 +118,15 @@ public final class RWUtils { } /** + * Trim InstanceIdentifier at indexOf(type). + */ + @Nonnull + public static <D extends DataObject> InstanceIdentifier<D> cutId(@Nonnull final InstanceIdentifier<? extends DataObject> id, + @Nonnull final Class<D> type) { + return cutId(id, InstanceIdentifier.create(type)); + } + + /** * Create an ordered map from a collection, checking for duplicity in the process. */ @Nonnull 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<D extends DataObject, B extends Builder<D>> extends AbstractGenericReader<D, B> { +class CompositeReader<D extends DataObject, B extends Builder<D>> + extends AbstractGenericReader<D, B> + implements Initializer<D> { private static final Logger LOG = LoggerFactory.getLogger(CompositeReader.class); @@ -155,9 +161,27 @@ class CompositeReader<D extends DataObject, B extends Builder<D>> extends Abstra : new CompositeReader<>(reader, childReaders); } + @SuppressWarnings("unchecked") + @Override + public void init(final DataBroker broker, final InstanceIdentifier<D> id, final ReadContext ctx) throws InitFailedException { + if (delegate instanceof Initializer) { + LOG.trace("{}: Initializing current: {}", this, id); + ((Initializer<D>) 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<D extends DataObject & Identifiable<K>, B extends Builder<D>, K extends Identifier<D>> extends CompositeReader<D, B> - implements ListReader<D, K, B> { + implements InitListReader<D, K, B> { private final ListReader<D, K, B> delegate; @@ -191,6 +215,19 @@ class CompositeReader<D extends DataObject, B extends Builder<D>> extends Abstra } @Override + public void init(final DataBroker broker, final InstanceIdentifier<D> id, final ReadContext ctx) + throws InitFailedException { + try { + final List<K> 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<? extends DataObject> builder, @Nonnull final List<D> readData) { delegate.merge(builder, readData); } @@ -201,5 +238,4 @@ class CompositeReader<D extends DataObject, B extends Builder<D>> 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<? extends DataObject, ? extends Builder<?>> rootReader : rootReaders.values()) { + if (rootReader instanceof Initializer<?>) { + ((Initializer) rootReader).init(broker, rootReader.getManagedDataObjectType(), ctx); + } + } + } + @Nonnull @Override public Optional<? extends DataObject> read(@Nonnull final InstanceIdentifier<? extends DataObject> 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<? extends DataObject, ? extends Builder<?>> getSubtreeHandler(@Nonnull final Set<InstanceIdentifier<?>> handledChildren, @Nonnull final Reader<? extends DataObject, ? extends Builder<?>> 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. * <p/> 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<O extends DataObject, B extends Builder<O>> + extends SubtreeReader<O, B> + implements InitReader<O, B> { + + private InitSubtreeReader(final InitReader<O, B> delegate, + final Set<InstanceIdentifier<?>> handledTypes) { + super(delegate, handledTypes); + } + + @Override + public void init(final DataBroker broker, final InstanceIdentifier<O> id, final ReadContext ctx) throws InitFailedException { + ((InitReader<O, B>) delegate).init(broker, id, ctx); + } + + /** + * Wrap a Reader as an initializing subtree Reader. + */ + static <D extends DataObject, B extends Builder<D>> Reader<D, B> createForReader(@Nonnull final Set<InstanceIdentifier<?>> handledChildren, + @Nonnull final Reader<D, B> reader) { + return (reader instanceof ListReader) + ? new InitSubtreeListReader<>((InitListReader) reader, handledChildren) + : new InitSubtreeReader<>(((InitReader<D, B>) reader), handledChildren); + } + + private static class InitSubtreeListReader<D extends DataObject & Identifiable<K>, B extends Builder<D>, K extends Identifier<D>> + extends SubtreeListReader<D, B, K> + implements InitListReader<D, K, B> { + + InitSubtreeListReader(final InitListReader<D, K, B> delegate, + final Set<InstanceIdentifier<?>> handledTypes) { + super(delegate, handledTypes); + } + + @Override + public void init(final DataBroker broker, final InstanceIdentifier<D> id, final ReadContext ctx) throws InitFailedException { + ((InitListReader<D, K, B>) 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<D extends DataObject, B extends Builder<D>> implements Reade private static final Logger LOG = LoggerFactory.getLogger(SubtreeReader.class); - private final Reader<D, B> delegate; + protected final Reader<D, B> delegate; private final Set<InstanceIdentifier<?>> handledChildTypes = new HashSet<>(); - private SubtreeReader(final Reader<D, B> delegate, Set<InstanceIdentifier<?>> handledTypes) { + SubtreeReader(final Reader<D, B> delegate, Set<InstanceIdentifier<?>> handledTypes) { this.delegate = delegate; for (InstanceIdentifier<?> handledType : handledTypes) { // Iid has to start with Reader's handled root type @@ -96,7 +96,7 @@ class SubtreeReader<D extends DataObject, B extends Builder<D>> 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<D extends DataObject, B extends Builder<D>> implements Reade : new SubtreeReader<>(reader, handledChildren); } - private static final class SubtreeListReader<D extends DataObject & Identifiable<K>, B extends Builder<D>, K extends Identifier<D>> + static class SubtreeListReader<D extends DataObject & Identifiable<K>, B extends Builder<D>, K extends Identifier<D>> extends SubtreeReader<D, B> implements ListReader<D, K, B> { - private final ListReader<D, K, B> delegate; + final ListReader<D, K, B> delegate; - private SubtreeListReader(final ListReader<D, K, B> delegate, + SubtreeListReader(final ListReader<D, K, B> delegate, final Set<InstanceIdentifier<?>> handledTypes) { super(delegate, handledTypes); this.delegate = delegate; |