Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [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 | * |
Carmelo Cascone | 3370c96 | 2019-02-07 18:24:19 -0800 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 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 | |
Carmelo Cascone | 3370c96 | 2019-02-07 18:24:19 -0800 | [diff] [blame] | 17 | package org.onosproject.gnmi.ctl; |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 18 | |
| 19 | |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 20 | import com.google.common.util.concurrent.Futures; |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 21 | import gnmi.Gnmi; |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 22 | import io.grpc.StatusRuntimeException; |
| 23 | import io.grpc.stub.ClientCallStreamObserver; |
| 24 | import io.grpc.stub.StreamObserver; |
| 25 | import org.onosproject.gnmi.api.GnmiEvent; |
| 26 | import org.onosproject.gnmi.api.GnmiUpdate; |
| 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.slf4j.Logger; |
| 29 | |
| 30 | import java.net.ConnectException; |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 31 | import java.util.concurrent.Future; |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 32 | import java.util.concurrent.ScheduledExecutorService; |
| 33 | import java.util.concurrent.TimeUnit; |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 34 | import java.util.concurrent.atomic.AtomicBoolean; |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 35 | |
| 36 | import static java.lang.String.format; |
| 37 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 38 | import static org.onlab.util.Tools.groupedThreads; |
| 39 | import static org.slf4j.LoggerFactory.getLogger; |
| 40 | |
| 41 | /** |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 42 | * A manager for the gNMI Subscribe RPC that opportunistically starts new RPC |
| 43 | * (e.g. when one fails because of errors) and posts subscribe events via the |
| 44 | * gNMI controller. |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 45 | */ |
| 46 | final class GnmiSubscriptionManager { |
| 47 | |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 48 | // FIXME: make this configurable |
| 49 | private static final long DEFAULT_RECONNECT_DELAY = 5; // Seconds |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 50 | |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 51 | private static final Logger log = getLogger(GnmiSubscriptionManager.class); |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 52 | |
| 53 | private final GnmiClientImpl client; |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 54 | private final DeviceId deviceId; |
| 55 | private final GnmiControllerImpl controller; |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 56 | private final StreamObserver<Gnmi.SubscribeResponse> responseObserver; |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 57 | |
| 58 | private final ScheduledExecutorService streamCheckerExecutor = |
| 59 | newSingleThreadScheduledExecutor(groupedThreads("onos/gnmi-subscribe-check", "%d", log)); |
| 60 | private Future<?> checkTask; |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 61 | |
| 62 | private ClientCallStreamObserver<Gnmi.SubscribeRequest> requestObserver; |
| 63 | private Gnmi.SubscribeRequest existingSubscription; |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 64 | private AtomicBoolean active = new AtomicBoolean(false); |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 65 | |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 66 | GnmiSubscriptionManager(GnmiClientImpl client, DeviceId deviceId, |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 67 | GnmiControllerImpl controller) { |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 68 | this.client = client; |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 69 | this.deviceId = deviceId; |
| 70 | this.controller = controller; |
| 71 | this.responseObserver = new InternalStreamResponseObserver(); |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void subscribe(Gnmi.SubscribeRequest request) { |
| 75 | synchronized (this) { |
| 76 | if (existingSubscription != null) { |
| 77 | if (existingSubscription.equals(request)) { |
| 78 | // Nothing to do. We are already subscribed for the same |
| 79 | // request. |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 80 | log.debug("Ignoring re-subscription to same request for {}", |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 81 | deviceId); |
| 82 | return; |
| 83 | } |
| 84 | log.debug("Cancelling existing subscription for {} before " + |
| 85 | "starting a new one", deviceId); |
| 86 | complete(); |
| 87 | } |
| 88 | existingSubscription = request; |
| 89 | sendSubscribeRequest(); |
| 90 | if (checkTask != null) { |
| 91 | checkTask = streamCheckerExecutor.scheduleAtFixedRate( |
| 92 | this::checkSubscription, 0, |
| 93 | DEFAULT_RECONNECT_DELAY, |
| 94 | TimeUnit.SECONDS); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void unsubscribe() { |
| 100 | synchronized (this) { |
| 101 | if (checkTask != null) { |
| 102 | checkTask.cancel(false); |
| 103 | checkTask = null; |
| 104 | } |
| 105 | existingSubscription = null; |
| 106 | complete(); |
| 107 | } |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | public void shutdown() { |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 111 | log.debug("Shutting down gNMI subscription manager for {}", deviceId); |
| 112 | unsubscribe(); |
| 113 | streamCheckerExecutor.shutdownNow(); |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 116 | private void checkSubscription() { |
| 117 | synchronized (this) { |
| 118 | if (existingSubscription != null && !active.get()) { |
| 119 | if (client.isServerReachable() || Futures.getUnchecked(client.probeService())) { |
| 120 | log.info("Re-starting Subscribe RPC for {}...", deviceId); |
| 121 | sendSubscribeRequest(); |
| 122 | } else { |
| 123 | log.debug("Not restarting Subscribe RPC for {}, server is NOT reachable", |
| 124 | deviceId); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | private void sendSubscribeRequest() { |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 131 | if (requestObserver == null) { |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 132 | log.debug("Starting new Subscribe RPC for {}...", deviceId); |
| 133 | client.execRpcNoTimeout( |
| 134 | s -> requestObserver = |
| 135 | (ClientCallStreamObserver<Gnmi.SubscribeRequest>) |
| 136 | s.subscribe(responseObserver) |
| 137 | ); |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 138 | } |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 139 | requestObserver.onNext(existingSubscription); |
| 140 | active.set(true); |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | public void complete() { |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 144 | synchronized (this) { |
| 145 | active.set(false); |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 146 | if (requestObserver != null) { |
| 147 | requestObserver.onCompleted(); |
| 148 | requestObserver.cancel("Terminated", null); |
| 149 | requestObserver = null; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 154 | /** |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 155 | * Handles messages received from the device on the Subscribe RPC. |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 156 | */ |
| 157 | private final class InternalStreamResponseObserver |
| 158 | implements StreamObserver<Gnmi.SubscribeResponse> { |
| 159 | |
| 160 | @Override |
| 161 | public void onNext(Gnmi.SubscribeResponse message) { |
| 162 | try { |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 163 | if (log.isTraceEnabled()) { |
| 164 | log.trace("Received SubscribeResponse from {}: {}", |
| 165 | deviceId, message.toString()); |
| 166 | } |
| 167 | controller.postEvent(new GnmiEvent(GnmiEvent.Type.UPDATE, new GnmiUpdate( |
| 168 | deviceId, message.getUpdate(), message.getSyncResponse()))); |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 169 | } catch (Throwable ex) { |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 170 | log.error("Exception processing SubscribeResponse from " + deviceId, |
| 171 | ex); |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public void onError(Throwable throwable) { |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 177 | complete(); |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 178 | if (throwable instanceof StatusRuntimeException) { |
| 179 | StatusRuntimeException sre = (StatusRuntimeException) throwable; |
| 180 | if (sre.getStatus().getCause() instanceof ConnectException) { |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 181 | log.warn("{} is unreachable ({})", |
| 182 | deviceId, sre.getCause().getMessage()); |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 183 | } else { |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 184 | log.warn("Error on Subscribe RPC for {}: {}", |
| 185 | deviceId, throwable.getMessage()); |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 186 | } |
| 187 | } else { |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 188 | log.error(format("Exception on Subscribe RPC for %s", |
| 189 | deviceId), throwable); |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 190 | } |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public void onCompleted() { |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 195 | complete(); |
| 196 | log.warn("Subscribe RPC for {} has completed", deviceId); |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 197 | } |
| 198 | } |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 199 | |
| 200 | @Override |
| 201 | protected void finalize() throws Throwable { |
| 202 | if (!streamCheckerExecutor.isShutdown()) { |
| 203 | log.error("Finalizing object but executor is still active! BUG? Shutting down..."); |
| 204 | shutdown(); |
| 205 | } |
| 206 | super.finalize(); |
| 207 | } |
Yi Tseng | a7f76c1 | 2018-12-14 14:19:18 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | |