Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.grpc.ctl; |
| 18 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 19 | import com.google.common.util.concurrent.Striped; |
Carmelo Cascone | b953669 | 2019-05-28 18:15:23 -0700 | [diff] [blame] | 20 | import io.grpc.LoadBalancerRegistry; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 21 | import io.grpc.ManagedChannel; |
| 22 | import io.grpc.ManagedChannelBuilder; |
Carmelo Cascone | b953669 | 2019-05-28 18:15:23 -0700 | [diff] [blame] | 23 | import io.grpc.NameResolverRegistry; |
| 24 | import io.grpc.internal.DnsNameResolverProvider; |
| 25 | import io.grpc.internal.PickFirstLoadBalancerProvider; |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 26 | import io.grpc.netty.GrpcSslContexts; |
| 27 | import io.grpc.netty.NettyChannelBuilder; |
| 28 | import io.netty.handler.ssl.SslContext; |
| 29 | import io.netty.handler.ssl.util.InsecureTrustManagerFactory; |
Andrea Campanella | a74bdba | 2018-05-15 16:45:00 +0200 | [diff] [blame] | 30 | import org.onlab.util.Tools; |
| 31 | import org.onosproject.cfg.ComponentConfigService; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 32 | import org.onosproject.grpc.api.GrpcChannelController; |
Andrea Campanella | a74bdba | 2018-05-15 16:45:00 +0200 | [diff] [blame] | 33 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 34 | import org.osgi.service.component.annotations.Activate; |
| 35 | import org.osgi.service.component.annotations.Component; |
| 36 | import org.osgi.service.component.annotations.Deactivate; |
| 37 | import org.osgi.service.component.annotations.Modified; |
| 38 | import org.osgi.service.component.annotations.Reference; |
| 39 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 40 | import org.slf4j.Logger; |
| 41 | import org.slf4j.LoggerFactory; |
| 42 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 43 | import javax.net.ssl.SSLException; |
| 44 | import java.net.URI; |
Andrea Campanella | a74bdba | 2018-05-15 16:45:00 +0200 | [diff] [blame] | 45 | import java.util.Dictionary; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 46 | import java.util.Map; |
| 47 | import java.util.Optional; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 48 | import java.util.concurrent.ConcurrentHashMap; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 49 | import java.util.concurrent.TimeUnit; |
Carmelo Cascone | 73f4530 | 2019-02-04 23:11:26 -0800 | [diff] [blame] | 50 | import java.util.concurrent.atomic.AtomicBoolean; |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 51 | import java.util.concurrent.locks.Lock; |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 52 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 53 | import static com.google.common.base.Preconditions.checkArgument; |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 54 | import static com.google.common.base.Preconditions.checkNotNull; |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 55 | import static com.google.common.base.Strings.isNullOrEmpty; |
Carmelo Cascone | 6d57f32 | 2018-12-13 23:15:17 -0800 | [diff] [blame] | 56 | import static java.lang.String.format; |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 57 | import static org.onosproject.grpc.ctl.OsgiPropertyConstants.ENABLE_MESSAGE_LOG; |
| 58 | import static org.onosproject.grpc.ctl.OsgiPropertyConstants.ENABLE_MESSAGE_LOG_DEFAULT; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 59 | |
| 60 | /** |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 61 | * Default implementation of the GrpcChannelController. |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 62 | */ |
Ray Milkey | 5739b2c | 2018-11-06 14:04:51 -0800 | [diff] [blame] | 63 | @Component(immediate = true, service = GrpcChannelController.class, |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 64 | property = { |
Carmelo Cascone | 73f4530 | 2019-02-04 23:11:26 -0800 | [diff] [blame] | 65 | ENABLE_MESSAGE_LOG + ":Boolean=" + ENABLE_MESSAGE_LOG_DEFAULT, |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 66 | }) |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 67 | public class GrpcChannelControllerImpl implements GrpcChannelController { |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 68 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 69 | private static final String GRPC = "grpc"; |
| 70 | private static final String GRPCS = "grpcs"; |
| 71 | |
| 72 | private static final int DEFAULT_MAX_INBOUND_MSG_SIZE = 256; // Megabytes. |
| 73 | private static final int MEGABYTES = 1024 * 1024; |
| 74 | |
Carmelo Cascone | b953669 | 2019-05-28 18:15:23 -0700 | [diff] [blame] | 75 | private static final PickFirstLoadBalancerProvider PICK_FIRST_LOAD_BALANCER_PROVIDER = |
| 76 | new PickFirstLoadBalancerProvider(); |
| 77 | private static final DnsNameResolverProvider DNS_NAME_RESOLVER_PROVIDER = |
| 78 | new DnsNameResolverProvider(); |
| 79 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andrea Campanella | a74bdba | 2018-05-15 16:45:00 +0200 | [diff] [blame] | 81 | protected ComponentConfigService componentConfigService; |
| 82 | |
Carmelo Cascone | 73f4530 | 2019-02-04 23:11:26 -0800 | [diff] [blame] | 83 | /** |
| 84 | * Indicates whether to log gRPC messages. |
| 85 | */ |
| 86 | private final AtomicBoolean enableMessageLog = new AtomicBoolean( |
| 87 | ENABLE_MESSAGE_LOG_DEFAULT); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 88 | |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 89 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 90 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 91 | private Map<URI, ManagedChannel> channels; |
| 92 | private Map<URI, GrpcLoggingInterceptor> interceptors; |
Carmelo Cascone | 73f4530 | 2019-02-04 23:11:26 -0800 | [diff] [blame] | 93 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 94 | private final Striped<Lock> channelLocks = Striped.lock(30); |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 95 | |
| 96 | @Activate |
| 97 | public void activate() { |
Andrea Campanella | a74bdba | 2018-05-15 16:45:00 +0200 | [diff] [blame] | 98 | componentConfigService.registerProperties(getClass()); |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 99 | channels = new ConcurrentHashMap<>(); |
Carmelo Cascone | 73f4530 | 2019-02-04 23:11:26 -0800 | [diff] [blame] | 100 | interceptors = new ConcurrentHashMap<>(); |
Carmelo Cascone | b953669 | 2019-05-28 18:15:23 -0700 | [diff] [blame] | 101 | LoadBalancerRegistry.getDefaultRegistry() |
| 102 | .register(PICK_FIRST_LOAD_BALANCER_PROVIDER); |
| 103 | NameResolverRegistry.getDefaultRegistry() |
| 104 | .register(DNS_NAME_RESOLVER_PROVIDER); |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 105 | log.info("Started"); |
| 106 | } |
| 107 | |
Andrea Campanella | a74bdba | 2018-05-15 16:45:00 +0200 | [diff] [blame] | 108 | @Modified |
| 109 | public void modified(ComponentContext context) { |
| 110 | if (context != null) { |
| 111 | Dictionary<?, ?> properties = context.getProperties(); |
Carmelo Cascone | 73f4530 | 2019-02-04 23:11:26 -0800 | [diff] [blame] | 112 | enableMessageLog.set(Tools.isPropertyEnabled( |
| 113 | properties, ENABLE_MESSAGE_LOG, ENABLE_MESSAGE_LOG_DEFAULT)); |
| 114 | log.info("Configured. Logging of gRPC messages is {}", |
Carmelo Cascone | 62d5c2e | 2019-03-07 18:53:17 -0800 | [diff] [blame] | 115 | enableMessageLog.get() ? "ENABLED" : "DISABLED"); |
Andrea Campanella | a74bdba | 2018-05-15 16:45:00 +0200 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 119 | @Deactivate |
| 120 | public void deactivate() { |
Carmelo Cascone | b953669 | 2019-05-28 18:15:23 -0700 | [diff] [blame] | 121 | LoadBalancerRegistry.getDefaultRegistry() |
| 122 | .deregister(PICK_FIRST_LOAD_BALANCER_PROVIDER); |
| 123 | NameResolverRegistry.getDefaultRegistry() |
| 124 | .register(DNS_NAME_RESOLVER_PROVIDER); |
Andrea Campanella | a74bdba | 2018-05-15 16:45:00 +0200 | [diff] [blame] | 125 | componentConfigService.unregisterProperties(getClass(), false); |
Carmelo Cascone | 6d57f32 | 2018-12-13 23:15:17 -0800 | [diff] [blame] | 126 | channels.values().forEach(ManagedChannel::shutdownNow); |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 127 | channels.clear(); |
Carmelo Cascone | 73f4530 | 2019-02-04 23:11:26 -0800 | [diff] [blame] | 128 | channels = null; |
| 129 | interceptors.values().forEach(GrpcLoggingInterceptor::close); |
| 130 | interceptors.clear(); |
| 131 | interceptors = null; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 132 | log.info("Stopped"); |
| 133 | } |
| 134 | |
| 135 | @Override |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 136 | public ManagedChannel create(URI channelUri) { |
| 137 | return create(channelUri, makeChannelBuilder(channelUri)); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Override |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 141 | public ManagedChannel create(URI channelUri, ManagedChannelBuilder<?> channelBuilder) { |
| 142 | checkNotNull(channelUri); |
| 143 | checkNotNull(channelBuilder); |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 144 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 145 | channelLocks.get(channelUri).lock(); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 146 | try { |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 147 | if (channels.containsKey(channelUri)) { |
| 148 | throw new IllegalArgumentException(format( |
| 149 | "A channel with ID '%s' already exists", channelUri)); |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 150 | } |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 151 | |
| 152 | log.info("Creating new gRPC channel {}...", channelUri); |
| 153 | |
| 154 | final GrpcLoggingInterceptor interceptor = new GrpcLoggingInterceptor( |
| 155 | channelUri, enableMessageLog); |
| 156 | channelBuilder.intercept(interceptor); |
| 157 | |
| 158 | final ManagedChannel channel = channelBuilder.build(); |
| 159 | |
| 160 | channels.put(channelUri, channelBuilder.build()); |
| 161 | interceptors.put(channelUri, interceptor); |
| 162 | |
| 163 | return channel; |
| 164 | } finally { |
| 165 | channelLocks.get(channelUri).unlock(); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | private NettyChannelBuilder makeChannelBuilder(URI channelUri) { |
| 170 | |
| 171 | checkArgument(channelUri.getScheme().equals(GRPC) |
| 172 | || channelUri.getScheme().equals(GRPCS), |
| 173 | format("Server URI scheme must be %s or %s", GRPC, GRPCS)); |
| 174 | checkArgument(!isNullOrEmpty(channelUri.getHost()), |
| 175 | "Server host address should not be empty"); |
| 176 | checkArgument(channelUri.getPort() > 0 && channelUri.getPort() <= 65535, |
| 177 | "Invalid server port"); |
| 178 | |
| 179 | final boolean useTls = channelUri.getScheme().equals(GRPCS); |
| 180 | |
| 181 | final NettyChannelBuilder channelBuilder = NettyChannelBuilder |
Carmelo Cascone | b953669 | 2019-05-28 18:15:23 -0700 | [diff] [blame] | 182 | .forAddress(channelUri.getHost(), channelUri.getPort()) |
| 183 | .nameResolverFactory(DNS_NAME_RESOLVER_PROVIDER) |
| 184 | .defaultLoadBalancingPolicy( |
| 185 | PICK_FIRST_LOAD_BALANCER_PROVIDER.getPolicyName()) |
| 186 | .maxInboundMessageSize( |
| 187 | DEFAULT_MAX_INBOUND_MSG_SIZE * MEGABYTES); |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 188 | |
| 189 | if (useTls) { |
| 190 | try { |
| 191 | // Accept any server certificate; this is insecure and |
| 192 | // should not be used in production. |
| 193 | final SslContext sslContext = GrpcSslContexts.forClient().trustManager( |
| 194 | InsecureTrustManagerFactory.INSTANCE).build(); |
| 195 | channelBuilder.sslContext(sslContext).useTransportSecurity(); |
| 196 | } catch (SSLException e) { |
| 197 | log.error("Failed to build SSL context", e); |
| 198 | return null; |
| 199 | } |
| 200 | } else { |
| 201 | channelBuilder.usePlaintext(); |
| 202 | } |
| 203 | |
| 204 | return channelBuilder; |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public void destroy(URI channelUri) { |
| 209 | checkNotNull(channelUri); |
| 210 | |
| 211 | channelLocks.get(channelUri).lock(); |
| 212 | try { |
pierventre | f92de51 | 2021-05-18 18:06:40 +0200 | [diff] [blame^] | 213 | log.info("Destroying gRPC channel {}...", channelUri); |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 214 | final ManagedChannel channel = channels.remove(channelUri); |
| 215 | if (channel != null) { |
| 216 | shutdownNowAndWait(channel, channelUri); |
| 217 | } |
| 218 | final GrpcLoggingInterceptor interceptor = interceptors.remove(channelUri); |
Carmelo Cascone | 73f4530 | 2019-02-04 23:11:26 -0800 | [diff] [blame] | 219 | if (interceptor != null) { |
| 220 | interceptor.close(); |
| 221 | } |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 222 | } finally { |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 223 | channelLocks.get(channelUri).unlock(); |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 224 | } |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 225 | } |
| 226 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 227 | private void shutdownNowAndWait(ManagedChannel channel, URI channelUri) { |
Carmelo Cascone | a71b849 | 2018-12-17 17:47:50 -0800 | [diff] [blame] | 228 | try { |
| 229 | if (!channel.shutdownNow() |
| 230 | .awaitTermination(5, TimeUnit.SECONDS)) { |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 231 | log.error("Channel {} did not terminate properly", |
| 232 | channelUri); |
Carmelo Cascone | a71b849 | 2018-12-17 17:47:50 -0800 | [diff] [blame] | 233 | } |
| 234 | } catch (InterruptedException e) { |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 235 | log.warn("Channel {} didn't shutdown in time", channelUri); |
Carmelo Cascone | a71b849 | 2018-12-17 17:47:50 -0800 | [diff] [blame] | 236 | Thread.currentThread().interrupt(); |
| 237 | } |
| 238 | } |
| 239 | |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 240 | @Override |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 241 | public Optional<ManagedChannel> get(URI channelUri) { |
| 242 | checkNotNull(channelUri); |
| 243 | return Optional.ofNullable(channels.get(channelUri)); |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 244 | } |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 245 | } |