Merge "Cleanup: Remove passing around of DataPersistenceProvider"
[controller.git] / opendaylight / netconf / netconf-testtool / src / main / java / org / opendaylight / controller / netconf / test / tool / client / stress / SyncExecutionStrategy.java
1 /*
2  * Copyright (c) 2015 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.netconf.test.tool.client.stress;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Lists;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.List;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.TimeoutException;
18 import java.util.concurrent.atomic.AtomicInteger;
19 import org.opendaylight.controller.netconf.api.NetconfMessage;
20 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
21 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfDeviceCommunicator;
22 import org.opendaylight.yangtools.yang.common.RpcResult;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 // TODO reuse code from org.opendaylight.controller.netconf.test.tool.client.stress.AsyncExecutionStrategy
27 class SyncExecutionStrategy implements ExecutionStrategy {
28     private static final Logger LOG = LoggerFactory.getLogger(SyncExecutionStrategy.class);
29
30     private final Parameters params;
31     private final List<NetconfMessage> preparedMessages;
32     private final NetconfDeviceCommunicator sessionListener;
33     private final List<Integer> editBatches;
34
35     public SyncExecutionStrategy(final Parameters params, final List<NetconfMessage> preparedMessages, final NetconfDeviceCommunicator sessionListener) {
36         this.params = params;
37         this.preparedMessages = preparedMessages;
38         this.sessionListener = sessionListener;
39         editBatches = countEditBatchSizes(params);
40     }
41
42     private static List<Integer> countEditBatchSizes(final Parameters params) {
43         final List<Integer> editBatches = Lists.newArrayList();
44         if (params.editBatchSize != params.editCount) {
45             final int fullBatches = params.editCount / params.editBatchSize;
46             for (int i = 0; i < fullBatches; i++) {
47                 editBatches.add(params.editBatchSize);
48             }
49
50             if (params.editCount % params.editBatchSize != 0) {
51                 editBatches.add(params.editCount % params.editBatchSize);
52             }
53         } else {
54             editBatches.add(params.editBatchSize);
55         }
56         return editBatches;
57     }
58
59     public void invoke() {
60         final AtomicInteger responseCounter = new AtomicInteger(0);
61
62         int batchI = 0;
63         for (final Integer editBatch : editBatches) {
64             for (int i = 0; i < editBatch; i++) {
65                 final int msgId = i + (batchI * params.editBatchSize);
66                 final NetconfMessage msg = preparedMessages.get(msgId);
67                 LOG.debug("Sending message {}", msgId);
68                 if(LOG.isDebugEnabled()) {
69                     LOG.debug("Sending message {}", XmlUtil.toString(msg.getDocument()));
70                 }
71                 final ListenableFuture<RpcResult<NetconfMessage>> netconfMessageFuture =
72                         sessionListener.sendRequest(msg, StressClient.EDIT_QNAME);
73                 // Wait for response
74                 waitForResponse(responseCounter, netconfMessageFuture);
75
76             }
77             batchI++;
78             LOG.info("Batch {} with size {} sent. Committing", batchI, editBatch);
79
80             // Commit batch sync
81             waitForResponse(responseCounter,
82                     sessionListener.sendRequest(StressClient.COMMIT_MSG, StressClient.COMMIT_QNAME));
83         }
84
85         Preconditions.checkState(responseCounter.get() == params.editCount + editBatches.size(), "Not all responses were received, only %s from %s", responseCounter.get(), params.editCount + editBatches.size());
86     }
87
88     private void waitForResponse(AtomicInteger responseCounter, final ListenableFuture<RpcResult<NetconfMessage>> netconfMessageFuture) {
89         try {
90             final RpcResult<NetconfMessage> netconfMessageRpcResult =
91                     netconfMessageFuture.get(params.msgTimeout, TimeUnit.SECONDS);
92             if (netconfMessageRpcResult.isSuccessful()) {
93                 responseCounter.incrementAndGet();
94                 LOG.debug("Received response {}", responseCounter.get());
95             } else {
96                 LOG.warn("Request failed {}", netconfMessageRpcResult);
97             }
98
99         } catch (final InterruptedException e) {
100             throw new RuntimeException(e);
101         } catch (final ExecutionException | TimeoutException e) {
102             throw new RuntimeException("Request not finished", e);
103         }
104     }
105 }