blob: c27a1348f8fc27d3596bba5b6c9b07eb37efad94 [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
80 * @throws Exception exception
81 */
82 private ServerBootstrap createServerBootStrap() throws Exception {
83 try {
84 bossGroup = new NioEventLoopGroup();
85 workerGroup = new NioEventLoopGroup();
86
87 return new ServerBootstrap()
88 .group(bossGroup, workerGroup)
89 .channel(NioServerSocketChannel.class)
90 .option(ChannelOption.SO_REUSEADDR, true)
91 .childOption(ChannelOption.SO_KEEPALIVE, true)
92 .childOption(ChannelOption.TCP_NODELAY, true);
93 } catch (Exception e) {
94 throw new Exception(e);
95 }
96 }
97
98 /**
99 * Start Server Controller and initialize agents.
100 *
101 * @param deviceAgent device agent
102 * @param packetAgent packet agen
103 */
104 public void start(ArtemisMoasAgent deviceAgent, ArtemisPacketProcessor packetAgent) {
105 if (isRunning) {
106 stop();
107 this.deviceAgent = deviceAgent;
108 this.packetAgent = packetAgent;
109 run();
110 } else {
111 this.deviceAgent = deviceAgent;
112 this.packetAgent = packetAgent;
113 run();
114 }
115 isRunning = true;
116 }
117
118 /**
119 * Stop Server Controller.
120 */
121 public void stop() {
122 if (isRunning) {
123 channel.channel().close();
124 bossGroup.shutdownGracefully();
125 workerGroup.shutdownGracefully();
126 isRunning = false;
127 }
128 }
129}