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 org.opendaylight.netconf.api.DocumentedException;
16 import org.opendaylight.netconf.api.NetconfMessage;
17 import org.opendaylight.netconf.api.NetconfSession;
18 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
19 import org.opendaylight.netconf.api.xml.XmlUtil;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import org.w3c.dom.Attr;
23 import org.w3c.dom.Document;
24 import org.w3c.dom.Element;
25 import org.w3c.dom.NamedNodeMap;
27 public final class SendErrorExceptionUtil {
28 private static final Logger LOG = LoggerFactory.getLogger(SendErrorExceptionUtil.class);
30 private SendErrorExceptionUtil() {}
32 public static void sendErrorMessage(final NetconfSession session,
33 final DocumentedException sendErrorException) {
34 LOG.trace("Sending error {}", sendErrorException.getMessage(), sendErrorException);
35 final Document errorDocument = createDocument(sendErrorException);
36 ChannelFuture channelFuture = session.sendMessage(new NetconfMessage(errorDocument));
37 channelFuture.addListener(new SendErrorVerifyingListener(sendErrorException));
40 public static void sendErrorMessage(final Channel channel, final DocumentedException sendErrorException) {
41 LOG.trace("Sending error {}", sendErrorException.getMessage(), sendErrorException);
42 final Document errorDocument = createDocument(sendErrorException);
43 ChannelFuture channelFuture = channel.writeAndFlush(new NetconfMessage(errorDocument));
44 channelFuture.addListener(new SendErrorVerifyingListener(sendErrorException));
47 public static void sendErrorMessage(final NetconfSession session, final DocumentedException sendErrorException,
48 final NetconfMessage incommingMessage) {
49 final Document errorDocument = createDocument(sendErrorException);
50 if (LOG.isTraceEnabled()) {
51 LOG.trace("Sending error {}", XmlUtil.toString(errorDocument));
54 tryToCopyAttributes(incommingMessage.getDocument(), errorDocument, sendErrorException);
55 ChannelFuture channelFuture = session.sendMessage(new NetconfMessage(errorDocument));
56 channelFuture.addListener(new SendErrorVerifyingListener(sendErrorException));
59 @SuppressWarnings("checkstyle:IllegalCatch")
60 private static void tryToCopyAttributes(final Document incommingDocument, final Document errorDocument,
61 final DocumentedException sendErrorException) {
63 final Element incommingRpc = incommingDocument.getDocumentElement();
64 Preconditions.checkState(
65 XmlNetconfConstants.RPC_KEY.equals(incommingRpc.getLocalName())
66 && XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0.equals(incommingRpc.getNamespaceURI()),
67 "Missing %s element", XmlNetconfConstants.RPC_KEY);
69 final Element rpcReply = errorDocument.getDocumentElement();
70 Preconditions.checkState(rpcReply.getTagName().equals(XmlNetconfConstants.RPC_REPLY_KEY),
71 "Missing %s element", XmlNetconfConstants.RPC_REPLY_KEY);
73 final NamedNodeMap incomingAttributes = incommingRpc.getAttributes();
74 for (int i = 0; i < incomingAttributes.getLength(); i++) {
75 final Attr attr = (Attr) incomingAttributes.item(i);
77 if (attr.getNodeName().equals(XmlUtil.XMLNS_ATTRIBUTE_KEY)) {
80 rpcReply.setAttributeNode((Attr) errorDocument.importNode(attr, true));
82 } catch (final Exception e) {
83 LOG.warn("Unable to copy incomming attributes to {}, returned rpc-error might be invalid for client",
84 sendErrorException, e);
88 private static Document createDocument(final DocumentedException sendErrorException) {
89 return sendErrorException.toXMLDocument();
93 * Checks if netconf error was sent successfully.
95 private static final class SendErrorVerifyingListener implements ChannelFutureListener {
96 private final DocumentedException sendErrorException;
98 SendErrorVerifyingListener(final DocumentedException sendErrorException) {
99 this.sendErrorException = sendErrorException;
103 public void operationComplete(final ChannelFuture channelFuture) {
104 Preconditions.checkState(channelFuture.isSuccess(), "Unable to send exception %s", sendErrorException,
105 channelFuture.cause());