}
}
- 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();
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;
* @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];
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");
}
/**
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 {
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
+import java.util.UUID;
import org.junit.Test;
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);
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());
--- /dev/null
+/*
+ * 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
* 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
}
@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();
+ }
+}
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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
* 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
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();
+ }
}
--- /dev/null
+/*
+ * 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));
+ }
+}