From f8273e10b19ecc82bdda81feef9982ce28d5de21 Mon Sep 17 00:00:00 2001 From: Jan Srnicek Date: Fri, 16 Dec 2016 13:07:56 +0100 Subject: HONEYCOMB-310: translation layer for acl plugin Not covered by this patch (moved to subsequent commits): - postman collection - distinguish ingress/egress ACLs while reading assigned acls - proper support for acl tag - unit tests improvements - read for acls (not necessarily assigned) - initializers Change-Id: I5a198ce1a6e20d0b1d95b4d2d83d0464fb86580c Signed-off-by: Jan Srnicek Signed-off-by: Marek Gradzki --- .../common/translate/util/ByteDataTranslator.java | 32 +++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (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/ByteDataTranslator.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/ByteDataTranslator.java index 17aff0e2e..d24157bf6 100644 --- a/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/ByteDataTranslator.java +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/ByteDataTranslator.java @@ -16,14 +16,17 @@ package io.fd.hc2vpp.common.translate.util; +import java.util.Objects; import javax.annotation.Nonnull; import javax.annotation.Nullable; /** - * Trait providing logic for working with binary-based data + * Trait providing logic for working with binary/hex-based data */ public interface ByteDataTranslator { + char[] HEX_CHARS = "0123456789abcdef".toCharArray(); + ByteDataTranslator INSTANCE = new ByteDataTranslator() { }; @@ -70,6 +73,8 @@ public interface ByteDataTranslator { } else if (value == BYTE_TRUE) { return Boolean.TRUE; } + + char[] HEX_CHARS = "0123456789abcdef".toCharArray(); throw new IllegalArgumentException(String.format("0 or 1 was expected but was %d", value)); } @@ -90,6 +95,8 @@ public interface ByteDataTranslator { } /** + + char[] HEX_CHARS = "0123456789abcdef".toCharArray(); * Return (interned) string from byte array while removing \u0000. Strings represented as fixed length byte[] from * vpp contain \u0000. */ @@ -105,4 +112,27 @@ public interface ByteDataTranslator { default int toJavaByte(final byte vppByte) { return Byte.toUnsignedInt(vppByte); } + + default String printHexBinary(@Nonnull final byte[] bytes) { + Objects.requireNonNull(bytes, "bytes array should not be null"); + return printHexBinary(bytes, 0, bytes.length); + } + + default String printHexBinary(@Nonnull final byte[] bytes, final int startIndex, final int endIndex) { + StringBuilder str = new StringBuilder(); + + appendHexByte(str, bytes[startIndex]); + for (int i = startIndex + 1; i < endIndex; i++) { + str.append(":"); + appendHexByte(str, bytes[i]); + } + + return str.toString(); + } + + default void appendHexByte(final StringBuilder sb, final byte b) { + final int v = b & 0xFF; + sb.append(HEX_CHARS[v >>> 4]); + sb.append(HEX_CHARS[v & 15]); + } } -- cgit 1.2.3-korg