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 f = session.sendMessage(new NetconfMessage(errorDocument));
38 f.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 f = channel.writeAndFlush(new NetconfMessage(errorDocument));
45 f.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 f = session.sendMessage(new NetconfMessage(errorDocument));
57 f.addListener(new SendErrorVerifyingListener(sendErrorException));
60 private static void tryToCopyAttributes(final Document incommingDocument, final Document errorDocument,
61 final DocumentedException sendErrorException) {
63 final Element incommingRpc = incommingDocument.getDocumentElement();
64 Preconditions.checkState(incommingRpc.getTagName().equals(XmlNetconfConstants.RPC_KEY), "Missing %s element",
65 XmlNetconfConstants.RPC_KEY);
67 final Element rpcReply = errorDocument.getDocumentElement();
68 Preconditions.checkState(rpcReply.getTagName().equals(XmlMappingConstants.RPC_REPLY_KEY), "Missing %s element",
69 XmlMappingConstants.RPC_REPLY_KEY);
71 final NamedNodeMap incomingAttributes = incommingRpc.getAttributes();
72 for (int i = 0; i < incomingAttributes.getLength(); i++) {
73 final Attr attr = (Attr) incomingAttributes.item(i);
75 if (attr.getNodeName().equals(XmlUtil.XMLNS_ATTRIBUTE_KEY)) {
78 rpcReply.setAttributeNode((Attr) errorDocument.importNode(attr, true));
80 } catch (final Exception e) {
81 LOG.warn("Unable to copy incomming attributes to {}, returned rpc-error might be invalid for client",
82 sendErrorException, e);
86 private static Document createDocument(final DocumentedException sendErrorException) {
87 return sendErrorException.toXMLDocument();
91 * Checks if netconf error was sent successfully.
93 private static final class SendErrorVerifyingListener implements ChannelFutureListener {
94 private final DocumentedException sendErrorException;
96 public SendErrorVerifyingListener(final DocumentedException sendErrorException) {
97 this.sendErrorException = sendErrorException;
101 public void operationComplete(final ChannelFuture channelFuture) throws Exception {
102 Preconditions.checkState(channelFuture.isSuccess(), "Unable to send exception %s", sendErrorException,
103 channelFuture.cause());