a693e8c59c3db5a07fc0538a675e72f550582b86
[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 public class WriteCandidateRunningTx extends WriteCandidateTx {
27
28     private static final Logger LOG  = LoggerFactory.getLogger(WriteCandidateRunningTx.class);
29
30     public WriteCandidateRunningTx(final RemoteDeviceId id, final NetconfBaseOps netOps, final boolean rollbackSupport) {
31         super(id, netOps, rollbackSupport);
32     }
33
34     @Override
35     protected synchronized void init() {
36         lockRunning();
37         super.init();
38     }
39
40     @Override
41     protected void cleanupOnSuccess() {
42         super.cleanupOnSuccess();
43         unlockRunning();
44     }
45
46     private void lockRunning() {
47         final FutureCallback<DOMRpcResult> lockRunningCallback = new FutureCallback<DOMRpcResult>() {
48             @Override
49             public void onSuccess(DOMRpcResult result) {
50                 if (isSuccess(result)) {
51                     if (LOG.isTraceEnabled()) {
52                         LOG.trace("Lock running succesfull");
53                     }
54                 } else {
55                     LOG.warn("{}: lock running invoked unsuccessfully: {}", id, result.getErrors());
56                 }
57             }
58
59             @Override
60             public void onFailure(Throwable t) {
61                 LOG.warn("{}: Failed to lock running. Failed to initialize transaction", id, t);
62                 throw new RuntimeException(id + ": Failed to lock running. Failed to initialize transaction", t);
63             }
64         };
65         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 }