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 ca3079bb16ce0a6ce2a3156064b275f0d0d23a80..9097da4d91d284c0eb53a3285295ab27724cd65a 100644 (file)
@@ -8,12 +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 io.netty.buffer.Unpooled;
-import io.netty.channel.ChannelHandler;
-import io.netty.handler.codec.DelimiterBasedFrameDecoder;
+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;
@@ -27,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.
@@ -39,14 +37,10 @@ import java.util.List;
 public final class NetconfMessageFactory implements ProtocolMessageFactory<NetconfMessage> {
 
     private static final Logger logger = LoggerFactory.getLogger(NetconfMessageFactory.class);
-
-    public static final byte[] endOfMessage = "]]>]]>".getBytes(Charsets.UTF_8);
-
-    public static final byte[] endOfChunk = "\n##\n".getBytes(Charsets.UTF_8);
-
-    private static final int MAX_CHUNK_SIZE = 1024; // Bytes
-
-    private FramingMechanism framing = FramingMechanism.EOM;
+    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;
 
@@ -58,30 +52,64 @@ public final class NetconfMessageFactory implements ProtocolMessageFactory<Netco
         this.clientId = clientId;
     }
 
-    public static ChannelHandler getDelimiterFrameDecoder() {
-        return new DelimiterBasedFrameDecoder(Integer.MAX_VALUE, Unpooled.copiedBuffer(endOfMessage));
-    }
-
     @Override
-    public List<NetconfMessage> parse(byte[] bytes) throws DeserializerException, DocumentedException {
-        String s = Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString();
-        logger.debug("Parsing message \n{}", s);
-        if (bytes[0] == '[') {
-            // yuma sends auth information in the first line. Ignore until ]\n
-            // is found.
-            int endOfAuthHeader = ByteArray.findByteSequence(bytes, new byte[] { ']', '\n' });
+    public NetconfMessage parse(byte[] bytes) throws DeserializerException, DocumentedException {
+        logMessage(bytes);
+
+        String additionalHeader = null;
+
+        if (startsWithAdditionalHeader(bytes)) {
+            // Auth information containing username, ip address... extracted for monitoring
+            int endOfAuthHeader = getAdditionalHeaderEndIndex(bytes);
             if (endOfAuthHeader > -1) {
+                byte[] additionalHeaderBytes = Arrays.copyOfRange(bytes, 0, endOfAuthHeader + 2);
+                additionalHeader = additionalHeaderToString(additionalHeaderBytes);
                 bytes = Arrays.copyOfRange(bytes, endOfAuthHeader + 2, bytes.length);
             }
         }
-        List<NetconfMessage> messages = Lists.newArrayList();
+        NetconfMessage message;
         try {
             Document doc = XmlUtil.readXmlToDocument(new ByteArrayInputStream(bytes));
-            messages.add(new NetconfMessage(doc));
+            message = new NetconfMessage(doc, additionalHeader);
         } catch (final SAXException | IOException | IllegalStateException e) {
             throw new NetconfDeserializerException("Could not parse message from " + new String(bytes), e);
         }
-        return messages;
+        return message;
+    }
+
+    private int getAdditionalHeaderEndIndex(byte[] bytes) {
+        for (byte[] possibleEnd : POSSIBLE_ENDS) {
+            int idx = ByteArray.findByteSequence(bytes, possibleEnd);
+
+            if (idx != -1) {
+                return idx;
+            }
+        }
+
+        return -1;
+    }
+
+    private boolean startsWithAdditionalHeader(byte[] bytes) {
+        for (byte[] possibleStart : POSSIBLE_STARTS) {
+            int i = 0;
+            for (byte b : possibleStart) {
+                if(bytes[i] != b)
+                    break;
+
+                return true;
+            }
+        }
+
+        return false;
+    };
+
+    private void logMessage(byte[] bytes) {
+        String s = Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString();
+        logger.debug("Parsing message \n{}", s);
+    }
+
+    private String additionalHeaderToString(byte[] bytes) {
+        return Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString();
     }
 
     @Override
@@ -90,52 +118,23 @@ public final class NetconfMessageFactory implements ProtocolMessageFactory<Netco
             Comment comment = netconfMessage.getDocument().createComment("clientId:" + clientId.get());
             netconfMessage.getDocument().appendChild(comment);
         }
-        byte[] bytes = (this.framing == FramingMechanism.EOM) ? this.putEOM(netconfMessage) : this
-                .putChunked(netconfMessage);
+        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);
-        return bytes;
-    }
-
-    private byte[] putEOM(NetconfMessage msg) {
-        // create byte buffer from the String XML
-        // all Netconf messages are encoded using UTF-8
-        final ByteBuffer msgBytes = Charsets.UTF_8.encode(xmlToString(msg.getDocument()));
-        final ByteBuffer result = ByteBuffer.allocate(msgBytes.limit() + endOfMessage.length);
-        result.put(msgBytes);
-        // put end of message
-        result.put(endOfMessage);
-        return result.array();
-    }
-
-    private byte[] putChunked(NetconfMessage msg) {
-        final ByteBuffer msgBytes = Charsets.UTF_8.encode(xmlToString(msg.getDocument()));
-        final NetconfMessageHeader h = new NetconfMessageHeader();
-        if (msgBytes.limit() > MAX_CHUNK_SIZE)
-            logger.warn("Netconf message too long, should be split.");
-        h.setLength(msgBytes.limit());
-        final byte[] headerBytes = h.toBytes();
-        final ByteBuffer result = ByteBuffer.allocate(headerBytes.length + msgBytes.limit() + endOfChunk.length);
-        result.put(headerBytes);
-        result.put(msgBytes);
-        result.put(endOfChunk);
-        return result.array();
+        byte[] b = new byte[msgBytes.limit()];
+        msgBytes.get(b);
+        return b;
     }
 
     private String xmlToString(Document doc) {
         return XmlUtil.toString(doc, false);
     }
-
-    /**
-     * For Hello message the framing is always EOM, but the framing mechanism
-     * may change.
-     *
-     * @param fm
-     *            new framing mechanism
-     */
-    public void setFramingMechanism(final FramingMechanism fm) {
-        logger.debug("Framing mechanism changed to {}", fm);
-        this.framing = fm;
-    }
 }