2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.pcep.pcc.mock;
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;
31 public class PCCMockTest {
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";
45 private PCEPDispatcher pceDispatcher;
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());
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"});
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();
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();
91 Main.main(new String[] {"--local-address", LOCAL_ADDRESS, "--remote-address", REMOTE_ADDRESS2 + "," + REMOTE_ADDRESS3 + "," + REMOTE_ADDRESS4,
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());
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());
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());
122 channel.close().get();
123 channel2.close().get();
124 channel3.close().get();
127 @Test(expected=UnsupportedOperationException.class)
128 public void testPrivateConstructor() throws Throwable {
129 final Constructor<MsgBuilderUtil> c = MsgBuilderUtil.class.getDeclaredConstructor();
130 c.setAccessible(true);
133 } catch (InvocationTargetException e) {