Wrap throwable in NetconfDocumentedException for AbstractWriteTx
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / sal / tx / WriteCandidateRunningTx.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.util.concurrent.FutureCallback;
12 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
13 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
14 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfRpcFutureCallback;
15 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * Tx implementation for netconf devices that support only candidate datastore and writable running
21  * The sequence goes exactly as with only candidate supported, with one addition:
22  * <ul>
23  *     <li>Running datastore is locked as the first thing and this lock has to succeed</li>
24  * </ul>
25  */
26 //TODO replace custom RPCs future callbacks with NetconfRpcFutureCallback
27 public class WriteCandidateRunningTx extends WriteCandidateTx {
28
29     private static final Logger LOG  = LoggerFactory.getLogger(WriteCandidateRunningTx.class);
30
31     public WriteCandidateRunningTx(final RemoteDeviceId id, final NetconfBaseOps netOps, final boolean rollbackSupport) {
32         super(id, netOps, rollbackSupport);
33     }
34
35     @Override
36     protected synchronized void init() {
37         lockRunning();
38         super.init();
39     }
40
41     @Override
42     protected void cleanupOnSuccess() {
43         super.cleanupOnSuccess();
44         unlockRunning();
45     }
46
47     private void lockRunning() {
48         final FutureCallback<DOMRpcResult> lockRunningCallback = new FutureCallback<DOMRpcResult>() {
49             @Override
50             public void onSuccess(DOMRpcResult result) {
51                 if (isSuccess(result)) {
52                     if (LOG.isTraceEnabled()) {
53                         LOG.trace("Lock running succesfull");
54                     }
55                 } else {
56                     LOG.warn("{}: lock running invoked unsuccessfully: {}", id, result.getErrors());
57                 }
58             }
59
60             @Override
61             public void onFailure(Throwable t) {
62                 LOG.warn("{}: Failed to lock running. Failed to initialize transaction", id, t);
63             }
64         };
65         resultsFutures.add(netOps.lockRunning(lockRunningCallback));
66     }
67
68     /**
69      * This has to be non blocking since it is called from a callback on commit and its netty threadpool that is really sensitive to blocking calls
70      */
71     private void unlockRunning() {
72         netOps.unlockRunning(new NetconfRpcFutureCallback("Unlock running", id));
73     }
74 }