X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Futil%2Fmessages%2FSendErrorExceptionUtil.java;h=4c4ff2fc583a0a1007ccd4b4706054c097eb2d0c;hp=a43216917befd447ec4a6aedb1b82b31934e1633;hb=25a9fb7730311a5ca298d8c6c8b24f0afb0e27be;hpb=0a23c5e6383fe72e6c5f59aa727ae2b27bb966f5 diff --git a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/messages/SendErrorExceptionUtil.java b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/messages/SendErrorExceptionUtil.java index a43216917b..4c4ff2fc58 100644 --- a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/messages/SendErrorExceptionUtil.java +++ b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/messages/SendErrorExceptionUtil.java @@ -9,12 +9,15 @@ 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; -import org.opendaylight.controller.netconf.util.xml.XMLNetconfUtil; -import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants; +import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants; import org.opendaylight.controller.netconf.util.xml.XmlUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -22,35 +25,34 @@ import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; -import org.w3c.dom.Node; - -import javax.xml.xpath.XPathConstants; -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.info("Sending error {}", sendErrorException.getMessage(), 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.info("Sending error {}", sendErrorException.getMessage(), 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, NetconfMessage incommingMessage) { final Document errorDocument = createDocument(sendErrorException); - logger.info("Sending error {}", XmlUtil.toString(errorDocument)); + 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 +70,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) { @@ -78,56 +81,24 @@ public class SendErrorExceptionUtil { } } - private static XPathExpression rpcErrorExpression = XMLNetconfUtil - .compileXPath("/netconf:rpc-reply/netconf:rpc-error"); - private static XPathExpression errorTypeExpression = XMLNetconfUtil.compileXPath("netconf:error-type"); - private static XPathExpression errorTagExpression = XMLNetconfUtil.compileXPath("netconf:error-tag"); - private static XPathExpression errorSeverityExpression = XMLNetconfUtil.compileXPath("netconf:error-severity"); - private static Document createDocument(final NetconfDocumentedException sendErrorException) { + return sendErrorException.toXMLDocument(); + } - final InputStream errIS = SendErrorExceptionUtil.class.getResourceAsStream("server_error.xml"); - Document originalErrorDocument; - try { - originalErrorDocument = XmlUtil.readXmlToDocument(errIS); - } catch (final Exception e) { - throw new IllegalStateException(e); - } - - final Document errorDocument = XmlUtil.createDocumentCopy(originalErrorDocument); - final Node rootNode = errorDocument.getFirstChild(); - - final Node rpcErrorNode = (Node) XmlUtil.evaluateXPath(rpcErrorExpression, rootNode, XPathConstants.NODE); - - final Node errorTypeNode = (Node) XmlUtil.evaluateXPath(errorTypeExpression, rpcErrorNode, XPathConstants.NODE); - errorTypeNode.setTextContent(sendErrorException.getErrorType().getTagValue()); - - final Node errorTagNode = (Node) XmlUtil.evaluateXPath(errorTagExpression, rpcErrorNode, XPathConstants.NODE); - errorTagNode.setTextContent(sendErrorException.getErrorTag().getTagValue()); - - final Node errorSeverityNode = (Node) XmlUtil.evaluateXPath(errorSeverityExpression, rpcErrorNode, - XPathConstants.NODE); - errorSeverityNode.setTextContent(sendErrorException.getErrorSeverity().getTagValue()); - - if (sendErrorException.getErrorInfo() != null && sendErrorException.getErrorInfo().isEmpty() == false) { - /* - * message-id - * rpc - */ - final Node errorInfoNode = errorDocument.createElementNS( - XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, "error-info"); + /** + * Checks if netconf error was sent successfully. + */ + private static final class SendErrorVerifyingListener implements ChannelFutureListener { + private final NetconfDocumentedException sendErrorException; - errorInfoNode.setPrefix(rootNode.getPrefix()); - rpcErrorNode.appendChild(errorInfoNode); - for (final Entry errorInfoEntry : sendErrorException.getErrorInfo().entrySet()) { - final Node node = errorDocument.createElementNS( - XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, errorInfoEntry.getKey()); - node.setTextContent(errorInfoEntry.getValue()); - errorInfoNode.appendChild(node); - } + 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()); } - return errorDocument; } - }