Merge "Bug 809: Enhancements to the toaster example"
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / messages / SendErrorExceptionUtil.java
index 6dc00bb1509e97b0a485b89e90b0248a642e7a1a..fdcaa2a5b8c98616385231d5c6c8c0f7e39c0adc 100644 (file)
@@ -10,6 +10,8 @@ package org.opendaylight.controller.netconf.util.messages;
 
 import com.google.common.base.Preconditions;
 import io.netty.channel.Channel;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelFutureListener;
 import org.opendaylight.controller.netconf.api.NetconfSession;
 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
 import org.opendaylight.controller.netconf.api.NetconfMessage;
@@ -29,20 +31,24 @@ import javax.xml.xpath.XPathExpression;
 import java.io.InputStream;
 import java.util.Map.Entry;
 
-public class SendErrorExceptionUtil {
+public final class SendErrorExceptionUtil {
     private static final Logger logger = LoggerFactory.getLogger(SendErrorExceptionUtil.class);
 
+    private SendErrorExceptionUtil() {}
+
     public static void sendErrorMessage(final NetconfSession session,
             final NetconfDocumentedException sendErrorException) {
         logger.trace("Sending error {}", sendErrorException.getMessage(), sendErrorException);
         final Document errorDocument = createDocument(sendErrorException);
-        session.sendMessage(new NetconfMessage(errorDocument));
+        ChannelFuture f = session.sendMessage(new NetconfMessage(errorDocument));
+        f.addListener(new SendErrorVerifyingListener(sendErrorException));
     }
 
     public static void sendErrorMessage(Channel channel, NetconfDocumentedException sendErrorException) {
         logger.trace("Sending error {}", sendErrorException.getMessage(), sendErrorException);
         final Document errorDocument = createDocument(sendErrorException);
-        channel.writeAndFlush(new NetconfMessage(errorDocument));
+        ChannelFuture f = channel.writeAndFlush(new NetconfMessage(errorDocument));
+        f.addListener(new SendErrorVerifyingListener(sendErrorException));
     }
 
     public static void sendErrorMessage(NetconfSession session, NetconfDocumentedException sendErrorException,
@@ -50,7 +56,8 @@ public class SendErrorExceptionUtil {
         final Document errorDocument = createDocument(sendErrorException);
         logger.trace("Sending error {}", XmlUtil.toString(errorDocument));
         tryToCopyAttributes(incommingMessage.getDocument(), errorDocument, sendErrorException);
-        session.sendMessage(new NetconfMessage(errorDocument));
+        ChannelFuture f = session.sendMessage(new NetconfMessage(errorDocument));
+        f.addListener(new SendErrorVerifyingListener(sendErrorException));
     }
 
     private static void tryToCopyAttributes(final Document incommingDocument, final Document errorDocument,
@@ -68,8 +75,9 @@ public class SendErrorExceptionUtil {
             for (int i = 0; i < incomingAttributes.getLength(); i++) {
                 final Attr attr = (Attr) incomingAttributes.item(i);
                 // skip namespace
-                if (attr.getNodeName().equals(XmlUtil.XMLNS_ATTRIBUTE_KEY))
+                if (attr.getNodeName().equals(XmlUtil.XMLNS_ATTRIBUTE_KEY)) {
                     continue;
+                }
                 rpcReply.setAttributeNode((Attr) errorDocument.importNode(attr, true));
             }
         } catch (final Exception e) {
@@ -109,7 +117,7 @@ public class SendErrorExceptionUtil {
                 XPathConstants.NODE);
         errorSeverityNode.setTextContent(sendErrorException.getErrorSeverity().getTagValue());
 
-        if (sendErrorException.getErrorInfo() != null && sendErrorException.getErrorInfo().isEmpty() == false) {
+        if (sendErrorException.getErrorInfo() != null && !sendErrorException.getErrorInfo().isEmpty()) {
             /*
              * <error-info> <bad-attribute>message-id</bad-attribute>
              * <bad-element>rpc</bad-element> </error-info>
@@ -130,4 +138,20 @@ public class SendErrorExceptionUtil {
         return errorDocument;
     }
 
+    /**
+     * Checks if netconf error was sent successfully.
+     */
+    private static final class SendErrorVerifyingListener implements ChannelFutureListener {
+        private final NetconfDocumentedException sendErrorException;
+
+        public SendErrorVerifyingListener(final NetconfDocumentedException sendErrorException) {
+            this.sendErrorException = sendErrorException;
+        }
+
+        @Override
+        public void operationComplete(final ChannelFuture channelFuture) throws Exception {
+            Preconditions.checkState(channelFuture.isSuccess(), "Unable to send exception {}", sendErrorException,
+                    channelFuture.cause());
+        }
+    }
 }