2 * Copyright (c) 2016 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package io.fd.honeycomb.v3po.translate.v3po.util;
19 import static com.google.common.base.Preconditions.checkArgument;
21 import com.google.common.base.Splitter;
22 import java.util.List;
23 import java.util.concurrent.ExecutionException;
24 import java.util.concurrent.Future;
25 import java.util.function.BiConsumer;
26 import javax.annotation.Nonnull;
27 import javax.annotation.Nullable;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
29 import org.openvpp.jvpp.dto.JVppReply;
31 public final class TranslateUtils {
33 // TODO move to vpp-translate-utils
35 public static final Splitter COLON_SPLITTER = Splitter.on(':');
37 private TranslateUtils() {}
39 public static <REP extends JVppReply<?>> REP getReply(Future<REP> future) {
42 } catch (InterruptedException e) {
43 Thread.currentThread().interrupt();
44 throw new IllegalStateException("Interrupted", e);
45 } catch (ExecutionException e) {
46 // Execution exception should not occur, since we are using return codes for errors
47 // TODO fix when using exceptions instead of return codes
48 throw new IllegalArgumentException("Future " + " should not fail with an exception", e);
52 public static byte[] ipv4AddressNoZoneToArray(final Ipv4AddressNoZone ipv4Addr) {
53 byte[] retval = new byte[4];
54 String[] dots = ipv4Addr.getValue().split("\\.");
56 for (int d = 3; d >= 0; d--) {
57 retval[d] = (byte) (Short.parseShort(dots[3 - d]) & 0xff);
63 * Return (interned) string from byte array while removing \u0000.
64 * Strings represented as fixed length byte[] from vpp contain \u0000.
66 public static String toString(final byte[] cString) {
67 return new String(cString).replaceAll("\\u0000", "").intern();
71 * Parse string represented mac address (using ":" as separator) into a byte array
74 public static byte[] parseMac(@Nonnull final String macAddress) {
75 final List<String> parts = COLON_SPLITTER.splitToList(macAddress);
76 checkArgument(parts.size() == 6, "Mac address is expected to have 6 parts but was: %s", macAddress);
77 return parseMacLikeString(parts);
80 private static byte[] parseMacLikeString(final List<String> strings) {
81 return strings.stream().limit(6).map(TranslateUtils::parseHexByte).collect(
82 () -> new byte[strings.size()],
83 new BiConsumer<byte[], Byte>() {
88 public void accept(final byte[] bytes, final Byte aByte) {
93 throw new UnsupportedOperationException("Parallel collect not supported");
97 private static byte parseHexByte(final String aByte) {
98 return (byte)Integer.parseInt(aByte, 16);
102 * Returns 0 if argument is null or false, 1 otherwise.
103 * @param value Boolean value to be converted
104 * @return byte value equal to 0 or 1
106 public static byte booleanToByte(@Nullable final Boolean value) {
107 return value != null && value ? (byte) 1 : (byte) 0;