Add default implementation for AsyncWriteTransaction#commit
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / PingPongTransaction.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.broker.impl;
9
10 import com.google.common.base.MoreObjects;
11 import com.google.common.base.MoreObjects.ToStringHelper;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.SettableFuture;
16 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
17 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
18
19 /**
20  * Transaction context. Tracks the relationship with the backend transaction.
21  * We never leak this class to the user and have it implement the {@link FutureCallback}
22  * interface so we have a simple way of propagating the result.
23  */
24 final class PingPongTransaction implements FutureCallback<Void> {
25     private final CheckedFuture<Void, TransactionCommitFailedException> submitFuture;
26     private final DOMDataReadWriteTransaction delegate;
27     private final SettableFuture<Void> future;
28     private DOMDataReadWriteTransaction frontendTransaction;
29
30     PingPongTransaction(final DOMDataReadWriteTransaction delegate) {
31         this.delegate = Preconditions.checkNotNull(delegate);
32         future = SettableFuture.create();
33         submitFuture = new PingPongFuture(future);
34     }
35
36     DOMDataReadWriteTransaction getTransaction() {
37         return delegate;
38     }
39
40     DOMDataReadWriteTransaction getFrontendTransaction() {
41         return frontendTransaction;
42     }
43
44     CheckedFuture<Void, TransactionCommitFailedException> getSubmitFuture() {
45         return submitFuture;
46     }
47
48     @Override
49     public void onSuccess(final Void result) {
50         future.set(result);
51     }
52
53     @Override
54     public void onFailure(final Throwable throwable) {
55         future.setException(throwable);
56     }
57
58     void recordFrontendTransaction(final DOMDataReadWriteTransaction tx) {
59         if (frontendTransaction != null) {
60             frontendTransaction = tx;
61         }
62     }
63
64     @Override
65     public String toString() {
66         return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
67     }
68
69     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
70         return toStringHelper.add("delegate", delegate);
71     }
72 }