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.controller.netconf.util.messages;
11 import com.google.common.base.Preconditions;
13 import io.netty.channel.Channel;
14 import io.netty.channel.ChannelFuture;
15 import io.netty.channel.ChannelFutureListener;
17 import org.opendaylight.controller.netconf.api.NetconfSession;
18 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
19 import org.opendaylight.controller.netconf.api.NetconfMessage;
20 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
21 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.w3c.dom.Attr;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
27 import org.w3c.dom.NamedNodeMap;
29 public final class SendErrorExceptionUtil {
30 private static final Logger logger = LoggerFactory.getLogger(SendErrorExceptionUtil.class);
32 private SendErrorExceptionUtil() {}
34 public static void sendErrorMessage(final NetconfSession session,
35 final NetconfDocumentedException sendErrorException) {
36 logger.trace("Sending error {}", sendErrorException.getMessage(), sendErrorException);
37 final Document errorDocument = createDocument(sendErrorException);
38 ChannelFuture f = session.sendMessage(new NetconfMessage(errorDocument));
39 f.addListener(new SendErrorVerifyingListener(sendErrorException));
42 public static void sendErrorMessage(Channel channel, NetconfDocumentedException sendErrorException) {
43 logger.trace("Sending error {}", sendErrorException.getMessage(), sendErrorException);
44 final Document errorDocument = createDocument(sendErrorException);
45 ChannelFuture f = channel.writeAndFlush(new NetconfMessage(errorDocument));
46 f.addListener(new SendErrorVerifyingListener(sendErrorException));
49 public static void sendErrorMessage(NetconfSession session, NetconfDocumentedException sendErrorException,
50 NetconfMessage incommingMessage) {
51 final Document errorDocument = createDocument(sendErrorException);
52 logger.trace("Sending error {}", XmlUtil.toString(errorDocument));
53 tryToCopyAttributes(incommingMessage.getDocument(), errorDocument, sendErrorException);
54 ChannelFuture f = session.sendMessage(new NetconfMessage(errorDocument));
55 f.addListener(new SendErrorVerifyingListener(sendErrorException));
58 private static void tryToCopyAttributes(final Document incommingDocument, final Document errorDocument,
59 final NetconfDocumentedException sendErrorException) {
61 final Element incommingRpc = incommingDocument.getDocumentElement();
62 Preconditions.checkState(incommingRpc.getTagName().equals(XmlNetconfConstants.RPC_KEY), "Missing "
63 + XmlNetconfConstants.RPC_KEY + " " + "element");
65 final Element rpcReply = errorDocument.getDocumentElement();
66 Preconditions.checkState(rpcReply.getTagName().equals(XmlNetconfConstants.RPC_REPLY_KEY), "Missing "
67 + XmlNetconfConstants.RPC_REPLY_KEY + " element");
69 final NamedNodeMap incomingAttributes = incommingRpc.getAttributes();
70 for (int i = 0; i < incomingAttributes.getLength(); i++) {
71 final Attr attr = (Attr) incomingAttributes.item(i);
73 if (attr.getNodeName().equals(XmlUtil.XMLNS_ATTRIBUTE_KEY)) {
76 rpcReply.setAttributeNode((Attr) errorDocument.importNode(attr, true));
78 } catch (final Exception e) {
79 logger.warn("Unable to copy incomming attributes to {}, returned rpc-error might be invalid for client",
80 sendErrorException, e);
84 private static Document createDocument(final NetconfDocumentedException sendErrorException) {
85 return sendErrorException.toXMLDocument();
89 * Checks if netconf error was sent successfully.
91 private static final class SendErrorVerifyingListener implements ChannelFutureListener {
92 private final NetconfDocumentedException sendErrorException;
94 public SendErrorVerifyingListener(final NetconfDocumentedException sendErrorException) {
95 this.sendErrorException = sendErrorException;
99 public void operationComplete(final ChannelFuture channelFuture) throws Exception {
100 Preconditions.checkState(channelFuture.isSuccess(), "Unable to send exception {}", sendErrorException,
101 channelFuture.cause());