2 * Copyright (c) 2014 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.listener;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.Strings;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.SettableFuture;
15 import io.netty.util.concurrent.Future;
16 import java.io.EOFException;
17 import java.util.ArrayDeque;
18 import java.util.ArrayList;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Queue;
22 import java.util.concurrent.Semaphore;
23 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
24 import java.util.concurrent.locks.Lock;
25 import java.util.concurrent.locks.ReentrantLock;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.opendaylight.netconf.api.FailedNetconfMessage;
28 import org.opendaylight.netconf.api.NetconfDocumentedException;
29 import org.opendaylight.netconf.api.NetconfMessage;
30 import org.opendaylight.netconf.api.NetconfTerminationReason;
31 import org.opendaylight.netconf.api.xml.XmlElement;
32 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
33 import org.opendaylight.netconf.api.xml.XmlUtil;
34 import org.opendaylight.netconf.client.NetconfClientDispatcher;
35 import org.opendaylight.netconf.client.NetconfClientSession;
36 import org.opendaylight.netconf.client.NetconfClientSessionListener;
37 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
38 import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration;
39 import org.opendaylight.netconf.sal.connect.api.RemoteDevice;
40 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceCommunicator;
41 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
42 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
43 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
44 import org.opendaylight.yangtools.yang.common.Empty;
45 import org.opendaylight.yangtools.yang.common.ErrorTag;
46 import org.opendaylight.yangtools.yang.common.ErrorType;
47 import org.opendaylight.yangtools.yang.common.QName;
48 import org.opendaylight.yangtools.yang.common.RpcResult;
49 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 public class NetconfDeviceCommunicator implements NetconfClientSessionListener, RemoteDeviceCommunicator {
54 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceCommunicator.class);
56 protected final RemoteDevice<NetconfDeviceCommunicator> remoteDevice;
57 private final @Nullable UserPreferences overrideNetconfCapabilities;
58 protected final RemoteDeviceId id;
59 private final Lock sessionLock = new ReentrantLock();
61 private final Semaphore semaphore;
62 private final int concurentRpcMsgs;
64 private final Queue<Request> requests = new ArrayDeque<>();
65 private NetconfClientSession currentSession;
67 private final SettableFuture<Empty> firstConnectionFuture = SettableFuture.create();
68 private Future<?> taskFuture;
70 // isSessionClosing indicates a close operation on the session is issued and
71 // tearDown will surely be called later to finish the close.
72 // Used to allow only one thread to enter tearDown and other threads should
73 // NOT enter it simultaneously and should end its close operation without
74 // calling tearDown to release the locks they hold to avoid deadlock.
75 private static final AtomicIntegerFieldUpdater<NetconfDeviceCommunicator> CLOSING_UPDATER =
76 AtomicIntegerFieldUpdater.newUpdater(NetconfDeviceCommunicator.class, "closing");
77 private volatile int closing;
79 public boolean isSessionClosing() {
83 public NetconfDeviceCommunicator(final RemoteDeviceId id,
84 final RemoteDevice<NetconfDeviceCommunicator> remoteDevice,
85 final UserPreferences netconfSessionPreferences, final int rpcMessageLimit) {
86 this(id, remoteDevice, rpcMessageLimit, requireNonNull(netconfSessionPreferences));
89 public NetconfDeviceCommunicator(final RemoteDeviceId id,
90 final RemoteDevice<NetconfDeviceCommunicator> remoteDevice, final int rpcMessageLimit) {
91 this(id, remoteDevice, rpcMessageLimit, null);
94 public NetconfDeviceCommunicator(final RemoteDeviceId id,
95 final RemoteDevice<NetconfDeviceCommunicator> remoteDevice, final int rpcMessageLimit,
96 final @Nullable UserPreferences overrideNetconfCapabilities) {
97 concurentRpcMsgs = rpcMessageLimit;
99 this.remoteDevice = remoteDevice;
100 this.overrideNetconfCapabilities = overrideNetconfCapabilities;
101 semaphore = rpcMessageLimit > 0 ? new Semaphore(rpcMessageLimit) : null;
105 public void onSessionUp(final NetconfClientSession session) {
108 LOG.debug("{}: Session established", id);
109 currentSession = session;
111 NetconfSessionPreferences netconfSessionPreferences =
112 NetconfSessionPreferences.fromNetconfSession(session);
113 LOG.trace("{}: Session advertised capabilities: {}", id,
114 netconfSessionPreferences);
116 final var localOverride = overrideNetconfCapabilities;
117 if (localOverride != null) {
118 final var sessionPreferences = localOverride.getSessionPreferences();
119 netconfSessionPreferences = localOverride.moduleBasedCapsOverrided()
120 ? netconfSessionPreferences.replaceModuleCaps(sessionPreferences)
121 : netconfSessionPreferences.addModuleCaps(sessionPreferences);
123 netconfSessionPreferences = localOverride.nonModuleBasedCapsOverrided()
124 ? netconfSessionPreferences.replaceNonModuleCaps(sessionPreferences)
125 : netconfSessionPreferences.addNonModuleCaps(sessionPreferences);
126 LOG.debug("{}: Session capabilities overridden, capabilities that will be used: {}", id,
127 netconfSessionPreferences);
130 remoteDevice.onRemoteSessionUp(netconfSessionPreferences, this);
132 sessionLock.unlock();
135 // FIXME: right, except ... this does not include the device schema setup, so is it really useful?
136 if (!firstConnectionFuture.set(Empty.value())) {
137 LOG.trace("{}: First connection already completed", id);
142 * Initialize remote connection.
144 * @param dispatcher {@code NetconfCLientDispatcher}
145 * @param config {@code NetconfClientConfiguration}
146 * @return a ListenableFuture that returns success on first successful connection and failure when the underlying
147 * reconnecting strategy runs out of reconnection attempts
149 public ListenableFuture<Empty> initializeRemoteConnection(final NetconfClientDispatcher dispatcher,
150 final NetconfClientConfiguration config) {
152 final Future<?> connectFuture;
153 if (config instanceof NetconfReconnectingClientConfiguration) {
154 // FIXME: This is weird. If I understand it correctly we want to know about the first connection so as to
155 // forward error state. Analyze the call graph to understand what is going on here. We really want
156 // to move reconnection away from the socket layer, so that it can properly interface with sessions
157 // and generally has some event-driven state (as all good network glue does). There is a second story
158 // which is we want to avoid duplicate code, so it depends on other users as well.
159 final var future = dispatcher.createReconnectingClient((NetconfReconnectingClientConfiguration) config);
161 connectFuture = future.firstSessionFuture();
163 taskFuture = connectFuture = dispatcher.createClient(config);
166 connectFuture.addListener(future -> {
167 if (!future.isSuccess() && !future.isCancelled()) {
168 LOG.debug("{}: Connection failed", id, future.cause());
169 remoteDevice.onRemoteSessionFailed(future.cause());
170 if (!firstConnectionFuture.isDone()) {
171 firstConnectionFuture.setException(future.cause());
175 return firstConnectionFuture;
178 public void disconnect() {
179 // If session is already in closing, no need to close it again
180 if (currentSession != null && startClosing() && currentSession.isUp()) {
181 currentSession.close();
185 private void tearDown(final String reason) {
186 if (!isSessionClosing()) {
187 LOG.warn("It's curious that no one to close the session but tearDown is called!");
189 LOG.debug("Tearing down {}", reason);
190 final List<UncancellableFuture<RpcResult<NetconfMessage>>> futuresToCancel = new ArrayList<>();
193 if (currentSession != null) {
194 currentSession = null;
196 * Walk all requests, check if they have been executing
197 * or cancelled and remove them from the queue.
199 final Iterator<Request> it = requests.iterator();
200 while (it.hasNext()) {
201 final Request r = it.next();
202 if (r.future.isUncancellable()) {
203 futuresToCancel.add(r.future);
205 } else if (r.future.isCancelled()) {
206 // This just does some house-cleaning
211 remoteDevice.onRemoteSessionDown();
214 sessionLock.unlock();
217 // Notify pending request futures outside of the sessionLock to avoid unnecessarily
218 // blocking the caller.
219 for (final UncancellableFuture<RpcResult<NetconfMessage>> future : futuresToCancel) {
220 if (Strings.isNullOrEmpty(reason)) {
221 future.set(createSessionDownRpcResult());
223 future.set(createErrorRpcResult(ErrorType.TRANSPORT, reason));
230 private RpcResult<NetconfMessage> createSessionDownRpcResult() {
231 return createErrorRpcResult(ErrorType.TRANSPORT,
232 String.format("The netconf session to %1$s is disconnected", id.getName()));
235 private static RpcResult<NetconfMessage> createErrorRpcResult(final ErrorType errorType, final String message) {
236 return RpcResultBuilder.<NetconfMessage>failed()
237 .withError(errorType, ErrorTag.OPERATION_FAILED, message).build();
241 public void onSessionDown(final NetconfClientSession session, final Exception exception) {
242 // If session is already in closing, no need to call tearDown again.
243 if (startClosing()) {
244 if (exception instanceof EOFException) {
245 LOG.info("{}: Session went down: {}", id, exception.getMessage());
247 LOG.warn("{}: Session went down", id, exception);
254 public void onSessionTerminated(final NetconfClientSession session, final NetconfTerminationReason reason) {
255 // onSessionTerminated is called directly by disconnect, no need to compare and set isSessionClosing.
256 LOG.warn("{}: Session terminated {}", id, reason);
257 tearDown(reason.getErrorMessage());
261 public void close() {
262 // Cancel reconnect if in progress
263 if (taskFuture != null) {
264 taskFuture.cancel(false);
266 // Disconnect from device
267 // tear down not necessary, called indirectly by the close in disconnect()
272 public void onMessage(final NetconfClientSession session, final NetconfMessage message) {
274 * Dispatch between notifications and messages. Messages need to be processed
275 * with lock held, notifications do not.
277 if (isNotification(message)) {
278 processNotification(message);
280 processMessage(message);
284 private void processMessage(final NetconfMessage message) {
285 Request request = null;
289 request = requests.peek();
290 if (request != null && request.future.isUncancellable()) {
291 request = requests.poll();
292 // we have just removed one request from the queue
293 // we can also release one permit
294 if (semaphore != null) {
299 LOG.warn("{}: Ignoring unsolicited message {}", id,
303 sessionLock.unlock();
306 if (request == null) {
307 // No matching request, bail out
312 if (message instanceof FailedNetconfMessage) {
313 request.future.set(NetconfMessageTransformUtil.toRpcResult((FailedNetconfMessage) message));
317 LOG.debug("{}: Message received {}", id, message);
319 if (LOG.isTraceEnabled()) {
320 LOG.trace("{}: Matched request: {} to response: {}", id, msgToS(request.request), msgToS(message));
324 NetconfMessageTransformUtil.checkValidReply(request.request, message);
325 } catch (final NetconfDocumentedException e) {
326 LOG.warn("{}: Invalid request-reply match, reply message contains different message-id, "
327 + "request: {}, response: {}", id, msgToS(request.request), msgToS(message), e);
329 request.future.set(RpcResultBuilder.<NetconfMessage>failed()
330 .withRpcError(NetconfMessageTransformUtil.toRpcError(e))
333 //recursively processing message to eventually find matching request
334 processMessage(message);
339 NetconfMessageTransformUtil.checkSuccessReply(message);
340 } catch (final NetconfDocumentedException e) {
341 LOG.warn("{}: Error reply from remote device, request: {}, response: {}",
342 id, msgToS(request.request), msgToS(message), e);
344 request.future.set(RpcResultBuilder.<NetconfMessage>failed()
345 .withRpcError(NetconfMessageTransformUtil.toRpcError(e))
350 request.future.set(RpcResultBuilder.success(message).build());
353 private static String msgToS(final NetconfMessage msg) {
354 return XmlUtil.toString(msg.getDocument());
358 public ListenableFuture<RpcResult<NetconfMessage>> sendRequest(final NetconfMessage message, final QName rpc) {
361 if (semaphore != null && !semaphore.tryAcquire()) {
362 LOG.warn("Limit of concurrent rpc messages was reached (limit: {}). Rpc reply message is needed. "
363 + "Discarding request of Netconf device with id: {}", concurentRpcMsgs, id.getName());
364 return FluentFutures.immediateFailedFluentFuture(new NetconfDocumentedException(
365 "Limit of rpc messages was reached (Limit :" + concurentRpcMsgs
366 + ") waiting for emptying the queue of Netconf device with id: " + id.getName()));
369 return sendRequestWithLock(message, rpc);
371 sessionLock.unlock();
375 private ListenableFuture<RpcResult<NetconfMessage>> sendRequestWithLock(final NetconfMessage message,
377 if (LOG.isTraceEnabled()) {
378 LOG.trace("{}: Sending message {}", id, msgToS(message));
381 if (currentSession == null) {
382 LOG.warn("{}: Session is disconnected, failing RPC request {}",
384 return FluentFutures.immediateFluentFuture(createSessionDownRpcResult());
387 final Request req = new Request(new UncancellableFuture<>(true), message);
390 currentSession.sendMessage(req.request).addListener(future -> {
391 if (!future.isSuccess()) {
392 // We expect that a session down will occur at this point
393 LOG.debug("{}: Failed to send request {}", id,
394 XmlUtil.toString(req.request.getDocument()),
397 if (future.cause() != null) {
398 req.future.set(createErrorRpcResult(ErrorType.TRANSPORT, future.cause().getLocalizedMessage()));
400 req.future.set(createSessionDownRpcResult()); // assume session is down
402 req.future.setException(future.cause());
404 LOG.trace("Finished sending request {}", req.request);
411 private void processNotification(final NetconfMessage notification) {
412 if (LOG.isTraceEnabled()) {
413 LOG.trace("{}: Notification received: {}", id, notification);
416 remoteDevice.onNotification(notification);
419 private static boolean isNotification(final NetconfMessage message) {
420 if (message.getDocument() == null) {
421 // We have no message, which mean we have a FailedNetconfMessage
424 final XmlElement xmle = XmlElement.fromDomDocument(message.getDocument());
425 return XmlNetconfConstants.NOTIFICATION_ELEMENT_NAME.equals(xmle.getName()) ;
428 private static final class Request {
429 final UncancellableFuture<RpcResult<NetconfMessage>> future;
430 final NetconfMessage request;
432 private Request(final UncancellableFuture<RpcResult<NetconfMessage>> future,
433 final NetconfMessage request) {
434 this.future = future;
435 this.request = request;
439 private boolean startClosing() {
440 return CLOSING_UPDATER.compareAndSet(this, 0, 1);