Cleanup eclipse warnings
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / PCEPProtocolSessionPromise.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 package org.opendaylight.protocol.pcep.impl;
9
10 import com.google.common.base.Preconditions;
11 import io.netty.bootstrap.Bootstrap;
12 import io.netty.channel.ChannelFuture;
13 import io.netty.channel.ChannelFutureListener;
14 import io.netty.channel.ChannelOption;
15 import io.netty.channel.EventLoop;
16 import io.netty.util.concurrent.DefaultPromise;
17 import io.netty.util.concurrent.EventExecutor;
18 import io.netty.util.concurrent.Future;
19 import io.netty.util.concurrent.Promise;
20 import java.net.InetSocketAddress;
21 import java.util.concurrent.TimeUnit;
22 import javax.annotation.concurrent.GuardedBy;
23 import javax.annotation.concurrent.ThreadSafe;
24 import org.opendaylight.protocol.pcep.PCEPSession;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 @ThreadSafe
29 public final class PCEPProtocolSessionPromise<S extends PCEPSession> extends DefaultPromise<S> {
30     private static final Logger LOG = LoggerFactory.getLogger(PCEPProtocolSessionPromise.class);
31     private InetSocketAddress address;
32     private final int retryTimer;
33     private final int connectTimeout;
34     private final Bootstrap b;
35     @GuardedBy("this")
36     private Future<?> pending;
37
38     PCEPProtocolSessionPromise(final EventExecutor executor, final InetSocketAddress address,
39             final int retryTimer, final int connectTimeout, final Bootstrap b) {
40         super(executor);
41         this.address = Preconditions.checkNotNull(address);
42         this.retryTimer = retryTimer;
43         this.connectTimeout = connectTimeout;
44         this.b = Preconditions.checkNotNull(b);
45     }
46
47     synchronized void connect() {
48         final PCEPProtocolSessionPromise<?> lock = this;
49
50         try {
51             LOG.debug("Promise {} attempting connect for {}ms", lock, Integer.valueOf(this.connectTimeout));
52             if (this.address.isUnresolved()) {
53                 this.address = new InetSocketAddress(this.address.getHostName(), this.address.getPort());
54             }
55
56             this.b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, this.connectTimeout);
57             this.b.remoteAddress(this.address);
58             final ChannelFuture connectFuture = this.b.connect();
59             connectFuture.addListener(new BootstrapConnectListener(lock));
60             this.pending = connectFuture;
61         } catch (Exception e) {
62             LOG.info("Failed to connect to {}", this.address, e);
63             this.setFailure(e);
64         }
65     }
66
67     @Override
68     public synchronized boolean cancel(final boolean mayInterruptIfRunning) {
69         if (super.cancel(mayInterruptIfRunning)) {
70             this.pending.cancel(mayInterruptIfRunning);
71             return true;
72         }
73
74         return false;
75     }
76
77     @Override
78     public synchronized Promise<S> setSuccess(final S result) {
79         LOG.debug("Promise {} completed", this);
80         return super.setSuccess(result);
81     }
82
83     private class BootstrapConnectListener implements ChannelFutureListener {
84         private final Object lock;
85
86         public BootstrapConnectListener(final Object lock) {
87             this.lock = lock;
88         }
89
90         @Override
91         public void operationComplete(final ChannelFuture cf) throws Exception {
92             synchronized (this.lock) {
93                 PCEPProtocolSessionPromise.LOG.debug("Promise {} connection resolved", this.lock);
94                 Preconditions.checkState(PCEPProtocolSessionPromise.this.pending.equals(cf));
95                 if (PCEPProtocolSessionPromise.this.isCancelled()) {
96                     if (cf.isSuccess()) {
97                         PCEPProtocolSessionPromise.LOG.debug("Closing channel for cancelled promise {}", this.lock);
98                         cf.channel().close();
99                     }
100                 } else if (cf.isSuccess()) {
101                     PCEPProtocolSessionPromise.LOG.debug("Promise {} connection successful", this.lock);
102                 } else {
103                     PCEPProtocolSessionPromise.LOG.debug("Attempt to connect to {} failed", PCEPProtocolSessionPromise.this.address, cf.cause());
104
105                     if (PCEPProtocolSessionPromise.this.retryTimer == 0) {
106                         PCEPProtocolSessionPromise.LOG.debug("Retry timer value is 0. Reconnection will not be attempted");
107                         PCEPProtocolSessionPromise.this.setFailure(cf.cause());
108                         return;
109                     }
110
111                     final EventLoop loop = cf.channel().eventLoop();
112                     loop.schedule(() -> {
113                         PCEPProtocolSessionPromise.LOG.debug("Attempting to connect to {}", PCEPProtocolSessionPromise.this.address);
114                         final Future<Void> reconnectFuture = PCEPProtocolSessionPromise.this.b.connect();
115                         reconnectFuture.addListener(BootstrapConnectListener.this);
116                         PCEPProtocolSessionPromise.this.pending = reconnectFuture;
117                     }, PCEPProtocolSessionPromise.this.retryTimer, TimeUnit.SECONDS);
118                     PCEPProtocolSessionPromise.LOG.debug("Next reconnection attempt in {}s", PCEPProtocolSessionPromise.this.retryTimer);
119                 }
120             }
121         }
122     }
123 }