2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netconf.sal.connect.netconf.sal;
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps.getSourceNode;
13 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_NODEID;
14 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME;
15 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_RUNNING_QNAME;
17 import com.google.common.util.concurrent.FutureCallback;
18 import com.google.common.util.concurrent.Futures;
19 import com.google.common.util.concurrent.ListenableFuture;
20 import com.google.common.util.concurrent.MoreExecutors;
21 import com.google.common.util.concurrent.SettableFuture;
22 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
23 import java.util.Collection;
24 import java.util.concurrent.ScheduledExecutorService;
25 import java.util.concurrent.ScheduledFuture;
26 import java.util.concurrent.TimeUnit;
27 import org.checkerframework.checker.lock.qual.GuardedBy;
28 import org.eclipse.jdt.annotation.NonNull;
29 import org.opendaylight.mdsal.dom.api.DOMActionService;
30 import org.opendaylight.mdsal.dom.api.DOMNotification;
31 import org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener;
32 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
33 import org.opendaylight.mdsal.dom.api.DOMRpcService;
34 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
35 import org.opendaylight.netconf.sal.connect.netconf.NetconfDeviceSchema;
36 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
37 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
38 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
39 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
40 import org.opendaylight.yangtools.concepts.ListenerRegistration;
41 import org.opendaylight.yangtools.yang.common.QName;
42 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * SalFacade proxy that invokes keepalive RPCs to prevent session shutdown from remote device
49 * and to detect incorrect session drops (netconf session is inactive, but TCP/SSH connection is still present).
50 * The keepalive RPC is a get-config with empty filter.
52 public final class KeepaliveSalFacade implements RemoteDeviceHandler {
53 private static final Logger LOG = LoggerFactory.getLogger(KeepaliveSalFacade.class);
55 // 2 minutes keepalive delay by default
56 private static final long DEFAULT_DELAY = TimeUnit.MINUTES.toSeconds(2);
58 // 1 minute transaction timeout by default
59 private static final long DEFAULT_TRANSACTION_TIMEOUT_MILLI = TimeUnit.MILLISECONDS.toMillis(60000);
61 private final KeepaliveTask keepaliveTask = new KeepaliveTask();
62 private final RemoteDeviceHandler salFacade;
63 private final ScheduledExecutorService executor;
65 private final long keepaliveDelaySeconds;
66 private final long timeoutNanos;
67 private final long delayNanos;
69 private final RemoteDeviceId id;
71 private volatile NetconfDeviceCommunicator listener;
72 private volatile DOMRpcService currentDeviceRpc;
74 public KeepaliveSalFacade(final RemoteDeviceId id, final RemoteDeviceHandler salFacade,
75 final ScheduledExecutorService executor, final long keepaliveDelaySeconds,
76 final long requestTimeoutMillis) {
78 this.salFacade = salFacade;
79 this.executor = requireNonNull(executor);
80 this.keepaliveDelaySeconds = keepaliveDelaySeconds;
81 delayNanos = TimeUnit.SECONDS.toNanos(keepaliveDelaySeconds);
82 timeoutNanos = TimeUnit.MILLISECONDS.toNanos(requestTimeoutMillis);
85 public KeepaliveSalFacade(final RemoteDeviceId id, final RemoteDeviceHandler salFacade,
86 final ScheduledExecutorService executor) {
87 this(id, salFacade, executor, DEFAULT_DELAY, DEFAULT_TRANSACTION_TIMEOUT_MILLI);
91 * Set the netconf session listener whenever ready.
93 * @param listener netconf session listener
95 public void setListener(final NetconfDeviceCommunicator listener) {
96 this.listener = listener;
100 * Cancel current keepalive and also reset current deviceRpc.
102 private synchronized void stopKeepalives() {
103 keepaliveTask.disableKeepalive();
104 currentDeviceRpc = null;
108 checkState(listener != null, "%s: Unable to reconnect, session listener is missing", id);
110 LOG.info("{}: Reconnecting inactive netconf session", id);
111 listener.disconnect();
115 public void onDeviceConnected(final NetconfDeviceSchema deviceSchema,
116 final NetconfSessionPreferences netconfSessionPreferences, final DOMRpcService deviceRpc) {
117 onDeviceConnected(deviceSchema, netconfSessionPreferences, deviceRpc, null);
121 public void onDeviceConnected(final NetconfDeviceSchema deviceSchema,
122 final NetconfSessionPreferences netconfSessionPreferences, final DOMRpcService deviceRpc,
123 final DOMActionService deviceAction) {
124 currentDeviceRpc = requireNonNull(deviceRpc);
125 salFacade.onDeviceConnected(deviceSchema, netconfSessionPreferences,
126 new KeepaliveDOMRpcService(deviceRpc), deviceAction);
128 LOG.debug("{}: Netconf session initiated, starting keepalives", id);
129 LOG.trace("{}: Scheduling keepalives every {}s", id, keepaliveDelaySeconds);
130 keepaliveTask.enableKeepalive();
134 public void onDeviceDisconnected() {
136 salFacade.onDeviceDisconnected();
140 public void onDeviceFailed(final Throwable throwable) {
142 salFacade.onDeviceFailed(throwable);
146 public void onNotification(final DOMNotification domNotification) {
147 keepaliveTask.recordActivity();
148 salFacade.onNotification(domNotification);
152 public void close() {
157 // Keepalive RPC static resources
158 private static final @NonNull ContainerNode KEEPALIVE_PAYLOAD =
159 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_NODEID,
160 getSourceNode(NETCONF_RUNNING_QNAME), NetconfMessageTransformUtil.EMPTY_FILTER);
163 * Invoke keepalive RPC and check the response. In case of any received response the keepalive
164 * is considered successful and schedules next keepalive with a fixed delay. If the response is unsuccessful (no
165 * response received, or the rcp could not even be sent) immediate reconnect is triggered as netconf session
166 * is considered inactive/failed.
168 private final class KeepaliveTask implements Runnable, FutureCallback<DOMRpcResult> {
169 private volatile long lastActivity;
171 private boolean suppressed;
179 final long local = lastActivity;
180 final long now = System.nanoTime();
181 final long inFutureNanos = local + delayNanos - now;
182 if (inFutureNanos > 0) {
183 reschedule(inFutureNanos);
189 void recordActivity() {
190 lastActivity = System.nanoTime();
193 synchronized void disableKeepalive() {
194 // unsuppressed -> suppressed
198 synchronized void enableKeepalive() {
201 // unscheduled -> unsuppressed
204 // suppressed -> unsuppressed
209 private synchronized void sendKeepalive(final long now) {
211 // suppressed -> unscheduled
216 final DOMRpcService deviceRpc = currentDeviceRpc;
217 if (deviceRpc == null) {
218 // deviceRpc is null, which means we hit the reconnect window and attempted to send keepalive while
219 // we were reconnecting. Next keepalive will be scheduled after reconnect so no action necessary here.
220 LOG.debug("{}: Skipping keepalive while reconnecting", id);
224 LOG.trace("{}: Invoking keepalive RPC", id);
225 final ListenableFuture<? extends DOMRpcResult> deviceFuture =
226 currentDeviceRpc.invokeRpc(NETCONF_GET_CONFIG_QNAME, KEEPALIVE_PAYLOAD);
229 Futures.addCallback(deviceFuture, this, MoreExecutors.directExecutor());
232 @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE",
233 justification = "Unrecognised NullableDecl")
235 public void onSuccess(final DOMRpcResult result) {
236 // No matter what response we got, rpc-reply or rpc-error,
237 // we got it from device so the netconf session is OK
238 if (result == null) {
239 LOG.warn("{} Keepalive RPC returned null with response. Reconnecting netconf session", id);
244 if (result.getResult() != null) {
247 final Collection<?> errors = result.getErrors();
248 if (!errors.isEmpty()) {
249 LOG.warn("{}: Keepalive RPC failed with error: {}", id, errors);
252 LOG.warn("{} Keepalive RPC returned null with response. Reconnecting netconf session", id);
259 public void onFailure(final Throwable throwable) {
260 LOG.warn("{}: Keepalive RPC failed. Reconnecting netconf session.", id, throwable);
264 private void reschedule() {
265 reschedule(delayNanos);
268 private void reschedule(final long delay) {
269 executor.schedule(this, delay, TimeUnit.NANOSECONDS);
274 * Request timeout task is called once the requestTimeoutMillis is reached. At that moment, if the request is not
275 * yet finished, we cancel it.
277 private final class RequestTimeoutTask implements FutureCallback<DOMRpcResult>, Runnable {
278 private final @NonNull SettableFuture<DOMRpcResult> userFuture = SettableFuture.create();
279 private final @NonNull ListenableFuture<? extends DOMRpcResult> deviceFuture;
281 RequestTimeoutTask(final ListenableFuture<? extends DOMRpcResult> rpcResultFuture) {
282 deviceFuture = requireNonNull(rpcResultFuture);
283 Futures.addCallback(deviceFuture, this, MoreExecutors.directExecutor());
288 deviceFuture.cancel(true);
289 userFuture.cancel(false);
290 keepaliveTask.enableKeepalive();
294 public void onSuccess(final DOMRpcResult result) {
295 // No matter what response we got,
296 // rpc-reply or rpc-error, we got it from device so the netconf session is OK.
297 userFuture.set(result);
298 keepaliveTask.enableKeepalive();
302 public void onFailure(final Throwable throwable) {
303 // User/Application RPC failed (The RPC did not reach the remote device or ...)
304 // FIXME: what other reasons could cause this ?)
305 LOG.warn("{}: Rpc failure detected. Reconnecting netconf session", id, throwable);
306 userFuture.setException(throwable);
307 // There is no point in keeping this session. Reconnect.
313 * DOMRpcService proxy that attaches reset-keepalive-task and schedule
314 * request-timeout-task to each RPC invocation.
316 public final class KeepaliveDOMRpcService implements DOMRpcService {
317 private final @NonNull DOMRpcService deviceRpc;
319 KeepaliveDOMRpcService(final DOMRpcService deviceRpc) {
320 this.deviceRpc = requireNonNull(deviceRpc);
323 public @NonNull DOMRpcService getDeviceRpc() {
328 public ListenableFuture<? extends DOMRpcResult> invokeRpc(final QName type, final NormalizedNode input) {
329 keepaliveTask.disableKeepalive();
330 final ListenableFuture<? extends DOMRpcResult> deviceFuture = deviceRpc.invokeRpc(type, input);
332 final RequestTimeoutTask timeout = new RequestTimeoutTask(deviceFuture);
333 final ScheduledFuture<?> timeoutFuture = executor.schedule(timeout, timeoutNanos, TimeUnit.NANOSECONDS);
334 deviceFuture.addListener(() -> timeoutFuture.cancel(false), MoreExecutors.directExecutor());
336 return timeout.userFuture;
340 public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T rpcListener) {
341 // There is no real communication with the device (yet), hence recordActivity() or anything
342 return deviceRpc.registerRpcListener(rpcListener);