2 * Copyright (c) 2014 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.html
9 package org.opendaylight.controller.netconf.it;
11 import static java.lang.Thread.sleep;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.fail;
14 import static org.opendaylight.controller.netconf.it.NetconfITSecureTest.getSessionListener;
16 import com.google.common.base.Throwables;
17 import com.google.common.collect.Lists;
18 import com.google.common.util.concurrent.FutureCallback;
19 import com.google.common.util.concurrent.Futures;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import io.netty.channel.nio.NioEventLoopGroup;
22 import io.netty.util.HashedWheelTimer;
23 import java.net.InetSocketAddress;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.LinkedList;
27 import java.util.List;
28 import java.util.concurrent.ExecutorService;
29 import java.util.concurrent.Executors;
30 import java.util.concurrent.Future;
31 import java.util.concurrent.TimeUnit;
32 import java.util.concurrent.TimeoutException;
33 import java.util.concurrent.atomic.AtomicInteger;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.opendaylight.controller.netconf.api.NetconfMessage;
38 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
39 import org.opendaylight.controller.netconf.client.NetconfClientDispatcherImpl;
40 import org.opendaylight.controller.netconf.client.TestingNetconfClient;
41 import org.opendaylight.controller.netconf.test.tool.Main.Params;
42 import org.opendaylight.controller.netconf.test.tool.NetconfDeviceSimulator;
43 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
44 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfDeviceCommunicator;
45 import org.opendaylight.yangtools.yang.common.QName;
46 import org.opendaylight.yangtools.yang.common.RpcResult;
48 public class NetconfITSecureTestTool
51 //set up port both for testool device and test
52 public static final int PORT = 17833;
53 private static final InetSocketAddress TLS_ADDRESS = new InetSocketAddress("127.0.0.1", PORT);
55 private String xmlFile = "netconfMessages/editConfig.xml";
57 private ExecutorService msgExec = Executors.newFixedThreadPool(8);
59 Collection<Future<?>> tasks = new LinkedList<Future<?>>();
61 final NetconfDeviceSimulator netconfDeviceSimulator = new NetconfDeviceSimulator();
64 public void setUp() throws Exception {
66 //Set up parameters for testtool device
67 Params params = new Params();
69 params.deviceCount = 1;
70 params.startingPort = PORT;
74 final List<Integer> openDevices = netconfDeviceSimulator.start(params);
78 public void tearDown() throws Exception {
83 * Test all requests are handled properly and no mismatch occurs in listener
85 @Test(timeout = 6*60*1000)
86 public void testSecureStress() throws Exception {
88 final int requests = 4000;
90 List<Future<?>> tasks = new ArrayList<>();
92 final NetconfClientDispatcher dispatch = new NetconfClientDispatcherImpl(new NioEventLoopGroup(), new NioEventLoopGroup(), new HashedWheelTimer());
94 final NetconfDeviceCommunicator sessionListener = getSessionListener();
96 try (TestingNetconfClient netconfClient = new TestingNetconfClient("testing-ssh-client", dispatch, NetconfITSecureTest.getClientConfiguration(sessionListener, TLS_ADDRESS));)
99 final AtomicInteger responseCounter = new AtomicInteger(0);
100 final List<ListenableFuture<RpcResult<NetconfMessage>>> futures = Lists.newArrayList();
102 for (int i = 0; i < requests; i++) {
104 NetconfMessage getConfig = XmlFileLoader.xmlFileToNetconfMessage(xmlFile);
106 getConfig = NetconfITSecureTest.changeMessageId(getConfig,i);
108 Runnable worker = new NetconfITSecureTestToolRunnable(getConfig,i, sessionListener, futures, responseCounter);
110 tasks.add(msgExec.submit(worker));
116 // Wait for every future
117 for (final Future<?> task : tasks){
121 task.get(3, TimeUnit.MINUTES);
122 } catch (final TimeoutException e) {
123 fail(String.format("Request %d is not responding", tasks.indexOf(task)));
127 for (final ListenableFuture<RpcResult<NetconfMessage>> future : futures) {
130 future.get(3, TimeUnit.MINUTES);
131 } catch (final TimeoutException e) {
132 fail(String.format("Reply %d is not responding", futures.indexOf(future)));
138 assertEquals(requests, responseCounter.get());
143 class NetconfITSecureTestToolRunnable implements Runnable {
145 private NetconfMessage getConfig;
147 private NetconfDeviceCommunicator sessionListener;
148 private List<ListenableFuture<RpcResult<NetconfMessage>>> futures;
149 private AtomicInteger responseCounter;
151 public NetconfITSecureTestToolRunnable(NetconfMessage getConfig, int it, NetconfDeviceCommunicator sessionListener, List<ListenableFuture<RpcResult<NetconfMessage>>> futures, AtomicInteger responseCounter){
152 this.getConfig = getConfig;
154 this.sessionListener = sessionListener;
155 this.futures = futures;
156 this.responseCounter = responseCounter;
162 ListenableFuture<RpcResult<NetconfMessage>> netconfMessageFuture;
164 netconfMessageFuture = sessionListener.sendRequest(getConfig, QName.create("namespace", "2012-12-12", "get"));
166 futures.add(netconfMessageFuture);
167 Futures.addCallback(netconfMessageFuture, new FutureCallback<RpcResult<NetconfMessage>>() {
170 public void onSuccess(final RpcResult<NetconfMessage> result) {
172 if(result.isSuccessful()&result.getErrors().isEmpty()) {
173 responseCounter.incrementAndGet();
176 fail(String.format("Message result not ok %s", result.getErrors().toString()));
182 public void onFailure(final Throwable t) {
184 fail(String.format("Message failed %s", Throwables.getStackTraceAsString(t)));