BUG-338 Allow incomming BGP connections.
[bgpcep.git] / bgp / testtool / src / test / java / org / opendaylight / protocol / bgp / testtool / BGPSpeakerMock.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.testtool;
9
10 import com.google.common.base.Preconditions;
11 import io.netty.channel.nio.NioEventLoopGroup;
12 import io.netty.channel.socket.SocketChannel;
13 import io.netty.util.HashedWheelTimer;
14 import io.netty.util.concurrent.DefaultPromise;
15 import io.netty.util.concurrent.GlobalEventExecutor;
16 import io.netty.util.concurrent.Promise;
17 import java.net.InetSocketAddress;
18 import java.util.HashMap;
19 import java.util.Map;
20 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
21 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
22 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
23 import org.opendaylight.protocol.bgp.rib.impl.BGPHandlerFactory;
24 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionImpl;
25 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionProposalImpl;
26 import org.opendaylight.protocol.bgp.rib.impl.server.BGPServerSessionNegotiatorFactory;
27 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
28 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
29 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionValidator;
30 import org.opendaylight.protocol.bgp.rib.impl.spi.ReusableBGPPeer;
31 import org.opendaylight.protocol.framework.AbstractDispatcher;
32 import org.opendaylight.protocol.framework.ProtocolSession;
33 import org.opendaylight.protocol.framework.SessionListener;
34 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.LinkstateAddressFamily;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.LinkstateSubsequentAddressFamily;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
45 import org.opendaylight.yangtools.yang.binding.Notification;
46
47 public class BGPSpeakerMock<M, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> extends AbstractDispatcher<S, L> {
48
49     private final SessionNegotiatorFactory<M, S, L> negotiatorFactory;
50     private final BGPHandlerFactory factory;
51
52     public BGPSpeakerMock(final SessionNegotiatorFactory<M, S, L> negotiatorFactory, final BGPHandlerFactory factory,
53             final DefaultPromise<BGPSessionImpl> defaultPromise) {
54         super(GlobalEventExecutor.INSTANCE, new NioEventLoopGroup(), new NioEventLoopGroup());
55         this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
56         this.factory = Preconditions.checkNotNull(factory);
57     }
58
59     public void createServer(final InetSocketAddress address) {
60         super.createServer(address, new PipelineInitializer<S>() {
61
62             @Override
63             public void initializeChannel(final SocketChannel ch, final Promise<S> promise) {
64                 ch.pipeline().addLast(BGPSpeakerMock.this.factory.getDecoders());
65                 ch.pipeline().addLast("negotiator",
66                         BGPSpeakerMock.this.negotiatorFactory.getSessionNegotiator(null, ch, promise));
67                 ch.pipeline().addLast(BGPSpeakerMock.this.factory.getEncoders());
68             }
69         });
70     }
71
72     public static void main(final String[] args) throws Exception {
73         final Map<Class<? extends AddressFamily>, Class<? extends SubsequentAddressFamily>> tables = new HashMap<>();
74         tables.put(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
75         tables.put(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
76
77         final BGPPeerRegistry peerRegistry = new BGPPeerRegistry() {
78             @Override
79             public void addPeer(final IpAddress ip, final ReusableBGPPeer peer, final BGPSessionPreferences prefs) {}
80
81             @Override
82             public void removePeer(final IpAddress ip) {}
83
84             @Override
85             public boolean isPeerConfigured(final IpAddress ip) {
86                 return true;
87             }
88
89             @Override
90             public BGPSessionListener getPeer(final IpAddress ip, final Ipv4Address sourceId, final Ipv4Address remoteId) throws BGPDocumentedException {
91                 return new SpeakerSessionListener();
92             }
93
94             @Override
95             public BGPSessionPreferences getPeerPreferences(final IpAddress ip) {
96                 return new BGPSessionProposalImpl((short) 90, new AsNumber(72L), new Ipv4Address("127.0.0.2"), tables).getProposal();
97             }
98
99             @Override
100             public void close() throws Exception {
101
102             }
103         };
104
105         final SessionNegotiatorFactory<Notification, BGPSessionImpl, BGPSessionListener> snf = new BGPServerSessionNegotiatorFactory(new HashedWheelTimer(), new BGPSessionValidator() {
106             @Override
107             public void validate(final Open openObj, final BGPSessionPreferences prefs) throws BGPDocumentedException {
108                 // NOOP
109             }
110         }, peerRegistry);
111
112         final BGPSpeakerMock<Notification, BGPSessionImpl, BGPSessionListener> mock = new BGPSpeakerMock<>(snf, new BGPHandlerFactory(ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry()), new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
113
114         mock.createServer(new InetSocketAddress("127.0.0.2", 12345));
115     }
116 }