diff options
author | 2018-05-31 09:34:43 +0200 | |
---|---|---|
committer | 2018-05-31 09:40:56 +0200 | |
commit | a626d4ad0333762de57eb3298b9c7b3f9f19f82e (patch) | |
tree | 496d56862000e8a3803fad6f216b8f1a64ce1802 /vpp-common/vpp-translate-utils/src/main/java | |
parent | bedf02b7e70a58ecbff98f261df816de3cf1e0a3 (diff) |
Update AddressTranslator
- added handling of IpPrefixe
- added UnitTests for the whole class
Change-Id: I9fbdcce1ca8734b7415c55559ef65e553e737e44
Signed-off-by: Michal Cmarada <michal.cmarada@pantheon.tech>
Diffstat (limited to 'vpp-common/vpp-translate-utils/src/main/java')
-rw-r--r-- | vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/AddressTranslator.java | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/AddressTranslator.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/AddressTranslator.java index b602dc569..c2f9deecb 100644 --- a/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/AddressTranslator.java +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/AddressTranslator.java @@ -21,6 +21,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import java.util.Arrays; import javax.annotation.Nonnull; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address; @@ -35,7 +36,7 @@ public interface AddressTranslator extends Ipv4Translator, Ipv6Translator, MacTr }; default byte[] ipAddressToArray(IpAddress address) { - checkNotNull(address, "Cannot resolve null adddress"); + checkNotNull(address, "Cannot resolve null address"); if (isIpv6(address)) { return ipv6AddressNoZoneToArray(new Ipv6AddressNoZone(address.getIpv6Address())); @@ -75,4 +76,26 @@ public interface AddressTranslator extends Ipv4Translator, Ipv6Translator, MacTr default boolean addressesEqual(final IpAddress left, final IpAddress right) { return Arrays.equals(left.getValue(), right.getValue()); } + + /** + * Extract mask length from prefixed address + */ + default byte extractPrefix(IpPrefix ipPrefix) { + if (isIpv6(ipPrefix)) { + return extractPrefix(ipPrefix.getIpv6Prefix()); + } else { + return extractPrefix(ipPrefix.getIpv4Prefix()); + } + } + + /** + * Convert address part of prefix to byte array + */ + default byte[] ipPrefixToArray(IpPrefix ipPrefix) { + if (isIpv6(ipPrefix)) { + return ipv6AddressPrefixToArray(ipPrefix.getIpv6Prefix()); + } else { + return ipv4AddressPrefixToArray(ipPrefix.getIpv4Prefix()); + } + } } |