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