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