Move NormalizedNode-based DataTreeCandidateNodes into API
[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
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.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.DataTreeCandidateNodes;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
25
26 abstract class AbstractModifiedNodeBasedCandidateNode implements DataTreeCandidateNode {
27     private final ModifiedNode mod;
28     private final TreeNode newMeta;
29     private final TreeNode oldMeta;
30
31     protected AbstractModifiedNodeBasedCandidateNode(final ModifiedNode mod, final TreeNode oldMeta,
32             final TreeNode newMeta) {
33         this.newMeta = newMeta;
34         this.oldMeta = oldMeta;
35         this.mod = requireNonNull(mod);
36     }
37
38     protected final ModifiedNode getMod() {
39         return mod;
40     }
41
42     protected final TreeNode getNewMeta() {
43         return newMeta;
44     }
45
46     protected final TreeNode getOldMeta() {
47         return oldMeta;
48     }
49
50     private static TreeNode childMeta(final TreeNode parent, final PathArgument id) {
51         return parent == null ? null : parent.getChild(id).orElse(null);
52     }
53
54     private static boolean canHaveChildren(final @Nullable TreeNode oldMeta, final @Nullable TreeNode newMeta) {
55         if (oldMeta != null) {
56             return oldMeta.getData() instanceof NormalizedNodeContainer;
57         }
58         if (newMeta != null) {
59             return newMeta.getData() instanceof NormalizedNodeContainer;
60         }
61         return false;
62     }
63
64     @SuppressWarnings("unchecked")
65     private static NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> getContainer(
66             final @Nullable TreeNode meta) {
67         return meta == null ? null : (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>)meta.getData();
68     }
69
70     private ChildNode childNode(final ModifiedNode childMod) {
71         final PathArgument id = childMod.getIdentifier();
72         return new ChildNode(childMod, childMeta(oldMeta, id), childMeta(newMeta, id));
73     }
74
75     @Override
76     public Collection<DataTreeCandidateNode> getChildNodes() {
77         switch (mod.getModificationType()) {
78             case APPEARED:
79             case DISAPPEARED:
80             case SUBTREE_MODIFIED:
81                 return Collections2.transform(mod.getChildren(), this::childNode);
82             case UNMODIFIED:
83                 // Unmodified node, but we still need to resolve potential children. canHaveChildren returns
84                 // false if both arguments are null.
85                 if (!canHaveChildren(oldMeta, newMeta)) {
86                     return ImmutableList.of();
87                 }
88
89                 return Collections2.transform(getContainer(newMeta != null ? newMeta : oldMeta).getValue(),
90                     DataTreeCandidateNodes::unmodified);
91             case DELETE:
92             case WRITE:
93                 // This is unusual, the user is requesting we follow into an otherwise-terminal node.
94                 // We need to fudge things based on before/after data to correctly fake the expectations.
95                 if (!canHaveChildren(oldMeta, newMeta)) {
96                     return ImmutableList.of();
97                 }
98                 return DataTreeCandidateNodes.containerDelta(getContainer(oldMeta), getContainer(newMeta));
99             default:
100                 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
101         }
102     }
103
104     @Override
105     public ModificationType getModificationType() {
106         return 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.empty() : Optional.of(meta.getData());
111     }
112
113     @Override
114     public final Optional<NormalizedNode<?, ?>> getDataAfter() {
115         return optionalData(newMeta);
116     }
117
118     @Override
119     public final Optional<NormalizedNode<?, ?>> getDataBefore() {
120         return optionalData(oldMeta);
121     }
122
123     @Override
124     public final DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
125         switch (mod.getModificationType()) {
126             case APPEARED:
127             case DISAPPEARED:
128             case SUBTREE_MODIFIED:
129                 final Optional<ModifiedNode> childMod = mod.getChild(identifier);
130                 if (childMod.isPresent()) {
131                     return childNode(childMod.get());
132                 }
133                 return null;
134             case UNMODIFIED:
135                 if (!canHaveChildren(oldMeta, newMeta)) {
136                     return null;
137                 }
138                 final Optional<NormalizedNode<?, ?>> maybeChild = getContainer(newMeta != null ? newMeta : oldMeta)
139                         .getChild(identifier);
140                 return maybeChild.isPresent() ? DataTreeCandidateNodes.unmodified(maybeChild.get()) : null;
141             case DELETE:
142             case WRITE:
143                 if (!canHaveChildren(oldMeta, newMeta)) {
144                     return null;
145                 }
146                 return DataTreeCandidateNodes.containerDelta(getContainer(oldMeta), getContainer(newMeta), identifier);
147             default:
148                 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
149         }
150     }
151
152     private static final class ChildNode extends AbstractModifiedNodeBasedCandidateNode {
153         ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
154             super(mod, oldMeta, newMeta);
155         }
156
157         @Override
158         public PathArgument getIdentifier() {
159             return getMod().getIdentifier();
160         }
161     }
162
163     @Override
164     public String toString() {
165         return this.getClass().getSimpleName() + "{mod = " + this.mod + ", oldMeta = " + this.oldMeta + ", newMeta = "
166                 + this.newMeta + "}";
167     }
168 }