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.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.Matchers.anySetOf;
16 import static org.mockito.Mockito.doNothing;
17 import static org.mockito.Mockito.doReturn;
18 import static org.mockito.Mockito.mock;
20 import com.google.common.base.Preconditions;
21 import com.google.common.collect.Lists;
22 import com.google.common.collect.Sets;
23 import com.google.common.io.ByteStreams;
24 import io.netty.channel.ChannelFuture;
25 import io.netty.channel.EventLoopGroup;
26 import io.netty.channel.nio.NioEventLoopGroup;
27 import io.netty.util.HashedWheelTimer;
28 import io.netty.util.concurrent.GlobalEventExecutor;
29 import java.io.DataOutputStream;
30 import java.io.InputStream;
31 import java.io.InputStreamReader;
32 import java.net.InetSocketAddress;
33 import java.net.Socket;
34 import java.util.Arrays;
35 import java.util.Collection;
36 import java.util.Collections;
37 import java.util.List;
39 import java.util.concurrent.ExecutionException;
40 import java.util.concurrent.ExecutorService;
41 import java.util.concurrent.Executors;
42 import java.util.concurrent.Future;
43 import java.util.concurrent.ThreadFactory;
44 import java.util.concurrent.atomic.AtomicLong;
45 import org.junit.After;
46 import org.junit.AfterClass;
47 import org.junit.Before;
48 import org.junit.BeforeClass;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.junit.runners.Parameterized;
52 import org.opendaylight.controller.config.util.capability.Capability;
53 import org.opendaylight.controller.config.util.xml.DocumentedException;
54 import org.opendaylight.controller.config.util.xml.XmlUtil;
55 import org.opendaylight.netconf.mapping.api.HandlingPriority;
56 import org.opendaylight.netconf.mapping.api.NetconfOperation;
57 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
58 import org.opendaylight.netconf.mapping.api.NetconfOperationService;
59 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
60 import org.opendaylight.controller.netconf.nettyutil.handler.exi.NetconfStartExiMessage;
61 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
62 import org.opendaylight.controller.netconf.util.messages.NetconfMessageUtil;
63 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
64 import org.opendaylight.netconf.api.NetconfMessage;
65 import org.opendaylight.netconf.api.monitoring.CapabilityListener;
66 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
67 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
68 import org.opendaylight.netconf.client.NetconfClientDispatcher;
69 import org.opendaylight.netconf.client.NetconfClientDispatcherImpl;
70 import org.opendaylight.netconf.client.SimpleNetconfClientSessionListener;
71 import org.opendaylight.netconf.client.TestingNetconfClient;
72 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
73 import org.opendaylight.netconf.client.conf.NetconfClientConfigurationBuilder;
74 import org.opendaylight.netconf.impl.osgi.AggregatedNetconfOperationServiceFactory;
75 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
76 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
77 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.CapabilitiesBuilder;
78 import org.slf4j.Logger;
79 import org.slf4j.LoggerFactory;
80 import org.w3c.dom.Document;
82 @RunWith(Parameterized.class)
83 public class ConcurrentClientsTest {
84 private static final Logger LOG = LoggerFactory.getLogger(ConcurrentClientsTest.class);
86 private static ExecutorService clientExecutor;
88 private static final int CONCURRENCY = 32;
89 private static final InetSocketAddress netconfAddress = new InetSocketAddress("127.0.0.1", 8303);
91 private int nettyThreads;
92 private Class<? extends Runnable> clientRunnable;
93 private Set<String> serverCaps;
95 public ConcurrentClientsTest(int nettyThreads, Class<? extends Runnable> clientRunnable, Set<String> serverCaps) {
96 this.nettyThreads = nettyThreads;
97 this.clientRunnable = clientRunnable;
98 this.serverCaps = serverCaps;
101 @Parameterized.Parameters()
102 public static Collection<Object[]> data() {
103 return Arrays.asList(new Object[][]{{4, TestingNetconfClientRunnable.class, NetconfServerSessionNegotiatorFactory.DEFAULT_BASE_CAPABILITIES},
104 {1, TestingNetconfClientRunnable.class, NetconfServerSessionNegotiatorFactory.DEFAULT_BASE_CAPABILITIES},
105 // empty set of capabilities = only base 1.0 netconf capability
106 {4, TestingNetconfClientRunnable.class, Collections.emptySet()},
107 {4, TestingNetconfClientRunnable.class, getOnlyExiServerCaps()},
108 {4, TestingNetconfClientRunnable.class, getOnlyChunkServerCaps()},
109 {4, BlockingClientRunnable.class, getOnlyExiServerCaps()},
110 {1, BlockingClientRunnable.class, getOnlyExiServerCaps()},
114 private EventLoopGroup nettyGroup;
115 private NetconfClientDispatcher netconfClientDispatcher;
117 HashedWheelTimer hashedWheelTimer;
118 private TestingNetconfOperation testingNetconfOperation;
120 public static NetconfMonitoringService createMockedMonitoringService() {
121 NetconfMonitoringService monitoring = mock(NetconfMonitoringService.class);
122 doNothing().when(monitoring).onSessionUp(any(NetconfServerSession.class));
123 doNothing().when(monitoring).onSessionDown(any(NetconfServerSession.class));
124 doReturn(new AutoCloseable() {
126 public void close() throws Exception {
129 }).when(monitoring).registerListener(any(NetconfMonitoringService.MonitoringListener.class));
130 doNothing().when(monitoring).onCapabilitiesChanged(anySetOf(Capability.class), anySetOf(Capability.class));
131 doReturn(new CapabilitiesBuilder().setCapability(Collections.<Uri>emptyList()).build()).when(monitoring).getCapabilities();
136 public static void setUpClientExecutor() {
137 clientExecutor = Executors.newFixedThreadPool(CONCURRENCY, new ThreadFactory() {
141 public Thread newThread(final Runnable r) {
142 Thread thread = new Thread(r);
143 thread.setName("client-" + i++);
144 thread.setDaemon(true);
151 public void setUp() throws Exception {
152 hashedWheelTimer = new HashedWheelTimer();
153 nettyGroup = new NioEventLoopGroup(nettyThreads);
154 netconfClientDispatcher = new NetconfClientDispatcherImpl(nettyGroup, nettyGroup, hashedWheelTimer);
156 AggregatedNetconfOperationServiceFactory factoriesListener = new AggregatedNetconfOperationServiceFactory();
158 testingNetconfOperation = new TestingNetconfOperation();
159 factoriesListener.onAddNetconfOperationServiceFactory(new TestingOperationServiceFactory(testingNetconfOperation));
161 SessionIdProvider idProvider = new SessionIdProvider();
163 NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
164 hashedWheelTimer, factoriesListener, idProvider, 5000, createMockedMonitoringService(), serverCaps);
166 NetconfServerDispatcherImpl.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcherImpl.ServerChannelInitializer(serverNegotiatorFactory);
167 final NetconfServerDispatcherImpl dispatch = new NetconfServerDispatcherImpl(serverChannelInitializer, nettyGroup, nettyGroup);
169 ChannelFuture s = dispatch.createServer(netconfAddress);
174 public void tearDown(){
175 hashedWheelTimer.stop();
177 nettyGroup.shutdownGracefully().get();
178 } catch (InterruptedException | ExecutionException e) {
179 LOG.warn("Ignoring exception while cleaning up after test", e);
184 public static void tearDownClientExecutor() {
185 clientExecutor.shutdownNow();
188 @Test(timeout = CONCURRENCY * 1000)
189 public void testConcurrentClients() throws Exception {
191 List<Future<?>> futures = Lists.newArrayListWithCapacity(CONCURRENCY);
193 for (int i = 0; i < CONCURRENCY; i++) {
194 futures.add(clientExecutor.submit(getInstanceOfClientRunnable()));
197 for (Future<?> future : futures) {
200 } catch (InterruptedException e) {
201 throw new IllegalStateException(e);
202 } catch (ExecutionException e) {
203 LOG.error("Thread for testing client failed", e);
204 fail("Client failed: " + e.getMessage());
208 assertEquals(CONCURRENCY, testingNetconfOperation.getMessageCount());
211 public static Set<String> getOnlyExiServerCaps() {
212 return Sets.newHashSet(
213 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0,
214 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_CAPABILITY_EXI_1_0
218 public static Set<String> getOnlyChunkServerCaps() {
219 return Sets.newHashSet(
220 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0,
221 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1
225 public Runnable getInstanceOfClientRunnable() throws Exception {
226 return clientRunnable.getConstructor(ConcurrentClientsTest.class).newInstance(this);
230 * Responds to all operations except start-exi and counts all requests
232 private static class TestingNetconfOperation implements NetconfOperation {
234 private final AtomicLong counter = new AtomicLong();
237 public HandlingPriority canHandle(Document message) {
238 return XmlUtil.toString(message).contains(NetconfStartExiMessage.START_EXI) ?
239 HandlingPriority.CANNOT_HANDLE :
240 HandlingPriority.HANDLE_WITH_MAX_PRIORITY;
244 public Document handle(Document requestMessage, NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
246 LOG.info("Handling netconf message from test {}", XmlUtil.toString(requestMessage));
247 counter.getAndIncrement();
248 return XmlUtil.readXmlToDocument("<test/>");
249 } catch (Exception e) {
250 throw new RuntimeException(e);
254 public long getMessageCount() {
255 return counter.get();
260 * Hardcoded operation service factory
262 private static class TestingOperationServiceFactory implements NetconfOperationServiceFactory {
263 private final NetconfOperation[] operations;
265 public TestingOperationServiceFactory(final NetconfOperation... operations) {
266 this.operations = operations;
270 public Set<Capability> getCapabilities() {
271 return Collections.emptySet();
275 public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
276 return new AutoCloseable(){
278 public void close() throws Exception {}
283 public NetconfOperationService createService(String netconfSessionIdForReporting) {
284 return new NetconfOperationService() {
287 public Set<NetconfOperation> getNetconfOperations() {
288 return Sets.newHashSet(operations);
292 public void close() {}
298 * Pure socket based blocking client
300 public final class BlockingClientRunnable implements Runnable {
306 } catch (Exception e) {
307 throw new IllegalStateException(Thread.currentThread().getName(), e);
311 private void run2() throws Exception {
312 InputStream clientHello = checkNotNull(XmlFileLoader
313 .getResourceAsStream("netconfMessages/client_hello.xml"));
314 InputStream getConfig = checkNotNull(XmlFileLoader.getResourceAsStream("netconfMessages/getConfig.xml"));
316 Socket clientSocket = new Socket(netconfAddress.getHostString(), netconfAddress.getPort());
317 DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
318 InputStreamReader inFromServer = new InputStreamReader(clientSocket.getInputStream());
320 StringBuffer sb = new StringBuffer();
321 while (sb.toString().endsWith("]]>]]>") == false) {
322 sb.append((char) inFromServer.read());
324 LOG.info(sb.toString());
326 outToServer.write(ByteStreams.toByteArray(clientHello));
327 outToServer.write("]]>]]>".getBytes());
329 // Thread.sleep(100);
330 outToServer.write(ByteStreams.toByteArray(getConfig));
331 outToServer.write("]]>]]>".getBytes());
334 sb = new StringBuffer();
335 while (sb.toString().endsWith("]]>]]>") == false) {
336 sb.append((char) inFromServer.read());
338 LOG.info(sb.toString());
339 clientSocket.close();
344 * TestingNetconfClient based runnable
346 public final class TestingNetconfClientRunnable implements Runnable {
351 final TestingNetconfClient netconfClient =
352 new TestingNetconfClient(Thread.currentThread().getName(), netconfClientDispatcher, getClientConfig());
353 long sessionId = netconfClient.getSessionId();
354 LOG.info("Client with session id {}: hello exchanged", sessionId);
356 final NetconfMessage getMessage = XmlFileLoader
357 .xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
358 NetconfMessage result = netconfClient.sendRequest(getMessage).get();
359 LOG.info("Client with session id {}: got result {}", sessionId, result);
361 Preconditions.checkState(NetconfMessageUtil.isErrorMessage(result) == false,
362 "Received error response: " + XmlUtil.toString(result.getDocument()) + " to request: "
363 + XmlUtil.toString(getMessage.getDocument()));
365 netconfClient.close();
366 LOG.info("Client with session id {}: ended", sessionId);
367 } catch (final Exception e) {
368 throw new IllegalStateException(Thread.currentThread().getName(), e);
372 private NetconfClientConfiguration getClientConfig() {
373 final NetconfClientConfigurationBuilder b = NetconfClientConfigurationBuilder.create();
374 b.withAddress(netconfAddress);
375 b.withAdditionalHeader(new NetconfHelloMessageAdditionalHeader("uname", "10.10.10.1", "830", "tcp",
377 b.withSessionListener(new SimpleNetconfClientSessionListener());
378 b.withReconnectStrategy(new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE,
379 NetconfClientConfigurationBuilder.DEFAULT_CONNECTION_TIMEOUT_MILLIS));