Add factory method for malformed messages 21/84821/1
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 30 Sep 2019 21:15:56 +0000 (23:15 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 30 Sep 2019 21:16:47 +0000 (23:16 +0200)
This is a utility method to make it easier for users to create nice
messages when they cannot make sense of a message.

JIRA: MDSAL-455
Change-Id: Ic8292b3d766d53b17a5e3e3dd7fd5a625e7618bc
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
dom/mdsal-dom-spi/src/main/java/org/opendaylight/mdsal/dom/spi/SimpleDOMActionResult.java

index 5e21ea3d1feff2a078b138bb19d117ae59a0930f..47593052803bd4195d5368cf34e3372887744eea 100644 (file)
@@ -16,11 +16,14 @@ import com.google.common.collect.ImmutableList;
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.util.Collection;
 import java.util.Optional;
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.mdsal.dom.api.DOMActionResult;
 import org.opendaylight.yangtools.concepts.Immutable;
 import org.opendaylight.yangtools.yang.common.RpcError;
+import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
+import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 
 @Beta
@@ -48,6 +51,12 @@ public final class SimpleDOMActionResult implements DOMActionResult, Immutable {
         this(errors, requireNonNull(output));
     }
 
+    // As per RFC7950 page 80 (top)
+    public static @NonNull SimpleDOMActionResult ofMalformedMessage(final Exception cause) {
+        return new SimpleDOMActionResult(ImmutableList.of(RpcResultBuilder.newError(ErrorType.RPC, "malformed-message",
+            cause.getMessage(), null, null, requireNonNull(cause))), null);
+    }
+
     @Override
     public Collection<RpcError> getErrors() {
         return errors;