blob: 2191f5b2cc4705ea75cc100b1fd1c5ef508a3f85 [file] [log] [blame]
slowr13fa5b02017-08-08 16:32:31 -07001/*
2 * Copyright 2015-present Open Networking Laboratory
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
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
45 public void run() {
46 final Controller ctrl = this;
47 try {
48 ServerBootstrap b = createServerBootStrap();
49 b.childHandler(new ChannelInitializer<SctpChannel>() {
50 @Override
51 public void initChannel(SctpChannel ch) throws Exception {
52 ch.pipeline().addLast(
53 //new LoggingHandler(LogLevel.INFO),
54 new XranChannelHandler(ctrl)
55 );
56 }
57 });
58 channel = b.bind(this.port).sync();
59 } catch (Exception e) {
60 log.warn(e.getMessage());
61 e.printStackTrace();
62 }
63 }
64
65 private ServerBootstrap createServerBootStrap() {
66 bossGroup = new NioEventLoopGroup(1);
67 workerGroup = new NioEventLoopGroup();
68
69 ServerBootstrap b = new ServerBootstrap();
70 b.group(bossGroup, workerGroup)
71 .channel(NioSctpServerChannel.class)
72 .handler(new LoggingHandler(LogLevel.INFO));
73 return b;
74 }
75
slowr67d05e42017-08-11 20:37:22 -070076 public void start(XranDeviceAgent deviceAgent, XranHostAgent hostAgent, XranPacketProcessor packetAgent, int port) {
slowr13fa5b02017-08-08 16:32:31 -070077 if (isRunning && this.port != port) {
78 stop();
79 this.deviceAgent = deviceAgent;
80 this.hostAgent = hostAgent;
81 this.packetAgent = packetAgent;
82 this.port = port;
83 run();
84 } else if (!isRunning) {
85 this.deviceAgent = deviceAgent;
86 this.hostAgent = hostAgent;
87 this.packetAgent = packetAgent;
88 this.port = port;
89 run();
90 isRunning = true;
91 }
92 }
93
94
95 public void stop() {
96 if (isRunning) {
97 channel.channel().close();
98 bossGroup.shutdownGracefully();
99 workerGroup.shutdownGracefully();
100 isRunning = false;
101 }
102 }
103}