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 com.google.common.base.Strings;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import com.google.common.util.concurrent.SettableFuture;
13 import io.netty.util.concurrent.Future;
14 import java.util.ArrayDeque;
15 import java.util.ArrayList;
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.Optional;
19 import java.util.Queue;
20 import java.util.concurrent.Semaphore;
21 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
22 import java.util.concurrent.locks.Lock;
23 import java.util.concurrent.locks.ReentrantLock;
24 import org.opendaylight.netconf.api.FailedNetconfMessage;
25 import org.opendaylight.netconf.api.NetconfDocumentedException;
26 import org.opendaylight.netconf.api.NetconfMessage;
27 import org.opendaylight.netconf.api.NetconfTerminationReason;
28 import org.opendaylight.netconf.api.xml.XmlElement;
29 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
30 import org.opendaylight.netconf.api.xml.XmlUtil;
31 import org.opendaylight.netconf.client.NetconfClientDispatcher;
32 import org.opendaylight.netconf.client.NetconfClientSession;
33 import org.opendaylight.netconf.client.NetconfClientSessionListener;
34 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
35 import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration;
36 import org.opendaylight.netconf.sal.connect.api.RemoteDevice;
37 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceCommunicator;
38 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
39 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
40 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
41 import org.opendaylight.yangtools.yang.common.QName;
42 import org.opendaylight.yangtools.yang.common.RpcError;
43 import org.opendaylight.yangtools.yang.common.RpcResult;
44 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 public class NetconfDeviceCommunicator
49 implements NetconfClientSessionListener, RemoteDeviceCommunicator<NetconfMessage> {
51 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceCommunicator.class);
53 protected final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> remoteDevice;
54 private final Optional<UserPreferences> overrideNetconfCapabilities;
55 protected final RemoteDeviceId id;
56 private final Lock sessionLock = new ReentrantLock();
58 private final Semaphore semaphore;
59 private final int concurentRpcMsgs;
61 private final Queue<Request> requests = new ArrayDeque<>();
62 private NetconfClientSession currentSession;
64 private final SettableFuture<NetconfDeviceCapabilities> firstConnectionFuture;
65 private Future<?> initFuture;
67 // isSessionClosing indicates a close operation on the session is issued and
68 // tearDown will surely be called later to finish the close.
69 // Used to allow only one thread to enter tearDown and other threads should
70 // NOT enter it simultaneously and should end its close operation without
71 // calling tearDown to release the locks they hold to avoid deadlock.
72 private static final AtomicIntegerFieldUpdater<NetconfDeviceCommunicator> CLOSING_UPDATER =
73 AtomicIntegerFieldUpdater.newUpdater(NetconfDeviceCommunicator.class, "closing");
74 private volatile int closing;
76 public boolean isSessionClosing() {
80 public NetconfDeviceCommunicator(
81 final RemoteDeviceId id,
82 final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> remoteDevice,
83 final UserPreferences netconfSessionPreferences, final int rpcMessageLimit) {
84 this(id, remoteDevice, Optional.of(netconfSessionPreferences), rpcMessageLimit);
87 public NetconfDeviceCommunicator(
88 final RemoteDeviceId id,
89 final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> remoteDevice,
90 final int rpcMessageLimit) {
91 this(id, remoteDevice, Optional.empty(), rpcMessageLimit);
94 private NetconfDeviceCommunicator(
95 final RemoteDeviceId id,
96 final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> remoteDevice,
97 final Optional<UserPreferences> overrideNetconfCapabilities, final int rpcMessageLimit) {
98 this.concurentRpcMsgs = rpcMessageLimit;
100 this.remoteDevice = remoteDevice;
101 this.overrideNetconfCapabilities = overrideNetconfCapabilities;
102 this.firstConnectionFuture = SettableFuture.create();
103 this.semaphore = rpcMessageLimit > 0 ? new Semaphore(rpcMessageLimit) : null;
107 public void onSessionUp(final NetconfClientSession session) {
110 LOG.debug("{}: Session established", id);
111 currentSession = session;
113 NetconfSessionPreferences netconfSessionPreferences =
114 NetconfSessionPreferences.fromNetconfSession(session);
115 LOG.trace("{}: Session advertised capabilities: {}", id,
116 netconfSessionPreferences);
118 if (overrideNetconfCapabilities.isPresent()) {
119 final NetconfSessionPreferences sessionPreferences = overrideNetconfCapabilities
120 .get().getSessionPreferences();
121 netconfSessionPreferences = overrideNetconfCapabilities.get().moduleBasedCapsOverrided()
122 ? netconfSessionPreferences.replaceModuleCaps(sessionPreferences)
123 : netconfSessionPreferences.addModuleCaps(sessionPreferences);
125 netconfSessionPreferences = overrideNetconfCapabilities.get().nonModuleBasedCapsOverrided()
126 ? netconfSessionPreferences.replaceNonModuleCaps(sessionPreferences)
127 : netconfSessionPreferences.addNonModuleCaps(sessionPreferences);
128 LOG.debug("{}: Session capabilities overridden, capabilities that will be used: {}", id,
129 netconfSessionPreferences);
132 remoteDevice.onRemoteSessionUp(netconfSessionPreferences, this);
133 if (!firstConnectionFuture.isDone()) {
134 firstConnectionFuture.set(netconfSessionPreferences.getNetconfDeviceCapabilities());
137 sessionLock.unlock();
142 * Initialize remote connection.
144 * @param dispatcher {@code NetconfCLientDispatcher}
145 * @param config {@code NetconfClientConfiguration}
146 * @return future that returns succes on first succesfull connection and failure when the underlying
147 * reconnecting strategy runs out of reconnection attempts
149 public ListenableFuture<NetconfDeviceCapabilities> initializeRemoteConnection(
150 final NetconfClientDispatcher dispatcher, final NetconfClientConfiguration config) {
151 if (config instanceof NetconfReconnectingClientConfiguration) {
152 initFuture = dispatcher.createReconnectingClient((NetconfReconnectingClientConfiguration) config);
154 initFuture = dispatcher.createClient(config);
158 initFuture.addListener(future -> {
159 if (!future.isSuccess() && !future.isCancelled()) {
160 LOG.debug("{}: Connection failed", id, future.cause());
161 NetconfDeviceCommunicator.this.remoteDevice.onRemoteSessionFailed(future.cause());
162 if (firstConnectionFuture.isDone()) {
163 firstConnectionFuture.setException(future.cause());
167 return firstConnectionFuture;
170 public void disconnect() {
171 // If session is already in closing, no need to close it again
172 if (currentSession != null && startClosing() && currentSession.isUp()) {
173 currentSession.close();
177 private void tearDown(final String reason) {
178 if (!isSessionClosing()) {
179 LOG.warn("It's curious that no one to close the session but tearDown is called!");
181 LOG.debug("Tearing down {}", reason);
182 final List<UncancellableFuture<RpcResult<NetconfMessage>>> futuresToCancel = new ArrayList<>();
185 if (currentSession != null) {
186 currentSession = null;
188 * Walk all requests, check if they have been executing
189 * or cancelled and remove them from the queue.
191 final Iterator<Request> it = requests.iterator();
192 while (it.hasNext()) {
193 final Request r = it.next();
194 if (r.future.isUncancellable()) {
195 futuresToCancel.add(r.future);
197 } else if (r.future.isCancelled()) {
198 // This just does some house-cleaning
203 remoteDevice.onRemoteSessionDown();
206 sessionLock.unlock();
209 // Notify pending request futures outside of the sessionLock to avoid unnecessarily
210 // blocking the caller.
211 for (final UncancellableFuture<RpcResult<NetconfMessage>> future : futuresToCancel) {
212 if (Strings.isNullOrEmpty(reason)) {
213 future.set(createSessionDownRpcResult());
215 future.set(createErrorRpcResult(RpcError.ErrorType.TRANSPORT, reason));
222 private RpcResult<NetconfMessage> createSessionDownRpcResult() {
223 return createErrorRpcResult(RpcError.ErrorType.TRANSPORT,
224 String.format("The netconf session to %1$s is disconnected", id.getName()));
227 private static RpcResult<NetconfMessage> createErrorRpcResult(final RpcError.ErrorType errorType,
228 final String message) {
229 return RpcResultBuilder.<NetconfMessage>failed()
230 .withError(errorType, NetconfDocumentedException.ErrorTag.OPERATION_FAILED.getTagValue(), message).build();
234 public void onSessionDown(final NetconfClientSession session, final Exception exception) {
235 // If session is already in closing, no need to call tearDown again.
236 if (startClosing()) {
237 LOG.warn("{}: Session went down", id, exception);
243 public void onSessionTerminated(final NetconfClientSession session, final NetconfTerminationReason reason) {
244 // onSessionTerminated is called directly by disconnect, no need to compare and set isSessionClosing.
245 LOG.warn("{}: Session terminated {}", id, reason);
246 tearDown(reason.getErrorMessage());
250 public void close() {
251 // Cancel reconnect if in progress
252 if (initFuture != null) {
253 initFuture.cancel(false);
255 // Disconnect from device
256 // tear down not necessary, called indirectly by the close in disconnect()
261 public void onMessage(final NetconfClientSession session, final NetconfMessage message) {
263 * Dispatch between notifications and messages. Messages need to be processed
264 * with lock held, notifications do not.
266 if (isNotification(message)) {
267 processNotification(message);
269 processMessage(message);
273 private void processMessage(final NetconfMessage message) {
274 Request request = null;
278 request = requests.peek();
279 if (request != null && request.future.isUncancellable()) {
280 request = requests.poll();
281 // we have just removed one request from the queue
282 // we can also release one permit
283 if (semaphore != null) {
288 LOG.warn("{}: Ignoring unsolicited message {}", id,
292 sessionLock.unlock();
295 if (request != null) {
297 if (FailedNetconfMessage.class.isInstance(message)) {
298 request.future.set(NetconfMessageTransformUtil.toRpcResult((FailedNetconfMessage) message));
302 LOG.debug("{}: Message received {}", id, message);
304 if (LOG.isTraceEnabled()) {
305 LOG.trace("{}: Matched request: {} to response: {}", id, msgToS(request.request), msgToS(message));
309 NetconfMessageTransformUtil.checkValidReply(request.request, message);
310 } catch (final NetconfDocumentedException e) {
312 "{}: Invalid request-reply match,"
313 + "reply message contains different message-id, request: {}, response: {}",
314 id, msgToS(request.request), msgToS(message), e);
316 request.future.set(RpcResultBuilder.<NetconfMessage>failed()
317 .withRpcError(NetconfMessageTransformUtil.toRpcError(e)).build());
319 //recursively processing message to eventually find matching request
320 processMessage(message);
326 NetconfMessageTransformUtil.checkSuccessReply(message);
327 } catch (final NetconfDocumentedException e) {
329 "{}: Error reply from remote device, request: {}, response: {}",
330 id, msgToS(request.request), msgToS(message), e);
332 request.future.set(RpcResultBuilder.<NetconfMessage>failed()
333 .withRpcError(NetconfMessageTransformUtil.toRpcError(e)).build());
337 request.future.set(RpcResultBuilder.success(message).build());
341 private static String msgToS(final NetconfMessage msg) {
342 return XmlUtil.toString(msg.getDocument());
346 public ListenableFuture<RpcResult<NetconfMessage>> sendRequest(final NetconfMessage message, final QName rpc) {
349 if (semaphore != null && !semaphore.tryAcquire()) {
350 LOG.warn("Limit of concurrent rpc messages was reached (limit: {}). Rpc reply message is needed. "
351 + "Discarding request of Netconf device with id {}", concurentRpcMsgs, id.getName());
352 return FluentFutures.immediateFailedFluentFuture(new NetconfDocumentedException(
353 "Limit of rpc messages was reached (Limit :" + concurentRpcMsgs
354 + ") waiting for emptying the queue of Netconf device with id" + id.getName()));
357 return sendRequestWithLock(message, rpc);
359 sessionLock.unlock();
363 private ListenableFuture<RpcResult<NetconfMessage>> sendRequestWithLock(final NetconfMessage message,
365 if (LOG.isTraceEnabled()) {
366 LOG.trace("{}: Sending message {}", id, msgToS(message));
369 if (currentSession == null) {
370 LOG.warn("{}: Session is disconnected, failing RPC request {}",
372 return FluentFutures.immediateFluentFuture(createSessionDownRpcResult());
375 final Request req = new Request(new UncancellableFuture<>(true), message);
378 currentSession.sendMessage(req.request).addListener(future -> {
379 if (!future.isSuccess()) {
380 // We expect that a session down will occur at this point
381 LOG.debug("{}: Failed to send request {}", id,
382 XmlUtil.toString(req.request.getDocument()),
385 if (future.cause() != null) {
386 req.future.set(createErrorRpcResult(RpcError.ErrorType.TRANSPORT,
387 future.cause().getLocalizedMessage()));
389 req.future.set(createSessionDownRpcResult()); // assume session is down
391 req.future.setException(future.cause());
393 LOG.trace("Finished sending request {}", req.request);
400 private void processNotification(final NetconfMessage notification) {
401 if (LOG.isTraceEnabled()) {
402 LOG.trace("{}: Notification received: {}", id, notification);
405 remoteDevice.onNotification(notification);
408 private static boolean isNotification(final NetconfMessage message) {
409 if (message.getDocument() == null) {
410 // We have no message, which mean we have a FailedNetconfMessage
413 final XmlElement xmle = XmlElement.fromDomDocument(message.getDocument());
414 return XmlNetconfConstants.NOTIFICATION_ELEMENT_NAME.equals(xmle.getName()) ;
417 private static final class Request {
418 final UncancellableFuture<RpcResult<NetconfMessage>> future;
419 final NetconfMessage request;
421 private Request(final UncancellableFuture<RpcResult<NetconfMessage>> future,
422 final NetconfMessage request) {
423 this.future = future;
424 this.request = request;
428 private boolean startClosing() {
429 return CLOSING_UPDATER.compareAndSet(this, 0, 1);