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