diff options
author | Marek Gradzki <mgradzki@cisco.com> | 2016-06-09 15:00:11 +0200 |
---|---|---|
committer | Maros Marsalek <mmarsale@cisco.com> | 2016-06-10 08:18:52 +0000 |
commit | a9bc172cde910489bfb6c09f00509cfc64d7b665 (patch) | |
tree | 01b9fed5d18e0fcc3495bfec55616eb3a50dc5be /v3po/translate-impl/src/main/java/io | |
parent | 70e243d1c48f9554bd4e28e80f7ff766db6fe857 (diff) |
Make filterSubtree recursive
Change-Id: I7b2b888fd7debb0aec3292a07fc35c0e6493d117
Signed-off-by: Marek Gradzki <mgradzki@cisco.com>
Diffstat (limited to 'v3po/translate-impl/src/main/java/io')
-rw-r--r-- | v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/read/AbstractCompositeReader.java | 30 |
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"); } } } |