Fix checkstyle issues in module sal-dom-broker
[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.ListenableFuture;
16 import com.google.common.util.concurrent.SettableFuture;
17 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
19 import org.opendaylight.controller.md.sal.common.impl.service.AbstractDataTransaction;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
21 import org.opendaylight.yangtools.yang.common.RpcResult;
22
23 /**
24  * Transaction context. Tracks the relationship with the backend transaction.
25  * We never leak this class to the user and have it implement the {@link FutureCallback}
26  * interface so we have a simple way of propagating the result.
27  */
28 final class PingPongTransaction implements FutureCallback<Void> {
29     private final CheckedFuture<Void, TransactionCommitFailedException> submitFuture;
30     private final ListenableFuture<RpcResult<TransactionStatus>> commitFuture;
31     private final DOMDataReadWriteTransaction delegate;
32     private final SettableFuture<Void> future;
33     private DOMDataReadWriteTransaction frontendTransaction;
34
35     PingPongTransaction(final DOMDataReadWriteTransaction delegate) {
36         this.delegate = Preconditions.checkNotNull(delegate);
37         future = SettableFuture.create();
38         submitFuture = new PingPongFuture(future);
39         commitFuture = AbstractDataTransaction.convertToLegacyCommitFuture(submitFuture);
40     }
41
42     DOMDataReadWriteTransaction getTransaction() {
43         return delegate;
44     }
45
46     DOMDataReadWriteTransaction getFrontendTransaction() {
47         return frontendTransaction;
48     }
49
50     CheckedFuture<Void, TransactionCommitFailedException> getSubmitFuture() {
51         return submitFuture;
52     }
53
54     ListenableFuture<RpcResult<TransactionStatus>> getCommitFuture() {
55         return commitFuture;
56     }
57
58     @Override
59     public void onSuccess(final Void result) {
60         future.set(result);
61     }
62
63     @Override
64     public void onFailure(final Throwable throwable) {
65         future.setException(throwable);
66     }
67
68     void recordFrontendTransaction(final DOMDataReadWriteTransaction tx) {
69         if (frontendTransaction != null) {
70             frontendTransaction = tx;
71         }
72     }
73
74     @Override
75     public String toString() {
76         return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
77     }
78
79     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
80         return toStringHelper.add("delegate", delegate);
81     }
82 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.