debanshur | 37cf6ba | 2018-05-08 20:07:30 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | |
| 17 | package org.onosproject.ovsdb.controller.impl; |
| 18 | |
| 19 | import io.netty.channel.ChannelInitializer; |
| 20 | import io.netty.channel.ChannelPipeline; |
| 21 | import io.netty.channel.socket.SocketChannel; |
| 22 | import io.netty.handler.codec.string.StringEncoder; |
| 23 | import io.netty.handler.ssl.SslHandler; |
| 24 | import io.netty.handler.timeout.IdleStateHandler; |
| 25 | import io.netty.handler.timeout.ReadTimeoutHandler; |
| 26 | import io.netty.util.CharsetUtil; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | import javax.net.ssl.SSLContext; |
| 31 | import javax.net.ssl.SSLEngine; |
| 32 | |
| 33 | /** |
| 34 | * Creates a ChannelInitializer for a server-side OVSDB channel. |
| 35 | */ |
| 36 | public class OvsdbChannelInitializer |
| 37 | extends ChannelInitializer<SocketChannel> { |
| 38 | |
| 39 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 40 | |
| 41 | private final SSLContext sslContext; |
| 42 | protected Controller controller; |
| 43 | |
| 44 | private static final int READER_IDLE_TIME = 20; |
| 45 | private static final int WRITER_IDLE_TIME = 25; |
| 46 | private static final int ALL_IDLE_TIME = 0; |
| 47 | private static final int TIMEOUT = 180; |
| 48 | |
| 49 | public OvsdbChannelInitializer(Controller controller, SSLContext sslContext) { |
| 50 | super(); |
| 51 | this.controller = controller; |
| 52 | this.sslContext = sslContext; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | protected void initChannel(SocketChannel channel) throws Exception { |
| 57 | |
| 58 | ChannelPipeline pipeline = channel.pipeline(); |
| 59 | if (sslContext != null) { |
| 60 | log.info("OVSDB SSL enabled."); |
| 61 | SSLEngine sslEngine = sslContext.createSSLEngine(); |
| 62 | |
| 63 | sslEngine.setNeedClientAuth(true); |
| 64 | sslEngine.setUseClientMode(false); |
| 65 | sslEngine.setEnabledProtocols(sslEngine.getSupportedProtocols()); |
| 66 | sslEngine.setEnabledCipherSuites(sslEngine.getSupportedCipherSuites()); |
| 67 | sslEngine.setEnableSessionCreation(true); |
| 68 | |
| 69 | SslHandler sslHandler = new SslHandler(sslEngine); |
| 70 | pipeline.addLast("ssl", sslHandler); |
| 71 | } else { |
| 72 | log.info("OVSDB SSL disabled."); |
| 73 | } |
| 74 | pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8)); |
| 75 | pipeline.addLast(new MessageDecoder()); |
| 76 | |
| 77 | pipeline.addLast(new IdleStateHandler(READER_IDLE_TIME, WRITER_IDLE_TIME, ALL_IDLE_TIME)); |
| 78 | pipeline.addLast(new ReadTimeoutHandler(TIMEOUT)); |
| 79 | controller.handleNewNodeConnection(channel); |
| 80 | } |
| 81 | } |
| 82 | |