Use String(byte[], Charset)
[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
9 package org.opendaylight.openflowjava.protocol.impl.core;
10
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;
21
22 /**
23  * Initializes (TCP) connection to device.
24  *
25  * @author martin.uhlir
26  */
27 public class TcpConnectionInitializer implements ServerFacade, ConnectionInitializer {
28
29     private static final Logger LOG = LoggerFactory.getLogger(TcpConnectionInitializer.class);
30
31     private final EventLoopGroup workerGroup;
32     private final boolean isEpollEnabled;
33     private final SettableFuture<Boolean> hasRun = SettableFuture.create();
34
35     private TcpChannelInitializer channelInitializer;
36     private Bootstrap bootstrap;
37
38     /**
39      * Constructor.
40      *
41      * @param workerGroup - shared worker group
42      */
43     public TcpConnectionInitializer(EventLoopGroup workerGroup, boolean isEpollEnabled) {
44         Preconditions.checkNotNull(workerGroup, "WorkerGroup can't be null");
45         this.workerGroup = workerGroup;
46         this.isEpollEnabled = isEpollEnabled;
47     }
48
49     @Override
50     public void run() {
51         bootstrap = new Bootstrap();
52         if (isEpollEnabled) {
53             bootstrap.group(workerGroup).channel(EpollSocketChannel.class).handler(channelInitializer);
54         } else {
55             bootstrap.group(workerGroup).channel(NioSocketChannel.class).handler(channelInitializer);
56         }
57         hasRun.set(true);
58     }
59
60     @Override
61     public ListenableFuture<Boolean> shutdown() {
62         final SettableFuture<Boolean> result = SettableFuture.create();
63         workerGroup.shutdownGracefully();
64         return result;
65     }
66
67     @Override
68     public ListenableFuture<Boolean> getIsOnlineFuture() {
69         return hasRun;
70     }
71
72     @Override
73     public void setThreadConfig(ThreadConfiguration threadConfig) {
74         // IGNORE
75     }
76
77     @Override
78     public void initiateConnection(String host, int port) {
79         try {
80             bootstrap.connect(host, port).sync();
81         } catch (InterruptedException e) {
82             LOG.error("Unable to initiate connection", e);
83         }
84     }
85
86     public void setChannelInitializer(TcpChannelInitializer channelInitializer) {
87         this.channelInitializer = channelInitializer;
88     }
89 }