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 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 java.net.InetSocketAddress;
18 import java.util.HashMap;
20 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
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.BGPSessionProposalImpl;
26 import org.opendaylight.protocol.bgp.rib.impl.server.BGPServerSessionNegotiatorFactory;
27 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
28 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
29 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionValidator;
30 import org.opendaylight.protocol.bgp.rib.impl.spi.ReusableBGPPeer;
31 import org.opendaylight.protocol.framework.AbstractDispatcher;
32 import org.opendaylight.protocol.framework.ProtocolSession;
33 import org.opendaylight.protocol.framework.SessionListener;
34 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.LinkstateAddressFamily;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.LinkstateSubsequentAddressFamily;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
45 import org.opendaylight.yangtools.yang.binding.Notification;
47 public class BGPSpeakerMock<M, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> extends AbstractDispatcher<S, L> {
49 private final SessionNegotiatorFactory<M, S, L> negotiatorFactory;
50 private final BGPHandlerFactory factory;
52 public BGPSpeakerMock(final SessionNegotiatorFactory<M, S, L> negotiatorFactory, final BGPHandlerFactory factory,
53 final DefaultPromise<BGPSessionImpl> defaultPromise) {
54 super(GlobalEventExecutor.INSTANCE, new NioEventLoopGroup(), new NioEventLoopGroup());
55 this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
56 this.factory = Preconditions.checkNotNull(factory);
59 public void createServer(final InetSocketAddress address) {
60 super.createServer(address, new PipelineInitializer<S>() {
63 public void initializeChannel(final SocketChannel ch, final Promise<S> promise) {
64 ch.pipeline().addLast(BGPSpeakerMock.this.factory.getDecoders());
65 ch.pipeline().addLast("negotiator",
66 BGPSpeakerMock.this.negotiatorFactory.getSessionNegotiator(null, ch, promise));
67 ch.pipeline().addLast(BGPSpeakerMock.this.factory.getEncoders());
72 public static void main(final String[] args) throws Exception {
73 final Map<Class<? extends AddressFamily>, Class<? extends SubsequentAddressFamily>> tables = new HashMap<>();
74 tables.put(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
75 tables.put(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
77 final BGPPeerRegistry peerRegistry = new BGPPeerRegistry() {
79 public void addPeer(final IpAddress ip, final ReusableBGPPeer peer, final BGPSessionPreferences prefs) {}
82 public void removePeer(final IpAddress ip) {}
85 public boolean isPeerConfigured(final IpAddress ip) {
90 public BGPSessionListener getPeer(final IpAddress ip, final Ipv4Address sourceId, final Ipv4Address remoteId) throws BGPDocumentedException {
91 return new SpeakerSessionListener();
95 public BGPSessionPreferences getPeerPreferences(final IpAddress ip) {
96 return new BGPSessionProposalImpl((short) 90, new AsNumber(72L), new Ipv4Address("127.0.0.2"), tables).getProposal();
100 public void close() throws Exception {
105 final SessionNegotiatorFactory<Notification, BGPSessionImpl, BGPSessionListener> snf = new BGPServerSessionNegotiatorFactory(new HashedWheelTimer(), new BGPSessionValidator() {
107 public void validate(final Open openObj, final BGPSessionPreferences prefs) throws BGPDocumentedException {
112 final BGPSpeakerMock<Notification, BGPSessionImpl, BGPSessionListener> mock = new BGPSpeakerMock<>(snf, new BGPHandlerFactory(ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry()), new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
114 mock.createServer(new InetSocketAddress("127.0.0.2", 12345));