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.SettableFuture;
15 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
16 import org.opendaylight.controller.cluster.datastore.messages.AbstractRead;
17 import org.opendaylight.controller.cluster.datastore.modification.AbstractModification;
18 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
21 import scala.concurrent.Future;
24 * Processes front-end transaction operations locally before being committed to the destination shard.
25 * Instances of this class are used when the destination shard is local to the caller.
27 * @author Thomas Pantelis
29 abstract class LocalTransactionContext extends AbstractTransactionContext {
30 private final DOMStoreTransaction txDelegate;
31 private final LocalTransactionReadySupport readySupport;
32 private Exception operationError;
34 LocalTransactionContext(DOMStoreTransaction txDelegate, TransactionIdentifier identifier,
35 LocalTransactionReadySupport readySupport) {
37 this.txDelegate = Preconditions.checkNotNull(txDelegate);
38 this.readySupport = readySupport;
41 protected abstract DOMStoreWriteTransaction getWriteDelegate();
43 protected abstract DOMStoreReadTransaction getReadDelegate();
46 @SuppressWarnings("checkstyle:IllegalCatch")
47 public void executeModification(AbstractModification modification) {
48 incrementModificationCount();
49 if (operationError == null) {
51 modification.apply(getWriteDelegate());
52 } catch (Exception e) {
59 public <T> void executeRead(AbstractRead<T> readCmd, final SettableFuture<T> proxyFuture) {
60 Futures.addCallback(readCmd.apply(getReadDelegate()), new FutureCallback<T>() {
62 public void onSuccess(final T result) {
63 proxyFuture.set(result);
67 public void onFailure(final Throwable failure) {
68 proxyFuture.setException(failure);
73 private LocalThreePhaseCommitCohort ready() {
74 logModificationCount();
75 return readySupport.onTransactionReady(getWriteDelegate(), operationError);
79 public Future<ActorSelection> readyTransaction() {
80 final LocalThreePhaseCommitCohort cohort = ready();
81 return cohort.initiateCoordinatedCommit();
85 public Future<Object> directCommit() {
86 final LocalThreePhaseCommitCohort cohort = ready();
87 return cohort.initiateDirectCommit();
91 public void closeTransaction() {