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