slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 1 | /* |
slowr | 577f322 | 2017-08-28 10:49:08 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 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.xran.controller; |
| 18 | |
| 19 | import io.netty.bootstrap.ServerBootstrap; |
| 20 | import io.netty.channel.ChannelFuture; |
| 21 | import io.netty.channel.ChannelInitializer; |
| 22 | import io.netty.channel.EventLoopGroup; |
| 23 | import io.netty.channel.nio.NioEventLoopGroup; |
| 24 | import io.netty.channel.sctp.SctpChannel; |
| 25 | import io.netty.channel.sctp.nio.NioSctpServerChannel; |
| 26 | import io.netty.handler.logging.LogLevel; |
| 27 | import io.netty.handler.logging.LoggingHandler; |
| 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
| 31 | /** |
| 32 | * Created by dimitris on 7/27/17. |
| 33 | */ |
| 34 | public class Controller { |
| 35 | protected static final Logger log = LoggerFactory.getLogger(Controller.class); |
| 36 | protected XranDeviceAgent deviceAgent; |
| 37 | protected XranHostAgent hostAgent; |
| 38 | protected XranPacketProcessor packetAgent; |
| 39 | private EventLoopGroup bossGroup; |
| 40 | private EventLoopGroup workerGroup; |
| 41 | private ChannelFuture channel; |
| 42 | private int port = 8007; |
| 43 | private boolean isRunning = false; |
| 44 | |
slowr | 577f322 | 2017-08-28 10:49:08 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Run SCTP server. |
| 47 | */ |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 48 | public void run() { |
| 49 | final Controller ctrl = this; |
| 50 | try { |
| 51 | ServerBootstrap b = createServerBootStrap(); |
| 52 | b.childHandler(new ChannelInitializer<SctpChannel>() { |
| 53 | @Override |
| 54 | public void initChannel(SctpChannel ch) throws Exception { |
| 55 | ch.pipeline().addLast( |
| 56 | //new LoggingHandler(LogLevel.INFO), |
| 57 | new XranChannelHandler(ctrl) |
| 58 | ); |
| 59 | } |
| 60 | }); |
| 61 | channel = b.bind(this.port).sync(); |
| 62 | } catch (Exception e) { |
| 63 | log.warn(e.getMessage()); |
| 64 | e.printStackTrace(); |
| 65 | } |
| 66 | } |
| 67 | |
slowr | 577f322 | 2017-08-28 10:49:08 -0700 | [diff] [blame] | 68 | /** |
| 69 | * Create bootstrap for server. |
| 70 | * |
| 71 | * @return server bootstrap |
| 72 | */ |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 73 | private ServerBootstrap createServerBootStrap() { |
| 74 | bossGroup = new NioEventLoopGroup(1); |
| 75 | workerGroup = new NioEventLoopGroup(); |
| 76 | |
| 77 | ServerBootstrap b = new ServerBootstrap(); |
| 78 | b.group(bossGroup, workerGroup) |
| 79 | .channel(NioSctpServerChannel.class) |
| 80 | .handler(new LoggingHandler(LogLevel.INFO)); |
| 81 | return b; |
| 82 | } |
| 83 | |
slowr | 577f322 | 2017-08-28 10:49:08 -0700 | [diff] [blame] | 84 | /** |
| 85 | * Initialize controller and start SCTP server. |
| 86 | * |
| 87 | * @param deviceAgent device agent |
| 88 | * @param hostAgent host agent |
| 89 | * @param packetAgent packet agent |
| 90 | * @param port port of server |
| 91 | */ |
slowr | 67d05e4 | 2017-08-11 20:37:22 -0700 | [diff] [blame] | 92 | public void start(XranDeviceAgent deviceAgent, XranHostAgent hostAgent, XranPacketProcessor packetAgent, int port) { |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 93 | if (isRunning && this.port != port) { |
| 94 | stop(); |
| 95 | this.deviceAgent = deviceAgent; |
| 96 | this.hostAgent = hostAgent; |
| 97 | this.packetAgent = packetAgent; |
| 98 | this.port = port; |
| 99 | run(); |
| 100 | } else if (!isRunning) { |
| 101 | this.deviceAgent = deviceAgent; |
| 102 | this.hostAgent = hostAgent; |
| 103 | this.packetAgent = packetAgent; |
| 104 | this.port = port; |
| 105 | run(); |
| 106 | isRunning = true; |
| 107 | } |
| 108 | } |
| 109 | |
slowr | 577f322 | 2017-08-28 10:49:08 -0700 | [diff] [blame] | 110 | /** |
| 111 | * Stop SCTP server. |
| 112 | */ |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 113 | public void stop() { |
| 114 | if (isRunning) { |
| 115 | channel.channel().close(); |
| 116 | bossGroup.shutdownGracefully(); |
| 117 | workerGroup.shutdownGracefully(); |
| 118 | isRunning = false; |
| 119 | } |
| 120 | } |
| 121 | } |