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