* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.protocol.util;
import io.netty.buffer.ByteBuf;
-import java.math.BigInteger;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ieee754.rev130819.Float32;
-import org.opendaylight.yangtools.yang.common.Uint16;
-import org.opendaylight.yangtools.yang.common.Uint32;
-import org.opendaylight.yangtools.yang.common.Uint64;
-import org.opendaylight.yangtools.yang.common.Uint8;
/**
* Utility class for ByteBuf's write methods.
output.writeMedium(value != null ? value : 0);
}
- /**
- * Writes 16-bit short <code>value</code> if not null, otherwise writes
- * zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
- * increased by 2.
- *
- * @param value
- * Short value to be written to the output.
- * @param output
- * ByteBuf, where value or zeros are written.
- */
- public static void writeShort(final Short value, final ByteBuf output) {
- output.writeShort(value != null ? value : 0);
- }
-
- /**
- * Writes 64-bit long <code>value</code> if not null, otherwise writes zeros
- * to the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by
- * 8.
- *
- * @param value
- * Long value to be written to the output.
- * @param output
- * ByteBuf, where value or zeros are written.
- */
- public static void writeLong(final Long value, final ByteBuf output) {
- output.writeLong(value != null ? value : 0L);
- }
-
- /**
- * Writes boolean <code>value</code> if not null, otherwise writes zero to
- * the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 1.
- *
- * @param value
- * Boolean value to be written to the output.
- * @param output
- * ByteBuf, where value or zero is written.
- */
- public static void writeBoolean(final Boolean value, final ByteBuf output) {
- if (value != null) {
- output.writeBoolean(value);
- } else {
- output.writeByte(0);
- }
- }
-
- /**
- * Writes unsigned byte <code>value</code> if not null, otherwise writes
- * zero to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
- * increased by 1.
- *
- * @param value
- * Short value to be write to the output.
- * @param output
- * ByteBuf, where value or zeros are written.
- * @deprecated Use {@link #writeUnsignedByte(Uint8, ByteBuf)} instead.
- */
- @Deprecated(forRemoval = true)
- public static void writeUnsignedByte(final Short value, final ByteBuf output) {
- output.writeByte(value != null ? value : 0);
- }
-
- /**
- * Writes unsigned byte <code>value</code> if not null, otherwise writes
- * zero to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
- * increased by 1.
- *
- * @param value
- * Short value to be write to the output.
- * @param output
- * ByteBuf, where value or zeros are written.
- */
- public static void writeUnsignedByte(final Uint8 value, final ByteBuf output) {
- output.writeByte(value != null ? value.byteValue() : 0);
- }
-
- /**
- * Writes unsigned 16-bit short integer <code>value</code> if not null,
- * otherwise writes zeros to the <code>output</code> ByteBuf. ByteBuf's
- * writerIndex is increased by 2.
- *
- * @param value
- * Integer value to be written to the output.
- * @param output
- * ByteBuf, where value or zeros are written.
- * @deprecated Use {@link #writeUnsignedShort(Uint16, ByteBuf)} instead.
- */
- @Deprecated(forRemoval = true)
- public static void writeUnsignedShort(final Integer value, final ByteBuf output) {
- output.writeShort(value != null ? value.shortValue() : 0);
- }
-
- /**
- * Writes unsigned 16-bit short integer <code>value</code> if not null,
- * otherwise writes zeros to the <code>output</code> ByteBuf. ByteBuf's
- * writerIndex is increased by 2.
- *
- * @param value
- * Integer value to be written to the output.
- * @param output
- * ByteBuf, where value or zeros are written.
- */
- public static void writeUnsignedShort(final Uint16 value, final ByteBuf output) {
- output.writeShort(value != null ? value.shortValue() : 0);
- }
-
- /**
- * Writes unsigned 32-bit integer <code>value</code> if not null, otherwise
- * writes zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
- * increased by 4.
- *
- * @param value
- * Long value to be written to the output.
- * @param output
- * ByteBuf, where value or zeros are written.
- * @deprecated Use {@link #writeUnsignedInt(Uint32, ByteBuf)} instead.
- */
- @Deprecated(forRemoval = true)
- public static void writeUnsignedInt(final Long value, final ByteBuf output) {
- output.writeInt(value != null ? value.intValue() : 0);
- }
-
- /**
- * Writes unsigned 32-bit integer <code>value</code> if not null, otherwise
- * writes zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
- * increased by 4.
- *
- * @param value
- * Long value to be written to the output.
- * @param output
- * ByteBuf, where value or zeros are written.
- */
- public static void writeUnsignedInt(final Uint32 value, final ByteBuf output) {
- output.writeInt(value != null ? value.intValue() : 0);
- }
-
- /**
- * Writes unsigned 64-bit integer <code>value</code> if not null, otherwise
- * writes zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
- * increased by 8.
- *
- * @param value
- * BigInteger value to be written to the output.
- * @param output
- * ByteBuf, where value or zeros are written.
- * @deprecated Use {@link #writeUnsignedLong(Uint64, ByteBuf)} instead.
- */
- @Deprecated(forRemoval = true)
- public static void writeUnsignedLong(final BigInteger value, final ByteBuf output) {
- output.writeLong(value != null ? value.longValue() : 0L);
- }
-
- /**
- * Writes unsigned 64-bit integer <code>value</code> if not null, otherwise
- * writes zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
- * increased by 8.
- *
- * @param value
- * BigInteger value to be written to the output.
- * @param output
- * ByteBuf, where value or zeros are written.
- */
- public static void writeUnsignedLong(final Uint64 value, final ByteBuf output) {
- output.writeLong(value != null ? value.longValue() : 0L);
- }
-
/**
* Writes IPv4 address if not null, otherwise writes zeros to the
* <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.protocol.util;
import static org.junit.Assert.assertArrayEquals;
-import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeBoolean;
import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeFloat32;
import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeInt;
import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv4Address;
import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv4Prefix;
import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv6Address;
import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv6Prefix;
-import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeLong;
import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeMedium;
-import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeShort;
-import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
-import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
-import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedLong;
-import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedShort;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ieee754.rev130819.Float32;
-import org.opendaylight.yangtools.yang.common.Uint16;
-import org.opendaylight.yangtools.yang.common.Uint32;
-import org.opendaylight.yangtools.yang.common.Uint64;
-import org.opendaylight.yangtools.yang.common.Uint8;
public class ByteBufWriteUtilTest {
-
- private static final byte[] ONE_BYTE_ZERO = {0};
-
- private static final byte[] TWO_BYTE_ZEROS = {0, 0};
-
- private static final byte[] FOUR_BYTE_ZEROS = {0, 0, 0, 0};
-
- private static final byte[] EIGHT_BYTE_ZEROS = { 0, 0, 0, 0, 0, 0, 0, 0 };
+ private static final byte[] FOUR_BYTE_ZEROS = { 0, 0, 0, 0 };
@Test
public void testWriteIntegerValue() {
assertArrayEquals(FOUR_BYTE_ZEROS, output.array());
}
- @Test
- public void testWriteShortValue() {
- final byte[] result = { 0, 5 };
- final ByteBuf output = Unpooled.buffer(Short.BYTES);
- writeShort((short) 5, output);
- assertArrayEquals(result, output.array());
-
- output.clear();
- writeShort(null, output);
- assertArrayEquals(TWO_BYTE_ZEROS, output.array());
- }
-
@Test
public void testWriteMediumValue() {
final byte[] result = { 0, 0, 5 };
assertArrayEquals(resultZero, output.array());
}
- @Test
- public void testWriteLongValue() {
- final byte[] result = { 0, 0, 0, 0, 0, 0, 0, 5 };
- final ByteBuf output = Unpooled.buffer(Long.BYTES);
- writeLong((long) 5, output);
- assertArrayEquals(result, output.array());
-
- output.clear();
- writeLong(null, output);
- assertArrayEquals(EIGHT_BYTE_ZEROS, output.array());
- }
-
- @Test
- public void testWriteBooleanValue() {
- final byte[] result = { 1 };
- final ByteBuf output = Unpooled.buffer(Byte.BYTES);
- writeBoolean(true, output);
- assertArrayEquals(result, output.array());
-
- output.clear();
- writeBoolean(null, output);
- assertArrayEquals(ONE_BYTE_ZERO, output.array());
- }
-
- @Test
- public void testWriteUnsignedByteValue() {
- final byte[] result = { 5 };
- final ByteBuf output = Unpooled.buffer(Byte.BYTES);
- writeUnsignedByte(Uint8.valueOf(5), output);
- assertArrayEquals(result, output.array());
-
- output.clear();
- writeUnsignedByte((Uint8) null, output);
- assertArrayEquals(ONE_BYTE_ZERO, output.array());
- }
-
- @Test
- public void testWriteUnsignedShortValue() {
- final byte[] result = { 0, 5 };
- final ByteBuf output = Unpooled.buffer(Short.BYTES);
- writeUnsignedShort(Uint16.valueOf(5), output);
- assertArrayEquals(result, output.array());
-
- output.clear();
- writeUnsignedShort((Uint16) null, output);
- assertArrayEquals(TWO_BYTE_ZEROS, output.array());
- }
-
- @Test
- public void testWriteUnsignedIntValue() {
- final byte[] result = { 0, 0, 0, 5 };
- final ByteBuf output = Unpooled.buffer(Integer.BYTES);
- ByteBufWriteUtil.writeUnsignedInt(Uint32.valueOf(5), output);
- assertArrayEquals(result, output.array());
-
- output.clear();
- writeUnsignedInt((Uint32) null, output);
- assertArrayEquals(FOUR_BYTE_ZEROS, output.array());
- }
-
- @Test
- public void testWriteUnsignedLongValue() {
- final byte[] result = { 0, 0, 0, 0, 0, 0, 0, 5 };
- final ByteBuf output = Unpooled.buffer(Long.BYTES);
- writeUnsignedLong(Uint64.valueOf(5), output);
- assertArrayEquals(result, output.array());
-
- output.clear();
- writeUnsignedLong((Uint64) null, output);
- assertArrayEquals(EIGHT_BYTE_ZEROS, output.array());
- }
-
@Test
public void testWriteIpv4Address() {
final byte[] result = { 127, 0, 0, 1 };