summaryrefslogtreecommitdiffstats
path: root/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read
diff options
context:
space:
mode:
Diffstat (limited to 'infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read')
-rw-r--r--infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/AbstractGenericReader.java90
-rw-r--r--infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/BindingBrokerReader.java96
-rw-r--r--infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/KeepaliveReaderWrapper.java173
-rw-r--r--infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/NoopReaderCustomizer.java34
-rw-r--r--infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveListReaderCustomizer.java65
-rw-r--r--infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveReader.java57
-rw-r--r--infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveReaderCustomizer.java103
-rw-r--r--infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/CompositeReader.java202
-rw-r--r--infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistry.java117
-rw-r--r--infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryBuilder.java109
-rw-r--r--infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/SubtreeReader.java250
-rw-r--r--infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/TypeHierarchy.java101
12 files changed, 1397 insertions, 0 deletions
diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/AbstractGenericReader.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/AbstractGenericReader.java
new file mode 100644
index 000000000..75a2a673c
--- /dev/null
+++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/AbstractGenericReader.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.translate.util.read;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.Optional;
+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 javax.annotation.Nonnull;
+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;
+
+@Beta
+public abstract class AbstractGenericReader<D extends DataObject, B extends Builder<D>> implements Reader<D, B> {
+
+ private static final Logger LOG = LoggerFactory.getLogger(AbstractGenericReader.class);
+
+ private final InstanceIdentifier<D> instanceIdentifier;
+
+ protected AbstractGenericReader(final InstanceIdentifier<D> managedDataObjectType) {
+ this.instanceIdentifier = RWUtils.makeIidWildcarded(managedDataObjectType);
+ }
+
+ @Nonnull
+ @Override
+ public final InstanceIdentifier<D> getManagedDataObjectType() {
+ return instanceIdentifier;
+ }
+
+ /**
+ * @param id {@link InstanceIdentifier} pointing to current node. In case of keyed list, key must be present.
+ *
+ */
+ protected Optional<D> readCurrent(@Nonnull final InstanceIdentifier<D> id,
+ @Nonnull final ReadContext ctx) throws ReadFailedException {
+ LOG.debug("{}: Reading current: {}", this, id);
+ final B builder = getBuilder(id);
+ // Cache empty value to determine if anything has changed later TODO cache in a field
+ final D emptyValue = builder.build();
+
+ LOG.trace("{}: Reading current attributes", this);
+ readCurrentAttributes(id, builder, ctx);
+
+ // Need to check whether anything was filled in to determine if data is present or not.
+ final D built = builder.build();
+ final Optional<D> read = built.equals(emptyValue)
+ ? Optional.absent()
+ : Optional.of(built);
+
+ LOG.debug("{}: Current node read successfully. Result: {}", this, read);
+ return read;
+ }
+
+ @Nonnull
+ @Override
+ @SuppressWarnings("unchecked")
+ public Optional<? extends DataObject> read(@Nonnull final InstanceIdentifier<? extends DataObject> id,
+ @Nonnull final ReadContext ctx)
+ throws ReadFailedException {
+ LOG.trace("{}: Reading : {}", this, id);
+ checkArgument(id.getTargetType().equals(getManagedDataObjectType().getTargetType()));
+ return readCurrent((InstanceIdentifier<D>) id, ctx);
+ }
+
+ @Override
+ public String toString() {
+ return String.format("Reader[%s]", getManagedDataObjectType().getTargetType().getSimpleName());
+ }
+}
diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/BindingBrokerReader.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/BindingBrokerReader.java
new file mode 100644
index 000000000..03df15175
--- /dev/null
+++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/BindingBrokerReader.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.translate.util.read;
+
+import com.google.common.base.Optional;
+import com.google.common.util.concurrent.CheckedFuture;
+import io.fd.honeycomb.translate.read.ReadContext;
+import io.fd.honeycomb.translate.read.Reader;
+import io.fd.honeycomb.translate.read.ReadFailedException;
+import javax.annotation.Nonnull;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.yangtools.concepts.Builder;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+/**
+ * Simple DataBroker backed reader allowing to delegate reads to different brokers.
+ */
+public final class BindingBrokerReader<D extends DataObject, B extends Builder<D>>
+ implements Reader<D, B>, AutoCloseable {
+
+ private final InstanceIdentifier<D> instanceIdentifier;
+ private final DataBroker dataBroker;
+ private final LogicalDatastoreType datastoreType;
+ private final ReflexiveReaderCustomizer<D, B> reflexiveReaderCustomizer;
+
+ public BindingBrokerReader(final InstanceIdentifier<D> instanceIdentifier,
+ final DataBroker dataBroker,
+ final LogicalDatastoreType datastoreType,
+ final Class<B> builderClass) {
+ this.reflexiveReaderCustomizer = new ReflexiveReaderCustomizer<>(instanceIdentifier.getTargetType(), builderClass);
+ this.instanceIdentifier = instanceIdentifier;
+ this.dataBroker = dataBroker;
+ this.datastoreType = datastoreType;
+ }
+
+ @Nonnull
+ @Override
+ public Optional<? extends DataObject> read(@Nonnull final InstanceIdentifier<? extends DataObject> id,
+ @Nonnull final ReadContext ctx) throws ReadFailedException {
+ try (final ReadOnlyTransaction readOnlyTransaction = dataBroker.newReadOnlyTransaction()) {
+ final CheckedFuture<? extends Optional<? extends DataObject>, org.opendaylight.controller.md.sal.common.api.data.ReadFailedException>
+ read = readOnlyTransaction.read(datastoreType, id);
+ try {
+ return read.checkedGet();
+ } catch (org.opendaylight.controller.md.sal.common.api.data.ReadFailedException e) {
+ throw new ReadFailedException(id, e);
+ }
+ }
+ }
+
+ @Override
+ public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder, @Nonnull final D readValue) {
+ reflexiveReaderCustomizer.merge(parentBuilder, readValue);
+ }
+
+ @Nonnull
+ @Override
+ public B getBuilder(final InstanceIdentifier<D> id) {
+ return reflexiveReaderCustomizer.getBuilder(id);
+ }
+
+ @Override
+ public void readCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
+ @Nonnull final B builder,
+ @Nonnull final ReadContext ctx) throws ReadFailedException {
+ throw new UnsupportedOperationException("Not supported");
+ }
+
+ @Nonnull
+ @Override
+ public InstanceIdentifier<D> getManagedDataObjectType() {
+ return instanceIdentifier;
+ }
+
+ @Override
+ public void close() throws Exception {
+ // Noop
+ }
+}
diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/KeepaliveReaderWrapper.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/KeepaliveReaderWrapper.java
new file mode 100644
index 000000000..f8d1458c0
--- /dev/null
+++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/KeepaliveReaderWrapper.java
@@ -0,0 +1,173 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.translate.util.read;
+
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import io.fd.honeycomb.translate.read.ReadContext;
+import io.fd.honeycomb.translate.read.Reader;
+import io.fd.honeycomb.translate.MappingContext;
+import io.fd.honeycomb.translate.ModificationCache;
+import io.fd.honeycomb.translate.read.ReadFailedException;
+import java.io.Closeable;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+import javax.annotation.Nonnegative;
+import javax.annotation.Nonnull;
+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;
+
+/**
+ * Reader wrapper that periodically invokes a read to determine whether reads are still fully functional.
+ * In case a specific error occurs, Keep-alive failure listener gets notified.
+ */
+public final class KeepaliveReaderWrapper<D extends DataObject, B extends Builder<D>> implements Reader<D, B>, Runnable, Closeable {
+
+ private static final Logger LOG = LoggerFactory.getLogger(KeepaliveReaderWrapper.class);
+
+ private static final NoopReadContext CTX = new NoopReadContext();
+
+ private final Reader<D, B> delegate;
+ private final Class<? extends Exception> exceptionType;
+ private final KeepaliveFailureListener failureListener;
+ private final ScheduledFuture<?> scheduledFuture;
+
+ /**
+ * Create new Keepalive wrapper
+ *
+ * @param delegate underlying reader performing actual reads
+ * @param executor scheduled executor service to schedule keepalive calls
+ * @param exception type of exception used to differentiate keepalive exception from other exceptions
+ * @param delayInSeconds number of seconds to wait between keepalive calls
+ * @param failureListener listener to be called whenever a keepalive failure is detected
+ */
+ public KeepaliveReaderWrapper(@Nonnull final Reader<D, B> delegate,
+ @Nonnull final ScheduledExecutorService executor,
+ @Nonnull final Class<? extends Exception> exception,
+ @Nonnegative final int delayInSeconds,
+ @Nonnull final KeepaliveFailureListener failureListener) {
+ this.delegate = delegate;
+ this.exceptionType = exception;
+ this.failureListener = failureListener;
+ Preconditions.checkArgument(delayInSeconds > 0, "Delay cannot be < 0");
+ LOG.debug("Starting keep-alive execution on top of: {} with delay of: {} seconds", delegate, delayInSeconds);
+ scheduledFuture = executor.scheduleWithFixedDelay(this, delayInSeconds, delayInSeconds, TimeUnit.SECONDS);
+ }
+
+ @Nonnull
+ public Optional<? extends DataObject> read(@Nonnull final InstanceIdentifier id,
+ @Nonnull final ReadContext ctx) throws ReadFailedException {
+ return delegate.read(id, ctx);
+ }
+
+ public void readCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
+ @Nonnull final B builder,
+ @Nonnull final ReadContext ctx) throws ReadFailedException {
+ delegate.readCurrentAttributes(id, builder, ctx);
+ }
+
+ @Nonnull
+ public B getBuilder(final InstanceIdentifier<D> id) {
+ return delegate.getBuilder(id);
+ }
+
+ public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder,
+ @Nonnull final D readValue) {
+ delegate.merge(parentBuilder, readValue);
+ }
+
+ @Nonnull
+ @Override
+ public InstanceIdentifier<D> getManagedDataObjectType() {
+ return delegate.getManagedDataObjectType();
+ }
+
+ @Override
+ public void run() {
+ LOG.trace("Invoking keepalive");
+ try {
+ final Optional<? extends DataObject> read = read(delegate.getManagedDataObjectType(), CTX);
+ LOG.debug("Keepalive executed successfully with data: {}", read);
+ } catch (Exception e) {
+ if (exceptionType.isAssignableFrom(e.getClass())) {
+ LOG.warn("Keepalive failed. Notifying listener", e);
+ failureListener.onKeepaliveFailure();
+ }
+ LOG.warn("Keepalive failed unexpectedly", e);
+ throw new IllegalArgumentException("Unexpected failure during keep-alive execution", e);
+ }
+ }
+
+ @Override
+ public void close() {
+ // Do not interrupt, it's not our executor
+ scheduledFuture.cancel(false);
+ }
+
+ /**
+ * Listener that gets called whenever keepalive fails as expected
+ */
+ public interface KeepaliveFailureListener {
+
+ void onKeepaliveFailure();
+ }
+
+ private static final class NoopMappingContext implements MappingContext {
+ @Override
+ public <T extends DataObject> Optional<T> read(@Nonnull final InstanceIdentifier<T> currentId) {
+ return Optional.absent();
+ }
+
+ @Override
+ public void delete(final InstanceIdentifier<?> path) {}
+
+ @Override
+ public <T extends DataObject> void merge(final InstanceIdentifier<T> path, final T data) {}
+
+ @Override
+ public <T extends DataObject> void put(final InstanceIdentifier<T> path, final T data) {}
+
+ @Override
+ public void close() {}
+ }
+
+ private static class NoopReadContext implements ReadContext {
+
+ private final ModificationCache modificationCache = new ModificationCache();
+
+ @Nonnull
+ @Override
+ public ModificationCache getModificationCache() {
+ return modificationCache;
+ }
+
+ @Nonnull
+ @Override
+ public MappingContext getMappingContext() {
+ return new NoopMappingContext();
+ }
+
+ @Override
+ public void close() {
+
+ }
+ }
+}
diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/NoopReaderCustomizer.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/NoopReaderCustomizer.java
new file mode 100644
index 000000000..66bff9383
--- /dev/null
+++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/NoopReaderCustomizer.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.util.read;
+
+import io.fd.honeycomb.translate.read.ReadContext;
+import io.fd.honeycomb.translate.read.ReadFailedException;
+import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
+import org.opendaylight.yangtools.concepts.Builder;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+public abstract class NoopReaderCustomizer<C extends DataObject, B extends Builder<C>> implements
+ ReaderCustomizer<C, B> {
+
+ @Override
+ public void readCurrentAttributes(InstanceIdentifier<C> id, final B builder, final ReadContext context) throws
+ ReadFailedException {
+ // Noop
+ }
+}
diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveListReaderCustomizer.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveListReaderCustomizer.java
new file mode 100644
index 000000000..bf2d109c7
--- /dev/null
+++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveListReaderCustomizer.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.translate.util.read;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+import com.google.common.base.Optional;
+import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer;
+import io.fd.honeycomb.translate.util.ReflectionUtils;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Collections;
+import java.util.List;
+import javax.annotation.Nonnull;
+import org.opendaylight.yangtools.concepts.Builder;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.Identifiable;
+import org.opendaylight.yangtools.yang.binding.Identifier;
+
+/**
+ * Might be slow !
+ */
+public abstract class ReflexiveListReaderCustomizer<C extends DataObject & Identifiable<K>, K extends Identifier<C>, B extends Builder<C>>
+ extends ReflexiveReaderCustomizer<C, B>
+ implements ListReaderCustomizer<C, K, B> {
+
+
+ public ReflexiveListReaderCustomizer(final Class<C> typeClass, final Class<B> builderClass) {
+ super(typeClass, builderClass);
+ }
+
+ @Override
+ public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder, @Nonnull final C readValue) {
+ merge(parentBuilder, Collections.singletonList(readValue));
+ }
+
+ @Override
+ public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder, @Nonnull final List<C> readData) {
+ final Optional<Method> method =
+ ReflectionUtils.findMethodReflex(parentBuilder.getClass(), "set" + getTypeClass().getSimpleName(),
+ Collections.singletonList(List.class), parentBuilder.getClass());
+
+ checkArgument(method.isPresent(), "Unable to set %s to %s", readData, parentBuilder);
+
+ try {
+ method.get().invoke(parentBuilder, readData);
+ } catch (IllegalAccessException | InvocationTargetException e) {
+ throw new IllegalArgumentException("Unable to set " + readData + " to " + parentBuilder, e);
+ }
+ }
+}
diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveReader.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveReader.java
new file mode 100644
index 000000000..2aa35514e
--- /dev/null
+++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveReader.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.translate.util.read;
+
+import io.fd.honeycomb.translate.read.ReadContext;
+import io.fd.honeycomb.translate.read.ReadFailedException;
+import javax.annotation.Nonnull;
+import org.opendaylight.yangtools.concepts.Builder;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+/**
+ * Reader that performs no read operation on its own, just fills in the hierarchy.
+ * <p/>
+ * Might be slow due to reflection !
+ */
+public class ReflexiveReader<C extends DataObject, B extends Builder<C>> extends AbstractGenericReader<C, B> {
+
+ private final ReflexiveReaderCustomizer<C, B> customizer;
+
+ public ReflexiveReader(final InstanceIdentifier<C> identifier, final Class<B> builderClass) {
+ super(identifier);
+ this.customizer = new ReflexiveReaderCustomizer<>(identifier.getTargetType(), builderClass);
+ }
+
+ @Override
+ public void readCurrentAttributes(@Nonnull final InstanceIdentifier<C> id, @Nonnull final B builder,
+ @Nonnull final ReadContext ctx)
+ throws ReadFailedException {
+ customizer.readCurrentAttributes(id, builder, ctx);
+ }
+
+ @Nonnull
+ @Override
+ public B getBuilder(final InstanceIdentifier<C> id) {
+ return customizer.getBuilder(id);
+ }
+
+ @Override
+ public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder, @Nonnull final C readValue) {
+ customizer.merge(parentBuilder, readValue);
+ }
+}
diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveReaderCustomizer.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveReaderCustomizer.java
new file mode 100644
index 000000000..6ceb7a2b1
--- /dev/null
+++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveReaderCustomizer.java
@@ -0,0 +1,103 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.translate.util.read;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.Lists;
+import io.fd.honeycomb.translate.util.ReflectionUtils;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Collections;
+import javax.annotation.Nonnull;
+import org.opendaylight.yangtools.concepts.Builder;
+import org.opendaylight.yangtools.yang.binding.Augmentation;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+/**
+ * Might be slow !
+ */
+class ReflexiveReaderCustomizer<C extends DataObject, B extends Builder<C>> extends NoopReaderCustomizer<C, B> {
+
+ private final Class<C> typeClass;
+ private final Class<B> builderClass;
+
+ public ReflexiveReaderCustomizer(final Class<C> typeClass, final Class<B> builderClass) {
+ this.typeClass = typeClass;
+ this.builderClass = builderClass;
+ }
+
+ protected Class<C> getTypeClass() {
+ return typeClass;
+ }
+
+ protected Class<B> getBuilderClass() {
+ return builderClass;
+ }
+
+ @Nonnull
+ @Override
+ public B getBuilder(@Nonnull InstanceIdentifier<C> id) {
+ try {
+ return builderClass.newInstance();
+ } catch (InstantiationException | IllegalAccessException e) {
+ throw new IllegalStateException("Unable to instantiate " + builderClass, e);
+ }
+ }
+
+ @Override
+ public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder, @Nonnull final C readValue) {
+ if (Augmentation.class.isAssignableFrom(typeClass)) {
+ mergeAugmentation(parentBuilder, (Class<? extends Augmentation<?>>) typeClass, readValue);
+ } else {
+ mergeRegular(parentBuilder, readValue);
+ }
+ }
+
+ private static void mergeRegular(@Nonnull final Builder<? extends DataObject> parentBuilder,
+ @Nonnull final DataObject readValue) {
+ final Optional<Method> method =
+ ReflectionUtils.findMethodReflex(parentBuilder.getClass(), "set",
+ Collections.singletonList(readValue.getClass()), parentBuilder.getClass());
+
+ checkArgument(method.isPresent(), "Unable to set %s to %s", readValue, parentBuilder);
+
+ try {
+ method.get().invoke(parentBuilder, readValue);
+ } catch (IllegalAccessException | InvocationTargetException e) {
+ throw new IllegalArgumentException("Unable to set " + readValue + " to " + parentBuilder, e);
+ }
+ }
+
+ private static void mergeAugmentation(@Nonnull final Builder<? extends DataObject> parentBuilder,
+ @Nonnull final Class<? extends Augmentation<?>> typeClass,
+ @Nonnull final DataObject readValue) {
+ final Optional<Method> method =
+ ReflectionUtils.findMethodReflex(parentBuilder.getClass(), "addAugmentation",
+ Lists.newArrayList(Class.class, Augmentation.class), parentBuilder.getClass());
+
+ checkArgument(method.isPresent(), "Not possible to add augmentations to builder: %s", parentBuilder);
+ try {
+ method.get().invoke(parentBuilder, typeClass, readValue);
+ } catch (IllegalAccessException | InvocationTargetException e) {
+ throw new IllegalArgumentException("Unable to set " + readValue + " to " + parentBuilder, e);
+ }
+ }
+
+}
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
new file mode 100644
index 000000000..91a195f44
--- /dev/null
+++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/CompositeReader.java
@@ -0,0 +1,202 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.translate.util.read.registry;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Iterables;
+import io.fd.honeycomb.translate.read.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.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.yangtools.concepts.Builder;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.Identifiable;
+import org.opendaylight.yangtools.yang.binding.Identifier;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+class CompositeReader<D extends DataObject, B extends Builder<D>> extends AbstractGenericReader<D, B> {
+
+ private static final Logger LOG = LoggerFactory.getLogger(CompositeReader.class);
+
+ private final Reader<D, B> delegate;
+ private final ImmutableMap<Class<?>, Reader<? extends DataObject, ? extends Builder<?>>> childReaders;
+
+ private CompositeReader(final Reader<D, B> reader,
+ final ImmutableMap<Class<?>, Reader<? extends DataObject, ? extends Builder<?>>> childReaders) {
+ super(reader.getManagedDataObjectType());
+ this.delegate = reader;
+ this.childReaders = childReaders;
+ }
+
+ @VisibleForTesting
+ ImmutableMap<Class<?>, Reader<? extends DataObject, ? extends Builder<?>>> getChildReaders() {
+ return childReaders;
+ }
+
+ @SuppressWarnings("unchecked")
+ public static <D extends DataObject> InstanceIdentifier<D> appendTypeToId(
+ final InstanceIdentifier<? extends DataObject> parentId, final InstanceIdentifier<D> type) {
+ final InstanceIdentifier.PathArgument t = new InstanceIdentifier.Item<>(type.getTargetType());
+ return (InstanceIdentifier<D>) InstanceIdentifier.create(Iterables.concat(
+ parentId.getPathArguments(), Collections.singleton(t)));
+ }
+
+ @Nonnull
+ @Override
+ public Optional<? extends DataObject> read(@Nonnull final InstanceIdentifier<? extends DataObject> id,
+ @Nonnull final ReadContext ctx) throws ReadFailedException {
+ if (shouldReadCurrent(id)) {
+ LOG.trace("{}: Reading current: {}", this, id);
+ return readCurrent((InstanceIdentifier<D>) id, ctx);
+ } else if (shouldDelegateToChild(id)) {
+ LOG.trace("{}: Reading child: {}", this, id);
+ return readSubtree(id, ctx);
+ } else {
+ // Fallback
+ LOG.trace("{}: Delegating read: {}", this, id);
+ return delegate.read(id, ctx);
+ }
+ }
+
+ private boolean shouldReadCurrent(@Nonnull final InstanceIdentifier<? extends DataObject> id) {
+ return id.getTargetType().equals(getManagedDataObjectType().getTargetType());
+ }
+
+ private boolean shouldDelegateToChild(@Nonnull final InstanceIdentifier<? extends DataObject> id) {
+ return childReaders.containsKey(RWUtils.getNextId(id, getManagedDataObjectType()).getType());
+ }
+
+ private Optional<? extends DataObject> readSubtree(final InstanceIdentifier<? extends DataObject> id,
+ final ReadContext ctx) throws ReadFailedException {
+ final InstanceIdentifier.PathArgument nextId = RWUtils.getNextId(id, getManagedDataObjectType());
+ final Reader<?, ? extends Builder<?>> nextReader = childReaders.get(nextId.getType());
+ checkArgument(nextReader != null, "Unable to read: %s. No delegate present, available readers at next level: %s",
+ id, childReaders.keySet());
+ return nextReader.read(id, ctx);
+ }
+
+ @SuppressWarnings("unchecked")
+ private void readChildren(final InstanceIdentifier<D> id, @Nonnull final ReadContext ctx, final B builder)
+ throws ReadFailedException {
+ LOG.debug("{}: Reading children: {}", this, childReaders.keySet());
+ for (Reader child : childReaders.values()) {
+ final InstanceIdentifier childId = appendTypeToId(id, child.getManagedDataObjectType());
+
+ LOG.debug("{}: Reading child from: {}", this, child);
+ if (child instanceof ListReader) {
+ final List<? extends DataObject> list = ((ListReader) child).readList(childId, ctx);
+ ((ListReader) child).merge(builder, list);
+ } else {
+ final Optional<? extends DataObject> read = child.read(childId, ctx);
+ if (read.isPresent()) {
+ child.merge(builder, read.get());
+ }
+ }
+ }
+ }
+
+ @Override
+ public void readCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, @Nonnull final B builder,
+ @Nonnull final ReadContext ctx)
+ throws ReadFailedException {
+ delegate.readCurrentAttributes(id, builder, ctx);
+ readChildren(id, ctx, builder);
+ }
+
+ @Nonnull
+ @Override
+ public B getBuilder(final InstanceIdentifier<D> id) {
+ return delegate.getBuilder(id);
+ }
+
+ @Override
+ public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder, @Nonnull final D readValue) {
+ delegate.merge(parentBuilder, readValue);
+ }
+
+ /**
+ * Wrap a Reader as a Composite Reader.
+ */
+ static <D extends DataObject, B extends Builder<D>> Reader<D, B> createForReader(
+ @Nonnull final Reader<D, B> reader,
+ @Nonnull final ImmutableMap<Class<?>, Reader<?, ? extends Builder<?>>> childReaders) {
+
+ return (reader instanceof ListReader)
+ ? new CompositeListReader<>((ListReader) reader, childReaders)
+ : new CompositeReader<>(reader, childReaders);
+ }
+
+ 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> {
+
+ private final ListReader<D, K, B> delegate;
+
+ private CompositeListReader(final ListReader<D, K, B> reader,
+ final ImmutableMap<Class<?>, Reader<? extends DataObject, ? extends Builder<?>>> childReaders) {
+ super(reader, childReaders);
+ this.delegate = reader;
+ }
+
+ @Nonnull
+ @Override
+ public List<D> readList(@Nonnull final InstanceIdentifier<D> id, @Nonnull final ReadContext ctx)
+ throws ReadFailedException {
+ LOG.trace("{}: Reading all list entries", this);
+ final List<K> allIds = delegate.getAllIds(id, ctx);
+ LOG.debug("{}: Reading list entries for: {}", this, allIds);
+
+ // Override read list in order to perform readCurrent + readChildren here
+ final ArrayList<D> allEntries = new ArrayList<>(allIds.size());
+ for (K key : allIds) {
+ final InstanceIdentifier.IdentifiableItem<D, K> currentBdItem = RWUtils.getCurrentIdItem(id, key);
+ final InstanceIdentifier<D> keyedId = RWUtils.replaceLastInId(id, currentBdItem);
+ final Optional<D> read = readCurrent(keyedId, ctx);
+ if (read.isPresent()) {
+ final DataObject singleItem = read.get();
+ checkArgument(getManagedDataObjectType().getTargetType().isAssignableFrom(singleItem.getClass()));
+ allEntries.add(getManagedDataObjectType().getTargetType().cast(singleItem));
+ }
+ }
+ return allEntries;
+ }
+
+ @Override
+ public void merge(@Nonnull final Builder<? extends DataObject> builder, @Nonnull final List<D> readData) {
+ delegate.merge(builder, readData);
+ }
+
+ @Override
+ public List<K> getAllIds(@Nonnull final InstanceIdentifier<D> id,
+ @Nonnull final ReadContext ctx) throws ReadFailedException {
+ return delegate.getAllIds(id, ctx);
+ }
+ }
+
+}
diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistry.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistry.java
new file mode 100644
index 000000000..ec3ce0605
--- /dev/null
+++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistry.java
@@ -0,0 +1,117 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.translate.util.read.registry;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Optional;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.LinkedListMultimap;
+import com.google.common.collect.Multimap;
+import io.fd.honeycomb.translate.read.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.registry.ReaderRegistry;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import javax.annotation.Nonnull;
+import org.opendaylight.yangtools.concepts.Builder;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Simple reader registry able to perform and aggregated read (ROOT read) on top of all provided readers. Also able to
+ * delegate a specific read to one of the delegate readers.
+ * <p/>
+ * This could serve as a utility to hold & hide all available readers in upper layers.
+ */
+public final class CompositeReaderRegistry implements ReaderRegistry {
+
+ private static final Logger LOG = LoggerFactory.getLogger(CompositeReaderRegistry.class);
+
+ private final Map<Class<? extends DataObject>, Reader<? extends DataObject, ? extends Builder<?>>> rootReaders;
+
+ /**
+ * Create new {@link CompositeReaderRegistry}.
+ *
+ * @param rootReaders List of delegate readers
+ */
+ public CompositeReaderRegistry(@Nonnull final List<Reader<? extends DataObject, ? extends Builder<?>>> rootReaders) {
+ this.rootReaders = RWUtils.uniqueLinkedIndex(checkNotNull(rootReaders), RWUtils.MANAGER_CLASS_FUNCTION);
+ }
+
+ @VisibleForTesting
+ Map<Class<? extends DataObject>, Reader<? extends DataObject, ? extends Builder<?>>> getRootReaders() {
+ return rootReaders;
+ }
+
+ @Override
+ @Nonnull
+ public Multimap<InstanceIdentifier<? extends DataObject>, ? extends DataObject> readAll(
+ @Nonnull final ReadContext ctx) throws ReadFailedException {
+
+ LOG.debug("Reading from all delegates: {}", this);
+ LOG.trace("Reading from all delegates: {}", rootReaders.values());
+
+ final Multimap<InstanceIdentifier<? extends DataObject>, DataObject> objects = LinkedListMultimap.create();
+ for (Reader<? extends DataObject, ? extends Builder<?>> rootReader : rootReaders.values()) {
+ LOG.debug("Reading from delegate: {}", rootReader);
+
+ if (rootReader instanceof ListReader) {
+ final List<? extends DataObject> listEntries =
+ ((ListReader) rootReader).readList(rootReader.getManagedDataObjectType(), ctx);
+ if (!listEntries.isEmpty()) {
+ objects.putAll(rootReader.getManagedDataObjectType(), listEntries);
+ }
+ } else {
+ final Optional<? extends DataObject> read = rootReader.read(rootReader.getManagedDataObjectType(), ctx);
+ if (read.isPresent()) {
+ objects.putAll(rootReader.getManagedDataObjectType(), Collections.singletonList(read.get()));
+ }
+ }
+ }
+
+ return objects;
+ }
+
+ @Nonnull
+ @Override
+ public Optional<? extends DataObject> read(@Nonnull final InstanceIdentifier<? extends DataObject> id,
+ @Nonnull final ReadContext ctx)
+ throws ReadFailedException {
+ final InstanceIdentifier.PathArgument first = checkNotNull(
+ Iterables.getFirst(id.getPathArguments(), null), "Empty id");
+ final Reader<? extends DataObject, ? extends Builder<?>> reader = rootReaders.get(first.getType());
+ checkNotNull(reader,
+ "Unable to read %s. Missing reader. Current readers for: %s", id, rootReaders.keySet());
+ LOG.debug("Reading from delegate: {}", reader);
+ return reader.read(id, ctx);
+ }
+
+ @Override
+ public String toString() {
+ return getClass().getSimpleName()
+ + rootReaders.keySet().stream().map(Class::getSimpleName).collect(Collectors.toList());
+ }
+}
diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryBuilder.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryBuilder.java
new file mode 100644
index 000000000..3e21bfca3
--- /dev/null
+++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryBuilder.java
@@ -0,0 +1,109 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.translate.util.read.registry;
+
+import com.google.common.collect.ImmutableMap;
+import io.fd.honeycomb.translate.read.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.util.AbstractSubtreeManagerRegistryBuilderBuilder;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+import javax.annotation.Nonnull;
+import javax.annotation.concurrent.NotThreadSafe;
+import org.opendaylight.yangtools.concepts.Builder;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@NotThreadSafe
+public final class CompositeReaderRegistryBuilder
+ extends AbstractSubtreeManagerRegistryBuilderBuilder<Reader<? extends DataObject, ? extends Builder<?>>, ReaderRegistry>
+ implements ModifiableReaderRegistryBuilder, ReaderRegistryBuilder {
+
+ private static final Logger LOG = LoggerFactory.getLogger(CompositeReaderRegistryBuilder.class);
+
+ @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);
+ }
+
+ @Override
+ public <D extends DataObject> void addStructuralReader(@Nonnull InstanceIdentifier<D> id,
+ @Nonnull Class<? extends Builder<D>> builderType) {
+ add(new ReflexiveReader<>(id, builderType));
+ }
+
+ /**
+ * Create {@link CompositeReaderRegistry} with Readers ordered according to submitted relationships.
+ * <p/>
+ * Note: The ordering only applies between nodes on the same level, inter-level and inter-subtree relationships are
+ * ignored.
+ */
+ @Override
+ public ReaderRegistry build() {
+ ImmutableMap<InstanceIdentifier<?>, Reader<? extends DataObject, ? extends Builder<?>>> mappedReaders =
+ getMappedHandlers();
+ LOG.debug("Building Reader registry with Readers: {}",
+ mappedReaders.keySet().stream()
+ .map(InstanceIdentifier::getTargetType)
+ .map(Class::getSimpleName)
+ .collect(Collectors.joining(", ")));
+
+ LOG.trace("Building Reader registry with Readers: {}", mappedReaders);
+ final List<InstanceIdentifier<?>> readerOrder = new ArrayList<>(mappedReaders.keySet());
+
+ // Wrap readers into composite readers recursively, collect roots and create registry
+ final TypeHierarchy typeHierarchy = TypeHierarchy.create(mappedReaders.keySet());
+ final List<Reader<? extends DataObject, ? extends Builder<?>>> orderedRootReaders =
+ typeHierarchy.getRoots().stream()
+ .map(rootId -> toCompositeReader(rootId, mappedReaders, typeHierarchy))
+ .collect(Collectors.toList());
+
+ // We are violating the ordering from mappedReaders, since we are forming a composite structure
+ // but at least order root writers
+ orderedRootReaders.sort((reader1, reader2) -> readerOrder.indexOf(reader1.getManagedDataObjectType())
+ - readerOrder.indexOf(reader2.getManagedDataObjectType()));
+
+ return new CompositeReaderRegistry(orderedRootReaders);
+ }
+
+ private Reader<? extends DataObject, ? extends Builder<?>> toCompositeReader(
+ final InstanceIdentifier<?> instanceIdentifier,
+ final ImmutableMap<InstanceIdentifier<?>, Reader<? extends DataObject, ? extends Builder<?>>> mappedReaders,
+ final TypeHierarchy typeHierarchy) {
+
+ // Order child readers according to the mappedReadersCollection
+ final ImmutableMap.Builder<Class<?>, Reader<?, ? extends Builder<?>>> childReadersMapB = ImmutableMap.builder();
+ for (InstanceIdentifier<?> childId : mappedReaders.keySet()) {
+ if (typeHierarchy.getDirectChildren(instanceIdentifier).contains(childId)) {
+ childReadersMapB.put(childId.getTargetType(), toCompositeReader(childId, mappedReaders, typeHierarchy));
+ }
+ }
+
+ final ImmutableMap<Class<?>, Reader<?, ? extends Builder<?>>> childReadersMap = childReadersMapB.build();
+ return childReadersMap.isEmpty()
+ ? mappedReaders.get(instanceIdentifier)
+ : CompositeReader.createForReader(mappedReaders.get(instanceIdentifier), childReadersMap);
+ }
+}
diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/SubtreeReader.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/SubtreeReader.java
new file mode 100644
index 000000000..720bd0b11
--- /dev/null
+++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/SubtreeReader.java
@@ -0,0 +1,250 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.translate.util.read.registry;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+import com.google.common.base.Optional;
+import com.google.common.base.Predicate;
+import com.google.common.collect.Iterables;
+import io.fd.honeycomb.translate.read.ListReader;
+import io.fd.honeycomb.translate.read.ReadContext;
+import io.fd.honeycomb.translate.read.ReadFailedException;
+import io.fd.honeycomb.translate.read.Reader;
+import io.fd.honeycomb.translate.util.RWUtils;
+import io.fd.honeycomb.translate.util.ReflectionUtils;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import org.opendaylight.yangtools.concepts.Builder;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.Identifiable;
+import org.opendaylight.yangtools.yang.binding.Identifier;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Simple Reader delegate for subtree Readers (Readers handling also children nodes) providing a list of all the
+ * children nodes being handled.
+ */
+class SubtreeReader<D extends DataObject, B extends Builder<D>> implements Reader<D, B> {
+
+ private static final Logger LOG = LoggerFactory.getLogger(SubtreeReader.class);
+
+ private final Reader<D, B> delegate;
+ private final Set<InstanceIdentifier<?>> handledChildTypes = new HashSet<>();
+
+ private 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
+ checkArgument(delegate.getManagedDataObjectType().getTargetType().equals(
+ handledType.getPathArguments().iterator().next().getType()),
+ "Handled node from subtree has to be identified by an instance identifier starting from: %s."
+ + "Instance identifier was: %s", getManagedDataObjectType().getTargetType(), handledType);
+ checkArgument(Iterables.size(handledType.getPathArguments()) > 1,
+ "Handled node from subtree identifier too short: %s", handledType);
+ handledChildTypes.add(InstanceIdentifier.create(Iterables.concat(
+ getManagedDataObjectType().getPathArguments(), Iterables.skip(handledType.getPathArguments(), 1))));
+ }
+ }
+
+ /**
+ * Return set of types also handled by this Reader. All of the types are children of the type managed by this Reader
+ * excluding the type of this Reader.
+ */
+ Set<InstanceIdentifier<?>> getHandledChildTypes() {
+ return handledChildTypes;
+ }
+
+ @Override
+ @Nonnull
+ public Optional<? extends DataObject> read(
+ @Nonnull final InstanceIdentifier<? extends DataObject> id,
+ @Nonnull final ReadContext ctx) throws ReadFailedException {
+ final InstanceIdentifier<?> wildcarded = RWUtils.makeIidWildcarded(id);
+
+ // Reading entire subtree and filtering if is current reader responsible
+ if (getHandledChildTypes().contains(wildcarded)) {
+ LOG.debug("{}: Subtree node managed by this writer requested: {}. Reading current and filtering", this, id);
+ // If there's no dedicated reader, use read current
+ final InstanceIdentifier<D> currentId = RWUtils.cutId(id, getManagedDataObjectType());
+ final Optional<? extends DataObject> current = delegate.read(currentId, ctx);
+ // then perform post-reading filtering (return only requested sub-node)
+ final Optional<? extends DataObject> readSubtree = current.isPresent()
+ ? filterSubtree(current.get(), id, getManagedDataObjectType().getTargetType())
+ : current;
+
+ LOG.debug("{}: Subtree: {} read successfully. Result: {}", this, id, readSubtree);
+ return readSubtree;
+
+ // Fallback solution, try delegate, maybe it can read the ID
+ } else {
+ return delegate.read(id, ctx);
+ }
+ }
+
+ @Override
+ public void readCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, @Nonnull final B builder,
+ @Nonnull final ReadContext ctx)
+ throws ReadFailedException {
+ delegate.readCurrentAttributes(id, builder, ctx);
+ }
+
+ @Nonnull
+ @Override
+ public B getBuilder(final InstanceIdentifier<D> id) {
+ return delegate.getBuilder(id);
+ }
+
+ @Override
+ public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder, @Nonnull final D readValue) {
+ delegate.merge(parentBuilder, readValue);
+ }
+
+ @Nonnull
+ private static Optional<? extends DataObject> filterSubtree(@Nonnull final DataObject parent,
+ @Nonnull final InstanceIdentifier<? extends DataObject> absolutPath,
+ @Nonnull final Class<?> managedType) {
+ final InstanceIdentifier.PathArgument nextId =
+ RWUtils.getNextId(absolutPath, InstanceIdentifier.create(parent.getClass()));
+
+ final Optional<? extends DataObject> nextParent = findNextParent(parent, nextId, managedType);
+
+ if (Iterables.getLast(absolutPath.getPathArguments()).equals(nextId)) {
+ return nextParent; // we found the dataObject identified by absolutePath
+ } else if (nextParent.isPresent()) {
+ return filterSubtree(nextParent.get(), absolutPath, nextId.getType());
+ } else {
+ return nextParent; // we can't go further, return Optional.absent()
+ }
+ }
+
+ private static Optional<? extends DataObject> findNextParent(@Nonnull final DataObject parent,
+ @Nonnull final InstanceIdentifier.PathArgument nextId,
+ @Nonnull final Class<?> managedType) {
+ // TODO is there a better way than reflection ? e.g. convert into NN and filter out with a utility
+ Optional<Method> method = ReflectionUtils.findMethodReflex(managedType, "get",
+ Collections.emptyList(), nextId.getType());
+
+ if (method.isPresent()) {
+ return Optional.fromNullable(filterSingle(parent, nextId, method.get()));
+ } else {
+ // List child nodes
+ method = ReflectionUtils.findMethodReflex(managedType,
+ "get" + nextId.getType().getSimpleName(), Collections.emptyList(), List.class);
+
+ if (method.isPresent()) {
+ return filterList(parent, nextId, method.get());
+ } else {
+ throw new IllegalStateException(
+ "Unable to filter " + nextId + " from " + parent + " getters not found using reflexion");
+ }
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private static Optional<? extends DataObject> filterList(final DataObject parent,
+ final InstanceIdentifier.PathArgument nextId,
+ final Method method) {
+ final List<? extends DataObject> invoke = (List<? extends DataObject>) invoke(method, nextId, parent);
+
+ checkArgument(nextId instanceof InstanceIdentifier.IdentifiableItem<?, ?>,
+ "Unable to perform wildcarded read for %s", nextId);
+ final Identifier key = ((InstanceIdentifier.IdentifiableItem) nextId).getKey();
+ // TODO replace with stream().filter().findFirst() when we switch to using java's Optional instead of Guava's
+ // because now we would have to do awkward Optional transformation since findFirstReturns guava's optional
+ return Iterables.tryFind(invoke, new Predicate<DataObject>() {
+
+ @Override
+ public boolean apply(@Nullable final DataObject input) {
+ final Optional<Method> keyGetter = ReflectionUtils.findMethodReflex(nextId.getType(), "get",
+ Collections.emptyList(), key.getClass());
+ final Object actualKey;
+ actualKey = invoke(keyGetter.get(), nextId, input);
+ return key.equals(actualKey);
+ }
+ });
+ }
+
+ private static DataObject filterSingle(final DataObject parent,
+ final InstanceIdentifier.PathArgument nextId, final Method method) {
+ return nextId.getType().cast(invoke(method, nextId, parent));
+ }
+
+ private static Object invoke(final Method method,
+ final InstanceIdentifier.PathArgument nextId, final DataObject parent) {
+ try {
+ return method.invoke(parent);
+ } catch (IllegalAccessException | InvocationTargetException e) {
+ throw new IllegalArgumentException("Unable to get " + nextId + " from " + parent, e);
+ }
+ }
+
+ @Override
+ @Nonnull
+ public InstanceIdentifier<D> getManagedDataObjectType() {
+ return delegate.getManagedDataObjectType();
+ }
+
+ /**
+ * Wrap a Reader as a 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 SubtreeListReader<>((ListReader) reader, handledChildren)
+ : new SubtreeReader<>(reader, handledChildren);
+ }
+
+ private static final 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;
+
+ private SubtreeListReader(final ListReader<D, K, B> delegate,
+ final Set<InstanceIdentifier<?>> handledTypes) {
+ super(delegate, handledTypes);
+ this.delegate = delegate;
+ }
+
+ @Nonnull
+ @Override
+ public List<D> readList(@Nonnull final InstanceIdentifier<D> id, @Nonnull final ReadContext ctx)
+ throws ReadFailedException {
+ return delegate.readList(id, ctx);
+ }
+
+ @Override
+ public void merge(@Nonnull final Builder<? extends DataObject> builder, @Nonnull final List<D> readData) {
+ delegate.merge(builder, readData);
+ }
+
+ @Override
+ public List<K> getAllIds(@Nonnull final InstanceIdentifier<D> id,
+ @Nonnull final ReadContext ctx) throws ReadFailedException {
+ return delegate.getAllIds(id, ctx);
+ }
+ }
+
+}
diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/TypeHierarchy.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/TypeHierarchy.java
new file mode 100644
index 000000000..a30663221
--- /dev/null
+++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/registry/TypeHierarchy.java
@@ -0,0 +1,101 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.translate.util.read.registry;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+import com.google.common.collect.Iterables;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+import javax.annotation.Nonnull;
+import org.jgrapht.experimental.dag.DirectedAcyclicGraph;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+final class TypeHierarchy {
+ private final DirectedAcyclicGraph<InstanceIdentifier<?>, Parent> hierarchy;
+
+ private TypeHierarchy(@Nonnull final DirectedAcyclicGraph<InstanceIdentifier<?>, Parent> hierarchy) {
+ this.hierarchy = hierarchy;
+ }
+
+ Set<InstanceIdentifier<?>> getAllChildren(InstanceIdentifier<?> id) {
+ final HashSet<InstanceIdentifier<?>> instanceIdentifiers = new HashSet<>();
+ for (InstanceIdentifier<?> childId : getDirectChildren(id)) {
+ instanceIdentifiers.add(childId);
+ instanceIdentifiers.addAll(getAllChildren(childId));
+ }
+ return instanceIdentifiers;
+ }
+
+ Set<InstanceIdentifier<?>> getDirectChildren(InstanceIdentifier<?> id) {
+ checkArgument(hierarchy.vertexSet().contains(id),
+ "Unknown reader: %s. Known readers: %s", id, hierarchy.vertexSet());
+
+ return hierarchy.outgoingEdgesOf(id).stream()
+ .map(hierarchy::getEdgeTarget)
+ .collect(Collectors.toSet());
+ }
+
+ Set<InstanceIdentifier<?>> getRoots() {
+ return hierarchy.vertexSet().stream()
+ .filter(vertex -> hierarchy.incomingEdgesOf(vertex).size() == 0)
+ .collect(Collectors.toSet());
+ }
+
+ /**
+ * Create reader hierarchy from a flat set of instance identifiers.
+ *
+ * @param allIds Set of unkeyed instance identifiers
+ */
+ static TypeHierarchy create(@Nonnull Set<InstanceIdentifier<?>> allIds) {
+ final DirectedAcyclicGraph<InstanceIdentifier<?>, Parent>
+ readersHierarchy = new DirectedAcyclicGraph<>((sourceVertex, targetVertex) -> new Parent());
+
+ for (InstanceIdentifier<?> allId : allIds) {
+ checkArgument(!Iterables.isEmpty(allId.getPathArguments()), "Empty ID detected");
+
+ if (Iterables.size(allId.getPathArguments()) == 1) {
+ readersHierarchy.addVertex(allId);
+ }
+
+ List<InstanceIdentifier.PathArgument> pathArgs = new LinkedList<>();
+ pathArgs.add(allId.getPathArguments().iterator().next());
+
+ for (InstanceIdentifier.PathArgument pathArgument : Iterables.skip(allId.getPathArguments(), 1)) {
+ final InstanceIdentifier<?> previous = InstanceIdentifier.create(pathArgs);
+ pathArgs.add(pathArgument);
+ final InstanceIdentifier<?> current = InstanceIdentifier.create(pathArgs);
+
+ readersHierarchy.addVertex(previous);
+ readersHierarchy.addVertex(current);
+
+ try {
+ readersHierarchy.addDagEdge(previous, current);
+ } catch (DirectedAcyclicGraph.CycleFoundException e) {
+ throw new IllegalArgumentException("Loop in hierarchy detected", e);
+ }
+ }
+ }
+
+ return new TypeHierarchy(readersHierarchy);
+ }
+
+ private static final class Parent{}
+}