BGPCEP-758: Use random ip for Pcc mock tests
[bgpcep.git] / pcep / pcc-mock / src / test / java / org / opendaylight / protocol / pcep / pcc / mock / PCCMockTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.protocol.pcep.pcc.mock;
10
11 import io.netty.channel.Channel;
12 import java.net.InetSocketAddress;
13 import java.util.Collections;
14 import java.util.List;
15 import org.junit.Test;
16 import org.opendaylight.protocol.pcep.PCEPCapability;
17 import org.opendaylight.protocol.util.InetSocketAddressUtil;
18
19 public final class PCCMockTest extends PCCMockCommon {
20     private final String localAddress2 = "127.0.0.2";
21     private final String localIpAddress = "127.0.0.1";
22     private final String[] mainInput = new String[]{"--local-address", localIpAddress,
23         "--remote-address", InetSocketAddressUtil.toHostAndPort(this.remoteAddress).toString(), "--pcc", "1",
24         "--lsp", "3", "--log-level", "DEBUG", "-ka", "10", "-d", "40", "--reconnect", "-1",
25         "--redelegation-timeout", "0", "--state-timeout", "-1"};
26
27     @Test
28     public void testSessionEstablishment() throws Exception {
29         final TestingSessionListenerFactory factory = new TestingSessionListenerFactory();
30         final Channel channel = createServer(factory, this.remoteAddress);
31         Main.main(this.mainInput);
32         Thread.sleep(1000);
33         //3 reported LSPs + syc
34         final int numMessages = 4;
35         final TestingSessionListener sessionListener = checkSessionListener(numMessages, channel, factory,
36                 this.localIpAddress);
37         checkSession(sessionListener.getSession(), 40, 10);
38     }
39
40
41     @Test
42     public void testMockPCCToManyPCE() throws Exception {
43         final InetSocketAddress serverAddress2 = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
44         final InetSocketAddress serverAddress3 = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
45         final InetSocketAddress serverAddress4 = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
46
47         final TestingSessionListenerFactory factory = new TestingSessionListenerFactory();
48         final TestingSessionListenerFactory factory2 = new TestingSessionListenerFactory();
49         final TestingSessionListenerFactory factory3 = new TestingSessionListenerFactory();
50         final Channel channel = createServer(factory, serverAddress2);
51         final Channel channel2 = createServer(factory2, serverAddress3);
52         final Channel channel3 = createServer(factory3, serverAddress4);
53
54         Main.main(new String[]{"--local-address", this.localIpAddress, "--remote-address",
55                 InetSocketAddressUtil.toHostAndPort(serverAddress2).toString() + ","
56                         + InetSocketAddressUtil.toHostAndPort(serverAddress3).toString() + ","
57                         + InetSocketAddressUtil.toHostAndPort(serverAddress4).toString(), "--pcc", "2"});
58         Thread.sleep(1000);
59         //PCE1
60         final int numMessages = 2;
61         checkSessionListener(numMessages, channel, factory, this.localIpAddress);
62         checkSessionListener(numMessages, channel, factory, this.localAddress2);
63         //PCE2
64         checkSessionListener(numMessages, channel2, factory2, this.localIpAddress);
65         checkSessionListener(numMessages, channel2, factory2, this.localAddress2);
66         //PCE3
67         checkSessionListener(numMessages, channel3, factory3, this.localIpAddress);
68         checkSessionListener(numMessages, channel3, factory3, this.localAddress2);
69     }
70
71     @Override
72     protected List<PCEPCapability> getCapabilities() {
73         return Collections.emptyList();
74     }
75 }