Merge "Refactor DOMDataBrokerImpl"
[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 import io.netty.channel.Channel;
13 import io.netty.channel.ChannelFuture;
14 import io.netty.channel.ChannelFutureListener;
15 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
16 import org.opendaylight.controller.netconf.api.NetconfMessage;
17 import org.opendaylight.controller.netconf.api.NetconfSession;
18 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
19 import org.opendaylight.controller.netconf.util.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;
26
27 public final class SendErrorExceptionUtil {
28     private static final Logger LOG = LoggerFactory.getLogger(SendErrorExceptionUtil.class);
29
30     private SendErrorExceptionUtil() {}
31
32     public static void sendErrorMessage(final NetconfSession session,
33             final NetconfDocumentedException sendErrorException) {
34         LOG.trace("Sending error {}", sendErrorException.getMessage(), sendErrorException);
35         final Document errorDocument = createDocument(sendErrorException);
36         ChannelFuture f = session.sendMessage(new NetconfMessage(errorDocument));
37         f.addListener(new SendErrorVerifyingListener(sendErrorException));
38     }
39
40     public static void sendErrorMessage(Channel channel, NetconfDocumentedException sendErrorException) {
41         LOG.trace("Sending error {}", sendErrorException.getMessage(), sendErrorException);
42         final Document errorDocument = createDocument(sendErrorException);
43         ChannelFuture f = channel.writeAndFlush(new NetconfMessage(errorDocument));
44         f.addListener(new SendErrorVerifyingListener(sendErrorException));
45     }
46
47     public static void sendErrorMessage(NetconfSession session, NetconfDocumentedException sendErrorException,
48             NetconfMessage incommingMessage) {
49         final Document errorDocument = createDocument(sendErrorException);
50         LOG.trace("Sending error {}", XmlUtil.toString(errorDocument));
51         tryToCopyAttributes(incommingMessage.getDocument(), errorDocument, sendErrorException);
52         ChannelFuture f = session.sendMessage(new NetconfMessage(errorDocument));
53         f.addListener(new SendErrorVerifyingListener(sendErrorException));
54     }
55
56     private static void tryToCopyAttributes(final Document incommingDocument, final Document errorDocument,
57             final NetconfDocumentedException sendErrorException) {
58         try {
59             final Element incommingRpc = incommingDocument.getDocumentElement();
60             Preconditions.checkState(incommingRpc.getTagName().equals(XmlNetconfConstants.RPC_KEY), "Missing %s element",
61                     XmlNetconfConstants.RPC_KEY);
62
63             final Element rpcReply = errorDocument.getDocumentElement();
64             Preconditions.checkState(rpcReply.getTagName().equals(XmlNetconfConstants.RPC_REPLY_KEY), "Missing %s element",
65                     XmlNetconfConstants.RPC_REPLY_KEY);
66
67             final NamedNodeMap incomingAttributes = incommingRpc.getAttributes();
68             for (int i = 0; i < incomingAttributes.getLength(); i++) {
69                 final Attr attr = (Attr) incomingAttributes.item(i);
70                 // skip namespace
71                 if (attr.getNodeName().equals(XmlUtil.XMLNS_ATTRIBUTE_KEY)) {
72                     continue;
73                 }
74                 rpcReply.setAttributeNode((Attr) errorDocument.importNode(attr, true));
75             }
76         } catch (final Exception e) {
77             LOG.warn("Unable to copy incomming attributes to {}, returned rpc-error might be invalid for client",
78                     sendErrorException, e);
79         }
80     }
81
82     private static Document createDocument(final NetconfDocumentedException sendErrorException) {
83         return sendErrorException.toXMLDocument();
84     }
85
86     /**
87      * Checks if netconf error was sent successfully.
88      */
89     private static final class SendErrorVerifyingListener implements ChannelFutureListener {
90         private final NetconfDocumentedException sendErrorException;
91
92         public SendErrorVerifyingListener(final NetconfDocumentedException sendErrorException) {
93             this.sendErrorException = sendErrorException;
94         }
95
96         @Override
97         public void operationComplete(final ChannelFuture channelFuture) throws Exception {
98             Preconditions.checkState(channelFuture.isSuccess(), "Unable to send exception %s", sendErrorException,
99                     channelFuture.cause());
100         }
101     }
102 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.