Add new revision for pcep types model
[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.mockito.Mockito.doReturn;
12 import static org.opendaylight.protocol.pcep.pcc.mock.PCCMockCommon.checkSessionListenerNotNull;
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.List;
22 import java.util.concurrent.TimeUnit;
23 import org.junit.After;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.Mock;
28 import org.mockito.MockitoAnnotations;
29 import org.opendaylight.protocol.concepts.KeyMapping;
30 import org.opendaylight.protocol.pcep.PCEPCapability;
31 import org.opendaylight.protocol.pcep.PCEPDispatcher;
32 import org.opendaylight.protocol.pcep.PCEPDispatcherDependencies;
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 import org.opendaylight.protocol.util.InetSocketAddressUtil;
41
42 public class PCCDispatcherImplTest {
43
44     private static final List<PCEPCapability> CAPS = new ArrayList<>();
45     private static final PCEPSessionProposalFactory PROPOSAL
46             = new BasePCEPSessionProposalFactory(10, 40, CAPS);
47     private final DefaultPCEPSessionNegotiatorFactory nf
48             = new DefaultPCEPSessionNegotiatorFactory(PROPOSAL, 0);
49     private PCCDispatcherImpl dispatcher;
50     private PCEPDispatcher pcepDispatcher;
51     private InetSocketAddress serverAddress;
52     private InetSocketAddress clientAddress;
53     private EventLoopGroup workerGroup;
54     private EventLoopGroup bossGroup;
55
56     @Mock
57     PCEPDispatcherDependencies dispatcherDependencies;
58
59     @Before
60     public void setUp() {
61         MockitoAnnotations.initMocks(this);
62         this.workerGroup = new NioEventLoopGroup();
63         this.bossGroup = new NioEventLoopGroup();
64         this.dispatcher = new PCCDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance()
65                 .getMessageHandlerRegistry());
66         this.pcepDispatcher = new PCEPDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance()
67                 .getMessageHandlerRegistry(),
68             this.nf, this.bossGroup, this.workerGroup);
69         this.serverAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
70         this.clientAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(0);
71         doReturn(KeyMapping.getKeyMapping()).when(this.dispatcherDependencies).getKeys();
72         doReturn(this.serverAddress).when(this.dispatcherDependencies).getAddress();
73         doReturn(null).when(this.dispatcherDependencies).getPeerProposal();
74     }
75
76     @After
77     public void tearDown() {
78         this.dispatcher.close();
79         closeEventLoopGroups();
80     }
81
82     private void closeEventLoopGroups() {
83         this.workerGroup.shutdownGracefully(0, 0, TimeUnit.SECONDS);
84         this.bossGroup.shutdownGracefully(0, 0, TimeUnit.SECONDS);
85     }
86
87     @Test(timeout = 20000)
88     public void testClientReconnect() throws Exception {
89         final Future<PCEPSession> futureSession = this.dispatcher
90                 .createClient(this.serverAddress, 1, new TestingSessionListenerFactory(), this.nf,
91                         KeyMapping.getKeyMapping(), this.clientAddress);
92         final TestingSessionListenerFactory slf = new TestingSessionListenerFactory();
93         doReturn(slf).when(this.dispatcherDependencies).getListenerFactory();
94
95         final ChannelFuture futureServer = this.pcepDispatcher.createServer(this.dispatcherDependencies);
96         futureServer.sync();
97         final Channel channel = futureServer.channel();
98         Assert.assertNotNull(futureSession.get());
99         checkSessionListenerNotNull(slf, this.clientAddress.getHostString());
100         final TestingSessionListener sl
101                 = checkSessionListenerNotNull(slf, this.clientAddress.getAddress().getHostAddress());
102         Assert.assertNotNull(sl.getSession());
103         Assert.assertTrue(sl.isUp());
104         channel.close().get();
105         closeEventLoopGroups();
106
107         this.workerGroup = new NioEventLoopGroup();
108         this.bossGroup = new NioEventLoopGroup();
109         this.pcepDispatcher = new PCEPDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance()
110                 .getMessageHandlerRegistry(),
111             this.nf, this.bossGroup, this.workerGroup);
112
113         final TestingSessionListenerFactory slf2 = new TestingSessionListenerFactory();
114         doReturn(slf2).when(this.dispatcherDependencies).getListenerFactory();
115         final ChannelFuture future2 = this.pcepDispatcher.createServer(this.dispatcherDependencies);
116         future2.sync();
117         final Channel channel2 = future2.channel();
118         final TestingSessionListener sl2
119                 = checkSessionListenerNotNull(slf2, this.clientAddress.getAddress().getHostAddress());
120         Assert.assertNotNull(sl2.getSession());
121         Assert.assertTrue(sl2.isUp());
122         channel2.close();
123     }
124 }