a74a347d3bbf5d29811ddf28dd2c9c825cb460cc
[controller.git] / opendaylight / netconf / netconf-impl / src / test / java / org / opendaylight / controller / netconf / impl / ConcurrentClientsTest.java
1 /*
2  * Copyright (c) 2013 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.impl;
10
11 import com.google.common.base.Optional;
12 import com.google.common.collect.Sets;
13 import io.netty.channel.ChannelFuture;
14 import io.netty.util.HashedWheelTimer;
15 import org.apache.commons.io.IOUtils;
16 import org.junit.After;
17 import org.junit.AfterClass;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.Mock;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.controller.config.util.ConfigRegistryJMXClient;
23 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
24 import org.opendaylight.controller.config.yang.store.api.YangStoreService;
25 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
26 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
27 import org.opendaylight.controller.netconf.api.NetconfMessage;
28 import org.opendaylight.controller.netconf.api.NetconfOperationRouter;
29 import org.opendaylight.controller.netconf.client.NetconfClient;
30 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
31 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl;
32 import org.opendaylight.controller.netconf.mapping.api.Capability;
33 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
34 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
35 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationFilter;
36 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
37 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
38 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
39 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.w3c.dom.Document;
43
44 import javax.management.ObjectName;
45 import javax.net.ssl.SSLContext;
46 import java.io.DataOutputStream;
47 import java.io.InputStream;
48 import java.io.InputStreamReader;
49 import java.lang.management.ManagementFactory;
50 import java.net.InetSocketAddress;
51 import java.net.Socket;
52 import java.util.ArrayList;
53 import java.util.Collections;
54 import java.util.List;
55 import java.util.Set;
56 import java.util.concurrent.TimeUnit;
57
58 import static com.google.common.base.Preconditions.checkNotNull;
59 import static org.junit.Assert.assertTrue;
60 import static org.mockito.Matchers.any;
61 import static org.mockito.Mockito.doReturn;
62 import static org.mockito.Mockito.mock;
63
64 public class ConcurrentClientsTest {
65
66     private static final int CONCURRENCY = 16;
67     public static final NetconfClientDispatcher NETCONF_CLIENT_DISPATCHER = new NetconfClientDispatcher(Optional.<SSLContext>absent());
68     @Mock
69     private YangStoreService yangStoreService;
70     @Mock
71     private ConfigRegistryJMXClient jmxClient;
72
73     private final InetSocketAddress netconfAddress = new InetSocketAddress("127.0.0.1", 8303);
74
75     static final Logger logger = LoggerFactory.getLogger(ConcurrentClientsTest.class);
76
77     private DefaultCommitNotificationProducer commitNot;
78     private NetconfServerDispatcher dispatch;
79
80     @Before
81     public void setUp() throws Exception {
82         { // init mocks
83             MockitoAnnotations.initMocks(this);
84             final YangStoreSnapshot yStore = mock(YangStoreSnapshot.class);
85             doReturn(yStore).when(this.yangStoreService).getYangStoreSnapshot();
86             doReturn(Collections.emptyMap()).when(yStore).getModuleMXBeanEntryMap();
87             doReturn(Collections.emptyMap()).when(yStore).getModuleMap();
88
89             final ConfigTransactionJMXClient mockedTCl = mock(ConfigTransactionJMXClient.class);
90             doReturn(mockedTCl).when(this.jmxClient).getConfigTransactionClient(any(ObjectName.class));
91
92             doReturn(Collections.emptySet()).when(jmxClient).lookupConfigBeans();
93         }
94
95         NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
96         factoriesListener.onAddNetconfOperationServiceFactory(mockOpF());
97
98         SessionIdProvider idProvider = new SessionIdProvider();
99         NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
100                 new HashedWheelTimer(5000, TimeUnit.MILLISECONDS), factoriesListener, idProvider);
101
102         commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
103
104         NetconfServerSessionListenerFactory listenerFactory = new NetconfServerSessionListenerFactory(
105                 factoriesListener, commitNot, idProvider);
106         dispatch = new NetconfServerDispatcher(Optional.<SSLContext> absent(), serverNegotiatorFactory, listenerFactory);
107
108         ChannelFuture s = dispatch.createServer(netconfAddress);
109         s.await();
110     }
111
112     @AfterClass
113     public static void tearDownStatic() {
114         NETCONF_CLIENT_DISPATCHER.close();
115     }
116
117     private NetconfOperationServiceFactory mockOpF() {
118         return new NetconfOperationServiceFactory() {
119             @Override
120             public NetconfOperationService createService(long netconfSessionId, String netconfSessionIdForReporting) {
121                 return new NetconfOperationService() {
122                     @Override
123                     public Set<Capability> getCapabilities() {
124                         return Collections.emptySet();
125                     }
126
127                     @Override
128                     public Set<NetconfOperation> getNetconfOperations() {
129                         return Sets.<NetconfOperation> newHashSet(new NetconfOperation() {
130                             @Override
131                             public HandlingPriority canHandle(Document message) {
132                                 return HandlingPriority.getHandlingPriority(Integer.MAX_VALUE);
133                             }
134
135                             @Override
136                             public Document handle(Document message, NetconfOperationRouter operationRouter)
137                                     throws NetconfDocumentedException {
138                                 try {
139                                     return XmlUtil.readXmlToDocument("<test/>");
140                                 } catch (Exception e) {
141                                     throw new RuntimeException(e);
142                                 }
143                             }
144                         });
145                     }
146
147                     @Override
148                     public Set<NetconfOperationFilter> getFilters() {
149                         return Collections.emptySet();
150                     }
151
152                     @Override
153                     public void close() {
154                     }
155                 };
156             }
157         };
158     }
159
160     @After
161     public void cleanUp() throws Exception {
162         commitNot.close();
163         dispatch.close();
164     }
165
166     @Test
167     public void multipleClients() throws Exception {
168         List<TestingThread> threads = new ArrayList<>();
169
170         final int attempts = 5;
171         for (int i = 0; i < CONCURRENCY; i++) {
172             TestingThread thread = new TestingThread(String.valueOf(i), attempts);
173             threads.add(thread);
174             thread.start();
175         }
176
177         for (TestingThread thread : threads) {
178             thread.join();
179             assertTrue(thread.success);
180         }
181     }
182
183     @Test
184     public void synchronizationTest() throws Exception {
185         new BlockingThread("foo").run2();
186     }
187
188     @Test
189     public void multipleBlockingClients() throws Exception {
190         List<BlockingThread> threads = new ArrayList<>();
191         for (int i = 0; i < CONCURRENCY; i++) {
192             BlockingThread thread = new BlockingThread(String.valueOf(i));
193             threads.add(thread);
194             thread.start();
195         }
196
197         for (BlockingThread thread : threads) {
198             thread.join();
199             assertTrue(thread.success);
200         }
201     }
202
203     class BlockingThread extends Thread {
204         Boolean success;
205
206         public BlockingThread(String name) {
207             super("client-" + name);
208         }
209
210         @Override
211         public void run() {
212             try {
213                 run2();
214                 success = true;
215             } catch (Exception e) {
216                 success = false;
217                 throw new RuntimeException(e);
218             }
219         }
220
221         private void run2() throws Exception {
222             InputStream clientHello = checkNotNull(XmlFileLoader
223                     .getResourceAsStream("netconfMessages/client_hello.xml"));
224             InputStream getConfig = checkNotNull(XmlFileLoader.getResourceAsStream("netconfMessages/getConfig.xml"));
225
226             Socket clientSocket = new Socket(netconfAddress.getHostString(), netconfAddress.getPort());
227             DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
228             InputStreamReader inFromServer = new InputStreamReader(clientSocket.getInputStream());
229
230             StringBuffer sb = new StringBuffer();
231             while (sb.toString().endsWith("]]>]]>") == false) {
232                 sb.append((char) inFromServer.read());
233             }
234             logger.info(sb.toString());
235
236             outToServer.write(IOUtils.toByteArray(clientHello));
237             outToServer.write("]]>]]>".getBytes());
238             outToServer.flush();
239             // Thread.sleep(100);
240             outToServer.write(IOUtils.toByteArray(getConfig));
241             outToServer.write("]]>]]>".getBytes());
242             outToServer.flush();
243             Thread.sleep(100);
244             sb = new StringBuffer();
245             while (sb.toString().endsWith("]]>]]>") == false) {
246                 sb.append((char) inFromServer.read());
247             }
248             logger.info(sb.toString());
249             clientSocket.close();
250         }
251     }
252
253     class TestingThread extends Thread {
254
255         private final String clientId;
256         private final int attempts;
257         private Boolean success;
258
259         TestingThread(String clientId, int attempts) {
260             this.clientId = clientId;
261             this.attempts = attempts;
262             setName("client-" + clientId);
263         }
264
265         @Override
266         public void run() {
267             try {
268                 final NetconfClient netconfClient = new NetconfClient(clientId, netconfAddress, NETCONF_CLIENT_DISPATCHER);
269                 long sessionId = netconfClient.getSessionId();
270                 logger.info("Client with sessionid {} hello exchanged", sessionId);
271
272                 final NetconfMessage getMessage = XmlFileLoader
273                         .xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
274                 NetconfMessage result = netconfClient.sendMessage(getMessage);
275                 logger.info("Client with sessionid {} got result {}", sessionId, result);
276                 netconfClient.close();
277                 logger.info("Client with session id {} ended", sessionId);
278                 success = true;
279             } catch (final Exception e) {
280                 success = false;
281                 throw new RuntimeException(e);
282             }
283         }
284     }
285 }