2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.impl.schema;
10 import static com.google.common.base.Preconditions.checkNotNull;
12 import com.google.common.base.Optional;
14 import java.util.Iterator;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 public final class NormalizedNodeUtils {
28 private NormalizedNodeUtils() {
29 throw new UnsupportedOperationException("Utilities class should not be instantiated");
32 public static Optional<NormalizedNode<?, ?>> findNode(final YangInstanceIdentifier rootPath, final NormalizedNode<?, ?> rootNode, final YangInstanceIdentifier childPath) {
33 final Optional<YangInstanceIdentifier> relativePath = childPath.relativeTo(rootPath);
34 if (relativePath.isPresent()) {
35 return findNode(rootNode, relativePath.get());
37 return Optional.absent();
41 public static Optional<NormalizedNode<?, ?>> findNode(final NormalizedNode<?, ?> tree, final YangInstanceIdentifier path) {
42 checkNotNull(tree, "Tree must not be null");
43 checkNotNull(path, "Path must not be null");
45 Optional<NormalizedNode<?, ?>> currentNode = Optional.<NormalizedNode<?, ?>> of(tree);
46 final Iterator<PathArgument> pathIterator = path.getPathArguments().iterator();
47 while (currentNode.isPresent() && pathIterator.hasNext()) {
48 currentNode = getDirectChild(currentNode.get(), pathIterator.next());
53 @SuppressWarnings({ "unchecked", "rawtypes" })
54 public static Optional<NormalizedNode<?, ?>> getDirectChild(final NormalizedNode<?, ?> node, final PathArgument pathArg) {
55 if (node instanceof LeafNode<?> || node instanceof LeafSetEntryNode<?>) {
56 return Optional.absent();
57 } else if (node instanceof DataContainerNode<?>) {
58 return (Optional) ((DataContainerNode<?>) node).getChild(pathArg);
59 } else if (node instanceof MapNode && pathArg instanceof NodeIdentifierWithPredicates) {
60 return (Optional) ((MapNode) node).getChild((NodeIdentifierWithPredicates) pathArg);
61 } else if (node instanceof LeafSetNode<?>) {
62 return (Optional) ((LeafSetNode<?>) node).getChild((NodeWithValue) pathArg);
64 return Optional.absent();