aa1ae48ceed8183951807beb85c1459f0eb757a1
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / AbstractModifiedNodeBasedCandidateNode.java
1 /*
2  * Copyright (c) 2015, 2016 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.Optional;
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Verify;
12 import com.google.common.collect.Collections2;
13 import java.util.Collection;
14 import java.util.Collections;
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.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     private final ModifiedNode mod;
26     private final TreeNode newMeta;
27     private final TreeNode oldMeta;
28
29     protected AbstractModifiedNodeBasedCandidateNode(final ModifiedNode mod, final TreeNode oldMeta,
30             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 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 static boolean canHaveChildren(@Nullable final TreeNode oldMeta, @Nullable final TreeNode newMeta) {
57         if (oldMeta != null) {
58             return oldMeta.getData() instanceof NormalizedNodeContainer;
59         }
60         if (newMeta != null) {
61             return newMeta.getData() instanceof NormalizedNodeContainer;
62         }
63         return false;
64     }
65
66     @SuppressWarnings("unchecked")
67     private static NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> getContainer(@Nullable final TreeNode meta) {
68         return (meta == null ? null : (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>)meta.getData());
69     }
70
71     private ChildNode childNode(final ModifiedNode childMod) {
72         final PathArgument id = childMod.getIdentifier();
73         return new ChildNode(childMod, childMeta(oldMeta, id), childMeta(newMeta, id));
74     }
75
76     @Override
77     @Nonnull
78     public Collection<DataTreeCandidateNode> getChildNodes() {
79         switch (mod.getModificationType()) {
80         case APPEARED:
81         case DISAPPEARED:
82         case SUBTREE_MODIFIED:
83             return Collections2.transform(mod.getChildren(), this::childNode);
84         case UNMODIFIED:
85             // Unmodified node, but we still need to resolve potential children. canHaveChildren returns
86             // false if both arguments are null.
87             if (canHaveChildren(oldMeta, newMeta)) {
88                 return Collections2.transform(getContainer(newMeta != null ? newMeta : oldMeta).getValue(),
89                     AbstractRecursiveCandidateNode::unmodifiedNode);
90             } else {
91                 return Collections.emptyList();
92             }
93         case DELETE:
94         case WRITE:
95             // This is unusual, the user is requesting we follow into an otherwise-terminal node.
96             // We need to fudge things based on before/after data to correctly fake the expectations.
97             if (canHaveChildren(oldMeta, newMeta)) {
98                 return AbstractDataTreeCandidateNode.deltaChildren(getContainer(oldMeta), getContainer(newMeta));
99             } else {
100                 return Collections.emptyList();
101             }
102         default:
103             throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
104         }
105     }
106
107     @Override
108     @Nonnull
109     public ModificationType getModificationType() {
110         return Verify.verifyNotNull(mod.getModificationType(), "Node %s does not have resolved modification type", mod);
111     }
112
113     private static Optional<NormalizedNode<?, ?>> optionalData(final TreeNode meta) {
114         if (meta != null) {
115             return Optional.of(meta.getData());
116         } else {
117             return Optional.absent();
118         }
119     }
120
121     @Override
122     @Nonnull
123     public final Optional<NormalizedNode<?, ?>> getDataAfter() {
124         return optionalData(newMeta);
125     }
126
127     @Override
128     @Nonnull
129     public final Optional<NormalizedNode<?, ?>> getDataBefore() {
130         return optionalData(oldMeta);
131     }
132
133     @Override
134     public final DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
135         switch (mod.getModificationType()) {
136         case APPEARED:
137         case DISAPPEARED:
138         case SUBTREE_MODIFIED:
139             final Optional<ModifiedNode> childMod = mod.getChild(identifier);
140             if (childMod.isPresent()) {
141                 return childNode(childMod.get());
142             }
143             return null;
144         case UNMODIFIED:
145             if (canHaveChildren(oldMeta, newMeta)) {
146                 final Optional<NormalizedNode<?, ?>> maybeChild = getContainer(newMeta != null ? newMeta : oldMeta).getChild(identifier);
147                 if (maybeChild.isPresent()) {
148                     return AbstractRecursiveCandidateNode.unmodifiedNode(maybeChild.get());
149                 } else {
150                     return null;
151                 }
152             } else {
153                 return null;
154             }
155         case DELETE:
156         case WRITE:
157             if (canHaveChildren(oldMeta, newMeta)) {
158                 return AbstractDataTreeCandidateNode.deltaChild(getContainer(oldMeta), getContainer(newMeta), identifier);
159             } else {
160                 return null;
161             }
162         default:
163             throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
164         }
165     }
166
167     private static final class ChildNode extends AbstractModifiedNodeBasedCandidateNode {
168         ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
169             super(mod, oldMeta, newMeta);
170         }
171
172         @Override
173         @Nonnull
174         public PathArgument getIdentifier() {
175             return getMod().getIdentifier();
176         }
177     }
178
179     @Override
180     public String toString() {
181         return this.getClass().getSimpleName() + "{mod = " + this.mod + ", oldMeta = " + this.oldMeta + ", newMeta = " +
182                 this.newMeta + "}";
183     }
184 }