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.translate.v3po.util;
19 import static com.google.common.base.Preconditions.checkArgument;
21 import com.google.common.base.Splitter;
22 import com.google.common.net.InetAddresses;
23 import java.net.UnknownHostException;
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.concurrent.ExecutionException;
27 import java.util.concurrent.Future;
28 import java.util.concurrent.TimeUnit;
29 import java.util.concurrent.TimeoutException;
30 import java.util.function.BiConsumer;
31 import javax.annotation.Nonnegative;
32 import javax.annotation.Nonnull;
33 import javax.annotation.Nullable;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.openvpp.jvpp.VppBaseCallException;
37 import org.openvpp.jvpp.dto.JVppReply;
39 public final class TranslateUtils {
41 public static final Splitter COLON_SPLITTER = Splitter.on(':');
42 public static final int DEFAULT_TIMEOUT_IN_SECONDS = 5;
44 private TranslateUtils() {
47 public static <REP extends JVppReply<?>> REP getReplyForWrite(@Nonnull Future<REP> future,
48 @Nonnull final InstanceIdentifier<?> replyType)
49 throws VppBaseCallException, WriteTimeoutException {
50 return getReplyForWrite(future, replyType, DEFAULT_TIMEOUT_IN_SECONDS);
53 public static <REP extends JVppReply<?>> REP getReplyForWrite(@Nonnull Future<REP> future,
54 @Nonnull final InstanceIdentifier<?> replyType,
55 @Nonnegative final int timeoutInSeconds)
56 throws VppBaseCallException, WriteTimeoutException {
58 return getReply(future, timeoutInSeconds);
59 } catch (TimeoutException e) {
60 throw new WriteTimeoutException(replyType, e);
64 public static <REP extends JVppReply<?>> REP getReplyForRead(@Nonnull Future<REP> future,
65 @Nonnull final InstanceIdentifier<?> replyType)
66 throws VppBaseCallException, ReadTimeoutException {
67 return getReplyForRead(future, replyType, DEFAULT_TIMEOUT_IN_SECONDS);
70 public static <REP extends JVppReply<?>> REP getReplyForRead(@Nonnull Future<REP> future,
71 @Nonnull final InstanceIdentifier<?> replyType,
72 @Nonnegative final int timeoutInSeconds)
73 throws VppBaseCallException, ReadTimeoutException {
75 return getReply(future, timeoutInSeconds);
76 } catch (TimeoutException e) {
77 throw new ReadTimeoutException(replyType, e);
81 public static <REP extends JVppReply<?>> REP getReply(@Nonnull Future<REP> future)
82 throws TimeoutException, VppBaseCallException {
83 return getReply(future, DEFAULT_TIMEOUT_IN_SECONDS);
86 public static <REP extends JVppReply<?>> REP getReply(@Nonnull Future<REP> future,
87 @Nonnegative final int timeoutInSeconds)
88 throws TimeoutException, VppBaseCallException {
90 checkArgument(timeoutInSeconds > 0, "Timeout cannot be < 0");
91 return future.get(timeoutInSeconds, TimeUnit.SECONDS);
92 } catch (InterruptedException e) {
93 Thread.currentThread().interrupt();
94 throw new IllegalStateException("Interrupted", e);
95 } catch (ExecutionException e) {
96 // Execution exception could generally contains any exception
97 // when using exceptions instead of return codes just rethrow it for processing on corresponding place
98 if (e instanceof ExecutionException && (e.getCause() instanceof VppBaseCallException)) {
99 throw (VppBaseCallException) (e.getCause());
101 throw new IllegalStateException(e);
106 * Transform Ipv4 address to a byte array acceptable by VPP. VPP expects incoming byte array to be in the same order
109 * @return byte array with address bytes
111 public static byte[] ipv4AddressNoZoneToArray(final Ipv4AddressNoZone ipv4Addr) {
112 return ipv4AddressNoZoneToArray(ipv4Addr.getValue());
115 public static byte[] ipv4AddressNoZoneToArray(final String ipv4Addr) {
116 byte[] retval = new byte[4];
117 String[] dots = ipv4Addr.split("\\.");
119 for (int d = 0; d < 4; d++) {
120 retval[d] = (byte) (Short.parseShort(dots[d]) & 0xff);
126 * Parse byte array returned by VPP representing an Ipv4 address. Vpp returns IP byte arrays in reversed order.
128 * @return Ipv4AddressNoZone containing string representation of IPv4 address constructed from submitted bytes. No
132 public static Ipv4AddressNoZone arrayToIpv4AddressNoZone(@Nonnull byte[] ip) {
133 // VPP sends ipv4 in a 16 byte array
134 if (ip.length == 16) {
135 ip = Arrays.copyOfRange(ip, 0, 4);
138 // Not reversing the byte array here!! because the IP coming from VPP is in reversed byte order
139 // compared to byte order it was submitted
140 return new Ipv4AddressNoZone(InetAddresses.toAddrString(InetAddresses.fromLittleEndianByteArray(ip)));
141 } catch (UnknownHostException e) {
142 throw new IllegalArgumentException("Unable to parse ipv4", e);
147 * Return (interned) string from byte array while removing \u0000. Strings represented as fixed length byte[] from
148 * vpp contain \u0000.
150 public static String toString(final byte[] cString) {
151 return new String(cString).replaceAll("\\u0000", "").intern();
155 * Parse string represented mac address (using ":" as separator) into a byte array
158 public static byte[] parseMac(@Nonnull final String macAddress) {
159 final List<String> parts = COLON_SPLITTER.splitToList(macAddress);
160 checkArgument(parts.size() == 6, "Mac address is expected to have 6 parts but was: %s", macAddress);
161 return parseMacLikeString(parts);
164 private static byte[] parseMacLikeString(final List<String> strings) {
165 return strings.stream().limit(6).map(TranslateUtils::parseHexByte).collect(
166 () -> new byte[strings.size()],
167 new BiConsumer<byte[], Byte>() {
172 public void accept(final byte[] bytes, final Byte aByte) {
177 throw new UnsupportedOperationException("Parallel collect not supported");
181 public static byte parseHexByte(final String aByte) {
182 return (byte) Integer.parseInt(aByte, 16);
186 * Returns 0 if argument is null or false, 1 otherwise.
188 * @param value Boolean value to be converted
189 * @return byte value equal to 0 or 1
191 public static byte booleanToByte(@Nullable final Boolean value) {
192 return value != null && value
198 * Returns Boolean.TRUE if argument is 0, Boolean.FALSE otherwise.
200 * @param value byte value to be converted
201 * @return Boolean value
202 * @throws IllegalArgumentException if argument is neither 0 nor 1
205 public static Boolean byteToBoolean(final byte value) {
207 return Boolean.FALSE;
208 } else if (value == 1) {
211 throw new IllegalArgumentException(String.format("0 or 1 was expected but was %d", value));
215 * Reverses bytes in the byte array
217 * @param bytes input array
218 * @return reversed array
220 public static byte[] reverseBytes(final byte[] bytes) {
221 final byte[] reversed = new byte[bytes.length];
223 for (byte aByte : bytes) {
224 reversed[bytes.length - i++] = aByte;