Add Unit test for bug fix 5998 and 5997
[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.FutureCallback;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
20 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
21 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfRpcFutureCallback;
22 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
23 import org.opendaylight.yangtools.yang.common.RpcResult;
24 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * Tx implementation for netconf devices that support only writable-running with no candidate
33  * The sequence goes as:
34  * <ol>
35  *   <li>Lock running datastore on tx construction
36  *     <ul>
37  *       <li> Lock has to succeed, if it does not, transaction is failed</li>
38  *     </ul>
39  *   </li>
40  *   <li>Edit-config in running N times
41  *     <ul>
42  *       <li>If any issue occurs during edit, datastore is unlocked and an exception is thrown</li>
43  *     </ul>
44  *   </li>
45  *   <li>Unlock running datastore on tx commit</li>
46  * </ol>
47  */
48 //TODO replace custom RPCs future callbacks with NetconfRpcFutureCallback
49 public class WriteRunningTx extends AbstractWriteTx {
50
51     private static final Logger LOG  = LoggerFactory.getLogger(WriteRunningTx.class);
52
53     public WriteRunningTx(final RemoteDeviceId id, final NetconfBaseOps netOps,
54                           final boolean rollbackSupport) {
55         super(netOps, id, rollbackSupport);
56     }
57
58     @Override
59     protected synchronized void init() {
60         lock();
61     }
62
63     private void lock() {
64         final FutureCallback<DOMRpcResult> lockCallback = new FutureCallback<DOMRpcResult>() {
65             @Override
66             public void onSuccess(DOMRpcResult result) {
67                 if (isSuccess(result)) {
68                     if (LOG.isTraceEnabled()) {
69                         LOG.trace("Lock running succesfull");
70                     }
71                 } else {
72                     LOG.warn("{}: lock running invoked unsuccessfully: {}", id, result.getErrors());
73                 }
74             }
75
76             @Override
77             public void onFailure(Throwable t) {
78                 LOG.warn("{}: Lock running operation failed. {}", id, t);
79             }
80         };
81         resultsFutures.add(netOps.lockRunning(lockCallback));
82     }
83
84     @Override
85     protected void cleanup() {
86         unlock();
87     }
88
89     @Override
90     public synchronized CheckedFuture<Void, TransactionCommitFailedException> submit() {
91         final ListenableFuture<Void> commmitFutureAsVoid = Futures.transform(commit(), new Function<RpcResult<TransactionStatus>, Void>() {
92             @Override
93             public Void apply(final RpcResult<TransactionStatus> input) {
94                 return null;
95             }
96         });
97
98         return Futures.makeChecked(commmitFutureAsVoid, new Function<Exception, TransactionCommitFailedException>() {
99             @Override
100             public TransactionCommitFailedException apply(final Exception input) {
101                 return new TransactionCommitFailedException("Submit of transaction " + getIdentifier() + " failed", input);
102             }
103         });
104     }
105
106     @Override
107     public synchronized ListenableFuture<RpcResult<TransactionStatus>> performCommit() {
108         unlock();
109
110         return resultsToTxStatus();
111     }
112
113     @Override
114     protected void editConfig(final YangInstanceIdentifier path,
115                               final Optional<NormalizedNode<?, ?>> data,
116                               final DataContainerChild<?, ?> editStructure,
117                               final Optional<ModifyAction> defaultOperation,
118                               final String operation) {
119         FutureCallback<DOMRpcResult> editConfigCallback = new FutureCallback<DOMRpcResult>() {
120             @Override
121             public void onSuccess(DOMRpcResult result) {
122                 if (isSuccess(result)) {
123                     if (LOG.isTraceEnabled()) {
124                         LOG.trace("Edit running succesfull");
125                     }
126                 } else {
127                     LOG.warn("{}: Edit running invoked unsuccessfully: {}", id, result.getErrors());
128                 }
129             }
130
131             @Override
132             public void onFailure(Throwable t) {
133                 LOG.warn("{}: Error {} data to (running){}, data: {}", id, operation, path, data.orNull(), t);
134             }
135         };
136         if (defaultOperation.isPresent()) {
137             resultsFutures.add(
138                     netOps.editConfigRunning(editConfigCallback, editStructure, defaultOperation.get(), rollbackSupport));
139         } else {
140             resultsFutures.add(netOps.editConfigRunning(editConfigCallback, editStructure, rollbackSupport));
141         }
142     }
143
144     private void unlock() {
145         netOps.unlockRunning(new NetconfRpcFutureCallback("Unlock running", id));
146     }
147 }