Add IetfYangUtil.physAddressBytes() 36/85236/4
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 19 Oct 2019 10:45:29 +0000 (12:45 +0200)
committerRobert Varga <nite@hq.sk>
Sat, 19 Oct 2019 12:36:10 +0000 (12:36 +0000)
This adds {mac,phys}AddressBytes() to extract these from well-known
objects.

JIRA: MDSAL-486
Change-Id: Id4a9825be476e4a83bf39c4372340b479dd76164
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
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

index 6f1a05417168bcbbafe588cf92b31c0bf563eba8..14e23dc86c908978346b332e8b0ee46906beed13 100644 (file)
@@ -25,7 +25,8 @@ import org.opendaylight.mdsal.binding.spec.reflect.StringValueObjectFactory;
 public abstract class AbstractIetfYangUtil<M, P> {
     private static final int MAC_BYTE_LENGTH = 6;
     private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
-    private static final byte[] HEX_VALUES;
+    private static final byte @NonNull[] EMPTY_BYTES = new byte[0];
+    private static final byte @NonNull[] HEX_VALUES;
 
     static {
         final byte[] b = new byte['f' + 1];
@@ -52,7 +53,6 @@ public abstract class AbstractIetfYangUtil<M, P> {
         this.physFactory = StringValueObjectFactory.create(physClass, "00:00");
     }
 
-
     /**
      * Convert the value of a MacAddress into the canonical representation.
      *
@@ -79,6 +79,10 @@ public abstract class AbstractIetfYangUtil<M, P> {
         return macFactory.newInstance(bytesToString(bytes, 17));
     }
 
+    public final byte @NonNull[] macAddressBytes(final @NonNull M macAddress) {
+        return stringToBytes(getValue(macAddress), MAC_BYTE_LENGTH);
+    }
+
     /**
      * Convert the value of a PhysAddress into the canonical representation.
      *
@@ -101,18 +105,17 @@ public abstract class AbstractIetfYangUtil<M, P> {
      */
     public final @NonNull P physAddressFor(final byte @NonNull[] bytes) {
         checkArgument(bytes.length > 0, "Physical address should have at least one byte");
-        return physFactory.newInstance(bytesToString(bytes, (bytes.length + 1) / 3));
+        return physFactory.newInstance(bytesToString(bytes, bytes.length * 3 - 1));
     }
 
-    public final byte @NonNull[] bytesFor(final @NonNull M macAddress) {
-        final String mac = getValue(macAddress);
-        final byte[] ret = new byte[MAC_BYTE_LENGTH];
-
-        for (int i = 0, base = 0; i < MAC_BYTE_LENGTH; ++i, base += 3) {
-            ret[i] = (byte) (hexValue(mac.charAt(base)) << 4 | hexValue(mac.charAt(base + 1)));
-        }
+    public final byte @NonNull[] physAddressBytes(final @NonNull P physAddress) {
+        final String str = getPhysValue(physAddress);
+        return str.isEmpty() ? EMPTY_BYTES : stringToBytes(str, str.length() / 3 + 1);
+    }
 
-        return ret;
+    @Deprecated
+    public final byte @NonNull[] bytesFor(final @NonNull M macAddress) {
+        return macAddressBytes(macAddress);
     }
 
     protected abstract String getValue(M macAddress);
@@ -183,4 +186,12 @@ public abstract class AbstractIetfYangUtil<M, P> {
         final int intVal = Byte.toUnsignedInt(byteVal);
         sb.append(HEX_CHARS[intVal >>> 4]).append(HEX_CHARS[intVal & 15]);
     }
+
+    private static byte @NonNull[] stringToBytes(final String str, final int length) {
+        final byte[] ret = new byte[length];
+        for (int i = 0, base = 0; i < length; ++i, base += 3) {
+            ret[i] = (byte) (hexValue(str.charAt(base)) << 4 | hexValue(str.charAt(base + 1)));
+        }
+        return ret;
+    }
 }
index a9e5b7bf49df247268c62fe827b2f3e9d2076ef7..29ea958b824116872a2b2c699328a09bdf4de1ea 100644 (file)
@@ -5,14 +5,12 @@
  * 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 org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
-import java.util.Arrays;
 import org.junit.Test;
 
 public class AbstractIetfYangUtilTest {
@@ -28,11 +26,24 @@ public class AbstractIetfYangUtilTest {
 
     @Test
     public void testMacToBytes() throws Exception {
-        final byte[] bytes1 = UTIL.bytesFor(new MacClass(CANON));
-        assertTrue(Arrays.equals(BYTES, bytes1));
+        final byte[] bytes1 = UTIL.macAddressBytes(new MacClass(CANON));
+        assertArrayEquals(BYTES, bytes1);
+
+        final byte[] bytes2 = UTIL.macAddressBytes(new MacClass("01:02:1E:5a:Fb:88"));
+        assertArrayEquals(BYTES, bytes2);
+    }
+
+    @Test
+    public void testPhysToBytes() throws Exception {
+        final byte[] bytes1 = UTIL.physAddressBytes(new PhysClass(CANON));
+        assertArrayEquals(BYTES, bytes1);
+
+        final byte[] bytes2 = UTIL.physAddressBytes(new PhysClass("01:02:1E:5a:Fb:88"));
+        assertArrayEquals(BYTES, bytes2);
 
-        final byte[] bytes2 = UTIL.bytesFor(new MacClass("01:02:1E:5a:Fb:88"));
-        assertTrue(Arrays.equals(BYTES, bytes2));
+        assertArrayEquals(new byte[0], UTIL.physAddressBytes(new PhysClass("")));
+        assertArrayEquals(new byte[] { (byte) 0xaa }, UTIL.physAddressBytes(new PhysClass("aa")));
+        assertArrayEquals(new byte[] { (byte) 0xaa, (byte) 0xbb }, UTIL.physAddressBytes(new PhysClass("aa:bb")));
     }
 
     @Test