Initial implementation of netconf monitoring module according to http://tools.ietf...
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / messages / SendErrorExceptionUtil.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.util.messages;
10
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;
26
27 import javax.xml.xpath.XPathConstants;
28 import javax.xml.xpath.XPathExpression;
29 import java.io.InputStream;
30 import java.util.Map.Entry;
31
32 public class SendErrorExceptionUtil {
33     private static final Logger logger = LoggerFactory.getLogger(SendErrorExceptionUtil.class);
34
35     public static void sendErrorMessage(final NetconfSession session,
36             final NetconfDocumentedException sendErrorException) {
37         logger.info("Sending error {}", sendErrorException.getMessage(), sendErrorException);
38         final Document errorDocument = createDocument(sendErrorException);
39         session.sendMessage(new NetconfMessage(errorDocument));
40     }
41
42     public static void sendErrorMessage(Channel channel, NetconfDocumentedException sendErrorException) {
43         logger.info("Sending error {}", sendErrorException.getMessage(), sendErrorException);
44         final Document errorDocument = createDocument(sendErrorException);
45         channel.writeAndFlush(new NetconfMessage(errorDocument));
46     }
47
48     public static void sendErrorMessage(NetconfSession session, NetconfDocumentedException sendErrorException,
49             NetconfMessage incommingMessage) {
50         final Document errorDocument = createDocument(sendErrorException);
51         logger.info("Sending error {}", XmlUtil.toString(errorDocument));
52         tryToCopyAttributes(incommingMessage.getDocument(), errorDocument, sendErrorException);
53         session.sendMessage(new NetconfMessage(errorDocument));
54     }
55
56     private static void tryToCopyAttributes(final Document incommingDocument, final Document errorDocument,
57             final NetconfDocumentedException sendErrorException) {
58         try {
59             final Element incommingRpc = incommingDocument.getDocumentElement();
60             Preconditions.checkState(incommingRpc.getTagName().equals(XmlNetconfConstants.RPC_KEY), "Missing "
61                     + XmlNetconfConstants.RPC_KEY + " " + "element");
62
63             final Element rpcReply = errorDocument.getDocumentElement();
64             Preconditions.checkState(rpcReply.getTagName().equals(XmlNetconfConstants.RPC_REPLY_KEY), "Missing "
65                     + XmlNetconfConstants.RPC_REPLY_KEY + " element");
66
67             final NamedNodeMap incomingAttributes = incommingRpc.getAttributes();
68             for (int i = 0; i < incomingAttributes.getLength(); i++) {
69                 final Attr attr = (Attr) incomingAttributes.item(i);
70                 // skip namespace
71                 if (attr.getNodeName().equals(XmlUtil.XMLNS_ATTRIBUTE_KEY))
72                     continue;
73                 rpcReply.setAttributeNode((Attr) errorDocument.importNode(attr, true));
74             }
75         } catch (final Exception e) {
76             logger.warn("Unable to copy incomming attributes to {}, returned rpc-error might be invalid for client",
77                     sendErrorException, e);
78         }
79     }
80
81     private static XPathExpression rpcErrorExpression = XMLNetconfUtil
82             .compileXPath("/netconf:rpc-reply/netconf:rpc-error");
83     private static XPathExpression errorTypeExpression = XMLNetconfUtil.compileXPath("netconf:error-type");
84     private static XPathExpression errorTagExpression = XMLNetconfUtil.compileXPath("netconf:error-tag");
85     private static XPathExpression errorSeverityExpression = XMLNetconfUtil.compileXPath("netconf:error-severity");
86
87     private static Document createDocument(final NetconfDocumentedException sendErrorException) {
88
89         final InputStream errIS = SendErrorExceptionUtil.class.getResourceAsStream("server_error.xml");
90         Document originalErrorDocument;
91         try {
92             originalErrorDocument = XmlUtil.readXmlToDocument(errIS);
93         } catch (final Exception e) {
94             throw new IllegalStateException(e);
95         }
96
97         final Document errorDocument = XmlUtil.createDocumentCopy(originalErrorDocument);
98         final Node rootNode = errorDocument.getFirstChild();
99
100         final Node rpcErrorNode = (Node) XmlUtil.evaluateXPath(rpcErrorExpression, rootNode, XPathConstants.NODE);
101
102         final Node errorTypeNode = (Node) XmlUtil.evaluateXPath(errorTypeExpression, rpcErrorNode, XPathConstants.NODE);
103         errorTypeNode.setTextContent(sendErrorException.getErrorType().getTagValue());
104
105         final Node errorTagNode = (Node) XmlUtil.evaluateXPath(errorTagExpression, rpcErrorNode, XPathConstants.NODE);
106         errorTagNode.setTextContent(sendErrorException.getErrorTag().getTagValue());
107
108         final Node errorSeverityNode = (Node) XmlUtil.evaluateXPath(errorSeverityExpression, rpcErrorNode,
109                 XPathConstants.NODE);
110         errorSeverityNode.setTextContent(sendErrorException.getErrorSeverity().getTagValue());
111
112         if (sendErrorException.getErrorInfo() != null && sendErrorException.getErrorInfo().isEmpty() == false) {
113             /*
114              * <error-info> <bad-attribute>message-id</bad-attribute>
115              * <bad-element>rpc</bad-element> </error-info>
116              */
117             final Node errorInfoNode = errorDocument.createElementNS(
118                     XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, "error-info");
119
120             errorInfoNode.setPrefix(rootNode.getPrefix());
121             rpcErrorNode.appendChild(errorInfoNode);
122             for (final Entry<String, String> errorInfoEntry : sendErrorException.getErrorInfo().entrySet()) {
123                 final Node node = errorDocument.createElementNS(
124                         XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, errorInfoEntry.getKey());
125                 node.setTextContent(errorInfoEntry.getValue());
126                 errorInfoNode.appendChild(node);
127             }
128
129         }
130         return errorDocument;
131     }
132
133 }