Merge "Barrier turn on/off - Split OutboundQueueManager" into stable/lithium
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / TcpConnectionInitializer.java
1 package org.opendaylight.openflowjava.protocol.impl.core;
2
3 import io.netty.bootstrap.Bootstrap;
4 import io.netty.channel.EventLoopGroup;
5 import io.netty.channel.nio.NioEventLoopGroup;
6 import io.netty.channel.socket.nio.NioSocketChannel;
7
8 import org.opendaylight.openflowjava.protocol.api.connection.ThreadConfiguration;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.SettableFuture;
15
16 /**
17  * Initializes (TCP) connection to device
18  * @author martin.uhlir
19  *
20  */
21 public class TcpConnectionInitializer implements ServerFacade,
22         ConnectionInitializer {
23
24     private static final Logger LOGGER = LoggerFactory
25             .getLogger(TcpConnectionInitializer.class);
26     private EventLoopGroup workerGroup;
27     private ThreadConfiguration threadConfig;
28
29     private TcpChannelInitializer channelInitializer;
30     private Bootstrap b;
31
32     /**
33      * Constructor
34      * @param workerGroup - shared worker group
35      */
36     public TcpConnectionInitializer(NioEventLoopGroup workerGroup) {
37         Preconditions.checkNotNull(workerGroup, "WorkerGroup can't be null");
38         this.workerGroup = workerGroup;
39     }
40
41     @Override
42     public void run() {
43         b = new Bootstrap();
44         b.group(workerGroup).channel(NioSocketChannel.class)
45             .handler(channelInitializer);
46     }
47
48     @Override
49     public ListenableFuture<Boolean> shutdown() {
50         final SettableFuture<Boolean> result = SettableFuture.create();
51         workerGroup.shutdownGracefully();
52         return result;
53     }
54
55     @Override
56     public ListenableFuture<Boolean> getIsOnlineFuture() {
57         return null;
58     }
59
60     @Override
61     public void setThreadConfig(ThreadConfiguration threadConfig) {
62         this.threadConfig = threadConfig;
63     }
64
65     @Override
66     public void initiateConnection(String host, int port) {
67         try {
68             b.connect(host, port).sync();
69         } catch (InterruptedException e) {
70             LOGGER.error("Unable to initiate connection", e);
71         }
72     }
73
74     /**
75      * @param channelInitializer
76      */
77     public void setChannelInitializer(TcpChannelInitializer channelInitializer) {
78         this.channelInitializer = channelInitializer;
79     }
80 }