From 87d1b044acc19ad0ec14b02be1d8938cffa0437f Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sat, 4 Jan 2020 11:40:52 +0100 Subject: [PATCH] Add uint24.yang uint24 is used in a variety of binary protocols (rsvp,lisp,bgp), this adds a common definition of this type and the corresponding netty utilities. JIRA: MDSAL-507 Change-Id: I5170c2bb9ed65d25bada3a85aca6c22f9ba4696b Signed-off-by: Robert Varga (cherry picked from commit 28d302faed8b6f57263991257163414ac073a37f) --- artifacts/pom.xml | 26 ++++++ common/mdsal-uint24-netty/pom.xml | 63 ++++++++++++++ .../uint24/netty/Uint24ByteBufUtils.java | 86 +++++++++++++++++++ .../uint24/netty/Uint24ByteBufUtilsTest.java | 83 ++++++++++++++++++ common/pom.xml | 1 + docs/pom.xml | 9 ++ features/features-mdsal/pom.xml | 14 +++ features/odl-mdsal-model-odl-uint24/pom.xml | 37 ++++++++ features/odl-mdsal-uint24-netty/pom.xml | 41 +++++++++ .../src/main/feature/feature.xml | 6 ++ features/pom.xml | 4 + model/odl-uint24/pom.xml | 23 +++++ model/odl-uint24/src/main/yang/uint24.yang | 30 +++++++ model/pom.xml | 1 + 14 files changed, 424 insertions(+) create mode 100644 common/mdsal-uint24-netty/pom.xml create mode 100644 common/mdsal-uint24-netty/src/main/java/org/opendaylight/mdsal/uint24/netty/Uint24ByteBufUtils.java create mode 100644 common/mdsal-uint24-netty/src/test/java/org/opendaylight/mdsal/uint24/netty/Uint24ByteBufUtilsTest.java create mode 100644 features/odl-mdsal-model-odl-uint24/pom.xml create mode 100644 features/odl-mdsal-uint24-netty/pom.xml create mode 100644 features/odl-mdsal-uint24-netty/src/main/feature/feature.xml create mode 100644 model/odl-uint24/pom.xml create mode 100644 model/odl-uint24/src/main/yang/uint24.yang diff --git a/artifacts/pom.xml b/artifacts/pom.xml index bb1e923b97..365ff05163 100644 --- a/artifacts/pom.xml +++ b/artifacts/pom.xml @@ -379,6 +379,18 @@ xml + + + org.opendaylight.mdsal.model + odl-uint24 + 5.0.7-SNAPSHOT + + + org.opendaylight.mdsal + mdsal-uint24-netty + 5.0.7-SNAPSHOT + + org.opendaylight.mdsal.model @@ -912,6 +924,20 @@ features xml + + org.opendaylight.mdsal.model + odl-mdsal-model-odl-uint24 + 5.0.7-SNAPSHOT + features + xml + + + org.opendaylight.mdsal + odl-mdsal-uint24-netty + 5.0.7-SNAPSHOT + features + xml + diff --git a/common/mdsal-uint24-netty/pom.xml b/common/mdsal-uint24-netty/pom.xml new file mode 100644 index 0000000000..2ec4ac4f21 --- /dev/null +++ b/common/mdsal-uint24-netty/pom.xml @@ -0,0 +1,63 @@ + + + + + 4.0.0 + + + org.opendaylight.mdsal + dom-parent + 5.0.7-SNAPSHOT + ../../dom/dom-parent + + + mdsal-uint24-netty + bundle + + + + org.opendaylight.yangtools + yang-common-netty + + + org.opendaylight.mdsal + yang-binding + + + org.opendaylight.mdsal.model + odl-uint24 + + + + org.opendaylight.yangtools + mockito-configuration + + + + + + + org.apache.felix + maven-bundle-plugin + + + org.opendaylight.mdsal.uint24.netty + + + + + + + + scm:git:http://git.opendaylight.org/gerrit/mdsal.git + scm:git:ssh://git.opendaylight.org:29418/mdsal.git + HEAD + https://wiki.opendaylight.org/view/MD-SAL:Main + + diff --git a/common/mdsal-uint24-netty/src/main/java/org/opendaylight/mdsal/uint24/netty/Uint24ByteBufUtils.java b/common/mdsal-uint24-netty/src/main/java/org/opendaylight/mdsal/uint24/netty/Uint24ByteBufUtils.java new file mode 100644 index 0000000000..d7bf5e8cb9 --- /dev/null +++ b/common/mdsal-uint24-netty/src/main/java/org/opendaylight/mdsal/uint24/netty/Uint24ByteBufUtils.java @@ -0,0 +1,86 @@ +/* + * 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.mdsal.uint24.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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.uint24.rev200104.Uint24; +import org.opendaylight.yangtools.yang.common.Uint32; + +@Beta +public final class Uint24ByteBufUtils { + private Uint24ByteBufUtils() { + // Hidden on purpose + } + + /** + * Read a {@link Uint24} from specified buffer. + * + * @param buf buffer + * @return A {@link Uint24} + * @throws NullPointerException if {@code buf} is null + * @throws IndexOutOfBoundsException if {@code buf} does not have enough data + */ + public static @NonNull Uint24 readUint24(final ByteBuf buf) { + return new Uint24(Uint32.fromIntBits(buf.readMedium())); + } + + /** + * Write a {@link Uint24} to specified buffer. + * + * @param buf buffer + * @param value A {@link Uint24} + * @throws NullPointerException if any argument is null + */ + public static void writeUint24(final ByteBuf buf, final Uint24 value) { + buf.writeMedium(value.getValue().intValue()); + } + + /** + * Write a {@link Uint24} property to specified buffer. If the {@code value} is known to be non-null, prefer to use + * {@link #writeUint24(ByteBuf, Uint24)} instead of this method. + * + * @param buf buffer + * @param value A {@link Uint24} + * @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 writeMandatoryUint24(final ByteBuf buf, final Uint24 value, final String name) { + if (value == null) { + throw new IllegalArgumentException(name + " is mandatory"); + } + writeUint24(buf, value); + } + + /** + * Write a {@link Uint24} value to specified buffer if it is not null. + * + * @param buf buffer + * @param value A {@link Uint24} + * @throws NullPointerException if {@code buf} is null + */ + public static void writeOptionalUint24(final ByteBuf buf, final @Nullable Uint24 value) { + if (value != null) { + writeUint24(buf, value); + } + } + + /** + * Write a {@link Uint24} value to specified buffer if it is not null, otherwise write three zero bytes. + * + * @param buf buffer + * @param value A {@link Uint24} + * @throws NullPointerException if {@code buf} is null + */ + public static void writeUint24OrZero(final ByteBuf buf, final @Nullable Uint24 value) { + buf.writeMedium(value != null ? value.getValue().intValue() : 0); + } +} diff --git a/common/mdsal-uint24-netty/src/test/java/org/opendaylight/mdsal/uint24/netty/Uint24ByteBufUtilsTest.java b/common/mdsal-uint24-netty/src/test/java/org/opendaylight/mdsal/uint24/netty/Uint24ByteBufUtilsTest.java new file mode 100644 index 0000000000..bcaf6f2107 --- /dev/null +++ b/common/mdsal-uint24-netty/src/test/java/org/opendaylight/mdsal/uint24/netty/Uint24ByteBufUtilsTest.java @@ -0,0 +1,83 @@ +/* + * 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.mdsal.uint24.netty; + +import static org.junit.Assert.assertEquals; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import org.junit.Test; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.uint24.rev200104.Uint24; +import org.opendaylight.yangtools.yang.common.Uint32; + +public class Uint24ByteBufUtilsTest { + private static final Uint24 ONE_TWO_THREE = new Uint24(Uint32.valueOf(0x010203)); + + @Test + public void testRead() { + final ByteBuf buf = Unpooled.buffer().writeMedium(0x010203); + assertEquals(ONE_TWO_THREE, Uint24ByteBufUtils.readUint24(buf)); + assertEquals(0, buf.readableBytes()); + } + + @Test + public void testWrite() { + final ByteBuf buf = Unpooled.buffer(); + Uint24ByteBufUtils.writeUint24(buf, ONE_TWO_THREE); + assertMedium(buf); + } + + @Test + public void testWriteMandatory() { + final ByteBuf buf = Unpooled.buffer(); + Uint24ByteBufUtils.writeMandatoryUint24(buf, ONE_TWO_THREE, "foo"); + assertMedium(buf); + } + + @Test(expected = IllegalArgumentException.class) + public void testWriteMandatoryNull() { + Uint24ByteBufUtils.writeMandatoryUint24(Unpooled.buffer(), null, "foo"); + } + + @Test + public void testWriteOptional() { + final ByteBuf buf = Unpooled.buffer(); + Uint24ByteBufUtils.writeOptionalUint24(buf, ONE_TWO_THREE); + assertMedium(buf); + } + + @Test + public void testWriteOptionalNull() { + final ByteBuf buf = Unpooled.buffer(); + Uint24ByteBufUtils.writeOptionalUint24(buf, null); + assertEquals(0, buf.readableBytes()); + } + + @Test + public void testWriteOrZero() { + final ByteBuf buf = Unpooled.buffer(); + Uint24ByteBufUtils.writeUint24OrZero(buf, ONE_TWO_THREE); + assertMedium(buf); + } + + @Test + public void testWriteOrZeroNull() { + final ByteBuf buf = Unpooled.buffer(); + Uint24ByteBufUtils.writeUint24OrZero(buf, null); + assertMedium(buf, 0); + } + + private static void assertMedium(final ByteBuf buf) { + assertMedium(buf, 0x010203); + } + + private static void assertMedium(final ByteBuf buf, final int value) { + assertEquals(3, buf.readableBytes()); + assertEquals(value, buf.readMedium()); + } +} diff --git a/common/pom.xml b/common/pom.xml index 4aba02d171..40dad8ed80 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -27,6 +27,7 @@ mdsal-common-api mdsal-common-util + mdsal-uint24-netty diff --git a/docs/pom.xml b/docs/pom.xml index 01e21ba672..e2b6e1db0c 100644 --- a/docs/pom.xml +++ b/docs/pom.xml @@ -188,6 +188,15 @@ mdsal-yanglib-rfc8525 + + org.opendaylight.mdsal.model + odl-uint24 + + + org.opendaylight.mdsal + mdsal-uint24-netty + + org.opendaylight.mdsal.model opendaylight-l2-types diff --git a/features/features-mdsal/pom.xml b/features/features-mdsal/pom.xml index 483dbe5f13..842b4ad5a4 100644 --- a/features/features-mdsal/pom.xml +++ b/features/features-mdsal/pom.xml @@ -130,6 +130,20 @@ xml + + + org.opendaylight.mdsal.model + odl-mdsal-model-odl-uint24 + features + xml + + + org.opendaylight.mdsal + odl-mdsal-uint24-netty + features + xml + + org.opendaylight.mdsal.model diff --git a/features/odl-mdsal-model-odl-uint24/pom.xml b/features/odl-mdsal-model-odl-uint24/pom.xml new file mode 100644 index 0000000000..443377218f --- /dev/null +++ b/features/odl-mdsal-model-odl-uint24/pom.xml @@ -0,0 +1,37 @@ + + + + 4.0.0 + + org.opendaylight.mdsal + feature-parent + 5.0.7-SNAPSHOT + ../feature-parent + + + org.opendaylight.mdsal.model + odl-mdsal-model-odl-uint24 + 5.0.7-SNAPSHOT + feature + OpenDaylight :: MD-SAL :: Model :: OpenDaylight uint24 type + OpenDaylight-specific model of uint24 + + + + org.opendaylight.mdsal + odl-mdsal-binding-base + features + xml + + + ${project.groupId} + odl-uint24 + + + diff --git a/features/odl-mdsal-uint24-netty/pom.xml b/features/odl-mdsal-uint24-netty/pom.xml new file mode 100644 index 0000000000..c7e8661d80 --- /dev/null +++ b/features/odl-mdsal-uint24-netty/pom.xml @@ -0,0 +1,41 @@ + + + + 4.0.0 + + org.opendaylight.mdsal + feature-parent + 5.0.7-SNAPSHOT + ../feature-parent + + + odl-mdsal-uint24-netty + feature + OpenDaylight :: MD-SAL :: uint24 :: Netty + Netty support for uint24 + + + + org.opendaylight.yangtools + odl-yangtools-netty + features + xml + + + org.opendaylight.mdsal.model + odl-mdsal-model-odl-uint24 + xml + features + + + org.opendaylight.mdsal + mdsal-uint24-netty + + + diff --git a/features/odl-mdsal-uint24-netty/src/main/feature/feature.xml b/features/odl-mdsal-uint24-netty/src/main/feature/feature.xml new file mode 100644 index 0000000000..542c2e60f6 --- /dev/null +++ b/features/odl-mdsal-uint24-netty/src/main/feature/feature.xml @@ -0,0 +1,6 @@ + + + + odl-yangtools-netty + + diff --git a/features/pom.xml b/features/pom.xml index 62a97b7ede..65c6472785 100644 --- a/features/pom.xml +++ b/features/pom.xml @@ -59,6 +59,10 @@ odl-mdsal-exp-yanglib-rfc7895 odl-mdsal-exp-yanglib-rfc8525 + + odl-mdsal-model-odl-uint24 + odl-mdsal-uint24-netty + odl-mdsal-model-rfc6991 diff --git a/model/odl-uint24/pom.xml b/model/odl-uint24/pom.xml new file mode 100644 index 0000000000..a8e245aedd --- /dev/null +++ b/model/odl-uint24/pom.xml @@ -0,0 +1,23 @@ + + + + + + + org.opendaylight.mdsal + binding-parent + 5.0.7-SNAPSHOT + ../../binding/binding-parent + + + 4.0.0 + org.opendaylight.mdsal.model + odl-uint24 + bundle + diff --git a/model/odl-uint24/src/main/yang/uint24.yang b/model/odl-uint24/src/main/yang/uint24.yang new file mode 100644 index 0000000000..24cf836fd4 --- /dev/null +++ b/model/odl-uint24/src/main/yang/uint24.yang @@ -0,0 +1,30 @@ +module odl-uint24 { + yang-version 1; + namespace "urn:opendaylight:params:xml:ns:yang:uint24"; + prefix "uint24"; + + organization "OpenDaylight"; + contact "Robert Varga "; + + description + "This module contains the definition of uint24, an uint32 restricted + to 24 bits. + + 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"; + + revision "2020-01-04" { + description "Initial revision."; + } + + typedef uint24 { + type uint32 { + range "0..16777215"; + } + description "24-bit unsigned integer."; + } +} diff --git a/model/pom.xml b/model/pom.xml index ef850fa86c..e39996e587 100644 --- a/model/pom.xml +++ b/model/pom.xml @@ -30,6 +30,7 @@ ietf l2-types general-entity + odl-uint24 -- 2.36.6