Move ShardManagerSnapshot to new package
[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.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 final class ChainedCommitCohort extends ShardDataTreeCohort {
20     private static final Logger LOG = LoggerFactory.getLogger(ChainedCommitCohort.class);
21     private final ReadWriteShardDataTreeTransaction transaction;
22     private final ShardDataTreeTransactionChain chain;
23     private final ShardDataTreeCohort delegate;
24
25     ChainedCommitCohort(final ShardDataTreeTransactionChain chain, final ReadWriteShardDataTreeTransaction transaction, final ShardDataTreeCohort delegate) {
26         this.transaction = Preconditions.checkNotNull(transaction);
27         this.delegate = Preconditions.checkNotNull(delegate);
28         this.chain = Preconditions.checkNotNull(chain);
29     }
30
31     @Override
32     public ListenableFuture<Void> commit() {
33         final ListenableFuture<Void> ret = delegate.commit();
34
35         Futures.addCallback(ret, new FutureCallback<Void>() {
36             @Override
37             public void onSuccess(Void result) {
38                 chain.clearTransaction(transaction);
39                 LOG.debug("Committed transaction {}", transaction);
40             }
41
42             @Override
43             public void onFailure(Throwable t) {
44                 LOG.error("Transaction {} commit failed, cannot recover", transaction, t);
45             }
46         });
47
48         return ret;
49     }
50
51     @Override
52     public ListenableFuture<Boolean> canCommit() {
53         return delegate.canCommit();
54     }
55
56     @Override
57     public ListenableFuture<Void> preCommit() {
58         return delegate.preCommit();
59     }
60
61     @Override
62     public ListenableFuture<Void> abort() {
63         return delegate.abort();
64     }
65
66     @Override
67     DataTreeCandidateTip getCandidate() {
68         return delegate.getCandidate();
69     }
70
71     @Override
72     DataTreeModification getDataTreeModification() {
73         return delegate.getDataTreeModification();
74     }
75 }