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 | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableMap; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 21 | import com.google.common.util.concurrent.Striped; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 22 | import io.grpc.CallOptions; |
| 23 | import io.grpc.Channel; |
| 24 | import io.grpc.ClientCall; |
| 25 | import io.grpc.ClientInterceptor; |
| 26 | import io.grpc.ForwardingClientCall; |
| 27 | import io.grpc.ForwardingClientCallListener; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 28 | import io.grpc.ManagedChannel; |
| 29 | import io.grpc.ManagedChannelBuilder; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 30 | import io.grpc.Metadata; |
| 31 | import io.grpc.MethodDescriptor; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 32 | import io.grpc.Status; |
| 33 | import io.grpc.StatusRuntimeException; |
Andrea Campanella | a74bdba | 2018-05-15 16:45:00 +0200 | [diff] [blame] | 34 | import org.onlab.util.Tools; |
| 35 | import org.onosproject.cfg.ComponentConfigService; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 36 | import org.onosproject.grpc.api.GrpcChannelId; |
| 37 | import org.onosproject.grpc.api.GrpcController; |
Carmelo Cascone | 6a1ae71 | 2018-08-10 12:19:47 -0700 | [diff] [blame] | 38 | import org.onosproject.grpc.proto.dummy.Dummy; |
| 39 | import org.onosproject.grpc.proto.dummy.DummyServiceGrpc; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 40 | import org.onosproject.net.DeviceId; |
Andrea Campanella | a74bdba | 2018-05-15 16:45:00 +0200 | [diff] [blame] | 41 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 42 | import org.osgi.service.component.annotations.Activate; |
| 43 | import org.osgi.service.component.annotations.Component; |
| 44 | import org.osgi.service.component.annotations.Deactivate; |
| 45 | import org.osgi.service.component.annotations.Modified; |
| 46 | import org.osgi.service.component.annotations.Reference; |
| 47 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 48 | import org.slf4j.Logger; |
| 49 | import org.slf4j.LoggerFactory; |
| 50 | |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 51 | import java.io.IOException; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 52 | import java.util.Collection; |
Andrea Campanella | a74bdba | 2018-05-15 16:45:00 +0200 | [diff] [blame] | 53 | import java.util.Dictionary; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 54 | import java.util.HashSet; |
| 55 | import java.util.Map; |
| 56 | import java.util.Optional; |
| 57 | import java.util.Set; |
| 58 | import java.util.concurrent.ConcurrentHashMap; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 59 | import java.util.concurrent.TimeUnit; |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 60 | import java.util.concurrent.locks.Lock; |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 61 | |
| 62 | import static com.google.common.base.Preconditions.checkNotNull; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * Default implementation of the GrpcController. |
| 66 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 67 | @Component(immediate = true, service = GrpcController.class) |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 68 | public class GrpcControllerImpl implements GrpcController { |
| 69 | |
Andrea Campanella | a74bdba | 2018-05-15 16:45:00 +0200 | [diff] [blame] | 70 | private static final String SET_FORWARDING_PIPELINE_CONFIG_METHOD = "p4.P4Runtime/SetForwardingPipelineConfig"; |
| 71 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andrea Campanella | a74bdba | 2018-05-15 16:45:00 +0200 | [diff] [blame] | 73 | protected ComponentConfigService componentConfigService; |
| 74 | |
| 75 | // Hint: set to true to log all gRPC messages received/sent on all channels |
| 76 | // Does not enable log on existing channels |
| 77 | private static final boolean DEFAULT_LOG_LEVEL = false; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 78 | //@Property(name = "enableMessageLog", boolValue = DEFAULT_LOG_LEVEL, |
| 79 | // label = "Indicates whether to log all gRPC messages sent and received on all channels") |
Andrea Campanella | a74bdba | 2018-05-15 16:45:00 +0200 | [diff] [blame] | 80 | public static boolean enableMessageLog = DEFAULT_LOG_LEVEL; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 81 | |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 82 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 83 | |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 84 | private Map<GrpcChannelId, ManagedChannel> channels; |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 85 | private final Striped<Lock> channelLocks = Striped.lock(30); |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 86 | |
| 87 | @Activate |
| 88 | public void activate() { |
Andrea Campanella | a74bdba | 2018-05-15 16:45:00 +0200 | [diff] [blame] | 89 | componentConfigService.registerProperties(getClass()); |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 90 | channels = new ConcurrentHashMap<>(); |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 91 | log.info("Started"); |
| 92 | } |
| 93 | |
Andrea Campanella | a74bdba | 2018-05-15 16:45:00 +0200 | [diff] [blame] | 94 | @Modified |
| 95 | public void modified(ComponentContext context) { |
| 96 | if (context != null) { |
| 97 | Dictionary<?, ?> properties = context.getProperties(); |
| 98 | enableMessageLog = Tools.isPropertyEnabled(properties, "enableMessageLog", |
| 99 | DEFAULT_LOG_LEVEL); |
| 100 | log.info("Configured. Log of gRPC messages is {}", enableMessageLog ? "enabled" : "disabled"); |
| 101 | } |
| 102 | } |
| 103 | |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 104 | @Deactivate |
| 105 | public void deactivate() { |
Andrea Campanella | a74bdba | 2018-05-15 16:45:00 +0200 | [diff] [blame] | 106 | componentConfigService.unregisterProperties(getClass(), false); |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 107 | channels.values().forEach(ManagedChannel::shutdown); |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 108 | channels.clear(); |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 109 | log.info("Stopped"); |
| 110 | } |
| 111 | |
| 112 | @Override |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 113 | public ManagedChannel connectChannel(GrpcChannelId channelId, |
| 114 | ManagedChannelBuilder<?> channelBuilder) |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 115 | throws IOException { |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 116 | checkNotNull(channelId); |
| 117 | checkNotNull(channelBuilder); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 118 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 119 | Lock lock = channelLocks.get(channelId); |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 120 | lock.lock(); |
| 121 | |
| 122 | try { |
Andrea Campanella | a74bdba | 2018-05-15 16:45:00 +0200 | [diff] [blame] | 123 | channelBuilder.intercept(new InternalLogChannelInterceptor(channelId)); |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 124 | ManagedChannel channel = channelBuilder.build(); |
| 125 | // Forced connection not yet implemented. Use workaround... |
| 126 | // channel.getState(true); |
| 127 | doDummyMessage(channel); |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 128 | channels.put(channelId, channel); |
| 129 | return channel; |
| 130 | } finally { |
| 131 | lock.unlock(); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 132 | } |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 135 | private void doDummyMessage(ManagedChannel channel) throws IOException { |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 136 | DummyServiceGrpc.DummyServiceBlockingStub dummyStub = DummyServiceGrpc |
| 137 | .newBlockingStub(channel) |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 138 | .withDeadlineAfter(CONNECTION_TIMEOUT_SECONDS, TimeUnit.SECONDS); |
| 139 | try { |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 140 | dummyStub.sayHello(Dummy.DummyMessageThatNoOneWouldReallyUse |
| 141 | .getDefaultInstance()); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 142 | } catch (StatusRuntimeException e) { |
| 143 | if (e.getStatus() != Status.UNIMPLEMENTED) { |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 144 | // UNIMPLEMENTED means that the server received our message but |
| 145 | // doesn't know how to handle it. Hence, channel is open. |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 146 | throw new IOException(e); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public boolean isChannelOpen(GrpcChannelId channelId) { |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 153 | checkNotNull(channelId); |
| 154 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 155 | Lock lock = channelLocks.get(channelId); |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 156 | lock.lock(); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 157 | |
| 158 | try { |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 159 | if (!channels.containsKey(channelId)) { |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 160 | log.warn("Can't check if channel open for unknown channel ID {}", |
| 161 | channelId); |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 162 | return false; |
| 163 | } |
| 164 | try { |
| 165 | doDummyMessage(channels.get(channelId)); |
| 166 | return true; |
| 167 | } catch (IOException e) { |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 168 | log.debug("Unable to send dummy message to {}: {}", |
| 169 | channelId, e.getCause().getMessage()); |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 170 | return false; |
| 171 | } |
| 172 | } finally { |
| 173 | lock.unlock(); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 177 | @Override |
| 178 | public void disconnectChannel(GrpcChannelId channelId) { |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 179 | checkNotNull(channelId); |
| 180 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 181 | Lock lock = channelLocks.get(channelId); |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 182 | lock.lock(); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 183 | |
| 184 | try { |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 185 | if (!channels.containsKey(channelId)) { |
| 186 | // Nothing to do. |
| 187 | return; |
| 188 | } |
| 189 | ManagedChannel channel = channels.get(channelId); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 190 | |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 191 | try { |
| 192 | channel.shutdown().awaitTermination(5, TimeUnit.SECONDS); |
| 193 | } catch (InterruptedException e) { |
| 194 | log.warn("Channel {} didn't shut down in time."); |
| 195 | channel.shutdownNow(); |
Ray Milkey | 5c7d488 | 2018-02-05 14:50:39 -0800 | [diff] [blame] | 196 | Thread.currentThread().interrupt(); |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | channels.remove(channelId); |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 200 | } finally { |
| 201 | lock.unlock(); |
| 202 | } |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public Map<GrpcChannelId, ManagedChannel> getChannels() { |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 207 | return ImmutableMap.copyOf(channels); |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public Collection<ManagedChannel> getChannels(final DeviceId deviceId) { |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 212 | checkNotNull(deviceId); |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 213 | final Set<ManagedChannel> deviceChannels = new HashSet<>(); |
| 214 | channels.forEach((k, v) -> { |
| 215 | if (k.deviceId().equals(deviceId)) { |
| 216 | deviceChannels.add(v); |
| 217 | } |
| 218 | }); |
| 219 | |
| 220 | return ImmutableSet.copyOf(deviceChannels); |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public Optional<ManagedChannel> getChannel(GrpcChannelId channelId) { |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 225 | checkNotNull(channelId); |
| 226 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 227 | Lock lock = channelLocks.get(channelId); |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame] | 228 | lock.lock(); |
| 229 | |
| 230 | try { |
| 231 | return Optional.ofNullable(channels.get(channelId)); |
| 232 | } finally { |
| 233 | lock.unlock(); |
| 234 | } |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 235 | } |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 236 | |
| 237 | /** |
| 238 | * gRPC client interceptor that logs all messages sent and received. |
| 239 | */ |
| 240 | private final class InternalLogChannelInterceptor implements ClientInterceptor { |
| 241 | |
| 242 | private final GrpcChannelId channelId; |
| 243 | |
| 244 | private InternalLogChannelInterceptor(GrpcChannelId channelId) { |
| 245 | this.channelId = channelId; |
| 246 | } |
| 247 | |
| 248 | @Override |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 249 | public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall( |
| 250 | MethodDescriptor<ReqT, RespT> methodDescriptor, |
| 251 | CallOptions callOptions, Channel channel) { |
| 252 | return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>( |
| 253 | channel.newCall(methodDescriptor, callOptions.withoutWaitForReady())) { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 254 | |
| 255 | @Override |
| 256 | public void sendMessage(ReqT message) { |
Andrea Campanella | a74bdba | 2018-05-15 16:45:00 +0200 | [diff] [blame] | 257 | if (enableMessageLog && !methodDescriptor.getFullMethodName() |
| 258 | .startsWith(SET_FORWARDING_PIPELINE_CONFIG_METHOD)) { |
| 259 | log.info("*** SENDING GRPC MESSAGE [{}]\n{}:\n{}", |
| 260 | channelId, methodDescriptor.getFullMethodName(), |
| 261 | message.toString()); |
| 262 | } |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 263 | super.sendMessage(message); |
| 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public void start(Listener<RespT> responseListener, Metadata headers) { |
| 268 | |
| 269 | ClientCall.Listener<RespT> listener = new ForwardingClientCallListener<RespT>() { |
| 270 | @Override |
| 271 | protected Listener<RespT> delegate() { |
| 272 | return responseListener; |
| 273 | } |
| 274 | |
| 275 | @Override |
| 276 | public void onMessage(RespT message) { |
Andrea Campanella | a74bdba | 2018-05-15 16:45:00 +0200 | [diff] [blame] | 277 | if (enableMessageLog) { |
| 278 | log.info("*** RECEIVED GRPC MESSAGE [{}]\n{}:\n{}", |
| 279 | channelId, methodDescriptor.getFullMethodName(), |
| 280 | message.toString()); |
| 281 | } |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 282 | super.onMessage(message); |
| 283 | } |
| 284 | }; |
| 285 | super.start(listener, headers); |
| 286 | } |
| 287 | }; |
| 288 | } |
| 289 | } |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 290 | } |