1467520dd1f78a86c843a2d6274a661200d74629
[onosfw.git] /
1 /*
2  * Copyright 2014 Open Networking Laboratory
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.openflow.controller.impl;
18
19 import java.util.concurrent.ThreadPoolExecutor;
20
21 import org.jboss.netty.channel.ChannelPipeline;
22 import org.jboss.netty.channel.ChannelPipelineFactory;
23 import org.jboss.netty.channel.Channels;
24 import org.jboss.netty.handler.execution.ExecutionHandler;
25 import org.jboss.netty.handler.timeout.IdleStateHandler;
26 import org.jboss.netty.handler.timeout.ReadTimeoutHandler;
27 import org.jboss.netty.util.ExternalResourceReleasable;
28 import org.jboss.netty.util.HashedWheelTimer;
29 import org.jboss.netty.util.Timer;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import javax.net.ssl.SSLEngine;
34
35 /**
36  * Creates a ChannelPipeline for a server-side openflow channel.
37  */
38 public class OpenflowPipelineFactory
39     implements ChannelPipelineFactory, ExternalResourceReleasable {
40
41     private final Logger log = LoggerFactory.getLogger(getClass());
42
43     private final SSLEngine sslEngine;
44     protected Controller controller;
45     protected ThreadPoolExecutor pipelineExecutor;
46     protected Timer timer;
47     protected IdleStateHandler idleHandler;
48     protected ReadTimeoutHandler readTimeoutHandler;
49
50     public OpenflowPipelineFactory(Controller controller,
51                                    ThreadPoolExecutor pipelineExecutor,
52                                    SSLEngine sslEngine) {
53         super();
54         this.controller = controller;
55         this.pipelineExecutor = pipelineExecutor;
56         this.timer = new HashedWheelTimer();
57         this.idleHandler = new IdleStateHandler(timer, 20, 25, 0);
58         this.readTimeoutHandler = new ReadTimeoutHandler(timer, 30);
59         this.sslEngine = sslEngine;
60     }
61
62     @Override
63     public ChannelPipeline getPipeline() throws Exception {
64         OFChannelHandler handler = new OFChannelHandler(controller);
65
66         ChannelPipeline pipeline = Channels.pipeline();
67         if (sslEngine != null) {
68             log.info("OpenFlow SSL enabled.");
69             pipeline.addLast("ssl",
70                              new org.jboss.netty.handler.ssl.SslHandler(sslEngine));
71         } else {
72             log.info("OpenFlow SSL disabled");
73         }
74         pipeline.addLast("ofmessagedecoder", new OFMessageDecoder());
75         pipeline.addLast("ofmessageencoder", new OFMessageEncoder());
76         pipeline.addLast("idle", idleHandler);
77         pipeline.addLast("timeout", readTimeoutHandler);
78         // XXX S ONOS: was 15 increased it to fix Issue #296
79         pipeline.addLast("handshaketimeout",
80                          new HandshakeTimeoutHandler(handler, timer, 60));
81         if (pipelineExecutor != null) {
82             pipeline.addLast("pipelineExecutor",
83                              new ExecutionHandler(pipelineExecutor));
84         }
85         pipeline.addLast("handler", handler);
86         return pipeline;
87     }
88
89     @Override
90     public void releaseExternalResources() {
91         timer.stop();
92     }
93 }