Bug 4940 - correctly implement default-request-timeout-millis
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / 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.controller.sal.connect.netconf.sal.tx;
10
11
12 import com.google.common.base.Function;
13 import com.google.common.util.concurrent.ListenableFuture;
14
15 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
16 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
17 import org.opendaylight.controller.sal.connect.netconf.util.NetconfBaseOps;
18 import org.opendaylight.controller.sal.connect.netconf.util.NetconfRpcFutureCallback;
19 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Tx implementation for netconf devices that support only candidate datastore and writable running
25  * The sequence goes exactly as with only candidate supported, with one addition:
26  * <ul>
27  *     <li>Running datastore is locked as the first thing and this lock has to succeed</li>
28  * </ul>
29  */
30 public class WriteCandidateRunningTx extends WriteCandidateTx {
31
32     private static final Logger LOG  = LoggerFactory.getLogger(WriteCandidateRunningTx.class);
33
34     public WriteCandidateRunningTx(final RemoteDeviceId id, final NetconfBaseOps netOps, final boolean rollbackSupport, final long requestTimeoutMillis) {
35         super(id, netOps, rollbackSupport, requestTimeoutMillis);
36     }
37
38     @Override
39     protected synchronized void init() {
40         lockRunning();
41         super.init();
42     }
43
44     @Override
45     protected void cleanupOnSuccess() {
46         super.cleanupOnSuccess();
47         unlockRunning();
48     }
49
50     private void lockRunning() {
51         final String operation = "Lock Running";
52         try {
53             invokeBlocking(operation, new Function<NetconfBaseOps, ListenableFuture<DOMRpcResult>>() {
54                 @Override
55                 public ListenableFuture<DOMRpcResult> apply(final NetconfBaseOps input) {
56                     return perfomRequestWithTimeout(operation, input.lockRunning(new NetconfRpcFutureCallback(operation, id)));
57
58                 }
59             });
60         } catch (final NetconfDocumentedException e) {
61             LOG.warn("{}: Failed to lock running. Failed to initialize transaction", e);
62             finished = true;
63             throw new RuntimeException(id + ": Failed to lock running. Failed to initialize transaction", e);
64         }
65     }
66
67     /**
68      * 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
69      */
70     private void unlockRunning() {
71         netOps.unlockRunning(new NetconfRpcFutureCallback("Unlock running", id));
72     }
73 }