blob: e8caaccb0434612c6ce67f1d46c621d7312eb08d [file] [log] [blame]
slowr13fa5b02017-08-08 16:32:31 -07001/*
slowr577f3222017-08-28 10:49:08 -07002 * Copyright 2015-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
17package org.onosproject.xran.controller;
18
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;
slowr13fa5b02017-08-08 16:32:31 -070029import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32/**
33 * Created by dimitris on 7/27/17.
34 */
35public 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;
slowr23a93e12017-09-01 13:26:18 -070044 private IpAddress bindAddress = IpAddress.valueOf("0.0.0.0");
slowr13fa5b02017-08-08 16:32:31 -070045 private boolean isRunning = false;
46
slowr577f3222017-08-28 10:49:08 -070047 /**
48 * Run SCTP server.
49 */
slowr13fa5b02017-08-08 16:32:31 -070050 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 });
slowr23a93e12017-09-01 13:26:18 -070063 channel = b.bind(this.bindAddress.toInetAddress(), this.port).sync();
slowr13fa5b02017-08-08 16:32:31 -070064 } catch (Exception e) {
65 log.warn(e.getMessage());
66 e.printStackTrace();
67 }
68 }
69
slowr577f3222017-08-28 10:49:08 -070070 /**
71 * Create bootstrap for server.
72 *
73 * @return server bootstrap
74 */
slowr13fa5b02017-08-08 16:32:31 -070075 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
slowr577f3222017-08-28 10:49:08 -070086 /**
87 * Initialize controller and start SCTP server.
slowr23a93e12017-09-01 13:26:18 -070088 * @param deviceAgent device agent
slowr577f3222017-08-28 10:49:08 -070089 * @param hostAgent host agent
90 * @param packetAgent packet agent
slowr23a93e12017-09-01 13:26:18 -070091 * @param xrancIp
slowr577f3222017-08-28 10:49:08 -070092 * @param port port of server
93 */
slowr23a93e12017-09-01 13:26:18 -070094 public void start(XranDeviceAgent deviceAgent, XranHostAgent hostAgent, XranPacketProcessor packetAgent,
95 IpAddress xrancIp, int port) {
96 if (isRunning && (this.port != port || !this.bindAddress.equals(xrancIp))) {
slowr13fa5b02017-08-08 16:32:31 -070097 stop();
98 this.deviceAgent = deviceAgent;
99 this.hostAgent = hostAgent;
100 this.packetAgent = packetAgent;
101 this.port = port;
slowr23a93e12017-09-01 13:26:18 -0700102 this.bindAddress = xrancIp;
slowr13fa5b02017-08-08 16:32:31 -0700103 run();
104 } else if (!isRunning) {
105 this.deviceAgent = deviceAgent;
106 this.hostAgent = hostAgent;
107 this.packetAgent = packetAgent;
108 this.port = port;
slowr23a93e12017-09-01 13:26:18 -0700109 this.bindAddress = xrancIp;
slowr13fa5b02017-08-08 16:32:31 -0700110 run();
111 isRunning = true;
112 }
113 }
114
slowr577f3222017-08-28 10:49:08 -0700115 /**
116 * Stop SCTP server.
117 */
slowr13fa5b02017-08-08 16:32:31 -0700118 public void stop() {
119 if (isRunning) {
120 channel.channel().close();
121 bossGroup.shutdownGracefully();
122 workerGroup.shutdownGracefully();
123 isRunning = false;
124 }
125 }
126}