YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.ovsdb.controller.impl; |
| 17 | |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 18 | import io.netty.bootstrap.Bootstrap; |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 19 | import io.netty.bootstrap.ServerBootstrap; |
| 20 | import io.netty.buffer.PooledByteBufAllocator; |
| 21 | import io.netty.channel.Channel; |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 22 | import io.netty.channel.ChannelDuplexHandler; |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 23 | import io.netty.channel.ChannelFuture; |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 24 | import io.netty.channel.ChannelFutureListener; |
| 25 | import io.netty.channel.ChannelHandlerContext; |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 26 | import io.netty.channel.ChannelInitializer; |
| 27 | import io.netty.channel.ChannelOption; |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 28 | import io.netty.channel.ChannelPipeline; |
| 29 | import io.netty.channel.EventLoop; |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 30 | import io.netty.channel.EventLoopGroup; |
| 31 | import io.netty.channel.ServerChannel; |
debanshur | 37cf6ba | 2018-05-08 20:07:30 +0530 | [diff] [blame] | 32 | import io.netty.channel.group.ChannelGroup; |
| 33 | import io.netty.channel.group.DefaultChannelGroup; |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 34 | import io.netty.channel.nio.NioEventLoopGroup; |
| 35 | import io.netty.channel.socket.SocketChannel; |
| 36 | import io.netty.channel.socket.nio.NioServerSocketChannel; |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 37 | import io.netty.channel.socket.nio.NioSocketChannel; |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 38 | import io.netty.handler.codec.string.StringEncoder; |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 39 | import io.netty.handler.timeout.IdleState; |
| 40 | import io.netty.handler.timeout.IdleStateEvent; |
| 41 | import io.netty.handler.timeout.IdleStateHandler; |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 42 | import io.netty.util.CharsetUtil; |
| 43 | |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 44 | import static org.onlab.util.Tools.groupedThreads; |
| 45 | |
debanshur | 37cf6ba | 2018-05-08 20:07:30 +0530 | [diff] [blame] | 46 | import java.io.FileInputStream; |
| 47 | import java.io.IOException; |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 48 | import java.net.InetSocketAddress; |
debanshur | 37cf6ba | 2018-05-08 20:07:30 +0530 | [diff] [blame] | 49 | import java.security.KeyManagementException; |
| 50 | import java.security.KeyStore; |
| 51 | import java.security.KeyStoreException; |
| 52 | import java.security.NoSuchAlgorithmException; |
| 53 | import java.security.UnrecoverableKeyException; |
| 54 | import java.security.cert.CertificateException; |
| 55 | import java.util.Objects; |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 56 | import java.util.concurrent.ExecutorService; |
| 57 | import java.util.concurrent.Executors; |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 58 | import java.util.concurrent.TimeUnit; |
| 59 | import java.util.concurrent.atomic.AtomicInteger; |
jaegonkim | 1af0ae5 | 2017-01-01 10:46:55 +0900 | [diff] [blame] | 60 | import java.util.function.Consumer; |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 61 | |
debanshur | 37cf6ba | 2018-05-08 20:07:30 +0530 | [diff] [blame] | 62 | import io.netty.util.concurrent.GlobalEventExecutor; |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 63 | import org.onlab.packet.IpAddress; |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 64 | import org.onlab.packet.TpPort; |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 65 | import org.onlab.util.Tools; |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 66 | import org.onosproject.ovsdb.controller.OvsdbConstant; |
| 67 | import org.onosproject.ovsdb.controller.OvsdbNodeId; |
| 68 | import org.onosproject.ovsdb.controller.driver.DefaultOvsdbClient; |
| 69 | import org.onosproject.ovsdb.controller.driver.OvsdbAgent; |
| 70 | import org.onosproject.ovsdb.controller.driver.OvsdbProviderService; |
| 71 | import org.onosproject.ovsdb.rfc.jsonrpc.Callback; |
| 72 | import org.slf4j.Logger; |
| 73 | import org.slf4j.LoggerFactory; |
| 74 | |
debanshur | 37cf6ba | 2018-05-08 20:07:30 +0530 | [diff] [blame] | 75 | import javax.net.ssl.KeyManagerFactory; |
| 76 | import javax.net.ssl.SSLContext; |
| 77 | import javax.net.ssl.TrustManagerFactory; |
| 78 | |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 79 | /** |
| 80 | * The main controller class. Handles all setup and network listeners - |
BitOhenry | 3e104cd | 2015-11-10 12:18:33 +0800 | [diff] [blame] | 81 | * distributed OVSDBClient. |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 82 | */ |
BitOhenry | f006ddd | 2015-11-17 13:25:41 +0800 | [diff] [blame] | 83 | public class Controller { |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame] | 84 | private static final Logger log = LoggerFactory |
BitOhenry | f006ddd | 2015-11-17 13:25:41 +0800 | [diff] [blame] | 85 | .getLogger(Controller.class); |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 86 | |
| 87 | private int ovsdbPort = OvsdbConstant.OVSDBPORT; |
| 88 | |
| 89 | private OvsdbAgent agent; |
| 90 | private Callback monitorCallback; |
| 91 | |
| 92 | private final ExecutorService executorService = Executors |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 93 | .newFixedThreadPool(10, groupedThreads("OVSDB-C", "executor-%d", log)); |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 94 | |
| 95 | private EventLoopGroup bossGroup; |
| 96 | private EventLoopGroup workerGroup; |
| 97 | private Class<? extends ServerChannel> serverChannelClass; |
| 98 | |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 99 | private static final int MAX_RETRY = 5; |
| 100 | private static final int IDLE_TIMEOUT_SEC = 10; |
| 101 | |
debanshur | 37cf6ba | 2018-05-08 20:07:30 +0530 | [diff] [blame] | 102 | private ChannelGroup cg; |
| 103 | |
| 104 | protected static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024; |
| 105 | |
| 106 | protected TlsParams tlsParams = new TlsParams(); |
| 107 | protected SSLContext sslContext; |
| 108 | protected KeyStore keyStore; |
| 109 | |
| 110 | protected static final short MIN_KS_LENGTH = 6; |
| 111 | private static final String JAVA_KEY_STORE = "JKS"; |
| 112 | |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 113 | /** |
| 114 | * Initialization. |
| 115 | */ |
| 116 | private void initEventLoopGroup() { |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 117 | bossGroup = new NioEventLoopGroup(0, Tools.groupedThreads("OVSDB-C", "boss-%d", log)); |
| 118 | workerGroup = new NioEventLoopGroup(0, Tools.groupedThreads("OVSDB-C", "worker-%d", log)); |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 119 | serverChannelClass = NioServerSocketChannel.class; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Accepts incoming connections. |
| 124 | */ |
| 125 | private void startAcceptingConnections() throws InterruptedException { |
debanshur | 37cf6ba | 2018-05-08 20:07:30 +0530 | [diff] [blame] | 126 | if (cg == null) { |
| 127 | return; |
| 128 | } |
| 129 | final ServerBootstrap b = new ServerBootstrap(); |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 130 | |
| 131 | b.group(bossGroup, workerGroup).channel(serverChannelClass) |
debanshur | 37cf6ba | 2018-05-08 20:07:30 +0530 | [diff] [blame] | 132 | .childHandler(new OvsdbChannelInitializer(this, sslContext)); |
| 133 | b.option(ChannelOption.SO_REUSEADDR, true); |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 134 | b.option(ChannelOption.SO_BACKLOG, 128); |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 135 | b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); |
debanshur | 37cf6ba | 2018-05-08 20:07:30 +0530 | [diff] [blame] | 136 | b.childOption(ChannelOption.SO_SNDBUF, Controller.SEND_BUFFER_SIZE); |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 137 | b.childOption(ChannelOption.SO_KEEPALIVE, true); |
debanshur | 37cf6ba | 2018-05-08 20:07:30 +0530 | [diff] [blame] | 138 | |
| 139 | cg.add(b.bind(ovsdbPort).syncUninterruptibly().channel()); |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /** |
BitOhenry | 3e104cd | 2015-11-10 12:18:33 +0800 | [diff] [blame] | 143 | * Tells controller that we're ready to accept OVSDB node loop. |
Ray Milkey | 9b36d81 | 2015-09-09 15:24:54 -0700 | [diff] [blame] | 144 | * @throws InterruptedException if thread is interrupted |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 145 | */ |
| 146 | public void run() throws InterruptedException { |
| 147 | initEventLoopGroup(); |
| 148 | startAcceptingConnections(); |
| 149 | } |
| 150 | |
| 151 | /** |
BitOhenry | 3e104cd | 2015-11-10 12:18:33 +0800 | [diff] [blame] | 152 | * Adds channel pipeline to handle a new connected node. |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 153 | */ |
| 154 | private class OnosCommunicationChannelInitializer |
| 155 | extends ChannelInitializer<SocketChannel> { |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 156 | @Override |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 157 | protected void initChannel(SocketChannel channel) throws Exception { |
| 158 | log.info("New channel created"); |
| 159 | channel.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8)); |
| 160 | channel.pipeline().addLast(new MessageDecoder()); |
| 161 | handleNewNodeConnection(channel); |
| 162 | |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
debanshur | 37cf6ba | 2018-05-08 20:07:30 +0530 | [diff] [blame] | 167 | * Sets new TLS parameters. |
| 168 | * |
| 169 | * @param newTlsParams Modified Tls Params |
| 170 | * @return true if restart is required |
| 171 | */ |
| 172 | protected boolean setTlsParameters(TlsParams newTlsParams) { |
| 173 | TlsParams oldParams = this.tlsParams; |
| 174 | this.tlsParams = newTlsParams; |
| 175 | return !Objects.equals(this.tlsParams, oldParams); // restart if TLS params change |
| 176 | } |
| 177 | |
| 178 | /** |
BitOhenry | 3e104cd | 2015-11-10 12:18:33 +0800 | [diff] [blame] | 179 | * Handles the new connection of node. |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 180 | * |
| 181 | * @param channel the channel to use. |
| 182 | */ |
debanshur | 37cf6ba | 2018-05-08 20:07:30 +0530 | [diff] [blame] | 183 | protected void handleNewNodeConnection(final Channel channel) { |
kdarapu | fce5abb | 2018-05-10 19:37:53 +0530 | [diff] [blame] | 184 | executorService.execute(() -> { |
| 185 | log.info("Handle new node connection"); |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 186 | |
kdarapu | fce5abb | 2018-05-10 19:37:53 +0530 | [diff] [blame] | 187 | IpAddress ipAddress = IpAddress |
| 188 | .valueOf(((InetSocketAddress) channel.remoteAddress()) |
| 189 | .getAddress().getHostAddress()); |
| 190 | long port = ((InetSocketAddress) channel.remoteAddress()) |
| 191 | .getPort(); |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 192 | |
kdarapu | fce5abb | 2018-05-10 19:37:53 +0530 | [diff] [blame] | 193 | log.info("Get connection from ip address {} : {}", |
| 194 | ipAddress.toString(), port); |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 195 | |
kdarapu | fce5abb | 2018-05-10 19:37:53 +0530 | [diff] [blame] | 196 | OvsdbNodeId nodeId = new OvsdbNodeId(ipAddress, port); |
| 197 | OvsdbProviderService ovsdbProviderService = getNodeInstance(nodeId, |
| 198 | agent, |
| 199 | monitorCallback, |
| 200 | channel); |
| 201 | ovsdbProviderService.setConnection(true); |
| 202 | OvsdbJsonRpcHandler ovsdbJsonRpcHandler = new OvsdbJsonRpcHandler( |
| 203 | nodeId); |
| 204 | ovsdbJsonRpcHandler |
| 205 | .setOvsdbProviderService(ovsdbProviderService); |
| 206 | channel.pipeline().addLast(ovsdbJsonRpcHandler); |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 207 | |
kdarapu | fce5abb | 2018-05-10 19:37:53 +0530 | [diff] [blame] | 208 | ovsdbProviderService.nodeAdded(); |
| 209 | ChannelFuture closeFuture = channel.closeFuture(); |
| 210 | closeFuture |
| 211 | .addListener(new ChannelConnectionListener( |
| 212 | ovsdbProviderService)); |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 213 | }); |
| 214 | } |
| 215 | |
| 216 | /** |
BitOhenry | 3e104cd | 2015-11-10 12:18:33 +0800 | [diff] [blame] | 217 | * Gets an OVSDB client instance. |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 218 | * |
BitOhenry | 3e104cd | 2015-11-10 12:18:33 +0800 | [diff] [blame] | 219 | * @param nodeId data OVSDB node id |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 220 | * @param agent OvsdbAgent |
| 221 | * @param monitorCallback Callback |
| 222 | * @param channel Channel |
| 223 | * @return OvsdbProviderService instance |
| 224 | */ |
| 225 | protected OvsdbProviderService getNodeInstance(OvsdbNodeId nodeId, |
| 226 | OvsdbAgent agent, |
| 227 | Callback monitorCallback, |
| 228 | Channel channel) { |
| 229 | OvsdbProviderService ovsdbProviderService = new DefaultOvsdbClient( |
| 230 | nodeId); |
| 231 | ovsdbProviderService.setAgent(agent); |
| 232 | ovsdbProviderService.setCallback(monitorCallback); |
| 233 | ovsdbProviderService.setChannel(channel); |
| 234 | return ovsdbProviderService; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Starts controller. |
| 239 | * |
| 240 | * @param agent OvsdbAgent |
| 241 | * @param monitorCallback Callback |
Jian Li | eaf3103 | 2018-05-03 15:54:03 +0900 | [diff] [blame] | 242 | * @param mode OVSDB server mode flag |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 243 | */ |
Jian Li | eaf3103 | 2018-05-03 15:54:03 +0900 | [diff] [blame] | 244 | public void start(OvsdbAgent agent, Callback monitorCallback, boolean mode) { |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 245 | this.agent = agent; |
| 246 | this.monitorCallback = monitorCallback; |
Jian Li | eaf3103 | 2018-05-03 15:54:03 +0900 | [diff] [blame] | 247 | // if the OVSDB server flag is configured as false, we do NOT listen on 6640 port |
| 248 | // therefore, ONOS only runs as an OVSDB client |
| 249 | if (mode) { |
| 250 | try { |
debanshur | 37cf6ba | 2018-05-08 20:07:30 +0530 | [diff] [blame] | 251 | this.init(); |
Jian Li | eaf3103 | 2018-05-03 15:54:03 +0900 | [diff] [blame] | 252 | this.run(); |
| 253 | } catch (InterruptedException e) { |
| 254 | log.warn("Interrupted while waiting to start"); |
| 255 | Thread.currentThread().interrupt(); |
| 256 | } |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Stops controller. |
| 262 | * |
| 263 | */ |
| 264 | public void stop() { |
debanshur | 37cf6ba | 2018-05-08 20:07:30 +0530 | [diff] [blame] | 265 | if (cg != null) { |
| 266 | cg.close(); |
| 267 | cg = null; |
| 268 | |
| 269 | workerGroup.shutdownGracefully(); |
| 270 | bossGroup.shutdownGracefully(); |
| 271 | // Wait until all threads are terminated. |
| 272 | try { |
| 273 | bossGroup.terminationFuture().sync(); |
| 274 | workerGroup.terminationFuture().sync(); |
| 275 | } catch (InterruptedException e) { |
| 276 | log.warn("Interrupted while stopping", e); |
| 277 | Thread.currentThread().interrupt(); |
| 278 | } |
| 279 | } |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 280 | } |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 281 | |
| 282 | /** |
| 283 | * Connect to the ovsdb server with given ip address and port number. |
| 284 | * |
| 285 | * @param ip ip address |
| 286 | * @param port port number |
| 287 | */ |
| 288 | public void connect(IpAddress ip, TpPort port) { |
jaegonkim | 1af0ae5 | 2017-01-01 10:46:55 +0900 | [diff] [blame] | 289 | connect(ip, port, e -> log.warn("Connection to the ovsdb {}:{} failed(cause: {})", ip, port, e)); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Connect to the ovsdb server with given ip address, port number, and failhandler. |
| 294 | * |
| 295 | * @param ip ip address |
| 296 | * @param port port number |
| 297 | * @param failhandler connection failure handler |
| 298 | */ |
| 299 | public void connect(IpAddress ip, TpPort port, Consumer<Exception> failhandler) { |
| 300 | ChannelFutureListener listener = new ConnectionListener(this, ip, port, failhandler); |
| 301 | try { |
| 302 | connectRetry(ip, port, listener); |
| 303 | } catch (Exception e) { |
| 304 | failhandler.accept(e); |
| 305 | } |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | private void connectRetry(IpAddress ip, TpPort port, ChannelFutureListener listener) { |
jaegonkim | 1af0ae5 | 2017-01-01 10:46:55 +0900 | [diff] [blame] | 309 | Bootstrap b = new Bootstrap(); |
| 310 | b.group(workerGroup) |
| 311 | .channel(NioSocketChannel.class) |
| 312 | .option(ChannelOption.TCP_NODELAY, true) |
| 313 | .handler(new ChannelInitializer<SocketChannel>() { |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 314 | |
jaegonkim | 1af0ae5 | 2017-01-01 10:46:55 +0900 | [diff] [blame] | 315 | @Override |
| 316 | protected void initChannel(SocketChannel channel) throws Exception { |
debanshur | 37cf6ba | 2018-05-08 20:07:30 +0530 | [diff] [blame] | 317 | ChannelPipeline pipeline = channel.pipeline(); |
| 318 | pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8)); |
| 319 | pipeline.addLast(new MessageDecoder()); |
| 320 | pipeline.addLast(new IdleStateHandler(IDLE_TIMEOUT_SEC, 0, 0)); |
| 321 | pipeline.addLast(new ConnectionHandler()); |
jaegonkim | 1af0ae5 | 2017-01-01 10:46:55 +0900 | [diff] [blame] | 322 | } |
| 323 | }); |
| 324 | b.remoteAddress(ip.toString(), port.toInt()); |
| 325 | b.connect().addListener(listener); |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | private class ConnectionListener implements ChannelFutureListener { |
BitOhenry | f006ddd | 2015-11-17 13:25:41 +0800 | [diff] [blame] | 329 | private Controller controller; |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 330 | private IpAddress ip; |
| 331 | private TpPort port; |
| 332 | private AtomicInteger count = new AtomicInteger(); |
jaegonkim | 1af0ae5 | 2017-01-01 10:46:55 +0900 | [diff] [blame] | 333 | private Consumer<Exception> failhandler; |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 334 | |
BitOhenry | f006ddd | 2015-11-17 13:25:41 +0800 | [diff] [blame] | 335 | public ConnectionListener(Controller controller, |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 336 | IpAddress ip, |
jaegonkim | 1af0ae5 | 2017-01-01 10:46:55 +0900 | [diff] [blame] | 337 | TpPort port, |
| 338 | Consumer<Exception> failhandler) { |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 339 | this.controller = controller; |
| 340 | this.ip = ip; |
| 341 | this.port = port; |
jaegonkim | 1af0ae5 | 2017-01-01 10:46:55 +0900 | [diff] [blame] | 342 | this.failhandler = failhandler; |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | @Override |
| 346 | public void operationComplete(ChannelFuture channelFuture) throws Exception { |
| 347 | if (!channelFuture.isSuccess()) { |
| 348 | channelFuture.channel().close(); |
| 349 | |
| 350 | if (count.incrementAndGet() < MAX_RETRY) { |
| 351 | final EventLoop loop = channelFuture.channel().eventLoop(); |
| 352 | |
| 353 | loop.schedule(() -> { |
jaegonkim | 1af0ae5 | 2017-01-01 10:46:55 +0900 | [diff] [blame] | 354 | try { |
| 355 | controller.connectRetry(this.ip, this.port, this); |
| 356 | } catch (Exception e) { |
| 357 | log.warn("Connection to the ovsdb server {}:{} failed(cause: {})", ip, port, e); |
| 358 | } |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 359 | }, 1L, TimeUnit.SECONDS); |
| 360 | } else { |
jaegonkim | 1af0ae5 | 2017-01-01 10:46:55 +0900 | [diff] [blame] | 361 | failhandler.accept(new Exception("max connection retry(" + MAX_RETRY + ") exceeded")); |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 362 | } |
| 363 | } else { |
| 364 | handleNewNodeConnection(channelFuture.channel()); |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | private class ConnectionHandler extends ChannelDuplexHandler { |
| 370 | |
| 371 | @Override |
| 372 | public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { |
| 373 | IdleStateEvent e = (IdleStateEvent) evt; |
| 374 | |
| 375 | if (e.state() == IdleState.READER_IDLE) { |
| 376 | ctx.close(); |
| 377 | } |
| 378 | } |
| 379 | } |
debanshur | 37cf6ba | 2018-05-08 20:07:30 +0530 | [diff] [blame] | 380 | |
| 381 | /** |
| 382 | * Initialize internal data structures. |
| 383 | */ |
| 384 | public void init() { |
| 385 | cg = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); |
| 386 | |
| 387 | if (tlsParams.isTlsEnabled()) { |
| 388 | initSsl(); |
| 389 | } |
| 390 | |
| 391 | } |
| 392 | |
| 393 | private void initSsl() { |
| 394 | try { |
| 395 | TrustManagerFactory tmFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 396 | KeyStore ts = KeyStore.getInstance(JAVA_KEY_STORE); |
| 397 | ts.load(new FileInputStream(tlsParams.tsLocation), tlsParams.tsPwd()); |
| 398 | tmFactory.init(ts); |
| 399 | |
| 400 | KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); |
| 401 | keyStore = KeyStore.getInstance(JAVA_KEY_STORE); |
| 402 | keyStore.load(new FileInputStream(tlsParams.ksLocation), tlsParams.ksPwd()); |
| 403 | kmf.init(keyStore, tlsParams.ksPwd()); |
| 404 | |
| 405 | sslContext = SSLContext.getInstance("TLS"); |
| 406 | sslContext.init(kmf.getKeyManagers(), tmFactory.getTrustManagers(), null); |
| 407 | } catch (NoSuchAlgorithmException | KeyStoreException | CertificateException | |
| 408 | IOException | KeyManagementException | UnrecoverableKeyException ex) { |
| 409 | log.error("SSL init failed: {}", ex.getMessage()); |
| 410 | } |
| 411 | } |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 412 | } |