Add logging in tx facade along with the RemoteDeviceId
[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.sal.connect.util.RemoteDeviceId;
24 import org.opendaylight.netconf.topology.singleton.api.NetconfDOMTransaction;
25 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
26 import org.opendaylight.yangtools.yang.common.RpcResult;
27 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import scala.concurrent.Future;
33
34 public class NetconfWriteOnlyTransaction implements DOMDataWriteTransaction {
35
36     private static final Logger LOG = LoggerFactory.getLogger(NetconfWriteOnlyTransaction.class);
37
38     private final RemoteDeviceId id;
39     private final NetconfDOMTransaction delegate;
40     private final ActorSystem actorSystem;
41
42     public NetconfWriteOnlyTransaction(final RemoteDeviceId id,
43                                        final ActorSystem actorSystem,
44                                        final NetconfDOMTransaction delegate) {
45         this.id = id;
46         this.delegate = delegate;
47         this.actorSystem = actorSystem;
48     }
49
50     @Override
51     public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path,
52                     final NormalizedNode<?,?> data) {
53         LOG.trace("{}: Write {} via NETCONF: {} with payload {}", id, store, path, data);
54
55         delegate.put(store, new NormalizedNodeMessage(path, data));
56     }
57
58     @Override
59     public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path,
60                       final NormalizedNode<?,?> data) {
61         LOG.trace("{}: Merge {} via NETCONF: {} with payload {}", id, store, path, data);
62
63         delegate.merge(store, new NormalizedNodeMessage(path, data));
64     }
65
66     @Override
67     public boolean cancel() {
68         LOG.trace("{}: Cancel", id);
69
70         return delegate.cancel();
71     }
72
73     @Override
74     public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
75         LOG.trace("{}: Delete {} via NETCONF: {}", id, store, path);
76
77         delegate.delete(store, path);
78     }
79
80     @Override
81     public CheckedFuture<Void, TransactionCommitFailedException> submit() {
82         LOG.trace("{}: Submit", id);
83
84         final Future<Void> submit = delegate.submit();
85         final SettableFuture<Void> settFuture = SettableFuture.create();
86         final CheckedFuture<Void, TransactionCommitFailedException> checkedFuture;
87         checkedFuture = Futures.makeChecked(settFuture, new Function<Exception, TransactionCommitFailedException>() {
88             @Nullable
89             @Override
90             public TransactionCommitFailedException apply(Exception input) {
91                 return new TransactionCommitFailedException("Transaction commit failed", input);
92             }
93         });
94         submit.onComplete(new OnComplete<Void>() {
95             @Override
96             public void onComplete(Throwable throwable, Void object) throws Throwable {
97                 if (throwable == null) {
98                     settFuture.set(object);
99                 } else {
100                     settFuture.setException(throwable);
101                 }
102             }
103         }, actorSystem.dispatcher());
104         return checkedFuture;
105     }
106
107     @Override
108     public ListenableFuture<RpcResult<TransactionStatus>> commit() {
109         LOG.trace("{}: Commit", id);
110
111         final Future<Void> commit = delegate.submit();
112         final SettableFuture<RpcResult<TransactionStatus>> settFuture = SettableFuture.create();
113         commit.onComplete(new OnComplete<Void>() {
114             @Override
115             public void onComplete(final Throwable throwable, final Void result) throws Throwable {
116                 if (throwable == null) {
117                     TransactionStatus status = TransactionStatus.SUBMITED;
118                     RpcResult<TransactionStatus> rpcResult = RpcResultBuilder.success(status).build();
119                     settFuture.set(rpcResult);
120                 } else {
121                     settFuture.setException(throwable);
122                 }
123             }
124         }, actorSystem.dispatcher());
125         return settFuture;
126     }
127
128     @Override
129     public Object getIdentifier() {
130         return this;
131     }
132 }