2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.netconf.util.messages;
11 import com.google.common.base.Preconditions;
12 import io.netty.channel.Channel;
13 import io.netty.channel.ChannelFuture;
14 import io.netty.channel.ChannelFutureListener;
15 import org.opendaylight.controller.netconf.api.NetconfSession;
16 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
17 import org.opendaylight.controller.netconf.api.NetconfMessage;
18 import org.opendaylight.controller.netconf.util.xml.XMLNetconfUtil;
19 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
20 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.w3c.dom.Attr;
24 import org.w3c.dom.Document;
25 import org.w3c.dom.Element;
26 import org.w3c.dom.NamedNodeMap;
27 import org.w3c.dom.Node;
29 import javax.xml.xpath.XPathConstants;
30 import javax.xml.xpath.XPathExpression;
31 import java.io.InputStream;
32 import java.util.Map.Entry;
34 public final class SendErrorExceptionUtil {
35 private static final Logger logger = LoggerFactory.getLogger(SendErrorExceptionUtil.class);
37 private SendErrorExceptionUtil() {}
39 public static void sendErrorMessage(final NetconfSession session,
40 final NetconfDocumentedException sendErrorException) {
41 logger.trace("Sending error {}", sendErrorException.getMessage(), sendErrorException);
42 final Document errorDocument = createDocument(sendErrorException);
43 ChannelFuture f = session.sendMessage(new NetconfMessage(errorDocument));
44 f.addListener(new SendErrorVerifyingListener(sendErrorException));
47 public static void sendErrorMessage(Channel channel, NetconfDocumentedException sendErrorException) {
48 logger.trace("Sending error {}", sendErrorException.getMessage(), sendErrorException);
49 final Document errorDocument = createDocument(sendErrorException);
50 ChannelFuture f = channel.writeAndFlush(new NetconfMessage(errorDocument));
51 f.addListener(new SendErrorVerifyingListener(sendErrorException));
54 public static void sendErrorMessage(NetconfSession session, NetconfDocumentedException sendErrorException,
55 NetconfMessage incommingMessage) {
56 final Document errorDocument = createDocument(sendErrorException);
57 logger.trace("Sending error {}", XmlUtil.toString(errorDocument));
58 tryToCopyAttributes(incommingMessage.getDocument(), errorDocument, sendErrorException);
59 ChannelFuture f = session.sendMessage(new NetconfMessage(errorDocument));
60 f.addListener(new SendErrorVerifyingListener(sendErrorException));
63 private static void tryToCopyAttributes(final Document incommingDocument, final Document errorDocument,
64 final NetconfDocumentedException sendErrorException) {
66 final Element incommingRpc = incommingDocument.getDocumentElement();
67 Preconditions.checkState(incommingRpc.getTagName().equals(XmlNetconfConstants.RPC_KEY), "Missing "
68 + XmlNetconfConstants.RPC_KEY + " " + "element");
70 final Element rpcReply = errorDocument.getDocumentElement();
71 Preconditions.checkState(rpcReply.getTagName().equals(XmlNetconfConstants.RPC_REPLY_KEY), "Missing "
72 + XmlNetconfConstants.RPC_REPLY_KEY + " element");
74 final NamedNodeMap incomingAttributes = incommingRpc.getAttributes();
75 for (int i = 0; i < incomingAttributes.getLength(); i++) {
76 final Attr attr = (Attr) incomingAttributes.item(i);
78 if (attr.getNodeName().equals(XmlUtil.XMLNS_ATTRIBUTE_KEY)) {
81 rpcReply.setAttributeNode((Attr) errorDocument.importNode(attr, true));
83 } catch (final Exception e) {
84 logger.warn("Unable to copy incomming attributes to {}, returned rpc-error might be invalid for client",
85 sendErrorException, e);
89 private static XPathExpression rpcErrorExpression = XMLNetconfUtil
90 .compileXPath("/netconf:rpc-reply/netconf:rpc-error");
91 private static XPathExpression errorTypeExpression = XMLNetconfUtil.compileXPath("netconf:error-type");
92 private static XPathExpression errorTagExpression = XMLNetconfUtil.compileXPath("netconf:error-tag");
93 private static XPathExpression errorSeverityExpression = XMLNetconfUtil.compileXPath("netconf:error-severity");
95 private static Document createDocument(final NetconfDocumentedException sendErrorException) {
97 final InputStream errIS = SendErrorExceptionUtil.class.getResourceAsStream("server_error.xml");
98 Document originalErrorDocument;
100 originalErrorDocument = XmlUtil.readXmlToDocument(errIS);
101 } catch (final Exception e) {
102 throw new IllegalStateException(e);
105 final Document errorDocument = XmlUtil.createDocumentCopy(originalErrorDocument);
106 final Node rootNode = errorDocument.getFirstChild();
108 final Node rpcErrorNode = (Node) XmlUtil.evaluateXPath(rpcErrorExpression, rootNode, XPathConstants.NODE);
110 final Node errorTypeNode = (Node) XmlUtil.evaluateXPath(errorTypeExpression, rpcErrorNode, XPathConstants.NODE);
111 errorTypeNode.setTextContent(sendErrorException.getErrorType().getTagValue());
113 final Node errorTagNode = (Node) XmlUtil.evaluateXPath(errorTagExpression, rpcErrorNode, XPathConstants.NODE);
114 errorTagNode.setTextContent(sendErrorException.getErrorTag().getTagValue());
116 final Node errorSeverityNode = (Node) XmlUtil.evaluateXPath(errorSeverityExpression, rpcErrorNode,
117 XPathConstants.NODE);
118 errorSeverityNode.setTextContent(sendErrorException.getErrorSeverity().getTagValue());
120 if (sendErrorException.getErrorInfo() != null && !sendErrorException.getErrorInfo().isEmpty()) {
122 * <error-info> <bad-attribute>message-id</bad-attribute>
123 * <bad-element>rpc</bad-element> </error-info>
125 final Node errorInfoNode = errorDocument.createElementNS(
126 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, "error-info");
128 errorInfoNode.setPrefix(rootNode.getPrefix());
129 rpcErrorNode.appendChild(errorInfoNode);
130 for (final Entry<String, String> errorInfoEntry : sendErrorException.getErrorInfo().entrySet()) {
131 final Node node = errorDocument.createElementNS(
132 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, errorInfoEntry.getKey());
133 node.setTextContent(errorInfoEntry.getValue());
134 errorInfoNode.appendChild(node);
138 return errorDocument;
142 * Checks if netconf error was sent successfully.
144 private static final class SendErrorVerifyingListener implements ChannelFutureListener {
145 private final NetconfDocumentedException sendErrorException;
147 public SendErrorVerifyingListener(final NetconfDocumentedException sendErrorException) {
148 this.sendErrorException = sendErrorException;
152 public void operationComplete(final ChannelFuture channelFuture) throws Exception {
153 Preconditions.checkState(channelFuture.isSuccess(), "Unable to send exception {}", sendErrorException,
154 channelFuture.cause());