01826b4746d248cc145fe5e5cf3be149bc0823f4
[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 com.google.common.net.InetAddresses;
12 import io.netty.channel.Channel;
13 import io.netty.channel.nio.NioEventLoopGroup;
14 import java.lang.reflect.Constructor;
15 import java.lang.reflect.InvocationTargetException;
16 import java.net.InetSocketAddress;
17 import java.net.UnknownHostException;
18 import java.util.concurrent.ExecutionException;
19 import org.junit.Assert;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.protocol.pcep.PCEPDispatcher;
23 import org.opendaylight.protocol.pcep.PCEPSession;
24 import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiatorFactory;
25 import org.opendaylight.protocol.pcep.impl.PCEPDispatcherImpl;
26 import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Stateful1;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs1;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
30
31 public class PCCMockTest {
32
33     private static final short KEEP_ALIVE = 30;
34     private static final short DEAD_TIMER = 120;
35     private static final String REMOTE_ADDRESS = "127.0.1.0";
36     private static final String REMOTE_ADDRESS2 = "127.0.2.0";
37     private static final String REMOTE_ADDRESS3 = "127.0.3.0";
38     private static final String REMOTE_ADDRESS4 = "127.0.4.0";
39     private static final InetSocketAddress SERVER_ADDRESS2 = new InetSocketAddress(REMOTE_ADDRESS2, 4189);
40     private static final InetSocketAddress SERVER_ADDRESS3 = new InetSocketAddress(REMOTE_ADDRESS3, 4189);
41     private static final InetSocketAddress SERVER_ADDRESS4 = new InetSocketAddress(REMOTE_ADDRESS4, 4189);
42     private static final String LOCAL_ADDRESS = "127.0.0.1";
43     private static final String LOCAL_ADDRESS2 = "127.0.0.2";
44
45     private PCEPDispatcher pceDispatcher;
46
47     @Before
48     public void setUp() {
49         final DefaultPCEPSessionNegotiatorFactory nf = new DefaultPCEPSessionNegotiatorFactory(
50                 new OpenBuilder().setKeepalive(KEEP_ALIVE).setDeadTimer(DEAD_TIMER).setSessionId((short) 0).build(), 0);
51         this.pceDispatcher = new PCEPDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry(),
52                 nf, new NioEventLoopGroup(), new NioEventLoopGroup());
53     }
54
55     @Test
56     public void testSessionEstablishment() throws UnknownHostException, InterruptedException, ExecutionException {
57         final TestingSessionListenerFactory factory = new TestingSessionListenerFactory();
58         final Channel channel = this.pceDispatcher.createServer(new InetSocketAddress(REMOTE_ADDRESS, 4567), factory).channel();
59         Main.main(new String[] {"--local-address", LOCAL_ADDRESS, "--remote-address", REMOTE_ADDRESS + ":4567", "--pcc", "1", "--lsp", "3",
60             "--log-level", "DEBUG", "-ka", "10", "-d", "40"});
61         Thread.sleep(1000);
62         final TestingSessionListener sessionListener = factory.getSessionListenerByRemoteAddress(InetAddresses.forString(LOCAL_ADDRESS));
63         Assert.assertTrue(sessionListener.isUp());
64         //3 reported LSPs + syc
65         Assert.assertEquals(4, sessionListener.messages().size());
66         final PCEPSession session = sessionListener.getSession();
67         Assert.assertNotNull(session);
68         Assert.assertEquals(40, session.getPeerPref().getDeadtimer().shortValue());
69         Assert.assertEquals(10, session.getPeerPref().getKeepalive().shortValue());
70         Assert.assertTrue(session.getRemoteTlvs().getAugmentation(Tlvs1.class).getStateful().getAugmentation(Stateful1.class).isInitiation());
71         channel.close().get();
72     }
73
74     @Test
75     public void testMockPCCToManyPCE() throws InterruptedException, ExecutionException, UnknownHostException {
76         final DefaultPCEPSessionNegotiatorFactory nf = new DefaultPCEPSessionNegotiatorFactory(
77                 new OpenBuilder().setKeepalive(KEEP_ALIVE).setDeadTimer(DEAD_TIMER).setSessionId((short) 0).build(), 0);
78         PCEPDispatcher dispatcher2 = new PCEPDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry(),
79                 nf, new NioEventLoopGroup(), new NioEventLoopGroup());
80         final DefaultPCEPSessionNegotiatorFactory nf2 = new DefaultPCEPSessionNegotiatorFactory(
81                 new OpenBuilder().setKeepalive(KEEP_ALIVE).setDeadTimer(DEAD_TIMER).setSessionId((short) 0).build(), 0);
82         PCEPDispatcher dispatcher3 = new PCEPDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry(),
83                 nf2, new NioEventLoopGroup(), new NioEventLoopGroup());
84         final TestingSessionListenerFactory factory = new TestingSessionListenerFactory();
85         final TestingSessionListenerFactory factory2 = new TestingSessionListenerFactory();
86         final TestingSessionListenerFactory factory3 = new TestingSessionListenerFactory();
87         final Channel channel = pceDispatcher.createServer(SERVER_ADDRESS2, factory).channel();
88         final Channel channel2 = dispatcher2.createServer(SERVER_ADDRESS3, factory2).channel();
89         final Channel channel3 = dispatcher3.createServer(SERVER_ADDRESS4, factory3).channel();
90
91         Main.main(new String[] {"--local-address", LOCAL_ADDRESS, "--remote-address", REMOTE_ADDRESS2 + "," + REMOTE_ADDRESS3 + "," + REMOTE_ADDRESS4,
92             "--pcc", "2"});
93         Thread.sleep(1000);
94         //PCE1
95         TestingSessionListener sessionListener1 = factory.getSessionListenerByRemoteAddress(InetAddresses.forString(LOCAL_ADDRESS));
96         TestingSessionListener sessionListener2 = factory.getSessionListenerByRemoteAddress(InetAddresses.forString(LOCAL_ADDRESS2));
97         Assert.assertNotNull(sessionListener1);
98         Assert.assertNotNull(sessionListener2);
99         Assert.assertTrue(sessionListener1.isUp());
100         Assert.assertTrue(sessionListener2.isUp());
101         Assert.assertEquals(2, sessionListener1.messages().size());
102         Assert.assertEquals(2, sessionListener2.messages().size());
103         //PCE2
104         sessionListener1 = factory2.getSessionListenerByRemoteAddress(InetAddresses.forString(LOCAL_ADDRESS));
105         sessionListener2 = factory2.getSessionListenerByRemoteAddress(InetAddresses.forString(LOCAL_ADDRESS2));
106         Assert.assertNotNull(sessionListener1);
107         Assert.assertNotNull(sessionListener2);
108         Assert.assertTrue(sessionListener1.isUp());
109         Assert.assertTrue(sessionListener2.isUp());
110         Assert.assertEquals(2, sessionListener1.messages().size());
111         Assert.assertEquals(2, sessionListener2.messages().size());
112         //PCE3
113         sessionListener1 = factory3.getSessionListenerByRemoteAddress(InetAddresses.forString(LOCAL_ADDRESS));
114         sessionListener2 = factory3.getSessionListenerByRemoteAddress(InetAddresses.forString(LOCAL_ADDRESS2));
115         Assert.assertNotNull(sessionListener1);
116         Assert.assertNotNull(sessionListener2);
117         Assert.assertTrue(sessionListener1.isUp());
118         Assert.assertTrue(sessionListener2.isUp());
119         Assert.assertEquals(2, sessionListener1.messages().size());
120         Assert.assertEquals(2, sessionListener2.messages().size());
121
122         channel.close().get();
123         channel2.close().get();
124         channel3.close().get();
125     }
126
127     @Test(expected=UnsupportedOperationException.class)
128     public void testPrivateConstructor() throws Throwable {
129         final Constructor<MsgBuilderUtil> c = MsgBuilderUtil.class.getDeclaredConstructor();
130         c.setAccessible(true);
131         try {
132             c.newInstance();
133         } catch (InvocationTargetException e) {
134             throw e.getCause();
135         }
136     }
137 }