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.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 javax.xml.XMLConstants;
16 import org.opendaylight.netconf.api.DocumentedException;
17 import org.opendaylight.netconf.api.NetconfMessage;
18 import org.opendaylight.netconf.api.NetconfSession;
19 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
20 import org.opendaylight.netconf.api.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;
28 public final class SendErrorExceptionUtil {
29 private static final Logger LOG = LoggerFactory.getLogger(SendErrorExceptionUtil.class);
31 private SendErrorExceptionUtil() {}
33 public static void sendErrorMessage(final NetconfSession session,
34 final DocumentedException sendErrorException) {
35 LOG.trace("Sending error", sendErrorException);
36 final Document errorDocument = createDocument(sendErrorException);
37 ChannelFuture channelFuture = session.sendMessage(new NetconfMessage(errorDocument));
38 channelFuture.addListener(new SendErrorVerifyingListener(sendErrorException));
41 public static void sendErrorMessage(final Channel channel, final DocumentedException sendErrorException) {
42 LOG.trace("Sending error", sendErrorException);
43 final Document errorDocument = createDocument(sendErrorException);
44 ChannelFuture channelFuture = channel.writeAndFlush(new NetconfMessage(errorDocument));
45 channelFuture.addListener(new SendErrorVerifyingListener(sendErrorException));
48 public static void sendErrorMessage(final NetconfSession session, final DocumentedException sendErrorException,
49 final NetconfMessage incommingMessage) {
50 final Document errorDocument = createDocument(sendErrorException);
51 if (LOG.isTraceEnabled()) {
52 LOG.trace("Sending error {}", XmlUtil.toString(errorDocument));
55 tryToCopyAttributes(incommingMessage.getDocument(), errorDocument, sendErrorException);
56 ChannelFuture channelFuture = session.sendMessage(new NetconfMessage(errorDocument));
57 channelFuture.addListener(new SendErrorVerifyingListener(sendErrorException));
60 @SuppressWarnings("checkstyle:IllegalCatch")
61 private static void tryToCopyAttributes(final Document incommingDocument, final Document errorDocument,
62 final DocumentedException sendErrorException) {
64 final Element incommingRpc = incommingDocument.getDocumentElement();
65 Preconditions.checkState(
66 XmlNetconfConstants.RPC_KEY.equals(incommingRpc.getLocalName())
67 && XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0.equals(incommingRpc.getNamespaceURI()),
68 "Missing %s element", XmlNetconfConstants.RPC_KEY);
70 final Element rpcReply = errorDocument.getDocumentElement();
71 Preconditions.checkState(rpcReply.getTagName().equals(XmlNetconfConstants.RPC_REPLY_KEY),
72 "Missing %s element", XmlNetconfConstants.RPC_REPLY_KEY);
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(XMLConstants.XMLNS_ATTRIBUTE)) {
81 rpcReply.setAttributeNode((Attr) errorDocument.importNode(attr, true));
83 } catch (final Exception e) {
84 LOG.warn("Unable to copy incomming attributes to {}, returned rpc-error might be invalid for client",
85 sendErrorException, e);
89 private static Document createDocument(final DocumentedException sendErrorException) {
90 return sendErrorException.toXMLDocument();
94 * Checks if netconf error was sent successfully.
96 private static final class SendErrorVerifyingListener implements ChannelFutureListener {
97 private final DocumentedException sendErrorException;
99 SendErrorVerifyingListener(final DocumentedException sendErrorException) {
100 this.sendErrorException = sendErrorException;
104 public void operationComplete(final ChannelFuture channelFuture) {
105 Preconditions.checkState(channelFuture.isSuccess(), "Unable to send exception %s", sendErrorException,
106 channelFuture.cause());