Eliminate NetconfMessageTransformUtil.checkValidReply() 40/105840/2
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 4 May 2023 21:02:42 +0000 (23:02 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 4 May 2023 21:16:55 +0000 (23:16 +0200)
This is method is used only by a single caller -- inline it, eliminating
a try/catch block and reducing verbosity.

JIRA: NETCONF-1006
Change-Id: Ie2193156c1d1ab65b577ac261fc401ed52256d17
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
plugins/netconf-client-mdsal/src/main/java/org/opendaylight/netconf/client/mdsal/impl/NetconfMessageTransformUtil.java
plugins/netconf-client-mdsal/src/main/java/org/opendaylight/netconf/sal/connect/netconf/listener/NetconfDeviceCommunicator.java

index 598ae0f83e35d0896e98cc62cfa502a278e25a83..4254a16184145e9388690d7cc10100afedb227ff 100644 (file)
@@ -12,7 +12,6 @@ import static org.opendaylight.netconf.common.mdsal.NormalizedDataUtil.NETCONF_Q
 
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
 import java.io.IOException;
 import java.time.Instant;
 import java.time.format.DateTimeParseException;
@@ -33,7 +32,6 @@ import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
 import org.opendaylight.netconf.api.DocumentedException;
 import org.opendaylight.netconf.api.EffectiveOperation;
-import org.opendaylight.netconf.api.NetconfDocumentedException;
 import org.opendaylight.netconf.api.NetconfMessage;
 import org.opendaylight.netconf.api.messages.NotificationMessage;
 import org.opendaylight.netconf.api.xml.XmlElement;
@@ -46,9 +44,6 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.not
 import org.opendaylight.yangtools.rfc7952.data.api.NormalizedMetadata;
 import org.opendaylight.yangtools.rfc7952.data.util.ImmutableNormalizedMetadata;
 import org.opendaylight.yangtools.rfc7952.data.util.ImmutableNormalizedMetadata.Builder;
-import org.opendaylight.yangtools.yang.common.ErrorSeverity;
-import org.opendaylight.yangtools.yang.common.ErrorTag;
-import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.XMLNamespace;
 import org.opendaylight.yangtools.yang.common.YangConstants;
@@ -264,18 +259,6 @@ public final class NetconfMessageTransformUtil {
                 .build();
     }
 
-    public static void checkValidReply(final NetconfMessage input, final NetconfMessage output)
-            throws NetconfDocumentedException {
-        final String inputMsgId = input.getDocument().getDocumentElement().getAttribute(MESSAGE_ID_ATTR);
-        final String outputMsgId = output.getDocument().getDocumentElement().getAttribute(MESSAGE_ID_ATTR);
-
-        if (!inputMsgId.equals(outputMsgId)) {
-            throw new NetconfDocumentedException("Response message contained unknown \"message-id\"", null,
-                    ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE, ErrorSeverity.ERROR,
-                    ImmutableMap.of("actual-message-id", outputMsgId, "expected-message-id", inputMsgId));
-        }
-    }
-
     public static NodeIdentifier toId(final PathArgument arg) {
         return arg instanceof NodeIdentifier nodeId ? nodeId : toId(arg.getNodeType());
     }
index 84a73617b7077545cfeb3ce747b82513efa147da..4253a409a1d9871d19b2534048849b0b2de69922 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.netconf.sal.connect.netconf.listener;
 
 import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableMap;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.SettableFuture;
@@ -24,7 +25,6 @@ import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.netconf.api.DocumentedException;
-import org.opendaylight.netconf.api.NetconfDocumentedException;
 import org.opendaylight.netconf.api.NetconfMessage;
 import org.opendaylight.netconf.api.NetconfTerminationReason;
 import org.opendaylight.netconf.api.xml.XmlElement;
@@ -280,7 +280,7 @@ public class NetconfDeviceCommunicator implements NetconfClientSessionListener,
         final Request request = pollRequest();
         if (request != null) {
             request.future.set(RpcResultBuilder.<NetconfMessage>failed()
-                .withRpcError(toRpcError(new NetconfDocumentedException(failure.getMessage(),
+                .withRpcError(toRpcError(new DocumentedException(failure.getMessage(),
                     ErrorType.APPLICATION, ErrorTag.MALFORMED_MESSAGE, ErrorSeverity.ERROR)))
                 .build());
         } else {
@@ -324,15 +324,21 @@ public class NetconfDeviceCommunicator implements NetconfClientSessionListener,
             LOG.trace("{}: Matched request: {} to response: {}", id, msgToS(request.request), msgToS(message));
         }
 
-        try {
-            NetconfMessageTransformUtil.checkValidReply(request.request, message);
-        } catch (final NetconfDocumentedException e) {
+        final String inputMsgId = request.request.getDocument().getDocumentElement()
+            .getAttribute(NetconfMessageTransformUtil.MESSAGE_ID_ATTR);
+        final String outputMsgId = message.getDocument().getDocumentElement()
+            .getAttribute(NetconfMessageTransformUtil.MESSAGE_ID_ATTR);
+        if (!inputMsgId.equals(outputMsgId)) {
+            // FIXME: we should be able to transform directly to RpcError without an intermediate exception
+            final var ex = new DocumentedException("Response message contained unknown \"message-id\"", null,
+                ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE, ErrorSeverity.ERROR,
+                ImmutableMap.of("actual-message-id", outputMsgId, "expected-message-id", inputMsgId));
             LOG.warn("{}: Invalid request-reply match, reply message contains different message-id, "
-                + "request: {}, response: {}", id, msgToS(request.request), msgToS(message), e);
+                + "request: {}, response: {}", id, msgToS(request.request), msgToS(message));
 
-            request.future.set(RpcResultBuilder.<NetconfMessage>failed().withRpcError(toRpcError(e)).build());
+            request.future.set(RpcResultBuilder.<NetconfMessage>failed().withRpcError(toRpcError(ex)).build());
 
-            //recursively processing message to eventually find matching request
+            // recursively processing message to eventually find matching request
             processMessage(message);
             return;
         }
@@ -381,7 +387,7 @@ public class NetconfDeviceCommunicator implements NetconfClientSessionListener,
             if (semaphore != null && !semaphore.tryAcquire()) {
                 LOG.warn("Limit of concurrent rpc messages was reached (limit: {}). Rpc reply message is needed. "
                     + "Discarding request of Netconf device with id: {}", concurentRpcMsgs, id.name());
-                return Futures.immediateFailedFuture(new NetconfDocumentedException(
+                return Futures.immediateFailedFuture(new DocumentedException(
                         "Limit of rpc messages was reached (Limit :" + concurentRpcMsgs
                         + ") waiting for emptying the queue of Netconf device with id: " + id.name()));
             }