2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.test.tool.client.stress;
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;
26 class AsyncExecutionStrategy implements ExecutionStrategy {
27 private static final Logger LOG = LoggerFactory.getLogger(AsyncExecutionStrategy.class);
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;
35 public AsyncExecutionStrategy(final Parameters params, final List<NetconfMessage> editConfigMsgs, final NetconfDeviceCommunicator sessionListener) {
37 this.preparedMessages = editConfigMsgs;
38 this.sessionListener = sessionListener;
39 this.editBatches = countEditBatchSizes(params, editConfigMsgs.size());
40 editAmount = editConfigMsgs.size();
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);
51 if (amount % params.editBatchSize != 0) {
52 editBatches.add(amount % params.editBatchSize);
55 editBatches.add(params.editBatchSize);
61 public void invoke() {
62 final AtomicInteger responseCounter = new AtomicInteger(0);
63 final List<ListenableFuture<RpcResult<NetconfMessage>>> futures = Lists.newArrayList();
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()));
74 final ListenableFuture<RpcResult<NetconfMessage>> netconfMessageFuture =
75 sessionListener.sendRequest(msg, StressClient.EDIT_QNAME);
76 futures.add(netconfMessageFuture);
79 LOG.info("Batch {} with size {} sent. Committing", batchI, editBatch);
80 futures.add(sessionListener.sendRequest(StressClient.COMMIT_MSG, StressClient.COMMIT_QNAME));
83 LOG.info("All batches sent. Waiting for responses");
84 // Wait for every future
85 for (final ListenableFuture<RpcResult<NetconfMessage>> future : futures) {
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());
92 LOG.warn("Request failed {}", netconfMessageRpcResult);
94 } catch (final InterruptedException e) {
95 throw new RuntimeException(e);
96 } catch (final ExecutionException | TimeoutException e) {
97 throw new RuntimeException("Request not finished", e);
101 Preconditions.checkState(responseCounter.get() == editAmount + editBatches.size(), "Not all responses were received, only %s from %s", responseCounter.get(), params.editCount + editBatches.size());