BUG-353: add remote-as awareness
[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.EventLoopGroup;
11 import io.netty.channel.socket.SocketChannel;
12 import io.netty.util.HashedWheelTimer;
13 import io.netty.util.Timer;
14 import io.netty.util.concurrent.Future;
15 import io.netty.util.concurrent.Promise;
16
17 import java.net.InetSocketAddress;
18
19 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
20 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
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.ReconnectStrategyFactory;
26 import org.opendaylight.protocol.framework.SessionListenerFactory;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
28
29 /**
30  * Implementation of BGPDispatcher.
31  */
32 public final class BGPDispatcherImpl extends AbstractDispatcher<BGPSessionImpl, BGPSessionListener> implements BGPDispatcher, AutoCloseable {
33         private final Timer timer = new HashedWheelTimer();
34
35         private final BGPHandlerFactory hf;
36
37         public BGPDispatcherImpl(final MessageRegistry messageRegistry, final EventLoopGroup bossGroup, final EventLoopGroup workerGroup) {
38                 super(bossGroup, workerGroup);
39                 this.hf = new BGPHandlerFactory(messageRegistry);
40         }
41
42         @Override
43         public Future<BGPSessionImpl> createClient(final InetSocketAddress address, final BGPSessionPreferences preferences,
44                         final AsNumber remoteAs, final BGPSessionListener listener, final ReconnectStrategy strategy) {
45                 final BGPSessionNegotiatorFactory snf = new BGPSessionNegotiatorFactory(this.timer, preferences, remoteAs);
46                 final SessionListenerFactory<BGPSessionListener> slf = new SessionListenerFactory<BGPSessionListener>() {
47                         @Override
48                         public BGPSessionListener getSessionListener() {
49                                 return listener;
50                         }
51                 };
52                 return super.createClient(address, strategy, new PipelineInitializer<BGPSessionImpl>() {
53                         @Override
54                         public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
55                                 ch.pipeline().addLast(hf.getDecoders());
56                                 ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(slf, ch, promise));
57                                 ch.pipeline().addLast(hf.getEncoders());
58                         }
59                 });
60         }
61
62         @Override
63         public Future<Void> createReconnectingClient(final InetSocketAddress address,
64                         final BGPSessionPreferences preferences, final AsNumber remoteAs,
65                         final BGPSessionListener listener, final ReconnectStrategyFactory connectStrategyFactory,
66                         final ReconnectStrategy reestablishStrategy) {
67                 final BGPSessionNegotiatorFactory snf = new BGPSessionNegotiatorFactory(this.timer, preferences, remoteAs);
68                 final SessionListenerFactory<BGPSessionListener> slf = new SessionListenerFactory<BGPSessionListener>() {
69                         @Override
70                         public BGPSessionListener getSessionListener() {
71                                 return listener;
72                         }
73                 };
74
75                 return super.createReconnectingClient(address, connectStrategyFactory, reestablishStrategy, new PipelineInitializer<BGPSessionImpl>() {
76                         @Override
77                         public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
78                                 ch.pipeline().addLast(hf.getDecoders());
79                                 ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(slf, ch, promise));
80                                 ch.pipeline().addLast(hf.getEncoders());
81                         }
82                 });
83         }
84
85         @Override
86         public void close() {
87         }
88 }