Populate data/ hierarchy
[yangtools.git] / data / 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
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.collect.Collections2;
14 import com.google.common.collect.ImmutableList;
15 import java.util.Collection;
16 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.data.api.schema.DistinctNodeContainer;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNodes;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
26 import org.opendaylight.yangtools.yang.data.spi.tree.TreeNode;
27
28 abstract class AbstractModifiedNodeBasedCandidateNode implements DataTreeCandidateNode {
29     private final ModifiedNode mod;
30     private final TreeNode newMeta;
31     private final TreeNode oldMeta;
32
33     protected AbstractModifiedNodeBasedCandidateNode(final ModifiedNode mod, final TreeNode oldMeta,
34             final TreeNode newMeta) {
35         this.newMeta = newMeta;
36         this.oldMeta = oldMeta;
37         this.mod = requireNonNull(mod);
38     }
39
40     protected final ModifiedNode getMod() {
41         return mod;
42     }
43
44     protected final TreeNode getNewMeta() {
45         return newMeta;
46     }
47
48     protected final TreeNode getOldMeta() {
49         return oldMeta;
50     }
51
52     private static TreeNode childMeta(final TreeNode parent, final PathArgument id) {
53         return parent == null ? null : parent.childByArg(id);
54     }
55
56     private static boolean canHaveChildren(final @Nullable TreeNode oldMeta, final @Nullable 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 DistinctNodeContainer<PathArgument, NormalizedNode> getContainer(
68             final @Nullable TreeNode meta) {
69         return meta == null ? null : (DistinctNodeContainer<PathArgument, NormalizedNode>)meta.getData();
70     }
71
72     private ChildNode childNode(final ModifiedNode childMod) {
73         final PathArgument id = childMod.getIdentifier();
74         return new ChildNode(childMod, childMeta(oldMeta, id), childMeta(newMeta, id));
75     }
76
77     @Override
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 ImmutableList.of();
89                 }
90
91                 return Collections2.transform(getContainer(newMeta != null ? newMeta : oldMeta).body(),
92                     DataTreeCandidateNodes::unmodified);
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 ImmutableList.of();
99                 }
100                 return DataTreeCandidateNodes.containerDelta(getContainer(oldMeta), getContainer(newMeta));
101             default:
102                 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
103         }
104     }
105
106     @Override
107     public ModificationType getModificationType() {
108         return verifyNotNull(mod.getModificationType(), "Node %s does not have resolved modification type", mod);
109     }
110
111     private static @NonNull Optional<NormalizedNode> optionalData(final TreeNode meta) {
112         return meta == null ? Optional.empty() : Optional.of(meta.getData());
113     }
114
115     @Override
116     public final Optional<NormalizedNode> getDataAfter() {
117         return optionalData(newMeta);
118     }
119
120     @Override
121     public final Optional<NormalizedNode> getDataBefore() {
122         return optionalData(oldMeta);
123     }
124
125     @Override
126     public final Optional<DataTreeCandidateNode> getModifiedChild(final PathArgument identifier) {
127         switch (mod.getModificationType()) {
128             case APPEARED:
129             case DISAPPEARED:
130             case SUBTREE_MODIFIED:
131                 final ModifiedNode child = mod.childByArg(identifier);
132                 return child == null ? Optional.empty() : Optional.of(childNode(child));
133             case UNMODIFIED:
134                 if (!canHaveChildren(oldMeta, newMeta)) {
135                     return Optional.empty();
136                 }
137                 return getContainer(newMeta != null ? newMeta : oldMeta).findChildByArg(identifier)
138                         .map(DataTreeCandidateNodes::unmodified);
139             case DELETE:
140             case WRITE:
141                 if (!canHaveChildren(oldMeta, newMeta)) {
142                     return Optional.empty();
143                 }
144                 return DataTreeCandidateNodes.containerDelta(getContainer(oldMeta), getContainer(newMeta), identifier);
145             default:
146                 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
147         }
148     }
149
150     private static final class ChildNode extends AbstractModifiedNodeBasedCandidateNode {
151         ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
152             super(mod, oldMeta, newMeta);
153         }
154
155         @Override
156         public PathArgument getIdentifier() {
157             return getMod().getIdentifier();
158         }
159     }
160
161     @Override
162     public String toString() {
163         return this.getClass().getSimpleName() + "{mod = " + this.mod + ", oldMeta = " + this.oldMeta + ", newMeta = "
164                 + this.newMeta + "}";
165     }
166 }