BUG-54 : switched channel pipeline to be protocol specific.
[bgpcep.git] / bgp / testtool / src / test / java / org / opendaylight / protocol / bgp / testtool / BGPSpeakerMock.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.bgp.testtool;
9
10 import io.netty.channel.socket.SocketChannel;
11 import io.netty.util.HashedWheelTimer;
12 import io.netty.util.concurrent.DefaultPromise;
13 import io.netty.util.concurrent.GlobalEventExecutor;
14 import io.netty.util.concurrent.Promise;
15
16 import java.io.IOException;
17 import java.net.InetSocketAddress;
18
19 import org.opendaylight.protocol.bgp.parser.BGPMessage;
20 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
21 import org.opendaylight.protocol.bgp.parser.impl.BGPMessageFactoryImpl;
22 import org.opendaylight.protocol.bgp.rib.impl.BGPHandlerFactory;
23 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionImpl;
24 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionNegotiatorFactory;
25 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionProposalImpl;
26 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
27 import org.opendaylight.protocol.concepts.ASNumber;
28 import org.opendaylight.protocol.concepts.IPv4;
29 import org.opendaylight.protocol.framework.AbstractDispatcher;
30 import org.opendaylight.protocol.framework.ProtocolHandlerFactory;
31 import org.opendaylight.protocol.framework.ProtocolMessage;
32 import org.opendaylight.protocol.framework.ProtocolSession;
33 import org.opendaylight.protocol.framework.SessionListener;
34 import org.opendaylight.protocol.framework.SessionListenerFactory;
35 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
36
37 import com.google.common.base.Preconditions;
38
39 public class BGPSpeakerMock<M extends ProtocolMessage, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> extends
40                 AbstractDispatcher<S, L> {
41
42         private final SessionNegotiatorFactory<M, S, L> negotiatorFactory;
43         private final ProtocolHandlerFactory<?> factory;
44
45         public BGPSpeakerMock(final SessionNegotiatorFactory<M, S, L> negotiatorFactory, final ProtocolHandlerFactory<?> factory,
46                         final DefaultPromise<BGPSessionImpl> defaultPromise) {
47                 this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
48                 this.factory = Preconditions.checkNotNull(factory);
49         }
50
51         @Override
52         public void initializeChannel(final SocketChannel ch, final Promise<S> promise, final SessionListenerFactory<L> listenerFactory) {
53                 ch.pipeline().addLast(this.factory.getDecoders());
54                 ch.pipeline().addLast("negotiator", this.negotiatorFactory.getSessionNegotiator(listenerFactory, ch, promise));
55                 ch.pipeline().addLast(this.factory.getEncoders());
56         }
57
58         public static void main(final String[] args) throws IOException {
59
60                 final SessionListenerFactory<BGPSessionListener> f = new SessionListenerFactory<BGPSessionListener>() {
61                         @Override
62                         public BGPSessionListener getSessionListener() {
63                                 return new SpeakerSessionListener();
64                         }
65                 };
66
67                 final BGPSessionPreferences prefs = new BGPSessionProposalImpl((short) 90, new ASNumber(25), IPv4.FAMILY.addressForString("127.0.0.2")).getProposal();
68
69                 final SessionNegotiatorFactory<BGPMessage, BGPSessionImpl, BGPSessionListener> snf = new BGPSessionNegotiatorFactory(new HashedWheelTimer(), prefs);
70
71                 final BGPSpeakerMock<BGPMessage, BGPSessionImpl, BGPSessionListener> mock = new BGPSpeakerMock<BGPMessage, BGPSessionImpl, BGPSessionListener>(snf, new BGPHandlerFactory(new BGPMessageFactoryImpl()), new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
72
73                 mock.createServer(new InetSocketAddress("127.0.0.2", 12345), f);
74         }
75 }