fb8265ddcbcfa0a7b6b38a0621939363ae179753
[bgpcep.git] / pcep / pcc-mock / src / test / java / org / opendaylight / protocol / pcep / pcc / mock / PCCDispatcherImplTest.java
1 /*
2  * Copyright (c) 2015 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 static org.opendaylight.protocol.pcep.pcc.mock.PCCMockCommon.checkSessionListenerNotNull;
12 import static org.opendaylight.protocol.pcep.pcc.mock.WaitForFutureSucces.waitFutureSuccess;
13
14 import io.netty.channel.Channel;
15 import io.netty.channel.ChannelFuture;
16 import io.netty.channel.EventLoopGroup;
17 import io.netty.channel.nio.NioEventLoopGroup;
18 import io.netty.util.concurrent.Future;
19 import java.net.InetSocketAddress;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.List;
23 import java.util.Random;
24 import java.util.concurrent.ExecutionException;
25 import org.junit.After;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.junit.runners.Parameterized;
31 import org.opendaylight.protocol.pcep.PCEPCapability;
32 import org.opendaylight.protocol.pcep.PCEPDispatcher;
33 import org.opendaylight.protocol.pcep.PCEPSession;
34 import org.opendaylight.protocol.pcep.PCEPSessionProposalFactory;
35 import org.opendaylight.protocol.pcep.impl.BasePCEPSessionProposalFactory;
36 import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiatorFactory;
37 import org.opendaylight.protocol.pcep.impl.PCEPDispatcherImpl;
38 import org.opendaylight.protocol.pcep.pcc.mock.protocol.PCCDispatcherImpl;
39 import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext;
40
41 public class PCCDispatcherImplTest {
42
43     private static final List<PCEPCapability> CAPS = new ArrayList<>();
44     private static final PCEPSessionProposalFactory PROPOSAL = new BasePCEPSessionProposalFactory(30, 120, CAPS);
45     private final DefaultPCEPSessionNegotiatorFactory nf = new DefaultPCEPSessionNegotiatorFactory(PROPOSAL, 0);
46     private final Random random = new Random();
47     private PCCDispatcherImpl dispatcher;
48     private PCEPDispatcher pcepDispatcher;
49     private InetSocketAddress serverAddress;
50     private InetSocketAddress clientAddress;
51     private EventLoopGroup workerGroup;
52     private EventLoopGroup bossGroup;
53
54     @Before
55     public void setUp() {
56         this.workerGroup = new NioEventLoopGroup();
57         this.bossGroup = new NioEventLoopGroup();
58         this.dispatcher = new PCCDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry());
59         this.pcepDispatcher = new PCEPDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry(),
60             this.nf, this.bossGroup, this.workerGroup);
61         this.serverAddress = new InetSocketAddress("127.0.5.0", getRandomPort());
62         this.clientAddress = new InetSocketAddress("127.0.4.0", getRandomPort());
63     }
64
65     @After
66     public void tearDown() throws InterruptedException, ExecutionException {
67         this.dispatcher.close();
68         closeEventLoopGroups();
69     }
70
71     private void closeEventLoopGroups() throws ExecutionException, InterruptedException {
72         this.workerGroup.shutdownGracefully().get();
73         this.bossGroup.shutdownGracefully().get();
74     }
75
76     @Test
77     public void testClientReconnect() throws Exception {
78         final Future<PCEPSession> futureSession = this.dispatcher.createClient(this.serverAddress, 1, new TestingSessionListenerFactory(),
79             this.nf, null, this.clientAddress);
80         waitFutureSuccess(futureSession);
81         final TestingSessionListenerFactory slf = new TestingSessionListenerFactory();
82         final ChannelFuture futureServer = this.pcepDispatcher.createServer(this.serverAddress, slf, null);
83         waitFutureSuccess(futureServer);
84         final Channel channel = futureServer.channel();
85         Assert.assertNotNull(futureSession.get());
86         checkSessionListenerNotNull(slf, "127.0.4.0");
87         final TestingSessionListener sl = checkSessionListenerNotNull(slf, this.clientAddress.getAddress().getHostAddress());
88         Assert.assertNotNull(sl.getSession());
89         Assert.assertTrue(sl.isUp());
90         channel.close().get();
91         closeEventLoopGroups();
92
93         this.workerGroup = new NioEventLoopGroup();
94         this.bossGroup = new NioEventLoopGroup();
95         this.pcepDispatcher = new PCEPDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry(),
96             this.nf, this.bossGroup, this.workerGroup);
97
98         final TestingSessionListenerFactory slf2 = new TestingSessionListenerFactory();
99         final ChannelFuture future2 = this.pcepDispatcher.createServer(this.serverAddress, slf2, null);
100         waitFutureSuccess(future2);
101         final Channel channel2 = future2.channel();
102         final TestingSessionListener sl2 = checkSessionListenerNotNull(slf2, this.clientAddress.getAddress().getHostAddress());
103         Assert.assertNotNull(sl2.getSession());
104         Assert.assertTrue(sl2.isUp());
105     }
106
107     private int getRandomPort() {
108         return this.random.nextInt(4000) + 1024;
109     }
110 }