Make filterSubtree recursive
authorMarek Gradzki <mgradzki@cisco.com>
Thu, 9 Jun 2016 13:00:11 +0000 (15:00 +0200)
committerMaros Marsalek <mmarsale@cisco.com>
Fri, 10 Jun 2016 08:18:52 +0000 (08:18 +0000)
Change-Id: I7b2b888fd7debb0aec3292a07fc35c0e6493d117
Signed-off-by: Marek Gradzki <mgradzki@cisco.com>
v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/read/AbstractCompositeReader.java

index afd9791..c99e0ed 100644 (file)
@@ -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");
             }
         }
     }