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.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.IPv4;
28 import org.opendaylight.protocol.framework.AbstractDispatcher;
29 import org.opendaylight.protocol.framework.ProtocolHandlerFactory;
30 import org.opendaylight.protocol.framework.ProtocolSession;
31 import org.opendaylight.protocol.framework.SessionListener;
32 import org.opendaylight.protocol.framework.SessionListenerFactory;
33 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
36 import com.google.common.base.Preconditions;
38 public class BGPSpeakerMock<M, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> extends
39 AbstractDispatcher<S, L> {
41 private final SessionNegotiatorFactory<M, S, L> negotiatorFactory;
42 private final ProtocolHandlerFactory<?> factory;
44 public BGPSpeakerMock(final SessionNegotiatorFactory<M, S, L> negotiatorFactory, final ProtocolHandlerFactory<?> factory,
45 final DefaultPromise<BGPSessionImpl> defaultPromise) {
46 this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
47 this.factory = Preconditions.checkNotNull(factory);
50 public void createServer(final InetSocketAddress address, final SessionListenerFactory<L> listenerFactory) {
51 super.createServer(address, new PipelineInitializer<S>() {
54 public void initializeChannel(final SocketChannel ch, final Promise<S> promise) {
55 ch.pipeline().addLast(factory.getDecoders());
56 ch.pipeline().addLast("negotiator", negotiatorFactory.getSessionNegotiator(listenerFactory, ch, promise));
57 ch.pipeline().addLast(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, new AsNumber((long) 25), IPv4.FAMILY.addressForString("127.0.0.2")).getProposal();
73 final SessionNegotiatorFactory<BGPMessage, BGPSessionImpl, BGPSessionListener> snf = new BGPSessionNegotiatorFactory(new HashedWheelTimer(), prefs);
75 final BGPSpeakerMock<BGPMessage, BGPSessionImpl, BGPSessionListener> mock = new BGPSpeakerMock<BGPMessage, BGPSessionImpl, BGPSessionListener>(snf, new BGPHandlerFactory(new BGPMessageFactoryImpl()), new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
77 mock.createServer(new InetSocketAddress("127.0.0.2", 12345), f);