Add RemoteDeviceServices
[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_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.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.DOMNotification;
30 import org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener;
31 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
32 import org.opendaylight.mdsal.dom.api.DOMRpcService;
33 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
34 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices;
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;
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 {
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 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 RemoteDeviceServices currentServices;
73
74     public KeepaliveSalFacade(final RemoteDeviceId id, final RemoteDeviceHandler 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 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         currentServices = 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 NetconfDeviceSchema deviceSchema,
116             final NetconfSessionPreferences sessionPreferences, final RemoteDeviceServices services) {
117         currentServices = requireNonNull(services);
118
119         final var devAction = services.actions();
120         // FIXME: wrap with keepalive
121         final var kaAction = devAction;
122
123         salFacade.onDeviceConnected(deviceSchema, sessionPreferences,
124             new RemoteDeviceServices(new KeepaliveDOMRpcService(services.rpcs()), kaAction));
125
126         LOG.debug("{}: Netconf session initiated, starting keepalives", id);
127         LOG.trace("{}: Scheduling keepalives every {}s", id, keepaliveDelaySeconds);
128         keepaliveTask.enableKeepalive();
129     }
130
131     @Override
132     public void onDeviceDisconnected() {
133         stopKeepalives();
134         salFacade.onDeviceDisconnected();
135     }
136
137     @Override
138     public void onDeviceFailed(final Throwable throwable) {
139         stopKeepalives();
140         salFacade.onDeviceFailed(throwable);
141     }
142
143     @Override
144     public void onNotification(final DOMNotification domNotification) {
145         keepaliveTask.recordActivity();
146         salFacade.onNotification(domNotification);
147     }
148
149     @Override
150     public void close() {
151         stopKeepalives();
152         salFacade.close();
153     }
154
155     // Keepalive RPC static resources
156     private static final @NonNull ContainerNode KEEPALIVE_PAYLOAD =
157         NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_NODEID,
158             getSourceNode(NETCONF_RUNNING_NODEID), NetconfMessageTransformUtil.EMPTY_FILTER);
159
160     /**
161      * Invoke keepalive RPC and check the response. In case of any received response the keepalive
162      * is considered successful and schedules next keepalive with a fixed delay. If the response is unsuccessful (no
163      * response received, or the rcp could not even be sent) immediate reconnect is triggered as netconf session
164      * is considered inactive/failed.
165      */
166     private final class KeepaliveTask implements Runnable, FutureCallback<DOMRpcResult> {
167         private volatile long lastActivity;
168         @GuardedBy("this")
169         private boolean suppressed;
170
171         KeepaliveTask() {
172             suppressed = false;
173         }
174
175         @Override
176         public void run() {
177             final long local = lastActivity;
178             final long now = System.nanoTime();
179             final long inFutureNanos = local + delayNanos - now;
180             if (inFutureNanos > 0) {
181                 reschedule(inFutureNanos);
182             } else {
183                 sendKeepalive(now);
184             }
185         }
186
187         void recordActivity() {
188             lastActivity = System.nanoTime();
189         }
190
191         synchronized void disableKeepalive() {
192             // unsuppressed -> suppressed
193             suppressed = true;
194         }
195
196         synchronized void enableKeepalive() {
197             recordActivity();
198             if (!suppressed) {
199                 // unscheduled -> unsuppressed
200                 reschedule();
201             } else {
202                 // suppressed -> unsuppressed
203                 suppressed = false;
204             }
205         }
206
207         private synchronized void sendKeepalive(final long now) {
208             if (suppressed) {
209                 // suppressed -> unscheduled
210                 suppressed = false;
211                 return;
212             }
213
214             final var localServices = currentServices;
215             if (localServices == null) {
216                 // deviceRpc is null, which means we hit the reconnect window and attempted to send keepalive while
217                 // we were reconnecting. Next keepalive will be scheduled after reconnect so no action necessary here.
218                 LOG.debug("{}: Skipping keepalive while reconnecting", id);
219                 return;
220             }
221
222             LOG.trace("{}: Invoking keepalive RPC", id);
223             final var deviceFuture = localServices.rpcs().invokeRpc(NETCONF_GET_CONFIG_QNAME, KEEPALIVE_PAYLOAD);
224
225             lastActivity = now;
226             Futures.addCallback(deviceFuture, this, MoreExecutors.directExecutor());
227         }
228
229         @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE",
230                 justification = "Unrecognised NullableDecl")
231         @Override
232         public void onSuccess(final DOMRpcResult result) {
233             // No matter what response we got, rpc-reply or rpc-error,
234             // we got it from device so the netconf session is OK
235             if (result == null) {
236                 LOG.warn("{} Keepalive RPC returned null with response. Reconnecting netconf session", id);
237                 reconnect();
238                 return;
239             }
240
241             if (result.getResult() != null) {
242                 reschedule();
243             } else {
244                 final Collection<?> errors = result.getErrors();
245                 if (!errors.isEmpty()) {
246                     LOG.warn("{}: Keepalive RPC failed with error: {}", id, errors);
247                     reschedule();
248                 } else {
249                     LOG.warn("{} Keepalive RPC returned null with response. Reconnecting netconf session", id);
250                     reconnect();
251                 }
252             }
253         }
254
255         @Override
256         public void onFailure(final Throwable throwable) {
257             LOG.warn("{}: Keepalive RPC failed. Reconnecting netconf session.", id, throwable);
258             reconnect();
259         }
260
261         private void reschedule() {
262             reschedule(delayNanos);
263         }
264
265         private void reschedule(final long delay) {
266             executor.schedule(this, delay, TimeUnit.NANOSECONDS);
267         }
268     }
269
270     /*
271      * Request timeout task is called once the requestTimeoutMillis is reached. At that moment, if the request is not
272      * yet finished, we cancel it.
273      */
274     private final class RequestTimeoutTask implements FutureCallback<DOMRpcResult>, Runnable {
275         private final @NonNull SettableFuture<DOMRpcResult> userFuture = SettableFuture.create();
276         private final @NonNull ListenableFuture<? extends DOMRpcResult> deviceFuture;
277
278         RequestTimeoutTask(final ListenableFuture<? extends DOMRpcResult> rpcResultFuture) {
279             deviceFuture = requireNonNull(rpcResultFuture);
280             Futures.addCallback(deviceFuture, this, MoreExecutors.directExecutor());
281         }
282
283         @Override
284         public void run() {
285             deviceFuture.cancel(true);
286             userFuture.cancel(false);
287             keepaliveTask.enableKeepalive();
288         }
289
290         @Override
291         public void onSuccess(final DOMRpcResult result) {
292             // No matter what response we got,
293             // rpc-reply or rpc-error, we got it from device so the netconf session is OK.
294             userFuture.set(result);
295             keepaliveTask.enableKeepalive();
296         }
297
298         @Override
299         public void onFailure(final Throwable throwable) {
300             // User/Application RPC failed (The RPC did not reach the remote device or ...)
301             // FIXME: what other reasons could cause this ?)
302             LOG.warn("{}: Rpc failure detected. Reconnecting netconf session", id, throwable);
303             userFuture.setException(throwable);
304             // There is no point in keeping this session. Reconnect.
305             reconnect();
306         }
307     }
308
309     /**
310      * DOMRpcService proxy that attaches reset-keepalive-task and schedule
311      * request-timeout-task to each RPC invocation.
312      */
313     public final class KeepaliveDOMRpcService implements DOMRpcService {
314         private final @NonNull DOMRpcService deviceRpc;
315
316         KeepaliveDOMRpcService(final DOMRpcService deviceRpc) {
317             this.deviceRpc = requireNonNull(deviceRpc);
318         }
319
320         public @NonNull DOMRpcService getDeviceRpc() {
321             return deviceRpc;
322         }
323
324         @Override
325         public ListenableFuture<? extends DOMRpcResult> invokeRpc(final QName type, final NormalizedNode input) {
326             keepaliveTask.disableKeepalive();
327             final ListenableFuture<? extends DOMRpcResult> deviceFuture = deviceRpc.invokeRpc(type, input);
328
329             final RequestTimeoutTask timeout = new RequestTimeoutTask(deviceFuture);
330             final ScheduledFuture<?> timeoutFuture = executor.schedule(timeout, timeoutNanos, TimeUnit.NANOSECONDS);
331             deviceFuture.addListener(() -> timeoutFuture.cancel(false), MoreExecutors.directExecutor());
332
333             return timeout.userFuture;
334         }
335
336         @Override
337         public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T rpcListener) {
338             // There is no real communication with the device (yet), hence recordActivity() or anything
339             return deviceRpc.registerRpcListener(rpcListener);
340         }
341     }
342 }