Fix delete operation for leaf nodes
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / util / NetconfMessageTransformUtil.java
index a3d53d268f188825a5bce0d6c49d53d13e55f430..3ee435b998ac6f1e6f4bbe29741c4828e848c8f9 100644 (file)
@@ -52,6 +52,8 @@ 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.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
@@ -287,15 +289,9 @@ public final class NetconfMessageTransformUtil {
         final String outputMsgId = output.getDocument().getDocumentElement().getAttribute(MESSAGE_ID_ATTR);
 
         if (!inputMsgId.equals(outputMsgId)) {
-            final Map<String, String> errorInfo = ImmutableMap.<String, String>builder()
-                    .put("actual-message-id", outputMsgId)
-                    .put("expected-message-id", inputMsgId)
-                    .build();
-
-            throw new NetconfDocumentedException("Response message contained unknown \"message-id\"",
-                    null, NetconfDocumentedException.ErrorType.PROTOCOL,
-                    NetconfDocumentedException.ErrorTag.BAD_ATTRIBUTE,
-                    ErrorSeverity.ERROR, errorInfo);
+            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));
         }
     }
 
@@ -317,26 +313,13 @@ public final class NetconfMessageTransformUtil {
         }
 
         return ex.getErrorSeverity() == ErrorSeverity.ERROR
-                ? RpcResultBuilder.newError(toRpcErrorType(ex.getErrorType()), ex.getErrorTag().getTagValue(),
+                ? RpcResultBuilder.newError(ex.getErrorType().toLegacy(), ex.getErrorTag().elementBody(),
                         ex.getLocalizedMessage(), null, infoBuilder.toString(), ex.getCause())
                 : RpcResultBuilder.newWarning(
-                        toRpcErrorType(ex.getErrorType()), ex.getErrorTag().getTagValue(),
+                        ex.getErrorType().toLegacy(), ex.getErrorTag().elementBody(),
                         ex.getLocalizedMessage(), null, infoBuilder.toString(), ex.getCause());
     }
 
-    private static RpcError.ErrorType toRpcErrorType(final NetconfDocumentedException.ErrorType type) {
-        switch (type) {
-            case PROTOCOL:
-                return RpcError.ErrorType.PROTOCOL;
-            case RPC:
-                return RpcError.ErrorType.RPC;
-            case TRANSPORT:
-                return RpcError.ErrorType.TRANSPORT;
-            default:
-                return RpcError.ErrorType.APPLICATION;
-        }
-    }
-
     public static NodeIdentifier toId(final PathArgument qname) {
         return qname instanceof NodeIdentifier ? (NodeIdentifier) qname : toId(qname.getNodeType());
     }
@@ -364,30 +347,38 @@ public final class NetconfMessageTransformUtil {
         return Builders.containerBuilder().withNodeIdentifier(name).withValue(ImmutableList.copyOf(node)).build();
     }
 
+    /**
+     * Create edit-config structure to invoke {@code operation} with {@code lastChildOverride} data on {@code dataPath}.
+     *
+     * @param ctx {@link EffectiveModelContext} device's model context
+     * @param dataPath {@link YangInstanceIdentifier} path to data in device's data-store
+     * @param operation Optional of {@link ModifyAction} action to be invoked
+     * @param lastChildOverride Optional of {@code NormalizedNode} data on which action will be invoked
+     * @return {@link DOMSourceAnyxmlNode} containing edit-config structure
+     */
     public static DOMSourceAnyxmlNode createEditConfigAnyxml(
             final EffectiveModelContext ctx, final YangInstanceIdentifier dataPath,
             final Optional<ModifyAction> operation, final Optional<NormalizedNode> lastChildOverride) {
-        final NormalizedNode configContent;
-        final NormalizedMetadata metadata;
         if (dataPath.isEmpty()) {
             Preconditions.checkArgument(lastChildOverride.isPresent(),
                     "Data has to be present when creating structure for top level element");
             Preconditions.checkArgument(lastChildOverride.get() instanceof DataContainerChild,
                     "Data has to be either container or a list node when creating structure for top level element, "
                             + "but was: %s", lastChildOverride.get());
-            configContent = lastChildOverride.get();
-            metadata = null;
-        } else {
-            configContent = ImmutableNodes.fromInstanceId(ctx, dataPath, lastChildOverride);
-            metadata = operation.map(oper -> leafMetadata(dataPath, oper)).orElse(null);
         }
 
-        final Element element = XmlUtil.createElement(BLANK_DOCUMENT, NETCONF_CONFIG_QNAME.getLocalName(),
+        final var element = XmlUtil.createElement(BLANK_DOCUMENT, NETCONF_CONFIG_QNAME.getLocalName(),
                 Optional.of(NETCONF_CONFIG_QNAME.getNamespace().toString()));
-
+        final var metadata = operation.map(o -> leafMetadata(dataPath, o)).orElse(null);
         try {
-            NetconfUtil.writeNormalizedNode(configContent, metadata, new DOMResult(element), SchemaPath.ROOT, ctx);
-        } catch (IOException | XMLStreamException e) {
+            if (lastChildOverride.isPresent()) {
+                // FIXME remove ImmutableNodes.fromInstanceId usage
+                final var configContent = ImmutableNodes.fromInstanceId(ctx, dataPath, lastChildOverride.get());
+                NetconfUtil.writeNormalizedNode(configContent, metadata, new DOMResult(element), SchemaPath.ROOT, ctx);
+            } else {
+                NetconfUtil.writeNormalizedNode(dataPath, metadata, new DOMResult(element), SchemaPath.ROOT, ctx);
+            }
+        } catch (final IOException | XMLStreamException e) {
             throw new IllegalStateException("Unable to serialize edit config content element for path " + dataPath, e);
         }
 
@@ -561,9 +552,7 @@ public final class NetconfMessageTransformUtil {
     public static RpcResult<NetconfMessage> toRpcResult(final FailedNetconfMessage message) {
         return RpcResultBuilder.<NetconfMessage>failed()
                 .withRpcError(toRpcError(new NetconfDocumentedException(message.getException().getMessage(),
-                    DocumentedException.ErrorType.APPLICATION,
-                    DocumentedException.ErrorTag.MALFORMED_MESSAGE,
-                    ErrorSeverity.ERROR)))
+                    ErrorType.APPLICATION, ErrorTag.MALFORMED_MESSAGE, ErrorSeverity.ERROR)))
                 .build();
     }
 }