blob: b7415c0df8f70b355cb56e6bb3dc04a2b81afaa4 [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;
Jian Li6322a362016-10-31 00:57:19 +090020import io.netty.bootstrap.Bootstrap;
21import io.netty.buffer.PooledByteBufAllocator;
Jian Li451cea32016-10-04 15:27:50 +090022import io.netty.channel.ChannelFuture;
23import io.netty.channel.ChannelOption;
24import io.netty.channel.EventLoopGroup;
25import io.netty.channel.nio.NioEventLoopGroup;
Jian Li6322a362016-10-31 00:57:19 +090026import io.netty.channel.socket.nio.NioDatagramChannel;
Jian Li451cea32016-10-04 15:27:50 +090027import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import java.net.InetSocketAddress;
31import java.util.List;
32
33/**
Jian Li451cea32016-10-04 15:27:50 +090034 * Bootstraps LISP netty channel, handles all setup and network listeners.
35 */
Jian Li6322a362016-10-31 00:57:19 +090036public class LispControllerBootstrap {
Jian Li451cea32016-10-04 15:27:50 +090037
Jian Li6322a362016-10-31 00:57:19 +090038 protected static final Logger log = LoggerFactory.getLogger(LispControllerBootstrap.class);
Jian Li451cea32016-10-04 15:27:50 +090039
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
Jian Li6322a362016-10-31 00:57:19 +090046 private EventLoopGroup eventLoopGroup;
Jian Li451cea32016-10-04 15:27:50 +090047
48 /**
49 * Stitches all channel handlers into server bootstrap.
50 */
51 public void run() {
52
53 try {
Jian Li6322a362016-10-31 00:57:19 +090054 final Bootstrap bootstrap = createServerBootstrap();
Jian Li451cea32016-10-04 15:27:50 +090055
56 configBootstrapOptions(bootstrap);
57
58 List<ChannelFuture> channelFutures = Lists.newArrayList();
59
60 lispPorts.forEach(p -> {
61 InetSocketAddress sa = new InetSocketAddress(p);
62 channelFutures.add(bootstrap.bind(sa));
63 log.info("Listening for LISP router connections on {}", sa);
64 });
65
66 for (ChannelFuture f : channelFutures) {
67 f.sync();
68 }
69
70 } catch (Exception e) {
71 throw new RuntimeException(e);
72 }
73 }
74
75 /**
76 * Initializes server bootstrap with given LISP channel initializer.
77 *
78 * @return initialized server bootstrap
79 */
Jian Li6322a362016-10-31 00:57:19 +090080 private Bootstrap createServerBootstrap() {
81 Bootstrap bootstrap = new Bootstrap();
82 eventLoopGroup = new NioEventLoopGroup();
83 bootstrap.group(eventLoopGroup)
84 .channel(NioDatagramChannel.class)
85 .handler(new LispChannelInitializer());
Jian Li451cea32016-10-04 15:27:50 +090086
87 return bootstrap;
88 }
89
90 /**
91 * Configures bootstrap options to tune the communication performance.
92 *
93 * @param bootstrap LISP server bootstrap
94 */
Jian Li6322a362016-10-31 00:57:19 +090095 private void configBootstrapOptions(Bootstrap bootstrap) {
96 bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
Jian Li451cea32016-10-04 15:27:50 +090097 }
98
99 /**
100 * Closes all open channels.
101 *
102 * @param channelFutures a collection of channel futures
103 */
104 private void closeChannels(List<ChannelFuture> channelFutures) {
105 try {
106 for (ChannelFuture f : channelFutures) {
107 f.channel().closeFuture().sync();
108 }
109 } catch (InterruptedException e) {
110 e.printStackTrace();
111 }
112 }
113
114 /**
115 * Launches LISP controller to listen control channel.
116 */
117 public void start() {
118 log.info("Starting LISP control I/O");
119
120 this.run();
121 }
122
123 /**
124 * Terminates LISP controller and lease all occupied resources.
125 */
126 public void stop() {
127 log.info("Stopping LISP control I/O");
128
129 try {
130 // try to shutdown all open event groups
Jian Li6322a362016-10-31 00:57:19 +0900131 eventLoopGroup.shutdownGracefully().sync();
Jian Li451cea32016-10-04 15:27:50 +0900132 } catch (InterruptedException e) {
133 e.printStackTrace();
134 }
135 }
136}