Mark methods as static
[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_NODES = 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 final 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 SUBTREE_MODIFIED:
88             return Collections2.transform(mod.getChildren(), new Function<ModifiedNode, DataTreeCandidateNode>() {
89                 @Override
90                 public DataTreeCandidateNode apply(final ModifiedNode input) {
91                     return childNode(input);
92                 }
93             });
94         case UNMODIFIED:
95             // Unmodified node, but we still need to resolve potential children. canHaveChildren returns
96             // false if both arguments are null.
97             if (canHaveChildren(oldMeta, newMeta)) {
98                 return Collections2.transform(getContainer(newMeta != null ? newMeta : oldMeta).getValue(), TO_UNMODIFIED_NODES);
99             } else {
100                 return Collections.emptyList();
101             }
102         case DELETE:
103         case WRITE:
104             // This is unusual, the user is requesting we follow into an otherwise-terminal node.
105             // We need to fudge things based on before/after data to correctly fake the expectations.
106             if (canHaveChildren(oldMeta, newMeta)) {
107                 return AbstractDataTreeCandidateNode.deltaChildren(getContainer(oldMeta), getContainer(newMeta));
108             } else {
109                 return Collections.emptyList();
110             }
111         default:
112             throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
113         }
114     }
115
116     @Override
117     public ModificationType getModificationType() {
118         return Verify.verifyNotNull(mod.getModificationType(), "Node %s does not have resolved modification type", mod);
119     }
120
121     private static Optional<NormalizedNode<?, ?>> optionalData(final TreeNode meta) {
122         if (meta != null) {
123             return Optional.<NormalizedNode<?,?>>of(meta.getData());
124         } else {
125             return Optional.absent();
126         }
127     }
128
129     @Override
130     public Optional<NormalizedNode<?, ?>> getDataAfter() {
131         return optionalData(newMeta);
132     }
133
134     @Override
135     public Optional<NormalizedNode<?, ?>> getDataBefore() {
136         return optionalData(oldMeta);
137     }
138
139     @Override
140     public DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
141         switch (mod.getModificationType()) {
142         case SUBTREE_MODIFIED:
143             final Optional<ModifiedNode> childMod = mod.getChild(identifier);
144             if (childMod.isPresent()) {
145                 return childNode(childMod.get());
146             }
147             return null;
148         case DELETE:
149         case UNMODIFIED:
150         case WRITE:
151             // FIXME: this is a linear walk. We need a Map of these in order to
152             //        do something like getChildMap().get(identifier);
153             for (DataTreeCandidateNode c : getChildNodes()) {
154                 if (identifier.equals(c.getIdentifier())) {
155                     return c;
156                 }
157             }
158             return null;
159         default:
160             throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
161         }
162     }
163
164     private static final class ChildNode extends AbstractModifiedNodeBasedCandidateNode {
165         ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
166             super(mod, oldMeta, newMeta);
167         }
168
169         @Override
170         public PathArgument getIdentifier() {
171             return getMod().getIdentifier();
172         }
173     }
174 }