BUG-45 : upgraded BGP Synchronization to use generated source code.
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPDispatcherImpl.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.rib.impl;
9
10 import io.netty.channel.socket.SocketChannel;
11 import io.netty.util.HashedWheelTimer;
12 import io.netty.util.Timer;
13 import io.netty.util.concurrent.Future;
14 import io.netty.util.concurrent.Promise;
15
16 import java.net.InetSocketAddress;
17
18 import org.opendaylight.protocol.bgp.parser.BGPMessageFactory;
19 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
20 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
21 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
22 import org.opendaylight.protocol.framework.AbstractDispatcher;
23 import org.opendaylight.protocol.framework.ReconnectStrategy;
24 import org.opendaylight.protocol.framework.SessionListenerFactory;
25
26 /**
27  * Implementation of BGPDispatcher.
28  */
29 public final class BGPDispatcherImpl extends AbstractDispatcher<BGPSessionImpl, BGPSessionListener> implements BGPDispatcher {
30         private final Timer timer = new HashedWheelTimer();
31
32         private final BGPHandlerFactory hf;
33
34         public BGPDispatcherImpl(final BGPMessageFactory parser) {
35                 super();
36                 this.hf = new BGPHandlerFactory(parser);
37         }
38
39         @Override
40         public Future<BGPSessionImpl> createClient(final InetSocketAddress address, final BGPSessionPreferences preferences,
41                         final BGPSessionListener listener, final ReconnectStrategy strategy) {
42                 final BGPSessionNegotiatorFactory snf = new BGPSessionNegotiatorFactory(this.timer, preferences);
43                 final SessionListenerFactory<BGPSessionListener> slf = new SessionListenerFactory<BGPSessionListener>() {
44                         @Override
45                         public BGPSessionListener getSessionListener() {
46                                 return listener;
47                         }
48                 };
49                 return super.createClient(address, strategy, new PipelineInitializer<BGPSessionImpl>() {
50                         @Override
51                         public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
52                                 ch.pipeline().addLast(hf.getDecoders());
53                                 ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(slf, ch, promise));
54                                 ch.pipeline().addLast(hf.getEncoders());
55                         }
56                 });
57         }
58 }