Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 16 | package org.onlab.netty; |
| 17 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 18 | import io.netty.bootstrap.Bootstrap; |
| 19 | import io.netty.bootstrap.ServerBootstrap; |
| 20 | import io.netty.buffer.PooledByteBufAllocator; |
| 21 | import io.netty.channel.Channel; |
| 22 | import io.netty.channel.ChannelFuture; |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 23 | import io.netty.channel.ChannelHandler; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 24 | import io.netty.channel.ChannelHandlerContext; |
| 25 | import io.netty.channel.ChannelInitializer; |
| 26 | import io.netty.channel.ChannelOption; |
| 27 | import io.netty.channel.EventLoopGroup; |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 28 | import io.netty.channel.ServerChannel; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 29 | import io.netty.channel.SimpleChannelInboundHandler; |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 30 | import io.netty.channel.epoll.EpollEventLoopGroup; |
| 31 | import io.netty.channel.epoll.EpollServerSocketChannel; |
| 32 | import io.netty.channel.epoll.EpollSocketChannel; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 33 | import io.netty.channel.nio.NioEventLoopGroup; |
| 34 | import io.netty.channel.socket.SocketChannel; |
| 35 | import io.netty.channel.socket.nio.NioServerSocketChannel; |
| 36 | import io.netty.channel.socket.nio.NioSocketChannel; |
| 37 | |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 38 | import java.io.FileInputStream; |
Madan Jampani | 348a9fe | 2014-11-09 01:37:51 -0800 | [diff] [blame] | 39 | import java.io.IOException; |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 40 | import java.security.KeyStore; |
| 41 | |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 42 | import java.util.Map; |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 43 | import java.util.concurrent.CompletableFuture; |
Madan Jampani | 348a9fe | 2014-11-09 01:37:51 -0800 | [diff] [blame] | 44 | import java.util.concurrent.ConcurrentHashMap; |
Madan Jampani | ec5ae34 | 2015-04-13 15:43:10 -0700 | [diff] [blame] | 45 | import java.util.concurrent.Executor; |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 46 | import java.util.concurrent.RejectedExecutionException; |
Madan Jampani | 348a9fe | 2014-11-09 01:37:51 -0800 | [diff] [blame] | 47 | import java.util.concurrent.TimeUnit; |
| 48 | import java.util.concurrent.TimeoutException; |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 49 | import java.util.concurrent.atomic.AtomicBoolean; |
Madan Jampani | 348a9fe | 2014-11-09 01:37:51 -0800 | [diff] [blame] | 50 | import java.util.concurrent.atomic.AtomicLong; |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 51 | import java.util.function.Consumer; |
| 52 | import java.util.function.Function; |
Madan Jampani | 348a9fe | 2014-11-09 01:37:51 -0800 | [diff] [blame] | 53 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 54 | import org.apache.commons.pool.KeyedPoolableObjectFactory; |
| 55 | import org.apache.commons.pool.impl.GenericKeyedObjectPool; |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 56 | import org.onosproject.store.cluster.messaging.Endpoint; |
| 57 | import org.onosproject.store.cluster.messaging.MessagingService; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 58 | import org.slf4j.Logger; |
| 59 | import org.slf4j.LoggerFactory; |
| 60 | |
| 61 | import com.google.common.cache.Cache; |
| 62 | import com.google.common.cache.CacheBuilder; |
Madan Jampani | 5f9ec9a | 2014-10-29 13:45:52 -0700 | [diff] [blame] | 63 | import com.google.common.cache.RemovalListener; |
| 64 | import com.google.common.cache.RemovalNotification; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 65 | |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 66 | import javax.net.ssl.KeyManagerFactory; |
| 67 | import javax.net.ssl.SSLContext; |
| 68 | import javax.net.ssl.SSLEngine; |
| 69 | import javax.net.ssl.TrustManagerFactory; |
| 70 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 71 | /** |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 72 | * Implementation of MessagingService based on <a href="http://netty.io/">Netty</a> framework. |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 73 | */ |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 74 | public class NettyMessaging implements MessagingService { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 75 | |
| 76 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 77 | |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 78 | private static final String REPLY_MESSAGE_TYPE = "NETTY_MESSAGING_REQUEST_REPLY"; |
| 79 | |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 80 | private Endpoint localEp; |
| 81 | private final AtomicBoolean started = new AtomicBoolean(false); |
| 82 | private final Map<String, Consumer<InternalMessage>> handlers = new ConcurrentHashMap<>(); |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 83 | private final AtomicLong messageIdGenerator = new AtomicLong(0); |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 84 | private final Cache<Long, CompletableFuture<byte[]>> responseFutures = CacheBuilder.newBuilder() |
Madan Jampani | 5f9ec9a | 2014-10-29 13:45:52 -0700 | [diff] [blame] | 85 | .expireAfterWrite(10, TimeUnit.SECONDS) |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 86 | .removalListener(new RemovalListener<Long, CompletableFuture<byte[]>>() { |
Madan Jampani | 5f9ec9a | 2014-10-29 13:45:52 -0700 | [diff] [blame] | 87 | @Override |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 88 | public void onRemoval(RemovalNotification<Long, CompletableFuture<byte[]>> entry) { |
Yuta HIGUCHI | c611d92 | 2015-02-10 16:07:38 -0800 | [diff] [blame] | 89 | if (entry.wasEvicted()) { |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 90 | entry.getValue().completeExceptionally(new TimeoutException("Timedout waiting for reply")); |
Yuta HIGUCHI | c611d92 | 2015-02-10 16:07:38 -0800 | [diff] [blame] | 91 | } |
Madan Jampani | 5f9ec9a | 2014-10-29 13:45:52 -0700 | [diff] [blame] | 92 | } |
| 93 | }) |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 94 | .build(); |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 95 | |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 96 | private final GenericKeyedObjectPool<Endpoint, Channel> channels |
| 97 | = new GenericKeyedObjectPool<Endpoint, Channel>(new OnosCommunicationChannelFactory()); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 98 | |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 99 | private EventLoopGroup serverGroup; |
| 100 | private EventLoopGroup clientGroup; |
| 101 | private Class<? extends ServerChannel> serverChannelClass; |
| 102 | private Class<? extends Channel> clientChannelClass; |
| 103 | |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 104 | protected static final boolean TLS_DISABLED = false; |
| 105 | protected boolean enableNettyTLS = TLS_DISABLED; |
| 106 | |
| 107 | protected String ksLocation; |
| 108 | protected String tsLocation; |
| 109 | protected char[] ksPwd; |
| 110 | protected char[] tsPwd; |
| 111 | |
Madan Jampani | 5e83f33 | 2014-10-20 15:35:09 -0700 | [diff] [blame] | 112 | private void initEventLoopGroup() { |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 113 | // try Epoll first and if that does work, use nio. |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 114 | try { |
Madan Jampani | 99e9fe2 | 2014-10-21 13:47:12 -0700 | [diff] [blame] | 115 | clientGroup = new EpollEventLoopGroup(); |
| 116 | serverGroup = new EpollEventLoopGroup(); |
| 117 | serverChannelClass = EpollServerSocketChannel.class; |
| 118 | clientChannelClass = EpollSocketChannel.class; |
| 119 | return; |
Thomas Vachuska | 2c2b768 | 2015-05-05 11:59:21 -0700 | [diff] [blame] | 120 | } catch (Throwable e) { |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 121 | log.debug("Failed to initialize native (epoll) transport. " |
| 122 | + "Reason: {}. Proceeding with nio.", e.getMessage()); |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 123 | } |
| 124 | clientGroup = new NioEventLoopGroup(); |
| 125 | serverGroup = new NioEventLoopGroup(); |
| 126 | serverChannelClass = NioServerSocketChannel.class; |
| 127 | clientChannelClass = NioSocketChannel.class; |
Madan Jampani | 5e83f33 | 2014-10-20 15:35:09 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 130 | public void start(Endpoint localEp) throws Exception { |
| 131 | if (started.get()) { |
| 132 | log.warn("Already running at local endpoint: {}", localEp); |
| 133 | return; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 134 | } |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 135 | this.localEp = localEp; |
Madan Jampani | cca4bdb | 2015-05-22 13:09:33 -0700 | [diff] [blame] | 136 | channels.setLifo(true); |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 137 | channels.setTestOnBorrow(true); |
| 138 | channels.setTestOnReturn(true); |
Madan Jampani | cca4bdb | 2015-05-22 13:09:33 -0700 | [diff] [blame] | 139 | channels.setMinEvictableIdleTimeMillis(60_000L); |
| 140 | channels.setTimeBetweenEvictionRunsMillis(30_000L); |
Madan Jampani | 5e83f33 | 2014-10-20 15:35:09 -0700 | [diff] [blame] | 141 | initEventLoopGroup(); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 142 | startAcceptingConnections(); |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 143 | started.set(true); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 146 | public void stop() throws Exception { |
| 147 | if (started.get()) { |
| 148 | channels.close(); |
| 149 | serverGroup.shutdownGracefully(); |
| 150 | clientGroup.shutdownGracefully(); |
| 151 | started.set(false); |
| 152 | } |
Madan Jampani | 8710093 | 2014-10-21 16:46:12 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 155 | @Override |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 156 | public CompletableFuture<Void> sendAsync(Endpoint ep, String type, byte[] payload) { |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 157 | InternalMessage message = new InternalMessage(messageIdGenerator.incrementAndGet(), |
| 158 | localEp, |
| 159 | type, |
| 160 | payload); |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 161 | return sendAsync(ep, message); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 164 | protected CompletableFuture<Void> sendAsync(Endpoint ep, InternalMessage message) { |
| 165 | CompletableFuture<Void> future = new CompletableFuture<>(); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 166 | try { |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 167 | if (ep.equals(localEp)) { |
| 168 | dispatchLocally(message); |
| 169 | future.complete(null); |
| 170 | } else { |
| 171 | Channel channel = null; |
| 172 | try { |
| 173 | channel = channels.borrowObject(ep); |
| 174 | channel.writeAndFlush(message).addListener(channelFuture -> { |
| 175 | if (!channelFuture.isSuccess()) { |
| 176 | future.completeExceptionally(channelFuture.cause()); |
| 177 | } else { |
| 178 | future.complete(null); |
| 179 | } |
| 180 | }); |
| 181 | } finally { |
| 182 | channels.returnObject(ep, channel); |
| 183 | } |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 184 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 185 | } catch (Exception e) { |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 186 | future.completeExceptionally(e); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 187 | } |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 188 | return future; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | @Override |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 192 | public CompletableFuture<byte[]> sendAndReceive(Endpoint ep, String type, byte[] payload) { |
| 193 | CompletableFuture<byte[]> response = new CompletableFuture<>(); |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 194 | Long messageId = messageIdGenerator.incrementAndGet(); |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 195 | responseFutures.put(messageId, response); |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 196 | InternalMessage message = new InternalMessage(messageId, localEp, type, payload); |
Madan Jampani | 15cd0b8 | 2014-10-28 08:40:23 -0700 | [diff] [blame] | 197 | try { |
| 198 | sendAsync(ep, message); |
Madan Jampani | 5f9ec9a | 2014-10-29 13:45:52 -0700 | [diff] [blame] | 199 | } catch (Exception e) { |
Madan Jampani | 15cd0b8 | 2014-10-28 08:40:23 -0700 | [diff] [blame] | 200 | responseFutures.invalidate(messageId); |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 201 | response.completeExceptionally(e); |
Madan Jampani | 15cd0b8 | 2014-10-28 08:40:23 -0700 | [diff] [blame] | 202 | } |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 203 | return response; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | @Override |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 207 | public void registerHandler(String type, Consumer<byte[]> handler, Executor executor) { |
| 208 | handlers.put(type, message -> executor.execute(() -> handler.accept(message.payload()))); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 211 | @Override |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 212 | public void registerHandler(String type, Function<byte[], byte[]> handler, Executor executor) { |
| 213 | handlers.put(type, message -> executor.execute(() -> { |
| 214 | byte[] responsePayload = handler.apply(message.payload()); |
| 215 | if (responsePayload != null) { |
| 216 | InternalMessage response = new InternalMessage(message.id(), |
| 217 | localEp, |
| 218 | REPLY_MESSAGE_TYPE, |
| 219 | responsePayload); |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 220 | sendAsync(message.sender(), response).whenComplete((result, error) -> { |
| 221 | if (error != null) { |
| 222 | log.debug("Failed to respond", error); |
| 223 | } |
| 224 | }); |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 225 | } |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 226 | })); |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | @Override |
Madan Jampani | 27b69c6 | 2015-05-15 15:49:02 -0700 | [diff] [blame] | 230 | public void registerHandler(String type, Function<byte[], CompletableFuture<byte[]>> handler) { |
| 231 | handlers.put(type, message -> { |
| 232 | handler.apply(message.payload()).whenComplete((result, error) -> { |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 233 | if (error == null) { |
| 234 | InternalMessage response = new InternalMessage(message.id(), |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 235 | localEp, |
| 236 | REPLY_MESSAGE_TYPE, |
| 237 | result); |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 238 | sendAsync(message.sender(), response).whenComplete((r, e) -> { |
| 239 | if (e != null) { |
| 240 | log.debug("Failed to respond", e); |
| 241 | } |
| 242 | }); |
Madan Jampani | 27b69c6 | 2015-05-15 15:49:02 -0700 | [diff] [blame] | 243 | } |
Madan Jampani | 27b69c6 | 2015-05-15 15:49:02 -0700 | [diff] [blame] | 244 | }); |
| 245 | }); |
| 246 | } |
| 247 | |
| 248 | @Override |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 249 | public void unregisterHandler(String type) { |
Madan Jampani | 49115e9 | 2015-03-14 10:43:33 -0700 | [diff] [blame] | 250 | handlers.remove(type); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 253 | private void startAcceptingConnections() throws InterruptedException { |
| 254 | ServerBootstrap b = new ServerBootstrap(); |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 255 | b.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024); |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 256 | b.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024); |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 257 | b.option(ChannelOption.SO_RCVBUF, 1048576); |
| 258 | b.option(ChannelOption.TCP_NODELAY, true); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 259 | b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 260 | b.group(serverGroup, clientGroup); |
| 261 | b.channel(serverChannelClass); |
| 262 | if (enableNettyTLS) { |
| 263 | b.childHandler(new SSLServerCommunicationChannelInitializer()); |
| 264 | } else { |
| 265 | b.childHandler(new OnosCommunicationChannelInitializer()); |
| 266 | } |
| 267 | b.option(ChannelOption.SO_BACKLOG, 128); |
| 268 | b.childOption(ChannelOption.SO_KEEPALIVE, true); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 269 | |
| 270 | // Bind and start to accept incoming connections. |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 271 | b.bind(localEp.port()).sync().addListener(future -> { |
| 272 | if (future.isSuccess()) { |
| 273 | log.info("{} accepting incoming connections on port {}", localEp.host(), localEp.port()); |
| 274 | } else { |
| 275 | log.warn("{} failed to bind to port {}", localEp.host(), localEp.port(), future.cause()); |
| 276 | } |
| 277 | }); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | private class OnosCommunicationChannelFactory |
| 281 | implements KeyedPoolableObjectFactory<Endpoint, Channel> { |
| 282 | |
| 283 | @Override |
| 284 | public void activateObject(Endpoint endpoint, Channel channel) |
| 285 | throws Exception { |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public void destroyObject(Endpoint ep, Channel channel) throws Exception { |
Madan Jampani | 8a0569e | 2015-05-26 12:06:00 -0700 | [diff] [blame] | 290 | log.debug("Closing connection to {}", ep); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 291 | channel.close(); |
| 292 | } |
| 293 | |
| 294 | @Override |
| 295 | public Channel makeObject(Endpoint ep) throws Exception { |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 296 | Bootstrap bootstrap = new Bootstrap(); |
| 297 | bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 298 | bootstrap.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 10 * 64 * 1024); |
| 299 | bootstrap.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 10 * 32 * 1024); |
| 300 | bootstrap.option(ChannelOption.SO_SNDBUF, 1048576); |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 301 | bootstrap.group(clientGroup); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 302 | // TODO: Make this faster: |
| 303 | // http://normanmaurer.me/presentations/2014-facebook-eng-netty/slides.html#37.0 |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 304 | bootstrap.channel(clientChannelClass); |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 305 | bootstrap.option(ChannelOption.SO_KEEPALIVE, true); |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 306 | if (enableNettyTLS) { |
| 307 | bootstrap.handler(new SSLClientCommunicationChannelInitializer()); |
| 308 | } else { |
| 309 | bootstrap.handler(new OnosCommunicationChannelInitializer()); |
| 310 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 311 | // Start the client. |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 312 | ChannelFuture f = bootstrap.connect(ep.host().toString(), ep.port()).sync(); |
Madan Jampani | 8a0569e | 2015-05-26 12:06:00 -0700 | [diff] [blame] | 313 | log.debug("Established a new connection to {}", ep); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 314 | return f.channel(); |
| 315 | } |
| 316 | |
| 317 | @Override |
| 318 | public void passivateObject(Endpoint ep, Channel channel) |
| 319 | throws Exception { |
| 320 | } |
| 321 | |
| 322 | @Override |
| 323 | public boolean validateObject(Endpoint ep, Channel channel) { |
| 324 | return channel.isOpen(); |
| 325 | } |
| 326 | } |
| 327 | |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 328 | private class SSLServerCommunicationChannelInitializer extends ChannelInitializer<SocketChannel> { |
| 329 | |
| 330 | private final ChannelHandler dispatcher = new InboundMessageDispatcher(); |
| 331 | private final ChannelHandler encoder = new MessageEncoder(); |
| 332 | |
| 333 | @Override |
| 334 | protected void initChannel(SocketChannel channel) throws Exception { |
| 335 | TrustManagerFactory tmFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 336 | KeyStore ts = KeyStore.getInstance("JKS"); |
| 337 | ts.load(new FileInputStream(tsLocation), tsPwd); |
| 338 | tmFactory.init(ts); |
| 339 | |
| 340 | KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); |
| 341 | KeyStore ks = KeyStore.getInstance("JKS"); |
| 342 | ks.load(new FileInputStream(ksLocation), ksPwd); |
| 343 | kmf.init(ks, ksPwd); |
| 344 | |
| 345 | SSLContext serverContext = SSLContext.getInstance("TLS"); |
| 346 | serverContext.init(kmf.getKeyManagers(), tmFactory.getTrustManagers(), null); |
| 347 | |
| 348 | SSLEngine serverSSLEngine = serverContext.createSSLEngine(); |
| 349 | |
| 350 | serverSSLEngine.setNeedClientAuth(true); |
| 351 | serverSSLEngine.setUseClientMode(false); |
| 352 | serverSSLEngine.setEnabledProtocols(serverSSLEngine.getSupportedProtocols()); |
| 353 | serverSSLEngine.setEnabledCipherSuites(serverSSLEngine.getSupportedCipherSuites()); |
| 354 | serverSSLEngine.setEnableSessionCreation(true); |
| 355 | |
| 356 | channel.pipeline().addLast("ssl", new io.netty.handler.ssl.SslHandler(serverSSLEngine)) |
| 357 | .addLast("encoder", encoder) |
| 358 | .addLast("decoder", new MessageDecoder()) |
| 359 | .addLast("handler", dispatcher); |
| 360 | } |
| 361 | |
| 362 | } |
| 363 | |
| 364 | private class SSLClientCommunicationChannelInitializer extends ChannelInitializer<SocketChannel> { |
| 365 | |
| 366 | private final ChannelHandler dispatcher = new InboundMessageDispatcher(); |
| 367 | private final ChannelHandler encoder = new MessageEncoder(); |
| 368 | |
| 369 | @Override |
| 370 | protected void initChannel(SocketChannel channel) throws Exception { |
| 371 | TrustManagerFactory tmFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 372 | KeyStore ts = KeyStore.getInstance("JKS"); |
| 373 | ts.load(new FileInputStream(tsLocation), tsPwd); |
| 374 | tmFactory.init(ts); |
| 375 | |
| 376 | KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); |
| 377 | KeyStore ks = KeyStore.getInstance("JKS"); |
| 378 | ks.load(new FileInputStream(ksLocation), ksPwd); |
| 379 | kmf.init(ks, ksPwd); |
| 380 | |
| 381 | SSLContext clientContext = SSLContext.getInstance("TLS"); |
| 382 | clientContext.init(kmf.getKeyManagers(), tmFactory.getTrustManagers(), null); |
| 383 | |
| 384 | SSLEngine clientSSLEngine = clientContext.createSSLEngine(); |
| 385 | |
| 386 | clientSSLEngine.setUseClientMode(true); |
| 387 | clientSSLEngine.setEnabledProtocols(clientSSLEngine.getSupportedProtocols()); |
| 388 | clientSSLEngine.setEnabledCipherSuites(clientSSLEngine.getSupportedCipherSuites()); |
| 389 | clientSSLEngine.setEnableSessionCreation(true); |
| 390 | |
| 391 | channel.pipeline().addLast("ssl", new io.netty.handler.ssl.SslHandler(clientSSLEngine)) |
| 392 | .addLast("encoder", encoder) |
| 393 | .addLast("decoder", new MessageDecoder()) |
| 394 | .addLast("handler", dispatcher); |
| 395 | } |
| 396 | |
| 397 | } |
| 398 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 399 | private class OnosCommunicationChannelInitializer extends ChannelInitializer<SocketChannel> { |
| 400 | |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 401 | private final ChannelHandler dispatcher = new InboundMessageDispatcher(); |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 402 | private final ChannelHandler encoder = new MessageEncoder(); |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 403 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 404 | @Override |
| 405 | protected void initChannel(SocketChannel channel) throws Exception { |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 406 | channel.pipeline() |
| 407 | .addLast("encoder", encoder) |
| 408 | .addLast("decoder", new MessageDecoder()) |
| 409 | .addLast("handler", dispatcher); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 413 | @ChannelHandler.Sharable |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 414 | private class InboundMessageDispatcher extends SimpleChannelInboundHandler<InternalMessage> { |
| 415 | |
| 416 | @Override |
| 417 | protected void channelRead0(ChannelHandlerContext ctx, InternalMessage message) throws Exception { |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 418 | try { |
| 419 | dispatchLocally(message); |
| 420 | } catch (RejectedExecutionException e) { |
| 421 | log.warn("Unable to dispatch message due to {}", e.getMessage()); |
| 422 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 423 | } |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 424 | |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 425 | @Override |
| 426 | public void exceptionCaught(ChannelHandlerContext context, Throwable cause) { |
Madan Jampani | 29e5dfd | 2014-10-07 17:26:25 -0700 | [diff] [blame] | 427 | log.error("Exception inside channel handling pipeline.", cause); |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 428 | context.close(); |
| 429 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 430 | } |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 431 | |
Madan Jampani | ba47223 | 2015-03-04 13:00:50 -0800 | [diff] [blame] | 432 | private void dispatchLocally(InternalMessage message) throws IOException { |
Madan Jampani | 49115e9 | 2015-03-14 10:43:33 -0700 | [diff] [blame] | 433 | String type = message.type(); |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 434 | if (REPLY_MESSAGE_TYPE.equals(type)) { |
Madan Jampani | ba47223 | 2015-03-04 13:00:50 -0800 | [diff] [blame] | 435 | try { |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 436 | CompletableFuture<byte[]> futureResponse = |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 437 | responseFutures.getIfPresent(message.id()); |
Madan Jampani | ba47223 | 2015-03-04 13:00:50 -0800 | [diff] [blame] | 438 | if (futureResponse != null) { |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 439 | futureResponse.complete(message.payload()); |
Madan Jampani | ba47223 | 2015-03-04 13:00:50 -0800 | [diff] [blame] | 440 | } else { |
| 441 | log.warn("Received a reply for message id:[{}]. " |
| 442 | + " from {}. But was unable to locate the" |
| 443 | + " request handle", message.id(), message.sender()); |
| 444 | } |
| 445 | } finally { |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 446 | responseFutures.invalidate(message.id()); |
Madan Jampani | ba47223 | 2015-03-04 13:00:50 -0800 | [diff] [blame] | 447 | } |
| 448 | return; |
| 449 | } |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 450 | Consumer<InternalMessage> handler = handlers.get(type); |
Madan Jampani | ba47223 | 2015-03-04 13:00:50 -0800 | [diff] [blame] | 451 | if (handler != null) { |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 452 | handler.accept(message); |
Madan Jampani | ba47223 | 2015-03-04 13:00:50 -0800 | [diff] [blame] | 453 | } else { |
| 454 | log.debug("No handler registered for {}", type); |
| 455 | } |
| 456 | } |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 457 | } |