Add HexString/DottedQuad/Uuid support to IetfYangUtil 18/86718/1
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 5 Jan 2020 12:33:34 +0000 (13:33 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 5 Jan 2020 13:40:26 +0000 (14:40 +0100)
HexString is similar to a MacAddress and PhysAddress, hence it is
very simple to support it.

DottedQuad is essentially an Ipv4Address, hence it is almost as
simple to support it, too.

Uuid can be created from java.util.UUID, so that is the input
we are expecting.

JIRA: MDSAL-508
Change-Id: I82abd68b23bf857deeb9a64eefb69164fe6c8b24
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 7cbd12d8894b1703b19e70b07436a937a7b8d0bf)

model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfInetUtil.java
model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfYangUtil.java
model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfYangUtilTest.java
model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/HexClass.java [new file with mode: 0644]
model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/MacUtil.java
model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/QuadClass.java [new file with mode: 0644]
model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/UuidClass.java [new file with mode: 0644]
model/ietf/rfc6991-ietf-yang-types/src/main/java/org/opendaylight/yang/gen/v1/urn/ietf/params/xml/ns/yang/ietf/yang/types/rev130715/IetfYangUtil.java
model/ietf/rfc6991-ietf-yang-types/src/test/java/org/opendaylight/yang/gen/v1/urn/ietf/params/xml/ns/yang/ietf/yang/types/rev130715/IetfYangUtilTest.java [new file with mode: 0644]

index 85dc1c632364c1c8d8c8d0b44dcc4a0f967b0074..a67cc8f13ec74b1d602e5aebcf659cd99b458fc5 100644 (file)
@@ -648,7 +648,7 @@ public abstract class AbstractIetfInetUtil<A4, A4NZ extends A4, P4, A6, A6NZ ext
         }
     }
 
-    private static String addressStringV4(final byte @NonNull[] bytes) {
+    static String addressStringV4(final byte @NonNull[] bytes) {
         final StringBuilder sb = new StringBuilder(15);
         appendIpv4String(sb, bytes);
         return sb.toString();
index 14e23dc86c908978346b332e8b0ee46906beed13..86fa3a4efe745683e59844d46ed0974b5cfe93b1 100644 (file)
@@ -11,6 +11,7 @@ import static com.google.common.base.Preconditions.checkArgument;
 
 import com.google.common.annotations.Beta;
 import java.util.Arrays;
+import java.util.UUID;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.mdsal.binding.spec.reflect.StringValueObjectFactory;
 
@@ -22,7 +23,7 @@ import org.opendaylight.mdsal.binding.spec.reflect.StringValueObjectFactory;
  * @param <P> phys-address type
  */
 @Beta
-public abstract class AbstractIetfYangUtil<M, P> {
+public abstract class AbstractIetfYangUtil<M, P, H, Q, U> {
     private static final int MAC_BYTE_LENGTH = 6;
     private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
     private static final byte @NonNull[] EMPTY_BYTES = new byte[0];
@@ -47,10 +48,17 @@ public abstract class AbstractIetfYangUtil<M, P> {
 
     private final StringValueObjectFactory<M> macFactory;
     private final StringValueObjectFactory<P> physFactory;
+    private final StringValueObjectFactory<H> hexFactory;
+    private final StringValueObjectFactory<Q> quadFactory;
+    private final StringValueObjectFactory<U> uuidFactory;
 
-    protected AbstractIetfYangUtil(final Class<M> macClass, final Class<P> physClass) {
+    protected AbstractIetfYangUtil(final Class<M> macClass, final Class<P> physClass, final Class<H> hexClass,
+            final Class<Q> quadClass, final Class<U> uuidClass) {
         this.macFactory = StringValueObjectFactory.create(macClass, "00:00:00:00:00:00");
         this.physFactory = StringValueObjectFactory.create(physClass, "00:00");
+        this.hexFactory = StringValueObjectFactory.create(hexClass, "00");
+        this.quadFactory = StringValueObjectFactory.create(quadClass, "0.0.0.0");
+        this.uuidFactory = StringValueObjectFactory.create(uuidClass, "f81d4fae-7dec-11d0-a765-00a0c91e6bf6");
     }
 
     /**
@@ -118,10 +126,40 @@ public abstract class AbstractIetfYangUtil<M, P> {
         return macAddressBytes(macAddress);
     }
 
+    public final @NonNull H hexStringFor(final byte @NonNull[] bytes) {
+        checkArgument(bytes.length > 0, "Hex string should have at least one byte");
+        return hexFactory.newInstance(bytesToString(bytes, bytes.length * 3 - 1));
+    }
+
+    public final byte @NonNull[] hexStringBytes(final @NonNull H hexString) {
+        final String str = getHexValue(hexString);
+        return stringToBytes(str, str.length() / 3 + 1);
+    }
+
+    public final @NonNull Q dottedQuadFor(final byte @NonNull[] bytes) {
+        checkArgument(bytes.length == 4, "Dotted-quad should have 4 bytes");
+        return quadFactory.newInstance(AbstractIetfInetUtil.addressStringV4(bytes));
+    }
+
+    public final byte @NonNull[] dottedQuadBytes(final @NonNull Q hexString) {
+        final String str = getQuadValue(hexString);
+        final byte[] bytes = new byte[4];
+        Ipv4Utils.fillIpv4Bytes(bytes, 0, str, 0, str.length());
+        return bytes;
+    }
+
+    public final @NonNull U uuidFor(final @NonNull UUID uuid) {
+        return uuidFactory.newInstance(uuid.toString());
+    }
+
     protected abstract String getValue(M macAddress);
 
     protected abstract String getPhysValue(P physAddress);
 
+    protected abstract String getHexValue(H hexString);
+
+    protected abstract String getQuadValue(Q dottedQuad);
+
     static byte hexValue(final char ch) {
         byte value;
         try {
index 29ea958b824116872a2b2c699328a09bdf4de1ea..2b44c9d040422f246acf10ecb68af4d6878f5631 100644 (file)
@@ -11,6 +11,7 @@ import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
 
+import java.util.UUID;
 import org.junit.Test;
 
 public class AbstractIetfYangUtilTest {
@@ -34,7 +35,22 @@ public class AbstractIetfYangUtilTest {
     }
 
     @Test
-    public void testPhysToBytes() throws Exception {
+    public void testBytesToHex() {
+        final HexClass hex = UTIL.hexStringFor(BYTES);
+        assertEquals(CANON, hex.getValue());
+    }
+
+    @Test
+    public void testHexToBytes() {
+        final byte[] bytes1 = UTIL.hexStringBytes(new HexClass(CANON));
+        assertArrayEquals(BYTES, bytes1);
+
+        final byte[] bytes2 = UTIL.hexStringBytes(new HexClass("01:02:1E:5a:Fb:88"));
+        assertArrayEquals(BYTES, bytes2);
+    }
+
+    @Test
+    public void testPhysToBytes() {
         final byte[] bytes1 = UTIL.physAddressBytes(new PhysClass(CANON));
         assertArrayEquals(BYTES, bytes1);
 
@@ -46,6 +62,22 @@ public class AbstractIetfYangUtilTest {
         assertArrayEquals(new byte[] { (byte) 0xaa, (byte) 0xbb }, UTIL.physAddressBytes(new PhysClass("aa:bb")));
     }
 
+    @Test
+    public void testQuadBytes() {
+        assertArrayEquals(new byte[] { 1, 2, 3, 4 }, UTIL.dottedQuadBytes(new QuadClass("1.2.3.4")));
+    }
+
+    @Test
+    public void testQuadFor() {
+        assertEquals("1.2.3.4", UTIL.dottedQuadFor(new byte[] { 1, 2, 3, 4 }).getValue());
+    }
+
+    @Test
+    public void testUuidFor() {
+        final UUID uuid = UUID.fromString("f81d4fae-7dec-11d0-a765-00a0c91e6bf6");
+        assertEquals("f81d4fae-7dec-11d0-a765-00a0c91e6bf6", UTIL.uuidFor(uuid).getValue());
+    }
+
     @Test
     public void canonizeMACTest() throws Exception {
         assertEquals(CANON, UTIL.canonizeMacAddress(new MacClass("01:02:1E:5A:FB:88")).getValue());
diff --git a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/HexClass.java b/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/HexClass.java
new file mode 100644 (file)
index 0000000..00ddd88
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * 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.model.ietf.util;
+
+import static java.util.Objects.requireNonNull;
+
+@SuppressWarnings("checkstyle:memberName")
+public final class HexClass {
+    private final String _value;
+
+    public HexClass(final String value) {
+        this._value = requireNonNull(value);
+    }
+
+    public HexClass(final HexClass template) {
+        this._value = template._value;
+    }
+
+    String getValue() {
+        return _value;
+    }
+}
\ No newline at end of file
index 6861734aaa8f072f2f4ddfb745a852a9b275053f..2130d4c8e72729d79d6bfa987c4edd0a0641361e 100644 (file)
@@ -5,12 +5,11 @@
  * 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.model.ietf.util;
 
-final class MacUtil extends AbstractIetfYangUtil<MacClass, PhysClass> {
+final class MacUtil extends AbstractIetfYangUtil<MacClass, PhysClass, HexClass, QuadClass, UuidClass> {
     MacUtil() {
-        super(MacClass.class, PhysClass.class);
+        super(MacClass.class, PhysClass.class, HexClass.class, QuadClass.class, UuidClass.class);
     }
 
     @Override
@@ -19,7 +18,17 @@ final class MacUtil extends AbstractIetfYangUtil<MacClass, PhysClass> {
     }
 
     @Override
-    protected String getPhysValue(PhysClass physAddress) {
+    protected String getPhysValue(final PhysClass physAddress) {
         return physAddress.getValue();
     }
-}
\ No newline at end of file
+
+    @Override
+    protected String getHexValue(final HexClass hexString) {
+        return hexString.getValue();
+    }
+
+    @Override
+    protected String getQuadValue(final QuadClass dottedQuad) {
+        return dottedQuad.getValue();
+    }
+}
diff --git a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/QuadClass.java b/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/QuadClass.java
new file mode 100644 (file)
index 0000000..9495670
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * 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.model.ietf.util;
+
+import static java.util.Objects.requireNonNull;
+
+@SuppressWarnings("checkstyle:memberName")
+public final class QuadClass {
+    private final String _value;
+
+    public QuadClass(final String value) {
+        this._value = requireNonNull(value);
+    }
+
+    public QuadClass(final QuadClass template) {
+        this._value = template._value;
+    }
+
+    String getValue() {
+        return _value;
+    }
+}
\ No newline at end of file
diff --git a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/UuidClass.java b/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/UuidClass.java
new file mode 100644 (file)
index 0000000..c6446ba
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * 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.model.ietf.util;
+
+import static java.util.Objects.requireNonNull;
+
+@SuppressWarnings("checkstyle:memberName")
+public final class UuidClass {
+    private final String _value;
+
+    public UuidClass(final String value) {
+        this._value = requireNonNull(value);
+    }
+
+    public UuidClass(final UuidClass template) {
+        this._value = template._value;
+    }
+
+    String getValue() {
+        return _value;
+    }
+}
\ No newline at end of file
index d10452e19d9f04de92c6299d6bd7ee754ee77782..0bc92bc5afc634dbf8c0c789f3820f2aa6ca9426 100644 (file)
@@ -14,11 +14,11 @@ import org.opendaylight.mdsal.model.ietf.util.AbstractIetfYangUtil;
  * Utility methods for working with types defined in ietf-yang-types.
  */
 @Beta
-public final class IetfYangUtil extends AbstractIetfYangUtil<MacAddress, PhysAddress> {
+public final class IetfYangUtil extends AbstractIetfYangUtil<MacAddress, PhysAddress, HexString, DottedQuad, Uuid> {
     public static final IetfYangUtil INSTANCE = new IetfYangUtil();
 
     private IetfYangUtil() {
-        super(MacAddress.class, PhysAddress.class);
+        super(MacAddress.class, PhysAddress.class, HexString.class, DottedQuad.class, Uuid.class);
     }
 
     @Override
@@ -30,4 +30,14 @@ public final class IetfYangUtil extends AbstractIetfYangUtil<MacAddress, PhysAdd
     protected String getPhysValue(final PhysAddress physAddress) {
         return physAddress.getValue();
     }
+
+    @Override
+    protected String getHexValue(final HexString hexString) {
+        return hexString.getValue();
+    }
+
+    @Override
+    protected String getQuadValue(final DottedQuad dottedQuad) {
+        return dottedQuad.getValue();
+    }
 }
diff --git a/model/ietf/rfc6991-ietf-yang-types/src/test/java/org/opendaylight/yang/gen/v1/urn/ietf/params/xml/ns/yang/ietf/yang/types/rev130715/IetfYangUtilTest.java b/model/ietf/rfc6991-ietf-yang-types/src/test/java/org/opendaylight/yang/gen/v1/urn/ietf/params/xml/ns/yang/ietf/yang/types/rev130715/IetfYangUtilTest.java
new file mode 100644 (file)
index 0000000..7548f6f
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * 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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.IetfYangUtil.INSTANCE;
+
+import java.util.UUID;
+import org.junit.Test;
+
+public class IetfYangUtilTest {
+    @Test
+    public void testDottedQuad() {
+        assertArrayEquals(new byte[] { 1, 2, 3, 4 }, INSTANCE.dottedQuadBytes(new DottedQuad("1.2.3.4")));
+        assertEquals(new DottedQuad("1.2.3.4"), INSTANCE.dottedQuadFor(new byte[] { 1, 2, 3, 4 }));
+    }
+
+    @Test
+    public void testHexString() {
+        assertArrayEquals(new byte[] { 0, 1 }, INSTANCE.hexStringBytes(new HexString("00:01")));
+        assertEquals(new HexString("00:01"), INSTANCE.hexStringFor(new byte[] { 0, 1 }));
+    }
+
+    @Test
+    public void testPhysAddress() {
+        assertArrayEquals(new byte[] { 0, 1} , INSTANCE.physAddressBytes(new PhysAddress("00:01")));
+        assertEquals(new PhysAddress("00:01"), INSTANCE.physAddressFor(new byte[] { 0, 1 }));
+    }
+
+    @Test
+    public void testMacAddress() {
+        assertArrayEquals(new byte[] { 0, 1, 2, 3, 4, 5 },
+            INSTANCE.macAddressBytes(new MacAddress("00:01:02:03:04:05")));
+        assertEquals(new MacAddress("00:01:02:03:04:05"), INSTANCE.macAddressFor(new byte[] { 0, 1, 2, 3, 4, 5 }));
+    }
+
+    @Test
+    public void testUuid() {
+        final UUID java = UUID.randomUUID();
+        assertEquals(new Uuid(java.toString()), INSTANCE.uuidFor(java));
+    }
+}