Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | |
| 19 | import com.google.common.collect.Maps; |
| 20 | import com.google.common.util.concurrent.Striped; |
| 21 | import io.grpc.ManagedChannel; |
Brian O'Connor | c694383 | 2018-12-12 17:27:11 -0800 | [diff] [blame] | 22 | import io.grpc.netty.GrpcSslContexts; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 23 | import io.grpc.netty.NettyChannelBuilder; |
Brian O'Connor | c694383 | 2018-12-12 17:27:11 -0800 | [diff] [blame] | 24 | import io.netty.handler.ssl.SslContext; |
| 25 | import io.netty.handler.ssl.util.InsecureTrustManagerFactory; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 26 | import org.onosproject.event.AbstractListenerManager; |
| 27 | import org.onosproject.event.Event; |
| 28 | import org.onosproject.event.EventListener; |
| 29 | import org.onosproject.grpc.api.GrpcChannelController; |
| 30 | import org.onosproject.grpc.api.GrpcChannelId; |
| 31 | import org.onosproject.grpc.api.GrpcClient; |
| 32 | import org.onosproject.grpc.api.GrpcClientController; |
| 33 | import org.onosproject.grpc.api.GrpcClientKey; |
| 34 | import org.onosproject.net.DeviceId; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 35 | import org.onosproject.net.device.DeviceAgentEvent; |
| 36 | import org.onosproject.net.device.DeviceAgentListener; |
| 37 | import org.onosproject.net.provider.ProviderId; |
Ray Milkey | 5739b2c | 2018-11-06 14:04:51 -0800 | [diff] [blame] | 38 | import org.osgi.service.component.annotations.Activate; |
Ray Milkey | 5739b2c | 2018-11-06 14:04:51 -0800 | [diff] [blame] | 39 | import org.osgi.service.component.annotations.Deactivate; |
| 40 | import org.osgi.service.component.annotations.Reference; |
| 41 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 42 | import org.slf4j.Logger; |
| 43 | |
Brian O'Connor | c694383 | 2018-12-12 17:27:11 -0800 | [diff] [blame] | 44 | import javax.net.ssl.SSLException; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 45 | import java.util.Map; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 46 | import java.util.concurrent.ConcurrentMap; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 47 | import java.util.concurrent.locks.Lock; |
| 48 | import java.util.function.Supplier; |
| 49 | |
| 50 | import static com.google.common.base.Preconditions.checkNotNull; |
Carmelo Cascone | a71b849 | 2018-12-17 17:47:50 -0800 | [diff] [blame] | 51 | import static java.lang.String.format; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 52 | import static org.slf4j.LoggerFactory.getLogger; |
| 53 | |
| 54 | /** |
| 55 | * Abstract class of a gRPC based client controller for specific gRPC client |
| 56 | * which provides basic gRPC client management and thread safe mechanism. |
| 57 | * |
| 58 | * @param <C> the gRPC client type |
| 59 | * @param <K> the key type of the gRPC client |
| 60 | * @param <E> the event type of the gRPC client |
| 61 | * @param <L> the event listener of event {@link E} |
| 62 | */ |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 63 | public abstract class AbstractGrpcClientController |
| 64 | <K extends GrpcClientKey, C extends GrpcClient, E extends Event, L extends EventListener<E>> |
| 65 | extends AbstractListenerManager<E, L> |
| 66 | implements GrpcClientController<K, C> { |
| 67 | |
| 68 | /** |
| 69 | * The default max inbound message size (MB). |
| 70 | */ |
| 71 | private static final int DEFAULT_MAX_INBOUND_MSG_SIZE = 256; // Megabytes. |
| 72 | private static final int MEGABYTES = 1024 * 1024; |
| 73 | private static final int DEFAULT_DEVICE_LOCK_SIZE = 30; |
| 74 | |
| 75 | private final Logger log = getLogger(getClass()); |
| 76 | private final Map<DeviceId, K> clientKeys = Maps.newHashMap(); |
| 77 | private final Map<K, C> clients = Maps.newHashMap(); |
| 78 | private final Map<DeviceId, GrpcChannelId> channelIds = Maps.newHashMap(); |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 79 | private final ConcurrentMap<DeviceId, ConcurrentMap<ProviderId, DeviceAgentListener>> |
| 80 | deviceAgentListeners = Maps.newConcurrentMap(); |
| 81 | private final Class<E> eventClass; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 82 | private final Striped<Lock> stripedLocks = Striped.lock(DEFAULT_DEVICE_LOCK_SIZE); |
| 83 | |
Ray Milkey | 5739b2c | 2018-11-06 14:04:51 -0800 | [diff] [blame] | 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Carmelo Cascone | a46f554 | 2018-12-12 23:41:01 -0800 | [diff] [blame] | 85 | protected GrpcChannelController grpcChannelController; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 86 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 87 | public AbstractGrpcClientController(Class<E> eventClass) { |
| 88 | this.eventClass = eventClass; |
| 89 | } |
| 90 | |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 91 | @Activate |
| 92 | public void activate() { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 93 | eventDispatcher.addSink(eventClass, listenerRegistry); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 94 | log.info("Started"); |
| 95 | } |
| 96 | |
| 97 | @Deactivate |
| 98 | public void deactivate() { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 99 | eventDispatcher.removeSink(eventClass); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 100 | clientKeys.keySet().forEach(this::removeClient); |
| 101 | clientKeys.clear(); |
| 102 | clients.clear(); |
| 103 | channelIds.clear(); |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 104 | deviceAgentListeners.clear(); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 105 | log.info("Stopped"); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public boolean createClient(K clientKey) { |
| 110 | checkNotNull(clientKey); |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 111 | return withDeviceLock(() -> doCreateClient(clientKey), |
| 112 | clientKey.deviceId()); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 115 | private boolean doCreateClient(K clientKey) { |
Ray Milkey | fb503a7 | 2018-12-19 14:03:17 -0800 | [diff] [blame] | 116 | DeviceId deviceId = clientKey.deviceId(); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 117 | |
| 118 | if (clientKeys.containsKey(deviceId)) { |
| 119 | final GrpcClientKey existingKey = clientKeys.get(deviceId); |
| 120 | if (clientKey.equals(existingKey)) { |
Carmelo Cascone | a71b849 | 2018-12-17 17:47:50 -0800 | [diff] [blame] | 121 | log.debug("Not creating {} as it already exists... (key={})", |
| 122 | clientName(clientKey), clientKey); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 123 | return true; |
| 124 | } else { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 125 | throw new IllegalArgumentException(format( |
| 126 | "A client already exists for device %s (%s)", |
| 127 | deviceId, clientKey)); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
Brian O'Connor | 1a37866 | 2018-12-12 17:27:11 -0800 | [diff] [blame] | 130 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 131 | final String method = clientKey.requiresSecureChannel() |
| 132 | ? "TLS" : "plaintext TCP"; |
| 133 | |
| 134 | log.info("Connecting {} client for {} to server at {} using {}...", |
| 135 | clientKey.serviceName(), deviceId, clientKey.serveUri(), method); |
Brian O'Connor | c694383 | 2018-12-12 17:27:11 -0800 | [diff] [blame] | 136 | |
| 137 | SslContext sslContext = null; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 138 | if (clientKey.requiresSecureChannel()) { |
Brian O'Connor | c694383 | 2018-12-12 17:27:11 -0800 | [diff] [blame] | 139 | try { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 140 | // FIXME: Accept any server certificate; this is insecure and |
| 141 | // should not be used in production |
| 142 | sslContext = GrpcSslContexts.forClient().trustManager( |
| 143 | InsecureTrustManagerFactory.INSTANCE).build(); |
Brian O'Connor | c694383 | 2018-12-12 17:27:11 -0800 | [diff] [blame] | 144 | } catch (SSLException e) { |
| 145 | log.error("Failed to build SSL Context", e); |
| 146 | return false; |
| 147 | } |
| 148 | } |
| 149 | |
Carmelo Cascone | 73f4530 | 2019-02-04 23:11:26 -0800 | [diff] [blame] | 150 | GrpcChannelId channelId = GrpcChannelId.of(clientKey.toString()); |
Brian O'Connor | c694383 | 2018-12-12 17:27:11 -0800 | [diff] [blame] | 151 | NettyChannelBuilder channelBuilder = NettyChannelBuilder |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 152 | .forAddress(clientKey.serveUri().getHost(), |
| 153 | clientKey.serveUri().getPort()) |
Brian O'Connor | c694383 | 2018-12-12 17:27:11 -0800 | [diff] [blame] | 154 | .maxInboundMessageSize(DEFAULT_MAX_INBOUND_MSG_SIZE * MEGABYTES); |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 155 | |
Brian O'Connor | c694383 | 2018-12-12 17:27:11 -0800 | [diff] [blame] | 156 | if (sslContext != null) { |
Brian O'Connor | c694383 | 2018-12-12 17:27:11 -0800 | [diff] [blame] | 157 | channelBuilder |
| 158 | .sslContext(sslContext) |
| 159 | .useTransportSecurity(); |
| 160 | } else { |
Brian O'Connor | c694383 | 2018-12-12 17:27:11 -0800 | [diff] [blame] | 161 | channelBuilder.usePlaintext(); |
| 162 | } |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 163 | |
Carmelo Cascone | a71b849 | 2018-12-17 17:47:50 -0800 | [diff] [blame] | 164 | final ManagedChannel channel; |
Ray Milkey | fb503a7 | 2018-12-19 14:03:17 -0800 | [diff] [blame] | 165 | |
Brian O'Connor | c694383 | 2018-12-12 17:27:11 -0800 | [diff] [blame] | 166 | try { |
| 167 | channel = grpcChannelController.connectChannel(channelId, channelBuilder); |
| 168 | } catch (Throwable e) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 169 | log.warn("Failed to connect to {} ({}) using {}: {}", |
| 170 | deviceId, clientKey.serveUri(), method, e.toString()); |
| 171 | log.debug("gRPC client connection exception", e); |
Brian O'Connor | c694383 | 2018-12-12 17:27:11 -0800 | [diff] [blame] | 172 | return false; |
| 173 | } |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 174 | |
Carmelo Cascone | a71b849 | 2018-12-17 17:47:50 -0800 | [diff] [blame] | 175 | final C client; |
| 176 | try { |
| 177 | client = createClientInstance(clientKey, channel); |
| 178 | } catch (Throwable e) { |
| 179 | log.error("Exception while creating {}", clientName(clientKey), e); |
| 180 | grpcChannelController.disconnectChannel(channelId); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 181 | return false; |
| 182 | } |
Carmelo Cascone | a71b849 | 2018-12-17 17:47:50 -0800 | [diff] [blame] | 183 | |
| 184 | if (client == null) { |
| 185 | log.error("Unable to create {}, implementation returned null... (key={})", |
| 186 | clientName(clientKey), clientKey); |
| 187 | grpcChannelController.disconnectChannel(channelId); |
| 188 | return false; |
| 189 | } |
| 190 | |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 191 | clientKeys.put(deviceId, clientKey); |
| 192 | clients.put(clientKey, client); |
| 193 | channelIds.put(deviceId, channelId); |
| 194 | |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | protected abstract C createClientInstance(K clientKey, ManagedChannel channel); |
| 199 | |
| 200 | @Override |
| 201 | public C getClient(DeviceId deviceId) { |
| 202 | checkNotNull(deviceId); |
| 203 | return withDeviceLock(() -> doGetClient(deviceId), deviceId); |
| 204 | } |
| 205 | |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 206 | private C doGetClient(DeviceId deviceId) { |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 207 | if (!clientKeys.containsKey(deviceId)) { |
| 208 | return null; |
| 209 | } |
| 210 | return clients.get(clientKeys.get(deviceId)); |
| 211 | } |
| 212 | |
| 213 | @Override |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 214 | public C getClient(K clientKey) { |
| 215 | checkNotNull(clientKey); |
| 216 | return clients.get(clientKey); |
| 217 | } |
| 218 | |
| 219 | @Override |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 220 | public void removeClient(DeviceId deviceId) { |
| 221 | checkNotNull(deviceId); |
| 222 | withDeviceLock(() -> doRemoveClient(deviceId), deviceId); |
| 223 | } |
| 224 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 225 | @Override |
| 226 | public void removeClient(K clientKey) { |
| 227 | checkNotNull(clientKey); |
| 228 | withDeviceLock(() -> doRemoveClient(clientKey), clientKey.deviceId()); |
| 229 | } |
| 230 | |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 231 | private Void doRemoveClient(DeviceId deviceId) { |
| 232 | if (clientKeys.containsKey(deviceId)) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 233 | doRemoveClient(clientKeys.get(deviceId)); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 234 | } |
| 235 | return null; |
| 236 | } |
| 237 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 238 | private Void doRemoveClient(K clientKey) { |
| 239 | if (clients.containsKey(clientKey)) { |
| 240 | clients.get(clientKey).shutdown(); |
| 241 | } |
| 242 | if (channelIds.containsKey(clientKey.deviceId())) { |
| 243 | grpcChannelController.disconnectChannel( |
| 244 | channelIds.get(clientKey.deviceId())); |
| 245 | } |
| 246 | clientKeys.remove(clientKey.deviceId()); |
| 247 | clients.remove(clientKey); |
| 248 | channelIds.remove(clientKey.deviceId()); |
| 249 | return null; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 252 | @Override |
| 253 | public void addDeviceAgentListener(DeviceId deviceId, ProviderId providerId, DeviceAgentListener listener) { |
| 254 | checkNotNull(deviceId, "deviceId cannot be null"); |
| 255 | checkNotNull(deviceId, "providerId cannot be null"); |
| 256 | checkNotNull(listener, "listener cannot be null"); |
| 257 | deviceAgentListeners.putIfAbsent(deviceId, Maps.newConcurrentMap()); |
| 258 | deviceAgentListeners.get(deviceId).put(providerId, listener); |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public void removeDeviceAgentListener(DeviceId deviceId, ProviderId providerId) { |
| 263 | checkNotNull(deviceId, "deviceId cannot be null"); |
| 264 | checkNotNull(providerId, "listener cannot be null"); |
| 265 | deviceAgentListeners.computeIfPresent(deviceId, (did, listeners) -> { |
| 266 | listeners.remove(providerId); |
| 267 | return listeners.isEmpty() ? null : listeners; |
| 268 | }); |
| 269 | } |
| 270 | |
| 271 | public void postEvent(E event) { |
| 272 | checkNotNull(event); |
| 273 | post(event); |
| 274 | } |
| 275 | |
| 276 | public void postEvent(DeviceAgentEvent event) { |
| 277 | // We should have only one event delivery mechanism. We have two now |
| 278 | // because we have two different types of events, DeviceAgentEvent and |
| 279 | // controller/protocol specific ones (e.g. P4Runtime or gNMI). |
| 280 | // TODO: extend device agent event to allow delivery protocol-specific |
| 281 | // events, e.g. packet-in |
| 282 | checkNotNull(event); |
| 283 | if (deviceAgentListeners.containsKey(event.subject())) { |
| 284 | deviceAgentListeners.get(event.subject()).values() |
| 285 | .forEach(l -> l.event(event)); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 286 | } |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | private <U> U withDeviceLock(Supplier<U> task, DeviceId deviceId) { |
| 290 | final Lock lock = stripedLocks.get(deviceId); |
| 291 | lock.lock(); |
| 292 | try { |
| 293 | return task.get(); |
| 294 | } finally { |
| 295 | lock.unlock(); |
| 296 | } |
| 297 | } |
Carmelo Cascone | a71b849 | 2018-12-17 17:47:50 -0800 | [diff] [blame] | 298 | |
| 299 | private String clientName(GrpcClientKey key) { |
| 300 | return format("%s client for %s", key.serviceName(), key.deviceId()); |
| 301 | } |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 302 | } |