2 * Copyright (c) 2015 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
8 package org.opendaylight.protocol.bgp.rib.impl.protocol;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.Preconditions;
13 import io.netty.bootstrap.Bootstrap;
14 import io.netty.channel.ChannelFuture;
15 import io.netty.channel.ChannelFutureListener;
16 import io.netty.channel.ChannelOption;
17 import io.netty.channel.EventLoop;
18 import io.netty.util.concurrent.DefaultPromise;
19 import io.netty.util.concurrent.GlobalEventExecutor;
20 import io.netty.util.concurrent.Promise;
21 import java.net.InetSocketAddress;
22 import java.util.concurrent.TimeUnit;
23 import javax.annotation.Nonnull;
24 import javax.annotation.concurrent.GuardedBy;
25 import org.opendaylight.protocol.bgp.rib.impl.StrictBGPPeerRegistry;
26 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
27 import org.opendaylight.protocol.bgp.rib.impl.spi.PeerRegistrySessionListener;
28 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public final class BGPProtocolSessionPromise<S extends BGPSession> extends DefaultPromise<S> {
34 private static final Logger LOG = LoggerFactory.getLogger(BGPProtocolSessionPromise.class);
35 private static final int CONNECT_TIMEOUT = 5000;
36 private final int retryTimer;
37 private final Bootstrap bootstrap;
39 private final AutoCloseable listenerRegistration;
41 private InetSocketAddress address;
43 private ChannelFuture pending;
45 private boolean peerSessionPresent;
47 private boolean connectSkipped;
50 public BGPProtocolSessionPromise(@Nonnull final InetSocketAddress remoteAddress, final int retryTimer,
51 @Nonnull final Bootstrap bootstrap, @Nonnull final BGPPeerRegistry peerRegistry) {
52 super(GlobalEventExecutor.INSTANCE);
53 this.address = requireNonNull(remoteAddress);
54 this.retryTimer = retryTimer;
55 this.bootstrap = requireNonNull(bootstrap);
56 this.listenerRegistration = requireNonNull(peerRegistry).registerPeerSessionListener(
57 new PeerRegistrySessionListenerImpl(StrictBGPPeerRegistry.getIpAddress(this.address)));
60 public synchronized void connect() {
61 if (this.peerSessionPresent) {
62 LOG.debug("Connection to {} already exists", this.address);
63 this.connectSkipped = true;
67 this.connectSkipped = false;
69 final BGPProtocolSessionPromise<?> lock = this;
71 LOG.debug("Promise {} attempting connect for {}ms", lock, CONNECT_TIMEOUT);
72 if (this.address.isUnresolved()) {
73 this.address = new InetSocketAddress(this.address.getHostName(), this.address.getPort());
76 this.bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, CONNECT_TIMEOUT);
77 this.bootstrap.remoteAddress(this.address);
78 final ChannelFuture connectFuture = this.bootstrap.connect();
79 connectFuture.addListener(new BootstrapConnectListener());
80 this.pending = connectFuture;
81 } catch (final Exception e) {
82 LOG.warn("Failed to connect to {}", this.address, e);
87 synchronized void reconnect() {
88 if (this.retryTimer == 0) {
89 LOG.debug("Retry timer value is 0. Reconnection will not be attempted");
90 this.setFailure(this.pending.cause());
94 final EventLoop loop = this.pending.channel().eventLoop();
96 synchronized (BGPProtocolSessionPromise.this) {
97 if (BGPProtocolSessionPromise.this.peerSessionPresent) {
98 LOG.debug("Connection to {} already exists", BGPProtocolSessionPromise.this.address);
99 BGPProtocolSessionPromise.this.connectSkipped = true;
103 BGPProtocolSessionPromise.this.connectSkipped = false;
104 LOG.debug("Attempting to connect to {}", BGPProtocolSessionPromise.this.address);
105 final ChannelFuture reconnectFuture = BGPProtocolSessionPromise.this.bootstrap.connect();
106 reconnectFuture.addListener(new BootstrapConnectListener());
107 BGPProtocolSessionPromise.this.pending = reconnectFuture;
109 }, this.retryTimer, TimeUnit.SECONDS);
110 LOG.debug("Next reconnection attempt in {}s", this.retryTimer);
114 public synchronized boolean cancel(final boolean mayInterruptIfRunning) {
115 closePeerSessionListener();
116 if (super.cancel(mayInterruptIfRunning)) {
117 requireNonNull(this.pending);
118 this.pending.cancel(mayInterruptIfRunning);
125 private synchronized void closePeerSessionListener() {
127 this.listenerRegistration.close();
128 } catch (final Exception e) {
129 LOG.debug("Exception encountered while closing peer registry session listener registration", e);
134 public synchronized Promise<S> setSuccess(final S result) {
135 LOG.debug("Promise {} completed", this);
136 return super.setSuccess(result);
139 private class BootstrapConnectListener implements ChannelFutureListener {
141 public void operationComplete(final ChannelFuture channelFuture) throws Exception {
142 synchronized (BGPProtocolSessionPromise.this) {
143 BGPProtocolSessionPromise.LOG.debug("Promise {} connection resolved", BGPProtocolSessionPromise.this);
144 Preconditions.checkState(BGPProtocolSessionPromise.this.pending.equals(channelFuture));
145 if (BGPProtocolSessionPromise.this.isCancelled()) {
146 if (channelFuture.isSuccess()) {
147 BGPProtocolSessionPromise.LOG.debug("Closing channel for cancelled promise {}",
148 BGPProtocolSessionPromise.this);
149 channelFuture.channel().close();
151 } else if (channelFuture.isSuccess()) {
152 BGPProtocolSessionPromise.LOG.debug("Promise {} connection successful",
153 BGPProtocolSessionPromise.this);
155 BGPProtocolSessionPromise.LOG.warn("Attempt to connect to {} failed",
156 BGPProtocolSessionPromise.this.address, channelFuture.cause());
157 BGPProtocolSessionPromise.this.reconnect();
163 private class PeerRegistrySessionListenerImpl implements PeerRegistrySessionListener {
164 private final IpAddress peerAddress;
166 PeerRegistrySessionListenerImpl(final IpAddress peerAddress) {
167 this.peerAddress = peerAddress;
171 public void onSessionCreated(@Nonnull final IpAddress ip) {
172 if (!ip.equals(this.peerAddress)) {
175 BGPProtocolSessionPromise.LOG.debug("Callback for session creation with peer {} received", ip);
176 synchronized (BGPProtocolSessionPromise.this) {
177 BGPProtocolSessionPromise.this.peerSessionPresent = true;
182 public void onSessionRemoved(@Nonnull final IpAddress ip) {
183 if (!ip.equals(this.peerAddress)) {
186 BGPProtocolSessionPromise.LOG.debug("Callback for session removal with peer {} received", ip);
187 synchronized (BGPProtocolSessionPromise.this) {
188 BGPProtocolSessionPromise.this.peerSessionPresent = false;
189 if (BGPProtocolSessionPromise.this.connectSkipped) {
190 BGPProtocolSessionPromise.this.connect();