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.Optional;
11 import com.google.common.base.Strings;
12 import com.google.common.collect.Lists;
13 import com.google.common.util.concurrent.FluentFuture;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.SettableFuture;
16 import io.netty.util.concurrent.Future;
17 import java.util.ArrayDeque;
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.Queue;
21 import java.util.concurrent.Semaphore;
22 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
23 import java.util.concurrent.locks.Lock;
24 import java.util.concurrent.locks.ReentrantLock;
25 import org.opendaylight.netconf.api.FailedNetconfMessage;
26 import org.opendaylight.netconf.api.NetconfDocumentedException;
27 import org.opendaylight.netconf.api.NetconfMessage;
28 import org.opendaylight.netconf.api.NetconfTerminationReason;
29 import org.opendaylight.netconf.api.xml.XmlElement;
30 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
31 import org.opendaylight.netconf.api.xml.XmlUtil;
32 import org.opendaylight.netconf.client.NetconfClientDispatcher;
33 import org.opendaylight.netconf.client.NetconfClientSession;
34 import org.opendaylight.netconf.client.NetconfClientSessionListener;
35 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
36 import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration;
37 import org.opendaylight.netconf.sal.connect.api.RemoteDevice;
38 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceCommunicator;
39 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
40 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
41 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
42 import org.opendaylight.yangtools.yang.common.QName;
43 import org.opendaylight.yangtools.yang.common.RpcError;
44 import org.opendaylight.yangtools.yang.common.RpcResult;
45 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 public class NetconfDeviceCommunicator
50 implements NetconfClientSessionListener, RemoteDeviceCommunicator<NetconfMessage> {
52 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceCommunicator.class);
54 protected final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> remoteDevice;
55 private final Optional<UserPreferences> overrideNetconfCapabilities;
56 protected final RemoteDeviceId id;
57 private final Lock sessionLock = new ReentrantLock();
59 private final Semaphore semaphore;
60 private final int concurentRpcMsgs;
62 private final Queue<Request> requests = new ArrayDeque<>();
63 private NetconfClientSession currentSession;
65 private final SettableFuture<NetconfDeviceCapabilities> firstConnectionFuture;
66 private Future<?> initFuture;
68 // isSessionClosing indicates a close operation on the session is issued and
69 // tearDown will surely be called later to finish the close.
70 // Used to allow only one thread to enter tearDown and other threads should
71 // NOT enter it simultaneously and should end its close operation without
72 // calling tearDown to release the locks they hold to avoid deadlock.
73 private static final AtomicIntegerFieldUpdater<NetconfDeviceCommunicator> CLOSING_UPDATER =
74 AtomicIntegerFieldUpdater.newUpdater(NetconfDeviceCommunicator.class, "closing");
75 private volatile int closing;
77 public boolean isSessionClosing() {
81 public NetconfDeviceCommunicator(
82 final RemoteDeviceId id,
83 final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> remoteDevice,
84 final UserPreferences netconfSessionPreferences, final int rpcMessageLimit) {
85 this(id, remoteDevice, Optional.of(netconfSessionPreferences), rpcMessageLimit);
88 public NetconfDeviceCommunicator(
89 final RemoteDeviceId id,
90 final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> remoteDevice,
91 final int rpcMessageLimit) {
92 this(id, remoteDevice, Optional.absent(), rpcMessageLimit);
95 private NetconfDeviceCommunicator(
96 final RemoteDeviceId id,
97 final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> remoteDevice,
98 final Optional<UserPreferences> overrideNetconfCapabilities, final int rpcMessageLimit) {
99 this.concurentRpcMsgs = rpcMessageLimit;
101 this.remoteDevice = remoteDevice;
102 this.overrideNetconfCapabilities = overrideNetconfCapabilities;
103 this.firstConnectionFuture = SettableFuture.create();
104 this.semaphore = rpcMessageLimit > 0 ? new Semaphore(rpcMessageLimit) : null;
108 public void onSessionUp(final NetconfClientSession session) {
111 LOG.debug("{}: Session established", id);
112 currentSession = session;
114 NetconfSessionPreferences netconfSessionPreferences =
115 NetconfSessionPreferences.fromNetconfSession(session);
116 LOG.trace("{}: Session advertised capabilities: {}", id,
117 netconfSessionPreferences);
119 if (overrideNetconfCapabilities.isPresent()) {
120 final NetconfSessionPreferences sessionPreferences = overrideNetconfCapabilities
121 .get().getSessionPreferences();
122 netconfSessionPreferences = overrideNetconfCapabilities.get().moduleBasedCapsOverrided()
123 ? netconfSessionPreferences.replaceModuleCaps(sessionPreferences)
124 : netconfSessionPreferences.addModuleCaps(sessionPreferences);
126 netconfSessionPreferences = overrideNetconfCapabilities.get().nonModuleBasedCapsOverrided()
127 ? netconfSessionPreferences.replaceNonModuleCaps(sessionPreferences)
128 : netconfSessionPreferences.addNonModuleCaps(sessionPreferences);
129 LOG.debug("{}: Session capabilities overridden, capabilities that will be used: {}", id,
130 netconfSessionPreferences);
133 remoteDevice.onRemoteSessionUp(netconfSessionPreferences, this);
134 if (!firstConnectionFuture.isDone()) {
135 firstConnectionFuture.set(netconfSessionPreferences.getNetconfDeviceCapabilities());
138 sessionLock.unlock();
143 * Initialize remote connection.
145 * @param dispatcher {@code NetconfCLientDispatcher}
146 * @param config {@code NetconfClientConfiguration}
147 * @return future that returns succes on first succesfull connection and failure when the underlying
148 * reconnecting strategy runs out of reconnection attempts
150 public ListenableFuture<NetconfDeviceCapabilities> initializeRemoteConnection(
151 final NetconfClientDispatcher dispatcher, final NetconfClientConfiguration config) {
152 if (config instanceof NetconfReconnectingClientConfiguration) {
153 initFuture = dispatcher.createReconnectingClient((NetconfReconnectingClientConfiguration) config);
155 initFuture = dispatcher.createClient(config);
159 initFuture.addListener(future -> {
160 if (!future.isSuccess() && !future.isCancelled()) {
161 LOG.debug("{}: Connection failed", id, future.cause());
162 NetconfDeviceCommunicator.this.remoteDevice.onRemoteSessionFailed(future.cause());
163 if (firstConnectionFuture.isDone()) {
164 firstConnectionFuture.setException(future.cause());
168 return firstConnectionFuture;
171 public void disconnect() {
172 // If session is already in closing, no need to close it again
173 if (currentSession != null && startClosing() && currentSession.isUp()) {
174 currentSession.close();
178 private void tearDown(final String reason) {
179 if (!isSessionClosing()) {
180 LOG.warn("It's curious that no one to close the session but tearDown is called!");
182 LOG.debug("Tearing down {}", reason);
183 final List<UncancellableFuture<RpcResult<NetconfMessage>>> futuresToCancel = Lists.newArrayList();
186 if (currentSession != null) {
187 currentSession = null;
189 * Walk all requests, check if they have been executing
190 * or cancelled and remove them from the queue.
192 final Iterator<Request> it = requests.iterator();
193 while (it.hasNext()) {
194 final Request r = it.next();
195 if (r.future.isUncancellable()) {
196 futuresToCancel.add(r.future);
198 } else if (r.future.isCancelled()) {
199 // This just does some house-cleaning
204 remoteDevice.onRemoteSessionDown();
207 sessionLock.unlock();
210 // Notify pending request futures outside of the sessionLock to avoid unnecessarily
211 // blocking the caller.
212 for (final UncancellableFuture<RpcResult<NetconfMessage>> future : futuresToCancel) {
213 if (Strings.isNullOrEmpty(reason)) {
214 future.set(createSessionDownRpcResult());
216 future.set(createErrorRpcResult(RpcError.ErrorType.TRANSPORT, reason));
223 private RpcResult<NetconfMessage> createSessionDownRpcResult() {
224 return createErrorRpcResult(RpcError.ErrorType.TRANSPORT,
225 String.format("The netconf session to %1$s is disconnected", id.getName()));
228 private static RpcResult<NetconfMessage> createErrorRpcResult(final RpcError.ErrorType errorType,
229 final String message) {
230 return RpcResultBuilder.<NetconfMessage>failed()
231 .withError(errorType, NetconfDocumentedException.ErrorTag.OPERATION_FAILED.getTagValue(), message).build();
235 public void onSessionDown(final NetconfClientSession session, final Exception exception) {
236 // If session is already in closing, no need to call tearDown again.
237 if (startClosing()) {
238 LOG.warn("{}: Session went down", id, exception);
244 public void onSessionTerminated(final NetconfClientSession session, final NetconfTerminationReason reason) {
245 // onSessionTerminated is called directly by disconnect, no need to compare and set isSessionClosing.
246 LOG.warn("{}: Session terminated {}", id, reason);
247 tearDown(reason.getErrorMessage());
251 public void close() {
252 // Cancel reconnect if in progress
253 if (initFuture != null) {
254 initFuture.cancel(false);
256 // Disconnect from device
257 // tear down not necessary, called indirectly by the close in disconnect()
262 public void onMessage(final NetconfClientSession session, final NetconfMessage message) {
264 * Dispatch between notifications and messages. Messages need to be processed
265 * with lock held, notifications do not.
267 if (isNotification(message)) {
268 processNotification(message);
270 processMessage(message);
274 private void processMessage(final NetconfMessage message) {
275 Request request = null;
279 request = requests.peek();
280 if (request != null && request.future.isUncancellable()) {
281 request = requests.poll();
282 // we have just removed one request from the queue
283 // we can also release one permit
284 if (semaphore != null) {
289 LOG.warn("{}: Ignoring unsolicited message {}", id,
293 sessionLock.unlock();
296 if (request != null) {
298 if (FailedNetconfMessage.class.isInstance(message)) {
299 request.future.set(NetconfMessageTransformUtil.toRpcResult((FailedNetconfMessage) message));
303 LOG.debug("{}: Message received {}", id, message);
305 if (LOG.isTraceEnabled()) {
306 LOG.trace("{}: Matched request: {} to response: {}", id, msgToS(request.request), msgToS(message));
310 NetconfMessageTransformUtil.checkValidReply(request.request, message);
311 } catch (final NetconfDocumentedException e) {
313 "{}: Invalid request-reply match,"
314 + "reply message contains different message-id, request: {}, response: {}",
315 id, msgToS(request.request), msgToS(message), e);
317 request.future.set(RpcResultBuilder.<NetconfMessage>failed()
318 .withRpcError(NetconfMessageTransformUtil.toRpcError(e)).build());
320 //recursively processing message to eventually find matching request
321 processMessage(message);
327 NetconfMessageTransformUtil.checkSuccessReply(message);
328 } catch (final NetconfDocumentedException e) {
330 "{}: Error reply from remote device, request: {}, response: {}",
331 id, msgToS(request.request), msgToS(message), e);
333 request.future.set(RpcResultBuilder.<NetconfMessage>failed()
334 .withRpcError(NetconfMessageTransformUtil.toRpcError(e)).build());
338 request.future.set(RpcResultBuilder.success(message).build());
342 private static String msgToS(final NetconfMessage msg) {
343 return XmlUtil.toString(msg.getDocument());
347 public FluentFuture<RpcResult<NetconfMessage>> sendRequest(final NetconfMessage message, final QName rpc) {
350 if (semaphore != null && !semaphore.tryAcquire()) {
351 LOG.warn("Limit of concurrent rpc messages was reached (limit: {}). Rpc reply message is needed. "
352 + "Discarding request of Netconf device with id {}", concurentRpcMsgs, id.getName());
353 return FluentFutures.immediateFailedFluentFuture(new NetconfDocumentedException(
354 "Limit of rpc messages was reached (Limit :" + concurentRpcMsgs
355 + ") waiting for emptying the queue of Netconf device with id" + id.getName()));
358 return sendRequestWithLock(message, rpc);
360 sessionLock.unlock();
364 private FluentFuture<RpcResult<NetconfMessage>> sendRequestWithLock(final NetconfMessage message,
366 if (LOG.isTraceEnabled()) {
367 LOG.trace("{}: Sending message {}", id, msgToS(message));
370 if (currentSession == null) {
371 LOG.warn("{}: Session is disconnected, failing RPC request {}",
373 return FluentFutures.immediateFluentFuture(createSessionDownRpcResult());
376 final Request req = new Request(new UncancellableFuture<>(true), message);
379 currentSession.sendMessage(req.request).addListener(future -> {
380 if (!future.isSuccess()) {
381 // We expect that a session down will occur at this point
382 LOG.debug("{}: Failed to send request {}", id,
383 XmlUtil.toString(req.request.getDocument()),
386 if (future.cause() != null) {
387 req.future.set(createErrorRpcResult(RpcError.ErrorType.TRANSPORT,
388 future.cause().getLocalizedMessage()));
390 req.future.set(createSessionDownRpcResult()); // assume session is down
392 req.future.setException(future.cause());
394 LOG.trace("Finished sending request {}", req.request);
401 private void processNotification(final NetconfMessage notification) {
402 if (LOG.isTraceEnabled()) {
403 LOG.trace("{}: Notification received: {}", id, notification);
406 remoteDevice.onNotification(notification);
409 private static boolean isNotification(final NetconfMessage message) {
410 if (message.getDocument() == null) {
411 // We have no message, which mean we have a FailedNetconfMessage
414 final XmlElement xmle = XmlElement.fromDomDocument(message.getDocument());
415 return XmlNetconfConstants.NOTIFICATION_ELEMENT_NAME.equals(xmle.getName()) ;
418 private static final class Request {
419 final UncancellableFuture<RpcResult<NetconfMessage>> future;
420 final NetconfMessage request;
422 private Request(final UncancellableFuture<RpcResult<NetconfMessage>> future,
423 final NetconfMessage request) {
424 this.future = future;
425 this.request = request;
429 private boolean startClosing() {
430 return CLOSING_UPDATER.compareAndSet(this, 0, 1);