8264d83318b1c584ea14ea413f9e9fe5201c7ed6
[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 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.GlobalEventExecutor;
16 import io.netty.util.concurrent.Promise;
17 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
18 import org.opendaylight.protocol.bgp.parser.impl.BGPMessageFactoryImpl;
19 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
20 import org.opendaylight.protocol.bgp.rib.impl.BGPHandlerFactory;
21 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionImpl;
22 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionNegotiatorFactory;
23 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionProposalImpl;
24 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
25 import org.opendaylight.protocol.framework.AbstractDispatcher;
26 import org.opendaylight.protocol.framework.ProtocolHandlerFactory;
27 import org.opendaylight.protocol.framework.ProtocolSession;
28 import org.opendaylight.protocol.framework.SessionListener;
29 import org.opendaylight.protocol.framework.SessionListenerFactory;
30 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
32 import org.opendaylight.yangtools.yang.binding.Notification;
33
34 import java.net.InetSocketAddress;
35
36 public class BGPSpeakerMock<M, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> extends AbstractDispatcher<S, L> {
37
38         private final SessionNegotiatorFactory<M, S, L> negotiatorFactory;
39         private final ProtocolHandlerFactory<?> factory;
40
41         public BGPSpeakerMock(final SessionNegotiatorFactory<M, S, L> negotiatorFactory, final ProtocolHandlerFactory<?> factory,
42                         final DefaultPromise<BGPSessionImpl> defaultPromise) {
43                 super(new NioEventLoopGroup(), new NioEventLoopGroup());
44                 this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
45                 this.factory = Preconditions.checkNotNull(factory);
46         }
47
48         public void createServer(final InetSocketAddress address, final SessionListenerFactory<L> listenerFactory) {
49                 super.createServer(address, new PipelineInitializer<S>() {
50
51                         @Override
52                         public void initializeChannel(final SocketChannel ch, final Promise<S> promise) {
53                                 ch.pipeline().addLast(BGPSpeakerMock.this.factory.getDecoders());
54                                 ch.pipeline().addLast("negotiator",
55                                                 BGPSpeakerMock.this.negotiatorFactory.getSessionNegotiator(listenerFactory, ch, promise));
56                                 ch.pipeline().addLast(BGPSpeakerMock.this.factory.getEncoders());
57                         }
58                 });
59         }
60
61         public static void main(final String[] args) throws Exception {
62
63                 final SessionListenerFactory<BGPSessionListener> f = new SessionListenerFactory<BGPSessionListener>() {
64                         @Override
65                         public BGPSessionListener getSessionListener() {
66                                 return new SpeakerSessionListener();
67                         }
68                 };
69
70                 final BGPSessionPreferences prefs = new BGPSessionProposalImpl((short) 90, 25, new Ipv4Address("127.0.0.2")).getProposal();
71
72                 final SessionNegotiatorFactory<Notification, BGPSessionImpl, BGPSessionListener> snf = new BGPSessionNegotiatorFactory(new HashedWheelTimer(), prefs);
73
74                 final BGPSpeakerMock<Notification, BGPSessionImpl, BGPSessionListener> mock = new BGPSpeakerMock<>(snf,
75                                 new BGPHandlerFactory(new BGPMessageFactoryImpl(ServiceLoaderBGPExtensionProviderContext.createConsumerContext().getMessageRegistry())),
76                                 new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
77
78                 mock.createServer(new InetSocketAddress("127.0.0.2", 12345), f);
79         }
80 }