2 * Copyright (c) 2016 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
9 package org.opendaylight.mdsal.dom.store.inmemory;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.ListeningExecutorService;
16 import java.util.ArrayList;
17 import java.util.Iterator;
18 import java.util.Map.Entry;
19 import org.opendaylight.mdsal.common.api.ReadFailedException;
20 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
21 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
22 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 class InmemoryDOMDataTreeShardWriteTransaction implements DOMDataTreeShardWriteTransaction {
33 private static final Logger LOG = LoggerFactory.getLogger(InmemoryDOMDataTreeShardWriteTransaction.class);
35 private enum SimpleCursorOperation {
38 void applyOnLeaf(final DOMDataTreeWriteCursor cursor, final PathArgument child,
39 final NormalizedNode<?, ?> data) {
40 cursor.merge(child, data);
45 void applyOnLeaf(final DOMDataTreeWriteCursor cursor, final PathArgument child,
46 final NormalizedNode<?, ?> data) {
52 void applyOnLeaf(final DOMDataTreeWriteCursor cursor, final PathArgument child,
53 final NormalizedNode<?, ?> data) {
54 cursor.write(child, data);
58 abstract void applyOnLeaf(DOMDataTreeWriteCursor cursor, PathArgument child, NormalizedNode<?, ?> data);
60 void apply(final DOMDataTreeWriteCursor cursor, final YangInstanceIdentifier path,
61 final NormalizedNode<?, ?> data) {
63 final Iterator<PathArgument> it = path.getPathArguments().iterator();
64 while (it.hasNext()) {
65 final PathArgument currentArg = it.next();
67 // We need to enter one level deeper, we are not at leaf (modified) node
68 cursor.enter(currentArg);
71 applyOnLeaf(cursor, currentArg, data);
74 cursor.exit(enterCount);
78 private final ArrayList<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>();
79 private final InMemoryDOMDataTreeShardChangePublisher changePublisher;
80 private final ShardDataModification modification;
81 private final ListeningExecutorService executor;
82 private final DataTree rootShardDataTree;
84 private DataTreeModification rootModification = null;
85 private DOMDataTreeWriteCursor cursor;
86 private boolean finished = false;
88 InmemoryDOMDataTreeShardWriteTransaction(final ShardDataModification root,
89 final DataTree rootShardDataTree,
90 final InMemoryDOMDataTreeShardChangePublisher changePublisher, final ListeningExecutorService executor) {
91 this.modification = Preconditions.checkNotNull(root);
92 this.rootShardDataTree = Preconditions.checkNotNull(rootShardDataTree);
93 this.changePublisher = Preconditions.checkNotNull(changePublisher);
94 this.executor = executor;
97 private DOMDataTreeWriteCursor getCursor() {
99 cursor = new ShardDataModificationCursor(modification, this);
104 void delete(final YangInstanceIdentifier path) {
105 final YangInstanceIdentifier relativePath = toRelative(path);
106 Preconditions.checkArgument(!YangInstanceIdentifier.EMPTY.equals(relativePath),
107 "Deletion of shard root is not allowed");
108 SimpleCursorOperation.DELETE.apply(getCursor(), relativePath , null);
111 void merge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
112 SimpleCursorOperation.MERGE.apply(getCursor(), toRelative(path), data);
115 void write(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
116 SimpleCursorOperation.DELETE.apply(getCursor(), toRelative(path), data);
119 private YangInstanceIdentifier toRelative(final YangInstanceIdentifier path) {
120 final Optional<YangInstanceIdentifier> relative =
121 path.relativeTo(modification.getPrefix().getRootIdentifier());
122 Preconditions.checkArgument(relative.isPresent());
123 return relative.get();
126 public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
127 throw new UnsupportedOperationException("Not implemented yet");
130 public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
131 throw new UnsupportedOperationException("Not implemented yet");
135 public void close() {
136 Preconditions.checkState(!finished, "Attempting to close an already finished transaction.");
137 modification.closeTransactions();
138 if (cursor != null) {
144 void cursorClosed() {
145 Preconditions.checkNotNull(cursor);
149 public boolean isFinished() {
154 public void ready() {
155 Preconditions.checkState(!finished, "Attempting to ready an already finished transaction.");
156 Preconditions.checkState(cursor == null, "Attempting to ready a transaction that has an open cursor.");
157 Preconditions.checkNotNull(modification, "Attempting to ready an empty transaction.");
159 LOG.debug("Readying open transaction on shard {}", modification.getPrefix());
160 rootModification = modification.seal();
162 cohorts.add(new InMemoryDOMDataTreeShardThreePhaseCommitCohort(rootShardDataTree, rootModification, changePublisher));
163 for (final Entry<DOMDataTreeIdentifier, ForeignShardModificationContext> entry : modification.getChildShards().entrySet()) {
164 cohorts.add(new ForeignShardThreePhaseCommitCohort(entry.getKey(), entry.getValue()));
170 public ListenableFuture<Void> submit() {
171 LOG.debug("Submitting open transaction on shard {}", modification.getPrefix());
173 Preconditions.checkNotNull(cohorts);
174 Preconditions.checkState(!cohorts.isEmpty(), "Transaction was not readied yet.");
176 final ListenableFuture<Void> submit = executor.submit(new ShardSubmitCoordinationTask(modification.getPrefix(), cohorts));
182 public ListenableFuture<Boolean> validate() {
183 LOG.debug("CanCommit on open transaction on shard {}", modification.getPrefix());
185 final ListenableFuture<Boolean> submit = executor.submit(new ShardCanCommitCoordinationTask(modification.getPrefix(), cohorts));
190 public ListenableFuture<Void> prepare() {
191 LOG.debug("PreCommit on open transaction on shard {}", modification.getPrefix());
193 final ListenableFuture<Void> submit = executor.submit(new ShardPreCommitCoordinationTask(modification.getPrefix(), cohorts));
198 public ListenableFuture<Void> commit() {
199 LOG.debug("Commit open transaction on shard {}", modification.getPrefix());
201 final ListenableFuture<Void> submit = executor.submit(new ShardCommitCoordinationTask(modification.getPrefix(), cohorts));
205 public void followUp() {
210 public DOMDataTreeWriteCursor createCursor(final DOMDataTreeIdentifier prefix) {
211 Preconditions.checkState(!finished, "Transaction is finished/closed already.");
212 Preconditions.checkState(cursor == null, "Previous cursor wasn't closed");
213 final DOMDataTreeWriteCursor ret = getCursor();
214 final YangInstanceIdentifier relativePath = toRelative(prefix.getRootIdentifier());
215 ret.enter(relativePath.getPathArguments());