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