ff55c01a25be1fc824daaf563018e9b9e10abfd2
[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 import org.opendaylight.controller.sal.core.spi.data.ForwardingDOMStoreThreePhaseCommitCohort;
16 import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction;
17
18 final class ChainedTransactionCommitImpl extends ForwardingDOMStoreThreePhaseCommitCohort {
19     private final SnapshotBackedWriteTransaction<String> transaction;
20     private final DOMStoreThreePhaseCommitCohort delegate;
21     private final DOMStoreTransactionChainImpl txChain;
22
23     ChainedTransactionCommitImpl(final SnapshotBackedWriteTransaction<String> transaction,
24             final DOMStoreThreePhaseCommitCohort delegate, final DOMStoreTransactionChainImpl txChain) {
25         this.transaction = Preconditions.checkNotNull(transaction);
26         this.delegate = Preconditions.checkNotNull(delegate);
27         this.txChain = Preconditions.checkNotNull(txChain);
28     }
29
30     @Override
31     protected DOMStoreThreePhaseCommitCohort delegate() {
32         return delegate;
33     }
34
35     @Override
36     public ListenableFuture<Void> commit() {
37         ListenableFuture<Void> commitFuture = super.commit();
38         Futures.addCallback(commitFuture, new FutureCallback<Void>() {
39             @Override
40             public void onFailure(final Throwable t) {
41                 txChain.transactionFailed(transaction, t);
42             }
43
44             @Override
45             public void onSuccess(final Void result) {
46                 txChain.transactionCommited(transaction);
47             }
48         });
49         return commitFuture;
50     }
51
52 }