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 com.google.common.base.Optional;
12 import com.google.common.collect.Sets;
13 import io.netty.channel.ChannelFuture;
14 import io.netty.channel.EventLoopGroup;
15 import io.netty.channel.nio.NioEventLoopGroup;
16 import io.netty.util.HashedWheelTimer;
17 import org.apache.commons.io.IOUtils;
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
22 import org.opendaylight.controller.netconf.api.NetconfMessage;
23 import org.opendaylight.controller.netconf.client.test.TestingNetconfClient;
24 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
25 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl;
26 import org.opendaylight.controller.netconf.impl.osgi.SessionMonitoringService;
27 import org.opendaylight.controller.netconf.mapping.api.Capability;
28 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
29 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
30 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
31 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
32 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
33 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
34 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
35 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.w3c.dom.Document;
40 import java.io.DataOutputStream;
41 import java.io.InputStream;
42 import java.io.InputStreamReader;
43 import java.lang.management.ManagementFactory;
44 import java.net.InetSocketAddress;
45 import java.net.Socket;
46 import java.util.ArrayList;
47 import java.util.Collections;
48 import java.util.List;
51 import static com.google.common.base.Preconditions.checkNotNull;
52 import static org.junit.Assert.fail;
53 import static org.mockito.Matchers.any;
54 import static org.mockito.Mockito.doNothing;
55 import static org.mockito.Mockito.mock;
57 public class ConcurrentClientsTest {
59 private static final int CONCURRENCY = 16;
60 private EventLoopGroup nettyGroup;
61 private NetconfClientDispatcher netconfClientDispatcher;
63 private final InetSocketAddress netconfAddress = new InetSocketAddress("127.0.0.1", 8303);
65 static final Logger logger = LoggerFactory.getLogger(ConcurrentClientsTest.class);
67 private DefaultCommitNotificationProducer commitNot;
68 private NetconfServerDispatcher dispatch;
72 HashedWheelTimer hashedWheelTimer;
74 public static SessionMonitoringService createMockedMonitoringService() {
75 SessionMonitoringService monitoring = mock(SessionMonitoringService.class);
76 doNothing().when(monitoring).onSessionUp(any(NetconfServerSession.class));
77 doNothing().when(monitoring).onSessionDown(any(NetconfServerSession.class));
82 public void setUp() throws Exception {
84 nettyGroup = new NioEventLoopGroup();
85 NetconfHelloMessageAdditionalHeader additionalHeader = new NetconfHelloMessageAdditionalHeader("uname", "10.10.10.1", "830", "tcp", "client");
86 netconfClientDispatcher = new NetconfClientDispatcher( nettyGroup, nettyGroup, additionalHeader, 5000);
88 NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
89 factoriesListener.onAddNetconfOperationServiceFactory(mockOpF());
91 SessionIdProvider idProvider = new SessionIdProvider();
92 hashedWheelTimer = new HashedWheelTimer();
93 NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
94 hashedWheelTimer, factoriesListener, idProvider, 5000, commitNot, createMockedMonitoringService());
96 commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
100 NetconfServerDispatcher.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerChannelInitializer(serverNegotiatorFactory);
101 dispatch = new NetconfServerDispatcher(serverChannelInitializer, nettyGroup, nettyGroup);
103 ChannelFuture s = dispatch.createServer(netconfAddress);
108 public void tearDown(){
109 hashedWheelTimer.stop();
110 nettyGroup.shutdownGracefully();
113 private NetconfOperationServiceFactory mockOpF() {
114 return new NetconfOperationServiceFactory() {
116 public NetconfOperationService createService(String netconfSessionIdForReporting) {
117 return new NetconfOperationService() {
119 public Set<Capability> getCapabilities() {
120 return Collections.emptySet();
124 public Set<NetconfOperation> getNetconfOperations() {
125 return Sets.<NetconfOperation> newHashSet(new NetconfOperation() {
127 public HandlingPriority canHandle(Document message) {
128 return HandlingPriority.getHandlingPriority(Integer.MAX_VALUE);
132 public Document handle(Document requestMessage, NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
134 return XmlUtil.readXmlToDocument("<test/>");
135 } catch (Exception e) {
136 throw new RuntimeException(e);
143 public void close() {
151 public void cleanUp() throws Exception {
156 public void multipleClients() throws Exception {
157 List<TestingThread> threads = new ArrayList<>();
159 final int attempts = 5;
160 for (int i = 0; i < CONCURRENCY; i++) {
161 TestingThread thread = new TestingThread(String.valueOf(i), attempts);
166 for (TestingThread thread : threads) {
168 if(thread.thrownException.isPresent()) {
169 Exception exception = thread.thrownException.get();
170 logger.error("Thread for testing client failed", exception);
171 fail("Client thread " + thread + " failed: " + exception.getMessage());
177 public void synchronizationTest() throws Exception {
178 new BlockingThread("foo").run2();
182 public void multipleBlockingClients() throws Exception {
183 List<BlockingThread> threads = new ArrayList<>();
184 for (int i = 0; i < CONCURRENCY; i++) {
185 BlockingThread thread = new BlockingThread(String.valueOf(i));
190 for (BlockingThread thread : threads) {
192 if(thread.thrownException.isPresent()) {
193 Exception exception = thread.thrownException.get();
194 logger.error("Thread for testing client failed", exception);
195 fail("Client thread " + thread + " failed: " + exception.getMessage());
200 class BlockingThread extends Thread {
201 private Optional<Exception> thrownException;
203 public BlockingThread(String name) {
204 super("client-" + name);
211 thrownException = Optional.absent();
212 } catch (Exception e) {
213 thrownException = Optional.of(e);
217 private void run2() throws Exception {
218 InputStream clientHello = checkNotNull(XmlFileLoader
219 .getResourceAsStream("netconfMessages/client_hello.xml"));
220 InputStream getConfig = checkNotNull(XmlFileLoader.getResourceAsStream("netconfMessages/getConfig.xml"));
222 Socket clientSocket = new Socket(netconfAddress.getHostString(), netconfAddress.getPort());
223 DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
224 InputStreamReader inFromServer = new InputStreamReader(clientSocket.getInputStream());
226 StringBuffer sb = new StringBuffer();
227 while (sb.toString().endsWith("]]>]]>") == false) {
228 sb.append((char) inFromServer.read());
230 logger.info(sb.toString());
232 outToServer.write(IOUtils.toByteArray(clientHello));
233 outToServer.write("]]>]]>".getBytes());
235 // Thread.sleep(100);
236 outToServer.write(IOUtils.toByteArray(getConfig));
237 outToServer.write("]]>]]>".getBytes());
240 sb = new StringBuffer();
241 while (sb.toString().endsWith("]]>]]>") == false) {
242 sb.append((char) inFromServer.read());
244 logger.info(sb.toString());
245 clientSocket.close();
249 class TestingThread extends Thread {
251 private final String clientId;
252 private final int attempts;
253 private Optional<Exception> thrownException;
255 TestingThread(String clientId, int attempts) {
256 this.clientId = clientId;
257 this.attempts = attempts;
258 setName("client-" + clientId);
264 final TestingNetconfClient netconfClient = new TestingNetconfClient(clientId, netconfAddress, netconfClientDispatcher);
265 long sessionId = netconfClient.getSessionId();
266 logger.info("Client with sessionid {} hello exchanged", sessionId);
268 final NetconfMessage getMessage = XmlFileLoader
269 .xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
270 NetconfMessage result = netconfClient.sendRequest(getMessage).get();
271 logger.info("Client with sessionid {} got result {}", sessionId, result);
272 netconfClient.close();
273 logger.info("Client with session id {} ended", sessionId);
274 thrownException = Optional.absent();
275 } catch (final Exception e) {
276 thrownException = Optional.of(e);