Do not force toString() in NetconfClientSession
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / messages / SendErrorExceptionUtil.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.util.messages;
10
11 import com.google.common.base.Preconditions;
12
13 import io.netty.channel.Channel;
14 import io.netty.channel.ChannelFuture;
15 import io.netty.channel.ChannelFutureListener;
16
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;
28
29 public final class SendErrorExceptionUtil {
30     private static final Logger logger = LoggerFactory.getLogger(SendErrorExceptionUtil.class);
31
32     private SendErrorExceptionUtil() {}
33
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));
40     }
41
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));
47     }
48
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));
56     }
57
58     private static void tryToCopyAttributes(final Document incommingDocument, final Document errorDocument,
59             final NetconfDocumentedException sendErrorException) {
60         try {
61             final Element incommingRpc = incommingDocument.getDocumentElement();
62             Preconditions.checkState(incommingRpc.getTagName().equals(XmlNetconfConstants.RPC_KEY), "Missing %s element",
63                     XmlNetconfConstants.RPC_KEY);
64
65             final Element rpcReply = errorDocument.getDocumentElement();
66             Preconditions.checkState(rpcReply.getTagName().equals(XmlNetconfConstants.RPC_REPLY_KEY), "Missing %s element",
67                     XmlNetconfConstants.RPC_REPLY_KEY);
68
69             final NamedNodeMap incomingAttributes = incommingRpc.getAttributes();
70             for (int i = 0; i < incomingAttributes.getLength(); i++) {
71                 final Attr attr = (Attr) incomingAttributes.item(i);
72                 // skip namespace
73                 if (attr.getNodeName().equals(XmlUtil.XMLNS_ATTRIBUTE_KEY)) {
74                     continue;
75                 }
76                 rpcReply.setAttributeNode((Attr) errorDocument.importNode(attr, true));
77             }
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);
81         }
82     }
83
84     private static Document createDocument(final NetconfDocumentedException sendErrorException) {
85         return sendErrorException.toXMLDocument();
86     }
87
88     /**
89      * Checks if netconf error was sent successfully.
90      */
91     private static final class SendErrorVerifyingListener implements ChannelFutureListener {
92         private final NetconfDocumentedException sendErrorException;
93
94         public SendErrorVerifyingListener(final NetconfDocumentedException sendErrorException) {
95             this.sendErrorException = sendErrorException;
96         }
97
98         @Override
99         public void operationComplete(final ChannelFuture channelFuture) throws Exception {
100             Preconditions.checkState(channelFuture.isSuccess(), "Unable to send exception %s", sendErrorException,
101                     channelFuture.cause());
102         }
103     }
104 }