From b6b5ca2897c00acb04367d009fa698235ba6de06 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sat, 19 Oct 2019 12:45:29 +0200 Subject: [PATCH] Add IetfYangUtil.physAddressBytes() This adds {mac,phys}AddressBytes() to extract these from well-known objects. JIRA: MDSAL-486 Change-Id: Id4a9825be476e4a83bf39c4372340b479dd76164 Signed-off-by: Robert Varga --- .../model/ietf/util/AbstractIetfYangUtil.java | 33 ++++++++++++------- .../ietf/util/AbstractIetfYangUtilTest.java | 25 ++++++++++---- 2 files changed, 40 insertions(+), 18 deletions(-) diff --git a/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfYangUtil.java b/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfYangUtil.java index 6f1a054171..14e23dc86c 100644 --- a/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfYangUtil.java +++ b/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfYangUtil.java @@ -25,7 +25,8 @@ import org.opendaylight.mdsal.binding.spec.reflect.StringValueObjectFactory; public abstract class AbstractIetfYangUtil { 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 { 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 { 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 { */ 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 { 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; + } } diff --git a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfYangUtilTest.java b/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfYangUtilTest.java index a9e5b7bf49..29ea958b82 100644 --- a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfYangUtilTest.java +++ b/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfYangUtilTest.java @@ -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 -- 2.36.6