CDS: Add stress test RPC to the cars model
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / 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.controller.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.controller.netconf.api.NetconfDocumentedException;
20 import org.opendaylight.controller.sal.connect.netconf.util.NetconfBaseOps;
21 import org.opendaylight.controller.sal.connect.netconf.util.NetconfRpcFutureCallback;
22 import org.opendaylight.controller.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
39  * </ul>
40  * <li/> Edit-config in running N times
41  * <ul>
42  * <li/> If any issue occurs during edit, datastore is unlocked and an exception is thrown
43  * </ul>
44  * <li/> Unlock running datastore on tx commit
45  * </ol>
46  */
47 public class WriteRunningTx extends AbstractWriteTx {
48
49     private static final Logger LOG  = LoggerFactory.getLogger(WriteRunningTx.class);
50
51     public WriteRunningTx(final RemoteDeviceId id, final NetconfBaseOps netOps,
52                           final boolean rollbackSupport, long requestTimeoutMillis) {
53         super(requestTimeoutMillis, netOps, id, rollbackSupport);
54     }
55
56     @Override
57     protected synchronized void init() {
58         lock();
59     }
60
61     private void lock() {
62         try {
63             invokeBlocking("Lock running", new Function<NetconfBaseOps, ListenableFuture<DOMRpcResult>>() {
64                 @Override
65                 public ListenableFuture<DOMRpcResult> apply(final NetconfBaseOps input) {
66                     return input.lockRunning(new NetconfRpcFutureCallback("Lock running", id));
67                 }
68             });
69         } catch (final NetconfDocumentedException e) {
70             LOG.warn("{}: Failed to initialize netconf transaction (lock running)", e);
71             finished = true;
72             throw new RuntimeException(id + ": Failed to initialize netconf transaction (lock running)", e);
73         }
74     }
75
76     @Override
77     protected void cleanup() {
78         unlock();
79     }
80
81     @Override
82     protected void handleEditException(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data, final NetconfDocumentedException e, final String editType) {
83         LOG.warn("{}: Error " + editType + " data to (running){}, data: {}, canceling", id, path, data, e);
84         cancel();
85         throw new RuntimeException(id + ": Error while " + editType + ": (running)" + path, e);
86     }
87
88     @Override
89     protected void handleDeleteException(final YangInstanceIdentifier path, final NetconfDocumentedException e) {
90         LOG.warn("{}: Error deleting data (running){}, canceling", id, path, e);
91         cancel();
92         throw new RuntimeException(id + ": Error while deleting (running)" + path, e);
93     }
94
95     @Override
96     public synchronized CheckedFuture<Void, TransactionCommitFailedException> submit() {
97         final ListenableFuture<Void> commmitFutureAsVoid = Futures.transform(commit(), new Function<RpcResult<TransactionStatus>, Void>() {
98             @Override
99             public Void apply(final RpcResult<TransactionStatus> input) {
100                 return null;
101             }
102         });
103
104         return Futures.makeChecked(commmitFutureAsVoid, new Function<Exception, TransactionCommitFailedException>() {
105             @Override
106             public TransactionCommitFailedException apply(final Exception input) {
107                 return new TransactionCommitFailedException("Submit of transaction " + getIdentifier() + " failed", input);
108             }
109         });
110     }
111
112     @Override
113     public synchronized ListenableFuture<RpcResult<TransactionStatus>> performCommit() {
114         unlock();
115         return Futures.immediateFuture(RpcResultBuilder.success(TransactionStatus.COMMITED).build());
116     }
117
118     @Override
119     protected void editConfig(final DataContainerChild<?, ?> editStructure, final Optional<ModifyAction> defaultOperation) throws NetconfDocumentedException {
120         invokeBlocking("Edit running", new Function<NetconfBaseOps, ListenableFuture<DOMRpcResult>>() {
121             @Override
122             public ListenableFuture<DOMRpcResult> apply(final NetconfBaseOps input) {
123                         return defaultOperation.isPresent()
124                                 ? input.editConfigRunning(new NetconfRpcFutureCallback("Edit running", id), editStructure, defaultOperation.get(),
125                                 rollbackSupport)
126                                 : input.editConfigRunning(new NetconfRpcFutureCallback("Edit running", id), editStructure,
127                                 rollbackSupport);
128             }
129         });
130     }
131
132     private void unlock() {
133         try {
134             invokeBlocking("Unlocking running", new Function<NetconfBaseOps, ListenableFuture<DOMRpcResult>>() {
135                 @Override
136                 public ListenableFuture<DOMRpcResult> apply(final NetconfBaseOps input) {
137                     return input.unlockRunning(new NetconfRpcFutureCallback("Unlock running", id));
138                 }
139             });
140         } catch (final NetconfDocumentedException e) {
141             LOG.warn("{}: Failed to unlock running datastore", e);
142             throw new RuntimeException(id + ": Failed to unlock running datastore", e);
143         }
144     }
145 }