Fix the dispatcher not being threadsafe
[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 final BGPHandlerFactory hf;
34
35         public BGPDispatcherImpl(final BGPMessageFactory parser) {
36                 super();
37                 this.hf = new BGPHandlerFactory(parser);
38         }
39
40         @Override
41         public Future<? extends BGPSession> createClient(final InetSocketAddress address, final BGPSessionPreferences preferences,
42                         final BGPSessionListener listener, final ReconnectStrategy strategy) {
43                 final BGPSessionNegotiatorFactory snf = new BGPSessionNegotiatorFactory(this.timer, preferences);
44                 final SessionListenerFactory<BGPSessionListener> slf = new SessionListenerFactory<BGPSessionListener>() {
45                         @Override
46                         public BGPSessionListener getSessionListener() {
47                                 return listener;
48                         }
49                 };
50                 return super.createClient(address, strategy, new PipelineInitializer<BGPSessionImpl>() {
51                         @Override
52                         public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
53                                 ch.pipeline().addLast(hf.getDecoders());
54                                 ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(slf, ch, promise));
55                                 ch.pipeline().addLast(hf.getEncoders());
56                         }
57                 });
58         }
59 }