Add method to register listener for unknown msg
[openflowjava.git] / 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 io.netty.bootstrap.Bootstrap;
12 import io.netty.channel.EventLoopGroup;
13 import io.netty.channel.epoll.EpollSocketChannel;
14 import io.netty.channel.socket.nio.NioSocketChannel;
15 import org.opendaylight.openflowjava.protocol.api.connection.ThreadConfiguration;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import com.google.common.base.Preconditions;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import com.google.common.util.concurrent.SettableFuture;
22
23 /**
24  * Initializes (TCP) connection to device
25  * @author martin.uhlir
26  *
27  */
28 public class TcpConnectionInitializer implements ServerFacade,
29         ConnectionInitializer {
30
31     private static final Logger LOG = LoggerFactory
32             .getLogger(TcpConnectionInitializer.class);
33     private EventLoopGroup workerGroup;
34     private ThreadConfiguration threadConfig;
35
36     private TcpChannelInitializer channelInitializer;
37     private Bootstrap b;
38     private boolean isEpollEnabled;
39
40     /**
41      * Constructor
42      * @param workerGroup - shared worker group
43      */
44     public TcpConnectionInitializer(EventLoopGroup workerGroup, boolean isEpollEnabled) {
45         Preconditions.checkNotNull(workerGroup, "WorkerGroup can't be null");
46         this.workerGroup = workerGroup;
47         this.isEpollEnabled = isEpollEnabled;
48     }
49
50     @Override
51     public void run() {
52         b = new Bootstrap();
53         if(isEpollEnabled) {
54             b.group(workerGroup).channel(EpollSocketChannel.class)
55                     .handler(channelInitializer);
56         } else {
57             b.group(workerGroup).channel(NioSocketChannel.class)
58                     .handler(channelInitializer);
59         }
60     }
61
62     @Override
63     public ListenableFuture<Boolean> shutdown() {
64         final SettableFuture<Boolean> result = SettableFuture.create();
65         workerGroup.shutdownGracefully();
66         return result;
67     }
68
69     @Override
70     public ListenableFuture<Boolean> getIsOnlineFuture() {
71         return null;
72     }
73
74     @Override
75     public void setThreadConfig(ThreadConfiguration threadConfig) {
76         this.threadConfig = threadConfig;
77     }
78
79     @Override
80     public void initiateConnection(String host, int port) {
81         try {
82             b.connect(host, port).sync();
83         } catch (InterruptedException e) {
84             LOG.error("Unable to initiate connection", e);
85         }
86     }
87
88     /**
89      * @param channelInitializer
90      */
91     public void setChannelInitializer(TcpChannelInitializer channelInitializer) {
92         this.channelInitializer = channelInitializer;
93     }
94 }