Change getIsOnlineFuture() return type
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / TcpConnectionInitializer.java
1 /*
2  * Copyright (c) 2015 Pantheon Technologies s.r.o. 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.openflowjava.protocol.impl.core;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.SettableFuture;
14 import io.netty.bootstrap.Bootstrap;
15 import io.netty.channel.EventLoopGroup;
16 import io.netty.channel.epoll.EpollSocketChannel;
17 import io.netty.channel.socket.nio.NioSocketChannel;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Initializes (TCP) connection to device.
23  *
24  * @author martin.uhlir
25  */
26 public class TcpConnectionInitializer implements ServerFacade, ConnectionInitializer {
27     private static final Logger LOG = LoggerFactory.getLogger(TcpConnectionInitializer.class);
28
29     private final SettableFuture<Void> hasRun = SettableFuture.create();
30     private final EventLoopGroup workerGroup;
31     private final boolean isEpollEnabled;
32
33     private TcpChannelInitializer channelInitializer;
34     private Bootstrap bootstrap;
35
36     /**
37      * Constructor.
38      *
39      * @param workerGroup - shared worker group
40      */
41     public TcpConnectionInitializer(final EventLoopGroup workerGroup, final boolean isEpollEnabled) {
42         this.workerGroup = requireNonNull(workerGroup, "WorkerGroup can't be null");
43         this.isEpollEnabled = isEpollEnabled;
44     }
45
46     @Override
47     public void run() {
48         bootstrap = new Bootstrap();
49         if (isEpollEnabled) {
50             bootstrap.group(workerGroup).channel(EpollSocketChannel.class).handler(channelInitializer);
51         } else {
52             bootstrap.group(workerGroup).channel(NioSocketChannel.class).handler(channelInitializer);
53         }
54         hasRun.set(null);
55     }
56
57     @Override
58     public ListenableFuture<Boolean> shutdown() {
59         final var result = SettableFuture.<Boolean>create();
60         workerGroup.shutdownGracefully();
61         return result;
62     }
63
64     @Override
65     public ListenableFuture<Void> getIsOnlineFuture() {
66         return hasRun;
67     }
68
69     @Override
70     public void initiateConnection(final String host, final int port) {
71         try {
72             bootstrap.connect(host, port).sync();
73         } catch (InterruptedException e) {
74             LOG.error("Unable to initiate connection", e);
75         }
76     }
77
78     public void setChannelInitializer(final TcpChannelInitializer channelInitializer) {
79         this.channelInitializer = channelInitializer;
80     }
81 }