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