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
9 package org.opendaylight.openflowjava.protocol.impl.core;
11 import com.google.common.base.Preconditions;
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 Preconditions.checkNotNull(workerGroup, "WorkerGroup can't be null");
45 this.workerGroup = workerGroup;
46 this.isEpollEnabled = isEpollEnabled;
51 bootstrap = new Bootstrap();
53 bootstrap.group(workerGroup).channel(EpollSocketChannel.class).handler(channelInitializer);
55 bootstrap.group(workerGroup).channel(NioSocketChannel.class).handler(channelInitializer);
61 public ListenableFuture<Boolean> shutdown() {
62 final SettableFuture<Boolean> result = SettableFuture.create();
63 workerGroup.shutdownGracefully();
68 public ListenableFuture<Boolean> getIsOnlineFuture() {
73 public void setThreadConfig(ThreadConfiguration threadConfig) {
78 public void initiateConnection(String host, int port) {
80 bootstrap.connect(host, port).sync();
81 } catch (InterruptedException e) {
82 LOG.error("Unable to initiate connection", e);
86 public void setChannelInitializer(TcpChannelInitializer channelInitializer) {
87 this.channelInitializer = channelInitializer;