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.NetconfDocumentedException;
14 import org.opendaylight.controller.netconf.api.NetconfMessage;
15 import org.opendaylight.controller.netconf.api.NetconfSession;
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;
23 import javax.xml.xpath.XPathConstants;
24 import javax.xml.xpath.XPathExpression;
25 import java.io.InputStream;
26 import java.util.Map.Entry;
28 public class SendErrorExceptionUtil {
29 private static final Logger logger = LoggerFactory.getLogger(SendErrorExceptionUtil.class);
31 public static void sendErrorMessage(final NetconfSession session,
32 final NetconfDocumentedException sendErrorException) {
33 logger.info("Sending error {}", sendErrorException.getMessage(), sendErrorException);
34 final Document errorDocument = createDocument(sendErrorException);
35 session.sendMessage(new NetconfMessage(errorDocument));
38 public static void sendErrorMessage(Channel channel, NetconfDocumentedException sendErrorException) {
39 logger.info("Sending error {}", sendErrorException.getMessage(), sendErrorException);
40 final Document errorDocument = createDocument(sendErrorException);
41 channel.writeAndFlush(new NetconfMessage(errorDocument));
44 public static void sendErrorMessage(NetconfSession session, NetconfDocumentedException sendErrorException,
45 NetconfMessage incommingMessage) {
46 final Document errorDocument = createDocument(sendErrorException);
47 logger.info("Sending error {}", XmlUtil.toString(errorDocument));
48 tryToCopyAttributes(incommingMessage.getDocument(), errorDocument, sendErrorException);
49 session.sendMessage(new NetconfMessage(errorDocument));
52 private static void tryToCopyAttributes(final Document incommingDocument, final Document errorDocument,
53 final NetconfDocumentedException sendErrorException) {
55 final Element incommingRpc = incommingDocument.getDocumentElement();
56 Preconditions.checkState(incommingRpc.getTagName().equals(XmlNetconfConstants.RPC_KEY), "Missing "
57 + XmlNetconfConstants.RPC_KEY + " " + "element");
59 final Element rpcReply = errorDocument.getDocumentElement();
60 Preconditions.checkState(rpcReply.getTagName().equals(XmlNetconfConstants.RPC_REPLY_KEY), "Missing "
61 + XmlNetconfConstants.RPC_REPLY_KEY + " element");
63 final NamedNodeMap incomingAttributes = incommingRpc.getAttributes();
64 for (int i = 0; i < incomingAttributes.getLength(); i++) {
65 final Attr attr = (Attr) incomingAttributes.item(i);
67 if (attr.getNodeName().equals(XmlUtil.XMLNS_ATTRIBUTE_KEY))
69 rpcReply.setAttributeNode((Attr) errorDocument.importNode(attr, true));
71 } catch (final Exception e) {
72 logger.warn("Unable to copy incomming attributes to {}, returned rpc-error might be invalid for client",
73 sendErrorException, e);
77 private static XPathExpression rpcErrorExpression = XMLNetconfUtil
78 .compileXPath("/netconf:rpc-reply/netconf:rpc-error");
79 private static XPathExpression errorTypeExpression = XMLNetconfUtil.compileXPath("netconf:error-type");
80 private static XPathExpression errorTagExpression = XMLNetconfUtil.compileXPath("netconf:error-tag");
81 private static XPathExpression errorSeverityExpression = XMLNetconfUtil.compileXPath("netconf:error-severity");
83 private static Document createDocument(final NetconfDocumentedException sendErrorException) {
85 final InputStream errIS = SendErrorExceptionUtil.class.getResourceAsStream("server_error.xml");
86 Document originalErrorDocument;
88 originalErrorDocument = XmlUtil.readXmlToDocument(errIS);
89 } catch (final Exception e) {
90 throw new IllegalStateException(e);
93 final Document errorDocument = XmlUtil.createDocumentCopy(originalErrorDocument);
94 final Node rootNode = errorDocument.getFirstChild();
96 final Node rpcErrorNode = (Node) XmlUtil.evaluateXPath(rpcErrorExpression, rootNode, XPathConstants.NODE);
98 final Node errorTypeNode = (Node) XmlUtil.evaluateXPath(errorTypeExpression, rpcErrorNode, XPathConstants.NODE);
99 errorTypeNode.setTextContent(sendErrorException.getErrorType().getTagValue());
101 final Node errorTagNode = (Node) XmlUtil.evaluateXPath(errorTagExpression, rpcErrorNode, XPathConstants.NODE);
102 errorTagNode.setTextContent(sendErrorException.getErrorTag().getTagValue());
104 final Node errorSeverityNode = (Node) XmlUtil.evaluateXPath(errorSeverityExpression, rpcErrorNode,
105 XPathConstants.NODE);
106 errorSeverityNode.setTextContent(sendErrorException.getErrorSeverity().getTagValue());
108 if (sendErrorException.getErrorInfo() != null && sendErrorException.getErrorInfo().isEmpty() == false) {
110 * <error-info> <bad-attribute>message-id</bad-attribute>
111 * <bad-element>rpc</bad-element> </error-info>
113 final Node errorInfoNode = errorDocument.createElementNS(
114 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, "error-info");
116 errorInfoNode.setPrefix(rootNode.getPrefix());
117 rpcErrorNode.appendChild(errorInfoNode);
118 for (final Entry<String, String> errorInfoEntry : sendErrorException.getErrorInfo().entrySet()) {
119 final Node node = errorDocument.createElementNS(
120 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, errorInfoEntry.getKey());
121 node.setTextContent(errorInfoEntry.getValue());
122 errorInfoNode.appendChild(node);
126 return errorDocument;