EventLoopGroup instances moved to constructor parameters for AbstractDispatcher.
[bgpcep.git] / pcep / testtool / src / test / java / org / opendaylight / protocol / pcep / testtool / PCCMock.java
1 /*
2  * Copyright (c) 2013 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 package org.opendaylight.protocol.pcep.testtool;
9
10 import com.google.common.base.Preconditions;
11 import io.netty.channel.nio.NioEventLoopGroup;
12 import io.netty.channel.socket.SocketChannel;
13 import io.netty.util.HashedWheelTimer;
14 import io.netty.util.concurrent.DefaultPromise;
15 import io.netty.util.concurrent.Future;
16 import io.netty.util.concurrent.GlobalEventExecutor;
17 import io.netty.util.concurrent.Promise;
18 import org.opendaylight.protocol.framework.AbstractDispatcher;
19 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
20 import org.opendaylight.protocol.framework.ProtocolHandlerFactory;
21 import org.opendaylight.protocol.framework.ProtocolSession;
22 import org.opendaylight.protocol.framework.ReconnectStrategy;
23 import org.opendaylight.protocol.framework.SessionListener;
24 import org.opendaylight.protocol.framework.SessionListenerFactory;
25 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
26 import org.opendaylight.protocol.pcep.PCEPSessionListener;
27 import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiatorFactory;
28 import org.opendaylight.protocol.pcep.impl.PCEPHandlerFactory;
29 import org.opendaylight.protocol.pcep.impl.PCEPSessionImpl;
30 import org.opendaylight.protocol.pcep.spi.pojo.PCEPExtensionProviderContextImpl;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.predundancy.group.id.tlv.PredundancyGroupIdBuilder;
35
36 import java.net.InetSocketAddress;
37
38 public class PCCMock<M, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> extends AbstractDispatcher<S, L> {
39
40         private final SessionNegotiatorFactory<M, S, L> negotiatorFactory;
41         private final ProtocolHandlerFactory<?> factory;
42
43         public PCCMock(final SessionNegotiatorFactory<M, S, L> negotiatorFactory, final ProtocolHandlerFactory<?> factory,
44                         final DefaultPromise<PCEPSessionImpl> defaultPromise) {
45                 super(new NioEventLoopGroup(), new NioEventLoopGroup());
46                 this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
47                 this.factory = Preconditions.checkNotNull(factory);
48         }
49
50         public Future<S> createClient(final InetSocketAddress address, final ReconnectStrategy strategy,
51                         final SessionListenerFactory<L> listenerFactory) {
52                 return super.createClient(address, strategy, new PipelineInitializer<S>() {
53                         @Override
54                         public void initializeChannel(final SocketChannel ch, final Promise<S> promise) {
55                                 ch.pipeline().addLast(PCCMock.this.factory.getDecoders());
56                                 ch.pipeline().addLast("negotiator", PCCMock.this.negotiatorFactory.getSessionNegotiator(listenerFactory, ch, promise));
57                                 ch.pipeline().addLast(PCCMock.this.factory.getEncoders());
58                         }
59                 });
60         }
61
62         public static void main(final String[] args) throws Exception {
63                 final TlvsBuilder builder = new TlvsBuilder();
64                 builder.setPredundancyGroupId(new PredundancyGroupIdBuilder().setIdentifier(new byte[] { (byte) 127, (byte) 2, (byte) 3, (byte) 7 }).build());
65
66                 final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> snf = new DefaultPCEPSessionNegotiatorFactory(new HashedWheelTimer(), new OpenBuilder().setKeepalive(
67                                 (short) 30).setDeadTimer((short) 120).setSessionId((short) 0).setTlvs(builder.build()).build(), 0);
68
69                 final PCCMock<Message, PCEPSessionImpl, PCEPSessionListener> pcc = new PCCMock<>(snf, new PCEPHandlerFactory(PCEPExtensionProviderContextImpl.getSingletonInstance().getMessageHandlerRegistry()), new DefaultPromise<PCEPSessionImpl>(GlobalEventExecutor.INSTANCE));
70
71                 pcc.createClient(new InetSocketAddress("127.0.0.3", 12345), new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 2000),
72                                 new SessionListenerFactory<PCEPSessionListener>() {
73
74                                         @Override
75                                         public PCEPSessionListener getSessionListener() {
76                                                 return new SimpleSessionListener();
77                                         }
78                                 }).get();
79         }
80 }