Cleanup warnings
[netconf.git] / netconf / 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_QNAME;
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.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.listener.NetconfDeviceCommunicator;
36 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
37 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
38 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
39 import org.opendaylight.yangtools.concepts.ListenerRegistration;
40 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
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;
46
47 /**
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.
51  */
52 public final class KeepaliveSalFacade implements RemoteDeviceHandler<NetconfSessionPreferences> {
53     private static final Logger LOG = LoggerFactory.getLogger(KeepaliveSalFacade.class);
54
55     // 2 minutes keepalive delay by default
56     private static final long DEFAULT_DELAY = TimeUnit.MINUTES.toSeconds(2);
57
58     // 1 minute transaction timeout by default
59     private static final long DEFAULT_TRANSACTION_TIMEOUT_MILLI = TimeUnit.MILLISECONDS.toMillis(60000);
60
61     private final KeepaliveTask keepaliveTask = new KeepaliveTask();
62     private final RemoteDeviceHandler<NetconfSessionPreferences> salFacade;
63     private final ScheduledExecutorService executor;
64
65     private final long keepaliveDelaySeconds;
66     private final long timeoutNanos;
67     private final long delayNanos;
68
69     private final RemoteDeviceId id;
70
71     private volatile NetconfDeviceCommunicator listener;
72     private volatile DOMRpcService currentDeviceRpc;
73
74     public KeepaliveSalFacade(final RemoteDeviceId id, final RemoteDeviceHandler<NetconfSessionPreferences> salFacade,
75                               final ScheduledExecutorService executor, final long keepaliveDelaySeconds,
76                               final long requestTimeoutMillis) {
77         this.id = id;
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);
83     }
84
85     public KeepaliveSalFacade(final RemoteDeviceId id, final RemoteDeviceHandler<NetconfSessionPreferences> salFacade,
86                               final ScheduledExecutorService executor) {
87         this(id, salFacade, executor, DEFAULT_DELAY, DEFAULT_TRANSACTION_TIMEOUT_MILLI);
88     }
89
90     /**
91      * Set the netconf session listener whenever ready.
92      *
93      * @param listener netconf session listener
94      */
95     public void setListener(final NetconfDeviceCommunicator listener) {
96         this.listener = listener;
97     }
98
99     /**
100      * Cancel current keepalive and also reset current deviceRpc.
101      */
102     private synchronized void stopKeepalives() {
103         keepaliveTask.disableKeepalive();
104         currentDeviceRpc = null;
105     }
106
107     void reconnect() {
108         checkState(listener != null, "%s: Unable to reconnect, session listener is missing", id);
109         stopKeepalives();
110         LOG.info("{}: Reconnecting inactive netconf session", id);
111         listener.disconnect();
112     }
113
114     @Override
115     public void onDeviceConnected(final MountPointContext remoteSchemaContext,
116                           final NetconfSessionPreferences netconfSessionPreferences, final DOMRpcService deviceRpc) {
117         onDeviceConnected(remoteSchemaContext, netconfSessionPreferences, deviceRpc, null);
118     }
119
120     @Override
121     public void onDeviceConnected(final MountPointContext remoteSchemaContext,
122             final NetconfSessionPreferences netconfSessionPreferences, final DOMRpcService deviceRpc,
123             final DOMActionService deviceAction) {
124         this.currentDeviceRpc = requireNonNull(deviceRpc);
125         salFacade.onDeviceConnected(remoteSchemaContext, netconfSessionPreferences,
126             new KeepaliveDOMRpcService(deviceRpc), deviceAction);
127
128         LOG.debug("{}: Netconf session initiated, starting keepalives", id);
129         LOG.trace("{}: Scheduling keepalives every {}s", id, keepaliveDelaySeconds);
130         keepaliveTask.enableKeepalive();
131     }
132
133     @Override
134     public void onDeviceDisconnected() {
135         stopKeepalives();
136         salFacade.onDeviceDisconnected();
137     }
138
139     @Override
140     public void onDeviceFailed(final Throwable throwable) {
141         stopKeepalives();
142         salFacade.onDeviceFailed(throwable);
143     }
144
145     @Override
146     public void onNotification(final DOMNotification domNotification) {
147         keepaliveTask.recordActivity();
148         salFacade.onNotification(domNotification);
149     }
150
151     @Override
152     public void close() {
153         stopKeepalives();
154         salFacade.close();
155     }
156
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);
161
162     /**
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.
167      */
168     private final class KeepaliveTask implements Runnable, FutureCallback<DOMRpcResult> {
169         private volatile long lastActivity;
170         @GuardedBy("this")
171         private boolean suppressed;
172
173         KeepaliveTask() {
174             suppressed = false;
175         }
176
177         @Override
178         public void run() {
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);
184             } else {
185                 sendKeepalive(now);
186             }
187         }
188
189         void recordActivity() {
190             lastActivity = System.nanoTime();
191         }
192
193         synchronized void disableKeepalive() {
194             // unsuppressed -> suppressed
195             suppressed = true;
196         }
197
198         synchronized void enableKeepalive() {
199             recordActivity();
200             if (!suppressed) {
201                 // unscheduled -> unsuppressed
202                 reschedule();
203             } else {
204                 // suppressed -> unsuppressed
205                 suppressed = false;
206             }
207         }
208
209         private synchronized void sendKeepalive(final long now) {
210             if (suppressed) {
211                 // suppressed -> unscheduled
212                 suppressed = false;
213                 return;
214             }
215
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);
221                 return;
222             }
223
224             LOG.trace("{}: Invoking keepalive RPC", id);
225             final ListenableFuture<? extends DOMRpcResult> deviceFuture =
226                 currentDeviceRpc.invokeRpc(NETCONF_GET_CONFIG_QNAME, KEEPALIVE_PAYLOAD);
227
228             lastActivity = now;
229             Futures.addCallback(deviceFuture, this, MoreExecutors.directExecutor());
230         }
231
232         @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE",
233                 justification = "Unrecognised NullableDecl")
234         @Override
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);
240                 reconnect();
241                 return;
242             }
243
244             if (result.getResult() != null) {
245                 reschedule();
246             } else {
247                 final Collection<?> errors = result.getErrors();
248                 if (!errors.isEmpty()) {
249                     LOG.warn("{}: Keepalive RPC failed with error: {}", id, errors);
250                     reschedule();
251                 } else {
252                     LOG.warn("{} Keepalive RPC returned null with response. Reconnecting netconf session", id);
253                     reconnect();
254                 }
255             }
256         }
257
258         @Override
259         public void onFailure(final Throwable throwable) {
260             LOG.warn("{}: Keepalive RPC failed. Reconnecting netconf session.", id, throwable);
261             reconnect();
262         }
263
264         private void reschedule() {
265             reschedule(delayNanos);
266         }
267
268         private void reschedule(final long delay) {
269             executor.schedule(this, delay, TimeUnit.NANOSECONDS);
270         }
271     }
272
273     /*
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.
276      */
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;
280
281         RequestTimeoutTask(final ListenableFuture<? extends DOMRpcResult> rpcResultFuture) {
282             this.deviceFuture = requireNonNull(rpcResultFuture);
283             Futures.addCallback(deviceFuture, this, MoreExecutors.directExecutor());
284         }
285
286         @Override
287         public void run() {
288             deviceFuture.cancel(true);
289             userFuture.cancel(false);
290             keepaliveTask.enableKeepalive();
291         }
292
293         @Override
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();
299         }
300
301         @Override
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.
308             reconnect();
309         }
310     }
311
312     /**
313      * DOMRpcService proxy that attaches reset-keepalive-task and schedule
314      * request-timeout-task to each RPC invocation.
315      */
316     public final class KeepaliveDOMRpcService implements DOMRpcService {
317         private final @NonNull DOMRpcService deviceRpc;
318
319         KeepaliveDOMRpcService(final DOMRpcService deviceRpc) {
320             this.deviceRpc = requireNonNull(deviceRpc);
321         }
322
323         public @NonNull DOMRpcService getDeviceRpc() {
324             return deviceRpc;
325         }
326
327         @Override
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);
331
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());
335
336             return timeout.userFuture;
337         }
338
339         @Override
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);
343         }
344     }
345 }