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;
18 import java.util.HashMap;
21 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
22 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
23 import org.opendaylight.protocol.bgp.rib.impl.BGPHandlerFactory;
24 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionImpl;
25 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionNegotiatorFactory;
26 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionProposalImpl;
27 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
28 import org.opendaylight.protocol.framework.AbstractDispatcher;
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.AsNumber;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.LinkstateAddressFamily;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.LinkstateSubsequentAddressFamily;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
41 import org.opendaylight.yangtools.yang.binding.Notification;
43 import com.google.common.base.Preconditions;
45 public class BGPSpeakerMock<M, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> extends AbstractDispatcher<S, L> {
47 private final SessionNegotiatorFactory<M, S, L> negotiatorFactory;
48 private final BGPHandlerFactory factory;
50 public BGPSpeakerMock(final SessionNegotiatorFactory<M, S, L> negotiatorFactory, final BGPHandlerFactory factory,
51 final DefaultPromise<BGPSessionImpl> defaultPromise) {
52 super(GlobalEventExecutor.INSTANCE, new NioEventLoopGroup(), new NioEventLoopGroup());
53 this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
54 this.factory = Preconditions.checkNotNull(factory);
57 public void createServer(final InetSocketAddress address, final SessionListenerFactory<L> listenerFactory) {
58 super.createServer(address, new PipelineInitializer<S>() {
61 public void initializeChannel(final SocketChannel ch, final Promise<S> promise) {
62 ch.pipeline().addLast(BGPSpeakerMock.this.factory.getDecoders());
63 ch.pipeline().addLast("negotiator",
64 BGPSpeakerMock.this.negotiatorFactory.getSessionNegotiator(listenerFactory, ch, promise));
65 ch.pipeline().addLast(BGPSpeakerMock.this.factory.getEncoders());
70 public static void main(final String[] args) throws Exception {
72 final SessionListenerFactory<BGPSessionListener> f = new SessionListenerFactory<BGPSessionListener>() {
74 public BGPSessionListener getSessionListener() {
75 return new SpeakerSessionListener();
79 final Map<Class<? extends AddressFamily>, Class<? extends SubsequentAddressFamily>> tables = new HashMap<>();
80 tables.put(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
81 tables.put(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
83 final BGPSessionPreferences prefs = new BGPSessionProposalImpl((short) 90, new AsNumber(72L), new Ipv4Address("127.0.0.2"), tables).getProposal();
85 final SessionNegotiatorFactory<Notification, BGPSessionImpl, BGPSessionListener> snf = new BGPSessionNegotiatorFactory(new HashedWheelTimer(), prefs, new AsNumber(72L));
87 final BGPSpeakerMock<Notification, BGPSessionImpl, BGPSessionListener> mock = new BGPSpeakerMock<>(snf, new BGPHandlerFactory(ServiceLoaderBGPExtensionProviderContext.createConsumerContext().getMessageRegistry()), new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
89 mock.createServer(new InetSocketAddress("127.0.0.2", 12345), f);