BUG-113: Factor MessageRegistry out of BGPMessageFactory
[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.socket.SocketChannel;
11 import io.netty.util.HashedWheelTimer;
12 import io.netty.util.concurrent.DefaultPromise;
13 import io.netty.util.concurrent.GlobalEventExecutor;
14 import io.netty.util.concurrent.Promise;
15
16 import java.io.IOException;
17 import java.net.InetSocketAddress;
18
19 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
20 import org.opendaylight.protocol.bgp.parser.impl.BGPMessageFactoryImpl;
21 import org.opendaylight.protocol.bgp.rib.impl.BGPHandlerFactory;
22 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionImpl;
23 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionNegotiatorFactory;
24 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionProposalImpl;
25 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
26 import org.opendaylight.protocol.framework.AbstractDispatcher;
27 import org.opendaylight.protocol.framework.ProtocolHandlerFactory;
28 import org.opendaylight.protocol.framework.ProtocolSession;
29 import org.opendaylight.protocol.framework.SessionListener;
30 import org.opendaylight.protocol.framework.SessionListenerFactory;
31 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
33 import org.opendaylight.yangtools.yang.binding.Notification;
34
35 import com.google.common.base.Preconditions;
36
37 public class BGPSpeakerMock<M, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> extends AbstractDispatcher<S, L> {
38
39         private final SessionNegotiatorFactory<M, S, L> negotiatorFactory;
40         private final ProtocolHandlerFactory<?> factory;
41
42         public BGPSpeakerMock(final SessionNegotiatorFactory<M, S, L> negotiatorFactory, final ProtocolHandlerFactory<?> factory,
43                         final DefaultPromise<BGPSessionImpl> defaultPromise) {
44                 this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
45                 this.factory = Preconditions.checkNotNull(factory);
46         }
47
48         public void createServer(final InetSocketAddress address, final SessionListenerFactory<L> listenerFactory) {
49                 super.createServer(address, new PipelineInitializer<S>() {
50
51                         @Override
52                         public void initializeChannel(final SocketChannel ch, final Promise<S> promise) {
53                                 ch.pipeline().addLast(BGPSpeakerMock.this.factory.getDecoders());
54                                 ch.pipeline().addLast("negotiator",
55                                                 BGPSpeakerMock.this.negotiatorFactory.getSessionNegotiator(listenerFactory, ch, promise));
56                                 ch.pipeline().addLast(BGPSpeakerMock.this.factory.getEncoders());
57                         }
58                 });
59         }
60
61         public static void main(final String[] args) throws IOException {
62
63                 final SessionListenerFactory<BGPSessionListener> f = new SessionListenerFactory<BGPSessionListener>() {
64                         @Override
65                         public BGPSessionListener getSessionListener() {
66                                 return new SpeakerSessionListener();
67                         }
68                 };
69
70                 final BGPSessionPreferences prefs = new BGPSessionProposalImpl((short) 90, 25, new Ipv4Address("127.0.0.2")).getProposal();
71
72                 final SessionNegotiatorFactory<Notification, BGPSessionImpl, BGPSessionListener> snf = new BGPSessionNegotiatorFactory(new HashedWheelTimer(), prefs);
73
74                 final BGPSpeakerMock<Notification, BGPSessionImpl, BGPSessionListener> mock = new BGPSpeakerMock<>(snf,
75                                 new BGPHandlerFactory(BGPMessageFactoryImpl.getInstance()),
76                                 new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
77
78                 mock.createServer(new InetSocketAddress("127.0.0.2", 12345), f);
79         }
80 }