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];
this.physFactory = StringValueObjectFactory.create(physClass, "00:00");
}
-
/**
* Convert the value of a MacAddress into the canonical representation.
*
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.
*
*/
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);
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;
+ }
}
* 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 {
@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