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.impl;
11 import com.google.common.base.Preconditions;
12 import io.netty.channel.ChannelFuture;
13 import io.netty.channel.EventLoopGroup;
14 import io.netty.channel.nio.NioEventLoopGroup;
15 import io.netty.channel.socket.SocketChannel;
16 import io.netty.util.concurrent.DefaultPromise;
17 import io.netty.util.concurrent.Future;
18 import io.netty.util.concurrent.GlobalEventExecutor;
19 import io.netty.util.concurrent.Promise;
20 import java.net.InetSocketAddress;
21 import java.util.concurrent.ExecutionException;
22 import org.junit.After;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.protocol.framework.AbstractDispatcher;
27 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
28 import org.opendaylight.protocol.framework.ProtocolSession;
29 import org.opendaylight.protocol.framework.ReconnectStrategy;
30 import org.opendaylight.protocol.framework.SessionListener;
31 import org.opendaylight.protocol.framework.SessionListenerFactory;
32 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
33 import org.opendaylight.protocol.pcep.PCEPSessionListener;
34 import org.opendaylight.protocol.pcep.spi.MessageRegistry;
35 import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
40 public class PCEPDispatcherImplTest {
42 private static final int PORT = 4189;
43 private static final InetSocketAddress CLIENT1_ADDRESS = new InetSocketAddress("127.0.0.10", PORT);
44 private static final InetSocketAddress CLIENT2_ADDRESS = new InetSocketAddress("127.0.0.11", PORT);
45 private static final short DEAD_TIMER = 120;
46 private static final short KEEP_ALIVE = 30;
48 private PCEPDispatcherImpl dispatcher;
50 private PCCMock<Message, PCEPSessionImpl, PCEPSessionListener> pccMock;
54 final Open open = new OpenBuilder().setSessionId((short) 0).setDeadTimer(DEAD_TIMER).setKeepalive(KEEP_ALIVE)
56 final EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
57 final MessageRegistry msgReg = ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance()
58 .getMessageHandlerRegistry();
59 this.dispatcher = new PCEPDispatcherImpl(msgReg, new DefaultPCEPSessionNegotiatorFactory(open, 0),
60 eventLoopGroup, eventLoopGroup);
61 this.pccMock = new PCCMock<>(new DefaultPCEPSessionNegotiatorFactory(open, 0),
62 new PCEPHandlerFactory(msgReg), new DefaultPromise<PCEPSessionImpl>(
63 GlobalEventExecutor.INSTANCE));
67 public void testCreateClientServer() throws InterruptedException, ExecutionException {
68 final ChannelFuture futureChannel = this.dispatcher.createServer(new InetSocketAddress("0.0.0.0", PORT),
69 new SessionListenerFactory<PCEPSessionListener>() {
71 public PCEPSessionListener getSessionListener() {
72 return new SimpleSessionListener();
75 final PCEPSessionImpl session1 = this.pccMock.createClient(CLIENT1_ADDRESS,
76 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 500),
77 new SessionListenerFactory<PCEPSessionListener>() {
79 public PCEPSessionListener getSessionListener() {
80 return new SimpleSessionListener();
84 final PCEPSessionImpl session2 = this.pccMock.createClient(CLIENT2_ADDRESS,
85 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 500),
86 new SessionListenerFactory<PCEPSessionListener>() {
88 public PCEPSessionListener getSessionListener() {
89 return new SimpleSessionListener();
93 Assert.assertTrue(futureChannel.channel().isActive());
94 Assert.assertEquals(CLIENT1_ADDRESS.getAddress().getHostAddress(), session1.getPeerPref().getIpAddress());
95 Assert.assertEquals(DEAD_TIMER, session1.getDeadTimerValue().shortValue());
96 Assert.assertEquals(KEEP_ALIVE, session1.getKeepAliveTimerValue().shortValue());
98 Assert.assertEquals(CLIENT2_ADDRESS.getAddress().getHostAddress(), session2.getPeerPref().getIpAddress());
99 Assert.assertEquals(DEAD_TIMER, session2.getDeadTimerValue().shortValue());
100 Assert.assertEquals(KEEP_ALIVE, session2.getKeepAliveTimerValue().shortValue());
104 Assert.assertTrue(futureChannel.channel().isActive());
108 public void testCreateDuplicateClient() throws InterruptedException, ExecutionException {
109 this.dispatcher.createServer(new InetSocketAddress("0.0.0.0", PORT),
110 new SessionListenerFactory<PCEPSessionListener>() {
112 public PCEPSessionListener getSessionListener() {
113 return new SimpleSessionListener();
116 final PCEPSessionImpl session1 = this.pccMock.createClient(CLIENT1_ADDRESS,
117 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 500),
118 new SessionListenerFactory<PCEPSessionListener>() {
120 public PCEPSessionListener getSessionListener() {
121 return new SimpleSessionListener();
126 this.pccMock.createClient(CLIENT1_ADDRESS,
127 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 500),
128 new SessionListenerFactory<PCEPSessionListener>() {
130 public PCEPSessionListener getSessionListener() {
131 return new SimpleSessionListener();
135 } catch(ExecutionException e) {
136 Assert.assertTrue(e.getMessage().contains("A conflicting session for address"));
143 public void testReconectClient() throws InterruptedException, ExecutionException {
144 this.dispatcher.createServer(new InetSocketAddress("0.0.0.0", PORT),
145 new SessionListenerFactory<PCEPSessionListener>() {
147 public PCEPSessionListener getSessionListener() {
148 return new SimpleSessionListener();
151 final PCEPSessionImpl session1 = this.pccMock.createClient(CLIENT1_ADDRESS,
152 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 500),
153 new SessionListenerFactory<PCEPSessionListener>() {
155 public PCEPSessionListener getSessionListener() {
156 return new SimpleSessionListener();
160 Assert.assertEquals(CLIENT1_ADDRESS.getAddress(), session1.getRemoteAddress());
161 Assert.assertEquals(DEAD_TIMER, session1.getDeadTimerValue().shortValue());
162 Assert.assertEquals(KEEP_ALIVE, session1.getKeepAliveTimerValue().shortValue());
165 final PCEPSessionImpl session2 = this.pccMock.createClient(CLIENT1_ADDRESS,
166 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 500),
167 new SessionListenerFactory<PCEPSessionListener>() {
169 public PCEPSessionListener getSessionListener() {
170 return new SimpleSessionListener();
174 Assert.assertEquals(CLIENT1_ADDRESS.getAddress(), session1.getRemoteAddress());
175 Assert.assertEquals(DEAD_TIMER, session2.getDeadTimerValue().shortValue());
176 Assert.assertEquals(KEEP_ALIVE, session2.getKeepAliveTimerValue().shortValue());
182 public void tearDown() {
183 this.dispatcher.close();
186 private static class PCCMock<M, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> extends
187 AbstractDispatcher<S, L> {
189 private final SessionNegotiatorFactory<M, S, L> negotiatorFactory;
190 private final PCEPHandlerFactory factory;
192 public PCCMock(final SessionNegotiatorFactory<M, S, L> negotiatorFactory, final PCEPHandlerFactory factory,
193 final DefaultPromise<PCEPSessionImpl> defaultPromise) {
194 super(GlobalEventExecutor.INSTANCE, new NioEventLoopGroup(), new NioEventLoopGroup());
195 this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
196 this.factory = Preconditions.checkNotNull(factory);
199 public Future<S> createClient(final InetSocketAddress address, final ReconnectStrategy strategy,
200 final SessionListenerFactory<L> listenerFactory) {
201 return super.createClient(address, strategy, new PipelineInitializer<S>() {
203 public void initializeChannel(final SocketChannel ch, final Promise<S> promise) {
204 ch.pipeline().addLast(PCCMock.this.factory.getDecoders());
205 ch.pipeline().addLast("negotiator",
206 PCCMock.this.negotiatorFactory.getSessionNegotiator(listenerFactory, ch, promise));
207 ch.pipeline().addLast(PCCMock.this.factory.getEncoders());