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 akka.actor.ActorSelection;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import com.google.common.util.concurrent.SettableFuture;
16 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
17 import org.opendaylight.controller.cluster.datastore.messages.AbstractRead;
18 import org.opendaylight.controller.cluster.datastore.modification.AbstractModification;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
22 import scala.concurrent.Future;
25 * Processes front-end transaction operations locally before being committed to the destination shard.
26 * Instances of this class are used when the destination shard is local to the caller.
28 * @author Thomas Pantelis
30 abstract class LocalTransactionContext extends AbstractTransactionContext {
31 private final DOMStoreTransaction txDelegate;
32 private final LocalTransactionReadySupport readySupport;
33 private Exception operationError;
35 LocalTransactionContext(final DOMStoreTransaction txDelegate, final TransactionIdentifier identifier,
36 final LocalTransactionReadySupport readySupport) {
38 this.txDelegate = Preconditions.checkNotNull(txDelegate);
39 this.readySupport = readySupport;
42 protected abstract DOMStoreWriteTransaction getWriteDelegate();
44 protected abstract DOMStoreReadTransaction getReadDelegate();
47 @SuppressWarnings("checkstyle:IllegalCatch")
48 public void executeModification(final AbstractModification modification) {
49 incrementModificationCount();
50 if (operationError == null) {
52 modification.apply(getWriteDelegate());
53 } catch (Exception e) {
60 public <T> void executeRead(final AbstractRead<T> readCmd, final SettableFuture<T> proxyFuture) {
61 Futures.addCallback(readCmd.apply(getReadDelegate()), new FutureCallback<T>() {
63 public void onSuccess(final T result) {
64 proxyFuture.set(result);
68 public void onFailure(final Throwable failure) {
69 proxyFuture.setException(failure);
71 }, MoreExecutors.directExecutor());
74 private LocalThreePhaseCommitCohort ready() {
75 logModificationCount();
76 return readySupport.onTransactionReady(getWriteDelegate(), operationError);
80 public Future<ActorSelection> readyTransaction() {
81 final LocalThreePhaseCommitCohort cohort = ready();
82 return cohort.initiateCoordinatedCommit();
86 public Future<Object> directCommit() {
87 final LocalThreePhaseCommitCohort cohort = ready();
88 return cohort.initiateDirectCommit();
92 public void closeTransaction() {