39362d8e5742b18fee29d684b0b3212b1a8c02ab
[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.lang.reflect.Constructor;
13 import java.lang.reflect.InvocationTargetException;
14 import java.net.InetSocketAddress;
15 import java.net.UnknownHostException;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.concurrent.ExecutionException;
19 import org.junit.Test;
20 import org.opendaylight.protocol.pcep.PCEPCapability;
21 import org.opendaylight.protocol.pcep.pcc.mock.spi.MsgBuilderUtil;
22
23 public class PCCMockTest extends PCCMockCommon {
24     private static final String REMOTE_ADDRESS2 = "127.0.2.0";
25     private static final String REMOTE_ADDRESS3 = "127.0.3.0";
26     private static final String REMOTE_ADDRESS4 = "127.0.4.0";
27     private static final String LOCAL_ADDRESS2 = "127.0.0.2";
28     private static final InetSocketAddress SERVER_ADDRESS2 = new InetSocketAddress(REMOTE_ADDRESS2, 4189);
29     private static final InetSocketAddress SERVER_ADDRESS3 = new InetSocketAddress(REMOTE_ADDRESS3, 4189);
30     private static final InetSocketAddress SERVER_ADDRESS4 = new InetSocketAddress(REMOTE_ADDRESS4, 4189);
31     private final String[] mainInput = new String[]{"--local-address", PCCMockCommon.LOCAL_ADDRESS, "--remote-address",
32         PCCMockCommon.REMOTE_ADDRESS + ":4560", "--pcc", "1", "--lsp", "3", "--log-level", "DEBUG", "-ka", "10", "-d", "40", "--reconnect", "-1",
33         "--redelegation-timeout", "0", "--state-timeout", "-1"};
34
35     @Test
36     public void testSessionEstablishment() throws Exception {
37         final TestingSessionListenerFactory factory = new TestingSessionListenerFactory();
38         final Channel channel = createServer(factory, this.socket);
39         Main.main(mainInput);
40         Thread.sleep(1000);
41         //3 reported LSPs + syc
42         final int numMessages = 4;
43         final TestingSessionListener sessionListener = checkSessionListener(numMessages, channel, factory, PCCMockCommon.LOCAL_ADDRESS);
44         checkSession(sessionListener.getSession(), 40, 10);
45     }
46
47
48     @Test
49     public void testMockPCCToManyPCE() throws Exception {
50         final TestingSessionListenerFactory factory = new TestingSessionListenerFactory();
51         final TestingSessionListenerFactory factory2 = new TestingSessionListenerFactory();
52         final TestingSessionListenerFactory factory3 = new TestingSessionListenerFactory();
53         final Channel channel = createServer(factory, SERVER_ADDRESS2);
54         final Channel channel2 = createServer(factory2, SERVER_ADDRESS3);
55         final Channel channel3 = createServer(factory3, SERVER_ADDRESS4);
56
57         Main.main(new String[]{"--local-address", PCCMockCommon.LOCAL_ADDRESS, "--remote-address", REMOTE_ADDRESS2 + "," + REMOTE_ADDRESS3 + "," + REMOTE_ADDRESS4, "--pcc", "2"});
58         Thread.sleep(1000);
59         //PCE1
60         int numMessages = 2;
61         checkSessionListener(numMessages, channel, factory, PCCMockCommon.LOCAL_ADDRESS);
62         checkSessionListener(numMessages, channel, factory, LOCAL_ADDRESS2);
63         //PCE2
64         checkSessionListener(numMessages, channel2, factory2, LOCAL_ADDRESS);
65         checkSessionListener(numMessages, channel2, factory2, LOCAL_ADDRESS2);
66         //PCE3
67         checkSessionListener(numMessages, channel3, factory3, PCCMockCommon.LOCAL_ADDRESS);
68         checkSessionListener(numMessages, channel3, factory3, LOCAL_ADDRESS2);
69     }
70
71     @Test(expected = UnsupportedOperationException.class)
72     public void testPrivateConstructor() throws Throwable {
73         final Constructor<MsgBuilderUtil> c = MsgBuilderUtil.class.getDeclaredConstructor();
74         c.setAccessible(true);
75         try {
76             c.newInstance();
77         } catch (final InvocationTargetException e) {
78             throw e.getCause();
79         }
80     }
81
82     @Override
83     protected List<PCEPCapability> getCapabilities() {
84         return Collections.emptyList();
85     }
86
87     @Override
88     protected int getPort() {
89         return 4560;
90     }
91 }