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;
12 import io.netty.channel.nio.NioEventLoopGroup;
13 import io.netty.channel.socket.SocketChannel;
14 import io.netty.util.HashedWheelTimer;
15 import io.netty.util.concurrent.DefaultPromise;
16 import io.netty.util.concurrent.GlobalEventExecutor;
17 import io.netty.util.concurrent.Promise;
19 import java.net.InetSocketAddress;
20 import java.util.HashMap;
23 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
24 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
25 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
26 import org.opendaylight.protocol.bgp.rib.impl.BGPHandlerFactory;
27 import org.opendaylight.protocol.bgp.rib.impl.BGPServerSessionNegotiatorFactory;
28 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionImpl;
29 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionProposalImpl;
30 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
31 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
32 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionValidator;
33 import org.opendaylight.protocol.bgp.rib.impl.spi.ReusableBGPPeer;
34 import org.opendaylight.protocol.framework.AbstractDispatcher;
35 import org.opendaylight.protocol.framework.ProtocolSession;
36 import org.opendaylight.protocol.framework.SessionListener;
37 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.LinkstateAddressFamily;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.LinkstateSubsequentAddressFamily;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
48 import org.opendaylight.yangtools.yang.binding.Notification;
50 public class BGPSpeakerMock<M, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> extends AbstractDispatcher<S, L> {
52 private final SessionNegotiatorFactory<M, S, L> negotiatorFactory;
53 private final BGPHandlerFactory factory;
55 public BGPSpeakerMock(final SessionNegotiatorFactory<M, S, L> negotiatorFactory, final BGPHandlerFactory factory,
56 final DefaultPromise<BGPSessionImpl> defaultPromise) {
57 super(GlobalEventExecutor.INSTANCE, new NioEventLoopGroup(), new NioEventLoopGroup());
58 this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
59 this.factory = Preconditions.checkNotNull(factory);
62 public void createServer(final InetSocketAddress address) {
63 super.createServer(address, new PipelineInitializer<S>() {
66 public void initializeChannel(final SocketChannel ch, final Promise<S> promise) {
67 ch.pipeline().addLast(BGPSpeakerMock.this.factory.getDecoders());
68 ch.pipeline().addLast("negotiator",
69 BGPSpeakerMock.this.negotiatorFactory.getSessionNegotiator(null, ch, promise));
70 ch.pipeline().addLast(BGPSpeakerMock.this.factory.getEncoders());
75 public static void main(final String[] args) throws Exception {
76 final Map<Class<? extends AddressFamily>, Class<? extends SubsequentAddressFamily>> tables = new HashMap<>();
77 tables.put(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
78 tables.put(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
80 final BGPPeerRegistry peerRegistry = new BGPPeerRegistry() {
82 public void addPeer(final IpAddress ip, final ReusableBGPPeer peer, final BGPSessionPreferences prefs) {}
85 public void removePeer(final IpAddress ip) {}
88 public boolean isPeerConfigured(final IpAddress ip) {
93 public BGPSessionListener getPeer(final IpAddress ip, final Ipv4Address sourceId, final Ipv4Address remoteId) throws BGPDocumentedException {
94 return new SpeakerSessionListener();
98 public BGPSessionPreferences getPeerPreferences(final IpAddress ip) {
99 return new BGPSessionProposalImpl((short) 90, new AsNumber(72L), new Ipv4Address("127.0.0.2"), tables).getProposal();
103 public void close() throws Exception {
108 final SessionNegotiatorFactory<Notification, BGPSessionImpl, BGPSessionListener> snf = new BGPServerSessionNegotiatorFactory(new HashedWheelTimer(), new BGPSessionValidator() {
110 public void validate(final Open openObj, final BGPSessionPreferences prefs) throws BGPDocumentedException {
115 final BGPSpeakerMock<Notification, BGPSessionImpl, BGPSessionListener> mock = new BGPSpeakerMock<>(snf, new BGPHandlerFactory(ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry()), new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
117 mock.createServer(new InetSocketAddress("127.0.0.2", 12345));