Bug 6714 - Use singleton service in clustered netconf topology
[netconf.git] / netconf / netconf-topology / src / main / java / org / opendaylight / netconf / topology / pipeline / tx / ProxyWriteOnlyTransaction.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.netconf.topology.pipeline.tx;
10
11 import akka.actor.ActorSystem;
12 import akka.dispatch.OnComplete;
13 import com.google.common.base.Function;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import com.google.common.util.concurrent.SettableFuture;
18 import javax.annotation.Nullable;
19 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
23 import org.opendaylight.netconf.topology.pipeline.ProxyNetconfDeviceDataBroker;
24 import org.opendaylight.netconf.topology.util.messages.NormalizedNodeMessage;
25 import org.opendaylight.yangtools.yang.common.RpcResult;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import scala.concurrent.Future;
29
30 public class ProxyWriteOnlyTransaction implements DOMDataWriteTransaction {
31
32     private final ProxyNetconfDeviceDataBroker delegate;
33     private final ActorSystem actorSystem;
34
35     public ProxyWriteOnlyTransaction(ActorSystem actorSystem, final ProxyNetconfDeviceDataBroker delegate) {
36         this.delegate = delegate;
37         this.actorSystem = actorSystem;
38     }
39
40     @Override
41     public void put (final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode < ?,?>data){
42         delegate.put(store, new NormalizedNodeMessage(path, data));
43     }
44
45     @Override
46     public void merge (final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode < ?,?>data){
47         delegate.merge(store, new NormalizedNodeMessage(path, data));
48     }
49
50     @Override
51     public boolean cancel () {
52         return delegate.cancel();
53     }
54
55     @Override
56     public void delete (final LogicalDatastoreType store, final YangInstanceIdentifier path){
57         delegate.delete(store, path);
58     }
59
60     @Override
61     public CheckedFuture<Void, TransactionCommitFailedException> submit() {
62         final Future<Void> submit = delegate.submit();
63         final SettableFuture<Void> settableFuture = SettableFuture.create();
64         final CheckedFuture<Void, TransactionCommitFailedException> checkedFuture = Futures.makeChecked(settableFuture, new Function<Exception, TransactionCommitFailedException>() {
65             @Nullable
66             @Override
67             public TransactionCommitFailedException apply(Exception input) {
68                 return new TransactionCommitFailedException("Transaction commit failed", input);
69             }
70         });
71         submit.onComplete(new OnComplete<Void>() {
72             @Override
73             public void onComplete(Throwable throwable, Void aVoid) throws Throwable {
74                 if (throwable == null) {
75                     settableFuture.set(aVoid);
76                 } else {
77                     settableFuture.setException(throwable);
78                 }
79             }
80         }, actorSystem.dispatcher());
81         return checkedFuture;
82     }
83
84     @Override
85     public ListenableFuture<RpcResult<TransactionStatus>> commit () {
86         final Future<RpcResult<TransactionStatus>> commit = delegate.commit();
87         final SettableFuture<RpcResult<TransactionStatus>> settableFuture = SettableFuture.create();
88         commit.onComplete(new OnComplete<RpcResult<TransactionStatus>>() {
89             @Override
90             public void onComplete(Throwable throwable, RpcResult<TransactionStatus> transactionStatusRpcResult) throws Throwable {
91                 if (throwable == null) {
92                     settableFuture.set(transactionStatusRpcResult);
93                 } else {
94                     settableFuture.setException(throwable);
95                 }
96             }
97         }, actorSystem.dispatcher());
98         return settableFuture;
99     }
100
101     @Override
102     public Object getIdentifier () {
103         return this;
104     }
105 }