Merge "Create transaction on the backend datastore only when neccessary"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ChainedCommitCohort.java
1 /*
2  * Copyright (c) 2015 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.cluster.datastore;
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.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 final class ChainedCommitCohort extends ShardDataTreeCohort {
19     private static final Logger LOG = LoggerFactory.getLogger(ChainedCommitCohort.class);
20     private final ReadWriteShardDataTreeTransaction transaction;
21     private final ShardDataTreeTransactionChain chain;
22     private final ShardDataTreeCohort delegate;
23
24     ChainedCommitCohort(final ShardDataTreeTransactionChain chain, final ReadWriteShardDataTreeTransaction transaction, final ShardDataTreeCohort delegate) {
25         this.transaction = Preconditions.checkNotNull(transaction);
26         this.delegate = Preconditions.checkNotNull(delegate);
27         this.chain = Preconditions.checkNotNull(chain);
28     }
29
30     @Override
31     public ListenableFuture<Void> commit() {
32         final ListenableFuture<Void> ret = delegate.commit();
33
34         Futures.addCallback(ret, new FutureCallback<Void>() {
35             @Override
36             public void onSuccess(Void result) {
37                 chain.clearTransaction(transaction);
38                 LOG.debug("Committed transaction {}", transaction);
39             }
40
41             @Override
42             public void onFailure(Throwable t) {
43                 LOG.error("Transaction {} commit failed, cannot recover", transaction, t);
44             }
45         });
46
47         return ret;
48     }
49
50     @Override
51     public ListenableFuture<Boolean> canCommit() {
52         return delegate.canCommit();
53     }
54
55     @Override
56     public ListenableFuture<Void> preCommit() {
57         return delegate.preCommit();
58     }
59
60     @Override
61     public ListenableFuture<Void> abort() {
62         return delegate.abort();
63     }
64
65     @Override
66     DataTreeCandidateTip getCandidate() {
67         return delegate.getCandidate();
68     }
69 }