55ff68e39f9d6e3626000efde345d7b4386db0fc
[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.SettableFuture;
15 import org.opendaylight.controller.cluster.datastore.identifiers.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;
22
23 /**
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.
26  *
27  * @author Thomas Pantelis
28  */
29 abstract class LocalTransactionContext extends AbstractTransactionContext {
30     private final DOMStoreTransaction txDelegate;
31     private final LocalTransactionReadySupport readySupport;
32     private Exception operationError;
33
34     LocalTransactionContext(DOMStoreTransaction txDelegate, TransactionIdentifier identifier,
35             LocalTransactionReadySupport readySupport) {
36         super(identifier);
37         this.txDelegate = Preconditions.checkNotNull(txDelegate);
38         this.readySupport = readySupport;
39     }
40
41     protected abstract DOMStoreWriteTransaction getWriteDelegate();
42
43     protected abstract DOMStoreReadTransaction getReadDelegate();
44
45     @Override
46     public void executeModification(AbstractModification modification) {
47         incrementModificationCount();
48         if(operationError == null) {
49             try {
50                 modification.apply(getWriteDelegate());
51             } catch (Exception e) {
52                 operationError = e;
53             }
54         }
55     }
56
57     @Override
58     public <T> void executeRead(AbstractRead<T> readCmd, final SettableFuture<T> proxyFuture) {
59         Futures.addCallback(readCmd.apply(getReadDelegate()), new FutureCallback<T>() {
60             @Override
61             public void onSuccess(final T result) {
62                 proxyFuture.set(result);
63             }
64
65             @Override
66             public void onFailure(final Throwable t) {
67                 proxyFuture.setException(t);
68             }
69         });
70     }
71
72     private LocalThreePhaseCommitCohort ready() {
73         logModificationCount();
74         return readySupport.onTransactionReady(getWriteDelegate(), operationError);
75     }
76
77     @Override
78     public Future<ActorSelection> readyTransaction() {
79         final LocalThreePhaseCommitCohort cohort = ready();
80         return cohort.initiateCoordinatedCommit();
81     }
82
83     @Override
84     public Future<Object> directCommit() {
85         final LocalThreePhaseCommitCohort cohort = ready();
86         return cohort.initiateDirectCommit();
87     }
88
89     @Override
90     public void closeTransaction() {
91         txDelegate.close();
92     }
93 }