Merge "BUG-54 : switched channel pipeline to be protocol specific. Added LengthFrameD...
[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.BGPSession;
20 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
21 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
22 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
23 import org.opendaylight.protocol.framework.AbstractDispatcher;
24 import org.opendaylight.protocol.framework.ReconnectStrategy;
25 import org.opendaylight.protocol.framework.SessionListenerFactory;
26
27 /**
28  * Implementation of BGPDispatcher.
29  */
30 public final class BGPDispatcherImpl extends AbstractDispatcher<BGPSessionImpl, BGPSessionListener> implements BGPDispatcher {
31         private final Timer timer = new HashedWheelTimer();
32
33         private BGPSessionNegotiatorFactory snf;
34
35         private final BGPHandlerFactory hf;
36
37         public BGPDispatcherImpl(final BGPMessageFactory parser) {
38                 super();
39                 this.hf = new BGPHandlerFactory(parser);
40         }
41
42         @Override
43         public Future<? extends BGPSession> createClient(final InetSocketAddress address, final BGPSessionPreferences preferences,
44                         final BGPSessionListener listener, final ReconnectStrategy strategy) {
45                 this.snf = new BGPSessionNegotiatorFactory(this.timer, preferences);
46                 final SessionListenerFactory<BGPSessionListener> slf = new SessionListenerFactory<BGPSessionListener>() {
47
48                         @Override
49                         public BGPSessionListener getSessionListener() {
50                                 return listener;
51                         }
52                 };
53                 return super.createClient(address, strategy, slf);
54         }
55
56         @Override
57         public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise,
58                         final SessionListenerFactory<BGPSessionListener> slf) {
59                 ch.pipeline().addLast(this.hf.getDecoders());
60                 ch.pipeline().addLast("negotiator", this.snf.getSessionNegotiator(slf, ch, promise));
61                 ch.pipeline().addLast(this.hf.getEncoders());
62         }
63 }