087eeb0937766ae4c2f7862d7d5f88e3a70f2423
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / client / stress / SyncExecutionStrategy.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.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;
24
25 class SyncExecutionStrategy extends AbstractExecutionStrategy {
26     private static final Logger LOG = LoggerFactory.getLogger(SyncExecutionStrategy.class);
27
28     SyncExecutionStrategy(final Parameters params, final List<NetconfMessage> preparedMessages,
29                           final NetconfDeviceCommunicator sessionListener) {
30         super(params, preparedMessages, sessionListener);
31     }
32
33     @Override
34     public void invoke() {
35         final AtomicInteger responseCounter = new AtomicInteger(0);
36
37         int batchI = 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()));
45                 }
46                 final ListenableFuture<RpcResult<NetconfMessage>> netconfMessageFuture =
47                         getSessionListener().sendRequest(msg, StressClient.EDIT_QNAME);
48                 // Wait for response
49                 waitForResponse(responseCounter, netconfMessageFuture);
50
51             }
52             batchI++;
53             LOG.info("Batch {} with size {} sent. Committing", batchI, editBatch);
54
55             // Commit batch sync
56             if (getParams().candidateDatastore) {
57                 waitForResponse(responseCounter,
58                         getSessionListener().sendRequest(StressClient.COMMIT_MSG, StressClient.COMMIT_QNAME));
59             }
60         }
61
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());
66     }
67
68     private void waitForResponse(final AtomicInteger responseCounter,
69                                  final ListenableFuture<RpcResult<NetconfMessage>> netconfMessageFuture) {
70         try {
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());
76             } else {
77                 LOG.warn("Request failed {}", netconfMessageRpcResult);
78             }
79
80         } catch (final InterruptedException e) {
81             throw new RuntimeException(e);
82         } catch (final ExecutionException | TimeoutException e) {
83             throw new RuntimeException("Request not finished", e);
84         }
85     }
86 }