-import org.opendaylight.protocol.concepts.ASNumber;
-import org.opendaylight.protocol.concepts.IPv4;
-import org.opendaylight.protocol.framework.DispatcherImpl;
-import org.opendaylight.protocol.framework.ProtocolMessageFactory;
-
-public class BGPSpeakerMock {
-
- DispatcherImpl dispatcher;
-
- BGPSpeakerMock() throws IOException {
- this.dispatcher = new DispatcherImpl(new BGPMessageFactory());
- }
-
- public static void main(final String[] args) throws IOException {
-
- final BGPSpeakerMock m = new BGPSpeakerMock();
-
- final ProtocolMessageFactory parser = new BGPMessageFactory();
-
- m.dispatcher.createServer(new InetSocketAddress("127.0.0.2", 12345), new BGPConnectionFactory() {
- @Override
- public BGPConnection createProtocolConnection(final InetSocketAddress address) {
- final BGPSessionProposalImpl prop = new BGPSessionProposalImpl((short) 90, new ASNumber(25), IPv4.FAMILY.addressForString("127.0.0.2"));
- final BGPSessionPreferences prefs = prop.getProposal();
- try {
- prop.close();
- } catch (final IOException e) {
- e.printStackTrace();
- }
- return new BGPConnectionImpl(address, new SpeakerSessionListener(m.dispatcher), prefs, new BGPSessionProposalCheckerImpl());
- }
- }, new BGPSessionFactory(parser));
- }
+import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionValidator;
+import org.opendaylight.protocol.bgp.rib.impl.spi.ReusableBGPPeer;
+import org.opendaylight.protocol.framework.AbstractDispatcher;
+import org.opendaylight.protocol.framework.ProtocolSession;
+import org.opendaylight.protocol.framework.SessionListener;
+import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
+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.rev131125.LinkstateAddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.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;
+import org.opendaylight.yangtools.yang.binding.Notification;
+
+public class BGPSpeakerMock<M, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> extends AbstractDispatcher<S, L> {
+
+ private final SessionNegotiatorFactory<M, S, L> negotiatorFactory;
+ private final BGPHandlerFactory factory;
+
+ public BGPSpeakerMock(final SessionNegotiatorFactory<M, S, L> negotiatorFactory, final BGPHandlerFactory factory,
+ final DefaultPromise<BGPSessionImpl> defaultPromise) {
+ super(GlobalEventExecutor.INSTANCE, new NioEventLoopGroup(), new NioEventLoopGroup());
+ this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
+ this.factory = Preconditions.checkNotNull(factory);
+ }
+
+ public void createServer(final InetSocketAddress address) {
+ super.createServer(address, new PipelineInitializer<S>() {
+
+ @Override
+ public void initializeChannel(final SocketChannel ch, final Promise<S> promise) {
+ ch.pipeline().addLast(BGPSpeakerMock.this.factory.getDecoders());
+ ch.pipeline().addLast("negotiator",
+ BGPSpeakerMock.this.negotiatorFactory.getSessionNegotiator(null, ch, promise));
+ ch.pipeline().addLast(BGPSpeakerMock.this.factory.getEncoders());
+ }
+ });
+ }
+
+ public static void main(final String[] args) throws Exception {
+ final Map<Class<? extends AddressFamily>, Class<? extends SubsequentAddressFamily>> tables = new HashMap<>();
+ tables.put(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
+ tables.put(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
+
+ final BGPPeerRegistry 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) 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"), tables).getProposal();
+ }
+
+ @Override
+ public void close() throws Exception {
+
+ }
+ };
+
+ final SessionNegotiatorFactory<Notification, BGPSessionImpl, BGPSessionListener> snf = new BGPServerSessionNegotiatorFactory(new HashedWheelTimer(), new BGPSessionValidator() {
+ @Override
+ public void validate(final Open openObj, final BGPSessionPreferences prefs) throws BGPDocumentedException {
+ // NOOP
+ }
+ }, peerRegistry);
+
+ final BGPSpeakerMock<Notification, BGPSessionImpl, BGPSessionListener> mock = new BGPSpeakerMock<>(snf, new BGPHandlerFactory(ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry()), new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
+
+ mock.createServer(new InetSocketAddress("127.0.0.2", 12345));
+ }