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.Optional;
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
13 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
14 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
19 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeUtils;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 import javax.annotation.concurrent.GuardedBy;
24 import java.util.Map.Entry;
26 final class InMemoryDataTreeModification implements DataTreeModification {
27 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTreeModification.class);
28 private final ModificationApplyOperation strategyTree;
29 private final InMemoryDataTreeSnapshot snapshot;
30 private final ModifiedNode rootNode;
33 private boolean sealed = false;
35 InMemoryDataTreeModification(final InMemoryDataTreeSnapshot snapshot, final ModificationApplyOperation resolver) {
36 this.snapshot = Preconditions.checkNotNull(snapshot);
37 this.strategyTree = Preconditions.checkNotNull(resolver);
38 this.rootNode = ModifiedNode.createUnmodified(snapshot.getRootNode());
41 ModifiedNode getRootModification() {
45 ModificationApplyOperation getStrategy() {
50 public synchronized void write(final InstanceIdentifier path, final NormalizedNode<?, ?> value) {
52 resolveModificationFor(path).write(value);
56 public synchronized void merge(final InstanceIdentifier path, final NormalizedNode<?, ?> data) {
58 mergeImpl(resolveModificationFor(path),data);
61 private void mergeImpl(final OperationWithModification op,final NormalizedNode<?,?> data) {
63 if(data instanceof NormalizedNodeContainer<?,?,?>) {
64 @SuppressWarnings({ "rawtypes", "unchecked" })
65 NormalizedNodeContainer<?,?,NormalizedNode<PathArgument, ?>> dataContainer = (NormalizedNodeContainer) data;
66 for(NormalizedNode<PathArgument, ?> child : dataContainer.getValue()) {
67 PathArgument childId = child.getIdentifier();
68 mergeImpl(op.forChild(childId), child);
75 public synchronized void delete(final InstanceIdentifier path) {
77 resolveModificationFor(path).delete();
81 public synchronized Optional<NormalizedNode<?, ?>> readNode(final InstanceIdentifier path) {
83 * Walk the tree from the top, looking for the first node between root and
84 * the requested path which has been modified. If no such node exists,
85 * we use the node itself.
87 final Entry<InstanceIdentifier, ModifiedNode> entry = TreeNodeUtils.findClosestsOrFirstMatch(rootNode, path, ModifiedNode.IS_TERMINAL_PREDICATE);
88 final InstanceIdentifier key = entry.getKey();
89 final ModifiedNode mod = entry.getValue();
91 final Optional<TreeNode> result = resolveSnapshot(key, mod);
92 if (result.isPresent()) {
93 NormalizedNode<?, ?> data = result.get().getData();
94 return NormalizedNodeUtils.findNode(key, data, path);
96 return Optional.absent();
100 private Optional<TreeNode> resolveSnapshot(final InstanceIdentifier path,
101 final ModifiedNode modification) {
102 final Optional<Optional<TreeNode>> potentialSnapshot = modification.getSnapshotCache();
103 if(potentialSnapshot.isPresent()) {
104 return potentialSnapshot.get();
108 return resolveModificationStrategy(path).apply(modification, modification.getOriginal(),
109 snapshot.getRootNode().getSubtreeVersion().next());
110 } catch (Exception e) {
111 LOG.error("Could not create snapshot for {}:{}", path,modification,e);
116 private ModificationApplyOperation resolveModificationStrategy(final InstanceIdentifier path) {
117 LOG.trace("Resolving modification apply strategy for {}", path);
118 return TreeNodeUtils.findNodeChecked(strategyTree, path);
121 private OperationWithModification resolveModificationFor(final InstanceIdentifier path) {
122 ModifiedNode modification = rootNode;
123 // We ensure strategy is present.
124 ModificationApplyOperation operation = resolveModificationStrategy(path);
125 for (PathArgument pathArg : path.getPath()) {
126 modification = modification.modifyChild(pathArg);
128 return OperationWithModification.from(operation, modification);
132 public synchronized void ready() {
133 Preconditions.checkState(!sealed, "Attempted to seal an already-sealed Data Tree.");
139 private void checkSealed() {
140 Preconditions.checkState(!sealed, "Data Tree is sealed. No further modifications allowed.");
144 public String toString() {
145 return "MutableDataTree [modification=" + rootNode + "]";
149 public synchronized DataTreeModification newModification() {
150 Preconditions.checkState(sealed, "Attempted to chain on an unsealed modification");
152 if(rootNode.getType() == ModificationType.UNMODIFIED) {
153 return snapshot.newModification();
157 * FIXME: Add advanced transaction chaining for modification of not rebased
160 * Current computation of tempRoot may yeld incorrect subtree versions
161 * if there are multiple concurrent transactions, which may break
162 * versioning preconditions for modification of previously occured write,
163 * directly nested under parent node, since node version is derived from
166 * For deeper nodes subtree version is derived from their respective metadata
167 * nodes, so this incorrect root subtree version is not affecting us.
169 TreeNode originalSnapshotRoot = snapshot.getRootNode();
170 Optional<TreeNode> tempRoot = strategyTree.apply(rootNode, Optional.of(originalSnapshotRoot), originalSnapshotRoot.getSubtreeVersion().next());
172 InMemoryDataTreeSnapshot tempTree = new InMemoryDataTreeSnapshot(snapshot.getSchemaContext(), tempRoot.get(), strategyTree);
173 return tempTree.newModification();