bef72a256b7f0cba591feabb06c543e99d321210
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / tx / NetconfWriteOnlyTransaction.java
1 /*
2  * Copyright (c) 2016 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.singleton.impl.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.singleton.api.NetconfDOMTransaction;
24 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
25 import org.opendaylight.yangtools.yang.common.RpcResult;
26 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import scala.concurrent.Future;
30
31 public class NetconfWriteOnlyTransaction implements DOMDataWriteTransaction {
32
33     private final NetconfDOMTransaction delegate;
34     private final ActorSystem actorSystem;
35
36     public NetconfWriteOnlyTransaction(final ActorSystem actorSystem, final NetconfDOMTransaction delegate) {
37         this.delegate = delegate;
38         this.actorSystem = actorSystem;
39     }
40
41     @Override
42     public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path,
43                     final NormalizedNode<?,?> data) {
44         delegate.put(store, new NormalizedNodeMessage(path, data));
45     }
46
47     @Override
48     public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path,
49                       final NormalizedNode<?,?> data) {
50         delegate.merge(store, new NormalizedNodeMessage(path, data));
51     }
52
53     @Override
54     public boolean cancel() {
55         return delegate.cancel();
56     }
57
58     @Override
59     public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
60         delegate.delete(store, path);
61     }
62
63     @Override
64     public CheckedFuture<Void, TransactionCommitFailedException> submit() {
65         final Future<Void> submit = delegate.submit();
66         final SettableFuture<Void> settFuture = SettableFuture.create();
67         final CheckedFuture<Void, TransactionCommitFailedException> checkedFuture;
68         checkedFuture = Futures.makeChecked(settFuture, new Function<Exception, TransactionCommitFailedException>() {
69             @Nullable
70             @Override
71             public TransactionCommitFailedException apply(Exception input) {
72                 return new TransactionCommitFailedException("Transaction commit failed", input);
73             }
74         });
75         submit.onComplete(new OnComplete<Void>() {
76             @Override
77             public void onComplete(Throwable throwable, Void object) throws Throwable {
78                 if (throwable == null) {
79                     settFuture.set(object);
80                 } else {
81                     settFuture.setException(throwable);
82                 }
83             }
84         }, actorSystem.dispatcher());
85         return checkedFuture;
86     }
87
88     @Override
89     public ListenableFuture<RpcResult<TransactionStatus>> commit() {
90         final Future<Void> commit = delegate.submit();
91         final SettableFuture<RpcResult<TransactionStatus>> settFuture = SettableFuture.create();
92         commit.onComplete(new OnComplete<Void>() {
93             @Override
94             public void onComplete(final Throwable throwable, final Void result) throws Throwable {
95                 if (throwable == null) {
96                     TransactionStatus status = TransactionStatus.SUBMITED;
97                     RpcResult<TransactionStatus> rpcResult = RpcResultBuilder.success(status).build();
98                     settFuture.set(rpcResult);
99                 } else {
100                     settFuture.setException(throwable);
101                 }
102             }
103         }, actorSystem.dispatcher());
104         return settFuture;
105     }
106
107     @Override
108     public Object getIdentifier() {
109         return this;
110     }
111 }