6c4d4e8c2acd8cafcc7389ab576262847171ebfe
[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 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));
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 = this.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 = this.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
107     @Test
108     public void testCreateDuplicateClient() throws InterruptedException, ExecutionException {
109         this.dispatcher.createServer(new InetSocketAddress("0.0.0.0", PORT),
110                 new SessionListenerFactory<PCEPSessionListener>() {
111                     @Override
112                     public PCEPSessionListener getSessionListener() {
113                         return new SimpleSessionListener();
114                     }
115                 });
116         final PCEPSessionImpl session1 = this.pccMock.createClient(CLIENT1_ADDRESS,
117                 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 500),
118                 new SessionListenerFactory<PCEPSessionListener>() {
119                     @Override
120                     public PCEPSessionListener getSessionListener() {
121                         return new SimpleSessionListener();
122                     }
123                 }).get();
124
125         try {
126             this.pccMock.createClient(CLIENT1_ADDRESS,
127                     new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 500),
128                     new SessionListenerFactory<PCEPSessionListener>() {
129                         @Override
130                         public PCEPSessionListener getSessionListener() {
131                             return new SimpleSessionListener();
132                         }
133                     }).get();
134             Assert.fail();
135         } catch(ExecutionException e) {
136             Assert.assertTrue(e.getMessage().contains("A conflicting session for address"));
137         } finally {
138             session1.close();
139         }
140     }
141
142     @Test
143     public void testReconectClient() throws InterruptedException, ExecutionException {
144         this.dispatcher.createServer(new InetSocketAddress("0.0.0.0", PORT),
145                 new SessionListenerFactory<PCEPSessionListener>() {
146                     @Override
147                     public PCEPSessionListener getSessionListener() {
148                         return new SimpleSessionListener();
149                     }
150                 });
151         final PCEPSessionImpl session1 = this.pccMock.createClient(CLIENT1_ADDRESS,
152                 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 500),
153                 new SessionListenerFactory<PCEPSessionListener>() {
154                     @Override
155                     public PCEPSessionListener getSessionListener() {
156                         return new SimpleSessionListener();
157                     }
158                 }).get();
159
160         Assert.assertEquals(CLIENT1_ADDRESS.getAddress(), session1.getRemoteAddress());
161         Assert.assertEquals(DEAD_TIMER, session1.getDeadTimerValue().shortValue());
162         Assert.assertEquals(KEEP_ALIVE, session1.getKeepAliveTimerValue().shortValue());
163         session1.close();
164
165         final PCEPSessionImpl session2 = this.pccMock.createClient(CLIENT1_ADDRESS,
166                 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 500),
167                 new SessionListenerFactory<PCEPSessionListener>() {
168                     @Override
169                     public PCEPSessionListener getSessionListener() {
170                         return new SimpleSessionListener();
171                     }
172                 }).get();
173
174         Assert.assertEquals(CLIENT1_ADDRESS.getAddress(), session1.getRemoteAddress());
175         Assert.assertEquals(DEAD_TIMER, session2.getDeadTimerValue().shortValue());
176         Assert.assertEquals(KEEP_ALIVE, session2.getKeepAliveTimerValue().shortValue());
177
178         session2.close();
179     }
180
181     @After
182     public void tearDown() {
183         this.dispatcher.close();
184     }
185
186     private static class PCCMock<M, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> extends
187             AbstractDispatcher<S, L> {
188
189         private final SessionNegotiatorFactory<M, S, L> negotiatorFactory;
190         private final PCEPHandlerFactory factory;
191
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);
197         }
198
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>() {
202                 @Override
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());
208                 }
209             });
210         }
211     }
212
213 }