2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.testtool;
10 import io.netty.channel.nio.NioEventLoopGroup;
11 import io.netty.channel.socket.SocketChannel;
12 import io.netty.util.HashedWheelTimer;
13 import io.netty.util.concurrent.DefaultPromise;
14 import io.netty.util.concurrent.GlobalEventExecutor;
15 import io.netty.util.concurrent.Promise;
17 import java.net.InetSocketAddress;
19 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
20 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
21 import org.opendaylight.protocol.bgp.rib.impl.BGPHandlerFactory;
22 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionImpl;
23 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionNegotiatorFactory;
24 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionProposalImpl;
25 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
26 import org.opendaylight.protocol.framework.AbstractDispatcher;
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;
34 import com.google.common.base.Preconditions;
36 public class BGPSpeakerMock<M, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> extends AbstractDispatcher<S, L> {
38 private final SessionNegotiatorFactory<M, S, L> negotiatorFactory;
39 private final BGPHandlerFactory factory;
41 public BGPSpeakerMock(final SessionNegotiatorFactory<M, S, L> negotiatorFactory, final BGPHandlerFactory factory,
42 final DefaultPromise<BGPSessionImpl> defaultPromise) {
43 super(GlobalEventExecutor.INSTANCE, new NioEventLoopGroup(), new NioEventLoopGroup());
44 this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
45 this.factory = Preconditions.checkNotNull(factory);
48 public void createServer(final InetSocketAddress address, final SessionListenerFactory<L> listenerFactory) {
49 super.createServer(address, new PipelineInitializer<S>() {
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());
61 public static void main(final String[] args) throws Exception {
63 final SessionListenerFactory<BGPSessionListener> f = new SessionListenerFactory<BGPSessionListener>() {
65 public BGPSessionListener getSessionListener() {
66 return new SpeakerSessionListener();
70 final BGPSessionPreferences prefs = new BGPSessionProposalImpl((short) 90, 25, new Ipv4Address("127.0.0.2")).getProposal();
72 final SessionNegotiatorFactory<Notification, BGPSessionImpl, BGPSessionListener> snf = new BGPSessionNegotiatorFactory(new HashedWheelTimer(), prefs);
74 final BGPSpeakerMock<Notification, BGPSessionImpl, BGPSessionListener> mock = new BGPSpeakerMock<>(snf,
75 new BGPHandlerFactory(ServiceLoaderBGPExtensionProviderContext.createConsumerContext().getMessageRegistry()),
76 new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
78 mock.createServer(new InetSocketAddress("127.0.0.2", 12345), f);