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.Preconditions;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.ListeningExecutorService;
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.Map.Entry;
17 import java.util.Optional;
18 import java.util.concurrent.atomic.AtomicLong;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
20 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
21 import org.opendaylight.mdsal.dom.spi.shard.DOMDataTreeShardWriteTransaction;
22 import org.opendaylight.mdsal.dom.spi.shard.ForeignShardModificationContext;
23 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
24 import org.opendaylight.yangtools.concepts.Identifiable;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 class InmemoryDOMDataTreeShardWriteTransaction implements DOMDataTreeShardWriteTransaction, Identifiable<String> {
35 private static final Logger LOG = LoggerFactory.getLogger(InmemoryDOMDataTreeShardWriteTransaction.class);
37 private enum SimpleCursorOperation {
40 void applyOnLeaf(final DOMDataTreeWriteCursor cur, final PathArgument child,
41 final NormalizedNode<?, ?> data) {
42 cur.merge(child, data);
47 void applyOnLeaf(final DOMDataTreeWriteCursor cur, final PathArgument child,
48 final NormalizedNode<?, ?> data) {
54 void applyOnLeaf(final DOMDataTreeWriteCursor cur, final PathArgument child,
55 final NormalizedNode<?, ?> data) {
56 cur.write(child, data);
60 abstract void applyOnLeaf(DOMDataTreeWriteCursor cur, PathArgument child, NormalizedNode<?, ?> data);
62 void apply(final DOMDataTreeWriteCursor cur, final YangInstanceIdentifier path,
63 final NormalizedNode<?, ?> data) {
65 final Iterator<PathArgument> it = path.getPathArguments().iterator();
68 final PathArgument currentArg = it.next();
70 applyOnLeaf(cur, currentArg, data);
74 // We need to enter one level deeper, we are not at leaf (modified) node
75 cur.enter(currentArg);
84 private static final AtomicLong COUNTER = new AtomicLong();
86 private final ArrayList<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>();
87 private final InMemoryDOMDataTreeShardChangePublisher changePublisher;
88 private final InMemoryDOMDataTreeShardProducer producer;
89 private final ShardDataModification modification;
90 private final ListeningExecutorService executor;
91 private final DataTree rootShardDataTree;
92 private final String identifier;
94 private DataTreeModification rootModification = null;
95 private DOMDataTreeWriteCursor cursor;
96 private boolean finished = false;
98 InmemoryDOMDataTreeShardWriteTransaction(final InMemoryDOMDataTreeShardProducer producer,
99 final ShardDataModification root,
100 final DataTree rootShardDataTree,
101 final InMemoryDOMDataTreeShardChangePublisher changePublisher,
102 final ListeningExecutorService executor) {
103 this.producer = producer;
104 this.modification = Preconditions.checkNotNull(root);
105 this.rootShardDataTree = Preconditions.checkNotNull(rootShardDataTree);
106 this.changePublisher = Preconditions.checkNotNull(changePublisher);
107 this.identifier = "INMEMORY-SHARD-TX-" + COUNTER.getAndIncrement();
108 LOG.debug("Shard transaction{} created", identifier);
109 this.executor = executor;
113 public String getIdentifier() {
117 private DOMDataTreeWriteCursor getCursor() {
118 if (cursor == null) {
119 cursor = new InMemoryShardDataModificationCursor(modification, this);
124 void delete(final YangInstanceIdentifier path) {
125 final YangInstanceIdentifier relativePath = toRelative(path);
126 Preconditions.checkArgument(!YangInstanceIdentifier.EMPTY.equals(relativePath),
127 "Deletion of shard root is not allowed");
128 SimpleCursorOperation.DELETE.apply(getCursor(), relativePath, null);
131 void merge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
132 SimpleCursorOperation.MERGE.apply(getCursor(), toRelative(path), data);
135 void write(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
136 SimpleCursorOperation.WRITE.apply(getCursor(), toRelative(path), data);
139 private YangInstanceIdentifier toRelative(final YangInstanceIdentifier path) {
140 final Optional<YangInstanceIdentifier> relative =
141 path.relativeTo(modification.getPrefix().getRootIdentifier());
142 Preconditions.checkArgument(relative.isPresent());
143 return relative.get();
147 public void close() {
148 Preconditions.checkState(!finished, "Attempting to close an already finished transaction.");
149 modification.closeTransactions();
150 if (cursor != null) {
153 producer.transactionAborted(this);
157 void cursorClosed() {
158 Preconditions.checkNotNull(cursor);
159 modification.closeCursor();
163 public boolean isFinished() {
168 public void ready() {
169 Preconditions.checkState(!finished, "Attempting to ready an already finished transaction.");
170 Preconditions.checkState(cursor == null, "Attempting to ready a transaction that has an open cursor.");
171 Preconditions.checkNotNull(modification, "Attempting to ready an empty transaction.");
173 LOG.debug("Readying open transaction on shard {}", modification.getPrefix());
174 rootModification = modification.seal();
176 producer.transactionReady(this, rootModification);
177 cohorts.add(new InMemoryDOMDataTreeShardThreePhaseCommitCohort(
178 rootShardDataTree, rootModification, changePublisher));
179 for (final Entry<DOMDataTreeIdentifier, ForeignShardModificationContext> entry :
180 modification.getChildShards().entrySet()) {
181 cohorts.add(new ForeignShardThreePhaseCommitCohort(entry.getKey(), entry.getValue()));
187 public ListenableFuture<Void> submit() {
188 LOG.debug("Submitting open transaction on shard {}", modification.getPrefix());
190 Preconditions.checkNotNull(cohorts);
191 Preconditions.checkState(!cohorts.isEmpty(), "Transaction was not readied yet.");
193 return executor.submit(new ShardSubmitCoordinationTask(modification.getPrefix(), cohorts, this));
197 public ListenableFuture<Boolean> validate() {
198 LOG.debug("CanCommit on open transaction on shard {}", modification.getPrefix());
199 return executor.submit(new ShardCanCommitCoordinationTask(modification.getPrefix(), cohorts));
203 public ListenableFuture<Void> prepare() {
204 LOG.debug("PreCommit on open transaction on shard {}", modification.getPrefix());
205 return executor.submit(new ShardPreCommitCoordinationTask(modification.getPrefix(), cohorts));
209 public ListenableFuture<Void> commit() {
210 LOG.debug("Commit open transaction on shard {}", modification.getPrefix());
211 return executor.submit(new ShardCommitCoordinationTask(modification.getPrefix(), cohorts, this));
214 DataTreeModification getRootModification() {
215 Preconditions.checkNotNull(rootModification, "Transaction wasn't sealed yet");
216 return rootModification;
219 void transactionCommited(final InmemoryDOMDataTreeShardWriteTransaction tx) {
220 producer.onTransactionCommited(tx);
224 public DOMDataTreeWriteCursor createCursor(final DOMDataTreeIdentifier prefix) {
225 Preconditions.checkState(!finished, "Transaction is finished/closed already.");
226 Preconditions.checkState(cursor == null, "Previous cursor wasn't closed");
227 final YangInstanceIdentifier relativePath = toRelative(prefix.getRootIdentifier());
228 final DOMDataTreeWriteCursor ret = getCursor();
229 ret.enter(relativePath.getPathArguments());