blob: 01c87dbcf58b90eeea0643135214d28ef6f02fbb [file] [log] [blame]
Jian Li451cea32016-10-04 15:27:50 +09001/*
2 * Copyright 2016-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 */
16package org.onosproject.lisp.ctl;
17
18import com.google.common.collect.ImmutableList;
19import com.google.common.collect.Lists;
20import io.netty.bootstrap.ServerBootstrap;
21import io.netty.channel.ChannelFuture;
22import io.netty.channel.ChannelOption;
23import io.netty.channel.EventLoopGroup;
24import io.netty.channel.nio.NioEventLoopGroup;
25import io.netty.channel.socket.nio.NioServerSocketChannel;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import java.net.InetSocketAddress;
30import java.util.List;
31
32/**
33 * The main LISP controller class.
34 * Bootstraps LISP netty channel, handles all setup and network listeners.
35 */
36public class LispController {
37
38 protected static final Logger log = LoggerFactory.getLogger(LispController.class);
39
40 private static final int LISP_DATA_PORT = 4341;
41 private static final int LISP_CONTROL_PORT = 4342;
42
43 // Configuration options
44 protected List<Integer> lispPorts = ImmutableList.of(LISP_DATA_PORT, LISP_CONTROL_PORT);
45
46 protected static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024;
47
48 private EventLoopGroup bossGroup;
49 private EventLoopGroup workerGroup;
50
51 /**
52 * Stitches all channel handlers into server bootstrap.
53 */
54 public void run() {
55
56 try {
57 final ServerBootstrap bootstrap = createServerBootstrap();
58
59 configBootstrapOptions(bootstrap);
60
61 List<ChannelFuture> channelFutures = Lists.newArrayList();
62
63 lispPorts.forEach(p -> {
64 InetSocketAddress sa = new InetSocketAddress(p);
65 channelFutures.add(bootstrap.bind(sa));
66 log.info("Listening for LISP router connections on {}", sa);
67 });
68
69 for (ChannelFuture f : channelFutures) {
70 f.sync();
71 }
72
73 } catch (Exception e) {
74 throw new RuntimeException(e);
75 }
76 }
77
78 /**
79 * Initializes server bootstrap with given LISP channel initializer.
80 *
81 * @return initialized server bootstrap
82 */
83 private ServerBootstrap createServerBootstrap() {
84 ServerBootstrap bootstrap = new ServerBootstrap();
85 bossGroup = new NioEventLoopGroup();
86 workerGroup = new NioEventLoopGroup();
87 bootstrap.group(bossGroup, workerGroup)
88 .channel(NioServerSocketChannel.class)
89 .childHandler(new LispChannelInitializer());
90
91 return bootstrap;
92 }
93
94 /**
95 * Configures bootstrap options to tune the communication performance.
96 *
97 * @param bootstrap LISP server bootstrap
98 */
99 private void configBootstrapOptions(ServerBootstrap bootstrap) {
100 bootstrap.option(ChannelOption.SO_REUSEADDR, true);
101 bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
102 bootstrap.option(ChannelOption.SO_SNDBUF, SEND_BUFFER_SIZE);
103 }
104
105 /**
106 * Closes all open channels.
107 *
108 * @param channelFutures a collection of channel futures
109 */
110 private void closeChannels(List<ChannelFuture> channelFutures) {
111 try {
112 for (ChannelFuture f : channelFutures) {
113 f.channel().closeFuture().sync();
114 }
115 } catch (InterruptedException e) {
116 e.printStackTrace();
117 }
118 }
119
120 /**
121 * Launches LISP controller to listen control channel.
122 */
123 public void start() {
124 log.info("Starting LISP control I/O");
125
126 this.run();
127 }
128
129 /**
130 * Terminates LISP controller and lease all occupied resources.
131 */
132 public void stop() {
133 log.info("Stopping LISP control I/O");
134
135 try {
136 // try to shutdown all open event groups
137 bossGroup.shutdownGracefully().sync();
138 workerGroup.shutdownGracefully().sync();
139 } catch (InterruptedException e) {
140 e.printStackTrace();
141 }
142 }
143}