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 org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps.getSourceNode;
12 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_NODEID;
13 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_PATH;
14 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_RUNNING_QNAME;
16 import com.google.common.util.concurrent.FutureCallback;
17 import com.google.common.util.concurrent.Futures;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import com.google.common.util.concurrent.MoreExecutors;
20 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
21 import java.util.concurrent.ScheduledExecutorService;
22 import java.util.concurrent.ScheduledFuture;
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.atomic.AtomicBoolean;
25 import org.opendaylight.mdsal.dom.api.DOMActionService;
26 import org.opendaylight.mdsal.dom.api.DOMNotification;
27 import org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener;
28 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
29 import org.opendaylight.mdsal.dom.api.DOMRpcService;
30 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
31 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
32 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
33 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
34 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
35 import org.opendaylight.yangtools.concepts.ListenerRegistration;
36 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
37 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
39 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * SalFacade proxy that invokes keepalive RPCs to prevent session shutdown from remote device
45 * and to detect incorrect session drops (netconf session is inactive, but TCP/SSH connection is still present).
46 * The keepalive RPC is a get-config with empty filter.
48 public final class KeepaliveSalFacade implements RemoteDeviceHandler<NetconfSessionPreferences> {
50 private static final Logger LOG = LoggerFactory.getLogger(KeepaliveSalFacade.class);
52 // 2 minutes keepalive delay by default
53 private static final long DEFAULT_DELAY = TimeUnit.MINUTES.toSeconds(2);
55 // 1 minute transaction timeout by default
56 private static final long DEFAULT_TRANSACTION_TIMEOUT_MILLI = TimeUnit.MILLISECONDS.toMillis(60000);
58 private final RemoteDeviceId id;
59 private final RemoteDeviceHandler<NetconfSessionPreferences> salFacade;
60 private final ScheduledExecutorService executor;
61 private final long keepaliveDelaySeconds;
62 private final ResetKeepalive resetKeepaliveTask;
63 private final long defaultRequestTimeoutMillis;
65 private volatile NetconfDeviceCommunicator listener;
66 private volatile ScheduledFuture<?> currentKeepalive;
67 private volatile DOMRpcService currentDeviceRpc;
68 private final AtomicBoolean lastKeepAliveSucceeded = new AtomicBoolean(false);
70 public KeepaliveSalFacade(final RemoteDeviceId id, final RemoteDeviceHandler<NetconfSessionPreferences> salFacade,
71 final ScheduledExecutorService executor, final long keepaliveDelaySeconds,
72 final long defaultRequestTimeoutMillis) {
74 this.salFacade = salFacade;
75 this.executor = executor;
76 this.keepaliveDelaySeconds = keepaliveDelaySeconds;
77 this.defaultRequestTimeoutMillis = defaultRequestTimeoutMillis;
78 this.resetKeepaliveTask = new ResetKeepalive();
81 public KeepaliveSalFacade(final RemoteDeviceId id, final RemoteDeviceHandler<NetconfSessionPreferences> salFacade,
82 final ScheduledExecutorService executor) {
83 this(id, salFacade, executor, DEFAULT_DELAY, DEFAULT_TRANSACTION_TIMEOUT_MILLI);
87 * Set the netconf session listener whenever ready.
89 * @param listener netconf session listener
91 public void setListener(final NetconfDeviceCommunicator listener) {
92 this.listener = listener;
96 * Just cancel current keepalive task.
97 * If its already started, let it finish ... not such a big deal.
100 * Then schedule next keepalive.
102 void resetKeepalive() {
103 LOG.trace("{}: Resetting netconf keepalive timer", id);
104 if (currentKeepalive != null) {
105 currentKeepalive.cancel(false);
107 scheduleKeepalives();
111 * Cancel current keepalive and also reset current deviceRpc.
113 private void stopKeepalives() {
114 if (currentKeepalive != null) {
115 currentKeepalive.cancel(false);
117 currentDeviceRpc = null;
121 checkState(listener != null, "%s: Unable to reconnect, session listener is missing", id);
123 LOG.info("{}: Reconnecting inactive netconf session", id);
124 listener.disconnect();
128 public void onDeviceConnected(final MountPointContext remoteSchemaContext,
129 final NetconfSessionPreferences netconfSessionPreferences, final DOMRpcService deviceRpc) {
130 onDeviceConnected(remoteSchemaContext, netconfSessionPreferences, deviceRpc, null);
134 public void onDeviceConnected(final MountPointContext remoteSchemaContext,
135 final NetconfSessionPreferences netconfSessionPreferences, final DOMRpcService deviceRpc,
136 final DOMActionService deviceAction) {
137 this.currentDeviceRpc = deviceRpc;
138 final DOMRpcService deviceRpc1 =
139 new KeepaliveDOMRpcService(deviceRpc, resetKeepaliveTask, defaultRequestTimeoutMillis, executor,
140 new ResponseWaitingScheduler());
142 salFacade.onDeviceConnected(remoteSchemaContext, netconfSessionPreferences, deviceRpc1, deviceAction);
144 LOG.debug("{}: Netconf session initiated, starting keepalives", id);
145 scheduleKeepalives();
148 private void scheduleKeepalives() {
149 lastKeepAliveSucceeded.set(true);
150 checkState(currentDeviceRpc != null);
151 LOG.trace("{}: Scheduling keepalives every {} {}", id, keepaliveDelaySeconds, TimeUnit.SECONDS);
152 currentKeepalive = executor.scheduleWithFixedDelay(new Keepalive(),
153 keepaliveDelaySeconds, keepaliveDelaySeconds, TimeUnit.SECONDS);
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) {
171 salFacade.onNotification(domNotification);
175 public void close() {
180 // Keepalive RPC static resources
181 private static final ContainerNode KEEPALIVE_PAYLOAD = NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_NODEID,
182 getSourceNode(NETCONF_RUNNING_QNAME), NetconfMessageTransformUtil.EMPTY_FILTER);
185 * Invoke keepalive RPC and check the response. In case of any received response the keepalive
186 * is considered successful and schedules next keepalive with a fixed delay. If the response is unsuccessful (no
187 * response received, or the rcp could not even be sent) immediate reconnect is triggered as netconf session
188 * is considered inactive/failed.
190 private class Keepalive implements Runnable, FutureCallback<DOMRpcResult> {
194 LOG.trace("{}: Invoking keepalive RPC", id);
197 final boolean lastJobSucceeded = lastKeepAliveSucceeded.getAndSet(false);
198 if (!lastJobSucceeded) {
199 onFailure(new IllegalStateException("Previous keepalive timed out"));
201 Futures.addCallback(currentDeviceRpc.invokeRpc(NETCONF_GET_CONFIG_PATH, KEEPALIVE_PAYLOAD), this,
202 MoreExecutors.directExecutor());
204 } catch (final NullPointerException e) {
205 LOG.debug("{}: Skipping keepalive while reconnecting", id);
206 // Empty catch block intentional
207 // Do nothing. The currentDeviceRpc was null and it means we hit the reconnect window and
208 // attempted to send keepalive while we were reconnecting. Next keepalive will be scheduled
209 // after reconnect so no action necessary here.
213 @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE",
214 justification = "Unrecognised NullableDecl")
216 public void onSuccess(final DOMRpcResult result) {
217 // No matter what response we got, rpc-reply or rpc-error,
218 // we got it from device so the netconf session is OK
219 if (result == null) {
220 LOG.warn("{} Keepalive RPC returned null with response. Reconnecting netconf session", id);
225 if (result.getResult() != null) {
226 lastKeepAliveSucceeded.set(true);
227 } else if (result.getErrors() != null) {
228 LOG.warn("{}: Keepalive RPC failed with error: {}", id, result.getErrors());
229 lastKeepAliveSucceeded.set(true);
231 LOG.warn("{} Keepalive RPC returned null with response. Reconnecting netconf session", id);
237 public void onFailure(final Throwable throwable) {
238 LOG.warn("{}: Keepalive RPC failed. Reconnecting netconf session.", id, throwable);
244 * Reset keepalive after each RPC response received.
246 private class ResetKeepalive implements FutureCallback<DOMRpcResult> {
248 public void onSuccess(final DOMRpcResult result) {
249 // No matter what response we got,
250 // rpc-reply or rpc-error, we got it from device so the netconf session is OK.
255 public void onFailure(final Throwable throwable) {
256 // User/Application RPC failed (The RPC did not reach the remote device or ..
257 // TODO what other reasons could cause this ?)
258 // There is no point in keeping this session. Reconnect.
259 LOG.warn("{}: Rpc failure detected. Reconnecting netconf session", id, throwable);
264 private final class ResponseWaitingScheduler {
266 private ScheduledFuture<?> schedule;
268 public void initScheduler(final Runnable runnable) {
269 if (currentKeepalive != null) {
270 currentKeepalive.cancel(true);
272 LOG.trace("Keepalive does not exist.");
274 scheduleKeepalives();
275 //Listening on the result should be done before the keepalive rpc will be send
276 final long delay = keepaliveDelaySeconds * 1000 - 500;
277 schedule = executor.schedule(runnable, delay, TimeUnit.MILLISECONDS);
280 public void stopScheduler() {
281 if (schedule != null) {
282 schedule.cancel(true);
284 LOG.trace("Scheduler does not exist.");
289 private static final class ResponseWaiting implements Runnable {
291 private final ListenableFuture<DOMRpcResult> rpcResultFuture;
292 private final ResponseWaitingScheduler responseWaitingScheduler;
294 ResponseWaiting(final ResponseWaitingScheduler responseWaitingScheduler,
295 final ListenableFuture<DOMRpcResult> rpcResultFuture) {
296 this.responseWaitingScheduler = responseWaitingScheduler;
297 this.rpcResultFuture = rpcResultFuture;
300 public void start() {
301 LOG.trace("Start to waiting for result.");
302 responseWaitingScheduler.initScheduler(this);
306 LOG.info("Stop to waiting for result.");
307 responseWaitingScheduler.stopScheduler();
312 if (!rpcResultFuture.isCancelled() && !rpcResultFuture.isDone()) {
313 LOG.trace("Waiting for result");
314 responseWaitingScheduler.initScheduler(this);
316 LOG.trace("Result has been cancelled or done.");
322 * Request timeout task is called once the defaultRequestTimeoutMillis is
323 * reached. At this moment, if the request is not yet finished, we cancel
326 private static final class RequestTimeoutTask implements Runnable {
327 private final ListenableFuture<DOMRpcResult> rpcResultFuture;
328 private final ResponseWaiting responseWaiting;
330 RequestTimeoutTask(final ListenableFuture<DOMRpcResult> rpcResultFuture,
331 final ResponseWaiting responseWaiting) {
332 this.rpcResultFuture = rpcResultFuture;
333 this.responseWaiting = responseWaiting;
338 if (!rpcResultFuture.isDone()) {
339 rpcResultFuture.cancel(true);
341 if (responseWaiting != null) {
342 responseWaiting.stop();
348 * DOMRpcService proxy that attaches reset-keepalive-task and schedule
349 * request-timeout-task to each RPC invocation.
351 public static final class KeepaliveDOMRpcService implements DOMRpcService {
353 private final DOMRpcService deviceRpc;
354 private final ResetKeepalive resetKeepaliveTask;
355 private final long defaultRequestTimeoutMillis;
356 private final ScheduledExecutorService executor;
357 private final ResponseWaitingScheduler responseWaitingScheduler;
359 KeepaliveDOMRpcService(final DOMRpcService deviceRpc, final ResetKeepalive resetKeepaliveTask,
360 final long defaultRequestTimeoutMillis, final ScheduledExecutorService executor,
361 final ResponseWaitingScheduler responseWaitingScheduler) {
362 this.deviceRpc = deviceRpc;
363 this.resetKeepaliveTask = resetKeepaliveTask;
364 this.defaultRequestTimeoutMillis = defaultRequestTimeoutMillis;
365 this.executor = executor;
366 this.responseWaitingScheduler = responseWaitingScheduler;
369 public DOMRpcService getDeviceRpc() {
374 public ListenableFuture<DOMRpcResult> invokeRpc(final SchemaPath type, final NormalizedNode<?, ?> input) {
375 final ListenableFuture<DOMRpcResult> rpcResultFuture = deviceRpc.invokeRpc(type, input);
376 final ResponseWaiting responseWaiting = new ResponseWaiting(responseWaitingScheduler, rpcResultFuture);
377 responseWaiting.start();
378 Futures.addCallback(rpcResultFuture, resetKeepaliveTask, MoreExecutors.directExecutor());
380 final RequestTimeoutTask timeoutTask = new RequestTimeoutTask(rpcResultFuture, responseWaiting);
381 executor.schedule(timeoutTask, defaultRequestTimeoutMillis, TimeUnit.MILLISECONDS);
383 return rpcResultFuture;
387 public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T listener) {
388 // There is no real communication with the device (yet), no reset here
389 return deviceRpc.registerRpcListener(listener);