Initial code drop of netconf protocol implementation
[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.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;
21 import org.w3c.dom.*;
22
23 import javax.xml.xpath.XPathConstants;
24 import javax.xml.xpath.XPathExpression;
25 import java.io.InputStream;
26 import java.util.Map.Entry;
27
28 public class SendErrorExceptionUtil {
29     private static final Logger logger = LoggerFactory.getLogger(SendErrorExceptionUtil.class);
30
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));
36     }
37
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));
42     }
43
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));
50     }
51
52     private static void tryToCopyAttributes(final Document incommingDocument, final Document errorDocument,
53             final NetconfDocumentedException sendErrorException) {
54         try {
55             final Element incommingRpc = incommingDocument.getDocumentElement();
56             Preconditions.checkState(incommingRpc.getTagName().equals(XmlNetconfConstants.RPC_KEY), "Missing "
57                     + XmlNetconfConstants.RPC_KEY + " " + "element");
58
59             final Element rpcReply = errorDocument.getDocumentElement();
60             Preconditions.checkState(rpcReply.getTagName().equals(XmlNetconfConstants.RPC_REPLY_KEY), "Missing "
61                     + XmlNetconfConstants.RPC_REPLY_KEY + " element");
62
63             final NamedNodeMap incomingAttributes = incommingRpc.getAttributes();
64             for (int i = 0; i < incomingAttributes.getLength(); i++) {
65                 final Attr attr = (Attr) incomingAttributes.item(i);
66                 // skip namespace
67                 if (attr.getNodeName().equals(XmlUtil.XMLNS_ATTRIBUTE_KEY))
68                     continue;
69                 rpcReply.setAttributeNode((Attr) errorDocument.importNode(attr, true));
70             }
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);
74         }
75     }
76
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");
82
83     private static Document createDocument(final NetconfDocumentedException sendErrorException) {
84
85         final InputStream errIS = SendErrorExceptionUtil.class.getResourceAsStream("server_error.xml");
86         Document originalErrorDocument;
87         try {
88             originalErrorDocument = XmlUtil.readXmlToDocument(errIS);
89         } catch (final Exception e) {
90             throw new IllegalStateException(e);
91         }
92
93         final Document errorDocument = XmlUtil.createDocumentCopy(originalErrorDocument);
94         final Node rootNode = errorDocument.getFirstChild();
95
96         final Node rpcErrorNode = (Node) XmlUtil.evaluateXPath(rpcErrorExpression, rootNode, XPathConstants.NODE);
97
98         final Node errorTypeNode = (Node) XmlUtil.evaluateXPath(errorTypeExpression, rpcErrorNode, XPathConstants.NODE);
99         errorTypeNode.setTextContent(sendErrorException.getErrorType().getTagValue());
100
101         final Node errorTagNode = (Node) XmlUtil.evaluateXPath(errorTagExpression, rpcErrorNode, XPathConstants.NODE);
102         errorTagNode.setTextContent(sendErrorException.getErrorTag().getTagValue());
103
104         final Node errorSeverityNode = (Node) XmlUtil.evaluateXPath(errorSeverityExpression, rpcErrorNode,
105                 XPathConstants.NODE);
106         errorSeverityNode.setTextContent(sendErrorException.getErrorSeverity().getTagValue());
107
108         if (sendErrorException.getErrorInfo() != null && sendErrorException.getErrorInfo().isEmpty() == false) {
109             /*
110              * <error-info> <bad-attribute>message-id</bad-attribute>
111              * <bad-element>rpc</bad-element> </error-info>
112              */
113             final Node errorInfoNode = errorDocument.createElementNS(
114                     XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, "error-info");
115
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);
123             }
124
125         }
126         return errorDocument;
127     }
128
129 }