From 00819e1fefea2ae58106d387d7858a3503153867 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Tue, 16 May 2023 00:23:29 +0200 Subject: [PATCH] Integrate AbstractIetfYangUtil We have a superfluous abstract class, integrate it into IetfYangUtil. JIRA: MDSAL-826 Change-Id: Idbd6c19fdb30cabb3a7852d94971d1c47e8fb1a2 Signed-off-by: Robert Varga --- .../types/rev130715/AbstractIetfYangUtil.java | 179 ------------------ .../yang/types/rev130715/IetfYangUtil.java | 156 +++++++++++++-- 2 files changed, 141 insertions(+), 194 deletions(-) delete mode 100644 model/ietf/rfc6991-ietf-yang-types/src/main/java/org/opendaylight/yang/gen/v1/urn/ietf/params/xml/ns/yang/ietf/yang/types/rev130715/AbstractIetfYangUtil.java diff --git a/model/ietf/rfc6991-ietf-yang-types/src/main/java/org/opendaylight/yang/gen/v1/urn/ietf/params/xml/ns/yang/ietf/yang/types/rev130715/AbstractIetfYangUtil.java b/model/ietf/rfc6991-ietf-yang-types/src/main/java/org/opendaylight/yang/gen/v1/urn/ietf/params/xml/ns/yang/ietf/yang/types/rev130715/AbstractIetfYangUtil.java deleted file mode 100644 index 5bfa17a93d..0000000000 --- a/model/ietf/rfc6991-ietf-yang-types/src/main/java/org/opendaylight/yang/gen/v1/urn/ietf/params/xml/ns/yang/ietf/yang/types/rev130715/AbstractIetfYangUtil.java +++ /dev/null @@ -1,179 +0,0 @@ -/* - * Copyright (c) 2016 Pantheon Technologies 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 com.google.common.base.Preconditions.checkArgument; - -import com.google.common.annotations.Beta; -import java.util.HexFormat; -import java.util.UUID; -import org.eclipse.jdt.annotation.NonNull; -import org.opendaylight.mdsal.binding.spec.reflect.StringValueObjectFactory; -import org.opendaylight.mdsal.model.ietf.util.Ipv4Utils; - -/** - * Abstract utility class for dealing with MAC addresses as defined in the ietf-yang-types model. This class is - * used by revision-specific classes. - * - * @param mac-address type - * @param

phys-address type - */ -@Beta -public abstract class AbstractIetfYangUtil { - private static final int MAC_BYTE_LENGTH = 6; - private static final HexFormat COLON_HEXFORMAT = HexFormat.ofDelimiter(":"); - private static final byte @NonNull[] EMPTY_BYTES = new byte[0]; - - private final StringValueObjectFactory macFactory; - private final StringValueObjectFactory

physFactory; - private final StringValueObjectFactory hexFactory; - private final StringValueObjectFactory quadFactory; - private final StringValueObjectFactory uuidFactory; - - protected AbstractIetfYangUtil(final Class macClass, final Class

physClass, final Class hexClass, - final Class quadClass, final Class 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"); - } - - /** - * Convert the value of a MacAddress into the canonical representation. - * - * @param macAddress Input MAC address - * @return A MacAddress containing the canonical representation. - * @throws NullPointerException if macAddress is null - */ - public final @NonNull M canonizeMacAddress(final @NonNull M macAddress) { - final char[] input = getValue(macAddress).toCharArray(); - return ensureLowerCase(input) ? macFactory.newInstance(String.valueOf(input)) : macAddress; - } - - /** - * Create a MacAddress object holding the canonical representation of the 6 bytes - * passed in as argument. - * @param bytes 6-byte input array - * @return MacAddress with canonical string derived from input bytes - * @throws NullPointerException if bytes is null - * @throws IllegalArgumentException if length of input is not 6 bytes - */ - public final @NonNull M macAddressFor(final byte @NonNull[] bytes) { - checkArgument(bytes.length == MAC_BYTE_LENGTH, "MAC address should have 6 bytes, not %s", bytes.length); - return macFactory.newInstance(COLON_HEXFORMAT.formatHex(bytes)); - } - - 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. - * - * @param physAddress Input MAC address - * @return A PhysAddress containing the canonical representation. - * @throws NullPointerException if physAddress is null - */ - public final @NonNull P canonizePhysAddress(final @NonNull P physAddress) { - final char[] input = getPhysValue(physAddress).toCharArray(); - return ensureLowerCase(input) ? physFactory.newInstance(String.valueOf(input)) : physAddress; - } - - /** - * Create a PhysAddress object holding the canonical representation of the bytes passed in as argument. - * - * @param bytes input array - * @return PhysAddress with canonical string derived from input bytes - * @throws NullPointerException if bytes is null - * @throws IllegalArgumentException if length of input is not at least 1 byte - */ - public final @NonNull P physAddressFor(final byte @NonNull[] bytes) { - checkArgument(bytes.length > 0, "Physical address should have at least one byte"); - return physFactory.newInstance(COLON_HEXFORMAT.formatHex(bytes)); - } - - 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); - } - - public final @NonNull H hexStringFor(final byte @NonNull[] bytes) { - checkArgument(bytes.length > 0, "Hex string should have at least one byte"); - return hexFactory.newInstance(COLON_HEXFORMAT.formatHex(bytes)); - } - - 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(Ipv4Utils.addressString(bytes)); - } - - public final @NonNull Q dottedQuadFor(final int bits) { - return quadFactory.newInstance(Ipv4Utils.addressString(bits)); - } - - public final int dottedQuadBits(final @NonNull Q dottedQuad) { - final String str = getQuadValue(dottedQuad); - return Ipv4Utils.addressBits(str, str.length()); - } - - public final byte @NonNull[] dottedQuadBytes(final @NonNull Q dottedQuad) { - final String str = getQuadValue(dottedQuad); - return Ipv4Utils.addressBytes(str, str.length()); - } - - 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); - - /** - * Make sure an array of characters does not include capital letters. This method assumes input conforms to - * MAC address format, e.g. it is composed of 6 groups of hexadecimal digits separated by colons. Behavior is - * undefined if the input does not meet this criteria. - * - * @param chars Input characters, may not be null - * @return True if the array has been modified - * @throws NullPointerException if input is null - */ - private static boolean ensureLowerCase(final char @NonNull[] chars) { - boolean ret = false; - - for (int i = 0; i < chars.length; ++i) { - final char c = chars[i]; - if (c >= 'A' && c <= 'F') { - // Weird notation to ensure constant folding to '(char) (c + 32)', a well-known property of ASCII - chars[i] = (char) (c + ('a' - 'A')); - ret = true; - } - } - - return ret; - } - - 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) ((HexFormat.fromHexDigit(str.charAt(base)) << 4) - + HexFormat.fromHexDigit(str.charAt(base + 1))); - } - return ret; - } -} diff --git a/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 b/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 index 90ce5c1741..4f859cbd96 100644 --- a/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 +++ b/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 @@ -7,36 +7,162 @@ */ package org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715; +import static com.google.common.base.Preconditions.checkArgument; + import com.google.common.annotations.Beta; +import java.util.HexFormat; +import java.util.UUID; +import org.eclipse.jdt.annotation.NonNull; +import org.opendaylight.mdsal.binding.spec.reflect.StringValueObjectFactory; +import org.opendaylight.mdsal.model.ietf.util.Ipv4Utils; /** - * Utility methods for working with types defined in ietf-yang-types. + * Utility methods for working with types defined in {@code ietf-yang-types}. */ @Beta -public final class IetfYangUtil extends AbstractIetfYangUtil { +public final class IetfYangUtil { public static final IetfYangUtil INSTANCE = new IetfYangUtil(); + private static final int MAC_BYTE_LENGTH = 6; + private static final HexFormat COLON_HEXFORMAT = HexFormat.ofDelimiter(":"); + private static final byte @NonNull[] EMPTY_BYTES = new byte[0]; + + private final StringValueObjectFactory macFactory; + private final StringValueObjectFactory physFactory; + private final StringValueObjectFactory hexFactory; + private final StringValueObjectFactory quadFactory; + private final StringValueObjectFactory uuidFactory; + private IetfYangUtil() { - super(MacAddress.class, PhysAddress.class, HexString.class, DottedQuad.class, Uuid.class); + macFactory = StringValueObjectFactory.create(MacAddress.class, "00:00:00:00:00:00"); + physFactory = StringValueObjectFactory.create(PhysAddress.class, "00:00"); + hexFactory = StringValueObjectFactory.create(HexString.class, "00"); + quadFactory = StringValueObjectFactory.create(DottedQuad.class, "0.0.0.0"); + uuidFactory = StringValueObjectFactory.create(Uuid.class, "f81d4fae-7dec-11d0-a765-00a0c91e6bf6"); + } + + /** + * Convert the value of a MacAddress into the canonical representation. + * + * @param macAddress Input MAC address + * @return A MacAddress containing the canonical representation. + * @throws NullPointerException if macAddress is null + */ + public @NonNull MacAddress canonizeMacAddress(final @NonNull MacAddress macAddress) { + final char[] input = macAddress.getValue().toCharArray(); + return ensureLowerCase(input) ? macFactory.newInstance(String.valueOf(input)) : macAddress; + } + + /** + * Create a MacAddress object holding the canonical representation of the 6 bytes + * passed in as argument. + * @param bytes 6-byte input array + * @return MacAddress with canonical string derived from input bytes + * @throws NullPointerException if bytes is null + * @throws IllegalArgumentException if length of input is not 6 bytes + */ + public @NonNull MacAddress macAddressFor(final byte @NonNull[] bytes) { + checkArgument(bytes.length == MAC_BYTE_LENGTH, "MAC address should have 6 bytes, not %s", bytes.length); + return macFactory.newInstance(COLON_HEXFORMAT.formatHex(bytes)); } - @Override - protected String getValue(final MacAddress macAddress) { - return macAddress.getValue(); + public byte @NonNull[] macAddressBytes(final @NonNull MacAddress macAddress) { + return stringToBytes(macAddress.getValue(), MAC_BYTE_LENGTH); } - @Override - protected String getPhysValue(final PhysAddress physAddress) { - return physAddress.getValue(); + /** + * Convert the value of a PhysAddress into the canonical representation. + * + * @param physAddress Input MAC address + * @return A PhysAddress containing the canonical representation. + * @throws NullPointerException if physAddress is null + */ + public @NonNull PhysAddress canonizePhysAddress(final @NonNull PhysAddress physAddress) { + final char[] input = physAddress.getValue().toCharArray(); + return ensureLowerCase(input) ? physFactory.newInstance(String.valueOf(input)) : physAddress; } - @Override - protected String getHexValue(final HexString hexString) { - return hexString.getValue(); + /** + * Create a PhysAddress object holding the canonical representation of the bytes passed in as argument. + * + * @param bytes input array + * @return PhysAddress with canonical string derived from input bytes + * @throws NullPointerException if bytes is null + * @throws IllegalArgumentException if length of input is not at least 1 byte + */ + public @NonNull PhysAddress physAddressFor(final byte @NonNull[] bytes) { + checkArgument(bytes.length > 0, "Physical address should have at least one byte"); + return physFactory.newInstance(COLON_HEXFORMAT.formatHex(bytes)); + } + + public byte @NonNull[] physAddressBytes(final @NonNull PhysAddress physAddress) { + final String str = physAddress.getValue(); + return str.isEmpty() ? EMPTY_BYTES : stringToBytes(str, str.length() / 3 + 1); + } + + public @NonNull HexString hexStringFor(final byte @NonNull[] bytes) { + checkArgument(bytes.length > 0, "Hex string should have at least one byte"); + return hexFactory.newInstance(COLON_HEXFORMAT.formatHex(bytes)); + } + + public byte @NonNull[] hexStringBytes(final @NonNull HexString hexString) { + final String str = hexString.getValue(); + return stringToBytes(str, str.length() / 3 + 1); + } + + public @NonNull DottedQuad dottedQuadFor(final byte @NonNull[] bytes) { + checkArgument(bytes.length == 4, "Dotted-quad should have 4 bytes"); + return quadFactory.newInstance(Ipv4Utils.addressString(bytes)); + } + + public @NonNull DottedQuad dottedQuadFor(final int bits) { + return quadFactory.newInstance(Ipv4Utils.addressString(bits)); + } + + public int dottedQuadBits(final @NonNull DottedQuad dottedQuad) { + final String str = dottedQuad.getValue(); + return Ipv4Utils.addressBits(str, str.length()); + } + + public byte @NonNull[] dottedQuadBytes(final @NonNull DottedQuad dottedQuad) { + final String str = dottedQuad.getValue(); + return Ipv4Utils.addressBytes(str, str.length()); + } + + public @NonNull Uuid uuidFor(final @NonNull UUID uuid) { + return uuidFactory.newInstance(uuid.toString()); + } + + /** + * Make sure an array of characters does not include capital letters. This method assumes input conforms to + * MAC address format, e.g. it is composed of 6 groups of hexadecimal digits separated by colons. Behavior is + * undefined if the input does not meet this criteria. + * + * @param chars Input characters, may not be null + * @return True if the array has been modified + * @throws NullPointerException if input is null + */ + private static boolean ensureLowerCase(final char @NonNull[] chars) { + boolean ret = false; + + for (int i = 0; i < chars.length; ++i) { + final char c = chars[i]; + if (c >= 'A' && c <= 'F') { + // Weird notation to ensure constant folding to '(char) (c + 32)', a well-known property of ASCII + chars[i] = (char) (c + ('a' - 'A')); + ret = true; + } + } + + return ret; } - @Override - protected String getQuadValue(final DottedQuad dottedQuad) { - return dottedQuad.getValue(); + 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) ((HexFormat.fromHexDigit(str.charAt(base)) << 4) + + HexFormat.fromHexDigit(str.charAt(base + 1))); + } + return ret; } } -- 2.36.6