BUG-730 : more unit tests to Util.
[bgpcep.git] / util / src / main / java / org / opendaylight / protocol / util / PCEPHexDumpParser.java
index fcb17e079f133420085ca523bce0e094c2cc412a..7b6218edf6fd888ce221c13285899d3264a19a87 100644 (file)
@@ -7,86 +7,83 @@
  */
 package org.opendaylight.protocol.util;
 
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import com.google.common.io.CharStreams;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.util.List;
-
 import org.apache.commons.codec.DecoderException;
 import org.apache.commons.codec.binary.Hex;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Preconditions;
-import com.google.common.collect.Lists;
-import com.google.common.io.CharStreams;
-
 /**
  * Parses PCEP messages from a text file. Messages need to follow this formatting:
- * 
+ *
  * Received PCEP Open message. Length:28.
- * 
+ *
  * 20 01 00 1c 01 10 00 18 20 1e 78 03 00 10 00 04 00 00 00 05 00 1a 00 04 00 00 00 b4
  */
 public final class PCEPHexDumpParser {
-       private static final int MINIMAL_LENGTH = 4;
-       private static final Logger LOG = LoggerFactory.getLogger(PCEPHexDumpParser.class);
-       private static final String LENGTH = "LENGTH:";
-
-       private PCEPHexDumpParser() {
+    private static final int MINIMAL_LENGTH = 4;
+    private static final Logger LOG = LoggerFactory.getLogger(PCEPHexDumpParser.class);
+    private static final String LENGTH = "LENGTH:";
 
-       }
+    private PCEPHexDumpParser() {
 
-       public static List<byte[]> parseMessages(final File file) throws IOException {
-               Preconditions.checkArgument(file != null, "Filename cannot be null");
-               return parseMessages(new FileInputStream(file));
-       }
+    }
 
-       public static List<byte[]> parseMessages(final InputStream is) throws IOException {
-               Preconditions.checkNotNull(is);
-               try (InputStreamReader isr = new InputStreamReader(is)) {
-                       return parseMessages(CharStreams.toString(isr));
-               } finally {
-                       is.close();
-               }
-       }
+    public static List<byte[]> parseMessages(final File file) throws IOException {
+        Preconditions.checkArgument(file != null, "Filename cannot be null");
+        return parseMessages(new FileInputStream(file));
+    }
 
-       private static List<byte[]> parseMessages(final String c) {
-               final String content = clearWhiteSpaceToUpper(c);
+    public static List<byte[]> parseMessages(final InputStream is) throws IOException {
+        Preconditions.checkNotNull(is);
+        try (InputStreamReader isr = new InputStreamReader(is)) {
+            return parseMessages(CharStreams.toString(isr));
+        }
+    }
 
-               final List<byte[]> messages = Lists.newLinkedList();
-               int idx = content.indexOf(LENGTH, 0);
-               while (idx > -1) {
-                       // next chars are final length, ending with '.'
-                       final int lengthIdx = idx + LENGTH.length();
-                       final int messageIdx = content.indexOf('.', lengthIdx);
+    private static List<byte[]> parseMessages(final String c) {
+        final String content = clearWhiteSpaceToUpper(c);
 
-                       final int length = Integer.valueOf(content.substring(lengthIdx, messageIdx));
-                       final int messageEndIdx = idx + length * 2;
+        final List<byte[]> messages = Lists.newLinkedList();
+        int idx = content.indexOf(LENGTH, 0);
+        while (idx > -1) {
+            // next chars are final length, ending with '.'
+            final int lengthIdx = idx + LENGTH.length();
+            final int messageIdx = content.indexOf('.', lengthIdx);
+            final int length = Integer.parseInt(content.substring(lengthIdx, messageIdx));
+            // dot
+            final int messageEndIdx = messageIdx + (length * 2) + 1;
 
-                       // Assert that message is longer than minimum 4(header.length == 4)
-                       // If length in PCEP message would be 0, loop would never end
-                       Preconditions.checkArgument(length >= MINIMAL_LENGTH, "Invalid message at index " + idx + ", length atribute is lower than "
-                                       + MINIMAL_LENGTH);
+            // Assert that message is longer than minimum 4(header.length == 4)
+            // If length in PCEP message would be 0, loop would never end
+            Preconditions.checkArgument(length >= MINIMAL_LENGTH, "Invalid message at index " + idx + ", length atribute is lower than "
+                + MINIMAL_LENGTH);
 
-                       final String hexMessage = content.substring(idx, messageEndIdx);
-                       byte[] message = null;
-                       try {
-                               message = Hex.decodeHex(hexMessage.toCharArray());
-                       } catch (final DecoderException e) {
-                               new RuntimeException(e);
-                       }
-                       messages.add(message);
-                       idx = messageEndIdx;
-                       idx = content.indexOf(LENGTH, idx);
-               }
-               LOG.info("Succesfully extracted {} messages", messages.size());
-               return messages;
-       }
+            // dot
+            final String hexMessage = content.substring(messageIdx + 1, messageEndIdx);
+            byte[] message = null;
+            try {
+                message = Hex.decodeHex(hexMessage.toCharArray());
+            } catch (final DecoderException e) {
+                throw new IllegalArgumentException("Failed to decode message", e);
+            }
+            messages.add(message);
+            idx = messageEndIdx;
+            idx = content.indexOf(LENGTH, idx);
+        }
+        LOG.info("Succesfully extracted {} messages", messages.size());
+        return messages;
+    }
 
-       private static String clearWhiteSpaceToUpper(final String line) {
-               return line.replaceAll("\\s", "").toUpperCase();
-       }
+    private static String clearWhiteSpaceToUpper(final String line) {
+        return line.replaceAll("\\s", "").toUpperCase();
+    }
 }