876b2855a334456065121cda9d3fcab55363aa98
[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 java.util.Collections;
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.NormalizedNodeContainer;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
23
24 abstract class AbstractModifiedNodeBasedCandidateNode implements DataTreeCandidateNode {
25
26     private static final Function<NormalizedNode<?, ?>, DataTreeCandidateNode> TO_UNMODIFIED_NODE = new Function<NormalizedNode<?, ?>, DataTreeCandidateNode>() {
27         @Override
28         public DataTreeCandidateNode apply(final NormalizedNode<?, ?> input) {
29             return AbstractRecursiveCandidateNode.unmodifiedNode(input);
30         }
31     };
32
33     private final ModifiedNode mod;
34     private final TreeNode newMeta;
35     private final TreeNode oldMeta;
36
37     protected AbstractModifiedNodeBasedCandidateNode(final ModifiedNode mod,
38             final TreeNode oldMeta, final TreeNode newMeta) {
39         this.newMeta = newMeta;
40         this.oldMeta = oldMeta;
41         this.mod = Preconditions.checkNotNull(mod);
42     }
43
44     protected final ModifiedNode getMod() {
45         return mod;
46     }
47
48     protected final TreeNode getNewMeta() {
49         return newMeta;
50     }
51
52     protected final TreeNode getOldMeta() {
53         return oldMeta;
54     }
55
56     private static TreeNode childMeta(final TreeNode parent, final PathArgument id) {
57         if (parent != null) {
58             return parent.getChild(id).orNull();
59         } else {
60             return null;
61         }
62     }
63
64     private static boolean canHaveChildren(@Nullable final TreeNode oldMeta, @Nullable final TreeNode newMeta) {
65         if (oldMeta != null) {
66             return oldMeta.getData() instanceof NormalizedNodeContainer;
67         }
68         if (newMeta != null) {
69             return newMeta.getData() instanceof NormalizedNodeContainer;
70         }
71         return false;
72     }
73
74     @SuppressWarnings("unchecked")
75     private static NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> getContainer(@Nullable final TreeNode meta) {
76         return (meta == null ? null : (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>)meta.getData());
77     }
78
79     private ChildNode childNode(final ModifiedNode childMod) {
80         final PathArgument id = childMod.getIdentifier();
81         return new ChildNode(childMod, childMeta(oldMeta, id), childMeta(newMeta, id));
82     }
83
84     @Override
85     public Collection<DataTreeCandidateNode> getChildNodes() {
86         switch (mod.getModificationType()) {
87         case APPEARED:
88         case DISAPPEARED:
89         case SUBTREE_MODIFIED:
90             return Collections2.transform(mod.getChildren(), new Function<ModifiedNode, DataTreeCandidateNode>() {
91                 @Override
92                 public DataTreeCandidateNode apply(final ModifiedNode input) {
93                     return childNode(input);
94                 }
95             });
96         case UNMODIFIED:
97             // Unmodified node, but we still need to resolve potential children. canHaveChildren returns
98             // false if both arguments are null.
99             if (canHaveChildren(oldMeta, newMeta)) {
100                 return Collections2.transform(getContainer(newMeta != null ? newMeta : oldMeta).getValue(), TO_UNMODIFIED_NODE);
101             } else {
102                 return Collections.emptyList();
103             }
104         case DELETE:
105         case WRITE:
106             // This is unusual, the user is requesting we follow into an otherwise-terminal node.
107             // We need to fudge things based on before/after data to correctly fake the expectations.
108             if (canHaveChildren(oldMeta, newMeta)) {
109                 return AbstractDataTreeCandidateNode.deltaChildren(getContainer(oldMeta), getContainer(newMeta));
110             } else {
111                 return Collections.emptyList();
112             }
113         default:
114             throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
115         }
116     }
117
118     @Override
119     public ModificationType getModificationType() {
120         return Verify.verifyNotNull(mod.getModificationType(), "Node %s does not have resolved modification type", mod);
121     }
122
123     private static Optional<NormalizedNode<?, ?>> optionalData(final TreeNode meta) {
124         if (meta != null) {
125             return Optional.<NormalizedNode<?,?>>of(meta.getData());
126         } else {
127             return Optional.absent();
128         }
129     }
130
131     @Override
132     public final Optional<NormalizedNode<?, ?>> getDataAfter() {
133         return optionalData(newMeta);
134     }
135
136     @Override
137     public final Optional<NormalizedNode<?, ?>> getDataBefore() {
138         return optionalData(oldMeta);
139     }
140
141     @Override
142     public final DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
143         switch (mod.getModificationType()) {
144         case APPEARED:
145         case DISAPPEARED:
146         case SUBTREE_MODIFIED:
147             final Optional<ModifiedNode> childMod = mod.getChild(identifier);
148             if (childMod.isPresent()) {
149                 return childNode(childMod.get());
150             }
151             return null;
152         case UNMODIFIED:
153             if (canHaveChildren(oldMeta, newMeta)) {
154                 final Optional<NormalizedNode<?, ?>> maybeChild = getContainer(newMeta != null ? newMeta : oldMeta).getChild(identifier);
155                 if (maybeChild.isPresent()) {
156                     return TO_UNMODIFIED_NODE.apply(maybeChild.get());
157                 } else {
158                     return null;
159                 }
160             } else {
161                 return null;
162             }
163         case DELETE:
164         case WRITE:
165             if (canHaveChildren(oldMeta, newMeta)) {
166                 return AbstractDataTreeCandidateNode.deltaChild(getContainer(oldMeta), getContainer(newMeta), identifier);
167             } else {
168                 return null;
169             }
170         default:
171             throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
172         }
173     }
174
175     private static final class ChildNode extends AbstractModifiedNodeBasedCandidateNode {
176         ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
177             super(mod, oldMeta, newMeta);
178         }
179
180         @Override
181         public PathArgument getIdentifier() {
182             return getMod().getIdentifier();
183         }
184     }
185 }