blob: 8a92ffde2a95f3673bfd7c4263480e464237060b [file] [log] [blame]
slowr13fa5b02017-08-08 16:32:31 -07001/*
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -08002 * Copyright 2017-present Open Networking Foundation
slowr13fa5b02017-08-08 16:32:31 -07003 *
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 Mavrommatis96b255a2017-12-06 13:09:25 -080017package org.onosproject.xran.impl.controller;
slowr13fa5b02017-08-08 16:32:31 -070018
19import io.netty.bootstrap.ServerBootstrap;
20import io.netty.channel.ChannelFuture;
21import io.netty.channel.ChannelInitializer;
22import io.netty.channel.EventLoopGroup;
23import io.netty.channel.nio.NioEventLoopGroup;
24import io.netty.channel.sctp.SctpChannel;
25import io.netty.channel.sctp.nio.NioSctpServerChannel;
26import io.netty.handler.logging.LogLevel;
27import io.netty.handler.logging.LoggingHandler;
slowr23a93e12017-09-01 13:26:18 -070028import org.onlab.packet.IpAddress;
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080029import org.onosproject.xran.XranDeviceAgent;
30import org.onosproject.xran.XranHostAgent;
31import org.onosproject.xran.XranPacketProcessor;
slowr13fa5b02017-08-08 16:32:31 -070032import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
35/**
36 * Created by dimitris on 7/27/17.
37 */
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080038public class XranServer {
39 protected static final Logger log = LoggerFactory.getLogger(XranServer.class);
slowr13fa5b02017-08-08 16:32:31 -070040 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;
slowr23a93e12017-09-01 13:26:18 -070047 private IpAddress bindAddress = IpAddress.valueOf("0.0.0.0");
slowr13fa5b02017-08-08 16:32:31 -070048 private boolean isRunning = false;
49
slowr577f3222017-08-28 10:49:08 -070050 /**
51 * Run SCTP server.
52 */
slowr13fa5b02017-08-08 16:32:31 -070053 public void run() {
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080054 final XranServer ctrl = this;
slowr13fa5b02017-08-08 16:32:31 -070055 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 });
slowr23a93e12017-09-01 13:26:18 -070066 channel = b.bind(this.bindAddress.toInetAddress(), this.port).sync();
slowr13fa5b02017-08-08 16:32:31 -070067 } catch (Exception e) {
68 log.warn(e.getMessage());
69 e.printStackTrace();
70 }
71 }
72
slowr577f3222017-08-28 10:49:08 -070073 /**
74 * Create bootstrap for server.
75 *
76 * @return server bootstrap
77 */
slowr13fa5b02017-08-08 16:32:31 -070078 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
slowr577f3222017-08-28 10:49:08 -070089 /**
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080090 * Initialize xranServer and start SCTP server.
slowr23a93e12017-09-01 13:26:18 -070091 * @param deviceAgent device agent
slowr577f3222017-08-28 10:49:08 -070092 * @param hostAgent host agent
93 * @param packetAgent packet agent
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080094 * @param xrancIp xran bind IP
slowr577f3222017-08-28 10:49:08 -070095 * @param port port of server
96 */
slowr23a93e12017-09-01 13:26:18 -070097 public void start(XranDeviceAgent deviceAgent, XranHostAgent hostAgent, XranPacketProcessor packetAgent,
98 IpAddress xrancIp, int port) {
99 if (isRunning && (this.port != port || !this.bindAddress.equals(xrancIp))) {
slowr13fa5b02017-08-08 16:32:31 -0700100 stop();
101 this.deviceAgent = deviceAgent;
102 this.hostAgent = hostAgent;
103 this.packetAgent = packetAgent;
104 this.port = port;
slowr23a93e12017-09-01 13:26:18 -0700105 this.bindAddress = xrancIp;
slowr13fa5b02017-08-08 16:32:31 -0700106 run();
107 } else if (!isRunning) {
108 this.deviceAgent = deviceAgent;
109 this.hostAgent = hostAgent;
110 this.packetAgent = packetAgent;
111 this.port = port;
slowr23a93e12017-09-01 13:26:18 -0700112 this.bindAddress = xrancIp;
slowr13fa5b02017-08-08 16:32:31 -0700113 run();
114 isRunning = true;
115 }
116 }
117
slowr577f3222017-08-28 10:49:08 -0700118 /**
119 * Stop SCTP server.
120 */
slowr13fa5b02017-08-08 16:32:31 -0700121 public void stop() {
122 if (isRunning) {
123 channel.channel().close();
124 bossGroup.shutdownGracefully();
125 workerGroup.shutdownGracefully();
126 isRunning = false;
127 }
128 }
129}