Merge "Fixed NPE in BindingCodecTree#getSubtreeCodec(YangInstanceIdentifier)"
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / AbstractModifiedNodeBasedCandidateNode.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 import com.google.common.base.Function;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Verify;
13 import com.google.common.collect.Collections2;
14 import java.util.Collection;
15 import javax.annotation.Nonnull;
16 import javax.annotation.Nullable;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
22
23
24 abstract class AbstractModifiedNodeBasedCandidateNode implements DataTreeCandidateNode {
25     private final ModifiedNode mod;
26     private final TreeNode newMeta;
27     private final TreeNode oldMeta;
28
29     protected AbstractModifiedNodeBasedCandidateNode(final ModifiedNode mod,
30             final TreeNode oldMeta, final TreeNode newMeta) {
31         this.newMeta = newMeta;
32         this.oldMeta = oldMeta;
33         this.mod = Preconditions.checkNotNull(mod);
34     }
35
36     protected final ModifiedNode getMod() {
37         return mod;
38     }
39
40     protected final TreeNode getNewMeta() {
41         return newMeta;
42     }
43
44     protected final TreeNode getOldMeta() {
45         return oldMeta;
46     }
47
48     private static final TreeNode childMeta(final TreeNode parent, final PathArgument id) {
49         if (parent != null) {
50             return parent.getChild(id).orNull();
51         } else {
52             return null;
53         }
54     }
55
56     private DataTreeCandidateNode childNode(final ModifiedNode input) {
57         final PathArgument id = input.getIdentifier();
58         return createChildNode(input, childMeta(oldMeta, id), childMeta(newMeta, id));
59     }
60
61     private static DataTreeCandidateNode createChildNode(@Nonnull final ModifiedNode input, @Nullable final TreeNode oldMeta, @Nullable final TreeNode newMeta) {
62         if(oldMeta == null) {
63             return RecursiveModificationCandidateNode.initialWriteNode(newMeta.getData());
64         }
65         if(newMeta == null) {
66             return RecursiveModificationCandidateNode.deleteNode(oldMeta.getData());
67         }
68         return new ChildNode(input, oldMeta, newMeta);
69     }
70
71     @Override
72     public Collection<DataTreeCandidateNode> getChildNodes() {
73         return Collections2.transform(mod.getChildren(), new Function<ModifiedNode, DataTreeCandidateNode>() {
74             @Override
75             public DataTreeCandidateNode apply(final ModifiedNode input) {
76                 return childNode(input);
77             }
78         });
79     }
80
81     @Override
82     public ModificationType getModificationType() {
83         return Verify.verifyNotNull(mod.getModificationType(), "Node %s does not have resolved modification type", mod);
84     }
85
86     private Optional<NormalizedNode<?, ?>> optionalData(final TreeNode meta) {
87         if (meta != null) {
88             return Optional.<NormalizedNode<?,?>>of(meta.getData());
89         } else {
90             return Optional.absent();
91         }
92     }
93
94     @Override
95     public Optional<NormalizedNode<?, ?>> getDataAfter() {
96         return optionalData(newMeta);
97     }
98
99     @Override
100     public Optional<NormalizedNode<?, ?>> getDataBefore() {
101         return optionalData(oldMeta);
102     }
103
104     @Override
105     public DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
106         final Optional<ModifiedNode> childMod = mod.getChild(identifier);
107         if (childMod.isPresent()) {
108             return childNode(childMod.get());
109         }
110         return null;
111     }
112
113     static final class ChildNode extends AbstractModifiedNodeBasedCandidateNode {
114         public ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
115             super(mod, oldMeta, newMeta);
116         }
117
118         @Override
119         public PathArgument getIdentifier() {
120             return getMod().getIdentifier();
121         }
122     }
123 }