From: Kevin Wang Date: Tue, 2 Aug 2016 23:08:37 +0000 (-0700) Subject: BUG-5619 Enable maven parallel build for bgpcep I X-Git-Tag: release/carbon~215 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=83c5547e5e0b16b587bf1fc5daeedffdf77e6726;p=bgpcep.git BUG-5619 Enable maven parallel build for bgpcep I - This patch mainly change the unit test to use random IP address and port whenever initializing a client or server for testing Change-Id: I101706aded57f435c80285728f4212cfe0b5669b Signed-off-by: Kevin Wang --- diff --git a/bgp/bmp-mock/src/test/java/org/opendaylight/protocol/bmp/mock/BmpMockDispatcherTest.java b/bgp/bmp-mock/src/test/java/org/opendaylight/protocol/bmp/mock/BmpMockDispatcherTest.java index e2813fe624..5e960b4f99 100644 --- a/bgp/bmp-mock/src/test/java/org/opendaylight/protocol/bmp/mock/BmpMockDispatcherTest.java +++ b/bgp/bmp-mock/src/test/java/org/opendaylight/protocol/bmp/mock/BmpMockDispatcherTest.java @@ -24,6 +24,7 @@ import org.opendaylight.protocol.bmp.api.BmpSessionListenerFactory; import org.opendaylight.protocol.bmp.impl.BmpDispatcherImpl; import org.opendaylight.protocol.bmp.spi.registry.BmpMessageRegistry; import org.opendaylight.protocol.concepts.KeyMapping; +import org.opendaylight.protocol.util.InetSocketAddressUtil; public class BmpMockDispatcherTest { @@ -34,13 +35,14 @@ public class BmpMockDispatcherTest { @Test public void testCreateClient() throws InterruptedException { final BmpMockDispatcher dispatcher = new BmpMockDispatcher(this.registry, this.sessionFactory); - final int port = getRandomPort(); + final int port = InetSocketAddressUtil.getRandomPort(); + final InetSocketAddress serverAddr = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(port); final BmpDispatcherImpl serverDispatcher = new BmpDispatcherImpl(new NioEventLoopGroup(), new NioEventLoopGroup(), this.registry, this.sessionFactory); - final ChannelFuture futureServer = serverDispatcher.createServer(new InetSocketAddress(InetAddresses.forString("0.0.0.0"), port), this.slf, Optional.absent()); + final ChannelFuture futureServer = serverDispatcher.createServer(serverAddr, this.slf, Optional.absent()); waitFutureSuccess(futureServer); - final ChannelFuture channelFuture = dispatcher.createClient(new InetSocketAddress(InetAddresses.forString("127.0.0.2"), 0), - new InetSocketAddress(InetAddresses.forString("127.0.0.3"), port)); + final ChannelFuture channelFuture = dispatcher.createClient(InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(0), + serverAddr); waitFutureSuccess(channelFuture); final Channel channel = channelFuture.sync().channel(); @@ -52,12 +54,12 @@ public class BmpMockDispatcherTest { @Test public void testCreateServer() throws InterruptedException { final BmpMockDispatcher dispatcher = new BmpMockDispatcher(this.registry, this.sessionFactory); - final int port = getRandomPort(); + final int port = InetSocketAddressUtil.getRandomPort(); final BmpDispatcherImpl serverDispatcher = new BmpDispatcherImpl(new NioEventLoopGroup(), new NioEventLoopGroup(), this.registry, this.sessionFactory); final ChannelFuture futureServer = dispatcher.createServer(new InetSocketAddress(InetAddresses.forString("0.0.0.0"), port)); waitFutureSuccess(futureServer); - final ChannelFuture channelFuture = serverDispatcher.createClient(new InetSocketAddress(InetAddresses.forString("127.0.0.3"), port), this.slf, Optional.absent()); + final ChannelFuture channelFuture = serverDispatcher.createClient(InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(port), this.slf, Optional.absent()); waitFutureSuccess(channelFuture); final Channel channel = channelFuture.sync().channel(); @@ -66,8 +68,4 @@ public class BmpMockDispatcherTest { serverDispatcher.close(); } - protected static int getRandomPort() { - return (int) (Math.random() * 64000 + 1024); - } - } diff --git a/bgp/bmp-mock/src/test/java/org/opendaylight/protocol/bmp/mock/BmpMockSessionTest.java b/bgp/bmp-mock/src/test/java/org/opendaylight/protocol/bmp/mock/BmpMockSessionTest.java index cd78964705..5e6d42559a 100644 --- a/bgp/bmp-mock/src/test/java/org/opendaylight/protocol/bmp/mock/BmpMockSessionTest.java +++ b/bgp/bmp-mock/src/test/java/org/opendaylight/protocol/bmp/mock/BmpMockSessionTest.java @@ -26,6 +26,7 @@ import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; +import org.opendaylight.protocol.util.InetSocketAddressUtil; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.InitiationMessage; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.PeerUp; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.RouteMonitoringMessage; @@ -33,8 +34,8 @@ import org.opendaylight.yangtools.yang.binding.Notification; public class BmpMockSessionTest { - private static final InetSocketAddress REMOTE_ADDRESS = new InetSocketAddress(InetAddresses.forString("127.0.0.1"), 0); - private static final InetSocketAddress LOCAL_ADDRESS = new InetSocketAddress(InetAddresses.forString("127.0.0.2"), 0); + private static final InetSocketAddress REMOTE_ADDRESS = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(0); + private static final InetSocketAddress LOCAL_ADDRESS = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(0); private ChannelHandlerContext context; private Channel channel; diff --git a/bgp/bmp-mock/src/test/java/org/opendaylight/protocol/bmp/mock/BmpMockTest.java b/bgp/bmp-mock/src/test/java/org/opendaylight/protocol/bmp/mock/BmpMockTest.java index f127d41c9e..e401397432 100644 --- a/bgp/bmp-mock/src/test/java/org/opendaylight/protocol/bmp/mock/BmpMockTest.java +++ b/bgp/bmp-mock/src/test/java/org/opendaylight/protocol/bmp/mock/BmpMockTest.java @@ -32,12 +32,12 @@ import org.opendaylight.protocol.bmp.spi.registry.BmpExtensionProviderActivator; import org.opendaylight.protocol.bmp.spi.registry.BmpExtensionProviderContext; import org.opendaylight.protocol.bmp.spi.registry.SimpleBmpExtensionProviderContext; import org.opendaylight.protocol.concepts.KeyMapping; +import org.opendaylight.protocol.util.InetSocketAddressUtil; import org.opendaylight.yangtools.yang.binding.Notification; public class BmpMockTest { private final BmpSessionListener sessionListener = Mockito.mock(BmpSessionListener.class); - private int serverPort; private BmpExtensionProviderActivator bmpActivator; private BmpDispatcher bmpDispatcher; @@ -49,7 +49,6 @@ public class BmpMockTest { this.bmpActivator.start(ctx); this.bmpDispatcher = new BmpDispatcherImpl(new NioEventLoopGroup(), new NioEventLoopGroup(), ctx.getBmpMessageRegistry(), new DefaultBmpSessionFactory()); - this.serverPort = BmpMockDispatcherTest.getRandomPort(); } @After @@ -60,13 +59,14 @@ public class BmpMockTest { @Test public void testMain() throws Exception { + final InetSocketAddress serverAddr = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(); final BmpSessionListenerFactory bmpSessionListenerFactory = () -> BmpMockTest.this.sessionListener; - final ChannelFuture futureServer = bmpDispatcher.createServer(new InetSocketAddress("127.0.0.1", serverPort), + final ChannelFuture futureServer = bmpDispatcher.createServer(serverAddr, bmpSessionListenerFactory, Optional.absent()); waitFutureSuccess(futureServer); Channel serverChannel = futureServer.channel(); - BmpMock.main(new String[]{"--remote_address", "127.0.0.1:" + serverPort, "--peers_count", "3", "--pre_policy_routes", "3"}); + BmpMock.main(new String[]{"--remote_address", InetSocketAddressUtil.toHostAndPort(serverAddr).toString(), "--peers_count", "3", "--pre_policy_routes", "3"}); Thread.sleep(1000); Mockito.verify(this.sessionListener).onSessionUp(Mockito.any(BmpSession.class)); //1 * Initiate message + 3 * PeerUp Notification + 9 * Route Monitoring message @@ -77,11 +77,13 @@ public class BmpMockTest { @Test public void testMainInPassiveMode() throws Exception { + final InetSocketAddress serverAddr = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(); final BmpSessionListenerFactory bmpSessionListenerFactory = () -> BmpMockTest.this.sessionListener; // create a local server in passive mode instead - BmpMock.main(new String[]{"--local_address", "127.0.0.1:" + serverPort, "--peers_count", "3", "--pre_policy_routes", "3", "--passive"}); - final ChannelFuture futureServer = bmpDispatcher.createClient(new InetSocketAddress("127.0.0.1", serverPort), + BmpMock.main(new String[]{"--local_address", InetSocketAddressUtil.toHostAndPort(serverAddr).toString(), + "--peers_count", "3", "--pre_policy_routes", "3", "--passive"}); + final ChannelFuture futureServer = bmpDispatcher.createClient(serverAddr, bmpSessionListenerFactory, Optional.absent()); waitFutureSuccess(futureServer); Channel serverChannel = futureServer.channel(); diff --git a/bgp/rib-impl/pom.xml b/bgp/rib-impl/pom.xml index b013afd0d1..83d8c87590 100644 --- a/bgp/rib-impl/pom.xml +++ b/bgp/rib-impl/pom.xml @@ -66,6 +66,11 @@ test-jar test + + ${project.groupId} + testtool-util + test + com.google.code.findbugs jsr305 diff --git a/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/BGPDispatcherImplTest.java b/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/BGPDispatcherImplTest.java index 0d836f1a5a..96e80dc542 100755 --- a/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/BGPDispatcherImplTest.java +++ b/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/BGPDispatcherImplTest.java @@ -38,6 +38,7 @@ import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext; import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext; import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry; import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences; +import org.opendaylight.protocol.util.InetSocketAddressUtil; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address; @@ -105,7 +106,7 @@ public class BGPDispatcherImplTest { } private void configureClient(final BGPExtensionProviderContext ctx) { - final InetSocketAddress clientAddress = new InetSocketAddress("127.0.11.0", 1791); + final InetSocketAddress clientAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(); final IpAddress clientPeerIp = new IpAddress(new Ipv4Address(clientAddress.getAddress().getHostAddress())); this.registry.addPeer(clientPeerIp, this.clientListener, createPreferences(clientAddress)); this.clientDispatcher = new TestClientDispatcher(this.boss, this.worker, ctx.getMessageRegistry(), clientAddress); @@ -136,7 +137,7 @@ public class BGPDispatcherImplTest { @Test public void testCreateClient() throws InterruptedException, ExecutionException { - final InetSocketAddress serverAddress = new InetSocketAddress("127.0.10.0", 1790); + final InetSocketAddress serverAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(); final Channel serverChannel = createServer(serverAddress); Thread.sleep(1000); final Future futureClient = this.clientDispatcher.createClient(serverAddress, this.registry, 2, Optional.absent()); @@ -155,7 +156,7 @@ public class BGPDispatcherImplTest { @Test public void testCreateReconnectingClient() throws Exception { - final InetSocketAddress serverAddress = new InetSocketAddress("127.0.20.0", 1792); + final InetSocketAddress serverAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(); final Future future = this.clientDispatcher.createReconnectingClient(serverAddress, this.registry, RETRY_TIMER, Optional.absent()); waitFutureSuccess(future); final Channel serverChannel = createServer(serverAddress); diff --git a/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/TestClientDispatcher.java b/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/TestClientDispatcher.java index 0e294f0815..715fd6d274 100755 --- a/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/TestClientDispatcher.java +++ b/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/TestClientDispatcher.java @@ -24,12 +24,12 @@ import org.opendaylight.protocol.concepts.KeyMapping; public class TestClientDispatcher { private final BGPHandlerFactory hf; - private final InetSocketAddress defaulAddress; + private final InetSocketAddress defaultAddress; private InetSocketAddress localAddress; private final BGPDispatcherImpl disp; protected TestClientDispatcher(final EventLoopGroup bossGroup, final EventLoopGroup workerGroup, final MessageRegistry messageRegistry, - final InetSocketAddress locaAddress) { + final InetSocketAddress localAddress) { this.disp = new BGPDispatcherImpl(messageRegistry, bossGroup, workerGroup) { @Override protected Bootstrap createClientBootStrap(final Optional keys, final EventLoopGroup workerGroup) { @@ -46,14 +46,14 @@ public class TestClientDispatcher { if (bootstrap.group() == null) { bootstrap.group(workerGroup); } - bootstrap.localAddress(locaAddress); + bootstrap.localAddress(localAddress); bootstrap.option(ChannelOption.SO_REUSEADDR, true); return bootstrap; } }; this.hf = new BGPHandlerFactory(messageRegistry); - this.localAddress = locaAddress; - this.defaulAddress = locaAddress; + this.localAddress = localAddress; + this.defaultAddress = localAddress; } public synchronized Future createClient(final InetSocketAddress remoteAddress, @@ -72,7 +72,7 @@ public class TestClientDispatcher { if (localAddress.isPresent()) { this.localAddress = localAddress.get(); } else { - this.localAddress = this.defaulAddress; + this.localAddress = this.defaultAddress; } } } diff --git a/pcep/impl/pom.xml b/pcep/impl/pom.xml index 9417b48c13..5c9bfd9899 100644 --- a/pcep/impl/pom.xml +++ b/pcep/impl/pom.xml @@ -57,6 +57,11 @@ ${project.groupId} util + + ${project.groupId} + testtool-util + test + io.netty netty-buffer diff --git a/pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/AbstractPCEPSessionTest.java b/pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/AbstractPCEPSessionTest.java index 9b78a5b9ab..f42aca5f75 100644 --- a/pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/AbstractPCEPSessionTest.java +++ b/pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/AbstractPCEPSessionTest.java @@ -31,6 +31,7 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; +import org.opendaylight.protocol.util.InetSocketAddressUtil; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Keepalive; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.KeepaliveBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Open; @@ -44,7 +45,6 @@ import org.opendaylight.yangtools.yang.binding.Notification; public class AbstractPCEPSessionTest { - protected static final String IP_ADDRESS = "127.0.0.1"; protected static final short KEEP_ALIVE = 15; protected static final short DEADTIMER = 40; @@ -66,6 +66,8 @@ public class AbstractPCEPSessionTest { @Mock private SocketAddress address; + protected final String ipAddress = InetSocketAddressUtil.getRandomLoopbackIpAddress(); + protected final int port = InetSocketAddressUtil.getRandomPort(); protected final List msgsSend = Lists.newArrayList(); protected Open openMsg; @@ -101,8 +103,8 @@ public class AbstractPCEPSessionTest { doReturn(this.pipeline).when(this.pipeline).addFirst(any(ChannelHandler.class)); doReturn(true).when(this.channel).isActive(); doReturn(mock(ChannelFuture.class)).when(this.channel).close(); - doReturn(new InetSocketAddress(IP_ADDRESS, 4189)).when(this.channel).remoteAddress(); - doReturn(new InetSocketAddress(IP_ADDRESS, 4189)).when(this.channel).localAddress(); + doReturn(new InetSocketAddress(ipAddress, port)).when(this.channel).remoteAddress(); + doReturn(new InetSocketAddress(ipAddress, port)).when(this.channel).localAddress(); this.openMsg = new OpenBuilder().setOpenMessage( new OpenMessageBuilder().setOpen( new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder().setDeadTimer( diff --git a/pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPDispatcherImplTest.java b/pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPDispatcherImplTest.java index 0c1f69836d..7eb1a68d61 100755 --- a/pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPDispatcherImplTest.java +++ b/pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPDispatcherImplTest.java @@ -44,12 +44,9 @@ import org.opendaylight.protocol.pcep.PCEPSessionProposalFactory; import org.opendaylight.protocol.pcep.impl.PCEPDispatcherImpl.ChannelPipelineInitializer; import org.opendaylight.protocol.pcep.spi.MessageRegistry; import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext; +import org.opendaylight.protocol.util.InetSocketAddressUtil; public class PCEPDispatcherImplTest { - - private static final int PORT = 4189; - private static final InetSocketAddress CLIENT1_ADDRESS = new InetSocketAddress("127.0.0.10", PORT); - private static final InetSocketAddress CLIENT2_ADDRESS = new InetSocketAddress("127.0.0.11", PORT); private static final short DEAD_TIMER = 120; private static final short KEEP_ALIVE = 30; private static final int RETRY_TIMER = 0; @@ -89,22 +86,26 @@ public class PCEPDispatcherImplTest { @Test public void testCreateClientServer() throws InterruptedException, ExecutionException { - final ChannelFuture futureChannel = this.dispatcher.createServer(new InetSocketAddress("0.0.0.0", PORT), + final int port = InetSocketAddressUtil.getRandomPort(); + final InetSocketAddress serverAddr = new InetSocketAddress("0.0.0.0", port); + final InetSocketAddress clientAddr1 = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(port); + final InetSocketAddress clientAddr2 = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(port); + final ChannelFuture futureChannel = this.dispatcher.createServer(serverAddr, () -> new SimpleSessionListener(), null); - final PCEPSessionImpl session1 = (PCEPSessionImpl) this.pccMock.createClient(CLIENT1_ADDRESS, + final PCEPSessionImpl session1 = (PCEPSessionImpl) this.pccMock.createClient(clientAddr1, RETRY_TIMER, CONNECT_TIMEOUT, () -> new SimpleSessionListener()).get(); - final PCEPSessionImpl session2 = (PCEPSessionImpl) this.pccMock.createClient(CLIENT2_ADDRESS, + final PCEPSessionImpl session2 = (PCEPSessionImpl) this.pccMock.createClient(clientAddr2, RETRY_TIMER, CONNECT_TIMEOUT, () -> new SimpleSessionListener()).get(); Assert.assertTrue(futureChannel.channel().isActive()); - Assert.assertEquals(CLIENT1_ADDRESS.getAddress().getHostAddress(), session1.getPeerPref().getIpAddress()); + Assert.assertEquals(clientAddr1.getAddress().getHostAddress(), session1.getPeerPref().getIpAddress()); Assert.assertEquals(DEAD_TIMER, session1.getDeadTimerValue().shortValue()); Assert.assertEquals(KEEP_ALIVE, session1.getKeepAliveTimerValue().shortValue()); - Assert.assertEquals(CLIENT2_ADDRESS.getAddress().getHostAddress(), session2.getPeerPref().getIpAddress()); + Assert.assertEquals(clientAddr2.getAddress().getHostAddress(), session2.getPeerPref().getIpAddress()); Assert.assertEquals(DEAD_TIMER, session2.getDeadTimerValue().shortValue()); Assert.assertEquals(KEEP_ALIVE, session2.getKeepAliveTimerValue().shortValue()); @@ -115,14 +116,18 @@ public class PCEPDispatcherImplTest { @Test public void testCreateDuplicateClient() throws InterruptedException, ExecutionException { - this.dispatcher.createServer(new InetSocketAddress("0.0.0.0", PORT), + final int port = InetSocketAddressUtil.getRandomPort(); + final InetSocketAddress serverAddr = new InetSocketAddress("0.0.0.0", port); + final InetSocketAddress clientAddr = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(port); + this.dispatcher.createServer(serverAddr, () -> new SimpleSessionListener(), null); - final PCEPSessionImpl session1 = (PCEPSessionImpl) this.pccMock.createClient(CLIENT1_ADDRESS, + final PCEPSessionImpl session1 = (PCEPSessionImpl) this.pccMock.createClient(clientAddr, RETRY_TIMER, CONNECT_TIMEOUT, () -> new SimpleSessionListener()).get(); try { - this.pccMock.createClient(CLIENT1_ADDRESS, RETRY_TIMER, CONNECT_TIMEOUT, + this.pccMock.createClient(clientAddr, + RETRY_TIMER, CONNECT_TIMEOUT, () -> new SimpleSessionListener()).get(); Assert.fail(); } catch (final ExecutionException e) { @@ -134,22 +139,24 @@ public class PCEPDispatcherImplTest { @Test public void testReconectClient() throws InterruptedException, ExecutionException { - this.dispatcher.createServer(new InetSocketAddress("0.0.0.0", PORT), + final int port = InetSocketAddressUtil.getRandomPort(); + final InetSocketAddress clientAddr = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(port); + this.dispatcher.createServer(new InetSocketAddress("0.0.0.0", port), () -> new SimpleSessionListener(), null); - final PCEPSessionImpl session1 = (PCEPSessionImpl) this.pccMock.createClient(CLIENT1_ADDRESS, + final PCEPSessionImpl session1 = (PCEPSessionImpl) this.pccMock.createClient(clientAddr, RETRY_TIMER, CONNECT_TIMEOUT, () -> new SimpleSessionListener()).get(); - Assert.assertEquals(CLIENT1_ADDRESS.getAddress(), session1.getRemoteAddress()); + Assert.assertEquals(clientAddr.getAddress(), session1.getRemoteAddress()); Assert.assertEquals(DEAD_TIMER, session1.getDeadTimerValue().shortValue()); Assert.assertEquals(KEEP_ALIVE, session1.getKeepAliveTimerValue().shortValue()); session1.close(); - final PCEPSessionImpl session2 = (PCEPSessionImpl) this.pccMock.createClient(CLIENT1_ADDRESS, + final PCEPSessionImpl session2 = (PCEPSessionImpl) this.pccMock.createClient(clientAddr, RETRY_TIMER, CONNECT_TIMEOUT, () -> new SimpleSessionListener()).get(); - Assert.assertEquals(CLIENT1_ADDRESS.getAddress(), session1.getRemoteAddress()); + Assert.assertEquals(clientAddr.getAddress(), session1.getRemoteAddress()); Assert.assertEquals(DEAD_TIMER, session2.getDeadTimerValue().shortValue()); Assert.assertEquals(KEEP_ALIVE, session2.getKeepAliveTimerValue().shortValue()); @@ -158,10 +165,13 @@ public class PCEPDispatcherImplTest { @Test public void testCustomizeBootstrap() { - final KeyMapping keys = KeyMapping.getKeyMapping(CLIENT1_ADDRESS.getAddress(), new String("CLIENT1_ADDRESS")); - keys.put(CLIENT2_ADDRESS.getAddress(), new String("CLIENT2_ADDRESS").getBytes() ); + final int port = InetSocketAddressUtil.getRandomPort(); + final InetSocketAddress clientAddr1 = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(port); + final InetSocketAddress clientAddr2 = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(port); + final KeyMapping keys = KeyMapping.getKeyMapping(clientAddr1.getAddress(), new String("CLIENT1_ADDRESS")); + keys.put(clientAddr2.getAddress(), new String("CLIENT2_ADDRESS").getBytes() ); - final ChannelFuture futureChannel = this.disp2Spy.createServer(new InetSocketAddress("0.0.0.0", PORT), + final ChannelFuture futureChannel = this.disp2Spy.createServer(new InetSocketAddress("0.0.0.0", port), () -> new SimpleSessionListener(), null); Mockito.verify(this.disp2Spy).createServerBootstrap(Mockito.any(PCEPDispatcherImpl.ChannelPipelineInitializer.class)); } diff --git a/pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPSessionImplTest.java b/pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPSessionImplTest.java index 3b1d63a325..29677fd23b 100644 --- a/pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPSessionImplTest.java +++ b/pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPSessionImplTest.java @@ -112,14 +112,14 @@ public class PCEPSessionImplTest extends AbstractPCEPSessionTest { @Test public void testSessionStatistics() { this.session.handleMessage(Util.createErrorMessage(PCEPErrors.LSP_RSVP_ERROR, null)); - Assert.assertEquals(IP_ADDRESS, this.session.getPeerPref().getIpAddress()); + Assert.assertEquals(ipAddress, this.session.getPeerPref().getIpAddress()); final PeerPref peerPref = this.session.getPeerPref(); - Assert.assertEquals(IP_ADDRESS, peerPref.getIpAddress()); + Assert.assertEquals(ipAddress, peerPref.getIpAddress()); Assert.assertEquals(DEADTIMER, peerPref.getDeadtimer().shortValue()); Assert.assertEquals(KEEP_ALIVE, peerPref.getKeepalive().shortValue()); Assert.assertEquals(0, peerPref.getSessionId().intValue()); final LocalPref localPref = this.session.getLocalPref(); - Assert.assertEquals(IP_ADDRESS, localPref.getIpAddress()); + Assert.assertEquals(ipAddress, localPref.getIpAddress()); Assert.assertEquals(DEADTIMER, localPref.getDeadtimer().shortValue()); Assert.assertEquals(KEEP_ALIVE, localPref.getKeepalive().shortValue()); Assert.assertEquals(0, localPref.getSessionId().intValue()); diff --git a/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCDispatcherImplTest.java b/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCDispatcherImplTest.java index fb8265ddcb..1539456fb8 100644 --- a/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCDispatcherImplTest.java +++ b/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCDispatcherImplTest.java @@ -20,7 +20,6 @@ import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.Random; import java.util.concurrent.ExecutionException; import org.junit.After; import org.junit.Assert; @@ -37,13 +36,13 @@ import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiatorFactory; import org.opendaylight.protocol.pcep.impl.PCEPDispatcherImpl; import org.opendaylight.protocol.pcep.pcc.mock.protocol.PCCDispatcherImpl; import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext; +import org.opendaylight.protocol.util.InetSocketAddressUtil; public class PCCDispatcherImplTest { private static final List CAPS = new ArrayList<>(); private static final PCEPSessionProposalFactory PROPOSAL = new BasePCEPSessionProposalFactory(30, 120, CAPS); private final DefaultPCEPSessionNegotiatorFactory nf = new DefaultPCEPSessionNegotiatorFactory(PROPOSAL, 0); - private final Random random = new Random(); private PCCDispatcherImpl dispatcher; private PCEPDispatcher pcepDispatcher; private InetSocketAddress serverAddress; @@ -58,8 +57,8 @@ public class PCCDispatcherImplTest { this.dispatcher = new PCCDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry()); this.pcepDispatcher = new PCEPDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry(), this.nf, this.bossGroup, this.workerGroup); - this.serverAddress = new InetSocketAddress("127.0.5.0", getRandomPort()); - this.clientAddress = new InetSocketAddress("127.0.4.0", getRandomPort()); + this.serverAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(); + this.clientAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(0); } @After @@ -83,7 +82,7 @@ public class PCCDispatcherImplTest { waitFutureSuccess(futureServer); final Channel channel = futureServer.channel(); Assert.assertNotNull(futureSession.get()); - checkSessionListenerNotNull(slf, "127.0.4.0"); + checkSessionListenerNotNull(slf, this.clientAddress.getHostString()); final TestingSessionListener sl = checkSessionListenerNotNull(slf, this.clientAddress.getAddress().getHostAddress()); Assert.assertNotNull(sl.getSession()); Assert.assertTrue(sl.isUp()); @@ -103,8 +102,4 @@ public class PCCDispatcherImplTest { Assert.assertNotNull(sl2.getSession()); Assert.assertTrue(sl2.isUp()); } - - private int getRandomPort() { - return this.random.nextInt(4000) + 1024; - } } diff --git a/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCIncrementalSyncTest.java b/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCIncrementalSyncTest.java index ed0e6d1447..1fc61fda37 100644 --- a/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCIncrementalSyncTest.java +++ b/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCIncrementalSyncTest.java @@ -21,6 +21,7 @@ import org.junit.Test; import org.opendaylight.protocol.pcep.PCEPCapability; import org.opendaylight.protocol.pcep.ietf.stateful07.PCEPStatefulCapability; import org.opendaylight.protocol.pcep.pcc.mock.protocol.PCCServerPeerProposal; +import org.opendaylight.protocol.util.InetSocketAddressUtil; public class PCCIncrementalSyncTest extends PCCMockCommon { @@ -30,15 +31,15 @@ public class PCCIncrementalSyncTest extends PCCMockCommon { * Create 8 lsp, then it disconnects after 5 sec and then after 5 sec reconnects with Pcc DBVersion 10 * After reconnection PCE has DBVersion 10, therefore there is 9 changes missed. 9 Pcrt + 1 Pcrt-Sync */ - private final String[] mainInputIncrementalSync = new String[]{"--local-address", PCCMockTest.LOCAL_ADDRESS, "--remote-address", - PCCMockTest.REMOTE_ADDRESS + ":4578", "--pcc", "1", "--lsp", lsp.toString(), "--log-level", "DEBUG", "-ka", "40", "-d", "120", + private final String[] mainInputIncrementalSync = new String[]{"--local-address", this.localAddress.getHostString(), "--remote-address", + InetSocketAddressUtil.toHostAndPort(this.remoteAddress).toString(), "--pcc", "1", "--lsp", lsp.toString(), "--log-level", "DEBUG", "-ka", "40", "-d", "120", "--reconnect", "-1", "--redelegation-timeout", "0", "--state-timeout", "-1", "--incremental-sync-procedure", "10", "5", "5"}; @Test public void testSessionIncrementalSyncEstablishment() throws UnknownHostException, InterruptedException, ExecutionException { final TestingSessionListenerFactory factory = new TestingSessionListenerFactory(); final BigInteger numberOflspAndDBv = BigInteger.valueOf(8); - final Channel channel = createServer(factory, socket, new PCCServerPeerProposal(numberOflspAndDBv)); + final Channel channel = createServer(factory, this.remoteAddress, new PCCServerPeerProposal(numberOflspAndDBv)); Main.main(mainInputIncrementalSync); Thread.sleep(1000); final TestingSessionListener pceSessionListener = getListener(factory); @@ -58,9 +59,4 @@ public class PCCIncrementalSyncTest extends PCCMockCommon { caps.add(new PCEPStatefulCapability(true, true, true, false, false, true, true)); return caps; } - - @Override - protected int getPort() { - return 4578; - } } diff --git a/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCMockCommon.java b/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCMockCommon.java index 2ba820d463..0bb6afea32 100644 --- a/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCMockCommon.java +++ b/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCMockCommon.java @@ -26,7 +26,6 @@ import io.netty.util.concurrent.Future; import java.math.BigInteger; import java.net.InetSocketAddress; import java.util.List; -import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import org.junit.Assert; import org.junit.Before; @@ -48,6 +47,7 @@ import org.opendaylight.protocol.pcep.pcc.mock.protocol.PCCSessionListener; import org.opendaylight.protocol.pcep.spi.PCEPExtensionProviderContext; import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext; import org.opendaylight.protocol.pcep.sync.optimizations.SyncOptimizationsActivator; +import org.opendaylight.protocol.util.InetSocketAddressUtil; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev150714.Tlvs3; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Stateful1; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Pcrpt; @@ -57,19 +57,17 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.iet import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message; public abstract class PCCMockCommon { - protected static final String REMOTE_ADDRESS = "127.0.1.0"; - protected static final String LOCAL_ADDRESS = "127.0.0.1"; private final static short KEEP_ALIVE = 40; private final static short DEAD_TIMER = 120; - protected final InetSocketAddress socket = new InetSocketAddress(PCCMockCommon.REMOTE_ADDRESS, getPort()); + protected final int port = InetSocketAddressUtil.getRandomPort(); + protected final InetSocketAddress remoteAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(port); + protected final InetSocketAddress localAddress = new InetSocketAddress("127.0.0.1", port); protected PCCSessionListener pccSessionListener; private PCEPDispatcher pceDispatcher; private PCCDispatcherImpl pccDispatcher; protected abstract List getCapabilities(); - protected abstract int getPort(); - @Before public void setUp() { final BasePCEPSessionProposalFactory proposal = new BasePCEPSessionProposalFactory(DEAD_TIMER, KEEP_ALIVE, getCapabilities()); @@ -200,25 +198,17 @@ public abstract class PCCMockCommon { protected Future createPCCSession(BigInteger DBVersion) { this.pccDispatcher = new PCCDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry()); final PCEPSessionNegotiatorFactory snf = getSessionNegotiatorFactory(); - final PCCTunnelManager tunnelManager = new PCCTunnelManagerImpl(3, getLocalAdress().getAddress(), 0, -1, new HashedWheelTimer(), + final PCCTunnelManager tunnelManager = new PCCTunnelManagerImpl(3, this.localAddress.getAddress(), 0, -1, new HashedWheelTimer(), Optional.absent()); - return pccDispatcher.createClient(getRemoteAdress(), -1, + return pccDispatcher.createClient(this.remoteAddress, -1, new PCEPSessionListenerFactory() { @Override public PCEPSessionListener getSessionListener() { pccSessionListener = new PCCSessionListener(1, tunnelManager, false); return pccSessionListener; } - }, snf, null, getLocalAdress(), DBVersion); - } - - private InetSocketAddress getLocalAdress() { - return new InetSocketAddress(PCCMockTest.LOCAL_ADDRESS, getPort()); - } - - private InetSocketAddress getRemoteAdress() { - return new InetSocketAddress(PCCMockTest.REMOTE_ADDRESS, getPort()); + }, snf, null, this.localAddress, DBVersion); } private PCEPSessionNegotiatorFactory getSessionNegotiatorFactory() { @@ -226,6 +216,6 @@ public abstract class PCCMockCommon { } protected TestingSessionListener getListener(final TestingSessionListenerFactory factory) { - return checkSessionListenerNotNull(factory, PCCMockTest.LOCAL_ADDRESS); + return checkSessionListenerNotNull(factory, this.localAddress.getHostString()); } } diff --git a/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCMockTest.java b/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCMockTest.java index 39362d8e57..cb989257dc 100644 --- a/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCMockTest.java +++ b/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCMockTest.java @@ -12,60 +12,61 @@ import io.netty.channel.Channel; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.net.InetSocketAddress; -import java.net.UnknownHostException; import java.util.Collections; import java.util.List; -import java.util.concurrent.ExecutionException; import org.junit.Test; import org.opendaylight.protocol.pcep.PCEPCapability; import org.opendaylight.protocol.pcep.pcc.mock.spi.MsgBuilderUtil; +import org.opendaylight.protocol.util.InetSocketAddressUtil; -public class PCCMockTest extends PCCMockCommon { - private static final String REMOTE_ADDRESS2 = "127.0.2.0"; - private static final String REMOTE_ADDRESS3 = "127.0.3.0"; - private static final String REMOTE_ADDRESS4 = "127.0.4.0"; - private static final String LOCAL_ADDRESS2 = "127.0.0.2"; - private static final InetSocketAddress SERVER_ADDRESS2 = new InetSocketAddress(REMOTE_ADDRESS2, 4189); - private static final InetSocketAddress SERVER_ADDRESS3 = new InetSocketAddress(REMOTE_ADDRESS3, 4189); - private static final InetSocketAddress SERVER_ADDRESS4 = new InetSocketAddress(REMOTE_ADDRESS4, 4189); - private final String[] mainInput = new String[]{"--local-address", PCCMockCommon.LOCAL_ADDRESS, "--remote-address", - PCCMockCommon.REMOTE_ADDRESS + ":4560", "--pcc", "1", "--lsp", "3", "--log-level", "DEBUG", "-ka", "10", "-d", "40", "--reconnect", "-1", +public final class PCCMockTest extends PCCMockCommon { + private final String[] mainInput = new String[] {"--local-address", this.localAddress.getHostString(), "--remote-address", + InetSocketAddressUtil.toHostAndPort(this.remoteAddress).toString(), "--pcc", "1", "--lsp", "3", "--log-level", "DEBUG", "-ka", "10", "-d", "40", "--reconnect", "-1", "--redelegation-timeout", "0", "--state-timeout", "-1"}; @Test public void testSessionEstablishment() throws Exception { final TestingSessionListenerFactory factory = new TestingSessionListenerFactory(); - final Channel channel = createServer(factory, this.socket); + final Channel channel = createServer(factory, this.remoteAddress); Main.main(mainInput); Thread.sleep(1000); //3 reported LSPs + syc final int numMessages = 4; - final TestingSessionListener sessionListener = checkSessionListener(numMessages, channel, factory, PCCMockCommon.LOCAL_ADDRESS); + final TestingSessionListener sessionListener = checkSessionListener(numMessages, channel, factory, this.localAddress.getHostString()); checkSession(sessionListener.getSession(), 40, 10); } @Test public void testMockPCCToManyPCE() throws Exception { + final String localAddress2 = "127.0.0.2"; + final InetSocketAddress serverAddress2 = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(); + final InetSocketAddress serverAddress3 = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(); + final InetSocketAddress serverAddress4 = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(); + final TestingSessionListenerFactory factory = new TestingSessionListenerFactory(); final TestingSessionListenerFactory factory2 = new TestingSessionListenerFactory(); final TestingSessionListenerFactory factory3 = new TestingSessionListenerFactory(); - final Channel channel = createServer(factory, SERVER_ADDRESS2); - final Channel channel2 = createServer(factory2, SERVER_ADDRESS3); - final Channel channel3 = createServer(factory3, SERVER_ADDRESS4); + final Channel channel = createServer(factory, serverAddress2); + final Channel channel2 = createServer(factory2, serverAddress3); + final Channel channel3 = createServer(factory3, serverAddress4); - Main.main(new String[]{"--local-address", PCCMockCommon.LOCAL_ADDRESS, "--remote-address", REMOTE_ADDRESS2 + "," + REMOTE_ADDRESS3 + "," + REMOTE_ADDRESS4, "--pcc", "2"}); + Main.main(new String[] {"--local-address", this.localAddress.getHostString(), "--remote-address", + InetSocketAddressUtil.toHostAndPort(serverAddress2).toString() + "," + + InetSocketAddressUtil.toHostAndPort(serverAddress3).toString() + "," + + InetSocketAddressUtil.toHostAndPort(serverAddress4).toString(), + "--pcc", "2"}); Thread.sleep(1000); //PCE1 int numMessages = 2; - checkSessionListener(numMessages, channel, factory, PCCMockCommon.LOCAL_ADDRESS); - checkSessionListener(numMessages, channel, factory, LOCAL_ADDRESS2); + checkSessionListener(numMessages, channel, factory, this.localAddress.getHostString()); + checkSessionListener(numMessages, channel, factory, localAddress2); //PCE2 - checkSessionListener(numMessages, channel2, factory2, LOCAL_ADDRESS); - checkSessionListener(numMessages, channel2, factory2, LOCAL_ADDRESS2); + checkSessionListener(numMessages, channel2, factory2, this.localAddress.getHostString()); + checkSessionListener(numMessages, channel2, factory2, localAddress2); //PCE3 - checkSessionListener(numMessages, channel3, factory3, PCCMockCommon.LOCAL_ADDRESS); - checkSessionListener(numMessages, channel3, factory3, LOCAL_ADDRESS2); + checkSessionListener(numMessages, channel3, factory3, this.localAddress.getHostString()); + checkSessionListener(numMessages, channel3, factory3, localAddress2); } @Test(expected = UnsupportedOperationException.class) @@ -83,9 +84,4 @@ public class PCCMockTest extends PCCMockCommon { protected List getCapabilities() { return Collections.emptyList(); } - - @Override - protected int getPort() { - return 4560; - } } diff --git a/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCSyncAvoidanceProcedureTest.java b/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCSyncAvoidanceProcedureTest.java index 56574b5e7a..18bb21ebd4 100644 --- a/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCSyncAvoidanceProcedureTest.java +++ b/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCSyncAvoidanceProcedureTest.java @@ -28,7 +28,7 @@ public class PCCSyncAvoidanceProcedureTest extends PCCMockCommon { public void testSessionAvoidanceDesynchronizedEstablishment() throws UnknownHostException, InterruptedException, ExecutionException { final TestingSessionListenerFactory factory = new TestingSessionListenerFactory(); - final Channel channel = createServer(factory, socket, new PCCPeerProposal()); + final Channel channel = createServer(factory, this.remoteAddress, new PCCPeerProposal()); PCEPSession session = createPCCSession(BigInteger.TEN).get(); assertNotNull(session); final TestingSessionListener pceSessionListener = getListener(factory); @@ -44,9 +44,4 @@ public class PCCSyncAvoidanceProcedureTest extends PCCMockCommon { caps.add(new PCEPStatefulCapability(true, true, true, false, false, false, true)); return caps; } - - @Override - protected int getPort() { - return 4567; - } } diff --git a/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCTriggeredFullDBResyncTest.java b/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCTriggeredFullDBResyncTest.java index 87739b0cc6..df48b5eb95 100644 --- a/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCTriggeredFullDBResyncTest.java +++ b/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCTriggeredFullDBResyncTest.java @@ -41,7 +41,7 @@ public class PCCTriggeredFullDBResyncTest extends PCCMockCommon { final TestingSessionListenerFactory factory = new TestingSessionListenerFactory(); final int lspQuantity = 3; final BigInteger numberOflspAndDBv = BigInteger.valueOf(lspQuantity); - this.channel = createServer(factory, socket, new PCCPeerProposal()); + this.channel = createServer(factory, this.remoteAddress, new PCCPeerProposal()); PCEPSession session = createPCCSession(numberOflspAndDBv).get(); assertNotNull(session); final TestingSessionListener pceSessionListener = getListener(factory); @@ -76,9 +76,4 @@ public class PCCTriggeredFullDBResyncTest extends PCCMockCommon { caps.add(new PCEPStatefulCapability(true, true, true, false, true, false, true)); return caps; } - - @Override - protected int getPort() { - return 4566; - } } diff --git a/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCTriggeredLspResyncTest.java b/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCTriggeredLspResyncTest.java index 5084bab344..ed7bb6c231 100644 --- a/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCTriggeredLspResyncTest.java +++ b/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCTriggeredLspResyncTest.java @@ -42,7 +42,7 @@ public class PCCTriggeredLspResyncTest extends PCCMockCommon { final int lspQuantity = 3; final BigInteger numberOflspAndDBv = BigInteger.valueOf(lspQuantity); - this.channel = createServer(factory, socket, new PCCPeerProposal()); + this.channel = createServer(factory, this.remoteAddress, new PCCPeerProposal()); PCEPSession session = createPCCSession(numberOflspAndDBv).get(); assertNotNull(session); final TestingSessionListener pceSessionListener = getListener(factory); @@ -77,9 +77,4 @@ public class PCCTriggeredLspResyncTest extends PCCMockCommon { caps.add(new PCEPStatefulCapability(true, true, true, false, true, false, true)); return caps; } - - @Override - protected int getPort() { - return 4584; - } } diff --git a/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCTriggeredSyncTest.java b/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCTriggeredSyncTest.java index 5f3072a871..d129313db7 100644 --- a/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCTriggeredSyncTest.java +++ b/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCTriggeredSyncTest.java @@ -39,7 +39,7 @@ public class PCCTriggeredSyncTest extends PCCMockCommon { @Test public void testSessionTriggeredSync() throws Exception { final TestingSessionListenerFactory factory = new TestingSessionListenerFactory(); - this.channel = createServer(factory, socket, new PCCPeerProposal()); + this.channel = createServer(factory, this.remoteAddress, new PCCPeerProposal()); final BigInteger numberOflspAndDBv = BigInteger.valueOf(3); PCEPSession session = createPCCSession(numberOflspAndDBv).get(); assertNotNull(session); @@ -77,10 +77,5 @@ public class PCCTriggeredSyncTest extends PCCMockCommon { caps.add(new PCEPStatefulCapability(true, true, true, true, false, false, true)); return caps; } - - @Override - protected int getPort() { - return 4582; - } } diff --git a/pcep/segment-routing/pom.xml b/pcep/segment-routing/pom.xml index f7010a7bfc..373b3e8f8f 100644 --- a/pcep/segment-routing/pom.xml +++ b/pcep/segment-routing/pom.xml @@ -130,6 +130,11 @@ test-jar test + + ${project.groupId} + testtool-util + test + org.opendaylight.controller sal-binding-broker-impl diff --git a/pcep/testtool/src/test/java/org/opendaylight/protocol/pcep/testtool/PCCMock.java b/pcep/testtool/src/test/java/org/opendaylight/protocol/pcep/testtool/PCCMock.java index c9c8057651..50bd8c09f1 100644 --- a/pcep/testtool/src/test/java/org/opendaylight/protocol/pcep/testtool/PCCMock.java +++ b/pcep/testtool/src/test/java/org/opendaylight/protocol/pcep/testtool/PCCMock.java @@ -7,6 +7,8 @@ */ package org.opendaylight.protocol.pcep.testtool; +import com.google.common.base.Preconditions; +import com.google.common.net.HostAndPort; import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.List; @@ -20,21 +22,27 @@ import org.opendaylight.protocol.pcep.impl.BasePCEPSessionProposalFactory; import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiatorFactory; import org.opendaylight.protocol.pcep.pcc.mock.protocol.PCCDispatcherImpl; import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext; +import org.opendaylight.protocol.util.InetSocketAddressUtil; public class PCCMock { public static void main(final String[] args) throws InterruptedException, ExecutionException { + Preconditions.checkArgument(args.length > 0, "Host and port of server must be provided."); final List caps = new ArrayList<>(); final PCEPSessionProposalFactory proposal = new BasePCEPSessionProposalFactory((short) 120, (short) 30, caps); final PCEPSessionNegotiatorFactory snf = new DefaultPCEPSessionNegotiatorFactory(proposal, 0); + final HostAndPort serverHostAndPort = HostAndPort.fromString(args[0]); + final InetSocketAddress serverAddr = new InetSocketAddress(serverHostAndPort.getHostText(), serverHostAndPort + .getPortOrDefault(12345)); + final InetSocketAddress clientAddr = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(0); try (final PCCDispatcherImpl pccDispatcher = new PCCDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry())) { - pccDispatcher.createClient(new InetSocketAddress("127.0.0.3", 12345), -1, new PCEPSessionListenerFactory() { + pccDispatcher.createClient(serverAddr, -1, new PCEPSessionListenerFactory() { @Override public PCEPSessionListener getSessionListener() { return new SimpleSessionListener(); } - }, snf, null, new InetSocketAddress("127.0.0.1", 12345)).get(); + }, snf, null, clientAddr).get(); } } } diff --git a/pcep/testtool/src/test/java/org/opendaylight/protocol/pcep/testtool/PCEPTestingToolTest.java b/pcep/testtool/src/test/java/org/opendaylight/protocol/pcep/testtool/PCEPTestingToolTest.java index 15a8651bae..d2d2099960 100644 --- a/pcep/testtool/src/test/java/org/opendaylight/protocol/pcep/testtool/PCEPTestingToolTest.java +++ b/pcep/testtool/src/test/java/org/opendaylight/protocol/pcep/testtool/PCEPTestingToolTest.java @@ -11,8 +11,8 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import org.junit.Assert; import org.junit.Test; +import org.opendaylight.protocol.util.InetSocketAddressUtil; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.KeepaliveBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.KeepaliveMessage; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.keepalive.message.KeepaliveMessageBuilder; @@ -20,13 +20,12 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.typ public class PCEPTestingToolTest { @Test - public void testSessionEstablishment() { - try { - Main.main(new String[]{"-a", "127.0.0.3:12345", "-ka", "10", "-d", "0", "--stateful", "--active", "--instant"}); - PCCMock.main(new String[0]); - } catch (final Exception e) { - Assert.fail(); - } + public void testSessionEstablishment() throws Exception { + final String serverAddr = InetSocketAddressUtil + .toHostAndPort(InetSocketAddressUtil.getRandomLoopbackInetSocketAddress()).toString(); + Main.main(new String[] {"-a", serverAddr, + "-ka", "10", "-d", "0", "--stateful", "--active", "--instant"}); + PCCMock.main(new String[] {serverAddr}); } @Test diff --git a/pcep/topology-provider/src/test/java/org/opendaylight/bgpcep/pcep/topology/provider/AbstractPCEPSessionTest.java b/pcep/topology-provider/src/test/java/org/opendaylight/bgpcep/pcep/topology/provider/AbstractPCEPSessionTest.java index b72da6a090..7f9fb8f077 100644 --- a/pcep/topology-provider/src/test/java/org/opendaylight/bgpcep/pcep/topology/provider/AbstractPCEPSessionTest.java +++ b/pcep/topology-provider/src/test/java/org/opendaylight/bgpcep/pcep/topology/provider/AbstractPCEPSessionTest.java @@ -43,6 +43,7 @@ import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFaile import org.opendaylight.protocol.pcep.PCEPSession; import org.opendaylight.protocol.pcep.PCEPSessionListener; import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiator; +import org.opendaylight.protocol.util.InetSocketAddressUtil; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Ero; @@ -68,17 +69,17 @@ public abstract class AbstractPCEPSessionTest TOPO_IID = InstanceIdentifier.builder(NetworkTopology.class).child( Topology.class, new TopologyKey(new TopologyId(TEST_TOPOLOGY_NAME))).build(); - protected static final String TEST_ADDRESS = "127.0.0.1"; - protected static final NodeId NODE_ID = new NodeId("pcc://" + TEST_ADDRESS); - protected static final String TEST_LSP_NAME = "tunnel0"; protected static final String IPV4_MASK = "/32"; - protected static final String ERO_IP_PREFIX = TEST_ADDRESS + IPV4_MASK; - protected static final String NEW_DESTINATION_ADDRESS = "127.0.1.0"; - protected static final String DST_IP_PREFIX = NEW_DESTINATION_ADDRESS + IPV4_MASK; protected static final short DEAD_TIMER = 30; protected static final short KEEP_ALIVE = 10; protected static final int RPC_TIMEOUT = 4; + protected final String testAddress = InetSocketAddressUtil.getRandomLoopbackIpAddress(); + protected final NodeId nodeId = new NodeId("pcc://" + testAddress); + protected final String eroIpPrefix = testAddress + IPV4_MASK; + protected final String newDestinationAddress = InetSocketAddressUtil.getRandomLoopbackIpAddress(); + protected final String dstIpPrefix = newDestinationAddress + IPV4_MASK; + protected List receivedMsgs; @Mock @@ -124,9 +125,9 @@ public abstract class AbstractPCEPSessionTest { - private static final String TUNNEL_NAME = "pcc_" + TEST_ADDRESS + "_tunnel_0"; + private final String TUNNEL_NAME = "pcc_" + testAddress + "_tunnel_0"; private Stateful07TopologySessionListener listener; @@ -116,17 +116,17 @@ public class Stateful07TopologySessionListenerTest extends AbstractPCEPSessionTe public void testStateful07TopologySessionListener() throws Exception { this.listener.onSessionUp(this.session); - assertEquals(TEST_ADDRESS, this.listener.getPeerId()); + assertEquals(testAddress, this.listener.getPeerId()); final SessionState state = this.listener.getSessionState(); assertNotNull(state); assertEquals(DEAD_TIMER, state.getLocalPref().getDeadtimer().shortValue()); assertEquals(KEEP_ALIVE, state.getLocalPref().getKeepalive().shortValue()); assertEquals(0, state.getLocalPref().getSessionId().intValue()); - assertEquals(TEST_ADDRESS, state.getLocalPref().getIpAddress()); + assertEquals(testAddress, state.getLocalPref().getIpAddress()); assertEquals(DEAD_TIMER, state.getPeerPref().getDeadtimer().shortValue()); assertEquals(KEEP_ALIVE, state.getPeerPref().getKeepalive().shortValue()); assertEquals(0, state.getPeerPref().getSessionId().intValue()); - assertEquals(TEST_ADDRESS, state.getPeerPref().getIpAddress()); + assertEquals(testAddress, state.getPeerPref().getIpAddress()); // add-lsp this.topologyRpcs.addLsp(createAddLspInput()); @@ -136,7 +136,7 @@ public class Stateful07TopologySessionListenerTest extends AbstractPCEPSessionTe final Requests req = pcinitiate.getPcinitiateMessage().getRequests().get(0); final long srpId = req.getSrp().getOperationId().getValue(); final Tlvs tlvs = createLspTlvs(req.getLsp().getPlspId().getValue(), true, - TEST_ADDRESS, TEST_ADDRESS, TEST_ADDRESS, Optional.absent()); + testAddress, testAddress, testAddress, Optional.absent()); final Pcrpt pcRpt = MsgBuilderUtil.createPcRtpMessage(new LspBuilder(req.getLsp()).setTlvs(tlvs).setPlspId(new PlspId(1L)).setSync(false).setRemove(false).setOperational(OperationalStatus.Active).build(), Optional.of(MsgBuilderUtil.createSrp(srpId)), MsgBuilderUtil.createPath(req.getEro().getSubobject())); final Pcrpt esm = MsgBuilderUtil.createPcRtpMessage(new LspBuilder().setSync(false).build(), Optional.of(MsgBuilderUtil.createSrp(0L)), null); this.listener.onMessage(this.session, esm); @@ -148,7 +148,7 @@ public class Stateful07TopologySessionListenerTest extends AbstractPCEPSessionTe final Node1 node = topology.getNode().get(0).getAugmentation(Node1.class); assertNotNull(node); PathComputationClient pcc = node.getPathComputationClient(); - assertEquals(TEST_ADDRESS, pcc.getIpAddress().getIpv4Address().getValue()); + assertEquals(testAddress, pcc.getIpAddress().getIpv4Address().getValue()); // reported lsp so far empty, has not received response (PcRpt) yet assertTrue(pcc.getReportedLsp().isEmpty()); this.listener.onMessage(this.session, pcRpt); @@ -161,7 +161,7 @@ public class Stateful07TopologySessionListenerTest extends AbstractPCEPSessionTe assertEquals(1, reportedLsp.getPath().size()); Path path = reportedLsp.getPath().get(0); assertEquals(1, path.getEro().getSubobject().size()); - assertEquals(ERO_IP_PREFIX, getLastEroIpPrefix(path.getEro())); + assertEquals(eroIpPrefix, getLastEroIpPrefix(path.getEro())); // check stats assertEquals(1, this.listener.getDelegatedLspsCount().intValue()); assertTrue(this.listener.getSynchronized()); @@ -173,9 +173,9 @@ public class Stateful07TopologySessionListenerTest extends AbstractPCEPSessionTe // update-lsp final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.update.lsp.args.ArgumentsBuilder updArgsBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.update.lsp.args.ArgumentsBuilder(); - updArgsBuilder.setEro(createEroWithIpPrefixes(Lists.newArrayList(ERO_IP_PREFIX, DST_IP_PREFIX))); + updArgsBuilder.setEro(createEroWithIpPrefixes(Lists.newArrayList(eroIpPrefix, dstIpPrefix))); updArgsBuilder.addAugmentation(Arguments3.class, new Arguments3Builder().setLsp(new LspBuilder().setDelegate(true).setAdministrative(true).build()).build()); - final UpdateLspInput update = new UpdateLspInputBuilder().setArguments(updArgsBuilder.build()).setName(TUNNEL_NAME).setNetworkTopologyRef(new NetworkTopologyRef(TOPO_IID)).setNode(NODE_ID).build(); + final UpdateLspInput update = new UpdateLspInputBuilder().setArguments(updArgsBuilder.build()).setName(TUNNEL_NAME).setNetworkTopologyRef(new NetworkTopologyRef(TOPO_IID)).setNode(nodeId).build(); this.topologyRpcs.updateLsp(update); assertEquals(2, this.receivedMsgs.size()); assertTrue(this.receivedMsgs.get(1) instanceof Pcupd); @@ -183,7 +183,7 @@ public class Stateful07TopologySessionListenerTest extends AbstractPCEPSessionTe final Updates upd = updateMsg.getPcupdMessage().getUpdates().get(0); final long srpId2 = upd.getSrp().getOperationId().getValue(); final Tlvs tlvs2 = createLspTlvs(upd.getLsp().getPlspId().getValue(), false, - NEW_DESTINATION_ADDRESS, TEST_ADDRESS, TEST_ADDRESS, Optional.absent()); + newDestinationAddress, testAddress, testAddress, Optional.absent()); final Pcrpt pcRpt2 = MsgBuilderUtil.createPcRtpMessage(new LspBuilder(upd.getLsp()).setTlvs(tlvs2).setSync(true).setRemove(false).setOperational(OperationalStatus.Active).build(), Optional.of(MsgBuilderUtil.createSrp(srpId2)), MsgBuilderUtil.createPath(upd.getPath().getEro().getSubobject())); this.listener.onMessage(this.session, pcRpt2); //check updated lsp @@ -195,7 +195,7 @@ public class Stateful07TopologySessionListenerTest extends AbstractPCEPSessionTe assertEquals(1, reportedLsp.getPath().size()); path = reportedLsp.getPath().get(0); assertEquals(2, path.getEro().getSubobject().size()); - assertEquals(DST_IP_PREFIX, getLastEroIpPrefix(path.getEro())); + assertEquals(dstIpPrefix, getLastEroIpPrefix(path.getEro())); // check stats assertEquals(1, this.listener.getDelegatedLspsCount().intValue()); assertTrue(this.listener.getSynchronized()); @@ -212,13 +212,13 @@ public class Stateful07TopologySessionListenerTest extends AbstractPCEPSessionTe // ensure-operational final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.ensure.lsp.operational.args.ArgumentsBuilder ensureArgs = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.ensure.lsp.operational.args.ArgumentsBuilder(); ensureArgs.addAugmentation(Arguments1.class, new Arguments1Builder().setOperational(OperationalStatus.Active).build()); - final EnsureLspOperationalInput ensure = new EnsureLspOperationalInputBuilder().setArguments(ensureArgs.build()).setName(TUNNEL_NAME).setNetworkTopologyRef(new NetworkTopologyRef(TOPO_IID)).setNode(NODE_ID).build(); + final EnsureLspOperationalInput ensure = new EnsureLspOperationalInputBuilder().setArguments(ensureArgs.build()).setName(TUNNEL_NAME).setNetworkTopologyRef(new NetworkTopologyRef(TOPO_IID)).setNode(nodeId).build(); final OperationResult result = this.topologyRpcs.ensureLspOperational(ensure).get().getResult(); //check result assertNull(result.getFailure()); // remove-lsp - final RemoveLspInput remove = new RemoveLspInputBuilder().setName(TUNNEL_NAME).setNetworkTopologyRef(new NetworkTopologyRef(TOPO_IID)).setNode(NODE_ID).build(); + final RemoveLspInput remove = new RemoveLspInputBuilder().setName(TUNNEL_NAME).setNetworkTopologyRef(new NetworkTopologyRef(TOPO_IID)).setNode(nodeId).build(); this.topologyRpcs.removeLsp(remove); assertEquals(3, this.receivedMsgs.size()); assertTrue(this.receivedMsgs.get(2) instanceof Pcinitiate); @@ -226,7 +226,7 @@ public class Stateful07TopologySessionListenerTest extends AbstractPCEPSessionTe final Requests req2 = pcinitiate2.getPcinitiateMessage().getRequests().get(0); final long srpId3 = req2.getSrp().getOperationId().getValue(); final Tlvs tlvs3 = createLspTlvs(req2.getLsp().getPlspId().getValue(), false, - TEST_ADDRESS, TEST_ADDRESS, TEST_ADDRESS, Optional.absent()); + testAddress, testAddress, testAddress, Optional.absent()); final Pcrpt pcRpt3 = MsgBuilderUtil.createPcRtpMessage(new LspBuilder(req2.getLsp()).setTlvs(tlvs3).setRemove(true).setSync(true).setOperational(OperationalStatus.Down).build(), Optional.of(MsgBuilderUtil.createSrp(srpId3)), MsgBuilderUtil.createPath(Collections.emptyList())); this.listener.onMessage(this.session, pcRpt3); // check if lsp was removed @@ -293,7 +293,7 @@ public class Stateful07TopologySessionListenerTest extends AbstractPCEPSessionTe final Requests req = pcinitiate.getPcinitiateMessage().getRequests().get(0); final long srpId = req.getSrp().getOperationId().getValue(); final Tlvs tlvs = createLspTlvs(req.getLsp().getPlspId().getValue(), true, - TEST_ADDRESS, TEST_ADDRESS, TEST_ADDRESS, Optional.absent()); + testAddress, testAddress, testAddress, Optional.absent()); final Pcrpt pcRpt = MsgBuilderUtil.createPcRtpMessage(new LspBuilder(req.getLsp()).setTlvs(tlvs).setSync(true).setRemove(false).setOperational(OperationalStatus.Active).build(), Optional.of(MsgBuilderUtil.createSrp(srpId)), MsgBuilderUtil.createPath(req.getEro().getSubobject())); this.listener.onMessage(this.session, pcRpt); assertEquals(1, getTopology().get().getNode().size()); @@ -320,9 +320,9 @@ public class Stateful07TopologySessionListenerTest extends AbstractPCEPSessionTe public void testUpdateUnknownLsp() throws InterruptedException, ExecutionException { this.listener.onSessionUp(this.session); final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.update.lsp.args.ArgumentsBuilder updArgsBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.update.lsp.args.ArgumentsBuilder(); - updArgsBuilder.setEro(createEroWithIpPrefixes(Lists.newArrayList(ERO_IP_PREFIX, DST_IP_PREFIX))); + updArgsBuilder.setEro(createEroWithIpPrefixes(Lists.newArrayList(eroIpPrefix, dstIpPrefix))); updArgsBuilder.addAugmentation(Arguments3.class, new Arguments3Builder().setLsp(new LspBuilder().setDelegate(true).setAdministrative(true).build()).build()); - final UpdateLspInput update = new UpdateLspInputBuilder().setArguments(updArgsBuilder.build()).setName(TUNNEL_NAME).setNetworkTopologyRef(new NetworkTopologyRef(TOPO_IID)).setNode(NODE_ID).build(); + final UpdateLspInput update = new UpdateLspInputBuilder().setArguments(updArgsBuilder.build()).setName(TUNNEL_NAME).setNetworkTopologyRef(new NetworkTopologyRef(TOPO_IID)).setNode(nodeId).build(); final UpdateLspOutput result = this.topologyRpcs.updateLsp(update).get().getResult(); assertEquals(FailureType.Unsent, result.getFailure()); assertEquals(1, result.getError().size()); @@ -334,7 +334,7 @@ public class Stateful07TopologySessionListenerTest extends AbstractPCEPSessionTe @Test public void testRemoveUnknownLsp() throws InterruptedException, ExecutionException { this.listener.onSessionUp(this.session); - final RemoveLspInput remove = new RemoveLspInputBuilder().setName(TUNNEL_NAME).setNetworkTopologyRef(new NetworkTopologyRef(TOPO_IID)).setNode(NODE_ID).build(); + final RemoveLspInput remove = new RemoveLspInputBuilder().setName(TUNNEL_NAME).setNetworkTopologyRef(new NetworkTopologyRef(TOPO_IID)).setNode(nodeId).build(); final OperationResult result = this.topologyRpcs.removeLsp(remove).get().getResult(); assertEquals(FailureType.Unsent, result.getFailure()); assertEquals(1, result.getError().size()); @@ -353,7 +353,7 @@ public class Stateful07TopologySessionListenerTest extends AbstractPCEPSessionTe final Requests req = pcinitiate.getPcinitiateMessage().getRequests().get(0); final long srpId = req.getSrp().getOperationId().getValue(); final Tlvs tlvs = createLspTlvs(req.getLsp().getPlspId().getValue(), true, - TEST_ADDRESS, TEST_ADDRESS, TEST_ADDRESS, Optional.absent()); + testAddress, testAddress, testAddress, Optional.absent()); final Pcrpt pcRpt = MsgBuilderUtil.createPcRtpMessage(new LspBuilder(req.getLsp()).setTlvs(tlvs).setPlspId(new PlspId(1L)).setSync(false).setRemove(false).setOperational(OperationalStatus.Active).build(), Optional.of(MsgBuilderUtil.createSrp(srpId)), MsgBuilderUtil.createPath(req.getEro().getSubobject())); this.listener.onMessage(this.session, pcRpt); @@ -398,10 +398,10 @@ public class Stateful07TopologySessionListenerTest extends AbstractPCEPSessionTe private AddLspInput createAddLspInput() { final ArgumentsBuilder argsBuilder = new ArgumentsBuilder(); final Ipv4CaseBuilder ipv4Builder = new Ipv4CaseBuilder(); - ipv4Builder.setIpv4(new Ipv4Builder().setSourceIpv4Address(new Ipv4Address(TEST_ADDRESS)).setDestinationIpv4Address(new Ipv4Address(TEST_ADDRESS)).build()); + ipv4Builder.setIpv4(new Ipv4Builder().setSourceIpv4Address(new Ipv4Address(testAddress)).setDestinationIpv4Address(new Ipv4Address(testAddress)).build()); argsBuilder.setEndpointsObj(new EndpointsObjBuilder().setAddressFamily(ipv4Builder.build()).build()); - argsBuilder.setEro(createEroWithIpPrefixes(Lists.newArrayList(ERO_IP_PREFIX))); + argsBuilder.setEro(createEroWithIpPrefixes(Lists.newArrayList(eroIpPrefix))); argsBuilder.addAugmentation(Arguments2.class, new Arguments2Builder().setLsp(new LspBuilder().setDelegate(true).setAdministrative(true).build()).build()); - return new AddLspInputBuilder().setName(TUNNEL_NAME).setArguments(argsBuilder.build()).setNetworkTopologyRef(new NetworkTopologyRef(TOPO_IID)).setNode(NODE_ID).build(); + return new AddLspInputBuilder().setName(TUNNEL_NAME).setArguments(argsBuilder.build()).setNetworkTopologyRef(new NetworkTopologyRef(TOPO_IID)).setNode(nodeId).build(); } } diff --git a/pcep/topology-provider/src/test/java/org/opendaylight/bgpcep/pcep/topology/provider/TopologyProgrammingTest.java b/pcep/topology-provider/src/test/java/org/opendaylight/bgpcep/pcep/topology/provider/TopologyProgrammingTest.java index c3d5168b4c..93cd460eda 100644 --- a/pcep/topology-provider/src/test/java/org/opendaylight/bgpcep/pcep/topology/provider/TopologyProgrammingTest.java +++ b/pcep/topology-provider/src/test/java/org/opendaylight/bgpcep/pcep/topology/provider/TopologyProgrammingTest.java @@ -187,57 +187,57 @@ public class TopologyProgrammingTest extends AbstractPCEPSessionTest getInetSocketAddress(input, null)); } + public static HostAndPort toHostAndPort(final InetSocketAddress address) { + return HostAndPort.fromParts(address.getHostString(), address.getPort()); + } + public static InetSocketAddress getInetSocketAddress(final String hostPortString, final Integer defaultPort) { final HostAndPort hostAndPort = HostAndPort.fromString(hostPortString); if (defaultPort != null) { @@ -39,4 +43,36 @@ public final class InetSocketAddressUtil { } return new InetSocketAddress(hostAndPort.getHostText(), hostAndPort.getPort()); } + + public static InetSocketAddress getRandomLoopbackInetSocketAddress(final int port) { + return new InetSocketAddress(getRandomLoopbackIpAddress(), port); + } + + public static InetSocketAddress getRandomLoopbackInetSocketAddress() { + return getRandomLoopbackInetSocketAddress(getRandomPort()); + } + + /** + * Generate a random high range port number + * + * @return A port number range from 10000 to 50000 + */ + public static int getRandomPort() { + final int randPort = 10000 + (int) Math.round(40000 * Math.random()); + return randPort; + } + + /** + * Generate a random loopback ip address + * IP address range: 127.50.50.50 ~ 127.250.250.250 + * We did not utilize the whole 127./8 range to avoid using common addresses like 127.0.0.1 + * @return Generated random loopback IP address + */ + public static String getRandomLoopbackIpAddress() { + final StringBuilder sb = new StringBuilder("127"); + for (int i = 0; i < 3; i++) { + sb.append(".").append(50 + (int) Math.round(Math.random() * 200)); + } + return sb.toString(); + } } diff --git a/testtool-util/src/test/java/org/opendaylight/protocol/util/InetSocketAddressUtilTest.java b/testtool-util/src/test/java/org/opendaylight/protocol/util/InetSocketAddressUtilTest.java index 3370547796..526b16588e 100644 --- a/testtool-util/src/test/java/org/opendaylight/protocol/util/InetSocketAddressUtilTest.java +++ b/testtool-util/src/test/java/org/opendaylight/protocol/util/InetSocketAddressUtilTest.java @@ -9,6 +9,8 @@ package org.opendaylight.protocol.util; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; import java.net.InetSocketAddress; import java.util.Arrays; @@ -49,4 +51,15 @@ public class InetSocketAddressUtilTest { assertEquals(new InetSocketAddress(ADDRESS1, PORT1), InetSocketAddressUtil.getInetSocketAddress(ADDRESS1 + ":" + PORT1, DEFAULT_PORT)); assertEquals(new InetSocketAddress(ADDRESS1, DEFAULT_PORT), InetSocketAddressUtil.getInetSocketAddress(ADDRESS1, DEFAULT_PORT)); } + + @Test + public void getRandomLoopbackInetSocketAddressTest() throws Exception { + final InetSocketAddress addr1 = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(); + final InetSocketAddress addr2 = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(); + assertNotNull(addr1); + assertNotNull(addr2); + assertNotEquals(addr1, addr2); + assertNotEquals(addr1.getHostString(), addr2.getHostString()); + assertNotEquals(addr1.getPort(), addr2.getPort()); + } } \ No newline at end of file