Fix license header violations in controller
[controller.git] / opendaylight / netconf / netconf-it / src / test / java / org / opendaylight / controller / netconf / it / NetconfITSecureTestTool.java
1 /*
2  * Copyright (c) 2014 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.controller.netconf.it;
10
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;
15
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;
47
48 public class NetconfITSecureTestTool
49 {
50
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);
54
55     private String xmlFile = "netconfMessages/editConfig.xml";
56
57     private ExecutorService msgExec = Executors.newFixedThreadPool(8);
58
59     Collection<Future<?>> tasks = new LinkedList<Future<?>>();
60
61     final NetconfDeviceSimulator netconfDeviceSimulator = new NetconfDeviceSimulator();
62
63     @Before
64     public void setUp() throws Exception {
65
66         //Set up parameters for testtool device
67         Params params = new Params();
68         params.debug = true;
69         params.deviceCount = 1;
70         params.startingPort = PORT;
71         params.ssh = true;
72         params.exi = true;
73
74         final List<Integer> openDevices = netconfDeviceSimulator.start(params);
75     }
76
77     @After
78     public void tearDown() throws Exception {
79
80     }
81
82     /**
83      * Test all requests are handled properly and no mismatch occurs in listener
84      */
85     @Test(timeout = 6*60*1000)
86     public void testSecureStress() throws Exception {
87
88         final int requests = 4000;
89
90         List<Future<?>> tasks = new ArrayList<>();
91
92         final NetconfClientDispatcher dispatch = new NetconfClientDispatcherImpl(new NioEventLoopGroup(), new NioEventLoopGroup(), new HashedWheelTimer());
93
94         final NetconfDeviceCommunicator sessionListener = getSessionListener();
95
96         try (TestingNetconfClient netconfClient = new TestingNetconfClient("testing-ssh-client", dispatch, NetconfITSecureTest.getClientConfiguration(sessionListener, TLS_ADDRESS));)
97         {
98
99             final AtomicInteger responseCounter = new AtomicInteger(0);
100             final List<ListenableFuture<RpcResult<NetconfMessage>>> futures = Lists.newArrayList();
101
102             for (int i = 0; i < requests; i++) {
103
104                 NetconfMessage getConfig = XmlFileLoader.xmlFileToNetconfMessage(xmlFile);
105
106                 getConfig = NetconfITSecureTest.changeMessageId(getConfig,i);
107
108                 Runnable worker = new NetconfITSecureTestToolRunnable(getConfig,i, sessionListener, futures, responseCounter);
109
110                 tasks.add(msgExec.submit(worker));
111
112             }
113
114             msgExec.shutdown();
115
116             // Wait for every future
117             for (final Future<?> task : tasks){
118                 try
119                 {
120
121                     task.get(3, TimeUnit.MINUTES);
122                 } catch (final TimeoutException e) {
123                     fail(String.format("Request %d is not responding", tasks.indexOf(task)));
124                 }
125             }
126
127             for (final ListenableFuture<RpcResult<NetconfMessage>> future : futures) {
128                 try {
129
130                     future.get(3, TimeUnit.MINUTES);
131                 } catch (final TimeoutException e) {
132                     fail(String.format("Reply %d is not responding", futures.indexOf(future)));
133                 }
134             }
135
136             sleep(5000);
137
138             assertEquals(requests, responseCounter.get());
139
140         }
141     }
142
143     class NetconfITSecureTestToolRunnable implements Runnable {
144
145         private NetconfMessage getConfig;
146         private int it;
147         private NetconfDeviceCommunicator sessionListener;
148         private List<ListenableFuture<RpcResult<NetconfMessage>>> futures;
149         private AtomicInteger responseCounter;
150
151         public NetconfITSecureTestToolRunnable(NetconfMessage getConfig, int it, NetconfDeviceCommunicator sessionListener, List<ListenableFuture<RpcResult<NetconfMessage>>> futures, AtomicInteger responseCounter){
152             this.getConfig = getConfig;
153             this.it = it;
154             this.sessionListener = sessionListener;
155             this.futures = futures;
156             this.responseCounter = responseCounter;
157         }
158
159         @Override
160         public void run(){
161
162             ListenableFuture<RpcResult<NetconfMessage>> netconfMessageFuture;
163
164             netconfMessageFuture = sessionListener.sendRequest(getConfig, QName.create("namespace", "2012-12-12", "get"));
165
166             futures.add(netconfMessageFuture);
167             Futures.addCallback(netconfMessageFuture, new FutureCallback<RpcResult<NetconfMessage>>() {
168
169                     @Override
170                     public void onSuccess(final RpcResult<NetconfMessage> result) {
171
172                         if(result.isSuccessful()&result.getErrors().isEmpty()) {
173                             responseCounter.incrementAndGet();
174                         } else {
175
176                             fail(String.format("Message result not ok %s", result.getErrors().toString()));
177
178                         }
179                     }
180
181                     @Override
182                     public void onFailure(final Throwable t) {
183
184                         fail(String.format("Message failed %s", Throwables.getStackTraceAsString(t)));
185
186                     }
187                 }
188             );
189         }
190     }
191
192 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.