2 * Copyright (c) 2015 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
8 package org.opendaylight.protocol.pcep.pcc.mock;
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Mockito.doReturn;
13 import static org.opendaylight.protocol.pcep.pcc.mock.PCCMockCommon.checkSessionListenerNotNull;
15 import io.netty.channel.Channel;
16 import io.netty.channel.ChannelFuture;
17 import io.netty.channel.EventLoopGroup;
18 import io.netty.channel.nio.NioEventLoopGroup;
19 import io.netty.util.concurrent.Future;
20 import java.net.InetSocketAddress;
21 import java.util.List;
22 import java.util.concurrent.TimeUnit;
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mock;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.opendaylight.protocol.concepts.KeyMapping;
30 import org.opendaylight.protocol.pcep.MessageRegistry;
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.MockPcepSessionErrorPolicy;
39 import org.opendaylight.protocol.pcep.pcc.mock.protocol.PCCDispatcherImpl;
40 import org.opendaylight.protocol.pcep.spi.pojo.DefaultPCEPExtensionConsumerContext;
41 import org.opendaylight.protocol.util.InetSocketAddressUtil;
42 import org.opendaylight.yangtools.yang.common.Uint8;
44 @RunWith(MockitoJUnitRunner.StrictStubs.class)
45 public class PCCDispatcherImplTest {
47 private static final PCEPSessionProposalFactory PROPOSAL = new BasePCEPSessionProposalFactory(Uint8.TEN,
48 Uint8.valueOf(40), List.of());
49 private final DefaultPCEPSessionNegotiatorFactory nf = new DefaultPCEPSessionNegotiatorFactory(PROPOSAL,
50 MockPcepSessionErrorPolicy.ZERO);
52 private PCCDispatcherImpl dispatcher;
53 private PCEPDispatcher pcepDispatcher;
54 private InetSocketAddress serverAddress;
55 private InetSocketAddress clientAddress;
56 private EventLoopGroup workerGroup;
57 private EventLoopGroup bossGroup;
58 private MessageRegistry registry;
61 PCEPDispatcherDependencies dispatcherDependencies;
65 workerGroup = new NioEventLoopGroup();
66 bossGroup = new NioEventLoopGroup();
68 registry = new DefaultPCEPExtensionConsumerContext().getMessageHandlerRegistry();
70 dispatcher = new PCCDispatcherImpl(registry);
71 pcepDispatcher = new PCEPDispatcherImpl(registry, nf, bossGroup, workerGroup);
72 serverAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
73 clientAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(0);
74 doReturn(KeyMapping.of()).when(dispatcherDependencies).getKeys();
75 doReturn(serverAddress).when(dispatcherDependencies).getAddress();
76 doReturn(null).when(dispatcherDependencies).getPeerProposal();
80 public void tearDown() {
82 closeEventLoopGroups();
85 private void closeEventLoopGroups() {
86 workerGroup.shutdownGracefully(0, 0, TimeUnit.SECONDS);
87 bossGroup.shutdownGracefully(0, 0, TimeUnit.SECONDS);
90 @Test(timeout = 20000)
91 public void testClientReconnect() throws Exception {
92 final Future<PCEPSession> futureSession = dispatcher.createClient(serverAddress, 1,
93 new TestingSessionListenerFactory(), nf, KeyMapping.of(), clientAddress);
94 final TestingSessionListenerFactory slf = new TestingSessionListenerFactory();
95 doReturn(slf).when(dispatcherDependencies).getListenerFactory();
97 final ChannelFuture futureServer = pcepDispatcher.createServer(dispatcherDependencies);
99 final Channel channel = futureServer.channel();
100 assertNotNull(futureSession.get());
101 checkSessionListenerNotNull(slf, clientAddress.getHostString());
102 final TestingSessionListener sl = checkSessionListenerNotNull(slf, clientAddress.getAddress().getHostAddress());
103 assertNotNull(sl.getSession());
104 assertTrue(sl.isUp());
105 channel.close().get();
106 closeEventLoopGroups();
108 workerGroup = new NioEventLoopGroup();
109 bossGroup = new NioEventLoopGroup();
110 pcepDispatcher = new PCEPDispatcherImpl(registry, nf, bossGroup, workerGroup);
112 final TestingSessionListenerFactory slf2 = new TestingSessionListenerFactory();
113 doReturn(slf2).when(dispatcherDependencies).getListenerFactory();
114 final ChannelFuture future2 = pcepDispatcher.createServer(dispatcherDependencies);
116 final Channel channel2 = future2.channel();
117 final TestingSessionListener sl2 = checkSessionListenerNotNull(slf2,
118 clientAddress.getAddress().getHostAddress());
119 assertNotNull(sl2.getSession());
120 assertTrue(sl2.isUp());