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.controller.md.sal.dom.store.impl.tree.data;
10 import static com.google.common.base.Preconditions.checkState;
12 import java.util.Map.Entry;
13 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
15 import org.opendaylight.controller.md.sal.dom.store.impl.tree.DataTreeModification;
16 import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreUtils;
17 import org.opendaylight.controller.md.sal.dom.store.impl.tree.TreeNodeUtils;
18 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
22 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeUtils;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import com.google.common.base.Optional;
27 import com.google.common.base.Preconditions;
29 final class InMemoryDataTreeModification implements DataTreeModification {
30 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTreeModification.class);
33 * FIXME: the thread safety of concurrent write/delete/read/seal operations
34 * needs to be evaluated.
36 private static final AtomicIntegerFieldUpdater<InMemoryDataTreeModification> SEALED_UPDATER =
37 AtomicIntegerFieldUpdater.newUpdater(InMemoryDataTreeModification.class, "sealed");
38 private volatile int sealed = 0;
40 private final ModificationApplyOperation strategyTree;
41 private final InMemoryDataTreeSnapshot snapshot;
42 private final NodeModification rootNode;
44 InMemoryDataTreeModification(final InMemoryDataTreeSnapshot snapshot, final ModificationApplyOperation resolver) {
45 this.snapshot = Preconditions.checkNotNull(snapshot);
46 this.strategyTree = Preconditions.checkNotNull(resolver);
47 this.rootNode = NodeModification.createUnmodified(snapshot.getRootNode());
50 NodeModification getRootModification() {
54 ModificationApplyOperation getStrategy() {
59 public void write(final InstanceIdentifier path, final NormalizedNode<?, ?> value) {
61 resolveModificationFor(path).write(value);
65 public void merge(final InstanceIdentifier path, final NormalizedNode<?, ?> data) {
67 mergeImpl(resolveModificationFor(path),data);
70 private void mergeImpl(final OperationWithModification op,final NormalizedNode<?,?> data) {
72 if(data instanceof NormalizedNodeContainer<?,?,?>) {
73 @SuppressWarnings({ "rawtypes", "unchecked" })
74 NormalizedNodeContainer<?,?,NormalizedNode<PathArgument, ?>> dataContainer = (NormalizedNodeContainer) data;
75 for(NormalizedNode<PathArgument, ?> child : dataContainer.getValue()) {
76 PathArgument childId = child.getIdentifier();
77 mergeImpl(op.forChild(childId), child);
84 public void delete(final InstanceIdentifier path) {
86 resolveModificationFor(path).delete();
90 public Optional<NormalizedNode<?, ?>> readNode(final InstanceIdentifier path) {
92 * Walk the tree from the top, looking for the first node between root and
93 * the requested path which has been modified. If no such node exists,
94 * we use the node itself.
96 final Entry<InstanceIdentifier, NodeModification> entry = TreeNodeUtils.findClosestsOrFirstMatch(rootNode, path, NodeModification.IS_TERMINAL_PREDICATE);
97 final InstanceIdentifier key = entry.getKey();
98 final NodeModification mod = entry.getValue();
100 final Optional<StoreMetadataNode> result = resolveSnapshot(key, mod);
101 if (result.isPresent()) {
102 NormalizedNode<?, ?> data = result.get().getData();
103 return NormalizedNodeUtils.findNode(key, data, path);
105 return Optional.absent();
109 private Optional<StoreMetadataNode> resolveSnapshot(final InstanceIdentifier path,
110 final NodeModification modification) {
111 final Optional<Optional<StoreMetadataNode>> potentialSnapshot = modification.getSnapshotCache();
112 if(potentialSnapshot.isPresent()) {
113 return potentialSnapshot.get();
117 return resolveModificationStrategy(path).apply(modification, modification.getOriginal(),
118 StoreUtils.increase(snapshot.getRootNode().getSubtreeVersion()));
119 } catch (Exception e) {
120 LOG.error("Could not create snapshot for {}:{}", path,modification,e);
125 private ModificationApplyOperation resolveModificationStrategy(final InstanceIdentifier path) {
126 LOG.trace("Resolving modification apply strategy for {}", path);
127 return TreeNodeUtils.findNodeChecked(strategyTree, path);
130 private OperationWithModification resolveModificationFor(final InstanceIdentifier path) {
131 NodeModification modification = rootNode;
132 // We ensure strategy is present.
133 ModificationApplyOperation operation = resolveModificationStrategy(path);
134 for (PathArgument pathArg : path.getPath()) {
135 modification = modification.modifyChild(pathArg);
137 return OperationWithModification.from(operation, modification);
142 final boolean success = SEALED_UPDATER.compareAndSet(this, 0, 1);
143 Preconditions.checkState(success, "Attempted to seal an already-sealed Data Tree.");
147 private void checkSealed() {
148 checkState(sealed == 0, "Data Tree is sealed. No further modifications allowed.");
152 public String toString() {
153 return "MutableDataTree [modification=" + rootNode + "]";
157 public DataTreeModification newModification() {
158 // FIXME: transaction chaining
159 throw new UnsupportedOperationException("Implement this as part of transaction chaining");