2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.bgp.testtool;
11 import com.google.common.base.Preconditions;
12 import io.netty.util.concurrent.Future;
13 import io.netty.util.concurrent.GenericFutureListener;
14 import java.net.InetSocketAddress;
15 import java.util.Collections;
16 import java.util.concurrent.ExecutionException;
17 import org.opendaylight.protocol.bgp.rib.impl.BGPDispatcherImpl;
18 import org.opendaylight.protocol.bgp.rib.impl.StrictBGPPeerRegistry;
19 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
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.rev100924.AsNumber;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 final class BGPPeerBuilder {
29 private static final int RETRY_TIMER = 10;
30 private static final Logger LOG = LoggerFactory.getLogger(BGPPeerBuilder.class);
32 private BGPPeerBuilder() {
33 throw new UnsupportedOperationException();
36 static void createPeer(final BGPDispatcher dispatcher, final Arguments arguments, final InetSocketAddress localAddress,
37 final BGPSessionListener sessionListener, final BgpParameters bgpParameters) {
38 final AsNumber as = arguments.getAs();
39 final BGPSessionPreferences proposal = new BGPSessionPreferences(as, arguments.getHoldTimer(), new Ipv4Address(localAddress.getAddress().getHostAddress()),
40 as, Collections.singletonList(bgpParameters));
41 final StrictBGPPeerRegistry strictBGPPeerRegistry = new StrictBGPPeerRegistry();
42 if (arguments.getInitiateConnection()) {
43 for (final InetSocketAddress remoteAddress : arguments.getRemoteAddresses()) {
44 strictBGPPeerRegistry.addPeer(StrictBGPPeerRegistry.getIpAddress(remoteAddress), sessionListener, proposal);
45 addFutureListener(localAddress, ((BGPDispatcherImpl) dispatcher).createClient(localAddress, remoteAddress, strictBGPPeerRegistry, RETRY_TIMER));
48 for (final InetSocketAddress remoteAddress : arguments.getRemoteAddresses()) {
49 strictBGPPeerRegistry.addPeer(StrictBGPPeerRegistry.getIpAddress(remoteAddress), sessionListener, proposal);
51 addFutureListener(localAddress, dispatcher.createServer(strictBGPPeerRegistry, localAddress));
53 LOG.debug("{} {}", sessionListener, proposal);
56 private static <T> void addFutureListener(final InetSocketAddress localAddress, final Future<T> future) {
57 future.addListener(new GenericFutureListener<Future<T>>() {
59 public void operationComplete(final Future<T> future) throws ExecutionException, InterruptedException {
60 Preconditions.checkArgument(future.isSuccess(), "Unable to start bgp session on %s", localAddress, future.cause());