BUG-3888 : remove remote-as from argument list
[bgpcep.git] / bgp / testtool / src / test / java / org / opendaylight / protocol / bgp / testtool / BGPSpeakerMock.java
index 9ba7954047005fc89fbec0b74275617fce438686..347ba9c03641a394fc89bec560f110f2b5aad6b2 100644 (file)
@@ -7,39 +7,99 @@
  */
 package org.opendaylight.protocol.bgp.testtool;
 
-import io.netty.util.HashedWheelTimer;
-
-import java.io.IOException;
+import com.google.common.base.Preconditions;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.util.concurrent.DefaultPromise;
+import io.netty.util.concurrent.GlobalEventExecutor;
 import java.net.InetSocketAddress;
-
-import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
-import org.opendaylight.protocol.bgp.parser.impl.BGPMessageFactory;
-import org.opendaylight.protocol.bgp.rib.impl.BGPSessionNegotiatorFactory;
+import java.util.HashMap;
+import java.util.Map;
+import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
+import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
+import org.opendaylight.protocol.bgp.rib.impl.BGPDispatcherImpl;
+import org.opendaylight.protocol.bgp.rib.impl.BGPHandlerFactory;
+import org.opendaylight.protocol.bgp.rib.impl.BGPServerSessionNegotiatorFactory;
+import org.opendaylight.protocol.bgp.rib.impl.BGPSessionImpl;
 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionProposalImpl;
+import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
-import org.opendaylight.protocol.concepts.ASNumber;
-import org.opendaylight.protocol.concepts.IPv4;
-import org.opendaylight.protocol.framework.DispatcherImpl;
-import org.opendaylight.protocol.framework.SessionListenerFactory;
+import org.opendaylight.protocol.bgp.rib.impl.spi.ReusableBGPPeer;
+import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateAddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateSubsequentAddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
 
 public class BGPSpeakerMock {
 
-       DispatcherImpl dispatcher = new DispatcherImpl();
+    private final BGPServerSessionNegotiatorFactory negotiatorFactory;
+    private final BGPHandlerFactory factory;
+    private final BGPDispatcherImpl disp;
+    private final BGPPeerRegistry peerRegistry;
+    private final Map<Class<? extends AddressFamily>, Class<? extends SubsequentAddressFamily>> tables;
+
+    private BGPSpeakerMock(final BGPServerSessionNegotiatorFactory negotiatorFactory, final BGPHandlerFactory factory,
+                           final DefaultPromise<BGPSessionImpl> defaultPromise) {
+        this.disp = new BGPDispatcherImpl(null, new NioEventLoopGroup(), new NioEventLoopGroup());
+        this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
+        this.factory = Preconditions.checkNotNull(factory);
+
+
+        this.peerRegistry = new BGPPeerRegistry() {
+            @Override
+            public void addPeer(final IpAddress ip, final ReusableBGPPeer peer, final BGPSessionPreferences prefs) {
+            }
+
+            @Override
+            public void removePeer(final IpAddress ip) {
+            }
+
+            @Override
+            public boolean isPeerConfigured(final IpAddress ip) {
+                return true;
+            }
+
+            @Override
+            public BGPSessionListener getPeer(final IpAddress ip, final Ipv4Address sourceId, final Ipv4Address remoteId, final Open open) throws BGPDocumentedException {
+                return new SpeakerSessionListener();
+            }
+
+            @Override
+            public BGPSessionPreferences getPeerPreferences(final IpAddress ip) {
+                return new BGPSessionProposalImpl((short) 90, new AsNumber(72L), new Ipv4Address("127.0.0.2"), BGPSpeakerMock.this.tables, new AsNumber(72L)).getProposal();
+            }
+
+            @Override
+            public void close() throws Exception {
+
+            }
+
+            @Override
+            public void removePeerSession(final IpAddress ip) {
+            }
+        };
+
+        this.tables = new HashMap<>();
+        this.tables.put(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
+        this.tables.put(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
+    }
 
-       public static void main(final String[] args) throws IOException {
+    public void main(final String[] args) {
 
-               final BGPSpeakerMock m = new BGPSpeakerMock();
+        final BGPServerSessionNegotiatorFactory snf = new BGPServerSessionNegotiatorFactory(this.peerRegistry);
 
-               final BGPSessionPreferences prefs = new BGPSessionProposalImpl((short) 90, new ASNumber(25), IPv4.FAMILY.addressForString("127.0.0.2")).getProposal();
+        final BGPSpeakerMock mock = new BGPSpeakerMock(snf, new BGPHandlerFactory(ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry()), new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
 
-               final SessionListenerFactory<BGPSessionListener> f = new SessionListenerFactory<BGPSessionListener>() {
-                       @Override
-                       public BGPSessionListener getSessionListener() {
-                               return new SpeakerSessionListener(m.dispatcher);
-                       }
-               };
+        mock.createServer(new InetSocketAddress("127.0.0.2", 12345));
+    }
 
-               m.dispatcher.createServer(new InetSocketAddress("127.0.0.2", 12345), f,
-                               new BGPSessionNegotiatorFactory(new HashedWheelTimer(), prefs), new BGPMessageFactory());
-       }
+    private void createServer(final InetSocketAddress address) {
+        this.disp.createServer(this.peerRegistry,address);
+    }
 }