Remove deprecated AsyncWriteTransaction#commit method
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / sal / tx / WriteRunningTx.java
1 /*
2  * Copyright (c) 2014 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.sal.connect.netconf.sal.tx;
10
11 import com.google.common.base.Function;
12 import com.google.common.base.Optional;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import java.util.ArrayList;
18 import java.util.List;
19 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
21 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
22 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfRpcFutureCallback;
23 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
24 import org.opendaylight.yangtools.yang.common.RpcResult;
25 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Tx implementation for netconf devices that support only writable-running with no candidate.
34  * The sequence goes as:
35  * <ol>
36  *   <li>Lock running datastore on tx construction
37  *     <ul>
38  *       <li> Lock has to succeed, if it does not, transaction is failed</li>
39  *     </ul>
40  *   </li>
41  *   <li>Edit-config in running N times
42  *     <ul>
43  *       <li>If any issue occurs during edit, datastore is unlocked and an exception is thrown</li>
44  *     </ul>
45  *   </li>
46  *   <li>Unlock running datastore on tx commit</li>
47  * </ol>
48  */
49 public class WriteRunningTx extends AbstractWriteTx {
50
51     private static final Logger LOG  = LoggerFactory.getLogger(WriteRunningTx.class);
52     private final List<Change> changes = new ArrayList<>();
53
54     public WriteRunningTx(final RemoteDeviceId id, final NetconfBaseOps netOps,
55                           final boolean rollbackSupport) {
56         super(netOps, id, rollbackSupport);
57     }
58
59     @Override
60     protected synchronized void init() {
61         lock();
62     }
63
64     private void lock() {
65         resultsFutures.add(netOps.lockRunning(new NetconfRpcFutureCallback("Lock running", id)));
66     }
67
68     @Override
69     protected void cleanup() {
70         unlock();
71     }
72
73     @Override
74     public synchronized CheckedFuture<Void, TransactionCommitFailedException> submit() {
75         final ListenableFuture<Void> commmitFutureAsVoid = Futures.transform(commitConfiguration(),
76                 (Function<RpcResult<Void>, Void>) input -> null, MoreExecutors.directExecutor());
77
78         return Futures.makeChecked(commmitFutureAsVoid,
79             input -> new TransactionCommitFailedException("Submit of transaction " + getIdentifier() + " failed",
80                 input));
81     }
82
83     @Override
84     public synchronized ListenableFuture<RpcResult<Void>> performCommit() {
85         for (final Change change : changes) {
86             resultsFutures.add(change.execute(id, netOps, rollbackSupport));
87         }
88         unlock();
89         return resultsToTxStatus();
90     }
91
92     @Override
93     protected void editConfig(final YangInstanceIdentifier path,
94                               final Optional<NormalizedNode<?, ?>> data,
95                               final DataContainerChild<?, ?> editStructure,
96                               final Optional<ModifyAction> defaultOperation,
97                               final String operation) {
98         changes.add(new Change(editStructure, defaultOperation));
99     }
100
101     private void unlock() {
102         netOps.unlockRunning(new NetconfRpcFutureCallback("Unlock running", id));
103     }
104
105     private static final class Change {
106
107         private final DataContainerChild<?, ?> editStructure;
108         private final Optional<ModifyAction> defaultOperation;
109
110         Change(final DataContainerChild<?, ?> editStructure, final Optional<ModifyAction> defaultOperation) {
111             this.editStructure = editStructure;
112             this.defaultOperation = defaultOperation;
113         }
114
115         private ListenableFuture<DOMRpcResult> execute(final RemoteDeviceId id, final NetconfBaseOps netOps,
116                                                        final boolean rollbackSupport) {
117             final NetconfRpcFutureCallback editConfigCallback = new NetconfRpcFutureCallback("Edit running", id);
118             if (defaultOperation.isPresent()) {
119                 return netOps.editConfigRunning(editConfigCallback, editStructure, defaultOperation.get(),
120                     rollbackSupport);
121             } else {
122                 return netOps.editConfigRunning(editConfigCallback, editStructure, rollbackSupport);
123             }
124         }
125     }
126 }