c7ba105c229b7dd05fe90da53552a5b47c0980f4
[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
31 /**
32  * Creates a ChannelPipeline for a server-side openflow channel.
33  */
34 public class OpenflowPipelineFactory
35     implements ChannelPipelineFactory, ExternalResourceReleasable {
36
37     protected Controller controller;
38     protected ThreadPoolExecutor pipelineExecutor;
39     protected Timer timer;
40     protected IdleStateHandler idleHandler;
41     protected ReadTimeoutHandler readTimeoutHandler;
42
43     public OpenflowPipelineFactory(Controller controller,
44                                    ThreadPoolExecutor pipelineExecutor) {
45         super();
46         this.controller = controller;
47         this.pipelineExecutor = pipelineExecutor;
48         this.timer = new HashedWheelTimer();
49         this.idleHandler = new IdleStateHandler(timer, 20, 25, 0);
50         this.readTimeoutHandler = new ReadTimeoutHandler(timer, 30);
51     }
52
53     @Override
54     public ChannelPipeline getPipeline() throws Exception {
55         OFChannelHandler handler = new OFChannelHandler(controller);
56
57         ChannelPipeline pipeline = Channels.pipeline();
58         pipeline.addLast("ofmessagedecoder", new OFMessageDecoder());
59         pipeline.addLast("ofmessageencoder", new OFMessageEncoder());
60         pipeline.addLast("idle", idleHandler);
61         pipeline.addLast("timeout", readTimeoutHandler);
62         // XXX S ONOS: was 15 increased it to fix Issue #296
63         pipeline.addLast("handshaketimeout",
64                          new HandshakeTimeoutHandler(handler, timer, 60));
65         if (pipelineExecutor != null) {
66             pipeline.addLast("pipelineExecutor",
67                              new ExecutionHandler(pipelineExecutor));
68         }
69         pipeline.addLast("handler", handler);
70         return pipeline;
71     }
72
73     @Override
74     public void releaseExternalResources() {
75         timer.stop();
76     }
77 }