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;
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();
}
}
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;
*/
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)}
public void testHexStringToBytes() {
byte[] data = ByteBufUtils.hexStringToBytes("01 02 03 04 05 ff");
- Assert.assertArrayEquals(expected, data);
+ Assert.assertArrayEquals(EXPECTED, data);
}
/**
public void testHexStringToBytes2() {
byte[] data = ByteBufUtils.hexStringToBytes("0102030405ff", false);
- Assert.assertArrayEquals(expected, data);
+ Assert.assertArrayEquals(EXPECTED, data);
}
/**
public void testHexStringToByteBuf() {
ByteBuf bb = ByteBufUtils.hexStringToByteBuf("01 02 03 04 05 ff");
- Assert.assertArrayEquals(expected, byteBufToByteArray(bb));
+ Assert.assertArrayEquals(EXPECTED, byteBufToByteArray(bb));
}
/**
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) {
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);
+ }
}
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
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;