c5833ddef5885096c00eb4a6696dfe975df5546a
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / 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.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.netconf.api.NetconfMessage;
20 import org.opendaylight.netconf.api.xml.XmlUtil;
21 import org.opendaylight.netconf.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 extends AbstractExecutionStrategy {
27     private static final Logger LOG = LoggerFactory.getLogger(AsyncExecutionStrategy.class);
28
29     AsyncExecutionStrategy(final Parameters params, final List<NetconfMessage> editConfigMsgs,
30                            final NetconfDeviceCommunicator sessionListener) {
31         super(params, editConfigMsgs, sessionListener);
32     }
33
34     @Override
35     public void invoke() {
36         final AtomicInteger responseCounter = new AtomicInteger(0);
37         final List<ListenableFuture<RpcResult<NetconfMessage>>> futures = Lists.newArrayList();
38
39         int batchI = 0;
40         for (final Integer editBatch : getEditBatches()) {
41             for (int i = 0; i < editBatch; i++) {
42                 final int msgId = i + batchI * getParams().editBatchSize;
43                 final NetconfMessage msg = getPreparedMessages().get(msgId);
44                 LOG.debug("Sending message {}", msgId);
45                 if (LOG.isDebugEnabled()) {
46                     LOG.debug("Sending message {}", XmlUtil.toString(msg.getDocument()));
47                 }
48                 final ListenableFuture<RpcResult<NetconfMessage>> netconfMessageFuture =
49                         getSessionListener().sendRequest(msg, StressClient.EDIT_QNAME);
50                 futures.add(netconfMessageFuture);
51             }
52             batchI++;
53             LOG.info("Batch {} with size {} sent. Committing", batchI, editBatch);
54             if (getParams().candidateDatastore) {
55                 futures.add(getSessionListener().sendRequest(StressClient.COMMIT_MSG, StressClient.COMMIT_QNAME));
56             }
57         }
58
59         LOG.info("All batches sent. Waiting for responses");
60         // Wait for every future
61         for (final ListenableFuture<RpcResult<NetconfMessage>> future : futures) {
62             try {
63                 final RpcResult<NetconfMessage> netconfMessageRpcResult = future.get(
64                     getParams().msgTimeout, TimeUnit.SECONDS);
65                 if (netconfMessageRpcResult.isSuccessful()) {
66                     responseCounter.incrementAndGet();
67                     LOG.debug("Received response {}", responseCounter.get());
68                 } else {
69                     LOG.warn("Request failed {}", netconfMessageRpcResult);
70                 }
71             } catch (final InterruptedException e) {
72                 throw new RuntimeException(e);
73             } catch (final ExecutionException | TimeoutException e) {
74                 throw new RuntimeException("Request not finished", e);
75             }
76         }
77
78         Preconditions.checkState(
79             responseCounter.get() == getEditAmount() + (getParams().candidateDatastore ? getEditBatches().size() : 0),
80             "Not all responses were received, only %s from %s",
81             responseCounter.get(), getParams().editCount + getEditBatches().size());
82     }
83 }