blob: 33e30d0d5c61908aee59b3b84bc32214c2391238 [file] [log] [blame]
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -07001/*
2 * Copyright 2017-present Open Networking Foundation
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.artemis.impl.moas;
18
19import com.google.common.annotations.Beta;
20import io.netty.bootstrap.ServerBootstrap;
21import io.netty.channel.ChannelFuture;
22import io.netty.channel.ChannelInitializer;
23import io.netty.channel.ChannelOption;
24import io.netty.channel.EventLoopGroup;
25import io.netty.channel.nio.NioEventLoopGroup;
26import io.netty.channel.socket.SocketChannel;
27import io.netty.channel.socket.nio.NioServerSocketChannel;
28import org.apache.commons.lang.exception.ExceptionUtils;
29import org.onosproject.artemis.ArtemisMoasAgent;
30import org.onosproject.artemis.ArtemisPacketProcessor;
31import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33
34
35/**
36 * MOAS Server Controller.
37 */
38@Beta
39public class MoasServerController {
40 private final Logger log = LoggerFactory.getLogger(getClass());
41
42 protected ArtemisMoasAgent deviceAgent;
43 protected ArtemisPacketProcessor packetAgent;
44
45 private EventLoopGroup bossGroup;
46 private EventLoopGroup workerGroup;
47 private ChannelFuture channel;
48 private int port = 32323;
49
50 private boolean isRunning = false;
51
52 /**
53 * Run the MOAS Servcer.
54 */
55 private void run() {
56 final MoasServerController ctrl = this;
57 try {
58 final ServerBootstrap bootstrap = createServerBootStrap();
59
60 bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
61 @Override
62 public void initChannel(SocketChannel ch) throws Exception {
63 ch.pipeline().addLast(
64 new MoasServerHandler(ctrl)
65 );
66 }
67 });
68
69 channel = bootstrap.bind(port).sync();
70 isRunning = true;
71 } catch (Exception e) {
72 log.warn(ExceptionUtils.getFullStackTrace(e));
73 }
74 }
75
76 /**
77 * Create netty server bootstrap.
78 *
79 * @return bootstrap
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070080 */
Ray Milkey986a47a2018-01-25 11:38:51 -080081 private ServerBootstrap createServerBootStrap() {
82 bossGroup = new NioEventLoopGroup();
83 workerGroup = new NioEventLoopGroup();
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070084
Ray Milkey986a47a2018-01-25 11:38:51 -080085 return new ServerBootstrap()
86 .group(bossGroup, workerGroup)
87 .channel(NioServerSocketChannel.class)
88 .option(ChannelOption.SO_REUSEADDR, true)
89 .childOption(ChannelOption.SO_KEEPALIVE, true)
90 .childOption(ChannelOption.TCP_NODELAY, true);
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070091 }
92
93 /**
94 * Start Server Controller and initialize agents.
95 *
96 * @param deviceAgent device agent
97 * @param packetAgent packet agen
98 */
99 public void start(ArtemisMoasAgent deviceAgent, ArtemisPacketProcessor packetAgent) {
100 if (isRunning) {
101 stop();
102 this.deviceAgent = deviceAgent;
103 this.packetAgent = packetAgent;
104 run();
105 } else {
106 this.deviceAgent = deviceAgent;
107 this.packetAgent = packetAgent;
108 run();
109 }
110 isRunning = true;
111 }
112
113 /**
114 * Stop Server Controller.
115 */
116 public void stop() {
117 if (isRunning) {
118 channel.channel().close();
119 bossGroup.shutdownGracefully();
120 workerGroup.shutdownGracefully();
121 isRunning = false;
122 }
123 }
124}