X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Futil%2Fmessages%2FSendErrorExceptionUtil.java;fp=opendaylight%2Fnetconf%2Fnetconf-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Futil%2Fmessages%2FSendErrorExceptionUtil.java;h=0000000000000000000000000000000000000000;hp=a187d546514a3bfb2e5abba9af1776ddcacde0c5;hb=9ba2b4eca79bcc0e78099b133296801c8d45a6c4;hpb=b2e81149739c87f0ecc2ce7f06448d7a5d3162b8 diff --git a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/messages/SendErrorExceptionUtil.java b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/messages/SendErrorExceptionUtil.java deleted file mode 100644 index a187d54651..0000000000 --- a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/messages/SendErrorExceptionUtil.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ - -package org.opendaylight.controller.netconf.util.messages; - -import com.google.common.base.Preconditions; -import io.netty.channel.Channel; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelFutureListener; -import org.opendaylight.controller.config.util.xml.DocumentedException; -import org.opendaylight.controller.config.util.xml.XmlMappingConstants; -import org.opendaylight.controller.config.util.xml.XmlUtil; -import org.opendaylight.controller.netconf.api.NetconfMessage; -import org.opendaylight.controller.netconf.api.NetconfSession; -import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.w3c.dom.Attr; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.NamedNodeMap; - -public final class SendErrorExceptionUtil { - private static final Logger LOG = LoggerFactory.getLogger(SendErrorExceptionUtil.class); - - private SendErrorExceptionUtil() {} - - public static void sendErrorMessage(final NetconfSession session, - final DocumentedException sendErrorException) { - LOG.trace("Sending error {}", sendErrorException.getMessage(), sendErrorException); - final Document errorDocument = createDocument(sendErrorException); - ChannelFuture f = session.sendMessage(new NetconfMessage(errorDocument)); - f.addListener(new SendErrorVerifyingListener(sendErrorException)); - } - - public static void sendErrorMessage(final Channel channel, final DocumentedException sendErrorException) { - LOG.trace("Sending error {}", sendErrorException.getMessage(), sendErrorException); - final Document errorDocument = createDocument(sendErrorException); - ChannelFuture f = channel.writeAndFlush(new NetconfMessage(errorDocument)); - f.addListener(new SendErrorVerifyingListener(sendErrorException)); - } - - public static void sendErrorMessage(final NetconfSession session, final DocumentedException sendErrorException, - final NetconfMessage incommingMessage) { - final Document errorDocument = createDocument(sendErrorException); - if (LOG.isTraceEnabled()) { - LOG.trace("Sending error {}", XmlUtil.toString(errorDocument)); - } - - tryToCopyAttributes(incommingMessage.getDocument(), errorDocument, sendErrorException); - ChannelFuture f = session.sendMessage(new NetconfMessage(errorDocument)); - f.addListener(new SendErrorVerifyingListener(sendErrorException)); - } - - private static void tryToCopyAttributes(final Document incommingDocument, final Document errorDocument, - final DocumentedException sendErrorException) { - try { - final Element incommingRpc = incommingDocument.getDocumentElement(); - Preconditions.checkState(incommingRpc.getTagName().equals(XmlNetconfConstants.RPC_KEY), "Missing %s element", - XmlNetconfConstants.RPC_KEY); - - final Element rpcReply = errorDocument.getDocumentElement(); - Preconditions.checkState(rpcReply.getTagName().equals(XmlMappingConstants.RPC_REPLY_KEY), "Missing %s element", - XmlMappingConstants.RPC_REPLY_KEY); - - final NamedNodeMap incomingAttributes = incommingRpc.getAttributes(); - for (int i = 0; i < incomingAttributes.getLength(); i++) { - final Attr attr = (Attr) incomingAttributes.item(i); - // skip namespace - if (attr.getNodeName().equals(XmlUtil.XMLNS_ATTRIBUTE_KEY)) { - continue; - } - rpcReply.setAttributeNode((Attr) errorDocument.importNode(attr, true)); - } - } catch (final Exception e) { - LOG.warn("Unable to copy incomming attributes to {}, returned rpc-error might be invalid for client", - sendErrorException, e); - } - } - - private static Document createDocument(final DocumentedException sendErrorException) { - return sendErrorException.toXMLDocument(); - } - - /** - * Checks if netconf error was sent successfully. - */ - private static final class SendErrorVerifyingListener implements ChannelFutureListener { - private final DocumentedException sendErrorException; - - public SendErrorVerifyingListener(final DocumentedException sendErrorException) { - this.sendErrorException = sendErrorException; - } - - @Override - public void operationComplete(final ChannelFuture channelFuture) throws Exception { - Preconditions.checkState(channelFuture.isSuccess(), "Unable to send exception %s", sendErrorException, - channelFuture.cause()); - } - } -}