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 com.google.common.collect.Lists;
12 import io.netty.channel.nio.NioEventLoopGroup;
13 import io.netty.util.concurrent.DefaultPromise;
14 import io.netty.util.concurrent.GlobalEventExecutor;
15 import java.net.InetSocketAddress;
16 import java.util.Collections;
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.spi.BGPPeerRegistry;
24 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
25 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateAddressFamily;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateSubsequentAddressFamily;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
36 public class BGPSpeakerMock {
38 private final BGPServerSessionNegotiatorFactory negotiatorFactory;
39 private final BGPHandlerFactory factory;
40 private final BGPDispatcherImpl disp;
41 private final BGPPeerRegistry peerRegistry;
43 private BGPSpeakerMock(final BGPServerSessionNegotiatorFactory negotiatorFactory, final BGPHandlerFactory factory,
44 final DefaultPromise<BGPSessionImpl> defaultPromise) {
45 this.disp = new BGPDispatcherImpl(null, new NioEventLoopGroup(), new NioEventLoopGroup());
46 this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
47 this.factory = Preconditions.checkNotNull(factory);
50 this.peerRegistry = new BGPPeerRegistry() {
52 public void addPeer(final IpAddress ip, final BGPSessionListener peer, final BGPSessionPreferences prefs) {
56 public void removePeer(final IpAddress ip) {
60 public boolean isPeerConfigured(final IpAddress ip) {
65 public BGPSessionListener getPeer(final IpAddress ip, final Ipv4Address sourceId, final Ipv4Address remoteId, final Open open) throws BGPDocumentedException {
66 return new SpeakerSessionListener();
70 public BGPSessionPreferences getPeerPreferences(final IpAddress ip) {
71 final BgpParameters bgpParameters = Main.createBgpParameters(Lists.newArrayList(
72 Main.createMPCapability(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class),
73 Main.createMPCapability(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class)));
74 return new BGPSessionPreferences(new AsNumber(72L), (short) 90, new Ipv4Address("127.0.0.2"), new AsNumber(72L), Collections.singletonList(bgpParameters));
78 public void close() throws Exception {
83 public void removePeerSession(final IpAddress ip) {
88 public void main(final String[] args) {
90 final BGPServerSessionNegotiatorFactory snf = new BGPServerSessionNegotiatorFactory(this.peerRegistry);
92 final BGPSpeakerMock mock = new BGPSpeakerMock(snf, new BGPHandlerFactory(ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry()), new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
94 mock.createServer(new InetSocketAddress("127.0.0.2", 12345));
97 private void createServer(final InetSocketAddress address) {
98 this.disp.createServer(this.peerRegistry,address);