Bug 498 - Fixed PCEP and BGP testtools
[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 io.netty.channel.nio.NioEventLoopGroup;
11 import io.netty.channel.socket.SocketChannel;
12 import io.netty.util.HashedWheelTimer;
13 import io.netty.util.concurrent.DefaultPromise;
14 import io.netty.util.concurrent.GlobalEventExecutor;
15 import io.netty.util.concurrent.Promise;
16
17 import java.net.InetSocketAddress;
18 import java.util.HashMap;
19 import java.util.Map;
20
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.BGPSessionNegotiatorFactory;
26 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionProposalImpl;
27 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
28 import org.opendaylight.protocol.framework.AbstractDispatcher;
29 import org.opendaylight.protocol.framework.ProtocolSession;
30 import org.opendaylight.protocol.framework.SessionListener;
31 import org.opendaylight.protocol.framework.SessionListenerFactory;
32 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.LinkstateAddressFamily;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.LinkstateSubsequentAddressFamily;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
41 import org.opendaylight.yangtools.yang.binding.Notification;
42
43 import com.google.common.base.Preconditions;
44
45 public class BGPSpeakerMock<M, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> extends AbstractDispatcher<S, L> {
46
47         private final SessionNegotiatorFactory<M, S, L> negotiatorFactory;
48         private final BGPHandlerFactory factory;
49
50         public BGPSpeakerMock(final SessionNegotiatorFactory<M, S, L> negotiatorFactory, final BGPHandlerFactory factory,
51                         final DefaultPromise<BGPSessionImpl> defaultPromise) {
52                 super(GlobalEventExecutor.INSTANCE, new NioEventLoopGroup(), new NioEventLoopGroup());
53                 this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
54                 this.factory = Preconditions.checkNotNull(factory);
55         }
56
57         public void createServer(final InetSocketAddress address, final SessionListenerFactory<L> listenerFactory) {
58                 super.createServer(address, new PipelineInitializer<S>() {
59
60                         @Override
61                         public void initializeChannel(final SocketChannel ch, final Promise<S> promise) {
62                                 ch.pipeline().addLast(BGPSpeakerMock.this.factory.getDecoders());
63                                 ch.pipeline().addLast("negotiator",
64                                                 BGPSpeakerMock.this.negotiatorFactory.getSessionNegotiator(listenerFactory, ch, promise));
65                                 ch.pipeline().addLast(BGPSpeakerMock.this.factory.getEncoders());
66                         }
67                 });
68         }
69
70         public static void main(final String[] args) throws Exception {
71
72                 final SessionListenerFactory<BGPSessionListener> f = new SessionListenerFactory<BGPSessionListener>() {
73                         @Override
74                         public BGPSessionListener getSessionListener() {
75                                 return new SpeakerSessionListener();
76                         }
77                 };
78
79                 final Map<Class<? extends AddressFamily>, Class<? extends SubsequentAddressFamily>> tables = new HashMap<>();
80                 tables.put(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
81                 tables.put(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
82
83                 final BGPSessionPreferences prefs = new BGPSessionProposalImpl((short) 90, new AsNumber(72L), new Ipv4Address("127.0.0.2"), tables).getProposal();
84
85                 final SessionNegotiatorFactory<Notification, BGPSessionImpl, BGPSessionListener> snf = new BGPSessionNegotiatorFactory(new HashedWheelTimer(), prefs, new AsNumber(72L));
86
87                 final BGPSpeakerMock<Notification, BGPSessionImpl, BGPSessionListener> mock = new BGPSpeakerMock<>(snf, new BGPHandlerFactory(ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry()), new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
88
89                 mock.createServer(new InetSocketAddress("127.0.0.2", 12345), f);
90         }
91 }