Merge "Add missing copyright text"
[controller.git] / opendaylight / netconf / netconf-testtool / src / main / java / org / opendaylight / controller / netconf / test / tool / client / stress / AsyncExecutionStrategy.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 class AsyncExecutionStrategy implements ExecutionStrategy {
27     private static final Logger LOG = LoggerFactory.getLogger(AsyncExecutionStrategy.class);
28
29     private final Parameters params;
30     private final List<NetconfMessage> preparedMessages;
31     private final NetconfDeviceCommunicator sessionListener;
32     private final List<Integer> editBatches;
33     private final int editAmount;
34
35     public AsyncExecutionStrategy(final Parameters params, final List<NetconfMessage> editConfigMsgs, final NetconfDeviceCommunicator sessionListener) {
36         this.params = params;
37         this.preparedMessages = editConfigMsgs;
38         this.sessionListener = sessionListener;
39         this.editBatches = countEditBatchSizes(params, editConfigMsgs.size());
40         editAmount = editConfigMsgs.size();
41     }
42
43     private static List<Integer> countEditBatchSizes(final Parameters params, final int amount) {
44         final List<Integer> editBatches = Lists.newArrayList();
45         if (params.editBatchSize != amount) {
46             final int fullBatches = amount / params.editBatchSize;
47             for (int i = 0; i < fullBatches; i++) {
48                 editBatches.add(params.editBatchSize);
49             }
50
51             if (amount % params.editBatchSize != 0) {
52                 editBatches.add(amount % params.editBatchSize);
53             }
54         } else {
55             editBatches.add(params.editBatchSize);
56         }
57         return editBatches;
58     }
59
60     @Override
61     public void invoke() {
62         final AtomicInteger responseCounter = new AtomicInteger(0);
63         final List<ListenableFuture<RpcResult<NetconfMessage>>> futures = Lists.newArrayList();
64
65         int batchI = 0;
66         for (final Integer editBatch : editBatches) {
67             for (int i = 0; i < editBatch; i++) {
68                 final int msgId = i + (batchI * params.editBatchSize);
69                 final NetconfMessage msg = preparedMessages.get(msgId);
70                 LOG.debug("Sending message {}", msgId);
71                 if(LOG.isDebugEnabled()) {
72                     LOG.debug("Sending message {}", XmlUtil.toString(msg.getDocument()));
73                 }
74                 final ListenableFuture<RpcResult<NetconfMessage>> netconfMessageFuture =
75                         sessionListener.sendRequest(msg, StressClient.EDIT_QNAME);
76                 futures.add(netconfMessageFuture);
77             }
78             batchI++;
79             LOG.info("Batch {} with size {} sent. Committing", batchI, editBatch);
80             futures.add(sessionListener.sendRequest(StressClient.COMMIT_MSG, StressClient.COMMIT_QNAME));
81         }
82
83         LOG.info("All batches sent. Waiting for responses");
84         // Wait for every future
85         for (final ListenableFuture<RpcResult<NetconfMessage>> future : futures) {
86             try {
87                 final RpcResult<NetconfMessage> netconfMessageRpcResult = future.get(params.msgTimeout, TimeUnit.SECONDS);
88                 if(netconfMessageRpcResult.isSuccessful()) {
89                     responseCounter.incrementAndGet();
90                     LOG.debug("Received response {}", responseCounter.get());
91                 } else {
92                     LOG.warn("Request failed {}", netconfMessageRpcResult);
93                 }
94             } catch (final InterruptedException e) {
95                 throw new RuntimeException(e);
96             } catch (final ExecutionException | TimeoutException e) {
97                 throw new RuntimeException("Request not finished", e);
98             }
99         }
100
101         Preconditions.checkState(responseCounter.get() == editAmount + editBatches.size(), "Not all responses were received, only %s from %s", responseCounter.get(), params.editCount + editBatches.size());
102     }
103 }