summaryrefslogtreecommitdiffstats
path: root/infra/translate-utils/src/main/java
diff options
context:
space:
mode:
authorMarek Gradzki <mgradzki@cisco.com>2018-05-16 10:30:28 +0200
committerMarek Gradzki <mgradzki@cisco.com>2018-05-16 12:07:20 +0200
commitdea26601bb63731c35675366849c40960e48992a (patch)
treeb0518cba643926d6c61ddbd7cc08620ef3f35d32 /infra/translate-utils/src/main/java
parent04ccd56d3db7cf937f6812a63eb88f8649860b47 (diff)
HONEYCOMB-437: implement ReflexiveListReaderCustomizer.readCurrentAttributes
ReflexiveListReaderCustomizer used default NOOP implementation, which caused issues during serialization, because BindingNormalizedNodeCodecRegistry.toNormalizedNode requrires key to be set. Change-Id: I7822e07efe57cbdee2c539583776f8cc677ddab7 Signed-off-by: Marek Gradzki <mgradzki@cisco.com>
Diffstat (limited to 'infra/translate-utils/src/main/java')
-rw-r--r--infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveListReaderCustomizer.java35
1 files changed, 27 insertions, 8 deletions
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
index b6430a355..e058964a0 100644
--- 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
@@ -16,24 +16,26 @@
package io.fd.honeycomb.translate.util.read;
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Preconditions.checkState;
+
import com.google.common.base.Optional;
import io.fd.honeycomb.translate.read.ReadContext;
import io.fd.honeycomb.translate.read.ReadFailedException;
import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer;
import io.fd.honeycomb.translate.util.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;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-
-import javax.annotation.Nonnull;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.util.Collections;
-import java.util.List;
-
-import static com.google.common.base.Preconditions.*;
+import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
/**
* Might be slow.
@@ -43,12 +45,29 @@ public class ReflexiveListReaderCustomizer<C extends DataObject & Identifiable<K
implements ListReaderCustomizer<C, K, B> {
private final List<K> staticKeys;
+ private final Class<? extends Identifier> keyType;
public ReflexiveListReaderCustomizer(@Nonnull final Class<C> typeClass, @Nonnull final Class<B> builderClass,
@Nonnull final List<K> staticKeys) {
super(typeClass, builderClass);
this.staticKeys = checkNotNull(staticKeys, "Static keys cannot be null");
checkState(!this.staticKeys.isEmpty(), "No static keys provided");
+ keyType = staticKeys.get(0).getClass();
+ }
+
+ @Override
+ public void readCurrentAttributes(final InstanceIdentifier<C> id, final B builder, final ReadContext context)
+ throws ReadFailedException {
+ final Optional<Method> method =
+ ReflectionUtils.findMethodReflex(builder.getClass(), "setKey",
+ Collections.singletonList(keyType), builder.getClass());
+ checkArgument(method.isPresent(), "Unable to setKey to %s", builder);
+
+ try {
+ method.get().invoke(builder, ((KeyedInstanceIdentifier)id).getKey());
+ } catch (IllegalAccessException | InvocationTargetException e) {
+ throw new IllegalArgumentException("Unable to setKey to " + builder, e);
+ }
}
@Override