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;
10 import static com.google.common.base.Preconditions.checkState;
12 import java.util.Map.Entry;
13 import java.util.concurrent.atomic.AtomicBoolean;
15 import org.opendaylight.controller.md.sal.dom.store.impl.tree.NodeModification;
16 import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreMetadataNode;
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.impl.schema.NormalizedNodeUtils;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 import com.google.common.base.Optional;
26 import com.google.common.base.Preconditions;
29 * FIXME: the thread safety of concurrent write/delete/read/seal operations
30 * needs to be evaluated.
32 class MutableDataTree {
33 private static final Logger LOG = LoggerFactory.getLogger(MutableDataTree.class);
34 private final AtomicBoolean sealed = new AtomicBoolean();
35 private final ModificationApplyOperation strategyTree;
36 private final DataAndMetadataSnapshot snapshot;
37 private final NodeModification rootModification;
39 private MutableDataTree(final DataAndMetadataSnapshot snapshot, final ModificationApplyOperation strategyTree) {
40 this.snapshot = snapshot;
41 this.strategyTree = strategyTree;
42 this.rootModification = NodeModification.createUnmodified(snapshot.getMetadataTree());
45 public void write(final InstanceIdentifier path, final NormalizedNode<?, ?> value) {
47 resolveModificationFor(path).write(value);
50 public void delete(final InstanceIdentifier path) {
52 resolveModificationFor(path).delete();
55 public Optional<NormalizedNode<?, ?>> read(final InstanceIdentifier path) {
56 Entry<InstanceIdentifier, NodeModification> modification = TreeNodeUtils.findClosestsOrFirstMatch(rootModification, path, NodeModification.IS_TERMINAL_PREDICATE);
58 Optional<StoreMetadataNode> result = resolveSnapshot(modification);
59 if (result.isPresent()) {
60 NormalizedNode<?, ?> data = result.get().getData();
61 return NormalizedNodeUtils.findNode(modification.getKey(), data, path);
63 return Optional.absent();
66 private Optional<StoreMetadataNode> resolveSnapshot(
67 final Entry<InstanceIdentifier, NodeModification> keyModification) {
68 InstanceIdentifier path = keyModification.getKey();
69 NodeModification modification = keyModification.getValue();
70 return resolveSnapshot(path, modification);
73 private Optional<StoreMetadataNode> resolveSnapshot(final InstanceIdentifier path,
74 final NodeModification modification) {
76 Optional<Optional<StoreMetadataNode>> potentialSnapshot = modification.getSnapshotCache();
77 if(potentialSnapshot.isPresent()) {
78 return potentialSnapshot.get();
80 return resolveModificationStrategy(path).apply(modification, modification.getOriginal(),
81 StoreUtils.increase(snapshot.getMetadataTree().getSubtreeVersion()));
82 } catch (Exception e) {
83 LOG.error("Could not create snapshot for {}", path,e);
88 private ModificationApplyOperation resolveModificationStrategy(final InstanceIdentifier path) {
89 LOG.trace("Resolving modification apply strategy for {}", path);
90 return TreeNodeUtils.findNodeChecked(strategyTree, path);
93 private OperationWithModification resolveModificationFor(final InstanceIdentifier path) {
94 NodeModification modification = rootModification;
95 // We ensure strategy is present.
96 ModificationApplyOperation operation = resolveModificationStrategy(path);
97 for (PathArgument pathArg : path.getPath()) {
98 modification = modification.modifyChild(pathArg);
100 return OperationWithModification.from(operation, modification);
103 public static MutableDataTree from(final DataAndMetadataSnapshot snapshot, final ModificationApplyOperation resolver) {
104 return new MutableDataTree(snapshot, resolver);
108 final boolean success = sealed.compareAndSet(false, true);
109 Preconditions.checkState(success, "Attempted to seal an already-sealed Data Tree.");
110 rootModification.seal();
113 private void checkSealed() {
114 checkState(!sealed.get(), "Data Tree is sealed. No further modifications allowed.");
117 protected NodeModification getRootModification() {
118 return rootModification;
122 public String toString() {
123 return "MutableDataTree [modification=" + rootModification + "]";