Bug 4295: Fixed incorrectly introduced nodes when MERGE was followed by DELETE
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / AbstractRecursiveCandidateNode.java
1 /*
2  * Copyright (c) 2015 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.tree;
9
10 import com.google.common.base.Function;
11 import com.google.common.base.Optional;
12 import com.google.common.collect.Collections2;
13 import java.util.Collection;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
18
19 abstract class AbstractRecursiveCandidateNode extends AbstractDataTreeCandidateNode {
20
21     protected AbstractRecursiveCandidateNode(final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> data) {
22         super(data);
23     }
24
25     @SuppressWarnings("unchecked")
26     static DataTreeCandidateNode deleteNode(final NormalizedNode<?, ?> data) {
27         if (data instanceof NormalizedNodeContainer) {
28             return new RecursiveDeleteCandidateNode((NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) data);
29         }
30         return new DeleteLeafCandidateNode(data);
31     }
32
33     @SuppressWarnings("unchecked")
34     static DataTreeCandidateNode replaceNode(final NormalizedNode<?, ?> oldData, final NormalizedNode<?, ?> newData) {
35         if (isContainer(oldData)) {
36             return new RecursiveReplaceCandidateNode((NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) oldData,
37                 (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) newData);
38         }
39         return new ReplaceLeafCandidateNode(oldData, newData);
40     }
41
42     @SuppressWarnings("unchecked")
43     static DataTreeCandidateNode unmodifiedNode(final NormalizedNode<?, ?> data) {
44         if (data instanceof NormalizedNodeContainer) {
45             return new RecursiveUnmodifiedCandidateNode((NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) data);
46         }
47         return new UnmodifiedLeafCandidateNode(data);
48     }
49
50     @SuppressWarnings("unchecked")
51     static DataTreeCandidateNode writeNode(final NormalizedNode<?, ?> data) {
52         if (data instanceof NormalizedNodeContainer) {
53             return new RecursiveWriteCandidateNode((NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) data);
54         }
55         return new WriteLeafCandidateNode(data);
56     }
57
58     protected static boolean isContainer(final NormalizedNode<?, ?> data) {
59         return data instanceof NormalizedNodeContainer;
60     }
61
62     @Override
63     public final DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
64         final Optional<NormalizedNode<?, ?>> potential = getData().getChild(identifier);
65         if (potential.isPresent()) {
66             return createChild(potential.get());
67         }
68         return null;
69     }
70
71     @Override
72     public final Collection<DataTreeCandidateNode> getChildNodes() {
73         return Collections2.transform(getData().getValue(), new Function<NormalizedNode<?,?>, DataTreeCandidateNode>() {
74             @Override
75             public DataTreeCandidateNode apply(final NormalizedNode<?, ?> input) {
76                 return createChild(input);
77             }
78         });
79     }
80
81     @SuppressWarnings("unchecked")
82     private DataTreeCandidateNode createChild(final NormalizedNode<?, ?> childData) {
83         if (isContainer(childData)) {
84             return createContainer((NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) childData);
85         } else {
86             return createLeaf(childData);
87         }
88     }
89
90     protected abstract DataTreeCandidateNode createContainer(NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> childData);
91
92     protected abstract DataTreeCandidateNode createLeaf(NormalizedNode<?,?> childData);
93 }