BUG-1092: rename data.api.InstanceIdentifier to YangInstanceIdentifier
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / NormalizedNodeUtils.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema;
9
10 import static com.google.common.base.Preconditions.checkNotNull;
11
12 import com.google.common.base.Optional;
13
14 import java.util.Iterator;
15
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;
26
27 public final class NormalizedNodeUtils {
28     private NormalizedNodeUtils() {
29         throw new UnsupportedOperationException("Utilities class should not be instantiated");
30     }
31
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());
36         } else {
37             return Optional.absent();
38         }
39     }
40
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");
44
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());
49         }
50         return currentNode;
51     }
52
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);
63         }
64         return Optional.absent();
65     }
66 }