Move sal-netconf-connector to plugins/
[netconf.git] / plugins / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / sal / KeepaliveSalFacade.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.sal.connect.netconf.sal;
9
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;
16
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;
44
45 /**
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.
49  */
50 public final class KeepaliveSalFacade implements RemoteDeviceHandler {
51     private static final Logger LOG = LoggerFactory.getLogger(KeepaliveSalFacade.class);
52
53     // 2 minutes keepalive delay by default
54     private static final long DEFAULT_DELAY = TimeUnit.MINUTES.toSeconds(2);
55
56     // 1 minute transaction timeout by default
57     private static final long DEFAULT_TRANSACTION_TIMEOUT_MILLI = TimeUnit.MILLISECONDS.toMillis(60000);
58
59     private final RemoteDeviceHandler salFacade;
60     private final ScheduledExecutorService executor;
61
62     private final long keepaliveDelaySeconds;
63     private final long timeoutNanos;
64     private final long delayNanos;
65
66     private final RemoteDeviceId id;
67
68     private volatile NetconfDeviceCommunicator listener;
69     private volatile KeepaliveTask task;
70
71     public KeepaliveSalFacade(final RemoteDeviceId id, final RemoteDeviceHandler salFacade,
72             final ScheduledExecutorService executor, final long keepaliveDelaySeconds,
73             final long requestTimeoutMillis) {
74         this.id = id;
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);
80     }
81
82     public KeepaliveSalFacade(final RemoteDeviceId id, final RemoteDeviceHandler salFacade,
83             final ScheduledExecutorService executor) {
84         this(id, salFacade, executor, DEFAULT_DELAY, DEFAULT_TRANSACTION_TIMEOUT_MILLI);
85     }
86
87     /**
88      * Set the netconf session listener whenever ready.
89      *
90      * @param listener netconf session listener
91      */
92     public void setListener(final NetconfDeviceCommunicator listener) {
93         this.listener = listener;
94     }
95
96     /**
97      * Cancel current keepalive and free it.
98      */
99     private synchronized void stopKeepalives() {
100         final var localTask = task;
101         if (localTask != null) {
102             localTask.disableKeepalive();
103             task = null;
104         }
105     }
106
107     private void disableKeepalive() {
108         final var localTask = task;
109         if (localTask != null) {
110             localTask.disableKeepalive();
111         }
112     }
113
114     private void enableKeepalive() {
115         final var localTask = task;
116         if (localTask != null) {
117             localTask.enableKeepalive();
118         }
119     }
120
121     void reconnect() {
122         checkState(listener != null, "%s: Unable to reconnect, session listener is missing", id);
123         stopKeepalives();
124         LOG.info("{}: Reconnecting inactive netconf session", id);
125         listener.disconnect();
126     }
127
128     @Override
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);
133
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);
139         } else {
140             throw new IllegalStateException("Unhandled " + devRpc);
141         }
142
143         salFacade.onDeviceConnected(deviceSchema, sessionPreferences, new RemoteDeviceServices(keepaliveRpcs,
144             // FIXME: wrap with keepalive
145             services.actions()));
146
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();
153         }
154     }
155
156     @Override
157     public void onDeviceDisconnected() {
158         stopKeepalives();
159         salFacade.onDeviceDisconnected();
160     }
161
162     @Override
163     public void onDeviceFailed(final Throwable throwable) {
164         stopKeepalives();
165         salFacade.onDeviceFailed(throwable);
166     }
167
168     @Override
169     public void onNotification(final DOMNotification domNotification) {
170         final var localTask = task;
171         if (localTask != null) {
172             localTask.recordActivity();
173         }
174         salFacade.onNotification(domNotification);
175     }
176
177     @Override
178     public void close() {
179         stopKeepalives();
180         salFacade.close();
181     }
182
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;
188     }
189
190     /**
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.
195      */
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);
200
201         private final Rpcs devRpc;
202
203         @GuardedBy("this")
204         private boolean suppressed = false;
205
206         private volatile long lastActivity;
207
208         KeepaliveTask(final Rpcs devRpc) {
209             this.devRpc = requireNonNull(devRpc);
210         }
211
212         @Override
213         public void run() {
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);
219             } else {
220                 sendKeepalive(now);
221             }
222         }
223
224         void recordActivity() {
225             lastActivity = System.nanoTime();
226         }
227
228         synchronized void disableKeepalive() {
229             // unsuppressed -> suppressed
230             suppressed = true;
231         }
232
233         synchronized void enableKeepalive() {
234             recordActivity();
235             if (!suppressed) {
236                 // unscheduled -> unsuppressed
237                 reschedule();
238             } else {
239                 // suppressed -> unsuppressed
240                 suppressed = false;
241             }
242         }
243
244         private synchronized void sendKeepalive(final long now) {
245             if (suppressed) {
246                 LOG.debug("{}: Skipping keepalive while disabled", id);
247                 // suppressed -> unscheduled
248                 suppressed = false;
249                 return;
250             }
251
252             LOG.trace("{}: Invoking keepalive RPC", id);
253             final var deviceFuture = devRpc.invokeNetconf(NETCONF_GET_CONFIG_QNAME, KEEPALIVE_PAYLOAD);
254             lastActivity = now;
255             Futures.addCallback(deviceFuture, this, MoreExecutors.directExecutor());
256         }
257
258         // FIXME: re-examine this suppression
259         @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE",
260                 justification = "Unrecognised NullableDecl")
261         @Override
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);
267                 reconnect();
268                 return;
269             }
270
271             if (result.value() != null) {
272                 reschedule();
273             } else {
274                 final var errors = result.errors();
275                 if (!errors.isEmpty()) {
276                     LOG.warn("{}: Keepalive RPC failed with error: {}", id, errors);
277                     reschedule();
278                 } else {
279                     LOG.warn("{} Keepalive RPC returned null with response. Reconnecting netconf session", id);
280                     reconnect();
281                 }
282             }
283         }
284
285         @Override
286         public void onFailure(final Throwable throwable) {
287             LOG.warn("{}: Keepalive RPC failed. Reconnecting netconf session.", id, throwable);
288             reconnect();
289         }
290
291         private void reschedule() {
292             reschedule(delayNanos);
293         }
294
295         private void reschedule(final long delay) {
296             executor.schedule(this, delay, TimeUnit.NANOSECONDS);
297         }
298     }
299
300     /*
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.
303      */
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;
307
308         RequestTimeoutTask(final ListenableFuture<V> rpcResultFuture) {
309             this.rpcResultFuture = requireNonNull(rpcResultFuture);
310             Futures.addCallback(rpcResultFuture, this, MoreExecutors.directExecutor());
311         }
312
313         @Override
314         public void run() {
315             rpcResultFuture.cancel(true);
316             userFuture.cancel(false);
317             enableKeepalive();
318         }
319
320         @Override
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);
325             enableKeepalive();
326         }
327
328         @Override
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.
335             reconnect();
336         }
337     }
338
339     /**
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}.
342      */
343     private final class NormalizedKeepaliveRpcs implements Rpcs.Normalized {
344         private final Rpcs.Normalized delegate;
345
346         NormalizedKeepaliveRpcs(final Rpcs.Normalized delegate) {
347             this.delegate = requireNonNull(delegate);
348         }
349
350         @Override
351         public ListenableFuture<? extends DOMRpcResult> invokeRpc(final QName type, final ContainerNode input) {
352             // FIXME: what happens if we disable keepalive and then invokeRpc() throws?
353             disableKeepalive();
354             return scheduleTimeout(delegate.invokeRpc(type, input));
355         }
356
357         @Override
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);
362         }
363     }
364
365     /**
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}.
368      */
369     private final class SchemalessKeepaliveRpcs implements Rpcs.Schemaless {
370         private final Rpcs.Schemaless delegate;
371
372         SchemalessKeepaliveRpcs(final Rpcs.Schemaless delegate) {
373             this.delegate = requireNonNull(delegate);
374         }
375
376         @Override
377         public ListenableFuture<? extends DOMRpcResult> invokeNetconf(final QName type, final ContainerNode input) {
378             // FIXME: what happens if we disable keepalive and then invokeRpc() throws?
379             disableKeepalive();
380             return scheduleTimeout(delegate.invokeNetconf(type, input));
381         }
382
383         @Override
384         public ListenableFuture<? extends DOMSource> invokeRpc(final QName type, final DOMSource input) {
385             // FIXME: what happens if we disable keepalive and then invokeRpc() throws?
386             disableKeepalive();
387             return scheduleTimeout(delegate.invokeRpc(type, input));
388         }
389     }
390 }