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; |
| 37 | import org.apache.felix.scr.annotations.Service; |
| 38 | import org.onosproject.grpc.api.GrpcChannelId; |
| 39 | import org.onosproject.grpc.api.GrpcController; |
| 40 | import org.onosproject.grpc.api.GrpcObserverHandler; |
| 41 | import org.onosproject.grpc.api.GrpcStreamObserverId; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 42 | import org.onosproject.grpc.ctl.dummy.Dummy; |
| 43 | import org.onosproject.grpc.ctl.dummy.DummyServiceGrpc; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 44 | import org.onosproject.net.DeviceId; |
| 45 | import org.slf4j.Logger; |
| 46 | import org.slf4j.LoggerFactory; |
| 47 | |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 48 | import java.io.IOException; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 49 | import java.util.Collection; |
| 50 | import java.util.HashSet; |
| 51 | import java.util.Map; |
| 52 | import java.util.Optional; |
| 53 | import java.util.Set; |
| 54 | import java.util.concurrent.ConcurrentHashMap; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 55 | import java.util.concurrent.TimeUnit; |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame^] | 56 | import java.util.concurrent.locks.Lock; |
| 57 | import java.util.concurrent.locks.ReentrantLock; |
| 58 | |
| 59 | import static com.google.common.base.Preconditions.checkNotNull; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Default implementation of the GrpcController. |
| 63 | */ |
| 64 | @Component(immediate = true) |
| 65 | @Service |
| 66 | public class GrpcControllerImpl implements GrpcController { |
| 67 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 68 | // Hint: set to true to log all gRPC messages received/sent on all channels. |
| 69 | // TODO: make configurable at runtime |
| 70 | public static boolean enableMessageLog = false; |
| 71 | |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 72 | private static final int CONNECTION_TIMEOUT_SECONDS = 20; |
| 73 | |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 74 | public static final Logger log = LoggerFactory |
| 75 | .getLogger(GrpcControllerImpl.class); |
| 76 | |
| 77 | private Map<GrpcStreamObserverId, GrpcObserverHandler> observers; |
| 78 | private Map<GrpcChannelId, ManagedChannel> channels; |
| 79 | private Map<GrpcChannelId, ManagedChannelBuilder<?>> channelBuilders; |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame^] | 80 | private final Map<GrpcChannelId, Lock> channelLocks = Maps.newConcurrentMap(); |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 81 | |
| 82 | @Activate |
| 83 | public void activate() { |
| 84 | observers = new ConcurrentHashMap<>(); |
| 85 | channels = new ConcurrentHashMap<>(); |
| 86 | channelBuilders = new ConcurrentHashMap<>(); |
| 87 | log.info("Started"); |
| 88 | } |
| 89 | |
| 90 | @Deactivate |
| 91 | public void deactivate() { |
| 92 | channels.values().forEach(ManagedChannel::shutdown); |
| 93 | observers.clear(); |
| 94 | channels.clear(); |
| 95 | channelBuilders.clear(); |
| 96 | log.info("Stopped"); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public void addObserver(GrpcStreamObserverId observerId, GrpcObserverHandler grpcObserverHandler) { |
| 101 | grpcObserverHandler.bindObserver(channels.get(observerId.serviceId().channelId())); |
| 102 | observers.put(observerId, grpcObserverHandler); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void removeObserver(GrpcStreamObserverId observerId) { |
| 107 | observers.get(observerId).removeObserver(); |
| 108 | observers.remove(observerId); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public Optional<GrpcObserverHandler> getObserverManager(GrpcStreamObserverId observerId) { |
| 113 | return Optional.ofNullable(observers.get(observerId)); |
| 114 | } |
| 115 | |
| 116 | @Override |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 117 | public ManagedChannel connectChannel(GrpcChannelId channelId, ManagedChannelBuilder<?> channelBuilder) |
| 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 | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame^] | 122 | Lock lock = channelLocks.computeIfAbsent(channelId, k -> new ReentrantLock()); |
| 123 | lock.lock(); |
| 124 | |
| 125 | try { |
| 126 | if (enableMessageLog) { |
| 127 | channelBuilder.intercept(new InternalLogChannelInterceptor(channelId)); |
| 128 | } |
| 129 | ManagedChannel channel = channelBuilder.build(); |
| 130 | // Forced connection not yet implemented. Use workaround... |
| 131 | // channel.getState(true); |
| 132 | doDummyMessage(channel); |
| 133 | channelBuilders.put(channelId, channelBuilder); |
| 134 | channels.put(channelId, channel); |
| 135 | return channel; |
| 136 | } finally { |
| 137 | lock.unlock(); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 138 | } |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 139 | } |
| 140 | |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 141 | private void doDummyMessage(ManagedChannel channel) throws IOException { |
| 142 | DummyServiceGrpc.DummyServiceBlockingStub dummyStub = DummyServiceGrpc.newBlockingStub(channel) |
| 143 | .withDeadlineAfter(CONNECTION_TIMEOUT_SECONDS, TimeUnit.SECONDS); |
| 144 | try { |
| 145 | dummyStub.sayHello(Dummy.DummyMessageThatNoOneWouldReallyUse.getDefaultInstance()); |
| 146 | } catch (StatusRuntimeException e) { |
| 147 | if (e.getStatus() != Status.UNIMPLEMENTED) { |
| 148 | // UNIMPLEMENTED means that server received our message but doesn't know how to handle it. |
| 149 | // Hence, channel is open. |
| 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)) { |
| 164 | log.warn("Can't check if channel open for unknown channel id {}", channelId); |
| 165 | return false; |
| 166 | } |
| 167 | try { |
| 168 | doDummyMessage(channels.get(channelId)); |
| 169 | return true; |
| 170 | } catch (IOException e) { |
| 171 | return false; |
| 172 | } |
| 173 | } finally { |
| 174 | lock.unlock(); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 178 | @Override |
| 179 | public void disconnectChannel(GrpcChannelId channelId) { |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame^] | 180 | checkNotNull(channelId); |
| 181 | |
| 182 | Lock lock = channelLocks.computeIfAbsent(channelId, k -> new ReentrantLock()); |
| 183 | lock.lock(); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 184 | |
| 185 | try { |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame^] | 186 | if (!channels.containsKey(channelId)) { |
| 187 | // Nothing to do. |
| 188 | return; |
| 189 | } |
| 190 | ManagedChannel channel = channels.get(channelId); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 191 | |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame^] | 192 | try { |
| 193 | channel.shutdown().awaitTermination(5, TimeUnit.SECONDS); |
| 194 | } catch (InterruptedException e) { |
| 195 | log.warn("Channel {} didn't shut down in time."); |
| 196 | channel.shutdownNow(); |
| 197 | } |
| 198 | |
| 199 | channels.remove(channelId); |
| 200 | channelBuilders.remove(channelId); |
| 201 | } finally { |
| 202 | lock.unlock(); |
| 203 | } |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public Map<GrpcChannelId, ManagedChannel> getChannels() { |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame^] | 208 | return ImmutableMap.copyOf(channels); |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public Collection<ManagedChannel> getChannels(final DeviceId deviceId) { |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame^] | 213 | checkNotNull(deviceId); |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 214 | final Set<ManagedChannel> deviceChannels = new HashSet<>(); |
| 215 | channels.forEach((k, v) -> { |
| 216 | if (k.deviceId().equals(deviceId)) { |
| 217 | deviceChannels.add(v); |
| 218 | } |
| 219 | }); |
| 220 | |
| 221 | return ImmutableSet.copyOf(deviceChannels); |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public Optional<ManagedChannel> getChannel(GrpcChannelId channelId) { |
Carmelo Cascone | fb92407 | 2017-08-29 20:21:55 +0200 | [diff] [blame^] | 226 | checkNotNull(channelId); |
| 227 | |
| 228 | Lock lock = channelLocks.computeIfAbsent(channelId, k -> new ReentrantLock()); |
| 229 | lock.lock(); |
| 230 | |
| 231 | try { |
| 232 | return Optional.ofNullable(channels.get(channelId)); |
| 233 | } finally { |
| 234 | lock.unlock(); |
| 235 | } |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 236 | } |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 237 | |
| 238 | /** |
| 239 | * gRPC client interceptor that logs all messages sent and received. |
| 240 | */ |
| 241 | private final class InternalLogChannelInterceptor implements ClientInterceptor { |
| 242 | |
| 243 | private final GrpcChannelId channelId; |
| 244 | |
| 245 | private InternalLogChannelInterceptor(GrpcChannelId channelId) { |
| 246 | this.channelId = channelId; |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> methodDescriptor, |
| 251 | CallOptions callOptions, Channel channel) { |
| 252 | return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(channel.newCall( |
| 253 | methodDescriptor, callOptions.withoutWaitForReady())) { |
| 254 | |
| 255 | @Override |
| 256 | public void sendMessage(ReqT message) { |
| 257 | log.info("*** SENDING GRPC MESSAGE [{}]\n{}:\n{}", channelId, methodDescriptor.getFullMethodName(), |
| 258 | message.toString()); |
| 259 | super.sendMessage(message); |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public void start(Listener<RespT> responseListener, Metadata headers) { |
| 264 | |
| 265 | ClientCall.Listener<RespT> listener = new ForwardingClientCallListener<RespT>() { |
| 266 | @Override |
| 267 | protected Listener<RespT> delegate() { |
| 268 | return responseListener; |
| 269 | } |
| 270 | |
| 271 | @Override |
| 272 | public void onMessage(RespT message) { |
| 273 | log.info("*** RECEIVED GRPC MESSAGE [{}]\n{}:\n{}", channelId, |
| 274 | methodDescriptor.getFullMethodName(), |
| 275 | message.toString()); |
| 276 | super.onMessage(message); |
| 277 | } |
| 278 | }; |
| 279 | super.start(listener, headers); |
| 280 | } |
| 281 | }; |
| 282 | } |
| 283 | } |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 284 | } |