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