Merge "Clean up netconf-parent root pom"
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / util / NetconfMessageTransformUtil.java
index 4cc4110653118a2b684b2aa0c42e132ae0e0219f..100073ce83a95f0eaa2bf99635f2dc68323f3e6d 100644 (file)
@@ -28,6 +28,7 @@ import javax.xml.transform.dom.DOMSource;
 import org.opendaylight.controller.config.util.xml.DocumentedException;
 import org.opendaylight.controller.config.util.xml.XmlElement;
 import org.opendaylight.controller.config.util.xml.XmlUtil;
+import org.opendaylight.netconf.api.FailedNetconfMessage;
 import org.opendaylight.netconf.api.NetconfDocumentedException;
 import org.opendaylight.netconf.api.NetconfMessage;
 import org.opendaylight.netconf.notifications.NetconfNotification;
@@ -41,6 +42,7 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.not
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
+import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -65,7 +67,7 @@ import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 
-public class NetconfMessageTransformUtil {
+public final class NetconfMessageTransformUtil {
 
     private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageTransformUtil.class);
 
@@ -204,7 +206,7 @@ public class NetconfMessageTransformUtil {
         final String inputMsgId = input.getDocument().getDocumentElement().getAttribute(MESSAGE_ID_ATTR);
         final String outputMsgId = output.getDocument().getDocumentElement().getAttribute(MESSAGE_ID_ATTR);
 
-        if (inputMsgId.equals(outputMsgId) == false) {
+        if (!inputMsgId.equals(outputMsgId)) {
             final Map<String, String> errorInfo = ImmutableMap.<String, String>builder()
                     .put("actual-message-id", outputMsgId)
                     .put("expected-message-id", inputMsgId)
@@ -310,8 +312,8 @@ public class NetconfMessageTransformUtil {
         } else {
             final Entry<QName, ModifyAction> modifyOperation = operation.isPresent()
                     ? new AbstractMap.SimpleEntry<>(NETCONF_OPERATION_QNAME, operation.get()) : null;
-            configContent = ImmutableNodes
-                    .fromInstanceId(ctx, dataPath, lastChildOverride, Optional.fromNullable(modifyOperation));
+            configContent = ImmutableNodes.fromInstanceId(ctx, dataPath, lastChildOverride.toJavaUtil(),
+                java.util.Optional.ofNullable(modifyOperation));
         }
 
         final Element element = XmlUtil.createElement(BLANK_DOCUMENT, NETCONF_CONFIG_QNAME.getLocalName(),
@@ -361,7 +363,8 @@ public class NetconfMessageTransformUtil {
                     NetconfNotification.RFC3339_DATE_PARSER.apply(eventTimeElement.getTextContent()),
                     notificationElement);
         } catch (final DocumentedException e) {
-            throw new IllegalArgumentException("Notification payload does not contain " + EVENT_TIME + " " + message);
+            throw new IllegalArgumentException("Notification payload does not contain " + EVENT_TIME + " " + message,
+                    e);
         } catch (final DateTimeParseException e) {
             LOG.warn("Unable to parse event time from {}. Setting time to {}", eventTimeElement,
                     NetconfNotification.UNKNOWN_EVENT_TIME, e);
@@ -404,4 +407,16 @@ public 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,
+                                        DocumentedException.ErrorSeverity.ERROR)))
+                .build();
+    }
 }