diff options
Diffstat (limited to 'infra/translate-utils/src/main')
7 files changed, 154 insertions, 18 deletions
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; |