From 44642bb269c539dc0a5a93682e2dadb413596656 Mon Sep 17 00:00:00 2001 From: Maros Marsalek Date: Thu, 26 May 2016 10:42:51 +0200 Subject: Move V3poUtils for vpp-translate-utils Change-Id: Id6218d5136e0e4c0f1848da20bba784b71feee13 Signed-off-by: Maros Marsalek --- .../v3po/interfaces/InterfaceCustomizer.java | 4 +- .../translate/v3po/interfaces/L2Customizer.java | 6 +- .../v3po/interfaces/RoutingCustomizer.java | 4 +- .../v3po/interfaces/SubInterfaceCustomizer.java | 6 +- .../translate/v3po/interfaces/TapCustomizer.java | 12 +-- .../v3po/interfaces/VhostUserCustomizer.java | 12 +-- .../v3po/interfaces/VlanTagRewriteCustomizer.java | 4 +- .../translate/v3po/interfaces/VxlanCustomizer.java | 6 +- .../v3po/interfaces/ip/Ipv4Customizer.java | 6 +- .../v3po/interfacesstate/InterfaceCustomizer.java | 6 +- .../v3po/interfacesstate/InterfaceUtils.java | 8 +- .../v3po/interfacesstate/L2Customizer.java | 4 +- .../v3po/interfacesstate/TapCustomizer.java | 6 +- .../v3po/interfacesstate/VhostUserCustomizer.java | 6 +- .../v3po/interfacesstate/VxlanCustomizer.java | 4 +- .../v3po/translate/v3po/utils/V3poUtils.java | 109 --------------------- .../translate/v3po/vpp/BridgeDomainCustomizer.java | 6 +- .../translate/v3po/vppstate/VersionCustomizer.java | 10 +- .../v3po/interfaces/VhostUserCustomizerTest.java | 6 +- .../v3po/translate/v3po/utils/V3poUtilsTest.java | 61 ------------ .../v3po/translate/v3po/util/TranslateUtils.java | 109 +++++++++++++++++++++ .../translate/v3po/util/TranslateUtilsTest.java | 62 ++++++++++++ 22 files changed, 229 insertions(+), 228 deletions(-) delete mode 100644 v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/utils/V3poUtils.java delete mode 100644 v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/utils/V3poUtilsTest.java create mode 100644 v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtils.java create mode 100644 v3po/vpp-translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtilsTest.java (limited to 'v3po') diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/InterfaceCustomizer.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/InterfaceCustomizer.java index eb6426241..4e81ca0f2 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/InterfaceCustomizer.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/InterfaceCustomizer.java @@ -20,7 +20,7 @@ import io.fd.honeycomb.v3po.translate.spi.write.ListWriterCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext; import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException; -import io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils; +import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils; import io.fd.honeycomb.v3po.translate.write.WriteContext; import io.fd.honeycomb.v3po.translate.write.WriteFailedException; import java.util.List; @@ -123,7 +123,7 @@ public class InterfaceCustomizer extends FutureJVppCustomizer implements ListWri LOG.debug("Updating interface flags for: {}, index: {}, enabled: {}", swIfName, swIfIndex, enabled); - SwInterfaceSetFlagsReply reply = V3poUtils.getReply(swInterfaceSetFlagsReplyFuture.toCompletableFuture()); + SwInterfaceSetFlagsReply reply = TranslateUtils.getReply(swInterfaceSetFlagsReplyFuture.toCompletableFuture()); if (reply.retval < 0) { LOG.warn("Failed to update interface flags for: {}, index: {}, enabled: {}", swIfName, swIfIndex, enabled); diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/L2Customizer.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/L2Customizer.java index 5aea5de2b..e3eff2d0f 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/L2Customizer.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/L2Customizer.java @@ -23,7 +23,7 @@ import io.fd.honeycomb.v3po.translate.spi.write.ChildWriterCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext; import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException; -import io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils; +import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils; import io.fd.honeycomb.v3po.translate.write.WriteContext; import io.fd.honeycomb.v3po.translate.write.WriteFailedException; import java.util.concurrent.CompletionStage; @@ -147,7 +147,7 @@ public class L2Customizer extends FutureJVppCustomizer implements ChildWriterCus final CompletionStage swInterfaceSetL2BridgeReplyCompletionStage = getFutureJVpp() .swInterfaceSetL2Bridge(getL2BridgeRequest(swIfIndex, bdId, shg, bvi, (byte) 1 /* enable */)); final SwInterfaceSetL2BridgeReply reply = - V3poUtils.getReply(swInterfaceSetL2BridgeReplyCompletionStage.toCompletableFuture()); + TranslateUtils.getReply(swInterfaceSetL2BridgeReplyCompletionStage.toCompletableFuture()); if (reply.retval < 0) { LOG.warn("Failed to update bridge based interconnection flags for: {}, interconnection: {}", ifcName, @@ -187,7 +187,7 @@ public class L2Customizer extends FutureJVppCustomizer implements ChildWriterCus getFutureJVpp() .swInterfaceSetL2Xconnect(getL2XConnectRequest(swIfIndex, outSwIfIndex, (byte) 1 /* enable */)); final SwInterfaceSetL2XconnectReply reply = - V3poUtils.getReply(swInterfaceSetL2XconnectReplyCompletionStage.toCompletableFuture()); + TranslateUtils.getReply(swInterfaceSetL2XconnectReplyCompletionStage.toCompletableFuture()); if (reply.retval < 0) { LOG.warn("Failed to update xconnect based interconnection flags for: {}, interconnection: {}", diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/RoutingCustomizer.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/RoutingCustomizer.java index bf355e479..74aee7ae5 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/RoutingCustomizer.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/RoutingCustomizer.java @@ -21,7 +21,7 @@ import io.fd.honeycomb.v3po.translate.spi.write.ChildWriterCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext; import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException; -import io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils; +import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils; import io.fd.honeycomb.v3po.translate.write.WriteContext; import io.fd.honeycomb.v3po.translate.write.WriteFailedException; import java.util.concurrent.CompletionStage; @@ -100,7 +100,7 @@ public class RoutingCustomizer extends FutureJVppCustomizer implements ChildWrit final CompletionStage swInterfaceSetTableReplyCompletionStage = getFutureJVpp().swInterfaceSetTable(getInterfaceSetTableRequest(swIfc, (byte) 0, /* isIpv6 */ vrfId)); final SwInterfaceSetTableReply reply = - V3poUtils.getReply(swInterfaceSetTableReplyCompletionStage.toCompletableFuture()); + TranslateUtils.getReply(swInterfaceSetTableReplyCompletionStage.toCompletableFuture()); if (reply.retval < 0) { LOG.debug("Failed to set routing for interface: {}, {}, vxlan: {}", name, swIfc, rt); throw new VppApiInvocationException("swInterfaceSetTable", reply.context, reply.retval); diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/SubInterfaceCustomizer.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/SubInterfaceCustomizer.java index fb47b6a9f..17342e347 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/SubInterfaceCustomizer.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/SubInterfaceCustomizer.java @@ -16,14 +16,14 @@ package io.fd.honeycomb.v3po.translate.v3po.interfaces; -import static io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils.booleanToByte; +import static io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils.booleanToByte; import com.google.common.base.Optional; import com.google.common.base.Preconditions; import io.fd.honeycomb.v3po.translate.v3po.util.AbstractInterfaceTypeCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext; import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException; -import io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils; +import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils; import io.fd.honeycomb.v3po.translate.write.WriteContext; import io.fd.honeycomb.v3po.translate.write.WriteFailedException; import java.util.concurrent.CompletionStage; @@ -90,7 +90,7 @@ public class SubInterfaceCustomizer extends AbstractInterfaceTypeCustomizer { final CompletionStage tapConnectFuture = getFutureJVpp().tapConnect(getTapConnectRequest(tap.getTapName(), tap.getMac(), tap.getDeviceInstance())); final TapConnectReply reply = - V3poUtils.getReply(tapConnectFuture.toCompletableFuture()); + TranslateUtils.getReply(tapConnectFuture.toCompletableFuture()); if (reply.retval < 0) { LOG.warn("Failed to set tap interface: {}, tap: {}", swIfName, tap); throw new VppApiInvocationException("tap_connect", reply.context, reply.retval); @@ -139,7 +139,7 @@ public class TapCustomizer extends AbstractInterfaceTypeCustomizer { final CompletionStage vxlanAddDelTunnelReplyCompletionStage = getFutureJVpp().tapModify(getTapModifyRequest(tap.getTapName(), index, tap.getMac(), tap.getDeviceInstance())); final TapModifyReply reply = - V3poUtils.getReply(vxlanAddDelTunnelReplyCompletionStage.toCompletableFuture()); + TranslateUtils.getReply(vxlanAddDelTunnelReplyCompletionStage.toCompletableFuture()); if (reply.retval < 0) { LOG.warn("Failed to modify tap interface: {}, tap: {}", swIfName, tap); throw new VppApiInvocationException("tap_modify", reply.context, reply.retval); @@ -155,7 +155,7 @@ public class TapCustomizer extends AbstractInterfaceTypeCustomizer { final CompletionStage vxlanAddDelTunnelReplyCompletionStage = getFutureJVpp().tapDelete(getTapDeleteRequest(index)); final TapDeleteReply reply = - V3poUtils.getReply(vxlanAddDelTunnelReplyCompletionStage.toCompletableFuture()); + TranslateUtils.getReply(vxlanAddDelTunnelReplyCompletionStage.toCompletableFuture()); if (reply.retval < 0) { LOG.warn("Failed to delete tap interface: {}, tap: {}", swIfName, dataBefore); throw new VppApiInvocationException("tap_modify", reply.context, reply.retval); @@ -175,7 +175,7 @@ public class TapCustomizer extends AbstractInterfaceTypeCustomizer { tapConnect.macAddress = new byte[6]; } else { tapConnect.useRandomMac = 0; - tapConnect.macAddress = V3poUtils.parseMac(mac.getValue()); + tapConnect.macAddress = TranslateUtils.parseMac(mac.getValue()); } if(deviceInstance == null) { @@ -198,7 +198,7 @@ public class TapCustomizer extends AbstractInterfaceTypeCustomizer { tapConnect.macAddress = new byte[6]; } else { tapConnect.useRandomMac = 0; - tapConnect.macAddress = V3poUtils.parseMac(mac.getValue()); + tapConnect.macAddress = TranslateUtils.parseMac(mac.getValue()); } if(deviceInstance == null) { diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/VhostUserCustomizer.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/VhostUserCustomizer.java index aef45c821..6c45e98ac 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/VhostUserCustomizer.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/VhostUserCustomizer.java @@ -21,7 +21,7 @@ import com.google.common.base.Preconditions; import io.fd.honeycomb.v3po.translate.v3po.util.AbstractInterfaceTypeCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext; import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException; -import io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils; +import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils; import io.fd.honeycomb.v3po.translate.write.WriteContext; import io.fd.honeycomb.v3po.translate.write.WriteFailedException; import java.util.concurrent.CompletionStage; @@ -85,7 +85,7 @@ public class VhostUserCustomizer extends AbstractInterfaceTypeCustomizer replyCompletionStage = getFutureJVpp().l2InterfaceVlanTagRewrite(getTagRewriteRequest(swIfIndex, cfg)); - final L2InterfaceVlanTagRewriteReply reply = V3poUtils.getReply(replyCompletionStage.toCompletableFuture()); + final L2InterfaceVlanTagRewriteReply reply = TranslateUtils.getReply(replyCompletionStage.toCompletableFuture()); if (reply.retval < 0) { LOG.debug("Failed to set tag rewrite for interface {}(id=): {}", ifname, swIfIndex, cfg); throw new VppApiInvocationException("l2InterfaceVlanTagRewrite", reply.context, reply.retval); diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/VxlanCustomizer.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/VxlanCustomizer.java index bec0e8366..5b2be71cc 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/VxlanCustomizer.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/VxlanCustomizer.java @@ -23,7 +23,7 @@ import com.google.common.net.InetAddresses; import io.fd.honeycomb.v3po.translate.v3po.util.AbstractInterfaceTypeCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext; import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException; -import io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils; +import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils; import io.fd.honeycomb.v3po.translate.write.WriteContext; import io.fd.honeycomb.v3po.translate.write.WriteFailedException; import java.net.InetAddress; @@ -112,7 +112,7 @@ public class VxlanCustomizer extends AbstractInterfaceTypeCustomizer { dstAddress.getAddress(), encapVrfId, -1, vni, isIpv6)); final VxlanAddDelTunnelReply reply = - V3poUtils.getReply(vxlanAddDelTunnelReplyCompletionStage.toCompletableFuture()); + TranslateUtils.getReply(vxlanAddDelTunnelReplyCompletionStage.toCompletableFuture()); if (reply.retval < 0) { LOG.debug("Failed to set vxlan tunnel for interface: {}, vxlan: {}", swIfName, vxlan); throw new VppApiInvocationException("vxlanAddDelTunnel", reply.context, reply.retval); @@ -166,7 +166,7 @@ public class VxlanCustomizer extends AbstractInterfaceTypeCustomizer { dstAddress.getAddress(), encapVrfId, -1, vni, isIpv6)); final VxlanAddDelTunnelReply reply = - V3poUtils.getReply(vxlanAddDelTunnelReplyCompletionStage.toCompletableFuture()); + TranslateUtils.getReply(vxlanAddDelTunnelReplyCompletionStage.toCompletableFuture()); if (reply.retval < 0) { LOG.debug("Failed to delete vxlan tunnel for interface: {}, vxlan: {}", swIfName, vxlan); throw new VppApiInvocationException("vxlanAddDelTunnel", reply.context, reply.retval); diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/ip/Ipv4Customizer.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/ip/Ipv4Customizer.java index 4deb38f52..fb891f838 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/ip/Ipv4Customizer.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/ip/Ipv4Customizer.java @@ -24,7 +24,7 @@ import io.fd.honeycomb.v3po.translate.spi.write.ChildWriterCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext; import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException; -import io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils; +import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils; import io.fd.honeycomb.v3po.translate.write.WriteContext; import io.fd.honeycomb.v3po.translate.write.WriteFailedException; import java.util.concurrent.CompletionStage; @@ -131,7 +131,7 @@ public class Ipv4Customizer extends FutureJVppCustomizer implements ChildWriterC LOG.debug("Setting Subnet(prefix-length) for interface: {}, {}. Subnet: {}, Ipv4: {}", name, swIfc, subnet, ipv4Addr); - byte[] addr = V3poUtils.ipv4AddressNoZoneToArray(ipv4Addr.getIp()); + byte[] addr = TranslateUtils.ipv4AddressNoZoneToArray(ipv4Addr.getIp()); checkArgument(plen > 0, "Invalid length"); checkNotNull(addr, "Null address"); @@ -141,7 +141,7 @@ public class Ipv4Customizer extends FutureJVppCustomizer implements ChildWriterC swIfc, (byte) 1 /* isAdd */, (byte) 0 /* isIpv6 */, (byte) 0 /* delAll */, plen.byteValue(), addr)); final SwInterfaceAddDelAddressReply reply = - V3poUtils.getReply(swInterfaceAddDelAddressReplyCompletionStage.toCompletableFuture()); + TranslateUtils.getReply(swInterfaceAddDelAddressReplyCompletionStage.toCompletableFuture()); if (reply.retval < 0) { LOG.warn("Failed to set Subnet(prefix-length) for interface: {}, {}, Subnet: {}, Ipv4: {}", name, swIfc, diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceCustomizer.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceCustomizer.java index 87c62f48f..4027941eb 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceCustomizer.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceCustomizer.java @@ -22,7 +22,7 @@ import io.fd.honeycomb.v3po.translate.read.ReadFailedException; import io.fd.honeycomb.v3po.translate.spi.read.ListReaderCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext; -import io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils; +import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -113,7 +113,7 @@ public class InterfaceCustomizer extends FutureJVppCustomizer final CompletableFuture swInterfaceDetailsReplyDumpCompletableFuture = getFutureJVpp().swInterfaceDump(request).toCompletableFuture(); - final SwInterfaceDetailsReplyDump ifaces = V3poUtils.getReply(swInterfaceDetailsReplyDumpCompletableFuture); + final SwInterfaceDetailsReplyDump ifaces = TranslateUtils.getReply(swInterfaceDetailsReplyDumpCompletableFuture); if (null == ifaces || null == ifaces.swInterfaceDetails) { LOG.debug("No interfaces found in VPP"); @@ -129,7 +129,7 @@ public class InterfaceCustomizer extends FutureJVppCustomizer .map((elt) -> { // Store interface name from VPP in context if not yet present if (!interfaceContext.containsName(elt.swIfIndex, context.getMappingContext())) { - interfaceContext.addName(elt.swIfIndex, V3poUtils.toString(elt.interfaceName), context.getMappingContext()); + interfaceContext.addName(elt.swIfIndex, TranslateUtils.toString(elt.interfaceName), context.getMappingContext()); } LOG.trace("Interface with name: {}, VPP name: {} and index: {} found in VPP", interfaceContext.getName(elt.swIfIndex, context.getMappingContext()), elt.interfaceName, elt.swIfIndex); diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceUtils.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceUtils.java index bbcb4fdb9..be9b34730 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceUtils.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceUtils.java @@ -22,7 +22,7 @@ import static io.fd.honeycomb.v3po.translate.v3po.interfacesstate.InterfaceCusto import com.google.common.base.Preconditions; import com.google.common.collect.Iterables; import io.fd.honeycomb.v3po.translate.ModificationCache; -import io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils; +import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils; import java.math.BigInteger; import java.util.Map; import java.util.Objects; @@ -171,7 +171,7 @@ public final class InterfaceUtils { } CompletionStage requestFuture = futureJvpp.swInterfaceDump(request); - SwInterfaceDetailsReplyDump ifaces = V3poUtils.getReply(requestFuture.toCompletableFuture()); + SwInterfaceDetailsReplyDump ifaces = TranslateUtils.getReply(requestFuture.toCompletableFuture()); if (null == ifaces || null == ifaces.swInterfaceDetails || ifaces.swInterfaceDetails.isEmpty()) { request.nameFilterValid = 0; @@ -180,7 +180,7 @@ public final class InterfaceUtils { // Or else just perform full dump and do inefficient filtering requestFuture = futureJvpp.swInterfaceDump(request); - ifaces = V3poUtils.getReply(requestFuture.toCompletableFuture()); + ifaces = TranslateUtils.getReply(requestFuture.toCompletableFuture()); // Update the cache allInterfaces.clear(); @@ -235,6 +235,6 @@ public final class InterfaceUtils { static boolean isInterfaceOfType(final Class ifcType, final SwInterfaceDetails cachedDetails) { - return ifcType.equals(getInterfaceType(V3poUtils.toString(cachedDetails.interfaceName))); + return ifcType.equals(getInterfaceType(TranslateUtils.toString(cachedDetails.interfaceName))); } } diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/L2Customizer.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/L2Customizer.java index f0d260de2..1fedd4a55 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/L2Customizer.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/L2Customizer.java @@ -24,7 +24,7 @@ import io.fd.honeycomb.v3po.translate.read.ReadFailedException; import io.fd.honeycomb.v3po.translate.spi.read.ChildReaderCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext; -import io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils; +import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils; import java.util.Optional; import java.util.concurrent.CompletableFuture; import javax.annotation.Nonnull; @@ -136,6 +136,6 @@ public class L2Customizer extends FutureJVppCustomizer final CompletableFuture bdCompletableFuture = getFutureJVpp().bridgeDomainSwIfDump(request).toCompletableFuture(); - return V3poUtils.getReply(bdCompletableFuture); + return TranslateUtils.getReply(bdCompletableFuture); } } diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/TapCustomizer.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/TapCustomizer.java index 358e9149a..d4fe30b57 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/TapCustomizer.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/TapCustomizer.java @@ -23,7 +23,7 @@ import io.fd.honeycomb.v3po.translate.read.ReadFailedException; import io.fd.honeycomb.v3po.translate.spi.read.ChildReaderCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext; -import io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils; +import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils; import java.util.Collections; import java.util.List; import java.util.Map; @@ -93,7 +93,7 @@ public class TapCustomizer extends FutureJVppCustomizer final CompletionStage swInterfaceTapDetailsReplyDumpCompletionStage = getFutureJVpp().swInterfaceTapDump(request); final SwInterfaceTapDetailsReplyDump reply = - V3poUtils.getReply(swInterfaceTapDetailsReplyDumpCompletionStage.toCompletableFuture()); + TranslateUtils.getReply(swInterfaceTapDetailsReplyDumpCompletionStage.toCompletableFuture()); if(null == reply || null == reply.swInterfaceTapDetails) { mappedTaps = Collections.emptyMap(); @@ -110,7 +110,7 @@ public class TapCustomizer extends FutureJVppCustomizer final SwInterfaceTapDetails swInterfaceTapDetails = mappedTaps.get(index); LOG.trace("Tap interface: {} attributes returned from VPP: {}", key.getName(), swInterfaceTapDetails); - builder.setTapName(V3poUtils.toString(swInterfaceTapDetails.devName)); + builder.setTapName(TranslateUtils.toString(swInterfaceTapDetails.devName)); LOG.debug("Tap interface: {}, id: {} attributes read as: {}", key.getName(), index, builder); } } diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/VhostUserCustomizer.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/VhostUserCustomizer.java index d5ae8faf3..de7514524 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/VhostUserCustomizer.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/VhostUserCustomizer.java @@ -23,7 +23,7 @@ import io.fd.honeycomb.v3po.translate.read.ReadFailedException; import io.fd.honeycomb.v3po.translate.spi.read.ChildReaderCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext; -import io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils; +import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils; import java.math.BigInteger; import java.util.Collections; import java.util.List; @@ -95,7 +95,7 @@ public class VhostUserCustomizer extends FutureJVppCustomizer final CompletionStage swInterfaceVhostUserDetailsReplyDumpCompletionStage = getFutureJVpp().swInterfaceVhostUserDump(request); final SwInterfaceVhostUserDetailsReplyDump reply = - V3poUtils.getReply(swInterfaceVhostUserDetailsReplyDumpCompletionStage.toCompletableFuture()); + TranslateUtils.getReply(swInterfaceVhostUserDetailsReplyDumpCompletionStage.toCompletableFuture()); if(null == reply || null == reply.swInterfaceVhostUserDetails) { mappedVhostUsers = Collections.emptyMap(); @@ -116,7 +116,7 @@ public class VhostUserCustomizer extends FutureJVppCustomizer builder.setRole(swInterfaceVhostUserDetails.isServer == 1 ? VhostUserRole.Server : VhostUserRole.Client); builder.setFeatures(BigInteger.valueOf(swInterfaceVhostUserDetails.features)); builder.setNumMemoryRegions((long) swInterfaceVhostUserDetails.numRegions); - builder.setSocket(V3poUtils.toString(swInterfaceVhostUserDetails.sockFilename)); + builder.setSocket(TranslateUtils.toString(swInterfaceVhostUserDetails.sockFilename)); builder.setVirtioNetHdrSize((long) swInterfaceVhostUserDetails.virtioNetHdrSz); builder.setConnectError(Integer.toString(swInterfaceVhostUserDetails.sockErrno)); diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/VxlanCustomizer.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/VxlanCustomizer.java index 81ba34062..9f385861a 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/VxlanCustomizer.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/VxlanCustomizer.java @@ -23,7 +23,7 @@ import io.fd.honeycomb.v3po.translate.read.ReadFailedException; import io.fd.honeycomb.v3po.translate.spi.read.ChildReaderCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext; -import io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils; +import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Arrays; @@ -91,7 +91,7 @@ public class VxlanCustomizer extends FutureJVppCustomizer final CompletionStage swInterfaceVxlanDetailsReplyDumpCompletionStage = getFutureJVpp().vxlanTunnelDump(request); final VxlanTunnelDetailsReplyDump reply = - V3poUtils.getReply(swInterfaceVxlanDetailsReplyDumpCompletionStage.toCompletableFuture()); + TranslateUtils.getReply(swInterfaceVxlanDetailsReplyDumpCompletionStage.toCompletableFuture()); // VPP keeps vxlan tunnel interfaces even after they were deleted (optimization) // However there ar no longer any vxlan tunnel specific fields assigned to it and this call diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/utils/V3poUtils.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/utils/V3poUtils.java deleted file mode 100644 index 8742d1d60..000000000 --- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/utils/V3poUtils.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright (c) 2016 Cisco and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.fd.honeycomb.v3po.translate.v3po.utils; - -import static com.google.common.base.Preconditions.checkArgument; - -import com.google.common.base.Splitter; -import java.util.List; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Future; -import java.util.function.BiConsumer; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone; -import org.openvpp.jvpp.dto.JVppReply; - -public final class V3poUtils { - - // TODO move to vpp-translate-utils - - public static final Splitter COLON_SPLITTER = Splitter.on(':'); - - private V3poUtils() {} - - public static > REP getReply(Future future) { - try { - return future.get(); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - throw new IllegalStateException("Interrupted", e); - } catch (ExecutionException e) { - // Execution exception should not occur, since we are using return codes for errors - // TODO fix when using exceptions instead of return codes - throw new IllegalArgumentException("Future " + " should not fail with an exception", e); - } - } - - public static byte[] ipv4AddressNoZoneToArray(final Ipv4AddressNoZone ipv4Addr) { - byte[] retval = new byte[4]; - String[] dots = ipv4Addr.getValue().split("\\."); - - for (int d = 3; d >= 0; d--) { - retval[d] = (byte) (Short.parseShort(dots[3 - d]) & 0xff); - } - return retval; - } - - /** - * Return (interned) string from byte array while removing \u0000. - * Strings represented as fixed length byte[] from vpp contain \u0000. - */ - public static String toString(final byte[] cString) { - return new String(cString).replaceAll("\\u0000", "").intern(); - } - - /** - * Parse string represented mac address (using ":" as separator) into a byte array - */ - @Nonnull - public static byte[] parseMac(@Nonnull final String macAddress) { - final List parts = COLON_SPLITTER.splitToList(macAddress); - checkArgument(parts.size() == 6, "Mac address is expected to have 6 parts but was: %s", macAddress); - return parseMacLikeString(parts); - } - - private static byte[] parseMacLikeString(final List strings) { - return strings.stream().limit(6).map(V3poUtils::parseHexByte).collect( - () -> new byte[strings.size()], - new BiConsumer() { - - private int i = -1; - - @Override - public void accept(final byte[] bytes, final Byte aByte) { - bytes[++i] = aByte; - } - }, - (bytes, bytes2) -> { - throw new UnsupportedOperationException("Parallel collect not supported"); - }); - } - - private static byte parseHexByte(final String aByte) { - return (byte)Integer.parseInt(aByte, 16); - } - - /** - * Returns 0 if argument is null or false, 1 otherwise. - * @param value Boolean value to be converted - * @return byte value equal to 0 or 1 - */ - public static byte booleanToByte(@Nullable final Boolean value) { - return value != null && value ? (byte) 1 : (byte) 0; - } -} diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/vpp/BridgeDomainCustomizer.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/vpp/BridgeDomainCustomizer.java index db1e61578..0b3545ddd 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/vpp/BridgeDomainCustomizer.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/vpp/BridgeDomainCustomizer.java @@ -24,7 +24,7 @@ import io.fd.honeycomb.v3po.translate.spi.write.ListWriterCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext; import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException; -import io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils; +import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils; import io.fd.honeycomb.v3po.translate.write.WriteContext; import io.fd.honeycomb.v3po.translate.write.WriteFailedException; import java.util.List; @@ -74,7 +74,7 @@ public class BridgeDomainCustomizer request.isAdd = ADD_OR_UPDATE_BD; final BridgeDomainAddDelReply reply = - V3poUtils.getReply(getFutureJVpp().bridgeDomainAddDel(request).toCompletableFuture()); + TranslateUtils.getReply(getFutureJVpp().bridgeDomainAddDel(request).toCompletableFuture()); if (reply.retval < 0) { LOG.warn("Bridge domain {} (id={}) add/update failed", bd.getName(), bdId); throw new VppApiInvocationException("bridgeDomainAddDel", reply.context, reply.retval); @@ -125,7 +125,7 @@ public class BridgeDomainCustomizer request.bdId = bdId; final BridgeDomainAddDelReply reply = - V3poUtils.getReply(getFutureJVpp().bridgeDomainAddDel(request).toCompletableFuture()); + TranslateUtils.getReply(getFutureJVpp().bridgeDomainAddDel(request).toCompletableFuture()); if (reply.retval < 0) { LOG.warn("Bridge domain {} (id={}) delete failed", bdName, bdId); throw new WriteFailedException.DeleteFailedException(id, diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/vppstate/VersionCustomizer.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/vppstate/VersionCustomizer.java index 4ad4a6e91..ad181a701 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/vppstate/VersionCustomizer.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/vppstate/VersionCustomizer.java @@ -20,7 +20,7 @@ import io.fd.honeycomb.v3po.translate.read.ReadContext; import io.fd.honeycomb.v3po.translate.read.ReadFailedException; import io.fd.honeycomb.v3po.translate.spi.read.ChildReaderCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer; -import io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils; +import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils; import javax.annotation.Nonnull; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppStateBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.Version; @@ -61,10 +61,10 @@ public final class VersionCustomizer } catch (Exception e) { throw new ReadFailedException(id, e); } - builder.setBranch(V3poUtils.toString(reply.version)); - builder.setName(V3poUtils.toString(reply.program)); - builder.setBuildDate(V3poUtils.toString(reply.buildDate)); - builder.setBuildDirectory(V3poUtils.toString(reply.buildDirectory)); + builder.setBranch(TranslateUtils.toString(reply.version)); + builder.setName(TranslateUtils.toString(reply.program)); + builder.setBuildDate(TranslateUtils.toString(reply.buildDate)); + builder.setBuildDirectory(TranslateUtils.toString(reply.buildDirectory)); } } diff --git a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/VhostUserCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/VhostUserCustomizerTest.java index a14718124..a6fb64331 100644 --- a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/VhostUserCustomizerTest.java +++ b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/VhostUserCustomizerTest.java @@ -36,7 +36,7 @@ import io.fd.honeycomb.v3po.translate.MappingContext; import io.fd.honeycomb.v3po.translate.ModificationCache; import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext; import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException; -import io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils; +import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils; import io.fd.honeycomb.v3po.translate.write.WriteContext; import io.fd.honeycomb.v3po.translate.write.WriteFailedException; import java.util.concurrent.CompletableFuture; @@ -152,7 +152,7 @@ public class VhostUserCustomizerTest { final CreateVhostUserIf actual = argumentCaptor.getValue(); assertEquals(0, actual.customDevInstance); - assertEquals(V3poUtils.booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole())), actual.isServer); + assertEquals(TranslateUtils.booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole())), actual.isServer); assertEquals(0, actual.renumber); assertEquals(0, actual.useCustomMac); assertArrayEquals(vhostUser.getSocket().getBytes(), actual.sockFilename); @@ -166,7 +166,7 @@ public class VhostUserCustomizerTest { final ModifyVhostUserIf actual = argumentCaptor.getValue(); assertEquals(0, actual.customDevInstance); - assertEquals(V3poUtils.booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole())), actual.isServer); + assertEquals(TranslateUtils.booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole())), actual.isServer); assertEquals(0, actual.renumber); assertEquals(swIfIndex, actual.swIfIndex); assertArrayEquals(vhostUser.getSocket().getBytes(), actual.sockFilename); diff --git a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/utils/V3poUtilsTest.java b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/utils/V3poUtilsTest.java deleted file mode 100644 index fcdca49b0..000000000 --- a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/utils/V3poUtilsTest.java +++ /dev/null @@ -1,61 +0,0 @@ -package io.fd.honeycomb.v3po.translate.v3po.utils; - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - -public class V3poUtilsTest { - - @Test - public void testToString() { - final byte[] expected = "test".getBytes(); - final byte[] cString = new byte[expected.length+10]; - System.arraycopy(expected, 0, cString, 0, expected.length); - final String jString = V3poUtils.toString(cString); - assertArrayEquals(expected, jString.getBytes()); - } - - @Test - public void testParseMac() throws Exception { - byte[] bytes = V3poUtils.parseMac("00:fF:7f:15:5e:A9"); - assertMac(bytes); - } - - private void assertMac(final byte[] bytes) { - assertEquals(6, bytes.length); - assertEquals((byte)0, bytes[0]); - assertEquals((byte)255, bytes[1]); - assertEquals((byte)127, bytes[2]); - assertEquals((byte)21, bytes[3]); - assertEquals((byte)94, bytes[4]); - assertEquals((byte)169, bytes[5]); - } - - @Test(expected = IllegalArgumentException.class) - public void testParseMacLonger() throws Exception { - byte[] bytes = V3poUtils.parseMac("00:fF:7f:15:5e:A9:88:77"); - assertMac(bytes); - } - - @Test(expected = IllegalArgumentException.class) - public void testParseMacShorter() throws Exception { - V3poUtils.parseMac("00:fF:7f"); - } - - @Test(expected = IllegalArgumentException.class) - public void testParseRandomString() throws Exception { - V3poUtils.parseMac("random{}}@$*&*!"); - } - - @Test(expected = NumberFormatException.class) - public void testParseMacNumberFormatEx() throws Exception { - V3poUtils.parseMac("00:XX:7f:15:5e:77\""); - } - - public void testBooleanToByte() { - assertEquals(0, V3poUtils.booleanToByte(null)); - assertEquals(0, V3poUtils.booleanToByte(false)); - assertEquals(1, V3poUtils.booleanToByte(true)); - } -} \ No newline at end of file diff --git a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtils.java b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtils.java new file mode 100644 index 000000000..9f6f1f676 --- /dev/null +++ b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtils.java @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.v3po.translate.v3po.util; + +import static com.google.common.base.Preconditions.checkArgument; + +import com.google.common.base.Splitter; +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.function.BiConsumer; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone; +import org.openvpp.jvpp.dto.JVppReply; + +public final class TranslateUtils { + + // TODO move to vpp-translate-utils + + public static final Splitter COLON_SPLITTER = Splitter.on(':'); + + private TranslateUtils() {} + + public static > REP getReply(Future future) { + try { + return future.get(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IllegalStateException("Interrupted", e); + } catch (ExecutionException e) { + // Execution exception should not occur, since we are using return codes for errors + // TODO fix when using exceptions instead of return codes + throw new IllegalArgumentException("Future " + " should not fail with an exception", e); + } + } + + public static byte[] ipv4AddressNoZoneToArray(final Ipv4AddressNoZone ipv4Addr) { + byte[] retval = new byte[4]; + String[] dots = ipv4Addr.getValue().split("\\."); + + for (int d = 3; d >= 0; d--) { + retval[d] = (byte) (Short.parseShort(dots[3 - d]) & 0xff); + } + return retval; + } + + /** + * Return (interned) string from byte array while removing \u0000. + * Strings represented as fixed length byte[] from vpp contain \u0000. + */ + public static String toString(final byte[] cString) { + return new String(cString).replaceAll("\\u0000", "").intern(); + } + + /** + * Parse string represented mac address (using ":" as separator) into a byte array + */ + @Nonnull + public static byte[] parseMac(@Nonnull final String macAddress) { + final List parts = COLON_SPLITTER.splitToList(macAddress); + checkArgument(parts.size() == 6, "Mac address is expected to have 6 parts but was: %s", macAddress); + return parseMacLikeString(parts); + } + + private static byte[] parseMacLikeString(final List strings) { + return strings.stream().limit(6).map(TranslateUtils::parseHexByte).collect( + () -> new byte[strings.size()], + new BiConsumer() { + + private int i = -1; + + @Override + public void accept(final byte[] bytes, final Byte aByte) { + bytes[++i] = aByte; + } + }, + (bytes, bytes2) -> { + throw new UnsupportedOperationException("Parallel collect not supported"); + }); + } + + private static byte parseHexByte(final String aByte) { + return (byte)Integer.parseInt(aByte, 16); + } + + /** + * Returns 0 if argument is null or false, 1 otherwise. + * @param value Boolean value to be converted + * @return byte value equal to 0 or 1 + */ + public static byte booleanToByte(@Nullable final Boolean value) { + return value != null && value ? (byte) 1 : (byte) 0; + } +} diff --git a/v3po/vpp-translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtilsTest.java b/v3po/vpp-translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtilsTest.java new file mode 100644 index 000000000..a3c37bade --- /dev/null +++ b/v3po/vpp-translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtilsTest.java @@ -0,0 +1,62 @@ +package io.fd.honeycomb.v3po.translate.v3po.util; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils; +import org.junit.Test; + +public class TranslateUtilsTest { + + @Test + public void testToString() { + final byte[] expected = "test".getBytes(); + final byte[] cString = new byte[expected.length+10]; + System.arraycopy(expected, 0, cString, 0, expected.length); + final String jString = TranslateUtils.toString(cString); + assertArrayEquals(expected, jString.getBytes()); + } + + @Test + public void testParseMac() throws Exception { + byte[] bytes = TranslateUtils.parseMac("00:fF:7f:15:5e:A9"); + assertMac(bytes); + } + + private void assertMac(final byte[] bytes) { + assertEquals(6, bytes.length); + assertEquals((byte)0, bytes[0]); + assertEquals((byte)255, bytes[1]); + assertEquals((byte)127, bytes[2]); + assertEquals((byte)21, bytes[3]); + assertEquals((byte)94, bytes[4]); + assertEquals((byte)169, bytes[5]); + } + + @Test(expected = IllegalArgumentException.class) + public void testParseMacLonger() throws Exception { + byte[] bytes = TranslateUtils.parseMac("00:fF:7f:15:5e:A9:88:77"); + assertMac(bytes); + } + + @Test(expected = IllegalArgumentException.class) + public void testParseMacShorter() throws Exception { + TranslateUtils.parseMac("00:fF:7f"); + } + + @Test(expected = IllegalArgumentException.class) + public void testParseRandomString() throws Exception { + TranslateUtils.parseMac("random{}}@$*&*!"); + } + + @Test(expected = NumberFormatException.class) + public void testParseMacNumberFormatEx() throws Exception { + TranslateUtils.parseMac("00:XX:7f:15:5e:77\""); + } + + public void testBooleanToByte() { + assertEquals(0, TranslateUtils.booleanToByte(null)); + assertEquals(0, TranslateUtils.booleanToByte(false)); + assertEquals(1, TranslateUtils.booleanToByte(true)); + } +} \ No newline at end of file -- cgit 1.2.3-korg