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