28e1041cd08c10fc010de482d121ff0cf07d5f1a
[onosfw.git] /
1 /*
2  * Copyright 2015 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.bgp.controller.impl;
18
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.timeout.ReadTimeoutHandler;
23 import org.jboss.netty.util.ExternalResourceReleasable;
24 import org.jboss.netty.util.HashedWheelTimer;
25 import org.jboss.netty.util.Timer;
26 import org.onosproject.bgp.controller.BgpController;
27
28 /**
29  * Creates a ChannelPipeline for a server-side bgp channel.
30  */
31 public class BgpPipelineFactory
32     implements ChannelPipelineFactory, ExternalResourceReleasable {
33
34     static final Timer TIMER = new HashedWheelTimer();
35     protected ReadTimeoutHandler readTimeoutHandler;
36     private boolean isBgpServ;
37     private BgpController bgpController;
38
39     /**
40      * Constructor to initialize the values.
41      *
42      * @param bgpController parent controller
43      * @param isBgpServ if it is a server or remote peer
44      */
45     public BgpPipelineFactory(BgpController bgpController, boolean isBgpServ) {
46         super();
47         this.isBgpServ = isBgpServ;
48         this.bgpController = bgpController;
49         /* hold time */
50         this.readTimeoutHandler = new ReadTimeoutHandler(TIMER, bgpController.getConfig().getHoldTime());
51     }
52
53     @Override
54     public ChannelPipeline getPipeline() throws Exception {
55         BgpChannelHandler handler = new BgpChannelHandler(bgpController);
56
57         ChannelPipeline pipeline = Channels.pipeline();
58         pipeline.addLast("bgpmessagedecoder", new BgpMessageDecoder());
59         pipeline.addLast("bgpmessageencoder", new BgpMessageEncoder());
60         pipeline.addLast("holdTime", readTimeoutHandler);
61         if (isBgpServ) {
62             pipeline.addLast("PassiveHandler", handler);
63         } else {
64             pipeline.addLast("ActiveHandler", handler);
65         }
66
67         return pipeline;
68     }
69
70     @Override
71     public void releaseExternalResources() {
72         TIMER.stop();
73     }
74 }