Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 16 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 17 | package org.onosproject.openflow.controller.impl; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 18 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 19 | import org.jboss.netty.channel.ChannelPipeline; |
| 20 | import org.jboss.netty.channel.ChannelPipelineFactory; |
| 21 | import org.jboss.netty.channel.Channels; |
| 22 | import org.jboss.netty.handler.execution.ExecutionHandler; |
alshabib | 5162a9d | 2015-12-07 17:49:37 -0800 | [diff] [blame] | 23 | import org.jboss.netty.handler.ssl.SslHandler; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 24 | import org.jboss.netty.handler.timeout.IdleStateHandler; |
| 25 | import org.jboss.netty.handler.timeout.ReadTimeoutHandler; |
| 26 | import org.jboss.netty.util.ExternalResourceReleasable; |
| 27 | import org.jboss.netty.util.HashedWheelTimer; |
| 28 | import org.jboss.netty.util.Timer; |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
alshabib | 5162a9d | 2015-12-07 17:49:37 -0800 | [diff] [blame] | 32 | import javax.net.ssl.SSLContext; |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 33 | import javax.net.ssl.SSLEngine; |
Yuta HIGUCHI | a809a17 | 2016-07-21 16:28:38 -0700 | [diff] [blame] | 34 | |
| 35 | import static org.onlab.util.Tools.groupedThreads; |
| 36 | |
alshabib | 5162a9d | 2015-12-07 17:49:37 -0800 | [diff] [blame] | 37 | import java.util.concurrent.ThreadPoolExecutor; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Creates a ChannelPipeline for a server-side openflow channel. |
| 41 | */ |
| 42 | public class OpenflowPipelineFactory |
| 43 | implements ChannelPipelineFactory, ExternalResourceReleasable { |
| 44 | |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 45 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 46 | |
alshabib | 5162a9d | 2015-12-07 17:49:37 -0800 | [diff] [blame] | 47 | |
| 48 | private final SSLContext sslContext; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 49 | protected Controller controller; |
| 50 | protected ThreadPoolExecutor pipelineExecutor; |
| 51 | protected Timer timer; |
| 52 | protected IdleStateHandler idleHandler; |
| 53 | protected ReadTimeoutHandler readTimeoutHandler; |
| 54 | |
| 55 | public OpenflowPipelineFactory(Controller controller, |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 56 | ThreadPoolExecutor pipelineExecutor, |
alshabib | 5162a9d | 2015-12-07 17:49:37 -0800 | [diff] [blame] | 57 | SSLContext sslContext) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 58 | super(); |
| 59 | this.controller = controller; |
| 60 | this.pipelineExecutor = pipelineExecutor; |
Yuta HIGUCHI | a809a17 | 2016-07-21 16:28:38 -0700 | [diff] [blame] | 61 | this.timer = new HashedWheelTimer(groupedThreads("OpenflowPipelineFactory", "timer-%d", log)); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 62 | this.idleHandler = new IdleStateHandler(timer, 20, 25, 0); |
| 63 | this.readTimeoutHandler = new ReadTimeoutHandler(timer, 30); |
alshabib | 5162a9d | 2015-12-07 17:49:37 -0800 | [diff] [blame] | 64 | this.sslContext = sslContext; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public ChannelPipeline getPipeline() throws Exception { |
| 69 | OFChannelHandler handler = new OFChannelHandler(controller); |
| 70 | |
| 71 | ChannelPipeline pipeline = Channels.pipeline(); |
alshabib | 5162a9d | 2015-12-07 17:49:37 -0800 | [diff] [blame] | 72 | if (sslContext != null) { |
Madan Jampani | 6bcf5f2 | 2016-04-18 13:49:17 -0700 | [diff] [blame] | 73 | log.debug("OpenFlow SSL enabled."); |
alshabib | 5162a9d | 2015-12-07 17:49:37 -0800 | [diff] [blame] | 74 | SSLEngine sslEngine = sslContext.createSSLEngine(); |
| 75 | |
| 76 | sslEngine.setNeedClientAuth(true); |
| 77 | sslEngine.setUseClientMode(false); |
| 78 | sslEngine.setEnabledProtocols(sslEngine.getSupportedProtocols()); |
| 79 | sslEngine.setEnabledCipherSuites(sslEngine.getSupportedCipherSuites()); |
| 80 | sslEngine.setEnableSessionCreation(true); |
| 81 | |
| 82 | SslHandler sslHandler = new SslHandler(sslEngine); |
| 83 | pipeline.addLast("ssl", sslHandler); |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 84 | } else { |
Madan Jampani | 6bcf5f2 | 2016-04-18 13:49:17 -0700 | [diff] [blame] | 85 | log.debug("OpenFlow SSL disabled."); |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 86 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 87 | pipeline.addLast("ofmessagedecoder", new OFMessageDecoder()); |
| 88 | pipeline.addLast("ofmessageencoder", new OFMessageEncoder()); |
| 89 | pipeline.addLast("idle", idleHandler); |
| 90 | pipeline.addLast("timeout", readTimeoutHandler); |
| 91 | // XXX S ONOS: was 15 increased it to fix Issue #296 |
| 92 | pipeline.addLast("handshaketimeout", |
| 93 | new HandshakeTimeoutHandler(handler, timer, 60)); |
| 94 | if (pipelineExecutor != null) { |
| 95 | pipeline.addLast("pipelineExecutor", |
| 96 | new ExecutionHandler(pipelineExecutor)); |
| 97 | } |
| 98 | pipeline.addLast("handler", handler); |
| 99 | return pipeline; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void releaseExternalResources() { |
| 104 | timer.stop(); |
| 105 | } |
| 106 | } |