public static <T extends StoreTreeNode<T>> T findNodeChecked(final T tree, final YangInstanceIdentifier path) { T current = tree; int depth = 1; for (PathArgument pathArg : path.getPathArguments()) { Optional<T> potential = current.getChild(pathArg); if (!potential.isPresent()) { throw new IllegalArgumentException(String.format("Child %s is not present in tree.", path.getAncestor(depth))); } current = potential.get(); ++depth; } return current; }
public static <T extends StoreTreeNode<T>> T findNodeChecked(final T tree, final YangInstanceIdentifier path) { T current = tree; int depth = 1; for (PathArgument pathArg : path.getPathArguments()) { Optional<T> potential = current.getChild(pathArg); if (!potential.isPresent()) { throw new IllegalArgumentException(String.format("Child %s is not present in tree.", path.getAncestor(depth))); } current = potential.get(); ++depth; } return current; }
public static <T extends StoreTreeNode<T>> Entry<YangInstanceIdentifier, T> findClosestsOrFirstMatch(final T tree, final YangInstanceIdentifier path, final Predicate<T> predicate) { Optional<T> parent = Optional.of(tree); Optional<T> current = Optional.of(tree); int nesting = 0; Iterator<PathArgument> pathIter = path.getPathArguments().iterator(); while (current.isPresent() && pathIter.hasNext() && !predicate.test(current.get())) { parent = current; current = current.get().getChild(pathIter.next()); nesting++; } if (current.isPresent()) { final YangInstanceIdentifier currentPath = path.getAncestor(nesting); return new SimpleImmutableEntry<>(currentPath, current.get()); } /* * Subtracting 1 from nesting level at this point is safe, because we * cannot reach here with nesting == 0: that would mean the above check * for current.isPresent() failed, which it cannot, as current is always * present. At any rate we verify state just to be on the safe side. */ verify(nesting > 0); return new SimpleImmutableEntry<>(path.getAncestor(nesting - 1), parent.get()); }
public static <T extends StoreTreeNode<T>> Entry<YangInstanceIdentifier, T> findClosestsOrFirstMatch(final T tree, final YangInstanceIdentifier path, final Predicate<T> predicate) { Optional<T> parent = Optional.of(tree); Optional<T> current = Optional.of(tree); int nesting = 0; Iterator<PathArgument> pathIter = path.getPathArguments().iterator(); while (current.isPresent() && pathIter.hasNext() && !predicate.test(current.get())) { parent = current; current = current.get().getChild(pathIter.next()); nesting++; } if (current.isPresent()) { final YangInstanceIdentifier currentPath = path.getAncestor(nesting); return new SimpleImmutableEntry<>(currentPath, current.get()); } /* * Subtracting 1 from nesting level at this point is safe, because we * cannot reach here with nesting == 0: that would mean the above check * for current.isPresent() failed, which it cannot, as current is always * present. At any rate we verify state just to be on the safe side. */ verify(nesting > 0); return new SimpleImmutableEntry<>(path.getAncestor(nesting - 1), parent.get()); }
@Nonnull @Override public YangInstanceIdentifier getAncestor(final int depth) { 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; 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; }
@Nonnull @Override public YangInstanceIdentifier getAncestor(final int depth) { 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; 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; }
private OperationWithModification resolveModificationFor(final YangInstanceIdentifier path) { upgradeIfPossible(); /* * Walk the strategy and modification trees in-sync, creating modification nodes as needed. * * If the user has provided wrong input, we may end up with a bunch of TOUCH nodes present * ending with an empty one, as we will throw the exception below. This fact could end up * being a problem, as we'd have bunch of phantom operations. * * That is fine, as we will prune any empty TOUCH nodes in the last phase of the ready * process. */ ModificationApplyOperation operation = strategyTree; ModifiedNode modification = rootNode; int depth = 1; for (final PathArgument pathArg : path.getPathArguments()) { final Optional<ModificationApplyOperation> potential = operation.getChild(pathArg); if (!potential.isPresent()) { throw new SchemaValidationFailedException(String.format("Child %s is not present in schema tree.", path.getAncestor(depth))); } operation = potential.get(); ++depth; modification = modification.modifyChild(pathArg, operation, version); } return OperationWithModification.from(operation, modification); }
private OperationWithModification resolveModificationFor(final YangInstanceIdentifier path) { upgradeIfPossible(); /* * Walk the strategy and modification trees in-sync, creating modification nodes as needed. * * If the user has provided wrong input, we may end up with a bunch of TOUCH nodes present * ending with an empty one, as we will throw the exception below. This fact could end up * being a problem, as we'd have bunch of phantom operations. * * That is fine, as we will prune any empty TOUCH nodes in the last phase of the ready * process. */ ModificationApplyOperation operation = strategyTree; ModifiedNode modification = rootNode; int depth = 1; for (final PathArgument pathArg : path.getPathArguments()) { final Optional<ModificationApplyOperation> potential = operation.getChild(pathArg); if (!potential.isPresent()) { throw new SchemaValidationFailedException(String.format("Child %s is not present in schema tree.", path.getAncestor(depth))); } operation = potential.get(); ++depth; modification = modification.modifyChild(pathArg, operation, version); } return OperationWithModification.from(operation, modification); }