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.util.concurrent.DefaultPromise;
13 import io.netty.util.concurrent.GlobalEventExecutor;
14 import java.net.InetSocketAddress;
15 import java.util.HashMap;
17 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
18 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
19 import org.opendaylight.protocol.bgp.rib.impl.BGPDispatcherImpl;
20 import org.opendaylight.protocol.bgp.rib.impl.BGPHandlerFactory;
21 import org.opendaylight.protocol.bgp.rib.impl.BGPServerSessionNegotiatorFactory;
22 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionImpl;
23 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionProposalImpl;
24 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
25 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
26 import org.opendaylight.protocol.bgp.rib.impl.spi.ReusableBGPPeer;
27 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateAddressFamily;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateSubsequentAddressFamily;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
39 public class BGPSpeakerMock {
41 private final BGPServerSessionNegotiatorFactory negotiatorFactory;
42 private final BGPHandlerFactory factory;
43 private final BGPDispatcherImpl disp;
44 private final BGPPeerRegistry peerRegistry;
45 private final Map<Class<? extends AddressFamily>, Class<? extends SubsequentAddressFamily>> tables;
47 private BGPSpeakerMock(final BGPServerSessionNegotiatorFactory negotiatorFactory, final BGPHandlerFactory factory,
48 final DefaultPromise<BGPSessionImpl> defaultPromise) {
49 this.disp = new BGPDispatcherImpl(null, new NioEventLoopGroup(), new NioEventLoopGroup());
50 this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
51 this.factory = Preconditions.checkNotNull(factory);
54 this.peerRegistry = new BGPPeerRegistry() {
56 public void addPeer(final IpAddress ip, final ReusableBGPPeer peer, final BGPSessionPreferences prefs) {
60 public void removePeer(final IpAddress ip) {
64 public boolean isPeerConfigured(final IpAddress ip) {
69 public BGPSessionListener getPeer(final IpAddress ip, final Ipv4Address sourceId, final Ipv4Address remoteId, final Open open) throws BGPDocumentedException {
70 return new SpeakerSessionListener();
74 public BGPSessionPreferences getPeerPreferences(final IpAddress ip) {
75 return new BGPSessionProposalImpl((short) 90, new AsNumber(72L), new Ipv4Address("127.0.0.2"), BGPSpeakerMock.this.tables, new AsNumber(72L)).getProposal();
79 public void close() throws Exception {
84 public void removePeerSession(final IpAddress ip) {
88 this.tables = new HashMap<>();
89 this.tables.put(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
90 this.tables.put(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
93 public void main(final String[] args) {
95 final BGPServerSessionNegotiatorFactory snf = new BGPServerSessionNegotiatorFactory(this.peerRegistry);
97 final BGPSpeakerMock mock = new BGPSpeakerMock(snf, new BGPHandlerFactory(ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry()), new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
99 mock.createServer(new InetSocketAddress("127.0.0.2", 12345));
102 private void createServer(final InetSocketAddress address) {
103 this.disp.createServer(this.peerRegistry,address);