Enhance Netconf message handling 74/111474/8
authorYaroslav Lastivka <yaroslav.lastivka@pantheon.tech>
Mon, 22 Apr 2024 08:55:42 +0000 (11:55 +0300)
committerRobert Varga <nite@hq.sk>
Sun, 19 May 2024 15:26:41 +0000 (15:26 +0000)
Enhance the `onMessage` method in the NetconfServerSession handler to
include detailed response handling and error logging for outgoing
Netconf messages. This update introduces a listener to the
`sendMessage` call, which logs failures to send messages and
confirms successful transmissions. The changes aim to improve
debugging capabilities by providing specific message IDs for
each failed or successful send operation, aiding in better
traceability and error handling in the communication process.

JIRA: NETCONF-1261
Change-Id: I088da6832d3ab267717ee122d53371bf05e8360c
Signed-off-by: Yaroslav Lastivka <yaroslav.lastivka@pantheon.tech>
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
protocol/netconf-server/src/main/java/org/opendaylight/netconf/server/NetconfServerSessionListener.java

index 7c4eba44484f7824f6e54901f0d730f3d390b9cd..bf533046c1f76312378cd1d620c8f1a48772f2fb 100644 (file)
@@ -30,6 +30,7 @@ import org.opendaylight.yangtools.yang.common.ErrorTag;
 import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.w3c.dom.Attr;
 import org.w3c.dom.Document;
 import org.w3c.dom.Node;
 
@@ -91,7 +92,18 @@ public class NetconfServerSessionListener implements NetconfSessionListener<Netc
             // there is no validation since the document may contain yang schemas
             final NetconfMessage message = processDocument(netconfMessage, session);
             LOG.debug("Responding with message {}", message);
-            session.sendMessage(message);
+            session.sendMessage(message).addListener(future -> {
+                final var cause = future.cause();
+                if (cause != null) {
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("Failed to send response {}", getMessageId(message), cause);
+                    }
+                    session.onOutgoingRpcError();
+                    monitoringSessionListener.onSessionEvent(SessionEvent.outRpcError(session));
+                } else if (LOG.isDebugEnabled()) {
+                    LOG.debug("Finished sending response {}", getMessageId(message));
+                }
+            });
             monitoringSessionListener.onSessionEvent(SessionEvent.inRpcSuccess(session));
         } catch (final RuntimeException e) {
             // TODO: should send generic error or close session?
@@ -109,6 +121,10 @@ public class NetconfServerSessionListener implements NetconfSessionListener<Netc
         }
     }
 
+    private static Attr getMessageId(final NetconfMessage message) {
+        return message.getDocument().getDocumentElement().getAttributeNode(XmlNetconfConstants.MESSAGE_ID);
+    }
+
     @Override
     public void onError(final NetconfServerSession session, final Exception failure) {
         session.onIncommingRpcFail();