X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Futil%2Fhandler%2FNetconfXMLToMessageDecoder.java;h=06a4dc7207204d573f5abb2cc28c32318fee891d;hb=c74edd02f5044772f226cea9da9967023f6842d6;hp=b697edfb053229fd75f602798c10150f0152b86c;hpb=2e7347fdc0eb7734ff59a4f902227a93ab6afece;p=controller.git diff --git a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/NetconfXMLToMessageDecoder.java b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/NetconfXMLToMessageDecoder.java index b697edfb05..06a4dc7207 100644 --- a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/NetconfXMLToMessageDecoder.java +++ b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/NetconfXMLToMessageDecoder.java @@ -7,73 +7,32 @@ */ package org.opendaylight.controller.netconf.util.handler; -import java.io.ByteArrayInputStream; -import java.nio.ByteBuffer; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufInputStream; +import io.netty.buffer.ByteBufUtil; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.ByteToMessageDecoder; + import java.util.List; -import org.opendaylight.controller.netconf.api.NetconfDeserializerException; import org.opendaylight.controller.netconf.api.NetconfMessage; import org.opendaylight.controller.netconf.util.xml.XmlUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.w3c.dom.Document; import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Charsets; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufUtil; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.ByteToMessageDecoder; - -public class NetconfXMLToMessageDecoder extends ByteToMessageDecoder { +public final class NetconfXMLToMessageDecoder extends ByteToMessageDecoder { private static final Logger LOG = LoggerFactory.getLogger(NetconfXMLToMessageDecoder.class); @Override @VisibleForTesting public void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { - if (in.readableBytes() == 0) { - LOG.debug("No more content in incoming buffer."); - return; - } - - in.markReaderIndex(); - try { + if (in.readableBytes() != 0) { LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in)); - byte[] bytes = new byte[in.readableBytes()]; - in.readBytes(bytes); - - logMessage(bytes); - - bytes = preprocessMessageBytes(bytes); - NetconfMessage message; - try { - Document doc = XmlUtil.readXmlToDocument(new ByteArrayInputStream(bytes)); - message = buildNetconfMessage(doc); - } catch (Exception e) { - throw new NetconfDeserializerException("Could not parse message from " + new String(bytes), e); - } - - out.add(message); - } finally { - in.discardReadBytes(); - cleanUpAfterDecode(); + out.add(new NetconfMessage(XmlUtil.readXmlToDocument(new ByteBufInputStream(in)))); + } else { + LOG.debug("No more content in incoming buffer."); } } - - protected void cleanUpAfterDecode() {} - - protected NetconfMessage buildNetconfMessage(Document doc) { - return new NetconfMessage(doc); - } - - protected byte[] preprocessMessageBytes(byte[] bytes) { - return bytes; - } - - private void logMessage(byte[] bytes) { - String s = Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString(); - LOG.debug("Parsing message \n{}", s); - } - }