Move toRpcError/toRpcResult() 37/105837/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 4 May 2023 20:45:40 +0000 (22:45 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 4 May 2023 20:46:32 +0000 (22:46 +0200)
These two methods are only used in NetconfDeviceCommunicator, move then
there to reduce the tangle that is NetconfMessageTransformUtil.

JIRA: NETCONF-1006
Change-Id: Idd484ae56e18ebf0255e2a942a5846a49c34f63c
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 83df099525b523685ece362ea68f5b3b7baed56a..fe8573012ef2351c6e678c422fac33612ab968af 100644 (file)
@@ -51,9 +51,6 @@ 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.RpcError;
-import org.opendaylight.yangtools.yang.common.RpcResult;
-import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 import org.opendaylight.yangtools.yang.common.XMLNamespace;
 import org.opendaylight.yangtools.yang.common.YangConstants;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -286,24 +283,6 @@ public final class NetconfMessageTransformUtil {
         }
     }
 
-    public static RpcError toRpcError(final NetconfDocumentedException ex) {
-        final StringBuilder infoBuilder = new StringBuilder();
-        final Map<String, String> errorInfo = ex.getErrorInfo();
-        if (errorInfo != null) {
-            for (final Entry<String, String> e : errorInfo.entrySet()) {
-                infoBuilder.append('<').append(e.getKey()).append('>').append(e.getValue())
-                        .append("</").append(e.getKey()).append('>');
-
-            }
-        }
-
-        return ex.getErrorSeverity() == ErrorSeverity.ERROR
-                ? RpcResultBuilder.newError(ex.getErrorType(), ex.getErrorTag(),
-                        ex.getLocalizedMessage(), null, infoBuilder.toString(), ex.getCause())
-                : RpcResultBuilder.newWarning(ex.getErrorType(), ex.getErrorTag(),
-                        ex.getLocalizedMessage(), null, infoBuilder.toString(), ex.getCause());
-    }
-
     public static NodeIdentifier toId(final PathArgument arg) {
         return arg instanceof NodeIdentifier nodeId ? nodeId : toId(arg.getNodeType());
     }
@@ -539,11 +518,4 @@ public final class NetconfMessageTransformUtil {
             }
         }
     }
-
-    public static RpcResult<NetconfMessage> toRpcResult(final Exception failure) {
-        return RpcResultBuilder.<NetconfMessage>failed()
-                .withRpcError(toRpcError(new NetconfDocumentedException(failure.getMessage(),
-                    ErrorType.APPLICATION, ErrorTag.MALFORMED_MESSAGE, ErrorSeverity.ERROR)))
-                .build();
-    }
 }
index 62d8dbc3ac109ba2bc409137922b224a8aeffc7c..133956a59b19d305a46ac2b6ed6ae7255f310577 100644 (file)
@@ -40,9 +40,11 @@ import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
 import org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil;
 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
 import org.opendaylight.yangtools.yang.common.Empty;
+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.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 import org.slf4j.Logger;
@@ -275,7 +277,10 @@ public class NetconfDeviceCommunicator implements NetconfClientSessionListener,
     public void onError(final NetconfClientSession session, final Exception failure) {
         final Request request = pollRequest();
         if (request != null) {
-            request.future.set(NetconfMessageTransformUtil.toRpcResult(failure));
+            request.future.set(RpcResultBuilder.<NetconfMessage>failed()
+                .withRpcError(toRpcError(new NetconfDocumentedException(failure.getMessage(),
+                    ErrorType.APPLICATION, ErrorTag.MALFORMED_MESSAGE, ErrorSeverity.ERROR)))
+                .build());
         } else {
             LOG.warn("{}: Ignoring unsolicited failure {}", id, failure.toString());
         }
@@ -323,9 +328,7 @@ public class NetconfDeviceCommunicator implements NetconfClientSessionListener,
             LOG.warn("{}: Invalid request-reply match, reply message contains different message-id, "
                 + "request: {}, response: {}", id, msgToS(request.request), msgToS(message), e);
 
-            request.future.set(RpcResultBuilder.<NetconfMessage>failed()
-                .withRpcError(NetconfMessageTransformUtil.toRpcError(e))
-                .build());
+            request.future.set(RpcResultBuilder.<NetconfMessage>failed().withRpcError(toRpcError(e)).build());
 
             //recursively processing message to eventually find matching request
             processMessage(message);
@@ -338,9 +341,7 @@ public class NetconfDeviceCommunicator implements NetconfClientSessionListener,
             LOG.warn("{}: Error reply from remote device, request: {}, response: {}",
                 id, msgToS(request.request), msgToS(message), e);
 
-            request.future.set(RpcResultBuilder.<NetconfMessage>failed()
-                .withRpcError(NetconfMessageTransformUtil.toRpcError(e))
-                .build());
+            request.future.set(RpcResultBuilder.<NetconfMessage>failed().withRpcError(toRpcError(e)).build());
             return;
         }
 
@@ -351,6 +352,27 @@ public class NetconfDeviceCommunicator implements NetconfClientSessionListener,
         return XmlUtil.toString(msg.getDocument());
     }
 
+    private static RpcError toRpcError(final NetconfDocumentedException ex) {
+        final var errorInfo = ex.getErrorInfo();
+        final String infoString;
+        if (errorInfo != null) {
+            final var sb = new StringBuilder();
+            for (var e : errorInfo.entrySet()) {
+                final var tag = e.getKey();
+                sb.append('<').append(tag).append('>').append(e.getValue()).append("</").append(tag).append('>');
+            }
+            infoString = sb.toString();
+        } else {
+            infoString = "";
+        }
+
+        return ex.getErrorSeverity() == ErrorSeverity.ERROR
+            ? RpcResultBuilder.newError(ex.getErrorType(), ex.getErrorTag(), ex.getLocalizedMessage(), null,
+                infoString, ex.getCause())
+            : RpcResultBuilder.newWarning(ex.getErrorType(), ex.getErrorTag(), ex.getLocalizedMessage(), null,
+                infoString, ex.getCause());
+    }
+
     @Override
     public ListenableFuture<RpcResult<NetconfMessage>> sendRequest(final NetconfMessage message, final QName rpc) {
         sessionLock.lock();