Bug 8153: Enforce check-style rules for netconf - netconf-util
[netconf.git] / netconf / netconf-util / src / main / java / org / opendaylight / 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.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.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;
27
28 public final class SendErrorExceptionUtil {
29     private static final Logger LOG = LoggerFactory.getLogger(SendErrorExceptionUtil.class);
30
31     private SendErrorExceptionUtil() {}
32
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));
39     }
40
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));
46     }
47
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));
53         }
54
55         tryToCopyAttributes(incommingMessage.getDocument(), errorDocument, sendErrorException);
56         ChannelFuture channelFuture = session.sendMessage(new NetconfMessage(errorDocument));
57         channelFuture.addListener(new SendErrorVerifyingListener(sendErrorException));
58     }
59
60     @SuppressWarnings("checkstyle:IllegalCatch")
61     private static void tryToCopyAttributes(final Document incommingDocument, final Document errorDocument,
62             final DocumentedException sendErrorException) {
63         try {
64             final Element incommingRpc = incommingDocument.getDocumentElement();
65             Preconditions.checkState(incommingRpc.getTagName().equals(XmlNetconfConstants.RPC_KEY),
66                     "Missing %s element", XmlNetconfConstants.RPC_KEY);
67
68             final Element rpcReply = errorDocument.getDocumentElement();
69             Preconditions.checkState(rpcReply.getTagName().equals(XmlMappingConstants.RPC_REPLY_KEY),
70                     "Missing %s element", XmlMappingConstants.RPC_REPLY_KEY);
71
72             final NamedNodeMap incomingAttributes = incommingRpc.getAttributes();
73             for (int i = 0; i < incomingAttributes.getLength(); i++) {
74                 final Attr attr = (Attr) incomingAttributes.item(i);
75                 // skip namespace
76                 if (attr.getNodeName().equals(XmlUtil.XMLNS_ATTRIBUTE_KEY)) {
77                     continue;
78                 }
79                 rpcReply.setAttributeNode((Attr) errorDocument.importNode(attr, true));
80             }
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);
84         }
85     }
86
87     private static Document createDocument(final DocumentedException sendErrorException) {
88         return sendErrorException.toXMLDocument();
89     }
90
91     /**
92      * Checks if netconf error was sent successfully.
93      */
94     private static final class SendErrorVerifyingListener implements ChannelFutureListener {
95         private final DocumentedException sendErrorException;
96
97         SendErrorVerifyingListener(final DocumentedException sendErrorException) {
98             this.sendErrorException = sendErrorException;
99         }
100
101         @Override
102         public void operationComplete(final ChannelFuture channelFuture) throws Exception {
103             Preconditions.checkState(channelFuture.isSuccess(), "Unable to send exception %s", sendErrorException,
104                     channelFuture.cause());
105         }
106     }
107 }