9cf5312c1efccb687a87b05c96612e39d8344a39
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / LocalTransactionContext.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
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.mdsal.dom.spi.store.DOMStoreReadTransaction;
20 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransaction;
21 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
22 import scala.concurrent.Future;
23
24 /**
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.
27  *
28  * @author Thomas Pantelis
29  */
30 abstract class LocalTransactionContext extends AbstractTransactionContext {
31     private final DOMStoreTransaction txDelegate;
32     private final LocalTransactionReadySupport readySupport;
33     private Exception operationError;
34
35     LocalTransactionContext(final DOMStoreTransaction txDelegate, final TransactionIdentifier identifier,
36             final LocalTransactionReadySupport readySupport) {
37         super(identifier);
38         this.txDelegate = Preconditions.checkNotNull(txDelegate);
39         this.readySupport = readySupport;
40     }
41
42     protected abstract DOMStoreWriteTransaction getWriteDelegate();
43
44     protected abstract DOMStoreReadTransaction getReadDelegate();
45
46     @Override
47     @SuppressWarnings("checkstyle:IllegalCatch")
48     public void executeModification(final AbstractModification modification, final Boolean havePermit) {
49         incrementModificationCount();
50         if (operationError == null) {
51             try {
52                 modification.apply(getWriteDelegate());
53             } catch (Exception e) {
54                 operationError = e;
55             }
56         }
57     }
58
59     @Override
60     public <T> void executeRead(final AbstractRead<T> readCmd, final SettableFuture<T> proxyFuture,
61             final Boolean havePermit) {
62         Futures.addCallback(readCmd.apply(getReadDelegate()), new FutureCallback<T>() {
63             @Override
64             public void onSuccess(final T result) {
65                 proxyFuture.set(result);
66             }
67
68             @Override
69             public void onFailure(final Throwable failure) {
70                 proxyFuture.setException(failure);
71             }
72         }, MoreExecutors.directExecutor());
73     }
74
75     private LocalThreePhaseCommitCohort ready() {
76         logModificationCount();
77         return readySupport.onTransactionReady(getWriteDelegate(), operationError);
78     }
79
80     @Override
81     public Future<ActorSelection> readyTransaction(final Boolean havePermit) {
82         final LocalThreePhaseCommitCohort cohort = ready();
83         return cohort.initiateCoordinatedCommit();
84     }
85
86     @Override
87     public Future<Object> directCommit(final Boolean havePermit) {
88         final LocalThreePhaseCommitCohort cohort = ready();
89         return cohort.initiateDirectCommit();
90     }
91
92     @Override
93     public void closeTransaction() {
94         txDelegate.close();
95     }
96 }