Improve NETCONF encoding performance 80/5380/3
authorRobert Varga <rovarga@cisco.com>
Sat, 15 Feb 2014 17:24:21 +0000 (18:24 +0100)
committerGerrit Code Review <gerrit@opendaylight.org>
Tue, 18 Feb 2014 11:08:12 +0000 (11:08 +0000)
This changes the protocol encoder such that it is piped directly into
the output buffer rather than constructing intermediate buffers and
strings.

Change-Id: I4938b2fd72b2321d21617c4e9c8a4d491778b657
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/NetconfHelloMessageToXMLEncoder.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/NetconfMessageToXMLEncoder.java

index a87d175..65d5b27 100644 (file)
@@ -7,12 +7,18 @@
  */
 package org.opendaylight.controller.netconf.util.handler;
 
-import java.nio.ByteBuffer;
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+
+import java.io.IOException;
+
+import javax.xml.transform.TransformerException;
 
 import org.opendaylight.controller.netconf.api.NetconfMessage;
 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Charsets;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
@@ -39,9 +45,9 @@ import com.google.common.base.Preconditions;
  * </pre>
  */
 public final class NetconfHelloMessageToXMLEncoder extends NetconfMessageToXMLEncoder {
-
     @Override
-    protected ByteBuffer encodeMessage(NetconfMessage msg) {
+    @VisibleForTesting
+    public void encode(ChannelHandlerContext ctx, NetconfMessage msg, ByteBuf out) throws IOException, TransformerException {
         Preconditions.checkState(msg instanceof NetconfHelloMessage, "Netconf message of type %s expected, was %s",
                 NetconfHelloMessage.class, msg.getClass());
         Optional<NetconfHelloMessageAdditionalHeader> headerOptional = ((NetconfHelloMessage) msg)
@@ -50,15 +56,9 @@ public final class NetconfHelloMessageToXMLEncoder extends NetconfMessageToXMLEn
         // If additional header present, serialize it along with netconf hello
         // message
         if (headerOptional.isPresent()) {
-            byte[] bytesFromHeader = headerOptional.get().toFormattedString().getBytes(Charsets.UTF_8);
-            byte[] bytesFromMessage = xmlToString(msg.getDocument()).getBytes(Charsets.UTF_8);
-
-            ByteBuffer byteBuffer = ByteBuffer.allocate(bytesFromHeader.length + bytesFromMessage.length)
-                    .put(bytesFromHeader).put(bytesFromMessage);
-            byteBuffer.flip();
-            return byteBuffer;
+            out.writeBytes(headerOptional.get().toFormattedString().getBytes(Charsets.UTF_8));
         }
 
-        return super.encodeMessage(msg);
+        super.encode(ctx, msg, out);
     }
 }
index df0f7ef..f9792a6 100644 (file)
@@ -8,24 +8,32 @@
 package org.opendaylight.controller.netconf.util.handler;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufOutputStream;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.handler.codec.MessageToByteEncoder;
 
-import java.nio.ByteBuffer;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
 
 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.Comment;
-import org.w3c.dom.Document;
 
 import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Charsets;
 import com.google.common.base.Optional;
 
 public class NetconfMessageToXMLEncoder extends MessageToByteEncoder<NetconfMessage> {
     private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageToXMLEncoder.class);
+    private static final TransformerFactory FACTORY = TransformerFactory.newInstance();
 
     private final Optional<String> clientId;
 
@@ -39,7 +47,7 @@ public class NetconfMessageToXMLEncoder extends MessageToByteEncoder<NetconfMess
 
     @Override
     @VisibleForTesting
-    public void encode(ChannelHandlerContext ctx, NetconfMessage msg, ByteBuf out) throws Exception {
+    public void encode(ChannelHandlerContext ctx, NetconfMessage msg, ByteBuf out) throws IOException, TransformerException {
         LOG.debug("Sent to encode : {}", msg);
 
         if (clientId.isPresent()) {
@@ -47,17 +55,14 @@ public class NetconfMessageToXMLEncoder extends MessageToByteEncoder<NetconfMess
             msg.getDocument().appendChild(comment);
         }
 
-        final ByteBuffer msgBytes = encodeMessage(msg);
-
-        LOG.trace("Putting message \n{}", xmlToString(msg.getDocument()));
-        out.writeBytes(msgBytes);
-    }
-
-    protected ByteBuffer encodeMessage(NetconfMessage msg) {
-        return Charsets.UTF_8.encode(xmlToString(msg.getDocument()));
-    }
+        try (OutputStream os = new ByteBufOutputStream(out)) {
+            Transformer transformer = FACTORY.newTransformer();
+            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
 
-    protected String xmlToString(Document doc) {
-        return XmlUtil.toString(doc, false);
+            StreamResult result = new StreamResult(new OutputStreamWriter(os));
+            DOMSource source = new DOMSource(msg.getDocument());
+            transformer.transform(source, result);
+        }
     }
 }

©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.