Fix netconf-connector-config groupId
[netconf.git] / opendaylight / 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.controller.config.util.xml.XmlUtil;
20 import org.opendaylight.netconf.api.NetconfMessage;
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     public AsyncExecutionStrategy(final Parameters params, final List<NetconfMessage> editConfigMsgs, 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 = Lists.newArrayList();
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(getParams().msgTimeout, TimeUnit.SECONDS);
63                 if(netconfMessageRpcResult.isSuccessful()) {
64                     responseCounter.incrementAndGet();
65                     LOG.debug("Received response {}", responseCounter.get());
66                 } else {
67                     LOG.warn("Request failed {}", netconfMessageRpcResult);
68                 }
69             } catch (final InterruptedException e) {
70                 throw new RuntimeException(e);
71             } catch (final ExecutionException | TimeoutException e) {
72                 throw new RuntimeException("Request not finished", e);
73             }
74         }
75
76         Preconditions.checkState(responseCounter.get() == getEditAmount() + (getParams().candidateDatastore ? getEditBatches().size() : 0),
77                 "Not all responses were received, only %s from %s", responseCounter.get(), getParams().editCount + getEditBatches().size());
78     }
79 }