From 3751ef96ae1427cc8d5ecb9cbba705e837bb63ca Mon Sep 17 00:00:00 2001 From: Michal Cmarada Date: Wed, 6 Feb 2019 09:41:39 +0100 Subject: fix after changes in VPP API - fixes for mac adress - fixes for ipaddress - fixes refactoring in ipsec Change-Id: Idc3e3557b72a5f1ac5b32b9738d90ca23ed6ed9e Signed-off-by: Michal Cmarada --- .../common/translate/util/Ipv4Translator.java | 33 ++++++++++++++++++ .../common/translate/util/Ipv6Translator.java | 39 ++++++++++++++++++++++ .../common/translate/util/MacTranslator.java | 17 ++++++++++ 3 files changed, 89 insertions(+) (limited to 'vpp-common/vpp-translate-utils/src') diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/Ipv4Translator.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/Ipv4Translator.java index 24f4ae9aa..5f8a283ed 100644 --- a/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/Ipv4Translator.java +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/Ipv4Translator.java @@ -18,9 +18,14 @@ package io.fd.hc2vpp.common.translate.util; import static com.google.common.base.Preconditions.checkNotNull; +import io.fd.vpp.jvpp.core.types.Address; +import io.fd.vpp.jvpp.core.types.AddressFamily; +import io.fd.vpp.jvpp.core.types.AddressUnion; +import io.fd.vpp.jvpp.core.types.Ip4Address; import java.util.Arrays; import javax.annotation.Nonnull; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil; +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.Ipv4Prefix; @@ -83,6 +88,34 @@ public interface Ipv4Translator extends ByteDataTranslator { return ipv4AddressNoZoneToArray(ipv4Addr.getValue()); } + /** + * Transform Ipv4 address to a Ip4Address acceptable by VPP. + * + * @return byte array with address bytes + */ + default Address ipv4AddressNoZoneToAddress(final Ipv4AddressNoZone ipv4Addr) { + Address address = new Address(); + address.af = AddressFamily.ADDRESS_IP4; + Ip4Address ip4Address = new Ip4Address(); + ip4Address.ip4Address = ipv4AddressNoZoneToArray(ipv4Addr); + address.un = new AddressUnion(ip4Address); + return address; + } + + /** + * Transform Ipv4 address to a Ip4Address acceptable by VPP. + * + * @return byte array with address bytes + */ + default Address ipv4AddressToAddress(final Ipv4Address ipv4Addr) { + Address address = new Address(); + address.af = AddressFamily.ADDRESS_IP4; + Ip4Address ip4Address = new Ip4Address(); + ip4Address.ip4Address = ipv4AddressNoZoneToArray(ipv4Addr.getValue()); + address.un = new AddressUnion(ip4Address); + return address; + } + default byte[] ipv4AddressNoZoneToArray(final String ipv4Addr) { byte[] retval = new byte[4]; String[] dots = ipv4Addr.split("\\."); diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/Ipv6Translator.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/Ipv6Translator.java index b1199d7f7..d5c0ae2e3 100644 --- a/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/Ipv6Translator.java +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/Ipv6Translator.java @@ -21,6 +21,11 @@ import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; import com.google.common.net.InetAddresses; +import io.fd.vpp.jvpp.core.types.Address; +import io.fd.vpp.jvpp.core.types.AddressFamily; +import io.fd.vpp.jvpp.core.types.AddressUnion; +import io.fd.vpp.jvpp.core.types.Ip6Address; +import io.fd.vpp.jvpp.core.types.Prefix; import javax.annotation.Nonnull; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress; @@ -45,8 +50,25 @@ public interface Ipv6Translator extends ByteDataTranslator { return InetAddresses.forString(address.getValue()).getAddress(); } + /** + * Transform Ipv6 address to a Address acceptable by VPP. + * + * @param address {@link Ipv6Address} to be translated + * @return Vpp {@link Address} from Ipv6 address + */ + default Address ipv6AddressToAddress(@Nonnull final Ipv6Address address) { + Address addr = new Address(); + addr.af = AddressFamily.ADDRESS_IP6; + Ip6Address ip6Address = new Ip6Address(); + ip6Address.ip6Address = ipv6AddressNoZoneToArray(address); + addr.un = new AddressUnion(ip6Address); + return addr; + } + /** * Creates address array from address part of {@link Ipv6Prefix} + * + * @return Ipv6 address as byte array */ default byte[] ipv6AddressPrefixToArray(@Nonnull final Ipv6Prefix ipv6Prefix) { checkNotNull(ipv6Prefix, "Cannot convert null prefix"); @@ -55,6 +77,23 @@ public interface Ipv6Translator extends ByteDataTranslator { new Ipv6Address(ipv6Prefix.getValue().substring(0, ipv6Prefix.getValue().indexOf('/'))))); } + /** + * Transforms {@link Prefix} from {@link Ipv6Prefix} + * @param ipv6Prefix prefix to be translated + * @return Vpp {@link Prefix} from {@link Ipv6Prefix} + */ + default Prefix ipv6AddressPrefixToPrefix(@Nonnull final Ipv6Prefix ipv6Prefix) { + checkNotNull(ipv6Prefix, "Cannot convert null prefix"); + Prefix prefix = new Prefix(); + prefix.address = new Address(); + prefix.address.af = AddressFamily.ADDRESS_IP6; + Ip6Address ip6Address = new Ip6Address(); + ip6Address.ip6Address = ipv6AddressPrefixToArray(ipv6Prefix); + prefix.address.un = new AddressUnion(ip6Address); + prefix.addressLength = extractPrefix(ipv6Prefix); + return prefix; + } + /** * Extracts {@link Ipv6Prefix} prefix */ diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/MacTranslator.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/MacTranslator.java index 71b8a15e6..657029eb3 100644 --- a/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/MacTranslator.java +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/MacTranslator.java @@ -20,6 +20,7 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.base.Splitter; +import io.fd.vpp.jvpp.core.types.MacAddress; import java.util.Arrays; import java.util.List; import java.util.function.BiConsumer; @@ -37,6 +38,9 @@ public interface MacTranslator { /** * Parse string represented mac address (using ":" as separator) into a byte array + * + * @param macAddress string representation of MAC address (using ":" as separator) + * @return byte array mac address */ @Nonnull default byte[] parseMac(@Nonnull final String macAddress) { @@ -45,6 +49,19 @@ public interface MacTranslator { return parseMacLikeString(parts); } + /** + * Parse string represented mac address (using ":" as separator) into a MacAddress in VPP + * + * @param macAddress string representation of MAC address (using ":" as separator) + * @return VPP MacAddress + */ + @Nonnull + default MacAddress parseMacAddress(@Nonnull final String macAddress) { + MacAddress mac = new MacAddress(); + mac.macaddress = parseMac(macAddress); + return mac; + } + default byte[] parseMacLikeString(final List strings) { return strings.stream().limit(6).map(this::parseHexByte).collect( () -> new byte[strings.size()], -- cgit 1.2.3-korg