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.Futures;
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.AtomicBoolean;
23 import java.util.concurrent.locks.Lock;
24 import java.util.concurrent.locks.ReentrantLock;
25 import org.opendaylight.controller.config.util.xml.XmlElement;
26 import org.opendaylight.controller.config.util.xml.XmlUtil;
27 import org.opendaylight.netconf.api.NetconfDocumentedException;
28 import org.opendaylight.netconf.api.NetconfMessage;
29 import org.opendaylight.netconf.api.NetconfTerminationReason;
30 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
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.yang.common.QName;
41 import org.opendaylight.yangtools.yang.common.RpcError;
42 import org.opendaylight.yangtools.yang.common.RpcResult;
43 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 public class NetconfDeviceCommunicator
48 implements NetconfClientSessionListener, RemoteDeviceCommunicator<NetconfMessage> {
50 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceCommunicator.class);
52 protected final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> remoteDevice;
53 private final Optional<UserPreferences> overrideNetconfCapabilities;
54 protected final RemoteDeviceId id;
55 private final Lock sessionLock = new ReentrantLock();
57 private final Semaphore semaphore;
58 private final int concurentRpcMsgs;
60 private final Queue<Request> requests = new ArrayDeque<>();
61 private NetconfClientSession session;
63 private Future<?> initFuture;
64 private final SettableFuture<NetconfDeviceCapabilities> firstConnectionFuture;
66 // isSessionClosing indicates a close operation on the session is issued and
67 // tearDown will surely be called later to finish the close.
68 // Used to allow only one thread to enter tearDown and other threads should
69 // NOT enter it simultaneously and should end its close operation without
70 // calling tearDown to release the locks they hold to avoid deadlock.
71 private final AtomicBoolean isSessionClosing = new AtomicBoolean(false);
73 public Boolean isSessionClosing() {
74 return isSessionClosing.get();
77 public NetconfDeviceCommunicator(
78 final RemoteDeviceId id,
79 final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> remoteDevice,
80 final UserPreferences netconfSessionPreferences, final int rpcMessageLimit) {
81 this(id, remoteDevice, Optional.of(netconfSessionPreferences), rpcMessageLimit);
84 public NetconfDeviceCommunicator(
85 final RemoteDeviceId id,
86 final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> remoteDevice,
87 final int rpcMessageLimit) {
88 this(id, remoteDevice, Optional.<UserPreferences>absent(), rpcMessageLimit);
91 private NetconfDeviceCommunicator(
92 final RemoteDeviceId id,
93 final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> remoteDevice,
94 final Optional<UserPreferences> overrideNetconfCapabilities, final int rpcMessageLimit) {
95 this.concurentRpcMsgs = rpcMessageLimit;
97 this.remoteDevice = remoteDevice;
98 this.overrideNetconfCapabilities = overrideNetconfCapabilities;
99 this.firstConnectionFuture = SettableFuture.create();
100 this.semaphore = rpcMessageLimit > 0 ? new Semaphore(rpcMessageLimit) : null;
104 public void onSessionUp(final NetconfClientSession session) {
107 LOG.debug("{}: Session established", id);
108 this.session = session;
110 NetconfSessionPreferences netconfSessionPreferences =
111 NetconfSessionPreferences.fromNetconfSession(session);
112 LOG.trace("{}: Session advertised capabilities: {}", id,
113 netconfSessionPreferences);
115 if (overrideNetconfCapabilities.isPresent()) {
116 final NetconfSessionPreferences sessionPreferences = overrideNetconfCapabilities
117 .get().getSessionPreferences();
118 netconfSessionPreferences = overrideNetconfCapabilities.get().moduleBasedCapsOverrided()
119 ? netconfSessionPreferences.replaceModuleCaps(sessionPreferences)
120 : netconfSessionPreferences.addModuleCaps(sessionPreferences);
122 netconfSessionPreferences = overrideNetconfCapabilities.get().nonModuleBasedCapsOverrided()
123 ? netconfSessionPreferences.replaceNonModuleCaps(sessionPreferences)
124 : netconfSessionPreferences.addNonModuleCaps(sessionPreferences);
125 LOG.debug("{}: Session capabilities overridden, capabilities that will be used: {}", id,
126 netconfSessionPreferences);
129 remoteDevice.onRemoteSessionUp(netconfSessionPreferences, this);
130 if (!firstConnectionFuture.isDone()) {
131 firstConnectionFuture.set(netconfSessionPreferences.getNetconfDeviceCapabilities());
134 sessionLock.unlock();
139 * Initialize remote connection.
141 * @param dispatcher {@code NetconfCLientDispatcher}
142 * @param config {@code NetconfClientConfiguration}
143 * @return future that returns succes on first succesfull connection and failure when the underlying
144 * reconnecting strategy runs out of reconnection attempts
146 public ListenableFuture<NetconfDeviceCapabilities> initializeRemoteConnection(
147 final NetconfClientDispatcher dispatcher, final NetconfClientConfiguration config) {
148 if (config instanceof NetconfReconnectingClientConfiguration) {
149 initFuture = dispatcher.createReconnectingClient((NetconfReconnectingClientConfiguration) config);
151 initFuture = dispatcher.createClient(config);
155 initFuture.addListener(future -> {
156 if (!future.isSuccess() && !future.isCancelled()) {
157 LOG.debug("{}: Connection failed", id, future.cause());
158 NetconfDeviceCommunicator.this.remoteDevice.onRemoteSessionFailed(future.cause());
159 if (firstConnectionFuture.isDone()) {
160 firstConnectionFuture.setException(future.cause());
164 return firstConnectionFuture;
167 public void disconnect() {
168 // If session is already in closing, no need to close it again
169 if (session != null && isSessionClosing.compareAndSet(false, true)) {
174 private void tearDown(final String reason) {
175 if (!isSessionClosing()) {
176 LOG.warn("It's curious that no one to close the session but tearDown is called!");
178 LOG.debug("Tearing down {}", reason);
179 final List<UncancellableFuture<RpcResult<NetconfMessage>>> futuresToCancel = Lists.newArrayList();
182 if (session != null) {
185 * Walk all requests, check if they have been executing
186 * or cancelled and remove them from the queue.
188 final Iterator<Request> it = requests.iterator();
189 while (it.hasNext()) {
190 final Request r = it.next();
191 if (r.future.isUncancellable()) {
192 futuresToCancel.add(r.future);
194 } else if (r.future.isCancelled()) {
195 // This just does some house-cleaning
200 remoteDevice.onRemoteSessionDown();
203 sessionLock.unlock();
206 // Notify pending request futures outside of the sessionLock to avoid unnecessarily
207 // blocking the caller.
208 for (final UncancellableFuture<RpcResult<NetconfMessage>> future : futuresToCancel) {
209 if (Strings.isNullOrEmpty(reason)) {
210 future.set(createSessionDownRpcResult());
212 future.set(createErrorRpcResult(RpcError.ErrorType.TRANSPORT, reason));
216 isSessionClosing.set(false);
219 private RpcResult<NetconfMessage> createSessionDownRpcResult() {
220 return createErrorRpcResult(RpcError.ErrorType.TRANSPORT,
221 String.format("The netconf session to %1$s is disconnected", id.getName()));
224 private static RpcResult<NetconfMessage> createErrorRpcResult(final RpcError.ErrorType errorType,
225 final String message) {
226 return RpcResultBuilder.<NetconfMessage>failed()
227 .withError(errorType, NetconfDocumentedException.ErrorTag.OPERATION_FAILED.getTagValue(), message).build();
231 public void onSessionDown(final NetconfClientSession session, final Exception exception) {
232 // If session is already in closing, no need to call tearDown again.
233 if (isSessionClosing.compareAndSet(false, true)) {
234 LOG.warn("{}: Session went down", id, exception);
240 public void onSessionTerminated(final NetconfClientSession session, final NetconfTerminationReason reason) {
241 // onSessionTerminated is called directly by disconnect, no need to compare and set isSessionClosing.
242 LOG.warn("{}: Session terminated {}", id, reason);
243 tearDown(reason.getErrorMessage());
247 public void close() {
248 // Cancel reconnect if in progress
249 if (initFuture != null) {
250 initFuture.cancel(false);
252 // Disconnect from device
253 // tear down not necessary, called indirectly by the close in disconnect()
258 public void onMessage(final NetconfClientSession session, final NetconfMessage message) {
260 * Dispatch between notifications and messages. Messages need to be processed
261 * with lock held, notifications do not.
263 if (isNotification(message)) {
264 processNotification(message);
266 processMessage(message);
270 private void processMessage(final NetconfMessage message) {
271 Request request = null;
275 request = requests.peek();
276 if (request != null && request.future.isUncancellable()) {
278 // we have just removed one request from the queue
279 // we can also release one permit
280 if (semaphore != null) {
285 LOG.warn("{}: Ignoring unsolicited message {}", id,
289 sessionLock.unlock();
292 if (request != null) {
294 LOG.debug("{}: Message received {}", id, message);
296 if (LOG.isTraceEnabled()) {
297 LOG.trace("{}: Matched request: {} to response: {}", id, msgToS(request.request), msgToS(message));
301 NetconfMessageTransformUtil.checkValidReply(request.request, message);
302 } catch (final NetconfDocumentedException e) {
304 "{}: Invalid request-reply match,"
305 + "reply message contains different message-id, request: {}, response: {}",
306 id, msgToS(request.request), msgToS(message), e);
308 request.future.set(RpcResultBuilder.<NetconfMessage>failed()
309 .withRpcError(NetconfMessageTransformUtil.toRpcError(e)).build());
311 //recursively processing message to eventually find matching request
312 processMessage(message);
318 NetconfMessageTransformUtil.checkSuccessReply(message);
319 } catch (final NetconfDocumentedException e) {
321 "{}: Error reply from remote device, request: {}, response: {}",
322 id, msgToS(request.request), msgToS(message), e);
324 request.future.set(RpcResultBuilder.<NetconfMessage>failed()
325 .withRpcError(NetconfMessageTransformUtil.toRpcError(e)).build());
329 request.future.set(RpcResultBuilder.success(message).build());
333 private static String msgToS(final NetconfMessage msg) {
334 return XmlUtil.toString(msg.getDocument());
338 public ListenableFuture<RpcResult<NetconfMessage>> sendRequest(final NetconfMessage message, final QName rpc) {
341 if (semaphore != null && !semaphore.tryAcquire()) {
342 LOG.warn("Limit of concurrent rpc messages was reached (limit :"
343 + concurentRpcMsgs + "). Rpc reply message is needed. Discarding request of Netconf device with id"
345 sessionLock.unlock();
346 return Futures.immediateFailedFuture(new NetconfDocumentedException(
347 "Limit of rpc messages was reached (Limit :" + concurentRpcMsgs
348 + ") waiting for emptying the queue of Netconf device with id" + id.getName()));
352 return sendRequestWithLock(message, rpc);
354 sessionLock.unlock();
358 private ListenableFuture<RpcResult<NetconfMessage>> sendRequestWithLock(final NetconfMessage message,
360 if (LOG.isTraceEnabled()) {
361 LOG.trace("{}: Sending message {}", id, msgToS(message));
364 if (session == null) {
365 LOG.warn("{}: Session is disconnected, failing RPC request {}",
367 return Futures.immediateFuture(createSessionDownRpcResult());
370 final Request req = new Request(new UncancellableFuture<>(true), message);
373 session.sendMessage(req.request).addListener(future -> {
374 if (!future.isSuccess()) {
375 // We expect that a session down will occur at this point
376 LOG.debug("{}: Failed to send request {}", id,
377 XmlUtil.toString(req.request.getDocument()),
380 if (future.cause() != null) {
381 req.future.set(createErrorRpcResult(RpcError.ErrorType.TRANSPORT,
382 future.cause().getLocalizedMessage()));
384 req.future.set(createSessionDownRpcResult()); // assume session is down
386 req.future.setException(future.cause());
388 LOG.trace("Finished sending request {}", req.request);
395 private void processNotification(final NetconfMessage notification) {
396 if (LOG.isTraceEnabled()) {
397 LOG.trace("{}: Notification received: {}", id, notification);
400 remoteDevice.onNotification(notification);
403 private static boolean isNotification(final NetconfMessage message) {
404 final XmlElement xmle = XmlElement.fromDomDocument(message.getDocument());
405 return XmlNetconfConstants.NOTIFICATION_ELEMENT_NAME.equals(xmle.getName()) ;
408 private static final class Request {
409 final UncancellableFuture<RpcResult<NetconfMessage>> future;
410 final NetconfMessage request;
412 private Request(final UncancellableFuture<RpcResult<NetconfMessage>> future,
413 final NetconfMessage request) {
414 this.future = future;
415 this.request = request;