X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fimpl%2FConcurrentClientsTest.java;h=b8b544b90d25e71395d6aee1c6489210a604d2f1;hb=refs%2Fchanges%2F13%2F23413%2F26;hp=ea2bb9c34e4801f5c66b33ef108110602ba5c60b;hpb=803d525860fbb1974b65ba5605ba5a9dfe1928a4;p=controller.git diff --git a/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/ConcurrentClientsTest.java b/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/ConcurrentClientsTest.java index ea2bb9c34e..b8b544b90d 100644 --- a/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/ConcurrentClientsTest.java +++ b/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/ConcurrentClientsTest.java @@ -12,12 +12,15 @@ import static com.google.common.base.Preconditions.checkNotNull; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anySetOf; import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import com.google.common.base.Preconditions; import com.google.common.collect.Lists; import com.google.common.collect.Sets; +import com.google.common.io.ByteStreams; import io.netty.channel.ChannelFuture; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; @@ -26,7 +29,6 @@ import io.netty.util.concurrent.GlobalEventExecutor; import java.io.DataOutputStream; import java.io.InputStream; import java.io.InputStreamReader; -import java.lang.management.ManagementFactory; import java.net.InetSocketAddress; import java.net.Socket; import java.util.Arrays; @@ -40,7 +42,6 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicLong; -import org.apache.commons.io.IOUtils; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; @@ -48,17 +49,20 @@ import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; -import org.opendaylight.controller.netconf.api.NetconfDocumentedException; +import org.opendaylight.controller.config.util.capability.Capability; +import org.opendaylight.controller.config.util.xml.DocumentedException; +import org.opendaylight.controller.config.util.xml.XmlUtil; import org.opendaylight.controller.netconf.api.NetconfMessage; +import org.opendaylight.controller.netconf.api.monitoring.CapabilityListener; +import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService; +import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants; import org.opendaylight.controller.netconf.client.NetconfClientDispatcher; import org.opendaylight.controller.netconf.client.NetconfClientDispatcherImpl; import org.opendaylight.controller.netconf.client.SimpleNetconfClientSessionListener; +import org.opendaylight.controller.netconf.client.TestingNetconfClient; import org.opendaylight.controller.netconf.client.conf.NetconfClientConfiguration; import org.opendaylight.controller.netconf.client.conf.NetconfClientConfigurationBuilder; -import org.opendaylight.controller.netconf.client.test.TestingNetconfClient; -import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl; -import org.opendaylight.controller.netconf.impl.osgi.SessionMonitoringService; -import org.opendaylight.controller.netconf.mapping.api.Capability; +import org.opendaylight.controller.netconf.impl.osgi.AggregatedNetconfOperationServiceFactory; import org.opendaylight.controller.netconf.mapping.api.HandlingPriority; import org.opendaylight.controller.netconf.mapping.api.NetconfOperation; import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution; @@ -68,16 +72,16 @@ import org.opendaylight.controller.netconf.nettyutil.handler.exi.NetconfStartExi import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader; import org.opendaylight.controller.netconf.util.messages.NetconfMessageUtil; import org.opendaylight.controller.netconf.util.test.XmlFileLoader; -import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants; -import org.opendaylight.controller.netconf.util.xml.XmlUtil; import org.opendaylight.protocol.framework.NeverReconnectStrategy; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.CapabilitiesBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Document; @RunWith(Parameterized.class) public class ConcurrentClientsTest { - private static final Logger logger = LoggerFactory.getLogger(ConcurrentClientsTest.class); + private static final Logger LOG = LoggerFactory.getLogger(ConcurrentClientsTest.class); private static ExecutorService clientExecutor; @@ -96,31 +100,35 @@ public class ConcurrentClientsTest { @Parameterized.Parameters() public static Collection data() { - return Arrays.asList(new Object[][]{ - {4, TestingNetconfClientRunnable.class, NetconfServerSessionNegotiatorFactory.DEFAULT_BASE_CAPABILITIES}, - {1, TestingNetconfClientRunnable.class, NetconfServerSessionNegotiatorFactory.DEFAULT_BASE_CAPABILITIES}, - // empty set of capabilities = only base 1.0 netconf capability - {4, TestingNetconfClientRunnable.class, Collections.emptySet()}, - {4, TestingNetconfClientRunnable.class, getOnlyExiServerCaps()}, - {4, TestingNetconfClientRunnable.class, getOnlyChunkServerCaps()}, - - {4, BlockingClientRunnable.class, getOnlyExiServerCaps()}, - {1, BlockingClientRunnable.class, getOnlyExiServerCaps()}, + return Arrays.asList(new Object[][]{{4, TestingNetconfClientRunnable.class, NetconfServerSessionNegotiatorFactory.DEFAULT_BASE_CAPABILITIES}, + {1, TestingNetconfClientRunnable.class, NetconfServerSessionNegotiatorFactory.DEFAULT_BASE_CAPABILITIES}, + // empty set of capabilities = only base 1.0 netconf capability + {4, TestingNetconfClientRunnable.class, Collections.emptySet()}, + {4, TestingNetconfClientRunnable.class, getOnlyExiServerCaps()}, + {4, TestingNetconfClientRunnable.class, getOnlyChunkServerCaps()}, + {4, BlockingClientRunnable.class, getOnlyExiServerCaps()}, + {1, BlockingClientRunnable.class, getOnlyExiServerCaps()}, }); } private EventLoopGroup nettyGroup; private NetconfClientDispatcher netconfClientDispatcher; - private DefaultCommitNotificationProducer commitNot; - HashedWheelTimer hashedWheelTimer; private TestingNetconfOperation testingNetconfOperation; - public static SessionMonitoringService createMockedMonitoringService() { - SessionMonitoringService monitoring = mock(SessionMonitoringService.class); + public static NetconfMonitoringService createMockedMonitoringService() { + NetconfMonitoringService monitoring = mock(NetconfMonitoringService.class); doNothing().when(monitoring).onSessionUp(any(NetconfServerSession.class)); doNothing().when(monitoring).onSessionDown(any(NetconfServerSession.class)); + doReturn(new AutoCloseable() { + @Override + public void close() throws Exception { + + } + }).when(monitoring).registerListener(any(NetconfMonitoringService.MonitoringListener.class)); + doNothing().when(monitoring).onCapabilitiesChanged(anySetOf(Capability.class), anySetOf(Capability.class)); + doReturn(new CapabilitiesBuilder().setCapability(Collections.emptyList()).build()).when(monitoring).getCapabilities(); return monitoring; } @@ -145,7 +153,7 @@ public class ConcurrentClientsTest { nettyGroup = new NioEventLoopGroup(nettyThreads); netconfClientDispatcher = new NetconfClientDispatcherImpl(nettyGroup, nettyGroup, hashedWheelTimer); - NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl(); + AggregatedNetconfOperationServiceFactory factoriesListener = new AggregatedNetconfOperationServiceFactory(); testingNetconfOperation = new TestingNetconfOperation(); factoriesListener.onAddNetconfOperationServiceFactory(new TestingOperationServiceFactory(testingNetconfOperation)); @@ -153,12 +161,10 @@ public class ConcurrentClientsTest { SessionIdProvider idProvider = new SessionIdProvider(); NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory( - hashedWheelTimer, factoriesListener, idProvider, 5000, commitNot, createMockedMonitoringService(), serverCaps); + hashedWheelTimer, factoriesListener, idProvider, 5000, createMockedMonitoringService(), serverCaps); - commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer()); - - NetconfServerDispatcher.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerChannelInitializer(serverNegotiatorFactory); - final NetconfServerDispatcher dispatch = new NetconfServerDispatcher(serverChannelInitializer, nettyGroup, nettyGroup); + NetconfServerDispatcherImpl.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcherImpl.ServerChannelInitializer(serverNegotiatorFactory); + final NetconfServerDispatcherImpl dispatch = new NetconfServerDispatcherImpl(serverChannelInitializer, nettyGroup, nettyGroup); ChannelFuture s = dispatch.createServer(netconfAddress); s.await(); @@ -166,12 +172,11 @@ public class ConcurrentClientsTest { @After public void tearDown(){ - commitNot.close(); hashedWheelTimer.stop(); try { nettyGroup.shutdownGracefully().get(); } catch (InterruptedException | ExecutionException e) { - logger.warn("Ignoring exception while cleaning up after test", e); + LOG.warn("Ignoring exception while cleaning up after test", e); } } @@ -195,7 +200,7 @@ public class ConcurrentClientsTest { } catch (InterruptedException e) { throw new IllegalStateException(e); } catch (ExecutionException e) { - logger.error("Thread for testing client failed", e); + LOG.error("Thread for testing client failed", e); fail("Client failed: " + e.getMessage()); } } @@ -236,9 +241,9 @@ public class ConcurrentClientsTest { } @Override - public Document handle(Document requestMessage, NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException { + public Document handle(Document requestMessage, NetconfOperationChainedExecution subsequentOperation) throws DocumentedException { try { - logger.info("Handling netconf message from test {}", XmlUtil.toString(requestMessage)); + LOG.info("Handling netconf message from test {}", XmlUtil.toString(requestMessage)); counter.getAndIncrement(); return XmlUtil.readXmlToDocument(""); } catch (Exception e) { @@ -261,13 +266,22 @@ public class ConcurrentClientsTest { this.operations = operations; } + @Override + public Set getCapabilities() { + return Collections.emptySet(); + } + + @Override + public AutoCloseable registerCapabilityListener(final CapabilityListener listener) { + return new AutoCloseable(){ + @Override + public void close() throws Exception {} + }; + } + @Override public NetconfOperationService createService(String netconfSessionIdForReporting) { return new NetconfOperationService() { - @Override - public Set getCapabilities() { - return Collections.emptySet(); - } @Override public Set getNetconfOperations() { @@ -307,13 +321,13 @@ public class ConcurrentClientsTest { while (sb.toString().endsWith("]]>]]>") == false) { sb.append((char) inFromServer.read()); } - logger.info(sb.toString()); + LOG.info(sb.toString()); - outToServer.write(IOUtils.toByteArray(clientHello)); + outToServer.write(ByteStreams.toByteArray(clientHello)); outToServer.write("]]>]]>".getBytes()); outToServer.flush(); // Thread.sleep(100); - outToServer.write(IOUtils.toByteArray(getConfig)); + outToServer.write(ByteStreams.toByteArray(getConfig)); outToServer.write("]]>]]>".getBytes()); outToServer.flush(); Thread.sleep(100); @@ -321,7 +335,7 @@ public class ConcurrentClientsTest { while (sb.toString().endsWith("]]>]]>") == false) { sb.append((char) inFromServer.read()); } - logger.info(sb.toString()); + LOG.info(sb.toString()); clientSocket.close(); } } @@ -337,19 +351,19 @@ public class ConcurrentClientsTest { final TestingNetconfClient netconfClient = new TestingNetconfClient(Thread.currentThread().getName(), netconfClientDispatcher, getClientConfig()); long sessionId = netconfClient.getSessionId(); - logger.info("Client with session id {}: hello exchanged", sessionId); + LOG.info("Client with session id {}: hello exchanged", sessionId); final NetconfMessage getMessage = XmlFileLoader .xmlFileToNetconfMessage("netconfMessages/getConfig.xml"); NetconfMessage result = netconfClient.sendRequest(getMessage).get(); - logger.info("Client with session id {}: got result {}", sessionId, result); + LOG.info("Client with session id {}: got result {}", sessionId, result); Preconditions.checkState(NetconfMessageUtil.isErrorMessage(result) == false, "Received error response: " + XmlUtil.toString(result.getDocument()) + " to request: " + XmlUtil.toString(getMessage.getDocument())); netconfClient.close(); - logger.info("Client with session id {}: ended", sessionId); + LOG.info("Client with session id {}: ended", sessionId); } catch (final Exception e) { throw new IllegalStateException(Thread.currentThread().getName(), e); }