BUG-5619 Enable maven parallel build for bgpcep I
[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.concurrent.ExecutionException;
24 import org.junit.After;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.junit.runners.Parameterized;
30 import org.opendaylight.protocol.pcep.PCEPCapability;
31 import org.opendaylight.protocol.pcep.PCEPDispatcher;
32 import org.opendaylight.protocol.pcep.PCEPSession;
33 import org.opendaylight.protocol.pcep.PCEPSessionProposalFactory;
34 import org.opendaylight.protocol.pcep.impl.BasePCEPSessionProposalFactory;
35 import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiatorFactory;
36 import org.opendaylight.protocol.pcep.impl.PCEPDispatcherImpl;
37 import org.opendaylight.protocol.pcep.pcc.mock.protocol.PCCDispatcherImpl;
38 import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext;
39 import org.opendaylight.protocol.util.InetSocketAddressUtil;
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 PCCDispatcherImpl dispatcher;
47     private PCEPDispatcher pcepDispatcher;
48     private InetSocketAddress serverAddress;
49     private InetSocketAddress clientAddress;
50     private EventLoopGroup workerGroup;
51     private EventLoopGroup bossGroup;
52
53     @Before
54     public void setUp() {
55         this.workerGroup = new NioEventLoopGroup();
56         this.bossGroup = new NioEventLoopGroup();
57         this.dispatcher = new PCCDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry());
58         this.pcepDispatcher = new PCEPDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry(),
59             this.nf, this.bossGroup, this.workerGroup);
60         this.serverAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
61         this.clientAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(0);
62     }
63
64     @After
65     public void tearDown() throws InterruptedException, ExecutionException {
66         this.dispatcher.close();
67         closeEventLoopGroups();
68     }
69
70     private void closeEventLoopGroups() throws ExecutionException, InterruptedException {
71         this.workerGroup.shutdownGracefully().get();
72         this.bossGroup.shutdownGracefully().get();
73     }
74
75     @Test
76     public void testClientReconnect() throws Exception {
77         final Future<PCEPSession> futureSession = this.dispatcher.createClient(this.serverAddress, 1, new TestingSessionListenerFactory(),
78             this.nf, null, this.clientAddress);
79         waitFutureSuccess(futureSession);
80         final TestingSessionListenerFactory slf = new TestingSessionListenerFactory();
81         final ChannelFuture futureServer = this.pcepDispatcher.createServer(this.serverAddress, slf, null);
82         waitFutureSuccess(futureServer);
83         final Channel channel = futureServer.channel();
84         Assert.assertNotNull(futureSession.get());
85         checkSessionListenerNotNull(slf, this.clientAddress.getHostString());
86         final TestingSessionListener sl = checkSessionListenerNotNull(slf, this.clientAddress.getAddress().getHostAddress());
87         Assert.assertNotNull(sl.getSession());
88         Assert.assertTrue(sl.isUp());
89         channel.close().get();
90         closeEventLoopGroups();
91
92         this.workerGroup = new NioEventLoopGroup();
93         this.bossGroup = new NioEventLoopGroup();
94         this.pcepDispatcher = new PCEPDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry(),
95             this.nf, this.bossGroup, this.workerGroup);
96
97         final TestingSessionListenerFactory slf2 = new TestingSessionListenerFactory();
98         final ChannelFuture future2 = this.pcepDispatcher.createServer(this.serverAddress, slf2, null);
99         waitFutureSuccess(future2);
100         final Channel channel2 = future2.channel();
101         final TestingSessionListener sl2 = checkSessionListenerNotNull(slf2, this.clientAddress.getAddress().getHostAddress());
102         Assert.assertNotNull(sl2.getSession());
103         Assert.assertTrue(sl2.isUp());
104     }
105 }