Remove opendaylight directory
[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.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
17 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
19 import org.opendaylight.netconf.api.NetconfDocumentedException;
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.common.RpcResultBuilder;
25 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Tx implementation for netconf devices that support only writable-running with no candidate
34  * The sequence goes as:
35  * <ol>
36  *   <li>Lock running datastore on tx construction
37  *     <ul>
38  *       <li> Lock has to succeed, if it does not, transaction is failed</li>
39  *     </ul>
40  *   </li>
41  *   <li>Edit-config in running N times
42  *     <ul>
43  *       <li>If any issue occurs during edit, datastore is unlocked and an exception is thrown</li>
44  *     </ul>
45  *   </li>
46  *   <li>Unlock running datastore on tx commit</li>
47  * </ol>
48  */
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, long requestTimeoutMillis) {
55         super(requestTimeoutMillis, netOps, id, rollbackSupport);
56     }
57
58     @Override
59     protected synchronized void init() {
60         lock();
61     }
62
63     private void lock() {
64         final String operation = "Lock running";
65         try {
66             invokeBlocking(operation, new Function<NetconfBaseOps, ListenableFuture<DOMRpcResult>>() {
67                 @Override
68                 public ListenableFuture<DOMRpcResult> apply(final NetconfBaseOps input) {
69                     return perfomRequestWithTimeout(operation, input.lockRunning(new NetconfRpcFutureCallback(operation, id)));
70                 }
71             });
72         } catch (final NetconfDocumentedException e) {
73             LOG.warn("{}: Failed to initialize netconf transaction (lock running)", id, e);
74             finished = true;
75             throw new RuntimeException(id + ": Failed to initialize netconf transaction (lock running)", e);
76         }
77     }
78
79     @Override
80     protected void cleanup() {
81         unlock();
82     }
83
84     @Override
85     protected void handleEditException(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data, final NetconfDocumentedException e, final String editType) {
86         LOG.warn("{}: Error {} data to (running){}, data: {}, canceling", id, editType, path, data, e);
87         cancel();
88         throw new RuntimeException(id + ": Error while " + editType + ": (running)" + path, e);
89     }
90
91     @Override
92     protected void handleDeleteException(final YangInstanceIdentifier path, final NetconfDocumentedException e) {
93         LOG.warn("{}: Error deleting data (running){}, canceling", id, path, e);
94         cancel();
95         throw new RuntimeException(id + ": Error while deleting (running)" + path, e);
96     }
97
98     @Override
99     public synchronized CheckedFuture<Void, TransactionCommitFailedException> submit() {
100         final ListenableFuture<Void> commitFutureAsVoid = Futures.transform(commit(), new Function<RpcResult<TransactionStatus>, Void>() {
101             @Override
102             public Void apply(final RpcResult<TransactionStatus> input) {
103                 return null;
104             }
105         });
106
107         return Futures.makeChecked(commitFutureAsVoid, new Function<Exception, TransactionCommitFailedException>() {
108             @Override
109             public TransactionCommitFailedException apply(final Exception input) {
110                 return new TransactionCommitFailedException("Submit of transaction " + getIdentifier() + " failed", input);
111             }
112         });
113     }
114
115     @Override
116     public synchronized ListenableFuture<RpcResult<TransactionStatus>> performCommit() {
117         unlock();
118         return Futures.immediateFuture(RpcResultBuilder.success(TransactionStatus.COMMITED).build());
119     }
120
121     @Override
122     protected void editConfig(final DataContainerChild<?, ?> editStructure, final Optional<ModifyAction> defaultOperation) throws NetconfDocumentedException {
123         final String operation = "Edit running";
124         invokeBlocking(operation, new Function<NetconfBaseOps, ListenableFuture<DOMRpcResult>>() {
125             @Override
126             public ListenableFuture<DOMRpcResult> apply(final NetconfBaseOps input) {
127                         return perfomRequestWithTimeout(operation, defaultOperation.isPresent()
128                                 ? input.editConfigRunning(new NetconfRpcFutureCallback(operation, id), editStructure, defaultOperation.get(),
129                                 rollbackSupport)
130                                 : input.editConfigRunning(new NetconfRpcFutureCallback(operation, id), editStructure,
131                                 rollbackSupport));
132             }
133         });
134     }
135
136     private void unlock() {
137         final String operation = "Unlocking running";
138         try {
139             invokeBlocking(operation, new Function<NetconfBaseOps, ListenableFuture<DOMRpcResult>>() {
140                 @Override
141                 public ListenableFuture<DOMRpcResult> apply(final NetconfBaseOps input) {
142                     return perfomRequestWithTimeout(operation, input.unlockRunning(new NetconfRpcFutureCallback(operation, id)));
143                 }
144             });
145         } catch (final NetconfDocumentedException e) {
146             LOG.warn("{}: Failed to unlock running datastore", id, e);
147             throw new RuntimeException(id + ": Failed to unlock running datastore", e);
148         }
149     }
150 }