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