Bug 5504 - Controller crashes with OOM. oldRoot/currentRoot, toString()
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / StackedYangInstanceIdentifier.java
index 6b1b58a38a2d4b86721bce366a19f8cdbcb70148..5ccc73759a97095fa280792268081cecaf207a4f 100644 (file)
@@ -48,7 +48,11 @@ final class StackedYangInstanceIdentifier extends YangInstanceIdentifier impleme
 
     @Override
     public StackedYangInstanceIdentifier clone() {
-        return new StackedYangInstanceIdentifier(parent, pathArgument, hashCode());
+        try {
+            return (StackedYangInstanceIdentifier) super.clone();
+        } catch (CloneNotSupportedException e) {
+            throw new IllegalStateException("clone() should be supported", e);
+        }
     }
 
     @Override
@@ -56,6 +60,38 @@ final class StackedYangInstanceIdentifier extends YangInstanceIdentifier impleme
         return parent;
     }
 
+    @Override
+    public YangInstanceIdentifier getAncestor(final int depth) {
+        Preconditions.checkArgument(depth >= 0, "Steps cannot be negative");
+
+        // Calculate how far up our FixedYangInstanceIdentifier ancestor is
+        int stackedDepth = 1;
+        YangInstanceIdentifier wlk = getParent();
+        while (wlk instanceof StackedYangInstanceIdentifier) {
+            wlk = wlk.getParent();
+            stackedDepth++;
+        }
+
+        // Guaranteed to come from FixedYangInstanceIdentifier
+        final int fixedDepth = wlk.getPathArguments().size();
+        if (fixedDepth >= depth) {
+            return wlk.getAncestor(depth);
+        }
+
+        // Calculate our depth and check argument
+        final int ourDepth = stackedDepth + fixedDepth;
+        Preconditions.checkArgument(depth <= ourDepth, "Depth %s exceeds maximum depth %s", depth, ourDepth);
+
+        // Requested depth is covered by the stack, traverse up for specified number of steps
+        final int toWalk = ourDepth - depth;
+        YangInstanceIdentifier result = this;
+        for (int i = 0; i < toWalk; ++i) {
+            result = result.getParent();
+        }
+
+        return result;
+    }
+
     @Override
     public boolean isEmpty() {
         return false;