From f8a41cf4e743eec82aacae13c558fb8234ffc64c Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Wed, 27 Nov 2019 11:07:31 +0100 Subject: [PATCH] Add yang-common-netty This adds common utilities for dealing with Uint8 types with ByteBufs, along with some convenience nullness-handling methods. JIRA: YANGTOOLS-1047 Change-Id: Idf5bef51db30c5397d440e2648fe9c417735aae2 Signed-off-by: Robert Varga --- artifacts/pom.xml | 13 + docs/pom.xml | 4 + features/features-yangtools/pom.xml | 6 + features/odl-yangtools-netty/pom.xml | 56 +++ .../src/main/feature/feature.xml | 6 + features/pom.xml | 1 + yang/pom.xml | 1 + yang/yang-common-netty/pom.xml | 48 ++ .../yang/common/netty/ByteBufUtils.java | 444 ++++++++++++++++++ .../netty/ByteBufUtilsNullnessTest.java | 128 +++++ .../yang/common/netty/ByteBufUtilsTest.java | 324 +++++++++++++ 11 files changed, 1031 insertions(+) create mode 100644 features/odl-yangtools-netty/pom.xml create mode 100644 features/odl-yangtools-netty/src/main/feature/feature.xml create mode 100644 yang/yang-common-netty/pom.xml create mode 100644 yang/yang-common-netty/src/main/java/org/opendaylight/yangtools/yang/common/netty/ByteBufUtils.java create mode 100644 yang/yang-common-netty/src/test/java/org/opendaylight/yangtools/yang/common/netty/ByteBufUtilsNullnessTest.java create mode 100644 yang/yang-common-netty/src/test/java/org/opendaylight/yangtools/yang/common/netty/ByteBufUtilsTest.java diff --git a/artifacts/pom.xml b/artifacts/pom.xml index b8e5723926..63fdbfe29a 100644 --- a/artifacts/pom.xml +++ b/artifacts/pom.xml @@ -250,6 +250,12 @@ 4.0.3-SNAPSHOT + + org.opendaylight.yangtools + yang-common-netty + 4.0.3-SNAPSHOT + + org.opendaylight.yangtools @@ -337,6 +343,13 @@ xml features + + org.opendaylight.yangtools + odl-yangtools-netty + 4.0.3-SNAPSHOT + xml + features + org.opendaylight.yangtools odl-yangtools-xpath-api diff --git a/docs/pom.xml b/docs/pom.xml index 5a016ef15b..7b79d12d26 100644 --- a/docs/pom.xml +++ b/docs/pom.xml @@ -34,6 +34,10 @@ org.opendaylight.yangtools yang-common + + org.opendaylight.yangtools + yang-common-netty + org.opendaylight.yangtools yang-data-api diff --git a/features/features-yangtools/pom.xml b/features/features-yangtools/pom.xml index 3c70360484..e9369d77f1 100644 --- a/features/features-yangtools/pom.xml +++ b/features/features-yangtools/pom.xml @@ -66,6 +66,12 @@ features xml + + org.opendaylight.yangtools + odl-yangtools-netty + features + xml + org.opendaylight.yangtools odl-yangtools-parser-api diff --git a/features/odl-yangtools-netty/pom.xml b/features/odl-yangtools-netty/pom.xml new file mode 100644 index 0000000000..b72a0e3fc3 --- /dev/null +++ b/features/odl-yangtools-netty/pom.xml @@ -0,0 +1,56 @@ + + + + 4.0.0 + + + org.opendaylight.odlparent + single-feature-parent + 6.0.3 + + + + org.opendaylight.yangtools + odl-yangtools-netty + 4.0.3-SNAPSHOT + feature + OpenDaylight :: Yangtools :: Netty integration + YANG Tools/netty-buffer utilities + + + + + org.opendaylight.yangtools + yangtools-artifacts + ${project.version} + import + pom + + + + + + + org.opendaylight.odlparent + odl-netty-4 + xml + features + + + org.opendaylight.yangtools + odl-yangtools-common + xml + features + + + org.opendaylight.yangtools + yang-common-netty + + + diff --git a/features/odl-yangtools-netty/src/main/feature/feature.xml b/features/odl-yangtools-netty/src/main/feature/feature.xml new file mode 100644 index 0000000000..6c3d767b28 --- /dev/null +++ b/features/odl-yangtools-netty/src/main/feature/feature.xml @@ -0,0 +1,6 @@ + + + + odl-netty-4 + + diff --git a/features/pom.xml b/features/pom.xml index ad63f695fe..c9764f4426 100644 --- a/features/pom.xml +++ b/features/pom.xml @@ -29,6 +29,7 @@ odl-yangtools-data-api odl-yangtools-data odl-yangtools-export + odl-yangtools-netty odl-yangtools-parser-api odl-yangtools-parser odl-yangtools-util diff --git a/yang/pom.xml b/yang/pom.xml index 6f1c2f1fd4..c9dc65f9fd 100644 --- a/yang/pom.xml +++ b/yang/pom.xml @@ -24,6 +24,7 @@ yang-common + yang-common-netty yang-data-api yang-data-xpath-api yang-data-jaxen diff --git a/yang/yang-common-netty/pom.xml b/yang/yang-common-netty/pom.xml new file mode 100644 index 0000000000..a684a3e3df --- /dev/null +++ b/yang/yang-common-netty/pom.xml @@ -0,0 +1,48 @@ + + + + + 4.0.0 + + org.opendaylight.yangtools + bundle-parent + 4.0.3-SNAPSHOT + ../../bundle-parent + + + yang-common-netty + bundle + ${project.artifactId} + Netty utilities for Common YANG definitions + + + + org.opendaylight.yangtools + yang-common + + + io.netty + netty-buffer + + + + + + + org.apache.felix + maven-bundle-plugin + + + org.opendaylight.yangtools.yang.common.netty + + + + + + diff --git a/yang/yang-common-netty/src/main/java/org/opendaylight/yangtools/yang/common/netty/ByteBufUtils.java b/yang/yang-common-netty/src/main/java/org/opendaylight/yangtools/yang/common/netty/ByteBufUtils.java new file mode 100644 index 0000000000..9569e40dd2 --- /dev/null +++ b/yang/yang-common-netty/src/main/java/org/opendaylight/yangtools/yang/common/netty/ByteBufUtils.java @@ -0,0 +1,444 @@ +/* + * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * 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.yangtools.yang.common.netty; + +import com.google.common.annotations.Beta; +import io.netty.buffer.ByteBuf; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; +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 methods for interacting with {@link ByteBuf}s. These add a number of methods for reading and writing various + * data types from/to ByteBufs. Methods fall into these categories: + *
    + *
  • {@code readUint*}, which extract the corresponding amount of data from a buffer and return an Uint type. These + * are more efficient than going the {@code Uint8.valueOf(buf.readUnsignedByte())} route.
  • + *
  • {@code writeUint*}, which write specified value into a buffer.
  • + *
  • {@code writeMandatory*}, which write a property not statically known to be non-null. These methods throw + * an {@link IllegalArgumentException} if the supplied value is null. Otherwise they will write it to provided + * buffer.
  • + *
  • {@code writeOptional*}, which write a value which can legally be null. In case the value is not null, it is + * written to the provided buffer. If the value null, the method does nothing.
  • + *
  • {@code writeOrZero*}, which write a value which can legally be null. In case the value is not null, it is + * written to the provided buffer. If the value is null, a {code zero} value of corresponding width is written + * instead.
  • + *
+ */ +@Beta +public final class ByteBufUtils { + private ByteBufUtils() { + + } + + /** + * Read an {@link Uint8} from specified buffer. + * + * @param buf buffer + * @return A {@link Uint8} + * @throws NullPointerException if {@code buf} is null + * @throws IndexOutOfBoundsException if {@code buf} does not have enough data + */ + public static @NonNull Uint8 readUint8(final ByteBuf buf) { + return Uint8.fromByteBits(buf.readByte()); + } + + /** + * Read a {@link Uint16} from specified buffer. + * + * @param buf buffer + * @return A {@link Uint16} + * @throws NullPointerException if {@code buf} is null + * @throws IndexOutOfBoundsException if {@code buf} does not have enough data + */ + public static @NonNull Uint16 readUint16(final ByteBuf buf) { + return Uint16.fromShortBits(buf.readShort()); + } + + /** + * Read a {@link Uint32} from specified buffer. + * + * @param buf buffer + * @return A {@link Uint32} + * @throws NullPointerException if {@code buf} is null + * @throws IndexOutOfBoundsException if {@code buf} does not have enough data + */ + public static @NonNull Uint32 readUint32(final ByteBuf buf) { + return Uint32.fromIntBits(buf.readInt()); + } + + /** + * Read a {@link Uint64} from specified buffer. + * + * @param buf buffer + * @return A {@link Uint64} + * @throws NullPointerException if {@code buf} is null + * @throws IndexOutOfBoundsException if {@code buf} does not have enough data + */ + public static @NonNull Uint64 readUint64(final ByteBuf buf) { + return Uint64.fromLongBits(buf.readLong()); + } + + /** + * Write a {@link Uint8} to specified buffer. + * + * @param buf buffer + * @param value A {@link Uint8} + * @throws NullPointerException if any argument is null + */ + public static void write(final ByteBuf buf, final Uint8 value) { + buf.writeByte(value.byteValue()); + } + + /** + * Write a {@link Uint16} to specified buffer. + * + * @param buf buffer + * @param value A {@link Uint16} + * @throws NullPointerException if any argument is null + */ + public static void write(final ByteBuf buf, final Uint16 value) { + buf.writeShort(value.shortValue()); + } + + /** + * Write a {@link Uint32} from specified buffer. + * + * @param buf buffer + * @param value A {@link Uint32} + * @throws NullPointerException if any argument is null + */ + public static void write(final ByteBuf buf, final Uint32 value) { + buf.writeInt(value.intValue()); + } + + /** + * Write a {@link Uint64} to specified buffer. + * + * @param buf buffer + * @param value A {@link Uint64} + * @throws NullPointerException if any argument is null + */ + public static void write(final ByteBuf buf, final Uint64 value) { + buf.writeLong(value.longValue()); + } + + /** + * Write a {@link Byte} property to specified buffer. If the {@code value} is known to be non-null, prefer + * {@link ByteBuf#writeByte(int)} instead of this method. + * + * @param buf buffer + * @param value A {@link Byte} + * @param name Property name for error reporting purposes + * @throws NullPointerException if {@code buf} is null + * @throws IllegalArgumentException if {@code value} is null + */ + public static void writeMandatory(final ByteBuf buf, final Byte value, final String name) { + buf.writeByte(nonNullArgument(value, name).byteValue()); + } + + /** + * Write a {@link Short} property to specified buffer. If the {@code value} is known to be non-null, prefer + * {@link ByteBuf#writeShort(int)} instead of this method. + * + * @param buf buffer + * @param value A {@link Short} + * @param name Property name for error reporting purposes + * @throws NullPointerException if {@code buf} is null + * @throws IllegalArgumentException if {@code value} is null + */ + public static void writeMandatory(final ByteBuf buf, final Short value, final String name) { + buf.writeShort(nonNullArgument(value, name).shortValue()); + } + + /** + * Write a {@link Integer} property to specified buffer. If the {@code value} is known to be non-null, prefer + * {@link ByteBuf#writeInt(int)} instead of this method. + * + * @param buf buffer + * @param value A {@link Integer} + * @param name Property name for error reporting purposes + * @throws NullPointerException if {@code buf} is null + * @throws IllegalArgumentException if {@code value} is null + */ + public static void writeMandatory(final ByteBuf buf, final Integer value, final String name) { + buf.writeInt(nonNullArgument(value, name).intValue()); + } + + /** + * Write a {@link Long} property to specified buffer. If the {@code value} is known to be non-null, prefer + * {@link ByteBuf#writeLong(long)} instead of this method. + * + * @param buf buffer + * @param value A {@link Long} + * @param name Property name for error reporting purposes + * @throws NullPointerException if {@code buf} is null + * @throws IllegalArgumentException if {@code value} is null + */ + public static void writeMandatory(final ByteBuf buf, final Long value, final String name) { + buf.writeLong(nonNullArgument(value, name).longValue()); + } + + /** + * Write a {@link Uint8} property to specified buffer. If the {@code value} is known to be non-null, prefer to use + * {@link #write(ByteBuf, Uint8)} instead of this method. + * + * @param buf buffer + * @param value A {@link Uint8} + * @param name Property name for error reporting purposes + * @throws NullPointerException if {@code buf} is null + * @throws IllegalArgumentException if {@code value} is null + */ + public static void writeMandatory(final ByteBuf buf, final Uint8 value, final String name) { + write(buf, nonNullArgument(value, name)); + } + + /** + * Write a {@link Uint16} property to specified buffer. If the {@code value} is known to be non-null, prefer to use + * {@link #write(ByteBuf, Uint16)} instead of this method. + * + * @param buf buffer + * @param value A {@link Uint16} + * @param name Property name for error reporting purposes + * @throws NullPointerException if {@code buf} is null + * @throws IllegalArgumentException if {@code value} is null + */ + public static void writeMandatory(final ByteBuf buf, final Uint16 value, final String name) { + write(buf, nonNullArgument(value, name)); + } + + /** + * Write a {@link Uint32} property to specified buffer. If the {@code value} is known to be non-null, prefer to use + * {@link #write(ByteBuf, Uint32)} instead of this method. + * + * @param buf buffer + * @param value A {@link Uint32} + * @param name Property name for error reporting purposes + * @throws NullPointerException if {@code buf} is null + * @throws IllegalArgumentException if {@code value} is null + */ + public static void writeMandatory(final ByteBuf buf, final Uint32 value, final String name) { + write(buf, nonNullArgument(value, name)); + } + + /** + * Write a {@link Uint64} property to specified buffer. If the {@code value} is known to be non-null, prefer to use + * {@link #write(ByteBuf, Uint64)} instead of this method. + * + * @param buf buffer + * @param value A {@link Uint64} + * @param name Property name for error reporting purposes + * @throws NullPointerException if {@code buf} is null + * @throws IllegalArgumentException if {@code value} is null + */ + public static void writeMandatory(final ByteBuf buf, final Uint64 value, final String name) { + write(buf, nonNullArgument(value, name)); + } + + /** + * Write a {@link Byte} value to specified buffer if it is not null. + * + * @param buf buffer + * @param value A {@link Byte} + * @throws NullPointerException if {@code buf} is null + */ + public static void writeOptional(final ByteBuf buf, final @Nullable Byte value) { + if (value != null) { + buf.writeByte(value.byteValue()); + } + } + + /** + * Write a {@link Byte} value to specified buffer if it is not null. + * + * @param buf buffer + * @param value A {@link Short} + * @throws NullPointerException if {@code buf} is null + */ + public static void writeOptional(final ByteBuf buf, final @Nullable Short value) { + if (value != null) { + buf.writeShort(value.shortValue()); + } + } + + /** + * Write a {@link Integer} value to specified buffer if it is not null. + * + * @param buf buffer + * @param value A {@link Integer} + * @throws NullPointerException if {@code buf} is null + */ + public static void writeOptional(final ByteBuf buf, final @Nullable Integer value) { + if (value != null) { + buf.writeInt(value.intValue()); + } + } + + /** + * Write a {@link Long} value to specified buffer if it is not null. + * + * @param buf buffer + * @param value A {@link Long} + * @throws NullPointerException if {@code buf} is null + */ + public static void writeOptional(final ByteBuf buf, final @Nullable Long value) { + if (value != null) { + buf.writeLong(value.longValue()); + } + } + + /** + * Write a {@link Uint8} value to specified buffer if it is not null. + * + * @param buf buffer + * @param value A {@link Uint8} + * @throws NullPointerException if {@code buf} is null + */ + public static void writeOptional(final ByteBuf buf, final @Nullable Uint8 value) { + if (value != null) { + write(buf, value); + } + } + + /** + * Write a {@link Uint16} value to specified buffer if it is not null. + * + * @param buf buffer + * @param value A {@link Uint16} + * @throws NullPointerException if {@code buf} is null + */ + public static void writeOptional(final ByteBuf buf, final @Nullable Uint16 value) { + if (value != null) { + write(buf, value); + } + } + + /** + * Write a {@link Uint32} value to specified buffer if it is not null. + * + * @param buf buffer + * @param value A {@link Uint32} + * @throws NullPointerException if {@code buf} is null + */ + public static void writeOptional(final ByteBuf buf, final @Nullable Uint32 value) { + if (value != null) { + write(buf, value); + } + } + + /** + * Write a {@link Uint64} value to specified buffer if it is not null. + * + * @param buf buffer + * @param value A {@link Uint64} + * @throws NullPointerException if {@code buf} is null + */ + public static void writeOptional(final ByteBuf buf, final @Nullable Uint64 value) { + if (value != null) { + write(buf, value); + } + } + + /** + * Write a {@link Byte} value to specified buffer if it is not null, otherwise write one zero byte. + * + * @param buf buffer + * @param value A {@link Byte} + * @throws NullPointerException if {@code buf} is null + */ + public static void writeOrZero(final ByteBuf buf, final @Nullable Byte value) { + buf.writeByte(value != null ? value.byteValue() : 0); + } + + /** + * Write a {@link Byte} value to specified buffer if it is not null, otherwise write two zero bytes. + * + * @param buf buffer + * @param value A {@link Byte} + * @throws NullPointerException if {@code buf} is null + */ + public static void writeOrZero(final ByteBuf buf, final @Nullable Short value) { + buf.writeShort(value != null ? value.shortValue() : (short) 0); + } + + /** + * Write a {@link Byte} value to specified buffer if it is not null, otherwise write four zero bytes. + * + * @param buf buffer + * @param value A {@link Byte} + * @throws NullPointerException if {@code buf} is null + */ + public static void writeOrZero(final ByteBuf buf, final @Nullable Integer value) { + buf.writeInt(value != null ? value.intValue() : 0); + } + + /** + * Write a {@link Byte} value to specified buffer if it is not null, otherwise write eight zero bytes. + * + * @param buf buffer + * @param value A {@link Byte} + * @throws NullPointerException if {@code buf} is null + */ + public static void writeOrZero(final ByteBuf buf, final @Nullable Long value) { + buf.writeLong(value != null ? value.longValue() : 0L); + } + + /** + * Write a {@link Byte} value to specified buffer if it is not null, otherwise write one zero byte. + * + * @param buf buffer + * @param value A {@link Byte} + * @throws NullPointerException if {@code buf} is null + */ + public static void writeOrZero(final ByteBuf buf, final @Nullable Uint8 value) { + buf.writeByte(value != null ? value.byteValue() : 0); + } + + /** + * Write a {@link Byte} value to specified buffer if it is not null, otherwise write two zero bytes. + * + * @param buf buffer + * @param value A {@link Byte} + * @throws NullPointerException if {@code buf} is null + */ + public static void writeOrZero(final ByteBuf buf, final @Nullable Uint16 value) { + buf.writeShort(value != null ? value.shortValue() : (short) 0); + } + + /** + * Write a {@link Byte} value to specified buffer if it is not null, otherwise write four zero bytes. + * + * @param buf buffer + * @param value A {@link Byte} + * @throws NullPointerException if {@code buf} is null + */ + public static void writeOrZero(final ByteBuf buf, final @Nullable Uint32 value) { + buf.writeInt(value != null ? value.intValue() : 0); + } + + /** + * Write a {@link Byte} value to specified buffer if it is not null, otherwise write eight zero bytes. + * + * @param buf buffer + * @param value A {@link Byte} + * @throws NullPointerException if {@code buf} is null + */ + public static void writeOrZero(final ByteBuf buf, final @Nullable Uint64 value) { + buf.writeLong(value != null ? value.longValue() : 0L); + } + + private static @NonNull T nonNullArgument(final @Nullable T obj, final String name) { + if (obj == null) { + throw new IllegalArgumentException(name + " is mandatory"); + } + return obj; + } +} diff --git a/yang/yang-common-netty/src/test/java/org/opendaylight/yangtools/yang/common/netty/ByteBufUtilsNullnessTest.java b/yang/yang-common-netty/src/test/java/org/opendaylight/yangtools/yang/common/netty/ByteBufUtilsNullnessTest.java new file mode 100644 index 0000000000..0f0c84315c --- /dev/null +++ b/yang/yang-common-netty/src/test/java/org/opendaylight/yangtools/yang/common/netty/ByteBufUtilsNullnessTest.java @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * 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.yangtools.yang.common.netty; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import org.junit.Before; +import org.junit.Test; +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 ByteBufUtilsNullnessTest { + private ByteBuf buf; + + @Before + public void before() { + buf = Unpooled.buffer(); + } + + @Test(expected = NullPointerException.class) + public void testReadUint8() { + ByteBufUtils.readUint8(null); + } + + @Test(expected = NullPointerException.class) + public void testReadUint16() { + ByteBufUtils.readUint16(null); + } + + @Test(expected = NullPointerException.class) + public void testReadUint32() { + ByteBufUtils.readUint8(null); + } + + @Test(expected = NullPointerException.class) + public void testReadUint64() { + ByteBufUtils.readUint64(null); + } + + @Test(expected = NullPointerException.class) + public void testWriteNullBuf8() { + ByteBufUtils.write(null, Uint8.ONE); + } + + @Test(expected = NullPointerException.class) + public void testWriteNullBuf16() { + ByteBufUtils.write(null, Uint16.ONE); + } + + @Test(expected = NullPointerException.class) + public void testWriteNullBuf32() { + ByteBufUtils.write(null, Uint32.ONE); + } + + @Test(expected = NullPointerException.class) + public void testWriteNullBuf64() { + ByteBufUtils.write(null, Uint64.ONE); + } + + @Test(expected = NullPointerException.class) + public void testWriteNull8() { + ByteBufUtils.write(buf, (Uint8) null); + } + + @Test(expected = NullPointerException.class) + public void testWriteNull16() { + ByteBufUtils.write(buf, (Uint16) null); + } + + @Test(expected = NullPointerException.class) + public void testWriteNull32() { + ByteBufUtils.write(buf, (Uint32) null); + } + + @Test(expected = NullPointerException.class) + public void testWriteNull64() { + ByteBufUtils.write(null, (Uint64) null); + } + + @Test(expected = IllegalArgumentException.class) + public void testWriteMandatoryByte() { + ByteBufUtils.writeMandatory(buf, (Byte) null, "name"); + } + + @Test(expected = IllegalArgumentException.class) + public void testWriteMandatoryShort() { + ByteBufUtils.writeMandatory(buf, (Short) null, "name"); + } + + @Test(expected = IllegalArgumentException.class) + public void testWriteMandatoryInt() { + ByteBufUtils.writeMandatory(buf, (Integer) null, "name"); + } + + @Test(expected = IllegalArgumentException.class) + public void testWriteMandatoryLong() { + ByteBufUtils.writeMandatory(buf, (Long) null, "name"); + } + + @Test(expected = IllegalArgumentException.class) + public void testWriteMandatoryUint8() { + ByteBufUtils.writeMandatory(buf, (Uint8) null, "name"); + } + + @Test(expected = IllegalArgumentException.class) + public void testWriteMandatoryUint16() { + ByteBufUtils.writeMandatory(buf, (Uint16) null, "name"); + } + + @Test(expected = IllegalArgumentException.class) + public void testWriteMandatoryUint32() { + ByteBufUtils.writeMandatory(buf, (Uint32) null, "name"); + } + + @Test(expected = IllegalArgumentException.class) + public void testWriteMandatoryUint64() { + ByteBufUtils.writeMandatory(buf, (Uint64) null, "name"); + } + + +} diff --git a/yang/yang-common-netty/src/test/java/org/opendaylight/yangtools/yang/common/netty/ByteBufUtilsTest.java b/yang/yang-common-netty/src/test/java/org/opendaylight/yangtools/yang/common/netty/ByteBufUtilsTest.java new file mode 100644 index 0000000000..08bb97794a --- /dev/null +++ b/yang/yang-common-netty/src/test/java/org/opendaylight/yangtools/yang/common/netty/ByteBufUtilsTest.java @@ -0,0 +1,324 @@ +/* + * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * 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.yangtools.yang.common.netty; + +import static org.junit.Assert.assertEquals; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import org.junit.Test; +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 ByteBufUtilsTest { + @Test + public void testWriteByte() { + test(Byte.MAX_VALUE); + test(Byte.MIN_VALUE); + } + + @Test + public void testWriteShort() { + test(Short.MAX_VALUE); + test(Short.MIN_VALUE); + } + + @Test + public void testWriteInt() { + test(Integer.MAX_VALUE); + test(Integer.MIN_VALUE); + } + + @Test + public void testWriteLong() { + test(Long.MAX_VALUE); + test(Long.MIN_VALUE); + } + + @Test + public void testWrite8() { + testUint(Uint8.ONE); + testUint(Uint8.TWO); + testUint(Uint8.TEN); + testUint(Uint8.MAX_VALUE); + } + + @Test + public void testWrite16() { + testUint(Uint16.ONE); + testUint(Uint16.TWO); + testUint(Uint16.TEN); + testUint(Uint16.MAX_VALUE); + } + + @Test + public void testWrite32() { + testUint(Uint32.ONE); + testUint(Uint32.TWO); + testUint(Uint32.TEN); + testUint(Uint32.MAX_VALUE); + } + + @Test + public void testWrite64() { + testUint(Uint64.ONE); + testUint(Uint64.TWO); + testUint(Uint64.TEN); + testUint(Uint64.MAX_VALUE); + } + + @Test + public void testWriteOptionalByte() { + final ByteBuf buf = Unpooled.buffer(); + ByteBufUtils.writeOptional(buf, (Byte) null); + assertEquals(0, buf.readableBytes()); + + ByteBufUtils.writeOptional(buf, Byte.MAX_VALUE); + assertEquals(1, buf.readableBytes()); + } + + @Test + public void testWriteOptionalShort() { + final ByteBuf buf = Unpooled.buffer(); + ByteBufUtils.writeOptional(buf, (Short) null); + assertEquals(0, buf.readableBytes()); + + ByteBufUtils.writeOptional(buf, Short.MAX_VALUE); + assertEquals(2, buf.readableBytes()); + } + + @Test + public void testWriteOptionalInt() { + final ByteBuf buf = Unpooled.buffer(); + ByteBufUtils.writeOptional(buf, (Integer) null); + assertEquals(0, buf.readableBytes()); + + ByteBufUtils.writeOptional(buf, Integer.MAX_VALUE); + assertEquals(4, buf.readableBytes()); + } + + @Test + public void testWriteOptionalLong() { + final ByteBuf buf = Unpooled.buffer(); + ByteBufUtils.writeOptional(buf, (Long) null); + assertEquals(0, buf.readableBytes()); + + ByteBufUtils.writeOptional(buf, Long.MAX_VALUE); + assertEquals(8, buf.readableBytes()); + } + + @Test + public void testWriteOptional8() { + final ByteBuf buf = Unpooled.buffer(); + ByteBufUtils.writeOptional(buf, (Uint8) null); + assertEquals(0, buf.readableBytes()); + + ByteBufUtils.writeOptional(buf, Uint8.MAX_VALUE); + assertUint(buf, Uint8.MAX_VALUE); + } + + @Test + public void testWriteOptional16() { + final ByteBuf buf = Unpooled.buffer(); + ByteBufUtils.writeOptional(buf, (Uint16) null); + assertEquals(0, buf.readableBytes()); + + ByteBufUtils.writeOptional(buf, Uint16.MAX_VALUE); + assertUint(buf, Uint16.MAX_VALUE); + } + + @Test + public void testWriteOptional32() { + final ByteBuf buf = Unpooled.buffer(); + ByteBufUtils.writeOptional(buf, (Uint32) null); + assertEquals(0, buf.readableBytes()); + + ByteBufUtils.writeOptional(buf, Uint32.MAX_VALUE); + assertUint(buf, Uint32.MAX_VALUE); + } + + @Test + public void testWriteOptional64() { + final ByteBuf buf = Unpooled.buffer(); + ByteBufUtils.writeOptional(buf, (Uint64) null); + assertEquals(0, buf.readableBytes()); + + ByteBufUtils.writeOptional(buf, Uint64.MAX_VALUE); + assertUint(buf, Uint64.MAX_VALUE); + } + + @Test + public void testWriteZeroByte() { + final ByteBuf buf = Unpooled.buffer(); + ByteBufUtils.writeOrZero(buf, (Byte) null); + assertByte(buf, 0); + + ByteBufUtils.writeOrZero(buf, Byte.MAX_VALUE); + assertByte(buf, Byte.MAX_VALUE); + } + + @Test + public void testWriteZeroShort() { + final ByteBuf buf = Unpooled.buffer(); + ByteBufUtils.writeOrZero(buf, (Short) null); + assertShort(buf, 0); + + ByteBufUtils.writeOrZero(buf, Short.MAX_VALUE); + assertShort(buf, Short.MAX_VALUE); + } + + @Test + public void testWriteZeroInt() { + final ByteBuf buf = Unpooled.buffer(); + ByteBufUtils.writeOrZero(buf, (Integer) null); + assertInt(buf, 0); + + ByteBufUtils.writeOrZero(buf, Integer.MAX_VALUE); + assertInt(buf, Integer.MAX_VALUE); + } + + @Test + public void testWriteZeroLong() { + final ByteBuf buf = Unpooled.buffer(); + ByteBufUtils.writeOrZero(buf, (Long) null); + assertLong(buf, 0); + + ByteBufUtils.writeOrZero(buf, Long.MAX_VALUE); + assertLong(buf, Long.MAX_VALUE); + } + + @Test + public void testWriteZero8() { + final ByteBuf buf = Unpooled.buffer(); + ByteBufUtils.writeOrZero(buf, (Uint8) null); + assertUint(buf, Uint8.ZERO); + + ByteBufUtils.writeOrZero(buf, Uint8.MAX_VALUE); + assertUint(buf, Uint8.MAX_VALUE); + } + + @Test + public void testWriteZero16() { + final ByteBuf buf = Unpooled.buffer(); + ByteBufUtils.writeOrZero(buf, (Uint16) null); + assertUint(buf, Uint16.ZERO); + + ByteBufUtils.writeOrZero(buf, Uint16.MAX_VALUE); + assertUint(buf, Uint16.MAX_VALUE); + } + + @Test + public void testWriteZero32() { + final ByteBuf buf = Unpooled.buffer(); + ByteBufUtils.writeOrZero(buf, (Uint32) null); + assertUint(buf, Uint32.ZERO); + + ByteBufUtils.writeOrZero(buf, Uint32.MAX_VALUE); + assertUint(buf, Uint32.MAX_VALUE); + } + + @Test + public void testWriteZero64() { + final ByteBuf buf = Unpooled.buffer(); + ByteBufUtils.writeOrZero(buf, (Uint64) null); + assertUint(buf, Uint64.ZERO); + + ByteBufUtils.writeOrZero(buf, Uint64.MAX_VALUE); + assertUint(buf, Uint64.MAX_VALUE); + } + + private static void test(final Byte value) { + final ByteBuf buf = Unpooled.buffer(); + ByteBufUtils.writeMandatory(buf, value, "foo"); + assertByte(buf, value); + } + + private static void test(final Short value) { + final ByteBuf buf = Unpooled.buffer(); + ByteBufUtils.writeMandatory(buf, value, "foo"); + assertShort(buf, value); + } + + private static void test(final Integer value) { + final ByteBuf buf = Unpooled.buffer(); + ByteBufUtils.writeMandatory(buf, value, "foo"); + assertInt(buf, value); + } + + private static void test(final Long value) { + final ByteBuf buf = Unpooled.buffer(); + ByteBufUtils.writeMandatory(buf, value, "foo"); + assertLong(buf, value); + } + + private static void testUint(final Uint8 value) { + final ByteBuf buf = Unpooled.buffer(); + ByteBufUtils.writeMandatory(buf, value, "foo"); + assertUint(buf, value); + } + + private static void testUint(final Uint16 value) { + final ByteBuf buf = Unpooled.buffer(); + ByteBufUtils.writeMandatory(buf, value, "foo"); + assertUint(buf, value); + } + + private static void testUint(final Uint32 value) { + final ByteBuf buf = Unpooled.buffer(); + ByteBufUtils.writeMandatory(buf, value, "foo"); + assertUint(buf, value); + } + + private static void testUint(final Uint64 value) { + final ByteBuf buf = Unpooled.buffer(); + ByteBufUtils.writeMandatory(buf, value, "foo"); + assertUint(buf, value); + } + + private static void assertByte(final ByteBuf buf, final int value) { + assertEquals(1, buf.readableBytes()); + assertEquals(value, buf.readByte()); + } + + private static void assertShort(final ByteBuf buf, final int value) { + assertEquals(2, buf.readableBytes()); + assertEquals(value, buf.readShort()); + } + + private static void assertInt(final ByteBuf buf, final int value) { + assertEquals(4, buf.readableBytes()); + assertEquals(value, buf.readInt()); + } + + private static void assertLong(final ByteBuf buf, final long value) { + assertEquals(8, buf.readableBytes()); + assertEquals(value, buf.readLong()); + } + + private static void assertUint(final ByteBuf buf, final Uint8 value) { + assertEquals(1, buf.readableBytes()); + assertEquals(value, ByteBufUtils.readUint8(buf)); + } + + private static void assertUint(final ByteBuf buf, final Uint16 value) { + assertEquals(2, buf.readableBytes()); + assertEquals(value, ByteBufUtils.readUint16(buf)); + } + + private static void assertUint(final ByteBuf buf, final Uint32 value) { + assertEquals(4, buf.readableBytes()); + assertEquals(value, ByteBufUtils.readUint32(buf)); + } + + private static void assertUint(final ByteBuf buf, final Uint64 value) { + assertEquals(8, buf.readableBytes()); + assertEquals(value, ByteBufUtils.readUint64(buf)); + } +} -- 2.36.6