995081979f619b479d434dff9caefe11aad5fc19
[bgpcep.git] / bgp / testtool / src / main / java / org / opendaylight / protocol / bgp / testtool / BGPPeerBuilder.java
1 /*
2  * Copyright (c) 2016 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.testtool;
10
11 import com.google.common.base.Preconditions;
12 import io.netty.util.concurrent.Future;
13 import java.net.InetSocketAddress;
14 import java.util.Collections;
15 import java.util.Optional;
16 import org.opendaylight.protocol.bgp.rib.impl.BGPDispatcherImpl;
17 import org.opendaylight.protocol.bgp.rib.impl.StrictBGPPeerRegistry;
18 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
19 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
20 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
21 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.BgpParameters;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpId;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 final class BGPPeerBuilder {
29     private static final int RETRY_TIMER = 10;
30     private static final Logger LOG = LoggerFactory.getLogger(BGPPeerBuilder.class);
31
32     private BGPPeerBuilder() {
33         throw new UnsupportedOperationException();
34     }
35
36     static void createPeer(final BGPDispatcher dispatcher, final Arguments arguments,
37             final InetSocketAddress localAddress, final BGPSessionListener sessionListener,
38             final BgpParameters bgpParameters) {
39         final AsNumber as = arguments.getAs();
40         final BGPSessionPreferences proposal = new BGPSessionPreferences(as, arguments.getHoldTimer(),
41                 new BgpId(localAddress.getAddress().getHostAddress()), as, Collections.singletonList(bgpParameters),
42                 Optional.empty());
43         final BGPPeerRegistry strictBGPPeerRegistry = dispatcher.getBGPPeerRegistry();
44         if (arguments.getInitiateConnection()) {
45             for (final InetSocketAddress remoteAddress : arguments.getRemoteAddresses()) {
46                 strictBGPPeerRegistry.addPeer(StrictBGPPeerRegistry.getIpAddress(remoteAddress), sessionListener,
47                         proposal);
48                 addFutureListener(localAddress, ((BGPDispatcherImpl) dispatcher).createClient(localAddress,
49                         remoteAddress, RETRY_TIMER, true));
50             }
51         } else {
52             for (final InetSocketAddress remoteAddress : arguments.getRemoteAddresses()) {
53                 strictBGPPeerRegistry.addPeer(StrictBGPPeerRegistry.getIpAddress(remoteAddress), sessionListener,
54                         proposal);
55             }
56             addFutureListener(localAddress, dispatcher.createServer(localAddress));
57         }
58         LOG.debug("{} {}", sessionListener, proposal);
59     }
60
61     private static <T> void addFutureListener(final InetSocketAddress localAddress, final Future<T> future) {
62         future.addListener(future1 -> Preconditions.checkArgument(future1.isSuccess(),
63                 "Unable to start bgp session on %s", localAddress, future1.cause()));
64     }
65 }