X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=netconf%2Fnetconf-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Fimpl%2FConcurrentClientsTest.java;h=1a4e4c319eb047fbaf4b79bfb9fa9bf89d323de6;hb=refs%2Fchanges%2F60%2F84160%2F6;hp=c49b3ad9783a92af545b16843500360b12c290a5;hpb=3d01a690f3d26dbd2bec429415b026e501fcead5;p=netconf.git diff --git a/netconf/netconf-impl/src/test/java/org/opendaylight/netconf/impl/ConcurrentClientsTest.java b/netconf/netconf-impl/src/test/java/org/opendaylight/netconf/impl/ConcurrentClientsTest.java index c49b3ad978..1a4e4c319e 100644 --- a/netconf/netconf-impl/src/test/java/org/opendaylight/netconf/impl/ConcurrentClientsTest.java +++ b/netconf/netconf-impl/src/test/java/org/opendaylight/netconf/impl/ConcurrentClientsTest.java @@ -5,18 +5,16 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.netconf.impl; -import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Preconditions.checkState; +import static java.util.Objects.requireNonNull; import static org.junit.Assert.assertEquals; -import static org.mockito.Matchers.any; +import static org.mockito.ArgumentMatchers.any; 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; @@ -29,6 +27,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.net.InetSocketAddress; import java.net.Socket; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -47,16 +46,16 @@ import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; -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.netconf.api.DocumentedException; import org.opendaylight.netconf.api.NetconfMessage; +import org.opendaylight.netconf.api.capability.Capability; import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader; import org.opendaylight.netconf.api.monitoring.CapabilityListener; import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService; import org.opendaylight.netconf.api.monitoring.SessionEvent; import org.opendaylight.netconf.api.monitoring.SessionListener; import org.opendaylight.netconf.api.xml.XmlNetconfConstants; +import org.opendaylight.netconf.api.xml.XmlUtil; import org.opendaylight.netconf.client.NetconfClientDispatcher; import org.opendaylight.netconf.client.NetconfClientDispatcherImpl; import org.opendaylight.netconf.client.SimpleNetconfClientSessionListener; @@ -69,11 +68,10 @@ import org.opendaylight.netconf.mapping.api.NetconfOperation; import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution; import org.opendaylight.netconf.mapping.api.NetconfOperationService; import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory; +import org.opendaylight.netconf.nettyutil.NeverReconnectStrategy; import org.opendaylight.netconf.nettyutil.handler.exi.NetconfStartExiMessage; import org.opendaylight.netconf.util.messages.NetconfMessageUtil; import org.opendaylight.netconf.util.test.XmlFileLoader; -import org.opendaylight.protocol.framework.NeverReconnectStrategy; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.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; @@ -88,11 +86,12 @@ public class ConcurrentClientsTest { private static final int CONCURRENCY = 32; private static final InetSocketAddress NETCONF_ADDRESS = new InetSocketAddress("127.0.0.1", 8303); - private int nettyThreads; - private Class clientRunnable; - private Set serverCaps; + private final int nettyThreads; + private final Class clientRunnable; + private final Set serverCaps; - public ConcurrentClientsTest(int nettyThreads, Class clientRunnable, Set serverCaps) { + public ConcurrentClientsTest(final int nettyThreads, final Class clientRunnable, + final Set serverCaps) { this.nettyThreads = nettyThreads; this.clientRunnable = clientRunnable; this.serverCaps = serverCaps; @@ -124,14 +123,11 @@ public class ConcurrentClientsTest { doNothing().when(sessionListener).onSessionUp(any(NetconfServerSession.class)); doNothing().when(sessionListener).onSessionDown(any(NetconfServerSession.class)); doNothing().when(sessionListener).onSessionEvent(any(SessionEvent.class)); - doReturn(new AutoCloseable() { - @Override - public void close() throws Exception { + doReturn((AutoCloseable) () -> { - } }).when(monitoring).registerCapabilitiesListener(any(NetconfMonitoringService.CapabilitiesListener.class)); doReturn(sessionListener).when(monitoring).getSessionListener(); - doReturn(new CapabilitiesBuilder().setCapability(Collections.emptyList()).build()).when(monitoring) + doReturn(new CapabilitiesBuilder().setCapability(Collections.emptyList()).build()).when(monitoring) .getCapabilities(); return monitoring; } @@ -175,8 +171,8 @@ public class ConcurrentClientsTest { .setBaseCapabilities(serverCaps) .build(); - NetconfServerDispatcherImpl.ServerChannelInitializer serverChannelInitializer = - new NetconfServerDispatcherImpl.ServerChannelInitializer(serverNegotiatorFactory); + ServerChannelInitializer serverChannelInitializer = + new ServerChannelInitializer(serverNegotiatorFactory); final NetconfServerDispatcherImpl dispatch = new NetconfServerDispatcherImpl(serverChannelInitializer, nettyGroup, nettyGroup); @@ -202,7 +198,7 @@ public class ConcurrentClientsTest { @Test(timeout = CONCURRENCY * 1000) public void testConcurrentClients() throws Exception { - List> futures = Lists.newArrayListWithCapacity(CONCURRENCY); + List> futures = new ArrayList<>(CONCURRENCY); for (int i = 0; i < CONCURRENCY; i++) { futures.add(clientExecutor.submit(getInstanceOfClientRunnable())); @@ -248,7 +244,7 @@ public class ConcurrentClientsTest { private final AtomicLong counter = new AtomicLong(); @Override - public HandlingPriority canHandle(Document message) { + public HandlingPriority canHandle(final Document message) { return XmlUtil.toString(message).contains(NetconfStartExiMessage.START_EXI) ? HandlingPriority.CANNOT_HANDLE : HandlingPriority.HANDLE_WITH_MAX_PRIORITY; @@ -256,8 +252,8 @@ public class ConcurrentClientsTest { @SuppressWarnings("checkstyle:IllegalCatch") @Override - public Document handle(Document requestMessage, NetconfOperationChainedExecution subsequentOperation) - throws DocumentedException { + public Document handle(final Document requestMessage, + final NetconfOperationChainedExecution subsequentOperation) throws DocumentedException { try { LOG.info("Handling netconf message from test {}", XmlUtil.toString(requestMessage)); counter.getAndIncrement(); @@ -289,15 +285,12 @@ public class ConcurrentClientsTest { @Override public AutoCloseable registerCapabilityListener(final CapabilityListener listener) { - return new AutoCloseable() { - @Override - public void close() throws Exception { - } + return () -> { }; } @Override - public NetconfOperationService createService(String netconfSessionIdForReporting) { + public NetconfOperationService createService(final String netconfSessionIdForReporting) { return new NetconfOperationService() { @Override @@ -328,17 +321,17 @@ public class ConcurrentClientsTest { } private void run2() throws Exception { - InputStream clientHello = checkNotNull(XmlFileLoader - .getResourceAsStream("netconfMessages/client_hello.xml")); - final InputStream getConfig = - checkNotNull(XmlFileLoader.getResourceAsStream("netconfMessages/getConfig.xml")); + InputStream clientHello = requireNonNull(XmlFileLoader.getResourceAsStream( + "netconfMessages/client_hello.xml")); + final InputStream getConfig = requireNonNull(XmlFileLoader.getResourceAsStream( + "netconfMessages/getConfig.xml")); Socket clientSocket = new Socket(NETCONF_ADDRESS.getHostString(), NETCONF_ADDRESS.getPort()); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); InputStreamReader inFromServer = new InputStreamReader(clientSocket.getInputStream()); - StringBuffer sb = new StringBuffer(); - while (sb.toString().endsWith("]]>]]>") == false) { + StringBuilder sb = new StringBuilder(); + while (!sb.toString().endsWith("]]>]]>")) { sb.append((char) inFromServer.read()); } LOG.info(sb.toString()); @@ -351,8 +344,8 @@ public class ConcurrentClientsTest { outToServer.write("]]>]]>".getBytes()); outToServer.flush(); Thread.sleep(100); - sb = new StringBuffer(); - while (sb.toString().endsWith("]]>]]>") == false) { + sb = new StringBuilder(); + while (!sb.toString().endsWith("]]>]]>")) { sb.append((char) inFromServer.read()); } LOG.info(sb.toString()); @@ -380,7 +373,7 @@ public class ConcurrentClientsTest { NetconfMessage result = netconfClient.sendRequest(getMessage).get(); LOG.info("Client with session id {}: got result {}", sessionId, result); - Preconditions.checkState(NetconfMessageUtil.isErrorMessage(result) == false, + checkState(NetconfMessageUtil.isErrorMessage(result) == false, "Received error response: " + XmlUtil.toString(result.getDocument()) + " to request: " + XmlUtil.toString(getMessage.getDocument()));