2 * Copyright (c) 2015 Pantheon Technologies s.r.o. 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.openflowjava.protocol.impl.core;
10 import static java.util.Objects.requireNonNull;
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.opendaylight.openflowjava.protocol.api.connection.ThreadConfiguration;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * Initializes (TCP) connection to device.
25 * @author martin.uhlir
27 public class TcpConnectionInitializer implements ServerFacade, ConnectionInitializer {
29 private static final Logger LOG = LoggerFactory.getLogger(TcpConnectionInitializer.class);
31 private final EventLoopGroup workerGroup;
32 private final boolean isEpollEnabled;
33 private final SettableFuture<Boolean> hasRun = SettableFuture.create();
35 private TcpChannelInitializer channelInitializer;
36 private Bootstrap bootstrap;
41 * @param workerGroup - shared worker group
43 public TcpConnectionInitializer(EventLoopGroup workerGroup, boolean isEpollEnabled) {
44 this.workerGroup = requireNonNull(workerGroup, "WorkerGroup can't be null");
45 this.isEpollEnabled = isEpollEnabled;
50 bootstrap = new Bootstrap();
52 bootstrap.group(workerGroup).channel(EpollSocketChannel.class).handler(channelInitializer);
54 bootstrap.group(workerGroup).channel(NioSocketChannel.class).handler(channelInitializer);
60 public ListenableFuture<Boolean> shutdown() {
61 final SettableFuture<Boolean> result = SettableFuture.create();
62 workerGroup.shutdownGracefully();
67 public ListenableFuture<Boolean> getIsOnlineFuture() {
72 public void setThreadConfig(ThreadConfiguration threadConfig) {
77 public void initiateConnection(String host, int port) {
79 bootstrap.connect(host, port).sync();
80 } catch (InterruptedException e) {
81 LOG.error("Unable to initiate connection", e);
85 public void setChannelInitializer(TcpChannelInitializer channelInitializer) {
86 this.channelInitializer = channelInitializer;