BUG-3888 : refactoring, removed unused session validators
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / TestClientDispatcher.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.protocol.bgp.rib.impl;
10
11 import com.google.common.base.Optional;
12 import io.netty.bootstrap.Bootstrap;
13 import io.netty.channel.EventLoopGroup;
14 import io.netty.util.concurrent.Future;
15 import java.net.InetSocketAddress;
16 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
17 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
18 import org.opendaylight.protocol.framework.ReconnectStrategy;
19 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
21
22 public class TestClientDispatcher {
23
24     private final BGPHandlerFactory hf;
25     private final InetSocketAddress defaulAddress;
26     private InetSocketAddress localAddress;
27     private final BGPDispatcherImpl disp;
28
29     protected TestClientDispatcher(final EventLoopGroup bossGroup, final EventLoopGroup workerGroup, final MessageRegistry messageRegistry,
30                                    final InetSocketAddress locaAddress) {
31         disp = new BGPDispatcherImpl(messageRegistry, bossGroup, workerGroup) {
32             @Override
33             protected void customizeBootstrap(final Bootstrap b) {
34                 b.localAddress(locaAddress);
35             }
36         };
37         this.hf = new BGPHandlerFactory(messageRegistry);
38         this.localAddress = locaAddress;
39         this.defaulAddress = locaAddress;
40     }
41
42     public synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress remoteAddress,
43                                                             final AsNumber remoteAs, final BGPPeerRegistry listener, final ReconnectStrategy strategy, final Optional<InetSocketAddress> localAddress) {
44         setLocalAddress(localAddress);
45         return disp.createClient(remoteAddress, remoteAs, listener, strategy);
46     }
47
48     public synchronized Future<Void> createReconnectingClient(final InetSocketAddress address,
49                                                               final AsNumber remoteAs, final BGPPeerRegistry peerRegistry, final ReconnectStrategyFactory reconnectStrategyFactory,
50                                                               final Optional<InetSocketAddress> localAddress) {
51         setLocalAddress(localAddress);
52         return disp.createReconnectingClient(address, remoteAs, peerRegistry, reconnectStrategyFactory, null);
53     }
54
55     private synchronized void setLocalAddress(final Optional<InetSocketAddress> localAddress) {
56         if (localAddress.isPresent()) {
57             this.localAddress = localAddress.get();
58         } else {
59             this.localAddress = defaulAddress;
60         }
61     }
62 }