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 SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> snf = new DefaultPCEPSessionNegotiatorFactory(
58 final EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
59 final MessageRegistry msgReg = ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance()
60 .getMessageHandlerRegistry();
61 this.dispatcher = new PCEPDispatcherImpl(msgReg, snf, eventLoopGroup, eventLoopGroup);
62 this.pccMock = new PCCMock<>(snf, 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 = 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 = 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.getPeerAddress());
95 Assert.assertEquals(DEAD_TIMER, session1.getDeadTimerValue().shortValue());
96 Assert.assertEquals(KEEP_ALIVE, session1.getKeepAliveTimerValue().shortValue());
98 Assert.assertEquals(CLIENT2_ADDRESS.getAddress().getHostAddress(), session2.getPeerAddress());
99 Assert.assertEquals(DEAD_TIMER, session2.getDeadTimerValue().shortValue());
100 Assert.assertEquals(KEEP_ALIVE, session2.getKeepAliveTimerValue().shortValue());
104 Assert.assertTrue(futureChannel.channel().isActive());
106 futureChannel.channel().close();
110 public void tearDown() {
111 this.dispatcher.close();
114 private static class PCCMock<M, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> extends
115 AbstractDispatcher<S, L> {
117 private final SessionNegotiatorFactory<M, S, L> negotiatorFactory;
118 private final PCEPHandlerFactory factory;
120 public PCCMock(final SessionNegotiatorFactory<M, S, L> negotiatorFactory, final PCEPHandlerFactory factory,
121 final DefaultPromise<PCEPSessionImpl> defaultPromise) {
122 super(GlobalEventExecutor.INSTANCE, new NioEventLoopGroup(), new NioEventLoopGroup());
123 this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
124 this.factory = Preconditions.checkNotNull(factory);
127 public Future<S> createClient(final InetSocketAddress address, final ReconnectStrategy strategy,
128 final SessionListenerFactory<L> listenerFactory) {
129 return super.createClient(address, strategy, new PipelineInitializer<S>() {
131 public void initializeChannel(final SocketChannel ch, final Promise<S> promise) {
132 ch.pipeline().addLast(PCCMock.this.factory.getDecoders());
133 ch.pipeline().addLast("negotiator",
134 PCCMock.this.negotiatorFactory.getSessionNegotiator(listenerFactory, ch, promise));
135 ch.pipeline().addLast(PCCMock.this.factory.getEncoders());