Fix bug on ByteBufUtils method serializeList 87/47987/4
authorJozef Bacigal <jozef.bacigal@pantheon.tech>
Fri, 4 Nov 2016 12:25:21 +0000 (13:25 +0100)
committerJozef Bacigal <jozef.bacigal@pantheon.tech>
Thu, 8 Dec 2016 08:19:34 +0000 (08:19 +0000)
Also
- optimize imports
- add test to serializeList method

Change-Id: I046e62729676a476818463a5034217778b4295f7
Signed-off-by: Jozef Bacigal <jozef.bacigal@pantheon.tech>
openflowjava-util/src/main/java/org/opendaylight/openflowjava/util/ByteBufUtils.java
openflowjava-util/src/test/java/org/opendaylight/openflowjava/util/ByteBufUtilsTest.java
simple-client/src/main/java/org/opendaylight/openflowjava/protocol/impl/clients/ScenarioServiceImpl.java

index 66c4272ef9ee4a2d4089eacd34a7f3fb85c83709..6be2ee39ea80736c287e0b7bba9fa4dec1a81673 100644 (file)
@@ -15,10 +15,8 @@ import com.google.common.collect.Lists;
 import com.google.common.primitives.UnsignedBytes;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.UnpooledByteBufAllocator;
-
-import java.io.ByteArrayOutputStream;
 import java.io.IOException;
-import java.io.ObjectOutputStream;
+import java.nio.ByteBuffer;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -371,10 +369,11 @@ public abstract class ByteBufUtils {
         return IetfYangUtil.INSTANCE.macAddressFor(tmp);
     }
     
-    public static byte[] serializableList(final List<Short> list) throws IOException{
-        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
-        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
-        objectOutputStream.writeObject(list);
-        return byteArrayOutputStream.toByteArray();
+    public static byte[] serializeList(final List<Short> list) throws IOException{
+        ByteBuffer byteBuffer = ByteBuffer.allocate(list.size() * 2);
+        for (Short aShort : list) {
+            byteBuffer.putShort(aShort);
+        }
+        return byteBuffer.array();
     }
 }
index 7113eb06a5d564cf8307aff011fe917e5a51cfaa..8196e69ba095f5f035f1a557e48ef7de0d6dd333 100644 (file)
@@ -11,16 +11,14 @@ package org.opendaylight.openflowjava.util;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.PooledByteBufAllocator;
 import io.netty.buffer.UnpooledByteBufAllocator;
-
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-
 import org.junit.Assert;
 import org.junit.Test;
 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
-import org.opendaylight.openflowjava.util.ByteBufUtils;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInputBuilder;
 
@@ -30,7 +28,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731
  */
 public class ByteBufUtilsTest {
 
-    private byte[] expected = new byte[]{0x01, 0x02, 0x03, 0x04, 0x05, (byte) 0xff};
+    private final byte[] EXPECTED = new byte[]{0x01, 0x02, 0x03, 0x04, 0x05, (byte) 0xff};
+    private final byte[] EXPECTEDVALUES1AND255 = new byte[]{0x00, 0x01, 0x00, (byte) 0xff};
 
     /**
      * Test of {@link org.opendaylight.openflowjava.util.ByteBufUtils#hexStringToBytes(String)}
@@ -39,7 +38,7 @@ public class ByteBufUtilsTest {
     public void testHexStringToBytes() {
         byte[] data = ByteBufUtils.hexStringToBytes("01 02 03 04 05 ff");
 
-        Assert.assertArrayEquals(expected, data);
+        Assert.assertArrayEquals(EXPECTED, data);
     }
 
     /**
@@ -49,7 +48,7 @@ public class ByteBufUtilsTest {
     public void testHexStringToBytes2() {
         byte[] data = ByteBufUtils.hexStringToBytes("0102030405ff", false);
 
-        Assert.assertArrayEquals(expected, data);
+        Assert.assertArrayEquals(EXPECTED, data);
     }
 
     /**
@@ -59,7 +58,7 @@ public class ByteBufUtilsTest {
     public void testHexStringToByteBuf() {
         ByteBuf bb = ByteBufUtils.hexStringToByteBuf("01 02 03 04 05 ff");
 
-        Assert.assertArrayEquals(expected, byteBufToByteArray(bb));
+        Assert.assertArrayEquals(EXPECTED, byteBufToByteArray(bb));
     }
 
     /**
@@ -70,7 +69,7 @@ public class ByteBufUtilsTest {
         ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
         ByteBufUtils.hexStringToByteBuf("01 02 03 04 05 ff", buffer);
 
-        Assert.assertArrayEquals(expected, byteBufToByteArray(buffer));
+        Assert.assertArrayEquals(EXPECTED, byteBufToByteArray(buffer));
     }
 
     private static byte[] byteBufToByteArray(ByteBuf bb) {
@@ -437,4 +436,16 @@ public class ByteBufUtilsTest {
         buffer.writeShort(10);
         ipv4Address = ByteBufUtils.readIpv6Address(buffer2);
     }
+
+    @Test
+    public void testSerializeList() throws IOException {
+
+        List<Short> shorts = new ArrayList<>();
+        shorts.add((short) 1);
+        shorts.add((short) 255);
+
+        final byte[] bytes = ByteBufUtils.serializeList(shorts);
+        Assert.assertTrue(bytes.length == shorts.size()*2);
+        Assert.assertArrayEquals(EXPECTEDVALUES1AND255, bytes);
+    }
 }
index 897cc6980e396380409109f147e08c93a99040fb..9e1df907b359ea8ab409cf374e021219af2ce2e5 100644 (file)
@@ -1,20 +1,20 @@
 package org.opendaylight.openflowjava.protocol.impl.clients;
 
 import com.google.common.base.Preconditions;
-import org.opendaylight.openflowjava.util.ByteBufUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.xml.sax.SAXException;
-
+import java.io.File;
+import java.io.IOException;
+import java.util.SortedMap;
+import java.util.TreeMap;
 import javax.xml.XMLConstants;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Unmarshaller;
 import javax.xml.validation.Schema;
 import javax.xml.validation.SchemaFactory;
-import java.io.File;
-import java.io.IOException;
-import java.util.*;
+import org.opendaylight.openflowjava.util.ByteBufUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.xml.sax.SAXException;
 
 /**
  * @author Jozef Bacigal
@@ -67,12 +67,12 @@ public class ScenarioServiceImpl implements ScenarioService {
         Preconditions.checkNotNull(scenario, "Scenario name not found. Check XML file, scenario name or directories.");
         SortedMap<Integer, ClientEvent> events = new TreeMap<>();
         Integer counter = 0;
-        for (Step stepType : scenario.getStep()) {
-            LOG.debug("Step {}: {}, type {}, bytes {}", stepType.getOrder(), stepType.getName(), stepType.getEvent().value(), stepType.getBytes().toArray());
-            switch (stepType.getEvent()) {
+        for (Step step : scenario.getStep()) {
+            LOG.debug("Step {}: {}, type {}, bytes {}", step.getOrder(), step.getName(), step.getEvent().value(), step.getBytes().toArray());
+            switch (step.getEvent()) {
                 case SLEEP_EVENT: events.put(counter++, new SleepEvent(1000)); break;
-                case SEND_EVENT: events.put(counter++, new SendEvent(ByteBufUtils.serializableList(stepType.getBytes()))); break;
-                case WAIT_FOR_MESSAGE_EVENT: events.put(counter++, new WaitForMessageEvent(ByteBufUtils.serializableList(stepType.getBytes()))); break;
+                case SEND_EVENT: events.put(counter++, new SendEvent(ByteBufUtils.serializeList(step.getBytes()))); break;
+                case WAIT_FOR_MESSAGE_EVENT: events.put(counter++, new WaitForMessageEvent(ByteBufUtils.serializeList(step.getBytes()))); break;
             }
         }
         return events;