Bug-2081: PCEP statistics
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / protocol / pcep / impl / PCEPDispatcherImplTest.java
1 /*
2  * Copyright (c) 2014 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.impl;
10
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;
39
40 public class PCEPDispatcherImplTest {
41
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;
47
48     private PCEPDispatcherImpl dispatcher;
49
50     private PCCMock<Message, PCEPSessionImpl, PCEPSessionListener> pccMock;
51
52     @Before
53     public void setUp() {
54         final Open open = new OpenBuilder().setSessionId((short) 0).setDeadTimer(DEAD_TIMER).setKeepalive(KEEP_ALIVE)
55                 .build();
56         final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> snf = new DefaultPCEPSessionNegotiatorFactory(
57                 open, 0);
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));
64     }
65
66     @Test
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>() {
70                     @Override
71                     public PCEPSessionListener getSessionListener() {
72                         return new SimpleSessionListener();
73                     }
74                 });
75         final PCEPSessionImpl session1 = pccMock.createClient(CLIENT1_ADDRESS,
76                 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 500),
77                 new SessionListenerFactory<PCEPSessionListener>() {
78                     @Override
79                     public PCEPSessionListener getSessionListener() {
80                         return new SimpleSessionListener();
81                     }
82                 }).get();
83
84         final PCEPSessionImpl session2 = pccMock.createClient(CLIENT2_ADDRESS,
85                 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 500),
86                 new SessionListenerFactory<PCEPSessionListener>() {
87                     @Override
88                     public PCEPSessionListener getSessionListener() {
89                         return new SimpleSessionListener();
90                     }
91                 }).get();
92
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());
97
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());
101
102         session1.close();
103         session2.close();
104         Assert.assertTrue(futureChannel.channel().isActive());
105
106         futureChannel.channel().close();
107     }
108
109     @After
110     public void tearDown() {
111         this.dispatcher.close();
112     }
113
114     private static class PCCMock<M, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> extends
115             AbstractDispatcher<S, L> {
116
117         private final SessionNegotiatorFactory<M, S, L> negotiatorFactory;
118         private final PCEPHandlerFactory factory;
119
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);
125         }
126
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>() {
130                 @Override
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());
136                 }
137             });
138         }
139     }
140
141 }