BUG-2383: deprecate ModificationType.MERGE
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / InMemoryDataTreeCandidate.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.tree;
9
10 import com.google.common.base.Function;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Collections2;
14 import java.util.Collection;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
19 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
25
26 final class InMemoryDataTreeCandidate extends AbstractDataTreeCandidate {
27     private static abstract class AbstractNode implements DataTreeCandidateNode {
28         private final ModifiedNode mod;
29         private final TreeNode newMeta;
30         private final TreeNode oldMeta;
31
32         protected AbstractNode(final ModifiedNode mod,
33                 final TreeNode oldMeta, final TreeNode newMeta) {
34             this.newMeta = newMeta;
35             this.oldMeta = oldMeta;
36             this.mod = Preconditions.checkNotNull(mod);
37         }
38
39         protected final ModifiedNode getMod() {
40             return mod;
41         }
42
43         protected final TreeNode getNewMeta() {
44             return newMeta;
45         }
46
47         protected final TreeNode getOldMeta() {
48             return oldMeta;
49         }
50
51         private static final TreeNode childMeta(final TreeNode parent, final PathArgument id) {
52             if (parent != null) {
53                 return parent.getChild(id).orNull();
54             } else {
55                 return null;
56             }
57         }
58
59         private DataTreeCandidateNode childNode(final ModifiedNode input) {
60             final PathArgument id = input.getIdentifier();
61             return new ChildNode(input, childMeta(oldMeta, id), childMeta(newMeta, id));
62         }
63
64         @Override
65         public Collection<DataTreeCandidateNode> getChildNodes() {
66             return Collections2.transform(mod.getChildren(), new Function<ModifiedNode, DataTreeCandidateNode>() {
67                 @Override
68                 public DataTreeCandidateNode apply(final ModifiedNode input) {
69                     return childNode(input);
70                 }
71             });
72         }
73
74         @Override
75         public ModificationType getModificationType() {
76             switch (mod.getOperation()) {
77             case DELETE:
78                 return ModificationType.DELETE;
79             case MERGE:
80                 // Merge into non-existing data is a write
81                 if (oldMeta == null) {
82                     return ModificationType.WRITE;
83                 }
84
85                 // Data-based checks to narrow down types
86                 final NormalizedNode<?, ?> data = newMeta.getData();
87
88                 // leaf or anyxml are always written
89                 if (!(data instanceof NormalizedNodeContainer)) {
90                     return ModificationType.WRITE;
91                 }
92
93                 // Unkeyed collections are always written
94                 if (data instanceof UnkeyedListNode || data instanceof OrderedMapNode || data instanceof OrderedLeafSetNode) {
95                     return ModificationType.WRITE;
96                 }
97
98                 // Everything else is subtree modified
99                 return ModificationType.SUBTREE_MODIFIED;
100             case TOUCH:
101                 return ModificationType.SUBTREE_MODIFIED;
102             case NONE:
103                 return ModificationType.UNMODIFIED;
104             case WRITE:
105                 return ModificationType.WRITE;
106             }
107
108             throw new IllegalStateException("Unhandled internal operation " + mod.getOperation());
109         }
110
111         private Optional<NormalizedNode<?, ?>> optionalData(final TreeNode meta) {
112             if (meta != null) {
113                 return Optional.<NormalizedNode<?,?>>of(meta.getData());
114             } else {
115                 return Optional.absent();
116             }
117         }
118
119         @Override
120         public Optional<NormalizedNode<?, ?>> getDataAfter() {
121             return optionalData(newMeta);
122         }
123
124         @Override
125         public Optional<NormalizedNode<?, ?>> getDataBefore() {
126             return optionalData(oldMeta);
127         }
128
129         @Override
130         public DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
131             final Optional<ModifiedNode> childMod = mod.getChild(identifier);
132             if(childMod.isPresent()) {
133                 return childNode(mod);
134             }
135             return null;
136         }
137     }
138
139     private static final class ChildNode extends AbstractNode {
140         public ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
141             super(mod, oldMeta, newMeta);
142         }
143
144         @Override
145         public PathArgument getIdentifier() {
146             return getMod().getIdentifier();
147         }
148     }
149
150     private static final class RootNode extends AbstractNode {
151         public RootNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
152             super(mod, oldMeta, newMeta);
153         }
154
155         @Override
156         public PathArgument getIdentifier() {
157             throw new IllegalStateException("Attempted to get identifier of the root node");
158         }
159     }
160
161     private final RootNode root;
162
163     InMemoryDataTreeCandidate(final YangInstanceIdentifier rootPath, final ModifiedNode modificationRoot,
164             final TreeNode beforeRoot, final TreeNode afterRoot) {
165         super(rootPath);
166         this.root = new RootNode(modificationRoot, beforeRoot, afterRoot);
167     }
168
169     TreeNode getAfterRoot() {
170         return root.getNewMeta();
171     }
172
173     TreeNode getBeforeRoot() {
174         return root.getOldMeta();
175     }
176
177     @Override
178     public DataTreeCandidateNode getRootNode() {
179         return root;
180     }
181 }