blob: 946a1bd40bef76aa52c288cc31ae0d926d5f8c0d [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;
28import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
31/**
32 * Created by dimitris on 7/27/17.
33 */
34public 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
slowr577f3222017-08-28 10:49:08 -070045 /**
46 * Run SCTP server.
47 */
slowr13fa5b02017-08-08 16:32:31 -070048 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
slowr577f3222017-08-28 10:49:08 -070068 /**
69 * Create bootstrap for server.
70 *
71 * @return server bootstrap
72 */
slowr13fa5b02017-08-08 16:32:31 -070073 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
slowr577f3222017-08-28 10:49:08 -070084 /**
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 */
slowr67d05e42017-08-11 20:37:22 -070092 public void start(XranDeviceAgent deviceAgent, XranHostAgent hostAgent, XranPacketProcessor packetAgent, int port) {
slowr13fa5b02017-08-08 16:32:31 -070093 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
slowr577f3222017-08-28 10:49:08 -0700110 /**
111 * Stop SCTP server.
112 */
slowr13fa5b02017-08-08 16:32:31 -0700113 public void stop() {
114 if (isRunning) {
115 channel.channel().close();
116 bossGroup.shutdownGracefully();
117 workerGroup.shutdownGracefully();
118 isRunning = false;
119 }
120 }
121}