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