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 org.opendaylight.controller.netconf.api.NetconfSession;
14 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
15 import org.opendaylight.controller.netconf.api.NetconfMessage;
16 import org.opendaylight.controller.netconf.util.xml.XMLNetconfUtil;
17 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
18 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.w3c.dom.Attr;
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Element;
24 import org.w3c.dom.NamedNodeMap;
25 import org.w3c.dom.Node;
27 import javax.xml.xpath.XPathConstants;
28 import javax.xml.xpath.XPathExpression;
29 import java.io.InputStream;
30 import java.util.Map.Entry;
32 public final class SendErrorExceptionUtil {
33 private static final Logger logger = LoggerFactory.getLogger(SendErrorExceptionUtil.class);
35 private SendErrorExceptionUtil() {}
37 public static void sendErrorMessage(final NetconfSession session,
38 final NetconfDocumentedException sendErrorException) {
39 logger.trace("Sending error {}", sendErrorException.getMessage(), sendErrorException);
40 final Document errorDocument = createDocument(sendErrorException);
41 session.sendMessage(new NetconfMessage(errorDocument));
44 public static void sendErrorMessage(Channel channel, NetconfDocumentedException sendErrorException) {
45 logger.trace("Sending error {}", sendErrorException.getMessage(), sendErrorException);
46 final Document errorDocument = createDocument(sendErrorException);
47 channel.writeAndFlush(new NetconfMessage(errorDocument));
50 public static void sendErrorMessage(NetconfSession session, NetconfDocumentedException sendErrorException,
51 NetconfMessage incommingMessage) {
52 final Document errorDocument = createDocument(sendErrorException);
53 logger.trace("Sending error {}", XmlUtil.toString(errorDocument));
54 tryToCopyAttributes(incommingMessage.getDocument(), errorDocument, sendErrorException);
55 session.sendMessage(new NetconfMessage(errorDocument));
58 private static void tryToCopyAttributes(final Document incommingDocument, final Document errorDocument,
59 final NetconfDocumentedException sendErrorException) {
61 final Element incommingRpc = incommingDocument.getDocumentElement();
62 Preconditions.checkState(incommingRpc.getTagName().equals(XmlNetconfConstants.RPC_KEY), "Missing "
63 + XmlNetconfConstants.RPC_KEY + " " + "element");
65 final Element rpcReply = errorDocument.getDocumentElement();
66 Preconditions.checkState(rpcReply.getTagName().equals(XmlNetconfConstants.RPC_REPLY_KEY), "Missing "
67 + XmlNetconfConstants.RPC_REPLY_KEY + " element");
69 final NamedNodeMap incomingAttributes = incommingRpc.getAttributes();
70 for (int i = 0; i < incomingAttributes.getLength(); i++) {
71 final Attr attr = (Attr) incomingAttributes.item(i);
73 if (attr.getNodeName().equals(XmlUtil.XMLNS_ATTRIBUTE_KEY)) {
76 rpcReply.setAttributeNode((Attr) errorDocument.importNode(attr, true));
78 } catch (final Exception e) {
79 logger.warn("Unable to copy incomming attributes to {}, returned rpc-error might be invalid for client",
80 sendErrorException, e);
84 private static XPathExpression rpcErrorExpression = XMLNetconfUtil
85 .compileXPath("/netconf:rpc-reply/netconf:rpc-error");
86 private static XPathExpression errorTypeExpression = XMLNetconfUtil.compileXPath("netconf:error-type");
87 private static XPathExpression errorTagExpression = XMLNetconfUtil.compileXPath("netconf:error-tag");
88 private static XPathExpression errorSeverityExpression = XMLNetconfUtil.compileXPath("netconf:error-severity");
90 private static Document createDocument(final NetconfDocumentedException sendErrorException) {
92 final InputStream errIS = SendErrorExceptionUtil.class.getResourceAsStream("server_error.xml");
93 Document originalErrorDocument;
95 originalErrorDocument = XmlUtil.readXmlToDocument(errIS);
96 } catch (final Exception e) {
97 throw new IllegalStateException(e);
100 final Document errorDocument = XmlUtil.createDocumentCopy(originalErrorDocument);
101 final Node rootNode = errorDocument.getFirstChild();
103 final Node rpcErrorNode = (Node) XmlUtil.evaluateXPath(rpcErrorExpression, rootNode, XPathConstants.NODE);
105 final Node errorTypeNode = (Node) XmlUtil.evaluateXPath(errorTypeExpression, rpcErrorNode, XPathConstants.NODE);
106 errorTypeNode.setTextContent(sendErrorException.getErrorType().getTagValue());
108 final Node errorTagNode = (Node) XmlUtil.evaluateXPath(errorTagExpression, rpcErrorNode, XPathConstants.NODE);
109 errorTagNode.setTextContent(sendErrorException.getErrorTag().getTagValue());
111 final Node errorSeverityNode = (Node) XmlUtil.evaluateXPath(errorSeverityExpression, rpcErrorNode,
112 XPathConstants.NODE);
113 errorSeverityNode.setTextContent(sendErrorException.getErrorSeverity().getTagValue());
115 if (sendErrorException.getErrorInfo() != null && !sendErrorException.getErrorInfo().isEmpty()) {
117 * <error-info> <bad-attribute>message-id</bad-attribute>
118 * <bad-element>rpc</bad-element> </error-info>
120 final Node errorInfoNode = errorDocument.createElementNS(
121 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, "error-info");
123 errorInfoNode.setPrefix(rootNode.getPrefix());
124 rpcErrorNode.appendChild(errorInfoNode);
125 for (final Entry<String, String> errorInfoEntry : sendErrorException.getErrorInfo().entrySet()) {
126 final Node node = errorDocument.createElementNS(
127 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, errorInfoEntry.getKey());
128 node.setTextContent(errorInfoEntry.getValue());
129 errorInfoNode.appendChild(node);
133 return errorDocument;