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