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.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;
30 * FIXME: the thread safety of concurrent write/delete/read/seal operations
31 * needs to be evaluated.
33 class MutableDataTree {
34 private static final Logger LOG = LoggerFactory.getLogger(MutableDataTree.class);
35 private final AtomicBoolean sealed = new AtomicBoolean();
36 private final ModificationApplyOperation strategyTree;
37 private final DataAndMetadataSnapshot snapshot;
38 private final NodeModification rootModification;
40 private MutableDataTree(final DataAndMetadataSnapshot snapshot, final ModificationApplyOperation strategyTree) {
41 this.snapshot = snapshot;
42 this.strategyTree = strategyTree;
43 this.rootModification = NodeModification.createUnmodified(snapshot.getMetadataTree());
46 public void write(final InstanceIdentifier path, final NormalizedNode<?, ?> value) {
48 resolveModificationFor(path).write(value);
51 public void merge(final InstanceIdentifier path, final NormalizedNode<?, ?> data) {
53 mergeImpl(resolveModificationFor(path),data);
56 private void mergeImpl(final OperationWithModification op,final NormalizedNode<?,?> data) {
58 if(data instanceof NormalizedNodeContainer<?,?,?>) {
59 @SuppressWarnings({ "rawtypes", "unchecked" })
60 NormalizedNodeContainer<?,?,NormalizedNode<PathArgument, ?>> dataContainer = (NormalizedNodeContainer) data;
61 for(NormalizedNode<PathArgument, ?> child : dataContainer.getValue()) {
62 PathArgument childId = child.getIdentifier();
63 mergeImpl(op.forChild(childId), child);
69 public void delete(final InstanceIdentifier path) {
71 resolveModificationFor(path).delete();
74 public Optional<NormalizedNode<?, ?>> read(final InstanceIdentifier path) {
75 Entry<InstanceIdentifier, NodeModification> modification = TreeNodeUtils.findClosestsOrFirstMatch(rootModification, path, NodeModification.IS_TERMINAL_PREDICATE);
77 Optional<StoreMetadataNode> result = resolveSnapshot(modification);
78 if (result.isPresent()) {
79 NormalizedNode<?, ?> data = result.get().getData();
80 return NormalizedNodeUtils.findNode(modification.getKey(), data, path);
82 return Optional.absent();
85 private Optional<StoreMetadataNode> resolveSnapshot(
86 final Entry<InstanceIdentifier, NodeModification> keyModification) {
87 InstanceIdentifier path = keyModification.getKey();
88 NodeModification modification = keyModification.getValue();
89 return resolveSnapshot(path, modification);
92 private Optional<StoreMetadataNode> resolveSnapshot(final InstanceIdentifier path,
93 final NodeModification modification) {
95 Optional<Optional<StoreMetadataNode>> potentialSnapshot = modification.getSnapshotCache();
96 if(potentialSnapshot.isPresent()) {
97 return potentialSnapshot.get();
99 return resolveModificationStrategy(path).apply(modification, modification.getOriginal(),
100 StoreUtils.increase(snapshot.getMetadataTree().getSubtreeVersion()));
101 } catch (Exception e) {
102 LOG.error("Could not create snapshot for {}:{}", path,modification,e);
107 private ModificationApplyOperation resolveModificationStrategy(final InstanceIdentifier path) {
108 LOG.trace("Resolving modification apply strategy for {}", path);
109 return TreeNodeUtils.findNodeChecked(strategyTree, path);
112 private OperationWithModification resolveModificationFor(final InstanceIdentifier path) {
113 NodeModification modification = rootModification;
114 // We ensure strategy is present.
115 ModificationApplyOperation operation = resolveModificationStrategy(path);
116 for (PathArgument pathArg : path.getPath()) {
117 modification = modification.modifyChild(pathArg);
119 return OperationWithModification.from(operation, modification);
122 public static MutableDataTree from(final DataAndMetadataSnapshot snapshot, final ModificationApplyOperation resolver) {
123 return new MutableDataTree(snapshot, resolver);
127 final boolean success = sealed.compareAndSet(false, true);
128 Preconditions.checkState(success, "Attempted to seal an already-sealed Data Tree.");
129 rootModification.seal();
132 private void checkSealed() {
133 checkState(!sealed.get(), "Data Tree is sealed. No further modifications allowed.");
136 protected NodeModification getRootModification() {
137 return rootModification;
141 public String toString() {
142 return "MutableDataTree [modification=" + rootModification + "]";