fad3c1cd1a9842334441f0ca636c9bad24bd1cb1
[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.FutureCallback;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.SettableFuture;
16 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
17 import org.opendaylight.mdsal.common.api.CommitInfo;
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<CommitInfo> {
25     private final DOMDataReadWriteTransaction delegate;
26     private final SettableFuture<CommitInfo> future;
27     private DOMDataReadWriteTransaction frontendTransaction;
28
29     PingPongTransaction(final DOMDataReadWriteTransaction delegate) {
30         this.delegate = Preconditions.checkNotNull(delegate);
31         future = SettableFuture.create();
32     }
33
34     DOMDataReadWriteTransaction getTransaction() {
35         return delegate;
36     }
37
38     DOMDataReadWriteTransaction getFrontendTransaction() {
39         return frontendTransaction;
40     }
41
42     ListenableFuture<CommitInfo> getSubmitFuture() {
43         return future;
44     }
45
46     @Override
47     public void onSuccess(final CommitInfo result) {
48         future.set(result);
49     }
50
51     @Override
52     public void onFailure(final Throwable throwable) {
53         future.setException(throwable);
54     }
55
56     void recordFrontendTransaction(final DOMDataReadWriteTransaction tx) {
57         if (frontendTransaction != null) {
58             frontendTransaction = tx;
59         }
60     }
61
62     @Override
63     public String toString() {
64         return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
65     }
66
67     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
68         return toStringHelper.add("delegate", delegate);
69     }
70 }