Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.lisp.ctl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.Lists; |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame^] | 20 | import io.netty.bootstrap.Bootstrap; |
| 21 | import io.netty.buffer.PooledByteBufAllocator; |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 22 | import io.netty.channel.ChannelFuture; |
| 23 | import io.netty.channel.ChannelOption; |
| 24 | import io.netty.channel.EventLoopGroup; |
| 25 | import io.netty.channel.nio.NioEventLoopGroup; |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame^] | 26 | import io.netty.channel.socket.nio.NioDatagramChannel; |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | import java.net.InetSocketAddress; |
| 31 | import java.util.List; |
| 32 | |
| 33 | /** |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 34 | * Bootstraps LISP netty channel, handles all setup and network listeners. |
| 35 | */ |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame^] | 36 | public class LispControllerBootstrap { |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 37 | |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame^] | 38 | protected static final Logger log = LoggerFactory.getLogger(LispControllerBootstrap.class); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 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 | |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame^] | 46 | private EventLoopGroup eventLoopGroup; |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Stitches all channel handlers into server bootstrap. |
| 50 | */ |
| 51 | public void run() { |
| 52 | |
| 53 | try { |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame^] | 54 | final Bootstrap bootstrap = createServerBootstrap(); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 55 | |
| 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 Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame^] | 80 | 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 Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 86 | |
| 87 | return bootstrap; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Configures bootstrap options to tune the communication performance. |
| 92 | * |
| 93 | * @param bootstrap LISP server bootstrap |
| 94 | */ |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame^] | 95 | private void configBootstrapOptions(Bootstrap bootstrap) { |
| 96 | bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 97 | } |
| 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 Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame^] | 131 | eventLoopGroup.shutdownGracefully().sync(); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 132 | } catch (InterruptedException e) { |
| 133 | e.printStackTrace(); |
| 134 | } |
| 135 | } |
| 136 | } |