da6d6d14fe9a2c5a574914df0e36d463b0345a09
[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 java.util.Optional;
17 import java.util.SortedSet;
18 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
19 import org.opendaylight.controller.cluster.datastore.messages.AbstractRead;
20 import org.opendaylight.controller.cluster.datastore.modification.AbstractModification;
21 import org.opendaylight.mdsal.common.api.ReadFailedException;
22 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
23 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransaction;
24 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
25 import scala.concurrent.Future;
26
27 /**
28  * Processes front-end transaction operations locally before being committed to the destination shard.
29  * Instances of this class are used when the destination shard is local to the caller.
30  *
31  * @author Thomas Pantelis
32  */
33 abstract class LocalTransactionContext extends AbstractTransactionContext {
34     private final DOMStoreTransaction txDelegate;
35     private final LocalTransactionReadySupport readySupport;
36     private Exception operationError;
37
38     LocalTransactionContext(final DOMStoreTransaction txDelegate, final TransactionIdentifier identifier,
39             final LocalTransactionReadySupport readySupport) {
40         super(identifier);
41         this.txDelegate = Preconditions.checkNotNull(txDelegate);
42         this.readySupport = readySupport;
43     }
44
45     protected abstract DOMStoreWriteTransaction getWriteDelegate();
46
47     protected abstract DOMStoreReadTransaction getReadDelegate();
48
49     @Override
50     @SuppressWarnings("checkstyle:IllegalCatch")
51     public void executeModification(final AbstractModification modification, final Boolean havePermit) {
52         incrementModificationCount();
53         if (operationError == null) {
54             try {
55                 modification.apply(getWriteDelegate());
56             } catch (Exception e) {
57                 operationError = e;
58             }
59         }
60     }
61
62     @Override
63     public <T> void executeRead(final AbstractRead<T> readCmd, final SettableFuture<T> proxyFuture,
64             final Boolean havePermit) {
65         Futures.addCallback(readCmd.apply(getReadDelegate()), new FutureCallback<T>() {
66             @Override
67             public void onSuccess(final T result) {
68                 proxyFuture.set(result);
69             }
70
71             @Override
72             public void onFailure(final Throwable failure) {
73                 proxyFuture.setException(failure instanceof Exception
74                         ? ReadFailedException.MAPPER.apply((Exception) failure) : failure);
75             }
76         }, MoreExecutors.directExecutor());
77     }
78
79     private LocalThreePhaseCommitCohort ready() {
80         logModificationCount();
81         return readySupport.onTransactionReady(getWriteDelegate(), operationError);
82     }
83
84     @Override
85     public Future<ActorSelection> readyTransaction(final Boolean havePermit,
86             final Optional<SortedSet<String>> participatingShardNames) {
87         final LocalThreePhaseCommitCohort cohort = ready();
88         return cohort.initiateCoordinatedCommit(participatingShardNames);
89     }
90
91     @Override
92     public Future<Object> directCommit(final Boolean havePermit) {
93         final LocalThreePhaseCommitCohort cohort = ready();
94         return cohort.initiateDirectCommit();
95     }
96
97     @Override
98     public void closeTransaction() {
99         txDelegate.close();
100     }
101 }