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 / NetconfMasterDOMTransaction.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 com.google.common.base.Optional;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
23 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
24 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceDataBroker;
25 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
26 import org.opendaylight.netconf.topology.singleton.api.NetconfDOMTransaction;
27 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import scala.concurrent.Future;
34 import scala.concurrent.impl.Promise.DefaultPromise;
35
36 public class NetconfMasterDOMTransaction implements NetconfDOMTransaction {
37
38     private static final Logger LOG = LoggerFactory.getLogger(NetconfMasterDOMTransaction.class);
39
40     private final RemoteDeviceId id;
41     private final DOMDataBroker delegateBroker;
42
43     private DOMDataReadOnlyTransaction readTx;
44     private DOMDataWriteTransaction writeTx;
45
46     public NetconfMasterDOMTransaction(final RemoteDeviceId id,
47                                        final SchemaContext schemaContext,
48                                        final DOMRpcService rpc,
49                                        final NetconfSessionPreferences netconfSessionPreferences) {
50         this(id, new NetconfDeviceDataBroker(id, schemaContext, rpc, netconfSessionPreferences));
51     }
52
53     public NetconfMasterDOMTransaction(final RemoteDeviceId id, final DOMDataBroker delegateBroker) {
54         this.id = id;
55         this.delegateBroker = delegateBroker;
56
57         // only ever need 1 readTx since it doesnt need to be closed
58         readTx = delegateBroker.newReadOnlyTransaction();
59     }
60
61     @Override
62     public Future<Optional<NormalizedNodeMessage>> read(final LogicalDatastoreType store,
63                                                         final YangInstanceIdentifier path) {
64         LOG.trace("{}: Read[{}] {} via NETCONF: {}", id, readTx.getIdentifier(), store, path);
65
66         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readFuture = readTx.read(store, path);
67
68         final DefaultPromise<Optional<NormalizedNodeMessage>> promise = new DefaultPromise<>();
69         Futures.addCallback(readFuture, new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
70             @Override
71             public void onSuccess(final Optional<NormalizedNode<?, ?>> result) {
72                 if (!result.isPresent()) {
73                     promise.success(Optional.absent());
74                 } else {
75                     promise.success(Optional.of(new NormalizedNodeMessage(path, result.get())));
76                 }
77             }
78
79             @Override
80             public void onFailure(@Nonnull final Throwable throwable) {
81                 promise.failure(throwable);
82             }
83         });
84         return promise.future();
85     }
86
87     @Override
88     public Future<Boolean> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
89         LOG.trace("{}: Exists[{}] {} via NETCONF: {}", id, readTx.getIdentifier(), store, path);
90
91         final CheckedFuture<Boolean, ReadFailedException> existsFuture = readTx.exists(store, path);
92
93         final DefaultPromise<Boolean> promise = new DefaultPromise<>();
94         Futures.addCallback(existsFuture, new FutureCallback<Boolean>() {
95             @Override
96             public void onSuccess(final Boolean result) {
97                 promise.success(result);
98             }
99
100             @Override
101             public void onFailure(@Nonnull final Throwable throwable) {
102                 promise.failure(throwable);
103             }
104         });
105         return promise.future();
106     }
107
108     @Override
109     public void put(final LogicalDatastoreType store, final NormalizedNodeMessage data) {
110         if (writeTx == null) {
111             writeTx = delegateBroker.newWriteOnlyTransaction();
112         }
113
114         LOG.trace("{}: Write[{}] {} via NETCONF: {} with payload {}", id, writeTx.getIdentifier(), store,
115                 data.getIdentifier(), data.getNode());
116
117         writeTx.put(store, data.getIdentifier(), data.getNode());
118     }
119
120     @Override
121     public void merge(final LogicalDatastoreType store, final NormalizedNodeMessage data) {
122         if (writeTx == null) {
123             writeTx = delegateBroker.newWriteOnlyTransaction();
124         }
125
126         LOG.trace("{}: Merge[{}] {} via NETCONF: {} with payload {}", id, writeTx.getIdentifier(),store,
127                 data.getIdentifier(), data.getNode());
128
129         writeTx.merge(store, data.getIdentifier(), data.getNode());
130     }
131
132     @Override
133     public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
134         if (writeTx == null) {
135             writeTx = delegateBroker.newWriteOnlyTransaction();
136         }
137
138         LOG.trace("{}: Delete[{}} {} via NETCONF: {}", id, writeTx.getIdentifier(), store, path);
139
140         writeTx.delete(store, path);
141     }
142
143     @Override
144     public boolean cancel() {
145         LOG.trace("{}: Cancel[{}} via NETCONF", id, writeTx.getIdentifier());
146
147         return writeTx.cancel();
148     }
149
150     @Override
151     public Future<Void> submit() {
152         LOG.trace("{}: Submit[{}} via NETCONF", id, writeTx.getIdentifier());
153
154         final CheckedFuture<Void, TransactionCommitFailedException> submitFuture = writeTx.submit();
155         final DefaultPromise<Void> promise = new DefaultPromise<>();
156         Futures.addCallback(submitFuture, new FutureCallback<Void>() {
157             @Override
158             public void onSuccess(final Void result) {
159                 promise.success(result);
160                 writeTx = null;
161             }
162
163             @Override
164             public void onFailure(@Nonnull final Throwable throwable) {
165                 promise.failure(throwable);
166                 writeTx = null;
167             }
168         });
169         return promise.future();
170     }
171
172 }