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