Do not instantiate constant lists on each message
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / messages / NetconfMessageFactory.java
index 891d40cf74fa520be681aa73109765424f5ab8e8..9097da4d91d284c0eb53a3285295ab27724cd65a 100644 (file)
@@ -8,9 +8,12 @@
 
 package org.opendaylight.controller.netconf.util.messages;
 
-import com.google.common.base.Charsets;
-import com.google.common.base.Optional;
-import com.google.common.collect.Lists;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+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;
@@ -24,11 +27,9 @@ import org.w3c.dom.Comment;
 import org.w3c.dom.Document;
 import org.xml.sax.SAXException;
 
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.nio.ByteBuffer;
-import java.util.Arrays;
-import java.util.List;
+import com.google.common.base.Charsets;
+import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableList;
 
 /**
  * NetconfMessageFactory for (de)serializing DOM documents.
@@ -36,6 +37,10 @@ import java.util.List;
 public final class NetconfMessageFactory implements ProtocolMessageFactory<NetconfMessage> {
 
     private static final Logger logger = LoggerFactory.getLogger(NetconfMessageFactory.class);
+    private static final List<byte[]> POSSIBLE_STARTS = ImmutableList.of(
+        "[".getBytes(Charsets.UTF_8), "\r\n[".getBytes(Charsets.UTF_8), "\n[".getBytes(Charsets.UTF_8));
+    private static final List<byte[]> POSSIBLE_ENDS = ImmutableList.of(
+        "]\n".getBytes(Charsets.UTF_8), "]\r\n".getBytes(Charsets.UTF_8));
 
     private final Optional<String> clientId;
 
@@ -73,8 +78,8 @@ public final class NetconfMessageFactory implements ProtocolMessageFactory<Netco
     }
 
     private int getAdditionalHeaderEndIndex(byte[] bytes) {
-        for (String possibleEnd : Lists.newArrayList("]\n", "]\r\n")) {
-            int idx = ByteArray.findByteSequence(bytes, possibleEnd.getBytes(Charsets.UTF_8));
+        for (byte[] possibleEnd : POSSIBLE_ENDS) {
+            int idx = ByteArray.findByteSequence(bytes, possibleEnd);
 
             if (idx != -1) {
                 return idx;
@@ -85,11 +90,10 @@ public final class NetconfMessageFactory implements ProtocolMessageFactory<Netco
     }
 
     private boolean startsWithAdditionalHeader(byte[] bytes) {
-        List<String> possibleStarts = Lists.newArrayList("[", "\r\n[", "\n[");
-        for (String possibleStart : possibleStarts) {
+        for (byte[] possibleStart : POSSIBLE_STARTS) {
             int i = 0;
-            for (byte b : possibleStart.getBytes(Charsets.UTF_8)) {
-                if(bytes[i]!=b)
+            for (byte b : possibleStart) {
+                if(bytes[i] != b)
                     break;
 
                 return true;
@@ -114,7 +118,14 @@ public final class NetconfMessageFactory implements ProtocolMessageFactory<Netco
             Comment comment = netconfMessage.getDocument().createComment("clientId:" + clientId.get());
             netconfMessage.getDocument().appendChild(comment);
         }
-        final ByteBuffer msgBytes = Charsets.UTF_8.encode(xmlToString(netconfMessage.getDocument()));
+        ByteBuffer msgBytes;
+        if(netconfMessage.getAdditionalHeader().isPresent()) {
+            String header = netconfMessage.getAdditionalHeader().get();
+            logger.trace("Header of netconf message parsed \n{}", header);
+            msgBytes = Charsets.UTF_8.encode(header + xmlToString(netconfMessage.getDocument()));
+        } else {
+            msgBytes = Charsets.UTF_8.encode(xmlToString(netconfMessage.getDocument()));
+        }
         String content = xmlToString(netconfMessage.getDocument());
 
         logger.trace("Putting message \n{}", content);