Switched BGPMessage concept to yangtools.binding.Notification.
[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.concepts.IPv4;
27 import org.opendaylight.protocol.framework.AbstractDispatcher;
28 import org.opendaylight.protocol.framework.ProtocolHandlerFactory;
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.yangtools.yang.binding.Notification;
35
36 import com.google.common.base.Preconditions;
37
38 public class BGPSpeakerMock<M, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> extends AbstractDispatcher<S, L> {
39
40         private final SessionNegotiatorFactory<M, S, L> negotiatorFactory;
41         private final ProtocolHandlerFactory<?> factory;
42
43         public BGPSpeakerMock(final SessionNegotiatorFactory<M, S, L> negotiatorFactory, final ProtocolHandlerFactory<?> factory,
44                         final DefaultPromise<BGPSessionImpl> defaultPromise) {
45                 this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
46                 this.factory = Preconditions.checkNotNull(factory);
47         }
48
49         public void createServer(final InetSocketAddress address, final SessionListenerFactory<L> listenerFactory) {
50                 super.createServer(address, new PipelineInitializer<S>() {
51
52                         @Override
53                         public void initializeChannel(final SocketChannel ch, final Promise<S> promise) {
54                                 ch.pipeline().addLast(BGPSpeakerMock.this.factory.getDecoders());
55                                 ch.pipeline().addLast("negotiator",
56                                                 BGPSpeakerMock.this.negotiatorFactory.getSessionNegotiator(listenerFactory, ch, promise));
57                                 ch.pipeline().addLast(BGPSpeakerMock.this.factory.getEncoders());
58                         }
59                 });
60         }
61
62         public static void main(final String[] args) throws IOException {
63
64                 final SessionListenerFactory<BGPSessionListener> f = new SessionListenerFactory<BGPSessionListener>() {
65                         @Override
66                         public BGPSessionListener getSessionListener() {
67                                 return new SpeakerSessionListener();
68                         }
69                 };
70
71                 final BGPSessionPreferences prefs = new BGPSessionProposalImpl((short) 90, new AsNumber((long) 25), IPv4.FAMILY.addressForString("127.0.0.2")).getProposal();
72
73                 final SessionNegotiatorFactory<Notification, BGPSessionImpl, BGPSessionListener> snf = new BGPSessionNegotiatorFactory(new HashedWheelTimer(), prefs);
74
75                 final BGPSpeakerMock<Notification, BGPSessionImpl, BGPSessionListener> mock = new BGPSpeakerMock<Notification, BGPSessionImpl, BGPSessionListener>(snf, new BGPHandlerFactory(new BGPMessageFactoryImpl()), new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
76
77                 mock.createServer(new InetSocketAddress("127.0.0.2", 12345), f);
78         }
79 }