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