summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarek Gradzki <mgradzki@cisco.com>2016-06-09 15:00:11 +0200
committerMaros Marsalek <mmarsale@cisco.com>2016-06-10 08:18:52 +0000
commitfa8f27d387882138835f518f6b95544097b49dcb (patch)
tree1173697bce01b925c67115d01bc056b407b89467
parent625b421f3c28e0457f039017b1160662d622f4bc (diff)
Make filterSubtree recursive
Change-Id: I7b2b888fd7debb0aec3292a07fc35c0e6493d117 Signed-off-by: Marek Gradzki <mgradzki@cisco.com>
-rw-r--r--v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/read/AbstractCompositeReader.java30
1 files changed, 21 insertions, 9 deletions
diff --git a/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/read/AbstractCompositeReader.java b/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/read/AbstractCompositeReader.java
index afd979167..c99e0edc4 100644
--- a/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/read/AbstractCompositeReader.java
+++ b/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/read/AbstractCompositeReader.java
@@ -185,29 +185,41 @@ abstract class AbstractCompositeReader<D extends DataObject, B extends Builder<D
// TODO move filtering out of here into a dedicated Filter ifc
@Nonnull
private static Optional<? extends DataObject> filterSubtree(@Nonnull final DataObject parent,
- @Nonnull final InstanceIdentifier<? extends DataObject> absolutPath,
- @Nonnull final Class<?> managedType) {
- // TODO is there a better way than reflection ? e.g. convert into NN and filter out with a utility
- // FIXME this needs to be recursive. right now it expects only 1 additional element in ID + test
-
+ @Nonnull final InstanceIdentifier<? extends DataObject> absolutPath,
+ @Nonnull final Class<?> managedType) {
final InstanceIdentifier.PathArgument nextId =
- RWUtils.getNextId(absolutPath, InstanceIdentifier.create(parent.getClass()));
+ 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.<Class<?>>emptyList(), nextId.getType());
+ Collections.<Class<?>>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.<Class<?>>emptyList(), List.class);
+ "get" + nextId.getType().getSimpleName(), Collections.<Class<?>>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");
+ "Unable to filter " + nextId + " from " + parent + " getters not found using reflexion");
}
}
}