YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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; |
| 32 | import io.netty.channel.nio.NioEventLoopGroup; |
| 33 | import io.netty.channel.socket.SocketChannel; |
| 34 | import io.netty.channel.socket.nio.NioServerSocketChannel; |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 35 | import io.netty.channel.socket.nio.NioSocketChannel; |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 36 | import io.netty.handler.codec.string.StringEncoder; |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 37 | import io.netty.handler.timeout.IdleState; |
| 38 | import io.netty.handler.timeout.IdleStateEvent; |
| 39 | import io.netty.handler.timeout.IdleStateHandler; |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 40 | import io.netty.util.CharsetUtil; |
| 41 | |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 42 | import static org.onlab.util.Tools.groupedThreads; |
| 43 | |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 44 | import java.net.InetSocketAddress; |
| 45 | import java.util.concurrent.ExecutorService; |
| 46 | import java.util.concurrent.Executors; |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 47 | import java.util.concurrent.TimeUnit; |
| 48 | import java.util.concurrent.atomic.AtomicInteger; |
jaegonkim | 1af0ae5 | 2017-01-01 10:46:55 +0900 | [diff] [blame] | 49 | import java.util.function.Consumer; |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 50 | |
| 51 | import org.onlab.packet.IpAddress; |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 52 | import org.onlab.packet.TpPort; |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 53 | import org.onlab.util.Tools; |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 54 | import org.onosproject.ovsdb.controller.OvsdbConstant; |
| 55 | import org.onosproject.ovsdb.controller.OvsdbNodeId; |
| 56 | import org.onosproject.ovsdb.controller.driver.DefaultOvsdbClient; |
| 57 | import org.onosproject.ovsdb.controller.driver.OvsdbAgent; |
| 58 | import org.onosproject.ovsdb.controller.driver.OvsdbProviderService; |
| 59 | import org.onosproject.ovsdb.rfc.jsonrpc.Callback; |
| 60 | import org.slf4j.Logger; |
| 61 | import org.slf4j.LoggerFactory; |
| 62 | |
| 63 | /** |
| 64 | * The main controller class. Handles all setup and network listeners - |
BitOhenry | 3e104cd | 2015-11-10 12:18:33 +0800 | [diff] [blame] | 65 | * distributed OVSDBClient. |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 66 | */ |
BitOhenry | f006ddd | 2015-11-17 13:25:41 +0800 | [diff] [blame] | 67 | public class Controller { |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 68 | protected static final Logger log = LoggerFactory |
BitOhenry | f006ddd | 2015-11-17 13:25:41 +0800 | [diff] [blame] | 69 | .getLogger(Controller.class); |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 70 | |
| 71 | private int ovsdbPort = OvsdbConstant.OVSDBPORT; |
| 72 | |
| 73 | private OvsdbAgent agent; |
| 74 | private Callback monitorCallback; |
| 75 | |
| 76 | private final ExecutorService executorService = Executors |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 77 | .newFixedThreadPool(10, groupedThreads("OVSDB-C", "executor-%d", log)); |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 78 | |
| 79 | private EventLoopGroup bossGroup; |
| 80 | private EventLoopGroup workerGroup; |
| 81 | private Class<? extends ServerChannel> serverChannelClass; |
| 82 | |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 83 | private static final int MAX_RETRY = 5; |
| 84 | private static final int IDLE_TIMEOUT_SEC = 10; |
| 85 | |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 86 | /** |
| 87 | * Initialization. |
| 88 | */ |
| 89 | private void initEventLoopGroup() { |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 90 | bossGroup = new NioEventLoopGroup(0, Tools.groupedThreads("OVSDB-C", "boss-%d", log)); |
| 91 | workerGroup = new NioEventLoopGroup(0, Tools.groupedThreads("OVSDB-C", "worker-%d", log)); |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 92 | serverChannelClass = NioServerSocketChannel.class; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Accepts incoming connections. |
| 97 | */ |
| 98 | private void startAcceptingConnections() throws InterruptedException { |
| 99 | ServerBootstrap b = new ServerBootstrap(); |
| 100 | |
| 101 | b.group(bossGroup, workerGroup).channel(serverChannelClass) |
| 102 | .childHandler(new OnosCommunicationChannelInitializer()); |
| 103 | b.option(ChannelOption.SO_BACKLOG, 128); |
| 104 | b.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024); |
| 105 | b.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024); |
| 106 | b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); |
| 107 | b.childOption(ChannelOption.SO_KEEPALIVE, true); |
jiangrui | 62fb79b | 2015-08-21 15:37:00 +0800 | [diff] [blame] | 108 | b.bind(ovsdbPort).sync(); |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /** |
BitOhenry | 3e104cd | 2015-11-10 12:18:33 +0800 | [diff] [blame] | 112 | * Tells controller that we're ready to accept OVSDB node loop. |
Ray Milkey | 9b36d81 | 2015-09-09 15:24:54 -0700 | [diff] [blame] | 113 | * @throws InterruptedException if thread is interrupted |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 114 | */ |
| 115 | public void run() throws InterruptedException { |
| 116 | initEventLoopGroup(); |
| 117 | startAcceptingConnections(); |
| 118 | } |
| 119 | |
| 120 | /** |
BitOhenry | 3e104cd | 2015-11-10 12:18:33 +0800 | [diff] [blame] | 121 | * Adds channel pipeline to handle a new connected node. |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 122 | */ |
| 123 | private class OnosCommunicationChannelInitializer |
| 124 | extends ChannelInitializer<SocketChannel> { |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 125 | @Override |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 126 | protected void initChannel(SocketChannel channel) throws Exception { |
| 127 | log.info("New channel created"); |
| 128 | channel.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8)); |
| 129 | channel.pipeline().addLast(new MessageDecoder()); |
| 130 | handleNewNodeConnection(channel); |
| 131 | |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
BitOhenry | 3e104cd | 2015-11-10 12:18:33 +0800 | [diff] [blame] | 136 | * Handles the new connection of node. |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 137 | * |
| 138 | * @param channel the channel to use. |
| 139 | */ |
| 140 | private void handleNewNodeConnection(final Channel channel) { |
| 141 | executorService.execute(new Runnable() { |
| 142 | @Override |
| 143 | public void run() { |
| 144 | log.info("Handle new node connection"); |
| 145 | |
| 146 | IpAddress ipAddress = IpAddress |
| 147 | .valueOf(((InetSocketAddress) channel.remoteAddress()) |
| 148 | .getAddress().getHostAddress()); |
| 149 | long port = ((InetSocketAddress) channel.remoteAddress()) |
| 150 | .getPort(); |
| 151 | |
| 152 | log.info("Get connection from ip address {} : {}", |
| 153 | ipAddress.toString(), port); |
| 154 | |
| 155 | OvsdbNodeId nodeId = new OvsdbNodeId(ipAddress, port); |
| 156 | OvsdbProviderService ovsdbProviderService = getNodeInstance(nodeId, |
| 157 | agent, |
| 158 | monitorCallback, |
| 159 | channel); |
| 160 | ovsdbProviderService.setConnection(true); |
| 161 | OvsdbJsonRpcHandler ovsdbJsonRpcHandler = new OvsdbJsonRpcHandler( |
| 162 | nodeId); |
| 163 | ovsdbJsonRpcHandler |
| 164 | .setOvsdbProviderService(ovsdbProviderService); |
| 165 | channel.pipeline().addLast(ovsdbJsonRpcHandler); |
| 166 | |
| 167 | ovsdbProviderService.nodeAdded(); |
| 168 | ChannelFuture closeFuture = channel.closeFuture(); |
| 169 | closeFuture |
| 170 | .addListener(new ChannelConnectionListener( |
| 171 | ovsdbProviderService)); |
| 172 | } |
| 173 | }); |
| 174 | } |
| 175 | |
| 176 | /** |
BitOhenry | 3e104cd | 2015-11-10 12:18:33 +0800 | [diff] [blame] | 177 | * Gets an OVSDB client instance. |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 178 | * |
BitOhenry | 3e104cd | 2015-11-10 12:18:33 +0800 | [diff] [blame] | 179 | * @param nodeId data OVSDB node id |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 180 | * @param agent OvsdbAgent |
| 181 | * @param monitorCallback Callback |
| 182 | * @param channel Channel |
| 183 | * @return OvsdbProviderService instance |
| 184 | */ |
| 185 | protected OvsdbProviderService getNodeInstance(OvsdbNodeId nodeId, |
| 186 | OvsdbAgent agent, |
| 187 | Callback monitorCallback, |
| 188 | Channel channel) { |
| 189 | OvsdbProviderService ovsdbProviderService = new DefaultOvsdbClient( |
| 190 | nodeId); |
| 191 | ovsdbProviderService.setAgent(agent); |
| 192 | ovsdbProviderService.setCallback(monitorCallback); |
| 193 | ovsdbProviderService.setChannel(channel); |
| 194 | return ovsdbProviderService; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Starts controller. |
| 199 | * |
| 200 | * @param agent OvsdbAgent |
| 201 | * @param monitorCallback Callback |
| 202 | */ |
| 203 | public void start(OvsdbAgent agent, Callback monitorCallback) { |
| 204 | this.agent = agent; |
| 205 | this.monitorCallback = monitorCallback; |
| 206 | try { |
| 207 | this.run(); |
| 208 | } catch (InterruptedException e) { |
| 209 | log.warn("Interrupted while waiting to start"); |
| 210 | Thread.currentThread().interrupt(); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Stops controller. |
| 216 | * |
| 217 | */ |
| 218 | public void stop() { |
| 219 | workerGroup.shutdownGracefully(); |
| 220 | bossGroup.shutdownGracefully(); |
| 221 | } |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 222 | |
| 223 | /** |
| 224 | * Connect to the ovsdb server with given ip address and port number. |
| 225 | * |
| 226 | * @param ip ip address |
| 227 | * @param port port number |
| 228 | */ |
| 229 | public void connect(IpAddress ip, TpPort port) { |
jaegonkim | 1af0ae5 | 2017-01-01 10:46:55 +0900 | [diff] [blame] | 230 | connect(ip, port, e -> log.warn("Connection to the ovsdb {}:{} failed(cause: {})", ip, port, e)); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Connect to the ovsdb server with given ip address, port number, and failhandler. |
| 235 | * |
| 236 | * @param ip ip address |
| 237 | * @param port port number |
| 238 | * @param failhandler connection failure handler |
| 239 | */ |
| 240 | public void connect(IpAddress ip, TpPort port, Consumer<Exception> failhandler) { |
| 241 | ChannelFutureListener listener = new ConnectionListener(this, ip, port, failhandler); |
| 242 | try { |
| 243 | connectRetry(ip, port, listener); |
| 244 | } catch (Exception e) { |
| 245 | failhandler.accept(e); |
| 246 | } |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | private void connectRetry(IpAddress ip, TpPort port, ChannelFutureListener listener) { |
jaegonkim | 1af0ae5 | 2017-01-01 10:46:55 +0900 | [diff] [blame] | 250 | Bootstrap b = new Bootstrap(); |
| 251 | b.group(workerGroup) |
| 252 | .channel(NioSocketChannel.class) |
| 253 | .option(ChannelOption.TCP_NODELAY, true) |
| 254 | .handler(new ChannelInitializer<SocketChannel>() { |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 255 | |
jaegonkim | 1af0ae5 | 2017-01-01 10:46:55 +0900 | [diff] [blame] | 256 | @Override |
| 257 | protected void initChannel(SocketChannel channel) throws Exception { |
| 258 | ChannelPipeline p = channel.pipeline(); |
| 259 | p.addLast(new MessageDecoder(), |
| 260 | new StringEncoder(CharsetUtil.UTF_8), |
| 261 | new IdleStateHandler(IDLE_TIMEOUT_SEC, 0, 0), |
| 262 | new ConnectionHandler()); |
| 263 | } |
| 264 | }); |
| 265 | b.remoteAddress(ip.toString(), port.toInt()); |
| 266 | b.connect().addListener(listener); |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | private class ConnectionListener implements ChannelFutureListener { |
BitOhenry | f006ddd | 2015-11-17 13:25:41 +0800 | [diff] [blame] | 270 | private Controller controller; |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 271 | private IpAddress ip; |
| 272 | private TpPort port; |
| 273 | private AtomicInteger count = new AtomicInteger(); |
jaegonkim | 1af0ae5 | 2017-01-01 10:46:55 +0900 | [diff] [blame] | 274 | private Consumer<Exception> failhandler; |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 275 | |
BitOhenry | f006ddd | 2015-11-17 13:25:41 +0800 | [diff] [blame] | 276 | public ConnectionListener(Controller controller, |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 277 | IpAddress ip, |
jaegonkim | 1af0ae5 | 2017-01-01 10:46:55 +0900 | [diff] [blame] | 278 | TpPort port, |
| 279 | Consumer<Exception> failhandler) { |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 280 | this.controller = controller; |
| 281 | this.ip = ip; |
| 282 | this.port = port; |
jaegonkim | 1af0ae5 | 2017-01-01 10:46:55 +0900 | [diff] [blame] | 283 | this.failhandler = failhandler; |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | @Override |
| 287 | public void operationComplete(ChannelFuture channelFuture) throws Exception { |
| 288 | if (!channelFuture.isSuccess()) { |
| 289 | channelFuture.channel().close(); |
| 290 | |
| 291 | if (count.incrementAndGet() < MAX_RETRY) { |
| 292 | final EventLoop loop = channelFuture.channel().eventLoop(); |
| 293 | |
| 294 | loop.schedule(() -> { |
jaegonkim | 1af0ae5 | 2017-01-01 10:46:55 +0900 | [diff] [blame] | 295 | try { |
| 296 | controller.connectRetry(this.ip, this.port, this); |
| 297 | } catch (Exception e) { |
| 298 | log.warn("Connection to the ovsdb server {}:{} failed(cause: {})", ip, port, e); |
| 299 | } |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 300 | }, 1L, TimeUnit.SECONDS); |
| 301 | } else { |
jaegonkim | 1af0ae5 | 2017-01-01 10:46:55 +0900 | [diff] [blame] | 302 | failhandler.accept(new Exception("max connection retry(" + MAX_RETRY + ") exceeded")); |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 303 | } |
| 304 | } else { |
| 305 | handleNewNodeConnection(channelFuture.channel()); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | private class ConnectionHandler extends ChannelDuplexHandler { |
| 311 | |
| 312 | @Override |
| 313 | public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { |
| 314 | IdleStateEvent e = (IdleStateEvent) evt; |
| 315 | |
| 316 | if (e.state() == IdleState.READER_IDLE) { |
| 317 | ctx.close(); |
| 318 | } |
| 319 | } |
| 320 | } |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 321 | } |