2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
10 import com.google.common.base.Function;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Collections2;
14 import java.util.Collection;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
19 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
26 final class InMemoryDataTreeCandidate extends AbstractDataTreeCandidate {
27 private static abstract class AbstractNode implements DataTreeCandidateNode {
28 private final ModifiedNode mod;
29 private final TreeNode newMeta;
30 private final TreeNode oldMeta;
32 protected AbstractNode(final ModifiedNode mod,
33 final TreeNode oldMeta, final TreeNode newMeta) {
34 this.newMeta = newMeta;
35 this.oldMeta = oldMeta;
36 this.mod = Preconditions.checkNotNull(mod);
39 protected final ModifiedNode getMod() {
43 protected final TreeNode getNewMeta() {
47 protected final TreeNode getOldMeta() {
51 private static final TreeNode childMeta(final TreeNode parent, final PathArgument id) {
53 return parent.getChild(id).orNull();
59 private DataTreeCandidateNode childNode(final ModifiedNode input) {
60 final PathArgument id = input.getIdentifier();
61 return new ChildNode(input, childMeta(oldMeta, id), childMeta(newMeta, id));
65 public Collection<DataTreeCandidateNode> getChildNodes() {
66 return Collections2.transform(mod.getChildren(), new Function<ModifiedNode, DataTreeCandidateNode>() {
68 public DataTreeCandidateNode apply(final ModifiedNode input) {
69 return childNode(input);
75 public ModificationType getModificationType() {
76 switch (mod.getOperation()) {
78 return ModificationType.DELETE;
80 // Merge into non-existing data is a write
81 if (oldMeta == null) {
82 return ModificationType.WRITE;
85 // Data-based checks to narrow down types
86 final NormalizedNode<?, ?> data = newMeta.getData();
88 // leaf or anyxml are always written
89 if (!(data instanceof NormalizedNodeContainer)) {
90 return ModificationType.WRITE;
93 // Unkeyed collections are always written
94 if (data instanceof UnkeyedListNode || data instanceof OrderedMapNode || data instanceof OrderedLeafSetNode) {
95 return ModificationType.WRITE;
98 // Everything else is subtree modified
99 return ModificationType.SUBTREE_MODIFIED;
101 return ModificationType.SUBTREE_MODIFIED;
103 return ModificationType.UNMODIFIED;
105 return ModificationType.WRITE;
108 throw new IllegalStateException("Unhandled internal operation " + mod.getOperation());
111 private Optional<NormalizedNode<?, ?>> optionalData(final TreeNode meta) {
113 return Optional.<NormalizedNode<?,?>>of(meta.getData());
115 return Optional.absent();
120 public Optional<NormalizedNode<?, ?>> getDataAfter() {
121 return optionalData(newMeta);
125 public Optional<NormalizedNode<?, ?>> getDataBefore() {
126 return optionalData(oldMeta);
130 public DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
131 final Optional<ModifiedNode> childMod = mod.getChild(identifier);
132 if(childMod.isPresent()) {
133 return childNode(mod);
139 private static final class ChildNode extends AbstractNode {
140 public ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
141 super(mod, oldMeta, newMeta);
145 public PathArgument getIdentifier() {
146 return getMod().getIdentifier();
150 private static final class RootNode extends AbstractNode {
151 public RootNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
152 super(mod, oldMeta, newMeta);
156 public PathArgument getIdentifier() {
157 throw new IllegalStateException("Attempted to get identifier of the root node");
161 private final RootNode root;
163 InMemoryDataTreeCandidate(final YangInstanceIdentifier rootPath, final ModifiedNode modificationRoot,
164 final TreeNode beforeRoot, final TreeNode afterRoot) {
166 this.root = new RootNode(modificationRoot, beforeRoot, afterRoot);
169 TreeNode getAfterRoot() {
170 return root.getNewMeta();
173 TreeNode getBeforeRoot() {
174 return root.getOldMeta();
178 public DataTreeCandidateNode getRootNode() {