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