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_NODEID;
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.concurrent.ScheduledExecutorService;
24 import java.util.concurrent.TimeUnit;
25 import javax.xml.transform.dom.DOMSource;
26 import org.checkerframework.checker.lock.qual.GuardedBy;
27 import org.eclipse.jdt.annotation.NonNull;
28 import org.opendaylight.mdsal.dom.api.DOMNotification;
29 import org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener;
30 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
31 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
32 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceId;
33 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices;
34 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices.Rpcs;
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.yangtools.concepts.ListenerRegistration;
40 import org.opendaylight.yangtools.yang.common.QName;
41 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * SalFacade proxy that invokes keepalive RPCs to prevent session shutdown from remote device
47 * and to detect incorrect session drops (netconf session is inactive, but TCP/SSH connection is still present).
48 * The keepalive RPC is a get-config with empty filter.
50 public final class KeepaliveSalFacade implements RemoteDeviceHandler {
51 private static final Logger LOG = LoggerFactory.getLogger(KeepaliveSalFacade.class);
53 // 2 minutes keepalive delay by default
54 private static final long DEFAULT_DELAY = TimeUnit.MINUTES.toSeconds(2);
56 // 1 minute transaction timeout by default
57 private static final long DEFAULT_TRANSACTION_TIMEOUT_MILLI = TimeUnit.MILLISECONDS.toMillis(60000);
59 private final RemoteDeviceHandler salFacade;
60 private final ScheduledExecutorService executor;
62 private final long keepaliveDelaySeconds;
63 private final long timeoutNanos;
64 private final long delayNanos;
66 private final RemoteDeviceId id;
68 private volatile NetconfDeviceCommunicator listener;
69 private volatile KeepaliveTask task;
71 public KeepaliveSalFacade(final RemoteDeviceId id, final RemoteDeviceHandler salFacade,
72 final ScheduledExecutorService executor, final long keepaliveDelaySeconds,
73 final long requestTimeoutMillis) {
75 this.salFacade = salFacade;
76 this.executor = requireNonNull(executor);
77 this.keepaliveDelaySeconds = keepaliveDelaySeconds;
78 delayNanos = TimeUnit.SECONDS.toNanos(keepaliveDelaySeconds);
79 timeoutNanos = TimeUnit.MILLISECONDS.toNanos(requestTimeoutMillis);
82 public KeepaliveSalFacade(final RemoteDeviceId id, final RemoteDeviceHandler salFacade,
83 final ScheduledExecutorService executor) {
84 this(id, salFacade, executor, DEFAULT_DELAY, DEFAULT_TRANSACTION_TIMEOUT_MILLI);
88 * Set the netconf session listener whenever ready.
90 * @param listener netconf session listener
92 public void setListener(final NetconfDeviceCommunicator listener) {
93 this.listener = listener;
97 * Cancel current keepalive and free it.
99 private synchronized void stopKeepalives() {
100 final var localTask = task;
101 if (localTask != null) {
102 localTask.disableKeepalive();
107 private void disableKeepalive() {
108 final var localTask = task;
109 if (localTask != null) {
110 localTask.disableKeepalive();
114 private void enableKeepalive() {
115 final var localTask = task;
116 if (localTask != null) {
117 localTask.enableKeepalive();
122 checkState(listener != null, "%s: Unable to reconnect, session listener is missing", id);
124 LOG.info("{}: Reconnecting inactive netconf session", id);
125 listener.disconnect();
129 public void onDeviceConnected(final NetconfDeviceSchema deviceSchema,
130 final NetconfSessionPreferences sessionPreferences, final RemoteDeviceServices services) {
131 final var devRpc = services.rpcs();
132 task = new KeepaliveTask(devRpc);
134 final Rpcs keepaliveRpcs;
135 if (devRpc instanceof Rpcs.Normalized normalized) {
136 keepaliveRpcs = new NormalizedKeepaliveRpcs(normalized);
137 } else if (devRpc instanceof Rpcs.Schemaless schemaless) {
138 keepaliveRpcs = new SchemalessKeepaliveRpcs(schemaless);
140 throw new IllegalStateException("Unhandled " + devRpc);
143 salFacade.onDeviceConnected(deviceSchema, sessionPreferences, new RemoteDeviceServices(keepaliveRpcs,
144 // FIXME: wrap with keepalive
145 services.actions()));
147 // We have performed a callback, which might have termined keepalives
148 final var localTask = task;
149 if (localTask != null) {
150 LOG.debug("{}: Netconf session initiated, starting keepalives", id);
151 LOG.trace("{}: Scheduling keepalives every {}s", id, keepaliveDelaySeconds);
152 localTask.enableKeepalive();
157 public void onDeviceDisconnected() {
159 salFacade.onDeviceDisconnected();
163 public void onDeviceFailed(final Throwable throwable) {
165 salFacade.onDeviceFailed(throwable);
169 public void onNotification(final DOMNotification domNotification) {
170 final var localTask = task;
171 if (localTask != null) {
172 localTask.recordActivity();
174 salFacade.onNotification(domNotification);
178 public void close() {
183 private <T> @NonNull ListenableFuture<T> scheduleTimeout(final ListenableFuture<T> invokeFuture) {
184 final var timeout = new RequestTimeoutTask<>(invokeFuture);
185 final var timeoutFuture = executor.schedule(timeout, timeoutNanos, TimeUnit.NANOSECONDS);
186 invokeFuture.addListener(() -> timeoutFuture.cancel(false), MoreExecutors.directExecutor());
187 return timeout.userFuture;
191 * Invoke keepalive RPC and check the response. In case of any received response the keepalive
192 * is considered successful and schedules next keepalive with a fixed delay. If the response is unsuccessful (no
193 * response received, or the rcp could not even be sent) immediate reconnect is triggered as netconf session
194 * is considered inactive/failed.
196 private final class KeepaliveTask implements Runnable, FutureCallback<DOMRpcResult> {
197 // Keepalive RPC static resources
198 static final @NonNull ContainerNode KEEPALIVE_PAYLOAD = NetconfMessageTransformUtil.wrap(
199 NETCONF_GET_CONFIG_NODEID, getSourceNode(NETCONF_RUNNING_NODEID), NetconfMessageTransformUtil.EMPTY_FILTER);
201 private final Rpcs devRpc;
204 private boolean suppressed = false;
206 private volatile long lastActivity;
208 KeepaliveTask(final Rpcs devRpc) {
209 this.devRpc = requireNonNull(devRpc);
214 final long local = lastActivity;
215 final long now = System.nanoTime();
216 final long inFutureNanos = local + delayNanos - now;
217 if (inFutureNanos > 0) {
218 reschedule(inFutureNanos);
224 void recordActivity() {
225 lastActivity = System.nanoTime();
228 synchronized void disableKeepalive() {
229 // unsuppressed -> suppressed
233 synchronized void enableKeepalive() {
236 // unscheduled -> unsuppressed
239 // suppressed -> unsuppressed
244 private synchronized void sendKeepalive(final long now) {
246 LOG.debug("{}: Skipping keepalive while disabled", id);
247 // suppressed -> unscheduled
252 LOG.trace("{}: Invoking keepalive RPC", id);
253 final var deviceFuture = devRpc.invokeNetconf(NETCONF_GET_CONFIG_QNAME, KEEPALIVE_PAYLOAD);
255 Futures.addCallback(deviceFuture, this, MoreExecutors.directExecutor());
258 // FIXME: re-examine this suppression
259 @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE",
260 justification = "Unrecognised NullableDecl")
262 public void onSuccess(final DOMRpcResult result) {
263 // No matter what response we got, rpc-reply or rpc-error,
264 // we got it from device so the netconf session is OK
265 if (result == null) {
266 LOG.warn("{} Keepalive RPC returned null with response. Reconnecting netconf session", id);
271 if (result.value() != null) {
274 final var errors = result.errors();
275 if (!errors.isEmpty()) {
276 LOG.warn("{}: Keepalive RPC failed with error: {}", id, errors);
279 LOG.warn("{} Keepalive RPC returned null with response. Reconnecting netconf session", id);
286 public void onFailure(final Throwable throwable) {
287 LOG.warn("{}: Keepalive RPC failed. Reconnecting netconf session.", id, throwable);
291 private void reschedule() {
292 reschedule(delayNanos);
295 private void reschedule(final long delay) {
296 executor.schedule(this, delay, TimeUnit.NANOSECONDS);
301 * Request timeout task is called once the requestTimeoutMillis is reached. At that moment, if the request is not
302 * yet finished, we cancel it.
304 private final class RequestTimeoutTask<V> implements FutureCallback<V>, Runnable {
305 private final @NonNull SettableFuture<V> userFuture = SettableFuture.create();
306 private final @NonNull ListenableFuture<? extends V> rpcResultFuture;
308 RequestTimeoutTask(final ListenableFuture<V> rpcResultFuture) {
309 this.rpcResultFuture = requireNonNull(rpcResultFuture);
310 Futures.addCallback(rpcResultFuture, this, MoreExecutors.directExecutor());
315 rpcResultFuture.cancel(true);
316 userFuture.cancel(false);
321 public void onSuccess(final V result) {
322 // No matter what response we got,
323 // rpc-reply or rpc-error, we got it from device so the netconf session is OK.
324 userFuture.set(result);
329 public void onFailure(final Throwable throwable) {
330 // User/Application RPC failed (The RPC did not reach the remote device or ...)
331 // FIXME: what other reasons could cause this ?)
332 LOG.warn("{}: Rpc failure detected. Reconnecting netconf session", id, throwable);
333 userFuture.setException(throwable);
334 // There is no point in keeping this session. Reconnect.
340 * Proxy for {@link Rpcs} which attaches a reset-keepalive-task and schedule request-timeout-task to each RPC
341 * invocation. Version for {@link Rpcs.Normalized}.
343 private final class NormalizedKeepaliveRpcs implements Rpcs.Normalized {
344 private final Rpcs.Normalized delegate;
346 NormalizedKeepaliveRpcs(final Rpcs.Normalized delegate) {
347 this.delegate = requireNonNull(delegate);
351 public ListenableFuture<? extends DOMRpcResult> invokeRpc(final QName type, final ContainerNode input) {
352 // FIXME: what happens if we disable keepalive and then invokeRpc() throws?
354 return scheduleTimeout(delegate.invokeRpc(type, input));
358 public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(
359 final T rpcListener) {
360 // There is no real communication with the device (yet), hence no recordActivity() or anything
361 return delegate.registerRpcListener(rpcListener);
366 * Proxy for {@link Rpcs} which attaches a reset-keepalive-task and schedule request-timeout-task to each RPC
367 * invocation. Version for {@link Rpcs.Schemaless}.
369 private final class SchemalessKeepaliveRpcs implements Rpcs.Schemaless {
370 private final Rpcs.Schemaless delegate;
372 SchemalessKeepaliveRpcs(final Rpcs.Schemaless delegate) {
373 this.delegate = requireNonNull(delegate);
377 public ListenableFuture<? extends DOMRpcResult> invokeNetconf(final QName type, final ContainerNode input) {
378 // FIXME: what happens if we disable keepalive and then invokeRpc() throws?
380 return scheduleTimeout(delegate.invokeNetconf(type, input));
384 public ListenableFuture<? extends DOMSource> invokeRpc(final QName type, final DOMSource input) {
385 // FIXME: what happens if we disable keepalive and then invokeRpc() throws?
387 return scheduleTimeout(delegate.invokeRpc(type, input));