Fix netconf-connector-config groupId
[netconf.git] / opendaylight / 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.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;
24
25 class SyncExecutionStrategy extends AbstractExecutionStrategy {
26     private static final Logger LOG = LoggerFactory.getLogger(SyncExecutionStrategy.class);
27
28     public SyncExecutionStrategy(final Parameters params, final List<NetconfMessage> preparedMessages, final NetconfDeviceCommunicator sessionListener) {
29         super(params, preparedMessages, sessionListener);
30     }
31
32     public void invoke() {
33         final AtomicInteger responseCounter = new AtomicInteger(0);
34
35         int batchI = 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()));
43                 }
44                 final ListenableFuture<RpcResult<NetconfMessage>> netconfMessageFuture =
45                         getSessionListener().sendRequest(msg, StressClient.EDIT_QNAME);
46                 // Wait for response
47                 waitForResponse(responseCounter, netconfMessageFuture);
48
49             }
50             batchI++;
51             LOG.info("Batch {} with size {} sent. Committing", batchI, editBatch);
52
53             // Commit batch sync
54             if (getParams().candidateDatastore) {
55                 waitForResponse(responseCounter,
56                         getSessionListener().sendRequest(StressClient.COMMIT_MSG, StressClient.COMMIT_QNAME));
57             }
58         }
59
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());
62     }
63
64     private void waitForResponse(AtomicInteger responseCounter, final ListenableFuture<RpcResult<NetconfMessage>> netconfMessageFuture) {
65         try {
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());
71             } else {
72                 LOG.warn("Request failed {}", netconfMessageRpcResult);
73             }
74
75         } catch (final InterruptedException e) {
76             throw new RuntimeException(e);
77         } catch (final ExecutionException | TimeoutException e) {
78             throw new RuntimeException("Request not finished", e);
79         }
80     }
81 }