2 * Copyright (c) 2013 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.controller.netconf.impl;
11 import static com.google.common.base.Preconditions.checkNotNull;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.fail;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.mock;
18 import com.google.common.base.Preconditions;
19 import com.google.common.collect.Lists;
20 import com.google.common.collect.Sets;
21 import io.netty.channel.ChannelFuture;
22 import io.netty.channel.EventLoopGroup;
23 import io.netty.channel.nio.NioEventLoopGroup;
24 import io.netty.util.HashedWheelTimer;
25 import io.netty.util.concurrent.GlobalEventExecutor;
26 import java.io.DataOutputStream;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import java.lang.management.ManagementFactory;
30 import java.net.InetSocketAddress;
31 import java.net.Socket;
32 import java.util.Arrays;
33 import java.util.Collection;
34 import java.util.Collections;
35 import java.util.List;
37 import java.util.concurrent.ExecutionException;
38 import java.util.concurrent.ExecutorService;
39 import java.util.concurrent.Executors;
40 import java.util.concurrent.Future;
41 import java.util.concurrent.ThreadFactory;
42 import java.util.concurrent.atomic.AtomicLong;
43 import org.apache.commons.io.IOUtils;
44 import org.junit.After;
45 import org.junit.AfterClass;
46 import org.junit.Before;
47 import org.junit.BeforeClass;
48 import org.junit.Test;
49 import org.junit.runner.RunWith;
50 import org.junit.runners.Parameterized;
51 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
52 import org.opendaylight.controller.netconf.api.NetconfMessage;
53 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
54 import org.opendaylight.controller.netconf.client.NetconfClientDispatcherImpl;
55 import org.opendaylight.controller.netconf.client.SimpleNetconfClientSessionListener;
56 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfiguration;
57 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfigurationBuilder;
58 import org.opendaylight.controller.netconf.client.test.TestingNetconfClient;
59 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl;
60 import org.opendaylight.controller.netconf.impl.osgi.SessionMonitoringService;
61 import org.opendaylight.controller.netconf.mapping.api.Capability;
62 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
63 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
64 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
65 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
66 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
67 import org.opendaylight.controller.netconf.nettyutil.handler.exi.NetconfStartExiMessage;
68 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
69 import org.opendaylight.controller.netconf.util.messages.NetconfMessageUtil;
70 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
71 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
72 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
73 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
74 import org.slf4j.Logger;
75 import org.slf4j.LoggerFactory;
76 import org.w3c.dom.Document;
78 @RunWith(Parameterized.class)
79 public class ConcurrentClientsTest {
80 private static final Logger logger = LoggerFactory.getLogger(ConcurrentClientsTest.class);
82 private static ExecutorService clientExecutor;
84 private static final int CONCURRENCY = 32;
85 private static final InetSocketAddress netconfAddress = new InetSocketAddress("127.0.0.1", 8303);
87 private int nettyThreads;
88 private Class<? extends Runnable> clientRunnable;
89 private Set<String> serverCaps;
91 public ConcurrentClientsTest(int nettyThreads, Class<? extends Runnable> clientRunnable, Set<String> serverCaps) {
92 this.nettyThreads = nettyThreads;
93 this.clientRunnable = clientRunnable;
94 this.serverCaps = serverCaps;
97 @Parameterized.Parameters()
98 public static Collection<Object[]> data() {
99 return Arrays.asList(new Object[][]{
100 {4, TestingNetconfClientRunnable.class, NetconfServerSessionNegotiatorFactory.DEFAULT_BASE_CAPABILITIES},
101 {1, TestingNetconfClientRunnable.class, NetconfServerSessionNegotiatorFactory.DEFAULT_BASE_CAPABILITIES},
102 // empty set of capabilities = only base 1.0 netconf capability
103 {4, TestingNetconfClientRunnable.class, Collections.emptySet()},
104 {4, TestingNetconfClientRunnable.class, getOnlyExiServerCaps()},
105 {4, TestingNetconfClientRunnable.class, getOnlyChunkServerCaps()},
107 {4, BlockingClientRunnable.class, getOnlyExiServerCaps()},
108 {1, BlockingClientRunnable.class, getOnlyExiServerCaps()},
112 private EventLoopGroup nettyGroup;
113 private NetconfClientDispatcher netconfClientDispatcher;
115 private DefaultCommitNotificationProducer commitNot;
117 HashedWheelTimer hashedWheelTimer;
118 private TestingNetconfOperation testingNetconfOperation;
120 public static SessionMonitoringService createMockedMonitoringService() {
121 SessionMonitoringService monitoring = mock(SessionMonitoringService.class);
122 doNothing().when(monitoring).onSessionUp(any(NetconfServerSession.class));
123 doNothing().when(monitoring).onSessionDown(any(NetconfServerSession.class));
128 public static void setUpClientExecutor() {
129 clientExecutor = Executors.newFixedThreadPool(CONCURRENCY, new ThreadFactory() {
133 public Thread newThread(final Runnable r) {
134 Thread thread = new Thread(r);
135 thread.setName("client-" + i++);
136 thread.setDaemon(true);
143 public void setUp() throws Exception {
144 hashedWheelTimer = new HashedWheelTimer();
145 nettyGroup = new NioEventLoopGroup(nettyThreads);
146 netconfClientDispatcher = new NetconfClientDispatcherImpl(nettyGroup, nettyGroup, hashedWheelTimer);
148 NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
150 testingNetconfOperation = new TestingNetconfOperation();
151 factoriesListener.onAddNetconfOperationServiceFactory(new TestingOperationServiceFactory(testingNetconfOperation));
153 SessionIdProvider idProvider = new SessionIdProvider();
155 NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
156 hashedWheelTimer, factoriesListener, idProvider, 5000, commitNot, createMockedMonitoringService(), serverCaps);
158 commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
160 NetconfServerDispatcher.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerChannelInitializer(serverNegotiatorFactory);
161 final NetconfServerDispatcher dispatch = new NetconfServerDispatcher(serverChannelInitializer, nettyGroup, nettyGroup);
163 ChannelFuture s = dispatch.createServer(netconfAddress);
168 public void tearDown(){
170 hashedWheelTimer.stop();
172 nettyGroup.shutdownGracefully().get();
173 } catch (InterruptedException | ExecutionException e) {
174 logger.warn("Ignoring exception while cleaning up after test", e);
179 public static void tearDownClientExecutor() {
180 clientExecutor.shutdownNow();
183 @Test(timeout = CONCURRENCY * 1000)
184 public void testConcurrentClients() throws Exception {
186 List<Future<?>> futures = Lists.newArrayListWithCapacity(CONCURRENCY);
188 for (int i = 0; i < CONCURRENCY; i++) {
189 futures.add(clientExecutor.submit(getInstanceOfClientRunnable()));
192 for (Future<?> future : futures) {
195 } catch (InterruptedException e) {
196 throw new IllegalStateException(e);
197 } catch (ExecutionException e) {
198 logger.error("Thread for testing client failed", e);
199 fail("Client failed: " + e.getMessage());
203 assertEquals(CONCURRENCY, testingNetconfOperation.getMessageCount());
206 public static Set<String> getOnlyExiServerCaps() {
207 return Sets.newHashSet(
208 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0,
209 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_CAPABILITY_EXI_1_0
213 public static Set<String> getOnlyChunkServerCaps() {
214 return Sets.newHashSet(
215 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0,
216 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1
220 public Runnable getInstanceOfClientRunnable() throws Exception {
221 return clientRunnable.getConstructor(ConcurrentClientsTest.class).newInstance(this);
225 * Responds to all operations except start-exi and counts all requests
227 private static class TestingNetconfOperation implements NetconfOperation {
229 private final AtomicLong counter = new AtomicLong();
232 public HandlingPriority canHandle(Document message) {
233 return XmlUtil.toString(message).contains(NetconfStartExiMessage.START_EXI) ?
234 HandlingPriority.CANNOT_HANDLE :
235 HandlingPriority.HANDLE_WITH_MAX_PRIORITY;
239 public Document handle(Document requestMessage, NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
241 logger.info("Handling netconf message from test {}", XmlUtil.toString(requestMessage));
242 counter.getAndIncrement();
243 return XmlUtil.readXmlToDocument("<test/>");
244 } catch (Exception e) {
245 throw new RuntimeException(e);
249 public long getMessageCount() {
250 return counter.get();
255 * Hardcoded operation service factory
257 private static class TestingOperationServiceFactory implements NetconfOperationServiceFactory {
258 private final NetconfOperation[] operations;
260 public TestingOperationServiceFactory(final NetconfOperation... operations) {
261 this.operations = operations;
265 public NetconfOperationService createService(String netconfSessionIdForReporting) {
266 return new NetconfOperationService() {
268 public Set<Capability> getCapabilities() {
269 return Collections.emptySet();
273 public Set<NetconfOperation> getNetconfOperations() {
274 return Sets.newHashSet(operations);
278 public void close() {}
284 * Pure socket based blocking client
286 public final class BlockingClientRunnable implements Runnable {
292 } catch (Exception e) {
293 throw new IllegalStateException(Thread.currentThread().getName(), e);
297 private void run2() throws Exception {
298 InputStream clientHello = checkNotNull(XmlFileLoader
299 .getResourceAsStream("netconfMessages/client_hello.xml"));
300 InputStream getConfig = checkNotNull(XmlFileLoader.getResourceAsStream("netconfMessages/getConfig.xml"));
302 Socket clientSocket = new Socket(netconfAddress.getHostString(), netconfAddress.getPort());
303 DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
304 InputStreamReader inFromServer = new InputStreamReader(clientSocket.getInputStream());
306 StringBuffer sb = new StringBuffer();
307 while (sb.toString().endsWith("]]>]]>") == false) {
308 sb.append((char) inFromServer.read());
310 logger.info(sb.toString());
312 outToServer.write(IOUtils.toByteArray(clientHello));
313 outToServer.write("]]>]]>".getBytes());
315 // Thread.sleep(100);
316 outToServer.write(IOUtils.toByteArray(getConfig));
317 outToServer.write("]]>]]>".getBytes());
320 sb = new StringBuffer();
321 while (sb.toString().endsWith("]]>]]>") == false) {
322 sb.append((char) inFromServer.read());
324 logger.info(sb.toString());
325 clientSocket.close();
330 * TestingNetconfClient based runnable
332 public final class TestingNetconfClientRunnable implements Runnable {
337 final TestingNetconfClient netconfClient =
338 new TestingNetconfClient(Thread.currentThread().getName(), netconfClientDispatcher, getClientConfig());
339 long sessionId = netconfClient.getSessionId();
340 logger.info("Client with session id {}: hello exchanged", sessionId);
342 final NetconfMessage getMessage = XmlFileLoader
343 .xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
344 NetconfMessage result = netconfClient.sendRequest(getMessage).get();
345 logger.info("Client with session id {}: got result {}", sessionId, result);
347 Preconditions.checkState(NetconfMessageUtil.isErrorMessage(result) == false,
348 "Received error response: " + XmlUtil.toString(result.getDocument()) + " to request: "
349 + XmlUtil.toString(getMessage.getDocument()));
351 netconfClient.close();
352 logger.info("Client with session id {}: ended", sessionId);
353 } catch (final Exception e) {
354 throw new IllegalStateException(Thread.currentThread().getName(), e);
358 private NetconfClientConfiguration getClientConfig() {
359 final NetconfClientConfigurationBuilder b = NetconfClientConfigurationBuilder.create();
360 b.withAddress(netconfAddress);
361 b.withAdditionalHeader(new NetconfHelloMessageAdditionalHeader("uname", "10.10.10.1", "830", "tcp",
363 b.withSessionListener(new SimpleNetconfClientSessionListener());
364 b.withReconnectStrategy(new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE,
365 NetconfClientConfigurationBuilder.DEFAULT_CONNECTION_TIMEOUT_MILLIS));