Merge "Bump javax.ws.rs-api version to 2.0"
[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 java.util.Iterator;
13
14 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeWithValue;
17 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24
25 import com.google.common.base.Optional;
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 InstanceIdentifier rootPath, final NormalizedNode<?, ?> rootNode, final InstanceIdentifier childPath) {
33         if(rootPath.contains(childPath)) {
34             int common = rootPath.getPath().size();
35             InstanceIdentifier relativePath = new InstanceIdentifier(childPath.getPath().subList(common, childPath.getPath().size()));
36             return findNode(rootNode, relativePath);
37         }
38         return Optional.absent();
39     }
40
41     public static Optional<NormalizedNode<?, ?>> findNode(final NormalizedNode<?, ?> tree, final InstanceIdentifier 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         Iterator<PathArgument> pathIterator = path.getPath().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 }