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 // TODO reuse code from org.opendaylight.controller.netconf.test.tool.client.stress.AsyncExecutionStrategy
27 class SyncExecutionStrategy implements ExecutionStrategy {
28 private static final Logger LOG = LoggerFactory.getLogger(SyncExecutionStrategy.class);
30 private final Parameters params;
31 private final List<NetconfMessage> preparedMessages;
32 private final NetconfDeviceCommunicator sessionListener;
33 private final List<Integer> editBatches;
35 public SyncExecutionStrategy(final Parameters params, final List<NetconfMessage> preparedMessages, final NetconfDeviceCommunicator sessionListener) {
37 this.preparedMessages = preparedMessages;
38 this.sessionListener = sessionListener;
39 editBatches = countEditBatchSizes(params);
42 private static List<Integer> countEditBatchSizes(final Parameters params) {
43 final List<Integer> editBatches = Lists.newArrayList();
44 if (params.editBatchSize != params.editCount) {
45 final int fullBatches = params.editCount / params.editBatchSize;
46 for (int i = 0; i < fullBatches; i++) {
47 editBatches.add(params.editBatchSize);
50 if (params.editCount % params.editBatchSize != 0) {
51 editBatches.add(params.editCount % params.editBatchSize);
54 editBatches.add(params.editBatchSize);
59 public void invoke() {
60 final AtomicInteger responseCounter = new AtomicInteger(0);
63 for (final Integer editBatch : editBatches) {
64 for (int i = 0; i < editBatch; i++) {
65 final int msgId = i + (batchI * params.editBatchSize);
66 final NetconfMessage msg = preparedMessages.get(msgId);
67 LOG.debug("Sending message {}", msgId);
68 if(LOG.isDebugEnabled()) {
69 LOG.debug("Sending message {}", XmlUtil.toString(msg.getDocument()));
71 final ListenableFuture<RpcResult<NetconfMessage>> netconfMessageFuture =
72 sessionListener.sendRequest(msg, StressClient.EDIT_QNAME);
74 waitForResponse(responseCounter, netconfMessageFuture);
78 LOG.info("Batch {} with size {} sent. Committing", batchI, editBatch);
81 waitForResponse(responseCounter,
82 sessionListener.sendRequest(StressClient.COMMIT_MSG, StressClient.COMMIT_QNAME));
85 Preconditions.checkState(responseCounter.get() == params.editCount + editBatches.size(), "Not all responses were received, only %s from %s", responseCounter.get(), params.editCount + editBatches.size());
88 private void waitForResponse(AtomicInteger responseCounter, final ListenableFuture<RpcResult<NetconfMessage>> netconfMessageFuture) {
90 final RpcResult<NetconfMessage> netconfMessageRpcResult =
91 netconfMessageFuture.get(params.msgTimeout, TimeUnit.SECONDS);
92 if (netconfMessageRpcResult.isSuccessful()) {
93 responseCounter.incrementAndGet();
94 LOG.debug("Received response {}", responseCounter.get());
96 LOG.warn("Request failed {}", netconfMessageRpcResult);
99 } catch (final InterruptedException e) {
100 throw new RuntimeException(e);
101 } catch (final ExecutionException | TimeoutException e) {
102 throw new RuntimeException("Request not finished", e);