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.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;
16 import java.io.IOException;
17 import java.net.InetSocketAddress;
19 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
20 import org.opendaylight.protocol.bgp.parser.impl.BGPMessageFactoryImpl;
21 import org.opendaylight.protocol.bgp.parser.spi.pojo.BGPExtensionConsumerContextImpl;
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.framework.AbstractDispatcher;
28 import org.opendaylight.protocol.framework.ProtocolHandlerFactory;
29 import org.opendaylight.protocol.framework.ProtocolSession;
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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
34 import org.opendaylight.yangtools.yang.binding.Notification;
36 import com.google.common.base.Preconditions;
38 public class BGPSpeakerMock<M, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> extends AbstractDispatcher<S, L> {
40 private final SessionNegotiatorFactory<M, S, L> negotiatorFactory;
41 private final ProtocolHandlerFactory<?> factory;
43 public BGPSpeakerMock(final SessionNegotiatorFactory<M, S, L> negotiatorFactory, final ProtocolHandlerFactory<?> factory,
44 final DefaultPromise<BGPSessionImpl> defaultPromise) {
45 this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
46 this.factory = Preconditions.checkNotNull(factory);
49 public void createServer(final InetSocketAddress address, final SessionListenerFactory<L> listenerFactory) {
50 super.createServer(address, new PipelineInitializer<S>() {
53 public void initializeChannel(final SocketChannel ch, final Promise<S> promise) {
54 ch.pipeline().addLast(BGPSpeakerMock.this.factory.getDecoders());
55 ch.pipeline().addLast("negotiator",
56 BGPSpeakerMock.this.negotiatorFactory.getSessionNegotiator(listenerFactory, ch, promise));
57 ch.pipeline().addLast(BGPSpeakerMock.this.factory.getEncoders());
62 public static void main(final String[] args) throws IOException {
64 final SessionListenerFactory<BGPSessionListener> f = new SessionListenerFactory<BGPSessionListener>() {
66 public BGPSessionListener getSessionListener() {
67 return new SpeakerSessionListener();
71 final BGPSessionPreferences prefs = new BGPSessionProposalImpl((short) 90, 25, new Ipv4Address("127.0.0.2")).getProposal();
73 final SessionNegotiatorFactory<Notification, BGPSessionImpl, BGPSessionListener> snf = new BGPSessionNegotiatorFactory(new HashedWheelTimer(), prefs);
75 final BGPSpeakerMock<Notification, BGPSessionImpl, BGPSessionListener> mock = new BGPSpeakerMock<>(snf,
76 new BGPHandlerFactory(new BGPMessageFactoryImpl(BGPExtensionConsumerContextImpl.getSingletonInstance().getMessageRegistry())),
77 new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
79 mock.createServer(new InetSocketAddress("127.0.0.2", 12345), f);