Do not delete configuration in initlizers on close
[honeycomb.git] / v3po / vpp-translate-utils / src / main / java / io / fd / honeycomb / v3po / translate / v3po / util / TranslateUtils.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package io.fd.honeycomb.v3po.translate.v3po.util;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20
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;
30
31 public final class TranslateUtils {
32
33     // TODO move to vpp-translate-utils
34
35     public static final Splitter COLON_SPLITTER = Splitter.on(':');
36
37     private TranslateUtils() {}
38
39     public static <REP extends JVppReply<?>> REP getReply(Future<REP> future) {
40         try {
41             return future.get();
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);
49         }
50     }
51
52     public static byte[] ipv4AddressNoZoneToArray(final Ipv4AddressNoZone ipv4Addr) {
53         byte[] retval = new byte[4];
54         String[] dots = ipv4Addr.getValue().split("\\.");
55
56         for (int d = 3; d >= 0; d--) {
57             retval[d] = (byte) (Short.parseShort(dots[3 - d]) & 0xff);
58         }
59         return retval;
60     }
61
62     /**
63      * Return (interned) string from byte array while removing \u0000.
64      * Strings represented as fixed length byte[] from vpp contain \u0000.
65      */
66     public static String toString(final byte[] cString) {
67         return new String(cString).replaceAll("\\u0000", "").intern();
68     }
69
70     /**
71      * Parse string represented mac address (using ":" as separator) into a byte array
72      */
73     @Nonnull
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);
78     }
79
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>() {
84
85                 private int i = -1;
86
87                 @Override
88                 public void accept(final byte[] bytes, final Byte aByte) {
89                     bytes[++i] = aByte;
90                 }
91             },
92             (bytes, bytes2) -> {
93                 throw new UnsupportedOperationException("Parallel collect not supported");
94             });
95     }
96
97     private static byte parseHexByte(final String aByte) {
98         return (byte)Integer.parseInt(aByte, 16);
99     }
100
101    /**
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
105      */
106     public static byte booleanToByte(@Nullable final Boolean value) {
107         return value != null && value ? (byte) 1 : (byte) 0;
108     }
109 }