2 * Copyright (c) 2015 Brocade Communications 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.cluster.datastore;
10 import static java.util.Objects.requireNonNull;
12 import akka.actor.ActorSelection;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import com.google.common.util.concurrent.SettableFuture;
17 import java.util.Optional;
18 import java.util.SortedSet;
19 import java.util.function.Consumer;
20 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
21 import org.opendaylight.controller.cluster.datastore.messages.AbstractRead;
22 import org.opendaylight.mdsal.common.api.ReadFailedException;
23 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
24 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransaction;
25 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import scala.concurrent.Future;
31 * Processes front-end transaction operations locally before being committed to the destination shard.
32 * Instances of this class are used when the destination shard is local to the caller.
34 * @author Thomas Pantelis
36 abstract class LocalTransactionContext extends TransactionContext {
37 private final DOMStoreTransaction txDelegate;
38 private final LocalTransactionReadySupport readySupport;
39 private Exception operationError;
41 LocalTransactionContext(final DOMStoreTransaction txDelegate, final TransactionIdentifier identifier,
42 final LocalTransactionReadySupport readySupport) {
44 this.txDelegate = requireNonNull(txDelegate);
45 this.readySupport = readySupport;
48 abstract DOMStoreWriteTransaction getWriteDelegate();
50 abstract DOMStoreReadTransaction getReadDelegate();
52 @SuppressWarnings("checkstyle:IllegalCatch")
53 private void executeModification(final Consumer<DOMStoreWriteTransaction> consumer) {
54 incrementModificationCount();
55 if (operationError == null) {
57 consumer.accept(getWriteDelegate());
58 } catch (Exception e) {
65 void executeDelete(final YangInstanceIdentifier path, final Boolean havePermit) {
66 executeModification(transaction -> transaction.delete(path));
70 void executeMerge(final YangInstanceIdentifier path, final NormalizedNode data, final Boolean havePermit) {
71 executeModification(transaction -> transaction.merge(path, data));
75 void executeWrite(final YangInstanceIdentifier path, final NormalizedNode data, final Boolean havePermit) {
76 executeModification(transaction -> transaction.write(path, data));
80 <T> void executeRead(final AbstractRead<T> readCmd, final SettableFuture<T> proxyFuture,
81 final Boolean havePermit) {
82 Futures.addCallback(readCmd.apply(getReadDelegate()), new FutureCallback<T>() {
84 public void onSuccess(final T result) {
85 proxyFuture.set(result);
89 public void onFailure(final Throwable failure) {
90 proxyFuture.setException(failure instanceof Exception
91 ? ReadFailedException.MAPPER.apply((Exception) failure) : failure);
93 }, MoreExecutors.directExecutor());
97 Future<ActorSelection> readyTransaction(final Boolean havePermit,
98 final Optional<SortedSet<String>> participatingShardNames) {
99 final LocalThreePhaseCommitCohort cohort = ready();
100 return cohort.initiateCoordinatedCommit(participatingShardNames);
104 Future<Object> directCommit(final Boolean havePermit) {
105 final LocalThreePhaseCommitCohort cohort = ready();
106 return cohort.initiateDirectCommit();
110 void closeTransaction() {
114 private LocalThreePhaseCommitCohort ready() {
115 logModificationCount();
116 return readySupport.onTransactionReady(getWriteDelegate(), operationError);