Merge "Cleanup RpcRoutingStrategy definition"
[controller.git] / opendaylight / md-sal / sal-inmemory-datastore / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / ChainedTransactionCommitImpl.java
1 /*
2  * Copyright (c) 2014 Cisco 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.md.sal.dom.store.impl;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
15
16 final class ChainedTransactionCommitImpl implements DOMStoreThreePhaseCommitCohort {
17     private final SnapshotBackedWriteTransaction transaction;
18     private final DOMStoreThreePhaseCommitCohort delegate;
19     private final DOMStoreTransactionChainImpl txChain;
20
21     protected ChainedTransactionCommitImpl(final SnapshotBackedWriteTransaction transaction,
22             final DOMStoreThreePhaseCommitCohort delegate, final DOMStoreTransactionChainImpl txChain) {
23         this.transaction = Preconditions.checkNotNull(transaction);
24         this.delegate = Preconditions.checkNotNull(delegate);
25         this.txChain = Preconditions.checkNotNull(txChain);
26     }
27
28     @Override
29     public ListenableFuture<Boolean> canCommit() {
30         return delegate.canCommit();
31     }
32
33     @Override
34     public ListenableFuture<Void> preCommit() {
35         return delegate.preCommit();
36     }
37
38     @Override
39     public ListenableFuture<Void> abort() {
40         return delegate.abort();
41     }
42
43     @Override
44     public ListenableFuture<Void> commit() {
45         ListenableFuture<Void> commitFuture = delegate.commit();
46         Futures.addCallback(commitFuture, new FutureCallback<Void>() {
47             @Override
48             public void onFailure(final Throwable t) {
49                 txChain.onTransactionFailed(transaction, t);
50             }
51
52             @Override
53             public void onSuccess(final Void result) {
54                 txChain.onTransactionCommited(transaction);
55             }
56         });
57         return commitFuture;
58     }
59 }