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.controller.config.util.xml.DocumentedException;
16 import org.opendaylight.controller.config.util.xml.XmlMappingConstants;
17 import org.opendaylight.controller.config.util.xml.XmlUtil;
18 import org.opendaylight.netconf.api.NetconfMessage;
19 import org.opendaylight.netconf.api.NetconfSession;
20 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
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.getMessage(), 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.getMessage(), 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(incommingRpc.getTagName().equals(XmlNetconfConstants.RPC_KEY),
66 "Missing %s element", XmlNetconfConstants.RPC_KEY);
68 final Element rpcReply = errorDocument.getDocumentElement();
69 Preconditions.checkState(rpcReply.getTagName().equals(XmlMappingConstants.RPC_REPLY_KEY),
70 "Missing %s element", XmlMappingConstants.RPC_REPLY_KEY);
72 final NamedNodeMap incomingAttributes = incommingRpc.getAttributes();
73 for (int i = 0; i < incomingAttributes.getLength(); i++) {
74 final Attr attr = (Attr) incomingAttributes.item(i);
76 if (attr.getNodeName().equals(XmlUtil.XMLNS_ATTRIBUTE_KEY)) {
79 rpcReply.setAttributeNode((Attr) errorDocument.importNode(attr, true));
81 } catch (final Exception e) {
82 LOG.warn("Unable to copy incomming attributes to {}, returned rpc-error might be invalid for client",
83 sendErrorException, e);
87 private static Document createDocument(final DocumentedException sendErrorException) {
88 return sendErrorException.toXMLDocument();
92 * Checks if netconf error was sent successfully.
94 private static final class SendErrorVerifyingListener implements ChannelFutureListener {
95 private final DocumentedException sendErrorException;
97 SendErrorVerifyingListener(final DocumentedException sendErrorException) {
98 this.sendErrorException = sendErrorException;
102 public void operationComplete(final ChannelFuture channelFuture) throws Exception {
103 Preconditions.checkState(channelFuture.isSuccess(), "Unable to send exception %s", sendErrorException,
104 channelFuture.cause());