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.netconf.test.tool.client.stress;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.ListenableFuture;
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;
25 class SyncExecutionStrategy extends AbstractExecutionStrategy {
26 private static final Logger LOG = LoggerFactory.getLogger(SyncExecutionStrategy.class);
28 SyncExecutionStrategy(final Parameters params, final List<NetconfMessage> preparedMessages,
29 final NetconfDeviceCommunicator sessionListener) {
30 super(params, preparedMessages, sessionListener);
34 public void invoke() {
35 final AtomicInteger responseCounter = new AtomicInteger(0);
38 for (final Integer editBatch : getEditBatches()) {
39 for (int i = 0; i < editBatch; i++) {
40 final int msgId = i + batchI * getParams().editBatchSize;
41 final NetconfMessage msg = getPreparedMessages().get(msgId);
42 LOG.debug("Sending message {}", msgId);
43 if (LOG.isDebugEnabled()) {
44 LOG.debug("Sending message {}", XmlUtil.toString(msg.getDocument()));
46 final ListenableFuture<RpcResult<NetconfMessage>> netconfMessageFuture =
47 getSessionListener().sendRequest(msg, StressClient.EDIT_QNAME);
49 waitForResponse(responseCounter, netconfMessageFuture);
53 LOG.info("Batch {} with size {} sent. Committing", batchI, editBatch);
56 if (getParams().candidateDatastore) {
57 waitForResponse(responseCounter,
58 getSessionListener().sendRequest(StressClient.COMMIT_MSG, StressClient.COMMIT_QNAME));
62 Preconditions.checkState(
63 responseCounter.get() == getEditAmount() + (getParams().candidateDatastore ? getEditBatches().size() : 0),
64 "Not all responses were received, only %s from %s",
65 responseCounter.get(), getParams().editCount + getEditBatches().size());
68 private void waitForResponse(final AtomicInteger responseCounter,
69 final ListenableFuture<RpcResult<NetconfMessage>> netconfMessageFuture) {
71 final RpcResult<NetconfMessage> netconfMessageRpcResult =
72 netconfMessageFuture.get(getParams().msgTimeout, TimeUnit.SECONDS);
73 if (netconfMessageRpcResult.isSuccessful()) {
74 responseCounter.incrementAndGet();
75 LOG.debug("Received response {}", responseCounter.get());
77 LOG.warn("Request failed {}", netconfMessageRpcResult);
80 } catch (final InterruptedException e) {
81 throw new RuntimeException(e);
82 } catch (final ExecutionException | TimeoutException e) {
83 throw new RuntimeException("Request not finished", e);