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.controller.config.util.xml.XmlUtil;
19 import org.opendaylight.netconf.api.NetconfMessage;
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 public SyncExecutionStrategy(final Parameters params, final List<NetconfMessage> preparedMessages, final NetconfDeviceCommunicator sessionListener) {
29 super(params, preparedMessages, sessionListener);
32 public void invoke() {
33 final AtomicInteger responseCounter = new AtomicInteger(0);
36 for (final Integer editBatch : getEditBatches()) {
37 for (int i = 0; i < editBatch; i++) {
38 final int msgId = i + (batchI * getParams().editBatchSize);
39 final NetconfMessage msg = getPreparedMessages().get(msgId);
40 LOG.debug("Sending message {}", msgId);
41 if(LOG.isDebugEnabled()) {
42 LOG.debug("Sending message {}", XmlUtil.toString(msg.getDocument()));
44 final ListenableFuture<RpcResult<NetconfMessage>> netconfMessageFuture =
45 getSessionListener().sendRequest(msg, StressClient.EDIT_QNAME);
47 waitForResponse(responseCounter, netconfMessageFuture);
51 LOG.info("Batch {} with size {} sent. Committing", batchI, editBatch);
54 if (getParams().candidateDatastore) {
55 waitForResponse(responseCounter,
56 getSessionListener().sendRequest(StressClient.COMMIT_MSG, StressClient.COMMIT_QNAME));
60 Preconditions.checkState(responseCounter.get() == getEditAmount() + (getParams().candidateDatastore ? getEditBatches().size() : 0),
61 "Not all responses were received, only %s from %s", responseCounter.get(), getParams().editCount + getEditBatches().size());
64 private void waitForResponse(AtomicInteger responseCounter, final ListenableFuture<RpcResult<NetconfMessage>> netconfMessageFuture) {
66 final RpcResult<NetconfMessage> netconfMessageRpcResult =
67 netconfMessageFuture.get(getParams().msgTimeout, TimeUnit.SECONDS);
68 if (netconfMessageRpcResult.isSuccessful()) {
69 responseCounter.incrementAndGet();
70 LOG.debug("Received response {}", responseCounter.get());
72 LOG.warn("Request failed {}", netconfMessageRpcResult);
75 } catch (final InterruptedException e) {
76 throw new RuntimeException(e);
77 } catch (final ExecutionException | TimeoutException e) {
78 throw new RuntimeException("Request not finished", e);